Vivek Kumar
Vivek Kumar

Reputation: 5050

listing all function of class in gdb

I am trying to learn debugging using gdb. I have got the starting.

I just want to know is it possible to list all the functions of a class including the default one provided by the compiler?

Or even other way without using and IDE

Thanks

=============================================================================

The code I am trying:

#include <iostream>

class MyClass
{
    public:
        void Hello() {
        }
        int a;
};

int main(int argc, char **argv)
{
    MyClass a;
    MyClass b = a;
    MyClass c;
    c = a;
    return 0;
}

=UPDATE 3====================================================================

I also want to list the compiler provided function name, if possible in gdb.

My question is in addition to question posted at

How to list class methods in gdb?

Upvotes: 1

Views: 1900

Answers (3)

user184968
user184968

Reputation:

You have written too simple class. This advice:

Once you have the executable loaded in gdb, type break (or b) and hit the tab key.

is correct in general but in your case MinGW does not create anything for MyClass. I compiled your program with MinGW and disassembled it:

(gdb) disassemble /m main
Dump of assembler code for function main(int, char**):
13      {
   0x0040138c <+0>:     push   %ebp
   0x0040138d <+1>:     mov    %esp,%ebp
   0x0040138f <+3>:     and    $0xfffffff0,%esp
   0x00401392 <+6>:     sub    $0x10,%esp
   0x00401395 <+9>:     call   0x40193c <__main>

14          MyClass a;
15          MyClass b = a;
   0x0040139a <+14>:    mov    0xc(%esp),%eax
   0x0040139e <+18>:    mov    %eax,0x8(%esp)

16          MyClass c;
17          c = a;
   0x004013a2 <+22>:    mov    0xc(%esp),%eax
   0x004013a6 <+26>:    mov    %eax,0x4(%esp)

18          return 0;
   0x004013aa <+30>:    mov    $0x0,%eax

19      }
   0x004013af <+35>:    leave
   0x004013b0 <+36>:    ret

End of assembler dump. 

As you can see only move instructions. For example your assigments c = a; results in just two move instructions and no function calls:

0x004013a2 <+22>:    mov    0xc(%esp),%eax
0x004013a6 <+26>:    mov    %eax,0x4(%esp)

As you can see the compiler chose not to generate anything for your class. In my opinion you chose too simple example to learn that you want.

I made you example a little bit more complex

#include <iostream>

class MyClass
{
    public:
        void Hello()
        {
          std::cout << "Hello\n";
        }
        int a;
};

int main(int argc, char **argv)
{
    MyClass a;
    a.Hello();
    MyClass b = a;
    MyClass c;
    c = a;
    return 0;
}

and under gdb break My shows this:

(gdb) b MyClass
MyClass           MyClass::Hello()
(gdb) b MyClass

and this is the output of nm:

D:\src-c++\test.names>nm -C ./m.exe | grep MyClass
00404060 r .eh_frame$_ZN7MyClass5HelloEv
00401c20 t .text$_ZN7MyClass5HelloEv
00401c20 T MyClass::Hello()

I just wanted to see what are the default function generated by the class, if one does not write it

Change you member class variable from 'int a' to std::string a and you will see default functions generated by compiler

#include <iostream>
#include <string>

class MyClass
{
public:
  void Hello() {
  }
  std::string a;
};

int main(int argc, char **argv)
{
  MyClass a;
  MyClass b = a;
  MyClass c;
  c = a;
  return 0;
}

And these are the compile-generated functions:

>nm -C ./a.out | grep My
00000000004009b8 W MyClass::MyClass(MyClass const&)
0000000000400964 W MyClass::MyClass()
000000000040097c W MyClass::~MyClass()
0000000000400994 W MyClass::operator=(MyClass const&)

Upvotes: 3

Stephen
Stephen

Reputation: 2863

Once you have the executable loaded in gdb, type break (or b) and hit the tab key. This will give you a list of symbols that gdb can set a breakpoint at. Often times gdb will ask you if you really want to display all the possibilities (sometimes there are thousands of possibilities).

If you have some idea of what the function might be called, type the first few characters of the function name and hit tab. This will reduce the number of results to a manageable size.

Upvotes: 1

rlc
rlc

Reputation: 2857

For example, when using break, type "break" space a single quote (') the name of your class, and hit TAB. It'll list the candidates, including generated code.

I.e. break 'PotatoLaucher::<tab>

Upvotes: 1

Related Questions