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 v.at(0) 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.
137 comments ↓
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.
Sheesh… MSVC debugger has been visualizing vectors for a while now.
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.
http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt
should solve your problems.
Mr. Dan Marinescu wrote some macros that automate looking at _M_impl and friends.
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.
"still-not-boundary-checked"
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.
In many standard library implementations containers are bounds checked when _DEBUG is defined.
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.
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
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!
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.
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.
if you want boundary checking on vectors, use the std::vector::at member function.
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.
Have you tried lldb?
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?
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
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).
Manger, I believe that you'll have to recompile stdlib.
undump on linux:
https://code.google.com/p/undump/
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?
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.
Oh, and I used (or tried to use) at() right there in TFA.
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.
Besides you can define -D _GLIBCXX_DEBUG flag and you won't exceed vector boundaries.
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: http://www.cplusplus.com/reference/vector/vector/operator/
And yes, there exist non-portable build flags turning on boundary checking in operator[] – and doing a bunch of other things.
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)
I like this site because so much useful stuff on here : D.
I conceive this web site holds some real superb information for everyone : D.
I must say got into this website. I found it to be interesting and loaded with unique points of view.
5/16/2019 @ 10:16:31 PM In my view, yosefk.com 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.
Respect to website author , some wonderful entropy.
Very interesting points you have remarked, appreciate it for putting up.
Very interesting points you have remarked, appreciate it for putting up.
I really enjoy examining on this blog , it has got great content .
google got me here. Thanks!
I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.
Deference to op , some superb selective information .
Enjoyed examining this, very good stuff, thanks .
I am not rattling great with English but I get hold this really easygoing to read .
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.
google got me here. Thanks!
I was looking at some of your articles on this site and I believe this internet site is really instructive! Keep on posting .
I really enjoy examining on this web , it has got cool posts .
Great, yahoo took me stright here. thanks btw for post. Cheers!
Intresting, will come back here more often.
Some truly good goodies on this web site , appreciate it for contribution.
Ha, here from google, this is what i was browsing for.
Me enjoying, will read more. Thanks!
I conceive this web site holds some real superb information for everyone : D.
Great, google took me stright here. thanks btw for this. Cheers!
Enjoyed reading through this, very good stuff, thankyou .
Just wanna input on few general things, The website layout is perfect, the articles is very superb : D.
Great, yahoo took me stright here. thanks btw for this. Cheers!
Thank You for this.
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!
This is awesome!
Great, yahoo took me stright here. thanks btw for this. Cheers!
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.
very nice post, i actually like this web site, carry on it
I conceive this web site holds some real superb information for everyone : D.
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?
Thank you for sharing your info. I really appreciate your efforts
and I am waiting for your further post thank you once again.
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.
I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.
Hi, happy that i found on this in yahoo. Thanks!
Thanks to my father who shared with me about this website, this weblog
is genuinely remarkable.
Respect to website author , some wonderful entropy.
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?
I simply must tell you that you have an excellent and unique site that I must say enjoyed reading.
bing brought me here. Thanks!
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.
Cheers
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!
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!
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!
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.
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!
Thanks to my father who informed me about this weblog, this website is
actually remarkable.
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!
I am regular visitor, how are you everybody?
This post posted at this web site is truly fastidious.
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!
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!
I was looking at some of your articles on this site and I believe this internet site is really instructive! Keep on posting .
I conceive you have mentioned some very interesting details , appreciate it for the post.
Great stuff to Read, glad that yandex took me here, Keep Up good Work
Enjoyed reading through this, very good stuff, thankyou .
Enjoyed reading through this, very good stuff, thankyou .
I like, will read more. Thanks!
I really got into this web. I found it to be interesting and loaded with unique points of interest.
Thanks so much for the article post.Really thank you! Cool.
I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.
Appreciate it for this howling post, I am glad I observed this internet site on yahoo.
yahoo brought me here. Thanks!
Hi, google lead me here, keep up great work.
I like this site because so much useful stuff on here : D.
Hello, Neat post. There is a problem together with
your website in web explorer, might test this? IE nonetheless is the
market chief and a good section of people will pass over your
fantastic writing because of this problem.
Great stuff to see, glad that Yahoo took me here, Keep Up good job
This is great!
This is great!
Excellent beat ! I wish to apprentice even as you amend your site,
how can i subscribe for a blog web site? The account helped me a appropriate deal.
I had been tiny bit familiar of this your broadcast offered
vivid transparent idea
I am not rattling great with English but I get hold this really easygoing to read .
Hi, bing lead me here, keep up good work.
This does interest me
I like this website its a master peace ! Glad I found this on google .
Hello, yahoo lead me here, keep up great work.
yahoo got me here. Cheers!
Parasite backlink SEO works well :)
This helps. Thanks!
Me enjoying, will read more. Cheers!
very nice post, i actually enjoyed this web site, carry on it
Appreciate it for this howling post, I am glad I observed this internet site on yahoo.
I truly enjoy looking through on this web site , it holds superb content .
Yeah bookmaking this wasn’t a risky decision outstanding post! .
I must say got into this web. I found it to be interesting and loaded with unique points of interest.
I consider something really special in this site.
Awesome blog! Is your theme custom made or did you download it from
somewhere? A theme like yours with a few simple tweeks would really make my blog stand out.
Please let me know where you got your design. Thank you
Hi, i really think i will be back to your website
I really like your blog.. very nice colors & theme.
Did you make this website yourself or did you hire someone to do it for you?
Plz respond as I'm looking to design my own blog
and would like to find out where u got this from. many thanks
Good day! I could have sworn I've been to this blog before but after reading through some of the post I realized
it's new to me. Anyhow, I'm definitely happy I found it and
I'll be book-marking and checking back often!
great advice you give
I've learn several good stuff here. Definitely value bookmarking for revisiting.
I wonder how so much effort you set to make this kind of wonderful informative site.
Skyking, this note is your next bit of info. Do transceive the agency at your convenience. No further information until next transmission. This is broadcast #3718. Do not delete.
Heya i'm for the first time here. I found this board and I find It truly useful & it helped me out a lot.
I am hoping to give one thing back and aid others such as you helped
me.
Useful information. Fortunate me I found your web site accidentally, and I'm shocked why this twist of
fate didn't happened in advance! I bookmarked it.
you are amazing
This blog is amazing! Thank you.
Have you ever considered publishing an e-book or guest authoring
on other websites? I have a blog centered on the same information you discuss and would really like to have you
share some stories/information. I know my subscribers would appreciate your work.
If you are even remotely interested, feel free to
send me an e-mail.
Hello every one, here every one is sharing such
experience, therefore it's good to read this blog, and I used to visit this webpage everyday.
Ha, here from yahoo, this is what i was searching for.
Highly energetic post, I loved that bit. Will there be a
part 2? pof natalielise
Your article has proven useful to me.
I am 43 years old and a mother this helped me!
I am 43 years old and a mother this helped me!
This paragraph will help the internet people for creating new web site or even a weblog from start to end.
I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while.
Howdy! I know this is kinda off topic however I'd figured I'd
ask. Would you be interested in trading links or maybe guest authoring a
blog post or vice-versa? My blog addresses
a lot of the same subjects as yours and
I think we could greatly benefit from each other.
If you are interested feel free to shoot me an email.
I look forward to hearing from you! Terrific blog by the way!
stays on topic and states valid points. Thank you.
Ha, here from bing, this is what i was searching for.