go-delve / delve

Delve is a debugger for the Go programming language.
MIT License
22.37k stars 2.13k forks source link

terminal,service: add raw examinemem dump #3721

Open aarzilli opened 1 month ago

aarzilli commented 1 month ago

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 commented 1 month ago

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

stapelberg commented 1 month ago

@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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 2 weeks ago

If we change the way we convert []byte to a string, like this https://github.com/go-delve/delve/pull/3721/commits/55905239afb70a9b906ed4fda18818bbf621ea83, we don't have to do the change to transcript. The examinemem builtin is already returning the "raw" version always.