This project explores migrating GDB to C++ as implementation language. GDB is currently written in C90.

Rationale

The goal for moving to C++ is to make GDB more robust, easier to maintain, and to lower barriers for newcomers.

GDB is already written in poor-man's C++-in-C. Examples:

In most cases, GDB's implementation of these features is inferior to that of the C++ compiler. Its exceptions are slower. Its data structures have less type-safety. Both cleanups and TRY_CATCH are error-prone in practice. We have run into various situations where errors could have been prevented through the use of better type-safety -- e.g., wrapper classes for the CU- versus section-offset case.

A gradual move to C++ would enable us to fix these problems.

We should be reasonably conservative in our choices of C++ constructs to use. We can also see what the GCC community came up with here -- our needs are a little different, but probably not drastically so.

Scope

We'd like to convert everything under src/gdb/ to C++, including GDBserver. Vectors, exceptions and cleanups are all shared between GDB and GDBserver, and all are things we would like to convert. More code will become shared as the Common and LocalRemoteFeatureParity projects progress.

Use of major C++ libraries, like glibmm, qt, boost, etc. are out of scope.

Requirements

Status

(2015/02) - Consensus among maintainers obtained. Plan marching ahead.

(2014/09) - Need to gather consensus amongst maintainers.

Coding conventions

We propose following GCC's C++ coding conventions, diverging only on finer detail, as we find necessary.

See these documents, which have extensive detail:

A point where we'll want to diverge is in permitting the use of C++ exceptions, because GDB already makes use of exceptions and RAII.

Transition plan

  1. Gather consensus amongst maintainers.
  2. Encourage new GDB contributions/patches to be valid C++.
  3. Add a --enable-build-with-cxx option (default: no) to compile GDB with a C++ compiler, keeping the support to build with a C compiler, and use the g++ -fpermissive option.
  4. Modify GDB so it can be compiled in C++ mode with G++'s -fpermissive option.
  5. Split the TRY_CATCH macro into TRY/CATCH macros to better map to C++'s 'throw'/'try...catch' in C++ mode, and eliminate all the volatile gdb_exception objects.
  6. Modify GDB so it can be compiled in C++ mode without G++'s -fpermissive option.

  7. Start compiling GDB with -Wc++-compat in C mode, to ensure C++ mode doesn't regress.
    • Test on a sufficient number of hosts. Make sure --enable-targets=all builds.

  8. Require a C++ compiler.
  9. Change the codebase to use C++'s native features.
    • Change the exception handling system to use C++ exceptions behind the scenes.
      • Reimplement throw_exception/TRY_CATCH with 'throw'/'try...catch' in C++ mode, letting cleanups co-exist while they're not completely phased out. Destructors of locals won't be called automatically until this step is done. (Note: this step may be done before a C++ compiler is required.)
    • Convert C-style inheritance to true C++ inheritance
      • struct breakpoint_ops, struct target_ops, struct varobj, etc.
    • Progressively convert cleanups to RAII.
    • Use standard containers and algorithms

      • (Note GCC hasn't eliminated VEC completely, but converted it into a template; consider importing that.)
    • Once all cleanups are converted/eliminated, we can remove the TRY/CATCH wrappers, making all code use 'throw'/'try...catch' directly.
      • (note: requires eliminating prepare_to_throw_exception too, fixing up immediate_quit/QUIT handling.)

Concerns raised, and answers

False: see Tom's code size experimentations

Tom's experimentations with different C++ compiler options addressed this. In particular, we have the -static/-static-libstdc++/-static-libgcc options as well as --enable-sjlj-exceptions, which all should allow building a lean self-contained in-process agent (which shared code with GDBserver) that doesn't force much into the target process, as well as providing an option for deploying a self-contained, lean GDBserver for constrained/embedded environments with possibly no shared C++ runtime deployed.

Android was one such example raised. C++ exceptions wasn't really supported by the Android SDK, at the time, but things may have moved on since. It is now easier to use exceptions there nowadays. See e.g., Do the Android NDK toolchain binaries build with or without exceptions by default? .

Older supported Unix hosts, particularly ones that might not have GCC available used to be a concern here, but GDB has been removing support for them anyway. E.g., IRIX and True64 are going away: GDB dropping support for mips-irix and alpha-tru64.

Plus, if we leave any such host behind, older GDB's will always be available if someone needs to debug something on them.

Resources

Branches and patches

A branch to build GDB in C++ mode, with -fpermissive:

git@github.com:palves/gdb.git palves/cxx-conversion-attempt

Browse commits here.

A branch that is based on the first, that fixes all -fpermissive errors/warnings, until GDB builds cleanly with -Werror:

git@github.com:palves/gdb.git palves/cxx-conversion-attempt-part-2-no-fpermissive

Browse commits here.

Pedro's scripts used for the automated conversions (based on Tromey's), are here (web version), or:

git@github.com:palves/gdb-refactoring-scripts.git master

To try them out, configure with --enable-build-with-cxx and --disable-werror, or just --enable-build-with-cxx and then build with cd gdb; make WERROR_CFLAGS="".

Note: The branches pass regression tests on x86_64 Fedora 20. The branches don't link with --enable-targets-all yet.

Development Strategy

While conversion is essential, doing so in a manner that limits disruption is important. To that end, we suggest the following development strategy.

We should be reasonably conservative in our choices of C++ constructs to use. We can also see what the GCC community comes up with here -- our needs are a little different, but probably not drastically so.

Before implementing a change, identify the benefit. Primarily we expect the benefit to be better code adaptability, code writability, or code readibility. However, improvements to memory use, compile time, and run time are feasible.

Prefer to follow the idioms and APIs of the C++ standard library when implementing new abstractions. This approach is most important for abstractions that have equivalents in the standard, but for which using the standard abstraction is undesirable. This approach preserves maximum flexibility in implementation.

Where reasonable, implement the change behind the existing APIs. For example, replace the bodies of an existing macro or function with the new implementation. Test this configuration for both correctness and performance. Send that change as a patch.

Change the uses of the old API to the new API in bite-size patches. A patch that changes every file is more disruptive than ten patches changing ten distinct sets of files.


OngoingWork

All content (C) 2008 Free Software Foundation. For terms of use, redistribution, and modification, please see the WikiLicense page.