Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list class methods in gdb?

Tags:

c++

debugging

gdb

I've been googling for this and checking through the gdb manual but can't seem to find an answer to what I'm trying to do.

Is there a way to get gdb to print out a listing of all the methods for a given class type? The print command only seems to show the data members and fields, none of the methods are displayed for it.

Additionally, to take it a step further, is there a way to print all the correct virtual methods given a base *pointer? Say like for example:

struct A {   virtual void foo() {} };  struct B : public A {   void foo() {} };  int main() {   A *b = new B; } 

How can I get gdb to print variable *b and have it show the correct virtual method(s)?

Thanks

like image 542
greatwolf Avatar asked Oct 18 '10 01:10

greatwolf


People also ask

How can I see all functions in GDB?

If specified, the info functions command will list the functions matching the regex. If omitted, the command wil list all functions in all loaded modules (main program and shared libraries).

What does the list command do in GDB?

To print lines from a source file, use the list command (abbreviated l ). By default, ten lines are printed. There are several ways to specify what part of the file you want to print; see Specify Location, for the full list.

What does print do in GDB?

The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages).


1 Answers

You can use ptype.

Suppose I add these lines to your example:

A alpha; B beta; 

Now in gdb I can ask for a description of a class type (or an instance of one):

(gdb) ptype alpha type = class A {   public:     virtual void foo(); }  (gdb) ptype A type = class A {   public:     virtual void foo(); }  (gdb) ptype beta type = class B : public A {   public:     virtual void foo(); }  (gdb) ptype B type = class B : public A {   public:     virtual void foo(); } 

If I try that with a pointer, I get the declared type:

(gdb) ptype b type = class A {   public:     virtual void foo(); } * 

If I want the real type, I must set the `print object' variable:

(gdb) set print object on (gdb) ptype b type = /* real type = B * */ class A {   public:     virtual void foo(); } * 

and then call ptype again to see what B has (I don't know how to do it in one step).

like image 97
Beta Avatar answered Oct 09 '22 04:10

Beta