Traceback() - get stack trace

Post your working scripts, libraries and tools
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Traceback() - get stack trace

18 Jan 2015, 10:00

This is a simple function which can be used to inspect/retrieve a stack trace. It must be called from within a function.

This function returns an array of objects representing a stack trace entry. Each object has the following fields:
  • offset - negative offset from the top of the call stack.
  • file - the script file (I'm not sure if this is equivalent to A_LineFile or if it's the file that contains the function, I suspect the former)
  • line - the line number at which the function is called
  • caller - function name
Syntax: tb := Traceback( [ actual := false ] )
If actual is true, the actual stack trace is returned which includes Traceback() itself

Example usage:

Code: Select all

#Include Traceback.ahk

f()
return

a() {
	b()
}

b() {
	c()
}

c() {
	out := "Stack trace:"
	for i, info in Traceback()
	{
		out .= Format("
		(LTrim Join`r`n
		`r`n
		Offset: {}
		File:   {}
		Line:   {}
		Caller: {}
		)", info.offset, info.file, info.line, info.caller)
	}
	ListVars
	WinWait ahk_id %A_ScriptHwnd%
	ControlSetText Edit1, %out%
	WinWaitClose
}

f() {
	a()
}
Source code:

Code: Select all

Traceback(actual:=false)
{
	r := [], i := 0, n := actual ? 0 : A_AhkVersion<"2" ? 1 : 2
	Loop
	{
		e := Exception(".", offset := -(A_Index + n))
		if (e.What == offset)
			break
		r[++i] := { "file": e.file, "line": e.Line, "caller": e.What, "offset": offset + n }
	}
	return r
}
User avatar
joedf
Posts: 7245
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: Traceback() - get stack trace

18 Jan 2015, 10:36

Neat little function there! I think I'll use it for debug-mode for some of my programs :)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB DDR4 3143 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
Randy31416
Posts: 46
Joined: 15 Jan 2014, 19:09

Re: Traceback() - get stack trace

21 Jan 2015, 17:04

This function works very nicely with interpreted AHK, but when I compile it (with the current version 1.1.19.01) and run the .exe all I get is a blank page -- no trace at all.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Traceback() - get stack trace

30 Jan 2015, 16:08

Randy31416 wrote:This function works very nicely with interpreted AHK, but when I compile it (with the current version 1.1.19.01) and run the .exe all I get is a blank page -- no trace at all.
Sorry for the late reply. Hmm, I'm not really sure about the behavior of Exception() in compiled scripts but I reckon that it should behave the same. Are you calling Traceback() from within a function or outside(e.g. auto-execute section/label subroutine)?
joedf wrote:Neat little function there! I think I'll use it for debug-mode for some of my programs
Thanks. It's really useful for large scripts with lots of subroutines.
lexikos
Posts: 6596
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Traceback() - get stack trace

02 Feb 2015, 04:28

Exception(x, some_negative_number) relies on the debugger's call stack, since AutoHotkey doesn't maintain a call stack as such just for script execution. The debugger code is omitted from AutoHotkeySC.bin, which becomes the executable part of each compiled script. Therefore, this function won't work in compiled scripts.

... with the possible exception of scripts compiled from AutoHotkey_H.exe, since they are based on a complete AutoHotkey executable.
Randy31416
Posts: 46
Joined: 15 Jan 2014, 19:09

Re: Traceback() - get stack trace

02 Feb 2015, 22:54

lexikos wrote:Therefore, this function won't work in compiled scripts.
Rats. But thanks, Lexicos, for the clear and unambiguous answer as usual.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: tmplinshi and 20 guests