Enable saving of mini dumps with lldb process save-core.

Also adds SB API to save a core and tests that use it.

Differential Revision: http://reviews.llvm.org/D14793

llvm-svn: 253734
diff --git a/lldb/source/Plugins/ObjectFile/PECOFF/CMakeLists.txt b/lldb/source/Plugins/ObjectFile/PECOFF/CMakeLists.txt
index 89c2bb2..5c7c488 100644
--- a/lldb/source/Plugins/ObjectFile/PECOFF/CMakeLists.txt
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/CMakeLists.txt
@@ -1,3 +1,4 @@
 add_lldb_library(lldbPluginObjectFilePECOFF
   ObjectFilePECOFF.cpp
+  WindowsMiniDump.cpp
   )
diff --git a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
index 8b99459..b662448 100644
--- a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "ObjectFilePECOFF.h"
+#include "WindowsMiniDump.h"
 
 #include "llvm/Support/COFF.h"
 
@@ -24,6 +25,7 @@
 #include "lldb/Core/Timer.h"
 #include "lldb/Core/UUID.h"
 #include "lldb/Symbol/ObjectFile.h"
+#include "lldb/Target/Process.h"
 #include "lldb/Target/SectionLoadList.h"
 #include "lldb/Target/Target.h"
 
@@ -42,7 +44,8 @@
                                    GetPluginDescriptionStatic(),
                                    CreateInstance,
                                    CreateMemoryInstance,
-                                   GetModuleSpecifications);
+                                   GetModuleSpecifications,
+                                   SaveCore);
 }
 
 void
@@ -148,6 +151,14 @@
     return specs.GetSize() - initial_count;
 }
 
+bool
+ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
+                           const lldb_private::FileSpec &outfile,
+                           lldb_private::Error &error)
+{
+    return SaveMiniDump(process_sp, outfile, error);
+}
+
 
 bool
 ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& data_sp)
diff --git a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h
index 1c7fefc..fd33cd3 100644
--- a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h
@@ -95,6 +95,11 @@
                              lldb_private::ModuleSpecList &specs);
 
     static bool
+    SaveCore (const lldb::ProcessSP &process_sp,
+              const lldb_private::FileSpec &outfile,
+              lldb_private::Error &error);
+
+    static bool
     MagicBytesMatch (lldb::DataBufferSP& data_sp);
     
     bool
diff --git a/lldb/source/Plugins/ObjectFile/PECOFF/WindowsMiniDump.cpp b/lldb/source/Plugins/ObjectFile/PECOFF/WindowsMiniDump.cpp
new file mode 100644
index 0000000..85b1c71
--- /dev/null
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/WindowsMiniDump.cpp
@@ -0,0 +1,55 @@
+//===-- WindowsMiniDump.cpp -------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// This function is separated out from ObjectFilePECOFF.cpp to name avoid name
+// collisions with WinAPI preprocessor macros.
+
+#include "WindowsMiniDump.h"
+#include "lldb/Host/FileSpec.h"
+#include "llvm/Support/ConvertUTF.h"
+
+#ifdef _WIN32
+#include "lldb/Host/windows/windows.h"
+#include <DbgHelp.h>  // for MiniDumpWriteDump
+#endif
+
+namespace lldb_private {
+
+bool
+SaveMiniDump(const lldb::ProcessSP &process_sp,
+             const lldb_private::FileSpec &outfile,
+             lldb_private::Error &error)
+{
+    if (!process_sp) return false;
+#ifdef _WIN32
+    HANDLE process_handle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_sp->GetID());
+    const std::string file_name = outfile.GetCString();
+    std::wstring wide_name;
+    wide_name.resize(file_name.size() + 1);
+    char * result_ptr = reinterpret_cast<char *>(&wide_name[0]);
+    const UTF8 *error_ptr = nullptr;
+    if (!llvm::ConvertUTF8toWide(sizeof(wchar_t), file_name, result_ptr, error_ptr)) {
+        error.SetErrorString("cannot convert file name");
+        return false;
+    }
+    HANDLE file_handle = ::CreateFileW(wide_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+    const auto result = ::MiniDumpWriteDump(process_handle, process_sp->GetID(), file_handle, MiniDumpNormal, NULL, NULL, NULL);
+    ::CloseHandle(file_handle);
+    ::CloseHandle(process_handle);
+    if (!result)
+    {
+        error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
+        return false;
+    }
+    return true;
+#endif
+    return false;
+}
+
+}  // namesapce lldb_private
diff --git a/lldb/source/Plugins/ObjectFile/PECOFF/WindowsMiniDump.h b/lldb/source/Plugins/ObjectFile/PECOFF/WindowsMiniDump.h
new file mode 100644
index 0000000..cbea88a
--- /dev/null
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/WindowsMiniDump.h
@@ -0,0 +1,24 @@
+//===-- WindowsMiniDump.h ---------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_WindowsMiniDump_h_
+#define liblldb_WindowsMiniDump_h_
+
+#include "lldb/Target/Process.h"
+
+namespace lldb_private {
+
+bool
+SaveMiniDump(const lldb::ProcessSP &process_sp,
+             const lldb_private::FileSpec &outfile,
+             lldb_private::Error &error);
+
+}  // namespace lldb_private
+
+#endif