Raised This Month: $51 Target: $400
 12% 

[HOW TO] Debug your plugins


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 10-31-2014 , 12:38   [HOW TO] Debug your plugins
Reply With Quote #1


In my opinion this tutorial should not be written, but I am tired of "coders" who ask stupid questions, which may find the answer through some simple debug messages, and when ou tell them to do that they ignore you or say that they have no clue what that means. I have faced such situations when someone asked me why his code does not work. I found the mistake, it was obvious, but he may find the problem on his account if he would examine the code, of course the situation above described above happened, so I decided to write this tutorial.

What mean to debug your plugin ?
By debugging you find the probable errors, you find the cause, or lower the number of possible cases where the problem might occur. This way you know exactly what to fix and what does not work. Also, you will see the evolution of your own code and you can detect its weaknesses, possible mistakes that you did or places where your logic did not work and the code may fail.

This sounds nice, I know, but how you do that ? It's a very complicated thing, you need a lot of knowledge to be able to do that, and advanced coders have problems too when it comes to debugging. Just kidding, it's a very simple process.

The main idea is to put messages( server console, chat, hud, it does not matter, you put what is more convenient to you ) in your code, you'll see exactly what is running and what is not. Messages should be placed in key positions of your code, will help you understand how your plugin thinks.

Ok, let's take a simple example. Basically, it will swap the values ​​of variables, only if someVar is bigger than 0. If a > b the server will close, otherwise it won't do anything. This is a stupid example, but I can't think of something better for now.

Code:
#include < amxmodx > new someVar new a = 5, b = 6 public plugin_init( ) {     register_srvcmd("test", "ServerCommand_Test") } public ServerCommand_Test( ) {     new otherVar     if(someVar)     {         otherVar = a         a = b         b = otherVar     }         if( a > b )     {         server_cmd("quit")     } }

But the plugin above doesn't work, for some reason you need it to work everytime, imagine this scenario. And you can't figure out why it's not working by yourself, so you start adding debug messages.

So, you may do:
Code:
#include < amxmodx > new someVar new a = 5, b = 6 public plugin_init( ) {     register_srvcmd("test", "ServerCommand_Test") } public plugin_end( ) {         //You make your someVar variable value 1 only here, you think it's ok     someVar = 1         server_print("someVar value was changed") } public ServerCommand_Test( ) {     server_print("Command was typed into server console")     new otherVar     if(someVar)     {         server_print("The values of the variables will be changed")         otherVar = a         a = b         b = otherVar     }         else     {         server_print("The values where not swapped")     }         if( a > b )     {         server_print("QUIT. WORKING")         server_cmd("quit")     }     else     {         server_print("Doesn't work" )     } }

Running the above plugin will give you

Code:
Command was typed into server console
The values where not swapped
Doesn't work
What this tells you ?
  • Your command is successfully registered.
  • Your values were not swapped.

Now you start thinking, why the values were not changed ? Well, we change them in an if, and the condition in the if is false, if the block of code from else is called. Now you think more. Why the condition is false ? Because the value of someVar is 0. Why the value of someVar is 0, I'm setting it to 1. After some more thinking, you ask yourself: "But what if I'm setting it to 1 to late ?". You find out when plugin_end forward is executed and you find your problem. You are setting someVar to 1 too late.
And you fix the problem, make it 1 when the command is used.

Now, you get this:
Code:
#include < amxmodx > new someVar new a = 5, b = 6 public plugin_init( ) {     register_srvcmd("test", "ServerCommand_Test") } public ServerCommand_Test( ) {     server_print("Command was typed into server console")     someVar = 1     new otherVar     if(someVar)     {         server_print("The values of the variables will be changed")         otherVar = a         a = b         b = otherVar     }         else     {         server_print("The values where not swapped")     }         if( a > b )     {         server_print("QUIT. WORKING")         server_cmd("quit")     }     else     {         server_print("Doesn't work" )     } }

And after testing, you get:
Code:
Command was typed into server console
The values of the variables will be changed
QUIT. WORKING
I have commented the quit line, so I will be able to copy paste the messages. But you will see, that by running the above plugin your server will close.

Now, your plugin is running perfectly and you can remove the debug messages.

And now you will have a clean code, which works:
Code:
#include < amxmodx > new someVar new a = 5, b = 6 public plugin_init( ) {     register_srvcmd("test", "ServerCommand_Test") } public ServerCommand_Test( ) {     someVar = 1     new otherVar     if(someVar)     {         otherVar = a         a = b         b = otherVar     }         if( a > b )     {         server_cmd("quit")     } }

Why I have used server_print ? Because this is a command that will be send from server console, no point in using client_print or whatever
But, if you are testing some things in-game you can do with client_print.

Don't blame me for writting this and for the dumb example above, I'm too tired to find something better, I may edit later and add a real case where debug helps you.

In most cases this will only help you to find the concept problems, if you are using some natives wrong, it doesn't help at all.
__________________

Last edited by HamletEagle; 10-31-2014 at 13:24.
HamletEagle is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-31-2014 , 18:45   Re: [HOW TO] Debug your plugins
Reply With Quote #2

I wouldn't ever put a quit command in my debugging steps. It's unnecessary. I like to be able to upload a new version then simply type "restart" in the server console.
__________________
fysiks is offline
zmd94
Veteran Member
Join Date: Nov 2013
Location: Malaysia (9w2zow).
Old 10-31-2014 , 20:59   Re: [HOW TO] Debug your plugins
Reply With Quote #3

