199

Please don't reply I should use ddd, nemiver, emacs, vim, or any other front-end, I just prefer gdb as it is, but would like to see its output with some terminal colors.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
elmarco
  • 31,633
  • 21
  • 64
  • 68
  • 4
    It won't give you colors (so I won't call this an Answer), but some ~/.gdbinit configuration will improve the experience. I use this as a bare minimum: set history save on set print pretty set output-radix 16 set height 0 – activout.se Nov 19 '08 at 19:35
  • Once highlight current line on `l` gets implemented https://sourceware.org/bugzilla/show_bug.cgi?id=21044 , I'll just add `l` to a `hook-stop` and enter Dev Nirvana. – Ciro Santilli OurBigBook.com Aug 02 '17 at 07:38

11 Answers11

208

.gdbinit

You can tweak your ~/.gdbinit to have colors. You can use mammon's .gdbinit which is available here:

https://github.com/gdbinit/gdbinit

You can tweak it as much as you want too. I found this thanks to this SO answer. Here's the kind of output that you can obtain:

.gdbinit

A GitHub repository is also available: https://github.com/gdbinit/Gdbinit

On a side note, the same idea was also applied to lldb.

GDB Dashboard

Following the same concept, GDB Dashboard provides a modular visual interface for GDB in Python.

GDB Dashboard

(void)walker

Another similar project uses GDB's Python support to provide more extensibility, so this is worth checking out: https://github.com/dholm/voidwalker

@dholm also provides his own .gdbinit inspired from the previous one.

(void)walker

pwndbg

Some projects provide a set of useful functions, including improved display. This is the case for PEDA or pwndbg. The latter gives the following description:

A PEDA replacement. In the spirit of our good friend windbg, pwndbg is pronounced pwnd-bag.

  • Speed
  • Resiliency
  • Clean code

It provides commands to support debugging and exploit development similar to the ones from PEDA, and better display (although this is not the main focus of the project). The software is still under development, and has not been properly released yet.

pwndbg

voltron

The project description states:

Voltron is an extensible debugger UI for hackers. It allows you to attach utility views running in other terminals to your debugger (LLDB or GDB), displaying helpful information such as disassembly, stack contents, register values, etc, while still giving you the same debugger CLI you're used to.

You can modify your .gdbinit to automatically integrate it. However, the display itself is outside of GDB (e.g. in a tmux split).

voltron

GEF

GEF is another option, and it is described as:

It is aimed to be used mostly by exploiters and reverse-engineers, to provide additional features to GDB using the Python API to assist during the process of dynamic analysis and exploit development.

GEF

Jeff Trull
  • 1,236
  • 11
  • 16
BenC
  • 8,729
  • 3
  • 49
  • 68
  • many thanks for your answer, do you have an idea how to switch off register output? (i am using gdb for C++ code and need no assembler level right away) – vak Jun 05 '15 at 12:47
  • 1
    @vak did you try `set $SHOWCPUREGISTERS = 0`? Basically you've got [several parameters that you can set](https://github.com/gdbinit/Gdbinit/blob/master/gdbinit#L70-L124), and you can always modify the code to suit your needs. – BenC Jun 13 '15 at 03:50
  • That `~/.gdbinit` file breaks TUI mode, and it doesn't show the C or C++ code line anymore when you run `f` or `frame` :(. – Gabriel Staples Oct 13 '21 at 00:18
106

It's not colours, but consider gdb's text gui. It makes a vast difference to how usable gdb is.

You can launch it with:

gdb -tui executable.out

Screenshot:

enter image description here

