Search code examples
c++gdb

gdb get line number of a demangled function name with one command


i cannot get the source line from gdb with one gdb command for a c++ demangled function name. but for a mangled name, it works.

a sample c++ app:

#include <string>
#include <iostream>

struct A
{
    virtual void print()
    {
        std::cout << "hello\n";
    }
};

int main(int argc, char** argv)
{
    A a;
    a.print("hello");
}

build g++ test.cpp -g -o test.bin get the line number in gdb of a mangled name (one command):

gdb test.bin
>info line *(_ZN1A5printEv+20)
Line 8 of "test.cpp" starts at address 0x1274 <A::print()+16> and ends at 0x1287 <A::print()+35>.

but when i try to get the line number with the demangled name :

>info line *(A::print()+20)
Cannot resolve method A::print to any overloaded instance

it works only in 2 steps:

> info line A::print()
Line 6 of "test.cpp" starts at address 0x1264 <A::print()> and ends at 0x1274 <A::print()+16>.
>info line *(0x1264+20)
Line 8 of "test.cpp" starts at address 0x1274 <A::print()+16> and ends at 0x1287 <A::print()+35>.

is there a way to get the line number with one gdb command?


Solution

  • To allow GDB to recognize A::print() as a single symbol, enclose it in single quotes

    info line *('A::print()'+20)