When a C program crashes...

  • One of the most common errors in C is (because the absent of run time array bound check:

      • Array index out of bound error...

  • Unlike Java, when a C program executes a run time error, it dies silently --- without reporting any error

  • Example:  compile and run this program

       #include <stdio.h>
    
       int main(int argc, char *argv[])                 
       {
          int a[10];
          int i = -872625577;  // Illegal array index !!
    
          a[i] = 1234;  // Program will crash here..
       }

DEMO: demo/C/gdb/crash1.c

How to find the "crash site" of a C program

  • C programmers use a debugging tool to find the location in the program where a run time error has occured

  • A commonly used debugger is:

       gdb   (GNU Debugger) 

  • In this brief introduction to gdb, I will show you

      1. How to find the crash location

      2. Learn some useful commands of gdb to find the cause of the run time error

  • You can find more detailed tutorials of gdb online --- e.g.:

Compile a C program to use the gdb debugger

 

  • In order to use the gdb debugger with a C program, the C compiler must insert some codes into a C program to communicate with the gdb program

  • The C compiler option that inserts communication code with gdb is:

      -g
      

  • How to compile a C program for use with gdb:

      gcc -g -o output  C-prog.c 

DEMO: demo/C/gdb/crash1.c

Command to run gdb with a (crashing) C program

  • Command to debug a C program c-prog using gdb:

      gdb  c-prog 

    You will then see som announcement messages and the gdb prompt (gdb)

  • Whenever you see the gdb prompt (gdb), you can enter a gdb command

  • You can press

      control-C  

    at any time to return to the gdb prompt

    This is especially helpful when your C program is executing an infinite loop....

Commonly used commands of gdb  running program, setup breakpoint and variable watch

 Breakpoint = a location in the program that when reached will return control to gdb 

 Command           Effect
 ===================================
 run               Start running the C program from the beginning

		   The program may stop for these reasons:
		      (1) It finished normally
		      (2) It executed a statement that is illegal and crashes
		      (3) The program reached a breakpoint
		      (4) A watch variable was updated by the last statement

 cont              Continue running the C program from the stopped location 

 break funcName    Setup a breakpoint at the start of function funcName

 break line#	   Setup a breakpoint at line "line#"

 watch var         Setup a variable watch

                   A running program will stop when a watch variable is updated

 info break        List all the breakpoints

 delete number     Delete breakpoint #number

DEMO: demo/C/gdb/gdb-demo.c (put a break point at main and at line 12)

Commonly used commands of gdb   examining variables and statements, step-by-step program execution

 Command           Effect
 ===================================
 print var         Print the  current value of variable "var"

 display var       Print the  value of variable "var" when program stops

 list              Print the program listing around the current statement
                   where the program has stopped

 where             Print the program location where the program has stopped
                   and the function call stack


next Execute the next statement and stop If the next statement is a function call, the function call will be executed and stop. I.e.: the program execution will remain the same scope step Execute the next statement and stop If the next statement is a function call, gdb will stop at the first statement in the new function finish Finish executing the current function and stop

Most commonly usage of gdb: find the crash location

  • If your C program crashes with a cryptic message (like "Memory fault (core dump)), do the following to locate the crash location:

      1. Compile C program with the -g option (if you have not done so):

          gcc -g  -o  myProg   myProg.c
        

      2. Run your C program using gdb:

          gdb myProg

      3. Starting running your program under gdb control:

          (gdb) run 

        When the program crashes, gdb will print out the crash location