Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fall back to env->exec_path() for default profile directory #28252

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,10 @@ inline const std::vector<std::string>& Environment::exec_argv() {
return exec_argv_;
}

inline const std::string& Environment::exec_path() const {
return exec_path_;
}

#if HAVE_INSPECTOR
inline void Environment::set_coverage_directory(const char* dir) {
coverage_directory_ = std::string(dir);
Expand Down
27 changes: 27 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,32 @@ void Environment::CreateProperties() {
set_process_object(process_object);
}

std::string GetExecPath(const std::vector<std::string>& argv) {
char exec_path_buf[2 * PATH_MAX];
size_t exec_path_len = sizeof(exec_path_buf);
std::string exec_path;
if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
exec_path = std::string(exec_path_buf, exec_path_len);
} else {
exec_path = argv[0];
}

// On OpenBSD process.execPath will be relative unless we
// get the full path before process.execPath is used.
#if defined(__OpenBSD__)
uv_fs_t req;
req.ptr = nullptr;
if (0 ==
uv_fs_realpath(env->event_loop(), &req, exec_path.c_str(), nullptr)) {
CHECK_NOT_NULL(req.ptr);
exec_path = std::string(static_cast<char*>(req.ptr));
}
uv_fs_req_cleanup(&req);
#endif

return exec_path;
}

Environment::Environment(IsolateData* isolate_data,
Local<Context> context,
const std::vector<std::string>& args,
Expand All @@ -274,6 +300,7 @@ Environment::Environment(IsolateData* isolate_data,
timer_base_(uv_now(isolate_data->event_loop())),
exec_argv_(exec_args),
argv_(args),
exec_path_(GetExecPath(args)),
should_abort_on_uncaught_toggle_(isolate_, 1),
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
flags_(flags),
Expand Down
2 changes: 2 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ class Environment : public MemoryRetainer {
void InitializeLibuv(bool start_profiler_idle_notifier);
inline const std::vector<std::string>& exec_argv();
inline const std::vector<std::string>& argv();
const std::string& exec_path() const;

typedef void (*HandleCleanupCb)(Environment* env,
uv_handle_t* handle,
Expand Down Expand Up @@ -1261,6 +1262,7 @@ class Environment : public MemoryRetainer {
std::shared_ptr<HostPort> inspector_host_port_;
std::vector<std::string> exec_argv_;
std::vector<std::string> argv_;
std::string exec_path_;

uint32_t module_id_counter_ = 0;
uint32_t script_id_counter_ = 0;
Expand Down
23 changes: 13 additions & 10 deletions src/inspector_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
40 changes: 9 additions & 31 deletions src/node_process_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,37 +180,15 @@ void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
#undef V

// process.execPath
{
char exec_path_buf[2 * PATH_MAX];
size_t exec_path_len = sizeof(exec_path_buf);
std::string exec_path;
if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
exec_path = std::string(exec_path_buf, exec_path_len);
} else {
exec_path = env->argv()[0];
}
// On OpenBSD process.execPath will be relative unless we
// get the full path before process.execPath is used.
#if defined(__OpenBSD__)
uv_fs_t req;
req.ptr = nullptr;
if (0 ==
uv_fs_realpath(env->event_loop(), &req, exec_path.c_str(), nullptr)) {
CHECK_NOT_NULL(req.ptr);
exec_path = std::string(static_cast<char*>(req.ptr));
}
uv_fs_req_cleanup(&req);
#endif
process
->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "execPath"),
String::NewFromUtf8(isolate,
exec_path.c_str(),
NewStringType::kInternalized,
exec_path.size())
.ToLocalChecked())
.Check();
}
process
->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "execPath"),
String::NewFromUtf8(isolate,
env->exec_path().c_str(),
NewStringType::kInternalized,
env->exec_path().size())
.ToLocalChecked())
.Check();

// process.debugPort
CHECK(process
Expand Down