Debugging embedded Lua 5.2.2 code
Asked Answered
A

2

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.

Adrial answered 8/9, 2013 at 2:16 Comment(0)
D
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.)

Dormancy answered 8/9, 2013 at 5:45 Comment(8)
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))Adrial
attempt to index global 'package' (a nil value): so, what happened to the package global in your environment?Dormancy
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.Dormancy
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.Adrial
@Adrial 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.Biafra
@Biafra 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); }Adrial
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.Biafra
@Biafra Can you provide an example please?Adrial
B
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.

Biafra answered 9/9, 2013 at 1:57 Comment(3)
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.Adrial
Are you trying to debug an actual C/C++ application that's embedding lua?Biafra
I made a room so we can discuss this, would be much cleaner then here. please join me chat.stackoverflow.com/rooms/37030/…Adrial

© 2022 - 2024 — McMap. All rights reserved.