LilyPond Contributor's Guide: 10.7.2 Debugging C++ code

### 10.7.2 Debugging C++ code

The GNU debugger, gdb, is the principal tool for debugging C++ code.

### Compiling LilyPond for use with gdb

In order to use gdb with LilyPond, it is necessary to compile LilyPond with debugging information. This is the current default mode of compilation. Often debugging becomes more complicated when the compiler has optimised variables and function calls away. In that case it may be helpful to run the following command in the main LilyPond source directory:

>     ./configure --disable-optimising
>     make

This will create a version of LilyPond with minimal optimization which will allow the debugger to access all variables and step through the source code in-order. It may not accurately reproduce bugs encountered with the optimized version, however.

You should not do make install if you want to use a debugger with LilyPond. The make install command will strip debugging information from the LilyPond binary.

### Typical gdb usage

Once you have compiled the LilyPond image with the necessary debugging information it will have been written to a location in a subfolder of your current working directory:

>     out/bin/lilypond

This is important as you will need to let gdb know where to find the image containing the symbol tables. You can invoke gdb from the command line using the following:

>     gdb out/bin/lilypond

This loads the LilyPond symbol tables into gdb. Then, to run LilyPond on ‘`test.ly`’ under the debugger, enter the following:

>     run test.ly

at the gdb prompt.

As an alternative to running gdb at the command line you may try a graphical interface to gdb such as ddd:

>     ddd out/bin/lilypond

You can also use sets of standard gdb commands stored in a .gdbinit file (see next section).

### Typical .gdbinit files

The behavior of gdb can be readily customized through the use of a .gdbinit file. A .gdbinit file is a file named .gdbinit (notice the “.” at the beginning of the file name) that is placed in a user’s home directory.

The .gdbinit file below is from Han-Wen. It sets breakpoints for all errors and defines functions for displaying scheme objects (ps), grobs (pgrob), and parsed music expressions (pmusic).

>     file $LILYPOND_GIT/build/out/bin/lilypond
>     b programming_error
>     b Grob::programming_error

>     define ps
>        print ly_display_scm($arg0)
>     end
>     define pgrob
>       print ly_display_scm($arg0->self_scm_)
>       print ly_display_scm($arg0->mutable_property_alist_)
>       print ly_display_scm($arg0->immutable_property_alist_)
>       print ly_display_scm($arg0->object_alist_)
>     end
>     define pmusic
>       print ly_display_scm($arg0->self_scm_)
>       print ly_display_scm($arg0->mutable_property_alist_)
>       print ly_display_scm($arg0->immutable_property_alist_)
>     end

#### LilyPond — Contributor’s Guide v2.22.2 (stable-branch).