1 //===-- WindowsMiniDump.cpp -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // This function is separated out from ObjectFilePECOFF.cpp to name avoid name 11 // collisions with WinAPI preprocessor macros. 12 13 #include "WindowsMiniDump.h" 14 #include "lldb/Host/FileSpec.h" 15 #include "llvm/Support/ConvertUTF.h" 16 17 #ifdef _WIN32 18 #include "lldb/Host/windows/windows.h" 19 #include <DbgHelp.h> // for MiniDumpWriteDump 20 #endif 21 22 namespace lldb_private { 23 24 bool 25 SaveMiniDump(const lldb::ProcessSP &process_sp, 26 const lldb_private::FileSpec &outfile, 27 lldb_private::Error &error) 28 { 29 if (!process_sp) return false; 30 #ifdef _WIN32 31 HANDLE process_handle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_sp->GetID()); 32 const std::string file_name = outfile.GetCString(); 33 std::wstring wide_name; 34 wide_name.resize(file_name.size() + 1); 35 char * result_ptr = reinterpret_cast<char *>(&wide_name[0]); 36 const UTF8 *error_ptr = nullptr; 37 if (!llvm::ConvertUTF8toWide(sizeof(wchar_t), file_name, result_ptr, error_ptr)) { 38 error.SetErrorString("cannot convert file name"); 39 return false; 40 } 41 HANDLE file_handle = ::CreateFileW(wide_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 42 const auto result = ::MiniDumpWriteDump(process_handle, process_sp->GetID(), file_handle, 43 MiniDumpWithFullMemoryInfo, NULL, NULL, NULL); 44 ::CloseHandle(file_handle); 45 ::CloseHandle(process_handle); 46 if (!result) 47 { 48 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 49 return false; 50 } 51 return true; 52 #endif 53 return false; 54 } 55 56 } // namesapce lldb_private 57