How to setBreakpoint on child_process.exec using "node inspect"

67 views Asked by At

I'm debugging a Node application by running;

node inspect index.js

At a certain point, index.js launches a new process by calling child_process.exec(). I want to be able to put a breakpoint programmatically on child_process.exec().

If I wanted to put a breakpoint on a 3rd party package, for example mylib, that's simple:

setBreakpoint("node_modules/mylib/index.js", 123) // This puts a bp on line 123

When running "scripts()" to list all loaded files, it returns the following:

// 3rd party libraries
node_modules/mylib/index.js
node_modules/... 

// internal libraries
node:child_process
node:... 

Ideally, I would want something like this:

setBreakpoint("node:child_process:exec") // Won't work, setBreakpoint expects a 2nd param of line number
setBreakpoint("node:child_process", ???) // Missing line of exec()

How can I know the line of exec()?

I can't read the file "node:child_process" because it doesn't exist on the file system:

debug> fs.readFileSync("node:child_process")
node:internal/fs/utils:347
    throw err;
    ^

Uncaught Error: ENOENT: no such file or directory, open 'node:child_process'

Any of the following solutions would work for me:

  1. Read the content of "node:child_process" somehow and find the definition of exec()
  2. Since I can access/call child_process.exec() in the debugger, maybe I can get the line number of exec() directly:
debug> child_process.exec
[Function: exec]

I was able to it manually by "stepping in" while debugging index.js:

 230 function exec(command, options, callback) {
>231   const opts = normalizeExecArgs(command, options, callback);
 232   return module.exports.execFile(opts.file,

I know that for the version of Node I have locally it's on line 231, but this can change for different Node versions, so this solution is not good enough.

0

There are 0 answers