CppCon 2015: Greg Law " Give me 15 minutes & I'll change your view of GDB"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is a risky click for me since my view of gdb is already very positive....

Edit: I'm not even 2 minutes in and already my mind is blown. I don't have to b main r all the time?

👍︎︎ 6 👤︎︎ u/YellowFlowerRanger 📅︎︎ Sep 05 2016 🗫︎ replies

[removed]

👍︎︎ 4 👤︎︎ u/[deleted] 📅︎︎ Sep 05 2016 🗫︎ replies

CTRL-P! That's the missing link.

👍︎︎ 1 👤︎︎ u/knotdjb 📅︎︎ Sep 06 2016 🗫︎ replies
Captions
all right so um so first of all apologies to any windows Debs because firstly this is kind of probably not going to be very interesting to you guys and secondly well just I'm I'm sorry for you and but maybe you'll maybe you'll come across so we're going to show a couple of things in in gdb that I've found over the years so we deal a lot with so we make this this tool called undo DB which is well the most common interface to it probably is gdb so a lot of our customers spend their time in gdb and we and we work with them and sometimes you're often it's more than looking at how to do something in gdb and I've kind of picked up some good stuff over the years then I thought it might be interesting to share so probably sorry so yeah I just want to start by by saying said it would be the first person in with the gdb is not intuitive right but it is very powerful and I think there's an important distinction to make between ease of use and ease of learning and it's not easy to learn but it is easy to use when you when you get to learn it and it's not that hard to learn right sometimes you just need to to know that these things exist so so here's a here's a pretty simple program not much more than hello world but it's get few lines so I can at least type next a few times and I compile just as normal and I run this in gdb right and and most of us many of us would have done this at some point and you can start which just saves you typing break at main and then run and it's okay I'm at line six of my program but I don't have any context right and well I can get a list so that's kind of okay but what I want to show you is probably the thing that I think is probably least known and most useful like the thing that schools best on both ranks which is think this thing called tu I mode which stands check text user interface and it's a really bad name because surely that is a text user interface right and and it's a bit well 1970s and and it's just kind of clunky but if you type and I don't know why you have to type this and why it's so obscure but if you type ctrl X a then you get the real T UI text user interface which is like screaming into the 1980s but it is still pretty useful right so now when I type next I get much more context of what my program is doing right it's not perfect right like all curses programs that sometimes goes a bit weird sometimes so if I type next again my programs printed out hello world and it's kind of screwed up my display and that's a reliable way to make it go wrong I couldn't I there are more interesting ways it goes wrong but none of them I could find that we're reliable but if it does you can just do ctrl L and it repaint your screen for use it's quite handy and you can actually do a bit more so you can if I go control X - I may not have enough characters for this but now let me let me make this slightly smaller so I can I was all perfectly set up for this before my system reset ah so we're way over here okay so and then you could control X 2 and row X 2 I get multiple windows and so here obviously I've got my my source code with my assembly and if I go control X 2 again it will cycle through and then I can get my registers as well and you can change what you're seeing so I can go tu I reg that's the general purpose registers but if I go to you I would float and it will show me the floating registers so you can sort of set things up so you just makes it much more convenient you don't have to spend all that time looking through and in flipping back and forth control X 1 I'll go back and let's go control X 1 and we go back to our source code and the other thing you'll find that you can do in this mode is by doing up doing up arrow here and it goes up and down which actually is really annoying because what certainly I do a lot of the time is you go up arrow in order to get the previous command right and and now you can't do that anymore but turns out I'm I'm you found this out really recently you can go ctrl P and then I can get my previous command so ctrl P and n into your I mode goes up and down my command history I think you can do ctrl P and n normally in CLI mode but but I certainly I would just normally use up and down there yeah oh yeah so you should just be able to press J and K and then it would be alright yeah I've been I don't know I don't know what's I don't know what's wrong with the world then so so there you go um so that's T UI how long do we have the kind of reaching through now the other thing I think this the second probably most useful thing to look I think a lot of people know about now but still not everybody is is there's a Python interpreter built into the gdb any anything since version seven and so this is very very powerful right so I can do just type Python and then I don't know so you can do print in a world Oh at least like a type and and the Python works alright now if I go I'm going to come out of to you I'm exists sometimes a bit clearer so I can and I can type multiple lines of Python and you can do it's pretty much a full Python interpreter at least as long as your gdb build isn't messed up which sometimes we encounter quite a lot actually but import OS and then I can print my pit is start get good right so you can do you know anything that you want to do in Python and actually you can you can define functions that you can then call from the command line and you can even define functions that then you can make become sort of first-class gdb commands which is kind of cool and it's not just that there's this Python interpreter sort of sitting there that you can just do stuff it's really tightly bound to gdb itself so for example let me let's put a couple of breakpoints in so switch back to there so if I break at main and I don't know a break at line nine as well so if I in Python I can go let's print gdb breakpoints and that gives me a list of these breakpoint objects right and they contain all the kind of things that you'd want so I could do breakpoints take the zeroth one and I don't necessarily okay and so you can find out information about those break points and you get all the normal things enable whether they're enabled and there's advanced things set and you can even set breakpoints from the Python so you can go let's try and get this right by I just need to create a breakpoint object and I give it a location specifier so let's put one on line seven and it looks and now that breakpoints appeared right and you can see the breakpoints on there on the left here so by using these kind of thing and you can do you can find out information about what this is what the source file is and you can look up all your local variables and one of the very cool things you can do that I don't have time to show now is that you can do python pretty printers right if you've got these structures that you've made me just when you type print my structure you want it to look nice you can do that and in whatever way that you want another really I think quite useful thing is well I'm a bit biased here but reversible debugging now gdb has inbuilt reversible debugging so you don't need to be a customer of ours to to take advantage of this our stuff is much much quicker and generally much more awesome but but still you have reversible debugging in gdb that works quite well so here I have a program that is just it's a bubble sort right very simple doesn't we do anything it just fills an array with randomized data and then it sorts it so doesn't even print anything out but I happen to know that there's there's a bug in there that doesn't quite go every time so if I run that in a loop it usually works and where this is the the peril of intermittent bugs when you're waiting for them but there we go right every so often it's eggsy's and ok it's made a core dump so that's useful so there it is so I can load that up so let's have a look I'm sort of usually do with a cool dump but we've got no stack information at all so clearly this is some kind of stack smashing bug and the core files of no use so in what I'm going to do instead and that runs no good now that informations all gone but if instead I it's not gdb like this and I'm going to actually this I'm going to put what I want to do is enable reversible debugging so that when it bolts I can step back a bit and then I can get the context right I don't need the back trace but I need to run it a bunch of times as well and obviously it's kind of tedious to do that by hand but you can set breakpoints and you can have commands that issue when those breakpoints are hit so if I set a breakpoint on main and I set another breakpoint on and I happen to know this is a bit of a hack but I happen to know that there's this act when it exits normally it goes through this line actually I probably could just do on the last line of main anyway that'll do and I'm going to apply commands to run when these breakpoints are hit so when breakpoint 3 is hit I just want to rerun and when breakpoint 2 is hit which is the one main sorry command - I want to enable recording and continue now oh hang on before I do that I want to set pagination off because that will get annoying and now when I rerun what it's doing is every time it hits the top breakpoint at main it's going to turn on recording and run through and then when it gets to the end it's going to just start again right and once again we have this kind of indeterminate way because this is a an intermittent bug but if we're patient enough eventually it doesn't get to the end it's stopped in hyperspace and I can start to look around at what's going on so I can look at my program counter ok so I'm there if I do X to look at the memory there we can't access that memory I appeared to have to have jumped into hyperspace as we saw the fallback traces is kind of garbage but I can reverse step I which is like step I but just going to go back one instruction and suddenly tada we're back in sensible land now I have a stack that works and you can look here I'm at the end so what's going on well I can look at the disassembly of the code and I can see this little arrow at the bottom tells me that I'm trying to issue a return instruction so not that surprising if you think we've got some kind of stack corruption well let's have a look what's going on there so if I look at the stack pointer your stack point is that so return on x86 at least will will will return to the top of the stack and if I at the top of the stack sure enough that is the bogus address that was that we were jumping to so someone's clearly written rubbish on top of my stack so how do I know what that is well if I watch that right watch point said sometimes called a data breakpoint and the idea is you set though these watch points and usually what you do is you run forwards until the data is changed what this is going to do is run backwards until that location in memory changes which is going to be who's written to my snack which ought to have been when I entered the function but we suspect and what we've seen is not and I'm going to reverse continue and so now I've gone back in time to when that snack was modified and perhaps slightly unsurprisingly it's when I'm writing into my array and let's have a look what's I at that time to have full data at this point I can see is 35 and if I do my very anoying up arrow will see that the array is only 32 elements long and of course the bug is is that we've got this we're using percent size of array when we should have used you know that's the number of bytes in the array not the number of elements and so there was three very quick things there's loads and loads of other stuff I just ignored my slides but is there's there's loads of cool stuff I'm going to do a longer open content session tomorrow well I'm still won't have time to go into all of this but I'll go into more of it and and some and some other cool stuff as well so thank you for your attention
Info
Channel: CppCon
Views: 191,585
Rating: 4.9249449 out of 5
Keywords: Greg Law, CppCon 2015, Computer Science (Field), + C (Programming Language), Programming Language (Software Genre), Bash Films, Conference Video Recording, Event Video Recording, Video Conferencing, Video Services + C (Programming Language)
Id: PorfLSr3DDI
Channel Id: undefined
Length: 14min 46sec (886 seconds)
Published: Wed Oct 21 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.