Jump to content
Sign in to follow this  
dgm5555

Debug step-through func with Tooltip display of variables

Recommended Posts

dgm5555

This is a small func which will enable code to be stepped through (ie paused at desired points to await user input (scroll-lock key to step on, or ESC to exit the script entirely))

It will print variables to the console which are passed to it

While waiting for user input it will display a tooltip of any global (non-array) variable whose name is copied to the the clipboard (pressing X1 mouse button will do this automatically)

EDIT: Sadly after further testing it seems Eval doesn't always return correct values when checking the globals, so looks like it's back to the old consolewrite method. If anyone can spot the problem I'd be very grateful. Hopefully if it's a bug in AutoIt it will get fixed at some point and this code will become useful...

It could also be easily expanded to allow on-the-fly modification of variables to assist with debugging and stepping though code

It was created as a simple alternative for the graphical AutoIt Debugger after I found that was extremely slow with array management and crashed a lot

Hope it's useful!!

David

#include-once
#include <Misc.au3> ; needed for key press and mouse button trapping
;#include <SciTE UDF.au3> ; needed to move to current point in SciTE when stepping through code
; if you want to enable this you will have to download it from the forum, and
; uncomment the _SciTEGoToLine($LineNo) within the function

; global $debugCode=0 ; 0= print to console then display tooltip while waiting for user input; 1=don't debug; 2=print to console only (no pause);


; #  debugStep DESCRIPTION  # =================================================================================================
; Title .........: debugStep
; AutoIt Version : 3.3.8.1+
; Language ......: English
; Description ...: Function to help script debugging.
; Author(s) .....: David
;EXAMPLES
; debugStep()       ; Displays tooltip of variable in clipboard and waits for user (depending on $debugCode option)
; debugStep( @ScriptLineNumber )       ; Prints out the linenumber, tooltip, and waits (depending on $debugCode option)
; debugStep( [@ScriptLineNumber] [,"$v1",$v2][,"$v3",$v4]...)       ; Prints out the linenumber, variables passed, tooltip, and waits (depending on $debugCode option)

; $lineMarker is optional (and can be any value, not just @ScriptLineNumber = the current line being evaluated)
; [$v1, $v2] etc must passed as a pair with a name and it's value. They don't have to be declared in the global scope

; Tooltips will be generated for a global variable whose name is copied to the clipboard (uses the eval function).
; Non-global variables from outside the function will not be reported correctly (if at all).
; You can optionally set a global variable to control debugging and switch it on and off throughout the code.
; $debugCode = 0 (default). Print to console then wait for user input while displaying tooltip of a variable name in clipboard. 
;              By default the scroll-lock key is used to move on to the next step-point,  
;              and the ESC key is used to exit the entire script
; $debugCode = 1 Skip all debugging functions (return immediately function is called)
;              This saves you having to comment out the debugStep call in the code
; $debugCode = 2 means print to console, but don't pause
;===============================================================================================================================


Func debugStep($lineMarker=0, $v1=0,$v2=0,$v3=0,$v4=0,$v5=0,$v6=0,$v7=0,$v8=0,$v9=0,$v10=0,$v11=0,$v12=0,$v13=0,$v14=0,$v15=0,$v16=0,$v17=0,$v18=0,$v19=0)

    if $debugCode = 0 Then Return
    Local $hDLL = DllOpen("user32.dll")
    Local $tooltipVar="", $oldClip="", $curVal=""
    Const $RArrowKey = "27", $shiftKey = "10", $scrollKey = "91", $ESCKey="1B", $mouseX1="05"

    ; ideal would be to highlight the current line in SciTE independent of the cursor...
;     _SciTEGoToLine($lineMarker)
     Send ("{home}{home}+{down}")

    If $lineMarker > 0 Then
        ConsoleWrite($lineMarker & ":")                        ;"Trace Line: " & $lineMarker & @CRLF)
        For $dePrintItr = 1 To @NumParams - 1 step 2
            ConsoleWrite("    " & Eval("v" & $dePrintItr) & "=" & Eval("v" & $dePrintItr+1) & ",")    ;& @CRLF)
        Next
        ConsoleWrite(@CRLF)
    EndIf

    if $debugCode = 1 Then Return
    If _IsPressed($scrollKey, $hDLL) Then
        Sleep(50)
        DllClose($hDLL)
        Return
    EndIf

    while Not _IsPressed($scrollKey, $hDLL)
        ; could also add some code here to assign or execute so variables could be modified on the fly within the code

        ; would be nice if you could have the tooltip with the value of the currently selected variable in SciTE but ControlCommand returns blank string
        $tooltipVar = ClipGet()        ; ControlCommand("[CLASS:SciTEWindow]", "", "Scintilla1", "GetCurrentSelection", "")
        $tooltipVar = StringReplace($tooltipVar,"$","")
        if IsDeclared ($tooltipVar) And $oldClip <> $tooltipVar AND False Then
            $oldClip = $tooltipVar
            $curVal = Eval($tooltipVar)
            ToolTip ( "$" & $tooltipVar & "=" & $curVal , MouseGetPos(0)+20, MouseGetPos(1)-30)
        EndIf
        Sleep(50)
        if _IsPressed($ESCKey, $hDLL) Then Exit
        if _IsPressed($mouseX1, $hDLL) Then
            MouseClick("left",MouseGetPos(0), MouseGetPos(1),2)
            send ("^c")
;            _SciTEGoToLine($lineMarker)
            While _IsPressed($mouseX1, $hDLL)
                Sleep (50)
            WEnd
        EndIf
    WEnd
    Sleep(500)

    DllClose($hDLL)
EndFunc
Edited by dgm5555

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×