Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigate using function call stack in gdb

In Visual Studio, if you click on an entry in the call stack, that opens editor and shows you the source code for that function. Is something similar possible in gdb? I use tui (text user interface) in gdb. Is it possible to make tui show source code for a given entry in backtrace?

If not, then how do you make use of information in backtrace? Do you manually open the files and navigate to correct line?

like image 757
The Vivandiere Avatar asked Jul 19 '16 01:07

The Vivandiere


People also ask

How do I go to a specific function in GDB?

To execute one line of code, type "step" or "s". If the line to be executed is a function call, gdb will step into that function and start executing its code one line at a time. If you want to execute the entire function with one keypress, type "next" or "n".

Can you call functions in GDB?

To call a function in the program, GDB has to temporarily modify the state of the inferior. This has potentially undesired side effects. Also, having GDB call nested functions is likely to be erroneous and may even crash the program being debugged.

What are the three Commands used in GDB to examine and move within the stack?

Able to view and traverse the function call stack using the where, up, down and frame commands.

How do you read a call stack?

Call stack is set of lines, which is usually read from top to bottom - meaning moving from current locations to callers. The bottom line was executed first. The top line is executed last and it is the current routine.


1 Answers

When you stopped with gdb (in any mode) in breakpoint and can see backtrace with backtrace or where commands, use up and down commands to focus on different functions (frames) of backtrace.

You may use up 2 to go two frames upper. list command will show you source lines around current frame.

I think, tui will change current displayed function/registers after up/down commands; and there can be no point-and-click backtrace in tui (is there any support of mouse in tui?). Only documented windows of tui are https://sourceware.org/gdb/onlinedocs/gdb/TUI-Commands.html

source, assembly, and command windows.

There are keys to change current frame in TUI, but not in the normal TUI mode (https://sourceware.org/gdb/onlinedocs/gdb/TUI-Keys.html), so you can use text commands of gdb.

There is also "TUI Single Key Mode", activated by Ctrl-x s, and there are up/down commands in this mode: u/d and w to get backtrace. The mode is documented at https://sourceware.org/gdb/onlinedocs/gdb/TUI-Single-Key-Mode.html#TUI-Single-Key-Mode:

25.3 TUI Single Key Mode

 w    where
 u    up
 d    down
 r    run
 s    step
 n    next
 c    continue
 f    finish
 q    exit the SingleKey mode.
 v    info locals

Other keys temporarily switch to the gdb command prompt. The key that was pressed is inserted in the editing buffer so that it is possible to type most gdb commands without interaction with the TUI SingleKey mode. Once the command is entered the TUI SingleKey mode is restored. The only way to permanently leave this mode is by typing q or C-x s.

You may also try some debugger with GUI (gnu ddd or KDbg), or any other gdb wrapper builtin in most Linux IDEs (list, wiki list: Eclipse, Netbeans, CLion, KDevelop, Code::Blocks, CodeLite, ...). They all are more modern and convenient for debugging.

like image 159
osgx Avatar answered Oct 14 '22 08:10

osgx