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