GeezerVison is showing github.com — cli: add --trace-uncaught flag · fredericp/node@31217a8
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: doc/api/cli.md +13Lines changed: 13 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -747,6 +747,18 @@ added: v12.2.0 | 747 | 747 | Prints TLS packet trace information to `stderr`. This can be used to debug TLS | 748 | 748 | connection problems. | 749 | 749 |
| | 750 | +### `--trace-uncaught` | | 751 | +<!-- YAML | | 752 | +added: REPLACEME | | 753 | +--> | | 754 | + | | 755 | +Print stack traces for uncaught exceptions; usually, the stack trace associated | | 756 | +with the creation of an `Error` is printed, whereas this makes Node.js also | | 757 | +print the stack trace associated with throwing the value (which does not need | | 758 | +to be an `Error` instance). | | 759 | + | | 760 | +Enabling this option may affect garbage collection behavior negatively. | | 761 | + | 750 | 762 | ### `--trace-warnings` | 751 | 763 | <!-- YAML | 752 | 764 | added: v6.0.0 | @@ -1044,6 +1056,7 @@ Node.js options that are allowed are: | 1044 | 1056 | * `--trace-events-enabled` | 1045 | 1057 | * `--trace-sync-io` | 1046 | 1058 | * `--trace-tls` | | 1059 | +* `--trace-uncaught` | 1047 | 1060 | * `--trace-warnings` | 1048 | 1061 | * `--track-heap-objects` | 1049 | 1062 | * `--unhandled-rejections` | |
Collapse file Copy file name to clipboardExpand all lines: doc/node.1 +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -337,6 +337,18 @@ Print a stack trace whenever synchronous I/O is detected after the first turn of | 337 | 337 | .It Fl -trace-tls | 338 | 338 | Prints TLS packet trace information to stderr. | 339 | 339 | . | | 340 | +.It Fl -trace-uncaught | | 341 | +Print stack traces for uncaught exceptions; usually, the stack trace associated | | 342 | +with the creation of an | | 343 | +.Sy Error | | 344 | +is printed, whereas this makes Node.js also | | 345 | +print the stack trace associated with throwing the value (which does not need | | 346 | +to be an | | 347 | +.Sy Error | | 348 | +instance). | | 349 | +.Pp | | 350 | +Enabling this option may affect garbage collection behavior negatively. | | 351 | +. | 340 | 352 | .It Fl -trace-warnings | 341 | 353 | Print stack traces for process warnings (including deprecations). | 342 | 354 | . | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -265,6 +265,8 @@ int Environment::InitializeInspector( | 265 | 265 | void Environment::InitializeDiagnostics() { | 266 | 266 | isolate_->GetHeapProfiler()->AddBuildEmbedderGraphCallback( | 267 | 267 | Environment::BuildEmbedderGraph, this); | | 268 | + if (options_->trace_uncaught) | | 269 | + isolate_->SetCaptureStackTraceForUncaughtExceptions(true); | 268 | 270 |
| 269 | 271 | #if defined HAVE_DTRACE || defined HAVE_ETW | 270 | 272 | InitDTrace(this); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.cc +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -380,6 +380,19 @@ static void ReportFatalException(Environment* env, | 380 | 380 | "%s\n%s: %s\n", *arrow_string, *name_string, *message_string); | 381 | 381 | } | 382 | 382 | } | | 383 | + | | 384 | + if (!env->options()->trace_uncaught) { | | 385 | + PrintErrorString("(Use `node --trace-uncaught ...` to show " | | 386 | + "where the exception was thrown)\n"); | | 387 | + } | | 388 | + } | | 389 | + | | 390 | + if (env->options()->trace_uncaught) { | | 391 | + Local<StackTrace> trace = message->GetStackTrace(); | | 392 | + if (!trace.IsEmpty()) { | | 393 | + PrintErrorString("Thrown at:\n"); | | 394 | + PrintStackTrace(env->isolate(), trace); | | 395 | + } | 383 | 396 | } | 384 | 397 |
| 385 | 398 | fflush(stderr); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -476,6 +476,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 476 | 476 | "prints TLS packet trace information to stderr", | 477 | 477 | &EnvironmentOptions::trace_tls, | 478 | 478 | kAllowedInEnvironment); | | 479 | + AddOption("--trace-uncaught", | | 480 | + "show stack traces for the `throw` behind uncaught exceptions", | | 481 | + &EnvironmentOptions::trace_uncaught, | | 482 | + kAllowedInEnvironment); | 479 | 483 | AddOption("--trace-warnings", | 480 | 484 | "show stack traces on process warnings", | 481 | 485 | &EnvironmentOptions::trace_warnings, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -140,6 +140,7 @@ class EnvironmentOptions : public Options { | 140 | 140 | bool trace_deprecation = false; | 141 | 141 | bool trace_sync_io = false; | 142 | 142 | bool trace_tls = false; | | 143 | + bool trace_uncaught = false; | 143 | 144 | bool trace_warnings = false; | 144 | 145 | std::string unhandled_rejections; | 145 | 146 | std::string userland_loader; | |
Collapse file Copy file name to clipboardExpand all lines: test/message/eval_messages.out +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,9 +55,11 @@ ReferenceError: y is not defined | 55 | 55 | var ______________________________________________; throw 10 | 56 | 56 | ^ | 57 | 57 | 10 | | 58 | +(Use `node --trace-uncaught ...` to show where the exception was thrown) | 58 | 59 |
| 59 | 60 | [eval]:1 | 60 | 61 | var ______________________________________________; throw 10 | 61 | 62 | ^ | 62 | 63 | 10 | | 64 | +(Use `node --trace-uncaught ...` to show where the exception was thrown) | 63 | 65 | done |
Collapse file Copy file name to clipboardExpand all lines: test/message/stdin_messages.out +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,9 +67,11 @@ ReferenceError: y is not defined | 67 | 67 | var ______________________________________________; throw 10 | 68 | 68 | ^ | 69 | 69 | 10 | | 70 | +(Use `node --trace-uncaught ...` to show where the exception was thrown) | 70 | 71 |
| 71 | 72 | [stdin]:1 | 72 | 73 | var ______________________________________________; throw 10 | 73 | 74 | ^ | 74 | 75 | 10 | | 76 | +(Use `node --trace-uncaught ...` to show where the exception was thrown) | 75 | 77 | done |
Collapse file Copy file name to clipboardExpand all lines: test/message/throw_error_with_getter_throw.out +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | throw { // eslint-disable-line no-throw-literal | 4 | 4 | ^ | 5 | 5 | [object Object] | | 6 | +(Use `node --trace-uncaught ...` to show where the exception was thrown) |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --trace-uncaught | | 2 | +'use strict'; | | 3 | +require('../common'); | | 4 | +throw { // eslint-disable-line no-throw-literal | | 5 | + get stack() { | | 6 | + throw new Error('weird throw but ok'); | | 7 | + }, | | 8 | + get name() { | | 9 | + throw new Error('weird throw but ok'); | | 10 | + }, | | 11 | +}; |
|