pkulchenko/MobDebug

Debugging lua scripts within mpv , crashes

Opened this issue · 6 comments

Hi Paul,

-I copied the mobdebug.lua under /usr/local/share/lua/5.1
-Created foo.lua in /opt/ZeroBraneStudio-1.90/myprograms so it is reachable by zbstudio with this content
require('mobdebug').start()
lineforthebreakpoint=1
I installed the plugin autostart from https://github.com/pkulchenko/ZeroBranePackage/blob/master/autostartdebug.lua
cp autostartdebug.lua /opt/ZeroBraneStudio-1.90/packages

then launched zbstudio
i put the dummy breakpoint on lineforthebreakpoint and it works when I run , it switches into debug and halts on this line

BUT if i trigger this from mpv latest relying on LuaJIT 2.1.0-beta3

mpv 0.33.0-209-gf2afae55e9 Copyright © 2000-2020 mpv/MPlayer/mplayer2 projects
built on Sun Jul 4 14:32:32 CEST 2021
FFmpeg library versions:
libavutil 56.70.100
libavcodec 58.134.100
libavformat 58.76.100
libswscale 5.9.100
libavfilter 7.110.100
libswresample 3.9.100
FFmpeg version: n4.4-78-g031c0cb0b4
then the call mpv --script=foo.lua bar.mp4 gives me Segmentation fault

Any idea Sir?

You can try getting a stack trace to see where it fails. You can also add print statements to the code to see if it fails somewhere inside mobdebug or if it happens in the mpv code. I'm not aware of any mobdebug issues that may lead to a segfault.

That sounds a good idea! Here is the backtrace using ddd mpv and inside run FoFg.mp4, then bt
The foo.lua script has been deported into ~/.config/mpv/scripts/ as I don't succeed to pass --script, we use the fact all script
are autoloaded.from this last directory , I got:
`GNU DDD 3.3.12 (x86_64-pc-linux-gnu), by Dorothea LReading symbols from mpv...done.
(gdb) run FoFg.mp4
Starting program: /usr/local/bin/mpv FoFg.mp4
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe8709700 (LWP 12796)]
ew Thread 0x7fffe7f08700 (LWP 12797)]
[New Thread 0x7fffe7707700 (LWP 12798)]
[New Thread 0x7fffe6f06700 (LWP 12799)]
[New Thread 0x7fffe6705700 (LWP 12800)]
[New Thread 0x7fffe5f04700 (LWP 12801)]
[New Thread 0x7fffe5703700 (LWP 12802)]
[New Thread 0x7fffe4f02700 (LWP 12803)]
[New Thread 0x7fffcffff700 (LWP 12804)]
[Thread 0x7fffe5f04700 (LWP 12801) exited]

Thread 9 "mpv/lua script " received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffe4f02700 (LWP 12803)]
0x00007fffe402de0c in timeout_markstart () from /usr/local/lib/lua/5.1/socket/core.so
(gdb) bt
#0 0x00007fffe402de0c in timeout_markstart () from /usr/local/lib/lua/5.1/socket/core.so
#1 0x00007fffe402e41a in buffer_meth_receive () from /usr/local/lib/lua/5.1/socket/core.so
#2 0x00007ffff5991dd7 in ?? () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
#3 0x00007ffff59a2035 in ?? () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
#4 0x00007ffff59d4dc6 in ?? () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
#5 0x00007ffff5993741 in ?? () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
#6 0x0000555555618828 in load_file (fname=0x5555559ffeb0 "/home/james/.config/mpv/scripts/foo.lua", L=0x40140378) at ../player/lua.c:240
#7 load_scripts (L=0x40140378) at ../player/lua.c:302
#8 0x00007ffff5991dd7 in ?? () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
#9 0x00007ffff59df22c in lua_pcall () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
#10 0x00005555556198e9 in run_lua (L=0x40140378) at ../player/lua.c:410
#11 0x00007ffff5991dd7 in ?? () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
#12 0x00007ffff59df369 in lua_cpcall () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
#13 0x0000555555616ffc in load_lua (args=) at ../player/lua.c:451
#14 0x0000555555620ad5 in run_script (arg=arg@entry=0x555555a262f0) at ../player/scripting.c:91
#15 0x0000555555620f09 in script_thread (p=0x555555a262f0) at ../player/scripting.c:103
#16 0x00007ffff4a39fa3 in start_thread (arg=) at pthread_create.c:486
#17 0x00007ffff496a4cf in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95`

