[PATCH] D111658: Add JSON output skeleton to llvm-readelf

Jayson Yan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 18 14:29:52 PDT 2021


Jaysonyan added a comment.

Thanks for all the feedback! I was working on updating the patch according to your comments and I've been thinking about what you've mentioned about handling multiple object files and archives.

> Finally, you should give some thought to how archive members are printed. I think you could either have them printed as individual objects within a JSON object representing the whole archive, or just as regular files, but with the names disambiguated. I've gone with the latter approach below:

Regarding the structure of outputting archives, I am leaning towards your first suggestion which is outputting each archive member as a child object within an overall JSON object. I think this would allow the output to be consumed easier since then there's no need to parse the names of archive member to determine what archive it falls under. Given your example I would like to produce this:

  {
      "object.o" : {
          "file-header" : { <output for file header> },
          "section-headers" : { <section header table> }
      },
      "archive.a" : {
          "member1.o": {
              "file-header" : { <output for file header> },
              "section-headers" : { <section header table> }
          },
          "member2.o": {
              "file-header" : { <output for file header> },
              "section-headers" : { <section header table> }
          },
      }
  }

In terms of outputting as a single JSON object I agree that this seems like the right approach and it also pokes some holes into my current implementation. Since we would need to print things like the outer `{}` or object file names we need to output JSON at a higher level than just the `JSONELFDumper`. Since the `json::OStream` being used does validation that you are outputting valid json we would need to use the same `json::OStream` for any time we print since it will need to have the correct context (ex: ensuring you are printing attributes inside a parent object). To avoid needing to pass a `json::OStream` to every method that needs printing I'm considering creating a subclass of `ScopedPrinter` called `JSONScopedPrinter` which holds on to a `json::OStream` rather than a `raw_ostream` that way we can just create a `JSONScopedPrinter` and pass it as a `ScopedPrinter` to all necessary functions since they already accept a `ScopedPrinter`. This would allow me to still use the same `json::OStream` for all printing while also not heavily impacting the existing functions.

I'm hoping this sounds reasonable and I'm interested if you have any alternative ideas.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D111658/new/

https://reviews.llvm.org/D111658



More information about the llvm-commits mailing list