1 / 11

Introduction to GDB

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;

hume
Download Presentation

Introduction to GDB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to GDB Chen, Lu-Hung, Department of Mathematics, National Taiwan University2009/02/21

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

More Related