debuggingluazerobrane

messed up lua paths after trying to set up debugging


I would like to debug my way through a large lua codebase. To do this, I downloaded ZeroBrane and followed their instructions to set up the bundled mobdebug.

The codebase is koreader. The following shellscript reproduces what I've done:

# dependencies for building koreader
sudo apt-get install build-essential git patch wget unzip \
gettext autoconf automake cmake libtool nasm luarocks libsdl2-dev \
libssl-dev libffi-dev libsdl2-dev libc6-dev-i386 xutils-dev linux-libc-dev:i386 zlib1g:i386

# get the source
git clone https://github.com/koreader/koreader.git
cd koreader && ./kodev fetch-thirdparty

# build it, this will take a long time
./kodev build

# assuming you have ZeroBrane installed
export ZBS=/opt/zbstudio
export LUA_PATH="./?.lua;$ZBS/lualibs/?/?.lua;$ZBS/lualibs/?.lua"
export LUA_CPATH="$ZBS/bin/linux/x86/?.so;$ZBS/bin/linux/x86/clibs/?.so"

# execute it, this will run lua
./kodev run

Executing ./kodev run leads to the following error message:

 [*] Current time: 10/14/19-17:55:34
./luajit: ./datastorage.lua:3: module 'libs/libkoreader-lfs' not found:
    no field package.preload['libs/libkoreader-lfs']
    no file './libs/libkoreader-lfs.lua'
    no file '/opt/zbstudio/lualibs/libs/libkoreader-lfs/libs/libkoreader-lfs.lua'
    no file '/opt/zbstudio/lualibs/libs/libkoreader-lfs.lua'
    no file '/opt/zbstudio/bin/linux/x86/libs/libkoreader-lfs.so'
    no file '/opt/zbstudio/bin/linux/x86/clibs/libs/libkoreader-lfs.so'
stack traceback:
    [C]: in function 'require'
    ./datastorage.lua:3: in main chunk
    [C]: in function 'require'
    ./reader.lua:18: in main chunk
    [C]: at 0x55a81bf25771
~/programming/koreader

If there are no definitions of LUA_PATH and LUA_CPATH, there are no problems and koreader runs just fine. So I assume that the import paths are somehow broken. How do I set this up correctly?

Maybe this helps you, if I read the code correctly, ./kodev run will (at some point) execute this:

-- set search path for 'require()'
package.path =
    "common/?.lua;rocks/share/lua/5.1/?.lua;frontend/?.lua;" ..
    package.path
package.cpath =
    "common/?.so;common/?.dll;/usr/lib/lua/?.so;rocks/lib/lua/5.1/?.so;" ..
    package.cpath

The actual location of libkoreader-lfs.so is:

/home/lklein/programming/koreader/base/build/x86_64-linux-gnu-debug/libs/libkoreader-lfs.so

Indeed, if I append this to the CPATH with

/home/lklein/programming/koreader/base/build/x86_64-linux-gnu-debug/?.so

then it can run. What is lua doing here? Is it always assuming some default LUA_PATH and LUA_CPATH if none are set? Because it works just fine without me specifying any path.


Solution

  • The default values for both paths are located in luaconf.h#LUA_PATH_DEFAULT ff. (5.3).

    That being said - your LUA_CPATH misses two things from the default one: LUA_CDIR/loadall.so and ./?.so. In your case the problem is due to lack of the second one.

    That is because the ./kodev run do some additional actions to set-up runtime environment. At some point the working directory is changed to EMU_DIR (e.g. ./koreader-emulator-x86_64-linux-gnu-debug/koreader). This very directory also has a valid libs/libkoreader-lfs.so.

    Change LUA_CPATH to contain ./?.so:

    export LUA_CPATH="$ZBS/bin/linux/x86/?.so;$ZBS/bin/linux/x86/clibs/?.so;./?.so"