From 32f80efe7e00a3a05d318f86ec9a06a94094c465 Mon Sep 17 00:00:00 2001 From: Benedikt Meurer Date: Wed, 12 May 2021 09:42:16 +0200 Subject: [PATCH] [inspector] Improve RemoteObject description for Wasm functions. The WebAssembly specification requires the "name" property of (exported) function wrappers to hold the index of the function within the module, and the default ToString algorithm for Function instances thus generates something along the lines of `function 42() { [native code] }`, which is technically correct, but not very useful to developers to diagnose (humans don't think of functions in a module in terms of their indices). With this CL, we change the description returned for Wasm (exported) functions to use the debug name of the Wasm function instead. Screenshot: https://imgur.com/a/FVPeXDU.png Doc: http://bit.ly/devtools-wasm-entities Fixed: chromium:1206620 Bug: chromium:1164241 Change-Id: I096abc287ea077556c13c71f8d71f64452ab4831 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2891570 Commit-Queue: Benedikt Meurer Auto-Submit: Benedikt Meurer Reviewed-by: Kim-Anh Tran Cr-Commit-Position: refs/heads/master@{#74517} --- src/debug/debug-interface.cc | 25 +++++++++++++++++-- .../wasm-evaluate-on-call-frame-expected.txt | 24 +++++++++--------- .../debugger/wasm-gc-breakpoints-expected.txt | 2 +- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/debug/debug-interface.cc b/src/debug/debug-interface.cc index 890ffb7b5b..0d56002e6e 100644 --- a/src/debug/debug-interface.cc +++ b/src/debug/debug-interface.cc @@ -14,6 +14,7 @@ #include "src/objects/js-generator-inl.h" #include "src/objects/stack-frame-info-inl.h" #include "src/regexp/regexp-stack.h" +#include "src/strings/string-builder-inl.h" #if V8_ENABLE_WEBASSEMBLY #include "src/debug/debug-wasm-objects-inl.h" @@ -65,8 +66,28 @@ Local GetFunctionDescription(Local function) { i::Handle::cast(receiver))); } if (receiver->IsJSFunction()) { - return Utils::ToLocal( - i::JSFunction::ToString(i::Handle::cast(receiver))); + auto function = i::Handle::cast(receiver); +#if V8_ENABLE_WEBASSEMBLY + if (function->shared().HasWasmExportedFunctionData()) { + auto isolate = function->GetIsolate(); + auto func_index = + function->shared().wasm_exported_function_data().function_index(); + auto instance = i::handle( + function->shared().wasm_exported_function_data().instance(), isolate); + if (instance->module()->origin == i::wasm::kWasmOrigin) { + // For asm.js functions, we can still print the source + // code (hopefully), so don't bother with them here. + auto debug_name = + i::GetWasmFunctionDebugName(isolate, instance, func_index); + i::IncrementalStringBuilder builder(isolate); + builder.AppendCString("function "); + builder.AppendString(debug_name); + builder.AppendCString("() { [native code] }"); + return Utils::ToLocal(builder.Finish().ToHandleChecked()); + } + } +#endif // V8_ENABLE_WEBASSEMBLY + return Utils::ToLocal(i::JSFunction::ToString(function)); } return Utils::ToLocal( receiver->GetIsolate()->factory()->function_native_code_string()); diff --git a/test/inspector/debugger/wasm-evaluate-on-call-frame-expected.txt b/test/inspector/debugger/wasm-evaluate-on-call-frame-expected.txt index 11f9f20abc..2605342e0c 100644 --- a/test/inspector/debugger/wasm-evaluate-on-call-frame-expected.txt +++ b/test/inspector/debugger/wasm-evaluate-on-call-frame-expected.txt @@ -55,18 +55,18 @@ Debugger paused in $main. > functions = Functions > typeof functions = "object" > Object.keys(functions) = Array(4) -> functions[0] = function 0() { [native code] } -> functions[1] = function 1() { [native code] } -> functions[2] = function 2() { [native code] } -> functions[3] = function 3() { [native code] } -> functions[4] = function 4() { [native code] } -> functions["$foo.bar"] = function 0() { [native code] } -> functions["$main"] = function 1() { [native code] } -> $main = function 1() { [native code] } -> functions["$func2"] = function 2() { [native code] } -> $func2 = function 2() { [native code] } -> functions["$func4"] = function 4() { [native code] } -> $func4 = function 4() { [native code] } +> functions[0] = function $foo.bar() { [native code] } +> functions[1] = function $main() { [native code] } +> functions[2] = function $func2() { [native code] } +> functions[3] = function $func2() { [native code] } +> functions[4] = function $func4() { [native code] } +> functions["$foo.bar"] = function $foo.bar() { [native code] } +> functions["$main"] = function $main() { [native code] } +> $main = function $main() { [native code] } +> functions["$func2"] = function $func2() { [native code] } +> $func2 = function $func2() { [native code] } +> functions["$func4"] = function $func4() { [native code] } +> $func4 = function $func4() { [native code] } Running test: testLocals Compile module. diff --git a/test/inspector/debugger/wasm-gc-breakpoints-expected.txt b/test/inspector/debugger/wasm-gc-breakpoints-expected.txt index 68d938ed39..68f2801b6e 100644 --- a/test/inspector/debugger/wasm-gc-breakpoints-expected.txt +++ b/test/inspector/debugger/wasm-gc-breakpoints-expected.txt @@ -25,7 +25,7 @@ at $main (0:107): instance: exports: "main" (Function) module: Module functions: "$main": (Function) - globals: "$global0": function 0() { [native code] } ((ref null $type3)) + globals: "$global0": function $main() { [native code] } ((ref null $type3)) at (anonymous) (0:17): -- skipped exports.main returned!