←  Programs

ComputerCraft | Programmable Computers for Minecraft

»

trace - Simple stack traces for errors

apemanzilla's Photo apemanzilla 01 Nov 2016

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:

Posted Image

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 by apemanzilla, 01 November 2016 - 02:01 PM.
Quote

Bomb Bloke's Photo Bomb Bloke 08 Nov 2016

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. :)
Quote

apemanzilla's Photo apemanzilla 08 Nov 2016

View PostBomb Bloke, on 08 November 2016 - 02:05 PM, said:

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.
Posted Image

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 by apemanzilla, 08 November 2016 - 03:40 PM.
Quote

ReBraLaCC's Photo ReBraLaCC 08 Nov 2016

ohh this looks usefull instead of having to rerun the program everytime :)
Quote

apemanzilla's Photo apemanzilla 08 Nov 2016

View PostReBraLaCC, on 08 November 2016 - 03:22 PM, said:

ohh this looks usefull instead of having to rerun the program everytime :)

That's the idea :)
Quote

Lyqyd's Photo Lyqyd 08 Nov 2016

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!
Quote

minebuild02's Photo minebuild02 17 Nov 2016

Could I use this in my OS?
Edited by minebuild02, 17 November 2016 - 03:47 PM.
Quote

apemanzilla's Photo apemanzilla 17 Nov 2016

View Postminebuild02, on 17 November 2016 - 03:47 PM, said:

Could I use this in my OS?

Yes, but please add my name in the credits if you do.
Quote

apemanzilla's Photo apemanzilla 07 Mar 2018

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 by apemanzilla, 07 March 2018 - 08:17 PM.
Quote

MisterMeister32's Photo MisterMeister32 23 Mar 2018

Very cool, mages debugging complex progams much easier.
Quote

Luca_S's Photo Luca_S 08 Aug 2018

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.
Quote

apemanzilla's Photo apemanzilla 08 Aug 2018

View PostLuca_S, on 08 August 2018 - 08:54 AM, said:

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.
Quote