Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get GDB to do a 'list' after every step?

Tags:

I can step along with gdb, but I have to give the "list" command every time I want to see where I am in source code.

(gdb) next
351     int right = get_variable(right_token, right_id);
(gdb) list
346         op = "<>";
347         right_id = parse_id_or_crash();
348     }
349     Token * right_token = tokens[parser_index - 1];
350     int left = get_variable(left_token, left_id);
351     int right = get_variable(right_token, right_id);
352     if (op == "<")
353         return left < right;
354     if (op == ">")
355         return left > right;

It would be great if gdb would automatically list the source code after every step. It would also be great if gdb could indicate where in the source code I am (like with a "->" or something). Seeing only one line of code at a time makes me a little claustrophobic.

like image 618
thejoshwolfe Avatar asked Dec 06 '10 01:12

thejoshwolfe


People also ask

How do I list breakpoints in GDB?

You can see these breakpoints with the GDB maintenance command `maint info breakpoints' . Using the same format as `info breakpoints' , display both the breakpoints you've set explicitly, and those GDB is using for internal purposes. Internal breakpoints are shown with negative breakpoint numbers.

How do I complete a loop in GDB?

Is there a gdb command to finish a loop construct? Execute until on the last line of the loop, or until NNN where NNN is the last line of the loop. (gdb) help until Execute until the program reaches a source line greater than the current or a specified location (same args as break command) within the current frame.

What does the list command do in GDB?

To print lines from a source file, use the list command (abbreviated l ). By default, ten lines are printed. There are several ways to specify what part of the file you want to print; see Specify Location, for the full list.

What does the next command in GDB allow you to do?

If you want to execute the entire function with one keypress, type "next" or "n". This is equivalent to the "step over" command of most debuggers. If you want gdb to resume normal execution, type "continue" or "c". gdb will run until your program ends, your program crashes, or gdb encounters a breakpoint.


2 Answers

Use gdb TUI mode http://sourceware.org/gdb/onlinedocs/gdb/TUI-Overview.html#TUI-Overview You can enter or leave the TUI mode with C-x A key binding.

like image 96
ks1322 Avatar answered Sep 20 '22 15:09

ks1322


hook-stop

define hook-stop
  l
end

Doc: https://sourceware.org/gdb/current/onlinedocs/gdb/Hooks.html

In addition, a pseudo-command, ‘stop’ exists. Defining (‘hook-stop’) makes the associated commands execute every time execution stops in your program: before breakpoint commands are run, displays are printed, or the stack frame is printed.

Learned from: https://stackoverflow.com/a/8374474/895245

Highlight the current line

This is the only thing missing to completely replace the buggy -tui mode completely.

It is currently not possible without Python scripting: https://sourceware.org/bugzilla/show_bug.cgi?id=21044

With Python scripting, I'm currently using: https://github.com/cyrus-and/gdb-dashboard

See also: How to highlight and color gdb output during interactive debugging?