Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate GDB to print stack frame at a particular breakpoint

Tags:

c

gdb

Is it possible to have gdb attached to the PID of a running process and every time the program hits a particular breakpoint have gdb output the stackframe to an external file?

I've had a look at this and this but there is no mention on whether it is possible to attach gdb to an already running process (instead of having gdb launch it).

I can otherwise attach gdb to the PID just fine, but I'd like to automate it to run bt, store the output in an external file and then run continue. At the moment I'm doing this manually and it's a pain when I have to do it every time a breakpoint is hit.

like image 231
Nobilis Avatar asked Jul 16 '13 09:07

Nobilis


People also ask

How do I go to a specific breakpoint in GDB?

1 Answer. Show activity on this post. Just press c. It will continue execution until the next breakpoint.

What GDB command verifies and displays the stack frames?

Selects a stack frame or displays the currently selected stack frame.

What are the three commands used in GDB to examine and move within the stack?

Able to view and traverse the function call stack using the where, up, down and frame commands.


2 Answers

Is it possible to have gdb attached to the PID of a running process??

Yes. Possible.

Updated:

Step 1:

In .gdbinit file add the following command,

define callstack
     set $Cnt = $arg0

     while($Cnt)
        commands $Cnt
        silent
        bt
        c
        end
        set $Cnt = $Cnt - 1
     end
end

Step 2: Invoke the gdb with -x <path to .gdbinit file >. Remember PID also for running process.

Step 3: Put Break points, whereever you need.

Step 4: Call the user defined command callstack and pass no .of break points.

    gdb> callstack <No.of. Break Points> 

Step 5: Now give 'c' to continue. Bcos process is already running.

For Logging I suggest to follow @VoidPointer's answer.

set pagination off
set logging file gdb.txt
set logging on 

works for me. Reference.

like image 111
Jeyaram Avatar answered Oct 17 '22 06:10

Jeyaram


If what you need is to automate printing stack-frame using gdb when knowing PID and functions then you can try this.. (Given minimal code to be functional)

/root/.gdb_init:

set pagination off
set logging file gdb.txt
set logging on

br fun_convert
# ^^ when breaking at function fun_convert, execute `commands` till next `end`
commands
    bt
    print "Sample print command 1 \n"
    continue
end

br file.c:451
# ^^ when breaking at line 451 of file.c, execute from `commands` till next `end`
commands
    bt
    print "Sample print command 2 \n"
    continue
end

continue

Invoking GDB for PID 6474 and command file /root/.gdb_init,

gdb -p 6474 -x /root/.gdb_init

Here, fun_convert is the function to break. This br is actual break gdb-command and you can also break at any line of file using br file.c:451. For more break options, check gdb help. You can add any gdb-commands you need between commands and end for corresponding br. For more info on commands, check help commands on gdb.

Note: SO's JS is broken on my browser, please pardon any mistakes and feel free to correct. Also can't add comments :(

like image 5
VoidPointer Avatar answered Oct 17 '22 04:10

VoidPointer