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