Commit 18084f17 authored by clemensh's avatar clemensh Committed by Commit bot

[wasm] Implement frame printing for debug

This makes wasm frames show up nicely in stack traces generated e.g. by
Isolate::PrintStack() and Isolate::PrintCurrentStackTrace().
With this CL, we print the script name, function index, function name,
pc and source position.

R=titzer@chromium.org, ahaas@chromium.org

Review-Url: https://codereview.chromium.org/2509323002
Cr-Commit-Position: refs/heads/master@{#41102}
parent f7795cbf
......@@ -1487,7 +1487,28 @@ void StackFrame::PrintIndex(StringStream* accumulator,
void WasmFrame::Print(StringStream* accumulator, PrintMode mode,
int index) const {
accumulator->Add("wasm frame");
PrintIndex(accumulator, mode, index);
accumulator->Add("WASM [");
Script* script = this->script();
accumulator->PrintName(script->name());
int pc = static_cast<int>(this->pc() - LookupCode()->instruction_start());
Vector<const uint8_t> raw_func_name;
Object* instance_or_undef = this->wasm_instance();
if (instance_or_undef->IsUndefined(this->isolate())) {
raw_func_name = STATIC_CHAR_VECTOR("<undefined>");
} else {
raw_func_name = WasmInstanceObject::cast(instance_or_undef)
->get_compiled_module()
->GetRawFunctionName(this->function_index());
}
const int kMaxPrintedFunctionName = 64;
char func_name[kMaxPrintedFunctionName + 1];
int func_name_len = std::min(kMaxPrintedFunctionName, raw_func_name.length());
memcpy(func_name, raw_func_name.start(), func_name_len);
func_name[func_name_len] = '\0';
accumulator->Add("], function #%u ('%s'), pc=%p, pos=%d\n",
this->function_index(), func_name, pc, this->position());
if (mode != OVERVIEW) accumulator->Add("\n");
}
Code* WasmFrame::unchecked_code() const {
......
......@@ -312,7 +312,7 @@ Handle<WasmCompiledModule> WasmCompiledModule::New(
}
wasm::WasmModule* WasmCompiledModule::module() const {
return reinterpret_cast<WasmModuleWrapper*>(*module_wrapper())->get();
return reinterpret_cast<WasmModuleWrapper*>(ptr_to_module_wrapper())->get();
}
void WasmCompiledModule::InitId() {
......@@ -365,3 +365,15 @@ uint32_t WasmCompiledModule::mem_size() const {
uint32_t WasmCompiledModule::default_mem_size() const {
return min_mem_pages() * WasmModule::kPageSize;
}
Vector<const uint8_t> WasmCompiledModule::GetRawFunctionName(
uint32_t func_index) {
DCHECK_GT(module()->functions.size(), func_index);
WasmFunction& function = module()->functions[func_index];
SeqOneByteString* bytes = ptr_to_module_bytes();
DCHECK_GE(static_cast<size_t>(bytes->length()), function.name_offset);
DCHECK_GE(static_cast<size_t>(bytes->length() - function.name_offset),
function.name_length);
return Vector<const uint8_t>(bytes->GetCharsAddress() + function.name_offset,
function.name_length);
}
......@@ -259,12 +259,18 @@ class WasmCompiledModule : public FixedArray {
static void RecreateModuleWrapper(Isolate* isolate,
Handle<FixedArray> compiled_module);
// Extract a function name from the given wasm instance.
// Get the function name of the function identified by the given index.
// Returns a null handle if the function is unnamed or the name is not a valid
// UTF-8 string.
static MaybeHandle<String> GetFunctionName(
Handle<WasmCompiledModule> compiled_module, uint32_t func_index);
// Get the raw bytes of the function name of the function identified by the
// given index.
// Meant to be used for debugging or frame printing.
// Does not allocate, hence gc-safe.
Vector<const uint8_t> GetRawFunctionName(uint32_t func_index);
private:
void InitId();
......
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