Skip to content

Commit

Permalink
src: fix --diagnostic-dir not working with --heapsnapshot-signal
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteMinds committed Sep 5, 2023
1 parent 6a5394e commit 884855a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/heap_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,16 @@ void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
auto options = GetHeapSnapshotOptions(args[1]);

if (filename_v->IsUndefined()) {
std::string dir = env->options()->diagnostic_dir;
if (dir.empty()) {
dir = Environment::GetCwd(env->exec_path());
}
DiagnosticFilename name(env, "Heap", "heapsnapshot");
std::string filename = dir + kPathSeparator + (*name);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env,
permission::PermissionScope::kFileSystemWrite,
Environment::GetCwd(env->exec_path()));
if (WriteSnapshot(env, *name, options).IsNothing()) return;
if (String::NewFromUtf8(isolate, *name).ToLocal(&filename_v)) {
env, permission::PermissionScope::kFileSystemWrite, dir);
if (WriteSnapshot(env, filename.c_str(), options).IsNothing()) return;
if (String::NewFromUtf8(isolate, filename.c_str()).ToLocal(&filename_v)) {
args.GetReturnValue().Set(filename_v);
}
return;
Expand Down
49 changes: 49 additions & 0 deletions test/sequential/test-diagnostic-dir-heapdump-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');

if (common.isWindows)
common.skip('test not supported on Windows');

const assert = require('assert');

if (process.argv[2] === 'child') {
const fs = require('fs');
const path = require('path');

assert.strictEqual(process.listenerCount('SIGUSR2'), 1);
process.kill(process.pid, 'SIGUSR2');
process.kill(process.pid, 'SIGUSR2');

// Asynchronously wait for the snapshot. Use an async loop to be a bit more
// robust in case platform or machine differences throw off the timing.
(function validate() {
const files = fs.readdirSync(tmpdir.path);

if (files.length === 0)
return setImmediate(validate);

assert.strictEqual(files.length, 2);

for (let i = 0; i < files.length; i++) {
assert.match(files[i], /^Heap\..+\.heapsnapshot$/);
JSON.parse(fs.readFileSync(path.join(tmpdir.path, files[i])));
}
})();
} else {
const { spawnSync } = require('child_process');

tmpdir.refresh();
const args = [
'--heapsnapshot-signal',
'SIGUSR2',
'--diagnostic-dir',
tmpdir.path,
__filename,
'child',
];
const child = spawnSync(process.execPath, args);

assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
}

0 comments on commit 884855a

Please sign in to comment.