blob: a13080691843befd6ba4cb4ec23be0580017891d [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- UnwindCursor.hpp ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//
Ed Maste6f723382016-08-30 13:08:21 +00009// C++ interface to lower levels of libunwind
Saleem Abdulrasool17552662015-04-24 19:39:17 +000010//===----------------------------------------------------------------------===//
11
12#ifndef __UNWINDCURSOR_HPP__
13#define __UNWINDCURSOR_HPP__
14
15#include <algorithm>
16#include <stdint.h>
17#include <stdio.h>
18#include <stdlib.h>
Saleem Abdulrasool17552662015-04-24 19:39:17 +000019#include <unwind.h>
20
Charles Davisfa2e6202018-08-30 21:29:00 +000021#ifdef _WIN32
22 #include <windows.h>
23 #include <ntverp.h>
24#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000025#ifdef __APPLE__
26 #include <mach-o/dyld.h>
27#endif
28
Charles Davisfa2e6202018-08-30 21:29:00 +000029#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
30// Provide a definition for the DISPATCHER_CONTEXT struct for old (Win7 and
31// earlier) SDKs.
32// MinGW-w64 has always provided this struct.
33 #if defined(_WIN32) && defined(_LIBUNWIND_TARGET_X86_64) && \
34 !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
35struct _DISPATCHER_CONTEXT {
36 ULONG64 ControlPc;
37 ULONG64 ImageBase;
38 PRUNTIME_FUNCTION FunctionEntry;
39 ULONG64 EstablisherFrame;
40 ULONG64 TargetIp;
41 PCONTEXT ContextRecord;
42 PEXCEPTION_ROUTINE LanguageHandler;
43 PVOID HandlerData;
44 PUNWIND_HISTORY_TABLE HistoryTable;
45 ULONG ScopeIndex;
46 ULONG Fill0;
47};
48 #endif
49
50struct UNWIND_INFO {
51 uint8_t Version : 3;
52 uint8_t Flags : 5;
53 uint8_t SizeOfProlog;
54 uint8_t CountOfCodes;
55 uint8_t FrameRegister : 4;
56 uint8_t FrameOffset : 4;
57 uint16_t UnwindCodes[2];
58};
59
60extern "C" _Unwind_Reason_Code __libunwind_seh_personality(
61 int, _Unwind_Action, uint64_t, _Unwind_Exception *,
62 struct _Unwind_Context *);
63
64#endif
65
Saleem Abdulrasool17552662015-04-24 19:39:17 +000066#include "config.h"
67
68#include "AddressSpace.hpp"
69#include "CompactUnwinder.hpp"
70#include "config.h"
71#include "DwarfInstructions.hpp"
72#include "EHHeaderParser.hpp"
73#include "libunwind.h"
74#include "Registers.hpp"
Martin Storsjo590ffef2017-10-23 19:29:36 +000075#include "RWMutex.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000076#include "Unwind-EHABI.h"
77
78namespace libunwind {
79
Ranjeet Singh421231a2017-03-31 15:28:06 +000080#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000081/// Cache of recently found FDEs.
82template <typename A>
83class _LIBUNWIND_HIDDEN DwarfFDECache {
84 typedef typename A::pint_t pint_t;
85public:
86 static pint_t findFDE(pint_t mh, pint_t pc);
87 static void add(pint_t mh, pint_t ip_start, pint_t ip_end, pint_t fde);
88 static void removeAllIn(pint_t mh);
89 static void iterateCacheEntries(void (*func)(unw_word_t ip_start,
90 unw_word_t ip_end,
91 unw_word_t fde, unw_word_t mh));
92
93private:
94
95 struct entry {
96 pint_t mh;
97 pint_t ip_start;
98 pint_t ip_end;
99 pint_t fde;
100 };
101
102 // These fields are all static to avoid needing an initializer.
103 // There is only one instance of this class per process.
Martin Storsjo590ffef2017-10-23 19:29:36 +0000104 static RWMutex _lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000105#ifdef __APPLE__
106 static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
107 static bool _registeredForDyldUnloads;
108#endif
109 // Can't use std::vector<> here because this code is below libc++.
110 static entry *_buffer;
111 static entry *_bufferUsed;
112 static entry *_bufferEnd;
113 static entry _initialBuffer[64];
114};
115
116template <typename A>
117typename DwarfFDECache<A>::entry *
118DwarfFDECache<A>::_buffer = _initialBuffer;
119
120template <typename A>
121typename DwarfFDECache<A>::entry *
122DwarfFDECache<A>::_bufferUsed = _initialBuffer;
123
124template <typename A>
125typename DwarfFDECache<A>::entry *
126DwarfFDECache<A>::_bufferEnd = &_initialBuffer[64];
127
128template <typename A>
129typename DwarfFDECache<A>::entry DwarfFDECache<A>::_initialBuffer[64];
130
131template <typename A>
Martin Storsjo590ffef2017-10-23 19:29:36 +0000132RWMutex DwarfFDECache<A>::_lock;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000133
134#ifdef __APPLE__
135template <typename A>
136bool DwarfFDECache<A>::_registeredForDyldUnloads = false;
137#endif
138
139template <typename A>
140typename A::pint_t DwarfFDECache<A>::findFDE(pint_t mh, pint_t pc) {
141 pint_t result = 0;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000142 _LIBUNWIND_LOG_IF_FALSE(_lock.lock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000143 for (entry *p = _buffer; p < _bufferUsed; ++p) {
144 if ((mh == p->mh) || (mh == 0)) {
145 if ((p->ip_start <= pc) && (pc < p->ip_end)) {
146 result = p->fde;
147 break;
148 }
149 }
150 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000151 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock_shared());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000152 return result;
153}
154
155template <typename A>
156void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
157 pint_t fde) {
Peter Zotov0717a2e2015-11-09 06:57:29 +0000158#if !defined(_LIBUNWIND_NO_HEAP)
Martin Storsjo590ffef2017-10-23 19:29:36 +0000159 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000160 if (_bufferUsed >= _bufferEnd) {
161 size_t oldSize = (size_t)(_bufferEnd - _buffer);
162 size_t newSize = oldSize * 4;
163 // Can't use operator new (we are below it).
164 entry *newBuffer = (entry *)malloc(newSize * sizeof(entry));
165 memcpy(newBuffer, _buffer, oldSize * sizeof(entry));
166 if (_buffer != _initialBuffer)
167 free(_buffer);
168 _buffer = newBuffer;
169 _bufferUsed = &newBuffer[oldSize];
170 _bufferEnd = &newBuffer[newSize];
171 }
172 _bufferUsed->mh = mh;
173 _bufferUsed->ip_start = ip_start;
174 _bufferUsed->ip_end = ip_end;
175 _bufferUsed->fde = fde;
176 ++_bufferUsed;
177#ifdef __APPLE__
178 if (!_registeredForDyldUnloads) {
179 _dyld_register_func_for_remove_image(&dyldUnloadHook);
180 _registeredForDyldUnloads = true;
181 }
182#endif
Martin Storsjo590ffef2017-10-23 19:29:36 +0000183 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Peter Zotov0717a2e2015-11-09 06:57:29 +0000184#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000185}
186
187template <typename A>
188void DwarfFDECache<A>::removeAllIn(pint_t mh) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000189 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000190 entry *d = _buffer;
191 for (const entry *s = _buffer; s < _bufferUsed; ++s) {
192 if (s->mh != mh) {
193 if (d != s)
194 *d = *s;
195 ++d;
196 }
197 }
198 _bufferUsed = d;
Martin Storsjo590ffef2017-10-23 19:29:36 +0000199 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000200}
201
202#ifdef __APPLE__
203template <typename A>
204void DwarfFDECache<A>::dyldUnloadHook(const struct mach_header *mh, intptr_t ) {
205 removeAllIn((pint_t) mh);
206}
207#endif
208
209template <typename A>
210void DwarfFDECache<A>::iterateCacheEntries(void (*func)(
211 unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
Martin Storsjo590ffef2017-10-23 19:29:36 +0000212 _LIBUNWIND_LOG_IF_FALSE(_lock.lock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000213 for (entry *p = _buffer; p < _bufferUsed; ++p) {
214 (*func)(p->ip_start, p->ip_end, p->fde, p->mh);
215 }
Martin Storsjo590ffef2017-10-23 19:29:36 +0000216 _LIBUNWIND_LOG_IF_FALSE(_lock.unlock());
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000217}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000218#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000219
220
221#define arrayoffsetof(type, index, field) ((size_t)(&((type *)0)[index].field))
222
Ranjeet Singh421231a2017-03-31 15:28:06 +0000223#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000224template <typename A> class UnwindSectionHeader {
225public:
226 UnwindSectionHeader(A &addressSpace, typename A::pint_t addr)
227 : _addressSpace(addressSpace), _addr(addr) {}
228
229 uint32_t version() const {
230 return _addressSpace.get32(_addr +
231 offsetof(unwind_info_section_header, version));
232 }
233 uint32_t commonEncodingsArraySectionOffset() const {
234 return _addressSpace.get32(_addr +
235 offsetof(unwind_info_section_header,
236 commonEncodingsArraySectionOffset));
237 }
238 uint32_t commonEncodingsArrayCount() const {
239 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
240 commonEncodingsArrayCount));
241 }
242 uint32_t personalityArraySectionOffset() const {
243 return _addressSpace.get32(_addr + offsetof(unwind_info_section_header,
244 personalityArraySectionOffset));
245 }
246 uint32_t personalityArrayCount() const {
247 return _addressSpace.get32(
248 _addr + offsetof(unwind_info_section_header, personalityArrayCount));
249 }
250 uint32_t indexSectionOffset() const {
251 return _addressSpace.get32(
252 _addr + offsetof(unwind_info_section_header, indexSectionOffset));
253 }
254 uint32_t indexCount() const {
255 return _addressSpace.get32(
256 _addr + offsetof(unwind_info_section_header, indexCount));
257 }
258
259private:
260 A &_addressSpace;
261 typename A::pint_t _addr;
262};
263
264template <typename A> class UnwindSectionIndexArray {
265public:
266 UnwindSectionIndexArray(A &addressSpace, typename A::pint_t addr)
267 : _addressSpace(addressSpace), _addr(addr) {}
268
269 uint32_t functionOffset(uint32_t index) const {
270 return _addressSpace.get32(
271 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
272 functionOffset));
273 }
274 uint32_t secondLevelPagesSectionOffset(uint32_t index) const {
275 return _addressSpace.get32(
276 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
277 secondLevelPagesSectionOffset));
278 }
279 uint32_t lsdaIndexArraySectionOffset(uint32_t index) const {
280 return _addressSpace.get32(
281 _addr + arrayoffsetof(unwind_info_section_header_index_entry, index,
282 lsdaIndexArraySectionOffset));
283 }
284
285private:
286 A &_addressSpace;
287 typename A::pint_t _addr;
288};
289
290template <typename A> class UnwindSectionRegularPageHeader {
291public:
292 UnwindSectionRegularPageHeader(A &addressSpace, typename A::pint_t addr)
293 : _addressSpace(addressSpace), _addr(addr) {}
294
295 uint32_t kind() const {
296 return _addressSpace.get32(
297 _addr + offsetof(unwind_info_regular_second_level_page_header, kind));
298 }
299 uint16_t entryPageOffset() const {
300 return _addressSpace.get16(
301 _addr + offsetof(unwind_info_regular_second_level_page_header,
302 entryPageOffset));
303 }
304 uint16_t entryCount() const {
305 return _addressSpace.get16(
306 _addr +
307 offsetof(unwind_info_regular_second_level_page_header, entryCount));
308 }
309
310private:
311 A &_addressSpace;
312 typename A::pint_t _addr;
313};
314
315template <typename A> class UnwindSectionRegularArray {
316public:
317 UnwindSectionRegularArray(A &addressSpace, typename A::pint_t addr)
318 : _addressSpace(addressSpace), _addr(addr) {}
319
320 uint32_t functionOffset(uint32_t index) const {
321 return _addressSpace.get32(
322 _addr + arrayoffsetof(unwind_info_regular_second_level_entry, index,
323 functionOffset));
324 }
325 uint32_t encoding(uint32_t index) const {
326 return _addressSpace.get32(
327 _addr +
328 arrayoffsetof(unwind_info_regular_second_level_entry, index, encoding));
329 }
330
331private:
332 A &_addressSpace;
333 typename A::pint_t _addr;
334};
335
336template <typename A> class UnwindSectionCompressedPageHeader {
337public:
338 UnwindSectionCompressedPageHeader(A &addressSpace, typename A::pint_t addr)
339 : _addressSpace(addressSpace), _addr(addr) {}
340
341 uint32_t kind() const {
342 return _addressSpace.get32(
343 _addr +
344 offsetof(unwind_info_compressed_second_level_page_header, kind));
345 }
346 uint16_t entryPageOffset() const {
347 return _addressSpace.get16(
348 _addr + offsetof(unwind_info_compressed_second_level_page_header,
349 entryPageOffset));
350 }
351 uint16_t entryCount() const {
352 return _addressSpace.get16(
353 _addr +
354 offsetof(unwind_info_compressed_second_level_page_header, entryCount));
355 }
356 uint16_t encodingsPageOffset() const {
357 return _addressSpace.get16(
358 _addr + offsetof(unwind_info_compressed_second_level_page_header,
359 encodingsPageOffset));
360 }
361 uint16_t encodingsCount() const {
362 return _addressSpace.get16(
363 _addr + offsetof(unwind_info_compressed_second_level_page_header,
364 encodingsCount));
365 }
366
367private:
368 A &_addressSpace;
369 typename A::pint_t _addr;
370};
371
372template <typename A> class UnwindSectionCompressedArray {
373public:
374 UnwindSectionCompressedArray(A &addressSpace, typename A::pint_t addr)
375 : _addressSpace(addressSpace), _addr(addr) {}
376
377 uint32_t functionOffset(uint32_t index) const {
378 return UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(
379 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
380 }
381 uint16_t encodingIndex(uint32_t index) const {
382 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(
383 _addressSpace.get32(_addr + index * sizeof(uint32_t)));
384 }
385
386private:
387 A &_addressSpace;
388 typename A::pint_t _addr;
389};
390
391template <typename A> class UnwindSectionLsdaArray {
392public:
393 UnwindSectionLsdaArray(A &addressSpace, typename A::pint_t addr)
394 : _addressSpace(addressSpace), _addr(addr) {}
395
396 uint32_t functionOffset(uint32_t index) const {
397 return _addressSpace.get32(
398 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
399 index, functionOffset));
400 }
401 uint32_t lsdaOffset(uint32_t index) const {
402 return _addressSpace.get32(
403 _addr + arrayoffsetof(unwind_info_section_header_lsda_index_entry,
404 index, lsdaOffset));
405 }
406
407private:
408 A &_addressSpace;
409 typename A::pint_t _addr;
410};
Ranjeet Singh421231a2017-03-31 15:28:06 +0000411#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000412
413class _LIBUNWIND_HIDDEN AbstractUnwindCursor {
414public:
415 // NOTE: provide a class specific placement deallocation function (S5.3.4 p20)
416 // This avoids an unnecessary dependency to libc++abi.
417 void operator delete(void *, size_t) {}
418
419 virtual ~AbstractUnwindCursor() {}
420 virtual bool validReg(int) { _LIBUNWIND_ABORT("validReg not implemented"); }
421 virtual unw_word_t getReg(int) { _LIBUNWIND_ABORT("getReg not implemented"); }
422 virtual void setReg(int, unw_word_t) {
423 _LIBUNWIND_ABORT("setReg not implemented");
424 }
425 virtual bool validFloatReg(int) {
426 _LIBUNWIND_ABORT("validFloatReg not implemented");
427 }
428 virtual unw_fpreg_t getFloatReg(int) {
429 _LIBUNWIND_ABORT("getFloatReg not implemented");
430 }
431 virtual void setFloatReg(int, unw_fpreg_t) {
432 _LIBUNWIND_ABORT("setFloatReg not implemented");
433 }
434 virtual int step() { _LIBUNWIND_ABORT("step not implemented"); }
435 virtual void getInfo(unw_proc_info_t *) {
436 _LIBUNWIND_ABORT("getInfo not implemented");
437 }
438 virtual void jumpto() { _LIBUNWIND_ABORT("jumpto not implemented"); }
439 virtual bool isSignalFrame() {
440 _LIBUNWIND_ABORT("isSignalFrame not implemented");
441 }
442 virtual bool getFunctionName(char *, size_t, unw_word_t *) {
443 _LIBUNWIND_ABORT("getFunctionName not implemented");
444 }
445 virtual void setInfoBasedOnIPRegister(bool = false) {
446 _LIBUNWIND_ABORT("setInfoBasedOnIPRegister not implemented");
447 }
448 virtual const char *getRegisterName(int) {
449 _LIBUNWIND_ABORT("getRegisterName not implemented");
450 }
451#ifdef __arm__
452 virtual void saveVFPAsX() { _LIBUNWIND_ABORT("saveVFPAsX not implemented"); }
453#endif
454};
455
Charles Davisfa2e6202018-08-30 21:29:00 +0000456#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)
457
458/// \c UnwindCursor contains all state (including all register values) during
459/// an unwind. This is normally stack-allocated inside a unw_cursor_t.
460template <typename A, typename R>
461class UnwindCursor : public AbstractUnwindCursor {
462 typedef typename A::pint_t pint_t;
463public:
464 UnwindCursor(unw_context_t *context, A &as);
465 UnwindCursor(CONTEXT *context, A &as);
466 UnwindCursor(A &as, void *threadArg);
467 virtual ~UnwindCursor() {}
468 virtual bool validReg(int);
469 virtual unw_word_t getReg(int);
470 virtual void setReg(int, unw_word_t);
471 virtual bool validFloatReg(int);
472 virtual unw_fpreg_t getFloatReg(int);
473 virtual void setFloatReg(int, unw_fpreg_t);
474 virtual int step();
475 virtual void getInfo(unw_proc_info_t *);
476 virtual void jumpto();
477 virtual bool isSignalFrame();
478 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
479 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
480 virtual const char *getRegisterName(int num);
481#ifdef __arm__
482 virtual void saveVFPAsX();
483#endif
484
485 DISPATCHER_CONTEXT *getDispatcherContext() { return &_dispContext; }
486 void setDispatcherContext(DISPATCHER_CONTEXT *disp) { _dispContext = *disp; }
487
488private:
489
490 pint_t getLastPC() const { return _dispContext.ControlPc; }
491 void setLastPC(pint_t pc) { _dispContext.ControlPc = pc; }
492 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
493 _dispContext.FunctionEntry = RtlLookupFunctionEntry(pc,
494 &_dispContext.ImageBase,
495 _dispContext.HistoryTable);
496 *base = _dispContext.ImageBase;
497 return _dispContext.FunctionEntry;
498 }
499 bool getInfoFromSEH(pint_t pc);
500 int stepWithSEHData() {
501 _dispContext.LanguageHandler = RtlVirtualUnwind(UNW_FLAG_UHANDLER,
502 _dispContext.ImageBase,
503 _dispContext.ControlPc,
504 _dispContext.FunctionEntry,
505 _dispContext.ContextRecord,
506 &_dispContext.HandlerData,
507 &_dispContext.EstablisherFrame,
508 NULL);
509 // Update some fields of the unwind info now, since we have them.
510 _info.lsda = reinterpret_cast<unw_word_t>(_dispContext.HandlerData);
511 if (_dispContext.LanguageHandler) {
512 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
513 } else
514 _info.handler = 0;
515 return UNW_STEP_SUCCESS;
516 }
517
518 A &_addressSpace;
519 unw_proc_info_t _info;
520 DISPATCHER_CONTEXT _dispContext;
521 CONTEXT _msContext;
522 UNWIND_HISTORY_TABLE _histTable;
523 bool _unwindInfoMissing;
524};
525
526
527template <typename A, typename R>
528UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
529 : _addressSpace(as), _unwindInfoMissing(false) {
530 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
531 "UnwindCursor<> does not fit in unw_cursor_t");
532 memset(&_info, 0, sizeof(_info));
533 memset(&_histTable, 0, sizeof(_histTable));
534 _dispContext.ContextRecord = &_msContext;
535 _dispContext.HistoryTable = &_histTable;
536 // Initialize MS context from ours.
537 R r(context);
538 _msContext.ContextFlags = CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
539#if defined(_LIBUNWIND_TARGET_X86_64)
540 _msContext.Rax = r.getRegister(UNW_X86_64_RAX);
541 _msContext.Rcx = r.getRegister(UNW_X86_64_RCX);
542 _msContext.Rdx = r.getRegister(UNW_X86_64_RDX);
543 _msContext.Rbx = r.getRegister(UNW_X86_64_RBX);
544 _msContext.Rsp = r.getRegister(UNW_X86_64_RSP);
545 _msContext.Rbp = r.getRegister(UNW_X86_64_RBP);
546 _msContext.Rsi = r.getRegister(UNW_X86_64_RSI);
547 _msContext.Rdi = r.getRegister(UNW_X86_64_RDI);
548 _msContext.R8 = r.getRegister(UNW_X86_64_R8);
549 _msContext.R9 = r.getRegister(UNW_X86_64_R9);
550 _msContext.R10 = r.getRegister(UNW_X86_64_R10);
551 _msContext.R11 = r.getRegister(UNW_X86_64_R11);
552 _msContext.R12 = r.getRegister(UNW_X86_64_R12);
553 _msContext.R13 = r.getRegister(UNW_X86_64_R13);
554 _msContext.R14 = r.getRegister(UNW_X86_64_R14);
555 _msContext.R15 = r.getRegister(UNW_X86_64_R15);
556 _msContext.Rip = r.getRegister(UNW_REG_IP);
557 union {
558 v128 v;
559 M128A m;
560 } t;
561 t.v = r.getVectorRegister(UNW_X86_64_XMM0);
562 _msContext.Xmm0 = t.m;
563 t.v = r.getVectorRegister(UNW_X86_64_XMM1);
564 _msContext.Xmm1 = t.m;
565 t.v = r.getVectorRegister(UNW_X86_64_XMM2);
566 _msContext.Xmm2 = t.m;
567 t.v = r.getVectorRegister(UNW_X86_64_XMM3);
568 _msContext.Xmm3 = t.m;
569 t.v = r.getVectorRegister(UNW_X86_64_XMM4);
570 _msContext.Xmm4 = t.m;
571 t.v = r.getVectorRegister(UNW_X86_64_XMM5);
572 _msContext.Xmm5 = t.m;
573 t.v = r.getVectorRegister(UNW_X86_64_XMM6);
574 _msContext.Xmm6 = t.m;
575 t.v = r.getVectorRegister(UNW_X86_64_XMM7);
576 _msContext.Xmm7 = t.m;
577 t.v = r.getVectorRegister(UNW_X86_64_XMM8);
578 _msContext.Xmm8 = t.m;
579 t.v = r.getVectorRegister(UNW_X86_64_XMM9);
580 _msContext.Xmm9 = t.m;
581 t.v = r.getVectorRegister(UNW_X86_64_XMM10);
582 _msContext.Xmm10 = t.m;
583 t.v = r.getVectorRegister(UNW_X86_64_XMM11);
584 _msContext.Xmm11 = t.m;
585 t.v = r.getVectorRegister(UNW_X86_64_XMM12);
586 _msContext.Xmm12 = t.m;
587 t.v = r.getVectorRegister(UNW_X86_64_XMM13);
588 _msContext.Xmm13 = t.m;
589 t.v = r.getVectorRegister(UNW_X86_64_XMM14);
590 _msContext.Xmm14 = t.m;
591 t.v = r.getVectorRegister(UNW_X86_64_XMM15);
592 _msContext.Xmm15 = t.m;
593#elif defined(_LIBUNWIND_TARGET_ARM)
594 _msContext.R0 = r.getRegister(UNW_ARM_R0);
595 _msContext.R1 = r.getRegister(UNW_ARM_R1);
596 _msContext.R2 = r.getRegister(UNW_ARM_R2);
597 _msContext.R3 = r.getRegister(UNW_ARM_R3);
598 _msContext.R4 = r.getRegister(UNW_ARM_R4);
599 _msContext.R5 = r.getRegister(UNW_ARM_R5);
600 _msContext.R6 = r.getRegister(UNW_ARM_R6);
601 _msContext.R7 = r.getRegister(UNW_ARM_R7);
602 _msContext.R8 = r.getRegister(UNW_ARM_R8);
603 _msContext.R9 = r.getRegister(UNW_ARM_R9);
604 _msContext.R10 = r.getRegister(UNW_ARM_R10);
605 _msContext.R11 = r.getRegister(UNW_ARM_R11);
606 _msContext.R12 = r.getRegister(UNW_ARM_R12);
607 _msContext.Sp = r.getRegister(UNW_ARM_SP);
608 _msContext.Lr = r.getRegister(UNW_ARM_LR);
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000609 _msContext.Pc = r.getRegister(UNW_ARM_IP);
610 for (int i = UNW_ARM_D0; i <= UNW_ARM_D31; ++i) {
Charles Davisfa2e6202018-08-30 21:29:00 +0000611 union {
612 uint64_t w;
613 double d;
614 } d;
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000615 d.d = r.getFloatRegister(i);
616 _msContext.D[i - UNW_ARM_D0] = d.w;
Charles Davisfa2e6202018-08-30 21:29:00 +0000617 }
Martin Storsjoce150112018-12-18 20:05:59 +0000618#elif defined(_LIBUNWIND_TARGET_AARCH64)
619 for (int i = UNW_ARM64_X0; i <= UNW_ARM64_X30; ++i)
620 _msContext.X[i - UNW_ARM64_X0] = r.getRegister(i);
621 _msContext.Sp = r.getRegister(UNW_REG_SP);
622 _msContext.Pc = r.getRegister(UNW_REG_IP);
623 for (int i = UNW_ARM64_D0; i <= UNW_ARM64_D31; ++i)
624 _msContext.V[i - UNW_ARM64_D0].D[0] = r.getFloatRegister(i);
Charles Davisfa2e6202018-08-30 21:29:00 +0000625#endif
626}
627
628template <typename A, typename R>
629UnwindCursor<A, R>::UnwindCursor(CONTEXT *context, A &as)
630 : _addressSpace(as), _unwindInfoMissing(false) {
631 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
632 "UnwindCursor<> does not fit in unw_cursor_t");
633 memset(&_info, 0, sizeof(_info));
634 memset(&_histTable, 0, sizeof(_histTable));
635 _dispContext.ContextRecord = &_msContext;
636 _dispContext.HistoryTable = &_histTable;
637 _msContext = *context;
638}
639
640
641template <typename A, typename R>
642bool UnwindCursor<A, R>::validReg(int regNum) {
643 if (regNum == UNW_REG_IP || regNum == UNW_REG_SP) return true;
644#if defined(_LIBUNWIND_TARGET_X86_64)
645 if (regNum >= UNW_X86_64_RAX && regNum <= UNW_X86_64_R15) return true;
646#elif defined(_LIBUNWIND_TARGET_ARM)
647 if (regNum >= UNW_ARM_R0 && regNum <= UNW_ARM_R15) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000648#elif defined(_LIBUNWIND_TARGET_AARCH64)
649 if (regNum >= UNW_ARM64_X0 && regNum <= UNW_ARM64_X30) return true;
Charles Davisfa2e6202018-08-30 21:29:00 +0000650#endif
651 return false;
652}
653
654template <typename A, typename R>
655unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
656 switch (regNum) {
657#if defined(_LIBUNWIND_TARGET_X86_64)
658 case UNW_REG_IP: return _msContext.Rip;
659 case UNW_X86_64_RAX: return _msContext.Rax;
660 case UNW_X86_64_RDX: return _msContext.Rdx;
661 case UNW_X86_64_RCX: return _msContext.Rcx;
662 case UNW_X86_64_RBX: return _msContext.Rbx;
663 case UNW_REG_SP:
664 case UNW_X86_64_RSP: return _msContext.Rsp;
665 case UNW_X86_64_RBP: return _msContext.Rbp;
666 case UNW_X86_64_RSI: return _msContext.Rsi;
667 case UNW_X86_64_RDI: return _msContext.Rdi;
668 case UNW_X86_64_R8: return _msContext.R8;
669 case UNW_X86_64_R9: return _msContext.R9;
670 case UNW_X86_64_R10: return _msContext.R10;
671 case UNW_X86_64_R11: return _msContext.R11;
672 case UNW_X86_64_R12: return _msContext.R12;
673 case UNW_X86_64_R13: return _msContext.R13;
674 case UNW_X86_64_R14: return _msContext.R14;
675 case UNW_X86_64_R15: return _msContext.R15;
676#elif defined(_LIBUNWIND_TARGET_ARM)
677 case UNW_ARM_R0: return _msContext.R0;
678 case UNW_ARM_R1: return _msContext.R1;
679 case UNW_ARM_R2: return _msContext.R2;
680 case UNW_ARM_R3: return _msContext.R3;
681 case UNW_ARM_R4: return _msContext.R4;
682 case UNW_ARM_R5: return _msContext.R5;
683 case UNW_ARM_R6: return _msContext.R6;
684 case UNW_ARM_R7: return _msContext.R7;
685 case UNW_ARM_R8: return _msContext.R8;
686 case UNW_ARM_R9: return _msContext.R9;
687 case UNW_ARM_R10: return _msContext.R10;
688 case UNW_ARM_R11: return _msContext.R11;
689 case UNW_ARM_R12: return _msContext.R12;
690 case UNW_REG_SP:
691 case UNW_ARM_SP: return _msContext.Sp;
692 case UNW_ARM_LR: return _msContext.Lr;
693 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000694 case UNW_ARM_IP: return _msContext.Pc;
Martin Storsjoce150112018-12-18 20:05:59 +0000695#elif defined(_LIBUNWIND_TARGET_AARCH64)
696 case UNW_REG_SP: return _msContext.Sp;
697 case UNW_REG_IP: return _msContext.Pc;
698 default: return _msContext.X[regNum - UNW_ARM64_X0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000699#endif
700 }
701 _LIBUNWIND_ABORT("unsupported register");
702}
703
704template <typename A, typename R>
705void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
706 switch (regNum) {
707#if defined(_LIBUNWIND_TARGET_X86_64)
708 case UNW_REG_IP: _msContext.Rip = value; break;
709 case UNW_X86_64_RAX: _msContext.Rax = value; break;
710 case UNW_X86_64_RDX: _msContext.Rdx = value; break;
711 case UNW_X86_64_RCX: _msContext.Rcx = value; break;
712 case UNW_X86_64_RBX: _msContext.Rbx = value; break;
713 case UNW_REG_SP:
714 case UNW_X86_64_RSP: _msContext.Rsp = value; break;
715 case UNW_X86_64_RBP: _msContext.Rbp = value; break;
716 case UNW_X86_64_RSI: _msContext.Rsi = value; break;
717 case UNW_X86_64_RDI: _msContext.Rdi = value; break;
718 case UNW_X86_64_R8: _msContext.R8 = value; break;
719 case UNW_X86_64_R9: _msContext.R9 = value; break;
720 case UNW_X86_64_R10: _msContext.R10 = value; break;
721 case UNW_X86_64_R11: _msContext.R11 = value; break;
722 case UNW_X86_64_R12: _msContext.R12 = value; break;
723 case UNW_X86_64_R13: _msContext.R13 = value; break;
724 case UNW_X86_64_R14: _msContext.R14 = value; break;
725 case UNW_X86_64_R15: _msContext.R15 = value; break;
726#elif defined(_LIBUNWIND_TARGET_ARM)
727 case UNW_ARM_R0: _msContext.R0 = value; break;
728 case UNW_ARM_R1: _msContext.R1 = value; break;
729 case UNW_ARM_R2: _msContext.R2 = value; break;
730 case UNW_ARM_R3: _msContext.R3 = value; break;
731 case UNW_ARM_R4: _msContext.R4 = value; break;
732 case UNW_ARM_R5: _msContext.R5 = value; break;
733 case UNW_ARM_R6: _msContext.R6 = value; break;
734 case UNW_ARM_R7: _msContext.R7 = value; break;
735 case UNW_ARM_R8: _msContext.R8 = value; break;
736 case UNW_ARM_R9: _msContext.R9 = value; break;
737 case UNW_ARM_R10: _msContext.R10 = value; break;
738 case UNW_ARM_R11: _msContext.R11 = value; break;
739 case UNW_ARM_R12: _msContext.R12 = value; break;
740 case UNW_REG_SP:
741 case UNW_ARM_SP: _msContext.Sp = value; break;
742 case UNW_ARM_LR: _msContext.Lr = value; break;
743 case UNW_REG_IP:
Martin Storsjoe5dbce22018-08-31 14:56:55 +0000744 case UNW_ARM_IP: _msContext.Pc = value; break;
Martin Storsjoce150112018-12-18 20:05:59 +0000745#elif defined(_LIBUNWIND_TARGET_AARCH64)
746 case UNW_REG_SP: _msContext.Sp = value; break;
747 case UNW_REG_IP: _msContext.Pc = value; break;
748 case UNW_ARM64_X0:
749 case UNW_ARM64_X1:
750 case UNW_ARM64_X2:
751 case UNW_ARM64_X3:
752 case UNW_ARM64_X4:
753 case UNW_ARM64_X5:
754 case UNW_ARM64_X6:
755 case UNW_ARM64_X7:
756 case UNW_ARM64_X8:
757 case UNW_ARM64_X9:
758 case UNW_ARM64_X10:
759 case UNW_ARM64_X11:
760 case UNW_ARM64_X12:
761 case UNW_ARM64_X13:
762 case UNW_ARM64_X14:
763 case UNW_ARM64_X15:
764 case UNW_ARM64_X16:
765 case UNW_ARM64_X17:
766 case UNW_ARM64_X18:
767 case UNW_ARM64_X19:
768 case UNW_ARM64_X20:
769 case UNW_ARM64_X21:
770 case UNW_ARM64_X22:
771 case UNW_ARM64_X23:
772 case UNW_ARM64_X24:
773 case UNW_ARM64_X25:
774 case UNW_ARM64_X26:
775 case UNW_ARM64_X27:
776 case UNW_ARM64_X28:
777 case UNW_ARM64_FP:
778 case UNW_ARM64_LR: _msContext.X[regNum - UNW_ARM64_X0] = value; break;
Charles Davisfa2e6202018-08-30 21:29:00 +0000779#endif
780 default:
781 _LIBUNWIND_ABORT("unsupported register");
782 }
783}
784
785template <typename A, typename R>
786bool UnwindCursor<A, R>::validFloatReg(int regNum) {
787#if defined(_LIBUNWIND_TARGET_ARM)
788 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) return true;
789 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) return true;
Martin Storsjoce150112018-12-18 20:05:59 +0000790#elif defined(_LIBUNWIND_TARGET_AARCH64)
791 if (regNum >= UNW_ARM64_D0 && regNum <= UNW_ARM64_D31) return true;
Charles Davisfa2e6202018-08-30 21:29:00 +0000792#endif
793 return false;
794}
795
796template <typename A, typename R>
797unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
798#if defined(_LIBUNWIND_TARGET_ARM)
799 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
800 union {
801 uint32_t w;
802 float f;
803 } d;
804 d.w = _msContext.S[regNum - UNW_ARM_S0];
805 return d.f;
806 }
807 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
808 union {
809 uint64_t w;
810 double d;
811 } d;
812 d.w = _msContext.D[regNum - UNW_ARM_D0];
813 return d.d;
814 }
815 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000816#elif defined(_LIBUNWIND_TARGET_AARCH64)
817 return _msContext.V[regNum - UNW_ARM64_D0].D[0];
Charles Davisfa2e6202018-08-30 21:29:00 +0000818#else
819 _LIBUNWIND_ABORT("float registers unimplemented");
820#endif
821}
822
823template <typename A, typename R>
824void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
825#if defined(_LIBUNWIND_TARGET_ARM)
826 if (regNum >= UNW_ARM_S0 && regNum <= UNW_ARM_S31) {
827 union {
828 uint32_t w;
829 float f;
830 } d;
831 d.f = value;
832 _msContext.S[regNum - UNW_ARM_S0] = d.w;
833 }
834 if (regNum >= UNW_ARM_D0 && regNum <= UNW_ARM_D31) {
835 union {
836 uint64_t w;
837 double d;
838 } d;
839 d.d = value;
840 _msContext.D[regNum - UNW_ARM_D0] = d.w;
841 }
842 _LIBUNWIND_ABORT("unsupported float register");
Martin Storsjoce150112018-12-18 20:05:59 +0000843#elif defined(_LIBUNWIND_TARGET_AARCH64)
844 _msContext.V[regNum - UNW_ARM64_D0].D[0] = value;
Charles Davisfa2e6202018-08-30 21:29:00 +0000845#else
846 _LIBUNWIND_ABORT("float registers unimplemented");
847#endif
848}
849
850template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
851 RtlRestoreContext(&_msContext, nullptr);
852}
853
854#ifdef __arm__
855template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {}
856#endif
857
858template <typename A, typename R>
859const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
Martin Storsjo43bb9f82018-12-12 22:24:42 +0000860 return R::getRegisterName(regNum);
Charles Davisfa2e6202018-08-30 21:29:00 +0000861}
862
863template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
864 return false;
865}
866
867#else // !defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) || !defined(_WIN32)
868
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000869/// UnwindCursor contains all state (including all register values) during
870/// an unwind. This is normally stack allocated inside a unw_cursor_t.
871template <typename A, typename R>
872class UnwindCursor : public AbstractUnwindCursor{
873 typedef typename A::pint_t pint_t;
874public:
875 UnwindCursor(unw_context_t *context, A &as);
876 UnwindCursor(A &as, void *threadArg);
877 virtual ~UnwindCursor() {}
878 virtual bool validReg(int);
879 virtual unw_word_t getReg(int);
880 virtual void setReg(int, unw_word_t);
881 virtual bool validFloatReg(int);
882 virtual unw_fpreg_t getFloatReg(int);
883 virtual void setFloatReg(int, unw_fpreg_t);
884 virtual int step();
885 virtual void getInfo(unw_proc_info_t *);
886 virtual void jumpto();
887 virtual bool isSignalFrame();
888 virtual bool getFunctionName(char *buf, size_t len, unw_word_t *off);
889 virtual void setInfoBasedOnIPRegister(bool isReturnAddress = false);
890 virtual const char *getRegisterName(int num);
891#ifdef __arm__
892 virtual void saveVFPAsX();
893#endif
894
895private:
896
Ranjeet Singh421231a2017-03-31 15:28:06 +0000897#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000898 bool getInfoFromEHABISection(pint_t pc, const UnwindInfoSections &sects);
Logan Chiena54f0962015-05-29 15:33:38 +0000899
900 int stepWithEHABI() {
901 size_t len = 0;
902 size_t off = 0;
903 // FIXME: Calling decode_eht_entry() here is violating the libunwind
904 // abstraction layer.
905 const uint32_t *ehtp =
906 decode_eht_entry(reinterpret_cast<const uint32_t *>(_info.unwind_info),
907 &off, &len);
908 if (_Unwind_VRS_Interpret((_Unwind_Context *)this, ehtp, off, len) !=
909 _URC_CONTINUE_UNWIND)
910 return UNW_STEP_END;
911 return UNW_STEP_SUCCESS;
912 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000913#endif
914
Ranjeet Singh421231a2017-03-31 15:28:06 +0000915#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000916 bool getInfoFromDwarfSection(pint_t pc, const UnwindInfoSections &sects,
917 uint32_t fdeSectionOffsetHint=0);
918 int stepWithDwarfFDE() {
919 return DwarfInstructions<A, R>::stepWithDwarf(_addressSpace,
920 (pint_t)this->getReg(UNW_REG_IP),
921 (pint_t)_info.unwind_info,
922 _registers);
923 }
924#endif
925
Ranjeet Singh421231a2017-03-31 15:28:06 +0000926#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000927 bool getInfoFromCompactEncodingSection(pint_t pc,
928 const UnwindInfoSections &sects);
929 int stepWithCompactEncoding() {
Ranjeet Singh421231a2017-03-31 15:28:06 +0000930 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000931 if ( compactSaysUseDwarf() )
932 return stepWithDwarfFDE();
933 #endif
934 R dummy;
935 return stepWithCompactEncoding(dummy);
936 }
937
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000938#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000939 int stepWithCompactEncoding(Registers_x86_64 &) {
940 return CompactUnwinder_x86_64<A>::stepWithCompactEncoding(
941 _info.format, _info.start_ip, _addressSpace, _registers);
942 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000943#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000944
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000945#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000946 int stepWithCompactEncoding(Registers_x86 &) {
947 return CompactUnwinder_x86<A>::stepWithCompactEncoding(
948 _info.format, (uint32_t)_info.start_ip, _addressSpace, _registers);
949 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000950#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000951
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000952#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000953 int stepWithCompactEncoding(Registers_ppc &) {
954 return UNW_EINVAL;
955 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000956#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000957
Martin Storsjo8338b0a2018-01-02 22:11:30 +0000958#if defined(_LIBUNWIND_TARGET_PPC64)
959 int stepWithCompactEncoding(Registers_ppc64 &) {
960 return UNW_EINVAL;
961 }
962#endif
963
964
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000965#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000966 int stepWithCompactEncoding(Registers_arm64 &) {
967 return CompactUnwinder_arm64<A>::stepWithCompactEncoding(
968 _info.format, _info.start_ip, _addressSpace, _registers);
969 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000970#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000971
John Baldwin56441d42017-12-12 21:43:36 +0000972#if defined(_LIBUNWIND_TARGET_MIPS_O32)
973 int stepWithCompactEncoding(Registers_mips_o32 &) {
974 return UNW_EINVAL;
975 }
976#endif
977
John Baldwin541a4352018-01-09 17:07:18 +0000978#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
979 int stepWithCompactEncoding(Registers_mips_newabi &) {
John Baldwin56441d42017-12-12 21:43:36 +0000980 return UNW_EINVAL;
981 }
982#endif
983
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000984 bool compactSaysUseDwarf(uint32_t *offset=NULL) const {
985 R dummy;
986 return compactSaysUseDwarf(dummy, offset);
987 }
988
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000989#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000990 bool compactSaysUseDwarf(Registers_x86_64 &, uint32_t *offset) const {
991 if ((_info.format & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF) {
992 if (offset)
993 *offset = (_info.format & UNWIND_X86_64_DWARF_SECTION_OFFSET);
994 return true;
995 }
996 return false;
997 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000998#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000999
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001000#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001001 bool compactSaysUseDwarf(Registers_x86 &, uint32_t *offset) const {
1002 if ((_info.format & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF) {
1003 if (offset)
1004 *offset = (_info.format & UNWIND_X86_DWARF_SECTION_OFFSET);
1005 return true;
1006 }
1007 return false;
1008 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001009#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001010
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001011#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001012 bool compactSaysUseDwarf(Registers_ppc &, uint32_t *) const {
1013 return true;
1014 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001015#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001016
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001017#if defined(_LIBUNWIND_TARGET_PPC64)
1018 bool compactSaysUseDwarf(Registers_ppc64 &, uint32_t *) const {
1019 return true;
1020 }
1021#endif
1022
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001023#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001024 bool compactSaysUseDwarf(Registers_arm64 &, uint32_t *offset) const {
1025 if ((_info.format & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF) {
1026 if (offset)
1027 *offset = (_info.format & UNWIND_ARM64_DWARF_SECTION_OFFSET);
1028 return true;
1029 }
1030 return false;
1031 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001032#endif
John Baldwin56441d42017-12-12 21:43:36 +00001033
1034#if defined(_LIBUNWIND_TARGET_MIPS_O32)
1035 bool compactSaysUseDwarf(Registers_mips_o32 &, uint32_t *) const {
1036 return true;
1037 }
1038#endif
1039
John Baldwin541a4352018-01-09 17:07:18 +00001040#if defined(_LIBUNWIND_TARGET_MIPS_NEWABI)
1041 bool compactSaysUseDwarf(Registers_mips_newabi &, uint32_t *) const {
John Baldwin56441d42017-12-12 21:43:36 +00001042 return true;
1043 }
1044#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +00001045#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001046
Ranjeet Singh421231a2017-03-31 15:28:06 +00001047#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001048 compact_unwind_encoding_t dwarfEncoding() const {
1049 R dummy;
1050 return dwarfEncoding(dummy);
1051 }
1052
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001053#if defined(_LIBUNWIND_TARGET_X86_64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001054 compact_unwind_encoding_t dwarfEncoding(Registers_x86_64 &) const {
1055 return UNWIND_X86_64_MODE_DWARF;
1056 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001057#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001058
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001059#if defined(_LIBUNWIND_TARGET_I386)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001060 compact_unwind_encoding_t dwarfEncoding(Registers_x86 &) const {
1061 return UNWIND_X86_MODE_DWARF;
1062 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001063#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001064
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001065#if defined(_LIBUNWIND_TARGET_PPC)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001066 compact_unwind_encoding_t dwarfEncoding(Registers_ppc &) const {
1067 return 0;
1068 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001069#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001070
Martin Storsjo8338b0a2018-01-02 22:11:30 +00001071#if defined(_LIBUNWIND_TARGET_PPC64)
1072 compact_unwind_encoding_t dwarfEncoding(Registers_ppc64 &) const {
1073 return 0;
1074 }
1075#endif
1076
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001077#if defined(_LIBUNWIND_TARGET_AARCH64)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001078 compact_unwind_encoding_t dwarfEncoding(Registers_arm64 &) const {
1079 return UNWIND_ARM64_MODE_DWARF;
1080 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001081#endif
Peter Zotov8d639992015-08-31 05:26:37 +00001082
Martin Storsjoa72285f2017-11-02 08:16:16 +00001083#if defined(_LIBUNWIND_TARGET_ARM)
1084 compact_unwind_encoding_t dwarfEncoding(Registers_arm &) const {
1085 return 0;
1086 }
1087#endif
1088
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001089#if defined (_LIBUNWIND_TARGET_OR1K)
Peter Zotov8d639992015-08-31 05:26:37 +00001090 compact_unwind_encoding_t dwarfEncoding(Registers_or1k &) const {
1091 return 0;
1092 }
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +00001093#endif
John Baldwin56441d42017-12-12 21:43:36 +00001094
1095#if defined (_LIBUNWIND_TARGET_MIPS_O32)
1096 compact_unwind_encoding_t dwarfEncoding(Registers_mips_o32 &) const {
1097 return 0;
1098 }
1099#endif
1100
John Baldwin541a4352018-01-09 17:07:18 +00001101#if defined (_LIBUNWIND_TARGET_MIPS_NEWABI)
1102 compact_unwind_encoding_t dwarfEncoding(Registers_mips_newabi &) const {
John Baldwin56441d42017-12-12 21:43:36 +00001103 return 0;
1104 }
1105#endif
Ranjeet Singh421231a2017-03-31 15:28:06 +00001106#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001107
Charles Davisfa2e6202018-08-30 21:29:00 +00001108#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1109 // For runtime environments using SEH unwind data without Windows runtime
1110 // support.
1111 pint_t getLastPC() const { /* FIXME: Implement */ return 0; }
1112 void setLastPC(pint_t pc) { /* FIXME: Implement */ }
1113 RUNTIME_FUNCTION *lookUpSEHUnwindInfo(pint_t pc, pint_t *base) {
1114 /* FIXME: Implement */
1115 *base = 0;
1116 return nullptr;
1117 }
1118 bool getInfoFromSEH(pint_t pc);
1119 int stepWithSEHData() { /* FIXME: Implement */ return 0; }
1120#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1121
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001122
1123 A &_addressSpace;
1124 R _registers;
1125 unw_proc_info_t _info;
1126 bool _unwindInfoMissing;
1127 bool _isSignalFrame;
1128};
1129
1130
1131template <typename A, typename R>
1132UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
1133 : _addressSpace(as), _registers(context), _unwindInfoMissing(false),
1134 _isSignalFrame(false) {
Asiri Rathnayake74d35252016-05-26 21:45:54 +00001135 static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001136 "UnwindCursor<> does not fit in unw_cursor_t");
1137 memset(&_info, 0, sizeof(_info));
1138}
1139
1140template <typename A, typename R>
1141UnwindCursor<A, R>::UnwindCursor(A &as, void *)
1142 : _addressSpace(as), _unwindInfoMissing(false), _isSignalFrame(false) {
1143 memset(&_info, 0, sizeof(_info));
1144 // FIXME
1145 // fill in _registers from thread arg
1146}
1147
1148
1149template <typename A, typename R>
1150bool UnwindCursor<A, R>::validReg(int regNum) {
1151 return _registers.validRegister(regNum);
1152}
1153
1154template <typename A, typename R>
1155unw_word_t UnwindCursor<A, R>::getReg(int regNum) {
1156 return _registers.getRegister(regNum);
1157}
1158
1159template <typename A, typename R>
1160void UnwindCursor<A, R>::setReg(int regNum, unw_word_t value) {
1161 _registers.setRegister(regNum, (typename A::pint_t)value);
1162}
1163
1164template <typename A, typename R>
1165bool UnwindCursor<A, R>::validFloatReg(int regNum) {
1166 return _registers.validFloatRegister(regNum);
1167}
1168
1169template <typename A, typename R>
1170unw_fpreg_t UnwindCursor<A, R>::getFloatReg(int regNum) {
1171 return _registers.getFloatRegister(regNum);
1172}
1173
1174template <typename A, typename R>
1175void UnwindCursor<A, R>::setFloatReg(int regNum, unw_fpreg_t value) {
1176 _registers.setFloatRegister(regNum, value);
1177}
1178
1179template <typename A, typename R> void UnwindCursor<A, R>::jumpto() {
1180 _registers.jumpto();
1181}
1182
1183#ifdef __arm__
1184template <typename A, typename R> void UnwindCursor<A, R>::saveVFPAsX() {
1185 _registers.saveVFPAsX();
1186}
1187#endif
1188
1189template <typename A, typename R>
1190const char *UnwindCursor<A, R>::getRegisterName(int regNum) {
1191 return _registers.getRegisterName(regNum);
1192}
1193
1194template <typename A, typename R> bool UnwindCursor<A, R>::isSignalFrame() {
1195 return _isSignalFrame;
1196}
1197
Charles Davisfa2e6202018-08-30 21:29:00 +00001198#endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1199
Ranjeet Singh421231a2017-03-31 15:28:06 +00001200#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001201struct EHABIIndexEntry {
1202 uint32_t functionOffset;
1203 uint32_t data;
1204};
1205
1206template<typename A>
1207struct EHABISectionIterator {
1208 typedef EHABISectionIterator _Self;
1209
1210 typedef std::random_access_iterator_tag iterator_category;
1211 typedef typename A::pint_t value_type;
1212 typedef typename A::pint_t* pointer;
1213 typedef typename A::pint_t& reference;
1214 typedef size_t size_type;
1215 typedef size_t difference_type;
1216
1217 static _Self begin(A& addressSpace, const UnwindInfoSections& sects) {
1218 return _Self(addressSpace, sects, 0);
1219 }
1220 static _Self end(A& addressSpace, const UnwindInfoSections& sects) {
Ed Schouten5d3f35b2017-03-07 15:21:57 +00001221 return _Self(addressSpace, sects,
1222 sects.arm_section_length / sizeof(EHABIIndexEntry));
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001223 }
1224
1225 EHABISectionIterator(A& addressSpace, const UnwindInfoSections& sects, size_t i)
1226 : _i(i), _addressSpace(&addressSpace), _sects(&sects) {}
1227
1228 _Self& operator++() { ++_i; return *this; }
1229 _Self& operator+=(size_t a) { _i += a; return *this; }
1230 _Self& operator--() { assert(_i > 0); --_i; return *this; }
1231 _Self& operator-=(size_t a) { assert(_i >= a); _i -= a; return *this; }
1232
1233 _Self operator+(size_t a) { _Self out = *this; out._i += a; return out; }
1234 _Self operator-(size_t a) { assert(_i >= a); _Self out = *this; out._i -= a; return out; }
1235
1236 size_t operator-(const _Self& other) { return _i - other._i; }
1237
1238 bool operator==(const _Self& other) const {
1239 assert(_addressSpace == other._addressSpace);
1240 assert(_sects == other._sects);
1241 return _i == other._i;
1242 }
1243
1244 typename A::pint_t operator*() const { return functionAddress(); }
1245
1246 typename A::pint_t functionAddress() const {
1247 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1248 EHABIIndexEntry, _i, functionOffset);
1249 return indexAddr + signExtendPrel31(_addressSpace->get32(indexAddr));
1250 }
1251
1252 typename A::pint_t dataAddress() {
1253 typename A::pint_t indexAddr = _sects->arm_section + arrayoffsetof(
1254 EHABIIndexEntry, _i, data);
1255 return indexAddr;
1256 }
1257
1258 private:
1259 size_t _i;
1260 A* _addressSpace;
1261 const UnwindInfoSections* _sects;
1262};
1263
1264template <typename A, typename R>
1265bool UnwindCursor<A, R>::getInfoFromEHABISection(
1266 pint_t pc,
1267 const UnwindInfoSections &sects) {
1268 EHABISectionIterator<A> begin =
1269 EHABISectionIterator<A>::begin(_addressSpace, sects);
1270 EHABISectionIterator<A> end =
1271 EHABISectionIterator<A>::end(_addressSpace, sects);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001272 if (begin == end)
1273 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001274
1275 EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
Momchil Velikov064d69a2017-07-24 09:19:32 +00001276 if (itNextPC == begin)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001277 return false;
1278 EHABISectionIterator<A> itThisPC = itNextPC - 1;
1279
1280 pint_t thisPC = itThisPC.functionAddress();
Momchil Velikov064d69a2017-07-24 09:19:32 +00001281 // If an exception is thrown from a function, corresponding to the last entry
1282 // in the table, we don't really know the function extent and have to choose a
1283 // value for nextPC. Choosing max() will allow the range check during trace to
1284 // succeed.
1285 pint_t nextPC = (itNextPC == end) ? std::numeric_limits<pint_t>::max()
1286 : itNextPC.functionAddress();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001287 pint_t indexDataAddr = itThisPC.dataAddress();
1288
1289 if (indexDataAddr == 0)
1290 return false;
1291
1292 uint32_t indexData = _addressSpace.get32(indexDataAddr);
1293 if (indexData == UNW_EXIDX_CANTUNWIND)
1294 return false;
1295
1296 // If the high bit is set, the exception handling table entry is inline inside
1297 // the index table entry on the second word (aka |indexDataAddr|). Otherwise,
1298 // the table points at an offset in the exception handling table (section 5 EHABI).
1299 pint_t exceptionTableAddr;
1300 uint32_t exceptionTableData;
1301 bool isSingleWordEHT;
1302 if (indexData & 0x80000000) {
1303 exceptionTableAddr = indexDataAddr;
1304 // TODO(ajwong): Should this data be 0?
1305 exceptionTableData = indexData;
1306 isSingleWordEHT = true;
1307 } else {
1308 exceptionTableAddr = indexDataAddr + signExtendPrel31(indexData);
1309 exceptionTableData = _addressSpace.get32(exceptionTableAddr);
1310 isSingleWordEHT = false;
1311 }
1312
1313 // Now we know the 3 things:
1314 // exceptionTableAddr -- exception handler table entry.
1315 // exceptionTableData -- the data inside the first word of the eht entry.
1316 // isSingleWordEHT -- whether the entry is in the index.
1317 unw_word_t personalityRoutine = 0xbadf00d;
1318 bool scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001319 uintptr_t lsda;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001320
1321 // If the high bit in the exception handling table entry is set, the entry is
1322 // in compact form (section 6.3 EHABI).
1323 if (exceptionTableData & 0x80000000) {
1324 // Grab the index of the personality routine from the compact form.
1325 uint32_t choice = (exceptionTableData & 0x0f000000) >> 24;
1326 uint32_t extraWords = 0;
1327 switch (choice) {
1328 case 0:
1329 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr0;
1330 extraWords = 0;
1331 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001332 lsda = isSingleWordEHT ? 0 : (exceptionTableAddr + 4);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001333 break;
1334 case 1:
1335 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr1;
1336 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1337 scope32 = false;
Logan Chiena54f0962015-05-29 15:33:38 +00001338 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001339 break;
1340 case 2:
1341 personalityRoutine = (unw_word_t) &__aeabi_unwind_cpp_pr2;
1342 extraWords = (exceptionTableData & 0x00ff0000) >> 16;
1343 scope32 = true;
Logan Chiena54f0962015-05-29 15:33:38 +00001344 lsda = exceptionTableAddr + (extraWords + 1) * 4;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001345 break;
1346 default:
1347 _LIBUNWIND_ABORT("unknown personality routine");
1348 return false;
1349 }
1350
1351 if (isSingleWordEHT) {
1352 if (extraWords != 0) {
1353 _LIBUNWIND_ABORT("index inlined table detected but pr function "
1354 "requires extra words");
1355 return false;
1356 }
1357 }
1358 } else {
1359 pint_t personalityAddr =
1360 exceptionTableAddr + signExtendPrel31(exceptionTableData);
1361 personalityRoutine = personalityAddr;
1362
1363 // ARM EHABI # 6.2, # 9.2
1364 //
1365 // +---- ehtp
1366 // v
1367 // +--------------------------------------+
1368 // | +--------+--------+--------+-------+ |
1369 // | |0| prel31 to personalityRoutine | |
1370 // | +--------+--------+--------+-------+ |
1371 // | | N | unwind opcodes | | <-- UnwindData
1372 // | +--------+--------+--------+-------+ |
1373 // | | Word 2 unwind opcodes | |
1374 // | +--------+--------+--------+-------+ |
1375 // | ... |
1376 // | +--------+--------+--------+-------+ |
1377 // | | Word N unwind opcodes | |
1378 // | +--------+--------+--------+-------+ |
1379 // | | LSDA | | <-- lsda
1380 // | | ... | |
1381 // | +--------+--------+--------+-------+ |
1382 // +--------------------------------------+
1383
1384 uint32_t *UnwindData = reinterpret_cast<uint32_t*>(exceptionTableAddr) + 1;
1385 uint32_t FirstDataWord = *UnwindData;
1386 size_t N = ((FirstDataWord >> 24) & 0xff);
1387 size_t NDataWords = N + 1;
1388 lsda = reinterpret_cast<uintptr_t>(UnwindData + NDataWords);
1389 }
1390
1391 _info.start_ip = thisPC;
1392 _info.end_ip = nextPC;
1393 _info.handler = personalityRoutine;
1394 _info.unwind_info = exceptionTableAddr;
1395 _info.lsda = lsda;
1396 // flags is pr_cache.additional. See EHABI #7.2 for definition of bit 0.
1397 _info.flags = isSingleWordEHT ? 1 : 0 | scope32 ? 0x2 : 0; // Use enum?
1398
1399 return true;
1400}
1401#endif
1402
Ranjeet Singh421231a2017-03-31 15:28:06 +00001403#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001404template <typename A, typename R>
1405bool UnwindCursor<A, R>::getInfoFromDwarfSection(pint_t pc,
1406 const UnwindInfoSections &sects,
1407 uint32_t fdeSectionOffsetHint) {
1408 typename CFI_Parser<A>::FDE_Info fdeInfo;
1409 typename CFI_Parser<A>::CIE_Info cieInfo;
1410 bool foundFDE = false;
1411 bool foundInCache = false;
1412 // If compact encoding table gave offset into dwarf section, go directly there
1413 if (fdeSectionOffsetHint != 0) {
1414 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1415 (uint32_t)sects.dwarf_section_length,
1416 sects.dwarf_section + fdeSectionOffsetHint,
1417 &fdeInfo, &cieInfo);
1418 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001419#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001420 if (!foundFDE && (sects.dwarf_index_section != 0)) {
1421 foundFDE = EHHeaderParser<A>::findFDE(
1422 _addressSpace, pc, sects.dwarf_index_section,
1423 (uint32_t)sects.dwarf_index_section_length, &fdeInfo, &cieInfo);
1424 }
1425#endif
1426 if (!foundFDE) {
1427 // otherwise, search cache of previously found FDEs.
1428 pint_t cachedFDE = DwarfFDECache<A>::findFDE(sects.dso_base, pc);
1429 if (cachedFDE != 0) {
1430 foundFDE =
1431 CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1432 (uint32_t)sects.dwarf_section_length,
1433 cachedFDE, &fdeInfo, &cieInfo);
1434 foundInCache = foundFDE;
1435 }
1436 }
1437 if (!foundFDE) {
1438 // Still not found, do full scan of __eh_frame section.
1439 foundFDE = CFI_Parser<A>::findFDE(_addressSpace, pc, sects.dwarf_section,
1440 (uint32_t)sects.dwarf_section_length, 0,
1441 &fdeInfo, &cieInfo);
1442 }
1443 if (foundFDE) {
1444 typename CFI_Parser<A>::PrologInfo prolog;
1445 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo, pc,
1446 &prolog)) {
1447 // Save off parsed FDE info
1448 _info.start_ip = fdeInfo.pcStart;
1449 _info.end_ip = fdeInfo.pcEnd;
1450 _info.lsda = fdeInfo.lsda;
1451 _info.handler = cieInfo.personality;
1452 _info.gp = prolog.spExtraArgSize;
1453 _info.flags = 0;
1454 _info.format = dwarfEncoding();
1455 _info.unwind_info = fdeInfo.fdeStart;
1456 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1457 _info.extra = (unw_word_t) sects.dso_base;
1458
1459 // Add to cache (to make next lookup faster) if we had no hint
1460 // and there was no index.
1461 if (!foundInCache && (fdeSectionOffsetHint == 0)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001462 #if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001463 if (sects.dwarf_index_section == 0)
1464 #endif
1465 DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
1466 fdeInfo.fdeStart);
1467 }
1468 return true;
1469 }
1470 }
Ed Maste41bc5a72016-08-30 15:38:10 +00001471 //_LIBUNWIND_DEBUG_LOG("can't find/use FDE for pc=0x%llX", (uint64_t)pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001472 return false;
1473}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001474#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001475
1476
Ranjeet Singh421231a2017-03-31 15:28:06 +00001477#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001478template <typename A, typename R>
1479bool UnwindCursor<A, R>::getInfoFromCompactEncodingSection(pint_t pc,
1480 const UnwindInfoSections &sects) {
1481 const bool log = false;
1482 if (log)
1483 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX, mh=0x%llX)\n",
1484 (uint64_t)pc, (uint64_t)sects.dso_base);
1485
1486 const UnwindSectionHeader<A> sectionHeader(_addressSpace,
1487 sects.compact_unwind_section);
1488 if (sectionHeader.version() != UNWIND_SECTION_VERSION)
1489 return false;
1490
1491 // do a binary search of top level index to find page with unwind info
1492 pint_t targetFunctionOffset = pc - sects.dso_base;
1493 const UnwindSectionIndexArray<A> topIndex(_addressSpace,
1494 sects.compact_unwind_section
1495 + sectionHeader.indexSectionOffset());
1496 uint32_t low = 0;
1497 uint32_t high = sectionHeader.indexCount();
1498 uint32_t last = high - 1;
1499 while (low < high) {
1500 uint32_t mid = (low + high) / 2;
1501 //if ( log ) fprintf(stderr, "\tmid=%d, low=%d, high=%d, *mid=0x%08X\n",
1502 //mid, low, high, topIndex.functionOffset(mid));
1503 if (topIndex.functionOffset(mid) <= targetFunctionOffset) {
1504 if ((mid == last) ||
1505 (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
1506 low = mid;
1507 break;
1508 } else {
1509 low = mid + 1;
1510 }
1511 } else {
1512 high = mid;
1513 }
1514 }
1515 const uint32_t firstLevelFunctionOffset = topIndex.functionOffset(low);
1516 const uint32_t firstLevelNextPageFunctionOffset =
1517 topIndex.functionOffset(low + 1);
1518 const pint_t secondLevelAddr =
1519 sects.compact_unwind_section + topIndex.secondLevelPagesSectionOffset(low);
1520 const pint_t lsdaArrayStartAddr =
1521 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low);
1522 const pint_t lsdaArrayEndAddr =
1523 sects.compact_unwind_section + topIndex.lsdaIndexArraySectionOffset(low+1);
1524 if (log)
1525 fprintf(stderr, "\tfirst level search for result index=%d "
1526 "to secondLevelAddr=0x%llX\n",
1527 low, (uint64_t) secondLevelAddr);
1528 // do a binary search of second level page index
1529 uint32_t encoding = 0;
1530 pint_t funcStart = 0;
1531 pint_t funcEnd = 0;
1532 pint_t lsda = 0;
1533 pint_t personality = 0;
1534 uint32_t pageKind = _addressSpace.get32(secondLevelAddr);
1535 if (pageKind == UNWIND_SECOND_LEVEL_REGULAR) {
1536 // regular page
1537 UnwindSectionRegularPageHeader<A> pageHeader(_addressSpace,
1538 secondLevelAddr);
1539 UnwindSectionRegularArray<A> pageIndex(
1540 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1541 // binary search looks for entry with e where index[e].offset <= pc <
1542 // index[e+1].offset
1543 if (log)
1544 fprintf(stderr, "\tbinary search for targetFunctionOffset=0x%08llX in "
1545 "regular page starting at secondLevelAddr=0x%llX\n",
1546 (uint64_t) targetFunctionOffset, (uint64_t) secondLevelAddr);
1547 low = 0;
1548 high = pageHeader.entryCount();
1549 while (low < high) {
1550 uint32_t mid = (low + high) / 2;
1551 if (pageIndex.functionOffset(mid) <= targetFunctionOffset) {
1552 if (mid == (uint32_t)(pageHeader.entryCount() - 1)) {
1553 // at end of table
1554 low = mid;
1555 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1556 break;
1557 } else if (pageIndex.functionOffset(mid + 1) > targetFunctionOffset) {
1558 // next is too big, so we found it
1559 low = mid;
1560 funcEnd = pageIndex.functionOffset(low + 1) + sects.dso_base;
1561 break;
1562 } else {
1563 low = mid + 1;
1564 }
1565 } else {
1566 high = mid;
1567 }
1568 }
1569 encoding = pageIndex.encoding(low);
1570 funcStart = pageIndex.functionOffset(low) + sects.dso_base;
1571 if (pc < funcStart) {
1572 if (log)
1573 fprintf(
1574 stderr,
1575 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1576 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1577 return false;
1578 }
1579 if (pc > funcEnd) {
1580 if (log)
1581 fprintf(
1582 stderr,
1583 "\tpc not in table, pc=0x%llX, funcStart=0x%llX, funcEnd=0x%llX\n",
1584 (uint64_t) pc, (uint64_t) funcStart, (uint64_t) funcEnd);
1585 return false;
1586 }
1587 } else if (pageKind == UNWIND_SECOND_LEVEL_COMPRESSED) {
1588 // compressed page
1589 UnwindSectionCompressedPageHeader<A> pageHeader(_addressSpace,
1590 secondLevelAddr);
1591 UnwindSectionCompressedArray<A> pageIndex(
1592 _addressSpace, secondLevelAddr + pageHeader.entryPageOffset());
1593 const uint32_t targetFunctionPageOffset =
1594 (uint32_t)(targetFunctionOffset - firstLevelFunctionOffset);
1595 // binary search looks for entry with e where index[e].offset <= pc <
1596 // index[e+1].offset
1597 if (log)
1598 fprintf(stderr, "\tbinary search of compressed page starting at "
1599 "secondLevelAddr=0x%llX\n",
1600 (uint64_t) secondLevelAddr);
1601 low = 0;
1602 last = pageHeader.entryCount() - 1;
1603 high = pageHeader.entryCount();
1604 while (low < high) {
1605 uint32_t mid = (low + high) / 2;
1606 if (pageIndex.functionOffset(mid) <= targetFunctionPageOffset) {
1607 if ((mid == last) ||
1608 (pageIndex.functionOffset(mid + 1) > targetFunctionPageOffset)) {
1609 low = mid;
1610 break;
1611 } else {
1612 low = mid + 1;
1613 }
1614 } else {
1615 high = mid;
1616 }
1617 }
1618 funcStart = pageIndex.functionOffset(low) + firstLevelFunctionOffset
1619 + sects.dso_base;
1620 if (low < last)
1621 funcEnd =
1622 pageIndex.functionOffset(low + 1) + firstLevelFunctionOffset
1623 + sects.dso_base;
1624 else
1625 funcEnd = firstLevelNextPageFunctionOffset + sects.dso_base;
1626 if (pc < funcStart) {
1627 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001628 "level compressed unwind table. funcStart=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001629 (uint64_t) pc, (uint64_t) funcStart);
1630 return false;
1631 }
1632 if (pc > funcEnd) {
1633 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info, pc=0x%llX not in second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001634 "level compressed unwind table. funcEnd=0x%llX",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001635 (uint64_t) pc, (uint64_t) funcEnd);
1636 return false;
1637 }
1638 uint16_t encodingIndex = pageIndex.encodingIndex(low);
1639 if (encodingIndex < sectionHeader.commonEncodingsArrayCount()) {
1640 // encoding is in common table in section header
1641 encoding = _addressSpace.get32(
1642 sects.compact_unwind_section +
1643 sectionHeader.commonEncodingsArraySectionOffset() +
1644 encodingIndex * sizeof(uint32_t));
1645 } else {
1646 // encoding is in page specific table
1647 uint16_t pageEncodingIndex =
1648 encodingIndex - (uint16_t)sectionHeader.commonEncodingsArrayCount();
1649 encoding = _addressSpace.get32(secondLevelAddr +
1650 pageHeader.encodingsPageOffset() +
1651 pageEncodingIndex * sizeof(uint32_t));
1652 }
1653 } else {
1654 _LIBUNWIND_DEBUG_LOG("malformed __unwind_info at 0x%0llX bad second "
Ed Maste41bc5a72016-08-30 15:38:10 +00001655 "level page",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001656 (uint64_t) sects.compact_unwind_section);
1657 return false;
1658 }
1659
1660 // look up LSDA, if encoding says function has one
1661 if (encoding & UNWIND_HAS_LSDA) {
1662 UnwindSectionLsdaArray<A> lsdaIndex(_addressSpace, lsdaArrayStartAddr);
1663 uint32_t funcStartOffset = (uint32_t)(funcStart - sects.dso_base);
1664 low = 0;
1665 high = (uint32_t)(lsdaArrayEndAddr - lsdaArrayStartAddr) /
1666 sizeof(unwind_info_section_header_lsda_index_entry);
1667 // binary search looks for entry with exact match for functionOffset
1668 if (log)
1669 fprintf(stderr,
1670 "\tbinary search of lsda table for targetFunctionOffset=0x%08X\n",
1671 funcStartOffset);
1672 while (low < high) {
1673 uint32_t mid = (low + high) / 2;
1674 if (lsdaIndex.functionOffset(mid) == funcStartOffset) {
1675 lsda = lsdaIndex.lsdaOffset(mid) + sects.dso_base;
1676 break;
1677 } else if (lsdaIndex.functionOffset(mid) < funcStartOffset) {
1678 low = mid + 1;
1679 } else {
1680 high = mid;
1681 }
1682 }
1683 if (lsda == 0) {
1684 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with HAS_LSDA bit set for "
Ed Maste41bc5a72016-08-30 15:38:10 +00001685 "pc=0x%0llX, but lsda table has no entry",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001686 encoding, (uint64_t) pc);
1687 return false;
1688 }
1689 }
1690
1691 // extact personality routine, if encoding says function has one
1692 uint32_t personalityIndex = (encoding & UNWIND_PERSONALITY_MASK) >>
1693 (__builtin_ctz(UNWIND_PERSONALITY_MASK));
1694 if (personalityIndex != 0) {
1695 --personalityIndex; // change 1-based to zero-based index
1696 if (personalityIndex > sectionHeader.personalityArrayCount()) {
1697 _LIBUNWIND_DEBUG_LOG("found encoding 0x%08X with personality index %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +00001698 "but personality table has only %d entires",
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001699 encoding, personalityIndex,
1700 sectionHeader.personalityArrayCount());
1701 return false;
1702 }
1703 int32_t personalityDelta = (int32_t)_addressSpace.get32(
1704 sects.compact_unwind_section +
1705 sectionHeader.personalityArraySectionOffset() +
1706 personalityIndex * sizeof(uint32_t));
1707 pint_t personalityPointer = sects.dso_base + (pint_t)personalityDelta;
1708 personality = _addressSpace.getP(personalityPointer);
1709 if (log)
1710 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1711 "personalityDelta=0x%08X, personality=0x%08llX\n",
1712 (uint64_t) pc, personalityDelta, (uint64_t) personality);
1713 }
1714
1715 if (log)
1716 fprintf(stderr, "getInfoFromCompactEncodingSection(pc=0x%llX), "
1717 "encoding=0x%08X, lsda=0x%08llX for funcStart=0x%llX\n",
1718 (uint64_t) pc, encoding, (uint64_t) lsda, (uint64_t) funcStart);
1719 _info.start_ip = funcStart;
1720 _info.end_ip = funcEnd;
1721 _info.lsda = lsda;
1722 _info.handler = personality;
1723 _info.gp = 0;
1724 _info.flags = 0;
1725 _info.format = encoding;
1726 _info.unwind_info = 0;
1727 _info.unwind_info_size = 0;
1728 _info.extra = sects.dso_base;
1729 return true;
1730}
Ranjeet Singh421231a2017-03-31 15:28:06 +00001731#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001732
1733
Charles Davisfa2e6202018-08-30 21:29:00 +00001734#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1735template <typename A, typename R>
1736bool UnwindCursor<A, R>::getInfoFromSEH(pint_t pc) {
1737 pint_t base;
1738 RUNTIME_FUNCTION *unwindEntry = lookUpSEHUnwindInfo(pc, &base);
1739 if (!unwindEntry) {
1740 _LIBUNWIND_DEBUG_LOG("\tpc not in table, pc=0x%llX", (uint64_t) pc);
1741 return false;
1742 }
1743 _info.gp = 0;
1744 _info.flags = 0;
1745 _info.format = 0;
1746 _info.unwind_info_size = sizeof(RUNTIME_FUNCTION);
1747 _info.unwind_info = reinterpret_cast<unw_word_t>(unwindEntry);
1748 _info.extra = base;
1749 _info.start_ip = base + unwindEntry->BeginAddress;
1750#ifdef _LIBUNWIND_TARGET_X86_64
1751 _info.end_ip = base + unwindEntry->EndAddress;
1752 // Only fill in the handler and LSDA if they're stale.
1753 if (pc != getLastPC()) {
1754 UNWIND_INFO *xdata = reinterpret_cast<UNWIND_INFO *>(base + unwindEntry->UnwindData);
1755 if (xdata->Flags & (UNW_FLAG_EHANDLER|UNW_FLAG_UHANDLER)) {
1756 // The personality is given in the UNWIND_INFO itself. The LSDA immediately
1757 // follows the UNWIND_INFO. (This follows how both Clang and MSVC emit
1758 // these structures.)
1759 // N.B. UNWIND_INFO structs are DWORD-aligned.
1760 uint32_t lastcode = (xdata->CountOfCodes + 1) & ~1;
1761 const uint32_t *handler = reinterpret_cast<uint32_t *>(&xdata->UnwindCodes[lastcode]);
1762 _info.lsda = reinterpret_cast<unw_word_t>(handler+1);
1763 if (*handler) {
1764 _info.handler = reinterpret_cast<unw_word_t>(__libunwind_seh_personality);
1765 } else
1766 _info.handler = 0;
1767 } else {
1768 _info.lsda = 0;
1769 _info.handler = 0;
1770 }
1771 }
1772#elif defined(_LIBUNWIND_TARGET_ARM)
1773 _info.end_ip = _info.start_ip + unwindEntry->FunctionLength;
1774 _info.lsda = 0; // FIXME
1775 _info.handler = 0; // FIXME
1776#endif
1777 setLastPC(pc);
1778 return true;
1779}
1780#endif
1781
1782
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001783template <typename A, typename R>
1784void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
1785 pint_t pc = (pint_t)this->getReg(UNW_REG_IP);
Ranjeet Singh421231a2017-03-31 15:28:06 +00001786#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001787 // Remove the thumb bit so the IP represents the actual instruction address.
1788 // This matches the behaviour of _Unwind_GetIP on arm.
1789 pc &= (pint_t)~0x1;
1790#endif
1791
1792 // If the last line of a function is a "throw" the compiler sometimes
1793 // emits no instructions after the call to __cxa_throw. This means
1794 // the return address is actually the start of the next function.
1795 // To disambiguate this, back up the pc when we know it is a return
1796 // address.
1797 if (isReturnAddress)
1798 --pc;
1799
1800 // Ask address space object to find unwind sections for this pc.
1801 UnwindInfoSections sects;
1802 if (_addressSpace.findUnwindSections(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001803#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001804 // If there is a compact unwind encoding table, look there first.
1805 if (sects.compact_unwind_section != 0) {
1806 if (this->getInfoFromCompactEncodingSection(pc, sects)) {
Ranjeet Singh421231a2017-03-31 15:28:06 +00001807 #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001808 // Found info in table, done unless encoding says to use dwarf.
1809 uint32_t dwarfOffset;
1810 if ((sects.dwarf_section != 0) && compactSaysUseDwarf(&dwarfOffset)) {
1811 if (this->getInfoFromDwarfSection(pc, sects, dwarfOffset)) {
1812 // found info in dwarf, done
1813 return;
1814 }
1815 }
1816 #endif
1817 // If unwind table has entry, but entry says there is no unwind info,
1818 // record that we have no unwind info.
1819 if (_info.format == 0)
1820 _unwindInfoMissing = true;
1821 return;
1822 }
1823 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001824#endif // defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001825
Charles Davisfa2e6202018-08-30 21:29:00 +00001826#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1827 // If there is SEH unwind info, look there next.
1828 if (this->getInfoFromSEH(pc))
1829 return;
1830#endif
1831
Ranjeet Singh421231a2017-03-31 15:28:06 +00001832#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001833 // If there is dwarf unwind info, look there next.
1834 if (sects.dwarf_section != 0) {
1835 if (this->getInfoFromDwarfSection(pc, sects)) {
1836 // found info in dwarf, done
1837 return;
1838 }
1839 }
1840#endif
1841
Ranjeet Singh421231a2017-03-31 15:28:06 +00001842#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001843 // If there is ARM EHABI unwind info, look there next.
1844 if (sects.arm_section != 0 && this->getInfoFromEHABISection(pc, sects))
1845 return;
1846#endif
1847 }
1848
Ranjeet Singh421231a2017-03-31 15:28:06 +00001849#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001850 // There is no static unwind info for this pc. Look to see if an FDE was
1851 // dynamically registered for it.
1852 pint_t cachedFDE = DwarfFDECache<A>::findFDE(0, pc);
1853 if (cachedFDE != 0) {
1854 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1855 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1856 const char *msg = CFI_Parser<A>::decodeFDE(_addressSpace,
1857 cachedFDE, &fdeInfo, &cieInfo);
1858 if (msg == NULL) {
1859 typename CFI_Parser<A>::PrologInfo prolog;
1860 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo, cieInfo,
1861 pc, &prolog)) {
1862 // save off parsed FDE info
1863 _info.start_ip = fdeInfo.pcStart;
1864 _info.end_ip = fdeInfo.pcEnd;
1865 _info.lsda = fdeInfo.lsda;
1866 _info.handler = cieInfo.personality;
1867 _info.gp = prolog.spExtraArgSize;
1868 // Some frameless functions need SP
1869 // altered when resuming in function.
1870 _info.flags = 0;
1871 _info.format = dwarfEncoding();
1872 _info.unwind_info = fdeInfo.fdeStart;
1873 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1874 _info.extra = 0;
1875 return;
1876 }
1877 }
1878 }
1879
1880 // Lastly, ask AddressSpace object about platform specific ways to locate
1881 // other FDEs.
1882 pint_t fde;
1883 if (_addressSpace.findOtherFDE(pc, fde)) {
1884 CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
1885 CFI_Parser<LocalAddressSpace>::CIE_Info cieInfo;
1886 if (!CFI_Parser<A>::decodeFDE(_addressSpace, fde, &fdeInfo, &cieInfo)) {
1887 // Double check this FDE is for a function that includes the pc.
1888 if ((fdeInfo.pcStart <= pc) && (pc < fdeInfo.pcEnd)) {
1889 typename CFI_Parser<A>::PrologInfo prolog;
1890 if (CFI_Parser<A>::parseFDEInstructions(_addressSpace, fdeInfo,
1891 cieInfo, pc, &prolog)) {
1892 // save off parsed FDE info
1893 _info.start_ip = fdeInfo.pcStart;
1894 _info.end_ip = fdeInfo.pcEnd;
1895 _info.lsda = fdeInfo.lsda;
1896 _info.handler = cieInfo.personality;
1897 _info.gp = prolog.spExtraArgSize;
1898 _info.flags = 0;
1899 _info.format = dwarfEncoding();
1900 _info.unwind_info = fdeInfo.fdeStart;
1901 _info.unwind_info_size = (uint32_t)fdeInfo.fdeLength;
1902 _info.extra = 0;
1903 return;
1904 }
1905 }
1906 }
1907 }
Ranjeet Singh421231a2017-03-31 15:28:06 +00001908#endif // #if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001909
1910 // no unwind info, flag that we can't reliably unwind
1911 _unwindInfoMissing = true;
1912}
1913
1914template <typename A, typename R>
1915int UnwindCursor<A, R>::step() {
1916 // Bottom of stack is defined is when unwind info cannot be found.
1917 if (_unwindInfoMissing)
1918 return UNW_STEP_END;
1919
1920 // Use unwinding info to modify register set as if function returned.
1921 int result;
Ranjeet Singh421231a2017-03-31 15:28:06 +00001922#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001923 result = this->stepWithCompactEncoding();
Charles Davisfa2e6202018-08-30 21:29:00 +00001924#elif defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
1925 result = this->stepWithSEHData();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001926#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001927 result = this->stepWithDwarfFDE();
Ranjeet Singh421231a2017-03-31 15:28:06 +00001928#elif defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +00001929 result = this->stepWithEHABI();
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001930#else
1931 #error Need _LIBUNWIND_SUPPORT_COMPACT_UNWIND or \
Charles Davisfa2e6202018-08-30 21:29:00 +00001932 _LIBUNWIND_SUPPORT_SEH_UNWIND or \
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001933 _LIBUNWIND_SUPPORT_DWARF_UNWIND or \
Logan Chien06b0c7a2015-07-19 15:23:10 +00001934 _LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001935#endif
1936
1937 // update info based on new PC
1938 if (result == UNW_STEP_SUCCESS) {
1939 this->setInfoBasedOnIPRegister(true);
1940 if (_unwindInfoMissing)
1941 return UNW_STEP_END;
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001942 }
1943
1944 return result;
1945}
1946
1947template <typename A, typename R>
1948void UnwindCursor<A, R>::getInfo(unw_proc_info_t *info) {
1949 *info = _info;
1950}
1951
1952template <typename A, typename R>
1953bool UnwindCursor<A, R>::getFunctionName(char *buf, size_t bufLen,
1954 unw_word_t *offset) {
1955 return _addressSpace.findFunctionName((pint_t)this->getReg(UNW_REG_IP),
1956 buf, bufLen, offset);
1957}
1958
1959} // namespace libunwind
1960
1961#endif // __UNWINDCURSOR_HPP__