From c491e4dfe6b2af972ec935524af0e0f0eee894b8 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 16 Jun 2019 20:10:20 +0800 Subject: [PATCH] src: fall back to env->exec_path() for default profile directory When the current working directory is deleted, fall back to exec_path as the default profile directory. PR-URL: https://github.com/nodejs/node/pull/28252 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/inspector_profiler.cc | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc index 1b20398dba4d3d..122118324f57e1 100644 --- a/src/inspector_profiler.cc +++ b/src/inspector_profiler.cc @@ -320,17 +320,20 @@ void EndStartedProfilers(Environment* env) { } } -std::string GetCwd() { +std::string GetCwd(Environment* env) { char cwd[PATH_MAX_BYTES]; size_t size = PATH_MAX_BYTES; int err = uv_cwd(cwd, &size); - // This can fail if the cwd is deleted. - // TODO(joyeecheung): store this in the Environment during Environment - // creation and fallback to exec_path and argv0, then we no longer need - // SetCoverageDirectory(). - CHECK_EQ(err, 0); - CHECK_GT(size, 0); - return cwd; + + if (err == 0) { + CHECK_GT(size, 0); + return cwd; + } + + // This can fail if the cwd is deleted. In that case, fall back to + // exec_path. + const std::string& exec_path = env->exec_path(); + return exec_path.substr(0, exec_path.find_last_of(kPathSeparator)); } void StartProfilers(Environment* env) { @@ -345,7 +348,7 @@ void StartProfilers(Environment* env) { if (env->options()->cpu_prof) { const std::string& dir = env->options()->cpu_prof_dir; env->set_cpu_prof_interval(env->options()->cpu_prof_interval); - env->set_cpu_prof_dir(dir.empty() ? GetCwd() : dir); + env->set_cpu_prof_dir(dir.empty() ? GetCwd(env) : dir); if (env->options()->cpu_prof_name.empty()) { DiagnosticFilename filename(env, "CPU", "cpuprofile"); env->set_cpu_prof_name(*filename); @@ -360,7 +363,7 @@ void StartProfilers(Environment* env) { if (env->options()->heap_prof) { const std::string& dir = env->options()->heap_prof_dir; env->set_heap_prof_interval(env->options()->heap_prof_interval); - env->set_heap_prof_dir(dir.empty() ? GetCwd() : dir); + env->set_heap_prof_dir(dir.empty() ? GetCwd(env) : dir); if (env->options()->heap_prof_name.empty()) { DiagnosticFilename filename(env, "Heap", "heapprofile"); env->set_heap_prof_name(*filename);