Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show function names in explore tool instead of only function indices #8639

Merged
merged 5 commits into from
May 21, 2024

Conversation

lpereira
Copy link
Contributor

Another one in the "let's make debugging a bit easier": show function names in the "explore" tool. (There's a bit of code repetition here to clean up the names, but it's slightly different when we're considering HTML output, so I think that's OK.)

Here's how it works on my machine:
image

@lpereira lpereira requested a review from a team as a code owner May 16, 2024 21:23
@lpereira lpereira requested review from pchickey and removed request for a team May 16, 2024 21:23
@lpereira lpereira force-pushed the show-func-names-in-explore branch from 9ef98c3 to deccfe3 Compare May 16, 2024 21:32
@jameysharp
Copy link
Contributor

Very nice!

This can be further improved by demangling the function names first. For some example code that does that, see:

let mut name = String::new();
demangle_function_name_or_index(
&mut name,
compiled.func_name(func_idx),
defined_idx.as_u32() as usize,
)
.unwrap();

I don't think we need to be nearly so careful about sanitizing function names in this setting. These strings are first serialized to JSON, and that should ensure that any string we provide is valid JSON. The JSON string is then used in a <script> tag; we might need to escape the JSON for embedding in HTML, but <script> tags are implicitly treated as CDATA in HTML5, right? Finally, the strings embedded in the JSON are spliced into the DOM using textContent, so they don't need to be escaped at that point.

As a result I think we should just give the user whatever they provided, preferably after demangling.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! One nitpick below before we merge this.

crates/wasmtime/src/runtime/module.rs Outdated Show resolved Hide resolved
@fitzgen
Copy link
Member

fitzgen commented May 16, 2024

Agreed demangling would be great -- also fine with deferring that to a follow up PR.

@lpereira lpereira requested a review from a team as a code owner May 16, 2024 23:10
@lpereira
Copy link
Contributor Author

lpereira commented May 16, 2024

Pushed the requested changes. Current output here is now this one (notice the pop-up showing the mangled name):

image

@jameysharp
Copy link
Contributor

Ooh, the pop-up is a nice touch. There's a lot more that could be done with wasmtime explore eventually.

I'd be happy to review this next week, or just as happy if you want to approve it @fitzgen.

@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label May 17, 2024
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Really excited to get this, and your other PR, in wasmtime explore.

I have a few comments and suggestions below that we should address before merging this however. I'll also hold off on reviewing the other PR until after this one merges.

Let me know if anything I said isn't clear or something like that. Thanks again!

@@ -7,27 +7,12 @@ class State {
}
}

const state = window.STATE = new State(window.WAT, window.ASM);
const state = (window.STATE = new State(window.WAT, window.ASM));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert this change; it just introduces unnecessary parens.

@@ -36,7 +21,7 @@ const nextHue = (function () {
return () => {
return hues[++i % hues.length];
};
}());
})();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. Let's not change code style when making unrelated changes.

@@ -45,7 +30,7 @@ const offsetToHue = new Map();

// Get the hue for the given offset, or assign it a new one if it doesn't have
// one already.
const hueForOffset = offset => {
const hueForOffset = (offset) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@@ -57,7 +42,7 @@ const hueForOffset = offset => {

// Get the hue for the given offset, only if the offset has already been
// assigned a hue.
const existingHueForOffset = offset => {
const existingHueForOffset = (offset) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

crates/explorer/src/index.js Outdated Show resolved Hide resolved
Comment on lines 191 to 192
funcHeader.textContent = `Disassembly of function <${func.demangled_name}>:`;
funcHeader.title = func.name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, this is the only non-stylistic change to the whole JS file? Can we remove all the other changes to this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. It was the auto-formatting tool that my editor uses. I'll have to force-push to fix this easily.

Comment on lines 187 to 197
let demangled_name = match name.splitn(2, "::").nth(1) {
Some(name) => {
let mut demangled = String::new();
demangle_function_name(&mut demangled, name)
.map_or_else(|_| format!("demangle-error::{}", name), |_| demangled)
}
None => name.to_string(),
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to let demangled_name be an Option<String> rather than making a clone of name and then have the JS choose the demangled name when available and fall back to the plain name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

crates/wasmtime/src/runtime/module.rs Outdated Show resolved Hide resolved
lpereira added 3 commits May 20, 2024 15:17
Map the iterator returned by Module::function_locations() to another
one that returns a 3-tuple containing the function name, the offset,
and the length of each function defined in this particular module.
@lpereira lpereira force-pushed the show-func-names-in-explore branch from 4304bb3 to c18e755 Compare May 20, 2024 22:21
@lpereira
Copy link
Contributor Author

Alright, force-pushed (sorry!) due to the formatting changes, but it should be a whole lot clearer now. I'll rebase the other PR once this one lands.

@lpereira lpereira force-pushed the show-func-names-in-explore branch from c18e755 to 277f57c Compare May 20, 2024 23:20
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks just about ready to merge, couple tiny things still to go first.

crates/explorer/src/index.js Outdated Show resolved Hide resolved
crates/wasmtime/src/runtime/module.rs Outdated Show resolved Hide resolved
@lpereira
Copy link
Contributor Author

Alright, @fitzgen, pushed a new revision that should address your comments.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks! And thanks for your patience with review :)

@fitzgen fitzgen added this pull request to the merge queue May 21, 2024
Merged via the queue into bytecodealliance:main with commit f994647 May 21, 2024
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants