-
Notifications
You must be signed in to change notification settings - Fork 37
/
process.cc
1162 lines (1063 loc) · 39.9 KB
/
process.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <features.h>
#include <link.h>
#include <unistd.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <set>
#include <sys/ucontext.h>
#include <sys/wait.h>
#include <csignal>
#include "libpstack/archreg.h"
#include "libpstack/dwarf.h"
#include "libpstack/proc.h"
#include "libpstack/global.h"
#include "libpstack/stringify.h"
#include "libpstack/ioflag.h"
#if defined(__amd64__)
#define BP(regs) (regs.rbp)
#define SP(regs) (regs.rsp)
#define IP(regs) (regs.rip)
#elif defined(__i386__)
#define BP(regs) regs.ebp
#define SP(regs) regs.esp
#define IP(regs) (regs.eip)
#elif defined(__aarch64__)
#define IP(regs) (regs.pc)
#endif
using namespace pstack;
std::ostream &
operator << (std::ostream &os, const JSON<std::pair<std::string, int>> &jt)
{
return JObject(os)
.field("file", jt.object.first)
.field("line", jt.object.second);
}
std::ostream &
operator << (std::ostream &os, const JSON<std::pair<Elf::Sym, std::string>> &js)
{
const auto &obj = js.object;
return JObject(os)
.field("st_name", obj.second)
.field("st_value", obj.first.st_value)
.field("st_size", obj.first.st_size)
.field("st_info", int(obj.first.st_info))
.field("st_other", int(obj.first.st_other))
.field("st_shndx", obj.first.st_shndx);
}
std::ostream &
operator << (std::ostream &os, const JSON<Procman::ProcessLocation, Procman::Process *> &)
{
return os;
}
std::ostream &
operator << (std::ostream &os, const JSON<Procman::StackFrame, Procman::Process *> &jt);
std::ostream &
operator << (std::ostream &os, const JSON<Procman::ThreadStack, Procman::Process *> &ts)
{
return JObject(os)
.field("ti_tid", ts.object.info.ti_tid)
.field("ti_lid", ts.object.info.ti_lid)
.field("ti_type", ts.object.info.ti_type)
.field("ti_pri", ts.object.info.ti_pri)
.field("ti_stack", ts.object.stack, ts.context);
}
namespace std {
bool
operator < (const std::pair<Elf::Addr, Elf::Object::sptr> &entry, Elf::Addr addr) {
return entry.first < addr;
}
}
template <typename ctx>
std::ostream &
operator << (std::ostream &os, const JSON<td_thr_type_e, ctx> &jt)
{
switch (jt.object) {
case TD_THR_ANY_TYPE: return os << json("TD_THR_ANY_TYPE");
case TD_THR_USER: return os << json("TD_THR_USER");
case TD_THR_SYSTEM: return os << json("TD_THR_SYSTEM");
default: return os << json("unknown type");
}
}
namespace pstack::Procman {
/*
* convert a gregset_t to an Elf::CoreRegs
*/
#ifndef __aarch64__
void
gregset2core(Elf::CoreRegisters &core, const gregset_t greg) {
#if defined(__i386__)
core.edi = greg[REG_EDI];
core.esi = greg[REG_ESI];
core.ebp = greg[REG_EBP];
core.esp = greg[REG_ESP];
core.ebx = greg[REG_EBX];
core.edx = greg[REG_EDX];
core.ecx = greg[REG_ECX];
core.eax = greg[REG_EAX];
core.eip = greg[REG_EIP];
#elif defined(__amd64__)
core.r8 = greg[REG_R8];
core.r9 = greg[REG_R9];
core.r10 = greg[REG_R10];
core.r11 = greg[REG_R11];
core.r12 = greg[REG_R12];
core.r13 = greg[REG_R13];
core.r14 = greg[REG_R14];
core.r15 = greg[REG_R15];
core.rdi = greg[REG_RDI];
core.rsi = greg[REG_RSI];
core.rbp = greg[REG_RBP];
core.rbx = greg[REG_RBX];
core.rdx = greg[REG_RDX];
core.rax = greg[REG_RAX];
core.rcx = greg[REG_RCX];
core.rsp = greg[REG_RSP];
core.rip = greg[REG_RIP];
#elif defined(__arm__)
// ARM has unfied types for NT_PRSTATUS and ucontext, and the offsets are
// actually the DWARF register numbers, too.
for (int i = 0; i < ELF_NGREG)
core.regs[i] = greg[i];
#endif
}
#endif
Process::Process(Elf::Object::sptr exec, Reader::sptr memory,
const PstackOptions &options, Dwarf::ImageCache &cache)
: entry(0)
, interpBase(0)
, vdsoBase(0)
, agent(nullptr)
, execImage(std::move(exec))
, options(options)
, sysent(0)
, imageCache(cache)
, io(std::move(memory))
{
if (execImage)
entry = execImage->getHeader().e_entry;
}
void
Process::load()
{
/*
* Attach the executable and any shared libs.
* The process is still running here, but unless its actively loading or
* unload a shared library, this relatively safe, and saves us a lot of
* work while the process is stopped.
*/
StopProcess here(this);
auto auxv = getAUXV();
if (auxv)
processAUXV(*auxv);
if (!execImage)
throw (Exception() << "no executable image located for process");
try {
Elf::Addr r_debug_addr = findRDebugAddr();
bool isStatic = r_debug_addr == 0 || r_debug_addr == Elf::Addr(-1);
if (isStatic)
addElfObject("", execImage, 0);
else
loadSharedObjects(r_debug_addr);
}
catch (const Exception &) {
// We were unable to read the link map.
// The primary cause is that the core file is truncated.
// Go do the Hail Mary version.
if (loadSharedObjectsFromFileNote())
return;
throw;
}
if (!options.nothreaddb) {
td_err_e the;
the = td_ta_new(this, &agent);
if (the != TD_OK) {
agent = nullptr;
if (verbose > 0 && the != TD_NOLIBTHREAD)
*debug << "failed to load thread agent: " << the << std::endl;
}
}
}
Dwarf::Info::sptr
Process::getDwarf(Elf::Object::sptr elf) const
{
return imageCache.getDwarf(elf);
}
const char *
auxtype2str(int auxtype) {
#define AUX_TYPE(t, v) if (auxtype == t) return #t;
#include "libpstack/elf/auxv.h"
return "unknown type";
#undef AUX_TYPE
}
void
Process::processAUXV(const Reader &auxio)
{
try {
for (auto &aux : ReaderArray<Elf::auxv_t>(auxio)) {
Elf::Addr hdr = aux.a_un.a_val;
switch (aux.a_type) {
case AT_ENTRY: {
if (verbose > 2)
*debug << "auxv: AT_ENTRY=" << hdr << std::endl;
// this provides a reference for relocating the executable when
// compared to the entrypoint there.
entry = hdr;
break;
}
case AT_SYSINFO: {
if (verbose > 2)
*debug << "auxv:AT_SYSINFO=" << hdr << std::endl;
sysent = hdr;
break;
}
case AT_SYSINFO_EHDR: {
try {
auto elf = std::make_shared<Elf::Object>(imageCache, io->view("(vdso image)", hdr, 65536));
vdsoBase = hdr;
addElfObject("(vdso image)", elf, hdr);
vdsoImage = elf;
if (verbose >= 2) {
*debug << "auxv: VDSO " << *elf->io
<< " loaded at " << std::hex << hdr << std::dec << "\n";
}
}
catch (const std::exception &ex) {
if (debug)
*debug << "auxv: warning: failed to load DSO: " << ex.what() << "\n";
}
break;
}
case AT_BASE:
if (verbose > 2)
*debug << "auxv: AT_BASE=" << hdr << std::endl;
interpBase = hdr;
break;
#ifdef AT_EXECFN
case AT_EXECFN: {
if (verbose > 2)
*debug << "auxv: AT_EXECFN=" << hdr << std::endl;
try {
auto exeName = io->readString(hdr);
if (verbose >= 2)
*debug << "filename from auxv: " << exeName << "\n";
if (!execImage) {
execImage = imageCache.getImageForName(exeName);
if (entry == 0)
entry = execImage->getHeader().e_entry;
}
}
catch (const Exception &ex) {
*debug << "failed to read AT_EXECFN: " << ex.what() << std::endl;
}
break;
}
#endif
default:
if (verbose > 2)
*debug << "auxv: " << auxtype2str( aux.a_type) << ": " << hdr << std::endl;
}
}
} catch (const std::exception &ex) {
if (verbose)
*debug << "exception while reading auxv: " << ex.what() << "\n";
}
}
static bool
buildDIEName(std::ostream &os, const Dwarf::DIE &die, bool first=true) {
// use the specification or abstract origin DIE instead of this if we have one.
auto spec = die.attribute(Dwarf::DW_AT_specification);
if (spec.valid())
return buildDIEName(os, Dwarf::DIE(spec), first);
auto origin = die.attribute(Dwarf::DW_AT_abstract_origin);
if (origin.valid())
return buildDIEName(os, Dwarf::DIE(origin), first);
// Don't walk up past compile units.
if (die.tag() == Dwarf::DW_TAG_compile_unit || die.tag() == Dwarf::DW_TAG_partial_unit)
return false;
auto parent = die.getParentOffset();
bool printedParent = buildDIEName(os, die.getUnit()->offsetToDIE(Dwarf::DIE(), parent), false);
auto tag = die.tag();
if (first ||
tag == Dwarf::DW_TAG_structure_type ||
tag == Dwarf::DW_TAG_class_type ||
tag == Dwarf::DW_TAG_namespace ) {
if (printedParent)
os << "::";
os << die.name();
return true;
}
return printedParent;
}
PrintableFrame::PrintableFrame(Process &proc, const StackFrame &frame)
: proc(proc)
, functionOffset(std::numeric_limits<Elf::Addr>::max())
, frame(frame)
{
auto location = frame.scopeIP(proc);
if (location.elf() == nullptr)
return;
Elf::Addr objIp = location.objLocation();
if (!proc.options.nodienames) {
auto function = location.die();
if (function) {
std::ostringstream sos;
buildDIEName(sos, function);
this->dieName = sos.str();
auto lowpc = function.attribute(Dwarf::DW_AT_low_pc);
if (lowpc.valid()) {
functionOffset = objIp - uintmax_t(lowpc);
} else {
const auto &ranges = function.getRanges();
if (ranges) {
functionOffset = objIp - (*ranges)[0].first;
} else {
// no function start address - we'll try and find it
// below in the ELF fallback code.
}
}
while (function) {
auto inl = function.findEntryForAddr(objIp, Dwarf::DW_TAG_inlined_subroutine);
if (!inl)
break;
inlined.push_back(inl);
function = std::move(inl);
}
}
}
if (functionOffset == std::numeric_limits<Elf::Addr>::max()) {
// If we have not worked out the start of the function, then we
// either didn't find the DIE for the function, or it didn't have
// enough info to find the first address.
//
// Fall back to using the ELF symbol instead.
auto maybesym = location.symbol();
if (maybesym)
functionOffset = objIp - maybesym->first.st_value;
}
}
Dwarf::DIE removeCV(Dwarf::DIE type) {
while (type &&
(type.tag() == Dwarf::DW_TAG_typedef
|| type.tag() == Dwarf::DW_TAG_const_type
|| type.tag() == Dwarf::DW_TAG_volatile_type))
type = Dwarf::DIE(type.attribute(Dwarf::DW_AT_type));
return type;
}
struct ArgPrint {
Process &p;
const StackFrame &frame;
ArgPrint(Process &p_, const StackFrame &frame_)
: p(p_), frame(frame_) {}
};
using namespace Dwarf;
struct RemoteValue {
const Process &p;
const Elf::Addr addr;
const DIE type;
std::vector<char> buf;
std::string error;
RemoteValue(const Process &p_, Elf::Addr addr_, bool isValue, DIE type_)
: p(p_)
, addr(addr_)
, type(removeCV( std::move(type_)) ) {
if (isValue) {
buf.resize(sizeof addr_);
memcpy(&buf[0], &addr_, sizeof addr_);
} else {
auto sizeAttr = type.attribute(DW_AT_byte_size);
size_t size;
if (sizeAttr.valid()) {
size = uintmax_t(sizeAttr);
} else if (type.tag() == DW_TAG_reference_type || type.tag() == DW_TAG_pointer_type) {
size = sizeof (void *);
} else {
size = 0;
}
if (!size) {
error = "<no size for type>";
} else {
buf.resize(size);
auto rc = p.io->read(addr, size, &buf[0]);
if (rc != size) {
error = "<failed to read from remote>";
}
}
}
}
};
std::ostream &
operator << (std::ostream &os, const RemoteValue &rv)
{
using namespace Dwarf;
if (rv.addr == 0)
return os << "(null)";
IOFlagSave _(os);
switch (rv.type.tag()) {
case DW_TAG_base_type: {
auto encoding = rv.type.attribute(DW_AT_encoding);
if (!encoding.valid())
throw (Exception() << "no encoding specified for base type");
union {
const int8_t *int8;
const int16_t *int16;
const int32_t *int32;
const int64_t *int64;
const float *float_;
const double *double_;
const void **voidp;
const char *cp;
} u;
u.cp = &rv.buf[0];
switch (uintmax_t(encoding)) {
case DW_ATE_address:
os << *u.voidp;
break;
case DW_ATE_boolean:
for (size_t i = 0;; ++i) {
if (i == rv.buf.size()) {
os << "false";
break;
}
if (rv.buf[i] != 0) {
os << "true";
break;
}
}
break;
case DW_ATE_signed:
case DW_ATE_signed_char:
switch (rv.buf.size()) {
case sizeof (int8_t):
os << *u.int8;
break;
case sizeof (int16_t):
os << *u.int16;
break;
case sizeof (int32_t):
os << *u.int32;
break;
case sizeof (int64_t):
os << *u.int64;
break;
default:
goto unknown;
}
break;
case DW_ATE_unsigned:
case DW_ATE_unsigned_char:
switch (rv.buf.size()) {
case sizeof (uint8_t):
os << *u.int8;
break;
case sizeof (uint16_t):
os << *u.int16;
break;
case sizeof (uint32_t):
os << *u.int32;
break;
case sizeof (uint64_t):
os << *u.int64;
break;
default:
goto unknown;
}
break;
case DW_ATE_float:
switch (rv.buf.size()) {
case sizeof(double):
os << *u.double_;
break;
case sizeof(float):
os << *u.float_;
break;
}
break;
unknown:
default:
os << "<unknown value type>";
break;
}
break;
}
case DW_TAG_reference_type:
case DW_TAG_pointer_type: {
auto ptr = *(Elf::Addr *)&rv.buf[0];
os << (void *)ptr;
auto reftype = removeCV(DIE(rv.type.attribute(DW_AT_type)));
if (reftype) {
if (reftype.name() == "char") {
std::string s = rv.p.io->readString(ptr);
os << " \"" << s << "\"";
} else {
if (ptr == 0)
os << "->nullptr";
else
os << "->" << RemoteValue(rv.p, ptr, false, reftype);
break;
}
}
break;
}
default:
os << "<unprintable type " << rv.type.name() << ">";
}
return os;
}
std::ostream &
operator << (std::ostream &os, const ArgPrint &ap)
{
ProcessLocation location = ap.frame.scopeIP(ap.p);
if (!location.die() || !ap.p.options.doargs)
return os;
using namespace Dwarf;
const char *sep = "";
for (auto child : location.die().children()) {
switch (child.tag()) {
case DW_TAG_formal_parameter: {
auto name = child.name();
auto type = DIE(child.attribute(DW_AT_type));
Elf::Addr addr = 0;
os << sep << name;
if (type) {
auto attr = child.attribute(Dwarf::DW_AT_location);
if (attr.valid()) {
ExpressionStack fbstack;
addr = fbstack.eval(ap.p, attr, &ap.frame, location.elfReloc());
os << "=";
try {
os << RemoteValue(ap.p, addr, fbstack.isValue, type);
}
catch (const Exception &ex) {
os << "<" << ex.what() << ">";
}
} else {
auto constVal = child.attribute(Dwarf::DW_AT_const_value);
if (constVal.valid())
os << "=" << intmax_t(constVal);
}
}
sep = ", ";
break;
}
default:
break;
}
}
return os;
}
std::ostream &operator << (std::ostream &os, UnwindMechanism mech) {
switch (mech) {
case UnwindMechanism::MACHINEREGS: return os << "machine registers";
case UnwindMechanism::DWARF: return os << "DWARF";
case UnwindMechanism::FRAMEPOINTER: return os << "frame pointer";
case UnwindMechanism::BAD_IP_RECOVERY: return os << "popped faulting IP";
case UnwindMechanism::TRAMPOLINE: return os << "signal trampoline";
case UnwindMechanism::LOGFILE: return os << "log file";
case UnwindMechanism::INVALID: return os << "invalid";
}
abort();
}
std::ostream &
Process::dumpStackText(std::ostream &os, const ThreadStack &thread)
{
os << std::dec;
os << "thread: " << (void *)thread.info.ti_tid << ", lwp: "
<< thread.info.ti_lid << ", type: " << thread.info.ti_type << "\n";
int frameNo = 0;
for (auto &frame : thread.stack)
dumpFrameText(os, frame, frameNo++);
return os;
}
std::ostream &
Process::dumpFrameText(std::ostream &os, const StackFrame &frame, int frameNo)
{
IOFlagSave _(os);
PrintableFrame pframe(*this, frame);
ProcessLocation location = frame.scopeIP(*this);
std::vector<std::pair<std::string, int>> source;
if (!options.nosrc)
source = location.source();
std::pair<std::string, int> src = source.size()
? source[0]
: std::make_pair( "", std::numeric_limits<Elf::Addr>::max());
if (!options.nodienames) {
// inlining comes from DIEs with DW_TAG_inlined_subroutine - so no
// point in trying this without DIE names
for (auto i = pframe.inlined.rbegin(); i != pframe.inlined.rend(); ++i) {
os << "#"
<< std::left << std::setw(2) << std::setfill(' ') << frameNo << " "
<< std::setw(ELF_BITS/4 + 2) << std::setfill(' ')
<< "inlined";
os << " in ";
buildDIEName(os, *i);
if (!options.nosrc) {
const auto &lineinfo = i->getUnit()->getLines();
if (lineinfo) {
os << " at " << src.first << ":" << src.second;
auto &fileEnt = lineinfo->files[intmax_t(i->attribute(Dwarf::DW_AT_call_file))];
auto &dirname = lineinfo->directories[fileEnt.dirindex];
const auto &name = verbose ? dirname + "/" + fileEnt.name : fileEnt.name;
src = std::make_pair( name, intmax_t(i->attribute(Dwarf::DW_AT_call_line)));
os << "\n";
}
}
}
}
os << "#"
<< std::left << std::setw(2) << std::setfill(' ') << frameNo << " "
<< std::right << "0x" << std::hex << std::setw(ELF_BITS/4) << std::setfill('0')
<< frame.rawIP() << std::dec;
if (location.inObject()) {
std::string name;
std::string flags = "";
if (frame.isSignalTrampoline)
flags += "*";
auto sym = location.symbol();
if (pframe.dieName != "") {
name = pframe.dieName;
} else if (sym) {
name = sym->second;
flags += location.die() ? "%" : "!";
} else {
name = "<unknown>";
}
os << " in "
<< name
<< flags
<< "(" << ArgPrint(*this, frame) << ")";
if (pframe.functionOffset != std::numeric_limits<Elf::Addr>::max())
os << "+" << pframe.functionOffset;
os << " in " << stringify(*location.elf()->io);
if (verbose)
os << "@0x" << std::hex << frame.rawIP() - location.elfReloc() << std::dec;
if (src.first != "")
os << " at " << src.first << ":" << std::dec << src.second;
} else {
os << " no information for frame";
}
if (verbose)
os << " via " << frame.mechanism;
os << "\n";
return os;
}
void
Process::addElfObject(std::string_view name, const Elf::Object::sptr &obj, Elf::Addr load)
{
objects.emplace(std::make_pair(load, MappedObject{ name, obj }));
if (verbose >= 2) {
IOFlagSave _(*debug);
*debug << "object " << name << " loaded at address "
<< std::hex << load << std::dec << std::endl;
}
}
/*
* Grovel through the rtld's internals to find any shared libraries.
*/
void
Process::loadSharedObjects(Elf::Addr rdebugAddr)
{
struct r_debug rDebug;
io->readObj(rdebugAddr, &rDebug);
/* Iterate over the r_debug structure's entries, loading libraries */
struct link_map map;
for (auto mapAddr = Elf::Addr(rDebug.r_map); mapAddr != 0; mapAddr = Elf::Addr(map.l_next)) {
io->readObj(mapAddr, &map);
// If we see the executable, just add it in and avoid going through the path
// replacement work
if (mapAddr == Elf::Addr(rDebug.r_map)) {
auto loadAddr = entry - execImage->getHeader().e_entry;
if (loadAddr != map.l_addr) {
*debug << "calculated load address for executable from process entrypoint ("
<< std::hex << loadAddr << ") does not match link map (" << map.l_addr
<< "). Trusting link-map\n" << std::dec;
}
addElfObject("(exe)", execImage, map.l_addr);
continue;
}
// If we've loaded the VDSO, and we see it in the link map, just skip it.
if (vdsoBase != 0 && map.l_addr == vdsoBase)
continue;
// Read the path to the file
if (map.l_name == 0)
continue;
std::string path = io->readString(Elf::Off(map.l_name));
if (path == "")
continue;
try {
addElfObject(path, nullptr, Elf::Addr(map.l_addr));
}
catch (const std::exception &e) {
if (debug)
*debug << "warning: can't load text for '" << path << "' at " <<
(void *)mapAddr << "/" << (void *)map.l_addr << ": " << e.what() << "\n";
continue;
}
}
}
Elf::Addr
Process::findRDebugAddr()
{
/*
* Calculate the address the executable was loaded at - we know the entry
* supplied by the kernel, and also the executable's desired entrypoint -
* the difference is the load address.
*/
Elf::Off loadAddr = entry - execImage->getHeader().e_entry;
// Find DT_DEBUG in the process's dynamic section.
for (auto &segment : execImage->getSegments(PT_DYNAMIC)) {
// Read from the process, not the executable - the linker will have updated the content.
auto dynReader = io->view("PT_DYNAMIC segment", segment.p_vaddr + loadAddr, segment.p_filesz);
ReaderArray<Elf::Dyn> dynamic(*dynReader);
for (auto &dyn : dynamic)
if (dyn.d_tag == DT_DEBUG && dyn.d_un.d_ptr != 0)
return dyn.d_un.d_ptr;
}
/*
* If there's no DT_DEBUG, we've probably got someone executing a shared
* library, which doesn't have an _r_debug symbol. Use the address of
* _r_debug in the interpreter
*/
if (interpBase && execImage->getInterpreter() != "") {
try {
addElfObject(execImage->getInterpreter(), nullptr, interpBase);
return resolveSymbol("_r_debug", false,
[this](const std::string_view name) {
return execImage->getInterpreter() == name;
});
}
catch (...) {
}
}
return 0;
}
std::tuple<Elf::Addr, Elf::Object::sptr, const Elf::Phdr *>
Process::findSegment(Elf::Addr addr)
{
auto it = objects.lower_bound(addr);
if (it != objects.begin()) {
--it;
auto obj = it->second.object(imageCache);
if (it->first + obj->endVA() >= addr) {
auto segment = obj->getSegmentForAddress(addr - it->first);
if (segment)
return std::make_tuple(it->first, obj, segment);
}
}
return std::tuple<Elf::Addr, Elf::Object::sptr, const Elf::Phdr *>();
}
std::tuple<Elf::Object::sptr, Elf::Addr, Elf::Sym>
Process::resolveSymbolDetail(const char *name, bool includeDebug,
std::function<bool(std::string_view)> match)
{
for (auto &loaded : objects) {
if (!match(loaded.second.name()))
continue;
auto obj = loaded.second.object(imageCache);
auto [sym,idx] = obj->findDynamicSymbol(name);
if (sym.st_shndx != SHN_UNDEF)
return std::make_tuple(obj, loaded.first, sym);
if (includeDebug) {
auto [sym, idx] = loaded.second.object(imageCache)->findDebugSymbol(name);
if (sym.st_shndx != SHN_UNDEF)
return std::make_tuple(obj, loaded.first, sym);
}
}
throw (Exception() << "symbol " << name << " not found");
}
Elf::Addr
Process::resolveSymbol(const char *name, bool includeDebug,
std::function<bool(std::string_view)> match)
{
auto info = resolveSymbolDetail(name, includeDebug, match);
return std::get<1>(info) + std::get<2>(info).st_value;
}
Process::~Process()
{
// don't leave the VDSO in the cache - a new copy will be entered for a new
// process.
imageCache.flush(vdsoImage);
td_ta_delete(agent);
}
void
ThreadStack::unwind(Process &p, Elf::CoreRegisters ®s)
{
stack.clear();
stack.reserve(20);
#ifdef __aarch64__
// for ARM, if we see __kernel_rt_sigreturn on the stack, we have a signal
// stack frame
Elf::Addr trampoline = 0;
if (p.vdsoImage) {
auto [sigreturnSym,idx] = p.vdsoImage->findDynamicSymbol("__kernel_rt_sigreturn");
if (sigreturnSym.st_shndx != SHN_UNDEF) {
trampoline = sigreturnSym.st_value + p.vdsoBase;
}
}
#endif
try {
stack.emplace_back(UnwindMechanism::MACHINEREGS, regs);
// Set up the first frame using the machine context registers
stack.front().setCoreRegs(regs);
for (int frameCount = 0; frameCount < p.options.maxframes; frameCount++) {
auto &prev = stack.back();
try {
auto maybeNewRegs = prev.unwind(p);
if (!maybeNewRegs)
break;
auto &newRegs = *maybeNewRegs;
stack.emplace_back(UnwindMechanism::DWARF, newRegs);
#ifdef __aarch64__
auto &cur = stack.back();
if (newRegs.pc == trampoline)
cur.isSignalTrampoline = true;
#endif
}
catch (const std::exception &ex) {
if (verbose > 2)
*debug << "failed to unwind frame with DWARF: "
<< ex.what() << std::endl;
// Some machine specific methods of unwinding if DWARF fails.
// if we're the top-of-stack, or there's a signal handler just
// above, and the instruction pointer in the current frame
// doesn't look like it comes from a code segment, then there's
// a strong likelihood that we jumped to an invalid location
// from an indirect call. The only action carried out for the
// frame is that the call instruction pushed the return address
// onto the stack. The calling frame is an exact copy of the
// called one, but with the instruction pointer read from the
// TOS, and the stack pointer adjusted.
//
// If we're wrong here, it's possible we do worse than we would
// have done had we fallen down to frame pointer unwinding, but
// we'd need to be executing an instruction in a piece of
// runtime-generated code, or something else that wasn't in a
// normal ELF phdr, so it seems more likely this is the best
// thing to do.
//
// For ARM, the concept is the same, but we look at the link
// register rather than a pushd return address
if (stack.size() == 1 || stack[stack.size() - 2].isSignalTrampoline) {
ProcessLocation badip = { p, IP(prev.regs) };
if (!badip.inObject() || (badip.codeloc->phdr().p_flags & PF_X) == 0) {
auto newRegs = prev.regs; // start with a copy of prev frames regs.
#if defined(__amd64__) || defined(__i386__)
// get stack pointer in the current frame, and read content of TOS
auto sp = SP(prev.regs);
Elf::Addr ip;
auto in = p.io->read(sp, sizeof ip, (char *)&ip);
if (in == sizeof ip) {
SP(newRegs) = sp + sizeof ip;
IP(newRegs) = ip; // .. insn pointer.
stack.emplace_back(UnwindMechanism::BAD_IP_RECOVERY, newRegs);
continue;
}
#elif defined(__aarch64__)
newRegs.pc = prev.regs.regs[30]; // Copy old link register into new instruction pointer.
stack.emplace_back(UnwindMechanism::BAD_IP_RECOVERY, newRegs);
continue;
#endif
}
}
#if defined(__aarch64__)
// Deal with unwinding through an ARM signal handler
if (trampoline && trampoline == prev.rawIP()) {
// the stack pointer is pointing directly at rt_sigframe. This is
// as per arch/arm64/kernel/signal.c
struct rt_sigframe {
siginfo_t si;
ucontext_t uc;
};
auto sigframe = p.io->readObj<rt_sigframe>(prev.regs.sp);
Elf::CoreRegisters newRegs;
for (int i = 0; i < 31; ++i)
newRegs.regs[i] = sigframe.uc.uc_mcontext.regs[i];
newRegs.sp = sigframe.uc.uc_mcontext.sp;
newRegs.pc = sigframe.uc.uc_mcontext.pc;
stack.emplace_back(UnwindMechanism::TRAMPOLINE, newRegs);
continue;
}
#endif
#if defined(__i386__)
// Deal with signal trampolines for i386
Elf::Addr reloc;
const Elf::Phdr *segment;
Elf::Object::sptr obj;
std::tie(reloc, obj, segment) = p.findSegment(prev.rawIP());
if (obj) {
Elf::Addr sigContextAddr = 0;
auto objip = prev.rawIP() - reloc;
// Find the gregset on the stack - it differs depending on
// whether this is realtime or "classic" frame
auto [restoreSym,idx] = obj->findDebugSymbol("__restore");
if (restoreSym.st_shndx != SHN_UNDEF && objip == restoreSym.st_value)
sigContextAddr = SP(prev.regs) + 4;
else {
auto [restoreRtSym,idx] = obj->findDebugSymbol("__restore_rt");
if (restoreRtSym.st_shndx != SHN_UNDEF && objip == restoreRtSym.st_value)
sigContextAddr = p.io->readObj<Elf::Addr>(SP(prev.regs) + 8) + 20;
}
if (sigContextAddr != 0) {
// This mapping is based on DWARF regnos, and ucontext.h
gregset_t regs;
p.io->readObj(sigContextAddr, ®s);
Elf::CoreRegisters core;
gregset2core(core, regs);
stack.emplace_back(UnwindMechanism::TRAMPOLINE, core);
continue;
}
}
#endif
#if defined(__i386__) || defined(__amd64__)
// frame-pointer unwinding.
// Use ebp/rbp to find return address and saved BP.