Skip to content

Commit f5880b2

Browse files
authored
Handle more custom sections in wasm-tools dump (#1540)
Hook up a few more custom section parsers to get more useful information in dumps.
1 parent 1064f69 commit f5880b2

7 files changed

Lines changed: 162 additions & 17 deletions

File tree

src/bin/wasm-tools/dump.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -446,23 +446,58 @@ impl<'a> Dump<'a> {
446446
self.color_print(c.range().start)?;
447447
write!(self.state, "name: {:?}", c.name())?;
448448
self.print(c.data_offset())?;
449-
if c.name() == "name" {
450-
let iter = NameSectionReader::new(c.data(), c.data_offset());
451-
self.print_custom_name_section(iter, |me, item, pos| {
452-
me.print_core_name(item, pos)
453-
})?;
454-
} else if c.name() == "component-name" {
455-
let iter = ComponentNameSectionReader::new(c.data(), c.data_offset());
456-
self.print_custom_name_section(iter, |me, item, pos| {
457-
me.print_component_name(item, pos)
458-
})?;
459-
} else {
460-
self.print_byte_header()?;
461-
for _ in 0..NBYTES {
462-
write!(self.dst, "---")?;
449+
match c.name() {
450+
"name" => {
451+
let iter = NameSectionReader::new(c.data(), c.data_offset());
452+
self.print_subsections(iter, |me, item, pos| {
453+
me.print_core_name(item, pos)
454+
})?;
455+
}
456+
"component-name" => {
457+
let iter = ComponentNameSectionReader::new(c.data(), c.data_offset());
458+
self.print_subsections(iter, |me, item, pos| {
459+
me.print_component_name(item, pos)
460+
})?;
461+
}
462+
"producers" => {
463+
let iter = ProducersSectionReader::new(c.data(), c.data_offset())?;
464+
self.print_iter(iter, |me, _pos, item| {
465+
write!(me.state, "field: {}", item.name)?;
466+
me.print(item.values.range().start)?;
467+
468+
me.print_iter(item.values, |me, pos, item| {
469+
write!(me.state, "{item:?}")?;
470+
me.print(pos)
471+
})
472+
})?;
473+
}
474+
"dylink.0" => {
475+
let iter = Dylink0SectionReader::new(c.data(), c.data_offset());
476+
self.print_subsections(iter, |me, item, pos| {
477+
write!(me.state, "{item:?}")?;
478+
me.print(pos)
479+
})?;
480+
}
481+
"metadata.code.branch_hint" => {
482+
let iter = BranchHintSectionReader::new(c.data(), c.data_offset())?;
483+
self.print_iter(iter, |me, _pos, item| {
484+
write!(me.state, "func: {}", item.func)?;
485+
me.print(item.hints.range().start)?;
486+
487+
me.print_iter(item.hints, |me, pos, item| {
488+
write!(me.state, "{item:?}")?;
489+
me.print(pos)
490+
})
491+
})?;
492+
}
493+
_ => {
494+
self.print_byte_header()?;
495+
for _ in 0..NBYTES {
496+
write!(self.dst, "---")?;
497+
}
498+
writeln!(self.dst, "-| ... {} bytes of data", c.data().len())?;
499+
self.cur += c.data().len();
463500
}
464-
writeln!(self.dst, "-| ... {} bytes of data", c.data().len())?;
465-
self.cur += c.data().len();
466501
}
467502
}
468503
Payload::UnknownSection {
@@ -510,7 +545,7 @@ impl<'a> Dump<'a> {
510545
})
511546
}
512547

513-
fn print_custom_name_section<'b, T>(
548+
fn print_subsections<'b, T>(
514549
&mut self,
515550
mut section: Subsections<'b, T>,
516551
print_item: impl Fn(&mut Self, T, usize) -> Result<()>,

tests/cli/dump-branch-hints.wat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
;; RUN: dump %
2+
3+
(module
4+
(func
5+
i32.const 0
6+
(@metadata.code.branch_hint "\00")
7+
if
8+
end
9+
10+
(@metadata.code.branch_hint "\01")
11+
if
12+
end
13+
)
14+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
0x0 | 00 61 73 6d | version 1 (Module)
2+
| 01 00 00 00
3+
0x8 | 01 04 | type section
4+
0xa | 01 | 1 count
5+
--- rec group 0 (implicit) ---
6+
0xb | 60 00 00 | [type 0] SubType { is_final: true, supertype_idx: None, composite_type: Func(FuncType { params: [], results: [] }) }
7+
0xe | 03 02 | func section
8+
0x10 | 01 | 1 count
9+
0x11 | 00 | [func 0] type 0
10+
0x12 | 00 23 | custom section
11+
0x14 | 19 6d 65 74 | name: "metadata.code.branch_hint"
12+
| 61 64 61 74
13+
| 61 2e 63 6f
14+
| 64 65 2e 62
15+
| 72 61 6e 63
16+
| 68 5f 68 69
17+
| 6e 74
18+
0x2e | 01 | 1 count
19+
0x2f | 00 | func: 0
20+
0x30 | 02 | 2 count
21+
0x31 | 03 01 00 | BranchHint { func_offset: 3, taken: false }
22+
0x34 | 06 01 01 | BranchHint { func_offset: 6, taken: true }
23+
0x37 | 0a 0c | code section
24+
0x39 | 01 | 1 count
25+
============== func 0 ====================
26+
0x3a | 0a | size of function
27+
0x3b | 00 | 0 local blocks
28+
0x3c | 41 00 | i32_const value:0
29+
0x3e | 04 40 | if blockty:Empty
30+
0x40 | 0b | end
31+
0x41 | 04 40 | if blockty:Empty
32+
0x43 | 0b | end
33+
0x44 | 0b | end

tests/cli/dump-dylink0.wat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
;; RUN: dump %
2+
3+
(module
4+
(@dylink.0
5+
(needed "a" "b")
6+
(mem-info (memory 1 1) (table 1 1))
7+
(export-info "a" 0)
8+
(import-info "a" "a" 0)
9+
(export-info "a" 2 binding-local binding-weak 0 undefined)
10+
)
11+
)

tests/cli/dump-dylink0.wat.stdout

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
0x0 | 00 61 73 6d | version 1 (Module)
2+
| 01 00 00 00
3+
0x8 | 00 2a | custom section
4+
0xa | 08 64 79 6c | name: "dylink.0"
5+
| 69 6e 6b 2e
6+
| 30
7+
0x13 | 02 05 02 01 | Needed(["a", "b"])
8+
| 61 01 62
9+
0x1a | 01 04 01 01 | MemInfo(MemInfo { memory_size: 1, memory_alignment: 1, table_size: 1, table_alignment: 1 })
10+
| 01 01
11+
0x20 | 03 04 01 01 | ExportInfo([ExportInfo { name: "a", flags: SymbolFlags(0x0) }])
12+
| 61 00
13+
0x26 | 04 06 01 01 | ImportInfo([ImportInfo { module: "a", field: "a", flags: SymbolFlags(0x0) }])
14+
| 61 01 61 00
15+
0x2e | 03 04 01 01 | ExportInfo([ExportInfo { name: "a", flags: SymbolFlags(BINDING_WEAK | BINDING_LOCAL | UNDEFINED) }])
16+
| 61 13
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
;; RUN: dump %
2+
3+
(module
4+
(@producers
5+
(language "foo" "bar")
6+
(language "foo" "bar")
7+
(sdk "foo" "bar")
8+
(processed-by "foo" "bar")
9+
)
10+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
0x0 | 00 61 73 6d | version 1 (Module)
2+
| 01 00 00 00
3+
0x8 | 00 48 | custom section
4+
0xa | 09 70 72 6f | name: "producers"
5+
| 64 75 63 65
6+
| 72 73
7+
0x14 | 03 | 3 count
8+
0x15 | 08 6c 61 6e | field: language
9+
| 67 75 61 67
10+
| 65
11+
0x1e | 02 | 2 count
12+
0x1f | 03 66 6f 6f | ProducersFieldValue { name: "foo", version: "bar" }
13+
| 03 62 61 72
14+
0x27 | 03 66 6f 6f | ProducersFieldValue { name: "foo", version: "bar" }
15+
| 03 62 61 72
16+
0x2f | 03 73 64 6b | field: sdk
17+
0x33 | 01 | 1 count
18+
0x34 | 03 66 6f 6f | ProducersFieldValue { name: "foo", version: "bar" }
19+
| 03 62 61 72
20+
0x3c | 0c 70 72 6f | field: processed-by
21+
| 63 65 73 73
22+
| 65 64 2d 62
23+
| 79
24+
0x49 | 01 | 1 count
25+
0x4a | 03 66 6f 6f | ProducersFieldValue { name: "foo", version: "bar" }
26+
| 03 62 61 72

0 commit comments

Comments
 (0)