Clang生成RTTI代码GDB调试问题:DWARF与RTTI类型名不匹配
sc_signal Template Class I’ve run into this exact mismatch between Clang’s DWARF debug info and RTTI type names when debugging polymorphic template classes with enum parameters. Here are several practical ways to fix the issue:
Solution 1: Use Clang’s Enum Identifier Mangling Option (Clang 10+)
Starting with Clang 10, there’s a compiler flag that forces Clang to use the enum’s symbolic name (like SC_ONE_WRITER) instead of its numeric value in RTTI mangled names. This aligns the RTTI type with what’s stored in DWARF debug info, so GDB can match them correctly.
Compile your code with this flag:
clang++ -g -O0 -fenum-enumulator-in-mangling your_source.cpp -o your_program
After compiling with this option, GDB will no longer throw the "RTTI symbol not found" warning and will correctly display the object’s dynamic type and fields.
Solution 2: Workaround for Older Clang Versions
If you’re stuck on a Clang version before 10 (where the above flag doesn’t exist), you can unify the type names by using a constexpr alias for your enum value in the template parameter:
Modify your code like this:
namespace sc_core { enum sc_writer_policy { SC_ONE_WRITER = 0, SC_MANY_WRITERS = 1, SC_UNCHECKED_WRITERS = 3 }; // Add a constexpr alias to use as the default template parameter constexpr sc_writer_policy SC_ONE_WRITER_ALIAS = SC_ONE_WRITER; class sc_object { public: virtual ~sc_object(){} }; // Use the alias instead of the raw enum value as the default template< class T, sc_writer_policy POL = SC_ONE_WRITER_ALIAS> class sc_signal : public sc_object { T dummy_field = 42; }; }
This makes Clang use the alias’s symbolic name in both DWARF and RTTI, eliminating the mismatch. GDB will now recognize the type correctly.
Solution 3: Patch GDB to Treat Enum Values as Equivalent
If you can’t modify your code or compiler flags, you can modify GDB’s source code to recognize that enum numeric values and their symbolic identifiers are equivalent for template parameter matching. This requires compiling GDB from source and adding logic in the type comparison code to handle enum template parameters. While this works, it’s more involved and only recommended if you’re comfortable working with GDB’s codebase.
Why This Happens
The root issue is a discrepancy in how Clang generates debug info vs RTTI:
- Clang stores the symbolic enum name (e.g.,
SC_ONE_WRITER) in DWARF debug info to make debugging more readable. - In older Clang versions, it uses the numeric enum value (e.g.,
(sc_core::sc_writer_policy)0) in RTTI mangled names.
GCC avoids this problem by using the numeric value in both places, but Clang’s approach prioritizes readability in debug info at the cost of RTTI compatibility with GDB. When you use the first or second solution, you force Clang to use the same symbolic name in both DWARF and RTTI, so GDB can match the types without issues.
内容的提问来源于stack exchange,提问作者random




