2

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.

lhk
  • 27,458
  • 30
  • 122
  • 201
  • It really takes long to build. I've waited like 20 minutes to verify it already: add `./?.so` to `LUA_CPATH`, it seems that run script changes directory to `base/build/...` and that's the only meaningful difference between your path and default one. I'll leave an answer once I can verify it. – Aki Oct 15 '19 at 08:49

2 Answers2

1

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"
Aki
  • 2,818
  • 1
  • 15
  • 23
  • awesome, thanks. (extra thanks for actually building the repo. It takes indeed very long :D) – lhk Oct 15 '19 at 09:27
0

Your code is trying to load library libkoreader-lfs, which is not found in any of the paths set up in LUA_CPATH or LUA_PATH. I don't know your project structure to give you the exact answer, but since it works when you don't set up those environment variables and doesn't work when you do, it looks like you are overwriting some default values for those variables.

Try adding to those variables instead of setting them:

export LUA_PATH="$LUA_PATH;./?.lua;$ZBS/lualibs/?/?.lua;$ZBS/lualibs/?.lua"
export LUA_CPATH="$LUA_CPATH;$ZBS/bin/linux/x86/?.so;$ZBS/bin/linux/x86/clibs/?.so"
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • I tried that, too. But it makes no difference. In fact, LUA_PATH doesn't exist before the script is executed. I think it's something lua specific. – lhk Oct 14 '19 at 17:40
  • You are not showing the entire script you are running. For example, your `package.cpath` includes `common/?.so` and `common?.dll` paths, but none of them are shown in the error message. Can you show the *entire* script that only includes setting path/cpath and `require` command that fails? Also, what is the actual location of the module you are trying to load relative to the script you are running? – Paul Kulchenko Oct 14 '19 at 18:01
  • This is a huge existing codebase, not just one script. It's not clear to me what's executed when. The code is here https://github.com/koreader/koreader maybe the structure makes sense to a lua pro. But I can't just give you the relevant snippets. I don't know which ones that would be. – lhk Oct 14 '19 at 18:30