From 54a8cd91da240a2a33bd7f8675107ee37175d710 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Fri, 27 Mar 2026 10:01:27 -0400 Subject: [PATCH 1/2] src: add contextify interceptor debug logs --- src/debug_utils-inl.h | 20 ++++++++++++++++++++ src/debug_utils.h | 1 + src/node_contextify.cc | 27 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/debug_utils-inl.h b/src/debug_utils-inl.h index e3db1ef15b96f2..aaf81b9935220a 100644 --- a/src/debug_utils-inl.h +++ b/src/debug_utils-inl.h @@ -62,6 +62,26 @@ 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(); + v8::TryCatch scope(isolate); + if (value->IsSymbol()) { + Utf8Value utf8_value(isolate, + value.As()->Description(isolate)); + return SPrintF("", utf8_value.ToString()); + } + if (value->IsString()) { + Utf8Value utf8_value(isolate, value); + return SPrintF("\"%s\"", utf8_value.ToString()); + } + Utf8Value utf8_value(isolate, value); + if (scope.HasCaught()) { + return ""; + } + return utf8_value.ToString(); + } + template >> diff --git a/src/debug_utils.h b/src/debug_utils.h index 8f6165e1b5faf4..0025395051135b 100644 --- a/src/debug_utils.h +++ b/src/debug_utils.h @@ -46,6 +46,7 @@ void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str); NODE_ASYNC_PROVIDER_TYPES(V) \ 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 cf5d9ad2255dca..c5cf1a3277d902 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() @@ -644,6 +657,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(); @@ -670,6 +686,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(); @@ -740,6 +759,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)) { @@ -767,6 +789,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() @@ -798,6 +822,9 @@ void ContextifyContext::IndexedPropertyEnumeratorCallback( // Still initializing if (IsStillInitializing(ctx)) return; + per_process::Debug(DebugCategory::CONTEXTIFY, + "IndexedPropertyEnumerator()\n"); + Local properties; // Only get own index properties. From 4656e6a0adc886f964bd06754c254dcbadc9ebf0 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Fri, 27 Mar 2026 15:32:13 -0400 Subject: [PATCH 2/2] fixup! src: add contextify interceptor debug logs --- src/debug_utils-inl.h | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/debug_utils-inl.h b/src/debug_utils-inl.h index aaf81b9935220a..3afab862996813 100644 --- a/src/debug_utils-inl.h +++ b/src/debug_utils-inl.h @@ -65,20 +65,19 @@ struct ToStringHelper { static std::string Convert(v8::Local value) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::TryCatch scope(isolate); - if (value->IsSymbol()) { - Utf8Value utf8_value(isolate, - value.As()->Description(isolate)); - return SPrintF("", utf8_value.ToString()); - } if (value->IsString()) { Utf8Value utf8_value(isolate, value); return SPrintF("\"%s\"", utf8_value.ToString()); } - Utf8Value utf8_value(isolate, value); - if (scope.HasCaught()) { + 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(); }