Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To get disassembly of an overloaded member function in GDB - C++

Tags:

c++

c++11

gdb

c++14

There are multiple libraries implementing a specific class - I am not sure which library got included - I don't have make files either.

I want to confirm this directly in GDB by looking into disassembly of a member method of a class.

How do I get disassembly of the overloaded member function in GDB?

like image 475
ultimate cause Avatar asked Jan 12 '18 04:01

ultimate cause


2 Answers

Consider this test:

struct Foo {
  int Fn(int x) const { return x + 42; }
  int Fn(void) const { return 24; }
};

int main()
{
  Foo f;
  return f.Fn() + f.Fn(1);
}

When this is compiled with debug info:

(gdb) info func Fn
All functions matching regular expression "Fn":

File t.cc:
int Foo::Fn() const;
int Foo::Fn(int) const;

(gdb) disas 'Foo::Fn(int) const'
Dump of assembler code for function Foo::Fn(int) const:
   0x000000000040051e <+0>: push   %rbp
   0x000000000040051f <+1>: mov    %rsp,%rbp
   0x0000000000400522 <+4>: mov    %rdi,-0x8(%rbp)
   0x0000000000400526 <+8>: mov    %esi,-0xc(%rbp)
   0x0000000000400529 <+11>:    mov    -0xc(%rbp),%eax
   0x000000000040052c <+14>:    add    $0x2a,%eax
   0x000000000040052f <+17>:    pop    %rbp
   0x0000000000400530 <+18>:    retq   
End of assembler dump.

When this is compiled without debug info:

(gdb) info func Fn
All functions matching regular expression "Fn":

Non-debugging symbols:
0x000000000040051e  Foo::Fn(int) const
0x0000000000400532  Foo::Fn() const

(gdb) disas 'Foo::Fn() const'
Dump of assembler code for function _ZNK3Foo2FnEv:
   0x0000000000400532 <+0>: push   %rbp
   0x0000000000400533 <+1>: mov    %rsp,%rbp
   0x0000000000400536 <+4>: mov    %rdi,-0x8(%rbp)
   0x000000000040053a <+8>: mov    $0x18,%eax
   0x000000000040053f <+13>:    pop    %rbp
   0x0000000000400540 <+14>:    retq   
End of assembler dump.
like image 148
Employed Russian Avatar answered Sep 28 '22 07:09

Employed Russian


If executable file got debug data, you may check based on file names, usually. Your command is

Ovreloaded functions use name mangling. Essentially they have unique names.

But you actually can print address of function, e.g

p 'A::function(int, bool, bool)'

It would print something like '$1= { bool(int, bool, bool)} ....' Now you should use disassemble command:

disassemble $1

Questionis, is library static? If it is a shared library, then all you need is to use ldd utility on your executable to figure out which shared object it uses.

like image 21
Swift - Friday Pie Avatar answered Sep 28 '22 07:09

Swift - Friday Pie