gdb: how to print the current line or find the current line number?

DebuggingGccGdb

Debugging Problem Overview


list commands prints a set of lines, but I need one single line, where I am and where an error has probably occurred.

Debugging Solutions


Solution 1 - Debugging

The 'frame' command will give you what you are looking for. (This can be abbreviated just 'f'). Here is an example:

(gdb) frame
\#0  zmq::xsub_t::xrecv (this=0x617180, msg_=0x7ffff00008e0) at xsub.cpp:139
139	        int rc = fq.recv (msg_);
(gdb)

Without an argument, 'frame' just tells you where you are at (with an argument it changes the frame). More information on the frame command can be found here.

Solution 2 - Debugging

Command where or frame can be used. where command will give more info with the function name

Solution 3 - Debugging

I do get the same information while debugging. Though not while I am checking the stacktrace. Most probably you would have used the optimization flag I think. Check this link - something related.

Try compiling with -g3 remove any optimization flag. Then it might work. HTH!

Solution 4 - Debugging

Keep in mind that gdb is a powerful command -capable of low level instructions- so is tied to assembly concepts.

What you are looking for is called de instruction pointer, i.e:

>The instruction pointer register points to the memory address which the processor will next attempt to execute. The instruction pointer is called ip in 16-bit mode, eip in 32-bit mode,and rip in 64-bit mode.

more detail here

all registers available on gdb execution can be shown with:

(gdb) info registers

with it you can find which mode your program is running (looking which of these registers exist)

then (here using most common register rip nowadays, replace with eip or very rarely ip if needed):

(gdb)info line *$rip

will show you line number and file source

(gdb) list *$rip

will show you that line with a few before and after

but probably

(gdb) frame

should be enough in many cases.

Solution 5 - Debugging

All the answers above are correct, What I prefer is to use tui mode (ctrl+X A or 'tui enable') which shows your location and the function in a separate window which is very helpful for the users. Hope that helps too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoris BurkovView Question on Stackoverflow
Solution 1 - Debugginguser3162307View Answer on Stackoverflow
Solution 2 - Debuggingpravu ppView Answer on Stackoverflow
Solution 3 - Debuggingkumar_m_kiranView Answer on Stackoverflow
Solution 4 - DebuggingalbfanView Answer on Stackoverflow
Solution 5 - DebuggingMazhar MIKView Answer on Stackoverflow