info commands:
- info locals
- info source
- info sources
- info stack
- info target
- info threads
- info variables
breakpoints:
- breakpoint <file:line>
- disable <bp#>
- enable <bp#>
- delete <bp#>
primary commands
- l - list some lines around current
- p - print a variable
- c - continue
- run
- s - single step (step into)
- n - next (step over)
- finish - step till stack frame returns
- <ret> - re-issue last command
- kill - kill child process being debugged
- frame n - inspect variables in stack frame n
To have source code available:
apt install cgdb
To start a gdb session with parameters:
cgdb --args executablename arg1 arg2 arg3
A sample session with a console program:
(gdb) break main Breakpoint 1 at 0x16e9: file /var/local/rpb/projects/pw/src/decrypt.cpp, line 21. (gdb) run Starting program: /var/local/rpb/projects/pw/debug/src/decrypt [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Breakpoint 1, main (argc=1, argv=0x7fffffffe008) at /var/local/rpb/projects/pw/src/decrypt.cpp:21 (gdb) n (gdb) n file name required (gdb) n [Inferior 1 (process 2589) exited with code 01]
In cgdb, use 'esc' to switch to code window. Use 'i' to switch back to gdb window.
'bt 5' to see the last 5 entries in stack
'catch throw' to catch an exception, then use backtrace to view stack entries
cgdb may require a TERM= setting to show colours in some sessions (diagnostic notes in Can't see colors in CGDB), and additionally, ncurses may need to be installed:
- TERM=xterm # previous versions
- TERM=xterm-256color #to get colors
- TERM=screen-256color #to get colors in screen
- TERM=linux # may or may not work
When building, need the -g parameter:
gcc -g test.cpp -l OpenCL -o test
2023/02/09
- In VSCode, in the 'Debug Console', use -exec to prefix and execute gdb commands
- GNU GDB Debugger Command Cheat Sheet