Page 1 of 1

Debugging AutoHotKey scripts for SciTE users

Posted: 02 Jan 2014, 21:21
by Joe Glines
I read a few posts on the forum about SciTE's built in debugging features however it wasn't straight-forward to me so I made a short video that helps show how to use it. Fincs has an awesome customized version for AutoHotKey which you can get here: http://fincs.ahk4.net/scite4ahk/

You can watch the ~6 minute video here and my script (which was built merely to show some of the tools) is below.



I should have prefaced my post with the warning that I am NOT a programmer. I might use incorrect terms here and there and I'm sure there are more elegant ways to demonstrate this but it should suffice for the basics.

Code: Select all

#SingleInstance, Force
#NoEnv
Var:=0

OutputDebug Line  %A_LineNumber%: A_Index is: %A_Index% and Var is: %Var%
GoSub Looper
MsgBox end of program
return

;***********************First loop********************************.
Looper:
loop, 15
{
var:=Var+A_Index
OutputDebug Line %A_LineNumber%: A_Index is: %A_Index% and Var is: %Var%
var:=Var+10
GoSub SubLooper
}
return

;***********************Sub loop********************************.
SubLooper:
Loop, 3
{
SubLooperVar:=A_Index   
OutputDebug Line %A_LineNumber%: A_Index is: %A_Index% and Var is: %Var% and Sublooper index is: %SubLooperVar%
}
return


Re: Debugging AutoHotKey scripts for SciTE users

Posted: 29 Jan 2014, 04:43
by vasili111
Very nice video. Thank you!

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 31 Jan 2014, 09:29
by Grendahl
Thanks for doing the video, the variable list means no more needing to use ListVars use for me!!!

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 14 Feb 2014, 15:25
by fincs
Really nice tutorial :)

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 16 Dec 2015, 22:07
by Joe Glines
I learned a few more things about using SciTE to debug and thought I'd document it in the below video. I also give short review of using the dmp function for displaying what text is in an object/array. (Newbies that are not used to playing with objects or arrays will find this very helpful)


I also realized that the SciTE output pane can be used as a command prompt. I'm sure there are more uses for it, but I didn't realize I could easily submit commands from it. :)

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 17 Dec 2015, 14:58
by evilC
Joe, there is an even better (imho) way of doing this.
Download DebugView from the microsoft site.
This is an app that traps debug messages from other apps - anything you output with OutputDebug will appear in debugview.
If you want to clear the debug view window, do OutputDebug DBGVIEWCLEAR (case for the debug message is important).
Then in scite, do Options->Open User properties, and add ahk.debugger.capture.streams=0 at the end to turn off the stream viewer (it gets annoying once you have debugview).
With debugview, you may see messages from other apps, which might get annoying . so in this case you can write a logging func to prefix a string to each message:

Code: Select all

Log(str){
   OutputDebug % "MyAppName: " str
}
Then in debugview, add a filter: MyFunc: * and you only see debug info from your app.
Usually it isnt an issue tho, you dont have to bother.

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 17 Dec 2015, 19:41
by Joe Glines
Sounds both cool and a bit complex. I like my current method as it is simple and easy but I'm looking forward to trying your suggestion (especially when I have complicated debug issues) Thanks evilC for taking the time to write it out!

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 18 Dec 2015, 03:57
by evilC
It's not complex, at it's simplest level, just run debugview and close the stream viewer. I find the stream viewer really annoying as it pops up in front of your code and you invariably have to move it around.
With debugview, you just arrange your scite window and debugview window how you want and then forget about it. Personally, I normally put use Gui, Show, x0 y0 in my gui scripts when writing them, so the gui always appears top left, with the scite window to the right of that and the debugview window at the bottom.

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 18 Dec 2015, 07:13
by Joe Glines
@evilC- I made the changes and tested it. Yes, I definitely prefer the DebugView window over the StreamViewer. I too would get annoyed with where StremViewer would end up. Having this on it's own (besides being able to track alll debug commands) is pretty awesome! Thanks for sharing!

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 21 Dec 2015, 09:03
by haichen
I am using Lexikos ListGlobalVars() with Outputdebug (if i don't have too much vars in a script).
https://autohotkey.com/board/topic/2092 ... ntry156570

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 21 Dec 2015, 10:00
by Joe Glines
@haichen , this sounds interesting however, in my example, I keep having to step through Lexicos' function during debug (which I don't think makes sense). Can you provide a working example how you use it?

Here is what I did

Code: Select all

var3:=2
var:="hi"

Loop {
    tick := A_TickCount
ind:=A_Index
OutputDebug,   % ListGlobalVars()
    Sleep, 2000
}
return

RAlt::
Browser_Forward::Reload