I just add debug at the end of .amxx file name and see the errors whether in the error log or in the console.

Then, I fix the problems line. Then, upload the new version and fixed code until the problems is solved (Certainly, we must "restart" the server).

Yet, I'm not sure whether this is the good way of debugging. ;)
zmd94 is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 10-31-2014 , 21:06   Re: [HOW TO] Debug your plugins
Reply With Quote #4

Quote:
Originally Posted by zmd94 View Post
I just add debug at the end of .amxx file name and see the errors whether in the error log or in the console.

Then, I fix the problems line. Then, upload the new version and fixed code until the problems is solved (Certainly, we must "restart" the server).

Yet, I'm not sure whether this is the good way of debugging. ;)
That works if your plugin is actually encountering run-time errors. If no errors are generated but it doesn't do what you expect it to, you will need to do the debugging described in the OP
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
zmd94
Veteran Member
Join Date: Nov 2013
Location: Malaysia (9w2zow).
Old 10-31-2014 , 21:18   Re: [HOW TO] Debug your plugins
Reply With Quote #5

Quote:
Originally Posted by YamiKaitou View Post
That works if your plugin is actually encountering run-time errors. If no errors are generated but it doesn't do what you expect it to, you will need to do the debugging described in the OP
I appreciate the explanation. ;)

Now, I got it. So, when there is no errors are generated but it doesn't do what I expect it to do, then I have to follow the above steps.

By the way, HamletEagle, nice sharing. I have misread this line at first:
Quote:
Originally Posted by HamletEagle
In most cases this will only help you to find the concept problems, if you are using some natives wrong, it doesn't help at all.

Last edited by zmd94; 10-31-2014 at 22:26.
zmd94 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-01-2014 , 08:56   Re: [HOW TO] Debug your plugins
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
I wouldn't ever put a quit command in my debugging steps. It's unnecessary. I like to be able to upload a new version then simply type "restart" in the server console.
It was not for debugging, it was the "original plugin" that was "not working".

@zmd, adding debug is a very useful step, but if your plugin doesn't have run time errors it won't help. By adding messages, you will see how your plugin works and why it doesn't do what you want.
__________________

Last edited by HamletEagle; 11-01-2014 at 08:59.
HamletEagle is offline
zmd94
Veteran Member
Join Date: Nov 2013
Location: Malaysia (9w2zow).
Old 11-01-2014 , 09:24   Re: [HOW TO] Debug your plugins
Reply With Quote #7

Quote:
Originally Posted by HamletEagle View Post
@zmd, adding debug is a very useful step, but if your plugin doesn't have run time errors it won't help. By adding messages, you will see how your plugin works and why it doesn't do what you want.
Yes, you are right.

By the way, I have learned new thing from this thread. It help me to improve my knowledge in debugging. ;)

Last edited by zmd94; 11-01-2014 at 09:25.
zmd94 is offline
GuskiS
Veteran Member
Join Date: Aug 2007
Location: Latvia
Old 11-03-2014 , 06:19   Re: [HOW TO] Debug your plugins
Reply With Quote #8

I prefer using log_amx, shorter and logged in log file, not sure that server_print does that.
As for example, you are missing plugin_end() in some of your examples, for me, it was hard to understand what is happening when you show actuall not working code, then do debugging code with plugin_end in it, and after that again missing that plugin_end().
Better example of debugging usage could be new round counter, when server decides to restart = counter goes on, but round is first and all things affected by that counter would fail if you would check for first round, for example. (Had thoses issues myself, hehe).
__________________
Finished mods:
Trouble in Terrorist Town
MurderMod
The Hidden
Cowboys vs Indians
JailBreak Supreme
Survival Madness
GuskiS is offline
tonykaram1993
Senior Member
Join Date: Mar 2013
Location: This World
Old 11-03-2014 , 06:45   Re: [HOW TO] Debug your plugins
Reply With Quote #9

I had to use the same concept to debug a problem I had with Event_HLTV, where if you are counting alive players here, the players who were dead the round before were not counted as alive on HLTV. But yeah, I do agree that this thread is useful for those who never thought of this.

From now on, we can simply tell them to check this topic and help themselfes.
__________________
My Plugins:
UltimatePlugin
UltimateSurf
UltimateAdmin
Code:
rcon version | rcon amxx version | rcon meta version
rcon amxx plugins | rcon meta list | rcon status
I AM INACTIVE ON THIS FORUM - For direct contact: [email protected]
tonykaram1993 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-03-2014 , 10:44   Re: [HOW TO] Debug your plugins
Reply With Quote #10

Quote:
Originally Posted by GuskiS View Post
I prefer using log_amx, shorter and logged in log file, not sure that server_print does that.
As for example, you are missing plugin_end() in some of your examples, for me, it was hard to understand what is happening when you show actuall not working code, then do debugging code with plugin_end in it, and after that again missing that plugin_end().
Better example of debugging usage could be new round counter, when server decides to restart = counter goes on, but round is first and all things affected by that counter would fail if you would check for first round, for example. (Had thoses issues myself, hehe).
I forgot to add plugin_end in the first example, thx. In the other one you use your logic and find out that it's useless to set there and remove it...
__________________

Last edited by HamletEagle; 11-03-2014 at 10:45.
HamletEagle is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:30.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode