LLDB mainline
WindowsMiniDump.cpp
Go to the documentation of this file.
1//===-- WindowsMiniDump.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// This function is separated out from ObjectFilePECOFF.cpp to name avoid name
10// collisions with WinAPI preprocessor macros.
11
12#include "WindowsMiniDump.h"
14#include "llvm/Support/ConvertUTF.h"
15
16#ifdef _WIN32
18#include <dbghelp.h>
19#endif
20
21namespace lldb_private {
22
23bool SaveMiniDump(const lldb::ProcessSP &process_sp,
24 const SaveCoreOptions &core_options,
26 if (!process_sp)
27 return false;
28#ifdef _WIN32
29 std::optional<FileSpec> outfileSpec = core_options.GetOutputFile();
30 const auto &outfile = outfileSpec.value();
31 HANDLE process_handle = ::OpenProcess(
32 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_sp->GetID());
33 const std::string file_name = outfile.GetPath();
34 std::wstring wide_name;
35 wide_name.resize(file_name.size() + 1);
36 char *result_ptr = reinterpret_cast<char *>(&wide_name[0]);
37 const llvm::UTF8 *error_ptr = nullptr;
38 if (!llvm::ConvertUTF8toWide(sizeof(wchar_t), file_name, result_ptr,
39 error_ptr)) {
40 error.SetErrorString("cannot convert file name");
41 return false;
42 }
43 HANDLE file_handle =
44 ::CreateFileW(wide_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL,
45 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
46 const auto result =
47 ::MiniDumpWriteDump(process_handle, process_sp->GetID(), file_handle,
48 MiniDumpWithFullMemoryInfo, NULL, NULL, NULL);
49 ::CloseHandle(file_handle);
50 ::CloseHandle(process_handle);
51 if (!result) {
52 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
53 return false;
54 }
55 return true;
56#endif
57 return false;
58}
59
60} // namesapce lldb_private
static llvm::raw_ostream & error(Stream &strm)
const std::optional< lldb_private::FileSpec > GetOutputFile() const
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
bool SaveMiniDump(const lldb::ProcessSP &process_sp, const SaveCoreOptions &core_options, lldb_private::Status &error)
@ eErrorTypeWin32
Standard Win32 error codes.
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:386