美文网首页
lldb 调试electron渲染进程下的插件

lldb 调试electron渲染进程下的插件

作者: ape_caesar | 来源:发表于2022-10-20 18:33 被阅读0次

    写在前面

    提问:最好的debug方式是什么?
    正确答案: console.log, print, std::cout <<, System.out.println (狗头)

    lldb是一个macos默认使用的一个调试器, LLDB 能够逐行调试程序,使开发者能够了解程序的变量值以及堆栈是如何变化的

    这个东西同样可以用来调试nodejs的原生插件,就是.node后缀的那些文件,node的原生插件是可以用不止c/c++语言编辑并编译出的动态链接库,类似.lib, .dylib等等,lldb可以调试他;

    既然能够调试node插件,那运行在electron环境下的node插件肯定也是可以调试的啦

    首先把调试的部分代码拿出来看看

    struct Wdw {
      std::string name;
      size_t age;
    };
    
    inline void error(const std::string& s) {
      throw std::runtime_error(s);
    }
    
    Napi::Value WdwAdd(const Napi::CallbackInfo& info) {
      Napi::Env env = info.Env();
    
      if (info.Length() < 2) {
        Napi::TypeError::New(env, "Wrong number of arguments")
            .ThrowAsJavaScriptException();
        return env.Null();
      }
      if (!info[0].IsNumber() || !info[1].IsNumber()) {
        Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
        return env.Null();
      }
      Wdw wdw;
      wdw.name = "wdw";
      wdw.age = 22;
    
      double arg0 = info[0].As<Napi::Number>().DoubleValue();
      double arg1 = info[1].As<Napi::Number>().DoubleValue();
    
      if (arg0 > 5) {
        error("How dare you!");
      }
      Napi::Number num = Napi::Number::New(env, arg0 + arg1 + wdw.age);
    
      return num;
    }
    
    

    第一步:编译一个debug版本的electron

    关于安装可以查看这篇文章

    第二步:使用Electron启动你的应用

    在官方工具里就是用命令e start ‘path/to/your/main.js’即可

    第三步:挂载lldb到渲染进程

    这里需要获取到渲染进程的PID, 我直接在渲染进程里调用console.log(process.pid)就有了,然后使用lldb -p PID就可以挂载成功了,其他的还有通过进程名称挂载等多种方式,去看官方文档

    第四步:开始调试

    (lldb) continue 首先我们挂载成功后程序会挂起,continue可以让程序继续执行

    (lldb) continue
    Process 43555 resuming
    

    (lldb) breakpoint set -n WdwAdd 我们添加一个断点,这个断点就是名称为WdwAdd的函数,如果怕同样的名称太多的话可以添加-s(--shlib)限定指定文件下,比如-s addon.node限定在这个文件下

    (lldb) breakpoint set -n WdwAdd -s addon.node
    Breakpoint 1: where = addon.node`WdwAdd(Napi::CallbackInfo const&) + 15 at addon.cc:9:19, address = 0x0000000105cbddff
    

    然后我们正常运行代码,让代码执行到我们打了断点的位置

    Process 43555 stopped
    * thread #1, name = 'CrRendererMain', queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
        frame #0: 0x0000000105cbddff addon.node`WdwAdd(info=0x00007ff7ba7d06d0) at addon.cc:9:19
       6    };
       7    
       8    Napi::Value WdwAdd(const Napi::CallbackInfo& info) {
    -> 9      Napi::Env env = info.Env();
       10   
       11     if (info.Length() < 2) {
       12       Napi::TypeError::New(env, "Wrong number of arguments")
    Target 0: (Electron Helper (Renderer)) stopped.
    

    这里能看到我们已经进入到代码里了,停在方法开头,然后我们可以用next命令来走下一步,相似的命令还有thread step-in/step-over/step-out

    假如我们走下一步走到了这个位置

    Process 95183 stopped
    * thread #1, name = 'CrRendererMain', queue = 'com.apple.main-thread', stop reason = step over
        frame #0: 0x0000000105cbdf3a addon.node`WdwAdd(info=0x00007ff7ba7d06d0) at addon.cc:21:12
       18       return env.Null();
       19     }
       20     Wdw wdw;
    -> 21     wdw.name = "wdw";
       22     wdw.age = 22;
       23   
       24     double arg0 = info[0].As<Napi::Number>().DoubleValue();
    Target 0: (Electron Helper (Renderer)) stopped.
    

    然后我们想查看wdw这个对象,就可以使用print wdw或者frame variable wdw来打印wdw

    (lldb) print wdw
    (Wdw) $10 = (name = "", age = 140659125325832)
    

    查crash位置

    在生产环境中遇到crash的问题,我们很难定位到问题到底出在哪里,如果我们有复现路径的话,那我们就可以很方便的用lldb查到准确的crash位置

    在挂载命令那里添加一个选项

    lldb -p PID -o ’backtrace‘
    -o
       --one-line
            Tells the debugger to execute this one-line lldb command after any
            file provided on the command line has been loaded.
    

    这样在程序运行crash的时候,他会先停下来,运行backtrace,这个命令就打打印frames栈,你就能看到crash的位置

    (lldb) backtrace
    * thread #1, name = 'CrRendererMain', queue = 'com.apple.main-thread', stop reason = signal SIGABRT
      * frame #0: 0x00007ff81177b00e libsystem_kernel.dylib`__pthread_kill + 10
        frame #1: 0x00007ff8117b11ff libsystem_pthread.dylib`pthread_kill + 263
        frame #2: 0x00007ff8116fcd24 libsystem_c.dylib`abort + 123
        frame #3: 0x00007ff81176d082 libc++abi.dylib`abort_message + 241
        frame #4: 0x00007ff81175e1a5 libc++abi.dylib`demangling_terminate_handler() + 242
        frame #5: 0x00007ff81165ae19 libobjc.A.dylib`_objc_terminate() + 104
        frame #6: 0x00007ff81176c4a7 libc++abi.dylib`std::__terminate(void (*)()) + 8
        frame #7: 0x00007ff81176ed05 libc++abi.dylib`__cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception*) + 27
        frame #8: 0x00007ff81176eccc libc++abi.dylib`__cxa_throw + 116
        frame #9: 0x00000001052c8765 addon.node`error(s="How dare you!") at addon.cc:9:3
        frame #10: 0x00000001052c8147 addon.node`WdwAdd(info=0x00007ff7bb1ce6d0) at addon.cc:32:5
        frame #11: 0x0000000108eef4ef ZegoExpressNodeNative.node`napi_value__* Napi::details::WrapCallback<Napi::details::CallbackData<Napi::Value (*)(Napi::CallbackInfo const&), Napi::Value>::Wrapper(napi_env__*, napi_callback_info__*)::'lambda'()>(Napi::details::CallbackData<Napi::Value (*)(Napi::CallbackInfo const&), Napi::Value>::Wrapper(napi_env__*, napi_callback_info__*)::'lambda'()) + 47
        frame #12: 0x0000000108eef48d ZegoExpressNodeNative.node`Napi::details::CallbackData<Napi::Value (*)(Napi::CallbackInfo const&), Napi::Value>::Wrapper(napi_env__*, napi_callback_info__*) + 29
    

    可以看到在我们的程序addon.node的文件9行的位置抛出了异常

    链接

    https://lldb.llvm.org/status/goals.html
    https://medium.com/@ahmedsulaiman/debugging-swift-code-with-lldb-b30c5cf2fd49

    相关文章

      网友评论

          本文标题:lldb 调试electron渲染进程下的插件

          本文链接:https://www.haomeiwen.com/subject/luumzrtx.html