It's called the TUI (no kidding). Start for example with gdbtui or gdb -tui ...


Please also see this answer by Ciro Santilli. It wasn't available in 2012 to the best of my knowledge, but definitely worth a look.

Answer from 0xC0000022L on Stack Overflow
🌐
Apple Developer
developer.apple.com › library › archive › documentation › DeveloperTools › gdb › gdb › gdb_8.html
Debugging with gdb - Examining Source Files
For example, we can use info line to discover the location of the object code for the first line of function m4_changequote: (gdb) info line m4_changequote Line 895 of "builtin.c" starts at pc 0x634c and ends at 0x6350.
🌐
zakuarbor
zakuarbor.github.io › blog › gdb-dissassemble-src
GDB - Mix Source Code with Assembly
May 9, 2022 - (gdb) help disassemble Disassemble a specified section of memory. Usage: disassemble[/m|/r|/s] START [, END] One can use either the /r or the /s option to view the assembly and C source code mixed together in the output.
🌐
University of Michigan
web.eecs.umich.edu › ~sugih › pointers › gdbQS.html
gdb QuickStart
To view the source code, type "list" or "l". gdb will print out the source code for the lines around the current line to be executed. To view other lines, just type "list [linenumber]", and gdb will print out the 20 or so lines around that line.
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Machine-Code.html
Machine Code (Debugging with GDB)
Here is an example for AMD x86-64 showing the difference between /m output and /s output. This example has one inline function defined in a header file, and the code is compiled with ‘-O2’ optimization. Note how the /m output is missing the disassembly of several instructions that are present in the /s output. ... (gdb) disas /m main Dump of assembler code for function main: 5 { 6 x = foo (y); 0x0000000000400400 <+0>: mov 0x200c2e(%rip),�x # 0x601034 <y> 0x0000000000400417 <+23>: mov �x,0x200c13(%rip) # 0x601030 <x> 7 return 0; 8 } 0x000000000040041d <+29>: xor �x,�x 0x000000000040041f <+31>: retq 0x0000000000400420 <+32>: add �x,�x 0x0000000000400422 <+34>: jmp 0x400417 <main+23> End of assembler dump.
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Source.html
Source (Debugging with GDB)
GDB can print parts of your program’s source, since the debugging information recorded in the program tells GDB what source files were used to build it. When your program stops, GDB spontaneously prints the line where it stopped. Likewise, when you select a stack frame (see Selecting a Frame), GDB prints the line where execution in that frame has stopped.
🌐
Red Hat
docs.redhat.com › en › documentation › red_hat_developer_toolset › 12 › html › user_guide › chap-gdb
Chapter 8. GNU Debugger (GDB) | User Guide | Red Hat Developer Toolset | 12 | Red Hat Documentation
Before you start the execution of the program you are debugging, gdb displays the first ten lines of the source code, and any subsequent use of this command lists another ten lines.
Find elsewhere
🌐
Brown
cs.brown.edu › courses › cs033 › docs › guides › gdb.pdf pdf
CSCI0330 Intro Computer Systems Doeppner gdb Cheatsheet Fall 2018
Steps through a single line of code. Steps over function calls. ... Breakpoint 1 at 0x8049377: file main.c, line 34. ... Steps through a single x86 instruction. Steps over calls​. ... Kills the current debugging session. ... Prints a stack trace, listing each function and its arguments. This does the same thing as the ... Prints a stack trace, listing each function and its arguments. This is the same as the commands ... Quits​ gdb.
🌐
Richard Johnsonbaugh
condor.depaul.edu › glancast › 373class › docs › gdb.html
Quick Gdb Guide
List lines of source code centered around a particular line. Example. List the lines centered around line 41. ... Show the next statement that will be executed. (gdb) where #0 mystrcpy (copyto=0x259fc6c "*", copyfrom=0x259fddc "ABC") at printch.cpp:27 #1 0x4010c8 in main (argc=3, argv=0x25b0cb8) at printch.cpp:40
🌐
Opensource.com
opensource.com › article › 21 › 3 › debug-code-gdb
Learn to debug code with the GNU Debugger | Opensource.com
Troubleshoot your code with the GNU Debugger. Download our new cheat sheet. ... The GNU Debugger, more commonly known by its command, gdb, is an interactive console to help you step through source code, analyze what gets executed, and essentially reverse-engineer what's going wrong in a buggy application.
🌐
Darkdust
darkdust.net › files › GDB Cheat Sheet.pdf pdf
Running # gdb
Start GDB and attach to process. ... Run the program to be debugged. ... Kill the running program. ... Set a new breakpoint. ... Remove a breakpoint. ... Delete all breakpoints. ... Enable a disabled breakpoint. ... Disable a breakpoint. ... Set a new watchpoint. ... Like breakpoints. ... Break/watch the named function. ... Show ...
🌐
USC CS and ECE
bytes.usc.edu › cs104 › wiki › gdb
GDB Cheat Sheet
layout next From the begining of GDB, entering ‘layout next’ once the program is running will show you source code around your current location in the program. This view can be helpful to those who are new to gdb, and especially helpful when working with source code you are not farmiliar with.
🌐
GitHub
gist.github.com › 8b52e2cf4142c35ba6de
GDB commands by function - simple guide · GitHub
gdbcomm.txt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters · Show hidden characters ·
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › List.html
List (Debugging with GDB)
Print lines centered around the line or lines of all the code locations that result from resolving locspec. ... Print lines from first to last. Both arguments are location specs. When a list command has two location specs, and the source file of the second location spec is omitted, this refers to the same source file as the first location spec. If either first or last resolve to more than one source line in the program, then the list command shows the list of resolved source lines and does not proceed with the source code listing.
Top answer
1 of 3
16

Use:

(gdb) list FUNCTION

See the online help of the list command for details:

(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.

For any non-toy projects, you'll probably hit a case like this:

$ gdb /bin/true
<...>
(gdb) start
<...>
(gdb) list printf
file: "/usr/include/bits/stdio2.h", line number: 104
file: "printf.c", line number: 29

Which lists the multiple definitions of a function in a code base. In the printf() case above, the non-overloaded pure C function has two definitions. One defined in stdio2.h. You can then use the list FILE:LINENUM form to specify which one you want to list:

(gdb) list printf.c:29
24  
25  /* Write formatted output to stdout from the format string FORMAT.  */
26  /* VARARGS1 */
27  int
28  __printf (const char *format, ...)
29  {
30    va_list arg;
31    int done;
32  
33    va_start (arg, format);

For the "sourcefile.c: No such file or directory" errors you're seeing, you need to tell GDB where to look for the source code. See GDB Manual: Source Path. Obviously you'll need to actually have the source code for the function you want to list on your machine.

2 of 3
1

For gcc:

Add a debugging option flag -g with the compilation of your source:

gcc -g test.c

To test, use:

gdb ./a.out
(gdb) list

For even more functionality, check out the man pages:

man gcc
man gdb
🌐
Uwa
teaching.csse.uwa.edu.au › units › CITS2230 › resources › gdb-intro.html
A Quickstart Guide to Debugging C Programs with gdb
You can display your source code inside gdb using the l (or list) command. gdb will print ten lines of source code at a time, with a line number at the start of each line. You can give the list command a line number or function name to tell gdb where to start.
🌐
Reddit
reddit.com › r/asm › can't see the source code of my assembly program with gdb
r/asm on Reddit: Can't see the source code of my assembly program with GDB
October 5, 2022 -

Hello reddit, I am trying to git into the world of assembly language but I am in a bit of a pickle regarding gdb

I am compiling assembly code that has calls to C functions on it. I am using nasm and gcc to compile the entirety of my code. However, when I try to debug it using GDB I can't see my source code (I get Source not available).

I have installed:

- gcc :(GCC) 12.2.0

- ld :GNU ld (GNU Binutils) 2.39.0

- NASM version 2.15.05

And I run:
nasm -f elf64 -o hello.o hello.asm
gcc -g hello.o -o hello -no-pie
gdb ./hello

I would like to see my assembly code and go step by step, just like one would with C code.

What am I missing? No matter what I do I keep getting the no source error