Debugging Code with GDB, anyone tried?

 

Hi guys,


I'm having some trouble in my EA and so I need to debug it. It's a pain in the ass to use Print through the whole program, and I need it to be executed step by step to know what's going on.


So my idea was to switch everything into a C code and debug it with GDB. To be able to do that I need to alter a lot of funcions like Symbol ( ), GetLastError ( ), and many many others, and also to write functions to read charts, which I'm thinking in creating a TXT with a lot of numbers. It would be awesome to convert a currency chart in some sort of txt file to be read by the program.


I wonder if anyone has done that in the past and if so, could share some functions used to adapt it to C and be able to read / write files on disk to read charts and to compile it and be able to debug it.


It's really too bad that MT4 doesn't have an internal debugger. I was reading also that in one of the EA Championships, the winner did the same thing, debugging it first and then putting it on the Meta Trader!


Thanks!

Marco


PS: I started to change the program and it will be REALLY hard to change everything for it to be compiled and later debugged. Anyone has a better alternative rather than using Print's everywhere???

 
You may find this interesting -> https://www.mql5.com/en/forum/122850/page2#252767.
 
marcoblbr:


So my idea was to switch everything into a C code and debug it with GDB. To be able to do that I need to alter a lot of funcions like Symbol ( ), GetLastError ( ), and many many others, and also to write functions to read charts, which I'm thinking in creating a TXT with a lot of numbers. It would be awesome to convert a currency chart in some sort of txt file to be read by the program.

Hmm... to me this sounds by orders of magnitude more cumbersome than simply inserting a few Print() here and there.


Wouldn't you create a whole bunch of new sources of potential error by porting your whole code forth and back and reimplementing half of the metatrader API in C? YMMV, but I personally have found that in 99% of all cases there never was a real need for me to use a debugger as long a I am debugging my own programs and have all source files.


You could make printing and viewing the output a bit simpler by using kernel32 OutputDebugString() and the DebugView application: http://www.forexfactory.com/showpost.php?p=3780539&postcount=16, wrap this into a conveniently short named function like log() and and use this instead of Print(). You could also consider entirely switching to MT5/MQL5 where a debugger will be integrated into the IDE before you start your above mentioned mega monster project of reimplementing half of the MT4 API.

 

Couldn't agree more. I personally have never needed anything more than a few Print() statements. And in most cases, even those aren't needed as most runtime errors cause an immediate "of course, silly me" statement before even opening the code.

CB

 

what about ordersend errors ? I have a problem with my EA i keep getting close order function invalid ticket errors in strategy tester i tried putting print statements so i could see exactly what ticket numbers are being used in my close order function but the print statements dont appear to work in strategy tester i cant think of a way to debug my ordersend function without running it live on real orders to see what the problem is but i really dont want to do that.

 

Errors encountered using trading functions are no different than any other type of error. Use Print() to check logic flow and also the content of all variables.

Print() statements do work correctly in the strategy tester. It may be possible that not all output is being displayed in the Experts tab. So look in the log-file instead and you'll see everything there.

CB

 

The following is what I have already used and what worked perfectly:

#import "kernel32.dll"
   void OutputDebugStringA(string msg);
#import

/**
* send information to OutputDebugString() to be viewed and logged
* by SysInternals DebugView (free download from microsoft)
* This is ideal for debugging as an alternative to Print().
* The function will take up to 8 string (or numeric) arguments 
* to be concatenated into one debug message.
*/
void log(
   string s1, 
   string s2="", 
   string s3="", 
   string s4="", 
   string s5="", 
   string s6="", 
   string s7="", 
   string s8=""
){
   string out = StringTrimRight(StringConcatenate(
      WindowExpertName(), ".mq4 ", Symbol(), 
      " ", s1, 
      " ", s2, 
      " ", s3, 
      " ", s4, 
      " ", s5, 
      " ", s6, 
      " ", s7, 
      " ", s8
   ));
   OutputDebugStringA(out);
}
call log() just like you would call Print(), it works with strings and also with numbers. The DebugView application can log to file and also filter the messages, so you could even invent different "log levels" as the first parameter and leave all the log() calls permanently in the code and for the final production release just modify the log() function to either do nothing or use ordinary Print() or log to a file instead.
 

I dont know what is happening with mine, i checked the log file it gets caught in a loop and prints order close error 4501 followed by invalid ticket number for OrderClose function over and over in the log file with no other print statements, i even put a print statement at the very beginning in the start function, first operation Print("Start"); to make sure the print statements work and it doesnt even show that in the log file the very first entry is the invalid ticket number in close order function error code it doesnt even show the loaded succesfully message or I also put a print statement right before the order open, it opens orders but doesnt show that print statement either

 

also it made an 850 megabyte log file which i couldnt open because the file size is so huge it crashed notepad and wordpad so i deleted it and now strategy tester is reporting "no history data 2010.06.07 13:51:00 TestGenerator: no history data 'EURUSD1'
even though my charts clearly does have that data because im looking at it in my trading chart EURUSD 1minute chart

EDIT: even though my charts clearly doesnt have that data because I accidently used saturday and sundays date haha

Well it made a new log file of a more reasonable 28k but still none of my print statements are in it, although at least it did say the loaded succesfully message this time its still shows the same orderclose / invalid ticket error in a continous loop ...

 
int start()
  {
//----
   Print("Start");
   Acc();
   Cross();
   Trade();
//----
   return(0);
  }
      if (Typ==0) Price_Cls=Bid;
      if (Typ==1) Price_Cls=Ask;
      Print("Closing Order Ticket is",Ticket);
      bool Ans=OrderClose(Ticket,Lot,Price_Cls,0,clr);
      
      if (Ans==false)
         {
         if(Errors(GetLastError())==false)
            return;

14:37:03 TestEA: loaded successfully

14:37:03 TestEA inputs: MA1=21; MA2=34; StopLoss=15;

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: open #1 sell 0.05 EURUSD at 1.2236 sl: 1.2253 ok

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: open #2 sell 0.05 EURUSD at 1.2236 sl: 1.2253 ok

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: invalid ticket for OrderClose function

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: OrderClose error 4051

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: invalid ticket for OrderClose function

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: OrderClose error 4051

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: invalid ticket for OrderClose function

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: OrderClose error 4051

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: invalid ticket for OrderClose function

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: OrderClose error 4051

14:37:03 2010.06.02 12:32 TestEA EURUSD,M1: invalid ticket for OrderClose function

etc .... see how there is no print statement in the log ?

 

Are you looking in the tester/logs folder?

Appears you're loking at the logfile which represents the journal tab rather than the one which represents the experts tab.

CB

Reason: