 Hello everyone. So, let's talk about the debugger which has been provided with the links. There are some other debuggers also, but I have chosen ddd. GDP is the command line version of ddd. So, ddd has GUI. So, I have chosen ddd. I got a tutorial from the University of Tufts site. So, that is just for referring. So, we will talk about roughly what is how to attach a simple program to ddd and inserting break points, how to hover over a variable and see what values are there in that variable, displaying data, watching, watch points, etc., going backwards in execution and conditional break points and some tips. We will conclude with that. You can open ddd on your machine by typing ddd. Generally, when you open ddd, you will get the screen divided into three windows. The first one which looks like a grid is the data window. Whichever data variables you are watching will be displayed in that window. The main portion, this thing is the code window and the last portion is the command line. You can enter your execution time, arguments, etc., on this command line. So, let us see how to attach a simple program to ddd. I have a program called binsearch.c. So, minus g is the flag which will enable debugging. So, remember whenever you want to debug a program, you will have to compile it with minus g flag which will construct a list of debugging symbols which is used by ddd. So, minus o is one other option which is used for giving a file name for output file different than the default one which is a dot out. So, I want something called binsearch. The program is compiled with minus g option now. The name of the executable that is created is binsearch. I can just try out one simple thing. So, the number found is four. There are some other messages you can ignore. I have just put it to demonstrate the use of ddd. So, let us try to open this program with ddd. With ddd, you do not open sources but you open executables. So, ddd binsearch not binsearch.c. Binsearch.c is the code, binsearch is the executable. But for this executable which code is to be used, ddd already knows that thing because of the g flag. We have the binsearch code here. Any problems? So, this is the code that has been compiled with the minus g flag. The first thing that comes to the mind is let us say what happens if something goes wrong. A segmentation fault or you know something runtime errors like it is doing unexpected things. So, what do you want to do? Loops and functions are places in the programs where most of the errors happen. Either writing some conditions x equal to equal to 0. You want to write x equal to 0. That becomes an assignment which is always 2 and you end up going into an infinite loop or some other situations where parameter passing does not happen properly. You assume something and it does something else. So, break points are the things which help you analyze these things, such errors. Let us say this is a loop, for loop which starts here. I click on the line and insert a break point here. It is shown by a stop symbol. So, that indicates when you run the program using ddd, ddd will stop at that point and from that point onwards you can get a step by step execution, a typical debugger thing. So, I can insert one more break point if I want which will be here. What else do I need to do? I need to watch, let us say what value found is assigned, what value x is assigned, whether scanf is working properly or not. Maybe here it is just a scanf statement. In a program, bigger program you might be scanning a string, ending the loop on end of file character or something like that which does not always work out properly. So, you may want to keep a watch on the value of the variable which is being assigned through command prompt. So, let us say I want to watch the value of x here. So, one method is I just run the program. I get a remote control kind of thing here. So, there is a run button here. The program has stopped at the first break point that I inserted. Now I can use step or next to continue step by step execution. So, what is the difference between step and next? Next will always take you to the next step that is next statement and step will step over the functions. So, if there is a function call, when you do step you will enter into the definition of the function and then you will continue the execution from there step by step and come out of it whenever that happens. But for next, if you use next it will step over the function, skip the call and come to the next statement to the function call. So, that is the difference. You can decide what you want to do. If you want to debug what is happening inside a function, you can go inside the function. Otherwise, you can skip over. So, this is important as in you might want to debug some parts of the program, not all. You do not want to waste time in clicking next 1000 times. So, that is important in that way. So, let us go step by step. Now the debugger is asking me for the list of you know elements in the array. You can enter it in the command prompt window just as you do in the normal execution. When I press enter, continue with the, I can continue with the step by step execution. Now this thing shows me at every iteration, you can point your pointer at the array and you will be shown the content of the array. As you can see, only one iteration has happened till now. So, the first element is, sorry, two iterations I guess. So, the two elements are initialized. All others contain garbage values. If I press next now, let one more execution to be happened, iteration, the third element is now initialized. So, you can go on doing like this. So, I might not want to hover my mouse over the variable every time. I might want to keep it in my data window permanently. I can select a of i, right click on the selection and say display a of i. I will get the display in the upper part which is the data window. Now I can continue with this and I can watch the data window. You can see that each time although i is increased, so you can keep a watch on i also. Let us run the program once again and this time let us enter some different set of values. So, let us keep a watch on ai as well as i. So, this i is 0 initially and ai contains some value. One iteration has happened. Now we can, I can pull it. So, let us continue with this step by step execution. So, continuing with this, we have come to a point where DDD is asking for the element to be searched. It centers something. Now we can hover over the variable called found which has been not been set at. Let us enter into the function call. Now here this might be a failure. So, this might go till the end of the whatever 10 iterations or 5 iterations. I do not want to do that. I am out of the function. Let us say what is contained in found minus 1. So, this is a failure search which is indicated by minus 1 in found. I can get to the end of the program. So, there is one more thing to be added to this. These break points can be conditional. So, I can rightly kind of break point go to the properties and insert a condition. So, now this break point will be activated only when the condition is met. Like for example, here the condition is, you can see that in here enabled stop only if found is not equal to 1. That is stop only if the search is successful. Otherwise you can go to the end. So, this is like conditional break points. You will want to execute certain branches taken, not the other branches taken. So, may be this is a useful thing. Lastly a tip on how to set break points, how to use break points. Let us say you have a 10,000 lines of code which has some 10, 100 functions, function calls, thousands of loops and you just want to, you just do not know. The error is not giving you any clue as to where it is going wrong. You just get a segmentation fault which happens fairly every time using GCC. You do some unauthorized memory access and you will get a segmentation fault. Core will be dumped. You will get no clue what is happening. So, what do I do in that case? Where do I insert the break points? I cannot insert break points at every line or 10,000 lines code. I do not have any clue what is going wrong. The first declaration part or the last printing part. So, what I can do is this. That is why I chose bin search, binary search. So, same algorithm can be applied here. Insert a break point in the middle of the program. See what is happening. Look at the data values that have been set, unset before the break point and look at what happens after the break point. You will get a clue what is going wrong before the break point or after the break point. Then you can continue in the other half, whichever you decide, kind of binary search. There are so many other options with DDD. I cannot just explain these here. So, I have just chosen those which are obvious and very easy to explain. But I would like to add to this. I have used DDD as well as Turbosy. Turbosy in of course my colleges. But the power of DDD is much more than TC. TC is anyways of ID. So, it has editor, it has debugger, etc. DDD is just a debugger. We have debugged programs like Postgres, which is let us say 2 lakhs, 3 lakhs lines of code with DDD. We can easily find out the code flow, where it is going, control flow, everything. There are some nice interfaces to print out the code flow, print out the diagram, that control flow diagram, something it is called. So, there are so many interfaces, so many plugins you can insert into DDD. Overall, you can change DDD as you want. It is open source. When you add display to a variable, you can just right click on it and you can add. Same with arrays. For arrays, it will be displayed like a table. But for pointers, like I have this V here into V array. So, if I right click on V, I get two options. One is display V and the other is display star V. It is not visible here. But it is one is V and the other is star V. So, you might want to display the contents of a pointer as well as the data. If you want the address, you can print V. Otherwise, you can print star V. That is one point that I forgot. So, that is all about it.