Debian Codegraph @

crt/aws-c-common/source/windows/system_info.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/system_info.h>
7
8 #include <aws/common/byte_buf.h>
9 #include <aws/common/logging.h>
10 #include <aws/common/thread.h>
11
12 #include <windows.h>
13
14 enum aws_platform_os aws_get_platform_build_os(void) {
15 return AWS_PLATFORM_OS_WINDOWS;
16 }
17
18 size_t aws_system_info_processor_count(void) {
19 SYSTEM_INFO info;
20 GetSystemInfo(&info);
21 return info.dwNumberOfProcessors;
22 }
23
24 size_t aws_system_info_page_size(void) {
25 SYSTEM_INFO info;
26 GetSystemInfo(&info);
27 return info.dwPageSize;
28 }
29
30 /* the next three functions need actual implementations before we can have proper numa alignment on windows.
31 * For now leave them stubbed out. */
32 uint16_t aws_get_cpu_group_count(void) {
33 return 1U;
34 }
35
36 size_t aws_get_cpu_count_for_group(uint16_t group_idx) {
37 (void)group_idx;
38 return aws_system_info_processor_count();
39 }
40
41 void aws_get_cpu_ids_for_group(uint16_t group_idx, struct aws_cpu_info *cpu_ids_array, size_t cpu_ids_array_length) {
42 (void)group_idx;
43
44 if (!cpu_ids_array_length) {
45 return;
46 }
47
48 /* a crude hint, but hyper-threads are numbered as the second half of the cpu id listing. */
49 size_t hyper_threads_hint = cpu_ids_array_length / 2 - 1;
50
51 for (size_t i = 0; i < cpu_ids_array_length; ++i) {
52 cpu_ids_array[i].cpu_id = (int32_t)i;
53 cpu_ids_array[i].suspected_hyper_thread = i > hyper_threads_hint;
54 }
55 }
56
57 bool aws_is_debugger_present(void) {
58 return IsDebuggerPresent();
59 }
60
61 void aws_debug_break(void) {
62 #ifdef DEBUG_BUILD
63 if (aws_is_debugger_present()) {
64 DebugBreak();
65 }
66 #endif
67 }
68
69 #if defined(AWS_OS_WINDOWS_DESKTOP)
70
71 /* If I meet the engineer that wrote the dbghelp.h file for the windows 8.1 SDK we're gonna have words! */
72 # ifdef _MSC_VER
73 # pragma warning(disable : 4091)
74 # endif
75 # include <dbghelp.h>
76
77 struct win_symbol_data {
78 struct _SYMBOL_INFO sym_info;
79 char symbol_name[1024];
80 };
81
82 typedef BOOL __stdcall SymInitialize_fn(_In_ HANDLE hProcess, _In_opt_ PCSTR UserSearchPath, _In_ BOOL fInvadeProcess);
83 typedef DWORD __stdcall SymSetOptions_fn(DWORD SymOptions);
84
85 typedef BOOL __stdcall SymFromAddr_fn(
86 _In_ HANDLE hProcess,
87 _In_ DWORD64 Address,
88 _Out_opt_ PDWORD64 Displacement,
89 _Inout_ PSYMBOL_INFO Symbol);
90
91 # if defined(_WIN64)
92 typedef BOOL __stdcall SymGetLineFromAddr_fn(
93 _In_ HANDLE hProcess,
94 _In_ DWORD64 qwAddr,
95 _Out_ PDWORD pdwDisplacement,
96 _Out_ PIMAGEHLP_LINE64 Line64);
97 # define SymGetLineFromAddrName "SymGetLineFromAddr64"
98 # else
99 typedef BOOL __stdcall SymGetLineFromAddr_fn(
100 _In_ HANDLE hProcess,
101 _In_ DWORD dwAddr,
102 _Out_ PDWORD pdwDisplacement,
103 _Out_ PIMAGEHLP_LINE Line);
104 # define SymGetLineFromAddrName "SymGetLineFromAddr"
105 # endif
106
107 static SymInitialize_fn *s_SymInitialize = NULL;
108 static SymSetOptions_fn *s_SymSetOptions = NULL;
109 static SymFromAddr_fn *s_SymFromAddr = NULL;
110 static SymGetLineFromAddr_fn *s_SymGetLineFromAddr = NULL;
111
112 static aws_thread_once s_init_once = AWS_THREAD_ONCE_STATIC_INIT;
113 static void s_init_dbghelp_impl(void *user_data) {
114 (void)user_data;
115 HMODULE dbghelp = LoadLibraryA("DbgHelp.dll");
116 if (!dbghelp) {
117 fprintf(stderr, "Failed to load DbgHelp.dll.\n");
118 goto done;
119 }
120
121 s_SymInitialize = (SymInitialize_fn *)GetProcAddress(dbghelp, "SymInitialize");
122 if (!s_SymInitialize) {
123 fprintf(stderr, "Failed to load SymInitialize from DbgHelp.dll.\n");
124 goto done;
125 }
126
127 s_SymSetOptions = (SymSetOptions_fn *)GetProcAddress(dbghelp, "SymSetOptions");
128 if (!s_SymSetOptions) {
129 fprintf(stderr, "Failed to load SymSetOptions from DbgHelp.dll\n");
130 goto done;
131 }
132
133 s_SymFromAddr = (SymFromAddr_fn *)GetProcAddress(dbghelp, "SymFromAddr");
134 if (!s_SymFromAddr) {
135 fprintf(stderr, "Failed to load SymFromAddr from DbgHelp.dll.\n");
136 goto done;
137 }
138
139 s_SymGetLineFromAddr = (SymGetLineFromAddr_fn *)GetProcAddress(dbghelp, SymGetLineFromAddrName);
140 if (!s_SymGetLineFromAddr) {
141 fprintf(stderr, "Failed to load " SymGetLineFromAddrName " from DbgHelp.dll.\n");
142 goto done;
143 }
144
145 HANDLE process = GetCurrentProcess();
146 AWS_FATAL_ASSERT(process);
147 s_SymInitialize(process, NULL, TRUE);
148 s_SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_ANYTHING | SYMOPT_LOAD_LINES);
149 return;
150
151 done:
152 if (dbghelp) {
153 FreeLibrary(dbghelp);
154 }
155 return;
156 }
157
158 static bool s_init_dbghelp() {
159 if (AWS_LIKELY(s_SymInitialize)) {
160 return true;
161 }
162
163 aws_thread_call_once(&s_init_once, s_init_dbghelp_impl, NULL);
164 return s_SymInitialize != NULL;
165 }
166
167 size_t aws_backtrace(void **stack_frames, size_t num_frames) {
168 return (int)CaptureStackBackTrace(0, (ULONG)num_frames, stack_frames, NULL);
169 }
170
171 char **aws_backtrace_symbols(void *const *stack_frames, size_t num_frames) {
172 if (!s_init_dbghelp()) {
173 return NULL;
174 }
175
176 struct aws_byte_buf symbols;
177 aws_byte_buf_init(&symbols, aws_default_allocator(), num_frames * 256);
178 /* pointers for each stack entry */
179 memset(symbols.buffer, 0, num_frames * sizeof(void *));
180 symbols.len += num_frames * sizeof(void *);
181
182 DWORD64 displacement = 0;
183 DWORD disp = 0;
184
185 struct aws_byte_cursor null_term = aws_byte_cursor_from_array("", 1);
186 HANDLE process = GetCurrentProcess();
187 AWS_FATAL_ASSERT(process);
188 for (size_t i = 0; i < num_frames; ++i) {
189 /* record a pointer to where the symbol will be */
190 *((char **)&symbols.buffer[i * sizeof(void *)]) = (char *)symbols.buffer + symbols.len;
191
192 uintptr_t address = (uintptr_t)stack_frames[i];
193 struct win_symbol_data sym_info;
194 AWS_ZERO_STRUCT(sym_info);
195 sym_info.sym_info.MaxNameLen = sizeof(sym_info.symbol_name);
196 sym_info.sym_info.SizeOfStruct = sizeof(struct _SYMBOL_INFO);
197
198 char sym_buf[1024]; /* scratch space for extracting info */
199 if (s_SymFromAddr(process, address, &displacement, &sym_info.sym_info)) {
200 /* record the address and name */
201 int len = snprintf(
202 sym_buf, AWS_ARRAY_SIZE(sym_buf), "at 0x%llX: %s", sym_info.sym_info.Address, sym_info.sym_info.Name);
203 if (len != -1) {
204 struct aws_byte_cursor symbol = aws_byte_cursor_from_array(sym_buf, len);
205 aws_byte_buf_append_dynamic(&symbols, &symbol);
206 }
207
208 IMAGEHLP_LINE line;
209 line.SizeOfStruct = sizeof(IMAGEHLP_LINE);
210 if (s_SymGetLineFromAddr(process, address, &disp, &line)) {
211 /* record file/line info */
212 len = snprintf(sym_buf, AWS_ARRAY_SIZE(sym_buf), "(%s:%lu)", line.FileName, line.LineNumber);
213 if (len != -1) {
214 struct aws_byte_cursor symbol = aws_byte_cursor_from_array(sym_buf, len);
215 aws_byte_buf_append_dynamic(&symbols, &symbol);
216 }
217 }
218 } else {
219 /* no luck, record the address and last error */
220 DWORD last_error = GetLastError();
221 int len = snprintf(
222 sym_buf,
223 AWS_ARRAY_SIZE(sym_buf),
224 "at 0x%p: Failed to lookup symbol: error %u",
225 stack_frames[i],
226 last_error);
227 if (len > 0) {
228 struct aws_byte_cursor sym_cur = aws_byte_cursor_from_array(sym_buf, len);
229 aws_byte_buf_append_dynamic(&symbols, &sym_cur);
230 }
231 }
232
233 /* Null terminator */
234 aws_byte_buf_append_dynamic(&symbols, &null_term);
235 }
236
237 return (char **)symbols.buffer; /* buffer must be freed by the caller */
238 }
239
240 char **aws_backtrace_addr2line(void *const *stack_frames, size_t stack_depth) {
241 return aws_backtrace_symbols(stack_frames, stack_depth);
242 }
243
244 void aws_backtrace_print(FILE *fp, void *call_site_data) {
245 struct _EXCEPTION_POINTERS *exception_pointers = call_site_data;
246 if (exception_pointers) {
247 fprintf(fp, "** Exception 0x%x occured **\n", exception_pointers->ExceptionRecord->ExceptionCode);
248 }
249
250 if (!s_init_dbghelp()) {
251 fprintf(fp, "Unable to initialize dbghelp.dll");
252 return;
253 }
254
255 void *stack[1024];
256 size_t num_frames = aws_backtrace(stack, 1024);
257 char **symbols = aws_backtrace_symbols(stack, num_frames);
258 for (size_t line = 0; line < num_frames; ++line) {
259 const char *symbol = symbols[line];
260 fprintf(fp, "%s\n", symbol);
261 }
262 fflush(fp);
263 aws_mem_release(aws_default_allocator(), symbols);
264 }
265
266 void aws_backtrace_log(int log_level) {
267 if (!s_init_dbghelp()) {
268 AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "Unable to initialize dbghelp.dll for backtrace");
269 return;
270 }
271
272 void *stack[1024];
273 size_t num_frames = aws_backtrace(stack, 1024);
274 char **symbols = aws_backtrace_symbols(stack, num_frames);
275 for (size_t line = 0; line < num_frames; ++line) {
276 const char *symbol = symbols[line];
277 AWS_LOGF(log_level, AWS_LS_COMMON_GENERAL, "%s", symbol);
278 }
279 aws_mem_release(aws_default_allocator(), symbols);
280 }
281 #else /* !AWS_OS_WINDOWS_DESKTOP */
282 size_t aws_backtrace(void **stack_frames, size_t num_frames) {
283 (void)stack_frames;
284 (void)num_frames;
285 return 0;
286 }
287 char **aws_backtrace_symbols(void *const *stack_frames, size_t stack_depth) {
288 (void)stack_frames;
289 (void)stack_depth;
290 return NULL;
291 }
292
293 char **aws_backtrace_addr2line(void *const *stack_frames, size_t stack_depth) {
294 return aws_backtrace_symbols(stack_frames, stack_depth);
295 }
296
297 void aws_backtrace_print(FILE *fp, void *call_site_data) {
298 (void)fp;
299 (void)call_site_data;
300 AWS_LOGF_TRACE(
301 AWS_LS_COMMON_GENERAL, "aws_backtrace_print: backtrace requested, but logging is unsupported on this platform");
302 }
303
304 void aws_backtrace_log(int log_level) {
305 (void)log_level;
306 AWS_LOGF_TRACE(
307 AWS_LS_COMMON_GENERAL, "aws_backtrace_log: backtrace requested, but logging is unsupported on this platform");
308 }
309
310 #endif /* AWS_OS_WINDOWS_DESKTOP */