forked from catboost/catboost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnwindCursor.hpp
2028 lines (1837 loc) · 68 KB
/
UnwindCursor.hpp
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
//===------------------------- UnwindCursor.hpp ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// C++ interface to lower levels of libunwind
//===----------------------------------------------------------------------===//
#ifndef __UNWINDCURSOR_HPP__
#define __UNWINDCURSOR_HPP__
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unwind.h>
#ifdef _WIN32
#include <windows.h>
#include <ntverp.h>
#endif
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
// Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
// earlier) SDKs.
// MinGW-w64 has always provided this struct.
#if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
!defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
struct _DISPATCHER_CONTEXT {
ULONG64 ControlPc;
ULONG64 ImageBase;
PRUNTIME_FUNCTION FunctionEntry;
ULONG64 EstablisherFrame;
ULONG64 TargetIp;
PCONTEXT ContextRecord;
PEXCEPTION_ROUTINE LanguageHandler;
PVOID HandlerData;
PUNWIND_HISTORY_TABLE HistoryTable;
ULONG ScopeIndex;
ULONG Fill0;
};
#endif
struct UNWIND_INFO {
uint8_t Version : 3;
uint8_t Flags : 5;
uint8_t SizeOfProlog;
uint8_t CountOfCodes;
uint8_t FrameRegister : 4;
uint8_t FrameOffset : 4;
uint16_t UnwindCodes[2];
};
extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
int, _Unwind_Action, uint64_t, _Unwind_Exception *,
struct _Unwind_Context *);
#endif
#include "config.h"
#include "AddressSpace.hpp"
#include "CompactUnwinder.hpp"
#include "config.h"
#include "DwarfInstructions.hpp"
#include "EHHeaderParser.hpp"
#include "libunwind.h"
#include "Registers.hpp"
#include "RWMutex.hpp"
#include "Unwind-EHABI.h"
namespace libunwind {
#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
/// Cache of recently found FDEs.
template <typename A>
class _LIBUNWIND_HIDDEN DwarfFDECache {
typedef typename A::pint_t pint_t;
public:
static constexpr pint_t kSearchAll = static_cast<pint_t>(-1);
static pint_t findFDE(pint_t mh, pint_t pc);
static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
static void removeAllIn(pint_t mh);
static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
unw_word_t ip_end,
unw_word_t fde, unw_word_t mh));
private:
struct entry {
pint_t mh;
pint_t ip_start;
pint_t ip_end;
pint_t fde;
};
// These fields are all static to avoid needing an initializer.
// There is only one instance of this class per process.
static RWMutex _lock;
#ifdef __APPLE__
static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
static bool _registeredForDyldUnloads;
#endif
static entry *_buffer;
static entry *_bufferUsed;
static entry *_bufferEnd;
static entry _initialBuffer[64];
};
template <typename A>
typename DwarfFDECache<A>::entry *
DwarfFDECache<A>::_buffer = _initialBuffer;
template <typename A>
typename DwarfFDECache<A>::entry *
DwarfFDECache<A>::_bufferUsed = _initialBuffer;
template <typename A>
typename DwarfFDECache<A>::entry *
DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
template <typename A>
typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
template <typename A>
RWMutex DwarfFDECache<A>::_lock;
#ifdef __APPLE__
template <typename A>
bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
#endif
template <typename A>
typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
pint_t result = 0;
_LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
for (entry *p = _buffer; p < _bufferUsed; ++p) {
if ((mh == p->mh) || (mh == kSearchAll)) {
if ((p->ip_start <= pc) && (pc < p->ip_end)) {
result = p->fde;
break;
}
}
}
_LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
return result;
}
template <typename A>
void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
pint_t fde) {
#if !defined(_LIBUNWIND_NO_HEAP)
_LIBUNWIND_LOG_IF_FALSE(_lock.lock());
if (_bufferUsed >= _bufferEnd) {
size_t oldSize = (size_t)(_bufferEnd - _buffer);
size_t newSize = oldSize * 4;
// Can't use operator new (we are below it).
entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
if (_buffer != _initialBuffer)
free(_buffer);
_buffer = newBuffer;
_bufferUsed = &newBuffer[oldSize];
_bufferEnd = &newBuffer[newSize];
}
_bufferUsed->mh = mh;
_bufferUsed->ip_start = ip_start;
_bufferUsed->ip_end = ip_end;
_bufferUsed->fde = fde;
++_bufferUsed;
#ifdef __APPLE__
if (!_registeredForDyldUnloads) {
_dyld_register_func_for_remove_image(&dyldUnloadHook);
_registeredForDyldUnloads = true;
}
#endif
_LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
#endif
}
template <typename A>
void DwarfFDECache<A>::removeAllIn(pint_t mh) {
_LIBUNWIND_LOG_IF_FALSE(_lock.lock());
entry *d = _buffer;
for (const entry *s = _buffer; s < _bufferUsed; ++s) {
if (s->mh != mh) {
if (d != s)
*d = *s;
++d;
}
}
_bufferUsed = d;
_LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
}
#ifdef __APPLE__
template <typename A>
void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
removeAllIn((pint_t) mh);
}
#endif
template <typename A>
void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
_LIBUNWIND_LOG_IF_FALSE(_lock.lock());
for (entry *p = _buffer; p < _bufferUsed; ++p) {
(*func)(p->ip_start, p->ip_end, p->fde, p->mh);
}
_LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
}
#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
template <typename A> class UnwindSectionHeader {
public:
UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
: _addressSpace(addressSpace), _addr(addr) {}
uint32_t version() const {
return _addressSpace.get32(_addr +
offsetof(unwind_info_section_header, version));
}
uint32_t commonEncodingsArraySectionOffset() const {
return _addressSpace.get32(_addr +
offsetof(unwind_info_section_header,
commonEncodingsArraySectionOffset));
}
uint32_t commonEncodingsArrayCount() const {
return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
commonEncodingsArrayCount));
}
uint32_t personalityArraySectionOffset() const {
return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
personalityArraySectionOffset));
}
uint32_t personalityArrayCount() const {
return _addressSpace.get32(
_addr + offsetof(unwind_info_section_header, personalityArrayCount));
}
uint32_t indexSectionOffset() const {
return _addressSpace.get32(
_addr + offsetof(unwind_info_section_header, indexSectionOffset));
}
uint32_t indexCount() const {
return _addressSpace.get32(
_addr + offsetof(unwind_info_section_header, indexCount));
}
private:
A &_addressSpace;
typename A::pint_t _addr;
};
template <typename A> class UnwindSectionIndexArray {
public:
UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
: _addressSpace(addressSpace), _addr(addr) {}
uint32_t functionOffset(uint32_t index) const {
return _addressSpace.get32(
_addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
functionOffset));
}
uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
return _addressSpace.get32(
_addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
secondLevelPagesSectionOffset));
}
uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
return _addressSpace.get32(
_addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
lsdaIndexArraySectionOffset));
}
private:
A &_addressSpace;
typename A::pint_t _addr;
};
template <typename A> class UnwindSectionRegularPageHeader {
public:
UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
: _addressSpace(addressSpace), _addr(addr) {}
uint32_t kind() const {
return _addressSpace.get32(
_addr + offsetof(unwind_info_regular_second_level_page_header, kind));
}
uint16_t entryPageOffset() const {
return _addressSpace.get16(
_addr + offsetof(unwind_info_regular_second_level_page_header,
entryPageOffset));
}
uint16_t entryCount() const {
return _addressSpace.get16(
_addr +
offsetof(unwind_info_regular_second_level_page_header, entryCount));
}
private:
A &_addressSpace;
typename A::pint_t _addr;
};
template <typename A> class UnwindSectionRegularArray {
public:
UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
: _addressSpace(addressSpace), _addr(addr) {}
uint32_t functionOffset(uint32_t index) const {
return _addressSpace.get32(
_addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
functionOffset));
}
uint32_t encoding(uint32_t index) const {
return _addressSpace.get32(
_addr +
arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
}
private:
A &_addressSpace;
typename A::pint_t _addr;
};
template <typename A> class UnwindSectionCompressedPageHeader {
public:
UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
: _addressSpace(addressSpace), _addr(addr) {}
uint32_t kind() const {
return _addressSpace.get32(
_addr +
offsetof(unwind_info_compressed_second_level_page_header, kind));
}
uint16_t entryPageOffset() const {
return _addressSpace.get16(
_addr + offsetof(unwind_info_compressed_second_level_page_header,
entryPageOffset));
}
uint16_t entryCount() const {
return _addressSpace.get16(
_addr +
offsetof(unwind_info_compressed_second_level_page_header, entryCount));
}
uint16_t encodingsPageOffset() const {
return _addressSpace.get16(
_addr + offsetof(unwind_info_compressed_second_level_page_header,
encodingsPageOffset));
}
uint16_t encodingsCount() const {
return _addressSpace.get16(
_addr + offsetof(unwind_info_compressed_second_level_page_header,
encodingsCount));
}
private:
A &_addressSpace;
typename A::pint_t _addr;
};
template <typename A> class UnwindSectionCompressedArray {
public:
UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
: _addressSpace(addressSpace), _addr(addr) {}
uint32_t functionOffset(uint32_t index) const {
return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
_addressSpace.get32(_addr + index * sizeof(uint32_t)));
}
uint16_t encodingIndex(uint32_t index) const {
return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
_addressSpace.get32(_addr + index * sizeof(uint32_t)));
}
private:
A &_addressSpace;
typename A::pint_t _addr;
};
template <typename A> class UnwindSectionLsdaArray {
public:
UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
: _addressSpace(addressSpace), _addr(addr) {}
uint32_t functionOffset(uint32_t index) const {
return _addressSpace.get32(
_addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
index, functionOffset));
}
uint32_t lsdaOffset(uint32_t index) const {
return _addressSpace.get32(
_addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
index, lsdaOffset));
}
private:
A &_addressSpace;
typename A::pint_t _addr;
};
#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
public:
// NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
// This avoids an unnecessary dependency to libc++abi.
void operator delete(void *, size_t) {}
virtual ~AbstractUnwindCursor() {}
virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
virtual void setReg(int, unw_word_t) {
_LIBUNWIND_ABORT("setReg not implemented");
}
virtual bool validFloatReg(int) {
_LIBUNWIND_ABORT("validFloatReg not implemented");
}
virtual unw_fpreg_t getFloatReg(int) {
_LIBUNWIND_ABORT("getFloatReg not implemented");
}
virtual void setFloatReg(int, unw_fpreg_t) {
_LIBUNWIND_ABORT("setFloatReg not implemented");
}
virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
virtual void getInfo(unw_proc_info_t *) {
_LIBUNWIND_ABORT("getInfo not implemented");
}
virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
virtual bool isSignalFrame() {
_LIBUNWIND_ABORT("isSignalFrame not implemented");
}
virtual bool getFunctionName(char *, size_t, unw_word_t *) {
_LIBUNWIND_ABORT("getFunctionName not implemented");
}
virtual void setInfoBasedOnIPRegister(bool = false) {
_LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
}
virtual const char *getRegisterName(int) {
_LIBUNWIND_ABORT("getRegisterName not implemented");
}
#ifdef __arm__
virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
#endif
};
#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
/// \c UnwindCursor contains all state (including all register values) during
/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
template <typename A, typename R>
class UnwindCursor : public AbstractUnwindCursor {
typedef typename A::pint_t pint_t;
public:
UnwindCursor(unw_context_t *context, A &as);
UnwindCursor(CONTEXT *context, A &as);
UnwindCursor(A &as, void *threadArg);
virtual ~UnwindCursor() {}
virtual bool validReg(int);
virtual unw_word_t getReg(int);
virtual void setReg(int, unw_word_t);
virtual bool validFloatReg(int);
virtual unw_fpreg_t getFloatReg(int);
virtual void setFloatReg(int, unw_fpreg_t);
virtual int step();
virtual void getInfo(unw_proc_info_t *);
virtual void jumpto();
virtual bool isSignalFrame();
virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
virtual const char *getRegisterName(int num);
#ifdef __arm__
virtual void saveVFPAsX();
#endif
DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
// libunwind does not and should not depend on C++ library which means that we
// need our own defition of inline placement new.
static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
private:
pint_t getLastPC() const { return _dispContext.ControlPc; }
void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
_dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
&_dispContext.ImageBase,
_dispContext.HistoryTable);
*base = _dispContext.ImageBase;
return _dispContext.FunctionEntry;
}
bool getInfoFromSEH(pint_t pc);
int stepWithSEHData() {
_dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
_dispContext.ImageBase,
_dispContext.ControlPc,
_dispContext.FunctionEntry,
_dispContext.ContextRecord,
&_dispContext.HandlerData,
&_dispContext.EstablisherFrame,
NULL);
// Update some fields of the unwind info now, since we have them.
_info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
if (_dispContext.LanguageHandler) {
_info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
} else
_info.handler = 0;
return UNW_STEP_SUCCESS;
}
A &_addressSpace;
unw_proc_info_t _info;
DISPATCHER_CONTEXT _dispContext;
CONTEXT _msContext;
UNWIND_HISTORY_TABLE _histTable;
bool _unwindInfoMissing;
};
template <typename A, typename R>
UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
: _addressSpace(as), _unwindInfoMissing(false) {
static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
"UnwindCursor<> does not fit in unw_cursor_t");
static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
"UnwindCursor<> requires more alignment than unw_cursor_t");
memset(&_info, 0, sizeof(_info));
memset(&_histTable, 0, sizeof(_histTable));
_dispContext.ContextRecord = &_msContext;
_dispContext.HistoryTable = &_histTable;
// Initialize MS context from ours.
R r(context);
_msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
#if defined(_LIBUNWIND_TARGET_X86_64)
_msContext.Rax = r.getRegister(UNW_X86_64_RAX);
_msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
_msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
_msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
_msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
_msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
_msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
_msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
_msContext.R8 = r.getRegister(UNW_X86_64_R8);
_msContext.R9 = r.getRegister(UNW_X86_64_R9);
_msContext.R10 = r.getRegister(UNW_X86_64_R10);
_msContext.R11 = r.getRegister(UNW_X86_64_R11);
_msContext.R12 = r.getRegister(UNW_X86_64_R12);
_msContext.R13 = r.getRegister(UNW_X86_64_R13);
_msContext.R14 = r.getRegister(UNW_X86_64_R14);
_msContext.R15 = r.getRegister(UNW_X86_64_R15);
_msContext.Rip = r.getRegister(UNW_REG_IP);
union {
v128 v;
M128A m;
} t;
t.v = r.getVectorRegister(UNW_X86_64_XMM0);
_msContext.Xmm0 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM1);
_msContext.Xmm1 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM2);
_msContext.Xmm2 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM3);
_msContext.Xmm3 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM4);
_msContext.Xmm4 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM5);
_msContext.Xmm5 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM6);
_msContext.Xmm6 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM7);
_msContext.Xmm7 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM8);
_msContext.Xmm8 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM9);
_msContext.Xmm9 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM10);
_msContext.Xmm10 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM11);
_msContext.Xmm11 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM12);
_msContext.Xmm12 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM13);
_msContext.Xmm13 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM14);
_msContext.Xmm14 = t.m;
t.v = r.getVectorRegister(UNW_X86_64_XMM15);
_msContext.Xmm15 = t.m;
#elif defined(_LIBUNWIND_TARGET_ARM)
_msContext.R0 = r.getRegister(UNW_ARM_R0);
_msContext.R1 = r.getRegister(UNW_ARM_R1);
_msContext.R2 = r.getRegister(UNW_ARM_R2);
_msContext.R3 = r.getRegister(UNW_ARM_R3);
_msContext.R4 = r.getRegister(UNW_ARM_R4);
_msContext.R5 = r.getRegister(UNW_ARM_R5);
_msContext.R6 = r.getRegister(UNW_ARM_R6);
_msContext.R7 = r.getRegister(UNW_ARM_R7);
_msContext.R8 = r.getRegister(UNW_ARM_R8);
_msContext.R9 = r.getRegister(UNW_ARM_R9);
_msContext.R10 = r.getRegister(UNW_ARM_R10);
_msContext.R11 = r.getRegister(UNW_ARM_R11);
_msContext.R12 = r.getRegister(UNW_ARM_R12);
_msContext.Sp = r.getRegister(UNW_ARM_SP);
_msContext.Lr = r.getRegister(UNW_ARM_LR);
_msContext.Pc = r.getRegister(UNW_ARM_IP);
for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
union {
uint64_t w;
double d;
} d;
d.d = r.getFloatRegister(i);
_msContext.D[i - UNW_ARM_D0] = d.w;
}
#elif defined(_LIBUNWIND_TARGET_AARCH64)
for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i)
_msContext.X[i - UNW_ARM64_X0] = r.getRegister(i);
_msContext.Sp = r.getRegister(UNW_REG_SP);
_msContext.Pc = r.getRegister(UNW_REG_IP);
for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i)
_msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i);
#endif
}
template <typename A, typename R>
UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
: _addressSpace(as), _unwindInfoMissing(false) {
static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
"UnwindCursor<> does not fit in unw_cursor_t");
memset(&_info, 0, sizeof(_info));
memset(&_histTable, 0, sizeof(_histTable));
_dispContext.ContextRecord = &_msContext;
_dispContext.HistoryTable = &_histTable;
_msContext = *context;
}
template <typename A, typename R>
bool UnwindCursor<A, R>::validReg(int regNum) {
if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
#if defined(_LIBUNWIND_TARGET_X86_64)
if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
#elif defined(_LIBUNWIND_TARGET_ARM)
if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true;
#elif defined(_LIBUNWIND_TARGET_AARCH64)
if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true;
#endif
return false;
}
template <typename A, typename R>
unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
switch (regNum) {
#if defined(_LIBUNWIND_TARGET_X86_64)
case UNW_REG_IP: return _msContext.Rip;
case UNW_X86_64_RAX: return _msContext.Rax;
case UNW_X86_64_RDX: return _msContext.Rdx;
case UNW_X86_64_RCX: return _msContext.Rcx;
case UNW_X86_64_RBX: return _msContext.Rbx;
case UNW_REG_SP:
case UNW_X86_64_RSP: return _msContext.Rsp;
case UNW_X86_64_RBP: return _msContext.Rbp;
case UNW_X86_64_RSI: return _msContext.Rsi;
case UNW_X86_64_RDI: return _msContext.Rdi;
case UNW_X86_64_R8: return _msContext.R8;
case UNW_X86_64_R9: return _msContext.R9;
case UNW_X86_64_R10: return _msContext.R10;
case UNW_X86_64_R11: return _msContext.R11;
case UNW_X86_64_R12: return _msContext.R12;
case UNW_X86_64_R13: return _msContext.R13;
case UNW_X86_64_R14: return _msContext.R14;
case UNW_X86_64_R15: return _msContext.R15;
#elif defined(_LIBUNWIND_TARGET_ARM)
case UNW_ARM_R0: return _msContext.R0;
case UNW_ARM_R1: return _msContext.R1;
case UNW_ARM_R2: return _msContext.R2;
case UNW_ARM_R3: return _msContext.R3;
case UNW_ARM_R4: return _msContext.R4;
case UNW_ARM_R5: return _msContext.R5;
case UNW_ARM_R6: return _msContext.R6;
case UNW_ARM_R7: return _msContext.R7;
case UNW_ARM_R8: return _msContext.R8;
case UNW_ARM_R9: return _msContext.R9;
case UNW_ARM_R10: return _msContext.R10;
case UNW_ARM_R11: return _msContext.R11;
case UNW_ARM_R12: return _msContext.R12;
case UNW_REG_SP:
case UNW_ARM_SP: return _msContext.Sp;
case UNW_ARM_LR: return _msContext.Lr;
case UNW_REG_IP:
case UNW_ARM_IP: return _msContext.Pc;
#elif defined(_LIBUNWIND_TARGET_AARCH64)
case UNW_REG_SP: return _msContext.Sp;
case UNW_REG_IP: return _msContext.Pc;
default: return _msContext.X[regNum - UNW_ARM64_X0];
#endif
}
_LIBUNWIND_ABORT("unsupported register");
}
template <typename A, typename R>
void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
switch (regNum) {
#if defined(_LIBUNWIND_TARGET_X86_64)
case UNW_REG_IP: _msContext.Rip = value; break;
case UNW_X86_64_RAX: _msContext.Rax = value; break;
case UNW_X86_64_RDX: _msContext.Rdx = value; break;
case UNW_X86_64_RCX: _msContext.Rcx = value; break;
case UNW_X86_64_RBX: _msContext.Rbx = value; break;
case UNW_REG_SP:
case UNW_X86_64_RSP: _msContext.Rsp = value; break;
case UNW_X86_64_RBP: _msContext.Rbp = value; break;
case UNW_X86_64_RSI: _msContext.Rsi = value; break;
case UNW_X86_64_RDI: _msContext.Rdi = value; break;
case UNW_X86_64_R8: _msContext.R8 = value; break;
case UNW_X86_64_R9: _msContext.R9 = value; break;
case UNW_X86_64_R10: _msContext.R10 = value; break;
case UNW_X86_64_R11: _msContext.R11 = value; break;
case UNW_X86_64_R12: _msContext.R12 = value; break;
case UNW_X86_64_R13: _msContext.R13 = value; break;
case UNW_X86_64_R14: _msContext.R14 = value; break;
case UNW_X86_64_R15: _msContext.R15 = value; break;
#elif defined(_LIBUNWIND_TARGET_ARM)
case UNW_ARM_R0: _msContext.R0 = value; break;
case UNW_ARM_R1: _msContext.R1 = value; break;
case UNW_ARM_R2: _msContext.R2 = value; break;
case UNW_ARM_R3: _msContext.R3 = value; break;
case UNW_ARM_R4: _msContext.R4 = value; break;
case UNW_ARM_R5: _msContext.R5 = value; break;
case UNW_ARM_R6: _msContext.R6 = value; break;
case UNW_ARM_R7: _msContext.R7 = value; break;
case UNW_ARM_R8: _msContext.R8 = value; break;
case UNW_ARM_R9: _msContext.R9 = value; break;
case UNW_ARM_R10: _msContext.R10 = value; break;
case UNW_ARM_R11: _msContext.R11 = value; break;
case UNW_ARM_R12: _msContext.R12 = value; break;
case UNW_REG_SP:
case UNW_ARM_SP: _msContext.Sp = value; break;
case UNW_ARM_LR: _msContext.Lr = value; break;
case UNW_REG_IP:
case UNW_ARM_IP: _msContext.Pc = value; break;
#elif defined(_LIBUNWIND_TARGET_AARCH64)
case UNW_REG_SP: _msContext.Sp = value; break;
case UNW_REG_IP: _msContext.Pc = value; break;
case UNW_ARM64_X0:
case UNW_ARM64_X1:
case UNW_ARM64_X2:
case UNW_ARM64_X3:
case UNW_ARM64_X4:
case UNW_ARM64_X5:
case UNW_ARM64_X6:
case UNW_ARM64_X7:
case UNW_ARM64_X8:
case UNW_ARM64_X9:
case UNW_ARM64_X10:
case UNW_ARM64_X11:
case UNW_ARM64_X12:
case UNW_ARM64_X13:
case UNW_ARM64_X14:
case UNW_ARM64_X15:
case UNW_ARM64_X16:
case UNW_ARM64_X17:
case UNW_ARM64_X18:
case UNW_ARM64_X19:
case UNW_ARM64_X20:
case UNW_ARM64_X21:
case UNW_ARM64_X22:
case UNW_ARM64_X23:
case UNW_ARM64_X24:
case UNW_ARM64_X25:
case UNW_ARM64_X26:
case UNW_ARM64_X27:
case UNW_ARM64_X28:
case UNW_ARM64_FP:
case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
#endif
default:
_LIBUNWIND_ABORT("unsupported register");
}
}
template <typename A, typename R>
bool UnwindCursor<A, R>::validFloatReg(int regNum) {
#if defined(_LIBUNWIND_TARGET_ARM)
if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
#elif defined(_LIBUNWIND_TARGET_AARCH64)
if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true;
#else
(void)regNum;
#endif
return false;
}
template <typename A, typename R>
unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
#if defined(_LIBUNWIND_TARGET_ARM)
if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
union {
uint32_t w;
float f;
} d;
d.w = _msContext.S[regNum - UNW_ARM_S0];
return d.f;
}
if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
union {
uint64_t w;
double d;
} d;
d.w = _msContext.D[regNum - UNW_ARM_D0];
return d.d;
}
_LIBUNWIND_ABORT("unsupported float register");
#elif defined(_LIBUNWIND_TARGET_AARCH64)
return _msContext.V[regNum - UNW_ARM64_D0].D[0];
#else
(void)regNum;
_LIBUNWIND_ABORT("float registers unimplemented");
#endif
}
template <typename A, typename R>
void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
#if defined(_LIBUNWIND_TARGET_ARM)
if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
union {
uint32_t w;
float f;
} d;
d.f = value;
_msContext.S[regNum - UNW_ARM_S0] = d.w;
}
if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
union {
uint64_t w;
double d;
} d;
d.d = value;
_msContext.D[regNum - UNW_ARM_D0] = d.w;
}
_LIBUNWIND_ABORT("unsupported float register");
#elif defined(_LIBUNWIND_TARGET_AARCH64)
_msContext.V[regNum - UNW_ARM64_D0].D[0] = value;
#else
(void)regNum;
(void)value;
_LIBUNWIND_ABORT("float registers unimplemented");
#endif
}
template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
RtlRestoreContext(&_msContext, nullptr);
}
#ifdef __arm__
template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
#endif
template <typename A, typename R>
const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
return R::getRegisterName(regNum);
}
template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
return false;
}
#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
/// UnwindCursor contains all state (including all register values) during
/// an unwind. This is normally stack allocated inside a unw_cursor_t.
template <typename A, typename R>
class UnwindCursor : public AbstractUnwindCursor{
typedef typename A::pint_t pint_t;
public:
UnwindCursor(unw_context_t *context, A &as);
UnwindCursor(A &as, void *threadArg);
virtual ~UnwindCursor() {}
virtual bool validReg(int);
virtual unw_word_t getReg(int);
virtual void setReg(int, unw_word_t);
virtual bool validFloatReg(int);
virtual unw_fpreg_t getFloatReg(int);
virtual void setFloatReg(int, unw_fpreg_t);
virtual int step();
virtual void getInfo(unw_proc_info_t *);
virtual void jumpto();
virtual bool isSignalFrame();
virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
virtual const char *getRegisterName(int num);
#ifdef __arm__
virtual void saveVFPAsX();
#endif
// libunwind does not and should not depend on C++ library which means that we
// need our own defition of inline placement new.
static void *operator new(size_t, UnwindCursor<A, R> *p) { return p; }
private:
#if defined(_LIBUNWIND_ARM_EHABI)
bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections §s);
int stepWithEHABI() {
size_t len = 0;
size_t off = 0;
// FIXME: Calling decode_eht_entry() here is violating the libunwind
// abstraction layer.
const uint32_t *ehtp =
decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
&off, &len);
if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
_URC_CONTINUE_UNWIND)
return UNW_STEP_END;
return UNW_STEP_SUCCESS;
}
#endif
#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
bool getInfoFromFdeCie(const typename CFI_Parser<A>::FDE_Info &fdeInfo,
const typename CFI_Parser<A>::CIE_Info &cieInfo,
pint_t pc, uintptr_t dso_base);
bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections §s,
uint32_t fdeSectionOffsetHint=0);
int stepWithDwarfFDE() {
return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
(pint_t)this->getReg(UNW_REG_IP),
(pint_t)_info.unwind_info,
_registers, _isSignalFrame);
}
#endif
#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
bool getInfoFromCompactEncodingSection(pint_t pc,
const UnwindInfoSections §s);
int stepWithCompactEncoding() {
#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
if ( compactSaysUseDwarf() )
return stepWithDwarfFDE();
#endif
R dummy;
return stepWithCompactEncoding(dummy);
}
#if defined(_LIBUNWIND_TARGET_X86_64)
int stepWithCompactEncoding(Registers_x86_64 &) {
return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
_info.format, _info.start_ip, _addressSpace, _registers);
}
#endif
#if defined(_LIBUNWIND_TARGET_I386)
int stepWithCompactEncoding(Registers_x86 &) {
return CompactUnwinder_x86<A>::stepWithCompactEncoding(
_info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
}
#endif
#if defined(_LIBUNWIND_TARGET_PPC)
int stepWithCompactEncoding(Registers_ppc &) {
return UNW_EINVAL;
}
#endif
#if defined(_LIBUNWIND_TARGET_PPC64)
int stepWithCompactEncoding(Registers_ppc64 &) {
return UNW_EINVAL;
}
#endif
#if defined(_LIBUNWIND_TARGET_AARCH64)
int stepWithCompactEncoding(Registers_arm64 &) {
return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
_info.format, _info.start_ip, _addressSpace, _registers);
}
#endif
#if defined(_LIBUNWIND_TARGET_MIPS_O32)
int stepWithCompactEncoding(Registers_mips_o32 &) {
return UNW_EINVAL;
}
#endif
#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
int stepWithCompactEncoding(Registers_mips_newabi &) {
return UNW_EINVAL;
}
#endif
#if defined(_LIBUNWIND_TARGET_SPARC)