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.
Related
I used exit(EXIT_FAILURE) in a precise function which is not the main one, but after its execution nothing is executed in the main function.
I really wanna know why ?
Thanks a lot in advance.
In order to understand how exit works it helps to understand how program termination upon returning from main works.
When the program returns from main it returns to a piece of helper code in the C or C++ runtime. That piece performs some cleanup and then calls the _exit system call with the return value of main. The kernel will stop execution of the process at that point.
The exit function has similar behavior, it performs the same cleanup as the helper code from the C and C++ runtime does when returned from main, then calls the _exit system call. It's this last call to the _exit system call that prevents execution of anything else (in main or otherwise) after calling of exit.
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.
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".
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.
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.