Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

NOTE: CentOS Enterprise Linux is built from the Red Hat Enterprise Linux source code. Other than logo and name changes CentOS Enterprise Linux is compatible with the equivalent Red Hat version. This document applies equally to both Red Hat and CentOS Enterprise Linux.

3.4. Debug the Executable

To start GDB, use the following commands:

gdb hello

After the initial copyright and configuration information, GDB returns its own prompt, (gdb). The following is a sample debugging session:

  1. To set a breakpoint, type:

    break main

    The following output displays:

    Breakpoint 1 at 0x132: file hello.c, line 15.

    Note the exact address and line number may vary, depending upon the target architecture being debugged and the exact layout of the C code in the hello.c file.

  2. To run the program, type:

    run

    The following output displays (when the program stops at a breakpoint):

        Starting program: hello
        Breakpoint 1, main () at hello.c:15
        15 a = 3;

    Again note that the exact line number and instruction displayed is dependent upon the source code layout.

  3. To print the value of variable, a, type:

    print a

    The following output displays:

    $1 = 0
  4. To execute the next command, type next:

    next

    The following output displays:

        16 b = 4;
  5. To display the value of a again, type:

    print a

    The following output displays:

        $2 = 3
  6. To display the program being debugged, type:

    list

    The following output displays:

    12         int
    13         main (void)
    14         {
    15           int b;
    16
    17           a = 3;
    18           b = 4;
    19
    20           printf ("Hello, world!\n");
    21
    22           foo (b);
    23
    24           return 0;
    25         }
  7. To list a specific function code, use the list command with the name of the function to be display. For example, type:

    list foo

    The following output displays:

     1          #include <stdio.h>
     2
     3          int a, c;
     4
     5          static void
     6          foo (int b)
     7          {
     8            c = a + b;
     9            printf ("%d + %d = %d\n", a, b, c);
    10          }
  8. To set a breakpoint at line seven, enter the following input (set a breakpoint at any line by entering break linenumber, where linenumber is the specific line number to break):

    break 8

    The following output displays:

    Breakpoint 2 at 0xf4: file hello.c, line 8.
  9. To resume normal execution of the program until the next breakpoint, type:

    continue

    The following output displays:

    Continuing.
    Hello, world!
    Breakpoint 2, foo (b=4) at hello.c:8
    8 c = a + b;
  10. To step to the next instruction and execute it, type:

    step

    The following output displays:

    9 printf ("%d + %d = %d\n", a, b, c);
  11. To display the value of c, type:

    print c

    The following output displays:

    $3 = 7
  12. To see how you got to where you are, type:

    backtrace

    The following output displays:

    #0 foo (b=4) at hello.c:9
    #1 0x15c in main () at hello.c:18
  13. To exit the program and quit the debugger, type:

    quit

 
 
  Published under the terms of the GNU General Public License Design by Interspire