mumble

A Lisp written in C, following the *Build Your Own Lisp* book
Log | Files | Refs | README

gbd-tutorial.txt (4392B)


      1 From: <https://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb/>
      2 
      3 In this article, let us discuss how to debug a c program using gdb debugger in 6 simple steps.  
      4 
      5 ### Write a sample C program with errors for debugging purpose
      6 
      7 To learn C program debugging, let us create the following C program that calculates and prints the factorial of a number. However this C program contains some errors in it for our debugging purpose.
      8 
      9 $ vim factorial.c
     10 # include <stdio.h>
     11 
     12 int main()
     13 {
     14 	int i, num, j;
     15 	printf ("Enter the number: ");
     16 	scanf ("%d", &num );
     17 
     18 	for (i=1; i<num; i++)
     19 		j=j\*i;    
     20 
     21 	printf("The factorial of %d is %d\\n",num,j);
     22 }
     23 
     24 $ cc factorial.c
     25 
     26 $ ./a.out
     27 Enter the number: 3
     28 The factorial of 3 is 12548672
     29 
     30 Let us debug it while reviewing the most useful commands in gdb.
     31 
     32 ### Step 1. Compile the C program with debugging option -g
     33 
     34 Compile your C program with -g option. This allows the compiler to collect the debugging information.
     35 
     36 $ cc -g factorial.c
     37 
     38 Note: The above command creates a.out file which will be used for debugging as shown below.
     39 
     40 ### Step 2. Launch gdb
     41 
     42 Launch the C debugger (gdb) as shown below.
     43 
     44 $ gdb a.out
     45 
     46 ### Step 3. Set up a break point inside C program
     47 
     48 Syntax:
     49 
     50 break line\_number
     51 
     52 Other formats:
     53 
     54 *   break \[file\_name\]:line\_number
     55 *   break \[file\_name\]:func\_name
     56 
     57 Places break point in the C program, where you suspect errors. While executing the program, the debugger will stop at the break point, and gives you the prompt to debug.
     58 
     59 So before starting up the program, let us place the following break point in our program.
     60 
     61 break 10
     62 Breakpoint 1 at 0x804846f: file factorial.c, line 10.
     63 
     64 ### Step 4. Execute the C program in gdb debugger
     65 
     66 run \[args\]
     67 
     68 You can start running the program using the run command in the gdb debugger. You can also give command line arguments to the program via run args. The example program we used here does not requires any command line arguments so let us give run, and start the program execution.
     69 
     70 run
     71 Starting program: /home/sathiyamoorthy/Debugging/c/a.out
     72 
     73 Once you executed the C program, it would execute until the first break point, and give you the prompt for debugging.
     74 
     75 Breakpoint 1, main () at factorial.c:10
     76 10			j=j\*i;
     77 
     78 You can use various gdb commands to debug the C program as explained in the sections below.
     79 
     80 ### Step 5. Printing the variable values inside gdb debugger
     81 
     82 Syntax: print {variable}
     83 
     84 Examples:
     85 print i
     86 print j
     87 print num
     88 
     89 (gdb) p i
     90 $1 = 1
     91 (gdb) p j
     92 $2 = 3042592
     93 (gdb) p num
     94 $3 = 3
     95 (gdb)
     96 
     97 As you see above, in the factorial.c, we have not initialized the variable j. So, it gets garbage value resulting in a big numbers as factorial values.
     98 
     99 Fix this issue by initializing variable j with 1, compile the C program and execute it again.
    100 
    101 Even after this fix there seems to be some problem in the factorial.c program, as it still gives wrong factorial value.
    102 
    103 So, place the break point in 10th line, and continue as explained in the next section.
    104 
    105 ### Step 6. Continue, stepping over and in – gdb commands
    106 
    107 There are three kind of gdb operations you can choose when the program stops at a break point. They are continuing until the next break point, stepping in, or stepping over the next program lines.
    108 
    109 *   c or continue: Debugger will continue executing until the next break point.
    110 *   n or next: Debugger will execute the next line as single instruction.
    111 *   s or step: Same as next, but does not treats function as a single instruction, instead goes into the function and executes it line by line.
    112 
    113 By continuing or stepping through you could have found that the issue is because we have not used the <= in the ‘for loop’ condition checking. So changing that from < to <= will solve the issue.
    114 
    115 ### gdb command shortcuts
    116 
    117 Use following shortcuts for most of the frequent gdb operations.
    118 
    119 *   l – list
    120 *   p – print
    121 *   c – continue
    122 *   s – step
    123 *   ENTER: pressing enter key would execute the previously executed command again.
    124 
    125 ### Miscellaneous gdb commands
    126 
    127 *   **l command:** Use gdb command l or list to print the source code in the debug mode. Use l line-number to view a specific line number (or) l function to view a specific function.
    128 *   **bt: backtrack** – Print backtrace of all stack frames, or innermost COUNT frames.
    129 *   **help** – View help for a particular gdb topic — help TOPICNAME.
    130 *   **quit** – Exit from the gdb debugger.