[Tutorial] Debugging
#1

Hello !
As the title says, I am here to share some knowledge on debugging and I expect this thread to be a contributed one - since every person has his own ways of debugging!
So:

1) What is debugging?

Quote:

Yes, it's from the wiki. Debugging is the process of adding temporary (or sometimes permanent) messages into code to give a visual representation of the script's process.

So, to simple it up for ya'll, it's adding print messages at some places in the code to see if they get executed or not !
Well, not always, as I have specified below, there are tons of methods too, but this is the most common one.


2) When can debugging be useful?


Quote:

Errors in code can cause the code to stop at the error and skip the rest of the code, or even crash or freeze the server. These errors aren't picked up by the compiler, as they aren't errors when the script is compiled. An example of an error can be found below.
It can be hard to identify and locate an error in big scripts, possibly taking months. This is where debugging becomes extremely useful.

Correct. Small typos seemingly not harmful can crash the compiler. So, the whole script ain't executed. You don't know what's causing the error, so how to go about it? Debug it FIRST, Skype a friend SECOND, spend at least 10 minutes over the code THIRD, and if you can't still get it, LASTLY, post on the forums.


3) Why should I debug?


Why not? If it saves you headaches, go ahead.
Say you have a 10,000 lined script and something doesn't work, you have no idea. What to do? Add print messages over it and see if it gets executed, which gives you the idea of what gets executed, what doesn't, which mostly pinpoints a callback/function.


4) So debugging means adding print messages all over?


Nah. Debugging can mean a lot of ways to many people, most of them being invented. I, for one, have never faced such a huge problem that I had to invent a whole debugging method on my own.
The most useful debugging method is indention. It helps you A LOT, believe me, A DAMN LOT!
For example:
pawn Код:
for(new i; i < 50; i++){
 if ( i % 2 == 0)
 {
    printf("%d", i);}
 }
}
This will not compile since there's an extra brace. But this's a small code. Imagine huge codes ! It's very useful.
You could easily debug this if it was:
pawn Код:
for(new i; i < 50; i++)
{
    if ( i % 2 == 0)
    {
        printf("%d", i);
    }
}

5) How to actually debug?

  • Check out the code, see the recent changes you have made to it.
  • If the code was fine before you added something, it obviously means the recent code you have added is bugged.
  • Add print messages all over the new code.
  • See the output.
  • You will be almost getting a pinpoint line where to look for a bug.

6) Examples

pawn Код:
public SomeCallback()
{
    return 1;
}

new some_coordinates[][3] =
{
   pos_X, pos_Y, pos_Z,
   pX, pY, pZ
};

new random_pos = random(sizeof(some_coordinates));
This most certainly will crash the compiler, because it's outside any function and callback.
So basically, you can debug it without anything here, you take the line out, see if it compiles, it will, so you know now, that the line is damned.

Example 2:

pawn Код:
// Credits to babul.

stock func1(a, b)
{
   a += b;
   callfuncb(3, 4);
   return 1;
}

stock callfuncb(c, d)
{
   c+= d;
   func1(1, 2);
   return 1;
}
This will get called INFINITELY until the memory stacks up and it will freeze in sometime (I guess.)

How to debug loops?
You can't of course, add print messages since you would basically receive infinite messages. So here, you are expected to apply logic and maybe, check out ******.

3) Example 3:

pawn Код:
#include .....
new bool : god_mode = false;

CMD:..(playerid)
{
 switch ( god_mode)
 {
   case true: #error
   case false: SetPlayerHealth
  }
}
So, this will allow you to go into godmode always. Debug? How?
Simple. Add printf messages all over!
pawn Код:
#include .....
new bool : god_mode = 0;
public OnFilterScript..()
{
    printf("%d", god_mode);
CMD:..(playerid)
{
 switch ( god_mode)
 {
   case true: printf("%d", god_mode); #error
   case false: SetPlayerHealth.. printf("%d", god_mode);
  }
}
You are gonna recieve
Quote:

0
0
0

Because you never set it to true in the case: false where you set player health to infinity !

If there's any major crashes, random crashes, debug CALLBACKS.
pawn Код:
public OnPlayerSpawn(playerid)
{
       print("OnPlayerSpawn executed!");
       return 1;
|
And so on !
----------------------------------------------------------------
So that's how you can debug code! This is only a small part of debugging.
I am waiting for them ingenious members out there to share their ways of debugging.
Cheers
Reply


Messages In This Thread
Debugging - by RajatPawar - 08.03.2013, 13:11
Re: Debugging - by MP2 - 08.03.2013, 13:13
Re: Debugging - by RajatPawar - 08.03.2013, 13:19

Forum Jump:


Users browsing this thread: 2 Guest(s)