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

Ingame Step-by-Step Debugger

Started by Lion4ever, 03 December 2015 - 04:42 PM
Lion4ever #1
Posted 03 December 2015 - 05:42 PM
I've always wanted to write a proper debugger for Lua but we can't without the debug library - it would be nice to have I just don't get why.
Well, good news: It is possible using code injection.

This is an advanced lua debugger without using the debug api.

Features:
  • stepping through your program while seeing the source
  • show local and globals variables
  • show which functions have been called (f4)
  • set breakpoint and run until you reach them
  • step-into own function and step-over
  • stepping backwards lightweight (undoing changed)
  • stepping backwards heavyweight (replay the program to last point)
  • stops on lines which throw errors (so you can see the locals or step back)
  • reverses some functions (like the turtle ones)
  • stop the program while it is running (f6 in source window)
  • lua prompt in the middle of the program execution (f10)
Is your turtle trying to build a house, but its allways taking the wrong turn at some point? Then this is perfect for you! You can step through your program (with f6) until it does the wrong turn and then you know excatly which line caused it! From there on you could even step backwards in your program (with f5) until the whole house is gone again to start over again! You can step into every function of your program, but be careful because you could even step into poop!

local function poop()
end

Screenshots:
Spoiler

The red line is a breakpoint:

Now the turtle stepping:


And after some stepping back:

Technical details:
Every start of a line gets a _lbpcf() call (line breakpoint call function). It prints the source and waits for user input. The local statements get replaced by _cnlv (create new local variable) so that they can be handled with the __index-metamethod of the environment. _bscf and _becf (block start/end call function) handle the scoping. For how the modified program look, see the .crash file that is created.


System requirements:
Required:
  • CraftOS 1.74 (might work for lower versions)
  • >0 tps
Recommended:
  • Advanced Computer with CraftOS 1.74
  • 20 tps
Installation:

pastebin get LdKK3BTE
Edited on 04 December 2015 - 05:22 PM
Creator #2
Posted 03 December 2015 - 05:51 PM
Wow, this actually really advanced and might help beginners ( and not so beginners).
Blue #3
Posted 03 December 2015 - 07:20 PM
Amazingly useful. Well done! :D/>
Lion4ever #4
Posted 03 December 2015 - 07:38 PM
Amazingly useful. Well done! :D/>
Thanks :)/>
Yevano #5
Posted 03 December 2015 - 08:27 PM
This looks pretty awesome! Will definitely have to try it out when I get back to my PC. I know I've been itching for a debugger ever since I started my own large project a little while ago (which is now approaching 2K lines :/). Stack traces help, but often I find myself using the ol' "print stuff until you figure out the control flow of the program" method.
SquidDev #6
Posted 03 December 2015 - 08:53 PM
I'm impressed (also slightly miffed at myself for not finishing what I started). It might be worth looking into a Lua parser instead of using find and replace - it should be less flakey when doing something like print("repeat " .. x .. " times") (the repeat will be replaced with a scope push).

As I said before, I'm impressed!
Lion4ever #7
Posted 03 December 2015 - 09:14 PM
This looks pretty awesome! Will definitely have to try it out when I get back to my PC. I know I've been itching for a debugger ever since I started my own large project a little while ago (which is now approaching 2K lines :/). Stack traces help, but often I find myself using the ol' "print stuff until you figure out the control flow of the program" method.
I tested it with a 1100 line program and it took like 3 seconds allready. It may be a bit slow on large files.

I'm impressed (also slightly miffed at myself for not finishing what I started).
Yeah, i looked at your program before i started programming this, but i think you are trying to inject code after the conversion to bytecode, but i think the upvalues might become a problem there, am i wrong?
If it is possible, there would not be any need to worry about syntax.

It might be worth looking into a Lua parser instead of using find and replace - it should be less flakey when doing something like print("repeat " .. x .. " times") (the repeat will be replaced with a scope push).

That is very true, using patterns sure is flakey. Atm my debugger is not able to handle quite a few unusual snytaxes which lua allowes. I will look into your parser, but replacing is for the normal stuff easier.
SquidDev #8
Posted 03 December 2015 - 09:26 PM
I'm impressed (also slightly miffed at myself for not finishing what I started).
Yeah, i looked at your program before i started programming this, but i think you are trying to inject code after the conversion to bytecode, but i think the upvalues might become a problem there, am i wrong?
If it is possible, there would not be any need to worry about syntax.

Normal upvalues shouldn't break: but upvalues within loops would. Sometimes I wonder if it would have been easier just to write a Lua interpreter in Lua :P/>.
Edited on 03 December 2015 - 08:27 PM
H4X0RZ #9
Posted 03 December 2015 - 09:43 PM
I'm impressed (also slightly miffed at myself for not finishing what I started).
Yeah, i looked at your program before i started programming this, but i think you are trying to inject code after the conversion to bytecode, but i think the upvalues might become a problem there, am i wrong?
If it is possible, there would not be any need to worry about syntax.

Normal upvalues shouldn't break: but upvalues within loops would. Sometimes I wonder if it would have been easier just to write a Lua interpreter in Lua :P/>/>.

Something like this?
http://yueliang.luaforge.net
Lion4ever #10
Posted 03 December 2015 - 09:50 PM
Normal upvalues shouldn't break: but upvalues within loops would. Sometimes I wonder if it would have been easier just to write a Lua interpreter in Lua :P/>.

That would be so slow :D/> Well, it would not matter for a debugger.

To prevent the keywords in strings problem, i could simply remove all strings with something like

gsub('"(.-)"',function(t) table.insert(strings,t) end)
and put them back after the modifying. Once i can think of a way to do that with both " and '
Yevano #11
Posted 03 December 2015 - 09:51 PM
Sometimes I wonder if it would have been easier just to write a Lua interpreter in Lua :P/>.

What about porting over luac and retrofitting it to add calls to the debugger framework? That could run much faster. I actually ported over a pure Lua luac implementation to CC not too long ago with the intent to add preemptive multithreading to Lua, but never ended up going further than porting luac.
Lion4ever #12
Posted 03 December 2015 - 09:54 PM
Something like this?
http://yueliang.luaforge.net

"28 seconds […] for Yueliang 0.1.0. (Native C is virtually instantaneous, so a comparison is pointless, really.)"
That sure is slow :D/>
Lion4ever #13
Posted 03 December 2015 - 10:45 PM
What about porting over luac and retrofitting it to add calls to the debugger framework? That could run much faster. I actually ported over a pure Lua luac implementation to CC not too long ago with the intent to add preemptive multithreading to Lua, but never ended up going further than porting luac.
Is your cc luac version uploaded somewhere?
Yevano #14
Posted 04 December 2015 - 12:00 AM
Is your cc luac version uploaded somewhere?

No and I'm not at my PC which has the port on it, but I'll certainly send it to you tomorrow if you want. To clarify, the code is originally from Yueliang, and I merely ported over the incompatibilities with CC's libraries (mostly file I/O). In my limited testing, it seemed to work great.
oeed #15
Posted 04 December 2015 - 01:25 AM
Wow. This is awesome. If I ever make an IDE with Silica I'd love to be able to do this.
Lion4ever #16
Posted 04 December 2015 - 11:29 AM
Wow. This is awesome. If I ever make an IDE with Silica I'd love to be able to do this.
I tried to add it to the LuaIDE on gravitystores github, but i could not get it to work…
Lion4ever #17
Posted 04 December 2015 - 06:18 PM
Updated to version 1.1

Added inline lua prompt (f10)
stopping while running on advanced computers (f6)
and bug fixes