Page 1 of 1

Traceback() - get stack trace

Posted: 18 Jan 2015, 10:00
by Coco
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
}

Re: Traceback() - get stack trace

Posted: 18 Jan 2015, 10:36
by joedf
Neat little function there! I think I'll use it for debug-mode for some of my programs :)

Re: Traceback() - get stack trace

Posted: 21 Jan 2015, 17:04
by Randy31416
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.

Re: Traceback() - get stack trace

Posted: 30 Jan 2015, 16:08
by Coco
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.

Re: Traceback() - get stack trace

Posted: 02 Feb 2015, 04:28
by lexikos
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.

Re: Traceback() - get stack trace

Posted: 02 Feb 2015, 22:54
by Randy31416
lexikos wrote:Therefore, this function won't work in compiled scripts.
Rats. But thanks, Lexicos, for the clear and unambiguous answer as usual.