From b07803ee2a97fdcf4ed6494d8d6593bf985a5150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Tue, 4 May 2021 21:11:20 +0200 Subject: [PATCH] [lldb] [Process/FreeBSD] Support SaveCore() using PT_COREDUMP Differential Revision: https://reviews.llvm.org/D109326 --- .../Process/FreeBSD/NativeProcessFreeBSD.cpp | 30 ++++++++++++++++++- .../Process/FreeBSD/NativeProcessFreeBSD.h | 2 ++ .../process_save_core/TestProcessSaveCore.py | 2 +- .../lldb-server/TestGdbRemoteSaveCore.py | 6 ++-- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp index d6426b3d2367..d795a43375ca 100644 --- a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp +++ b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp @@ -131,7 +131,8 @@ NativeProcessFreeBSD::Factory::Attach( 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 @@ void NativeProcessFreeBSD::MonitorClone(::pid_t child_pid, bool is_vfork, } } } + +llvm::Expected +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(); +} diff --git a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h index 7ec9d17d4cf4..44b8a53699bb 100644 --- a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h +++ b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.h @@ -91,6 +91,8 @@ public: bool SupportHardwareSingleStepping() const; + llvm::Expected SaveCore(llvm::StringRef path_hint) override; + protected: llvm::Expected> GetSoftwareBreakpointTrapOpcode(size_t size_hint) override; diff --git a/lldb/test/API/functionalities/process_save_core/TestProcessSaveCore.py b/lldb/test/API/functionalities/process_save_core/TestProcessSaveCore.py index 9897fb6b4910..42955975a71b 100644 --- a/lldb/test/API/functionalities/process_save_core/TestProcessSaveCore.py +++ b/lldb/test/API/functionalities/process_save_core/TestProcessSaveCore.py @@ -64,7 +64,7 @@ class ProcessSaveCoreTestCase(TestBase): 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") diff --git a/lldb/test/API/tools/lldb-server/TestGdbRemoteSaveCore.py b/lldb/test/API/tools/lldb-server/TestGdbRemoteSaveCore.py index 405a73bcdba8..10ba1e91134f 100644 --- a/lldb/test/API/tools/lldb-server/TestGdbRemoteSaveCore.py +++ b/lldb/test/API/tools/lldb-server/TestGdbRemoteSaveCore.py @@ -38,15 +38,15 @@ class TestGdbSaveCore(gdbremote_testcase.GdbRemoteTestCaseBase): 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")