diff --git a/src/node.cc b/src/node.cc index 0d62c89b030703..a9759041b0a21c 100644 --- a/src/node.cc +++ b/src/node.cc @@ -194,6 +194,7 @@ static node_module* modlist_builtin; static node_module* modlist_internal; static node_module* modlist_linked; static node_module* modlist_addon; +static bool trace_forced = false; static bool trace_enabled = false; static std::string trace_enabled_categories; // NOLINT(runtime/string) static bool abort_on_uncaught_exception = false; @@ -331,6 +332,16 @@ static struct { tracing_agent_->Stop(); } + void SetTracingAgent(tracing::Agent* agent) { + if (agent) { + tracing_agent_.reset(agent); + platform_->SetTracingController(agent->GetTracingController()); + } else { + tracing_agent_.reset(nullptr); + platform_->SetTracingController(nullptr); + } + } + NodePlatform* Platform() { return platform_; } @@ -353,6 +364,7 @@ static struct { "so event tracing is not available.\n"); } void StopTracingAgent() {} + void SetTracingAgent(tracing::Agent* agent) {} NodePlatform* Platform() { return nullptr; @@ -1996,6 +2008,35 @@ static void Exit(const FunctionCallbackInfo& args) { } +static void StartTracingAgent(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + if (!trace_enabled) { + if (args[0]->IsString()) { + Utf8Value categories(env->isolate(), args[0]); + trace_enabled_categories = *categories; + } + trace_enabled = true; + tracing::Agent* agent = new tracing::Agent(); + v8_platform.SetTracingAgent(agent); + tracing::TraceEventHelper::SetTracingController( + agent->GetTracingController()); + v8_platform.StartTracingAgent(); + return args.GetReturnValue().Set(true); + } + args.GetReturnValue().Set(false); +} + +static void StopTracingAgent(const FunctionCallbackInfo& args) { + if (trace_enabled && !trace_forced) { + v8_platform.StopTracingAgent(); + v8_platform.SetTracingAgent(nullptr); + tracing::TraceEventHelper::SetTracingController(nullptr); + trace_enabled = false; + return args.GetReturnValue().Set(true); + } + args.GetReturnValue().Set(false); +} + static void Uptime(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); double uptime; @@ -3234,6 +3275,9 @@ void SetupProcessObject(Environment* env, env->SetMethod(process, "uptime", Uptime); env->SetMethod(process, "memoryUsage", MemoryUsage); + env->SetMethod(process, "startTracingAgent", StartTracingAgent); + env->SetMethod(process, "stopTracingAgent", StopTracingAgent); + env->SetMethod(process, "binding", Binding); env->SetMethod(process, "_linkedBinding", LinkedBinding); env->SetMethod(process, "_internalBinding", InternalBinding); @@ -3667,6 +3711,7 @@ static void ParseArgs(int* argc, } else if (strcmp(arg, "--no-force-async-hooks-checks") == 0) { no_force_async_hooks_checks = true; } else if (strcmp(arg, "--trace-events-enabled") == 0) { + trace_forced = true; trace_enabled = true; } else if (strcmp(arg, "--trace-event-categories") == 0) { const char* categories = argv[index + 1]; diff --git a/src/node_platform.cc b/src/node_platform.cc index d96db086925c01..63f2b28e2404cc 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -114,12 +114,7 @@ int PerIsolatePlatformData::unref() { NodePlatform::NodePlatform(int thread_pool_size, TracingController* tracing_controller) { - if (tracing_controller) { - tracing_controller_.reset(tracing_controller); - } else { - TracingController* controller = new TracingController(); - tracing_controller_.reset(controller); - } + SetTracingController(tracing_controller); background_task_runner_ = std::make_shared(thread_pool_size); } @@ -287,6 +282,14 @@ TracingController* NodePlatform::GetTracingController() { return tracing_controller_.get(); } +void NodePlatform::SetTracingController(v8::TracingController* controller) { + if (controller) { + tracing_controller_.reset(controller); + } else { + tracing_controller_.reset(new TracingController()); + } +} + template TaskQueue::TaskQueue() : lock_(), tasks_available_(), tasks_drained_(), diff --git a/src/node_platform.h b/src/node_platform.h index b7546057871a1d..e27812f0c3e80e 100644 --- a/src/node_platform.h +++ b/src/node_platform.h @@ -129,6 +129,7 @@ class NodePlatform : public MultiIsolatePlatform { double MonotonicallyIncreasingTime() override; double CurrentClockTimeMillis() override; v8::TracingController* GetTracingController() override; + void SetTracingController(v8::TracingController* controller); void FlushForegroundTasks(v8::Isolate* isolate);