Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug binary module of nodejs?

Tags:

I have a node.js module written in C++ that provides some bindings for a C++ library. The library crashes with SIGSEGV, so I need to debug it with GDB and find out what goes wrong.

I already have the source for the module in ./node_modules/somelib/ and if I go to that folder and type npm install the library is compiled and can be used through a require('somelib') from node.js. I can attach gdb to node and reproduce the error, but in the stacktrace I just see node_modules/somelib/Release/somelib.node.

I'm not sure if this is important but the library is compiled using node-gyp.

  • Question 1: How do I load the source code or point gdb to the source code?
  • Question 2: How do I configure node-gyp to produce debug symbols?
like image 253
lanoxx Avatar asked Apr 22 '14 19:04

lanoxx


People also ask

Which Node module helps debug Node applications?

NODE_DEBUG enables debugging messages using the Node. js util. debuglog (see below), but also consult the documentation of your primary modules and frameworks to discover further options.

Does Nodejs have a debugger?

Node. js includes a command-line debugging utility. The Node. js debugger client is not a full-featured debugger, but simple stepping and inspection are possible.


2 Answers

I just found the answer to this in the node-gyp documentation. The solution is to invoke the build process with the --debug flag. That means to invoke node-gyp configure --debug and/or node-gyp build --debug. Then instead of a Release folder a Debug folder will be created. gdb will then automatically load the source files.

like image 199
lanoxx Avatar answered Oct 21 '22 19:10

lanoxx


Shamelessly copied from an archive of the (now broken) link provided by @Peter Cordes

First, compile your add-on using node-gyp with the --debug flag.

$ node-gyp --debug configure rebuild

Second, if you're still in "playground" mode like I am, you're probably loading your module with something like

var ObjModule = require('./ObjModule/build/Release/objModule');

However, when you rebuild using node-gyp in debug mode, node-gyp throws away the Release version and creates a Debug version instead. So update the module path:

var ObjModule = require('./ObjModule/build/Debug/objModule');

Alright, now we're ready to debug our C++ add-on. Run gdb against the node binary, which is a C++ application. Now, node itself doesn't know about your add-on, so when you try to set a breakpoint on your add-on function (in this case, StringReverse) it complains that the specific function is not defined. Fear not, your add-on is part of the "future shared library load" it refers to, and will be loaded once you require() your add-on in JavaScript.

$ gdb node
...
Reading symbols from node...done.
(gdb) break StringReverse
Function "StringReverse" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y  

OK, now we just have to run the application:

(gdb) run ../modTest.js 
...
Breakpoint 1, StringReverse (args=...) at ../objModule.cpp:49
like image 20
JBaczuk Avatar answered Oct 21 '22 19:10

JBaczuk