As you can see, the main features are:

  • shows what line of the source we are on and surrounding lines
  • shows breakpoints
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
John Carter
  • 53,924
  • 26
  • 111
  • 144
  • 6
    Wow! Thanks! I've been looking for a long time for a nice graphical shell around gdb, and I have tried xxgdb, kgdb and ddd, but none of them worked very good for me, so I've stuck with the plain old command line interface. But this is absolutely perfect! – Thomas Padron-McCarthy Jan 10 '09 at 19:47
  • 54
    Ctrl-x Ctrl-a: Enter this to switch to and from the text gui mode, works even without the command line option. – jturcotte Nov 05 '10 at 14:10
  • 3
    Printing to stdout from the program breaks the interface for me. Any workarounds besides redirecting it? – Ciro Santilli OurBigBook.com May 09 '15 at 09:25
  • 2
    I get the same problem with stdout breaking the interface. Ctrl-L or whatever your redraw binding is makes it usable at least. For people with vi editing mode enabled, Ctrl-X Ctrl-A doesn't work, but the command 'layout src' will put you in TUI mode with the source shown like the image. – wilywampa May 28 '15 at 03:41
  • @wilywampa And to leave tui with vim bindings, `tui disable` will be added soon: http://stackoverflow.com/a/30763033/895245 – Ciro Santilli OurBigBook.com Jun 23 '15 at 09:47
  • 3
    Open a second terminal and then issue the command: $ tty Use the result to direct stdout from the gdb session to that terminal using the (gdb) set inferior-tty command. ex. from my .gdbinit set inferior-tty /dev/tty2 now your stdout will not mess up $gdb -tui. – netskink Jan 18 '16 at 00:02
  • @jturcotte: Or `layout reg` to switch to this mode with registers and disassembly showing. – Peter Cordes Apr 11 '20 at 17:26
  • 1
    These are also referred as the `layouts` in gdb, to see other layout options use `help layout`, available options are: `layout src` (this answer), `layout asm`, `layout split`, `layout regs`. – lol Apr 30 '20 at 09:19
  • There's probably a `gdbtui` command installed if you don't want to bother with the option syntax – cassepipe Apr 08 '23 at 17:06
46

I know you did not want a frontend. But how about cgdb it is very close to gdb, it is textmode but has a source window above with syntax highlight on the code.

Justin M. Keyes
  • 6,679
  • 3
  • 33
  • 60
