NodeJS C++插件开发(三) - 调试概述
如需转载请标明出处
QQ技术交流群:129518033
文章目录
相关阅读:
NodeJS C++插件开发(一) - 简介
NodeJS C++插件开发(二) - 示例代码hello world
前言
前面文章介绍了如何编写、编译和运行NodeJS C++插件。在NodeJS C++插件开发过程中,调试源码肯定是必不可少的。本文将简要介绍几种NodeJS C++插件的调试方法,主要包括Visual Studio Code、Visual Studio、gdb、QT。
1. Visual Studio Code
1.1 windows msvc(Visual Studio)
Visual Studio Code中
【Run(Ctrl + Shift + D)】-> 【create a launch.json file】-> 【C++ (Windows)】
配置launch.json启动文件
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) 启动",
"type": "cppvsdbg",
"request": "launch",
"program": "C:/Program Files/nodejs/node.exe",
"args": ["${workspaceFolder}/index.js"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal"
}
]
}
注意:只有带有调试信息的NodeJS C++插件才能进行源码调试。
1.2 linux gcc/g++
Visual Studio Code中
【Run(Ctrl + Shift + D)】-> 【create a launch.json file】-> 【C++ (GDB/LLDB)】
配置launch.json启动文件
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "/usr/local/bin/node",
"args": ["${workspaceFolder}/index.js"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
注意:只有带有调试信息的NodeJS C++插件才能进行源码调试。
2. Visual Studio
使用Visual Studio加载CMakeLists.txt工程(这里只介绍cmake-js方式)。
- 生成Visual Studio工程
$ cmake-js configure
- 使用Visual Studio运行addon.sln
编译NodeJS C++插件
【生成】-【生成解决方案】
- 设置运行命令
右键修改addon工程【属性】
【调试】 -> 【命令】: C:/Program Files/nodejs/node.exe
【调试】 -> 【命令参数】$(projectDir)/../index.js
注意:只有带有调试信息的NodeJS C++插件才能进行源码调试。
3. gdb
3.1 本地gdb调试
- 编译出debug版本的程序
$ node-gyp configure build --debug
注意:只有带有调试信息的NodeJS C++插件才能进行源码调试。
- 进行gdb调试
$ gdb node
GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from node...
(gdb) run index.js
Starting program: /usr/local/bin/node index.js
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7a20700 (LWP 72217)]
[New Thread 0x7ffff721f700 (LWP 72218)]
[New Thread 0x7ffff6a1e700 (LWP 72219)]
[New Thread 0x7ffff621d700 (LWP 72220)]
[New Thread 0x7ffff5a1c700 (LWP 72221)]
[New Thread 0x7ffff7fc8700 (LWP 72222)]
hello world
[Thread 0x7ffff5a1c700 (LWP 72221) exited]
[Thread 0x7ffff621d700 (LWP 72220) exited]
[Thread 0x7ffff6a1e700 (LWP 72219) exited]
[Thread 0x7ffff721f700 (LWP 72218) exited]
[Thread 0x7ffff7a20700 (LWP 72217) exited]
[Thread 0x7ffff7fc8700 (LWP 72222) exited]
[Inferior 1 (process 72213) exited normally]
(gdb) list
1 #include <node_api.h>
2
3 napi_value helloMethod(napi_env env, napi_callback_info info)
4 {
5 napi_value result;
6
7 napi_create_string_utf8(env, "hello world", NAPI_AUTO_LENGTH, &result);
8
9 return result;
10 }
(gdb) break 7
Breakpoint 1 at 0x7ffff7fbc19c: file /home/dev/git/node-webdev/code/2.cppAddon/1.cpp_addon_hello_world/3.hello_world_napi_base/addon.cpp, line 7.
(gdb) run
Starting program: /usr/local/bin/node index.js
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7a20700 (LWP 72224)]
[New Thread 0x7ffff721f700 (LWP 72225)]
[New Thread 0x7ffff6a1e700 (LWP 72226)]
[New Thread 0x7ffff621d700 (LWP 72227)]
[New Thread 0x7ffff5a1c700 (LWP 72228)]
[New Thread 0x7ffff7fc8700 (LWP 72229)]
Thread 1 "node" hit Breakpoint 1, helloMethod (env=0x45f23a0, info=0x7fffffffcd80)
at /home/dev/git/node-webdev/code/2.cppAddon/1.cpp_addon_hello_world/3.hello_world_napi_base/addon.cpp:7
7 napi_create_string_utf8(env, "hello world", NAPI_AUTO_LENGTH, &result);
(gdb) print env
$1 = (napi_env) 0x45f23a0
(gdb) continue
Continuing.
hello world
[Thread 0x7ffff5a1c700 (LWP 72228) exited]
[Thread 0x7ffff621d700 (LWP 72227) exited]
[Thread 0x7ffff6a1e700 (LWP 72226) exited]
[Thread 0x7ffff721f700 (LWP 72225) exited]
[Thread 0x7ffff7a20700 (LWP 72224) exited]
[Thread 0x7ffff7fc8700 (LWP 72229) exited]
[Inferior 1 (process 72223) exited normally]
(gdb) quit
注意:gdb -tui node可用ui调试
3.2 远程gdb调试
使用gdb + gdbserver的方式可以进行远程调试。该方式不受限与CPU和操作系统,只要在远端安装gdbserver,在本机安装配套的gdb调试器即可进行调试。
例如Visual Studio 2019中支持的【C++的linux开发】,底层也是使用该原理。
【由于篇幅优先,此处不做详述,会有专门系列介绍远程gdb调试】
4. QT
使用QT Creator加载CMakeLists.txt工程(这里只介绍cmake-js方式)。
- 设置构建目录
【Project】-> 【build】-> 【Build directory】-> 【<project folder>/build】
其中,也就是CMakeLists.txt所在的%{sourceDir},build也就是node默认的编译路径。
- 设置自定义编译命令
【Project】-> 【build】->【Add Build Step】-> 【Custom Process Step】:
Command: cmake-js
Arguments: rebuild -D
Working directory: %{sourceDir}
- 设置自定义清除命令
【Project】-> 【build】->【Add Clean Step】-> 【Custom Process Step】:
Command: cmake-js
Arguments: clean
Working directory: %{sourceDir}
- 设置自定义运行命令
【Project】-> 【Run】->【Add】->【Custom Executable】
Executable: /usr/local/bin/node
Arguments: %{sourceDir}/index.js
Working directory: %{sourceDir}
按F5进行调试
License
License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎
Reference: