Skip to content

Conversation

@nikitalita
Copy link
Contributor

Adds --yaml=<filename> option to pdb2yaml. This is added primarily because we can't always count on having a utf-8 shell to redirect our output from, and if the shell is UTF-16 (like certain Windows shells), we'll output garbage.

@llvmbot
Copy link
Member

llvmbot commented Feb 20, 2024

@llvm/pr-subscribers-debuginfo

Author: None (nikitalita)

Changes

Adds --yaml=&lt;filename&gt; option to pdb2yaml. This is added primarily because we can't always count on having a utf-8 shell to redirect our output from, and if the shell is UTF-16 (like certain Windows shells), we'll output garbage.


Full diff: https://github.com/llvm/llvm-project/pull/82300.diff

3 Files Affected:

  • (modified) llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp (+5-2)
  • (modified) llvm/tools/llvm-pdbutil/YAMLOutputStyle.h (+1-1)
  • (modified) llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp (+33-2)
diff --git a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
index 80b76657facc7c..10700aadfabc56 100644
--- a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
+++ b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
@@ -37,11 +37,14 @@ static bool checkModuleSubsection(opts::ModuleSubsection MS) {
                 });
 }
 
-YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
-    : File(File), Out(outs()), Obj(File.getAllocator()) {
+YAMLOutputStyle::YAMLOutputStyle(PDBFile &File, raw_ostream &OS)
+    : File(File), Out(OS), Obj(File.getAllocator()) {
   Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
 }
 
+YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
+    : YAMLOutputStyle::YAMLOutputStyle(File, llvm::outs()) {}
+
 Error YAMLOutputStyle::dump() {
   if (opts::pdb2yaml::StreamDirectory)
     opts::pdb2yaml::StreamMetadata = true;
diff --git a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.h b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.h
index 5d53e0b65d03c9..b8badecfca405a 100644
--- a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.h
+++ b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.h
@@ -21,7 +21,7 @@ namespace pdb {
 class YAMLOutputStyle : public OutputStyle {
 public:
   YAMLOutputStyle(PDBFile &File);
-
+  YAMLOutputStyle(PDBFile &File, raw_ostream &Output);
   Error dump() override;
 
 private:
diff --git a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
index fa870666fe61a3..e1336501103dfc 100644
--- a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
+++ b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
@@ -716,6 +716,10 @@ cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
 cl::list<std::string> InputFilename(cl::Positional,
                                     cl::desc("<input PDB file>"), cl::Required,
                                     cl::sub(PdbToYamlSubcommand));
+cl::opt<std::string> PdbYamlOutputFile(
+    "yaml", cl::desc("the name of the yaml file to write (default stdout)"),
+    cl::sub(PdbToYamlSubcommand));
+
 } // namespace pdb2yaml
 
 namespace merge {
@@ -893,8 +897,35 @@ static void pdb2Yaml(StringRef Path) {
   std::unique_ptr<IPDBSession> Session;
   auto &File = loadPDB(Path, Session);
 
-  auto O = std::make_unique<YAMLOutputStyle>(File);
-
+  std::unique_ptr<YAMLOutputStyle> O;
+  std::unique_ptr<raw_fd_ostream> OutputFile;
+  if (!opts::pdb2yaml::PdbYamlOutputFile.empty()) {
+    std::error_code EC;
+    if (!sys::fs::exists(opts::pdb2yaml::PdbYamlOutputFile)) {
+      auto ParentPath =
+          sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile);
+      if (!ParentPath.empty()) {
+        EC = sys::fs::create_directories(
+            sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile));
+        if (EC) {
+          errs() << "Error creating directory: "
+                 << sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile)
+                 << "\n";
+          exit(1);
+        }
+      }
+    }
+    OutputFile = std::make_unique<raw_fd_ostream>(
+        opts::pdb2yaml::PdbYamlOutputFile, EC, sys::fs::FA_Write);
+    if (EC || !OutputFile) {
+      errs() << "Error opening file for writing: "
+             << opts::pdb2yaml::PdbYamlOutputFile << "\n";
+      exit(1);
+    }
+    O = std::make_unique<YAMLOutputStyle>(File, *OutputFile);
+  } else {
+    O = std::make_unique<YAMLOutputStyle>(File);
+  }
   ExitOnErr(O->dump());
 }
 

@github-actions
Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

@tru
Copy link
Collaborator

tru commented Apr 10, 2024

We need tests for this as well.

@nikitalita
Copy link
Contributor Author

--yaml test has been added

@nikitalita
Copy link
Contributor Author

The CI seems to be hanging on Windows "Waiting for Agent"; not sure how to fix that.

@nikitalita
Copy link
Contributor Author

It passes the tests now.

@nikitalita
Copy link
Contributor Author

@tru this one too, please?

@nikitalita nikitalita force-pushed the pdb2yaml-output-option branch from 84b3927 to f01feaa Compare May 23, 2025 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants