6

How can I debug Lua 5.2.2 code that is embedded inside of my C++ application?

I have already taken a look at this question and all the IDEs provided in it deal with 5.1 and lower and when I try to use them with 5.2.2 they crash.

Community
  • 1
  • 1
Caesar
  • 9,483
  • 8
  • 40
  • 66

2 Answers2

5

You should be able to debug your application using ZeroBrane Studio by following instructions for Lua 5.2 debugging. Note that you'll need to have luasocket compiled against Lua5.2. (The crash you see is likely because your application loads luasocket that is compiled against Lua5.1, which in turn loads Lua5.1 DLL or fails to find required symbols.)

If you don't want to compile luasocket, you can get binaries for Windows/OSX/Linux from this folder and its subfolders; just make sure that these libraries are in LUA_CPATH before any folders that may have luasocket compiled against Lua5.1.

[Updated based on chat discussion] The reason you may be getting multiple VM issue is that your app is probably statically compiles Lua interpreter. You then load luasocket (directly or through mobdebug), which is compiled against lua52.dll, which loads another copy of the interpreter. To avoid this you have two choices: (1) compile luasocket into your app the same way you include lua interpreter itself; you won't need anything else except one mobdebug.lua file to debug your app, or (2) use proxy dll; it will look like lua52.dll, but will actually proxy your calls to your statically compiled lua library, avoiding problems with multiple VMs. The proxy dll is for Lua 5.1, but you can tweak the script to make it work for Lua 5.2.

(If your interpreter is not statically compiled, you may still get two interpreters if the Lua DLL you load is named differently from lua52.dll.)

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • That didn't work for me. My program crashes as soon as I call lua_call. This is what I did, I placed this code into of my .lua file `package.path = package.path .. "C:/ZeroBraneStudio/lualibs/mobdebug/mobdebug.lua" package.cpath = package.cpath .. "C:/ZeroBraneStudio/bin/clibs52/socket/?.dll" require('mobdebug').start()` and the error I get is `PANIC: unprotected error in call to Lua API (main.lua:3: attempt to index global 'package' (a nil value))` – Caesar Sep 08 '13 at 15:15
  • `attempt to index global 'package' (a nil value)`: so, what happened to the `package` global in your environment? – Paul Kulchenko Sep 08 '13 at 16:04
  • Also, you may want to add `...clibs52/mime/?.dll` as it may be loaded from `socket.lua` and you want to make sure that it's loaded from `clibs52`. It may be more convenient to switch the troubleshooting discussion to [ZeroBrane Studio maillist](http://studio.zerobrane.com/community.html). – Paul Kulchenko Sep 08 '13 at 16:06
  • I don't know, even putting in something simple like this `package.path = package.path .. ";../?.lua"` ends up crashing it with the same error. I think there is a step I'm missing. – Caesar Sep 08 '13 at 16:37
  • @Caesar Can you think of anything that might have set your `package` table to nil or erased it? Or perhaps a change in the running `_ENV` from sandboxed code? When an uncaught lua error is allowed to propogate into C space, that's what happens -- it PANICS. – greatwolf Sep 08 '13 at 20:27
  • @greatwolf Not really, this is a basic code I'm running that I found from a tutorial. I know the comments aren't great for posting codes but here it is `int main () { lua_State* lua = luaL_newstate(); luaopen_base ( lua ); int error = luaL_loadfile(lua, mainLua); lua_call(lua,0,0); }` – Caesar Sep 09 '13 at 01:28
  • You cannot call `luaopen_base` like that. You have to call it like a lua C funct -- using `lua_call` or `lua_pcall` for example. – greatwolf Sep 09 '13 at 01:32
  • @greatwolf Can you provide an example please? – Caesar Sep 09 '13 at 01:36
3

In response to OP's commented request, here's how you should open the lua standard library "base" from C++:

#include "lua.hpp"

//...
int main ()
{
  lua_State* L = luaL_newstate();
  luaL_requiref(L, "base", luaopen_base, 0);

  // ...
  int error = luaL_loadfile(L, mainLua); 
  lua_call(L, 0, 0);

  lua_close(L);
}

Note that you can open all the standard libraries at once by replacing:

luaL_requiref(L, "base", luaopen_base, 0);

with

luaL_openlibs(L);

The Lua 5.2 reference manual Section 6 has more info about this.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • Thanks, that seemed to do the trick for that error but I seem to have another error. `PANIC: unprotected error in call to Lua API (error loading module 'socket.core' from file 'C:/ZeroBraneStudio/bin/clibs52/socket\core.dll': The specified module could not be found. )` I have looked into that location and I can tell you that it is there through copy and paste. – Caesar Sep 09 '13 at 02:05
  • Are you trying to debug an actual C/C++ application that's embedding lua? – greatwolf Sep 09 '13 at 02:07
  • I made a room so we can discuss this, would be much cleaner then here. please join me http://chat.stackoverflow.com/rooms/37030/debugging-embedded-lua-5-2-2-code – Caesar Sep 09 '13 at 02:08