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) {
isolate->is_profiling();
}
void WasmCode::LogCode(Isolate* isolate, const char* source_url,
int script_id) const {
DCHECK(ShouldBeLogged(isolate));
if (IsAnonymous()) return;
std::string WasmCode::DebugName() const {
if (IsAnonymous()) {
return "anonymous function";
}
ModuleWireBytes wire_bytes(native_module_->wire_bytes());
const WasmModule* module = native_module_->module();
ModuleWireBytes wire_bytes(native_module()->wire_bytes());
const WasmModule* module = native_module()->module();
WireBytesRef name_ref =
module->lazily_generated_names.LookupFunctionName(wire_bytes, index());
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;
if (kind() == kWasmToJsWrapper) {
name_buffer = "wasm-to-js:";
size_t prefix_len = name_buffer.size();
constexpr size_t kMaxSigLength = 128;
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(
base::VectorOf(&name_buffer[prefix_len], kMaxSigLength), sig);
name_buffer.resize(prefix_len + sig_length);
......@@ -259,13 +242,41 @@ void WasmCode::LogCode(Isolate* isolate, const char* source_url,
name_buffer += '-';
name_buffer.append(name.begin(), name.size());
}
name = base::VectorOf(name_buffer);
} else if (name.empty()) {
name_buffer.resize(32);
name_buffer.resize(
SNPrintF(base::VectorOf(&name_buffer.front(), name_buffer.size()),
"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,
......@@ -334,7 +345,7 @@ void WasmCode::Validate() const {
#endif
}
void WasmCode::MaybePrint(const char* name) const {
void WasmCode::MaybePrint() const {
// Determines whether flags want this code to be printed.
bool function_index_matches =
(!IsAnonymous() &&
......@@ -342,7 +353,8 @@ void WasmCode::MaybePrint(const char* name) const {
if (FLAG_print_code ||
(kind() == kFunction ? (FLAG_print_wasm_code || function_index_matches)
: 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(
safepoint_table_offset, handler_table_offset, constant_pool_offset,
code_comments_offset, instr_size, protected_instructions_data, reloc_info,
source_position_table, kind, tier, for_debugging}};
code->MaybePrint();
code->Validate();
......
......@@ -318,7 +318,7 @@ class V8_EXPORT_PRIVATE WasmCode final {
void Validate() 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,
Address current_pc = kNullAddress) const;
......@@ -420,6 +420,10 @@ class V8_EXPORT_PRIVATE WasmCode final {
std::unique_ptr<const byte[]> ConcatenateBytes(
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
// this process, will have a {trap_handler_index} associated with them.
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