gdb调试的基本使用

时间:2025-02-18 14:09:32
(gdb) list 1 #include <> 2 void debug(char *str) 3 { 4 printf("debug info :%s\n",str ); 5 } 6 7 main(int argc,char *argv[]){ 8 int i,j; 9 j=0; 10 for(i=0;i<10;i++){ (gdb) 11 j+=5; 12 printf("now j=%d\n", j); 13 debug("x=======x"); 14 } 15 }(gdb) Line number 16 out of range; has 15 lines. (gdb) break main Breakpoint 1 at 0x4004fa: file , line 9. (gdb) r Starting program: /mnt/hgfs/www/c/gcc/e1 Breakpoint 1, main (argc=1, argv=0x7fffffffe538) at :9 9 j=0; (gdb) watch i Hardware watchpoint 2: i (gdb) watch j Hardware watchpoint 3: j (gdb) c Continuing. Hardware watchpoint 3: j Old value = 0 New value = 5 main (argc=1, argv=0x7fffffffe538) at :12 12 printf("now j=%d\n", j); (gdb) c Continuing. now j=5 debug info :x=======x Hardware watchpoint 2: i Old value = 0 New value = 1 0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at :10 10 for(i=0;i<10;i++){ (gdb) c Continuing. Hardware watchpoint 3: j Old value = 5 New value = 10 main (argc=1, argv=0x7fffffffe538) at :12 12 printf("now j=%d\n", j); (gdb) c Continuing. now j=10 debug info :x=======x Hardware watchpoint 2: i Old value = 1 New value = 2 0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at :10 10 for(i=0;i<10;i++){ (gdb) c Continuing. Hardware watchpoint 3: j Old value = 10 New value = 15 main (argc=1, argv=0x7fffffffe538) at :12 12 printf("now j=%d\n", j); (gdb) c Continuing. now j=15 debug info :x=======x Hardware watchpoint 2: i Old value = 2 New value = 3 0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at :10 10 for(i=0;i<10;i++){ (gdb) c Continuing. Hardware watchpoint 3: j Old value = 15 New value = 20 main (argc=1, argv=0x7fffffffe538) at :12 12 printf("now j=%d\n", j); (gdb) c Continuing. now j=20 debug info :x=======x Hardware watchpoint 2: i Old value = 3 New value = 4 0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at :10 10 for(i=0;i<10;i++){ (gdb) set var i=8 (gdb) c Continuing. Hardware watchpoint 3: j Old value = 20 New value = 25 main (argc=1, argv=0x7fffffffe538) at :12 12 printf("now j=%d\n", j); (gdb) c Continuing. now j=25 debug info :x=======x Hardware watchpoint 2: i Old value = 8 New value = 9 0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at :10 10 for(i=0;i<10;i++){ (gdb) c Continuing. Hardware watchpoint 3: j Old value = 25 New value = 30 main (argc=1, argv=0x7fffffffe538) at :12 12 printf("now j=%d\n", j); (gdb) c Continuing. now j=30 debug info :x=======x Hardware watchpoint 2: i Old value = 9 New value = 10 0x0000000000400533 in main (argc=1, argv=0x7fffffffe538) at :10 10 for(i=0;i<10;i++){ (gdb) c Continuing. Watchpoint 2 deleted because the program has left the block in which its expression is valid. Watchpoint 3 deleted because the program has left the block in which its expression is valid. __libc_start_main (main=0x4004eb <main>, argc=1, ubp_av=0x7fffffffe538, init=<value optimized out>, fini=<value optimized out>, rtld_fini=<value optimized out>, stack_end=0x7fffffffe528) at :258 258 exit (result); (gdb) c Continuing. Program exited with code 026.