138

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

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Boris Burkov
  • 13,420
  • 17
  • 74
  • 109
  • 27
    `backtrace` or `where`, even `info line` or just simply `bt` (for backtrace). http://www.dirac.org/linux/gdb/ for a gdb tutorial – dwalter Jan 29 '13 at 11:32
  • `bt` or `backtrace` or `where` print stack of function calls, `f` or `frame` print next line to execute. – Eric Feb 23 '20 at 08:24
  • for others answers, see the same question here : [GDB: How to check current line number during debug](https://stackoverflow.com/questions/16657288/gdb-how-to-check-current-line-number-during-debug) –  Dec 03 '21 at 14:28

5 Answers5

143

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.

user3162307
  • 1,626
  • 2
  • 11
  • 5
43

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

pravu pp
  • 792
  • 7
  • 13
31

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!

Community
  • 1
  • 1
kumar_m_kiran
  • 3,982
  • 4
  • 47
  • 72
  • 1
    Ah, so although my program was compiled with `-g` to contain debug info, I'm currently in a stack frame, taken from one of shared libraries, which apparently wasn't compiled to contain line information. Thanks, kumar. – Boris Burkov Jan 31 '13 at 06:06
  • Comment of the year award. Thank you so much! – Defacto Apr 22 '21 at 18:47
22

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.

albfan
  • 12,542
  • 4
  • 61
  • 80
  • 2
    GDB doesn't support 16-bit mode I believe, so `ip` is never used here. Also, instead of explicitly spelling the name of program counter, you can use GDB's alias for it: `$pc`. So `x/10i $pc` will disassemble 10 instructions at current instruction pointer regardless of architecture — it will work on i386, x86_64, ARM etc.. – Ruslan Aug 17 '16 at 09:23
15

All the other answers are correct, but 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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Mazhar MIK
  • 1,022
  • 12
  • 14