gdb how to find where the function exited - c++

I have put a break point on a function by attaching to a running program. The function is long, and returns same error value from multiple places. Is there any way to find out where the function has exited without stepping through each line or putting breakpoints on all return values?
There is finish command which gives me the return value, but it doesn't say where it exited.
Thanks in advance

Enable reverse debugging and then put a breakpoint after the function call. Once the breakpoint is hit, do reverse-next/reverse-step to go backwards to the return statement that terminated the function.

Related

Why would a PyDev breakpoint on a return statement only work part of the time?

I have some code that calculates the price of a stock option using Monte Carlo and returns a discounted price. The final few lines of the relevant method look like this:
if(payoffType == pt.LongCall or payoffType == pt.LongPut):
discountedPrice=discountedValue
elif(payoffType == pt.ShortCall or payoffType == pt.ShortPut):
discountedPrice=(-1.0)*discountedValue
else:
raise Exception
#endif
print "dv:", discountedValue, " px:", discountedPrice
return discountedPrice
At a higher level of the program, I create four pricers, which are passed to instances of a portfolio class that calls the price() method on the pricer it has received.
When I set the breakpoint on the if statement or the print statement, the breakpoints work as expected. When I set the breakpoint on the return statement, the breakpoint is interpreted correctly on the first pass through the pricing code, but then skipped on subsequent passes.
On occasion, if I have set a breakpoint somewhere in the flow of execution between the first pass through the pricing code and the second pass, the breakpoint will be picked up.
I obviously have a workaround, but I'm curious if anyone else has observed this behavior in the PyDev debugger, and if so, does anyone know the root cause?
The issues I know of are:
If you have a StackOverflowError anywhere in the code, Python will disable the tracing that the debugger uses.
I know there are some issues with asynchronous code which could make the debugger break.
A workaround is using a programmatic breakpoint (i.e.: pydevd.settrace -- the remote debugger session: http://www.pydev.org/manual_adv_remote_debugger.html has more details on it) -- it resets the tracing even if Python broke it in a stack overflow error and will always be hit to (the issue on asynchronous code is that the debugger tries to run with untraced threads, but sometimes it's not able to restore it on some conditions when dealing with asynchronous code).

Step into using GDB

I am performing some analysis of code using gdb. Just suppose the program is like
#include<stdio.h>
getinput()
{
char buffer[8];
gets(buffer);
puts(buffer);
}
int main()
{
getinput();
return 0;
}
I have complied it using gcc withsome other switches as mentioned below:
gcc -ggdb -mpreferred-stack-boundary -fno-stack-protection -o demo demo.c
On analysis with gdb, when I insert break point on getinput() and gets(buffer) function..
Then using "s" step through function.. on gets(buffer) function..
it does not ask for the user input, rather it asks for userinput. GDB directly steps into function gets...but i don't want it to move into gets functions..
(gdb) break 6
Breakpoint 2 at 0x8048441: file demo.c, line 6.
(gdb) s
The program is not being run.
(gdb) run
Starting program: /root/BufferOverflow/demo
Breakpoint 1, main () at demo.c:11
11 getinput();
(gdb) s
Breakpoint 2, getinput () at demo.c:6
6 gets(buffer);
(gdb) s
_IO_gets (buf=0xbfffeda8 "y\204\004\b") at iogets.c:32
32 iogets.c: No such file or directory.
(gdb)
I am getting the error shown above. Can anyone please help me regarding this ???
Then using "s" step through function.. on gets(buffer) function..
Using step command on line 6 will step into gets() function. If you don't want this, use next command. It will move to the next line of code in getinput() function (to puts() call).
gdb has a variety of ways to advance through a program. An excellent overview can be found here.
The most common sequence will be something like:
break [line or function/method]
run args
next
print [interesting variables]
The next will skip any function calls on the line and move to the next source line in the current function. The step will step into a function.
If you accidentally step into a non-interesting function the finish command will run until the end of the current function.
A common case is stepping into a function that is evaluating an argument.
my_method(gets(buffer));
A step on this line will lead you into the assembler of the gets() function. Usually this is not what you want. For such cases, I will usually just set a breakpoint on my_method and then cont.
gdb tries to avoid this situation with the step-mode setting. It doesn't always work however as you see in your example.
gdb is a great tool that will save you time and misery. If you are working as a developer then certainly take time to master it.

How to check current breakpoint function in TRACE32?

I'm trying to check if the program stopped in a function in a TRACE32.
I know I can see the the functions in FRAME window but no idea how to copy them to a variable inside my script.
Any idea how to do that ?
You get the name of the function, where the program counter points to with:
PRINT sYmbol.FUNCTION(PP())
(Instead of printing the result you can also assign it to a macro.)
So one approach to check if you've stopped in function myFunc() would be:
PRINT STRing.ComPare(sYmbol.FUNCTION(PP()),"*\myFunc")
Another way is to check if the program counter is inside the first and last address of your function myFunc():
PRINT (ADDRESS.OFFSET(sYmbol.BEGIN(`myFunc`))<=Register(PP))&&(Register(PP)<=ADDRESS.OFFSET(sYmbol.END(`myFunc`)))

How to set a breakpoint to the end of a function in TRACE32?

I want to set a breakpoint to the end of a function in Lauterbach.
I know that this can be achieved using Break.set sYmbol.EXIT(function_name).
Unfortunately, this isn't working.
Can you indicate another command for this ?
To set breakpoint at the end of the function, you can use
g.r command. This command will run the program till the return statement of the function.
You should use BREAK.SET sYmbol.END(function_name), I had the same error and this fixed it.

Lauterbach execute script when breakpoint is hit

I am using Lauterbach to debug a PowerPC embedded C software. I want to execute the below ALGO from a .cmm(PRACTICE) script. Pleas let me know if it is possible:
Set Breakpoint
When Breakpoint is hit, execute a .cmm file. This .cmm file will rewrite the values of an array.
Continue execution of program
I don't want to stub the whole function. The code has to be untouched.
Set the breakpoint with
Break.Set <addr> /Program /CMD "DO myScript.cmm"
To continue the execution of the target program, add the command Go to the end of the called PRACTICE script.
If you can't add the command Go to the end of the called PRACTICE script, you'll need a veneer-script like this:
// Content of myScript.cmm
DO myAlgorithm.cmm
Go
ENDDO
The Break.Set command knows also an option /RESUME, but this is not suitable for your case, since it won't wait until the called PRACTICE script has finished.
As you have mentioned !
I don't want to stub the whole function. The code has to be untouched.
You can try this;
;set a breakpoint on function
BREAK.SET <function_name/addr>\<LINE NUMBER>
;store address of current program counter(PC)
&pc=r(pc)
&call=address.offset(<function_name/addr>\<LINE NUMBER>) ;This will give the address of a function where breakpoint is set.
;Compare the address if it hit on correct function
IF (&pc==&call)
Do call_meonceHIT.cmm ;your desired .cmm script.
Break.Delete /ALL ; to delete all the set breakpoint.
This will make sure that breakpoint is hitting correct function or runnable.