Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

C++ support in libcc1: A comprehensive update

October 6, 2016
Alexandre Oliva
Related topics:
Developer tools
Related products:
Developer Toolset

    GDB relies on libcc1's GCC and GDB plugins to implement the "compile code" feature, now extended to support the C++ language.

    The Compile and Execute machinery enables GDB users to compile and execute code snippets within the context of an existing process. This allows users to perform inspection and modification of the program state using the target language well beyond the feature set historically exposed by symbolic debuggers. Almost anything that can be expressed in C, and now also in C++, can be compiled, loaded into the running program, and executed on the spot! It is envisioned that this machinery may also be used in the future to speed up conditional breakpoints, and as a foundation for more advanced features such as "Edit and Continue".

    The libcc1 module offers plugins for GDB and GCC that allow GDB to start GCC to compile a user-supplied code snippet. The plugins combine GDB and GCC into a single multi-process program. Through the plugins, GCC can query GDB about the meaning, in the target program, of names encountered in the snippet, and GDB can incrementally inform GCC about variables, functions, types and other constructs present in the program.

    For example, when a GDB user stops a program within a C++ function and enters the command:

    (gdb) compile code
    >   for (auto i = x.begin(); i != x.end(); i++)
    >     if (i->counter > 50)
    >       cout << i->name << end
    

    ... GDB will start an instance of GCC's C++ compiler to compile the code snippet. GCC will find a reference to symbol x, and ask GDB for its meaning. GDB will, in response, proceed to define the type of x, and then the locations (source-code line and run-time memory address) of the variable.

    According to C++ scoping rules, GCC and GDB together might resolve the name to an automatic or static variable in a local scope of the active function, or of an enclosing function; to a member of an enclosing namespace scope; or to a static or non-static data member of an enclosing class.

    Once type and name are defined, GCC can compile calls to the begin and end member functions, to the point of deducing the auto type of i, presumably an iterator type. The debugger supplies enough information for the compiler to behave as if x and its type had been defined in the same translation unit as the code snippet.

    If the iterator type operates, as expected, as a pointer to another type, GDB will have introduced to GCC this other type before defining the iterator type. It will also have defined the operator functions that make the iterator type behave like a pointer. This enables the data members of the other type, namely counter and name, to be referenced through the iterator object i.

    Finally, when GCC encounters cout, and later endl, it will query GDB, that will define their types and locations in the running program, so that the object code generated from the snippet and loaded into the running program will operate on the same cout and endl objects that the program already uses, be they the standard definitions in the ::std namespace, be they anything else in scope at the point in which the program stopped.

    Features

    Although C++ shares a number of features with the previously-supported C language, even some shared features required a significantly different interface in the libcc1 extensions for C++.

    For example, while both languages support tagged types such as struct, union and enum, C's name spaces for tagged types are flat, but C++ supports nesting and separate namespaces, so care has to be taken to define tagged types in the appropriate scopes, and the ability to separate declaration from definition of class types becomes strictly necessary to support some scenarios involving member classes.

    The newly-introduced interface for C++ support enables GDB (a) to navigate in namespace, class, and function scopes; (b) to forward-declare class types and templates; and (c) to define template specializations; classes; class inheritance; static and nonstatic possibly mutable data members; static, virtual and nonvirtual member functions; and regular, explicit, defaulted and deleted constructors, destructors, and overloaded operators.

    Although member access controls (public, protected and private) are specified for class members, GDB has historically enabled expressions to bypass access controls. We have continued this tradition in libcc1's C++ support. After all, what good would a symbolic debugger be that couldn't inspect private class members? Therefore, access to private and protected members is allowed in the user-supplied code snippet as if the snippet was in a friend function of every class type. This does not affect, however, function template overload resolution and deduction, not even template constructs designed specifically to tell whether a certain member is accessible, a possibility introduced in C++11. (I.e., the code snippet itself may bypass access controls, but that won't enable template function specializations named by it to do so.)

    Reference and closure types, the null pointer type and constant, exception specifications, and types for pointers to data members and to member functions are supported by the libcc1 interface for C++, so GDB can inform GCC about symbols in the user program that involve any of these features.

    Default arguments for functions and templates can involve elaborate expressions in the user program. GDB can construct such expressions incrementally, using expression building interfaces also used to construct the arbitrarily complex expressions that may appear in template function signatures. Alas, not all of these features are currently usable by GDB. Default arguments, for example, are not always represented in debug information, but that is the only source of information besides symbol names that GDB could rely on to reconstruct symbolic information from the running program.

    Other debug information shortcomings have largely guided our approach to templates. Since generic definitions are not represented in debug information, specializations not present in the user program cannot be used in the code snippet, and the libcc1 interface for C++ enables GDB to forward-declare but not to define templates. Definitions are only allowed for full specializations of template classes and functions: implicit and explicit specializations are introduced in the same way through libcc1. This design spares the interface from supporting template class partial specializations and nested templates.

    Unfortunately, absent generic template definitions, we cannot represent members thereof. Thus, if the user program declares a member of a template class as a friend, the library client has to emulate this by declaring the corresponding member of each specialization of the template class as a friend instead. We have arranged for GCC to emit debug information that makes all specializations of a template that are denoted by a friend declaration to appear as if they had been individually declared as friends, so that GDB is spared of this emulation, and of telling apart cases in which the language distinguishes the friendship of e.g. a member of a member class of implicit versus explicit specializations of a template class, without information about specialization implicitness in current debug information.

    Another design decision that might surprise clients of the libcc1 interface for C++ is the treatment given to constructors and destructors. For each source-level constructor or destructor, C++ compilers may define up to four separate entry points, each for slightly different use cases. Since defining a member function through the libcc1 interface amounts to little more than telling GCC the address of the member function's entry point in the running program, the debugger needs some way to inform GCC about the address of each of these so-called clones (although they really aren't identical), so that GCC can use the appropriate constructors and destructors, depending on the use case and the available definitions. This is why these member functions are defined through a slightly different API that separates declaration of the source-level member function from definition of each of the corresponding entry points.

    Status

    A work-in-progress implementation of the libcc1 interface for C++ is available in the GCC GIT repository, in the aoliva/libcp1 branch, in the libcc1 directory, mainly in libcp1* files. We aim for inclusion in the upcoming GCC 7.

    The corresponding GDB implementation is also underway, in the GDB GIT repository, branch users/pmuldoon/c++compile, in the gdb/compile directory, mainly in compile-cplus-* files.

    The interface specification and documentation are in the top-level include directory, in header files named gcc-cp-*, in both projects' trees.

    Last updated: February 23, 2024

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.