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.
Related
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.
I just was looking up funciton attributes for gcc
(http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html)
and came across the returns_twice attribute.
And I am absolutely clueless in what case a function can return twice... I looked up quickly the mentioned vfork() and setjmp() but continue without an idea how an applicable scenario looks like - anyone of you used it or can explain a bit?
The setjmp function is analogous to creating a label (in the goto sense), as such you will first return from setjmp when you set the label, and then each time that you actually jump to it.
If it seems weird, rest assured, you should not be using setjmp in your daily programming. Or actually... you should probably not be using it at all. It is a very low-level command that break the expected execution flow (much like goto) and, especially in C++, most of the invariants you could expect.
When you call setjmp, it establishes that as a return point, then execution continues at the code immediately following the setjmp call.
At some point later in the code, calling longjmp (with the jump buffer initialized by the previous call to setjmp) returns execution to start from that same point again (i.e., the code immediately following the call the setjmp).
Therefore, the original call returns normally, then at arbitrary later times, execution returns (or at least may return) to the same point again.
The attribute simply warns the compiler of that fact.
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.
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.
This is a followup to Clojure: Compile time insertion of pre/post functions
My goal is to call a debug function instead of throwing an exception. I am looking for the best way to store a list of stack frames, function calls and their arguments, to accomplish this.
I want to have a function (my-uber-debug), so that when I call it (instead of throwing an exception), the following things happen:
a new Java window pops up
there is a record of the current clojure stack frame
for each stack frame, there is a record of the argument passed to the function
This is so that I can move up/down the stack frames, and examine the arguments passed to get to this current point. [If somehow, magically, we can get the variables defined in "let" environments, that'd be awesome too.]
Current Idea
I'm going to have a thread local variable uber-debug, which has type:
List of StackFrames
where StackFrame = function + arguments
At each function call, it's going to push (cons the current function + arguments to uber-debug), then at the end of a function call, it's going to remove the first element from uber-debug
Then, when I call (my-uber-debug), it just pops up a new java window, and lets me interact with uber-debug
Question
The ideas I've had so far are probably not ideal for setting this up. What is the right way to solve this problem?
Edit:
The question is NOT about the Swing/GUI part. It's about how to store the stack frames.
Thanks!
Your answer may depend on a lot of factors, so I am going to answer this by giving you my thoughts.
If you merely want to store function calls and their parameters when an exception occurs, then either write a macro or function as a wrapper to accomplish this. You would then have to pass all functions to be called to this wrapper. The wrapper would perform the try catch operation and whatever else you need.
You might also want to look into Clojure meta data in addition to writing the wrapper, because your running code could look at its meta-data and make some decisions based on that as well. I have never used meta data, but the information at the link looks promising.
As a final thought, it might be helpful for you to further delineate what you want to accomplish by doing this by editing your original post and putting the information there.
For example, are these stack traces for a library or a main program?
As to storing all this information, are multiple threads going to need it, or just one?
Can you get by storing the information in a let binding at the highest level of your program, or do you need something like a ref?