gdb stops in a command file if there is an error. How to continue despite the error?
Solution 1
gdb's command language doesn't have a way to ignore an error when processing a command.
This is easily done, though, if your gdb was built with the Python extension. Search for the "ignore-errors" script. With that, you can:
(gdb) ignore-errors print *foo
... and any errors from print will be shown but not abort the rest of your script.
Solution 2
You can also do this:
gdb a.out < analyze.v2.gdb
This will execute the commands in analyze.v2.gdb line by line, even if an error occurs.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 05, 2022Comments
-
Admin about 2 years
I my real gdb script while analyzing a core file I try to dereference a pointer and get "Error in sourced command file: Cannot access memory at address " and then my gdb script stops. What I want is just to go on executing my gdb script without stopping. Is it possible?
This is a test program and a test gdb script that demonstrates my problem. In this situation the pointer has NULL value but in a real situation the pointer will like have not null invalid value.
This is test C program:
#include <stdio.h> struct my_struct { int v1; int v2; }; int main() { my_struct *p; printf("%d %d\n", p->v1, p->v2); return 0; }
This is a test gdb script:
>cat analyze.gdb p p->v1 q
And this is demonstration of the problem (what I want from gdb here is to get this error message and then go process
quit
command):>gdb -silent a.out ./core.22384 -x ./analyze.gdb Reading symbols from /a.out...done. [New Thread 22384] Core was generated by `./a.out'. Program terminated with signal 11, Segmentation fault. #0 0x0000000000400598 in main () at main.cpp:11 11 printf("%d %d\n", p->v1, p->v2); ./analyze.gdb:1: Error in sourced command file: Cannot access memory at address 0x0 Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.x86_64
Update
Thanks to Tom. This is a gdb script that handles this problem:>cat ./analyze.v2.gdb python def my_ignore_errors(arg): try: gdb.execute("print \"" + "Executing command: " + arg + "\"") gdb.execute (arg) except: gdb.execute("print \"" + "ERROR: " + arg + "\"") pass my_ignore_errors("p p") my_ignore_errors("p p->v1") gdb.execute("quit")
This is how it works:
>gdb -silent ./a.out -x ./analyze.v2.gdb -c ./core.15045 Reading symbols from /import/home/a.out...done. [New Thread 15045] Core was generated by `./a.out'. Program terminated with signal 11, Segmentation fault. #0 0x0000000000400598 in main () at main.cpp:11 11 printf("%d %d\n", p->v1, p->v2); $1 = "Executing command: p p" $2 = (my_struct *) 0x0 $3 = "Executing command: p p->v1" $4 = "ERROR: p p->v1" $5 = "Executing command: quit"
-
Admin almost 11 yearsThank you for your advice. I did't managed to make use of
ignore-errors
since I getUndefined command: "ignore-errors"
but I created a similar command and I worked. -
Tom Tromey almost 11 yearsYeah, you had to search for it. sourceware.org/ml/gdb/2010-06/msg00100.html
-
Admin almost 11 yearsLet me explain. In gdb on my server there ignore-errors.py but I don't know yet how to use it. Simply entering
ignore-errors print p
does not work. Nevertheless I made use of the idea in the ignore-errors.py and created my ownmy_ignore_errors
. You can see it in my updated question. -
Tom Tromey almost 11 yearsYou can just "source ignore-errors.py". That will register the "ignore-errors" command.