Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected variables not defined when using node inspect?

I'm trying to familiarize myself with debugging using the node inspect command. I've written as simple script use_debugger.js which defines a function and calls it:

function count(nums, maxes) {
  debugger;
  return maxes;
}

count([1, 2, 3], [4, 5]);

When I drop into the debugger, I would expect nums to be defined and have the value [1, 2, 3]. However, if I run node inspect use_debugger.js and then type c and nums in the REPL, I get that it is not defined:

Kurts-MacBook-Pro:Scratch kurtpeek$ node --inspect-brk use_debugger.js
Debugger listening on ws://127.0.0.1:9229/7adfaada-c939-44a3-9150-6d1326b8c7d0
For help, see: https://nodejs.org/en/docs/inspector
^C
Kurts-MacBook-Pro:Scratch kurtpeek$ node inspect use_debugger.js
< Debugger listening on ws://127.0.0.1:9229/54fb6a12-82c8-4454-8307-6d120b0c26e8
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in use_debugger.js:1
> 1 (function (exports, require, module, __filename, __dirname) { function count(nums, maxes) {
  2   debugger;
  3   return maxes;
debug> c
break in use_debugger.js:2
  1 (function (exports, require, module, __filename, __dirname) { function count(nums, maxes) {
> 2   debugger;
  3   return maxes;
  4 }
debug> maxes
repl:1
maxes
^

ReferenceError: maxes is not defined
    at repl:1:1
    at Script.runInContext (vm.js:101:20)
    at Object.runInContext (vm.js:279:6)
    at REPLServer.controlEval (internal/deps/node-inspect/lib/internal/inspect_repl.js:521:25)
    at bound (domain.js:396:14)
    at REPLServer.runBound [as eval] (domain.js:409:12)
    at REPLServer.onLine (repl.js:621:10)
    at REPLServer.emit (events.js:182:13)
    at REPLServer.EventEmitter.emit (domain.js:442:20)
    at REPLServer.Interface._onLine (readline.js:290:10)
debug> 

Should I not be able to 'access' the variables in the function's scope, nums and maxes?

Update

To further document estus' answer, the exec maxes command worked for me:

debug> exec maxes
[ 4, 5 ]

As stated in https://nodejs.org/api/debugger.html#debugger_information, the exec expr command executes an expression in the debugging script's context.

like image 959
Kurt Peek Avatar asked Mar 05 '23 09:03

Kurt Peek


1 Answers

maxes

evaluates maxes in REPL scope, not count function scope.

As explained in the documentation:

repl - Open debugger's repl for evaluation in debugging script's context

exec expr - Execute an expression in debugging script's context

To evaluate maxes in debugging scope, it should be either:

repl
maxes

Or:

exec maxes
like image 156
Estus Flask Avatar answered Mar 12 '23 06:03

Estus Flask