Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb: logging something instead of breaking?

Tags:

unix

logging

gdb

Is it possible to have gdb log something to the terminal instead of breaking on it? For example I would like to set a 'breakpoint' on some method and have gdb print self as well as the parameters each time the method is invoked. Basically I want to insert print statements into arbitrary places without actually recompiling.

thanks for any suggestions


This is what i have so far after these helpful comments:

define logFoo
b fooMethod
commands
po self
end
end

GDB doesn't seem to like the nested end statements though. any thoughts?

like image 979
D.C. Avatar asked Jun 01 '11 19:06

D.C.


People also ask

How do I skip a breakpoint in GDB?

To skip a breakpoint a certain number of times, we use the ignore command. The ignore command takes two arguments: the breakpoint number to skip, and the number of times to skip it. (gdb) ignore 2 5 Will ignore next 5 crossings of breakpoint 2.

How do I set a break in GDB?

To do this, just type "break [functionname]". gdb will stop your program just before that function is called. Breakpoints stay set when your program ends, so you do not have to reset them unless you quit gdb and restart it.

How do you save breakpoints in GDB?

To save breakpoint definitions to a file use the save breakpoints command. This command saves all current breakpoint definitions together with their commands and ignore counts, into a file filename suitable for use in a later debugging session.

What is watchpoint in GDB?

Setting watchpoints. You can use a watchpoint to stop execution whenever the value of an expression changes, without having to predict a particular place where this may happen. Depending on your system, watchpoints may be implemented in software or hardware.


1 Answers

You can use Breakpoint Command Lists. There is an example how to do it.

For example, here is how you could use breakpoint commands to print the value of x at entry to foo whenever x is positive.

 break foo if x>0
 commands
 silent
 printf "x is %d\n",x
 cont
 end
like image 105
ks1322 Avatar answered Oct 10 '22 03:10

ks1322