tags:

views:

2461

answers:

5

How do you debug lua code embedded in a c++ application?

From what I gather, either I need to buy a special IDE and link in their special lua runtime (ugh). Or I need to build a debug console in to the game engine, using the lua debug API calls.

I am leaning toward writing my own debug console, but it seems like a lot of work. Time that I could better spend polishing the other portions of the game.

A: 

If you are using windows and VS - Can you use the trick we use?

Copy the lua code in a file. Then in the lua code make a call to the Debugger api (in C++ this is DebuggerBreak() I think - see here). then when the lua code executes the debugger will fire up and you should be able to specify the file. Then debug as normal?

Preet Sangha
That solves half the problem. What I'd really like to do is execute the lua code line by line. If in understand your solution, I'll be able to break at any line in the lua script, but I'll be in the c++ debugger. How would I inspect local lua variables and such.
caspin
I see this as a problem in two parts - one is easy inspection which would be hard as you'd be writing debugger extensions to do this. The second way would be determine how the the lua 'engine' stores these values and inspect them either manually or via macro's perhaps.
Preet Sangha
+2  A: 

I don't see how calling DebuggerBreak should work, since that is .NET specific. I would assume that only works with the forked Lua targeting the CLR.

If you are using standard Lua you have some rudementary debugging facilities through the lua function call debug.debug(). That will throw Lua into your console, so if you are running lua from a console, you should be able issue lua commands interactively to inspect your current state. debug.debug() wont put you into the current stack frame, so you have to use debug.getlocal() to read the values of your variables.

I haven't tried it myself yet, but I actually don't think making your own workable debug console is that much work. Remember Lua is not as complicated language as C++, so doing this is a lot easier than making a real C++ debugger like say gdb.

I think there are a lot of people who have done similar things already, whos code you could look at. Here is CLI debugger written in only lua. Just one lua file. Shouldn't be to hard use and modify for your needs.

Adam Smith
+4  A: 

There are several tools floating around that can do at least parts of what you want. I have seen references to a VS plugin, there is a SciTE debugger extension in Lua for Windows, and there is the Kepler project's RemDebug, as well as their LuaEclipse.

RemDebug may be on the track of what you need, as it was built to allow for debugging CGI scripts written in Lua. It does require access to the LuaSocket module to provide a communications channel between the target script and a controller as well as a couple of other modules.

A bigger issue might be the ability to load arbitrary modules from within whatever sandbox the game engine has put around your scripts. If you have some control over the engine, then that won't be as big an issue.

This isn't currently possible for developers of Adobe Lightroom plugins, for example, because Lightroom does not expose require inside the plugin's sandbox.

A surprise to me has been how rarely I have felt a need for a debugger when working with Lua. I've built several small applications in it for various projects and have been surprised at how well a combination of complete stack backtraces and the occasional print call works to locate the bugs that require "strict" didn't prevent in the first place.

RBerteig
The RemDebugger is almost exactly what I was looking for. It should be usable as is, and give me a good base to customize from.
caspin
+2  A: 

How about Decoda?? there is a video that explains how to use it, and it works pretty darn well for embedded lua source. (i am a happy customer). and it's pretty cheap.

Joe
+1  A: 

If you are building a GUI and using LUA you can look at Crank Software's Stortboard Suite

It uses LUA for it's script engine and in it's 1.1 version comes with an integrated eclipse based LUA debugger

http://cranksoftware.com/blog/?p=242

Jason