CallTree Function

Purpose

Returns a string containing called subroutines.

Syntax

$ = CallTree [(start% [,end%])]

Description

CallTree returns a string with a list of procedure calls up to the position CallTree is invoked, this is called procedure call tree hierarchy.

The number of entries that CallTree must return can be limited by using the optional parameters start% and end%. If start% < end% one entry is returned: entry start%. CallTree(1, 3) will return the first three entries of the call tree hierarchy (list). CallTree(3, 3) or CallTree(3, 0) return entry 3 of the list. If start% <= 0, then end% will be ignored. Slicing a part of the list is mainly interesting within recursively called functions.

CallTree(-1) returns the approximate number of entries in the list

CallTree(0) same as CallTree

CallTree(1) returns the name and the parameter of the actual called Procedure, Sub, or Function.

CallTree(2) returns the name and parameters that calls the actual Procedure, Sub, or Function.

CallTree(3) returns the name and the parameters that has called the one returned from CallTree(2).

CallTree is to be used for debugging purposes. It is a feature independent of other debugging facilities of GFA-BASIC 32. CallTree is used at the start a subroutine body to find out which subroutines called it and which routines called the caller. This list provides a kind of cross reference of calling procedures. The example shows the CallTree for the function rt() each time it is called.

Example

Ocx ToolBar tb1

tb1.AddItem

Me.BackColor = colBtnFace

t(1, 12, tb1.Button(1), , Me)

Do

Sleep

Loop Until Me Is Nothing

 

Sub t(a#, b%, c , Optional ox, d)

Local j% = 9   // dummy

Print rt(4)    // calculate faculty

EndSub

 

Function rt(ByVal i|) As Double  // Faculty

Print CallTree                 // show in Win_1

Local h As Hash String

Split h[] = CallTree, "\r\n"

qq(h[])                        // another way

MsgBox "levels: " & CallTree(-1) _

& #13#10 & CallTree(1, 3)    // in a msgbox

Debug.Print "CallTree"         // in the output...

Debug.Print CallTree           // ...window

If(i > 1) Then Return rt(i - 1) * i

Return 1

End Func

 

Sub qq(hs As Hash String)

Local a$

For Each a In hs[]

' Print a // Copies CallTree output to screen

Next

EndSub

This program calculates the faculty of a value by recursively calling rt(). The Function rt() shows 4 possible ways of inspecting the call tree hierarchy. First it prints the call list in the client area of the window. Then the list is split in to a Hash array and then the Hash is 'printed' into the client area as well. Third, the list is displayed using a Message Box. Finally, the tree is printed in the Debug output window.

The first time in function rt() CallTree returns:

CallTree
Function rt( 4)
Sub t 1, 12, ToolBar(tb1) - Button, , Form(Win_1)

The program is executing function rt() with the parameter 4. The function was called from Sub t, which was called with the parameters 1, 12, an object - the Toolbar.Button object owned by Toolbar(tb1) -, empty (optional parameter declared As Variant), and the last parameter, a Form object with the name Win_1.

The next message box shows:

CallTree
Function rt( 3)
Function rt( 4)
Sub t 1, 12, ToolBar(tb1) - Button, , Form(Win_1)

Again, the program is currently executing the function rt(), now recursively called with parameter 3 from rt(), which itself was called earlier with parameter 4 from Sub t.

The third time:

CallTree
Function rt( 2)
Function rt( 3)
Function rt( 4)
Sub t 1, 12, ToolBar(tb1) - Button, , Form(Win_1)

The last time:

CallTree
Function rt( 1)
Function rt( 2)
Function rt( 3)
Function rt( 4)
Sub t 1, 12, ToolBar(tb1) - Button, , Form(Win_1)

Remarks

Especially for recursive subroutines CallTree occupies much stack memory, because of the nature of information; names and parameters as plain text. This could lead to a stack overflow. However, a stack overflow with CallTree will almost certainly create a stack overflow without CallTree, only some time later. Note that the performance decrease when using CallTree is significant.

In an EXE CallTree returns "".

A Naked subroutine is not included in the list. This is also true for code compiled with the $StepOff directive.

See Also

Naked, $StepOff

{Created by Sjouke Hamstra; Last updated: 26/09/2014 by James Gaite}