How to highlight and color gdb output during interactive debugging?

前端 未结 11 1449
南方客
南方客 2020-12-02 03:42

Please don\'t reply I should use ffffd, 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.

相关标签:
11条回答
  • 2020-12-02 03:48

    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)
    
    0 讨论(0)
  • 2020-12-02 03:51

    cgdb is much better than gdb -tui

    0 讨论(0)
  • 2020-12-02 03:56

    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
    0 讨论(0)
  • 2020-12-02 03:58

    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

    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)

    0 讨论(0)
  • 2020-12-02 03:59

    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.

    0 讨论(0)
  • 2020-12-02 04:00
    #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.

    0 讨论(0)
提交回复
热议问题