GDB: How to check current line number during debug - c++

How do I check the current line number that I'm stopped in when debugging with GDB? I would have thought this would be obvious (and maybe it is) but I don't see it on the GDB Cheat Sheet.

Some digging around revealed the following methods:
frame: This command was exactly what I was looking for. Output looked as follows:
(gdb) frame
#0 MyDialog::on_saveButton_clicked (this=0x72bf9e0) at src/ui/dialog/MyDialog.cxx:86
86 _item->save();
(gdb)
where or bt (same effect): This prints out the call stack, ending on the current line.
list *$pc: This doesn't tell you the exact line but it prints out the surrounding lines with the current line in the center.

x/i $eip
eip(rip) points to the next instruction

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.

What is GDB's "here"?

I am trying to troubleshoot a bus error with some inline SSE2 assembly. The source code has a macro that uses 5 pointers, and I suspect one of them is not aligned.
I set a breakpoint on the source line. But when I perform a disass, it disassembles from the top of the function, and not where the debugger is stopped. There are hundreds of lines of assembly, so its not really helpful to me. Pressing ENTER 30 to 40 times in response to "Press ENTER to continue" got old very quickly.
I tried a disass $pc, but it dsassembled from the top of the function. I also tried a disass . (with the dot meaning "here"), but that resulted in:
A syntax error in expression, near `.'.
What does GDB use to denote "here"?
You were correct with the use of $pc to represent the current location. The reason that this did not do what you expected when used with the disassemble command is that the disassemble command tries by default to disassemble the function containing the given address which is what you are seeing.
There are alternative forms that can be given to disassemble, for example start,end where start and end are addresses, or start,+length where start is an address and length is a number of bytes.
Try help disassemble at the gdb prompt for more information.
As an alternative you can also use the x (examine) command to display instructions, without the smart find the beginning of the function behaviour, so x/10i $pc will display 10 instructions starting from $pc. This can be helpful if you only want the instructions disassembled, however you don't have access to the /m or /r modifiers that are available on the disassemble command. These modifiers display interleaved source and assembler (for /m) or the raw instruction bytes (for /r).
Also, if the whole press ENTER to continue thing is getting old then you can try set height 0 to turn off the pager, do make sure that you have enough scroll back in your terminal though :)

How to interpret gdb disassemble output?

I am trying to match the gdb disassemble output (disas [address]) against the source code. I know that such mapping can be done using (gdb) info line *address to find the matching line. However I do not quite understand the format of the output of disassemble. Specifically, what do the following numbers, +4722, and +4281, mean ?
0x00002ad61e45bd02 <+4722>: jmpq 0x2ad61e45bb49 <MsgManager::ForwardMsg(boost::shared_ptr<Channel>, boost::shared_ptr<Msg>, boost::shared_ptr<Context>)+4281>
I am using GNU gdb (GDB) 7.4.1.
Specifically, what do the following numbers, +4722, and +4281, mean
The instruction at address 0x00002ad61e45bd02, which is 4722 bytes from the start of current function (most likely MsgManager::ForwardMsg()) is a jump to address 0x2ad61e45bb49, which is 4281 bytes from the start of MsgManager::ForwardMsg().
You may also find (gdb) disas/m command handy.

gdb - list source of current function without typing its name

In GDB, the command:
list function
will list all the source for the function.
Is there a command that will list all of the source of the function you are currently in, without requiring you to manually type the function name?
(gdb) help list
List specified function or line.
With no argument, lists ten more lines after or around previous listing.
"list -" lists the ten lines before a previous ten-line listing.
One argument specifies a line, and ten lines are listed around that line.
Two arguments with comma between specify starting and ending lines to list.
Lines can be specified in these ways:
LINENUM, to list around that line in current file,
FILE:LINENUM, to list around that line in that file,
FUNCTION, to list around beginning of that function,
FILE:FUNCTION, to distinguish among like-named static functions.
*ADDRESS, to list around the line containing that address.
With two args if one is empty it stands for ten lines away from the other arg.
The *ADDRESS is what is interesting.
On x86/x64 current pointer is in rip register so:
(gdb) list *$pc
0x7ffff7b018a0 is at ../sysdeps/unix/syscall-template.S:82.
77 in ../sysdeps/unix/syscall-template.S
The example is from cat command as I don't have anything with debug info at hand.
When you are stopped in a function type bt for backtrace.
Backtrace will list the current stack. The element at the top, #0, is usually the function you are interested in and the source file and line number is listed also.
For example:
(gdb) bt
#0 myClass::EntityTypeStruct::readAttributes (this=0x7fffd00066e0, buf=0x7fffd0006020 "", len=48)
at /team/project/src/EntityTypeStruct.cc:55
#1 0x000000000044ca86 in workerThread (ts=0x7fffea71dcc0)
at /team/project/src/threads/workerThread.cc:219
#2 0x00007ffff775e9d1 in start_thread () from /lib64/libpthread.so.0
#3 0x00007ffff6c07b5d in clone () from /lib64/libc.so.6
See http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_7.html#SEC42 for more info.
Also, when you set a breakpoint you can specify commands that will run everytime you hit that breakpoint. See http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_6.html#SEC35
So, if you know how many lines are in your function you could set a command to list all source lines of the function:
(gdb) break myClass::EntityTypeStruct::readAttributes
Breakpoint 1 at 0x61ec3b: file /team/project/src/EntityTypeStruct.cc, line 38.
(gdb) commands 1
list 38,104
end
gdbtui can be useful to view the source during debugging.
The 'frame' command shows the function name and the current line location and sets the current line for list to the current executable line.
set listsize 17
frame
list
lists the 8 lines surrounding the current line.

GDB backtrace :Find total number of frame

I am trying to find the start and end of stack using macro from a core file. To accomplish the same I am trying to find the esp value from last and first frame . Difference of the same will give me the stack size in use.
Do we have a way to find number of frames in the stack?
bt give me all the frame . from frame 0 i can find the top of the stack?
do we have a way to find the last frame number ?
Do do we have another way to find the start of the stack and end of the stack ?
Thanks
You can find the frame number of the outermost frame using bt -1:
(gdb) bt -1
#9 0x0000000000464c45 in main (argc=<optimized out>, argv=<optimized out>)
at ../../binutils-gdb/gdb/gdb.c:32
You can see that the outermost frame is #9.
However, scripting this is a bit of a pain. For example, you can't use frame -1 to select that frame.
At this point you have two options.
One option is to use the gdb Python API to iterate over frames and do whatever you like. This is the simplest route, if it is available to you. The Python API is documented and easy to use; in this case you will mostly be interested in the gdb.Frame code.
If you can't use Python, you can use the traditional, horrible gdb hack of using set logging to write the output of bt -1 to a file; then shell to rewrite the contents of this file to be a valid gdb command (like frame 9); and finally source the resulting transformed file.