You can add a debugger check to any of your functions. A debugger check is a function that is called before the real function in Debugger mode. In that function, you can verify that the argument values are correct, or anything you like, and halt the program calling the debugger if something is wrong.
To add a debugger check, you must use
ProcedureCDLL instead of
ProcedureDLL and append
_DEBUG to the function name, and use
IncludeFile #PB_Compiler_Home+"TailBite/Helper Libraries/TB_Debugger.pb" (change this if you have not installed TailBite inside your PB folder)
<TailBite Install Folder>/Samples/MyDebugErrorPlugin/MyDebugErrorPlugin.pb for an example of all the debug helper functions.
Note that debug functions don't return a value. After the PureBasic debugger calls the debug function (
MyDiv_DEBUG in the example), it calls the original function,
You've probably noticed the
TB_DebugError call in the debug function above.
TB_DebugError is a special function provided by TailBite (in the TB_Debugger include file installed with TailBite), which allows you to halt program flow and send a message to the debugger window, just like other PureBasic functions do when an error occurs. That is, you should only use it in debug functions (they're of no other use), otherwise applications that use your library won't even compile.
And remember that debug functions must be
ProcedureCDLL, otherwise they will crash (unless they have no arguments, it's a stack issue).
If you want to use both debug functions and variable argument functions:
ProcedureDLL MyDiv(a, b) ProcedureReturn a/b EndProcedure ProcedureCDLL MyDiv_DEBUG(a, b) If b=0 TB_DebugError("Division by zero!") EndIf EndProcedure ProcedureDLL MyDiv2(a, b, c) ProcedureReturn a/b/c EndProcedure ProcedureCDLL MyDiv2_DEBUG(a, b, c) If b=0 Or c=0 TB_DebugError("Division by zero!") EndIf EndProcedure
New in TailBite 1.4.0:
To support the new PB 4.30 debugger functions, more helper functions have been added to TB_Debugger: TB_DebugWarning(message.s), TB_DebugFileExists(filename.s), TB_DebugCheckLabel(*Label), TB_DebugCheckProcedure(*Proc, ReturnType, Count, *params.TB_Debug_CheckParams), TB_DebugCheckUnicode().
They are all more or less wrappers to the PB Functions, which you can use directly if you so choose (see the import section in the include file). They are all pretty self explanatory, except TB_DebugCheckProcedure. To see the other functions in use, see the example mentioned at the top of this page.
To make this function work, you need to pass it the pointer to the procedure to check, the return type (e.g. #PB_Integer), number of parameters (arguments - starts at 1) and a pointer to a TB_Debug_CheckParams structure. The TB_Debug_CheckParams structure is defined in the TB_Debugger.pb include, and you fill it with the type of the arguments (max of 6 unless you modify the needed part in the include). It is used in the debugger example, however you should still read the header file in the PB SDK, as this function is not perfect and has certain limitations.