aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/BunJSCModule.cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-07-02 04:05:04 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-07-02 04:05:04 -0700
commitefdc60372fa3e753a821f69c75d8a5d501f25427 (patch)
tree12372c1f8d6bd6148b82d4d34e563b504582ae55 /src/bun.js/bindings/BunJSCModule.cpp
parent6f3a092564e25a62d04fc45555be6987c8d715e2 (diff)
downloadbun-efdc60372fa3e753a821f69c75d8a5d501f25427.tar.gz
bun-efdc60372fa3e753a821f69c75d8a5d501f25427.tar.zst
bun-efdc60372fa3e753a821f69c75d8a5d501f25427.zip
[bun:jsc] Add `generateHeapSnapshotForDebugging`
Diffstat (limited to 'src/bun.js/bindings/BunJSCModule.cpp')
-rw-r--r--src/bun.js/bindings/BunJSCModule.cpp29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/bun.js/bindings/BunJSCModule.cpp b/src/bun.js/bindings/BunJSCModule.cpp
index dd5c4b062..3b5995fa0 100644
--- a/src/bun.js/bindings/BunJSCModule.cpp
+++ b/src/bun.js/bindings/BunJSCModule.cpp
@@ -20,6 +20,11 @@
#include "JavaScriptCore/Completion.h"
#include "JavaScriptCore/Error.h"
#include "JavaScriptCore/ErrorInstance.h"
+#include "JavaScriptCore/HeapSnapshotBuilder.h"
+#include "JavaScriptCore/JSONObject.h"
+#include "JavaScriptCore/DeferTermination.h"
+#include "JavaScriptCore/VMTrapsInlines.h"
+
#include "mimalloc.h"
using namespace JSC;
@@ -339,6 +344,27 @@ JSC_DEFINE_HOST_FUNCTION(functionDrainMicrotasks, (JSGlobalObject * globalObject
return JSValue::encode(jsUndefined());
}
+JSC_DECLARE_HOST_FUNCTION(functionGenerateHeapSnapshotForDebugging);
+JSC_DEFINE_HOST_FUNCTION(functionGenerateHeapSnapshotForDebugging, (JSGlobalObject * globalObject, CallFrame*))
+{
+ VM& vm = globalObject->vm();
+ JSLockHolder lock(vm);
+ DeferTermination deferScope(vm);
+ auto scope = DECLARE_THROW_SCOPE(vm);
+ String jsonString;
+ {
+ DeferGCForAWhile deferGC(vm); // Prevent concurrent GC from interfering with the full GC that the snapshot does.
+
+ HeapSnapshotBuilder snapshotBuilder(vm.ensureHeapProfiler(), HeapSnapshotBuilder::SnapshotType::GCDebuggingSnapshot);
+ snapshotBuilder.buildSnapshot();
+
+ jsonString = snapshotBuilder.json();
+ }
+ scope.releaseAssertNoException();
+
+ return JSValue::encode(JSONParse(globalObject, WTFMove(jsonString)));
+}
+
JSC::JSObject* createJSCModule(JSC::JSGlobalObject* globalObject)
{
VM& vm = globalObject->vm();
@@ -346,7 +372,7 @@ JSC::JSObject* createJSCModule(JSC::JSGlobalObject* globalObject)
{
JSC::ObjectInitializationScope initializationScope(vm);
- object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 22);
+ object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 23);
object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "callerSourceOrigin"_s), 1, functionCallerSourceOrigin, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "describe"_s), 1, functionDescribe, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "describeArray"_s), 1, functionDescribeArray, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
@@ -369,6 +395,7 @@ JSC::JSObject* createJSCModule(JSC::JSGlobalObject* globalObject)
object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "startRemoteDebugger"_s), 2, functionStartRemoteDebugger, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "totalCompileTime"_s), 1, functionTotalCompileTime, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "getProtectedObjects"_s), 1, functionGetProtectedObjects, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
+ object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "generateHeapSnapshotForDebugging"_s), 0, functionGenerateHeapSnapshotForDebugging, NoIntrinsic, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | 0);
}
return object;