GDB Debugging - Displaying x86 Assembly from C program and stepping through instructions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'll give an example of how to write a C program and using the gununu bugger will spit out the assembly language for that C program now this has a couple different tasks that it can accomplish I find it useful for a number of reasons the first is for debugging and the second is for looking at the assembly code if you want to learn how to code a specific instruction or task and assembly and third to learn x86 assembly language so let's get right into it we'll create a simple program just to load up and load in the debugger and display the assembly so let's create a program called ASM example dot C and do the skeleton okay all I'm going to do is a couple very simple variable assignments and I'll do an addition or two and then use the print function just so we have a function call so we can see how to do that so what do we na equals 1 B equals 2 and C equals 3 and let's do a e+ equals b a equals a plus c which are the equivalent and then finally let's print the value of a and let's save it and we will compile it with the - g flag so that we can do our debugging the name of our program and we use the - o flag to name our program ASM example and we just compiled it and we are ready to run it so let's use the gdb command with our program name to load up our program in the canoe debugger so let's get right into debugging we'll use the list command to display our source code you can see our C source starts at line 1 and goes through 16 so we'll do info 1 16 to say let's start at line 1 go all the way to 16 and sorry use the list command 1 16 and you can see our entire see source and real quick let's check our breakpoints we have no breakpoints set and in order to display the assembly for this example we'll just create a breakpoint when we enter the main function so let's do B main info B and you can see that we have our breakpoint on the main function at line 6 so let's get right into it and run our code and you can see that we broke at line 6 where we're setting the variable a to a value of 1 so let's get right into showing this M bleak code so the command is disassemble so it's shortcut for that so it's D is a SS and here is our assembly language so this assembly code is the equivalent of this C source code and real quick I'll just give an overview of the columns you can see each of these as an instruction that the compiler generated from the C source code and they're executed in sequence and this column here is the memory location of the instruction so this push instruction is located at this memory address so it's basically pushing the EBP register onto the stack and then this move instruction is at this memory location here's the offset from the previous instruction so you can see this instruction is you know one byte off this one is three bytes off this one is six and you know it's basically just the offset from the beginning of the the function from the first instruction so the third column is the actual instruction and the next one or two columns are the arguments to the instruction so some have one some have two for example the push is just pushes this is pushing a register the EBP register onto the stack this one is moving the the ESP register the stack pointer register into the base pointer register this is performing an function to subtract this is just moving so this command here is the equivalent of setting a so this this is actually a hard-coded value one which is moving into this memory location offset of 14 hex from the stack pointer but I'm not going to get into too much of the instructions but you can look them up and get more information about them and you can see here this call when in assembly whenever a function is called we get the call function and you can see that's memory address this instruction is located at this memory address and it's calling the printf function and that printf function resides at this memory location so real quick I'll show you how to step through assembly language code and very similar to stepping through C source code the command that we'll want to use to start off with is the next but when we're dealing with instructions instead of C source code we may have multiple and structions if you just want to execute one individual instruction you use the next I command you can see the next instruction that will be executed marked by this equal arrow sign so let's do next I and then use this symbol command and you can see now we executed this is the command we just executed so if we use the next die again it'll execute this so now we are a should have a value of 1 and let's print the address of a and that is the address of a which is an offset of 14 hex from this stack pointer if we want to print a memory on location we use X followed by the format specifier I'll do a digit and we can do the these values in the parenthesis is a register so you use the ESP plus the offset and you can see that we just printed the value in memory there but I don't want to go too deep into that so we can keep using next I and the disassemble command and one thing to point out is an assembly which is very useful is the instruction pointer so if you want to look at your registers you can do info reg and this gives you the value of all your registers and of course I'm on a 32-bit system so these are the 32-bit registers and one of particular interest is the EIP which is your instruction pointer which tells you the next instruction that's going to be executed so if anytime you want to know it's going to be executed next you can look at this instruction pointer and see that 804 8436 is going to be executed next and that corresponds to this instruction here which is going to be executed if we use the next die so I'll keep using the next die and I will keep going so you can see now we're moving you move instructions are being called and this is actually moving values that are being pushed to the function but that's we don't need to be too concerned with that so we can use the next die next I and you can see I'm just trying to get to this call to illustrate how we can step into this function or step over similar to how we did with the C source so let's do next I so now we're at the point where we're going to call this printf function and we can take two options here there's two options we can do we can go into the printf function and debug the assembly language inside the printf or we can step over it so I'll show you how to step over it so if you don't want to go into the function you'll use the next I and we just stepped over that function call and we're going to do the return so this move will return it we're moving the value 0 onto the EAX register and I'll continue and that shows how we stepped over the call function so again I'll just give an example how to step into the printf function if we so wanted to so let's do info B so what I'm going to do here is I'm going to delete that break point d1 and I will set a breakpoint at line 12 so we'll go right into the printf function so we'll do B 12 and let's go ahead and run our program and you can see we just broke at the printf function in the source code so let's display our assembly code and we'll need to execute a couple instructions to get to the function and here we are at our call instruction so I'll show you how to go into the printf function if you still wanted to do so so you can use the step I which is similar to the step command which we use for source code so if you want to step into an instruction until a function of an instruction you can use step I and now we're actually in the printf function here so we're in this function and you can see the commands that are going to be issued in that printf function and we can do continue just I just wanted to show how you could go into it and our program continued running and printed the value of a so I'll include the the source code to this little example if you want to try to step through some of this and hope you hopefully find it useful thanks
Info
Channel: systempeeps
Views: 63,844
Rating: undefined out of 5
Keywords: C++, C programming, c programming language, debug, gnu, gnu debugger, gdb, assembly, x86, x64, step, functions, linux, windows, ubuntu, tutorial, overview, programming, scripting, troubleshooting, coding
Id: wIuZajISL-E
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Tue Jan 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.