Very funny, gdb. Ve-ery funny.

Have you ever opened a core dump with gdb, tried to print a C++ std::vector element, and got the following?

(gdb) p v[0]
You can't do that without a process to debug.

So after seeing this for years, my thoughts traveled along the path of, we could make a process out of the core dump.

No really, there used to be Unices with a program called undump that did just that. All you need to do is take the (say) ELF core dump file and generate an ELF executable file which loads the memory image saved in the core (that's actually the easier, portable part) and initializes registers to the right values (the harder, less portable part). I even wrote a limited version of undump for PowerPC once.

So we expended some effort on it at work.

And then I thought I'd just check a live C++ process (which I normally don't do, for various reasons). Let's print a vector element:

(gdb) p v[0]
Could not find operator[].
(gdb) p
Cannot evaluate function -- may be inlined.

Very funny, gdb. "You can't do that without a process to debug". Well, I guess you never did say that I could do that with a process to debug, now did you. Because, sure enough, I can't. Rolling on the floor, laughing. Ahem.

I suggest that we all ditch our evil C arrays and switch to slow-compiling, still-not-boundary-checked, still-not-working-in-debuggers-after-all-these-YEARS std::vector, std::array and any of the other zillion "improvements".

And gdb has these pretty printers which, if installed correctly (not easy with several gcc/STL versions around), can display std::vector – as in all of its 10000 elements, if that's how many elements it has. But they still don't let you print vec[0].member.vec2[5].member2. Sheesh!

P.S. undump could be useful for other things, say a nice sort of obfuscating scripting language compiler – Perl used to use undump for that AFAIK. And undump would in fact let you call functions in core dumps – if said functions could be, um, found by gdb. Still, ouch.

P.P.S. What gdb prints and when depends on things I do not comprehend. I failed to reproduce the reported behavior in full at home. I've seen it for years at work though.


#1 Ben Craig on 12.19.13 at 12:53 pm

Debugging std::vector isn't too bad. You can dig in to the structure (p v), static_cast the first element to your data type (because it is almost certainly a void *), then you can do your array tricks on the static_casted pointer. I wish all the STL containers were as "easy" as vector. Yes, I know that all that stuff is a far cry from "p v[0]".

The real nightmare is when you want to look at any node based container. The container only has pointers to node base classes. The node base class doesn't include your element type. So you either get to cast each node to a different internal data structure type, or you get to do some funky pointer offsetting + casts. And your traversal isn't anywhere as simple as switching from "p v[0]" to "p v[1]", you have to go through a bunch of "next" calls.

#2 Assaf on 12.25.13 at 10:56 pm

Sheesh… MSVC debugger has been visualizing vectors for a while now.

#3 Yossi Kreinin on 12.25.13 at 11:25 pm

gdb "visualizes" vectors as well (well, it pretty-prints them and then Eclipse and similar visualize them). The problem is evaluating expressions with vec[5] in them.

#4 Michael Moser on 01.07.14 at 12:48 am

should solve your problems.
Mr. Dan Marinescu wrote some macros that automate looking at _M_impl and friends.

#5 Ilya Kasnacheev on 01.11.14 at 8:38 am

There is this crazy effort underway to make every linux process serializable: allow not only memory be [un]dumped but all open file descriptors status (including bringing socked in the same mode) and other environment too.

This seems to be doable. They are gradually patching the kernel to make every thing reversible.

#6 Moschops on 01.11.14 at 8:54 am


Oh for God's sake, please, GIVE IT A REST. If you want to use something that IS boundary checked, then do so. It's a simply modification to make yourself, or just get a library that does boundary checking, and leave those of us who don't want boundary checking in peace. Nobody's holding a gnu to your head.

#7 thwest on 01.11.14 at 9:16 am

In many standard library implementations containers are bounds checked when _DEBUG is defined.

#8 Nimrod on 01.11.14 at 10:10 am

Debugging C++ code with gdb is such a frustrating and arcane experience that this issue is just one drop in a sea of problems. Essentially most people resort to printf()'s (sorry, "cerr << " if you're into this iostream nightmare).

The *real* problem is that, for some unknown reason, people don't see this as a problem and no measures are taken to improve the situation significantly.

The suggestion to use gdb macros (stl_views) is also problematic: Due to the way emacs – gdb interface is working (and you should really debug in emacs) printing more than a few tens of elements gets really slow.

#9 maht on 01.11.14 at 10:12 am

the first thing I do with a project is say "how am I going to debug this when it fails?", you found out too late

#10 Mark on 01.11.14 at 12:01 pm

