best practices for debugging a c++ add-in

2,328 views
Skip to first unread message

Dave Horton

unread,
Jun 27, 2012, 1:19:26 AM6/27/12
to nod...@googlegroups.com
Any suggestions along these lines welcome.  I'm writing my first C++ add-in and struggling with a couple of things:

1) I can use gdb to set breakpoints in my code, but node-gyp by default doesn't include symbols and I haven't been able to figure out how to get it to generate debug code (non optimized, no symbols).  I tried "defines": ['DEBUG'] but that didn't work....

2) I tried putting simple printf's in my code to stdout or stderr but they don't appear so I assume they are being gobbled up by node some how.

For a first exercise, I am simply trying to create a module that returns a wrapped C++ object, with a property which when accessed in JS triggers a C++ getter.  I can compile the module ok, and my test script can create an instance, but it appears my getter is never called for some reason (it should return an integer value of 5060 but it returns undefined).  For anyone willing to point out what I assume is an obvious flaw, I have posted a gist here: git://gist.github.com/3001626.git

I've read the docs on nodejs.org on the basics of creating modules, but if anyone knows of useful sample code for a newbie to node (knowledgeable of javascript and C++) I'd appreciate the pointer.  Thanks

Dave Horton

unread,
Jun 27, 2012, 1:24:56 AM6/27/12
to nod...@googlegroups.com

Nathan Rajlich

unread,
Jun 27, 2012, 2:32:24 AM6/27/12
to nod...@googlegroups.com
For 1), you can create a debug build of your addon by adding the --debug switch when running node-gyp (note that it will be built into build/Debug instead of build/Release).

I'm not sure about 2), I've used printfs for quick debugging before in addons and it wasn't a problem.

On Tue, Jun 26, 2012 at 10:24 PM, Dave Horton <da...@dchorton.com> wrote:

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Ben Noordhuis

unread,
Jun 27, 2012, 9:11:47 AM6/27/12
to nod...@googlegroups.com
I just tried your example and it works for me, i.e. ua.port == 5060.
That it works may be accidental, though - there is at least one bug.
:-)

GetPort() needs a HandleScope and you need to pass the return value
through scope.Close(...).

Dave Horton

unread,
Jun 27, 2012, 9:15:07 AM6/27/12
to nod...@googlegroups.com
Yes, for reasons I don't understand it is working for me this morning as well!

Anyways, thanks very much for the pointer on the bug, as well as the information in an earlier post about creating a Debug build.  It's awesome that node has such a responsive, helpful community!  Makes me excited to participate!

Marcel Laverdet

unread,
Jun 27, 2012, 10:32:40 PM6/27/12
to nod...@googlegroups.com
> GetPort() needs a HandleScope and you need to pass the return value through scope.Close(...).

This is false. v8 builds a HandleScope for you automatically when a function or accessor is called through JS. You only need to use HandleScope at the very very top level (node.cc, or stuff like fibers and threads-a-gogo). You may also want to use HandleScope if you want to manage your handle lifetimes manually when doing lots of work in v8 c++ land.

Alexander List

unread,
Jan 14, 2016, 12:00:42 PM1/14/16
to nodejs
Dave, 

How did you use gdb to set breakpoints in your C++ add-in code, that got triggered after calling `node test.js`?

Thanks!

davi...@vt.edu

unread,
Jan 10, 2017, 8:35:53 AM1/10/17
to nodejs, alexande...@gmail.com
Dave,

I was trying to figure this out too. Just cracked it -- see my blog post for details. The short version is: recompile the add-on using node-gyp --debug, possibly change the module load path to use the Debug version, and then set break points as normal.
Reply all
Reply to author
Forward
0 new messages