@@ -25,6 +25,7 @@ using v8::FunctionCallbackInfo;
25
25
using v8::FunctionTemplate;
26
26
using v8::Global;
27
27
using v8::HandleScope;
28
+ using v8::HeapProfiler;
28
29
using v8::HeapSnapshot;
29
30
using v8::Isolate;
30
31
using v8::JustVoid;
@@ -36,6 +37,7 @@ using v8::Number;
36
37
using v8::Object;
37
38
using v8::ObjectTemplate;
38
39
using v8::String;
40
+ using v8::Uint8Array;
39
41
using v8::Value;
40
42
41
43
namespace node {
@@ -340,15 +342,19 @@ class HeapSnapshotStream : public AsyncWrap,
340
342
HeapSnapshotPointer snapshot_;
341
343
};
342
344
343
- inline void TakeSnapshot (Environment* env, v8::OutputStream* out) {
344
- HeapSnapshotPointer snapshot {
345
- env->isolate ()->GetHeapProfiler ()->TakeHeapSnapshot () };
345
+ inline void TakeSnapshot (Environment* env,
346
+ v8::OutputStream* out,
347
+ HeapProfiler::HeapSnapshotOptions options) {
348
+ HeapSnapshotPointer snapshot{
349
+ env->isolate ()->GetHeapProfiler ()->TakeHeapSnapshot (options)};
346
350
snapshot->Serialize (out, HeapSnapshot::kJSON );
347
351
}
348
352
349
353
} // namespace
350
354
351
- Maybe<void > WriteSnapshot (Environment* env, const char * filename) {
355
+ Maybe<void > WriteSnapshot (Environment* env,
356
+ const char * filename,
357
+ HeapProfiler::HeapSnapshotOptions options) {
352
358
uv_fs_t req;
353
359
int err;
354
360
@@ -365,7 +371,7 @@ Maybe<void> WriteSnapshot(Environment* env, const char* filename) {
365
371
}
366
372
367
373
FileOutputStream stream (fd, &req);
368
- TakeSnapshot (env, &stream);
374
+ TakeSnapshot (env, &stream, options );
369
375
if ((err = stream.status ()) < 0 ) {
370
376
env->ThrowUVException (err, " write" , nullptr , filename);
371
377
return Nothing<void >();
@@ -410,10 +416,28 @@ BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
410
416
return MakeBaseObject<HeapSnapshotStream>(env, std::move (snapshot), obj);
411
417
}
412
418
419
+ HeapProfiler::HeapSnapshotOptions GetHeapSnapshotOptions (
420
+ Local<Value> options_value) {
421
+ CHECK (options_value->IsUint8Array ());
422
+ Local<Uint8Array> arr = options_value.As <Uint8Array>();
423
+ uint8_t * options =
424
+ static_cast <uint8_t *>(arr->Buffer ()->Data ()) + arr->ByteOffset ();
425
+ HeapProfiler::HeapSnapshotOptions result;
426
+ result.snapshot_mode = options[0 ]
427
+ ? HeapProfiler::HeapSnapshotMode::kExposeInternals
428
+ : HeapProfiler::HeapSnapshotMode::kRegular ;
429
+ result.numerics_mode = options[1 ]
430
+ ? HeapProfiler::NumericsMode::kExposeNumericValues
431
+ : HeapProfiler::NumericsMode::kHideNumericValues ;
432
+ return result;
433
+ }
434
+
413
435
void CreateHeapSnapshotStream (const FunctionCallbackInfo<Value>& args) {
414
436
Environment* env = Environment::GetCurrent (args);
415
- HeapSnapshotPointer snapshot {
416
- env->isolate ()->GetHeapProfiler ()->TakeHeapSnapshot () };
437
+ CHECK_EQ (args.Length (), 1 );
438
+ auto options = GetHeapSnapshotOptions (args[0 ]);
439
+ HeapSnapshotPointer snapshot{
440
+ env->isolate ()->GetHeapProfiler ()->TakeHeapSnapshot (options)};
417
441
CHECK (snapshot);
418
442
BaseObjectPtr<AsyncWrap> stream =
419
443
CreateHeapSnapshotStream (env, std::move (snapshot));
@@ -424,13 +448,13 @@ void CreateHeapSnapshotStream(const FunctionCallbackInfo<Value>& args) {
424
448
void TriggerHeapSnapshot (const FunctionCallbackInfo<Value>& args) {
425
449
Environment* env = Environment::GetCurrent (args);
426
450
Isolate* isolate = args.GetIsolate ();
427
-
451
+ CHECK_EQ (args. Length (), 2 );
428
452
Local<Value> filename_v = args[0 ];
453
+ auto options = GetHeapSnapshotOptions (args[1 ]);
429
454
430
455
if (filename_v->IsUndefined ()) {
431
456
DiagnosticFilename name (env, " Heap" , " heapsnapshot" );
432
- if (WriteSnapshot (env, *name).IsNothing ())
433
- return ;
457
+ if (WriteSnapshot (env, *name, options).IsNothing ()) return ;
434
458
if (String::NewFromUtf8 (isolate, *name).ToLocal (&filename_v)) {
435
459
args.GetReturnValue ().Set (filename_v);
436
460
}
@@ -439,8 +463,7 @@ void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
439
463
440
464
BufferValue path (isolate, filename_v);
441
465
CHECK_NOT_NULL (*path);
442
- if (WriteSnapshot (env, *path).IsNothing ())
443
- return ;
466
+ if (WriteSnapshot (env, *path, options).IsNothing ()) return ;
444
467
return args.GetReturnValue ().Set (filename_v);
445
468
}
446
469
0 commit comments