Debugging

Previous  Next

 

To run a script with a debugger requires 2 or 3 steps befor ecalling mb_run;

 

1.Optionally Load and Save Comments to Debugger during parsing
2.Enable Debugger in MyBASIC2 Interpreter
3.Start the Debugger User Interface

 

Step 1 and 2 are accomplished by calling mb_debug_enable while step 3 requires to start a debugger user interface application.  There is currently 2 versions this application, a Command Line Interface and a GUI Interface.  Starting the debugger user interface depends on your MyBASIC2 environment.  Under Windows you would use ShellExecute[Ex] or CreateProcess.  In a C/C++ environment, use system, _spawn or _execl.

 

Loading Comments

 

mb_debug_enable( bas, MB_DBG_ENABLE_COMMENTS, 0 );

 

if ( bFileMode )

{

  nStatus = mb_load_file_ex( bas, strFileName, NULL, NULL, dwRunOptionFlags );

}

else

{

  nStatus = mb_load_string_ex( bas, strBuffer, dwRunOptionFlags );

}

 

Enabling MyBASIC2 Debugger

 

mb_debug_enable( bas, MB_DBG_ENABLE_ALL, GetMainWnd() );

 

if ( bGUI )

{

  strDebugger = "MyBASIC2Debugger.exe";

}

else

{

  strDebugger = "MB2DebugConsole.exe";

}

 

Starting the Debugger User Interface

 

The MyBASIC2 creates an Event to synchronize the start of the UI side.  It has a timeout of 10 seconds.  If the UI or the MyBASIC2 hasn't started and reset the event in 10 seconds an error is signalled.  Below is code to start either the Command Line or GUI debugger.

 

/////////////////////////////////////////////////////////////////////////////

//

static BOOL _start_debugger( mb_interpreter_t* bas, BOOL bGUI )

{

  CEvent   event( FALSE, FALSE, MB_DEBUG_EVENT_NAME );

 

  if ( mb_debug_enable( bas, MB_DBG_ENABLE_ALL, (DWORD)AfxGetMainWnd() ) != MB_FUNC_OK )

  {

     return FALSE;

  }

  if ( !event.Lock( 10 ) )

  {

     // Debugger not running.  Start the debugger

     CString  strDebugger;

 

     if ( bGUI )

     {

        strDebugger = "\"MyBASIC2Debugger.exe\"";

     }

     else

     {

        strDebugger = "\"MB2DebugConsole.exe\"";

     }

     STARTUPINFO stStartUpInfo = { 0 };

     PROCESS_INFORMATION stProcInfo = { 0 };

 

     if ( !CreateProcess( NULL, strDebugger.GetBuffer( 2 ), 0, 0, TRUE, CREATE_NEW_CONSOLE, 0, 0, &stStartUpInfo, &stProcInfo ) )

     {

        DWORD err = GetLastError();

        mb_debug_enable( bas, MB_DBG_ENABLE_NONE, 0 );

        return FALSE;

     }

     if ( !event.Lock( 2000 ) )

     {

        mb_debug_enable( bas, MB_DBG_ENABLE_NONE, 0 );

        return FALSE;

     }

     event.ResetEvent();

  }

  return TRUE;

}