I work on a parallel C++ debugger and we've made quite a few improvements to gdb, not all of which have been merged upstream yet. You could grab our patches from the website and see if they help.

Btw boost arrays are bound checked as a compile-time option, not that that helps you much now!

#11 Paul on 01.11.14 at 1:14 pm

Microsoft VC++ has been letting you debug core dumps as processes for years. And VC++ has let you debug stl containers, including node containers, for years too, as it provides a debugging description language for generic data.

#12 jon w on 01.11.14 at 2:16 pm

The problem is gdb, not C++. I've wanted a slick, modern debugger for Unix for the last 15 years, but not had the gumption to actually make one.

#13 fag on 01.11.14 at 8:41 pm

if you want boundary checking on vectors, use the std::vector::at member function.

#14 Yossi Kreinin on 01.12.14 at 1:45 am

VC++ is nice but gdb supports way more platforms. In general, a lot of people tell me what I could do instead of using GNU and std::vector… oh the possibilities. The things I could do instead of programming make my head spin, for starters. Why we do what we do is such an interesting topic in itself.

#15 Nick on 01.12.14 at 2:42 am

Have you tried lldb?

#16 Yossi Kreinin on 01.12.14 at 2:47 am

Not yet. Is it actually better? Does it support cross-debugging MIPS? Does it work with DWARF3 or does it want me to compile with clang to get the benefits?

#17 Nick on 01.12.14 at 3:03 am

I can't answer that, sadly. I used only for Objective-C. Though I just tried it and lldb could not execute 'at' method. After some research I found out that those methods are inlined, so there are no real function copies exist. Same thing happens with all templates functions in standard library

#18 Manger on 01.12.14 at 12:46 pm

Have you tried disabling optimizations with -O0? GDB is trying to find and run code associated to operator[]() and at(), which is impossible without an attached process (there is no code to run), or when the functions are inlined (and, with optimizations, I believe pretty much all std::vector functions are).

#19 Nick on 01.13.14 at 9:29 am

Manger, I believe that you'll have to recompile stdlib.

#20 NS on 07.13.14 at 5:05 am

undump on linux:

As for bounds checking, why do you say that that doesn't exist? It's kind of a big selling point of vector, ever since its inception. Have you never used std::vector::at before?

#21 Yossi Kreinin on 07.13.14 at 8:55 am

The undump you're telling me about is the one the guy working with me wrote. It's not completely finished yet, but thanks for the tip.

As to std::vector, there's also operator[] which is what most people use, and that's not bounds checked. No thanks for the tip to use the uglier syntax; a better though still not great suggestion would be to roll one's own bounds checked vector.

#22 Yossi Kreinin on 07.13.14 at 8:56 am

Oh, and I used (or tried to use) at() right there in TFA.

#23 argothiel on 11.15.14 at 3:01 pm

What do you mean by bounds checking in []? The whole point of [] instead of at() is to create an element, if it doesn't exist yet.

#24 argothiel on 11.15.14 at 3:55 pm

Besides you can define -D _GLIBCXX_DEBUG flag and you won't exceed vector boundaries.

#25 Yossi Kreinin on 11.15.14 at 5:44 pm

Create if it doesn't exist? Maybe in std::map, but not in std::vector where the behavior for out of range accesses with operator[] is undefined:

And yes, there exist non-portable build flags turning on boundary checking in operator[] – and doing a bunch of other things.

#26 hong on 08.04.15 at 3:27 am

Actually I tried with gdb 7.7.1 and compile the program with "-O0" option, there comes no problem at all with operator []:

(gdb) p xx[5]
$2 = (__gnu_cxx::__alloc_traits<std::allocator >::value_type &) @0x630a18: 2.2017165780746879

(xx is std::vector)

#27 cheatbreaker download on 05.15.19 at 6:40 pm

I like this site because so much useful stuff on here : D.

#28 fortnite aimbot download on 05.16.19 at 1:06 pm

I conceive this web site holds some real superb information for everyone : D.

#29 fortnite aimbot download on 05.16.19 at 5:00 pm

I must say got into this website. I found it to be interesting and loaded with unique points of view.

#30 Jerica Kiehne on 05.17.19 at 5:16 am

5/16/2019 @ 10:16:31 PM In my view, does a great job of covering topics of this sort! Even if frequently deliberately polemic, the material posted is more often than not thoughtful and thought-provoking.

#31 nonsense diamond 1.9 on 05.17.19 at 7:15 am

Respect to website author , some wonderful entropy.

#32 fallout 76 cheats on 05.17.19 at 10:40 am

Very interesting points you have remarked, appreciate it for putting up.

#33 red dead redemption 2 digital key resale on 05.17.19 at 3:50 pm

Very interesting points you have remarked, appreciate it for putting up.

#34 redline v3.0 on 05.17.19 at 6:54 pm

I really enjoy examining on this blog , it has got great content .

#35 chaturbate hack cheat engine 2018 on 05.18.19 at 8:20 am

google got me here. Thanks!

#36 sniper fury cheats windows 10 on 05.18.19 at 3:11 pm

I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.

#37 mining simulator codes 2019 on 05.19.19 at 7:13 am

Deference to op , some superb selective information .

#38 smutstone on 05.20.19 at 11:52 am

Enjoyed examining this, very good stuff, thanks .

#39 redline v3.0 on 05.21.19 at 7:22 am

I am not rattling great with English but I get hold this really easygoing to read .

#40 free fire hack version unlimited diamond on 05.21.19 at 4:39 pm

I’m impressed, I have to admit. Genuinely rarely should i encounter a weblog that’s both educative and entertaining, and let me tell you, you may have hit the nail about the head. Your idea is outstanding; the problem is an element that insufficient persons are speaking intelligently about. I am delighted we came across this during my look for something with this.

#41 nonsense diamond on 05.22.19 at 6:29 pm

google got me here. Thanks!

#42 krunker aimbot on 05.23.19 at 6:48 am

I was looking at some of your articles on this site and I believe this internet site is really instructive! Keep on posting .

#43 bitcoin adder v.1.3.00 free download on 05.23.19 at 10:27 am

I really enjoy examining on this web , it has got cool posts .

#44 vn hax on 05.23.19 at 7:10 pm

Great, yahoo took me stright here. thanks btw for post. Cheers!

#45 v9 on 05.24.19 at 7:59 am

Intresting, will come back here more often.

#46 ispoofer pogo activate seriale on 05.24.19 at 6:30 pm

Some truly good goodies on this web site , appreciate it for contribution.

#47 cheats for hempire game on 05.26.19 at 6:40 am

Ha, here from google, this is what i was browsing for.

#48 iobit uninstaller 7.5 key on 05.26.19 at 9:26 am

Me enjoying, will read more. Thanks!

#49 smart defrag 6.2 serial key on 05.26.19 at 3:47 pm

I conceive this web site holds some real superb information for everyone : D.

#50 resetter epson l1110 on 05.26.19 at 6:33 pm

Great, google took me stright here. thanks btw for this. Cheers!

#51 sims 4 seasons free code on 05.27.19 at 7:50 am

Enjoyed reading through this, very good stuff, thankyou .

#52 rust hacks on 05.27.19 at 8:19 pm

Just wanna input on few general things, The website layout is perfect, the articles is very superb : D.

#53 strucid hacks on 05.28.19 at 10:37 am

Great, yahoo took me stright here. thanks btw for this. Cheers!

#54 expressvpn key on 05.28.19 at 7:40 pm

Thank You for this.

#55 gamefly free trial on 05.29.19 at 3:31 am

Hello are using WordPress for your site platform?
I'm new to the blog world but I'm trying to get started
and create my own. Do you need any coding expertise to make your
own blog? Any help would be greatly appreciated!

#56 ispoofer pokemon go license key on 05.29.19 at 8:56 am

This is awesome!

#57 aimbot free download fortnite on 05.29.19 at 12:55 pm

Great, yahoo took me stright here. thanks btw for this. Cheers!

#58 gamefly free trial on 05.29.19 at 2:31 pm

If some one desires to be updated with newest technologies therefore he must be visit this site
and be up to date all the time.

#59 redline v3.0 on 05.29.19 at 5:21 pm

very nice post, i actually like this web site, carry on it

#60 vn hax on 05.30.19 at 6:36 am

I conceive this web site holds some real superb information for everyone : D.

#61 gamefly free trial on 05.30.19 at 8:47 am

Thank you for the auspicious writeup. It in truth was a entertainment account it.
Look advanced to more added agreeable from you!
However, how could we keep up a correspondence?

#62 how to get help in windows 10 on 05.30.19 at 4:32 pm

Thank you for sharing your info. I really appreciate your efforts
and I am waiting for your further post thank you once again.

#63 gamefly free trial on 05.31.19 at 1:05 pm

I'm gone to tell my little brother, that he should
also pay a quick visit this blog on regular
basis to take updated from latest news update.

#64 xbox one mods free download on 05.31.19 at 1:09 pm

I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.

#65 fortnite aimbot download on 05.31.19 at 3:52 pm

Hi, happy that i found on this in yahoo. Thanks!

#66 gamefly free trial on 06.01.19 at 2:38 am

Thanks to my father who shared with me about this website, this weblog
is genuinely remarkable.

#67 mpl pro on 06.01.19 at 6:41 pm

Respect to website author , some wonderful entropy.

#68 gamefly free trial on 06.02.19 at 1:17 am

Currently it sounds like WordPress is the top blogging platform out there right now.
(from what I've read) Is that what you are using on your blog?

#69 hacks counter blox script on 06.02.19 at 6:50 am

I simply must tell you that you have an excellent and unique site that I must say enjoyed reading.

#70 krunker aimbot on 06.03.19 at 10:39 am

bing brought me here. Thanks!

#71 gamefly free trial on 06.04.19 at 12:13 am

I’m not that much of a online reader to be honest but your blogs really nice, keep it up!
I'll go ahead and bookmark your website to come back later.

#72 gamefly free trial on 06.05.19 at 6:11 am

What i do not understood is in truth how you are no longer actually a
lot more neatly-liked than you might be now.
You're so intelligent. You realize therefore significantly with
regards to this subject, produced me for my part imagine it from a lot of varied
angles. Its like women and men are not involved until
it's one thing to do with Woman gaga! Your personal
stuffs excellent. Always maintain it up!

#73 gamefly free trial on 06.05.19 at 7:10 am

I truly love your site.. Great colors & theme. Did
you develop this website yourself? Please reply back as I'm attempting to create my own website and would like to find out where you got this from or what the
theme is named. Thank you!

#74 gamefly free trial on 06.05.19 at 11:12 pm

Do you mind if I quote a couple of your posts as long
as I provide credit and sources back to your
webpage? My website is in the exact same niche as yours and my users would truly benefit from
some of the information you present here. Please let me know if this ok with you.
Appreciate it!

#75 gamefly free trial on 06.06.19 at 8:18 pm

Its like you read my mind! You seem to know a lot about this, like you wrote the
book in it or something. I think that you can do with some pics to drive the message home a
bit, but instead of that, this is magnificent blog.
An excellent read. I'll certainly be back.

#76 gamefly free trial on 06.06.19 at 10:59 pm

Thanks for your marvelous posting! I certainly enjoyed reading it, you're a great author.I will make
certain to bookmark your blog and will often come back sometime soon. I want to encourage yourself to continue your great
job, have a nice weekend!

#77 gamefly free trial on 06.07.19 at 3:45 am

Thanks to my father who informed me about this weblog, this website is
actually remarkable.

#78 togel singapore hongkong on 06.09.19 at 2:44 pm

Tһis is really interesting, Ⲩоu’re a very skilled bloɡger. I’ve combined your feed and antiсipate searching more of your great post. Additi᧐naⅼly, I have shɑred with your site in my soсial websites!

#79 playstation 4 best games ever made 2019 on 06.12.19 at 3:05 am

I am regular visitor, how are you everybody?
This post posted at this web site is truly fastidious.

#80 ps4 best games ever made 2019 on 06.12.19 at 3:22 am

After I originally left a comment I seem to have clicked the -Notify me
when new comments are added- checkbox and now each time a
comment is added I recieve 4 emails with the exact same comment.
Is there an easy method you can remove me from
that service? Thank you!

#81 quest bars cheap on 06.14.19 at 5:14 pm

Someone essentially help to make critically posts I would state.

That is the very first time I frequented your website page and to this point?

I amazed with the research you made to create this particular submit incredible.

Wonderful task!

#82 roblox executor on 06.17.19 at 7:44 am

I was looking at some of your articles on this site and I believe this internet site is really instructive! Keep on posting .

#83 proxo key generator on 06.19.19 at 12:15 pm

I conceive you have mentioned some very interesting details , appreciate it for the post.

#84 vn hax pubg on 06.20.19 at 8:54 pm

Great stuff to Read, glad that yandex took me here, Keep Up good Work

#85 nonsense diamond key on 06.21.19 at 9:58 am

Enjoyed reading through this, very good stuff, thankyou .

#86 star valor cheats on 06.23.19 at 7:22 pm

Enjoyed reading through this, very good stuff, thankyou .

#87 gmod hacks on 06.24.19 at 5:24 pm

I like, will read more. Thanks!

#88 geometry dash 2.11 download pc on 06.25.19 at 10:03 pm

I really got into this web. I found it to be interesting and loaded with unique points of interest.

#89 Grizzly vildlakseolie on 06.26.19 at 7:35 am

Thanks so much for the article post.Really thank you! Cool.

#90 skisploit on 06.26.19 at 8:39 am

I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.

Leave a Comment