@@ -25,6 +25,7 @@ using v8::FunctionCallbackInfo;
2525using v8::FunctionTemplate;
2626using v8::Global;
2727using v8::HandleScope;
28+ using v8::HeapProfiler;
2829using v8::HeapSnapshot;
2930using v8::Isolate;
3031using v8::JustVoid;
@@ -36,6 +37,7 @@ using v8::Number;
3637using v8::Object;
3738using v8::ObjectTemplate;
3839using v8::String;
40+ using v8::Uint8Array;
3941using v8::Value;
4042
4143namespace node {
@@ -340,15 +342,19 @@ class HeapSnapshotStream : public AsyncWrap,
340342 HeapSnapshotPointer snapshot_;
341343};
342344
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)};
346350 snapshot->Serialize (out, HeapSnapshot::kJSON );
347351}
348352
349353} // namespace
350354
351- Maybe<void > WriteSnapshot (Environment* env, const char * filename) {
355+ Maybe<void > WriteSnapshot (Environment* env,
356+ const char * filename,
357+ HeapProfiler::HeapSnapshotOptions options) {
352358 uv_fs_t req;
353359 int err;
354360
@@ -365,7 +371,7 @@ Maybe<void> WriteSnapshot(Environment* env, const char* filename) {
365371 }
366372
367373 FileOutputStream stream (fd, &req);
368- TakeSnapshot (env, &stream);
374+ TakeSnapshot (env, &stream, options );
369375 if ((err = stream.status ()) < 0 ) {
370376 env->ThrowUVException (err, " write" , nullptr , filename);
371377 return Nothing<void >();
@@ -410,10 +416,28 @@ BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
410416 return MakeBaseObject<HeapSnapshotStream>(env, std::move (snapshot), obj);
411417}
412418
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+
413435void CreateHeapSnapshotStream (const FunctionCallbackInfo<Value>& args) {
414436 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)};
417441 CHECK (snapshot);
418442 BaseObjectPtr<AsyncWrap> stream =
419443 CreateHeapSnapshotStream (env, std::move (snapshot));
@@ -424,13 +448,13 @@ void CreateHeapSnapshotStream(const FunctionCallbackInfo<Value>& args) {
424448void TriggerHeapSnapshot (const FunctionCallbackInfo<Value>& args) {
425449 Environment* env = Environment::GetCurrent (args);
426450 Isolate* isolate = args.GetIsolate ();
427-
451+ CHECK_EQ (args. Length (), 2 );
428452 Local<Value> filename_v = args[0 ];
453+ auto options = GetHeapSnapshotOptions (args[1 ]);
429454
430455 if (filename_v->IsUndefined ()) {
431456 DiagnosticFilename name (env, " Heap" , " heapsnapshot" );
432- if (WriteSnapshot (env, *name).IsNothing ())
433- return ;
457+ if (WriteSnapshot (env, *name, options).IsNothing ()) return ;
434458 if (String::NewFromUtf8 (isolate, *name).ToLocal (&filename_v)) {
435459 args.GetReturnValue ().Set (filename_v);
436460 }
@@ -439,8 +463,7 @@ void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
439463
440464 BufferValue path (isolate, filename_v);
441465 CHECK_NOT_NULL (*path);
442- if (WriteSnapshot (env, *path).IsNothing ())
443- return ;
466+ if (WriteSnapshot (env, *path, options).IsNothing ()) return ;
444467 return args.GetReturnValue ().Set (filename_v);
445468}
446469
0 commit comments