Skip to content

terminal,service: add raw examinemem dump#3721

Merged
derekparker merged 1 commit into
go-delve:masterfrom
aarzilli:examinemem2
Oct 3, 2024
Merged

terminal,service: add raw examinemem dump#3721
derekparker merged 1 commit into
go-delve:masterfrom
aarzilli:examinemem2

Conversation

@aarzilli

@aarzilli aarzilli commented May 8, 2024

Copy link
Copy Markdown
Member

Change the examinemem command to have a new format 'raw' that just
prints the raw memory bytes.

Change the transcript command to add a new flag that disables prompt
echo to the output file.

Fixes #3706

@aarzilli

Copy link
Copy Markdown
Member Author

@stapelberg thoughts on this as an implementation for your feature request?

@stapelberg

Copy link
Copy Markdown
Contributor

@stapelberg thoughts on this as an implementation for your feature request?

Hey, thank you so much for sending this PR!

I just tried it out like so:

(dlv) x -size 8 -count 3 -x &b
0xc0045b7db0:   0x000000c005a38000   0x000000000002ca2b   0x000000000002ca2c   
(dlv) transcript -t -x -n /tmp/test.binary
(dlv) x -fmt raw -count 182827 -size 1 0x000000c005a38000
(dlv) transcript -off

This seems to work: the byte slice contents match exactly.

It would be convenient if examinemem also accepted hexadecimal -count values, so that one can just copy&paste instead of having to do the hex→dec conversion:

(dlv) x -fmt raw -count 0x000000000002ca2b -size 1 0x000000c005a38000
Command failed: count/len must be a positive integer

Or, even better, if the whole sequence of commands (examine slice header, construct transcript and examinemem commands) could somehow be encapsulated into a “dumpslice” command or similar.

Thanks again

@aarzilli

Copy link
Copy Markdown
Member Author

It would be convenient if examinemem also accepted hexadecimal -count values

Done

Or, even better, if the whole sequence of commands (examine slice header, construct transcript and examinemem commands) could somehow be encapsulated into a “dumpslice” command or similar.

That seems a bit too specific to be a command, and it could be added anyway with a user script.

@stapelberg

Copy link
Copy Markdown
Contributor

It would be convenient if examinemem also accepted hexadecimal -count values

Done

Thanks, this works now and is convenient :)

Or, even better, if the whole sequence of commands (examine slice header, construct transcript and examinemem commands) could somehow be encapsulated into a “dumpslice” command or similar.

That seems a bit too specific to be a command, and it could be added anyway with a user script.

I tried writing a starlark script for it (before filing the feature request) but didn’t get very far. I stumbled over basic hurdles like getting an expression from the command line into my starlark function. Unfortunately I wasn’t able to find examples on the web.

Would you mind sketching how such a user script would look like?

@aarzilli

Copy link
Copy Markdown
Member Author

If you are willing to do some post-processing you can just do:

def command_dumpbytearr(args):
	s = eval(None, args).Variable
	write_file("test.txt", examine_memory(s.Base, s.Len).Mem)

you don't even need this patch, otherwise dlv_command("x -fmt ...")

@stapelberg

Copy link
Copy Markdown
Contributor

Thanks for the example! I ended up with this version:

# Syntax: dumpbytearr <var> <outputfile>
def command_dumpbytearr(args):
	var_name, filename = args.split(" ")
	s = eval(None, var_name).Variable
	dlv_command("transcript -t -x -n %s" % filename)
	dlv_command("x -fmt raw -count %d -size 1 %d" % (s.Len, s.Base))
	dlv_command("transcript -off")

From my perspective, the PR is ready to be merged — it’s helpful and seems to work in all of my tests :)

@derekparker

Copy link
Copy Markdown
Member

Overall the implementation seems fine for the examinemem command changes, but I'm unsure about the transcript changes. Would that change ever be useful outside of this specific use case? It seems unlikely that you wouldn't want the command echo in any other case.

Perhaps it would be better to omit the change to transcript in favor of the examinemem starlark builtin accepting the format argument and then combine that with write_file in a starlark command?

@aarzilli

Copy link
Copy Markdown
Member Author

If we change the way we convert []byte to a string, like this 5590523, we don't have to do the change to transcript. The examinemem builtin is already returning the "raw" version always.

@stapelberg

Copy link
Copy Markdown
Contributor

Any update on this PR? I still find myself frequently reaching for this functionality, so it would be great to have it included in Delve properly. Thank you!

@derekparker

Copy link
Copy Markdown
Member

If we change the way we convert []byte to a string, like this 5590523, we don't have to do the change to transcript. The examinemem builtin is already returning the "raw" version always.

I think this is ok. @stapelberg would this work for you? You'd still have to write a custom command to dump the bytes to a file or something however.

@stapelberg

Copy link
Copy Markdown
Contributor

I think this is ok. @stapelberg would this work for you? You'd still have to write a custom command to dump the bytes to a file or something however.

Just had a chance to try this out. I used the following dumpbytearr.star:

# Syntax: dumpbytearr <var> <outputfile>
def command_dumpbytearr(args):
	var_name, filename = args.split(" ")
	s = eval(None, var_name).Variable
        mem = examine_memory(s.Base, s.Len).Mem
        write_file(filename, mem)

And it seems to work fine. So, yes, this works.

Thank you!

Change the examinemem command to have a new format 'raw' that just
prints the raw memory bytes.
Change the transcript command to add a new flag that disables prompt
echo to the output file.

Fixes go-delve#3706
@aarzilli

Copy link
Copy Markdown
Member Author

Rebased.

@derekparker derekparker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@derekparker derekparker merged commit 52405ba into go-delve:master Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: dump byte slice memory contents into file

3 participants