[Solved] Advanced debugging tips for humungous scripts

Get help with using AutoHotkey and its commands and hotkeys
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

[Solved] Advanced debugging tips for humungous scripts

23 Feb 2015, 11:13

Hi guys,

I've got a bug in my script that is driving me absolutely nuts for about a week. This script has been a work-in-progress for over six months and has come a long way, minus this bizarre beast of a bug. I'm not going to get into the code for 2 reasons:
1. It's a work thing (so I probably shouldn't anyway), but more importantly,
2. It's gotta be at least a thousand lines of code, with nearly 30 objects and a ton of images, files and other stuff it needs. I would not wish that on anybody. :lol:
I've been working hard to figure this nasty little goober out, but it's a "one step forward, a gajillion steps back" kinda situation. I've tried all the simple stuff (I use consistent syntax throughout the script, plus a pile of try/catches that write errors to a file, playing around with ErrorLevel and A_LastError, etc.) but because the script is so gigantic I have no way of even knowing what to look for. I have a general "neighborhood" of 3-4 places in the code where it might possibly be, but I don't see anything wrong with what I have there (and no syntax/compiler errors). And as if this thing wasn't mind-blowingly frustrating enough, the dumb thing WORKS in .ahk form, but not as an EXE. :wtf: :crazy:

So my question is: what are your strategies, tips, tricks, tools etc. for "debugging the undebuggable"? It's not like in C# or Java where all exceptions are automatically printed somewhere with tons of info, so how do you guys approach fixing seemingly undetectable bugs in huge scripts? Thanks!
Last edited by Miguel7 on 23 Feb 2015, 13:36, edited 1 time in total.
GeekDude
Posts: 886
Joined: 02 Oct 2013, 22:13

Re: Advanced debugging tips for humungous scripts

23 Feb 2015, 11:21

SciTE4AHK and AHK Studio both have built in script debuggers. The most important thing about debugging scripts, however, is to know where the bug would be occurring. Also, it's much easier to debug, read, and write scripts that aren't monolithic. I'd suggest taking your code and working it into a few different files, each one for a different aspect of your program.

There are a few things that don't work when compiled. One of these (that I see most commonly) is dynamic script execution. It's somewhat of a hack, and requires a separate ahk interpreter (unless you use some of hotkeyit's voodoo). Another thing is dynamic includes (which I almost never see), which are also much more hackish than the previous one. Another thing to check is if you've hard coded the script name anywhere, which would definitely change if you compile (or even rename) the script.

I suppose the biggest thing I want you to take away from this post is don't debug the undebuggable, but rather don't write/debug undebuggable code in the first place. If necessary, take out the broken functionality and refactor, then re-add the functionality and debug it then.
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: Advanced debugging tips for humungous scripts

23 Feb 2015, 11:52

Wow, thanks for the quick response GeekDude! :)

Please let me walk through your points to make sure we're on the same page here:

- Built-in debuggers:
I am definitely going to check that out! I've been working on a debugger myself, but it's been on the back burner because of this project... but if there's one already out there then why reinvent the wheel? :lol:

- Know where the bug would be
I have a few ideas on this, like I said. It's still a ton of code to go thru but at least I have a general sense of where it might be

- Better to break down the script into multiple files
I totally hear you there! My script has a file for each class, and that has been a lifesaver! Glad to hear I wasn't too far off there lol.

- Some things that don't work when compiled
Thankfully there's none of that here. I didn't even know dynamic includes and stuff were possible, but they sound hackish to me too.

- "don't write/debug undebuggable code in the first place"
I love the idea, and I've done everything I can think of to prevent it, but unfortunately "stuff happens" and there was no way I could have seen this one coming. But it broke enough stuff that I felt like bugging you guys about it (no corny pun intended :lol:) was the only way I could find out how to better approach the issue. Which brings me to a question about your last point:

