Traceback() - get stack trace

Post your working scripts, libraries and tools for AHK v1.1 and older
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

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: 8953
Joined: 29 Sep 2013, 17:08
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, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Randy31416
Posts: 58
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
Contact:

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: 9583
Joined: 30 Sep 2013, 04:07
Contact:

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: 58
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 (v1)”

Who is online

Users browsing this forum: ConnorMcLoud, Google [Bot], makdc96 and 153 guests