How to let GDB continue until the program enters another function? - c++

Say I am in A() and A() calls B(). I just entered A() and I want the program to run until I am in B(). It doesn't have to be a specific function B(). I just want my program to pause whenever it enters a new function. Is there a way to do that?

For calls, as mentioned at: List of all function calls made in an application :
set confirm off
rbreak .
rbreak sets a breakpoint for every function that matches a given regular expression, . matches all functions.
This command might take a while to run for a large executable with lots of functions. But once it finishes, runtime will be efficient.
The exit is trickier, since we can't know at compile time where we will land: How to set a breakpoint in GDB where the function returns?
At How to break on instruction with a specific opcode in GDB? I also provided a script that single steps until a desired instruction is found, which you could use to find callq. That one has the advantage of not making you wait on a large executable, but execution will be very slow, so the target can't be very far away.

There would be a nice solution in form of setting a breakpoint on call instruction, but as this answer states there is no way to do that.
I think, the easiest solution would be to set that breakpoints manually or try to write a script in Python which finds function calls in the currect function listing and sets desired breakpoints.

Related

gdb proceed execution until new frame on the call stack

Is there a way to proceed execution in gdb until a function is called and then pause inside that function? It is a pain to use n and s for the code I am working with. I would much prefer a nextFrame and fin, assuming a nextFrame existed which took me into a new function. It would be extra cool if nextFrame could tell me when we only unwind the stack from the current scope, i.e. we do not make another function call, and then it could pause at the last line of the current scope.
Basically, I want to view my codebase from a callstack perspective and not from a line by line perspective.
P.S. Assuming such a facility exists, I would imagine it being problematic to use with boost. Eg. if I have a line like boost::shared_ptr<MyType> a = foo(); then it will pause first inside boost code, before it pauses inside foo(). This is a problem because I am not interested in the boost code and only want to see what is inside foo.
P.S. I also have clang. I wonder if this is possible in clang.
Use b function_name to apply the break point inside a function.
Your program in execution will pause at the entry of that function.
Alternatively You can also use
b filename:line_number to pause the execution of your program at the specific line in a file.

How do I set a break point to stop before entering the main program (or function) (GDB)

I know how to set breakpoints at specific lines (so I could set a break at line 1)
or i can also do :
break main (to set a break point at the entering of the main program)
but how do you set a break point BEFORE it enters a certain function or the main program?
also if anyone has the time. how do you start execution of a gdb program at a specific breakpoint (say if i set a breakpoint at line 7).
There is no command (AFAIK) to put a breakpoint on all calls of some specific function (there could be multiple places from where a function is called, including virtual functions, function pointers, which gdb can't even discover).
How to start before main has been explained multiple times, including here. You can then step up to main.
In general why would you care to stop before function is entered? The difference is only in argument setup/stack. You put a breakpoint on function startup, then you move to the frame above ("up" command) and examine it as if function has not been called yet.
Similarly, it does not make much sense* to resume execution at some random point in your code, without having local variables / registers / arguments properly set up. You can execute a function (using "call" command). This does make sense.
(*) It is possible, though, but if you don't set up context appropriately, you will crash. Do "info break". This will give you an address at which breakpoint has been inserted (let's say it's 0x00000000004005ea. Then set your PC to that address (on x86-64 that would be "set $rip = 0x00000000004005ea"), set up your stack, registers, etc and then "continue".

break gdb when next fn is pushed onto the stack

is it possible that gdb breaks when next function is pushed onto the stack. If yes, how ?
There could be situations when you dont know what is the next fn that would be called from current fn, for example, calling the next function using callback.
If the inferior is stopped and you want to step into the next function call, you can just step until you reach it.
If you want a way to say "please continue but stop when the next function is called" -- well, there is no built-in way to do that in gdb. If it's a real need, you could try to implement it in a couple of ways.
One way would be to use Python to automate the stepping. The idea is, call step until the newest frame changes.
Another way would be to try to set a watchpoint on the frame pointer. This only works if your code has frame pointers, though.

Trap a function call?

Is there a way to trap a function call in C++. Like a process may be calling setLocale even though. I want to find whether is there any call to it or not ?
What I can understand from you statement. It is fairly easy to do with any debugger. Run you code on gdb add a breakpoint on the first line of your function. When this breakpoint hits see the backtrace to know how this function was called.

Tracing write access to class instance/memory range in gdb

I am trying to debug a small operating system I have written in an university course in C++. At runtime somewhere one of my objects is getting corrupted. It seems like this happens due to accidentally writing to the wrong memory address. As I am unable to find the place where this happens from pure looking at the code, I need another way.
As this is an operating system, I cannot attach tools like valgrind to it, but I can run it in emulators (bochs/qemu) with gdb attached.
Is there a way in gdb to trace write access to a class instance or more general a specific memory range? I would like to break as soon as write access happens, so I can verify if this is valid or not.
You can put a watchpoint:
watch x
This will break when x is modified. x can be any type of variable. If you have:
class A;
A x;
Then gdb will break whenever x is modified.
You can actually put a watchpoint on any expression, and gdb will break when the expression changes. Be careful with this, though, because if the expression isn't something that the underlying hardware supports, gdb will have to evaluate this after every instruction, which leads to awful performance. For example, if A above is a class with many members then gdb can watch the entire instance x, but the way it'll work is:
execute an instruction
jump to the debug breakpoint
check if x has changed
return to the program
Naturally, this is very slow. If x is an int then gdb can use a hardware breakpoint.
If you have a specific memory address you can watch it too:
watch *0x1234
This will break when the contents of [0x1234] changes.
You can also set a read breakpoint using rwatch, or awatch to set a read/write breakpoint.
If you know at least approximately where it happens you can also just use "display" instead of watch and manually step line by line until you see when the change happens. Watching an address using "watch" is just too painfully slow.