From ffdcb7bcfff03f96ad963f148a8251049feda9f9 Mon Sep 17 00:00:00 2001 From: fesily Date: Fri, 17 Mar 2023 21:34:52 +0800 Subject: [PATCH 01/51] add launcher config --- .gitmodules | 4 +- 3rd/frida_gum/gumpp | 2 +- 3rd/json | 1 + compile/common/package_json.lua | 997 +++++++++++++------------ compile/macos/runtime.lua | 1 + compile/windows/runtime.lua | 1 + extension/js/configurationProvider.js | 3 + extension/script/frontend/proxy.lua | 32 +- src/launcher/autoattach/autoattach.cpp | 10 +- src/launcher/autoattach/lua_module.cpp | 21 +- src/launcher/config/config.cpp | 77 ++ src/launcher/config/config.h | 23 + 12 files changed, 651 insertions(+), 521 deletions(-) create mode 160000 3rd/json create mode 100644 src/launcher/config/config.cpp create mode 100644 src/launcher/config/config.h diff --git a/.gitmodules b/.gitmodules index 3f804f6b7..7c87d52db 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,4 +10,6 @@ [submodule "3rd/frida_gum/gumpp"] path = 3rd/frida_gum/gumpp url = https://github.com/fesily/gumpp - +[submodule "3rd/json"] + path = 3rd/json + url = https://github.com/nlohmann/json diff --git a/3rd/frida_gum/gumpp b/3rd/frida_gum/gumpp index 41bee742d..54ad1d5a6 160000 --- a/3rd/frida_gum/gumpp +++ b/3rd/frida_gum/gumpp @@ -1 +1 @@ -Subproject commit 41bee742d0176aaf569a2513464339543e63c2de +Subproject commit 54ad1d5a6c8d4856f1db0a3899625a2370b1ca19 diff --git a/3rd/json b/3rd/json new file mode 160000 index 000000000..546370c9e --- /dev/null +++ b/3rd/json @@ -0,0 +1 @@ +Subproject commit 546370c9e778d99e7176641123e5cc1d0b62acab diff --git a/compile/common/package_json.lua b/compile/common/package_json.lua index bf9d79e7b..275b5af4b 100644 --- a/compile/common/package_json.lua +++ b/compile/common/package_json.lua @@ -1,492 +1,505 @@ -local platform = ... -platform = platform or "unknown-unknown" - -local OS, ARCH = platform:match "^([^-]+)-([^-]+)$" - -local json = { - name = "lua-debug", - version = "1.61.0", - publisher = "actboy168", - displayName = "Lua Debug", - description = "VSCode debugger extension for Lua", - icon = "images/logo.png", - private = true, - author = { - name = "actboy168", - }, - bugs = { - url = "https://github.com/actboy168/lua-debug/issues", - }, - repository = { - type = "git", - url = "https://github.com/actboy168/lua-debug", - }, - keywords = { - "lua", - "debug", - "debuggers", - }, - categories = { - "Debuggers", - }, - engines = { - vscode = "^1.61.0", - }, - extensionKind = { - "workspace", - }, - main = "./js/extension.js", - activationEvents = { - "onCommand:extension.lua-debug.runEditorContents", - "onCommand:extension.lua-debug.debugEditorContents", - "onDebugInitialConfigurations", - "onDebugDynamicConfigurations", - "onDebugResolve:lua", - }, - capabilities = { - untrustedWorkspaces = { - description = "Debugging is disabled in Restricted Mode.", - supported = false, - }, - }, - contributes = { - breakpoints = { - { - language = "lua", - }, - { - language = "html", - }, - }, - commands = { - { - command = "extension.lua-debug.runEditorContents", - icon = "$(play)", - title = "Run File", - }, - { - command = "extension.lua-debug.debugEditorContents", - icon = "$(debug-alt-small)", - title = "Debug File", - }, - { - command = "extension.lua-debug.showIntegerAsDec", - title = "Show as Dec", - }, - { - command = "extension.lua-debug.showIntegerAsHex", - title = "Show as Hex", - }, - }, - configuration = { - properties = { - ["lua.debug.variables.showIntegerAsHex"] = { - default = false, - description = "Show integer as hex.", - type = "boolean", - }, - }, - }, - debuggers = { - { - type = "lua", - languages = { - "lua", - }, - label = "Lua Debug", - configurationSnippets = { - { - label = "Lua Debug: Launch Script", - description = "A new configuration for launching a lua debug program", - body = { - type = "lua", - request = "launch", - name = "${1:launch}", - stopOnEntry = true, - program = "^\"\\${workspaceFolder}/${2:main.lua}\"", - arg = { - }, - }, - }, - { - label = "Lua Debug: Attach", - description = "A new configuration for attaching a lua debug program", - body = { - type = "lua", - request = "attach", - name = "${1:attach}", - stopOnEntry = true, - address = "127.0.0.1:4278", - } - } - } - } - }, - menus = { - ["debug/variables/context"] = { - { - command = "extension.lua-debug.showIntegerAsDec", - group = "1_view", - when = "debugConfigurationType == 'lua' && debugProtocolVariableMenuContext == 'integer/hex'", - }, - { - command = "extension.lua-debug.showIntegerAsHex", - group = "1_view", - when = "debugConfigurationType == 'lua' && debugProtocolVariableMenuContext == 'integer/dec'", - }, - }, - ["editor/title/run"] = { - { - command = "extension.lua-debug.runEditorContents", - when = "resourceLangId == lua", - }, - { - command = "extension.lua-debug.debugEditorContents", - when = "resourceLangId == lua", - }, - }, - }, - }, -} - -local attributes = {} - -attributes.common = { - luaVersion = { - default = "5.4", - enum = { - "5.1", - "5.2", - "5.3", - "5.4", - "latest", - "jit", - }, - markdownDescription = "%lua.debug.launch.luaVersion.description%", - type = "string", - }, - outputCapture = { - default = { - }, - items = { - enum = { - "print", - "io.write", - "stdout", - "stderr", - }, - }, - markdownDescription = "From where to capture output messages: print or stdout/stderr streams.", - type = "array", - }, - pathFormat = { - default = "path", - enum = { - "path", - "linuxpath", - }, - markdownDescription = "Path format", - type = "string", - }, - sourceFormat = { - default = "path", - enum = { - "path", - "string", - "linuxpath", - }, - markdownDescription = "Source format", - type = "string", - }, - sourceMaps = { - default = { - { - "./*", - "${workspaceFolder}/*", - }, - }, - markdownDescription = "The source path of the remote host and the source path of local.", - type = "array", - }, - skipFiles = { - default = { - }, - items = { - type = "string", - }, - markdownDescription = "An array of glob patterns for files to skip when debugging.", - type = "array", - }, - stopOnEntry = { - default = false, - markdownDescription = "Automatically stop after entry.", - type = "boolean", - }, - stopOnThreadEntry = { - default = true, - markdownDescription = "Automatically stop after thread entry.", - type = "boolean", - }, - address = { - default = "127.0.0.1:4278", - markdownDescription = [[ -Debugger address. -1. IPv4 e.g. `127.0.0.1:4278` -2. IPv6 e.g. `[::1]:4278` -3. Unix domain socket e.g. `@c:\\unix.sock`]], - type = { - "string", - "null", - }, - }, - client = { - default = true, - markdownDescription = "Choose whether to `connect` or `listen`.", - type = "boolean", - }, - inject = { - default = "none", - markdownDescription = "How to inject debugger.", - enum = { - "none", - }, - type = "string", - }, -} - -if OS == "win32" then - attributes.common.inject.default = "hook" - table.insert(attributes.common.inject.enum, "hook") -elseif OS == "darwin" then - attributes.common.inject.default = "lldb" - table.insert(attributes.common.inject.enum, "hook") - table.insert(attributes.common.inject.enum, "lldb") - attributes.common.inject_executable = { - markdownDescription = "inject executable path", - type = { - "string", - "null", - }, - } -end - -attributes.attach = { -} - -if OS == "win32" or OS == "darwin" then - attributes.attach.processId = { - default = "${command:pickProcess}", - markdownDescription = "Id of process to attach to.", - type = "string", - } - attributes.attach.processName = { - default = "lua.exe", - markdownDescription = "Name of process to attach to.", - type = "string", - } - json.activationEvents[#json.activationEvents+1] = "onCommand:extension.lua-debug.pickProcess" - json.contributes.debuggers[1].variables = { - pickProcess = "extension.lua-debug.pickProcess", - } -end - -attributes.launch = { - luaexe = { - markdownDescription = "Absolute path to the lua exe.", - type = "string", - }, - program = { - default = "${workspaceFolder}/main.lua", - markdownDescription = "Lua program to debug - set this to the path of the script", - type = "string", - }, - arg = { - default = { - }, - markdownDescription = "Command line argument, arg[1] ... arg[n]", - type = "array", - }, - arg0 = { - default = { - }, - markdownDescription = "Command line argument, arg[-n] ... arg[0]", - type = { - "string", - "array", - }, - }, - path = { - default = "${workspaceFolder}/?.lua", - markdownDescription = "%lua.debug.launch.path.description%", - type = { - "string", - "array", - "null", - }, - }, - cpath = { - markdownDescription = "%lua.debug.launch.cpath.description%", - type = { - "string", - "array", - "null", - }, - }, - luaArch = { - markdownDescription = "%lua.debug.launch.luaArch.description%", - type = "string", - }, - cwd = { - default = "${workspaceFolder}", - markdownDescription = "Working directory at program startup", - type = { - "string", - "null", - }, - }, - env = { - additionalProperties = { - type = { - "string", - "null", - }, - }, - default = { - PATH = "${workspaceFolder}", - }, - markdownDescription = "Environment variables passed to the program. The value `null` removes thevariable from the environment.", - type = "object", - }, - console = { - default = "integratedTerminal", - enum = { - "internalConsole", - "integratedTerminal", - "externalTerminal", - }, - enummarkdownDescriptions = { - "%lua.debug.launch.console.internalConsole.description%", - "%lua.debug.launch.console.integratedTerminal.description%", - "%lua.debug.launch.console.externalTerminal.description%", - }, - markdownDescription = "%lua.debug.launch.console.description%", - type = "string", - }, - runtimeExecutable = { - default = OS == "win32" and "${workspaceFolder}/lua.exe" or "${workspaceFolder}/lua", - markdownDescription = "Runtime to use. Either an absolute path or the name of a runtime availableon the PATH.", - type = { - "string", - "null", - }, - }, - runtimeArgs = { - default = "${workspaceFolder}/main.lua", - markdownDescription = "Arguments passed to the runtime executable.", - type = { - "string", - "array", - "null", - }, - }, -} - -if OS == "win32" or OS == "darwin" then - local snippets = json.contributes.debuggers[1].configurationSnippets - snippets[#snippets+1] = { - label = "Lua Debug: Launch Process", - description = "A new configuration for launching a lua process", - body = { - type = "lua", - request = "launch", - name = "${1:launch process}", - stopOnEntry = true, - runtimeExecutable = "^\"\\${workspaceFolder}/lua.exe\"", - runtimeArgs = "^\"\\${workspaceFolder}/${2:main.lua}\"", - } - } - snippets[#snippets+1] = { - label = "Lua Debug: Attach Process", - description = "A new configuration for attaching a lua debug program", - body = { - type = "lua", - request = "attach", - name = "${1:attach}", - stopOnEntry = true, - processId = "^\"\\${command:pickProcess}\"", - } - } -end - -if OS == "win32" then - attributes.common.sourceCoding = { - default = "utf8", - enum = { - "utf8", - "ansi", - }, - markdownDescription = "%lua.debug.launch.sourceCoding.description%", - type = "string", - } - attributes.common.useWSL = { - default = true, - description = "Use Windows Subsystem for Linux.", - type = "boolean", - } - attributes.launch.luaexe.default = "${workspaceFolder}/lua.exe" - attributes.launch.cpath.default = "${workspaceFolder}/?.dll" -else - attributes.launch.luaexe.default = "${workspaceFolder}/lua" - attributes.launch.cpath.default = "${workspaceFolder}/?.so" -end - -local function SupportedArchs() - if OS == "win32" then - return "x86_64", "x86" - elseif OS == "darwin" then - if ARCH == "arm64" then - return "arm64", "x86_64" - else - return "x86_64" - end - elseif OS == "linux" then - if ARCH == "arm64" then - return "arm64" - else - return "x86_64" - end - end -end - -local Archs = {SupportedArchs()} -attributes.launch.luaArch.default = Archs[1] -attributes.launch.luaArch.enum = Archs - -for k, v in pairs(attributes.common) do - attributes.attach[k] = v - attributes.launch[k] = v -end -json.contributes.debuggers[1].configurationAttributes = { - launch = {properties=attributes.launch}, - attach = {properties=attributes.attach}, -} - -local configuration = json.contributes.configuration.properties -for _, name in ipairs {"luaArch", "luaVersion", "sourceCoding", "path", "cpath","console"} do - local attr = attributes.launch[name] or attributes.attach[name] - if attr then - local cfg = {} - for k, v in pairs(attr) do - if k == 'markdownDescription' then - k = 'description' - end - if k == 'enummarkdownDescriptions' then - k = 'enumDescriptions' - end - cfg[k] = v - end - configuration["lua.debug.settings."..name] = cfg - end -end - -return json +local platform = ... +platform = platform or "unknown-unknown" + +local OS, ARCH = platform:match "^([^-]+)-([^-]+)$" + +local json = { + name = "lua-debug", + version = "1.61.0", + publisher = "actboy168", + displayName = "Lua Debug", + description = "VSCode debugger extension for Lua", + icon = "images/logo.png", + private = true, + author = { + name = "actboy168", + }, + bugs = { + url = "https://github.com/actboy168/lua-debug/issues", + }, + repository = { + type = "git", + url = "https://github.com/actboy168/lua-debug", + }, + keywords = { + "lua", + "debug", + "debuggers", + }, + categories = { + "Debuggers", + }, + engines = { + vscode = "^1.61.0", + }, + extensionKind = { + "workspace", + }, + main = "./js/extension.js", + activationEvents = { + "onCommand:extension.lua-debug.runEditorContents", + "onCommand:extension.lua-debug.debugEditorContents", + "onDebugInitialConfigurations", + "onDebugDynamicConfigurations", + "onDebugResolve:lua", + }, + capabilities = { + untrustedWorkspaces = { + description = "Debugging is disabled in Restricted Mode.", + supported = false, + }, + }, + contributes = { + breakpoints = { + { + language = "lua", + }, + { + language = "html", + }, + }, + commands = { + { + command = "extension.lua-debug.runEditorContents", + icon = "$(play)", + title = "Run File", + }, + { + command = "extension.lua-debug.debugEditorContents", + icon = "$(debug-alt-small)", + title = "Debug File", + }, + { + command = "extension.lua-debug.showIntegerAsDec", + title = "Show as Dec", + }, + { + command = "extension.lua-debug.showIntegerAsHex", + title = "Show as Hex", + }, + }, + configuration = { + properties = { + ["lua.debug.variables.showIntegerAsHex"] = { + default = false, + description = "Show integer as hex.", + type = "boolean", + }, + }, + }, + debuggers = { + { + type = "lua", + languages = { + "lua", + }, + label = "Lua Debug", + configurationSnippets = { + { + label = "Lua Debug: Launch Script", + description = "A new configuration for launching a lua debug program", + body = { + type = "lua", + request = "launch", + name = "${1:launch}", + stopOnEntry = true, + program = "^\"\\${workspaceFolder}/${2:main.lua}\"", + arg = { + }, + }, + }, + { + label = "Lua Debug: Attach", + description = "A new configuration for attaching a lua debug program", + body = { + type = "lua", + request = "attach", + name = "${1:attach}", + stopOnEntry = true, + address = "127.0.0.1:4278", + } + } + } + } + }, + menus = { + ["debug/variables/context"] = { + { + command = "extension.lua-debug.showIntegerAsDec", + group = "1_view", + when = "debugConfigurationType == 'lua' && debugProtocolVariableMenuContext == 'integer/hex'", + }, + { + command = "extension.lua-debug.showIntegerAsHex", + group = "1_view", + when = "debugConfigurationType == 'lua' && debugProtocolVariableMenuContext == 'integer/dec'", + }, + }, + ["editor/title/run"] = { + { + command = "extension.lua-debug.runEditorContents", + when = "resourceLangId == lua", + }, + { + command = "extension.lua-debug.debugEditorContents", + when = "resourceLangId == lua", + }, + }, + }, + }, +} + +local attributes = {} + +attributes.common = { + luaVersion = { + default = "5.4", + enum = { + "5.1", + "5.2", + "5.3", + "5.4", + "latest", + "jit", + }, + markdownDescription = "%lua.debug.launch.luaVersion.description%", + type = "string", + }, + outputCapture = { + default = { + }, + items = { + enum = { + "print", + "io.write", + "stdout", + "stderr", + }, + }, + markdownDescription = "From where to capture output messages: print or stdout/stderr streams.", + type = "array", + }, + pathFormat = { + default = "path", + enum = { + "path", + "linuxpath", + }, + markdownDescription = "Path format", + type = "string", + }, + sourceFormat = { + default = "path", + enum = { + "path", + "string", + "linuxpath", + }, + markdownDescription = "Source format", + type = "string", + }, + sourceMaps = { + default = { + { + "./*", + "${workspaceFolder}/*", + }, + }, + markdownDescription = "The source path of the remote host and the source path of local.", + type = "array", + }, + skipFiles = { + default = { + }, + items = { + type = "string", + }, + markdownDescription = "An array of glob patterns for files to skip when debugging.", + type = "array", + }, + stopOnEntry = { + default = false, + markdownDescription = "Automatically stop after entry.", + type = "boolean", + }, + stopOnThreadEntry = { + default = true, + markdownDescription = "Automatically stop after thread entry.", + type = "boolean", + }, + address = { + default = "127.0.0.1:4278", + markdownDescription = [[ +Debugger address. +1. IPv4 e.g. `127.0.0.1:4278` +2. IPv6 e.g. `[::1]:4278` +3. Unix domain socket e.g. `@c:\\unix.sock`]], + type = { + "string", + "null", + }, + }, + client = { + default = true, + markdownDescription = "Choose whether to `connect` or `listen`.", + type = "boolean", + }, + inject = { + default = "none", + markdownDescription = "How to inject debugger.", + enum = { + "none", + }, + type = "string", + }, + signature = { + default = "null", + type = "object", + markdownDescription = "signature info", + properties = { + module = { + default = "null", + type = "string", + markdownDescription = "lua module path or name", + }, + }, + } +} + +if OS == "win32" then + attributes.common.inject.default = "hook" + table.insert(attributes.common.inject.enum, "hook") +elseif OS == "darwin" then + attributes.common.inject.default = "lldb" + table.insert(attributes.common.inject.enum, "hook") + table.insert(attributes.common.inject.enum, "lldb") + attributes.common.inject_executable = { + markdownDescription = "inject executable path", + type = { + "string", + "null", + }, + } +end + +attributes.attach = { +} + +if OS == "win32" or OS == "darwin" then + attributes.attach.processId = { + default = "${command:pickProcess}", + markdownDescription = "Id of process to attach to.", + type = "string", + } + attributes.attach.processName = { + default = "lua.exe", + markdownDescription = "Name of process to attach to.", + type = "string", + } + json.activationEvents[#json.activationEvents + 1] = "onCommand:extension.lua-debug.pickProcess" + json.contributes.debuggers[1].variables = { + pickProcess = "extension.lua-debug.pickProcess", + } +end + +attributes.launch = { + luaexe = { + markdownDescription = "Absolute path to the lua exe.", + type = "string", + }, + program = { + default = "${workspaceFolder}/main.lua", + markdownDescription = "Lua program to debug - set this to the path of the script", + type = "string", + }, + arg = { + default = { + }, + markdownDescription = "Command line argument, arg[1] ... arg[n]", + type = "array", + }, + arg0 = { + default = { + }, + markdownDescription = "Command line argument, arg[-n] ... arg[0]", + type = { + "string", + "array", + }, + }, + path = { + default = "${workspaceFolder}/?.lua", + markdownDescription = "%lua.debug.launch.path.description%", + type = { + "string", + "array", + "null", + }, + }, + cpath = { + markdownDescription = "%lua.debug.launch.cpath.description%", + type = { + "string", + "array", + "null", + }, + }, + luaArch = { + markdownDescription = "%lua.debug.launch.luaArch.description%", + type = "string", + }, + cwd = { + default = "${workspaceFolder}", + markdownDescription = "Working directory at program startup", + type = { + "string", + "null", + }, + }, + env = { + additionalProperties = { + type = { + "string", + "null", + }, + }, + default = { + PATH = "${workspaceFolder}", + }, + markdownDescription = + "Environment variables passed to the program. The value `null` removes thevariable from the environment.", + type = "object", + }, + console = { + default = "integratedTerminal", + enum = { + "internalConsole", + "integratedTerminal", + "externalTerminal", + }, + enummarkdownDescriptions = { + "%lua.debug.launch.console.internalConsole.description%", + "%lua.debug.launch.console.integratedTerminal.description%", + "%lua.debug.launch.console.externalTerminal.description%", + }, + markdownDescription = "%lua.debug.launch.console.description%", + type = "string", + }, + runtimeExecutable = { + default = OS == "win32" and "${workspaceFolder}/lua.exe" or "${workspaceFolder}/lua", + markdownDescription = "Runtime to use. Either an absolute path or the name of a runtime availableon the PATH.", + type = { + "string", + "null", + }, + }, + runtimeArgs = { + default = "${workspaceFolder}/main.lua", + markdownDescription = "Arguments passed to the runtime executable.", + type = { + "string", + "array", + "null", + }, + }, +} + +if OS == "win32" or OS == "darwin" then + local snippets = json.contributes.debuggers[1].configurationSnippets + snippets[#snippets + 1] = { + label = "Lua Debug: Launch Process", + description = "A new configuration for launching a lua process", + body = { + type = "lua", + request = "launch", + name = "${1:launch process}", + stopOnEntry = true, + runtimeExecutable = "^\"\\${workspaceFolder}/lua.exe\"", + runtimeArgs = "^\"\\${workspaceFolder}/${2:main.lua}\"", + } + } + snippets[#snippets + 1] = { + label = "Lua Debug: Attach Process", + description = "A new configuration for attaching a lua debug program", + body = { + type = "lua", + request = "attach", + name = "${1:attach}", + stopOnEntry = true, + processId = "^\"\\${command:pickProcess}\"", + } + } +end + +if OS == "win32" then + attributes.common.sourceCoding = { + default = "utf8", + enum = { + "utf8", + "ansi", + }, + markdownDescription = "%lua.debug.launch.sourceCoding.description%", + type = "string", + } + attributes.common.useWSL = { + default = true, + description = "Use Windows Subsystem for Linux.", + type = "boolean", + } + attributes.launch.luaexe.default = "${workspaceFolder}/lua.exe" + attributes.launch.cpath.default = "${workspaceFolder}/?.dll" +else + attributes.launch.luaexe.default = "${workspaceFolder}/lua" + attributes.launch.cpath.default = "${workspaceFolder}/?.so" +end + +local function SupportedArchs() + if OS == "win32" then + return "x86_64", "x86" + elseif OS == "darwin" then + if ARCH == "arm64" then + return "arm64", "x86_64" + else + return "x86_64" + end + elseif OS == "linux" then + if ARCH == "arm64" then + return "arm64" + else + return "x86_64" + end + end +end + +local Archs = { SupportedArchs() } +attributes.launch.luaArch.default = Archs[1] +attributes.launch.luaArch.enum = Archs + +for k, v in pairs(attributes.common) do + attributes.attach[k] = v + attributes.launch[k] = v +end +json.contributes.debuggers[1].configurationAttributes = { + launch = { properties = attributes.launch }, + attach = { properties = attributes.attach }, +} + +local configuration = json.contributes.configuration.properties +for _, name in ipairs { "luaArch", "luaVersion", "sourceCoding", "path", "cpath", "console" } do + local attr = attributes.launch[name] or attributes.attach[name] + if attr then + local cfg = {} + for k, v in pairs(attr) do + if k == 'markdownDescription' then + k = 'description' + end + if k == 'enummarkdownDescriptions' then + k = 'enumDescriptions' + end + cfg[k] = v + end + configuration["lua.debug.settings."..name] = cfg + end +end + +return json diff --git a/compile/macos/runtime.lua b/compile/macos/runtime.lua index a4f5bb81a..3b4d642e7 100644 --- a/compile/macos/runtime.lua +++ b/compile/macos/runtime.lua @@ -22,6 +22,7 @@ lm:lua_library 'launcher' { "3rd/bee.lua", "3rd/frida_gum/gumpp", "3rd/lua/lua54", + "3rd/json/single_include", "src/launcher", }, sources = { diff --git a/compile/windows/runtime.lua b/compile/windows/runtime.lua index fdfec993f..b089aba15 100644 --- a/compile/windows/runtime.lua +++ b/compile/windows/runtime.lua @@ -38,6 +38,7 @@ lm:lua_library ('launcher.'..ArchAlias[platform]) { includes = { "3rd/bee.lua", "3rd/frida_gum/gumpp", + "3rd/json/single_include", "src/launcher", }, sources = { diff --git a/extension/js/configurationProvider.js b/extension/js/configurationProvider.js index d5a6b5e87..76601bc44 100644 --- a/extension/js/configurationProvider.js +++ b/extension/js/configurationProvider.js @@ -243,6 +243,9 @@ function resolveConfig(folder, config) { throw new Error('Missing `address` to debug'); } } + if (config.signature != null) { + config.signature.version = config.version + } config.configuration = { variables: vscode.workspace.getConfiguration("lua.debug.variables") } diff --git a/extension/script/frontend/proxy.lua b/extension/script/frontend/proxy.lua index f31cfb89b..df4f48fce 100644 --- a/extension/script/frontend/proxy.lua +++ b/extension/script/frontend/proxy.lua @@ -4,6 +4,7 @@ local fs = require 'bee.filesystem' local sp = require 'bee.subprocess' local platform_os = require 'frontend.platform_os' local process_inject = require 'frontend.process_inject' +local json = require 'common.json' local server local client local initReq @@ -23,6 +24,14 @@ local function ipc_send_latest(pid) fd:close() end +local function ipc_send_config(pid, config) + fs.create_directories(WORKDIR / "tmp") + local ipc = require "common.ipc" + local fd = assert(ipc(WORKDIR, pid, "config", "w")) + fd:write(json.encode(config)) + fd:close() +end + local function response_initialize(req) client.sendmsg { type = 'response', @@ -59,10 +68,11 @@ local function attach_process(pkg, pid) if args.luaVersion == "latest" then ipc_send_latest(pid) end + ipc_send_config(pid, args.signature) local ok, errmsg = process_inject.inject(pid, "attach", args) if not ok then - return false, errmsg - end + return false, errmsg + end server = network(getUnixAddress(pid), true) server.sendmsg(initReq) @@ -80,8 +90,8 @@ local function proxy_attach(pkg) local args = pkg.arguments platform_os.init(args) if platform_os() ~= "Windows" and platform_os() ~= "macOS" then - attach_tcp(pkg, args) - return + attach_tcp(pkg, args) + return end if args.processId then local processId = tonumber(args.processId) @@ -92,7 +102,7 @@ local function proxy_attach(pkg) return end if args.processName then - local pids = require "frontend.query_process"(args.processName) + local pids = require "frontend.query_process" (args.processName) if #pids == 0 then response_error(pkg, ('Cannot found process `%s`.'):format(args.processName)) return @@ -113,7 +123,7 @@ local function create_server(args, pid) local s, address if args.address ~= nil then s = network(args.address, args.client) - address = (args.client and "s:" or "c:") .. args.address + address = (args.client and "s:" or "c:")..args.address else pid = pid or sp.get_id() s = network(getUnixAddress(pid), true) @@ -158,11 +168,15 @@ local function proxy_launch_console(pkg) response_error(pkg, "`runtimeExecutable` need specify `inject` or `address`.") return end - local process, err = debuger_factory.create_process_in_console(args, function (process) + local process, err = debuger_factory.create_process_in_console(args, function(process) local address server, address = create_server(args, process:get_id()) - if args.luaVersion == "latest" and type(address) == "number" then - ipc_send_latest(address) + + if type(address) == "number" then + if args.luaVersion == "latest" then + ipc_send_latest(address) + end + ipc_send_config(address, args.signature) end end) if not process then diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index 33ba59e7d..2449d9b87 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -1,13 +1,11 @@ #include #include #include -#include -#include +#include #include #include #include -#include #include #include @@ -16,6 +14,8 @@ namespace luadebug::autoattach { constexpr auto find_lua_module_key = "lua_newstate"; static bool is_lua_module(const char* module_path) { + if (std::string_view(module_path).find(config.get_lua_module()) != std::string_view::npos) + return true; if (Gum::Process::module_find_export_by_name(module_path, find_lua_module_key)) return true; return Gum::Process::module_find_symbol_by_name(module_path, find_lua_module_key) != nullptr; @@ -36,6 +36,10 @@ namespace luadebug::autoattach { } void start() { + if (!Config::init_from_file()) { + log::info("can't load config"); + return; + } bool found = false; lua_module rm = {}; Gum::Process::enumerate_modules([&rm, &found](const Gum::ModuleDetails& details) -> bool { diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index e5a800cb4..bde7ed96b 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include @@ -7,20 +9,6 @@ #include namespace luadebug::autoattach { - static lua_version lua_version_from_string [[maybe_unused]] (const std::string_view& v) { - if (v == "luajit") - return lua_version::luajit; - if (v == "lua51") - return lua_version::lua51; - if (v == "lua52") - return lua_version::lua52; - if (v == "lua53") - return lua_version::lua53; - if (v == "lua54") - return lua_version::lua54; - return lua_version::unknown; - } - static const char* lua_version_to_string(lua_version v) { switch (v) { case lua_version::lua51: @@ -39,10 +27,13 @@ namespace luadebug::autoattach { } static bool in_module(const lua_module& m, void* addr) { - return addr > m.memory_address && addr <= (void*)((intptr_t)m.memory_address + m.memory_size); + return addr >= m.memory_address && addr <= (void*)((intptr_t)m.memory_address + m.memory_size); } static lua_version get_lua_version(const lua_module& m) { + auto version = config.get_lua_version(); + if (version != lua_version::unknown) + return version; /* luaJIT_version_2_1_0_beta3 luaJIT_version_2_1_0_beta2 diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp new file mode 100644 index 000000000..662d7d410 --- /dev/null +++ b/src/launcher/config/config.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std::string_view_literals; +namespace luadebug::autoattach { + static lua_version lua_version_from_string [[maybe_unused]] (const std::string_view& v) { + if (v == "jit" || v == "luajit") + return lua_version::luajit; + else if (v == "5.1" || v == "lua51") + return lua_version::lua51; + else if (v == "5.2" || v == "lua52") + return lua_version::lua52; + else if (v == "5.3" || v == "lua53") + return lua_version::lua53; + else if (v == "5.4" || v == "lua54") + return lua_version::lua54; + return lua_version::unknown; + } + + Config config; + + lua_version Config::get_lua_version() const { + const auto key = "version"sv; + + auto it = values->find(key); + if (!values || it == values->end()) { + return lua_version::unknown; + } + + return lua_version_from_string(it->get()); + } + + std::string Config::get_lua_module() const { + const auto key = "module"sv; + + auto it = values->find(key); + if (it == values->end()) { + return {}; + } + const auto& value = it->get(); + if (!fs::exists(value)) + return {}; + + return value; + } + + bool Config::init_from_file() { + config.values = std::make_unique(); + auto dllpath = bee::path_helper::dll_path(); + if (!dllpath) { + return false; + } + + auto filename = std::format("{}/tmp/pid_{}_config", dllpath.value().parent_path().parent_path().generic_string(), Gum::Process::get_id()); + + std::ifstream s(filename, s.in); + if (!s.is_open()) + return false; + try { + s >> *config.values; + } catch (const nlohmann::json::exception& e) { + log::info("init_from_file error: {}", e.what()); + } + + return true; + } + +} // namespace luadebug::autoattach \ No newline at end of file diff --git a/src/launcher/config/config.h b/src/launcher/config/config.h new file mode 100644 index 000000000..049df88fd --- /dev/null +++ b/src/launcher/config/config.h @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include + +namespace luadebug::autoattach { + + enum class lua_version; + class Config { + std::unique_ptr values; + + public: + lua_version get_lua_version() const; + + std::string get_lua_module() const; + + static bool init_from_file(); + }; + + extern Config config; +} // namespace luadebug::autoattach \ No newline at end of file From 65e15599d0e49ef8f9d13c06c830ee7a010edcf3 Mon Sep 17 00:00:00 2001 From: fesily Date: Fri, 17 Mar 2023 21:39:39 +0800 Subject: [PATCH 02/51] fix config lua version --- extension/js/configurationProvider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/js/configurationProvider.js b/extension/js/configurationProvider.js index 76601bc44..b6e6cea23 100644 --- a/extension/js/configurationProvider.js +++ b/extension/js/configurationProvider.js @@ -244,7 +244,7 @@ function resolveConfig(folder, config) { } } if (config.signature != null) { - config.signature.version = config.version + config.signature.version = config.luaVersion } config.configuration = { variables: vscode.workspace.getConfiguration("lua.debug.variables") From fcd627a094ce40d77d05d687dd57fd2048c50de4 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 12:37:38 +0800 Subject: [PATCH 03/51] reset package_json --- compile/common/package_json.lua | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/compile/common/package_json.lua b/compile/common/package_json.lua index 275b5af4b..8670afe9f 100644 --- a/compile/common/package_json.lua +++ b/compile/common/package_json.lua @@ -296,7 +296,7 @@ if OS == "win32" or OS == "darwin" then markdownDescription = "Name of process to attach to.", type = "string", } - json.activationEvents[#json.activationEvents + 1] = "onCommand:extension.lua-debug.pickProcess" + json.activationEvents[#json.activationEvents+1] = "onCommand:extension.lua-debug.pickProcess" json.contributes.debuggers[1].variables = { pickProcess = "extension.lua-debug.pickProcess", } @@ -366,8 +366,7 @@ attributes.launch = { default = { PATH = "${workspaceFolder}", }, - markdownDescription = - "Environment variables passed to the program. The value `null` removes thevariable from the environment.", + markdownDescription = "Environment variables passed to the program. The value `null` removes thevariable from the environment.", type = "object", }, console = { @@ -406,7 +405,7 @@ attributes.launch = { if OS == "win32" or OS == "darwin" then local snippets = json.contributes.debuggers[1].configurationSnippets - snippets[#snippets + 1] = { + snippets[#snippets+1] = { label = "Lua Debug: Launch Process", description = "A new configuration for launching a lua process", body = { @@ -418,7 +417,7 @@ if OS == "win32" or OS == "darwin" then runtimeArgs = "^\"\\${workspaceFolder}/${2:main.lua}\"", } } - snippets[#snippets + 1] = { + snippets[#snippets+1] = { label = "Lua Debug: Attach Process", description = "A new configuration for attaching a lua debug program", body = { @@ -471,7 +470,7 @@ local function SupportedArchs() end end -local Archs = { SupportedArchs() } +local Archs = {SupportedArchs()} attributes.launch.luaArch.default = Archs[1] attributes.launch.luaArch.enum = Archs @@ -480,12 +479,12 @@ for k, v in pairs(attributes.common) do attributes.launch[k] = v end json.contributes.debuggers[1].configurationAttributes = { - launch = { properties = attributes.launch }, - attach = { properties = attributes.attach }, + launch = {properties=attributes.launch}, + attach = {properties=attributes.attach}, } local configuration = json.contributes.configuration.properties -for _, name in ipairs { "luaArch", "luaVersion", "sourceCoding", "path", "cpath", "console" } do +for _, name in ipairs {"luaArch", "luaVersion", "sourceCoding", "path", "cpath","console"} do local attr = attributes.launch[name] or attributes.attach[name] if attr then local cfg = {} @@ -502,4 +501,4 @@ for _, name in ipairs { "luaArch", "luaVersion", "sourceCoding", "path", "cpath" end end -return json +return json \ No newline at end of file From cea9c3c09b91ce52efce0720ed643d3e540fca28 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 14:12:15 +0800 Subject: [PATCH 04/51] reset packgae_json --- compile/common/package_json.lua | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/compile/common/package_json.lua b/compile/common/package_json.lua index 8670afe9f..1b056f7ef 100644 --- a/compile/common/package_json.lua +++ b/compile/common/package_json.lua @@ -252,18 +252,6 @@ Debugger address. }, type = "string", }, - signature = { - default = "null", - type = "object", - markdownDescription = "signature info", - properties = { - module = { - default = "null", - type = "string", - markdownDescription = "lua module path or name", - }, - }, - } } if OS == "win32" then From c085d01c87f16045650f86a376cde7135de300f8 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 14:15:27 +0800 Subject: [PATCH 05/51] reset --- compile/common/package_json.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compile/common/package_json.lua b/compile/common/package_json.lua index 1b056f7ef..e37139bba 100644 --- a/compile/common/package_json.lua +++ b/compile/common/package_json.lua @@ -489,4 +489,4 @@ for _, name in ipairs {"luaArch", "luaVersion", "sourceCoding", "path", "cpath", end end -return json \ No newline at end of file +return json From 710d3fcd44168a65045c9379ecba936cf14574c0 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 14:21:22 +0800 Subject: [PATCH 06/51] reset --- compile/common/package_json.lua | 984 ++++++++++++++++---------------- 1 file changed, 492 insertions(+), 492 deletions(-) diff --git a/compile/common/package_json.lua b/compile/common/package_json.lua index e37139bba..bf9d79e7b 100644 --- a/compile/common/package_json.lua +++ b/compile/common/package_json.lua @@ -1,492 +1,492 @@ -local platform = ... -platform = platform or "unknown-unknown" - -local OS, ARCH = platform:match "^([^-]+)-([^-]+)$" - -local json = { - name = "lua-debug", - version = "1.61.0", - publisher = "actboy168", - displayName = "Lua Debug", - description = "VSCode debugger extension for Lua", - icon = "images/logo.png", - private = true, - author = { - name = "actboy168", - }, - bugs = { - url = "https://github.com/actboy168/lua-debug/issues", - }, - repository = { - type = "git", - url = "https://github.com/actboy168/lua-debug", - }, - keywords = { - "lua", - "debug", - "debuggers", - }, - categories = { - "Debuggers", - }, - engines = { - vscode = "^1.61.0", - }, - extensionKind = { - "workspace", - }, - main = "./js/extension.js", - activationEvents = { - "onCommand:extension.lua-debug.runEditorContents", - "onCommand:extension.lua-debug.debugEditorContents", - "onDebugInitialConfigurations", - "onDebugDynamicConfigurations", - "onDebugResolve:lua", - }, - capabilities = { - untrustedWorkspaces = { - description = "Debugging is disabled in Restricted Mode.", - supported = false, - }, - }, - contributes = { - breakpoints = { - { - language = "lua", - }, - { - language = "html", - }, - }, - commands = { - { - command = "extension.lua-debug.runEditorContents", - icon = "$(play)", - title = "Run File", - }, - { - command = "extension.lua-debug.debugEditorContents", - icon = "$(debug-alt-small)", - title = "Debug File", - }, - { - command = "extension.lua-debug.showIntegerAsDec", - title = "Show as Dec", - }, - { - command = "extension.lua-debug.showIntegerAsHex", - title = "Show as Hex", - }, - }, - configuration = { - properties = { - ["lua.debug.variables.showIntegerAsHex"] = { - default = false, - description = "Show integer as hex.", - type = "boolean", - }, - }, - }, - debuggers = { - { - type = "lua", - languages = { - "lua", - }, - label = "Lua Debug", - configurationSnippets = { - { - label = "Lua Debug: Launch Script", - description = "A new configuration for launching a lua debug program", - body = { - type = "lua", - request = "launch", - name = "${1:launch}", - stopOnEntry = true, - program = "^\"\\${workspaceFolder}/${2:main.lua}\"", - arg = { - }, - }, - }, - { - label = "Lua Debug: Attach", - description = "A new configuration for attaching a lua debug program", - body = { - type = "lua", - request = "attach", - name = "${1:attach}", - stopOnEntry = true, - address = "127.0.0.1:4278", - } - } - } - } - }, - menus = { - ["debug/variables/context"] = { - { - command = "extension.lua-debug.showIntegerAsDec", - group = "1_view", - when = "debugConfigurationType == 'lua' && debugProtocolVariableMenuContext == 'integer/hex'", - }, - { - command = "extension.lua-debug.showIntegerAsHex", - group = "1_view", - when = "debugConfigurationType == 'lua' && debugProtocolVariableMenuContext == 'integer/dec'", - }, - }, - ["editor/title/run"] = { - { - command = "extension.lua-debug.runEditorContents", - when = "resourceLangId == lua", - }, - { - command = "extension.lua-debug.debugEditorContents", - when = "resourceLangId == lua", - }, - }, - }, - }, -} - -local attributes = {} - -attributes.common = { - luaVersion = { - default = "5.4", - enum = { - "5.1", - "5.2", - "5.3", - "5.4", - "latest", - "jit", - }, - markdownDescription = "%lua.debug.launch.luaVersion.description%", - type = "string", - }, - outputCapture = { - default = { - }, - items = { - enum = { - "print", - "io.write", - "stdout", - "stderr", - }, - }, - markdownDescription = "From where to capture output messages: print or stdout/stderr streams.", - type = "array", - }, - pathFormat = { - default = "path", - enum = { - "path", - "linuxpath", - }, - markdownDescription = "Path format", - type = "string", - }, - sourceFormat = { - default = "path", - enum = { - "path", - "string", - "linuxpath", - }, - markdownDescription = "Source format", - type = "string", - }, - sourceMaps = { - default = { - { - "./*", - "${workspaceFolder}/*", - }, - }, - markdownDescription = "The source path of the remote host and the source path of local.", - type = "array", - }, - skipFiles = { - default = { - }, - items = { - type = "string", - }, - markdownDescription = "An array of glob patterns for files to skip when debugging.", - type = "array", - }, - stopOnEntry = { - default = false, - markdownDescription = "Automatically stop after entry.", - type = "boolean", - }, - stopOnThreadEntry = { - default = true, - markdownDescription = "Automatically stop after thread entry.", - type = "boolean", - }, - address = { - default = "127.0.0.1:4278", - markdownDescription = [[ -Debugger address. -1. IPv4 e.g. `127.0.0.1:4278` -2. IPv6 e.g. `[::1]:4278` -3. Unix domain socket e.g. `@c:\\unix.sock`]], - type = { - "string", - "null", - }, - }, - client = { - default = true, - markdownDescription = "Choose whether to `connect` or `listen`.", - type = "boolean", - }, - inject = { - default = "none", - markdownDescription = "How to inject debugger.", - enum = { - "none", - }, - type = "string", - }, -} - -if OS == "win32" then - attributes.common.inject.default = "hook" - table.insert(attributes.common.inject.enum, "hook") -elseif OS == "darwin" then - attributes.common.inject.default = "lldb" - table.insert(attributes.common.inject.enum, "hook") - table.insert(attributes.common.inject.enum, "lldb") - attributes.common.inject_executable = { - markdownDescription = "inject executable path", - type = { - "string", - "null", - }, - } -end - -attributes.attach = { -} - -if OS == "win32" or OS == "darwin" then - attributes.attach.processId = { - default = "${command:pickProcess}", - markdownDescription = "Id of process to attach to.", - type = "string", - } - attributes.attach.processName = { - default = "lua.exe", - markdownDescription = "Name of process to attach to.", - type = "string", - } - json.activationEvents[#json.activationEvents+1] = "onCommand:extension.lua-debug.pickProcess" - json.contributes.debuggers[1].variables = { - pickProcess = "extension.lua-debug.pickProcess", - } -end - -attributes.launch = { - luaexe = { - markdownDescription = "Absolute path to the lua exe.", - type = "string", - }, - program = { - default = "${workspaceFolder}/main.lua", - markdownDescription = "Lua program to debug - set this to the path of the script", - type = "string", - }, - arg = { - default = { - }, - markdownDescription = "Command line argument, arg[1] ... arg[n]", - type = "array", - }, - arg0 = { - default = { - }, - markdownDescription = "Command line argument, arg[-n] ... arg[0]", - type = { - "string", - "array", - }, - }, - path = { - default = "${workspaceFolder}/?.lua", - markdownDescription = "%lua.debug.launch.path.description%", - type = { - "string", - "array", - "null", - }, - }, - cpath = { - markdownDescription = "%lua.debug.launch.cpath.description%", - type = { - "string", - "array", - "null", - }, - }, - luaArch = { - markdownDescription = "%lua.debug.launch.luaArch.description%", - type = "string", - }, - cwd = { - default = "${workspaceFolder}", - markdownDescription = "Working directory at program startup", - type = { - "string", - "null", - }, - }, - env = { - additionalProperties = { - type = { - "string", - "null", - }, - }, - default = { - PATH = "${workspaceFolder}", - }, - markdownDescription = "Environment variables passed to the program. The value `null` removes thevariable from the environment.", - type = "object", - }, - console = { - default = "integratedTerminal", - enum = { - "internalConsole", - "integratedTerminal", - "externalTerminal", - }, - enummarkdownDescriptions = { - "%lua.debug.launch.console.internalConsole.description%", - "%lua.debug.launch.console.integratedTerminal.description%", - "%lua.debug.launch.console.externalTerminal.description%", - }, - markdownDescription = "%lua.debug.launch.console.description%", - type = "string", - }, - runtimeExecutable = { - default = OS == "win32" and "${workspaceFolder}/lua.exe" or "${workspaceFolder}/lua", - markdownDescription = "Runtime to use. Either an absolute path or the name of a runtime availableon the PATH.", - type = { - "string", - "null", - }, - }, - runtimeArgs = { - default = "${workspaceFolder}/main.lua", - markdownDescription = "Arguments passed to the runtime executable.", - type = { - "string", - "array", - "null", - }, - }, -} - -if OS == "win32" or OS == "darwin" then - local snippets = json.contributes.debuggers[1].configurationSnippets - snippets[#snippets+1] = { - label = "Lua Debug: Launch Process", - description = "A new configuration for launching a lua process", - body = { - type = "lua", - request = "launch", - name = "${1:launch process}", - stopOnEntry = true, - runtimeExecutable = "^\"\\${workspaceFolder}/lua.exe\"", - runtimeArgs = "^\"\\${workspaceFolder}/${2:main.lua}\"", - } - } - snippets[#snippets+1] = { - label = "Lua Debug: Attach Process", - description = "A new configuration for attaching a lua debug program", - body = { - type = "lua", - request = "attach", - name = "${1:attach}", - stopOnEntry = true, - processId = "^\"\\${command:pickProcess}\"", - } - } -end - -if OS == "win32" then - attributes.common.sourceCoding = { - default = "utf8", - enum = { - "utf8", - "ansi", - }, - markdownDescription = "%lua.debug.launch.sourceCoding.description%", - type = "string", - } - attributes.common.useWSL = { - default = true, - description = "Use Windows Subsystem for Linux.", - type = "boolean", - } - attributes.launch.luaexe.default = "${workspaceFolder}/lua.exe" - attributes.launch.cpath.default = "${workspaceFolder}/?.dll" -else - attributes.launch.luaexe.default = "${workspaceFolder}/lua" - attributes.launch.cpath.default = "${workspaceFolder}/?.so" -end - -local function SupportedArchs() - if OS == "win32" then - return "x86_64", "x86" - elseif OS == "darwin" then - if ARCH == "arm64" then - return "arm64", "x86_64" - else - return "x86_64" - end - elseif OS == "linux" then - if ARCH == "arm64" then - return "arm64" - else - return "x86_64" - end - end -end - -local Archs = {SupportedArchs()} -attributes.launch.luaArch.default = Archs[1] -attributes.launch.luaArch.enum = Archs - -for k, v in pairs(attributes.common) do - attributes.attach[k] = v - attributes.launch[k] = v -end -json.contributes.debuggers[1].configurationAttributes = { - launch = {properties=attributes.launch}, - attach = {properties=attributes.attach}, -} - -local configuration = json.contributes.configuration.properties -for _, name in ipairs {"luaArch", "luaVersion", "sourceCoding", "path", "cpath","console"} do - local attr = attributes.launch[name] or attributes.attach[name] - if attr then - local cfg = {} - for k, v in pairs(attr) do - if k == 'markdownDescription' then - k = 'description' - end - if k == 'enummarkdownDescriptions' then - k = 'enumDescriptions' - end - cfg[k] = v - end - configuration["lua.debug.settings."..name] = cfg - end -end - -return json +local platform = ... +platform = platform or "unknown-unknown" + +local OS, ARCH = platform:match "^([^-]+)-([^-]+)$" + +local json = { + name = "lua-debug", + version = "1.61.0", + publisher = "actboy168", + displayName = "Lua Debug", + description = "VSCode debugger extension for Lua", + icon = "images/logo.png", + private = true, + author = { + name = "actboy168", + }, + bugs = { + url = "https://github.com/actboy168/lua-debug/issues", + }, + repository = { + type = "git", + url = "https://github.com/actboy168/lua-debug", + }, + keywords = { + "lua", + "debug", + "debuggers", + }, + categories = { + "Debuggers", + }, + engines = { + vscode = "^1.61.0", + }, + extensionKind = { + "workspace", + }, + main = "./js/extension.js", + activationEvents = { + "onCommand:extension.lua-debug.runEditorContents", + "onCommand:extension.lua-debug.debugEditorContents", + "onDebugInitialConfigurations", + "onDebugDynamicConfigurations", + "onDebugResolve:lua", + }, + capabilities = { + untrustedWorkspaces = { + description = "Debugging is disabled in Restricted Mode.", + supported = false, + }, + }, + contributes = { + breakpoints = { + { + language = "lua", + }, + { + language = "html", + }, + }, + commands = { + { + command = "extension.lua-debug.runEditorContents", + icon = "$(play)", + title = "Run File", + }, + { + command = "extension.lua-debug.debugEditorContents", + icon = "$(debug-alt-small)", + title = "Debug File", + }, + { + command = "extension.lua-debug.showIntegerAsDec", + title = "Show as Dec", + }, + { + command = "extension.lua-debug.showIntegerAsHex", + title = "Show as Hex", + }, + }, + configuration = { + properties = { + ["lua.debug.variables.showIntegerAsHex"] = { + default = false, + description = "Show integer as hex.", + type = "boolean", + }, + }, + }, + debuggers = { + { + type = "lua", + languages = { + "lua", + }, + label = "Lua Debug", + configurationSnippets = { + { + label = "Lua Debug: Launch Script", + description = "A new configuration for launching a lua debug program", + body = { + type = "lua", + request = "launch", + name = "${1:launch}", + stopOnEntry = true, + program = "^\"\\${workspaceFolder}/${2:main.lua}\"", + arg = { + }, + }, + }, + { + label = "Lua Debug: Attach", + description = "A new configuration for attaching a lua debug program", + body = { + type = "lua", + request = "attach", + name = "${1:attach}", + stopOnEntry = true, + address = "127.0.0.1:4278", + } + } + } + } + }, + menus = { + ["debug/variables/context"] = { + { + command = "extension.lua-debug.showIntegerAsDec", + group = "1_view", + when = "debugConfigurationType == 'lua' && debugProtocolVariableMenuContext == 'integer/hex'", + }, + { + command = "extension.lua-debug.showIntegerAsHex", + group = "1_view", + when = "debugConfigurationType == 'lua' && debugProtocolVariableMenuContext == 'integer/dec'", + }, + }, + ["editor/title/run"] = { + { + command = "extension.lua-debug.runEditorContents", + when = "resourceLangId == lua", + }, + { + command = "extension.lua-debug.debugEditorContents", + when = "resourceLangId == lua", + }, + }, + }, + }, +} + +local attributes = {} + +attributes.common = { + luaVersion = { + default = "5.4", + enum = { + "5.1", + "5.2", + "5.3", + "5.4", + "latest", + "jit", + }, + markdownDescription = "%lua.debug.launch.luaVersion.description%", + type = "string", + }, + outputCapture = { + default = { + }, + items = { + enum = { + "print", + "io.write", + "stdout", + "stderr", + }, + }, + markdownDescription = "From where to capture output messages: print or stdout/stderr streams.", + type = "array", + }, + pathFormat = { + default = "path", + enum = { + "path", + "linuxpath", + }, + markdownDescription = "Path format", + type = "string", + }, + sourceFormat = { + default = "path", + enum = { + "path", + "string", + "linuxpath", + }, + markdownDescription = "Source format", + type = "string", + }, + sourceMaps = { + default = { + { + "./*", + "${workspaceFolder}/*", + }, + }, + markdownDescription = "The source path of the remote host and the source path of local.", + type = "array", + }, + skipFiles = { + default = { + }, + items = { + type = "string", + }, + markdownDescription = "An array of glob patterns for files to skip when debugging.", + type = "array", + }, + stopOnEntry = { + default = false, + markdownDescription = "Automatically stop after entry.", + type = "boolean", + }, + stopOnThreadEntry = { + default = true, + markdownDescription = "Automatically stop after thread entry.", + type = "boolean", + }, + address = { + default = "127.0.0.1:4278", + markdownDescription = [[ +Debugger address. +1. IPv4 e.g. `127.0.0.1:4278` +2. IPv6 e.g. `[::1]:4278` +3. Unix domain socket e.g. `@c:\\unix.sock`]], + type = { + "string", + "null", + }, + }, + client = { + default = true, + markdownDescription = "Choose whether to `connect` or `listen`.", + type = "boolean", + }, + inject = { + default = "none", + markdownDescription = "How to inject debugger.", + enum = { + "none", + }, + type = "string", + }, +} + +if OS == "win32" then + attributes.common.inject.default = "hook" + table.insert(attributes.common.inject.enum, "hook") +elseif OS == "darwin" then + attributes.common.inject.default = "lldb" + table.insert(attributes.common.inject.enum, "hook") + table.insert(attributes.common.inject.enum, "lldb") + attributes.common.inject_executable = { + markdownDescription = "inject executable path", + type = { + "string", + "null", + }, + } +end + +attributes.attach = { +} + +if OS == "win32" or OS == "darwin" then + attributes.attach.processId = { + default = "${command:pickProcess}", + markdownDescription = "Id of process to attach to.", + type = "string", + } + attributes.attach.processName = { + default = "lua.exe", + markdownDescription = "Name of process to attach to.", + type = "string", + } + json.activationEvents[#json.activationEvents+1] = "onCommand:extension.lua-debug.pickProcess" + json.contributes.debuggers[1].variables = { + pickProcess = "extension.lua-debug.pickProcess", + } +end + +attributes.launch = { + luaexe = { + markdownDescription = "Absolute path to the lua exe.", + type = "string", + }, + program = { + default = "${workspaceFolder}/main.lua", + markdownDescription = "Lua program to debug - set this to the path of the script", + type = "string", + }, + arg = { + default = { + }, + markdownDescription = "Command line argument, arg[1] ... arg[n]", + type = "array", + }, + arg0 = { + default = { + }, + markdownDescription = "Command line argument, arg[-n] ... arg[0]", + type = { + "string", + "array", + }, + }, + path = { + default = "${workspaceFolder}/?.lua", + markdownDescription = "%lua.debug.launch.path.description%", + type = { + "string", + "array", + "null", + }, + }, + cpath = { + markdownDescription = "%lua.debug.launch.cpath.description%", + type = { + "string", + "array", + "null", + }, + }, + luaArch = { + markdownDescription = "%lua.debug.launch.luaArch.description%", + type = "string", + }, + cwd = { + default = "${workspaceFolder}", + markdownDescription = "Working directory at program startup", + type = { + "string", + "null", + }, + }, + env = { + additionalProperties = { + type = { + "string", + "null", + }, + }, + default = { + PATH = "${workspaceFolder}", + }, + markdownDescription = "Environment variables passed to the program. The value `null` removes thevariable from the environment.", + type = "object", + }, + console = { + default = "integratedTerminal", + enum = { + "internalConsole", + "integratedTerminal", + "externalTerminal", + }, + enummarkdownDescriptions = { + "%lua.debug.launch.console.internalConsole.description%", + "%lua.debug.launch.console.integratedTerminal.description%", + "%lua.debug.launch.console.externalTerminal.description%", + }, + markdownDescription = "%lua.debug.launch.console.description%", + type = "string", + }, + runtimeExecutable = { + default = OS == "win32" and "${workspaceFolder}/lua.exe" or "${workspaceFolder}/lua", + markdownDescription = "Runtime to use. Either an absolute path or the name of a runtime availableon the PATH.", + type = { + "string", + "null", + }, + }, + runtimeArgs = { + default = "${workspaceFolder}/main.lua", + markdownDescription = "Arguments passed to the runtime executable.", + type = { + "string", + "array", + "null", + }, + }, +} + +if OS == "win32" or OS == "darwin" then + local snippets = json.contributes.debuggers[1].configurationSnippets + snippets[#snippets+1] = { + label = "Lua Debug: Launch Process", + description = "A new configuration for launching a lua process", + body = { + type = "lua", + request = "launch", + name = "${1:launch process}", + stopOnEntry = true, + runtimeExecutable = "^\"\\${workspaceFolder}/lua.exe\"", + runtimeArgs = "^\"\\${workspaceFolder}/${2:main.lua}\"", + } + } + snippets[#snippets+1] = { + label = "Lua Debug: Attach Process", + description = "A new configuration for attaching a lua debug program", + body = { + type = "lua", + request = "attach", + name = "${1:attach}", + stopOnEntry = true, + processId = "^\"\\${command:pickProcess}\"", + } + } +end + +if OS == "win32" then + attributes.common.sourceCoding = { + default = "utf8", + enum = { + "utf8", + "ansi", + }, + markdownDescription = "%lua.debug.launch.sourceCoding.description%", + type = "string", + } + attributes.common.useWSL = { + default = true, + description = "Use Windows Subsystem for Linux.", + type = "boolean", + } + attributes.launch.luaexe.default = "${workspaceFolder}/lua.exe" + attributes.launch.cpath.default = "${workspaceFolder}/?.dll" +else + attributes.launch.luaexe.default = "${workspaceFolder}/lua" + attributes.launch.cpath.default = "${workspaceFolder}/?.so" +end + +local function SupportedArchs() + if OS == "win32" then + return "x86_64", "x86" + elseif OS == "darwin" then + if ARCH == "arm64" then + return "arm64", "x86_64" + else + return "x86_64" + end + elseif OS == "linux" then + if ARCH == "arm64" then + return "arm64" + else + return "x86_64" + end + end +end + +local Archs = {SupportedArchs()} +attributes.launch.luaArch.default = Archs[1] +attributes.launch.luaArch.enum = Archs + +for k, v in pairs(attributes.common) do + attributes.attach[k] = v + attributes.launch[k] = v +end +json.contributes.debuggers[1].configurationAttributes = { + launch = {properties=attributes.launch}, + attach = {properties=attributes.attach}, +} + +local configuration = json.contributes.configuration.properties +for _, name in ipairs {"luaArch", "luaVersion", "sourceCoding", "path", "cpath","console"} do + local attr = attributes.launch[name] or attributes.attach[name] + if attr then + local cfg = {} + for k, v in pairs(attr) do + if k == 'markdownDescription' then + k = 'description' + end + if k == 'enummarkdownDescriptions' then + k = 'enumDescriptions' + end + cfg[k] = v + end + configuration["lua.debug.settings."..name] = cfg + end +end + +return json From 56628cc4d2dc589bf52e38b95c4beaec0b6da7f7 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 14:54:35 +0800 Subject: [PATCH 07/51] add module --- compile/common/package_json.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compile/common/package_json.lua b/compile/common/package_json.lua index bf9d79e7b..50eb10215 100644 --- a/compile/common/package_json.lua +++ b/compile/common/package_json.lua @@ -252,6 +252,11 @@ Debugger address. }, type = "string", }, + module = { + defualt = "null", + markdownDescription = "specify lua module path/name", + type = "string", + } } if OS == "win32" then From e2fa78d1ef28d3c56f48e5aa368ad4a80e078c44 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 14:54:51 +0800 Subject: [PATCH 08/51] update gumpp --- 3rd/frida_gum/gumpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/frida_gum/gumpp b/3rd/frida_gum/gumpp index 54ad1d5a6..041cd4819 160000 --- a/3rd/frida_gum/gumpp +++ b/3rd/frida_gum/gumpp @@ -1 +1 @@ -Subproject commit 54ad1d5a6c8d4856f1db0a3899625a2370b1ca19 +Subproject commit 041cd48199564415da68431d83340c6df2f1112f From ca1fcc06ef108449ca5246e54abe1eefb36b07f4 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 14:55:32 +0800 Subject: [PATCH 09/51] remove signature --- extension/js/configurationProvider.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/extension/js/configurationProvider.js b/extension/js/configurationProvider.js index b6e6cea23..d5a6b5e87 100644 --- a/extension/js/configurationProvider.js +++ b/extension/js/configurationProvider.js @@ -243,9 +243,6 @@ function resolveConfig(folder, config) { throw new Error('Missing `address` to debug'); } } - if (config.signature != null) { - config.signature.version = config.luaVersion - } config.configuration = { variables: vscode.workspace.getConfiguration("lua.debug.variables") } From 1968158af01089cedb702e7d99bb407ed6b7ab86 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 14:58:34 +0800 Subject: [PATCH 10/51] remove ipc_send_latest --- extension/script/frontend/proxy.lua | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/extension/script/frontend/proxy.lua b/extension/script/frontend/proxy.lua index df4f48fce..e098be924 100644 --- a/extension/script/frontend/proxy.lua +++ b/extension/script/frontend/proxy.lua @@ -16,18 +16,14 @@ local function getUnixAddress(pid) return "@"..(path / ("pid_%d"):format(pid)):string() end -local function ipc_send_latest(pid) - fs.create_directories(WORKDIR / "tmp") - local ipc = require "common.ipc" - local fd = assert(ipc(WORKDIR, pid, "luaVersion", "w")) - fd:write("latest") - fd:close() -end - -local function ipc_send_config(pid, config) +local function ipc_send_config(pid, args) fs.create_directories(WORKDIR / "tmp") local ipc = require "common.ipc" local fd = assert(ipc(WORKDIR, pid, "config", "w")) + local config = { + version = args.luaVersion, + module = args.module + } fd:write(json.encode(config)) fd:close() end @@ -65,10 +61,7 @@ end local function attach_process(pkg, pid) local args = pkg.arguments - if args.luaVersion == "latest" then - ipc_send_latest(pid) - end - ipc_send_config(pid, args.signature) + ipc_send_config(pid, args) local ok, errmsg = process_inject.inject(pid, "attach", args) if not ok then return false, errmsg @@ -173,10 +166,7 @@ local function proxy_launch_console(pkg) server, address = create_server(args, process:get_id()) if type(address) == "number" then - if args.luaVersion == "latest" then - ipc_send_latest(address) - end - ipc_send_config(address, args.signature) + ipc_send_config(address, args) end end) if not process then From 2c569d218dfb1de6fe249746a1d72837ec9b9491 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 15:23:21 +0800 Subject: [PATCH 11/51] pass version to luadebug --- extension/script/attach.lua | 21 +++++------- extension/script/debugger.lua | 45 +++++++++++++++---------- src/launcher/autoattach/autoattach.cpp | 4 +-- src/launcher/autoattach/autoattach.h | 3 +- src/launcher/autoattach/lua_module.cpp | 17 ---------- src/launcher/autoattach/lua_module.h | 8 ----- src/launcher/autoattach/lua_version.cpp | 33 ++++++++++++++++++ src/launcher/autoattach/lua_version.h | 14 ++++++++ src/launcher/config/config.cpp | 14 -------- src/launcher/hook/create_watchdog.cpp | 2 ++ src/launcher/hook/watchdog.cpp | 2 +- src/launcher/hook/watchdog.h | 2 ++ src/launcher/main.cpp | 13 ++++--- 13 files changed, 98 insertions(+), 80 deletions(-) create mode 100644 src/launcher/autoattach/lua_version.cpp create mode 100644 src/launcher/autoattach/lua_version.h diff --git a/extension/script/attach.lua b/extension/script/attach.lua index da30f4935..7f85417ff 100644 --- a/extension/script/attach.lua +++ b/extension/script/attach.lua @@ -1,4 +1,4 @@ -local path, pid = ... +local path, pid, version = ... if _VERSION == nil or type == nil or assert == nil @@ -19,6 +19,10 @@ if is_luajit and jit == nil then return "wait initialized" end +if version == "" then + version = nil +end + local function dofile(filename, ...) local load = _VERSION == "Lua 5.1" and loadstring or load local f = assert(io.open(filename)) @@ -26,20 +30,11 @@ local function dofile(filename, ...) f:close() return assert(load(str, "=(debugger.lua)"))(...) end -local function isLatest() - local ipc = dofile(path.."/script/common/ipc.lua") - local fd = ipc(path, pid, "luaVersion") - local result = false - if fd then - result = "latest" == fd:read "a" - fd:close() - end - return result -end + local dbg = dofile(path.."/script/debugger.lua", path) dbg:start { address = ("@%s/tmp/pid_%s"):format(path, pid), - latest = isLatest(), + version = version, } dbg:event "wait" -return "ok" \ No newline at end of file +return "ok" diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index b3b495ee2..5b264026e 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -101,26 +101,37 @@ local function detectLuaDebugPath(cfg) end end - local rt = "/runtime/"..PLATFORM - if cfg.latest then - rt = rt.."/lua-latest" - elseif _VERSION == "Lua 5.4" then - rt = rt.."/lua54" - elseif _VERSION == "Lua 5.3" then - rt = rt.."/lua53" - elseif _VERSION == "Lua 5.2" then - rt = rt.."/lua52" - elseif _VERSION == "Lua 5.1" then - if (tostring(assert):match('builtin') ~= nil) then - rt = rt.."/luajit" - jit.off() - else - rt = rt.."/lua51" + local function get_luadebug_dir() + local version = cfg.version + if not version then + if tostring(assert):match('builtin') ~= nil then + version = "jit" + jit.off() + else + version = _VERSION + end end - else - error(_VERSION.." is not supported.") + + local t = { + ["Lua 5.4"] = "lua54", + ["Lua 5.3"] = "lua53", + ["Lua 5.2"] = "lua52", + ["Lua 5.1"] = "lua51", + latest = "lua-latest", + ["5.4"] = "lua54", + ["5.3"] = "lua53", + ["5.2"] = "lua52", + ["5.1"] = "lua51", + ["jit"] = "luajit", + } + if not t[version] then + error(_VERSION.." is not supported.") + end + return t[version] end + local rt = "/runtime/"..PLATFORM.."/"..get_luadebug_dir() + local ext = isWindows() and "dll" or "so" return root..rt..'/luadebug.'..ext end diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index 2449d9b87..5ff873517 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -31,8 +31,8 @@ namespace luadebug::autoattach { return false; } - attach_status attach_lua_vm(lua::state L) { - return debuggerAttach(L); + attach_status attach_lua_vm(lua::state L, lua_version version) { + return debuggerAttach(L, version); } void start() { diff --git a/src/launcher/autoattach/autoattach.h b/src/launcher/autoattach/autoattach.h index 05df43ef8..184a349f4 100644 --- a/src/launcher/autoattach/autoattach.h +++ b/src/launcher/autoattach/autoattach.h @@ -1,5 +1,6 @@ #pragma once +#include #include namespace luadebug::autoattach { @@ -8,6 +9,6 @@ namespace luadebug::autoattach { fatal, wait, }; - typedef attach_status (*fn_attach)(lua::state L); + typedef attach_status (*fn_attach)(lua::state L, lua_version verison); void initialize(fn_attach attach, bool ap); } diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index bde7ed96b..f8e0e7609 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -9,23 +9,6 @@ #include namespace luadebug::autoattach { - static const char* lua_version_to_string(lua_version v) { - switch (v) { - case lua_version::lua51: - return "lua51"; - case lua_version::lua52: - return "lua52"; - case lua_version::lua53: - return "lua53"; - case lua_version::lua54: - return "lua54"; - case lua_version::luajit: - return "luajit"; - default: - return "unknown"; - } - } - static bool in_module(const lua_module& m, void* addr) { return addr >= m.memory_address && addr <= (void*)((intptr_t)m.memory_address + m.memory_size); } diff --git a/src/launcher/autoattach/lua_module.h b/src/launcher/autoattach/lua_module.h index 23397c3c4..317379cde 100644 --- a/src/launcher/autoattach/lua_module.h +++ b/src/launcher/autoattach/lua_module.h @@ -7,14 +7,6 @@ namespace luadebug::autoattach { struct watchdog; - enum class lua_version { - unknown, - luajit, - lua51, - lua52, - lua53, - lua54, - }; struct lua_module { std::string path; std::string name; diff --git a/src/launcher/autoattach/lua_version.cpp b/src/launcher/autoattach/lua_version.cpp new file mode 100644 index 000000000..d6d12ba0e --- /dev/null +++ b/src/launcher/autoattach/lua_version.cpp @@ -0,0 +1,33 @@ +#include + +namespace luadebug::autoattach { + const char* lua_version_to_string(lua_version v) { + switch (v) { + case lua_version::lua51: + return "lua51"; + case lua_version::lua52: + return "lua52"; + case lua_version::lua53: + return "lua53"; + case lua_version::lua54: + return "lua54"; + case lua_version::luajit: + return "luajit"; + default: + return "unknown"; + } + } + lua_version lua_version_from_string(const std::string_view& v) { + if (v == "jit" || v == "luajit") + return lua_version::luajit; + else if (v == "5.1" || v == "lua51") + return lua_version::lua51; + else if (v == "5.2" || v == "lua52") + return lua_version::lua52; + else if (v == "5.3" || v == "lua53") + return lua_version::lua53; + else if (v == "5.4" || v == "lua54") + return lua_version::lua54; + return lua_version::unknown; + } +} \ No newline at end of file diff --git a/src/launcher/autoattach/lua_version.h b/src/launcher/autoattach/lua_version.h new file mode 100644 index 000000000..1797b3338 --- /dev/null +++ b/src/launcher/autoattach/lua_version.h @@ -0,0 +1,14 @@ +#pragma once +#include +namespace luadebug::autoattach { + enum class lua_version { + unknown, + luajit, + lua51, + lua52, + lua53, + lua54, + }; + const char* lua_version_to_string(lua_version v); + lua_version lua_version_from_string(const std::string_view& v); +} \ No newline at end of file diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp index 662d7d410..ccad7baa2 100644 --- a/src/launcher/config/config.cpp +++ b/src/launcher/config/config.cpp @@ -12,20 +12,6 @@ using namespace std::string_view_literals; namespace luadebug::autoattach { - static lua_version lua_version_from_string [[maybe_unused]] (const std::string_view& v) { - if (v == "jit" || v == "luajit") - return lua_version::luajit; - else if (v == "5.1" || v == "lua51") - return lua_version::lua51; - else if (v == "5.2" || v == "lua52") - return lua_version::lua52; - else if (v == "5.3" || v == "lua53") - return lua_version::lua53; - else if (v == "5.4" || v == "lua54") - return lua_version::lua54; - return lua_version::unknown; - } - Config config; lua_version Config::get_lua_version() const { diff --git a/src/launcher/hook/create_watchdog.cpp b/src/launcher/hook/create_watchdog.cpp index e62dcef1c..d0a2ed1e8 100644 --- a/src/launcher/hook/create_watchdog.cpp +++ b/src/launcher/hook/create_watchdog.cpp @@ -53,6 +53,7 @@ namespace luadebug::autoattach { if (context->init(resolver, get_watch_points(version))) { // TODO: fix other thread pc context->hook(); + context->version = version; return context; } if (version == lua_version::unknown) { @@ -61,6 +62,7 @@ namespace luadebug::autoattach { if (context->init(resolver, get_watch_points(lua_version::unknown))) { // TODO: fix other thread pc context->hook(); + context->version = version; return context; } return nullptr; diff --git a/src/launcher/hook/watchdog.cpp b/src/launcher/hook/watchdog.cpp index 83271c52f..d84ae0e5e 100644 --- a/src/launcher/hook/watchdog.cpp +++ b/src/launcher/hook/watchdog.cpp @@ -116,7 +116,7 @@ namespace luadebug::autoattach { void watchdog::attach_lua(lua::state L, lua::debug ar, lua::hook fn) { reset_luahook(L, ar); - switch (attach_lua_vm(L)) { + switch (attach_lua_vm(L, version)) { case attach_status::fatal: case attach_status::success: // TODO: how to free so diff --git a/src/launcher/hook/watchdog.h b/src/launcher/hook/watchdog.h index dbc8cf71f..11d984b99 100644 --- a/src/launcher/hook/watchdog.h +++ b/src/launcher/hook/watchdog.h @@ -38,5 +38,7 @@ namespace luadebug::autoattach { int origin_hookmask; int origin_hookcount; fn_attach attach_lua_vm; + public: + lua_version version; }; } diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index b99d9342e..e05430696 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -11,6 +11,7 @@ # define DLLEXPORT __declspec(dllexport) # define DLLEXPORT_DECLARATION __cdecl #endif +#include #include #include @@ -34,7 +35,7 @@ namespace luadebug::autoattach { return tmp; } - static attach_status attach(lua::state L) { + static attach_status attach(lua::state L, lua_version verison) { log::info("attach lua vm entry"); auto r = bee::path_helper::dll_path(); if (!r) { @@ -49,12 +50,10 @@ namespace luadebug::autoattach { return attach_status::fatal; } lua::call(L, root.generic_u8string().c_str()); -#ifdef _WIN32 - lua::call(L, std::to_string(GetCurrentProcessId()).c_str()); -#else - lua::call(L, std::to_string(getpid()).c_str()); -#endif - if (lua::pcall(L, 2, 1, 0)) { + lua::call(L, std::to_string(Gum::Process::get_id()).c_str()); + lua::call(L, verison == lua_version::unknown ? "" : lua_version_to_string(verison)); + + if (lua::pcall(L, 3, 1, 0)) { /* 这里失败无法调用log::fatal,因为无法知道调试器已经加载到哪一步才失败的。 所以调试器不应该把错误抛到这里。 From 6dccc088a683b76887638a643cb202a971a1a9b4 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 15:26:53 +0800 Subject: [PATCH 12/51] fix format --- src/launcher/hook/watchdog.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/launcher/hook/watchdog.h b/src/launcher/hook/watchdog.h index 11d984b99..986df44d5 100644 --- a/src/launcher/hook/watchdog.h +++ b/src/launcher/hook/watchdog.h @@ -38,6 +38,7 @@ namespace luadebug::autoattach { int origin_hookmask; int origin_hookcount; fn_attach attach_lua_vm; + public: lua_version version; }; From 5738af299e237c4f041f83e0bdb4d20f40ea1c89 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 16:04:06 +0800 Subject: [PATCH 13/51] force load_luadebug_dll --- src/launcher/autoattach/lua_module.cpp | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index f8e0e7609..f37d1135a 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -62,6 +62,45 @@ namespace luadebug::autoattach { } } + bool load_luadebug_dll(lua_version version) { + if (version != lua_version::unknown) + return false; + + auto dllpath = bee::path_helper::dll_path(); + if (!dllpath) { + return false; + } + auto os = +#if defined(_WIN32) + "windows" +#elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) + "darwin" +#else + ; + return false; +#endif + ; + auto arch = +#if defined(_M_ARM64) || defined(__aarch64__) + "arm64" +#elif defined(_M_IX86) || defined(__i386__) + "x86" +#elif defined(_M_X64) || defined(__x86_64__) + "x86_64" +#else + ; + return false; +#endif + ; + auto platform = std::format("{}-{}", os, arch); + auto path = dllpath.value().parent_path().parent_path() / "runtime" / platform / lua_version_to_string(version); + std::string error; + if (!Gum::Process::module_load(path.c_str(), &error)) { + log::fatal("load remotedebug dll failed: {}", error); + } + return true; + } + bool lua_module::initialize(fn_attach attach_lua_vm) { resolver.module_name = path; auto error_msg = lua::initialize(resolver); @@ -72,6 +111,9 @@ namespace luadebug::autoattach { version = get_lua_version(*this); log::info("current lua version: {}", lua_version_to_string(version)); + if (!load_luadebug_dll(version)) { + return false; + } watchdog = create_watchdog(attach_lua_vm, version, resolver); if (!watchdog) { // TODO: more errmsg From fb0659f5c10b92a4fd9e0f8e6cb380c5b3ca3d12 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 20:30:34 +0800 Subject: [PATCH 14/51] fix build error --- src/launcher/autoattach/lua_module.cpp | 2 +- src/launcher/main.cpp | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index f37d1135a..54abbe6a9 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -93,7 +93,7 @@ namespace luadebug::autoattach { #endif ; auto platform = std::format("{}-{}", os, arch); - auto path = dllpath.value().parent_path().parent_path() / "runtime" / platform / lua_version_to_string(version); + auto path = (dllpath.value().parent_path().parent_path() / "runtime" / platform / lua_version_to_string(version)).string(); std::string error; if (!Gum::Process::module_load(path.c_str(), &error)) { log::fatal("load remotedebug dll failed: {}", error); diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index e05430696..67aa0a502 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -3,11 +3,9 @@ #include #include #ifndef _WIN32 -# include # define DLLEXPORT __attribute__((visibility("default"))) # define DLLEXPORT_DECLARATION #else -# include # define DLLEXPORT __declspec(dllexport) # define DLLEXPORT_DECLARATION __cdecl #endif @@ -16,9 +14,9 @@ #include namespace luadebug::autoattach { - static std::string readfile(const fs::path& filename) { + static std::string readfile(const fs::path &filename) { #ifdef _WIN32 - FILE* f = _wfopen(filename.c_str(), L"rb"); + FILE *f = _wfopen(filename.c_str(), L"rb"); #else FILE* f = fopen(filename.c_str(), "rb"); #endif @@ -43,7 +41,7 @@ namespace luadebug::autoattach { return attach_status::fatal; } auto root = r.value().parent_path().parent_path(); - auto buf = readfile(root / "script" / "attach.lua"); + auto buf = readfile(root / "script" / "attach.lua"); if (lua::loadbuffer(L, buf.data(), buf.size(), "=(attach.lua)")) { log::fatal("load attach.lua error: {}", lua::tostring(L, -1)); lua::pop(L, 1); From 9054deee1f08af58c6cf4fd252fb55f2e917d8ea Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 21:16:11 +0800 Subject: [PATCH 15/51] format --- src/launcher/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index 67aa0a502..f57e82920 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -2,6 +2,7 @@ #include #include #include + #ifndef _WIN32 # define DLLEXPORT __attribute__((visibility("default"))) # define DLLEXPORT_DECLARATION @@ -9,6 +10,7 @@ # define DLLEXPORT __declspec(dllexport) # define DLLEXPORT_DECLARATION __cdecl #endif + #include #include #include From e728a3b4857f14807e2cf0670dfe331cdc873db0 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 18 Mar 2023 21:22:47 +0800 Subject: [PATCH 16/51] format --- src/launcher/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index f57e82920..a2aba6ea8 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -43,7 +43,7 @@ namespace luadebug::autoattach { return attach_status::fatal; } auto root = r.value().parent_path().parent_path(); - auto buf = readfile(root / "script" / "attach.lua"); + auto buf = readfile(root / "script" / "attach.lua"); if (lua::loadbuffer(L, buf.data(), buf.size(), "=(attach.lua)")) { log::fatal("load attach.lua error: {}", lua::tostring(L, -1)); lua::pop(L, 1); From 8bb60fea74e1cf03d6e98aac4a4d56d04f87b004 Mon Sep 17 00:00:00 2001 From: fesily Date: Mon, 20 Mar 2023 14:44:35 +0800 Subject: [PATCH 17/51] Update package_json.lua fix grammar error --- compile/common/package_json.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compile/common/package_json.lua b/compile/common/package_json.lua index 11d859bd5..6ea0f9768 100644 --- a/compile/common/package_json.lua +++ b/compile/common/package_json.lua @@ -253,7 +253,7 @@ Debugger address. type = "string", }, module = { - defualt = "null", + default = "null", markdownDescription = "specify lua module path/name", type = "string", } From 11fca9c7e512beb95a419056d1204fb34ed3a573 Mon Sep 17 00:00:00 2001 From: fesily Date: Mon, 20 Mar 2023 15:31:03 +0800 Subject: [PATCH 18/51] version add latest --- src/launcher/autoattach/lua_version.cpp | 4 ++++ src/launcher/autoattach/lua_version.h | 1 + src/launcher/hook/create_watchdog.cpp | 1 + 3 files changed, 6 insertions(+) diff --git a/src/launcher/autoattach/lua_version.cpp b/src/launcher/autoattach/lua_version.cpp index d6d12ba0e..c1cdba2b5 100644 --- a/src/launcher/autoattach/lua_version.cpp +++ b/src/launcher/autoattach/lua_version.cpp @@ -13,6 +13,8 @@ namespace luadebug::autoattach { return "lua54"; case lua_version::luajit: return "luajit"; + case lua_version::latest: + return "lua-latest"; default: return "unknown"; } @@ -28,6 +30,8 @@ namespace luadebug::autoattach { return lua_version::lua53; else if (v == "5.4" || v == "lua54") return lua_version::lua54; + else if (v == "latest" || v == "lua-latest") + return lua_version::latest; return lua_version::unknown; } } \ No newline at end of file diff --git a/src/launcher/autoattach/lua_version.h b/src/launcher/autoattach/lua_version.h index 1797b3338..406009d70 100644 --- a/src/launcher/autoattach/lua_version.h +++ b/src/launcher/autoattach/lua_version.h @@ -8,6 +8,7 @@ namespace luadebug::autoattach { lua52, lua53, lua54, + latest, }; const char* lua_version_to_string(lua_version v); lua_version lua_version_from_string(const std::string_view& v); diff --git a/src/launcher/hook/create_watchdog.cpp b/src/launcher/hook/create_watchdog.cpp index d0a2ed1e8..09696d83f 100644 --- a/src/launcher/hook/create_watchdog.cpp +++ b/src/launcher/hook/create_watchdog.cpp @@ -27,6 +27,7 @@ namespace luadebug::autoattach { { watch_point::type::common, "luaV_gettable" }, { watch_point::type::common, "luaV_settable" }, }; + case lua_version::latest: case lua_version::lua54: return { { watch_point::type::common, "luaD_poscall" }, From 4fa4d91bc02ef2112287785fecd97132600f6427 Mon Sep 17 00:00:00 2001 From: fesily Date: Wed, 22 Mar 2023 16:39:25 +0800 Subject: [PATCH 19/51] fix build err --- compile/common/launcher.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/compile/common/launcher.lua b/compile/common/launcher.lua index 754a5d065..f1e4831ef 100644 --- a/compile/common/launcher.lua +++ b/compile/common/launcher.lua @@ -20,6 +20,7 @@ lm:lua_source 'launcher_source' { "3rd/bee.lua", "3rd/frida_gum/gumpp", "3rd/lua/lua54", + "3rd/json/single_include", "src/launcher", }, sources = { From c8bdd8f2afef5ddb4f28f8617b4cd426ca268bd5 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 23 Mar 2023 10:53:37 +0800 Subject: [PATCH 20/51] fix all error --- extension/script/debugger.lua | 12 ++++++------ extension/script/frontend/proxy.lua | 2 ++ src/launcher/autoattach/lua_module.cpp | 27 ++++++++++++++------------ src/launcher/config/config.cpp | 5 ++--- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index 5b264026e..b61bcfb82 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -118,14 +118,14 @@ local function detectLuaDebugPath(cfg) ["Lua 5.2"] = "lua52", ["Lua 5.1"] = "lua51", latest = "lua-latest", - ["5.4"] = "lua54", - ["5.3"] = "lua53", - ["5.2"] = "lua52", - ["5.1"] = "lua51", - ["jit"] = "luajit", + lua54 = "lua54", + lua53 = "lua53", + lua52 = "lua52", + lua51 = "lua51", + luajit = "luajit", } if not t[version] then - error(_VERSION.." is not supported.") + error(version.." is not supported.") end return t[version] end diff --git a/extension/script/frontend/proxy.lua b/extension/script/frontend/proxy.lua index e098be924..cb7084c51 100644 --- a/extension/script/frontend/proxy.lua +++ b/extension/script/frontend/proxy.lua @@ -11,12 +11,14 @@ local initReq local m = {} local function getUnixAddress(pid) + --TODO: clear unix file local path = WORKDIR / "tmp" fs.create_directories(path) return "@"..(path / ("pid_%d"):format(pid)):string() end local function ipc_send_config(pid, args) + --TODO: clear config file fs.create_directories(WORKDIR / "tmp") local ipc = require "common.ipc" local fd = assert(ipc(WORKDIR, pid, "config", "w")) diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index 54abbe6a9..9e0ae63ec 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -63,23 +63,25 @@ namespace luadebug::autoattach { } bool load_luadebug_dll(lua_version version) { - if (version != lua_version::unknown) - return false; - auto dllpath = bee::path_helper::dll_path(); if (!dllpath) { return false; } - auto os = +#define LUADEBUG_FILE "luadebug" #if defined(_WIN32) - "windows" + auto os = "windows"; + auto luadebug_name = + LUADEBUG_FILE + ".dll"; #elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) - "darwin" + auto os = "darwin"; + auto luadebug_name = + LUADEBUG_FILE + ".so"; #else - ; return false; #endif - ; + ; auto arch = #if defined(_M_ARM64) || defined(__aarch64__) "arm64" @@ -93,10 +95,10 @@ namespace luadebug::autoattach { #endif ; auto platform = std::format("{}-{}", os, arch); - auto path = (dllpath.value().parent_path().parent_path() / "runtime" / platform / lua_version_to_string(version)).string(); + auto path = (dllpath.value().parent_path().parent_path() / "runtime" / platform / lua_version_to_string(version) / luadebug_name).string(); std::string error; if (!Gum::Process::module_load(path.c_str(), &error)) { - log::fatal("load remotedebug dll failed: {}", error); + log::fatal("load debugger [{}] failed: {}", path, error); } return true; } @@ -111,8 +113,9 @@ namespace luadebug::autoattach { version = get_lua_version(*this); log::info("current lua version: {}", lua_version_to_string(version)); - if (!load_luadebug_dll(version)) { - return false; + if (version != lua_version::unknown) { + if (!load_luadebug_dll(version)) + return false; } watchdog = create_watchdog(attach_lua_vm, version, resolver); if (!watchdog) { diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp index ccad7baa2..62df63e07 100644 --- a/src/launcher/config/config.cpp +++ b/src/launcher/config/config.cpp @@ -45,10 +45,9 @@ namespace luadebug::autoattach { if (!dllpath) { return false; } + auto filename = (dllpath.value().parent_path().parent_path() / "tmp" / std::format("ipc_{}_config", Gum::Process::get_id())).string(); - auto filename = std::format("{}/tmp/pid_{}_config", dllpath.value().parent_path().parent_path().generic_string(), Gum::Process::get_id()); - - std::ifstream s(filename, s.in); + std::ifstream s(filename, s.binary); if (!s.is_open()) return false; try { From fb1d2d0cfbb7906e24bf5b0a29f37d80d2d65dcc Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 23 Mar 2023 13:37:42 +0800 Subject: [PATCH 21/51] attach luaVersion default unknown --- compile/common/package_json.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compile/common/package_json.lua b/compile/common/package_json.lua index d2d8e4711..bf5d084c1 100644 --- a/compile/common/package_json.lua +++ b/compile/common/package_json.lua @@ -476,6 +476,10 @@ for k, v in pairs(attributes.common) do attributes.attach[k] = v attributes.launch[k] = v end + +table.insert(attributes.attach.luaVersion.enum, "unknown") +attributes.attach.luaVersion.default = "unknown" + json.contributes.debuggers[1].configurationAttributes = { launch = { properties = attributes.launch }, attach = { properties = attributes.attach }, From ce666d000a6a05b3898df839db4dd59d05215394 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 25 Mar 2023 13:20:50 +0800 Subject: [PATCH 22/51] add lua_module_backlist --- src/launcher/autoattach/autoattach.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index 2fc8e8434..78ba2ce3e 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -13,6 +13,17 @@ namespace luadebug::autoattach { fn_attach debuggerAttach; +#ifdef _WIN32 +# define EXT ".dll" +#else +# define EXT ".so" +#endif + + constexpr auto lua_module_backlist[] = { + "launcher" EXT, + "luadebug" EXT, + }; + constexpr auto find_lua_module_key = "lua_newstate"; constexpr auto lua_module_strings = std::array { "luaJIT_BC_%s", // luajit @@ -26,7 +37,12 @@ namespace luadebug::autoattach { " $", // others }; static bool is_lua_module(const char* module_path, bool check_export = true, bool check_strings = false) { - if (std::string_view(module_path).find(config.get_lua_module()) != std::string_view::npos) + auto str = std::string_view(module_path); + for (auto& s : lua_module_backlist) { + if (str.find(s) != std::string_view::npos) + return false; + } + if (str.find(config.get_lua_module()) != std::string_view::npos) return true; if (check_export && Gum::Process::module_find_export_by_name(module_path, find_lua_module_key)) return true; if (Gum::Process::module_find_symbol_by_name(module_path, find_lua_module_key)) return true; From 9d495d60ecd5b86495e09e0ac68d576a960c5850 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 25 Mar 2023 13:32:26 +0800 Subject: [PATCH 23/51] fix build error --- src/launcher/autoattach/autoattach.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index 78ba2ce3e..8dd531b8e 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -18,8 +18,7 @@ namespace luadebug::autoattach { #else # define EXT ".so" #endif - - constexpr auto lua_module_backlist[] = { + constexpr auto lua_module_backlist = { "launcher" EXT, "luadebug" EXT, }; From ff05aa45cfc98768c4a162f41c34447710399afd Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 25 Mar 2023 13:41:40 +0800 Subject: [PATCH 24/51] update gumpp --- 3rd/frida_gum/gumpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/frida_gum/gumpp b/3rd/frida_gum/gumpp index fdea61ce5..ff3d3c8f0 160000 --- a/3rd/frida_gum/gumpp +++ b/3rd/frida_gum/gumpp @@ -1 +1 @@ -Subproject commit fdea61ce5fa7e11e6e84817ef11a9a44e73d76d5 +Subproject commit ff3d3c8f063f0c935eb057e364363ceeea0431bf From e7d22455a33d64716af98af09acf337cf6f5b026 Mon Sep 17 00:00:00 2001 From: fesily Date: Sat, 25 Mar 2023 15:47:49 +0800 Subject: [PATCH 25/51] module support symlink --- 3rd/frida_gum/gumpp | 2 +- src/launcher/autoattach/autoattach.cpp | 6 ++++-- src/launcher/config/config.cpp | 14 +++++++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/3rd/frida_gum/gumpp b/3rd/frida_gum/gumpp index ff3d3c8f0..3fcb831c2 160000 --- a/3rd/frida_gum/gumpp +++ b/3rd/frida_gum/gumpp @@ -1 +1 @@ -Subproject commit ff3d3c8f063f0c935eb057e364363ceeea0431bf +Subproject commit 3fcb831c2c6cfc4e8942c31a3dba04095cec55f9 diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index 8dd531b8e..269f1d4f7 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -41,8 +41,10 @@ namespace luadebug::autoattach { if (str.find(s) != std::string_view::npos) return false; } - if (str.find(config.get_lua_module()) != std::string_view::npos) - return true; + + auto target_lua_module = config.get_lua_module(); + if (!target_lua_module.empty() && str.find(config.get_lua_module()) != std::string_view::npos) return true; + if (check_export && Gum::Process::module_find_export_by_name(module_path, find_lua_module_key)) return true; if (Gum::Process::module_find_symbol_by_name(module_path, find_lua_module_key)) return true; // TODO: when signature mode, check strings diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp index 62df63e07..56f451248 100644 --- a/src/launcher/config/config.cpp +++ b/src/launcher/config/config.cpp @@ -32,11 +32,19 @@ namespace luadebug::autoattach { if (it == values->end()) { return {}; } - const auto& value = it->get(); + auto value = it->get(); if (!fs::exists(value)) - return {}; + return value; - return value; + do { + if (!fs::is_symlink(value)) + return value; + auto link = fs::read_symlink(value); + if (link.is_absolute()) + value = link.string(); + else + value = (fs::path(value).parent_path() / link).lexically_normal().string(); + } while (true); } bool Config::init_from_file() { From 82d832d657b5295946df4116466046b00e1c4609 Mon Sep 17 00:00:00 2001 From: fesily Date: Mon, 27 Mar 2023 10:08:25 +0800 Subject: [PATCH 26/51] Optimize config --- src/launcher/autoattach/autoattach.cpp | 22 +++--- src/launcher/autoattach/lua_module.cpp | 43 ++--------- src/launcher/autoattach/lua_module.h | 2 + src/launcher/autoattach/wait_dll.cpp | 12 +-- src/launcher/autoattach/wait_dll.h | 6 +- src/launcher/config/config.cpp | 103 ++++++++++++++++++++----- src/launcher/config/config.h | 30 +++---- src/launcher/util/log.cpp | 19 ++--- 8 files changed, 140 insertions(+), 97 deletions(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index 269f1d4f7..cec38e8e0 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -35,15 +35,15 @@ namespace luadebug::autoattach { "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" " $", // others }; - static bool is_lua_module(const char* module_path, bool check_export = true, bool check_strings = false) { + static bool is_lua_module(const char* module_path, const config::Config& config, bool check_export = true, bool check_strings = false) { auto str = std::string_view(module_path); for (auto& s : lua_module_backlist) { if (str.find(s) != std::string_view::npos) return false; } - auto target_lua_module = config.get_lua_module(); - if (!target_lua_module.empty() && str.find(config.get_lua_module()) != std::string_view::npos) return true; + auto target_lua_module = config.lua_module; + if (!target_lua_module.empty() && str.find(target_lua_module) != std::string_view::npos) return true; if (check_export && Gum::Process::module_find_export_by_name(module_path, find_lua_module_key)) return true; if (Gum::Process::module_find_symbol_by_name(module_path, find_lua_module_key)) return true; @@ -59,7 +59,7 @@ namespace luadebug::autoattach { } static void start(); - static bool load_lua_module(const std::string& path) { + static bool load_lua_module(const std::string& path, const config::Config& config) { constexpr auto check_export = #ifdef _WIN32 true @@ -67,7 +67,7 @@ namespace luadebug::autoattach { false #endif ; - if (!is_lua_module(path.c_str(), check_export, true)) { + if (!is_lua_module(path.c_str(), config, check_export, true)) { return false; } // find lua module lazy @@ -80,14 +80,15 @@ namespace luadebug::autoattach { } void start() { - if (!Config::init_from_file()) { + auto config = config::init_from_file(); + if (!config) { log::info("can't load config"); return; } bool found = false; lua_module rm = {}; - Gum::Process::enumerate_modules([&rm, &found](const Gum::ModuleDetails& details) -> bool { - if (is_lua_module(details.path())) { + Gum::Process::enumerate_modules([&rm, &found, conf = &(*config)](const Gum::ModuleDetails& details) -> bool { + if (is_lua_module(details.path(), *conf)) { auto range = details.range(); rm.memory_address = range.base_address; rm.memory_size = range.size; @@ -99,12 +100,15 @@ namespace luadebug::autoattach { return true; }); if (!found) { - if (!wait_dll(load_lua_module)) { + if (!wait_dll([conf = std::move(*config)](const std::string& path) { + return load_lua_module(path, conf); + })) { log::fatal("can't find lua module"); } return; } log::info("find lua module path:{}", rm.path); + rm.config = std::move(*config); if (!rm.initialize(attach_lua_vm)) { return; } diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index 9e0ae63ec..77687e0dc 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -14,9 +14,6 @@ namespace luadebug::autoattach { } static lua_version get_lua_version(const lua_module& m) { - auto version = config.get_lua_version(); - if (version != lua_version::unknown) - return version; /* luaJIT_version_2_1_0_beta3 luaJIT_version_2_1_0_beta2 @@ -63,39 +60,10 @@ namespace luadebug::autoattach { } bool load_luadebug_dll(lua_version version) { - auto dllpath = bee::path_helper::dll_path(); - if (!dllpath) { + auto luadebug_path = config::get_luadebug_path(version); + if (!luadebug_path) return false; - } -#define LUADEBUG_FILE "luadebug" -#if defined(_WIN32) - auto os = "windows"; - auto luadebug_name = - LUADEBUG_FILE - ".dll"; -#elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) - auto os = "darwin"; - auto luadebug_name = - LUADEBUG_FILE - ".so"; -#else - return false; -#endif - ; - auto arch = -#if defined(_M_ARM64) || defined(__aarch64__) - "arm64" -#elif defined(_M_IX86) || defined(__i386__) - "x86" -#elif defined(_M_X64) || defined(__x86_64__) - "x86_64" -#else - ; - return false; -#endif - ; - auto platform = std::format("{}-{}", os, arch); - auto path = (dllpath.value().parent_path().parent_path() / "runtime" / platform / lua_version_to_string(version) / luadebug_name).string(); + auto path = (*luadebug_path).string(); std::string error; if (!Gum::Process::module_load(path.c_str(), &error)) { log::fatal("load debugger [{}] failed: {}", path, error); @@ -110,7 +78,10 @@ namespace luadebug::autoattach { log::fatal("lua initialize failed, can't find {}", error_msg); return false; } - version = get_lua_version(*this); + version = config.version; + if (version == lua_version::unknown) { + version = get_lua_version(*this); + } log::info("current lua version: {}", lua_version_to_string(version)); if (version != lua_version::unknown) { diff --git a/src/launcher/autoattach/lua_module.h b/src/launcher/autoattach/lua_module.h index 317379cde..f797b74dc 100644 --- a/src/launcher/autoattach/lua_module.h +++ b/src/launcher/autoattach/lua_module.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -15,6 +16,7 @@ namespace luadebug::autoattach { lua_version version = lua_version::unknown; lua_resolver resolver; watchdog* watchdog = nullptr; + config::Config config; bool initialize(fn_attach attach_lua_vm); }; diff --git a/src/launcher/autoattach/wait_dll.cpp b/src/launcher/autoattach/wait_dll.cpp index 5063c7bea..92bd1b4c3 100644 --- a/src/launcher/autoattach/wait_dll.cpp +++ b/src/launcher/autoattach/wait_dll.cpp @@ -32,7 +32,7 @@ extern "C" bool gum_darwin_query_all_image_infos(mach_port_t task, _GumDarwinAll #endif namespace luadebug::autoattach { #ifdef _WIN32 - bool wait_dll(bool (*loaded)(std::string const&)) { + bool wait_dll(WaitDllCallBack_t loaded) { typedef struct _LDR_DLL_UNLOADED_NOTIFICATION_DATA { ULONG Flags; // Reserved. PCUNICODE_STRING FullDllName; // The full path name of the DLL module. @@ -91,19 +91,19 @@ namespace luadebug::autoattach { 0, [](LdrDllNotificationReason NotificationReason, PLDR_DLL_NOTIFICATION_DATA const NotificationData, PVOID Context) { if (NotificationReason == LdrDllNotificationReason::LDR_DLL_NOTIFICATION_REASON_LOADED) { auto path = fs::path(std::wstring(NotificationData->Loaded.FullDllName->Buffer, NotificationData->Loaded.FullDllName->Length)).string(); - auto f = (decltype(loaded))Context; - if (f(path)) { + auto ptr = (decltype(loaded)*)Context; + if ((*ptr)(path)) { if (dllNotification.Cookie) dllNotification.dllUnregisterNotification(dllNotification.Cookie); + delete ptr; } } }, - loaded, &dllNotification.Cookie + (void*)new decltype(loaded)(loaded), &dllNotification.Cookie ); return true; } #elif defined(__APPLE__) - using WaitDllCallBack_t = bool (*)(std::string const&); struct WaitDllListener : Gum::NoLeaveInvocationListener { WaitDllCallBack_t loaded; Gum::RefPtr interceptor; @@ -153,7 +153,7 @@ namespace luadebug::autoattach { // TODO: support linux - bool wait_dll(bool (*loaded)(std::string const&)) { + bool wait_dll(WaitDllCallBack_t loaded) { return false; } #endif diff --git a/src/launcher/autoattach/wait_dll.h b/src/launcher/autoattach/wait_dll.h index 8ace7a505..0c56ef908 100644 --- a/src/launcher/autoattach/wait_dll.h +++ b/src/launcher/autoattach/wait_dll.h @@ -1,7 +1,11 @@ #pragma once +#include + +#include #include namespace luadebug::autoattach { - bool wait_dll(bool (*loaded)(std::string const&)); + using WaitDllCallBack_t = std::function; + bool wait_dll(WaitDllCallBack_t loaded); } diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp index 56f451248..eeaafd256 100644 --- a/src/launcher/config/config.cpp +++ b/src/launcher/config/config.cpp @@ -11,25 +11,24 @@ #include using namespace std::string_view_literals; -namespace luadebug::autoattach { - Config config; - - lua_version Config::get_lua_version() const { +namespace luadebug::config { + using namespace autoattach; + static lua_version get_lua_version(nlohmann::json& values) { const auto key = "version"sv; - auto it = values->find(key); - if (!values || it == values->end()) { + auto it = values.find(key); + if (!values || it == values.end()) { return lua_version::unknown; } return lua_version_from_string(it->get()); } - std::string Config::get_lua_module() const { + static std::string get_lua_module(nlohmann::json& values) { const auto key = "module"sv; - auto it = values->find(key); - if (it == values->end()) { + auto it = values.find(key); + if (it == values.end()) { return {}; } auto value = it->get(); @@ -47,24 +46,90 @@ namespace luadebug::autoattach { } while (true); } - bool Config::init_from_file() { - config.values = std::make_unique(); - auto dllpath = bee::path_helper::dll_path(); - if (!dllpath) { - return false; - } - auto filename = (dllpath.value().parent_path().parent_path() / "tmp" / std::format("ipc_{}_config", Gum::Process::get_id())).string(); + std::optional init_from_file() { + Config config; + nlohmann::json values; + + auto tmp = get_tmp_dir(); + if (!tmp) + return std::nullopt; + auto filename = ((*tmp) / std::format("ipc_{}_config", Gum::Process::get_id())).string(); std::ifstream s(filename, s.binary); if (!s.is_open()) - return false; + return std::nullopt; try { - s >> *config.values; + s >> values; } catch (const nlohmann::json::exception& e) { log::info("init_from_file error: {}", e.what()); } - return true; + config.version = get_lua_version(values); + config.lua_module = get_lua_module(values); + + return config; + } + + std::optional get_plugin_root() { + auto dllpath = bee::path_helper::dll_path(); + if (!dllpath) { + return std::nullopt; + } + return dllpath.value().parent_path().parent_path(); + } + + std::optional get_tmp_dir() { + auto root = get_plugin_root(); + if (!root) + return std::nullopt; + return (*root) / "tmp"; + } + + std::optional get_runtime_dir() { + auto root = get_plugin_root(); + if (!root) + return std::nullopt; + auto os = +#if defined(_WIN32) + "windows"; +#elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) + "darwin"; +#else + "linux"; +#endif + auto arch = +#if defined(_M_ARM64) || defined(__aarch64__) + "arm64"; +#elif defined(_M_IX86) || defined(__i386__) + "x86"; +#elif defined(_M_X64) || defined(__x86_64__) + "x86_64"; +#else +# error "Unknown architecture" +#endif + auto platform = std::format("{}-{}", os, arch); + return (*root) / "runtime" / platform; + } + + std::optional get_lua_runtime_dir(lua_version version) { + auto runtime = get_runtime_dir(); + if (!runtime) + return std::nullopt; + return (*runtime) / lua_version_to_string(version); + } + + std::optional get_luadebug_path(lua_version version) { + auto runtime = get_lua_runtime_dir(version); + if (!runtime) + return std::nullopt; +#define LUADEBUG_FILE "luadebug" + +#if defined(_WIN32) +# define EXT ".dll" +#else +# define EXT ".so" +#endif + return (*runtime) / (LUADEBUG_FILE EXT); } } // namespace luadebug::autoattach \ No newline at end of file diff --git a/src/launcher/config/config.h b/src/launcher/config/config.h index 049df88fd..4d8876e6c 100644 --- a/src/launcher/config/config.h +++ b/src/launcher/config/config.h @@ -1,23 +1,23 @@ +#pragma once +#include +#include + #include -#include #include #include #include #include -namespace luadebug::autoattach { - - enum class lua_version; - class Config { - std::unique_ptr values; - - public: - lua_version get_lua_version() const; - - std::string get_lua_module() const; - - static bool init_from_file(); +namespace luadebug::config { + using lua_version = autoattach::lua_version; + std::optional get_plugin_root(); + std::optional get_tmp_dir(); + std::optional get_runtime_dir(); + std::optional get_lua_runtime_dir(lua_version version); + std::optional get_luadebug_path(lua_version version); + struct Config { + lua_version version = lua_version::unknown; + std::string lua_module; }; - - extern Config config; + std::optional init_from_file(); } // namespace luadebug::autoattach \ No newline at end of file diff --git a/src/launcher/util/log.cpp b/src/launcher/util/log.cpp index afe808782..247dba360 100644 --- a/src/launcher/util/log.cpp +++ b/src/launcher/util/log.cpp @@ -13,6 +13,10 @@ # include #endif +#include + +#include + namespace luadebug::log { static bool attach_mode = false; @@ -55,18 +59,11 @@ namespace luadebug::log { } void notify_frontend(const std::string& msg) { - auto dllpath = bee::path_helper::dll_path(); - if (!dllpath) { + auto tmp = config::get_tmp_dir(); + if (!tmp) return; - } - auto rootpath = dllpath.value().parent_path().parent_path(); - auto path = std::format("{}/tmp/pid_{}", rootpath.generic_u8string(), -#if defined(_WIN32) - GetCurrentProcessId() -#else - getpid() -#endif - ); + auto path = ((*tmp) / std::format("pid_{}", Gum::Process::get_id())).string(); + if (!socket::initialize()) { return; } From b5f9176e8cc316007b1def54d8f925a2fc14703d Mon Sep 17 00:00:00 2001 From: fesily Date: Mon, 27 Mar 2023 10:13:17 +0800 Subject: [PATCH 27/51] fix blacklist --- src/launcher/autoattach/autoattach.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index cec38e8e0..b2cac5aac 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -13,16 +13,6 @@ namespace luadebug::autoattach { fn_attach debuggerAttach; -#ifdef _WIN32 -# define EXT ".dll" -#else -# define EXT ".so" -#endif - constexpr auto lua_module_backlist = { - "launcher" EXT, - "luadebug" EXT, - }; - constexpr auto find_lua_module_key = "lua_newstate"; constexpr auto lua_module_strings = std::array { "luaJIT_BC_%s", // luajit @@ -37,8 +27,10 @@ namespace luadebug::autoattach { }; static bool is_lua_module(const char* module_path, const config::Config& config, bool check_export = true, bool check_strings = false) { auto str = std::string_view(module_path); - for (auto& s : lua_module_backlist) { - if (str.find(s) != std::string_view::npos) + // black module + auto root = config::get_plugin_root(); + if (root) { + if (str.find((*root).string()) != std::string_view::npos) return false; } From 3e535358090780c5f2173ab6cb7e69ab43819653 Mon Sep 17 00:00:00 2001 From: fesily Date: Mon, 27 Mar 2023 10:17:06 +0800 Subject: [PATCH 28/51] update gumpp --- 3rd/frida_gum/gumpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rd/frida_gum/gumpp b/3rd/frida_gum/gumpp index 3fcb831c2..c02d1612f 160000 --- a/3rd/frida_gum/gumpp +++ b/3rd/frida_gum/gumpp @@ -1 +1 @@ -Subproject commit 3fcb831c2c6cfc4e8942c31a3dba04095cec55f9 +Subproject commit c02d1612f28263c2cd4e19882c77df496a90d43a From 9c02d9cb9285090ef3c7e5477b97e2918bed4fa5 Mon Sep 17 00:00:00 2001 From: fesily Date: Mon, 27 Mar 2023 10:20:53 +0800 Subject: [PATCH 29/51] optimize wait_dll --- src/launcher/autoattach/wait_dll.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/launcher/autoattach/wait_dll.cpp b/src/launcher/autoattach/wait_dll.cpp index 92bd1b4c3..fa1bc115b 100644 --- a/src/launcher/autoattach/wait_dll.cpp +++ b/src/launcher/autoattach/wait_dll.cpp @@ -91,7 +91,7 @@ namespace luadebug::autoattach { 0, [](LdrDllNotificationReason NotificationReason, PLDR_DLL_NOTIFICATION_DATA const NotificationData, PVOID Context) { if (NotificationReason == LdrDllNotificationReason::LDR_DLL_NOTIFICATION_REASON_LOADED) { auto path = fs::path(std::wstring(NotificationData->Loaded.FullDllName->Buffer, NotificationData->Loaded.FullDllName->Length)).string(); - auto ptr = (decltype(loaded)*)Context; + auto ptr = (WaitDllCallBack_t*)Context; if ((*ptr)(path)) { if (dllNotification.Cookie) dllNotification.dllUnregisterNotification(dllNotification.Cookie); @@ -99,7 +99,7 @@ namespace luadebug::autoattach { } } }, - (void*)new decltype(loaded)(loaded), &dllNotification.Cookie + (void*)new WaitDllCallBack_t(std::move(loaded)), &dllNotification.Cookie ); return true; } @@ -145,7 +145,7 @@ namespace luadebug::autoattach { if (!interceptor) return false; auto listener = new WaitDllListener; - listener->loaded = loaded; + listener->loaded = std::move(loaded); listener->interceptor = interceptor; return interceptor->attach((void*)infos.notification_address, listener, nullptr); } From 6b0fbdb3c7db423e6454ae09365e5759c1da9e34 Mon Sep 17 00:00:00 2001 From: fesily Date: Tue, 28 Mar 2023 09:49:53 +0800 Subject: [PATCH 30/51] fix blacklist --- src/launcher/autoattach/autoattach.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index b2cac5aac..911d327b2 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -13,6 +13,16 @@ namespace luadebug::autoattach { fn_attach debuggerAttach; +#ifdef _WIN32 +# define EXT ".dll" +#else +# define EXT ".so" +#endif + constexpr auto lua_module_backlist = { + "launcher" EXT, + "luadebug" EXT, + }; + constexpr auto find_lua_module_key = "lua_newstate"; constexpr auto lua_module_strings = std::array { "luaJIT_BC_%s", // luajit @@ -27,11 +37,16 @@ namespace luadebug::autoattach { }; static bool is_lua_module(const char* module_path, const config::Config& config, bool check_export = true, bool check_strings = false) { auto str = std::string_view(module_path); - // black module + // blacklist module auto root = config::get_plugin_root(); if (root) { - if (str.find((*root).string()) != std::string_view::npos) - return false; + if (str.find((*root).string()) != std::string_view::npos) { + // in luadebug root dir + for (auto& s : lua_module_backlist) { + if (str.find(s) != std::string_view::npos) + return false; + } + } } auto target_lua_module = config.lua_module; From a6b082a671effdf9dde955de314f83946694b9f6 Mon Sep 17 00:00:00 2001 From: fesily Date: Tue, 28 Mar 2023 13:24:07 +0800 Subject: [PATCH 31/51] fix miss debugger key --- extension/script/debugger.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index b61bcfb82..bc1a77787 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -123,6 +123,11 @@ local function detectLuaDebugPath(cfg) lua52 = "lua52", lua51 = "lua51", luajit = "luajit", + ["5.4"] = "lua54", + ["5.3"] = "lua53", + ["5.2"] = "lua52", + ["5.1"] = "lua51", + jit = 'luajit' } if not t[version] then error(version.." is not supported.") From 3b18cc6678ed7d581743e75a608ed499389d2d0b Mon Sep 17 00:00:00 2001 From: fesily Date: Wed, 29 Mar 2023 16:56:49 +0800 Subject: [PATCH 32/51] Update config.cpp --- src/launcher/config/config.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp index eeaafd256..80ff68577 100644 --- a/src/launcher/config/config.cpp +++ b/src/launcher/config/config.cpp @@ -17,7 +17,7 @@ namespace luadebug::config { const auto key = "version"sv; auto it = values.find(key); - if (!values || it == values.end()) { + if (it == values.end()) { return lua_version::unknown; } @@ -132,4 +132,4 @@ namespace luadebug::config { return (*runtime) / (LUADEBUG_FILE EXT); } -} // namespace luadebug::autoattach \ No newline at end of file +} // namespace luadebug::autoattach From b272ecb615face8298ed8cf777121c05976c0d6b Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 30 Mar 2023 13:46:20 +0800 Subject: [PATCH 33/51] fix launch mode --- src/launcher/autoattach/autoattach.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index 911d327b2..1c6be49b3 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -90,7 +90,6 @@ namespace luadebug::autoattach { auto config = config::init_from_file(); if (!config) { log::info("can't load config"); - return; } bool found = false; lua_module rm = {}; From e2b6068cc3dd4d02f3e1e5b34d14f4a5da30b99c Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 30 Mar 2023 13:49:46 +0800 Subject: [PATCH 34/51] fix launch config --- extension/script/frontend/debuger_factory.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extension/script/frontend/debuger_factory.lua b/extension/script/frontend/debuger_factory.lua index 577738cc4..49f862f93 100644 --- a/extension/script/frontend/debuger_factory.lua +++ b/extension/script/frontend/debuger_factory.lua @@ -221,6 +221,9 @@ local function create_process_in_console(args, callback) if not process then return nil, err end + if callback then + callback(process) + end if args.inject ~= "none" then local ok, errmsg = process_inject.inject(process, "launch", args) if not ok then @@ -231,9 +234,6 @@ local function create_process_in_console(args, callback) end end end - if callback then - callback(process) - end if need_resume then process:resume() end From 56bbaf60336638bc0fe053acf2ad8cb51eb0c097 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 30 Mar 2023 15:02:35 +0800 Subject: [PATCH 35/51] fix get_runtime_dir --- src/launcher/config/config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp index 80ff68577..2059dbb91 100644 --- a/src/launcher/config/config.cpp +++ b/src/launcher/config/config.cpp @@ -103,7 +103,7 @@ namespace luadebug::config { #elif defined(_M_IX86) || defined(__i386__) "x86"; #elif defined(_M_X64) || defined(__x86_64__) - "x86_64"; + "x64"; #else # error "Unknown architecture" #endif From 517b7b54a32561c45c82ae2571b6fe27e31f84bf Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 11:02:44 +0800 Subject: [PATCH 36/51] fix error --- src/launcher/autoattach/autoattach.cpp | 21 +++++++++++---------- src/launcher/autoattach/autoattach.h | 2 +- src/launcher/autoattach/ctx.h | 2 ++ src/launcher/autoattach/lua_module.cpp | 4 ++-- src/launcher/autoattach/lua_module.h | 1 - src/launcher/hook/create_watchdog.cpp | 2 -- src/launcher/hook/watchdog.cpp | 2 +- src/launcher/hook/watchdog.h | 2 -- src/launcher/main.cpp | 6 ++++-- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index e346591a9..9d78c51ba 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -34,7 +34,7 @@ namespace luadebug::autoattach { "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" " $", // others }; - static bool is_lua_module(const char* module_path, const config::Config& config, bool check_export = true, bool check_strings = false) { + static bool is_lua_module(const char* module_path, bool check_export = true, bool check_strings = false) { auto str = std::string_view(module_path); // blacklist module auto root = config::get_plugin_root(); @@ -48,7 +48,7 @@ namespace luadebug::autoattach { } } - auto target_lua_module = config.lua_module; + const auto& target_lua_module = ctx::get()->config->lua_module; if (!target_lua_module.empty() && str.find(target_lua_module) != std::string_view::npos) return true; if (check_export && Gum::Process::module_find_export_by_name(module_path, find_lua_module_key)) return true; @@ -65,7 +65,7 @@ namespace luadebug::autoattach { } static void start(); - static bool load_lua_module(const std::string& path, const config::Config& config) { + static bool load_lua_module(const std::string& path) { constexpr auto check_export = #ifdef _WIN32 true @@ -73,7 +73,7 @@ namespace luadebug::autoattach { false #endif ; - if (!is_lua_module(path.c_str(), config, check_export, true)) { + if (!is_lua_module(path.c_str(), check_export, true)) { return false; } // find lua module lazy @@ -93,10 +93,14 @@ namespace luadebug::autoattach { if (!config) { log::info("can't load config"); } + else { + ctx->config = *config; + } + bool found = false; lua_module rm = {}; - Gum::Process::enumerate_modules([&rm, &found, conf = &(*config)](const Gum::ModuleDetails& details) -> bool { - if (is_lua_module(details.path(), *conf)) { + Gum::Process::enumerate_modules([&rm, &found](const Gum::ModuleDetails& details) -> bool { + if (is_lua_module(details.path())) { auto range = details.range(); rm.memory_address = range.base_address; rm.memory_size = range.size; @@ -110,9 +114,7 @@ namespace luadebug::autoattach { if (!found) { if (ctx->wait_dll) return; - if (!wait_dll([conf = std::move(*config)](const std::string& path) { - return load_lua_module(path, conf); - })) { + if (!wait_dll(load_lua_module)) { log::fatal("can't find lua module"); } else { @@ -122,7 +124,6 @@ namespace luadebug::autoattach { } log::info("find lua module path:{}", rm.path); - rm.config = std::move(*config); if (!rm.initialize()) { return; } diff --git a/src/launcher/autoattach/autoattach.h b/src/launcher/autoattach/autoattach.h index 3df53aa6e..a3a4ffd28 100644 --- a/src/launcher/autoattach/autoattach.h +++ b/src/launcher/autoattach/autoattach.h @@ -13,6 +13,6 @@ namespace luadebug::autoattach { launch, attach, }; - attach_status attach_lua(lua::state L, lua_version verison); + attach_status attach_lua(lua::state L); void initialize(work_mode mode); } diff --git a/src/launcher/autoattach/ctx.h b/src/launcher/autoattach/ctx.h index 1d2a18dee..2da9a04ff 100644 --- a/src/launcher/autoattach/ctx.h +++ b/src/launcher/autoattach/ctx.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include @@ -15,6 +16,7 @@ namespace luadebug::autoattach { work_mode mode; std::optional lua_module; bool wait_dll = false; + std::optional config; static ctx* get() { static ctx obj; diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index 25aa66d15..71c444646 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -60,7 +61,6 @@ namespace luadebug::autoattach { } } - bool lua_module::initialize() { bool load_luadebug_dll(lua_version version) { auto luadebug_path = config::get_luadebug_path(version); if (!luadebug_path) @@ -80,7 +80,7 @@ namespace luadebug::autoattach { log::fatal("lua initialize failed, can't find {}", error_msg); return false; } - version = config.version; + version = ctx::get()->config->version; if (version == lua_version::unknown) { version = get_lua_version(*this); } diff --git a/src/launcher/autoattach/lua_module.h b/src/launcher/autoattach/lua_module.h index f6b4ba668..a11043afe 100644 --- a/src/launcher/autoattach/lua_module.h +++ b/src/launcher/autoattach/lua_module.h @@ -17,7 +17,6 @@ namespace luadebug::autoattach { lua_resolver resolver; work_mode mode; struct watchdog* watchdog = nullptr; - config::Config config; bool initialize(); ~lua_module(); diff --git a/src/launcher/hook/create_watchdog.cpp b/src/launcher/hook/create_watchdog.cpp index 7e6f2ea97..15a7bf915 100644 --- a/src/launcher/hook/create_watchdog.cpp +++ b/src/launcher/hook/create_watchdog.cpp @@ -64,7 +64,6 @@ namespace luadebug::autoattach { if (context->init_watch(resolver, get_watch_points(mode, version))) { // TODO: fix other thread pc context->hook(); - context->version = version; return context.release(); } if (version == lua_version::unknown) { @@ -75,7 +74,6 @@ namespace luadebug::autoattach { if (context->init_watch(resolver, get_watch_points(mode, lua_version::unknown))) { // TODO: fix other thread pc context->hook(); - context->version = version; return context.release(); } // TODO: more errmsg diff --git a/src/launcher/hook/watchdog.cpp b/src/launcher/hook/watchdog.cpp index 0587b7391..de4d37557 100644 --- a/src/launcher/hook/watchdog.cpp +++ b/src/launcher/hook/watchdog.cpp @@ -183,7 +183,7 @@ namespace luadebug::autoattach { void watchdog::attach_lua(lua::state L, lua::debug ar, lua::hook fn) { reset_luahook(L, ar); - switch (autoattach::attach_lua(L, version)) { + switch (autoattach::attach_lua(L)) { case attach_status::fatal: case attach_status::success: // TODO: how to free so diff --git a/src/launcher/hook/watchdog.h b/src/launcher/hook/watchdog.h index 04c7273d8..21652dc22 100644 --- a/src/launcher/hook/watchdog.h +++ b/src/launcher/hook/watchdog.h @@ -43,7 +43,5 @@ namespace luadebug::autoattach { std::set lua_state_hooked; uint8_t luahook_index; lua::hook luahook_func = nullptr; - public: - lua_version version; }; } diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index c9d430a6d..37dbc2dfc 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -35,7 +36,7 @@ namespace luadebug::autoattach { return tmp; } - static attach_status attach_lua(lua::state L, lua_version verison) { + attach_status attach_lua(lua::state L) { log::info("attach lua vm entry"); auto r = bee::path_helper::dll_path(); if (!r) { @@ -49,9 +50,10 @@ namespace luadebug::autoattach { lua::pop(L, 1); return attach_status::fatal; } + auto version = ctx::get()->lua_module->version; lua::call(L, root.generic_u8string().c_str()); lua::call(L, std::to_string(Gum::Process::get_id()).c_str()); - lua::call(L, verison == lua_version::unknown ? "" : lua_version_to_string(verison)); + lua::call(L, version == lua_version::unknown ? "" : lua_version_to_string(version)); if (lua::pcall(L, 3, 1, 0)) { /* From d7219c25727a203901ff939a65e5460e221e516a Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 11:03:26 +0800 Subject: [PATCH 37/51] Update proxy.lua --- extension/script/frontend/proxy.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/extension/script/frontend/proxy.lua b/extension/script/frontend/proxy.lua index cb7084c51..b0d9d7ec6 100644 --- a/extension/script/frontend/proxy.lua +++ b/extension/script/frontend/proxy.lua @@ -11,7 +11,6 @@ local initReq local m = {} local function getUnixAddress(pid) - --TODO: clear unix file local path = WORKDIR / "tmp" fs.create_directories(path) return "@"..(path / ("pid_%d"):format(pid)):string() From 9f9e3194fc6b702711e2b35a46c9ca897a29d0b2 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 11:05:45 +0800 Subject: [PATCH 38/51] format --- src/launcher/autoattach/autoattach.cpp | 7 ++----- src/launcher/config/config.h | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index 9d78c51ba..f7d13a791 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -89,13 +89,10 @@ namespace luadebug::autoattach { return; } - auto config = config::init_from_file(); - if (!config) { + ctx->config = config::init_from_file(); + if (!ctx->config) { log::info("can't load config"); } - else { - ctx->config = *config; - } bool found = false; lua_module rm = {}; diff --git a/src/launcher/config/config.h b/src/launcher/config/config.h index 4d8876e6c..b37e1b738 100644 --- a/src/launcher/config/config.h +++ b/src/launcher/config/config.h @@ -20,4 +20,4 @@ namespace luadebug::config { std::string lua_module; }; std::optional init_from_file(); -} // namespace luadebug::autoattach \ No newline at end of file +} // namespace luadebug::autoattach From d190e9baa5c7b0a65dec6009678ffcdb54ec3aa5 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 11:09:48 +0800 Subject: [PATCH 39/51] reset wait_dll --- src/launcher/autoattach/wait_dll.cpp | 16 ++++++++-------- src/launcher/autoattach/wait_dll.h | 8 ++------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/launcher/autoattach/wait_dll.cpp b/src/launcher/autoattach/wait_dll.cpp index fa1bc115b..dcef3cdeb 100644 --- a/src/launcher/autoattach/wait_dll.cpp +++ b/src/launcher/autoattach/wait_dll.cpp @@ -32,7 +32,7 @@ extern "C" bool gum_darwin_query_all_image_infos(mach_port_t task, _GumDarwinAll #endif namespace luadebug::autoattach { #ifdef _WIN32 - bool wait_dll(WaitDllCallBack_t loaded) { + bool wait_dll(bool (*loaded)(std::string const&)) { typedef struct _LDR_DLL_UNLOADED_NOTIFICATION_DATA { ULONG Flags; // Reserved. PCUNICODE_STRING FullDllName; // The full path name of the DLL module. @@ -91,19 +91,19 @@ namespace luadebug::autoattach { 0, [](LdrDllNotificationReason NotificationReason, PLDR_DLL_NOTIFICATION_DATA const NotificationData, PVOID Context) { if (NotificationReason == LdrDllNotificationReason::LDR_DLL_NOTIFICATION_REASON_LOADED) { auto path = fs::path(std::wstring(NotificationData->Loaded.FullDllName->Buffer, NotificationData->Loaded.FullDllName->Length)).string(); - auto ptr = (WaitDllCallBack_t*)Context; - if ((*ptr)(path)) { + auto f = (decltype(loaded))Context; + if (f(path)) { if (dllNotification.Cookie) dllNotification.dllUnregisterNotification(dllNotification.Cookie); - delete ptr; } } }, - (void*)new WaitDllCallBack_t(std::move(loaded)), &dllNotification.Cookie + loaded, &dllNotification.Cookie ); return true; } #elif defined(__APPLE__) + using WaitDllCallBack_t = bool (*)(std::string const&); struct WaitDllListener : Gum::NoLeaveInvocationListener { WaitDllCallBack_t loaded; Gum::RefPtr interceptor; @@ -145,7 +145,7 @@ namespace luadebug::autoattach { if (!interceptor) return false; auto listener = new WaitDllListener; - listener->loaded = std::move(loaded); + listener->loaded = loaded; listener->interceptor = interceptor; return interceptor->attach((void*)infos.notification_address, listener, nullptr); } @@ -153,8 +153,8 @@ namespace luadebug::autoattach { // TODO: support linux - bool wait_dll(WaitDllCallBack_t loaded) { + bool wait_dll(bool (*loaded)(std::string const&)) { return false; } #endif -} +} \ No newline at end of file diff --git a/src/launcher/autoattach/wait_dll.h b/src/launcher/autoattach/wait_dll.h index 0c56ef908..9e9eef392 100644 --- a/src/launcher/autoattach/wait_dll.h +++ b/src/launcher/autoattach/wait_dll.h @@ -1,11 +1,7 @@ #pragma once -#include - -#include #include namespace luadebug::autoattach { - using WaitDllCallBack_t = std::function; - bool wait_dll(WaitDllCallBack_t loaded); -} + bool wait_dll(bool (*loaded)(std::string const&)); +} \ No newline at end of file From 614be756c8e8c951f8818df594b20b2e6c8b3a99 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 11:12:06 +0800 Subject: [PATCH 40/51] reset wait_dll --- src/launcher/autoattach/wait_dll.cpp | 2 +- src/launcher/autoattach/wait_dll.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/launcher/autoattach/wait_dll.cpp b/src/launcher/autoattach/wait_dll.cpp index dcef3cdeb..5063c7bea 100644 --- a/src/launcher/autoattach/wait_dll.cpp +++ b/src/launcher/autoattach/wait_dll.cpp @@ -157,4 +157,4 @@ namespace luadebug::autoattach { return false; } #endif -} \ No newline at end of file +} diff --git a/src/launcher/autoattach/wait_dll.h b/src/launcher/autoattach/wait_dll.h index 9e9eef392..8ace7a505 100644 --- a/src/launcher/autoattach/wait_dll.h +++ b/src/launcher/autoattach/wait_dll.h @@ -4,4 +4,4 @@ namespace luadebug::autoattach { bool wait_dll(bool (*loaded)(std::string const&)); -} \ No newline at end of file +} From 47241f09406f4ba2d3bd23b7a1b0730c197ba5d1 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 11:16:45 +0800 Subject: [PATCH 41/51] format code --- src/launcher/autoattach/autoattach.cpp | 1 - src/launcher/autoattach/autoattach.h | 1 - src/launcher/autoattach/lua_module.h | 2 +- src/launcher/autoattach/lua_version.cpp | 2 +- src/launcher/autoattach/lua_version.h | 2 +- src/launcher/main.cpp | 2 +- 6 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index f7d13a791..f039e857e 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/src/launcher/autoattach/autoattach.h b/src/launcher/autoattach/autoattach.h index a3a4ffd28..e68f3185a 100644 --- a/src/launcher/autoattach/autoattach.h +++ b/src/launcher/autoattach/autoattach.h @@ -1,6 +1,5 @@ #pragma once -#include #include namespace luadebug::autoattach { diff --git a/src/launcher/autoattach/lua_module.h b/src/launcher/autoattach/lua_module.h index a11043afe..8d9b4aeab 100644 --- a/src/launcher/autoattach/lua_module.h +++ b/src/launcher/autoattach/lua_module.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include diff --git a/src/launcher/autoattach/lua_version.cpp b/src/launcher/autoattach/lua_version.cpp index c1cdba2b5..7efcd4931 100644 --- a/src/launcher/autoattach/lua_version.cpp +++ b/src/launcher/autoattach/lua_version.cpp @@ -34,4 +34,4 @@ namespace luadebug::autoattach { return lua_version::latest; return lua_version::unknown; } -} \ No newline at end of file +} diff --git a/src/launcher/autoattach/lua_version.h b/src/launcher/autoattach/lua_version.h index 406009d70..76d92e8a6 100644 --- a/src/launcher/autoattach/lua_version.h +++ b/src/launcher/autoattach/lua_version.h @@ -12,4 +12,4 @@ namespace luadebug::autoattach { }; const char* lua_version_to_string(lua_version v); lua_version lua_version_from_string(const std::string_view& v); -} \ No newline at end of file +} diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index 37dbc2dfc..5748d13f5 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -21,7 +21,7 @@ namespace luadebug::autoattach { #ifdef _WIN32 FILE *f = _wfopen(filename.c_str(), L"rb"); #else - FILE* f = fopen(filename.c_str(), "rb"); + FILE *f = fopen(filename.c_str(), "rb"); #endif if (!f) { return std::string(); From 63667ee06fb5ffc6f4c02b087bf894feb3ce2076 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 15:38:54 +0800 Subject: [PATCH 42/51] Update src/launcher/config/config.cpp Co-authored-by: actboy168 --- src/launcher/config/config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp index 2059dbb91..6ae393221 100644 --- a/src/launcher/config/config.cpp +++ b/src/launcher/config/config.cpp @@ -101,7 +101,7 @@ namespace luadebug::config { #if defined(_M_ARM64) || defined(__aarch64__) "arm64"; #elif defined(_M_IX86) || defined(__i386__) - "x86"; + "ia32"; #elif defined(_M_X64) || defined(__x86_64__) "x64"; #else From 2876ad79a29773942f86e290880094b087856292 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 15:49:31 +0800 Subject: [PATCH 43/51] format version --- extension/script/debugger.lua | 22 ++++++++++------------ src/launcher/autoattach/lua_module.cpp | 1 + src/launcher/autoattach/lua_version.cpp | 12 ++++++------ 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index bc1a77787..b6203a951 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -105,32 +105,30 @@ local function detectLuaDebugPath(cfg) local version = cfg.version if not version then if tostring(assert):match('builtin') ~= nil then - version = "jit" + version = "luajit" jit.off() else - version = _VERSION + local version_t ={ + ["Lua 5.4"] = "lua54", + ["Lua 5.3"] = "lua53", + ["Lua 5.2"] = "lua52", + ["Lua 5.1"] = "lua51", + } + version = version_t[_VERSION] end end local t = { - ["Lua 5.4"] = "lua54", - ["Lua 5.3"] = "lua53", - ["Lua 5.2"] = "lua52", - ["Lua 5.1"] = "lua51", latest = "lua-latest", lua54 = "lua54", lua53 = "lua53", lua52 = "lua52", lua51 = "lua51", luajit = "luajit", - ["5.4"] = "lua54", - ["5.3"] = "lua53", - ["5.2"] = "lua52", - ["5.1"] = "lua51", - jit = 'luajit' } + if not t[version] then - error(version.." is not supported.") + error(tostring(version).." is not supported.") end return t[version] end diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index 71c444646..ec824e921 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -70,6 +70,7 @@ namespace luadebug::autoattach { if (!Gum::Process::module_load(path.c_str(), &error)) { log::fatal("load debugger [{}] failed: {}", path, error); } + setenv("LUA_DEBUG_CORE", path.c_str(), true); return true; } diff --git a/src/launcher/autoattach/lua_version.cpp b/src/launcher/autoattach/lua_version.cpp index 7efcd4931..12d85c34b 100644 --- a/src/launcher/autoattach/lua_version.cpp +++ b/src/launcher/autoattach/lua_version.cpp @@ -20,17 +20,17 @@ namespace luadebug::autoattach { } } lua_version lua_version_from_string(const std::string_view& v) { - if (v == "jit" || v == "luajit") + if (v == "jit") return lua_version::luajit; - else if (v == "5.1" || v == "lua51") + else if (v == "5.1") return lua_version::lua51; - else if (v == "5.2" || v == "lua52") + else if (v == "5.2") return lua_version::lua52; - else if (v == "5.3" || v == "lua53") + else if (v == "5.3") return lua_version::lua53; - else if (v == "5.4" || v == "lua54") + else if (v == "5.4") return lua_version::lua54; - else if (v == "latest" || v == "lua-latest") + else if (v == "latest") return lua_version::latest; return lua_version::unknown; } From bee2b39fb0745b8cac3411e2bae086fb445368b2 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 15:56:39 +0800 Subject: [PATCH 44/51] format code --- extension/script/debugger.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index b6203a951..4fc57ea94 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -108,7 +108,7 @@ local function detectLuaDebugPath(cfg) version = "luajit" jit.off() else - local version_t ={ + local version_t = { ["Lua 5.4"] = "lua54", ["Lua 5.3"] = "lua53", ["Lua 5.2"] = "lua52", @@ -226,7 +226,7 @@ function dbg:event(...) end function dbg:set_wait(name, f) - _G[name] = function(...) + _G[name] = function (...) _G[name] = nil f(...) self:event 'wait' @@ -238,7 +238,7 @@ function dbg:setup_patch() local rawxpcall = xpcall function pcall(f, ...) return rawxpcall(f, - function(msg) + function (msg) self:event("exception", msg) return msg end, @@ -247,7 +247,7 @@ function dbg:setup_patch() function xpcall(f, msgh, ...) return rawxpcall(f, - function(msg) + function (msg) self:event("exception", msg) return msgh and msgh(msg) or msg end @@ -268,7 +268,7 @@ function dbg:setup_patch() function coroutine.wrap(f) local wf = rawcoroutinewrap(f) local _, co = debug.getupvalue(wf, 1) - return function(...) + return function (...) self:event("thread", co, 0) return coreturn(co, wf(...)) end From 6eb73dba90982968e65a97dae6f09bf5933cf513 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 16:10:57 +0800 Subject: [PATCH 45/51] fix windows --- src/launcher/autoattach/lua_module.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index ec824e921..8094846da 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -70,7 +70,11 @@ namespace luadebug::autoattach { if (!Gum::Process::module_load(path.c_str(), &error)) { log::fatal("load debugger [{}] failed: {}", path, error); } +#ifdef _WIN32 + _putenv("LUA_DEBUG_CORE", std::format("LUA_DEBUG_CORE={}", path).c_str()); +#else setenv("LUA_DEBUG_CORE", path.c_str(), true); +#endif return true; } From 51cac20ec3d8ae5423b8a15a97150238553d625e Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 16:15:03 +0800 Subject: [PATCH 46/51] fix err --- src/launcher/autoattach/lua_module.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index 8094846da..0e31d962a 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -71,7 +71,7 @@ namespace luadebug::autoattach { log::fatal("load debugger [{}] failed: {}", path, error); } #ifdef _WIN32 - _putenv("LUA_DEBUG_CORE", std::format("LUA_DEBUG_CORE={}", path).c_str()); + _putenv(std::format("LUA_DEBUG_CORE={}", path).c_str()); #else setenv("LUA_DEBUG_CORE", path.c_str(), true); #endif From 31cb1234f17d2b72cdc689bfae1b04375462ee7f Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 6 Apr 2023 16:37:25 +0800 Subject: [PATCH 47/51] add debugger_path --- extension/script/attach.lua | 7 ++++++- extension/script/debugger.lua | 2 +- src/launcher/autoattach/lua_module.cpp | 10 +++------- src/launcher/autoattach/lua_module.h | 1 + src/launcher/main.cpp | 1 + 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/extension/script/attach.lua b/extension/script/attach.lua index 7f85417ff..cc4261e02 100644 --- a/extension/script/attach.lua +++ b/extension/script/attach.lua @@ -1,4 +1,4 @@ -local path, pid, version = ... +local path, pid, version, debugger_path = ... if _VERSION == nil or type == nil or assert == nil @@ -23,6 +23,10 @@ if version == "" then version = nil end +if debugger_path == "" then + debugger_path = nil +end + local function dofile(filename, ...) local load = _VERSION == "Lua 5.1" and loadstring or load local f = assert(io.open(filename)) @@ -35,6 +39,7 @@ local dbg = dofile(path.."/script/debugger.lua", path) dbg:start { address = ("@%s/tmp/pid_%s"):format(path, pid), version = version, + debugger_path = debugger_path, } dbg:event "wait" return "ok" diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index 4fc57ea94..d1ba79990 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -147,7 +147,7 @@ local function initDebugger(dbg, cfg) local luadebug = os.getenv "LUA_DEBUG_CORE" local updateenv = false if not luadebug then - luadebug = detectLuaDebugPath(cfg) + luadebug = cfg.debugger_path or detectLuaDebugPath(cfg) updateenv = true end local isWindows = package.config:sub(1, 1) == "\\" diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index 0e31d962a..8a928d0d7 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -61,7 +61,7 @@ namespace luadebug::autoattach { } } - bool load_luadebug_dll(lua_version version) { + bool load_luadebug_dll(lua_module* lm, lua_version version) { auto luadebug_path = config::get_luadebug_path(version); if (!luadebug_path) return false; @@ -70,11 +70,7 @@ namespace luadebug::autoattach { if (!Gum::Process::module_load(path.c_str(), &error)) { log::fatal("load debugger [{}] failed: {}", path, error); } -#ifdef _WIN32 - _putenv(std::format("LUA_DEBUG_CORE={}", path).c_str()); -#else - setenv("LUA_DEBUG_CORE", path.c_str(), true); -#endif + lm->debugger_path = path; return true; } @@ -93,7 +89,7 @@ namespace luadebug::autoattach { watchdog = create_watchdog(mode, version, resolver); if (version != lua_version::unknown) { - if (!load_luadebug_dll(version)) + if (!load_luadebug_dll(this, version)) return false; } if (!watchdog) { diff --git a/src/launcher/autoattach/lua_module.h b/src/launcher/autoattach/lua_module.h index aae9b7476..eab90a882 100644 --- a/src/launcher/autoattach/lua_module.h +++ b/src/launcher/autoattach/lua_module.h @@ -17,6 +17,7 @@ namespace luadebug::autoattach { lua_resolver resolver; work_mode mode; struct watchdog* watchdog = nullptr; + std::string debugger_path; lua_module(work_mode mode) : mode(mode) {} diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index 5748d13f5..206ee8b34 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -54,6 +54,7 @@ namespace luadebug::autoattach { lua::call(L, root.generic_u8string().c_str()); lua::call(L, std::to_string(Gum::Process::get_id()).c_str()); lua::call(L, version == lua_version::unknown ? "" : lua_version_to_string(version)); + lua::call(L, ctx::get()->lua_module->debugger_path.c_str()); if (lua::pcall(L, 3, 1, 0)) { /* From ff4b5fc68d2c0aa863deea7ebe0a937969050cff Mon Sep 17 00:00:00 2001 From: fesily Date: Fri, 7 Apr 2023 13:01:46 +0800 Subject: [PATCH 48/51] fix version --- extension/script/attach.lua | 7 +------ extension/script/debugger.lua | 18 +++++++++--------- src/launcher/autoattach/lua_version.cpp | 12 ++++++------ src/launcher/main.cpp | 2 -- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/extension/script/attach.lua b/extension/script/attach.lua index cc4261e02..a94759f8a 100644 --- a/extension/script/attach.lua +++ b/extension/script/attach.lua @@ -1,4 +1,4 @@ -local path, pid, version, debugger_path = ... +local path, pid, debugger_path = ... if _VERSION == nil or type == nil or assert == nil @@ -19,10 +19,6 @@ if is_luajit and jit == nil then return "wait initialized" end -if version == "" then - version = nil -end - if debugger_path == "" then debugger_path = nil end @@ -38,7 +34,6 @@ end local dbg = dofile(path.."/script/debugger.lua", path) dbg:start { address = ("@%s/tmp/pid_%s"):format(path, pid), - version = version, debugger_path = debugger_path, } dbg:event "wait" diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index d1ba79990..6cb388361 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -118,19 +118,19 @@ local function detectLuaDebugPath(cfg) end end - local t = { - latest = "lua-latest", - lua54 = "lua54", - lua53 = "lua53", - lua52 = "lua52", - lua51 = "lua51", - luajit = "luajit", + local valid = { + ["lua-latest"] = true, + lua54 = true, + lua53 = true, + lua52 = true, + lua51 = true, + luajit = true, } - if not t[version] then + if not valid[version] then error(tostring(version).." is not supported.") end - return t[version] + return version end local rt = "/runtime/"..PLATFORM.."/"..get_luadebug_dir() diff --git a/src/launcher/autoattach/lua_version.cpp b/src/launcher/autoattach/lua_version.cpp index 12d85c34b..554f9364b 100644 --- a/src/launcher/autoattach/lua_version.cpp +++ b/src/launcher/autoattach/lua_version.cpp @@ -20,17 +20,17 @@ namespace luadebug::autoattach { } } lua_version lua_version_from_string(const std::string_view& v) { - if (v == "jit") + if (v == "luajit") return lua_version::luajit; - else if (v == "5.1") + else if (v == "lua51") return lua_version::lua51; - else if (v == "5.2") + else if (v == "lua52") return lua_version::lua52; - else if (v == "5.3") + else if (v == "lua53") return lua_version::lua53; - else if (v == "5.4") + else if (v == "lua54") return lua_version::lua54; - else if (v == "latest") + else if (v == "lua-latest") return lua_version::latest; return lua_version::unknown; } diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index 206ee8b34..31ca08552 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -50,10 +50,8 @@ namespace luadebug::autoattach { lua::pop(L, 1); return attach_status::fatal; } - auto version = ctx::get()->lua_module->version; lua::call(L, root.generic_u8string().c_str()); lua::call(L, std::to_string(Gum::Process::get_id()).c_str()); - lua::call(L, version == lua_version::unknown ? "" : lua_version_to_string(version)); lua::call(L, ctx::get()->lua_module->debugger_path.c_str()); if (lua::pcall(L, 3, 1, 0)) { From 0de91ca5563968089def7a0233f3da0ee8c9dbba Mon Sep 17 00:00:00 2001 From: fesily Date: Fri, 7 Apr 2023 14:47:51 +0800 Subject: [PATCH 49/51] reset debugger --- extension/script/debugger.lua | 48 +++++++++++++---------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/extension/script/debugger.lua b/extension/script/debugger.lua index 6cb388361..381d902c0 100644 --- a/extension/script/debugger.lua +++ b/extension/script/debugger.lua @@ -101,40 +101,26 @@ local function detectLuaDebugPath(cfg) end end - local function get_luadebug_dir() - local version = cfg.version - if not version then - if tostring(assert):match('builtin') ~= nil then - version = "luajit" - jit.off() - else - local version_t = { - ["Lua 5.4"] = "lua54", - ["Lua 5.3"] = "lua53", - ["Lua 5.2"] = "lua52", - ["Lua 5.1"] = "lua51", - } - version = version_t[_VERSION] - end - end - - local valid = { - ["lua-latest"] = true, - lua54 = true, - lua53 = true, - lua52 = true, - lua51 = true, - luajit = true, - } - - if not valid[version] then - error(tostring(version).." is not supported.") + local rt = "/runtime/"..PLATFORM + if cfg.latest then + rt = rt.."/lua-latest" + elseif _VERSION == "Lua 5.4" then + rt = rt.."/lua54" + elseif _VERSION == "Lua 5.3" then + rt = rt.."/lua53" + elseif _VERSION == "Lua 5.2" then + rt = rt.."/lua52" + elseif _VERSION == "Lua 5.1" then + if (tostring(assert):match('builtin') ~= nil) then + rt = rt.."/luajit" + jit.off() + else + rt = rt.."/lua51" end - return version + else + error(_VERSION.." is not supported.") end - local rt = "/runtime/"..PLATFORM.."/"..get_luadebug_dir() - local ext = isWindows() and "dll" or "so" return root..rt..'/luadebug.'..ext end From a4c28086e8ac82386601de24ebd0722ddbe82a3b Mon Sep 17 00:00:00 2001 From: actboy168 Date: Fri, 7 Apr 2023 15:32:58 +0800 Subject: [PATCH 50/51] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/launcher/autoattach/autoattach.cpp | 59 ++++++----- src/launcher/autoattach/autoattach.h | 3 +- src/launcher/autoattach/ctx.h | 7 +- src/launcher/autoattach/lua_module.cpp | 7 +- src/launcher/autoattach/session.cpp | 72 +++++++++++++ src/launcher/autoattach/session.h | 19 ++++ src/launcher/config/config.cpp | 135 ------------------------- src/launcher/config/config.h | 23 ----- src/launcher/hook/create_watchdog.cpp | 10 +- src/launcher/hook/create_watchdog.h | 2 +- src/launcher/hook/watchdog.cpp | 7 +- src/launcher/hook/watchdog.h | 5 +- src/launcher/main.cpp | 4 +- src/launcher/util/log.cpp | 5 +- src/launcher/util/path.cpp | 68 +++++++++++++ src/launcher/util/path.h | 14 +++ 16 files changed, 228 insertions(+), 212 deletions(-) create mode 100644 src/launcher/autoattach/session.cpp create mode 100644 src/launcher/autoattach/session.h delete mode 100644 src/launcher/config/config.cpp delete mode 100644 src/launcher/config/config.h create mode 100644 src/launcher/util/path.cpp create mode 100644 src/launcher/util/path.h diff --git a/src/launcher/autoattach/autoattach.cpp b/src/launcher/autoattach/autoattach.cpp index cafcae996..0c88ab1cf 100644 --- a/src/launcher/autoattach/autoattach.cpp +++ b/src/launcher/autoattach/autoattach.cpp @@ -1,8 +1,9 @@ #include #include +#include #include -#include #include +#include #include #include @@ -33,10 +34,11 @@ namespace luadebug::autoattach { "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" " $", // others }; - static bool is_lua_module(const char* module_path, bool check_export = true, bool check_strings = false) { + + static bool is_lua_module(session& session, const char* module_path, bool check_export, bool check_strings) { auto str = std::string_view(module_path); // blacklist module - auto root = config::get_plugin_root(); + auto root = get_plugin_root(); if (root) { if (str.find((*root).string()) != std::string_view::npos) { // in luadebug root dir @@ -46,10 +48,8 @@ namespace luadebug::autoattach { } } } - - const auto& target_lua_module = ctx::get()->config->lua_module; + const auto& target_lua_module = session.lua_module; if (!target_lua_module.empty() && str.find(target_lua_module) != std::string_view::npos) return true; - if (check_export && Gum::Process::module_find_export_by_name(module_path, find_lua_module_key)) return true; if (Gum::Process::module_find_symbol_by_name(module_path, find_lua_module_key)) return true; // TODO: when signature mode, check strings @@ -72,7 +72,7 @@ namespace luadebug::autoattach { false #endif ; - if (!is_lua_module(path.c_str(), check_export, true)) { + if (!is_lua_module(*ctx::get()->session, path.c_str(), check_export, true)) { return false; } // find lua module lazy @@ -84,27 +84,22 @@ namespace luadebug::autoattach { if (ctx->lua_module) { return; } - - ctx->config = config::init_from_file(); - if (!ctx->config) { - log::info("can't load config"); - } - - bool found = false; - lua_module rm(ctx->mode); - Gum::Process::enumerate_modules([&rm, &found](const Gum::ModuleDetails& details) -> bool { - if (is_lua_module(details.path())) { - auto range = details.range(); - rm.memory_address = range.base_address; - rm.memory_size = range.size; - rm.path = details.path(); - rm.name = details.name(); - found = true; + Gum::Process::enumerate_modules([&ctx](const Gum::ModuleDetails& details) -> bool { + auto& session = *ctx->session; + if (is_lua_module(session, details.path(), true, false)) { + ctx->lua_module = lua_module { session.mode }; + auto range = details.range(); + ctx->lua_module->memory_address = range.base_address; + ctx->lua_module->memory_size = range.size; + ctx->lua_module->path = details.path(); + ctx->lua_module->name = details.name(); + ctx->lua_module->version = session.version; + ctx->session.reset(); return false; } return true; }); - if (!found) { + if (!ctx->lua_module) { if (ctx->wait_dll) return; if (!wait_dll(load_lua_module)) { @@ -116,23 +111,27 @@ namespace luadebug::autoattach { return; } - log::info("find lua module path:{}", rm.path); - if (!rm.initialize()) { + log::info("find lua module path:{}", ctx->lua_module->path); + if (!ctx->lua_module->initialize()) { + ctx->lua_module.reset(); return; } - ctx->lua_module = std::move(rm); } - void start(work_mode mode) { + void start() { auto ctx = ctx::get(); std::lock_guard guard(ctx->mtx); - ctx->mode = mode; start_impl(ctx); } - void start() { + void start(work_mode mode) { auto ctx = ctx::get(); std::lock_guard guard(ctx->mtx); + ctx->session = init_session(mode); + if (!ctx->session) { + log::fatal("can't load config"); + return; + } start_impl(ctx); } diff --git a/src/launcher/autoattach/autoattach.h b/src/launcher/autoattach/autoattach.h index e68f3185a..b74490107 100644 --- a/src/launcher/autoattach/autoattach.h +++ b/src/launcher/autoattach/autoattach.h @@ -12,6 +12,7 @@ namespace luadebug::autoattach { launch, attach, }; - attach_status attach_lua(lua::state L); + struct lua_module; + attach_status attach_lua(lua::state L, lua_module &module); void initialize(work_mode mode); } diff --git a/src/launcher/autoattach/ctx.h b/src/launcher/autoattach/ctx.h index 2da9a04ff..6bf7cb358 100644 --- a/src/launcher/autoattach/ctx.h +++ b/src/launcher/autoattach/ctx.h @@ -1,7 +1,7 @@ #pragma once #include #include -#include +#include #include #include @@ -13,10 +13,9 @@ namespace luadebug::autoattach { struct ctx { std::mutex mtx; - work_mode mode; - std::optional lua_module; bool wait_dll = false; - std::optional config; + std::optional lua_module; + std::optional session; static ctx* get() { static ctx obj; diff --git a/src/launcher/autoattach/lua_module.cpp b/src/launcher/autoattach/lua_module.cpp index 8a928d0d7..a2ad8b77e 100644 --- a/src/launcher/autoattach/lua_module.cpp +++ b/src/launcher/autoattach/lua_module.cpp @@ -2,10 +2,10 @@ #include #include #include -#include #include #include #include +#include #include #include @@ -62,7 +62,7 @@ namespace luadebug::autoattach { } bool load_luadebug_dll(lua_module* lm, lua_version version) { - auto luadebug_path = config::get_luadebug_path(version); + auto luadebug_path = get_luadebug_path(version); if (!luadebug_path) return false; auto path = (*luadebug_path).string(); @@ -81,13 +81,12 @@ namespace luadebug::autoattach { log::fatal("lua initialize failed, can't find {}", error_msg); return false; } - version = ctx::get()->config->version; if (version == lua_version::unknown) { version = get_lua_version(*this); } log::info("current lua version: {}", lua_version_to_string(version)); - watchdog = create_watchdog(mode, version, resolver); + watchdog = create_watchdog(*this); if (version != lua_version::unknown) { if (!load_luadebug_dll(this, version)) return false; diff --git a/src/launcher/autoattach/session.cpp b/src/launcher/autoattach/session.cpp new file mode 100644 index 000000000..db354f429 --- /dev/null +++ b/src/launcher/autoattach/session.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std::string_view_literals; + +namespace luadebug::autoattach { + static lua_version get_lua_version(nlohmann::json& values) { + const auto key = "version"sv; + auto it = values.find(key); + if (it == values.end()) { + return lua_version::unknown; + } + return lua_version_from_string(it->get()); + } + + static std::string get_lua_module(nlohmann::json& values) { + const auto key = "module"sv; + + auto it = values.find(key); + if (it == values.end()) { + return {}; + } + auto value = it->get(); + if (!fs::exists(value)) + return value; + + do { + if (!fs::is_symlink(value)) + return value; + auto link = fs::read_symlink(value); + if (link.is_absolute()) + value = link.string(); + else + value = (fs::path(value).parent_path() / link).lexically_normal().string(); + } while (true); + } + + std::optional init_session(work_mode mode) { + session session; + nlohmann::json values; + + auto tmp = get_tmp_dir(); + if (!tmp) + return std::nullopt; + auto filename = ((*tmp) / std::format("ipc_{}_config", Gum::Process::get_id())).string(); + + std::ifstream s(filename, s.binary); + if (!s.is_open()) + return std::nullopt; + try { + s >> values; + } catch (const nlohmann::json::exception& e) { + log::info("init_from_file error: {}", e.what()); + } + + session.mode = mode; + session.version = get_lua_version(values); + session.lua_module = get_lua_module(values); + + return session; + } +} // namespace luadebug::autoattach diff --git a/src/launcher/autoattach/session.h b/src/launcher/autoattach/session.h new file mode 100644 index 000000000..9c382ce4e --- /dev/null +++ b/src/launcher/autoattach/session.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include +#include + +namespace luadebug::autoattach { + struct session { + work_mode mode; + lua_version version = lua_version::unknown; + std::string lua_module; + }; + std::optional init_session(work_mode mode); +} diff --git a/src/launcher/config/config.cpp b/src/launcher/config/config.cpp deleted file mode 100644 index 6ae393221..000000000 --- a/src/launcher/config/config.cpp +++ /dev/null @@ -1,135 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -using namespace std::string_view_literals; -namespace luadebug::config { - using namespace autoattach; - static lua_version get_lua_version(nlohmann::json& values) { - const auto key = "version"sv; - - auto it = values.find(key); - if (it == values.end()) { - return lua_version::unknown; - } - - return lua_version_from_string(it->get()); - } - - static std::string get_lua_module(nlohmann::json& values) { - const auto key = "module"sv; - - auto it = values.find(key); - if (it == values.end()) { - return {}; - } - auto value = it->get(); - if (!fs::exists(value)) - return value; - - do { - if (!fs::is_symlink(value)) - return value; - auto link = fs::read_symlink(value); - if (link.is_absolute()) - value = link.string(); - else - value = (fs::path(value).parent_path() / link).lexically_normal().string(); - } while (true); - } - - std::optional init_from_file() { - Config config; - nlohmann::json values; - - auto tmp = get_tmp_dir(); - if (!tmp) - return std::nullopt; - auto filename = ((*tmp) / std::format("ipc_{}_config", Gum::Process::get_id())).string(); - - std::ifstream s(filename, s.binary); - if (!s.is_open()) - return std::nullopt; - try { - s >> values; - } catch (const nlohmann::json::exception& e) { - log::info("init_from_file error: {}", e.what()); - } - - config.version = get_lua_version(values); - config.lua_module = get_lua_module(values); - - return config; - } - - std::optional get_plugin_root() { - auto dllpath = bee::path_helper::dll_path(); - if (!dllpath) { - return std::nullopt; - } - return dllpath.value().parent_path().parent_path(); - } - - std::optional get_tmp_dir() { - auto root = get_plugin_root(); - if (!root) - return std::nullopt; - return (*root) / "tmp"; - } - - std::optional get_runtime_dir() { - auto root = get_plugin_root(); - if (!root) - return std::nullopt; - auto os = -#if defined(_WIN32) - "windows"; -#elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) - "darwin"; -#else - "linux"; -#endif - auto arch = -#if defined(_M_ARM64) || defined(__aarch64__) - "arm64"; -#elif defined(_M_IX86) || defined(__i386__) - "ia32"; -#elif defined(_M_X64) || defined(__x86_64__) - "x64"; -#else -# error "Unknown architecture" -#endif - auto platform = std::format("{}-{}", os, arch); - return (*root) / "runtime" / platform; - } - - std::optional get_lua_runtime_dir(lua_version version) { - auto runtime = get_runtime_dir(); - if (!runtime) - return std::nullopt; - return (*runtime) / lua_version_to_string(version); - } - - std::optional get_luadebug_path(lua_version version) { - auto runtime = get_lua_runtime_dir(version); - if (!runtime) - return std::nullopt; -#define LUADEBUG_FILE "luadebug" - -#if defined(_WIN32) -# define EXT ".dll" -#else -# define EXT ".so" -#endif - return (*runtime) / (LUADEBUG_FILE EXT); - } - -} // namespace luadebug::autoattach diff --git a/src/launcher/config/config.h b/src/launcher/config/config.h deleted file mode 100644 index b37e1b738..000000000 --- a/src/launcher/config/config.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include -#include - -#include -#include -#include -#include -#include - -namespace luadebug::config { - using lua_version = autoattach::lua_version; - std::optional get_plugin_root(); - std::optional get_tmp_dir(); - std::optional get_runtime_dir(); - std::optional get_lua_runtime_dir(lua_version version); - std::optional get_luadebug_path(lua_version version); - struct Config { - lua_version version = lua_version::unknown; - std::string lua_module; - }; - std::optional init_from_file(); -} // namespace luadebug::autoattach diff --git a/src/launcher/hook/create_watchdog.cpp b/src/launcher/hook/create_watchdog.cpp index 4ee080d73..d2f183a8f 100644 --- a/src/launcher/hook/create_watchdog.cpp +++ b/src/launcher/hook/create_watchdog.cpp @@ -56,22 +56,22 @@ namespace luadebug::autoattach { } } - watchdog* create_watchdog(work_mode mode, lua_version version, const lua::resolver& resolver) { - auto context = std::make_unique(); + watchdog* create_watchdog(lua_module& module) { + auto context = std::make_unique(module); if (!context->init()) { return nullptr; } - if (context->init_watch(resolver, get_watch_points(mode, version))) { + if (context->init_watch(module.resolver, get_watch_points(module.mode, module.version))) { // TODO: fix other thread pc context->hook(); return context.release(); } - if (version == lua_version::unknown) { + if (module.version == lua_version::unknown) { // TODO: more errmsg log::fatal("watchdog initialize failed"); return nullptr; } - if (context->init_watch(resolver, get_watch_points(mode, lua_version::unknown))) { + if (context->init_watch(module.resolver, get_watch_points(module.mode, lua_version::unknown))) { // TODO: fix other thread pc context->hook(); return context.release(); diff --git a/src/launcher/hook/create_watchdog.h b/src/launcher/hook/create_watchdog.h index 0b493546b..de1a50cee 100644 --- a/src/launcher/hook/create_watchdog.h +++ b/src/launcher/hook/create_watchdog.h @@ -11,5 +11,5 @@ namespace luadebug::lua { namespace luadebug::autoattach { struct watchdog; - watchdog* create_watchdog(work_mode mode, lua_version v, const lua::resolver& resolver); + watchdog* create_watchdog(lua_module& module); } diff --git a/src/launcher/hook/watchdog.cpp b/src/launcher/hook/watchdog.cpp index 7004a1c34..6afb5487a 100644 --- a/src/launcher/hook/watchdog.cpp +++ b/src/launcher/hook/watchdog.cpp @@ -132,8 +132,9 @@ namespace luadebug::autoattach { } }; - watchdog::watchdog() - : interceptor { Gum::Interceptor_obtain() } {} + watchdog::watchdog(struct lua_module& lua_module) + : lua_module(lua_module) + , interceptor { Gum::Interceptor_obtain() } {} watchdog::~watchdog() { if (luahook_set) { trampoline::destroy(this, luahook_index); @@ -203,7 +204,7 @@ namespace luadebug::autoattach { } void watchdog::attach_lua(lua::state L, lua::debug ar) { - switch (autoattach::attach_lua(L)) { + switch (autoattach::attach_lua(L, lua_module)) { case attach_status::fatal: case attach_status::success: // TODO: how to free so diff --git a/src/launcher/hook/watchdog.h b/src/launcher/hook/watchdog.h index 9e360edc8..fd209ad4c 100644 --- a/src/launcher/hook/watchdog.h +++ b/src/launcher/hook/watchdog.h @@ -12,11 +12,13 @@ #include namespace luadebug::autoattach { + struct lua_module; + struct watchdog { static_assert(std::is_same_v); public: - watchdog(); + watchdog(struct lua_module& lua_module); ~watchdog(); watchdog(const watchdog&) = delete; bool init(); @@ -27,6 +29,7 @@ namespace luadebug::autoattach { void attach_lua(lua::state L, lua::debug ar); private: + struct lua_module& lua_module; std::mutex mtx; std::vector watch_points; Gum::RefPtr interceptor; diff --git a/src/launcher/main.cpp b/src/launcher/main.cpp index 31ca08552..4487bada9 100644 --- a/src/launcher/main.cpp +++ b/src/launcher/main.cpp @@ -36,7 +36,7 @@ namespace luadebug::autoattach { return tmp; } - attach_status attach_lua(lua::state L) { + attach_status attach_lua(lua::state L, lua_module &module) { log::info("attach lua vm entry"); auto r = bee::path_helper::dll_path(); if (!r) { @@ -52,7 +52,7 @@ namespace luadebug::autoattach { } lua::call(L, root.generic_u8string().c_str()); lua::call(L, std::to_string(Gum::Process::get_id()).c_str()); - lua::call(L, ctx::get()->lua_module->debugger_path.c_str()); + lua::call(L, module.debugger_path.c_str()); if (lua::pcall(L, 3, 1, 0)) { /* diff --git a/src/launcher/util/log.cpp b/src/launcher/util/log.cpp index 3efa37837..9eacfa34e 100644 --- a/src/launcher/util/log.cpp +++ b/src/launcher/util/log.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -16,8 +17,6 @@ # include #endif -#include - #include namespace luadebug::log { @@ -55,7 +54,7 @@ namespace luadebug::log { } void notify_frontend(const std::string& msg) { - auto tmp = config::get_tmp_dir(); + auto tmp = get_tmp_dir(); if (!tmp) return; auto path = ((*tmp) / std::format("pid_{}", Gum::Process::get_id())).string(); diff --git a/src/launcher/util/path.cpp b/src/launcher/util/path.cpp new file mode 100644 index 000000000..e4031c283 --- /dev/null +++ b/src/launcher/util/path.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +namespace luadebug { + std::optional get_plugin_root() { + auto dllpath = bee::path_helper::dll_path(); + if (!dllpath) { + return std::nullopt; + } + return dllpath.value().parent_path().parent_path(); + } + + std::optional get_tmp_dir() { + auto root = get_plugin_root(); + if (!root) + return std::nullopt; + return (*root) / "tmp"; + } + + std::optional get_runtime_dir() { + auto root = get_plugin_root(); + if (!root) + return std::nullopt; + auto os = +#if defined(_WIN32) + "windows"; +#elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) + "darwin"; +#else + "linux"; +#endif + auto arch = +#if defined(_M_ARM64) || defined(__aarch64__) + "arm64"; +#elif defined(_M_IX86) || defined(__i386__) + "ia32"; +#elif defined(_M_X64) || defined(__x86_64__) + "x64"; +#else +# error "Unknown architecture" +#endif + auto platform = std::format("{}-{}", os, arch); + return (*root) / "runtime" / platform; + } + + std::optional get_lua_runtime_dir(autoattach::lua_version version) { + auto runtime = get_runtime_dir(); + if (!runtime) + return std::nullopt; + return (*runtime) / autoattach::lua_version_to_string(version); + } + + std::optional get_luadebug_path(autoattach::lua_version version) { + auto runtime = get_lua_runtime_dir(version); + if (!runtime) + return std::nullopt; +#define LUADEBUG_FILE "luadebug" + +#if defined(_WIN32) +# define EXT ".dll" +#else +# define EXT ".so" +#endif + return (*runtime) / (LUADEBUG_FILE EXT); + } + +} diff --git a/src/launcher/util/path.h b/src/launcher/util/path.h new file mode 100644 index 000000000..49c17fae7 --- /dev/null +++ b/src/launcher/util/path.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +#include + +namespace luadebug { + std::optional get_plugin_root(); + std::optional get_tmp_dir(); + std::optional get_runtime_dir(); + std::optional get_lua_runtime_dir(autoattach::lua_version version); + std::optional get_luadebug_path(autoattach::lua_version version); +} From 20f27619ba87587da8f60d83e01460be353de77c Mon Sep 17 00:00:00 2001 From: actboy168 Date: Fri, 7 Apr 2023 15:39:42 +0800 Subject: [PATCH 51/51] fixes bug --- src/launcher/autoattach/ctx.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/launcher/autoattach/ctx.h b/src/launcher/autoattach/ctx.h index 6bf7cb358..cb711d14c 100644 --- a/src/launcher/autoattach/ctx.h +++ b/src/launcher/autoattach/ctx.h @@ -15,7 +15,7 @@ namespace luadebug::autoattach { std::mutex mtx; bool wait_dll = false; std::optional lua_module; - std::optional session; + std::optional session; static ctx* get() { static ctx obj;