Jump to content




trace - Simple stack traces for errors

utility lua computer

11 replies to this topic

#1 apemanzilla

  • Members
  • 1,421 posts

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:

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.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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

#3 apemanzilla

  • Members
  • 1,421 posts

Posted 08 November 2016 - 03:13 PM

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.


#4 ReBraLaCC

  • Members
  • 100 posts
  • LocationSublime Text 2

Posted 08 November 2016 - 03:22 PM

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

#5 apemanzilla

  • Members
  • 1,421 posts

Posted 08 November 2016 - 03:44 PM

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

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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!

#7 minebuild02

  • Members
  • 97 posts

Posted 17 November 2016 - 03:47 PM

Could I use this in my OS?

Edited by minebuild02, 17 November 2016 - 03:47 PM.


#8 apemanzilla

  • Members
  • 1,421 posts

Posted 17 November 2016 - 04:00 PM

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.

#9 apemanzilla

  • Members
  • 1,421 posts

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


#10 MisterMeister32

  • Members
  • 6 posts
  • LocationGermany

Posted 23 March 2018 - 06:26 PM

Very cool, mages debugging complex progams much easier.

#11 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 08 August 2018 - 08: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.

#12 apemanzilla

  • Members
  • 1,421 posts

Posted 08 August 2018 - 05:07 PM

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users