-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlib.cpp
More file actions
530 lines (462 loc) · 16.9 KB
/
lib.cpp
File metadata and controls
530 lines (462 loc) · 16.9 KB
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
#include <inttypes.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
// #include <android-base/logging.h>
#if defined(USE_BIONIC_UAPI_HEADERS)
#include <uapi/asm-arm/asm/perf_regs.h>
#undef PERF_REG_EXTENDED_MASK
#include <uapi/asm-x86/asm/perf_regs.h>
#undef PERF_REG_EXTENDED_MASK
#include <uapi/asm-riscv/asm/perf_regs.h>
#undef PERF_REG_EXTENDED_MASK
#define perf_event_arm_regs perf_event_arm64_regs
#include <uapi/asm-arm64/asm/perf_regs.h>
#undef PERF_REG_EXTENDED_MASK
#else
#include <asm-arm/asm/perf_regs.h>
#undef PERF_REG_EXTENDED_MASK
#include <asm-x86/asm/perf_regs.h>
#undef PERF_REG_EXTENDED_MASK
#include <asm-riscv/asm/perf_regs.h>
#undef PERF_REG_EXTENDED_MASK
#define perf_event_arm_regs perf_event_arm64_regs
#include <asm-arm64/asm/perf_regs.h>
#undef PERF_REG_EXTENDED_MASK
#endif
#include <linux/perf_event.h>
#include <unwindstack/Unwinder.h>
#include <unwindstack/MachineArm.h>
#include <unwindstack/MachineArm64.h>
#include <unwindstack/MachineX86.h>
#include <unwindstack/MachineX86_64.h>
#include <unwindstack/Maps.h>
#include <unwindstack/RegsArm.h>
#include <unwindstack/RegsArm64.h>
#include <unwindstack/RegsX86.h>
#include <unwindstack/RegsX86_64.h>
#include <unwindstack/UserArm.h>
#include <unwindstack/UserArm64.h>
#include <unwindstack/UserX86.h>
#include <unwindstack/UserX86_64.h>
#include <unwindstack/Maps.h>
#include <unwindstack/Arch.h>
#include <unwindstack/AndroidUnwinder.h>
// Use the demangler from libc++.
extern "C" char* __cxa_demangle(const char*, char*, size_t*, int* status);
namespace unwinddaemon {
class MyAndroidRemoteUnwinder : public unwindstack::AndroidRemoteUnwinder {
public:
MyAndroidRemoteUnwinder(pid_t pid, bool show_pc)
: unwindstack::AndroidRemoteUnwinder(pid), show_pc(show_pc) {}
void SetArch(unwindstack::ArchEnum arch) { arch_ = arch; };
void SetMaxFrames(size_t max_frames) { max_frames_ = max_frames; };
void SetDisplayBuildID(bool display_build_id) { display_build_id_ = display_build_id; }
std::string FormatFrame(const unwindstack::FrameData& frame) const;
virtual ~MyAndroidRemoteUnwinder() = default;
protected:
bool InternalInitialize(unwindstack::ErrorData& error) override;
bool show_pc = false;
bool display_build_id_ = false;
};
bool MyAndroidRemoteUnwinder::InternalInitialize(unwindstack::ErrorData& error) {
// 初始化的时候已经处理了 arch_
maps_.reset(new unwindstack::RemoteMaps(pid_));
if (!maps_->Parse()) {
error.code = unwindstack::ERROR_MAPS_PARSE;
return false;
}
if (process_memory_ == nullptr) {
process_memory_ = unwindstack::Memory::CreateProcessMemoryCached(pid_);
}
return true;
}
std::string MyAndroidRemoteUnwinder::FormatFrame(const unwindstack::FrameData& frame) const {
std::string data;
if (show_pc) {
if (ArchIs32Bit(arch_)) {
data += android::base::StringPrintf(" #%02zu pc %08" PRIx64 " %08" PRIx64, frame.num, frame.pc, frame.rel_pc);
} else {
data += android::base::StringPrintf(" #%02zu pc %016" PRIx64 " %016" PRIx64, frame.num, frame.pc, frame.rel_pc);
}
} else {
if (ArchIs32Bit(arch_)) {
data += android::base::StringPrintf(" #%02zu pc %08" PRIx64, frame.num, frame.rel_pc);
} else {
data += android::base::StringPrintf(" #%02zu pc %016" PRIx64, frame.num, frame.rel_pc);
}
}
auto map_info = frame.map_info;
if (map_info == nullptr) {
// No valid map associated with this frame.
data += " <unknown>";
} else if (!map_info->name().empty()) {
data += " ";
data += map_info->GetFullName();
} else {
data += android::base::StringPrintf(" <anonymous:%" PRIx64 ">", map_info->start());
}
if (map_info != nullptr && map_info->elf_start_offset() != 0) {
data += android::base::StringPrintf(" (offset 0x%" PRIx64 ")", map_info->elf_start_offset());
}
if (!frame.function_name.empty()) {
char* demangled_name = __cxa_demangle(frame.function_name.c_str(), nullptr, nullptr, nullptr);
if (demangled_name == nullptr) {
data += " (";
data += frame.function_name;
} else {
data += " (";
data += demangled_name;
free(demangled_name);
}
if (frame.function_offset != 0) {
data += android::base::StringPrintf("+%" PRId64, frame.function_offset);
}
data += ')';
}
if (map_info != nullptr && display_build_id_) {
std::string build_id = map_info->GetPrintableBuildID();
if (!build_id.empty()) {
data += " (BuildId: " + build_id + ')';
}
}
return data;
}
class UnwinderWithPC : public unwindstack::Unwinder {
public:
UnwinderWithPC(size_t max_frames, unwindstack::Maps* maps, unwindstack::Regs* regs, std::shared_ptr<unwindstack::Memory> process_memory, bool show_pc)
: unwindstack::Unwinder(max_frames, maps, regs, process_memory), show_pc(show_pc) {}
virtual ~UnwinderWithPC() = default;
bool Init();
std::string FormatFrame(size_t frame_num) const;
protected:
bool show_pc = false;
};
std::string UnwinderWithPC::FormatFrame(size_t frame_num) const {
if (frame_num >= frames_.size()) {
return "";
}
const unwindstack::FrameData& frame = frames_[frame_num];
std::string data;
if (show_pc) {
if (ArchIs32Bit(arch_)) {
data += android::base::StringPrintf(" #%02zu pc %08" PRIx64 " %08" PRIx64, frame.num, frame.pc, frame.rel_pc);
} else {
data += android::base::StringPrintf(" #%02zu pc %016" PRIx64 " %016" PRIx64, frame.num, frame.pc, frame.rel_pc);
}
} else {
if (ArchIs32Bit(arch_)) {
data += android::base::StringPrintf(" #%02zu pc %08" PRIx64, frame.num, frame.rel_pc);
} else {
data += android::base::StringPrintf(" #%02zu pc %016" PRIx64, frame.num, frame.rel_pc);
}
}
auto map_info = frame.map_info;
if (map_info == nullptr) {
// No valid map associated with this frame.
data += " <unknown>";
} else if (!map_info->name().empty()) {
data += " ";
data += map_info->GetFullName();
} else {
data += android::base::StringPrintf(" <anonymous:%" PRIx64 ">", map_info->start());
}
if (map_info != nullptr && map_info->elf_start_offset() != 0) {
data += android::base::StringPrintf(" (offset 0x%" PRIx64 ")", map_info->elf_start_offset());
}
if (!frame.function_name.empty()) {
char* demangled_name = __cxa_demangle(frame.function_name.c_str(), nullptr, nullptr, nullptr);
if (demangled_name == nullptr) {
data += " (";
data += frame.function_name;
} else {
data += " (";
data += demangled_name;
free(demangled_name);
}
if (frame.function_offset != 0) {
data += android::base::StringPrintf("+%" PRId64, frame.function_offset);
}
data += ')';
}
if (map_info != nullptr && display_build_id_) {
std::string build_id = map_info->GetPrintableBuildID();
if (!build_id.empty()) {
data += " (BuildId: " + build_id + ')';
}
}
return data;
}
struct UnwindOption {
uint64_t abi;
uint64_t stack_size;
uint64_t dyn_size;
uint64_t reg_mask;
bool show_pc;
};
enum ArchType {
ARCH_X86_32,
ARCH_X86_64,
ARCH_ARM,
ARCH_ARM64,
ARCH_UNSUPPORTED,
};
class ScopedCurrentArch {
public:
explicit ScopedCurrentArch(ArchType arch) : saved_arch(current_arch) {
current_arch = arch;
}
~ScopedCurrentArch() {
current_arch = saved_arch;
}
static ArchType GetCurrentArch() { return current_arch; }
private:
ArchType saved_arch;
static ArchType current_arch;
};
ArchType ScopedCurrentArch::current_arch = ARCH_ARM64;
struct RegSet {
ArchType arch;
uint64_t valid_mask;
uint64_t data[64];
RegSet(int abi, uint64_t valid_mask, const uint64_t* valid_regs);
bool GetRegValue(size_t regno, uint64_t* value) const;
bool GetSpRegValue(uint64_t* value) const;
};
ArchType GetArchForAbi(ArchType machine_arch, int abi) {
if (abi == PERF_SAMPLE_REGS_ABI_32) {
if (machine_arch == ARCH_X86_64) {
return ARCH_X86_32;
}
if (machine_arch == ARCH_ARM64) {
return ARCH_ARM;
}
} else if (abi == PERF_SAMPLE_REGS_ABI_64) {
if (machine_arch == ARCH_X86_32) {
return ARCH_X86_64;
}
if (machine_arch == ARCH_ARM) {
return ARCH_ARM64;
}
}
return machine_arch;
}
RegSet::RegSet(int abi, uint64_t valid_mask, const uint64_t* valid_regs) : valid_mask(valid_mask) {
arch = GetArchForAbi(ScopedCurrentArch::GetCurrentArch(), abi);
memset(data, 0, sizeof(data));
for (int i = 0, j = 0; i < 64; ++i) {
if ((valid_mask >> i) & 1) {
data[i] = valid_regs[j++];
}
}
// actually, don't need this
// if (ScopedCurrentArch::GetCurrentArch() == ARCH_ARM64 && abi == PERF_SAMPLE_REGS_ABI_32) {
// // The kernel dumps arm64 regs, but we need arm regs. So map arm64 regs into arm regs.
// data[PERF_REG_ARM_PC] = data[PERF_REG_ARM64_PC];
// }
}
bool RegSet::GetRegValue(size_t regno, uint64_t* value) const {
if ((valid_mask >> regno) & 1) {
*value = data[regno];
return true;
}
return false;
}
bool RegSet::GetSpRegValue(uint64_t* value) const {
size_t regno;
switch (arch) {
case ARCH_X86_32:
regno = PERF_REG_X86_SP;
break;
case ARCH_X86_64:
regno = PERF_REG_X86_SP;
break;
case ARCH_ARM:
regno = PERF_REG_ARM_SP;
break;
case ARCH_ARM64:
regno = PERF_REG_ARM64_SP;
break;
default:
return false;
}
return GetRegValue(regno, value);
}
std::string DumpFrames(const UnwinderWithPC& unwinder) {
std::string str;
for (size_t i = 0; i < unwinder.NumFrames(); i++) {
str += unwinder.FormatFrame(i) + "\n";
}
return str;
}
unwindstack::Regs* GetBacktraceRegs(const RegSet& regs) {
switch (regs.arch) {
case ARCH_ARM: {
unwindstack::arm_user_regs arm_user_regs;
memset(&arm_user_regs, 0, sizeof(arm_user_regs));
static_assert(static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0),
"");
static_assert(
static_cast<int>(unwindstack::ARM_REG_LAST) == static_cast<int>(PERF_REG_ARM_MAX), "");
for (size_t i = unwindstack::ARM_REG_R0; i < unwindstack::ARM_REG_LAST; ++i) {
arm_user_regs.regs[i] = static_cast<uint32_t>(regs.data[i]);
}
return unwindstack::RegsArm::Read(&arm_user_regs);
}
case ARCH_ARM64: {
unwindstack::arm64_user_regs arm64_user_regs;
memset(&arm64_user_regs, 0, sizeof(arm64_user_regs));
static_assert(
static_cast<int>(unwindstack::ARM64_REG_R0) == static_cast<int>(PERF_REG_ARM64_X0), "");
static_assert(
static_cast<int>(unwindstack::ARM64_REG_R30) == static_cast<int>(PERF_REG_ARM64_LR), "");
memcpy(&arm64_user_regs.regs[unwindstack::ARM64_REG_R0], ®s.data[PERF_REG_ARM64_X0],
sizeof(uint64_t) * (PERF_REG_ARM64_LR - PERF_REG_ARM64_X0 + 1));
arm64_user_regs.sp = regs.data[PERF_REG_ARM64_SP];
arm64_user_regs.pc = regs.data[PERF_REG_ARM64_PC];
auto regs =
static_cast<unwindstack::RegsArm64*>(unwindstack::RegsArm64::Read(&arm64_user_regs));
uint64_t arm64_pac_mask_ = 0;
regs->SetPACMask(arm64_pac_mask_);
return regs;
}
case ARCH_X86_32: {
unwindstack::x86_user_regs x86_user_regs;
memset(&x86_user_regs, 0, sizeof(x86_user_regs));
x86_user_regs.eax = static_cast<uint32_t>(regs.data[PERF_REG_X86_AX]);
x86_user_regs.ebx = static_cast<uint32_t>(regs.data[PERF_REG_X86_BX]);
x86_user_regs.ecx = static_cast<uint32_t>(regs.data[PERF_REG_X86_CX]);
x86_user_regs.edx = static_cast<uint32_t>(regs.data[PERF_REG_X86_DX]);
x86_user_regs.ebp = static_cast<uint32_t>(regs.data[PERF_REG_X86_BP]);
x86_user_regs.edi = static_cast<uint32_t>(regs.data[PERF_REG_X86_DI]);
x86_user_regs.esi = static_cast<uint32_t>(regs.data[PERF_REG_X86_SI]);
x86_user_regs.esp = static_cast<uint32_t>(regs.data[PERF_REG_X86_SP]);
x86_user_regs.eip = static_cast<uint32_t>(regs.data[PERF_REG_X86_IP]);
return unwindstack::RegsX86::Read(&x86_user_regs);
}
case ARCH_X86_64: {
unwindstack::x86_64_user_regs x86_64_user_regs;
memset(&x86_64_user_regs, 0, sizeof(x86_64_user_regs));
x86_64_user_regs.rax = regs.data[PERF_REG_X86_AX];
x86_64_user_regs.rbx = regs.data[PERF_REG_X86_BX];
x86_64_user_regs.rcx = regs.data[PERF_REG_X86_CX];
x86_64_user_regs.rdx = regs.data[PERF_REG_X86_DX];
x86_64_user_regs.r8 = regs.data[PERF_REG_X86_R8];
x86_64_user_regs.r9 = regs.data[PERF_REG_X86_R9];
x86_64_user_regs.r10 = regs.data[PERF_REG_X86_R10];
x86_64_user_regs.r11 = regs.data[PERF_REG_X86_R11];
x86_64_user_regs.r12 = regs.data[PERF_REG_X86_R12];
x86_64_user_regs.r13 = regs.data[PERF_REG_X86_R13];
x86_64_user_regs.r14 = regs.data[PERF_REG_X86_R14];
x86_64_user_regs.r15 = regs.data[PERF_REG_X86_R15];
x86_64_user_regs.rdi = regs.data[PERF_REG_X86_DI];
x86_64_user_regs.rsi = regs.data[PERF_REG_X86_SI];
x86_64_user_regs.rbp = regs.data[PERF_REG_X86_BP];
x86_64_user_regs.rsp = regs.data[PERF_REG_X86_SP];
x86_64_user_regs.rip = regs.data[PERF_REG_X86_IP];
return unwindstack::RegsX86_64::Read(&x86_64_user_regs);
}
default:
return nullptr;
}
}
const char* UnwindCallChain(char* map_buffer, UnwindOption* opt, uint64_t* regs_buf, void* stack_buf) {
// std::cerr << "pid:" << pid << "reg_mask:" << opt->reg_mask << "abi:" << opt->abi << std::endl;
RegSet regs(opt->abi, opt->reg_mask, regs_buf);
uint64_t sp_reg_value;
if (!regs.GetSpRegValue(&sp_reg_value)) {
std::cerr << "can't get sp reg value";
char* empty_result = (char*)malloc(1);
if (empty_result) {
empty_result[0] = '\0';
}
return empty_result;
}
uint64_t stack_addr = sp_reg_value;
size_t stack_size = opt->dyn_size;
std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
if (!unwind_regs) {
char* empty_result = (char*)malloc(1);
if (empty_result) {
empty_result[0] = '\0';
}
return empty_result;
}
std::shared_ptr<unwindstack::Memory> stack_memory = unwindstack::Memory::CreateOfflineMemory(
reinterpret_cast<const uint8_t*>(stack_buf), stack_addr, stack_addr + stack_size
);
std::unique_ptr<unwindstack::Maps> maps;
maps.reset(new unwindstack::BufferMaps(map_buffer));
maps->Parse();
UnwinderWithPC unwinder(512, maps.get(), unwind_regs.get(), stack_memory, opt->show_pc);
// default is true
// unwinder.SetResolveNames(false);
unwinder.Unwind();
std::string frame_info = DumpFrames(unwinder);
// int len = frame_info.length();
// send(client_sockfd, &len, 4, 0);
// send(client_sockfd, frame_info.c_str(), len, 0);
char* result_on_heap = (char*)malloc(frame_info.length() + 1);
if (result_on_heap == nullptr) {
return nullptr;
}
strcpy(result_on_heap, frame_info.c_str());
return result_on_heap;
}
const char* UnwindCallChainV2(int pid, UnwindOption* opt, uint64_t* regs_buf, void* stack_buf) {
// SetMinimumLogSeverity(android::base::DEBUG);
RegSet regs(opt->abi, opt->reg_mask, regs_buf);
uint64_t sp_reg_value;
if (!regs.GetSpRegValue(&sp_reg_value)) {
std::cerr << "can't get sp reg value";
char* empty_result = (char*)malloc(1);
if (empty_result) {
empty_result[0] = '\0';
}
return empty_result;
}
uint64_t stack_addr = sp_reg_value;
size_t stack_size = opt->dyn_size;
std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
if (!unwind_regs) {
char* empty_result = (char*)malloc(1);
if (empty_result) {
empty_result[0] = '\0';
}
return empty_result;
}
MyAndroidRemoteUnwinder unwinder((pid_t) pid, opt->show_pc);
unwindstack::AndroidUnwinderData data;
unwindstack::ErrorData error;
unwinder.SetArch(unwind_regs->Arch());
unwinder.SetMaxFrames(512);
unwinder.SetDisplayBuildID(false);
if (!unwinder.Initialize(error)){
std::cerr << "Initialize failed" << std::endl;
};
if (!unwinder.Unwind(unwind_regs.get(), data)){
std::cerr << "Unwind failed" << std::endl;
};
std::string frame_info;
for (const auto& frame : data.frames) {
frame_info += unwinder.FormatFrame(frame) + '\n';
}
char* result_on_heap = (char*)malloc(frame_info.length() + 1);
if (result_on_heap == nullptr) {
return nullptr;
}
strcpy(result_on_heap, frame_info.c_str());
return result_on_heap;
}
}
__attribute__ ((visibility("default")))
extern "C" const char* StackPlz(char* map_buffer, void* opt, void* regs_buf, void* stack_buf) {
return unwinddaemon::UnwindCallChain(map_buffer, (unwinddaemon::UnwindOption*) opt, (uint64_t*) regs_buf, stack_buf);
}
__attribute__ ((visibility("default")))
extern "C" const char* StackPlzV2(int pid, void* opt, void* regs_buf, void* stack_buf) {
return unwinddaemon::UnwindCallChainV2(pid, (unwinddaemon::UnwindOption*) opt, (uint64_t*) regs_buf, stack_buf);
}