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.
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 .
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 :(
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.