actboy168 / lua-debug

Lua Debug Adapter for Visual Studio Code
MIT License
435 stars 94 forks source link

What configuration do I need to debug with the mpv media player? #164

Closed stax76 closed 2 years ago

stax76 commented 2 years ago

In launch.json do I need "request": "attach" or "request": "launch" ? Or might both work?

I tried both methods and had some success with both, but not full success.

In both cases, the breakpoints were not hit in the lua script that was loaded by mpv, but they were hit in another file called with dofile.

local aaa = 'D:\\Work'

package.path = package.path .. ';' .. aaa .. '/?.lua;' .. aaa .. '/?/init.lua'

print "aaa"   -- this showed in an external terminal where I started mpv
require "debugger":start "127.0.0.1:4278":event "wait"
print "bbb"   -- this showed in the VS Code Debug console
dofile('D:/Work/test.lua')   -- in this file it breaks

Is maybe the way how mpv loads the script the problem? Code is here:

https://github.com/mpv-player/mpv/blob/master/player/lua.c#L247

I've made a mpv debug build with msys2 and this guide:

https://github.com/mpv-player/mpv/blob/master/DOCS/compile-windows.md#native-compilation-with-msys2

The project I work on is a mpv frontend:

https://github.com/stax76/mpv.net

Perhaps @avih, who is a mpv dev that is active with scripting, can help us. He has helped me before with scripting.

My launch.json config:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "attach",
            "type": "lua",
            "request": "attach",
            "stopOnEntry": true,
            "address": "127.0.0.1:4278",
        },
        {
            "type": "lua",
            "request": "launch",
            "name": "launch",
            "runtimeExecutable": "D:/Software/Development/MSYS2/mingw64/bin/mpv.exe",
            "runtimeArgs": ["--script=d:\\Work\\delete-current-file.lua", "D:\\Samples\\castele.m2v"],
            "stopOnEntry": false,
            "luaVersion": "5.1",
        }
    ]
}
avih commented 2 years ago

Perhaps @avih, who is a mpv dev that is active with scripting, can help us.

So far I don't understand the issue or its context.

Is the problem about being able to debug lua scripts while running in mpv? I don't have any experience with this, but IIRC some lua debug things depend on luasocket, which is probably unlikely to work with the embedded version of lua at libmpv.

Or is it about a specific issue in some script which you're not sure how to solve? (if yes, where's the script, and what's the issue?)

Or something else?

stax76 commented 2 years ago

Is the problem about being able to debug lua scripts while running in mpv?

That's it, yes. Thanks for the input.

stax76 commented 2 years ago

Here is a mpv debug build:

https://www.mediafire.com/file/2cez89ij61twdaa/mpv-debug.zip/file

https://www.dropbox.com/s/56d5heku1iiub7p/mpv-debug.zip?dl=0

sumneko commented 2 years ago

image

This is because the file is loaded does not meet the specification.

see: https://www.lua.org/manual/5.1/manual.html#3.8

source: If the function was defined in a string, then source is that string. If the function was defined in a file, then source starts with a '@' followed by the file name.

You should use load(filecontent, '@' .. filepath)

stax76 commented 2 years ago

I will try this, thanks!

actboy168 commented 2 years ago

This launch.json is enough to load the debugger.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lua",
            "request": "launch",
            "name": "launch",
            "runtimeExecutable": "${workspaceFolder}/mpv.exe",
            "stopOnEntry": true,
            "luaVersion": "5.1",
        }
    ]
}

As @sumneko said, mpv doesn't set the chunkname correctly, so the debugger can't get information about the source file.

actboy168 commented 2 years ago

For mpv's built-in lua, you can use sourcemaps to map them to the correct files. But you have to make sure that the mapped file is exactly the same as the file used by mpv. Of course the best solution is to let mpv use the correct chunkname.


            "sourceMaps": [
                [
                    "mp.*",
                    "${workspaceFolder}/mpv/player/lua/*.lua"
                ],
                [
                    "@*",
                    "${workspaceFolder}/mpv/player/lua/*"
                ]
            ]

For non-built-in lua, mpv does not add @ in chunkname, so sourcemap can't solve it either.

stax76 commented 2 years ago

I have it fully working now, wrote about it in the mpv thread, thank you for your great work and support, greatly appreciated.

stax76 commented 2 years ago

@actboy168 Change was added to mpv master here:

https://github.com/mpv-player/mpv/commit/84821dbcb6d9e16b8f11da2135208e4f3e66fcd0