Question

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"
Was it helpful?

Solution

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.

OTHER TIPS

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.

If you just want to exit if any error occurs, you can use the -batch gdb option:

Run in batch mode. Exit with status 0 after processing all the command files specified with ‘-x’ (and all commands from initialization files, if not inhibited with ‘-n’). Exit with nonzero status if an error occurs in executing the GDB commands in the command files. [...]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top