This is an archive of the discontinued LLVM Phabricator instance.

[trace] Add an option to dump instructions in json and to a file
ClosedPublic

Authored by wallace on Jun 21 2022, 5:49 PM.

Details

Summary

In order to provide simple scripting support on top of instruction traces, a simple solution is to enhance the dump instructions command and allow printing in json and directly to a file. The format is verbose and not space efficient, but it's not supposed to be used for really large traces, in which case the TraceCursor API is the way to go.

  • add a -j option for printing the dump in json
  • add a -J option for pretty printing the json output
  • add a -F option for specifying an output file
  • add a -a option for dumping all the instructions available starting at the initial point configured with the other flags. This is useful for dumping all the instructions to a file in one go
  • add tests for all cases
  • refactored the instruction dumper and abstracted the actual "printing" logic. There are two writer implementations: CLI and JSON. This made the dumper itself much more readable and maintanable

sample output:

(lldb) thread trace dump instructions  -t -a --id 100 -J
[
  {
    "id": 100,
    "tsc": "43591204528448966"
    "loadAddress": "0x407a91",
    "module": "a.out",
    "symbol": "void std::deque<Foo, std::allocator<Foo>>::_M_push_back_aux<Foo>(Foo&&)",
    "mnemonic": "movq",
    "source": "/usr/include/c++/8/bits/deque.tcc",
    "line": 492,
    "column": 30
  },
  ...

Diff Detail

Event Timeline

wallace created this revision.Jun 21 2022, 5:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2022, 5:49 PM
wallace requested review of this revision.Jun 21 2022, 5:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2022, 5:49 PM
wallace edited the summary of this revision. (Show Details)Jun 21 2022, 5:50 PM
wallace updated this revision to Diff 438884.Jun 21 2022, 5:54 PM

add a test for -a

jj10306 requested changes to this revision.Jun 22 2022, 6:08 AM

looks great overall, just a couple minor comments!

lldb/include/lldb/Target/TraceInstructionDumper.h
46

nit: wdyt ab renaming this to TraceInstructionsDumper since now we have the TraceInstructionWriter which is responsible for displaying a single instruction? With how things are currently named it's slightly confusing at first glance because there names sound like they are both doing the same thing (something related to a single instruction).

75

since the JSON subclass doesn't need to implement this, consider providing a default stubbed out implementation at the super class level so that subclasses aren't necessarily required to implement it?

120–122

nit: to be consistent with the --raw flag of the thread trace dump instructions command which says:

-r ( --raw )
     Dump only instruction address without disassembly nor symbol information.
lldb/source/Commands/CommandObjectThread.cpp
2210

Why is this stored on the command object and not the TraceinstructionDumperOptions?
Can you explain the separation of responsibility of the dumper options versus the options here?

lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
121–122

nit: maybe use the new HasId method here?

lldb/source/Target/TraceInstructionDumper.cpp
90–124

Move declarations to header?

193
219–224

the values being passed to the attribute method are c strings. Unsure how this method handles a nullptr, but consider checking if these are nullptr before calling attribute if necessary?

This revision now requires changes to proceed.Jun 22 2022, 6:08 AM
wallace marked 4 inline comments as done.Jun 22 2022, 9:59 AM
wallace added inline comments.
lldb/include/lldb/Target/TraceInstructionDumper.h
46

I'm renaming TraceInstructionWriter to OutputWriter, just because that one is an internal class

lldb/source/Commands/CommandObjectThread.cpp
2210

basically just because this field is not used in the dumper at all. The dumper requires a Stream object, which is configured by the caller, either a file stream or a standard output stream (or maybe a python string stream). So in this case, because this field is only used by this command handler to create the actual Stream to pass to the dumper, I'm restricting this variable to here.

lldb/source/Target/TraceInstructionDumper.cpp
90–124

in this case I'd rather not do that because I don't want anyone to see these classes. They are very restricted to the use case of the dumper and no one should try to use them for any reason. So I'm making them very private intentionally

219–224

good catch!

wallace updated this revision to Diff 439072.Jun 22 2022, 9:59 AM

address comments

jj10306 accepted this revision.Jun 22 2022, 10:17 AM
jj10306 added inline comments.
lldb/source/Target/TraceInstructionDumper.cpp
190–191

the inline comment should be next to the second parameter, right?

This revision is now accepted and ready to land.Jun 22 2022, 10:17 AM
wallace added inline comments.Jun 22 2022, 10:18 AM
lldb/source/Target/TraceInstructionDumper.cpp
190–191

silly me