- "If necessary, take out the broken functionality and refactor, then re-add the functionality and debug it then."
I think I get the gist, but what does it mean to "refactor"? I know in other languages whenever you rename something you get a message box asking if you want to "refactor", but I never did figure out what that actually does).

But anyway, like I said thank you very much for your suggestions! I'm definitely going to try some of these things out.
GeekDude
Posts: 886
Joined: 02 Oct 2013, 22:13

Re: Advanced debugging tips for humungous scripts

23 Feb 2015, 12:05

wikipedia wrote:Code refactoring is the process of restructuring existing computer code – changing the factoring – without changing its external behavior. Refactoring improves nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve source code maintainability, and create a more expressive internal architecture or object model to improve extensibility.
https://en.wikipedia.org/wiki/Code_refactoring
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: Advanced debugging tips for humungous scripts

23 Feb 2015, 13:36

Thank you! :superhappy:

I created a copy of the last working version of the script, then slowly started to work in some of the new stuff I was asked to add from the "broken" version. I'm far from done, but I've already got a few of the more important changes they asked for working. And now if I add something and the dumb thing breaks again, I'll know exactly what it was that broke in the first place (We can add "Make sure you backup your code like crazy" to the list of tips :lol: - I'm glad I do, because that made it possible to go back to when it worked, and make the change from there).

I also downloaded SCITE4AHK and its built-in debugger is great.
GeekDude
Posts: 886
Joined: 02 Oct 2013, 22:13

Re: [Solved] Advanced debugging tips for humungous scripts

23 Feb 2015, 13:39

Miguel7 wrote:We can add "Make sure you backup your code like crazy" to the list of tips
Version control. I use GitHub, but since this sounds like more of a private project you might be better off with a local git repo or another service like bitbucket
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: [Solved] Advanced debugging tips for humungous scripts

24 Feb 2015, 03:58

Use OutputDebug and the Stream window. To change the length of the string the Stream window holds, edit this line in %ProgramFiles%\AutoHotkey\SciTE\tools\SciTEDebug.ahk :
ctext := SubStr(ctext, -102X) ; Limit the output to 1 KB of data
GeekDude
Posts: 886
Joined: 02 Oct 2013, 22:13

Re: [Solved] Advanced debugging tips for humungous scripts

24 Feb 2015, 12:00

Personally I prefer to use normal consoles, via AllocConsole and CONOUT$. This is not, however, compatible with S4AHK, but I'm sure if I looked I could get it to work.
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: [Solved] Advanced debugging tips for humungous scripts

24 Feb 2015, 14:07

GeekDude wrote:This is not, however, compatible with S4AHK, but I'm sure if I looked I could get it to work.
For the Run command, it seems it's enough to change the subsystem for input/output to be redirected to the console (vs to Scite's Output Window): command.go.subsystem.$(file.patterns.ahk)=2.
lexikos
Posts: 7085
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [Solved] Advanced debugging tips for humungous scripts

24 Feb 2015, 20:48

Code: Select all

FileAppend something`n, *
...outputs to the SciTE output pane by default (when not debugging).
Heezea
Posts: 53
Joined: 30 Sep 2013, 21:33

Re: [Solved] Advanced debugging tips for humungous scripts

26 Feb 2015, 16:31

lexikos wrote:

Code: Select all

FileAppend something`n, *
...outputs to the SciTE output pane by default (when not debugging).
Wow, thanks, this is awesome. I wish I'd known this all those times...
lexikos
Posts: 7085
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [Solved] Advanced debugging tips for humungous scripts

26 Feb 2015, 18:24

I've also found that if you do DllCall("AllocConsole") and Run a console app, its output usually shows in the SciTE output pane, but FileAppend ..., CONOUT$ goes to the (hidden) console window. You can show it with WinShow % "ahk_id " DllCall("GetConsoleWindow").

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Frosti, jvalk87, mmmax, scriptor2016, TAC109 and 31 guests