]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MIUtilSystemWindows.cpp
Merge OpenSSL 1.0.2e.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MIUtilSystemWindows.cpp
1 //===-- MIUtilSystemWindows.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 #if defined(_MSC_VER)
11
12 // Third party headers
13 #include <memory> // std::unique_ptr
14 #include <Windows.h>
15 #include <WinBase.h> // ::FormatMessage()
16
17 // In-house headers:
18 #include "MIUtilSystemWindows.h"
19 #include "MICmnResources.h"
20 #include "MIUtilFileStd.h"
21
22 //++ ------------------------------------------------------------------------------------
23 // Details: CMIUtilSystemWindows constructor.
24 // Type:    Method.
25 // Args:    None.
26 // Return:  None.
27 // Throws:  None.
28 //--
29 CMIUtilSystemWindows::CMIUtilSystemWindows(void)
30 {
31 }
32
33 //++ ------------------------------------------------------------------------------------
34 // Details: CMIUtilSystemWindows destructor.
35 // Type:    Method.
36 // Args:    None.
37 // Return:  None.
38 // Throws:  None.
39 //--
40 CMIUtilSystemWindows::~CMIUtilSystemWindows(void)
41 {
42 }
43
44 //++ ------------------------------------------------------------------------------------
45 // Details: Retrieve the OS system error message for the given system error code.
46 // Type:    Method.
47 // Args:    vError      - (R) OS error code value.
48 //          vrwErrorMsg - (W) The error message.
49 // Return:  MIstatus::success - Functional succeeded.
50 //          MIstatus::failure - Functional failed.
51 // Throws:  None.
52 //--
53 bool
54 CMIUtilSystemWindows::GetOSErrorMsg(const MIint vError, CMIUtilString &vrwErrorMsg) const
55 {
56     // Reset
57     vrwErrorMsg.clear();
58
59     const MIuint nBufLen = 1024;
60     std::unique_ptr<char[]> pBuffer;
61     pBuffer.reset(new char[nBufLen]);
62
63     // CMIUtilString Format is not used as cannot replicate the behavior of ::FormatMessage which
64     // can take into account locality while retrieving the error message from the system.
65     const int nLength = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, (DWORD)vError,
66                                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&pBuffer[0]), nBufLen, nullptr);
67     bool bOk = MIstatus::success;
68     if (nLength != 0)
69         vrwErrorMsg = &pBuffer[0];
70     else
71         bOk = MIstatus::failure;
72
73     return bOk;
74 }
75
76 //++ ------------------------------------------------------------------------------------
77 // Details: Retrieve if possible the OS last error description.
78 // Type:    Method.
79 // Args:    None.
80 // Return:  CMIUtilString - Error description.
81 // Throws:  None.
82 //--
83 CMIUtilString
84 CMIUtilSystemWindows::GetOSLastError(void) const
85 {
86     CMIUtilString errorMsg;
87     const DWORD dwLastError = ::GetLastError();
88     if (dwLastError != 0)
89     {
90         if (!GetOSErrorMsg(dwLastError, errorMsg))
91             errorMsg = MIRSRC(IDE_OS_ERR_RETRIEVING);
92     }
93     else
94         errorMsg = MIRSRC(IDE_OS_ERR_UNKNOWN);
95
96     return errorMsg;
97 }
98
99 //++ ------------------------------------------------------------------------------------
100 // Details: Retrieves the fully qualified path for the this application. If the function
101 //          fails the string is filled with the error message.
102 // Type:    Method.
103 // Args:    vrwFileNamePath   - (W) The executable's name and path or last error description.
104 // Return:  MIstatus::success - Functional succeeded.
105 //          MIstatus::failure - Functional failed.
106 // Throws:  None.
107 //--
108 bool
109 CMIUtilSystemWindows::GetExecutablesPath(CMIUtilString &vrwFileNamePath) const
110 {
111     bool bOk = MIstatus::success;
112     HMODULE hModule = ::GetModuleHandle(nullptr);
113     char pPath[MAX_PATH];
114     if (!::GetModuleFileName(hModule, &pPath[0], MAX_PATH))
115     {
116         bOk = MIstatus::failure;
117         vrwFileNamePath = GetOSLastError();
118     }
119     else
120         vrwFileNamePath = &pPath[0];
121
122     return bOk;
123 }
124
125 //++ ------------------------------------------------------------------------------------
126 // Details: Retrieves the fully qualified path for the Log file for this application.
127 //          If the function fails the string is filled with the error message.
128 // Type:    Method.
129 // Args:    vrwFileNamePath   - (W) The Log file's name and path or last error description.
130 // Return:  MIstatus::success - Functional succeeded.
131 //          MIstatus::failure - Functional failed.
132 // Throws:  None.
133 //--
134 bool
135 CMIUtilSystemWindows::GetLogFilesPath(CMIUtilString &vrwFileNamePath) const
136 {
137     vrwFileNamePath = CMIUtilString(".");
138     return MIstatus::success;
139 }
140
141 #endif // #if defined( _MSC_VER )