News for hackers. Stuff that matters.
Re:Very funny, gdb. Ve-ery funny by DannyBee on Saturday January 11, 2014 @01:05PM (#7043187)
It'd be nice to know what version of GDB this was.

Years ago, I used to be the C++ maintainer for GDB. By the time I left, things like doing this on a live process worked about 90% of the time (up from 0%). It turns out what he's trying to do is harder than one would think, but it should actually work.

Part of the reason it doesn't really work all that often on coredumps is because you often need access to vtables, thunk functions (which you could theoretically execute without a live process in most ABI's, but it doesn't know this). Depending on the class, there may also be runonce constructors (for static data and associated guard variables), etc.

Re:Very funny, gdb. Ve-ery funny by eliasmacpherson on Saturday January 11, 2014 @01:33PM (#7043281)
https://stackoverflow.com/questions/253099/how-do-i-print-th... I think this might possibly work for the core dump? I don't have time to check now myself...
Re:Very funny, gdb. Ve-ery funny by eliasmacpherson on Sunday January 12, 2014 @11:35AM (#7046401)
Took the time to check and it doesn't work with a core dump. Oh well.
Re:Very funny, gdb. Ve-ery funny by MichaelMoser123 on Saturday January 11, 2014 @04:22PM (#7043804)
std::vector does not have any virtual functions, so how comes it needs the vtable for this?

http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.t...

Here they have some functions that inspect vector by means of _M_impl and friends, why can't gdb do this?

Re:Very funny, gdb. Ve-ery funny by DannyBee on Saturday January 11, 2014 @05:42PM (#7044083)
I was explaining the general problem.

As for std::vector, GDB can and does do this now, through python based pretty printers.

That gdb script is completely unnecessary for this now. Most distros ship with gdb + appropriate pretty printers pre-installed, but they are also available in the libstdc++ repo.

See https://sourceware.org/gdb/wiki/STLSupport

If you use this, print on a vector just works, no extra commands or anything needed.

Re:Very funny, gdb. Ve-ery funny by makomk on Sunday January 12, 2014 @05:06AM (#7045672)
The pretty printers let you print the entire vector, but do they let you access individual elements within the (potentially very large) vector? That's what this blog post is complaining about not being able to do.
Re:Very funny, gdb. Ve-ery funny by EliRivers on Sunday January 12, 2014 @11:31AM (#7046385)
Works absolutely fine on my very standard, almost vanilla installation.

Code:

  #include <vector>
  using namespace std;
  int main()
  {
    vector<int> beans;
    beans.resize(5);
    beans[3] = 4;
    beans [4] = 66;
  }
Build command:

  g++ -g 156.cpp
gdb debugging session:

  GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
  SKIPPED OUT WARRANT INFO HERE
  Reading symbols from /badcode/a.out...done.
  (gdb) break 156.cpp:10
  Breakpoint 1 at 0x4008e6: file 156.cpp, line 10.
  (gdb) run
  Starting program: /badcode/a.out

  Breakpoint 1, main () at 156.cpp:10
  10	  beans [4] = 66;
  (gdb) print beans[3]
  $2 = (int &) @0x60401c: 4
  (gdb) print beans[4]
  $3 = (int &) @0x604020: 0
Re:Very funny, gdb. Ve-ery funny by DannyBee on Sunday January 12, 2014 @04:21PM (#7047497)
So, I thought the post above me asked a different question, related to printing out vectors in general :) Possibly they didn't mean to, but asking gdb to "p v[0]" is a function call execution of operator[]. As mentioned in the grandparent, this should work fine on a live process, but is harder on a not live one.

It's possible the parent meant "why can't GDB make v[0] do an inspection directly on the structure". The simple answer is: "It can't possibly know what the function call does". So you'd have to teach it, specifically, that std::vector is special, or provide a mechanism to override the native calls with python scripting (IE be able to say "here is a replacement for T &operator[int] on std::vector that is in python, and works without a live process").

GDB actually does real overload resolution (it has to, otherwise in some 'simple' cases, it'd have to ask you 100 questions about what functions you are trying to call), making that strategy more complicated.

If your only mechanism for reproduction is a core dump, yes this is a pain in the ass. But this shouldn't be true, even remotely, since you always have things like gdbserver/etc.