Home > Article > Backend Development > Detailed explanation of C++ function debugging: How to debug problems in functions in inherited classes?
To debug a function in an inherited class, you can use the following trick: Use gdb's "break" command to set a breakpoint, even if the function is overridden in the derived class. Use gdb's "catch" command to catch exceptions in derived class functions. Use Clang's "-gsplit-dwarf" compilation option to generate separate DWARF debug information entries.
Detailed explanation of C function debugging: debugging functions in inherited classes
Debugging functions in inherited classes in C may cause People find it difficult. This is because debuggers often set breakpoints to base class functions when a function is defined in a base class but called from a derived class. This can make the debugging process difficult because you cannot directly access the overridden functions in the derived class.
To solve this problem, you can use the following techniques:
1. Use gdb's "break" command
gdb's "break" command allows You set a breakpoint even if the function is overridden in a derived class. To use this command, first obtain the address of the derived class function. Then, set a breakpoint using the following syntax:
(gdb) break *0x12345678
where 0x12345678
is the address of the derived class function.
2. Use gdb's "catch" command
gdb's "catch" command is also a useful debugging tool. It allows you to catch exceptions in derived class functions. To use this command, use the following syntax:
(gdb) catch throw C++Exception
where C Exception
is the exception type.
3. Use Clang's "-gsplit-dwarf" compilation option
The Clang compiler provides a compilation option called "-gsplit-dwarf". This option generates a DWARF debug message containing separate entries for derived class functions. This makes it easier for the debugger to locate derived class functions.
Practical Case
Let us consider the following code example:
class Base { public: void foo() { std::cout << "Base::foo()" << std::endl; } }; class Derived : public Base { public: void foo() override { std::cout << "Derived::foo()" << std::endl; } }; int main() { Derived d; d.foo(); }
To debug the Derived::foo()
function, We can use the following steps:
Derived::foo()
function: (nm a.out | grep Derived::foo)
This should do Output lines similar to the following:
0x12345678 T Derived::foo
(gdb a.out)
(gdb) break *0x12345678
(gdb) run
Derived::foo()
function is called. Using these tips, you can easily debug problems in functions in inherited classes.
The above is the detailed content of Detailed explanation of C++ function debugging: How to debug problems in functions in inherited classes?. For more information, please follow other related articles on the PHP Chinese website!