-
Notifications
You must be signed in to change notification settings - Fork 35
/
dwarfproc.cc
660 lines (582 loc) · 22 KB
/
dwarfproc.cc
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
#include "libpstack/dwarf.h"
#include "libpstack/elf.h"
#include "libpstack/proc.h"
#include "libpstack/global.h"
#include "libpstack/dwarf_reader.h"
#include <stack>
#include <unistd.h>
extern std::ostream & operator << (std::ostream &os, const pstack::Dwarf::DIE &);
namespace pstack::Procman {
void
StackFrame::setCoreRegs(const Elf::CoreRegisters &sys)
{
#define REGMAP(number, field) Elf::setReg(regs, number, sys.field);
#include "libpstack/archreg.h"
#undef REGMAP
}
void
StackFrame::getCoreRegs(Elf::CoreRegisters &core) const
{
#define REGMAP(number, field) core.field = Elf::getReg(regs, number);
#include "libpstack/archreg.h"
#undef REGMAP
}
Elf::Addr
StackFrame::rawIP() const
{
return Elf::getReg(regs, IPREG);
}
ProcessLocation
StackFrame::scopeIP(Process &proc) const
{
// For a return address on the stack, it normally represents the next
// instruction after a call. For functions that don't return, this might
// land outside the caller function - so we subtract one, putting us in the
// middle of the call instruction. This will also improve source line
// details, as the actual return address is likely on the line of code
// *after* the call rather than at it.
//
// There are two exceptions: first, the instruction pointer we grab from
// the process's register state - this is the currently executing
// instruction, so accurately reflects the position in the top stack frame.
//
// The other is for signal trampolines - In this case, the return address
// has been synthesized to be the entrypoint of a function (eg,
// __restore_rt) to handle return from the signal handler, and will be the
// first instruction in the function - there's no previous call instruction
// to point at, so we use it directly.
auto raw = rawIP();
if (raw == 0)
return { proc, raw };
if (mechanism == UnwindMechanism::MACHINEREGS)
return { proc, raw };
if (isSignalTrampoline)
return { proc, raw };
ProcessLocation location(proc, raw);
const auto *lcie = location.cie();
if (lcie != nullptr && lcie->isSignalHandler)
return location;
return {proc, raw - 1};
}
uintptr_t
StackFrame::getFrameBase(Process &p) const
{
ProcessLocation location = scopeIP(p);
const Dwarf::DIE &f = location.die();
if (f) {
auto base = f.attribute(Dwarf::DW_AT_frame_base);
if (base.valid()) {
ExpressionStack stack;
return stack.eval(p, base, this, location.elfReloc());
}
}
return 0;
}
enum DW_LLE : uint8_t {
#define DW_LLE_VAL(name, value) name = value,
#include "libpstack/dwarf/lle.h"
DW_LLE_invalid
#undef DW_LLE_VAL
};
std::ostream &
operator <<( std::ostream &os, DW_LLE lle) {
#define DW_LLE_VAL(name, value) case name: return os << #name;
switch (lle) {
#include "libpstack/dwarf/lle.h"
default: return os << "(unknown LLE " << uint8_t(lle) << ")";
}
#undef DW_LLE_VAL
};
/*
* Evaluate an expression specified by an exprloc, or as inferred by a location list
*/
uintmax_t
ExpressionStack::eval(Process &proc, const Dwarf::DIE::Attribute &attr,
const StackFrame *frame, Elf::Addr reloc)
{
Dwarf::Unit::sptr unit = attr.die.getUnit();
const Dwarf::Info *dwarf = unit->dwarf;
auto loc = frame->scopeIP(proc);
auto ip = loc.location();
const auto &unitEntry = unit->root();
auto unitLow = unitEntry.attribute(Dwarf::DW_AT_low_pc);
// default base address is relocation of the object + base of unit.
uint64_t base = reloc + uintmax_t(unitLow);
switch (attr.form()) {
case Dwarf::DW_FORM_sec_offset:
if (unit->version >= 5) {
// For dwarf 5, this will be a debug_loclists entry.
const Elf::Section &sec = dwarf->elf->getDebugSection(".debug_loclists", SHT_NULL);
const Elf::Section &addrsec = dwarf->elf->getDebugSection(".debug_addr", SHT_NULL);
Dwarf::DWARFReader r(sec.io(), uintmax_t(attr));
for (;;) {
auto lle = DW_LLE(r.getu8());
switch (lle) {
case DW_LLE_end_of_list:
return 0; // failed to find a loclist for the given IP.
case DW_LLE_offset_pair:
{
auto start = r.getuleb128();
auto end = r.getuleb128();
auto len = r.getuleb128();
if (base + start <= ip && ip < base + end) {
Dwarf::DWARFReader exr(r.io, r.getOffset(), r.getOffset() + len);
return eval(proc, exr, frame, loc.elfReloc());
}
r.skip(len);
break;
}
case DW_LLE_base_address:
base = reloc + r.getuint(unit->addrlen);
break;
case DW_LLE_base_addressx:
{
auto idx = r.getuleb128();
addrsec.io()->readObj(idx * unit->addrlen, &base);
}
break;
case DW_LLE_start_length: {
auto start = r.getuint(unit->addrlen);
auto end = start + r.getuleb128();
auto len = r.getuleb128();
if (base + start <= ip && ip < base + end) {
Dwarf::DWARFReader exr(r.io, r.getOffset(), r.getOffset() + len);
return eval(proc, exr, frame, loc.elfReloc());
}
r.skip(len);
break;
}
default:
abort(); // can implement it when we see it.
}
}
} else {
// For dwarf 4, this will be a debug_loc entry.
auto &sec = dwarf->elf->getDebugSection(".debug_loc", SHT_NULL);
// convert this object-relative addr to a unit-relative one
Dwarf::DWARFReader r(sec.io(), uintmax_t(attr));
for (;;) {
Elf::Addr start = r.getint(sizeof start);
Elf::Addr end = r.getint(sizeof end);
if (start == 0 && end == 0)
return 0;
auto len = r.getuint(2);
if (ip >= base + start && ip < base + end) {
Dwarf::DWARFReader exr(r.io, r.getOffset(), r.getOffset() + Elf::Word(len));
return eval(proc, exr, frame, loc.elfReloc());
}
r.skip(len);
}
}
abort();
case Dwarf::DW_FORM_block1:
case Dwarf::DW_FORM_block:
case Dwarf::DW_FORM_exprloc: {
const auto &block = Dwarf::Block(attr);
Dwarf::DWARFReader r(dwarf->debugInfo.io(), block.offset, block.offset + block.length);
return eval(proc, r, frame, reloc);
}
default:
abort();
}
}
uintmax_t
ExpressionStack::eval(Process &proc, Dwarf::DWARFReader &r, const StackFrame *frame, Elf::Addr reloc)
{
using namespace Dwarf;
int piece = 0;
isValue = false;
while (!r.empty()) {
auto op = ExpressionOp(r.getu8());
switch (op) {
case DW_OP_deref: {
uintmax_t addr = poptop();
auto value = proc.io->readObj<Elf::Addr>(addr);
push(intptr_t(value));
break;
}
case DW_OP_consts: {
push(r.getsleb128());
break;
}
case DW_OP_constu: {
push(r.getuleb128());
break;
}
case DW_OP_const2s: {
push(int16_t(r.getu16()));
break;
}
case DW_OP_const4u: {
push(r.getu32());
break;
}
case DW_OP_const4s: {
push(int32_t(r.getu32()));
break;
}
case DW_OP_minus: {
auto tos = poptop();
auto second = poptop();
push(second - tos);
break;
}
case DW_OP_plus: {
auto tos = poptop();
auto second = poptop();
push(second + tos);
break;
}
case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31: {
auto offset = r.getsleb128();
push(Elf::getReg(frame->regs, op - DW_OP_breg0) + offset);
break;
}
case DW_OP_lit0: case DW_OP_lit1: case DW_OP_lit2: case DW_OP_lit3: case DW_OP_lit4:
case DW_OP_lit5: case DW_OP_lit6: case DW_OP_lit7: case DW_OP_lit8: case DW_OP_lit9:
case DW_OP_lit10: case DW_OP_lit11: case DW_OP_lit12: case DW_OP_lit13: case DW_OP_lit14:
case DW_OP_lit15: case DW_OP_lit16: case DW_OP_lit17: case DW_OP_lit18: case DW_OP_lit19:
case DW_OP_lit20: case DW_OP_lit21: case DW_OP_lit22: case DW_OP_lit23: case DW_OP_lit24:
case DW_OP_lit25: case DW_OP_lit26: case DW_OP_lit27: case DW_OP_lit28: case DW_OP_lit29:
case DW_OP_lit30: case DW_OP_lit31:
push(op - DW_OP_lit0);
break;
case DW_OP_and: {
auto lhs = poptop();
auto rhs = poptop();
push(lhs & rhs);
break;
}
case DW_OP_or: {
auto lhs = poptop();
auto rhs = poptop();
push(lhs | rhs);
break;
}
case DW_OP_le: {
auto rhs = poptop();
auto lhs = poptop();
push(value_type(lhs <= rhs));
break;
}
case DW_OP_ge: {
auto rhs = poptop();
auto lhs = poptop();
push(value_type(lhs >= rhs));
break;
}
case DW_OP_eq: {
auto rhs = poptop();
auto lhs = poptop();
push(value_type(lhs == rhs));
break;
}
case DW_OP_lt: {
auto rhs = poptop();
auto lhs = poptop();
push(value_type(lhs < rhs));
break;
}
case DW_OP_gt: {
auto rhs = poptop();
auto lhs = poptop();
push(value_type(lhs > rhs));
break;
}
case DW_OP_ne: {
auto rhs = poptop();
auto lhs = poptop();
push(value_type(lhs != rhs));
break;
}
case DW_OP_shl: {
auto rhs = poptop();
auto lhs = poptop();
push(lhs << rhs);
break;
}
case DW_OP_shr: {
auto rhs = poptop();
auto lhs = poptop();
push(lhs >> rhs);
break;
}
case DW_OP_addr: {
auto value = r.getuint(r.addrLen);
push(value + reloc);
break;
}
case DW_OP_call_frame_cfa:
push(frame->cfa);
break;
case DW_OP_fbreg:
// Yuk - find DW_AT_frame_base, and offset from that.
push(frame->getFrameBase(proc) + r.getsleb128());
break;
case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
isValue = true;
inReg = op - DW_OP_reg0;
push(Elf::getReg(frame->regs, op - DW_OP_reg0));
break;
case DW_OP_regx:
push(Elf::getReg(frame->regs, int(r.getsleb128())));
break;
case DW_OP_entry_value:
case DW_OP_GNU_entry_value: {
auto len = r.getuleb128();
DWARFReader r2(r.io, r.getOffset(), r.getOffset() + len);
push(eval(proc, r2, frame, reloc));
break;
}
case DW_OP_piece: {
auto bytes = r.getuleb128();
auto value = poptop();
uintmax_t mask = bytes < sizeof mask ? std::numeric_limits<uintmax_t>::max() << 8 * bytes : 0;
value &= ~mask;
if (piece++ != 0) {
// This is not the first piece - pop the existing piece off the top of the stack, and fold this in.
auto existing = poptop();
existing <<= (8 * bytes);
value |= existing;
}
push(value);
break;
}
case DW_OP_stack_value:
break; // XXX: the returned value is not a location, but the underlying value itself.
case DW_OP_GNU_parameter_ref:
{
if (debug)
*debug << "can't handle DW_OP_GNU_parameter_ref: ";
auto loc = frame->scopeIP(proc);
auto unit = loc.die().getUnit();
auto off = r.getuint(4);
auto die = unit->offsetToDIE(DIE(), off + unit->offset);
if (debug)
*debug << json(die) << "\n";
auto attr = die.attribute(DW_AT_type);
if (attr) {
auto typeDie = DIE(attr);
if (debug)
*debug << json(typeDie) << "\n";
}
if (debug)
*debug << "\n";
return -1;
}
// FALLTHROUGH
default:
abort();
if (debug)
*debug << "error evaluating DWARF OP " << op << " (" << int(op) << ")\n";
return -1;
}
}
return poptop();
}
StackFrame::StackFrame(UnwindMechanism mechanism, const Elf::CoreRegisters ®s_)
: regs(regs_)
, cfa(0)
, mechanism(mechanism)
, isSignalTrampoline(false)
{}
std::optional<Elf::CoreRegisters> StackFrame::unwind(Process &p) {
ProcessLocation location = scopeIP(p);
const Dwarf::CFI *cfi = location.cfi();
const Dwarf::FDE *fde = location.fde();
const Dwarf::CIE *cie = location.cie();
if (fde == nullptr || cie == nullptr || cfi == nullptr)
throw (Exception() << "no FDE/CIE/CFI for instruction address " << std::hex << location.location());
if (cie->isSignalHandler)
isSignalTrampoline = true;
// relocate from process address to object address
Elf::Off objaddr = location.location() - location.elfReloc();
using namespace Dwarf;
DWARFReader r(cfi->io, fde->instructions, fde->end);
auto iter = location.dwarf()->callFrameForAddr.find(objaddr);
if (iter == location.dwarf()->callFrameForAddr.end())
location.dwarf()->callFrameForAddr[objaddr] = cie->execInsns(r, fde->iloc, objaddr);
const CallFrame &dcf = location.dwarf()->callFrameForAddr[objaddr];
// Given the registers available, and the state of the call unwind data,
// calculate the CFA at this point.
Elf::CoreRegisters out;
switch (dcf.cfaValue.type) {
case SAME:
case UNDEF:
case ARCH:
cfa = Elf::getReg(regs, dcf.cfaReg);
break;
case VAL_OFFSET:
case VAL_EXPRESSION:
case REG:
abort();
break;
case OFFSET:
cfa = Elf::getReg(regs, dcf.cfaReg) + dcf.cfaValue.u.offset;
break;
case EXPRESSION: {
ExpressionStack stack;
auto start = dcf.cfaValue.u.expression.offset;
auto end = start + dcf.cfaValue.u.expression.length;
DWARFReader r(location.cfi()->io, start, end);
cfa = stack.eval(p, r, this, location.elfReloc());
break;
}
default:
cfa = -1;
}
auto rarInfo = dcf.registers.find(cie->rar);
for (const auto &entry : dcf.registers) {
const RegisterUnwind &unwind = entry.second;
int regno = entry.first;
switch (unwind.type) {
case ARCH:
#ifdef CFA_RESTORE_REGNO
// "The CFA is defined to be the stack pointer in the calling frame."
if (regno == CFA_RESTORE_REGNO)
Elf::setReg(out, regno, cfa);
else
Elf::setReg(out, regno, Elf::getReg(regs, regno));
break;
#endif
case UNDEF:
case SAME:
Elf::setReg(out, regno, Elf::getReg(regs, regno));
break;
case OFFSET: // XXX: assume addrLen = sizeof Elf_Addr
Elf::setReg(out, regno, p.io->readObj<Elf::Addr>(cfa + unwind.u.offset));
break;
case REG:
Elf::setReg(out, regno, Elf::getReg(out,unwind.u.reg));
break;
case VAL_EXPRESSION:
case EXPRESSION: {
ExpressionStack stack;
stack.push(cfa);
DWARFReader reader(cfi->io, unwind.u.expression.offset,
unwind.u.expression.offset + unwind.u.expression.length);
auto val = stack.eval(p, reader, this, location.elfReloc());
// EXPRESSIONs give an address, VAL_EXPRESSION gives a literal.
if (unwind.type == EXPRESSION)
p.io->readObj(val, &val);
Elf::setReg(out, regno, val);
break;
}
default:
break;
}
}
// If the return address isn't defined, then we can't unwind.
if (rarInfo == dcf.registers.end() || rarInfo->second.type == UNDEF || cfa == 0) {
if (verbose > 1) {
*debug << "DWARF unwinding stopped at "
<< std::hex << location.location() << std::dec
<< ": " <<
(rarInfo == dcf.registers.end() ? "no RAR register found"
: rarInfo->second.type == UNDEF ? "RAR register undefined"
: "null CFA for frame")
<< std::endl;
}
return std::nullopt;
}
// We know the RAR is defined, so make that the instruction pointer in the
// new frame.
if (cie && cie->rar != IPREG)
Elf::setReg(out, IPREG, Elf::getReg(out, cie->rar));
return out;
}
const Elf::MaybeNamedSymbol &
CodeLocation::symbol() const {
if (!symbol_ && dwarf_) {
symbol_ = dwarf_->elf->findSymbolByAddress(location(), STT_NOTYPE);
}
return symbol_;
}
Elf::MaybeNamedSymbol
ProcessLocation::symbol() const {
if (codeloc)
return codeloc->symbol();
return std::nullopt;
}
const Dwarf::FDE *
CodeLocation::fde() const {
if (fde_ == nullptr) {
const Dwarf::CFI *cfip = cfi();
if (cfip != nullptr)
fde_ = cfi()->findFDE(location_);
}
return fde_;
}
const Dwarf::DIE &
CodeLocation::die() const {
if (!die_ && dwarf_) {
Dwarf::Unit::sptr u = dwarf_->lookupUnit(location_);
if (u) {
die_ = u->root().findEntryForAddr(location_, Dwarf::DW_TAG_subprogram);
}
}
return die_;
}
ProcessLocation::ProcessLocation(Process &proc, Elf::Addr address_) {
auto [ elfReloc, elf, phdr ] = proc.findSegment(address_);
auto dwarf = elf ? proc.getDwarf(elf) : nullptr;
if (dwarf) {
codeloc = std::make_shared<CodeLocation>(dwarf, phdr, address_ - elfReloc);
} else {
codeloc = nullptr;
}
location_ = address_;
}
const Dwarf::CIE *
ProcessLocation::cie() const {
const Dwarf::FDE *lfde = fde();
if (lfde == nullptr)
return nullptr;
return &cfi()->getCIE(lfde->cieOff);
}
std::vector<std::pair<std::string, int>>
CodeLocation::source() const
{
if (dwarf_)
return dwarf_->sourceFromAddr(location_);
return {};
}
std::vector<std::pair<std::string, int>>
ProcessLocation::source() const {
return codeloc ? codeloc->source() : std::vector<std::pair<std::string, int>>();
}
CodeLocation::CodeLocation() : location_(0), phdr_(nullptr), cie_(nullptr), fde_(nullptr), cfi_(nullptr) {
}
CodeLocation::CodeLocation(Dwarf::Info::sptr info, const Elf::Phdr *phdr, Elf::Addr off)
: location_(off)
, dwarf_(std::move(info)), phdr_(phdr), cie_(nullptr), fde_(nullptr), cfi_(nullptr)
{
}
Dwarf::CFI *
CodeLocation::cfi() const {
Dwarf::CFI *cfi = dwarf_->getCFI();
if (cfi == nullptr)
return nullptr;
return cfi;
}
const Dwarf::CFI *
ProcessLocation::cfi() const {
if (codeloc == nullptr)
return nullptr;
return codeloc->cfi();
}
}