From 144ef93735fe213795e8f2b00a7da5f8ec416ea2 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Sun, 5 Apr 2026 18:31:13 -0400 Subject: [PATCH] src: add contextify interceptor debug logs PR-URL: https://github.com/nodejs/node/pull/62460 Reviewed-By: Joyee Cheung --- src/debug_utils-inl.h | 19 +++++++++++++++++++ src/debug_utils.h | 1 + src/node_contextify.cc | 27 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/src/debug_utils-inl.h b/src/debug_utils-inl.h index e3db1ef15b9..3afab862996 100644 --- a/src/debug_utils-inl.h +++ b/src/debug_utils-inl.h @@ -62,6 +62,25 @@ struct ToStringHelper { static std::string Convert(const std::string& value) { return value; } static std::string_view Convert(std::string_view value) { return value; } static std::string Convert(bool value) { return value ? "true" : "false"; } + + static std::string Convert(v8::Local value) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + if (value->IsString()) { + Utf8Value utf8_value(isolate, value); + return SPrintF("\"%s\"", utf8_value.ToString()); + } + v8::MaybeLocal maybe_detail = + value->ToDetailString(isolate->GetCurrentContext()); + v8::Local detail; + if (!maybe_detail.ToLocal(&detail)) { + // This will only occur when terminating. No exception is expected + // with `ToDetailString`. + return ""; + } + Utf8Value utf8_value(isolate, detail); + return utf8_value.ToString(); + } + template >> diff --git a/src/debug_utils.h b/src/debug_utils.h index 587836d73f9..ffb8fe27001 100644 --- a/src/debug_utils.h +++ b/src/debug_utils.h @@ -47,6 +47,7 @@ void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str); V(BOOTSTRAP) \ V(CRYPTO) \ V(COMPILE_CACHE) \ + V(CONTEXTIFY) \ V(DIAGNOSTICS) \ V(HUGEPAGES) \ V(INSPECTOR_SERVER) \ diff --git a/src/node_contextify.cc b/src/node_contextify.cc index a8502aea853..b67b51a0088 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -23,6 +23,7 @@ #include "base_object-inl.h" #include "cppgc/allocation.h" +#include "debug_utils-inl.h" #include "memory_tracker-inl.h" #include "module_wrap.h" #include "node_context_data.h" @@ -484,6 +485,9 @@ Intercepted ContextifyContext::PropertyQueryCallback( return Intercepted::kNo; } + per_process::Debug( + DebugCategory::CONTEXTIFY, "PropertyQuery(%s)\n", property); + Local context = ctx->context(); Local sandbox = ctx->sandbox(); @@ -530,6 +534,9 @@ Intercepted ContextifyContext::PropertyGetterCallback( return Intercepted::kNo; } + per_process::Debug( + DebugCategory::CONTEXTIFY, "PropertyGetter(name: %s)\n", property); + Local context = ctx->context(); Local sandbox = ctx->sandbox(); @@ -567,6 +574,12 @@ Intercepted ContextifyContext::PropertySetterCallback( return Intercepted::kNo; } + per_process::Debug(DebugCategory::CONTEXTIFY, + "PropertySetter(name: %s, value: %s), use-strict(%s)\n", + property, + value, + args.ShouldThrowOnError()); + Local context = ctx->context(); PropertyAttribute attributes = PropertyAttribute::None; bool is_declared_on_global_proxy = ctx->global_proxy() @@ -655,6 +668,9 @@ Intercepted ContextifyContext::PropertyDescriptorCallback( return Intercepted::kNo; } + per_process::Debug( + DebugCategory::CONTEXTIFY, "PropertyDescriptor(name: %s)\n", property); + Local context = ctx->context(); Local sandbox = ctx->sandbox(); @@ -681,6 +697,9 @@ Intercepted ContextifyContext::PropertyDefinerCallback( return Intercepted::kNo; } + per_process::Debug( + DebugCategory::CONTEXTIFY, "PropertyDefiner(name: %s)\n", property); + Local context = ctx->context(); Isolate* isolate = Isolate::GetCurrent(); @@ -751,6 +770,9 @@ Intercepted ContextifyContext::PropertyDeleterCallback( return Intercepted::kNo; } + per_process::Debug( + DebugCategory::CONTEXTIFY, "PropertyDeleter(name: %s)\n", property); + Maybe success = ctx->sandbox()->Delete(ctx->context(), property); if (success.FromMaybe(false)) { @@ -778,6 +800,8 @@ void ContextifyContext::PropertyEnumeratorCallback( // Still initializing if (IsStillInitializing(ctx)) return; + per_process::Debug(DebugCategory::CONTEXTIFY, "PropertyEnumerator()\n"); + Local properties; // Only get own named properties, exclude indices. if (!ctx->sandbox() @@ -809,6 +833,9 @@ void ContextifyContext::IndexedPropertyEnumeratorCallback( // Still initializing if (IsStillInitializing(ctx)) return; + per_process::Debug(DebugCategory::CONTEXTIFY, + "IndexedPropertyEnumerator()\n"); + Local properties; // Only get own index properties.