GDB: Automatic "Next"?

Quick this time.

Is it possible (besides pressing permanently pressing) for gdb to constantly next through the program in turn to find where the error occurs?

Edit: continue not what I would like; I would like to effectively track the execution of the program in turn, since you could receive from next ing again and again.

+8
c debugging gdb
Apr 28 2018-11-11T00:
source share
5 answers

Here is something what a hack is, I'm a little confused to post it. But if you only need one-time, it can do well enough so that you can get the information you need. There really should be a better way.

You can define a silly little gdb script that executes step or next commands a certain number of times:

 # file: step_mult.gdb define step_mult set $step_mult_max = 1000 if $argc >= 1 set $step_mult_max = $arg0 end set $step_mult_count = 0 while ($step_mult_count < $step_mult_max) set $step_mult_count = $step_mult_count + 1 printf "step #%d\n", $step_mult_count step end end 

(I used the step instead of next without much reason, just change it to whatever you need.)

Then you can run this command (with an extra counter) and it will display each step or next .

Here is an example of a program that will crash when trying to dereference a NULL pointer:

 #include<stdio.h> int x[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,9, 10 }; int* p[11]; int main() { int i; for (i = 0; i < 11; ++i) { p[i] = &x[i]; } p[5] = 0; for (i = 0; i < 11; ++i) { printf( "*p[%d] == %d\n", i, *p[i]); } return 0; } 

Here the gdb session (on Windows) debugs this program and uses the step_mult script:

 C:\temp>gdb test.exe GNU gdb (GDB) 7.2 ... Reading symbols from C:\temp/test.exe...done. (gdb) source c:/temp/step_mult.gdb (gdb) start Temporary breakpoint 1 at 0x401385: file C:\temp\test.c, line 23. Starting program: C:\temp/test.exe [New Thread 5396.0x1638] Temporary breakpoint 1, main () at C:\temp\test.c:23 23 for (i = 0; i < 11; ++i) { (gdb) step_mult 70 step #1 24 p[i] = &x[i]; step #2 23 for (i = 0; i < 11; ++i) { step #3 24 p[i] = &x[i]; step #4 23 for (i = 0; i < 11; ++i) { step #5 24 p[i] = &x[i]; step #6 23 for (i = 0; i < 11; ++i) { step #7 24 p[i] = &x[i]; step #8 23 for (i = 0; i < 11; ++i) { step #9 24 p[i] = &x[i]; step #10 23 for (i = 0; i < 11; ++i) { step #11 24 p[i] = &x[i]; step #12 23 for (i = 0; i < 11; ++i) { step #13 24 p[i] = &x[i]; step #14 23 for (i = 0; i < 11; ++i) { step #15 24 p[i] = &x[i]; step #16 23 for (i = 0; i < 11; ++i) { step #17 24 p[i] = &x[i]; step #18 23 for (i = 0; i < 11; ++i) { step #19 24 p[i] = &x[i]; step #20 23 for (i = 0; i < 11; ++i) { step #21 24 p[i] = &x[i]; step #22 23 for (i = 0; i < 11; ++i) { step #23 27 p[5] = 0; step #24 29 for (i = 0; i < 11; ++i) { step #25 30 printf( "*p[%d] == %d\n", i, *p[i]); step #26 *p[0] == 0 29 for (i = 0; i < 11; ++i) { step #27 30 printf( "*p[%d] == %d\n", i, *p[i]); step #28 *p[1] == 1 29 for (i = 0; i < 11; ++i) { step #29 30 printf( "*p[%d] == %d\n", i, *p[i]); step #30 *p[2] == 2 29 for (i = 0; i < 11; ++i) { step #31 30 printf( "*p[%d] == %d\n", i, *p[i]); step #32 *p[3] == 3 29 for (i = 0; i < 11; ++i) { step #33 30 printf( "*p[%d] == %d\n", i, *p[i]); step #34 *p[4] == 4 29 for (i = 0; i < 11; ++i) { step #35 30 printf( "*p[%d] == %d\n", i, *p[i]); step #36 Program received signal SIGSEGV, Segmentation fault. 0x004013d2 in main () at C:\temp\test.c:30 30 printf( "*p[%d] == %d\n", i, *p[i]); step #37 Program received signal SIGSEGV, Segmentation fault. 0x004013d2 in main () at C:\temp\test.c:30 30 printf( "*p[%d] == %d\n", i, *p[i]); step #38 Program exited with code 030000000005. step #39 The program is not being run. (gdb) 

Unfortunately, since the script does not stop when segfault occurs, gdb decides to just stop debugging the program, so you cannot make any additional useful requests. But a magazine can be useful.

I am sure there are many ways to make a script more intelligent. Unfortunately, I have no idea how to do this, and user level documents for GDB do not seem too useful for these details. It would be best if the script could detect segfault or signal, and just stop, instead of relying on some arbitrary account. I assume that the gdb / MI interface, or perhaps even the Python scripting interface, may have a good mechanism, but I don't know anything about them.

After the first start, you can use the displayed account (37 in my example) and restart the program and give an account that is just shy of where it crashed before and take control manually.

As I said, this is not particularly beautiful - but it can lead you there .

+7
Apr 28 2018-11-11T00:
source share

You can use continue or c to continue executing the next breakpoint.

Also see https://stackoverflow.com>

0
Apr 28 '11 at 1:24
source share

The continue command will work until a breakpoint occurs, the application throws an exception (i.e., a failure), or the application terminates.

0
Apr 28 '11 at 1:26
source share

See also this post: c - automatic transition on GDB - automatic printout of lines, and free start? which uses the python interface for gdb.

0
May 27 '12 at 17:00
source share

Thanks for this. I was able to modify it a bit to help with my debugging efforts on memory overflow problem. It was nice to see the account where the failure occurred, so I could stop it with one or two instructions until the 2nd launch.

-one
May 04 '19 at 10:00
source share



All Articles