 now we will see how to debug our programs with ddd. So, what is ddd? ddd is basically for monitoring your program execution many times after writing the C program you have to debug it. So, may be it will be for C program C plus plus java ddd provides a common interface for all these debuggers. So, it is basically a front end it internally you just other available debuggers and gives you user friendly interface. So, we can use it for debugging executable binaries output of our gcc or g plus plus compiler. We can also use it for java white code output python pearl many support for many different languages is available. So, how to invoke ddd simple command ddd the other way is we can directly specify executable as a command line argument or you can attach some running program to ddd we will see that later. First let us start with a simple C program debugging we will switch to demo directly. So, that we can better understand it just start your browser mozilla browser and enter this path in your browser to get a sample C program once you enter it will ask you whether you want to save the file or not got the file just go to the save as file save as and save it in the current folder desktop folder the save file do not save it as a web page save it as a dot c file only after that just open the file from g edit open the terminal window if you do not have go to the terminal window and move to the desktop folder by default you will be here do cd desktop and vim sample dot c. So, this is a normal c file for bubble sort the simple bubble sort on array let us just walk through this code briefly go below this is the main starting point arguments, arguments passed are all elements in array and we want our program to sort this out here we are allocating an array for the elements this loop converts the text into the numbers using function a to i. So, it converts from string to integer the arguments that we have and this is main bubble sort function we are passing array to it and size size of array and this is simple printing let us just go to the bubble sort quickly. So, arguments are array a and size that is 0 to the number of element last element in the array and this is the simple implementation of bubble sort. So, now let us compile this program and we see how it works using ddd. So, just get out of vim and compile this program using gcc hyphen o sample dot o. So, that our output file will be sample dot o. Now let us run this program arguments are the elements we pass yeah. So, that is the bug we have to find using ddd. So, as you can see 4 elements we have passed our 10th, 20th, 10th, 30th, 25th and as a output we are getting 0. So, we will use now ddd to find this bug. So, use ddd sample dot o one step we imaged out whenever you want to debug any program using ddd you have to specify option g while compiling it. So, here specify gcc sample dot c and hyphen g hyphen o hyphen g enables the generation of debugging debug information in your executable. So, that ddd can debug it. So, do not miss this option otherwise ddd will not be able to debug your program. So, now we will do ddd sample dot o and now you can see the sample dot c is already open for you. So, this is your source window and this is debug window as you can see there is a prompt gdb. So, ddd internally uses the debugger known as gdb for debugging your program. So, let us just run this on the gdb prompt you can type run it has exited let us go for the menu is first. So, from the program menu go to the program menu and select run now program run. So, it will ask you for arguments here give any 4, 25 and say run. So, now in the gdb prompt you can type gdb prompt you will see the output it is same as what we had earlier. Now we will start debugging this. So, first thing that we want is a program should stop initially for us. So, we wanted to break on the main. So, click on main this is the arguments window it will immediately show you the text that you have selected and then go to the break and selected and you will see a break point has been inserted on the first instruction in your function. Now, again go to the program run arguments are already there last times arguments and choose run. So, now the program has stopped on the break point we have selected. Now from this point onwards we can go through stepping a program we can monitor data values and all. So, let us go step by step. So, here we are allocating array here the green arrow indicates that your execution has stopped on this line. So, let us go to the view menu and click on command tools. So, you will get this window that gives you all commands for executing this program view command tool. So, now the there is a next button in this that allows you to go to the next line. So, clicking on this your execution has moved forward the array a has been allocated using my lock and now this for loop is extracting the text from your command line into your array a. So, let us monitor contents of this array a for that click on a and in the window type in your arguments window type a of 0 a into square bracket 0 at the rate bracket arc c that was the first argument to main arc c minus 1. So, we are just saying it to display the contents of array a and its length is arc c minus 1 click on display and you will see array has been displayed here this is your data window it shows contents of the variables. So, I suppose everyone is now able to see this in data window contents of array a. So, now right click on this and select rotate. So, it will show your contents horizontally you can view it in one view it will just show it horizontally it was being displayed vertically it will show that horizontally. So, now as you can see contents are all 0s. Now, this for loop is going to convert 1 by 1 or element extract it into the array a make sure you have this command tool available you can display it using view command tool. Now, just click next. So, it will go to the first step and as you can see the element 20 has been extracted into array a. So, we go on extracting every element 10, 30, 25 and it has come out of loop. So, here we ensure that our the arguments that we have passed on the command line are getting extracted properly will go through this function directly will go over this function. So, click next and it goes to the for loop now just look have a look at array contents it is 0 10, 20, 25. So, this means this function bubble sort has some problem it is adding 0 to our program. So, we have to go we have to analyze it through. So, let us finish execution of this just click click on continue. So, program will run and finish. So, program exited normally and now run it again it has stopped there will go through this again now you can use until command. So, that the program will complete execution of loop and will stop on the next line as we have already monitored how the for loop works and we know it is running correctly we directly want to move to the next line will click on until. So, it has come out of that loop directly doing all iterations in that loop everyone has reached here bubble sort function. So, the array contents we have extracted arguments in array and now we want to step into this bubble sort remember that when we click next the control got to this for directly now we want to debug this function through for that we have to use step. So, step goes through the function. So, as you can see now the control is in this bubble function. So, it has stopped on the first line in bubble. Now, as you can see this array a has disappeared from data window this is because our stack frame has changed we have lost reference to the variable a that was allocated by earlier function we are on different stack frame now you can always view the stack frames using the status back trace click on status back trace and you will see two stack frames there this one and other one. So, one is the main command main function and this is the second call second stack frame you can just see the contents of other stack frame if you click on main the array a reappears. So, it is the array a is valid only in that function and in bubble sort it is not there. So, now we need to monitor a that is in bubble sorts context. So, let us add it again to data window similarly click on a into square bracket here in the argument window type a into square bracket 0 at the rate and specify size into the bracket and click on display you can also add size to the display like this you can add as many variables you want to the data window and you can monitor their execution then everyone getting this. So, now as you can see size is 5 and array already contains 0. So, there is a problem the arguments to the function the size argument to the function is probably missing we have to give 4 rather we have to give arc c minus 1, but we are passing arc c and it is taking the last element also let us debug through this for loop and verify this. So, command tool will use next for this. So, the first iteration and see it has swapped 10 and 20 as we know in bubble sort in every iteration it puts highest element to the end. So, again the second time. So, it has swapped 25 and 30 and now let us monitor this array closely and it has swapped 0 and 30. So, that means it is treating 0 as one of the elements in array. So, there is a problem this is because the size that we have passed is wrong. So, let us try to correct this and see if this works properly click on finish. So, it will come out of loop finish forces it to come out of the current stack frame that is the current function. So, after clicking finish it comes out of bubble sort directly and as you can see the output is 0, 10, 20, 25. So, there is the problem let us click on continue and now we will try to correct this problem. So, run it again with the same arguments will go to the bubble sort using step and now size is 5 and our prediction is size should be 1 less than what it is. So, let us try to set it here itself we will try to mutate it here and see if the program works fine. So, we will click on size it is there in the argument window and click on set it will ask you for a value the current value is 5 and we think that it should be actually 4. So, change it to 4 and click on ok. So, as you can see the size is 4 now we have changed it and now let us try to run this program normally we will click on finish. So, that the function will complete and now as you can see the array contents are fine. So, this means that the size that we are passing is 1 greater than what it should be there is the bug. So, we will now come out of this let us say continuous and let us close this ddd file exit open the sample dot c and go to the line calling bubble sort and change this arc c to arc c minus 1 just save and close this and now we will compile this again and now we will try to run this again with the same arguments and we are getting correct outputs. So, this is how we identified bug using ddd and we corrected it. So, we will just quickly go through these slides to see some additional things we can do with ddd. So, as we have seen invoking you can just give ddd and then open your file later the other way that we used is ddd executable and you can also attach ddd to the running program you just have to give process id and ddd will allow you to debug the running program. So, the syntax for this is the last one ddd program and pid you have to specify for quitting as we already know we are quite familiar with file and exit you can also enter gdb on the prompt quit on the gdb prompt I will just go to the ddd for better demonstration you have this gdb prompt here you can also type quit here to get out of ddd other way is pressing ctrl q. So, we have to use hyphen g for compiling then you can start ddd first and open your file later using file open. So, you can specify executable file there and you can debug it alternatively if you can also give source file dot c file and you can debug it. Now, this is one interesting thing looking up definition ddd allows you to look for the definitions of functions it allows you to browse through your code easily. So, I have seen the call for bubble sort here and I want to locate where definition of it is in source. So, just click on bubble sort and click on look up and it will automatically navigate you the definition of that function click on the function you want to search and click on look up now stopping execution as we have already seen we can set break points to stop execution. The other thing is watch points will not go through details of it but as we had set a break point on some instruction you can also set watch point on some variable so, that whenever value of that variable changes your program execution stops. So, maybe you can set the watch point on some array element and whenever its value gets modified your program execution stops and you can say where it is getting modified. So, that is watch points you can if you have some problems like infinite loops or something you will never get to the ddd terminal whenever you run it. So, you can use escape there to stop execution program how to run we have already seen the execution window is the other thing we have seen that the output appears here in gdb window the other way to view is program run in execution window and ddd will create a separate window for you and show output there it just to see how your output looks like when your program is executing. Examining data as we already seen there is something like value tips break on the main function again and I will run this I will run it several steps. Now you do not always have to create a data viewer something to watch the variables simplest way is just take your cursor to the variable you want to see and it will show the contents. So, arc c is 5 it is showing it will also show contents on the status line here as you can see arc c equal to 5. So, this is the simplest view to monitor way to monitor your variable values. The other way is print you can always print some variables on gdb prompt just type print and say arc c any variable you want to see value and it will print the value for you otherwise just click on it and there is a print tool available it will show the value in gdb window. We have already seen how to examine stack using back trace option then resuming execution the next we already seen it goes to the next execution line step also goes through the next execution line, but it allows you to debug through the functions next we will go over the functions it will directly execute function and go to the next line, but step will run through the function for you then until we already seen then continue continues execution normally from the current point and kill if you want to terminate your program abnormally you can use kill. So, I mean the running context and I am done with debugging I can directly say kill. So, the execution will stop there only now there is some interesting feature undoing program execution means just like you do undo in your other editors or something you can do undo in your debug it does not actually go backwards through your program, but what it does is it saves the execution status of every line and as you do undo it will show the previously saved execution status. So, you can go backwards in your program you it is it feels like you are running program backwards. So, it is also useful sometime. So, that is all about gdb you can always refer to the ddd manual for more details everything is given there in detail that is it. Thanks.