m.divya.mohan
m.divya.mohan

Reputation: 2330

Examine boost shared_ptr with gdb

Following is my source code:

#include <iostream>
#include <boost/shared_ptr.hpp>

class MyClass
{
    public:
        MyClass()
        {
            i=10;
        }
    private:
        int i;
};


int main(int argc, const char *argv[])
{
    boost::shared_ptr <MyClass> obj(new MyClass());
    return 0;
}

I want to examine obj in gdb, and view the value of member variable i.

This is what I get with normal print:

29          boost::shared_ptr <MyClass> obj(new MyClass());
(gdb) n
30          return 0;
(gdb) p obj
$1 = {px = 0x602010, pn = {pi_ = 0x602030}}

I tried the tip mentioned in this link , but does not work.

(gdb) call (obj.get())->print()
Cannot evaluate function -- may be inlined

Is there any other way? gdb version is 7.0.1.

Upvotes: 20

Views: 13087

Answers (4)

Ganesh M
Ganesh M

Reputation: 3706

Try this:

print (*obj.px).i

complete code is below:

 (gdb) list 1,23
1       #include <iostream>
2       #include <boost/shared_ptr.hpp>
3       #include <string>
4
5       class MyClass
6       {
7           public:
8               MyClass()
9                   : name("Testing")
10              {
11                  i=10;
12              }
13          private:
14              int i;
15              std::string name;
16      };
17
18
19      int main(int argc, const char *argv[])
20      {
21          boost::shared_ptr <MyClass> obj(new MyClass());
22          return 0;
23      }
(gdb) p obj
$9 = {px = 0x602010, pn = {pi_ = 0x602060}}
(gdb) p (*obj.px).i
$10 = 10
(gdb) p (*obj.px).name
$11 = {static npos = 18446744073709551615,
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
    _M_p = 0x602048 "Testing"}}

Upvotes: 2

m.divya.mohan
m.divya.mohan

Reputation: 2330

Got it.!

(gdb) set print pretty
(gdb) p obj
$5 = {
  px = 0x602010,
  pn = {
    pi_ = 0x602030
  }
}
(gdb) p obj.px
$6 = (MyClass *) 0x602010



(gdb) p *(obj.px)
$7 = {
  i = 10
}

Upvotes: 30

Anand Rathi
Anand Rathi

Reputation: 786

When you compile use -ggdb option and see if that works

http://sourceware.org/gdb/onlinedocs/gdb/Inline-Functions.html

Inlining is an optimization that inserts a copy of the function body directly at each call site, instead of jumping to a shared routine. gdb displays inlined functions just like non-inlined functions. They appear in backtraces. You can view their arguments and local variables, step into them with step, skip them with next, and escape from them with finish. You can check whether a function was inlined by using the info frame command.

For gdb to support inlined functions, the compiler must record information about inlining in the debug information — gcc using the dwarf 2 format does this, and several other compilers do also. gdb only supports inlined functions when using dwarf 2. Versions of gcc before 4.1 do not emit two required attributes (‘DW_AT_call_file’ and ‘DW_AT_call_line’); gdb does not display inlined function calls with earlier versions of gcc. It instead displays the arguments and local variables of inlined functions as local variables in the caller.

The body of an inlined function is directly included at its call site; unlike a non-inlined function, there are no instructions devoted to the call. gdb still pretends that the call site and the start of the inlined function are different instructions. Stepping to the call site shows the call site, and then stepping again shows the first line of the inlined function, even though no additional instructions are executed.

This makes source-level debugging much clearer; you can see both the context of the call and then the effect of the call. Only stepping by a single instruction using stepi or nexti does not do this; single instruction steps always show the inlined body.

There are some ways that gdb does not pretend that inlined function calls are the same as normal calls:

Setting breakpoints at the call site of an inlined function may not work, because the call site does not contain any code. gdb may incorrectly move the breakpoint to the next line of the enclosing function, after the call. This limitation will be removed in a future version of gdb; until then, set a breakpoint on an earlier line or inside the inlined function instead. gdb cannot locate the return value of inlined calls after using the finish command. This is a limitation of compiler-generated debugging information; after finish, you can step to the next line and print a variable where your program stored the return value.

Upvotes: 0

dans3itz
dans3itz

Reputation: 1615

This is going to be a hard one to answer. GDB 7.x added Python scripting support. There are some resources on the web. Rather than make a poor attempt to advise on something I don't have first experience in, I'll refer you to a past post:

C++ GDB Python Pretty Printing Tutorial?

Upvotes: 0

Related Questions