Some basics on debugging your source code, without the use of a debugger program.
I have been programming with C and C++ for over ten years now. I have used debuggers, but I have always had the problem of not getting them to work. I have learned how to debug without any debuggers. That is what I am going to share in this article.
What’s a Bug?
The first computer literally had a bug on the circuit board. You know what today’s computer bugs are right? It’s where your computer program is not working like it should. Sometimes it crashes the computer program, or the results do not come out correctly. Sometimes it’s just a situation where it sometimes works right, but then sometimes messes up. It’s not literally a bug (That can happen though). It’s a result of some computer code not written correctly.
Search And Locate
The first thing to do is find where the problem is. To do that we need feed back. Just for example, I will talk like we are writing a console program. In C and C++ we have printf, and also cout in libraries. We can use one of those two functions to locate or problem. Look at the following code example with comments.
int main(int argc,char** argv)//You do not need to understand engines to see my point.
{
while(EngineRunning())//do loop, while engine running.
{
RunEngine();//do engine runnning logic.
TurnPower();//alternator or generator.
UseFuel ();//Takes fuel to run an engine.
}
return 0;//leave with a good code.
}
This program crashes, and that is all the feed back I have right now. So What I need to do is find the location of the crash. Using cout I will locate the problem. Take a look at the same source code, but with debugging symbols.
int main(int argc,char** argv)//You do not need to understand engines to see my point.
{
cout<<<”Program Started”;
cout<<<”Enter While Loop”;
while(EngineRunning())//do loop, while engine running.
{
cout<<<”While Loop Pass”;
RunEngine();//do engine runnning logic.
cout<<<”After Run Engine”;
TurnPower();//alternator or generator.
cout<<<”After Turn Power”;
UseFuel ();//Takes fuel to run an engine.
cout<<<”After using fuel”;
}
return 0;//leave with a good code.
}
The way this type of debugging works, is as the program advances in the code, it will print out information. When the program crashes, all the output stops. The output just before the error message, or the crash message, will show us the location of the problem. Lets say the console output looked like the following;
Program Stated
Enter While Loop
While Loop Pass
After Run Engine
After Turn Power
Segment Fault: Program terminated, core dump.
That tells me there is a problem with the “UseFuel()” function call. I know that because UseFuel() comes right after “cout<<”After Turn Power”;” After printing that message the computer then crashes without another output. That points to the location of the crash.
The next step that follows, is to debug the function “UseFuel()” like I did the main program. The following code output shows the exact location, take a look:
void UseFuel()//gets fuel from the gas tank.
{
cout<<<”UseFuel() Entered”;
FuelTank.fuel-=10;//take away 10 fuel units. (gas hogg)
cout<<<”After fuel level down 10″;
*Engine.fuelintake+=10;//it’s a ten cylindor.
cout<<<”After Fuel given to engine”;
cout<<<”Out Function UseFuel”;
}
The output on the console with the new added debugging symbols would make the console look like this:
Program Stated
Enter While Loop
While Loop Pass One
After Run Engine
After Turn Power
UseFuel() Entered
After fuel level down 10
Segment Fault: Program terminated, core dump.
That output tells me that the problem is the line *Engine.fuelintake+=10;. The member fuelintake of *Engine is the bug.
Examining The Data
We know where the problem is, but we do not know why it crashes. This part of the article will show you a way to find why the program crashes. Let me explain what the fuelintake variable is. It’s a member of Engine that is an int*. What I will do is check it’s value, and because it’s a pointer I will look at it’s address. See the source code:
void UseFuel()//gets fuel from the gas tank.
{
cout<<<”UseFuel() Entered”;
FuelTank.fuel-=10;//take away 10 fuel units. (gas hogg)
cout<<<”After fuel level down 10″;
cout<<<”Address fuelintake == (”<<<”)”;//The Variable Watch.
cout<<<”Value holding == (”<<*Engine.fuelintake”<<”)”;//The Variable Watch.
*Engine.fuelintake+=10;//it’s a ten cylindor.
cout<<<”After Fuel given to engine”;
cout<<<”Out Function UseFuel”;
}
The output on the console with the new added debugging symbols makes the console look like this:
Program Stated
Enter While Loop
While Loop Pass One
After Run Engine
After Turn Power
UseFuel() Entered
After fuel level down 10
0
Segment Fault: Program terminated, core dump.
After looking at those variables I have found the problem. The fuelintake pointer had an allocation error. When the program did an allocation call, it gave the address zero (0). That means NULL, and it’s an allocation error code. The pointer fuelintake is pointing at nothing, and to read or write at that address causes a segment fault, or access violation. That is why it crashed just after checking the address with cout.
What I do after that is look at the allocation program, and also put in some error checking there. The error checking should have been added there already, but for some reason I missed it. This bug has made my program better. (Not a real program.)
No More Bugs
For a C or C++ console program I used cout or printf(). If I was programming with the Windows API I would have used MessageBox() to locate the problem, and get information about the variables. Another example I will use is the allegro programming library, I use allegro_message() for my debugger. What you use to locate and examine with depends on what your programming with.
Something that can be use other then console output, is buffer files. It depends on the language, but you have to close the file right after writing your debug output. Other wise you will end up with a empty file after the crash.
That shows you the general idea, I hope this article will help people out there with the debugging of their buggy software.












Leave Your Response