1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "common.h"
27 #include "iinterfacepointer.h"
28 #include <sstream>
29 
winErrorMessage(unsigned long error)30 std::string winErrorMessage(unsigned long error)
31 {
32     char *lpMsgBuf;
33     const int len = FormatMessageA(
34             FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
35             NULL, error, 0, (LPSTR)&lpMsgBuf, 0, NULL);
36     if (len) {
37         const std::string rc(lpMsgBuf, len);
38         LocalFree(lpMsgBuf);
39         return rc;
40     }
41     std::ostringstream str;
42     str << "Unknown error " << error;
43     return str.str();
44 }
45 
winErrorMessage()46 std::string winErrorMessage()
47 {
48     return winErrorMessage(GetLastError());
49 }
50 
msgDebugEngineComResult(HRESULT hr)51 std::string msgDebugEngineComResult(HRESULT hr)
52 {
53     switch (hr) {
54     case S_OK:
55         return std::string("S_OK");
56     case S_FALSE:
57         return std::string("S_FALSE");
58     case E_FAIL:
59         break;
60     case E_INVALIDARG:
61         return std::string("E_INVALIDARG");
62     case E_NOINTERFACE:
63         return std::string("E_NOINTERFACE");
64     case E_OUTOFMEMORY:
65         return std::string("E_OUTOFMEMORY");
66     case E_UNEXPECTED:
67         return std::string("E_UNEXPECTED");
68     case E_NOTIMPL:
69         return std::string("E_NOTIMPL");
70     }
71     if (hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED))
72         return std::string("ERROR_ACCESS_DENIED");
73     if (hr == HRESULT_FROM_NT(STATUS_CONTROL_C_EXIT))
74         return std::string("STATUS_CONTROL_C_EXIT");
75     return std::string("E_FAIL ") + winErrorMessage(HRESULT_CODE(hr));
76 }
77 
msgDebugEngineComFailed(const char * func,HRESULT hr)78 std::string msgDebugEngineComFailed(const char *func, HRESULT hr)
79 {
80     std::string rc = func;
81     rc += " failed: ";
82     rc += msgDebugEngineComResult(hr);
83     return rc;
84 }
85 
currentThreadId(IDebugSystemObjects * sysObjects)86 ULONG currentThreadId(IDebugSystemObjects *sysObjects)
87 {
88     ULONG id = 0;
89     if (sysObjects->GetCurrentThreadId(&id) == S_OK)
90         return id;
91     return 0;
92 }
93 
currentThreadId(CIDebugClient * client)94 ULONG currentThreadId(CIDebugClient *client)
95 {
96     IInterfacePointer<IDebugSystemObjects> sysObjects(client);
97     if (sysObjects)
98         return currentThreadId(sysObjects.data());
99     return 0;
100 }
101 
currentProcessId(IDebugSystemObjects * sysObjects)102 ULONG currentProcessId(IDebugSystemObjects *sysObjects)
103 {
104     ULONG64 handle = 0;
105     if (sysObjects->GetCurrentProcessHandle(&handle) == S_OK)
106         return GetProcessId((HANDLE)handle);
107     return 0;
108 }
109 
currentProcessId(CIDebugClient * client)110 ULONG currentProcessId(CIDebugClient *client)
111 {
112     IInterfacePointer<IDebugSystemObjects> sysObjects(client);
113     if (sysObjects)
114         return currentProcessId(sysObjects.data());
115     return 0;
116 }
117 
moduleNameByOffset(CIDebugSymbols * symbols,ULONG64 offset)118 std::string moduleNameByOffset(CIDebugSymbols *symbols, ULONG64 offset)
119 {
120     enum { BufSize = 512 };
121     ULONG index = 0;
122     ULONG64 base = 0;
123     // Convert module base address to module index
124     HRESULT hr = symbols->GetModuleByOffset(offset, 0, &index, &base);
125     if (FAILED(hr))
126         return std::string();
127     // Obtain module name
128     char buf[BufSize];
129     buf[0] = '\0';
130     hr = symbols->GetModuleNameString(DEBUG_MODNAME_MODULE, index, base, buf, BufSize, 0);
131     if (FAILED(hr))
132         return std::string();
133     return std::string(buf);
134 }
135 
sourceFileNameByOffset(CIDebugSymbols * symbols,ULONG64 offset,PULONG lineNumber)136 std::string sourceFileNameByOffset(CIDebugSymbols *symbols, ULONG64 offset, PULONG lineNumber)
137 {
138     enum { BufSize = 512 };
139     char buf[BufSize];
140     buf[0] = '\0';
141     HRESULT hr = symbols->GetLineByOffset(offset, lineNumber, buf, BufSize, NULL, NULL);
142     if (FAILED(hr))
143         return std::string();
144     return std::string(buf);
145 }
146