Commit a207c15b authored by Ng Zhi An's avatar Ng Zhi An Committed by V8 LUCI CQ

[wasm] Print function name in disassembly

We already have some logic to try to get a reasonable name for the
function when logging code. It looks up the name custom section, and
falls back to the function index. Extract this into a helper, and call
it when disassembly the code.

Bug: v8:12098
Change-Id: Ieebe6594bc3184fa655f878faa0cb67c248d7f56
Fixed: v8:12098
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3125355Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76769}
parent ce11ac40
...@@ -217,40 +217,23 @@ bool WasmCode::ShouldBeLogged(Isolate* isolate) { ...@@ -217,40 +217,23 @@ bool WasmCode::ShouldBeLogged(Isolate* isolate) {
isolate->is_profiling(); isolate->is_profiling();
} }
void WasmCode::LogCode(Isolate* isolate, const char* source_url, std::string WasmCode::DebugName() const {
int script_id) const { if (IsAnonymous()) {
DCHECK(ShouldBeLogged(isolate)); return "anonymous function";
if (IsAnonymous()) return; }
ModuleWireBytes wire_bytes(native_module_->wire_bytes()); ModuleWireBytes wire_bytes(native_module()->wire_bytes());
const WasmModule* module = native_module_->module(); const WasmModule* module = native_module()->module();
WireBytesRef name_ref = WireBytesRef name_ref =
module->lazily_generated_names.LookupFunctionName(wire_bytes, index()); module->lazily_generated_names.LookupFunctionName(wire_bytes, index());
WasmName name = wire_bytes.GetNameOrNull(name_ref); WasmName name = wire_bytes.GetNameOrNull(name_ref);
const WasmDebugSymbols& debug_symbols = module->debug_symbols;
auto load_wasm_source_map = isolate->wasm_load_source_map_callback();
auto source_map = native_module_->GetWasmSourceMap();
if (!source_map && debug_symbols.type == WasmDebugSymbols::Type::SourceMap &&
!debug_symbols.external_url.is_empty() && load_wasm_source_map) {
WasmName external_url =
wire_bytes.GetNameOrNull(debug_symbols.external_url);
std::string external_url_string(external_url.data(), external_url.size());
HandleScope scope(isolate);
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
Local<v8::String> source_map_str =
load_wasm_source_map(v8_isolate, external_url_string.c_str());
native_module_->SetWasmSourceMap(
std::make_unique<WasmModuleSourceMap>(v8_isolate, source_map_str));
}
std::string name_buffer; std::string name_buffer;
if (kind() == kWasmToJsWrapper) { if (kind() == kWasmToJsWrapper) {
name_buffer = "wasm-to-js:"; name_buffer = "wasm-to-js:";
size_t prefix_len = name_buffer.size(); size_t prefix_len = name_buffer.size();
constexpr size_t kMaxSigLength = 128; constexpr size_t kMaxSigLength = 128;
name_buffer.resize(prefix_len + kMaxSigLength); name_buffer.resize(prefix_len + kMaxSigLength);
const FunctionSig* sig = module->functions[index_].sig; const FunctionSig* sig = module->functions[index()].sig;
size_t sig_length = PrintSignature( size_t sig_length = PrintSignature(
base::VectorOf(&name_buffer[prefix_len], kMaxSigLength), sig); base::VectorOf(&name_buffer[prefix_len], kMaxSigLength), sig);
name_buffer.resize(prefix_len + sig_length); name_buffer.resize(prefix_len + sig_length);
...@@ -259,13 +242,41 @@ void WasmCode::LogCode(Isolate* isolate, const char* source_url, ...@@ -259,13 +242,41 @@ void WasmCode::LogCode(Isolate* isolate, const char* source_url,
name_buffer += '-'; name_buffer += '-';
name_buffer.append(name.begin(), name.size()); name_buffer.append(name.begin(), name.size());
} }
name = base::VectorOf(name_buffer);
} else if (name.empty()) { } else if (name.empty()) {
name_buffer.resize(32); name_buffer.resize(32);
name_buffer.resize( name_buffer.resize(
SNPrintF(base::VectorOf(&name_buffer.front(), name_buffer.size()), SNPrintF(base::VectorOf(&name_buffer.front(), name_buffer.size()),
"wasm-function[%d]", index())); "wasm-function[%d]", index()));
name = base::VectorOf(name_buffer); } else {
name_buffer.append(name.begin(), name.end());
}
return name_buffer;
}
void WasmCode::LogCode(Isolate* isolate, const char* source_url,
int script_id) const {
DCHECK(ShouldBeLogged(isolate));
if (IsAnonymous()) return;
ModuleWireBytes wire_bytes(native_module_->wire_bytes());
const WasmModule* module = native_module_->module();
std::string fn_name = DebugName();
WasmName name = base::VectorOf(fn_name);
const WasmDebugSymbols& debug_symbols = module->debug_symbols;
auto load_wasm_source_map = isolate->wasm_load_source_map_callback();
auto source_map = native_module_->GetWasmSourceMap();
if (!source_map && debug_symbols.type == WasmDebugSymbols::Type::SourceMap &&
!debug_symbols.external_url.is_empty() && load_wasm_source_map) {
WasmName external_url =
wire_bytes.GetNameOrNull(debug_symbols.external_url);
std::string external_url_string(external_url.data(), external_url.size());
HandleScope scope(isolate);
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
Local<v8::String> source_map_str =
load_wasm_source_map(v8_isolate, external_url_string.c_str());
native_module_->SetWasmSourceMap(
std::make_unique<WasmModuleSourceMap>(v8_isolate, source_map_str));
} }
// Record source positions before adding code, otherwise when code is added, // Record source positions before adding code, otherwise when code is added,
...@@ -334,7 +345,7 @@ void WasmCode::Validate() const { ...@@ -334,7 +345,7 @@ void WasmCode::Validate() const {
#endif #endif
} }
void WasmCode::MaybePrint(const char* name) const { void WasmCode::MaybePrint() const {
// Determines whether flags want this code to be printed. // Determines whether flags want this code to be printed.
bool function_index_matches = bool function_index_matches =
(!IsAnonymous() && (!IsAnonymous() &&
...@@ -342,7 +353,8 @@ void WasmCode::MaybePrint(const char* name) const { ...@@ -342,7 +353,8 @@ void WasmCode::MaybePrint(const char* name) const {
if (FLAG_print_code || if (FLAG_print_code ||
(kind() == kFunction ? (FLAG_print_wasm_code || function_index_matches) (kind() == kFunction ? (FLAG_print_wasm_code || function_index_matches)
: FLAG_print_wasm_stub_code)) { : FLAG_print_wasm_stub_code)) {
Print(name); std::string name = DebugName();
Print(name.c_str());
} }
} }
...@@ -1255,6 +1267,7 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace( ...@@ -1255,6 +1267,7 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace(
safepoint_table_offset, handler_table_offset, constant_pool_offset, safepoint_table_offset, handler_table_offset, constant_pool_offset,
code_comments_offset, instr_size, protected_instructions_data, reloc_info, code_comments_offset, instr_size, protected_instructions_data, reloc_info,
source_position_table, kind, tier, for_debugging}}; source_position_table, kind, tier, for_debugging}};
code->MaybePrint(); code->MaybePrint();
code->Validate(); code->Validate();
......
...@@ -318,7 +318,7 @@ class V8_EXPORT_PRIVATE WasmCode final { ...@@ -318,7 +318,7 @@ class V8_EXPORT_PRIVATE WasmCode final {
void Validate() const; void Validate() const;
void Print(const char* name = nullptr) const; void Print(const char* name = nullptr) const;
void MaybePrint(const char* name = nullptr) const; void MaybePrint() const;
void Disassemble(const char* name, std::ostream& os, void Disassemble(const char* name, std::ostream& os,
Address current_pc = kNullAddress) const; Address current_pc = kNullAddress) const;
...@@ -420,6 +420,10 @@ class V8_EXPORT_PRIVATE WasmCode final { ...@@ -420,6 +420,10 @@ class V8_EXPORT_PRIVATE WasmCode final {
std::unique_ptr<const byte[]> ConcatenateBytes( std::unique_ptr<const byte[]> ConcatenateBytes(
std::initializer_list<base::Vector<const byte>>); std::initializer_list<base::Vector<const byte>>);
// Tries to get a reasonable name. Lazily looks up the name section, and falls
// back to the function index. Return value is guaranteed to not be empty.
std::string DebugName() const;
// Code objects that have been registered with the global trap handler within // Code objects that have been registered with the global trap handler within
// this process, will have a {trap_handler_index} associated with them. // this process, will have a {trap_handler_index} associated with them.
int trap_handler_index() const { int trap_handler_index() const {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment