Debugging -
RajatPawar - 08.03.2013
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
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
Re: Debugging -
MP2 - 08.03.2013
If this already exists on the wiki, wouldn't it just be easier to link to it rather than creating an entire new one?
P.S. I wrote the wiki one.
Also
Quote:
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 !
|
Not entirely correct. Perhaps you want to see what parameters get passed to a function - doesn't mean the code won't be executed.
Quote:
Correct. Errors seemingly stupid can crash the compiler.
|
You're telling me the script, running
AFTER COMPILATION, is crashing the
COMPILER? Okay. And how are the errors 'stupid'?
Quote:
The most useful debugging method is indention.
|
Indentation isn't debugging.
Quote:
This most certainly will crash the compiler, because it's outside any function and callback.
|
It won't crash the compiler. The compiler will produce an error. You can't debug something if the compiler is already telling you the exact line the error is on.....
Quote:
How to debug loops?
You can't of course
|
Why not?
Wiki tutorial:
https://sampwiki.blast.hk/wiki/Debugging
One thing you overlooked (and I must admit myself also on the wiki tutorial) is that debugging isn't JUST for crashes. If you have a bug in-game, such as when you enter a weapon pickup you get teleported inside a house, you need to debug that to find out what's going on. Clearly your pickup IDs are mixed up, but you'll need to printf some stuff to find out what exactly is going on.
Sorry for the negative feedback, but I think negative feedback is more important than 'AWESOME JOB MAN AMAZING LOL REP ++++'.
Re: Debugging -
RajatPawar - 08.03.2013
1) Err yes, I did see it, but it's quite complicated for new guys. Me, for one, to be honest, I gave up on the killing spree code on the wiki with all the arrays and things. But you have done a commendable job on it.
2) Also, I have tweaked the definition.
3) My language was wrong there, I meant to say something else. Stupid errors can be an extra brace somewhere, a missing semicolon, and stupid errors crashing the script could be initialising a new variable and assigning it to a CreateDynamicCP, for some reason, that crashes me.
I didn't mean to say 'stupid' as in their intensity - I just meant the frustration behind the small/silly mistake you make.