110 likes | 293 Views
Introduction to GDB. Chen, Lu-Hung , Department of Mathematics, National Taiwan University 2009/02/21. test.c. #include < stdio.h > #include < stdlib.h > int main( int argc , char * argv []) { int n = 10, i ; double * ptr = (double*) malloc (n* sizeof (double)); double mean = 0.0;
E N D
Introduction to GDB Chen, Lu-Hung, Department of Mathematics, National Taiwan University2009/02/21
test.c #include <stdio.h> #include <stdlib.h> int main(intargc, char *argv[]) { int n = 10, i; double *ptr = (double*)malloc(n*sizeof(double)); double mean = 0.0; for (i=0;i<n;i++) ptr[i] = (double)i; n = 15; for (i=0;i<n;i++) mean += ptr[i]/(double)n; printf(“mean = %f\n",mean); ptr = 0; ptr[5] = 11.0; for (i=0;i<n;i++) mean += ptr[i]/(double)n; printf(“mean = %f\n",mean); free(ptr); return 0; } Example
What’s wrong??? • Should be:> gcctest.c –o mytest> ./mytestmean = 4.500000mean = 5.100000 • But...> gcctest.c –o mytest> ./mytestmean = 4.500000mean = 5.100000 Main Title
Invoke gdb:> gcc–gtest.c –o mytest> gdb ./mytest(gdb) • Running programs under gdb: • runstart your program under gdb. • run argumentsSpecify the arguments to give your program as the arguments of the run command. • killKill the process of your program running under gdb. • quitQuit gdb. Main Title
Print source lines: • list linenumPrint lines centered around line number linenum in the current source file. • list functionPrint lines centered around the beginning of function function. • list –Print lines just before the lines last printed. Main Title
Ex: print lines around 10th line (gdb)line 105{ 6 int n = 10, i; 7 double *ptr = (double*)malloc(n*sizeof(double)); 8 double mean = 0.0; 9 for (i=0;i<n;i++) 10 ptr[i] = (double)i; 11 n = 15; 12 for (i=0;i<n;i++) 13 mean += ptr[i]/(double)n; 14 printf("mean = %f\n",mean); Main Title
Breakpoints: • break linenumSet a breakpoint at line linenum. • break … if condSet a breakpoint with condition cond; evaluate the expression cond each time the breakpoint is reached, and stop only if cond evaluates as true. • info breakPrint a table of all breakpoints. • delete numberDelete the breakpoint number. If number is not specified, delete all breakpoints. • continueResume execution of your program from a breakpoint. Main Title
Examining data: • print var • printfexpression • display varDisplay var whenever your program stops. • display expression • info displayPrint the list of expressions currently set up to display automatically. • undisplaynumberRemove item numbers number from the list of expressions to display. Introduction to MATLAB
Ex: check if the elements of ptr is properly assigned (gdb)break 11(gdb)run(gdb)print n$1 = 10(gdb)print *ptr@n$2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}(gdb)delete list ptr[0] to ptr[n] Main Title
Ex: what happened when accumulating mean? (gdb)break 13 (gdb)run(gdb)display i1: i = 0(gdb)display mean2: mean = 0(gdb)continue2: mean = 01: i = 1(gdb)continue2: mean = 0.066666666666666666 1: i = 2 ...............(gdb)continue2: mean = 31: i = 10 Oops! Should be 0.1! Array out of bound! Introduction to MATLAB
Why segmentation fault? GDB core dump debugging: • gcoreCreate a core file of the running image. • btPrint a back-trace of the call stack. • Ex: (gdb)run(gdb)gcoreSaved corefile core.12103(gdb)bt#0 0x000000000040063e in main (argc=1, argv=0x7fff6848c068) at test.c:16 Some problem at line 16! Introduction to MATLAB