Adrian McCarthy | f7d1893 | 2015-11-20 23:09:11 +0000 | [diff] [blame] | 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" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 14 | #include "lldb/Utility/FileSpec.h" |
Adrian McCarthy | f7d1893 | 2015-11-20 23:09:11 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ConvertUTF.h" |
| 16 | |
| 17 | #ifdef _WIN32 |
| 18 | #include "lldb/Host/windows/windows.h" |
Hafiz Abid Qadeer | f6ee79c | 2016-12-15 15:00:41 +0000 | [diff] [blame] | 19 | #include <dbghelp.h> // for MiniDumpWriteDump |
Adrian McCarthy | f7d1893 | 2015-11-20 23:09:11 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
| 22 | namespace lldb_private { |
| 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | bool SaveMiniDump(const lldb::ProcessSP &process_sp, |
| 25 | const lldb_private::FileSpec &outfile, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 26 | lldb_private::Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | if (!process_sp) |
Adrian McCarthy | f7d1893 | 2015-11-20 23:09:11 +0000 | [diff] [blame] | 28 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | #ifdef _WIN32 |
| 30 | HANDLE process_handle = ::OpenProcess( |
| 31 | 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]); |
Adrian McCarthy | 5908249 | 2016-09-30 16:11:42 +0000 | [diff] [blame] | 36 | const llvm::UTF8 *error_ptr = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | if (!llvm::ConvertUTF8toWide(sizeof(wchar_t), file_name, result_ptr, |
| 38 | error_ptr)) { |
| 39 | error.SetErrorString("cannot convert file name"); |
| 40 | return false; |
| 41 | } |
| 42 | HANDLE file_handle = |
| 43 | ::CreateFileW(wide_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 44 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 45 | const auto result = |
| 46 | ::MiniDumpWriteDump(process_handle, process_sp->GetID(), file_handle, |
| 47 | MiniDumpWithFullMemoryInfo, NULL, NULL, NULL); |
| 48 | ::CloseHandle(file_handle); |
| 49 | ::CloseHandle(process_handle); |
| 50 | if (!result) { |
| 51 | error.SetError(::GetLastError(), lldb::eErrorTypeWin32); |
| 52 | return false; |
| 53 | } |
| 54 | return true; |
| 55 | #endif |
| 56 | return false; |
Adrian McCarthy | f7d1893 | 2015-11-20 23:09:11 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | } // namesapce lldb_private |