]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MIUtilSystemWindows.cpp
MFV: r277654
[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 CMIUtilSystemWindows::GetOSErrorMsg( const MIint vError, CMIUtilString & vrwErrorMsg  ) const
65 {
66         // Reset
67         vrwErrorMsg.clear();
68
69         const MIuint nBufLen = 1024;
70         std::unique_ptr< char[] > pBuffer;
71         pBuffer.reset( new char[ nBufLen ] ); 
72                 
73         // CMIUtilString Format is not used as cannot replicate the behavior of ::FormatMessage which
74         // can take into account locality while retrieving the error message from the system.
75         const int nLength = ::FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
76                                                                                 nullptr, (DWORD) vError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), 
77                                                                                 reinterpret_cast< LPTSTR >( &pBuffer[ 0 ] ),
78                                                                                 nBufLen, nullptr );
79         bool bOk = MIstatus::success;
80         if( nLength != 0 )
81                 vrwErrorMsg = &pBuffer[ 0 ];
82         else
83                 bOk = MIstatus::failure;
84
85         return bOk;
86 }
87
88 //++ ------------------------------------------------------------------------------------
89 // Details:     Retrieve if possible the OS last error description.
90 // Type:        Method.
91 // Args:        None.
92 // Return:      CMIUtilString - Error description.
93 // Throws:      None.
94 //--
95 CMIUtilString 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 CMIUtilSystemWindows::GetExecutablesPath( CMIUtilString & vrwFileNamePath ) const
120 {
121         bool bOk = MIstatus::success;
122         HMODULE hModule = ::GetModuleHandle( nullptr );
123         char pPath[ MAX_PATH ];
124         const DWORD nLen = ::GetModuleFileName( hModule, &pPath[ 0 ], MAX_PATH );
125         const CMIUtilString strLastErr( GetOSLastError() );
126         if( (nLen != 0) && (strLastErr == "Unknown OS error") )
127                 vrwFileNamePath = &pPath[ 0 ];
128         else
129         {
130                 bOk = MIstatus::failure;
131                 vrwFileNamePath = strLastErr;
132         }
133
134         return bOk;
135 }
136
137 //++ ------------------------------------------------------------------------------------
138 // Details:     Retrieves the fully qualified path for the Log file for this application. 
139 //                      If the function fails the string is filled with the error message.
140 // Type:        Method.
141 // Args:        vrwFileNamePath - (W) The Log file's name and path or last error description.
142 // Return:      MIstatus::success - Functional succeeded.
143 //                      MIstatus::failure - Functional failed.
144 // Throws:      None.
145 //--
146 bool CMIUtilSystemWindows::GetLogFilesPath( CMIUtilString & vrwFileNamePath ) const
147 {
148         return GetExecutablesPath( vrwFileNamePath );
149 }
150
151 #endif // #if defined( _MSC_VER )