gdb step into functions until a breakpoint is reached - gdb

I am trying to trace function call made.
When running a gdb, i m adding a breakpoint say at line no "n"
I want to step into all function starting from line "n" to line "n+1"
Note:
Line n has internally many function calls
Please help me with this problem

Related

gdb rbreak and commands (or dprintf behavior)?

Taking the example from http://shanekirk.com/2017/08/gdb-tips-and-tricks-2-setting-breakpoints-with-regular-expressions/ - when I use rbreak, I get something like:
(gdb) rb TestFixture.h:.
Breakpoint 1 at 0x4008b6: file TestFixture.h, line 5.
void TestFixture::setUp();
Breakpoint 2 at 0x4008d4: file TestFixture.h, line 6.
void TestFixture::tearDown();
Breakpoint 3 at 0x4008f2: file TestFixture.h, line 7.
void TestFixture::testA();
Breakpoint 4 at 0x400910: file TestFixture.h, line 8.
void TestFixture::testB();
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004008b6 in TestFixture::setUp() at TestFixture.h:5
2 breakpoint keep y 0x00000000004008d4 in TestFixture::tearDown() at TestFixture.h:6
3 breakpoint keep y 0x00000000004008f2 in TestFixture::testA() at TestFixture.h:7
4 breakpoint keep y 0x0000000000400910 in TestFixture::testB() at TestFixture.h:8
Now, what I want is basically a dprintf-like behavior: once one of this breakpoints is hit, I just want the function name printed out, and then continue (basically, a function call trace)
However, the way I understand gdb - in order to do that, I would issue a rbreak [regex] first, then I get a bunch of breakpoints, then for each and every one of those I'd had to type manually:
commands [number-of-breakpoint]
print "[name of function]"
continue
end
... which quickly becomes a chore, especially if you end up with a lot more breakpoints than the 4 in the above example (say hundreds).
Now, it would be rather cool, if I could use something like "regex dprintf", or rdprintf, as in:
rdprintf TestFixture.h:., "%s\n", $__breakname__
... but as far as I know, there is no such command...
Or, if after issuing a rbreak TestFixture.h:., I could target the commands for those breakpoints as:
commands 1-4
print $__breakname__
continue
end
... but again, I think this does not exist either...
So is there a way to use gdb to provide this kind of a function call trace printout - without me manually typing the names of breakpoints and their commands, similar to how rbreak allows you to set multiple breakpoints with one command?
EDIT: just found List of all function calls made in an application - record function-call-history /ilc might be interesting, but there doesn't seem to be a way to limit the scope of what functions to trace, say with a regex...
Ok, via the link above, found https://stackoverflow.com/a/39124320/277826 - turns out, you can issue command for multiple breakpoints, as found by rbreak; and to print the name of the function, just use backtrace 1:
(gdb) command 1-36
Type commands for breakpoint(s) 1-36, one per line.
End with a line saying just "end".
>silent
>bt 1
>continue
>end
(gdb) r
... or with python, printing the frame at bt 0 and its parent's frame name:
command 1-36
silent
python print("{} <- {}".format( gdb.execute("bt 0", False, True).strip(), gdb.newest_frame().older().name() ))
continue
end
... or even better, python printing bt 0 function name and args, and parent name:
command 1-36
silent
python nf = gdb.newest_frame(); nfb = nf.block()
python nfargs = [ "{}={}".format(sym, nf.read_var(sym, nfb)) for sym in nfb if sym.is_argument ]
python print("#0 {}({}) <- {}".format(nf.name(), ",".join(nfargs), nf.older().name() ))
continue
end
... which would print something like:
#0 Searcher::FlagFromCmd(this=0x7fffffffaed8,cmd=808) <- FindLiveStrip::GrabToggles
#0 Searcher::FlagFromCmd(this=0x7fffffffaed8,cmd=807) <- FindLiveStrip::ToggleChanged
... and this seems to work fine; though if there are other options, I'd love to know about them.

gdb how to find where the function exited

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.

gdb print file name line number when program is running

I need to debug a flow using gdb - I do not know the call stack hence cannot set break points and going thru first entry points will be really very tedious in project code that runs thru thousands of line.
In same regards is there a way that when I start the program execution via gdb we enable some commands (after some initial breakpoint) - hence when the program starts processing further on it print file name line number without user interaction - something like code flow itself?
Well I want to list lines of code when executing via GDB - like we do
when breakpoint is set and we run 'step'.
You can run step in infinite loop like this:
(gdb) start
Temporary breakpoint 2, main () at ttt123.cpp:23
23 vector<A> v1;
(gdb) while 1
>step
>end

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.

Debugging attached process with gdb - how to escape from a loop

I am debugging code that looks like this:
while (true){
// do something ...
size_t i = foo(); // <- bp set here
if (flag_set) break;
}
// More code follows here ...
I want to break at the foo() function call, invoke it a few times and then jump out of the while loop completely (lets assume that we are guaranteed that the flag will be set - so we can break out of the loop.
How do I break out of the loop completely?. finish simply runs to the next iteration. What I want to do is to exit the current "code chunk" (in this case, the while loop)
You want the advance command, which takes the same arguments as the break command. Using your code as an example (but with line numbers added):
10 while (true){
11 // do something ...
12 size_t i = foo(); // <- bp set here
13 if (flag_set) break;
14 }
15
16 // More code follows here ...
17 someFunction();
Say your original breakpoint on line 12 was breakpoint 1, and after breaking a few times you wanted to skip to line 17, you would type something like:
disable 1
advance 17
which would disable breakpoint 1 (so it doesn't get hit for the rest of the loop) and then keep executing the program until it hit line 17.
Set a breakpoint before the loop. Then cursor to the foo() call, and use Debug|Run to Line. This is so useful that I have dedicated a function key to it.
Set a second breakpoint after the loop. disable the breakpoint inside the loop. cont. enable the breakpoint again.
I don't know of any easier way.
Try using the jump command. Per gdb help, on this system at least:
jump -- Continue program being debugged at specified line or address
What you need is until command. This is the easiest way to avoid stepping through the loop. From gdb manual:
Continue running until a source line past the current line, in the current stack frame, is reached. This command is used to avoid single stepping through a loop more than once. It is like the next command, except that when until encounters a jump, it automatically continues execution until the program counter is greater than the address of the jump.