Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ask GDB to break at a function only after it's called certain times?

Tags:

For example, I have a function NamespaceA::ClassB::FunctionC() in my program. I know I can ask GDB to break there, by "break NamespaceA::ClassB::FunctionC". But what should I do if I only want GDB to break there, after this function is called, say, 100 times?

I think a workaround solution is adding one more variable in the program, and then there is "break ... if cond" command in GDB i can use. But can i achieve the same thing without adding variables to my program?

Thank you.

like image 984
Yang Chi Avatar asked Oct 11 '11 21:10

Yang Chi


People also ask

How do you set a breakpoint in GDB at a specific line?

You can also set breakpoints on function names. 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 I stop GDB after continue?

You should interrupt the process that is attached by gdb. Do not interrupt gdb itself. Interrupt the process by either ctrl-c in the terminal in which the process was started or send the process the SIGINT by kill -2 procid. With procid the id of the process being attached.

How do you break a loop in GDB?

Like you can cancel a program on the command line, GDB lets you use ctrl-c to stop a program wherever it currently is. Hit ctrl-c now to break the infinite loop.

What is the command in GDB that can be used to find out the next instruction to be executed?

It is often useful to do ' display/i $pc ' when stepping by machine instructions. This makes GDB automatically display the next instruction to be executed, each time your program stops. See Automatic Display.


1 Answers

(gdb) continue 100 

should do the trick

(gdb) help continue Continue program being debugged, after signal or breakpoint.  If proceeding from breakpoint, a number N may be used as an argument,  which means to set the ignore count of that breakpoint to N – 1 (so that  the breakpoint won’t break until the Nth time it is reached). 
like image 68
sehe Avatar answered Sep 29 '22 20:09

sehe