Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging embedded Lua 5.2.2 code

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.

like image 963
Caesar Avatar asked Sep 08 '13 02:09

Caesar


2 Answers

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

like image 122
Paul Kulchenko Avatar answered Nov 10 '22 05:11

Paul Kulchenko


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.

like image 25
greatwolf Avatar answered Nov 10 '22 05:11

greatwolf