Johan
  • 20,067
  • 28
  • 92
  • 110
  • 2
    sudo apt-get install cgdb – cs01 Feb 24 '16 at 18:29
  • Just tried running it: it doesn't see any gdb history, and it also has a screwed up prompt, where there is some whitespace between the caret and the actual place where symbol being typed *(this is likely because I have [a colored prompt in gdb](https://github.com/Hi-Angel/dotfiles/blob/eac639ee200f0203f58028a244ba157a5b4373a5/.gdbinit#L67))*. Not impressed at all. – Hi-Angel Jun 10 '19 at 11:59
  • @Hi-Angel I guess cgdb isn’t using [GNU Readline](https://en.wikipedia.org/wiki/GNU_Readline) or any other line-editing library. If that’s the case, then it won’t have decent prompt. Another powerful functionality provided by line-editor is to jump to previous word (`Alt` `f` by default for Readline). By the way, it doesn’t correctly shows color on my terminal, but some garbage like `[?2004h` instead. – Franklin Yu Jul 22 '20 at 01:16
  • 1
    According to [GDB Wiki](https://sourceware.org/gdb/wiki/GDB%20Front%20Ends#Using_the_old_.28deprecated.29_annotations_mechanism_.28please_fix_them.21.29), cgdb is using a *deprecated* mechanism to interact with GDB. – Franklin Yu Jul 22 '20 at 01:25
  • 1
    Oh, about the terminal output garbage it’s [known issue not fixed](https://github.com/cgdb/cgdb/issues/189). Sadly maintainer didn’t reply this issue. – Franklin Yu Jul 22 '20 at 01:45
23

New in upcoming GDB 8.3!

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=gdb/NEWS

Terminal styling is now available for the CLI and the TUI. GNU Source Highlight can additionally be used to provide styling of source code snippets. See the "set style" commands, below, for more information.

screenshot of gdb 8.2.91.20190401-23.fc30

elmarco
  • 31,633
  • 21
  • 64
  • 68
  • 1
    note your machine need at least **512MB** of RAM, otherwise, the `gcc` compiler will start thrashing. – Evandro Coan Jul 03 '19 at 18:47
  • Note: gdb must have been compiled with this feature enabled. If you happen to be on Gentoo something like this ```echo 'sys-devel/gdb source-highlight' >> /etc/portage/package.use/my.use && emerge sys-devel/gdb``` is what is needed to recompile with it enabled. – Chris Jul 25 '21 at 18:29
  • 2
    Note: compiling with source-highlight is only necessary for *source code* styling, the rest of the interface is colored by default since GDB 8.3. Note also that [since GDB 10.1](https://github.com/bminor/binutils-gdb/commit/f6474de9aacec990d44d2d65a337668b389efd99), you get source highlighting if you have the Pygments Python package installed. No need to have GDB compiled with source-highlight! – philb Sep 03 '21 at 14:04
  • Crap, RHEL 8 ships with 8.2 by default xD – Dan M. May 16 '22 at 16:38
17

It is possible to greatly enhance the appears of gdb through the use of colors. This is done via any of the following methods:

  1. Colorized prompt via the "set prompt". E.g., make the prompt bold and red:

    set prompt \033[1;31m(gdb) \033[m

    or make the prompt a new shape, bold and red:

    set prompt \033[01;31m\n\n#####################################> \033[0m

    enter image description here

  2. Colorized commands via hooks

  3. Colorized syntax highlighting of the "list" command.

All examples are available at the following blog posts written by Michael Kelleher:

"Beautify GDB", May 12, 2010 (via archive.org)

"Experimental GDB syntax highlighting", May 15, 2010 (via archive.org)

Scott Yang
  • 339
  • 3
  • 6
Mike
  • 733
  • 1
  • 7
  • 10
  • 9
    @Mike: it would be useful to post the contents of those links here as the site is no longer accessible and robots.txt prevented archive.org from indexing it. – Lucian Adrian Grijincu Apr 27 '12 at 18:35
  • 1
    You can get the relevant information here: https://sourceware.org/gdb/current/onlinedocs/gdb/Prompt.html – musiphil Sep 14 '13 at 00:35
  • 1
    The links are now pointed to an archive.org cache of the blog posts. – Alex Quinn Sep 24 '14 at 20:50
  • 1
    @Mike, it appears that you're the author of the blog posts; if so, you should disclose that in the answer. – David Z Feb 20 '15 at 01:14
  • For minimalist answer better use `set prompt \1\033[1;31m\2(gdb) \1\033[m\2` otherwise line editing on the prompt will be broken. – catpnosis Oct 13 '18 at 10:05
7
#into .gdbinit
shell mkfifo /tmp/colorPipe

define hook-disassemble
echo \n
shell cat /tmp/colorPipe | c++filt | highlight --syntax=asm -s darkness -Oxterm256 &
set logging redirect on
set logging on /tmp/colorPipe
end 

define hookpost-disassemble
hookpost-list
end 

define hook-list
echo \n
shell cat /tmp/colorPipe | c++filt | highlight --syntax=cpp -s darkness -Oxterm256 &
set logging redirect on
set logging on /tmp/colorPipe
end 

define hookpost-list
set logging off 
set logging redirect off 
shell sleep 0.1s
end 

define hook-quit
shell rm /tmp/colorPipe
end 

define re
hookpost-disassemble
echo \033[0m
end 
document re
Restore colorscheme
end 

Warning: Buggy. No TUI support, 'user-mode' hack.

Found the main part here and modified it a bit. Needs highlight, c++filt. If colors get messed up issue re command.

ftk
  • 71
  • 1
  • 2
7

cgdb is much better than gdb -tui

justin.yqyang
  • 97
  • 1
  • 3
  • Agreed. Simple lines printed by my program trashed gdb -tui's console. This does not happen under cgdb. Thanks for the tip! – Randall Cook Sep 10 '14 at 23:07
  • As a vim user I beg to differ. If you have configured `gdb` to work with readline's vi mode in your `~/.inputrc` you have a nice command line vim mode experience with `j` and `k` to go up and down history whereas in cgdb the vi navigates the source code IIRC – cassepipe Apr 08 '23 at 17:05
5

Neat, I just found this hack using colout: https://github.com/nojhan/colout/blob/master/colout/example.gdbinit

before - after

elmarco
  • 31,633
  • 21
  • 64
  • 68
4

I wanted to highlight as follows: emphasise the lines of a stack trace which belong to my source files (rather than libraries).

The solution was to use gdb-python (on MSYS; on Linux typically gdb comes with Python built-in already?), hook backtrace, use

python stack_trace = gdb.execute('backtrace', False, True')

Then process stack_trace with Python's regexes, and print them out. Bold and other colours are achieved by a function like this:

def term_style(*v):
    """1 is bold, 30--37 are the 8 colours, but specifying bold may also
    change the colour. 40--47 are background colours."""
    return '\x1B['+';'.join(map(str, v))+'m'

#Use like this:
print term_style(1) + 'This will be bold' + term_style(0) #Reset.
print term_style(1,30) + 'This will be bold and coloured' + term_style(0)
print term_style(1,30,40) + 'Plus coloured background' + term_style(0)
Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124
4

Another good combination of colors is given by this configuration. It renders inspecting the backtraces a lot easier. To use it, just save that file as ~/.gdbinit and run gdb normally

Andrea Araldo
  • 1,332
  • 14
  • 20
  • Thanks, this was precisely what i was looking for. I was looking into a multi-threaded application with a long call stack and this is perfect for those backtraces. – Johan Bjäreholt Jan 29 '18 at 14:05
-3

you can get whatever colors you want;

# gdb
(gdb) shell echo -en '\E[47;34m'"\033[1m"
...
anything is now blue foreground and white background
...
(gdb) shell tput sgr0
... back to normal
RandomNickName42
  • 5,923
  • 1
  • 36
  • 35