How can a function return hidden debug info in addition to user variable ?

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

How can a function return hidden debug info in addition to user variable ?

Post by fxm »

The principle is that the function defines in its body a static structure with the user variable as the first element, and returns this variable by reference, so that:
- The user just reads this variable as usual.
- The debugger can access the hidden structure via the address of the returned user variable.

The most penalizing, from CPU load point of view, when debugging the program while it is running is not the local storing of dedicated information inside the procedures bodies but the code added for visualization.

Therefore this local storing (but accessible for a debugger) can kept in final version, and only the external part for such information displaying will be removed.

Simple case: the debug variables are of the same type as the user variable:
- A simple static array is defined in the body of the function.
- The function returns by reference the first element of the array.
- Example:

Code: Select all

Function test() Byref As Const Integer
    Static As Integer array(0 To 3)
    array(0) = 123
    array(1) = 456  '' permanent code
    array(2) = 789  '' permanent code
    Return array(0)
End Function

'------------------------------------------------------------------------------

' User

Dim As Integer I
I = test()
Print I
Print

'------------------------------------------------------------------------------

' Debugger (non permanent code)

Dim As Const Integer Const Ptr pi = @(test())
Print pi[0]
Print pi[1]
Print pi[2]
Print

Sleep

Improved case: the debug variables are of more than one type or of a different type than the user variable:
- A static specific type object is defined in the body of the function.
- The function returns by reference the first field of the object.
- Example:

Code: Select all

Function test() Byref As Const Integer
    Type debug
        Dim As Integer i
        Dim As Double d
        Dim As String * 9 s
    End Type
    Static As debug db
    db.i = 123
    db.d = 3.14159      '' permanent code
    db.s = "FreeBASIC"  '' permanent code
    Return db.i
End Function
'------------------------------------------------------------------------------

' User

Dim As Integer I
I = test()
Print I
Print

'------------------------------------------------------------------------------

' Debugger (non permanent code)

Type debug
    Dim As Integer i
    Dim As Double d
    Dim As String * 9 s
End Type

Dim As Const debug Const Ptr pdb = Cptr(debug Ptr, @(test()))
Print pdb->i
Print pdb->d
Print pdb->s
Print

Sleep
Post Reply