[Lldb-commits] [PATCH] D109326: [lldb] [Process/FreeBSD] Support SaveCore() using PT_COREDUMP

Michał Górny via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 6 07:33:45 PDT 2021


mgorny created this revision.
mgorny added reviewers: emaste, krytarowski, labath, JDevlieghere.
mgorny requested review of this revision.

https://reviews.llvm.org/D109326

Files:
  lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
  lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h
  lldb/test/API/functionalities/process_save_core/TestProcessSaveCore.py
  lldb/test/API/tools/lldb-server/TestGdbRemoteSaveCore.py


Index: lldb/test/API/tools/lldb-server/TestGdbRemoteSaveCore.py
===================================================================
--- lldb/test/API/tools/lldb-server/TestGdbRemoteSaveCore.py
+++ lldb/test/API/tools/lldb-server/TestGdbRemoteSaveCore.py
@@ -38,15 +38,15 @@
         self.assertTrue(process, PROCESS_IS_VALID)
         self.assertEqual(process.GetProcessID(), procs["inferior"].pid)
 
-    @skipUnlessPlatform(oslist=["netbsd"])
+    @skipUnlessPlatform(oslist=["freebsd", "netbsd"])
     def test_netbsd_path(self):
         core = lldbutil.append_to_process_working_directory(self, "core")
         self.coredump_test(core, core)
 
-    @skipUnlessPlatform(oslist=["netbsd"])
+    @skipUnlessPlatform(oslist=["freebsd", "netbsd"])
     def test_netbsd_no_path(self):
         self.coredump_test()
 
-    @skipUnlessPlatform(oslist=["netbsd"])
+    @skipUnlessPlatform(oslist=["freebsd", "netbsd"])
     def test_netbsd_bad_path(self):
         self.coredump_test("/dev/null/cantwritehere")
Index: lldb/test/API/functionalities/process_save_core/TestProcessSaveCore.py
===================================================================
--- lldb/test/API/functionalities/process_save_core/TestProcessSaveCore.py
+++ lldb/test/API/functionalities/process_save_core/TestProcessSaveCore.py
@@ -64,7 +64,7 @@
             if (os.path.isfile(core)):
                 os.unlink(core)
 
-    @skipUnlessPlatform(["netbsd"])
+    @skipUnlessPlatform(["freebsd", "netbsd"])
     def test_save_core_via_process_plugin(self):
         self.build()
         exe = self.getBuildArtifact("a.out")
Index: lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h
===================================================================
--- lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h
+++ lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h
@@ -91,6 +91,8 @@
 
   bool SupportHardwareSingleStepping() const;
 
+  llvm::Expected<std::string> SaveCore(llvm::StringRef path_hint) override;
+
 protected:
   llvm::Expected<llvm::ArrayRef<uint8_t>>
   GetSoftwareBreakpointTrapOpcode(size_t size_hint) override;
Index: lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
===================================================================
--- lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
+++ lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
@@ -131,7 +131,8 @@
 NativeProcessFreeBSD::Extension
 NativeProcessFreeBSD::Factory::GetSupportedExtensions() const {
   return Extension::multiprocess | Extension::fork | Extension::vfork |
-         Extension::pass_signals | Extension::auxv | Extension::libraries_svr4;
+         Extension::pass_signals | Extension::auxv | Extension::libraries_svr4 |
+         Extension::savecore;
 }
 
 // Public Instance Methods
@@ -1009,3 +1010,30 @@
     }
   }
 }
+
+llvm::Expected<std::string>
+NativeProcessFreeBSD::SaveCore(llvm::StringRef path_hint) {
+  using namespace llvm::sys::fs;
+
+  llvm::SmallString<128> path{path_hint};
+  Status error;
+  struct ptrace_coredump pc = {};
+
+  // Try with the suggested path first.  If there is no suggested path or it
+  // failed to open, use a temporary file.
+  if (path.empty() ||
+      openFile(path, pc.pc_fd, CD_CreateNew, FA_Write, OF_None)) {
+    if (std::error_code errc =
+            createTemporaryFile("lldb", "core", pc.pc_fd, path))
+      return llvm::createStringError(errc, "Unable to create a temporary file");
+  }
+  error = PtraceWrapper(PT_COREDUMP, GetID(), &pc, sizeof(pc));
+
+  std::error_code close_err = closeFile(pc.pc_fd);
+  if (error.Fail())
+    return error.ToError();
+  if (close_err)
+    return llvm::createStringError(
+        close_err, "Unable to close the core dump after writing");
+  return path.str().str();
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109326.370922.patch
Type: text/x-patch
Size: 3794 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210906/f99c5e17/attachment.bin>


More information about the lldb-commits mailing list