GeezerVison is showing github.com — process: split routines used to enhance fatal exception stack traces · acohn/node@9445492
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/events.js +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,11 +25,15 @@ const { Math, Object, Reflect } = primordials; | 25 | 25 |
| 26 | 26 | var spliceOne; | 27 | 27 |
| | 28 | +const { | | 29 | + kEnhanceStackBeforeInspector, | | 30 | + codes | | 31 | +} = require('internal/errors'); | 28 | 32 | const { | 29 | 33 | ERR_INVALID_ARG_TYPE, | 30 | 34 | ERR_OUT_OF_RANGE, | 31 | 35 | ERR_UNHANDLED_ERROR | 32 | | -} = require('internal/errors').codes; | | 36 | +} = codes; | 33 | 37 |
| 34 | 38 | const { | 35 | 39 | inspect | @@ -142,8 +146,8 @@ function enhanceStackTrace(err, own) { | 142 | 146 | ownStack.splice(off + 1, len - 2, | 143 | 147 | ' [... lines matching original stack trace ...]'); | 144 | 148 | } | 145 | | - // Do this last, because it is the only operation with side effects. | 146 | | - err.stack = err.stack + sep + ownStack.join('\n'); | | 149 | + | | 150 | + return err.stack + sep + ownStack.join('\n'); | 147 | 151 | } | 148 | 152 |
| 149 | 153 | EventEmitter.prototype.emit = function emit(type, ...args) { | @@ -162,11 +166,10 @@ EventEmitter.prototype.emit = function emit(type, ...args) { | 162 | 166 | er = args[0]; | 163 | 167 | if (er instanceof Error) { | 164 | 168 | try { | 165 | | - const { kExpandStackSymbol } = require('internal/util'); | 166 | 169 | const capture = {}; | 167 | 170 | // eslint-disable-next-line no-restricted-syntax | 168 | 171 | Error.captureStackTrace(capture, EventEmitter.prototype.emit); | 169 | | - Object.defineProperty(er, kExpandStackSymbol, { | | 172 | + Object.defineProperty(er, kEnhanceStackBeforeInspector, { | 170 | 173 | value: enhanceStackTrace.bind(null, er, capture), | 171 | 174 | configurable: true | 172 | 175 | }); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js +15-2Lines changed: 15 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -299,9 +299,22 @@ process.emitWarning = emitWarning; | 299 | 299 | } | 300 | 300 |
| 301 | 301 | function setupPrepareStackTrace() { | 302 | | - const { setPrepareStackTraceCallback } = internalBinding('errors'); | 303 | | - const { prepareStackTrace } = require('internal/errors'); | | 302 | + const { | | 303 | + setEnhanceStackForFatalException, | | 304 | + setPrepareStackTraceCallback | | 305 | + } = internalBinding('errors'); | | 306 | + const { | | 307 | + prepareStackTrace, | | 308 | + fatalExceptionStackEnhancers: { | | 309 | + beforeInspector, | | 310 | + afterInspector | | 311 | + } | | 312 | + } = require('internal/errors'); | | 313 | + // Tell our PrepareStackTraceCallback passed to the V8 API | | 314 | + // to call prepareStackTrace(). | 304 | 315 | setPrepareStackTraceCallback(prepareStackTrace); | | 316 | + // Set the function used to enhance the error stack for printing | | 317 | + setEnhanceStackForFatalException(beforeInspector, afterInspector); | 305 | 318 | } | 306 | 319 |
| 307 | 320 | function setupProcessObject() { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/errors.js +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -622,6 +622,45 @@ function addNumericalSeparator(val) { | 622 | 622 | return `${val.slice(0, i)}${res}`; | 623 | 623 | } | 624 | 624 |
| | 625 | +// Used to enhance the stack that will be picked up by the inspector | | 626 | +const kEnhanceStackBeforeInspector = Symbol('kEnhanceStackBeforeInspector'); | | 627 | + | | 628 | +// These are supposed to be called only on fatal exceptions before | | 629 | +// the process exits. | | 630 | +const fatalExceptionStackEnhancers = { | | 631 | + beforeInspector(error) { | | 632 | + if (typeof error[kEnhanceStackBeforeInspector] !== 'function') { | | 633 | + return error.stack; | | 634 | + } | | 635 | + | | 636 | + try { | | 637 | + // Set the error.stack here so it gets picked up by the | | 638 | + // inspector. | | 639 | + error.stack = error[kEnhanceStackBeforeInspector](); | | 640 | + } catch { | | 641 | + // We are just enhancing the error. If it fails, ignore it. | | 642 | + } | | 643 | + return error.stack; | | 644 | + }, | | 645 | + afterInspector(error) { | | 646 | + const originalStack = error.stack; | | 647 | + const { | | 648 | + inspect, | | 649 | + inspectDefaultOptions: { | | 650 | + colors: defaultColors | | 651 | + } | | 652 | + } = lazyInternalUtilInspect(); | | 653 | + const colors = internalBinding('util').guessHandleType(2) === 'TTY' && | | 654 | + require('internal/tty').hasColors() || | | 655 | + defaultColors; | | 656 | + try { | | 657 | + return inspect(error, { colors }); | | 658 | + } catch { | | 659 | + return originalStack; | | 660 | + } | | 661 | + } | | 662 | +}; | | 663 | + | 625 | 664 | module.exports = { | 626 | 665 | addCodeToName, // Exported for NghttpError | 627 | 666 | codes, | @@ -638,6 +677,8 @@ module.exports = { | 638 | 677 | // This is exported only to facilitate testing. | 639 | 678 | E, | 640 | 679 | prepareStackTrace, | | 680 | + kEnhanceStackBeforeInspector, | | 681 | + fatalExceptionStackEnhancers | 641 | 682 | }; | 642 | 683 |
| 643 | 684 | // To declare an error message, use the E(sym, val, def) function above. The sym | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/execution.js -12Lines changed: 0 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -150,10 +150,6 @@ function createOnGlobalUncaughtException() { | 150 | 150 | } else if (!process.emit('uncaughtException', er, type)) { | 151 | 151 | // If someone handled it, then great. Otherwise, die in C++ land | 152 | 152 | // since that means that we'll exit the process, emit the 'exit' event. | 153 | | - const { inspect } = require('internal/util/inspect'); | 154 | | - const colors = internalBinding('util').guessHandleType(2) === 'TTY' && | 155 | | - require('internal/tty').hasColors() || | 156 | | - inspect.defaultOptions.colors; | 157 | 153 | try { | 158 | 154 | if (!process._exiting) { | 159 | 155 | process._exiting = true; | @@ -163,14 +159,6 @@ function createOnGlobalUncaughtException() { | 163 | 159 | } catch { | 164 | 160 | // Nothing to be done about it at this point. | 165 | 161 | } | 166 | | - try { | 167 | | - const { kExpandStackSymbol } = require('internal/util'); | 168 | | - if (typeof er[kExpandStackSymbol] === 'function') | 169 | | - er[kExpandStackSymbol](); | 170 | | - er.stack = inspect(er, { colors }); | 171 | | - } catch { | 172 | | - // Nothing to be done about it at this point. | 173 | | - } | 174 | 162 | return false; | 175 | 163 | } | 176 | 164 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -414,6 +414,5 @@ module.exports = { | 414 | 414 | // Used by the buffer module to capture an internal reference to the | 415 | 415 | // default isEncoding implementation, just in case userland overrides it. | 416 | 416 | kIsEncodingSymbol: Symbol('kIsEncodingSymbol'), | 417 | | - kExpandStackSymbol: Symbol('kExpandStackSymbol'), | 418 | 417 | kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol') | 419 | 418 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1683,5 +1683,6 @@ function formatWithOptions(inspectOptions, ...args) { | 1683 | 1683 | module.exports = { | 1684 | 1684 | inspect, | 1685 | 1685 | format, | 1686 | | - formatWithOptions | | 1686 | + formatWithOptions, | | 1687 | + inspectDefaultOptions | 1687 | 1688 | }; |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -408,6 +408,8 @@ constexpr size_t kFsStatsBufferLength = | 408 | 408 | V(crypto_key_object_constructor, v8::Function) \ | 409 | 409 | V(domain_callback, v8::Function) \ | 410 | 410 | V(domexception_function, v8::Function) \ | | 411 | + V(enhance_fatal_stack_after_inspector, v8::Function) \ | | 412 | + V(enhance_fatal_stack_before_inspector, v8::Function) \ | 411 | 413 | V(fs_use_promises_symbol, v8::Symbol) \ | 412 | 414 | V(host_import_module_dynamically_callback, v8::Function) \ | 413 | 415 | V(host_initialize_import_meta_object_callback, v8::Function) \ | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector_agent.cc +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -570,7 +570,7 @@ class NodeInspectorClient : public V8InspectorClient { | 570 | 570 | } | 571 | 571 | } | 572 | 572 |
| 573 | | - void FatalException(Local<Value> error, Local<Message> message) { | | 573 | + void ReportUncaughtException(Local<Value> error, Local<Message> message) { | 574 | 574 | Isolate* isolate = env_->isolate(); | 575 | 575 | Local<Context> context = env_->context(); | 576 | 576 |
| @@ -836,10 +836,11 @@ void Agent::WaitForDisconnect() { | 836 | 836 | } | 837 | 837 | } | 838 | 838 |
| 839 | | -void Agent::FatalException(Local<Value> error, Local<Message> message) { | | 839 | +void Agent::ReportUncaughtException(Local<Value> error, | | 840 | + Local<Message> message) { | 840 | 841 | if (!IsListening()) | 841 | 842 | return; | 842 | | - client_->FatalException(error, message); | | 843 | + client_->ReportUncaughtException(error, message); | 843 | 844 | WaitForDisconnect(); | 844 | 845 | } | 845 | 846 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/inspector_agent.h +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,8 +65,8 @@ class Agent { | 65 | 65 | void WaitForConnect(); | 66 | 66 | // Blocks till all the sessions with "WaitForDisconnectOnShutdown" disconnect | 67 | 67 | void WaitForDisconnect(); | 68 | | - void FatalException(v8::Local<v8::Value> error, | 69 | | - v8::Local<v8::Message> message); | | 68 | + void ReportUncaughtException(v8::Local<v8::Value> error, | | 69 | + v8::Local<v8::Message> message); | 70 | 70 |
| 71 | 71 | // Async stack traces instrumentation. | 72 | 72 | void AsyncTaskScheduled(const v8_inspector::StringView& taskName, void* task, | |
|