Skip to content

Commit

Permalink
Defer parsing of wasm stack symbolication maps until point of first u…
Browse files Browse the repository at this point in the history
…se instead of during startup
  • Loading branch information
kg committed Mar 19, 2024
1 parent b9dfd37 commit ef8780b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/mono/browser/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//!
//! This is generated file, see src/mono/wasm/runtime/rollup.config.js

//! This is not considered public API with backward compatibility guarantees.
//! This is not considered public API with backward compatibility guarantees.

declare interface NativePointer {
__brandNativePointer: "NativePointer";
Expand Down Expand Up @@ -336,7 +336,7 @@ interface AssetEntry {
*/
pendingDownload?: LoadingResource;
}
type SingleAssetBehaviors =
type SingleAssetBehaviors =
/**
* The binary of the .NET runtime.
*/
Expand All @@ -362,14 +362,14 @@ type SingleAssetBehaviors =
*/
| "manifest"
/**
* The debugging symbols
* The debugging symbols used to demangle wasm stack traces; from 'wasm:0x123456' to a C function name
*/
| "symbols"
/**
* Load segmentation rules file for Hybrid Globalization.
*/
| "segmentation-rules";
type AssetBehaviors = SingleAssetBehaviors |
type AssetBehaviors = SingleAssetBehaviors |
/**
* Load asset as a managed resource assembly.
*/
Expand Down
38 changes: 28 additions & 10 deletions src/mono/browser/runtime/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function mono_log_error(msg: string, ...data: any) {
}

export const wasm_func_map = new Map<number, string>();
const wasm_pending_symbol_tables = new Array<string>();
const regexes: any[] = [];

// V8
Expand All @@ -54,6 +55,8 @@ regexes.push(/(?<replaceSection><[^ >]+>[.:]wasm-function\[(?<funcNum>[0-9]+)\])

export function mono_wasm_symbolicate_string(message: string): string {
try {
performDeferredSymbolMapParsing();

if (wasm_func_map.size == 0)
return message;

Expand Down Expand Up @@ -143,22 +146,37 @@ export function mono_wasm_trace_logger(log_domain_ptr: CharPtr, log_level_ptr: C


export function parseSymbolMapFile(text: string) {
text.split(/[\r\n]/).forEach((line: string) => {
const parts: string[] = line.split(/:/);
if (parts.length < 2)
return;

parts[1] = parts.splice(1).join(":");
wasm_func_map.set(Number(parts[0]), parts[1]);
});
// Symbol map parsing is very expensive, so doing it during startup is wasteful
// instead, we defer it until the first time the symbol map is needed - which
// may be never
wasm_pending_symbol_tables.push(text);
mono_log_debug(`Deferred loading of ${text.length}ch symbol map`);
}

mono_log_debug(`Loaded ${wasm_func_map.size} symbols`);
function performDeferredSymbolMapParsing() {
while (wasm_pending_symbol_tables.length > 0) {
const text = wasm_pending_symbol_tables.shift()!;
try {
text.split(/[\r\n]/).forEach((line: string) => {
const parts: string[] = line.split(/:/);
if (parts.length < 2)
return;

parts[1] = parts.splice(1).join(":");
wasm_func_map.set(Number(parts[0]), parts[1]);
});
mono_log_debug(`Loaded ${wasm_func_map.size} symbols`);
} catch (exc) {
mono_log_warn(`Failed to load symbol map: ${exc}`);
}
}
}

export function mono_wasm_get_func_id_to_name_mappings() {
performDeferredSymbolMapParsing();
return [...wasm_func_map.values()];
}

export function mono_wasm_console_clear() {
console.clear();
}
}

0 comments on commit ef8780b

Please sign in to comment.