vscode emmy_lua调试OpenResty lua代码

背景

我是windows下的vscode远程开发linux下的openresty项目,现需要调试lua代码,因此vscode安装了emmy_lua 0.5.3到远程服务器

过程

在vscode里安装最新版的emmy_lua,但是调试时候报version `GLIBC_2.28' not found

就是emmy_core.so需要高版本的gclib,加入emmy_lua群后热心的大佬说自己编译debugger,步骤如下

 git clone https://github.com/EmmyLua/EmmyLuaDebugger.git
 cd EmmyLuaDebugger/
 cmake .
 make && make install
 # 这个目录在vscode中安装了扩展后就会有
 mv /root/.vscode-server/extensions/tangzx.emmylua-0.5.3/debugger/emmy/linux/emmy_core.so /root/.vscode-server/extensions/tangzx.emmylua-0.5.3/debugger/emmy/linux/emmy_core.so.bak
 cd emmy_core/
 cp emmy_core.so /root/.vscode-server/extensions/tangzx.emmylua-0.5.3/debugger/emmy/linux

vscode launch.json配置

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "emmylua_new",
            "request": "launch",
            "name": "EmmyLua New Debug",
            "host": "localhost",
            "port": 9955,
            "ext": [
                ".lua",
                ".lua.txt",
                ".lua.bytes"
            ],
            "ideConnectDebugger": false
        },

        {
            "name": "Open Resty",
            "type": "cppdbg",
            "request": "launch",
            "program": "/learn/openresty/install/bin/openresty",//需要配置
            "cwd": "/learn/openresty/example/openresty/simplehttp",
            "args": ["-c", "/learn/openresty/example/openresty/simplehttp/conf/nginx.conf"],
            "stopAtEntry": false,
            "environment": [],
            "externalConsole": false,//可以配置,这边不能搞成true
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
        }
    ]
}

当然在能正常调试前,还需要做些准备工作

worker_processes 1;
events {
  worker_connections 512;
}
daemon off;
master_process off;

http {
  # 此处配置init.lua
  init_by_lua_file  /learn/openresty/example/openresty/simplehttp/service/http/init.lua;
  server {
    listen 80;
    server_name *.*;
   location = /example {
         # 仅有rewrite是不行的。需要有content返回,否则404
         rewrite_by_lua_file /learn/openresty/example/openresty/simplehttp/service/http/rewrite_example.lua;
         content_by_lua_file /learn/openresty/example/openresty/simplehttp/service/http/content_example.lua;
            
     }
    location /test {
      content_by_lua_block {
        ngx.say("hello, world1")
      }
    }
  }
}

init.lua如下

package.cpath = package.cpath .. ";/root/.vscode-server/extensions/tangzx.emmylua-0.5.3/debugger/emmy/linux/emmy_core.so"
-- 这边引入了emmy_core,我们重新编译的就是这个
local dbg = require("emmy_core")
-- 连接到emmy debugger监听的端口
dbg.tcpConnect("localhost", 9966)

按照上面的配置,那么调试的步骤是先启动emmy_lua_new, 监听9966端口,再启动OpenResty。
在rewrite_example.lua文件中设置断点,访问localhost/example即可进入断点:

posted @ 2022-07-02 12:57  泛舟瓦尔登湖  阅读(1786)  评论(1编辑  收藏  举报