;~ https://autohotkey.com/board/topic/20925-listvars/#entry156570
ListGlobalVars()
{
    static hwndEdit, pSFW, pSW, bkpSFW, bkpSW
    
    if !hwndEdit
    {
        dhw := A_DetectHiddenWindows
        DetectHiddenWindows, On
        Process, Exist
        ControlGet, hwndEdit, Hwnd,, Edit1, ahk_class AutoHotkey ahk_pid %ErrorLevel%
        DetectHiddenWindows, %dhw%
        
        astr := A_IsUnicode ? "astr":"str"
        ptr := A_PtrSize=8 ? "ptr":"uint"
        hmod := DllCall("GetModuleHandle", "str", "user32.dll", ptr)
        pSFW := DllCall("GetProcAddress", ptr, hmod, astr, "SetForegroundWindow", ptr)
        pSW := DllCall("GetProcAddress", ptr, hmod, astr, "ShowWindow", ptr)
        DllCall("VirtualProtect", ptr, pSFW, ptr, 8, "uint", 0x40, "uint*", 0)
        DllCall("VirtualProtect", ptr, pSW, ptr, 8, "uint", 0x40, "uint*", 0)
        bkpSFW := NumGet(pSFW+0, 0, "int64")
        bkpSW := NumGet(pSW+0, 0, "int64")
    }

    if (A_PtrSize=8) {
        NumPut(0x0000C300000001B8, pSFW+0, 0, "int64")  ; return TRUE
        NumPut(0x0000C300000001B8, pSW+0, 0, "int64")   ; return TRUE
    } else {
        NumPut(0x0004C200000001B8, pSFW+0, 0, "int64")  ; return TRUE
        NumPut(0x0008C200000001B8, pSW+0, 0, "int64")   ; return TRUE
    }
    
    ListVars
    
    NumPut(bkpSFW, pSFW+0, 0, "int64")
    NumPut(bkpSW, pSW+0, 0, "int64")
    
    ControlGetText, text,, ahk_id %hwndEdit%

    RegExMatch(text, "sm)(?<=^Global Variables \(alphabetical\)`r`n-{50}`r`n).*", text)
    return text
}

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 21 Dec 2015, 10:07
by evilC
Joe, when you are debugging, you have a number of options:

1) "Step In" (The Right arrow or F10)
This steps "into" functions etc. If you "step in" on a line that is a call to another func (eg lex's ListGlobalVars), you will debug that func.
Also, if you have SetTimers psuedo-threads running, step in can jump to those as well.

2) "Step Over" (The down arrow or F11)
This steps "over" functions etc. It will run until the next line.

3) "Step Out" (Up and left arrow, or Shift+F11)
Runs until you exit from the current function.
Useful if you accidentally "Step In" - will keep going until that function call ends.

4) Run (Play icon or F5)
Play until next breakpoint.

So TL;DR - Use Step Over (F11) to avoid debugging stuff you do not want to, and use Step Out if you Step In by mistake.

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 21 Dec 2015, 11:15
by haichen
Hi Joe, if you put OutputDebug DBGVIEWCLEAR into the loop you can see it a little better. Lexikos function does the same as the listvars command. So maybe a big loop is not the best example, but if you want to have a look at all vars at a special codeline this may be an easy solution. Its like a better Msgbox, % var1 var2 var3 .. (without stopping the script and changing the active window)

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 21 Dec 2015, 15:41
by Joe Glines
@EvilC- thanks for point out the 3rd option. I new it did behaved this way on labels but I didn't realize it would behave this way on functions too.

@haichen- I thought you meant you were using the function in the same line as Outputdebug. Thank you for the clarification.

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 26 Jan 2016, 11:53
by tbolto00
When debugging this script using F11 (step by step); it activates notepad, but then it makes Scite the active window. Thus, when I debug the next line, it sends a 1 in Scite rather than Notepad. Is there a way to prevent Scite from constantly making itself the active window?

WinActivate, ahk_class Notepad
Send 1
Send 2
Send 3
Send 4
Send 5

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 26 Jan 2016, 14:31
by evilC
Yeah, that's annoying. FWIW it's no better in C with Visual Studio in my experience, so I don't think it's inherently an AHK / SciTe problem.
However, it may well be fixable in SciTe4AHK as a lot of the code is actually AHK code, so if the part that focuses SciTe is AHK code, and is not fundamentally required, it could be deactivated.
Another solution would be to run an AHK script that redirects F11 to the SciTe window (eg by using ControlSend). Potentially what you could have the script do when you hit F11 would be to store the active window, send F11 to SciTe (Which would grab focus), then re-focus the window you had focused.

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 27 Jan 2016, 01:17
by lexikos
You could just remove the IfWinNotActive/WinActivate lines from SciTE_RedrawLine and SciTE_EnsureFileIsOpen in SciTEDebug.ahk...

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 19 Aug 2020, 22:38
by pmobin
Anyone know where I can get a copy of dmp.ahk? The .de site is no longer.
Thanks

Re: Debugging AutoHotKey scripts for SciTE users

Posted: 19 Aug 2020, 22:43
by gregster
pmobin wrote:
19 Aug 2020, 22:38
Anyone know where I can get a copy of dmp.ahk? The .de site is no longer.
Thanks
There is a link in Joe's post above: https://www.autohotkey.com/boards/viewtopic.php?f=7&t=1254#p62711
Link: https://www.the-automator.com/download/dmp.ahk