gdb stops in a command file if there is an error. How to continue despite the error?

10,762

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.

Share:
10,762
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    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
    Admin almost 11 years
    Thank you for your advice. I did't managed to make use of ignore-errors since I get Undefined command: "ignore-errors" but I created a similar command and I worked.
  • Tom Tromey
    Tom Tromey almost 11 years
    Yeah, you had to search for it. sourceware.org/ml/gdb/2010-06/msg00100.html
  • Admin
    Admin almost 11 years
    Let 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 own my_ignore_errors. You can see it in my updated question.
  • Tom Tromey
    Tom Tromey almost 11 years
    You can just "source ignore-errors.py". That will register the "ignore-errors" command.