Search code examples
c++gdb

Configure gdb tui to use c++ style for specific file extension


My company uses C++ in a custom framework. Because reasons, some c++ code resides in files with extension .foo. Using gdb on those files is supported (I think some work by somebody smarter than me was needed, but it is done) - I can use gdb and it steps fine through those files.

Now I like to use gdb tui mode. I know tui can colorize c++ code. In fact, in our project it does colorize c++ code as long as the code is in .h file, but it doesn't colorize code from .foo files.

How do I tell gdb that .foo files are C++ code and please colorize accordingly?

I saw https://sourceware.org/gdb/current/onlinedocs/gdb.html/Output-Styling.html. I tried following:

(gdb) show style sources
Source code styling is enabled.
(gdb) show style tui-current-position
Undefined show style command: "tui-current-position". Try "help show style".

I have gdb 11.2-1


Solution

  • GDB 11.2 can be built with support for the GNU Source Highlight library. If you (or your team) have modified GDB then you must also be building GDB. If you install the GNU Source Highlight development package before building GDB then GDB should pick this up.

    The benefit of doing this is that GNU Source Highlight, highlights source files based on their declared language in the debug information, so you should automatically get C++ styling.

    If the above is not possible then, as ssbssa said in a comment, you must be using the Python Pygments library for styling. It is possible to update Pygments so that it knows that *.foo files should be highlighted as C++.

    To do this you must first find your Python Pygments install, and locate the file pygments/lexers/c_cpp.py, inside this file you'll find some code a little like this:

        filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++',
                     '*.cc', '*.hh', '*.cxx', '*.hxx',
                     '*.C', '*.H', '*.cp', '*.CPP']
    

    Update this by adding , '*.foo' to the end so you have:

        filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++',
                     '*.cc', '*.hh', '*.cxx', '*.hxx',
                     '*.C', '*.H', '*.cp', '*.CPP', '*.foo']
    

    Now, next to the c_cpp.py file you should find file called _mapping.py. This files caches the extension to lexer mapping, and needs to be regenerated. Do this by running the _mapping.py file, like python3 ./_mapping.py.

    With that done, restart GDB, and hopefully, *.foo files should now be highlighted as C++.

    If you are unable to edit the lexer files because of permissions, then download and install the Pygments package into your home directory, and then set PYTHONPATH so that GDB finds your local install of the library before the system install, you're then free to edit your local install as needed.