When i put print as suggested in local function start(controller_host, controller_port)
it crashed when the function ends event with try catch..

New foo.lua:

` function script_path()
--Note return os.path.dirname(file) DOES NOT WORK
local str = debug.getinfo(2, "S").source:sub(1)
return str:match("(.*/)")
end

local script_path = script_path() -- implement in your host golang program

--relocate mobdebug localy to hijack it with some print
package.path = script_path .. [[mobdebug/?.lua;]] .. package.path

print(package.path)

function catch(what)
return what[1]
end

function try(what)
status, result = pcall(what[1])
if not status then
what2
end
return result
end

try {
function()
local mdb=require('mobdebug')
--Crashes at the end of this next call, RETURN BADLY HANDLED
mdb.start()
end,

catch {
function(error)
print('caught error: ' .. error)
end
}
}

lineforthebreakpoint=1`

@sosie-js, thank yor for the details.

#0 0x00007fffe402de0c in timeout_markstart () from /usr/local/lib/lua/5.1/socket/core.so
#1 0x00007fffe402e41a in buffer_meth_receive () from /usr/local/lib/lua/5.1/socket/core.so

Given the stack trace, this looks like a luasocket issue (and the fact that a socket connection is initiated from the start() call confirms that). There has been several discussions about handling of lua buffers and their impact on luasocket (see for example, here and here). The discussions are not directly related to luasocket, but there is a known luasocket issue that surfaced with Lua 5.4.3, but the issue actually existed with earlier versions of Lua, but it was less predictable and probably highly context dependent (so I'm not too surprised that it may be showing up only in your case).

I'd suggest a couple of things. Try using the most recent version of mobdebug, as there are several changes I implemented to address this issue. You can also try to update the version of luasocket to the one with the proposed patch (it's referenced in the ticket I mentioned). Another option would be to comment out timeout_markstart call to see if this fixes the issue or if it pushes the error somewhere else.

Paul you are welcome.

I tried already the latest mobdebug 0.801
I have seen since an extra @ poluting script_path , so I modified script_path function it to
function script_path() --Note return os.path.dirname(__file__) DOES NOT WORK local str = debug.getinfo(2, "S").source:sub(1) str=str:gsub("@", "") return str:match("(.*/)") end

after lancing zbstudio, if i do
lua5.1 ~/.config/mpv/scripts/foo.lua
or
luajit ~/.config/mpv/scripts/foo.lua

then no problem to debug, no crash.

Now, if zbstudio is closed, then mpv FoFg.mp4 gives
Could not connect to localhost:8172: closed which is normal and it does not crash

It is then when Zbstudio is open and the hook set that it crashes.
thus I commented it out --debug.sethook(debug_hook, HOOKMASK)
but then debugging in zbstudio did no work anymore.
Seem your hook triggers the Segmentation Fault inside luajit called indirectly by mpv

I put some print in local function debug_hook(event, line)
What I saw by comparing luajit ~/.config/mpv/scripts/foo.lua VS mpv FoFg.mp4 outputs
, is there are more 2* 3 group extra calls in the crash case of the section after setting debug.sethook(debug_hook, HOOKMASK):
"if jit and not (ngx and type(ngx) == "table" and ngx.say) then"
...(coro is 'main' each time)
going out with "return"
Maybe the stack is thus twisted or the timeout_markstart is reached (watchdog somewhere?)

PS: I dont have the luasocket issue in 5.1/luajit you mentioned. Http/https works fine

I don't know about your hook bur maybe https://github.com/mpv-player/mpv/blob/master/TOOLS/lua/test-hooks.lua can give a clue of incompatibility debug.sethook(debug_hook, HOOKMASK)