GCC Code Coverage Report


Directory: src/gate/
File: src/gate/callstacks.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 92 116 79.3%
Functions: 11 11 100.0%
Branches: 23 42 54.8%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/callstacks.h"
30 #include "gate/strings.h"
31 #include "gate/results.h"
32 #include "gate/platforms.h"
33
34
35 #if defined(GATE_SYS_WINCE)
36 # define GATE_CALLSTACKS_WINCE_TRACE_IMPL 1
37 # define GATE_CALLSTACKS_DEFAULT_SETJMP 1
38 # define GATE_CALLSTACKS_WIN32_CALLSTACK_IMPL
39 #elif defined(GATE_SYS_WASM_WEB)
40 # define GATE_CALLSTACKS_NO_IMPL 1
41 # define GATE_CALLSTACKS_NO_FORK_IMPL 1
42 #elif defined(GATE_SYS_WASM_WASI)
43 # define GATE_CALLSTACKS_NO_IMPL 1
44 # define GATE_CALLSTACKS_DEFAULT_SETJMP 1
45 #elif defined(GATE_SYS_DOS)
46 # define GATE_CALLSTACKS_DEFAULT_SETJMP 1
47 # define GATE_CALLSTACKS_DOS_IMPL 1
48 #elif defined(GATE_SYS_WIN) && !defined(GATE_SYS_WIN16)
49 # define GATE_CALLSTACKS_WIN32_TRACE_DBG_IMPL 1
50 # define GATE_CALLSTACKS_DEFAULT_SETJMP 1
51 # define GATE_CALLSTACKS_WIN32_CALLSTACK_IMPL
52 #elif defined(GATE_SYS_BEOS)
53 # define GATE_CALLSTACKS_NO_IMPL 1
54 # define GATE_CALLSTACKS_DEFAULT_SETJMP 1
55 #elif defined(GATE_SYS_ANDROID)
56 # define GATE_CALLSTACKS_NO_IMPL 1
57 # define GATE_CALLSTACKS_DEFAULT_SETJMP 1
58 #elif defined(GATE_SYS_POSIX)
59 # define GATE_CALLSTACKS_POSIX_IMPL 1
60 #elif defined(GATE_SYS_EFI)
61 # define GATE_CALLSTACKS_EFI_IMPL 1
62 # define GATE_CALLSTACKS_DEFAULT_SETJMP 1
63 #else
64 # define GATE_CALLSTACKS_NO_IMPL 1
65 # define GATE_CALLSTACKS_DEFAULT_SETJMP 1
66 #endif
67
68
69 #if defined(GATE_CALLSTACKS_NO_FORK_IMPL)
70 gate_result_t gate_callstack_fork(gate_callstack_jmpbuf_t* jmpbuffer,
71 gate_entrypoint_t entry, void* param,
72 gate_entrypoint_t branch, void* branch_param)
73 {
74 return GATE_RESULT_NOTIMPLEMENTED;
75 }
76
77 void gate_callstack_jump(gate_callstack_jmpbuf_t* jmpbuffer)
78 {
79 }
80
81 #endif /* GATE_CALLSTACKS_NO_FORK_IMPL */
82
83 #if defined(GATE_CALLSTACKS_DEFAULT_SETJMP)
84
85 #if defined(GATE_SYS_EFI) && defined(GATE_COMPILER_MSVC) && (_MSC_VER < 1930)
86 typedef void* jmp_buf[48];
87 #define setjmp _setjmp
88 extern int setjmp(jmp_buf buf);
89 extern void longjmp(jmp_buf buf, int n);
90 #else
91 #include <setjmp.h>
92 #endif
93
94 typedef struct gate_native_jmp_buf_class
95 {
96 jmp_buf buf;
97 } gate_native_jmp_buf_t;
98
99 gate_result_t gate_callstack_fork(gate_callstack_jmpbuf_t* jmpbuffer,
100 gate_entrypoint_t entry, void* param,
101 gate_entrypoint_t branch, void* branch_param)
102 {
103 gate_result_t ret = GATE_RESULT_OK;
104 gate_native_jmp_buf_t* ptr_buf = (gate_native_jmp_buf_t*)jmpbuffer;
105 if (0 == setjmp(ptr_buf->buf))
106 {
107 if (entry)
108 {
109 ret = entry(param);
110 }
111 }
112 else
113 {
114 if (branch)
115 {
116 ret = branch(branch_param);
117 }
118 }
119 gate_mem_clear(jmpbuffer, sizeof(gate_callstack_jmpbuf_t));
120 return ret;
121 }
122
123 void gate_callstack_jump(gate_callstack_jmpbuf_t* jmpbuffer)
124 {
125 gate_native_jmp_buf_t* ptr_buf = (gate_native_jmp_buf_t*)jmpbuffer;
126 longjmp(ptr_buf->buf, 1);
127 }
128
129 #endif /* GATE_CALLSTACKS_DEFAULT_SETJMP */
130
131
132
133
134 #if defined(GATE_CALLSTACKS_WINCE_TRACE_IMPL)
135
136 #include "gate/libraries.h"
137
138 #include <windows.h>
139 #if defined(GATE_COMPILER_MSVC)
140 #include <Kfuncs.h>
141 #endif
142
143 typedef struct _CallSnapshot
144 {
145 DWORD dwReturnAddr;
146 } CallSnapshot;
147
148 typedef ULONG(*GetThreadCallStack_t)(
149 HANDLE hThrd,
150 ULONG dwMaxFrames,
151 LPVOID lpFrames[],
152 DWORD dwFlags,
153 DWORD dwSkip
154 );
155
156
157 static gate_library_t gate_callstack_lib = NULL;
158 static GetThreadCallStack_t GetThreadCallStackFunction = NULL;
159
160 static gate_bool_t gate_callstack_init()
161 {
162 static gate_string_t const lib_binary = { "coredll.dll", 11, NULL };
163 gate_result_t ret;
164
165 if (gate_callstack_lib == NULL)
166 {
167 ret = gate_library_open(&lib_binary, &gate_callstack_lib, 0);
168 if (GATE_FAILED(ret))
169 {
170 return false;
171 }
172 }
173 if (GetThreadCallStackFunction == NULL)
174 {
175 ret = gate_library_get_function_name(gate_callstack_lib, "GetThreadCallStack", &GetThreadCallStackFunction);
176 if (GATE_FAILED(ret))
177 {
178 return false;
179 }
180 }
181
182 return true;
183 }
184
185 gate_result_t gate_callstack_trace(gate_callstack_address_t* addresses, gate_size_t addresses_count, gate_size_t* addresses_used)
186 {
187 HANDLE hThread = GetCurrentThread();
188 CallSnapshot stack_addresses[1024];
189 DWORD dwCount, dwIndex;
190
191 if (!gate_callstack_init())
192 {
193 return GATE_RESULT_NOTSUPPORTED;
194 }
195
196 dwCount = GetThreadCallStackFunction(hThread, sizeof(stack_addresses) / sizeof(stack_addresses[0]),
197 (void**)&stack_addresses[0], 0, 0);
198
199 if (dwCount > addresses_count)
200 {
201 dwCount = addresses_count;
202 }
203 for (dwIndex = 0; dwIndex != dwCount; ++dwIndex)
204 {
205 addresses[dwIndex] = (gate_callstack_address_t)stack_addresses[dwIndex].dwReturnAddr;
206 }
207 if (addresses_used != NULL)
208 {
209 *addresses_used = dwCount;
210 }
211 return GATE_RESULT_OK;
212 }
213 gate_result_t gate_callstack_print(gate_callstack_address_t address, char* buffer, gate_size_t buffer_length, gate_size_t* buffer_used)
214 {
215 gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
216
217 return ret;
218 }
219
220 #endif /* GATE_CALLSTACKS_WINCE_IMPL */
221
222
223
224
225
226 #if defined(GATE_CALLSTACKS_WIN32_TRACE_DBG_IMPL)
227
228 #include "gate/libraries.h"
229
230 #define WIN32_LEAN_AND_MEAN
231 #include <tchar.h>
232 #include <windows.h>
233
234 #include "gate/platforms.h"
235
236 static gate_library_t gate_win32_dbghlp_module = NULL;
237
238 #define MAX_SYM_NAME 2000
239
240 typedef enum
241 {
242 SymNone = 0,
243 SymCoff,
244 SymCv,
245 SymPdb,
246 SymExport,
247 SymDeferred,
248 SymSym, // .sym file
249 SymDia,
250 SymVirtual,
251 NumSymTypes
252 } SYM_TYPE;
253
254 typedef struct _IMAGEHLP_MODULE64
255 {
256 DWORD SizeOfStruct; /* set to sizeof(IMAGEHLP_MODULE64) */
257 DWORD64 BaseOfImage; /* base load address of module */
258 DWORD ImageSize; /* virtual size of the loaded module */
259 DWORD TimeDateStamp; /* date/time stamp from pe header */
260 DWORD CheckSum; /* checksum from the pe header */
261 DWORD NumSyms; /* number of symbols in the symbol table */
262 SYM_TYPE SymType; /* type of symbols loaded */
263 CHAR ModuleName[32]; /* module name */
264 CHAR ImageName[256]; /* image name */
265 /* new elements: 07-Jun-2002 */
266 CHAR LoadedImageName[256]; /* symbol file name */
267 CHAR LoadedPdbName[256]; /* pdb file name */
268 DWORD CVSig; /* Signature of the CV record in the debug directories */
269 CHAR CVData[MAX_PATH * 3]; /* Contents of the CV record */
270 DWORD PdbSig; /* Signature of PDB */
271 GUID PdbSig70; /* Signature of PDB (VC 7 and up) */
272 DWORD PdbAge; /* DBI age of pdb */
273 BOOL PdbUnmatched; /* loaded an unmatched pdb */
274 BOOL DbgUnmatched; /* loaded an unmatched dbg */
275 BOOL LineNumbers; /* we have line number information */
276 BOOL GlobalSymbols; /* we have internal symbol information */
277 BOOL TypeInfo; /* we have type information */
278 } IMAGEHLP_MODULE64, * PIMAGEHLP_MODULE64;
279
280 typedef PVOID(WINAPI* PtrSymFunctionTableAccess64)(HANDLE hProcess, DWORD64 AddrBase);
281 static PtrSymFunctionTableAccess64 SymFunctionTableAccess64 = NULL;
282
283 typedef DWORD64(WINAPI* PtrSymGetModuleBase64)(HANDLE hProcess, DWORD64 qwAddr);
284 static PtrSymGetModuleBase64 SymGetModuleBase64 = NULL;
285
286 typedef enum
287 {
288 AddrMode1616,
289 AddrMode1632,
290 AddrModeReal,
291 AddrModeFlat
292 } ADDRESS_MODE;
293
294 typedef struct _tagADDRESS64
295 {
296 DWORD64 Offset;
297 WORD Segment;
298 ADDRESS_MODE Mode;
299 } ADDRESS64, * LPADDRESS64;
300
301
302 typedef BOOL(__stdcall* PREAD_PROCESS_MEMORY_ROUTINE64)(
303 HANDLE hProcess,
304 DWORD64 qwBaseAddress,
305 PVOID lpBuffer,
306 DWORD nSize,
307 LPDWORD lpNumberOfBytesRead
308 );
309
310 typedef PVOID(__stdcall* PFUNCTION_TABLE_ACCESS_ROUTINE64)(
311 HANDLE hProcess,
312 DWORD64 AddrBase
313 );
314
315 typedef DWORD64(__stdcall* PGET_MODULE_BASE_ROUTINE64)(
316 HANDLE hProcess,
317 DWORD64 Address
318 );
319
320 typedef DWORD64(__stdcall* PTRANSLATE_ADDRESS_ROUTINE64)(
321 HANDLE hProcess,
322 HANDLE hThread,
323 LPADDRESS64 lpaddr
324 );
325
326 typedef struct _KDHELP64
327 {
328 DWORD64 Thread;
329 DWORD ThCallbackStack;
330 DWORD ThCallbackBStore;
331 DWORD NextCallback;
332 DWORD FramePointer;
333 DWORD64 KiCallUserMode;
334 DWORD64 KeUserCallbackDispatcher;
335 DWORD64 SystemRangeStart;
336 DWORD64 Reserved[8];
337 } KDHELP64, * PKDHELP64;
338
339 typedef struct _tagSTACKFRAME64
340 {
341 ADDRESS64 AddrPC;
342 ADDRESS64 AddrReturn;
343 ADDRESS64 AddrFrame;
344 ADDRESS64 AddrStack;
345 ADDRESS64 AddrBStore;
346 PVOID FuncTableEntry;
347 DWORD64 Params[4];
348 BOOL Far;
349 BOOL Virtual;
350 DWORD64 Reserved[3];
351 KDHELP64 KdHelp;
352 } STACKFRAME64, * LPSTACKFRAME64;
353
354 typedef BOOL(WINAPI* PtrSymInitialize)(HANDLE, PCSTR, BOOL);
355 static PtrSymInitialize SymInitialize = NULL;
356
357 typedef BOOL(WINAPI* PtrSymCleanup)(HANDLE);
358 static PtrSymCleanup SymCleanup = NULL;
359
360 typedef struct _IMAGEHLP_SYMBOL64
361 {
362 DWORD SizeOfStruct; // set to sizeof(IMAGEHLP_SYMBOL64)
363 DWORD64 Address; // virtual address including dll base address
364 DWORD Size; // estimated size of symbol, can be zero
365 DWORD Flags; // info about the symbols, see the SYMF defines
366 DWORD MaxNameLength; // maximum size of symbol name in 'Name'
367 CHAR Name[1]; // symbol name (null terminated string)
368 } IMAGEHLP_SYMBOL64, * PIMAGEHLP_SYMBOL64;
369
370 typedef BOOL(WINAPI* PtrStackWalk64)(
371 DWORD MachineType,
372 HANDLE hProcess,
373 HANDLE hThread,
374 LPSTACKFRAME64 StackFrame,
375 PVOID ContextRecord,
376 PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,
377 PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
378 PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
379 PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress
380 );
381 static PtrStackWalk64 StackWalk64 = NULL;
382
383
384 typedef BOOL(WINAPI* PtrSymGetModuleInfo64)(HANDLE hProcess, DWORD64 qwAddr, PIMAGEHLP_MODULE64 ModuleInfo);
385 static PtrSymGetModuleInfo64 SymGetModuleInfo64 = NULL;
386
387 typedef BOOL(WINAPI* PtrSymGetSymFromAddr64)(HANDLE hProcess, DWORD64 qwAddr, PDWORD64 pdwDisplacement, PIMAGEHLP_SYMBOL64 Symbol);
388 static PtrSymGetSymFromAddr64 SymGetSymFromAddr64 = NULL;
389
390 static gate_bool_t gate_callstack_load_api(void* ptr_to_func_ptr, char const* func_name)
391 {
392 gate_result_t ret;
393
394 if ((*(void**)ptr_to_func_ptr) == NULL)
395 {
396 ret = gate_library_get_function_name(gate_win32_dbghlp_module, func_name, ptr_to_func_ptr);
397 if (GATE_FAILED(ret))
398 {
399 return false;
400 }
401 }
402 return true;
403 }
404
405 static gate_bool_t gate_callstack_init()
406 {
407 static gate_string_t const str_dbghelp = { "Dbghelp.dll", 11, NULL };
408 gate_result_t ret;
409
410 if (gate_win32_dbghlp_module == NULL)
411 {
412 ret = gate_library_open(&str_dbghelp, &gate_win32_dbghlp_module, 0);
413 if (GATE_FAILED(ret))
414 {
415 return false;
416 }
417 }
418
419 ret = true;
420 ret &= gate_callstack_load_api(&SymInitialize, "SymInitialize");
421 ret &= gate_callstack_load_api(&SymCleanup, "SymCleanup");
422 ret &= gate_callstack_load_api(&StackWalk64, "StackWalk64");
423 ret &= gate_callstack_load_api(&SymGetModuleInfo64, "SymGetModuleInfo64");
424 ret &= gate_callstack_load_api(&SymGetSymFromAddr64, "SymGetSymFromAddr64");
425 ret &= gate_callstack_load_api(&SymFunctionTableAccess64, "SymFunctionTableAccess64");
426 ret &= gate_callstack_load_api(&SymGetModuleBase64, "SymGetModuleBase64");
427 ret &= gate_callstack_load_api(&SymInitialize, "SymInitialize");
428 ret &= gate_callstack_load_api(&SymInitialize, "SymInitialize");
429 ret &= gate_callstack_load_api(&SymInitialize, "SymInitialize");
430
431 return ret;
432 }
433
434 #ifndef IMAGE_FILE_MACHINE_I386
435 #define IMAGE_FILE_MACHINE_I386 0x014c
436 #endif
437
438 #ifndef IMAGE_FILE_MACHINE_AMD64
439 #define IMAGE_FILE_MACHINE_AMD64 0x8664
440 #endif
441
442 #if defined(GATE_COMPILER_MSVC) || defined(GATE_COMPILER_CLANG)
443 # define GATE_WINAPI_CPU_CONTEXT CONTEXT
444 # if (GATE_ARCH == GATE_ARCH_X86X64)
445 # define MACHINE_FLAG IMAGE_FILE_MACHINE_AMD64
446 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
447 gate_platform.Win32RtlCaptureContext(&ctx); \
448 frame.AddrFrame.Mode = AddrModeFlat; \
449 frame.AddrFrame.Offset = (DWORD64)ctx.Rbp; \
450 frame.AddrStack.Mode = AddrModeFlat; \
451 frame.AddrStack.Offset = (DWORD64)ctx.Rsp; \
452 frame.AddrPC.Mode = AddrModeFlat; \
453 frame.AddrPC.Offset = (DWORD64)ctx.Rip; \
454 } while(0)
455 # elif (GATE_ARCH == GATE_ARCH_ARM64)
456 # define MACHINE_FLAG IMAGE_FILE_MACHINE_ARM64
457 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
458 gate_platform.Win32RtlCaptureContext(&ctx); \
459 frame.AddrFrame.Mode = AddrModeFlat; \
460 frame.AddrFrame.Offset = (DWORD64)ctx.Fp; \
461 frame.AddrStack.Mode = AddrModeFlat; \
462 frame.AddrStack.Offset = (DWORD64)ctx.Sp; \
463 frame.AddrPC.Mode = AddrModeFlat; \
464 frame.AddrPC.Offset = (DWORD64)ctx.Pc; \
465 frame.AddrReturn.Mode = AddrModeFlat; \
466 frame.AddrReturn.Offset = (DWORD64)ctx.Lr; \
467 } while(0)
468 # elif (GATE_ARCH == GATE_ARCH_ARM32)
469 # define MACHINE_FLAG IMAGE_FILE_MACHINE_ARM
470 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
471 gate_platform.Win32RtlCaptureContext(&ctx); \
472 frame.AddrFrame.Mode = AddrModeFlat; \
473 frame.AddrFrame.Offset = (DWORD64)ctx.R12; \
474 frame.AddrStack.Mode = AddrModeFlat; \
475 frame.AddrStack.Offset = (DWORD64)ctx.Sp; \
476 frame.AddrPC.Mode = AddrModeFlat; \
477 frame.AddrPC.Offset = (DWORD64)ctx.Pc; \
478 } while(0)
479 # elif (GATE_ARCH == GATE_ARCH_X86IA32)
480 # define MACHINE_FLAG IMAGE_FILE_MACHINE_I386
481 # if defined(GATE_COMPILER_CLANG)
482 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
483 gate_platform.Win32RtlCaptureContext(&ctx); \
484 frame.AddrFrame.Mode = AddrModeFlat; \
485 frame.AddrFrame.Offset = (DWORD64)ctx.Ebp; \
486 frame.AddrStack.Mode = AddrModeFlat; \
487 frame.AddrStack.Offset = (DWORD64)ctx.Esp; \
488 frame.AddrPC.Mode = AddrModeFlat; \
489 frame.AddrPC.Offset = (DWORD64)ctx.Eip; \
490 } while(0)
491 # else
492 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
493 size_t * ptrFrame = 0; \
494 size_t ptrStack = 0; \
495 _asm { mov ptrFrame, ebp } \
496 _asm { mov ptrStack, esp } \
497 ctx.Ebp = (DWORD)(size_t)(ptrFrame); \
498 ctx.Esp = (DWORD)ptrStack; \
499 ctx.Eip = (DWORD)*(ptrFrame + 1); \
500 frame.AddrFrame.Mode = AddrModeFlat; \
501 frame.AddrFrame.Offset = (DWORD64)ctx.Ebp; \
502 frame.AddrStack.Mode = AddrModeFlat; \
503 frame.AddrStack.Offset = (DWORD64)ctx.Esp; \
504 frame.AddrPC.Mode = AddrModeFlat; \
505 frame.AddrPC.Offset = (DWORD64)ctx.Eip; \
506 } while(0)
507 # endif
508 # else
509 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
510 frame.AddrFrame.Mode = AddrModeFlat; \
511 frame.AddrFrame.Offset = (DWORD64)0; \
512 frame.AddrStack.Mode = AddrModeFlat; \
513 frame.AddrStack.Offset = (DWORD64)0; \
514 frame.AddrPC.Mode = AddrModeFlat; \
515 frame.AddrPC.Offset = (DWORD64)0; \
516 } while(0)
517 # endif
518 #elif defined(GATE_COMPILER_GCC)
519 # define GATE_WINAPI_CPU_CONTEXT CONTEXT
520 # if defined(GATE_SYS_WIN64)
521 # define MACHINE_FLAG IMAGE_FILE_MACHINE_AMD64
522 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
523 gate_platform.Win32RtlCaptureContext(&ctx); \
524 frame.AddrFrame.Mode = AddrModeFlat; \
525 frame.AddrFrame.Offset = (DWORD64)ctx.Rbp; \
526 frame.AddrStack.Mode = AddrModeFlat; \
527 frame.AddrStack.Offset = (DWORD64)ctx.Rsp; \
528 frame.AddrPC.Mode = AddrModeFlat; \
529 frame.AddrPC.Offset = (DWORD64)ctx.Rip; \
530 } while(0)
531 # else
532 # define MACHINE_FLAG IMAGE_FILE_MACHINE_I386
533 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
534 size_t * ptrFrame = 0; \
535 size_t ptrStack = 0; \
536 asm ( "movl %%ebp, %0;" : "=r" ( ptrFrame ) ); \
537 asm ( "movl %%esp, %0;" : "=r" ( ptrStack ) ); \
538 ctx.Ebp = (DWORD)(size_t)(ptrFrame); \
539 ctx.Esp = (DWORD)ptrStack; \
540 ctx.Eip = (DWORD)*(ptrFrame + 1); \
541 frame.AddrFrame.Mode = AddrModeFlat; \
542 frame.AddrFrame.Offset = (DWORD64)ctx.Ebp; \
543 frame.AddrStack.Mode = AddrModeFlat; \
544 frame.AddrStack.Offset = (DWORD64)ctx.Esp; \
545 frame.AddrPC.Mode = AddrModeFlat; \
546 frame.AddrPC.Offset = (DWORD64)ctx.Eip; \
547 } while(0)
548 # endif
549 #elif defined(GATE_COMPILER_WATCOM)
550 # define GATE_WINAPI_CPU_CONTEXT CONTEXT
551 # if (GATE_ARCH == GATE_ARCH_X86IA32)
552 # define MACHINE_FLAG IMAGE_FILE_MACHINE_I386
553 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
554 size_t * ptrFrame = 0; \
555 size_t ptrStack = 0; \
556 ctx.Ebp = (DWORD)(size_t)(ptrFrame); \
557 ctx.Esp = (DWORD)ptrStack; \
558 ctx.Eip = (DWORD)*(ptrFrame + 1); \
559 frame.AddrFrame.Mode = AddrModeFlat; \
560 frame.AddrFrame.Offset = (DWORD64)ctx.Ebp; \
561 frame.AddrStack.Mode = AddrModeFlat; \
562 frame.AddrStack.Offset = (DWORD64)ctx.Esp; \
563 frame.AddrPC.Mode = AddrModeFlat; \
564 frame.AddrPC.Offset = (DWORD64)ctx.Eip; \
565 } while(0)
566 # endif
567 #elif defined(GATE_COMPILER_TCC)
568 typedef struct
569 {
570 DWORD ControlWord;
571 DWORD StatusWord;
572 DWORD TagWord;
573 DWORD ErrorOffset;
574 DWORD ErrorSelector;
575 DWORD DataOffset;
576 DWORD DataSelector;
577 BYTE RegisterArea[80];
578 DWORD Spare0;
579 } WIN32_CPU_FLOATING_SAVE_AREA;
580
581 typedef struct
582 {
583 DWORD ContextFlags;
584
585 DWORD Dr0;
586 DWORD Dr1;
587 DWORD Dr2;
588 DWORD Dr3;
589 DWORD Dr6;
590 DWORD Dr7;
591
592 WIN32_CPU_FLOATING_SAVE_AREA FloatSave;
593
594 DWORD SegGs;
595 DWORD SegFs;
596 DWORD SegEs;
597 DWORD SegDs;
598
599 DWORD Edi;
600 DWORD Esi;
601 DWORD Ebx;
602 DWORD Edx;
603 DWORD Ecx;
604 DWORD Eax;
605
606 DWORD Ebp;
607 DWORD Eip;
608 DWORD SegCs;
609 DWORD EFlags;
610 DWORD Esp;
611 DWORD SegSs;
612
613 BYTE ExtendedRegisters[512];
614 } WIN32_CPU_CONTEXT;
615
616 #define GATE_WINAPI_CPU_CONTEXT WIN32_CPU_CONTEXT
617 # if (GATE_ARCH == GATE_ARCH_X86IA32)
618 # define MACHINE_FLAG IMAGE_FILE_MACHINE_I386
619 # define GATE_DEBUG_GETFRAME(frame, ctx) do { \
620 size_t * ptrFrame = 0; \
621 size_t ptrStack = 0; \
622 ctx.Ebp = (DWORD)(size_t)(ptrFrame); \
623 ctx.Esp = (DWORD)ptrStack; \
624 ctx.Eip = (DWORD)*(ptrFrame + 1); \
625 frame.AddrFrame.Mode = AddrModeFlat; \
626 frame.AddrFrame.Offset = (DWORD64)ctx.Ebp; \
627 frame.AddrStack.Mode = AddrModeFlat; \
628 frame.AddrStack.Offset = (DWORD64)ctx.Esp; \
629 frame.AddrPC.Mode = AddrModeFlat; \
630 frame.AddrPC.Offset = (DWORD64)ctx.Eip; \
631 } while(0)
632 # endif
633
634 #endif
635
636
637 gate_result_t gate_callstack_trace(gate_callstack_address_t* addresses, gate_size_t addresses_count, gate_size_t* addresses_used)
638 {
639 gate_result_t ret;
640 HANDLE hThread = GetCurrentThread();
641 HANDLE hProcess = NULL;
642 STACKFRAME64 frame;
643 GATE_WINAPI_CPU_CONTEXT ctx;
644
645 DWORD dwMachineFlag = MACHINE_FLAG;
646 LPCONTEXT pctx = NULL;
647 gate_size_t frames_found = 0;
648 BOOL symcleanup_required = FALSE;
649
650 do
651 {
652 if (!gate_callstack_init())
653 {
654 ret = GATE_RESULT_NOTSUPPORTED;
655 break;
656 }
657
658 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
659 if (hProcess == NULL)
660 {
661 ret = GATE_RESULT_OUTOFRESOURCES;
662 break;
663 }
664
665 symcleanup_required = SymInitialize(hProcess, NULL, TRUE);
666
667 gate_mem_clear(&frame, sizeof(frame));
668 gate_mem_clear(&ctx, sizeof(ctx));
669
670 GATE_DEBUG_GETFRAME(frame, ctx);
671
672 #if (GATE_ARCH == GATE_ARCH_ARM64)
673 dwMachineFlag = IMAGE_FILE_MACHINE_ARM64;
674 pctx = &ctx;
675 #elif (GATE_ARCH == GATE_ARCH_X86X64)
676 dwMachineFlag = IMAGE_FILE_MACHINE_AMD64;
677 pctx = &ctx;
678 #endif
679 if (!StackWalk64(dwMachineFlag, hProcess, hThread, &frame, pctx, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL))
680 {
681 ret = GATE_RESULT_FAILED;
682 break;
683 }
684 ret = GATE_RESULT_OK;
685 while (addresses_count-- != 0)
686 {
687 if (StackWalk64(dwMachineFlag, hProcess, hThread, &frame, pctx, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL))
688 {
689 *addresses = (gate_callstack_address_t)(gate_uintptr_t)frame.AddrPC.Offset;
690 ++frames_found;
691 ++addresses;
692 }
693 else
694 {
695 break;
696 }
697 }
698 if (addresses_used != 0)
699 {
700 *addresses_used = frames_found;
701 }
702 } while (0);
703
704 if (symcleanup_required)
705 {
706 SymCleanup(hProcess);
707 }
708
709 if (hProcess != NULL)
710 {
711 CloseHandle(hProcess);
712 }
713
714 return ret;
715 }
716 gate_result_t gate_callstack_print(gate_callstack_address_t address, char* buffer, gate_size_t buffer_length, gate_size_t* buffer_used)
717 {
718 gate_result_t ret = GATE_RESULT_FAILED;
719 HANDLE hProcess = NULL;
720 IMAGEHLP_MODULE64 mod64;
721 DWORD64 dwAddr;
722 DWORD64 dwDisp = 0;
723 char bufSym[sizeof(IMAGEHLP_SYMBOL64) + MAX_SYM_NAME];
724 IMAGEHLP_SYMBOL64* pSym = (IMAGEHLP_SYMBOL64*)&bufSym[0];
725 gate_size_t len;
726
727 do
728 {
729 if (!gate_callstack_init())
730 {
731 ret = GATE_RESULT_NOTSUPPORTED;
732 break;
733 }
734
735 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
736 if (hProcess == NULL)
737 {
738 ret = GATE_RESULT_OUTOFRESOURCES;
739 break;
740 }
741
742 gate_mem_clear(&mod64, sizeof(mod64));
743 mod64.SizeOfStruct = sizeof(mod64);
744 dwAddr = (DWORD64)(gate_uintptr_t)address;
745 SymGetModuleInfo64(hProcess, dwAddr, &mod64);
746 gate_mem_clear(pSym, sizeof(IMAGEHLP_SYMBOL64) + MAX_SYM_NAME);
747 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
748 pSym->MaxNameLength = MAX_SYM_NAME;
749
750 if (SymGetSymFromAddr64(hProcess, dwAddr, &dwDisp, pSym))
751 {
752 len = gate_str_print_text(buffer, buffer_length, pSym->Name, gate_str_length(pSym->Name));
753 if (buffer_used != NULL)
754 {
755 *buffer_used = len;
756 }
757 ret = GATE_RESULT_OK;
758 }
759 else
760 {
761 gate_str_print_hex_uint64(buffer, buffer_length, (gate_uint64_t)(gate_uintptr_t)address, false);
762 ret = GATE_RESULT_OK_PARTIAL;
763 }
764 } while (0);
765
766 if (hProcess != NULL)
767 {
768 CloseHandle(hProcess);
769 }
770
771 return ret;
772 }
773
774 #endif /* GATE_CALLSTACKS_WIN32_DBG_IMPL */
775
776
777 #ifdef GATE_CALLSTACKS_WIN32_CALLSTACK_IMPL
778
779 #include "gate/platform/windows/win32callstack.h"
780
781 #include "gate/platforms.h"
782 #include <windows.h>
783
784
785 #if defined(GATE_COMPILE_ASM_SUPPORT)
786
787 #ifndef MEM_COMMIT
788 #define MEM_COMMIT 0x1000
789 #endif
790
791 #ifndef PAGE_READWRITE
792 #define PAGE_READWRITE 0x04
793 #endif
794
795 #ifndef PAGE_EXECUTE_READ
796 #define PAGE_EXECUTE_READ 0x20
797 #endif
798
799 #ifndef PAGE_EXECUTE_READWRITE
800 #define PAGE_EXECUTE_READWRITE 0x40
801 #endif
802
803 #ifndef MEM_RELEASE
804 #define MEM_RELEASE 0x8000
805 #endif
806
807 gate_result_t gate_callstack_create(gate_size_t* ptr_stacksize, void** ptr_callstack)
808 {
809 gate_result_t ret = GATE_RESULT_FAILED;
810 LPVOID stack_buffer = NULL;
811 DWORD old_protect = 0;
812 gate_size_t stacksize;
813 do
814 {
815 if (!ptr_callstack || !ptr_stacksize)
816 {
817 ret = GATE_RESULT_INVALIDARG;
818 break;
819 }
820 stacksize = *ptr_stacksize;
821 stacksize &= ~((gate_size_t)0xff);
822 if (stacksize == 0)
823 {
824 stacksize = 256 * 1024;
825 }
826 if (stacksize < 4096)
827 {
828 stacksize = 4096;
829 }
830 stack_buffer = VirtualAlloc(NULL, stacksize, MEM_COMMIT, PAGE_READWRITE);
831 if (NULL == stack_buffer)
832 {
833 break;
834 }
835 if (!VirtualProtect(stack_buffer, stacksize, PAGE_EXECUTE_READWRITE, &old_protect))
836 {
837 VirtualFree(stack_buffer, 0, MEM_RELEASE);
838 }
839
840 ret = GATE_RESULT_OK;
841 *ptr_stacksize = stacksize;
842 *ptr_callstack = stack_buffer;
843
844 } while (0);
845 return ret;
846 }
847 gate_result_t gate_callstack_destroy(void* ptr_callstack, gate_size_t stacksize)
848 {
849 gate_result_t ret = GATE_RESULT_FAILED;
850 if (ptr_callstack)
851 {
852 if (VirtualFree((LPVOID)ptr_callstack, 0, MEM_RELEASE))
853 {
854 ret = GATE_RESULT_OK;
855 }
856 }
857 return ret;
858 }
859
860
861 gate_result_t gate_callstack_run(void* ptr_callstack, gate_size_t stacksize,
862 gate_callstack_context_t* ptr_caller_ctx_to_save,
863 gate_entrypoint_t entry_func, void* param,
864 gate_result_t* ptr_returnvalue)
865 {
866 gate_result_t ret = GATE_RESULT_FAILED;
867 gate_size_t stack_addr = (gate_size_t)ptr_callstack;
868 gate_callstack_context_t dummy_context = GATE_INIT_EMPTY;
869 void* entry_ret = NULL;
870
871 #if !defined(GATE_SYS_WINCE)
872 gate_size_t stack_top = stack_addr + (stacksize - 4);
873 stack_top -= (stack_top & 0x0f);
874 entry_ret = gate_win32_callstack_create((void*)stack_top,
875 (ptr_caller_ctx_to_save != NULL) ? ptr_caller_ctx_to_save : &dummy_context,
876 entry_func, param);
877 ret = GATE_RESULT_OK;
878 #endif
879 if (ptr_returnvalue)
880 {
881 *ptr_returnvalue = (gate_result_t)(gate_intptr_t)entry_ret;
882 }
883 return ret;
884 }
885
886 gate_result_t gate_callstack_switch(gate_callstack_context_t* current_to_save,
887 gate_callstack_context_t* next_to_load)
888 {
889 gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
890 #if !defined(GATE_SYS_WINCE)
891 if (NULL == gate_win32_callstack_switch(current_to_save, next_to_load))
892 {
893 ret = GATE_RESULT_FAILED;
894 }
895 else
896 {
897 ret = GATE_RESULT_OK;
898 }
899 #endif
900 return ret;
901 }
902
903 #else /* GATE_COMPILE_ASM_SUPPORT */
904
905 /* use FIBERs if no native ASM support is present */
906
907 typedef struct gate_callstack_fiber_class
908 {
909 LPVOID fiber;
910 LPVOID parent;
911 gate_entrypoint_t entrypoint;
912 void* param;
913 gate_result_t result;
914
915 } gate_callstack_fiber_t;
916
917
918 static VOID WINAPI gate_callstack_fiber_dispatcher(LPVOID lpFiberParameter)
919 {
920 gate_callstack_fiber_t* ptr_fiber = (gate_callstack_fiber_t*)lpFiberParameter;
921 if (ptr_fiber && ptr_fiber->entrypoint)
922 {
923 ptr_fiber->result = ptr_fiber->entrypoint(ptr_fiber->param);
924 gate_win32_fibers()->Win32SwitchToFiber(ptr_fiber->parent);
925 }
926 }
927
928
929 gate_result_t gate_callstack_create(gate_size_t* ptr_stacksize, void** ptr_callstack)
930 {
931 gate_result_t ret = GATE_RESULT_FAILED;
932 gate_callstack_fiber_t* ptr_fiber = NULL;
933 gate_size_t stacksize = 0;
934 gate_win32_fibers_t* fibers = NULL;
935
936 do
937 {
938 if (!ptr_callstack || !ptr_stacksize)
939 {
940 ret = GATE_RESULT_INVALIDARG;
941 break;
942 }
943 stacksize = *ptr_stacksize;
944 stacksize &= ~((gate_size_t)0xff);
945 if (stacksize == 0)
946 {
947 stacksize = 256 * 1024;
948 }
949 if (stacksize < 4096)
950 {
951 stacksize = 4096;
952 }
953
954 fibers = gate_win32_fibers();
955
956 if (!fibers)
957 {
958 ret = GATE_RESULT_NOTSUPPORTED;
959 break;
960 }
961
962 ptr_fiber = (gate_callstack_fiber_t*)gate_mem_alloc(sizeof(gate_callstack_fiber_t));
963 if (!ptr_fiber)
964 {
965 ret = GATE_RESULT_OUTOFMEMORY;
966 break;
967 }
968 gate_mem_clear(ptr_fiber, sizeof(gate_callstack_fiber_t));
969
970 ptr_fiber->fiber = fibers->Win32CreateFiber(stacksize, &gate_callstack_fiber_dispatcher, ptr_fiber);
971 if (ptr_fiber == NULL)
972 {
973 ret = gate_platform_print_last_error(NULL, 0);
974 break;
975 }
976
977 *ptr_callstack = (void*)ptr_fiber;
978 *ptr_stacksize = stacksize;
979 ptr_fiber = NULL;
980 ret = GATE_RESULT_OK;
981 } while (0);
982
983 if (ptr_fiber)
984 {
985 gate_mem_dealloc(ptr_fiber);
986 }
987 return ret;
988 }
989 gate_result_t gate_callstack_destroy(void* ptr_callstack, gate_size_t stacksize)
990 {
991 gate_result_t ret = GATE_RESULT_FAILED;
992 gate_callstack_fiber_t* ptr_fiber = (gate_callstack_fiber_t*)ptr_callstack;
993
994 GATE_UNUSED_ARG(stacksize);
995
996 if (ptr_fiber)
997 {
998 gate_win32_fibers_t* fibers = gate_win32_fibers();
999 if (fibers)
1000 {
1001 fibers->Win32DeleteFiber(ptr_fiber->fiber);
1002 }
1003 gate_mem_dealloc(ptr_fiber);
1004 ret = GATE_RESULT_OK;
1005 }
1006 return ret;
1007 }
1008
1009
1010 gate_result_t gate_callstack_run(void* ptr_callstack, gate_size_t stacksize,
1011 gate_callstack_context_t* ptr_caller_ctx_to_save,
1012 gate_entrypoint_t entry_func, void* param,
1013 gate_result_t* ptr_returnvalue)
1014 {
1015 gate_result_t ret = GATE_RESULT_FAILED;
1016 gate_callstack_fiber_t* ptr_fiber = (gate_callstack_fiber_t*)ptr_callstack;
1017 gate_callstack_context_t local_context = GATE_INIT_EMPTY;
1018 LPVOID current_fiber = NULL;
1019 gate_bool_t was_fiber = false;
1020
1021 GATE_UNUSED_ARG(stacksize);
1022
1023 do
1024 {
1025 gate_win32_fibers_t* fibers = gate_win32_fibers();
1026 if (!fibers)
1027 {
1028 break;
1029 }
1030
1031 if (NULL == ptr_caller_ctx_to_save)
1032 {
1033 ptr_caller_ctx_to_save = &local_context;
1034 }
1035
1036 ptr_fiber->entrypoint = entry_func;
1037 ptr_fiber->param = param;
1038
1039 current_fiber = fibers->Win32ConvertThreadToFiber(NULL);
1040 if (current_fiber != NULL)
1041 {
1042 /* previously a thread, now a fiber */
1043 was_fiber = false;
1044 }
1045 else
1046 {
1047 /* was already a fiber, retrieve its handle*/
1048 was_fiber = true;
1049 current_fiber = fibers->Win32GetCurrentFiber();
1050 if (NULL == current_fiber)
1051 {
1052 ret = GATE_RESULT_OUTOFRESOURCES;
1053 break;
1054 }
1055 }
1056 ptr_fiber->parent = current_fiber;
1057 ptr_caller_ctx_to_save->store[0] = current_fiber;
1058
1059 fibers->Win32SwitchToFiber(ptr_fiber->fiber);
1060
1061 ptr_fiber->parent = NULL;
1062
1063 if (ptr_returnvalue)
1064 {
1065 *ptr_returnvalue = ptr_fiber->result;
1066 }
1067
1068 if (!was_fiber)
1069 {
1070 if (fibers->Win32ConvertFiberToThread)
1071 {
1072 fibers->Win32ConvertFiberToThread();
1073 }
1074 }
1075 ret = GATE_RESULT_OK;
1076 } while (0);
1077
1078 return ret;
1079 }
1080
1081 gate_result_t gate_callstack_switch(gate_callstack_context_t* current_to_save,
1082 gate_callstack_context_t* next_to_load)
1083 {
1084 gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
1085 #if !defined(GATE_SYS_WINCE)
1086 gate_win32_fibers_t* fibers = gate_win32_fibers();
1087 if (fibers->Win32SwitchToFiber)
1088 {
1089 PVOID current_fiber = fibers->Win32GetCurrentFiber();
1090 PVOID next_fiber = next_to_load->store[0];
1091 current_to_save->store[0] = current_fiber;
1092 fibers->Win32SwitchToFiber(next_fiber);
1093 ret = GATE_RESULT_OK;
1094 }
1095 #endif
1096 return ret;
1097 }
1098
1099
1100 #endif /* GATE_COMPILE_ASM_SUPPORT */
1101
1102
1103 #endif /* GATE_CALLSTACKS_WIN32_CALLSTACK_IMPL */
1104
1105
1106
1107
1108
1109
1110 #if defined(GATE_CALLSTACKS_POSIX_IMPL)
1111
1112 #include <stdlib.h>
1113 #include <dlfcn.h>
1114 #include <stdio.h>
1115 #include <setjmp.h>
1116 #include <signal.h>
1117 #include <sys/mman.h>
1118 #include "gate/platform/posix/posixcallstack.h"
1119 #include "gate/platform/posix/posixucontext.h"
1120 #include "gate/debugging.h"
1121
1122 #if defined(HAVE_BACKTRACE)
1123 #include <execinfo.h>
1124 #endif
1125
1126 #define DEFAULT_STACK_SIZE (1024 * 1024)
1127 #define MINMUM_STACK_SIZE (4 * 1024)
1128
1129 #if defined(GATE_SYS_BSD)
1130 typedef size_t backtrace_length_t;
1131 #else
1132 typedef int backtrace_length_t;
1133 #endif
1134
1135 typedef backtrace_length_t(*backtrace_func_t)(void** buffer, backtrace_length_t size);
1136 typedef char** (*backtrace_symbols_func_t)(void* const* buffer, backtrace_length_t size);
1137
1138 static backtrace_func_t volatile backtrace_func = NULL;
1139 static backtrace_symbols_func_t volatile backtrace_symbols_func = NULL;
1140
1141
1142 #if defined(HAVE_BACKTRACE)
1143
1144 2 static gate_bool_t load_backtrace_functions()
1145 {
1146 2 backtrace_func = &backtrace;
1147 2 backtrace_symbols_func = &backtrace_symbols;
1148 2 return true;
1149 }
1150
1151 #else
1152
1153 static void* volatile execinfo_symbols = NULL;
1154
1155 static void close_execinfo_atexit()
1156 {
1157 if (NULL != execinfo_symbols)
1158 {
1159 void* const lib = execinfo_symbols;
1160 execinfo_symbols = NULL;
1161 dlclose(lib);
1162 }
1163 }
1164
1165 static void load_backtrace_from_libexecinfo()
1166 {
1167 if (execinfo_symbols == NULL)
1168 {
1169 execinfo_symbols = gate_posix_dlopen("libexecinfo.so", RTLD_GLOBAL | RTLD_NOW);
1170 if (NULL != execinfo_symbols)
1171 {
1172 gate_platform_atexit(&close_execinfo_atexit);
1173 }
1174 }
1175
1176 if (execinfo_symbols != NULL)
1177 {
1178 backtrace_func = dlsym(execinfo_symbols, "backtrace");
1179 backtrace_symbols_func = dlsym(execinfo_symbols, "backtrace_symbols");
1180 }
1181 }
1182
1183 static gate_bool_t load_backtrace_functions()
1184 {
1185 if (!backtrace_func || backtrace_symbols_func)
1186 {
1187 /* lookup in global function table: */
1188 backtrace_func = gate_posix_global_sym("backtrace");
1189 backtrace_symbols_func = gate_posix_global_sym("backtrace_symbols");
1190
1191 if ((NULL == backtrace_func) || (NULL == backtrace_symbols_func))
1192 {
1193 load_backtrace_from_libexecinfo();
1194 }
1195 }
1196 return backtrace_func && backtrace_symbols_func;
1197 }
1198
1199 #endif
1200
1201
1202 1 gate_result_t gate_callstack_trace(gate_callstack_address_t* addresses, gate_size_t addresses_count, gate_size_t* addresses_used)
1203 {
1204 1 gate_result_t ret = GATE_RESULT_OK;
1205 void* stackaddress[1024];
1206 backtrace_length_t index;
1207 backtrace_length_t stackaddress_count;
1208
1209 1 load_backtrace_functions();
1210
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!backtrace_func)
1212 {
1213 ✗ GATE_DEBUG_TRACE("backtrace() not supported");
1214 ✗ return GATE_RESULT_NOTSUPPORTED;
1215 }
1216
1217 1 stackaddress_count = backtrace_func(stackaddress, sizeof(stackaddress) / sizeof(stackaddress[0]));
1218
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (stackaddress_count > 0)
1219 {
1220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (stackaddress_count > (backtrace_length_t)addresses_count)
1221 {
1222 ✗ stackaddress_count = (backtrace_length_t)addresses_count;
1223 }
1224
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (index = 0; index != stackaddress_count; ++index)
1225 {
1226 12 addresses[index] = stackaddress[index];
1227 }
1228 1 *addresses_used = (gate_size_t)stackaddress_count;
1229 1 ret = GATE_RESULT_OK;
1230 }
1231 else
1232 {
1233 ✗ *addresses_used = 0;
1234 }
1235 1 return ret;
1236 }
1237
1238 1 gate_result_t gate_callstack_print(gate_callstack_address_t address, char* buffer, gate_size_t buffer_length, gate_size_t* buffer_used)
1239 {
1240 gate_size_t len;
1241 char** names;
1242
1243 1 load_backtrace_functions();
1244
1245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (backtrace_symbols_func == NULL)
1246 {
1247 ✗ return GATE_RESULT_NOTSUPPORTED;
1248 }
1249
1250 1 names = backtrace_symbols_func((void**)&address, 1);
1251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (names == NULL)
1252 {
1253 ✗ return GATE_RESULT_FAILED;
1254 }
1255
1256 1 len = gate_str_print_text(buffer, buffer_length, names[0], gate_str_length(names[0]));
1257 1 free(names);
1258 1 buffer[len] = 0;
1259 1 *buffer_used = len;
1260 1 return GATE_RESULT_OK;
1261 }
1262
1263 #define GATE_CALLSTACK_MALLOC 1
1264
1265 6 static void check_stack_size(gate_size_t* ptr_stacksize)
1266 {
1267
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (*ptr_stacksize == 0)
1268 {
1269 1 *ptr_stacksize = DEFAULT_STACK_SIZE;
1270 }
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (*ptr_stacksize < MINMUM_STACK_SIZE)
1272 {
1273 ✗ *ptr_stacksize = MINMUM_STACK_SIZE;
1274 }
1275 /* apply 32-bit maximum and 16-bit alignment */
1276 6 *ptr_stacksize &= 0xfffffff0;
1277 6 }
1278
1279 2 gate_result_t gate_callstack_create(gate_size_t* ptr_stacksize, void** ptr_callstack)
1280 {
1281 void* ptr;
1282 gate_size_t stacksize;
1283
1284 2 check_stack_size(ptr_stacksize);
1285 2 stacksize = *ptr_stacksize;
1286
1287 #if defined(GATE_CALLSTACK_MALLOC)
1288 2 ptr = gate_mem_alloc(stacksize);
1289 #else
1290 ptr = mmap(NULL, stacksize, PROT_READ | PROT_WRITE /*| PROT_EXEC*/,
1291 MAP_PRIVATE | MAP_ANONYMOUS /*| MAP_GROWSDOWN*/ | MAP_STACK, -1, 0);
1292 if (ptr == MAP_FAILED)
1293 {
1294 return GATE_RESULT_FAILED;
1295 }
1296 #endif
1297
1298
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptr_callstack)
1299 {
1300 2 *ptr_callstack = ptr;
1301 }
1302 else
1303 {
1304 ✗ gate_callstack_destroy(ptr, stacksize);
1305 }
1306 2 return GATE_RESULT_OK;
1307 }
1308
1309 2 gate_result_t gate_callstack_destroy(void* ptr_callstack, gate_size_t stacksize)
1310 {
1311 2 check_stack_size(&stacksize);
1312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ptr_callstack)
1313 {
1314 ✗ return GATE_RESULT_INVALIDARG;
1315 }
1316 #if defined(GATE_CALLSTACK_MALLOC)
1317 2 gate_mem_dealloc(ptr_callstack);
1318 #else
1319 if (0 == munmap(ptr_callstack, stacksize))
1320 {
1321 return GATE_RESULT_OK;
1322 }
1323 else
1324 {
1325 return GATE_RESULT_FAILED;
1326 }
1327 #endif
1328 2 return GATE_RESULT_OK;
1329 }
1330
1331 typedef struct gate_callstack_session_class
1332 {
1333 gate_entrypoint_t entry_func;
1334 void* param;
1335 gate_result_t* ptr_returnvalue;
1336 gate_callstack_context_t* parent_context;
1337 } gate_callstack_session_t;
1338
1339 union gate_callstack_entry_dispatcher_args
1340 {
1341 gate_callstack_session_t* session;
1342 int args[4];
1343 };
1344
1345 2 static void gate_callstack_entry_dispatcher(int a0, int a1, int a2, int a3)
1346 {
1347 union gate_callstack_entry_dispatcher_args args;
1348 gate_result_t result;
1349 gate_callstack_context_t dummy_context;
1350
1351 2 args.args[0] = a0;
1352 2 args.args[1] = a1;
1353 2 args.args[2] = a2;
1354 2 args.args[3] = a3;
1355 2 result = args.session->entry_func(args.session->param);
1356
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (args.session->ptr_returnvalue)
1357 {
1358 1 *args.session->ptr_returnvalue = result;
1359 }
1360 1 gate_posix_uctx_set(args.session->parent_context);
1361 ✗ }
1362
1363 2 gate_result_t gate_callstack_run(void* ptr_callstack, gate_size_t stacksize,
1364 gate_callstack_context_t* ptr_caller_ctx_to_save,
1365 gate_entrypoint_t entry_func, void* param,
1366 gate_result_t* ptr_returnvalue)
1367 {
1368 2 gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
1369 2 gate_size_t stack_addr = (gate_size_t)ptr_callstack;
1370 void* stack_top;
1371 2 gate_callstack_context_t context = GATE_INIT_EMPTY;
1372 2 gate_callstack_context_t new_stack_context = GATE_INIT_EMPTY;
1373 void* entry_ret;
1374 gate_callstack_session_t session;
1375 union gate_callstack_entry_dispatcher_args disp_args;
1376
1377 2 check_stack_size(&stacksize);
1378
1379
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (NULL == ptr_caller_ctx_to_save)
1380 {
1381 1 ptr_caller_ctx_to_save = &context;
1382 }
1383
1384
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (gate_posix_callstack_supported())
1385 {
1386 ✗ stack_addr += (stacksize - 4);
1387 ✗ stack_addr -= (stack_addr & 0x0f);
1388 ✗ stack_top = (void*)stack_addr;
1389
1390 ✗ entry_ret = gate_posix_callstack_create(stack_top, ptr_caller_ctx_to_save, entry_func, param);
1391 ✗ ret = GATE_RESULT_OK;
1392 ✗ if (ptr_returnvalue)
1393 {
1394 ✗ *ptr_returnvalue = (gate_result_t)(gate_intptr_t)entry_ret;
1395 }
1396 }
1397 else
1398 {
1399 do
1400 {
1401 2 ret = gate_posix_uctx_init();
1402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
1403
1404
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (0 != gate_posix_uctx_get(&new_stack_context))
1405 {
1406 ✗ ret = GATE_RESULT_FAILED;
1407 ✗ break;
1408 }
1409 2 gate_posix_uctx_setup(&new_stack_context, ptr_callstack, stacksize);
1410 2 session.entry_func = entry_func;
1411 2 session.param = param;
1412 2 session.ptr_returnvalue = ptr_returnvalue;
1413 2 session.parent_context = ptr_caller_ctx_to_save;
1414 2 disp_args.session = &session;
1415 2 gate_posix_uctx_make(&new_stack_context, &gate_callstack_entry_dispatcher,
1416 disp_args.args[0], disp_args.args[1], disp_args.args[2], disp_args.args[3]);
1417 2 gate_posix_uctx_swap(ptr_caller_ctx_to_save, &new_stack_context);
1418
1419 2 ret = GATE_RESULT_OK;
1420 } while (0);
1421 }
1422 2 return ret;
1423 }
1424
1425 1 gate_result_t gate_callstack_switch(gate_callstack_context_t* current_to_save, gate_callstack_context_t* next_to_load)
1426 {
1427 1 gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
1428
1429
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (gate_posix_callstack_supported())
1430 {
1431 ✗ if (NULL == gate_posix_callstack_switch(current_to_save, next_to_load))
1432 {
1433 ✗ ret = GATE_RESULT_FAILED;
1434 }
1435 else
1436 {
1437 ✗ ret = GATE_RESULT_OK;
1438 }
1439 }
1440 else
1441 {
1442 1 gate_posix_uctx_swap(current_to_save, next_to_load);
1443 ✗ ret = GATE_RESULT_OK;
1444 }
1445 ✗ return ret;
1446 }
1447
1448
1449 typedef struct gate_native_jmp_buf_class
1450 {
1451 sigjmp_buf buf;
1452 } gate_native_jmp_buf_t;
1453
1454 9 gate_result_t gate_callstack_fork(gate_callstack_jmpbuf_t* jmpbuffer,
1455 gate_entrypoint_t entry, void* param,
1456 gate_entrypoint_t branch, void* branch_param)
1457 {
1458 9 gate_result_t volatile ret = GATE_RESULT_OK;
1459 9 gate_native_jmp_buf_t* volatile ptr_buf = (gate_native_jmp_buf_t*)jmpbuffer;
1460
1461 if (sizeof(gate_callstack_jmpbuf_t) < sizeof(gate_native_jmp_buf_t))
1462 {
1463 /* generic jump buffer is to small */
1464 return GATE_RESULT_OUTOFMEMORY;
1465 }
1466
1467
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 4 times.
9 if (0 == sigsetjmp(ptr_buf->buf, 1))
1468 {
1469
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (entry)
1470 {
1471 9 ret = entry(param);
1472 }
1473 }
1474 else
1475 {
1476
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (branch)
1477 {
1478 4 ret = branch(branch_param);
1479 }
1480 }
1481 9 gate_mem_clear(jmpbuffer, sizeof(gate_callstack_jmpbuf_t));
1482 9 return ret;
1483 }
1484
1485 4 void gate_callstack_jump(gate_callstack_jmpbuf_t* jmpbuffer)
1486 {
1487 4 gate_native_jmp_buf_t* ptr_buf = (gate_native_jmp_buf_t*)jmpbuffer;
1488 4 siglongjmp(ptr_buf->buf, 1);
1489 }
1490
1491
1492 #endif /* GATE_CALLSTACKS_POSIX_IMPL */
1493
1494
1495
1496 #if defined(GATE_CALLSTACKS_EFI_IMPL)
1497
1498 #include "gate/platform/efi/efi_gate.h"
1499
1500 #if defined(GATE_COMPILER_MSVC)
1501 # include "gate/platform/windows/win32callstack.h"
1502 # define gate_callstack_create_impl gate_win32_callstack_create
1503 # define gate_callstack_switch_impl gate_win32_callstack_switch
1504 #else
1505 # include "gate/platform/posix/posixcallstack.h"
1506 # define gate_callstack_create_impl gate_posix_callstack_create
1507 # define gate_callstack_switch_impl gate_posix_callstack_switch
1508 #endif
1509
1510
1511
1512 gate_result_t gate_callstack_trace(gate_callstack_address_t* addresses, gate_size_t addresses_count, gate_size_t* addresses_used)
1513 {
1514 /*TODO*/
1515 return GATE_RESULT_NOTIMPLEMENTED;
1516 }
1517 gate_result_t gate_callstack_print(gate_callstack_address_t address, char* buffer, gate_size_t buffer_length, gate_size_t* buffer_used)
1518 {
1519 /*TODO*/
1520 return GATE_RESULT_NOTIMPLEMENTED;
1521 }
1522
1523
1524
1525 gate_result_t gate_callstack_create(gate_size_t* ptr_stacksize, void** ptr_callstack)
1526 {
1527 gate_result_t ret = GATE_RESULT_FAILED;
1528 void* ptr;
1529 gate_size_t stacksize = 0;
1530
1531 if (!ptr_stacksize || !ptr_callstack)
1532 {
1533 ret = GATE_RESULT_INVALIDARG;
1534 }
1535 else
1536 {
1537 stacksize = *ptr_stacksize;
1538 if (stacksize < 4096)
1539 {
1540 stacksize = 4096;
1541 }
1542 stacksize &= ~((gate_size_t)0xff);
1543 if (stacksize == 0)
1544 {
1545 stacksize = 256 * 1024;
1546 }
1547 ptr = gate_mem_alloc(stacksize);
1548 if (ptr)
1549 {
1550 ret = GATE_RESULT_OK;
1551 if (ptr_callstack)
1552 {
1553 *ptr_callstack = ptr;
1554 *ptr_stacksize = stacksize;
1555 }
1556 else
1557 {
1558 gate_mem_dealloc(ptr);
1559 }
1560 }
1561 }
1562
1563 return ret;
1564 }
1565
1566 gate_result_t gate_callstack_destroy(void* ptr_callstack, gate_size_t stacksize)
1567 {
1568 if (!ptr_callstack)
1569 {
1570 return GATE_RESULT_INVALIDARG;
1571 }
1572
1573 gate_mem_dealloc(ptr_callstack);
1574 return GATE_RESULT_OK;
1575 }
1576
1577
1578 gate_result_t gate_callstack_run(void* ptr_callstack, gate_size_t stacksize,
1579 gate_callstack_context_t* ptr_caller_ctx_to_save,
1580 gate_entrypoint_t entry_func, void* param,
1581 gate_result_t* ptr_returnvalue)
1582 {
1583 gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
1584 gate_size_t stack_addr = (gate_size_t)ptr_callstack;
1585 void* stack_top;
1586 gate_callstack_context_t context = GATE_INIT_EMPTY;
1587 void* entry_ret;
1588
1589 if (NULL == ptr_caller_ctx_to_save)
1590 {
1591 ptr_caller_ctx_to_save = &context;
1592 }
1593
1594 stack_addr += (stacksize - 4);
1595 stack_addr -= (stack_addr & 0x0f);
1596 stack_top = (void*)stack_addr;
1597
1598 entry_ret = gate_callstack_create_impl(stack_top, ptr_caller_ctx_to_save, entry_func, param);
1599 ret = GATE_RESULT_OK;
1600 if (ptr_returnvalue)
1601 {
1602 *ptr_returnvalue = (gate_result_t)(gate_intptr_t)entry_ret;
1603 }
1604 return ret;
1605 }
1606
1607 gate_result_t gate_callstack_switch(gate_callstack_context_t* current_to_save,
1608 gate_callstack_context_t* next_to_load)
1609 {
1610 gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
1611 if (NULL == gate_callstack_switch_impl(current_to_save, next_to_load))
1612 {
1613 ret = GATE_RESULT_FAILED;
1614 }
1615 else
1616 {
1617 ret = GATE_RESULT_OK;
1618 }
1619 return ret;
1620 }
1621
1622 #endif /* GATE_CALLSTACKS_EFI_IMPL */
1623
1624
1625 #if defined(GATE_CALLSTACKS_DOS_IMPL)
1626
1627 #include "gate/platforms.h"
1628 #include "gate/platform/dos/doscallstack.h"
1629
1630
1631 gate_result_t gate_callstack_trace(gate_callstack_address_t* addresses, gate_size_t addresses_count, gate_size_t* addresses_used)
1632 {
1633 GATE_UNUSED_ARG(addresses);
1634 GATE_UNUSED_ARG(addresses_count);
1635 GATE_UNUSED_ARG(addresses_used);
1636 return GATE_RESULT_FAILED;
1637 }
1638
1639 gate_result_t gate_callstack_print(gate_callstack_address_t address, char* buffer, gate_size_t buffer_length, gate_size_t* buffer_used)
1640 {
1641 GATE_UNUSED_ARG(address);
1642 GATE_UNUSED_ARG(buffer);
1643 GATE_UNUSED_ARG(buffer_length);
1644 GATE_UNUSED_ARG(buffer_used);
1645 return GATE_RESULT_FAILED;
1646 }
1647
1648 #define DEFAULT_STACK_SIZE 16384
1649
1650 gate_result_t gate_callstack_create(gate_size_t* ptr_stacksize, void** ptr_callstack)
1651 {
1652 gate_result_t result = GATE_RESULT_OUTOFMEMORY;
1653 gate_size_t para_count;
1654 unsigned segment = 0;
1655 gate_size_t stacksize;
1656
1657 if (!ptr_stacksize || !ptr_callstack)
1658 {
1659 return GATE_RESULT_INVALIDARG;
1660 }
1661 stacksize = *ptr_stacksize;
1662
1663 if (stacksize == 0)
1664 {
1665 stacksize = DEFAULT_STACK_SIZE;
1666 }
1667 if (stacksize > 32768)
1668 {
1669 stacksize = 32768;
1670 }
1671 if (stacksize < 256)
1672 {
1673 stacksize = 256;
1674 }
1675 stacksize = (stacksize + 0x0f) & ~((gate_size_t)0x0f);
1676
1677 para_count = (stacksize + 15) / 16;
1678
1679 result = gate_dos_alloc(para_count, &segment);
1680 if (GATE_SUCCEEDED(result))
1681 {
1682 *ptr_callstack = (void*)gate_dos_farptr(segment, 0);
1683 *ptr_stacksize = stacksize;
1684 }
1685 return result;
1686 }
1687
1688 gate_result_t gate_callstack_destroy(void* ptr_callstack, gate_size_t stacksize)
1689 {
1690 unsigned segment = gate_dos_farptr_seg(ptr_callstack);
1691 GATE_UNUSED_ARG(stacksize);
1692 return gate_dos_dealloc(segment);
1693 }
1694
1695
1696 gate_result_t gate_callstack_run(void* ptr_callstack, gate_size_t stacksize, gate_callstack_context_t* ptr_caller_ctx_to_save,
1697 gate_entrypoint_t entry_func, void* param, gate_result_t* ptr_returnvalue)
1698 {
1699 gate_result_t result;
1700 char __far* ptr_stack_begin = (char __far*)ptr_callstack;
1701 char __far* ptr_stack_end;
1702
1703 if (stacksize == 0)
1704 {
1705 stacksize = DEFAULT_STACK_SIZE;
1706 }
1707 gate_mem_clear(ptr_callstack, stacksize);
1708 ptr_stack_end = ptr_stack_begin + stacksize - 2;
1709
1710
1711 result = gate_dos_callstack_create(ptr_stack_end, ptr_caller_ctx_to_save, entry_func, param);
1712 if (GATE_SUCCEEDED(result))
1713 {
1714 if (ptr_returnvalue)
1715 {
1716 *ptr_returnvalue = result;
1717 }
1718 }
1719 return result;
1720 }
1721
1722 gate_result_t gate_callstack_switch(gate_callstack_context_t* current_to_save,
1723 gate_callstack_context_t* next_to_load)
1724 {
1725 return gate_dos_callstack_switch(current_to_save, next_to_load);
1726 }
1727
1728 #endif /* GATE_CALLSTACKS_DOS_IMPL */
1729
1730
1731
1732 #if defined(GATE_CALLSTACKS_NO_IMPL)
1733
1734 gate_result_t gate_callstack_trace(gate_callstack_address_t* addresses, gate_size_t addresses_count, gate_size_t* addresses_used)
1735 {
1736 (void)addresses;
1737 (void)addresses_count;
1738 (void)addresses_used;
1739 return GATE_RESULT_NOTIMPLEMENTED;
1740 }
1741 gate_result_t gate_callstack_print(gate_callstack_address_t address, char* buffer, gate_size_t buffer_length, gate_size_t* buffer_used)
1742 {
1743 (void)address;
1744 (void)buffer;
1745 (void)buffer_length;
1746 (void)buffer_used;
1747 return GATE_RESULT_NOTIMPLEMENTED;
1748 }
1749
1750 gate_result_t gate_callstack_create(gate_size_t* ptr_stacksize, void** ptr_callstack)
1751 {
1752 (void)ptr_stacksize;
1753 (void)ptr_callstack;
1754 return GATE_RESULT_NOTIMPLEMENTED;
1755 }
1756 gate_result_t gate_callstack_destroy(void* ptr_callstack, gate_size_t stacksize)
1757 {
1758 (void)ptr_callstack;
1759 (void)stacksize;
1760 return GATE_RESULT_NOTIMPLEMENTED;
1761 }
1762 gate_result_t gate_callstack_run(void* ptr_callstack, gate_size_t stacksize, gate_callstack_context_t* ptr_caller_ctx_to_save,
1763 gate_entrypoint_t entry_func, void* param, gate_result_t* ptr_returnvalue)
1764 {
1765 (void)ptr_callstack;
1766 (void)stacksize;
1767 (void)ptr_caller_ctx_to_save;
1768 (void)entry_func;
1769 (void)param;
1770 (void)ptr_returnvalue;
1771 return GATE_RESULT_NOTIMPLEMENTED;
1772 }
1773
1774 gate_result_t gate_callstack_switch(gate_callstack_context_t* current_to_save, gate_callstack_context_t* next_to_load)
1775 {
1776 (void)current_to_save;
1777 (void)next_to_load;
1778 return GATE_RESULT_NOTIMPLEMENTED;
1779 }
1780
1781
1782 #endif /* GATE_CALLSTACKS_NO_IMPL */
1783
1784