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"
13 #include "lldb/Utility/FileSpec.h"
14 #include "llvm/Support/ConvertUTF.h"
15 
16 #ifdef _WIN32
18 #include <dbghelp.h>
19 #endif
20 
21 namespace lldb_private {
22 
23 bool SaveMiniDump(const lldb::ProcessSP &process_sp,
24  const lldb_private::FileSpec &outfile,
26  if (!process_sp)
27  return false;
28 #ifdef _WIN32
29  HANDLE process_handle = ::OpenProcess(
30  PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_sp->GetID());
31  const std::string file_name = outfile.GetCString();
32  std::wstring wide_name;
33  wide_name.resize(file_name.size() + 1);
34  char *result_ptr = reinterpret_cast<char *>(&wide_name[0]);
35  const llvm::UTF8 *error_ptr = nullptr;
36  if (!llvm::ConvertUTF8toWide(sizeof(wchar_t), file_name, result_ptr,
37  error_ptr)) {
38  error.SetErrorString("cannot convert file name");
39  return false;
40  }
41  HANDLE file_handle =
42  ::CreateFileW(wide_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL,
43  CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
44  const auto result =
45  ::MiniDumpWriteDump(process_handle, process_sp->GetID(), file_handle,
46  MiniDumpWithFullMemoryInfo, NULL, NULL, NULL);
47  ::CloseHandle(file_handle);
48  ::CloseHandle(process_handle);
49  if (!result) {
50  error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
51  return false;
52  }
53  return true;
54 #endif
55  return false;
56 }
57 
58 } // namesapce lldb_private
lldb::eErrorTypeWin32
@ eErrorTypeWin32
Standard Win32 error codes.
Definition: lldb-enumerations.h:312
lldb_private::FileSpec::GetCString
const char * GetCString(bool denormalize=true) const
Definition: FileSpec.cpp:364
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
WindowsMiniDump.h
lldb_private::Status
Definition: Status.h:44
windows.h
lldb_private::SaveMiniDump
bool SaveMiniDump(const lldb::ProcessSP &process_sp, const lldb_private::FileSpec &outfile, lldb_private::Status &error)
Definition: WindowsMiniDump.cpp:23
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
FileSpec.h