This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
apemanzilla's profile picture

trace - Simple stack traces for errors

Started by apemanzilla, 01 November 2016 - 12:59 PM
apemanzilla #1
Posted 01 November 2016 - 01:59 PM
As it turns out, CC supports xpcall, which is a pretty neat function that allows hackishly getting more detailed stack traces by abusing error levels. I used this to write trace:



In this example, example.lua is the following file:

local function a()
  error("example error") --# line 2
end

local function b()
  a() --# line 6
end

local function c()
  b() --# line 10
end

c() --# line 13

It's really easy to use trace - just download with pastebin get CYQp63kb trace, and then run any command preceded by trace (see example). When/if the program crashes, a stacktrace will be generated automatically. This is incredibly useful for debugging large projects as it allows you to see exactly what caused the error.

If you're curious as to how it works you're welcome to read the code, but as I said earlier, it's pretty hacky.
Edited on 01 November 2016 - 01:01 PM
Bomb Bloke #2
Posted 08 November 2016 - 02:05 PM
It could use some checks to ensure its output will be visible; this window-API-crashing snippet results in blankness for eg:

term.setTextColour(123)
print("hello")

I suspect this script may be one I'll frequently be pointing AaP posters to, however. :)/>
apemanzilla #3
Posted 08 November 2016 - 03:13 PM
It could use some checks to ensure its output will be visible; this window-API-crashing snippet results in blankness for eg:

term.setTextColour(123)
print("hello")

I suspect this script may be one I'll frequently be pointing AaP posters to, however. :)/>

That's a pretty evil snippet, I'm blaming the window API for that one, hehe. Regardless, I've fixed this particular issue - just redownload from the same PB code.


While it does blame the (technically correct) call to print, it would be more helpful if the window API simply errored if you attempt to set a non-blittable color.

I've also implemented a new feature - if the original error thrown by the program does not contain the correct source (file and line number) then it will show an additional entry containing the correct source at the beginning of the stacktrace. (This can happen when a program is terminated, which omits the source, or when a non-one error level is used, which changes the source)
Edited on 08 November 2016 - 02:40 PM
ReBraLaCC #4
Posted 08 November 2016 - 03:22 PM
ohh this looks usefull instead of having to rerun the program everytime :)/>
apemanzilla #5
Posted 08 November 2016 - 03:44 PM
ohh this looks usefull instead of having to rerun the program everytime :)/>

That's the idea :)/>
Lyqyd #6
Posted 08 November 2016 - 04:46 PM
I'm not really sure why the window API doesn't just do what the term API does and select the color represented by the highest true bit:


term.setTextColor(127) --# 64
term.setTextColor(129) --# 128

But erroring immediately would be preferable to creating a time bomb!
minebuild02 #7
Posted 17 November 2016 - 03:47 PM
Could I use this in my OS?
Edited on 17 November 2016 - 02:47 PM
apemanzilla #8
Posted 17 November 2016 - 04:00 PM
Could I use this in my OS?

Yes, but please add my name in the credits if you do.
apemanzilla #9
Posted 07 March 2018 - 08:15 PM
Minor update - trace now supports pcall and xpcall - if you use these in your program, they'll give you a stack trace instead of an error message when you run your program via trace. The original paste has been updated, so simply redownload it to get the update.
Edited on 07 March 2018 - 07:17 PM
MisterMeister32 #10
Posted 23 March 2018 - 06:26 PM
Very cool, mages debugging complex progams much easier.
Luca_S #11
Posted 08 August 2018 - 09:54 AM
Awesome program!
I tried to use it with coroutines and I got something working, but, considering I have absolutely no idea how it works ;)/>, it is also pretty much hacked together.

https://pastebin.com/hGgTDpkP

I would be happy if you could tell me if there's something I could improve about it.
If you'd allow me to, I'm probably going to use this in my OS to build full stacktraces if an app crashes. Of course you would get proper credit.
apemanzilla #12
Posted 08 August 2018 - 06:07 PM
Awesome program!
I tried to use it with coroutines and I got something working, but, considering I have absolutely no idea how it works ;)/>, it is also pretty much hacked together.

https://pastebin.com/hGgTDpkP

I would be happy if you could tell me if there's something I could improve about it.
If you'd allow me to, I'm probably going to use this in my OS to build full stacktraces if an app crashes. Of course you would get proper credit.

Looks OK. I'm fine with you redistributing and using the code.