[Guide] How to Debug Your Server

09/18/2013 22:01 Spirited#1
Who is this guide for?
This guide is for beginning programmers who care about how their source is made and would like to learn how to debug their source properly. This guide is not for naive members who think they can run a server without any programming knowledge what-so-ever.

Introduction:
So, what is debugging? The formal definition is "Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected." The story behind it is that Grace Hopper (a computer scientist in the 1940s when punch cards were used as input, output, and programming) found a moth stuck in one of the holes of a punch card. Upon removing the bug, the program worked (thus why we call removing problems "debugging").

Debugging by Comparison:
One method of debugging is by comparing expected output with actual output. It's a very simple thing to do, output the actual values in variables as your program executes using print line / write line statements, then compare those values with your expected values.

Debugging in Visual Studio:
A lot of IDEs (Integrated Development Environments), such as Visual Studio, offer a variety of debugging tools. In this guide (since most of the community uses C#), I'll be showing you some of the tools in Visual Studio. First, let's prepare the project for debugging. Run your project in debugging mode. While the project is running, open the Debug menu. Under "Debug > Windows", open the "Locals", "Auto", and "Watch 1" windows. Drag each window to where you'd like them to be in the solution (I drag mine to the right side with my solution explorer).

Now that your project is set up, stop the project and find a line where you would like to start debugging from in your code (you can do this while the project is running as well). I've created a test project to debug, and will be starting in my main function after initializing an array with random values. On the line you wish to start from, right click and select "Breakpoint > Insert Breakpoint". You may also do this by clicking in the gray margin to the left of the line.

[Only registered and activated users can see links. Click Here To Register...]

Next, start your project (or have the project run the code where the breakpoint will be hit). When the project attempts to run the code where the breakpoint is, it will pause execution on that line and allow you to see what's stored in variables. Open your locals window or hover your mouse over variables to see what's inside!

[Only registered and activated users can see links. Click Here To Register...]

Keep the locals window open (it will change because we're now going to let it run to the next line). At the very top of the window in your toolbar, you'll see the debug toolbar (shown below).

[Only registered and activated users can see links. Click Here To Register...]

Hover your mouse over the buttons to see what they do. There are some very useful buttons there. To execute to the next line (called a step), or to step into a function (if the line you're on calls a function), use the "step into" button. To step over a function (if the line you're on calls a function), use "step over". Finally, if you want to step out of a function, use "step out" and the project will run the rest of the function and bring you back to the outside of the function.

Step through each line, one at a time, and notice how your locals change.

[Only registered and activated users can see links. Click Here To Register...]

Notice the yellow arrow to the left of the line you're on. I'm going to change the code above it, then drag the yellow arrow to the line I want it to jump back to (so I can run the same code again).

[Only registered and activated users can see links. Click Here To Register...]

Now, sometimes you'll have a lot of local variables to sort through. This can be problematic if you're trying to focus on a group of variables. That's where the autos window and watch window come in. The autos window will focus on values not only in variables, but in assignments. See the example below:

[Only registered and activated users can see links. Click Here To Register...]

The watch window will only show variables that you want to watch. To add a variable to the watch list, right click on a variable and select "Add Watch". Also, when you hover your mouse over a variable to show the value inside it, you can pin it to the page using the pin icon.

[Only registered and activated users can see links. Click Here To Register...]

You can also edit values in variables by double clicking on the value in the tooltip (popup value box when you hover over a variable) or in the autos / locals / watch window.

Conclusion:
There's more to debugging, but I don't want to make this too long. Hopefully this has been helpful to some. Sorry for how bad this guide probably is (I haven't proofread it). Good luck.

Edit: Unit testing information...
[Only registered and activated users can see links. Click Here To Register...]
09/18/2013 23:01 Super Aids#2
If you don't know how to debug, you simply shouldn't be running a server in the first place.
09/18/2013 23:44 Smallxmac#3
I know right! Debugging is very easy and is important to see if the information you want is really going though the program. Many people think debugging is just hitting the debug button on the source and see of an Exception gets thrown. Then they have no idea how to tell what the problem is because they did not do custom Exception...
09/18/2013 23:56 Super Aids#4
and because they use tries with empty catches everywhere.
09/19/2013 00:39 CriticallyDev#5
I don't understand..why wasn't this information shared years ago? When people actually gave a shat?

This guide is completely useless now for two obvious reasons:
1. "People" pay others to program-setup-run their projects.
2. Common sense.
09/19/2013 00:57 go for it#6
well unlike everyone else, i think that without this kind of tutorials this forum will fails even more, yes people who can't debug in visual studio should not own server, but the fact is that they do and the fact is that this will help them and save alot of useless threads
09/19/2013 01:47 Super Aids#7
They will still make useless threads.
09/19/2013 01:57 go for it#8
Quote:
Originally Posted by Super Aids View Post
They will still make useless threads.
you can't live life expecting that everything won't work out the way it was planned to be, that way no one will ever do something useful :)
09/19/2013 02:56 Spirited#9
There are very educated people in my life and in this community who were very interested in a guide like this. They learn quickly but they're scared to ask questions because of these types of responses from other members in the community. I was very scared to ask questions when I was teaching myself programming, and I've been doing my best to reverse that. This community should be open to all levels of programmers. Now, please keep on topic and please stop discouraging members (be more aware of what you're saying).