Add support for UnwinderFromPid object.
This object is able to be easily used from other code and can be used
to replace the libbacktrace calls in other parts of the platform.
Also, demangle the function names when calling FormatFrame.
Bug: 120606663
Test: Unit tests pass, debuggerd using this code directly passes unit
Test: tests.
Change-Id: Ifd8cf9bdd89174c1736810711d20e9f37f29b1bf
diff --git a/libunwindstack/tools/unwind.cpp b/libunwindstack/tools/unwind.cpp
index e729453..1812e50 100644
--- a/libunwindstack/tools/unwind.cpp
+++ b/libunwindstack/tools/unwind.cpp
@@ -57,12 +57,6 @@
}
void DoUnwind(pid_t pid) {
- unwindstack::RemoteMaps remote_maps(pid);
- if (!remote_maps.Parse()) {
- printf("Failed to parse map data.\n");
- return;
- }
-
unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid);
if (regs == nullptr) {
printf("Unable to get remote reg data\n");
@@ -95,15 +89,13 @@
}
printf("\n");
- auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
- unwindstack::Unwinder unwinder(128, &remote_maps, regs, process_memory);
+ unwindstack::UnwinderFromPid unwinder(1024, pid);
+ if (!unwinder.Init(regs->Arch())) {
+ printf("Failed to init unwinder object.\n");
+ return;
+ }
- unwindstack::JitDebug jit_debug(process_memory);
- unwinder.SetJitDebug(&jit_debug, regs->Arch());
-
- unwindstack::DexFiles dex_files(process_memory);
- unwinder.SetDexFiles(&dex_files, regs->Arch());
-
+ unwinder.SetRegs(regs);
unwinder.Unwind();
// Print the frames.
diff --git a/libunwindstack/tools/unwind_for_offline.cpp b/libunwindstack/tools/unwind_for_offline.cpp
index 5ae8874..4f67d67 100644
--- a/libunwindstack/tools/unwind_for_offline.cpp
+++ b/libunwindstack/tools/unwind_for_offline.cpp
@@ -240,12 +240,6 @@
return 1;
}
- unwindstack::RemoteMaps maps(pid);
- if (!maps.Parse()) {
- printf("Unable to parse maps.\n");
- return 1;
- }
-
// Save the current state of the registers.
if (!SaveRegs(regs)) {
return 1;
@@ -253,35 +247,38 @@
// Do an unwind so we know how much of the stack to save, and what
// elf files are involved.
+ unwindstack::UnwinderFromPid unwinder(1024, pid);
+ if (!unwinder.Init(regs->Arch())) {
+ printf("Unable to init unwinder object.\n");
+ return 1;
+ }
+ unwinder.SetRegs(regs);
uint64_t sp = regs->sp();
- auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
- unwindstack::JitDebug jit_debug(process_memory);
- unwindstack::Unwinder unwinder(1024, &maps, regs, process_memory);
- unwinder.SetJitDebug(&jit_debug, regs->Arch());
unwinder.Unwind();
std::unordered_map<uint64_t, map_info_t> maps_by_start;
std::vector<std::pair<uint64_t, uint64_t>> stacks;
+ unwindstack::Maps* maps = unwinder.GetMaps();
uint64_t sp_map_start = 0;
- unwindstack::MapInfo* map_info = maps.Find(sp);
+ unwindstack::MapInfo* map_info = maps->Find(sp);
if (map_info != nullptr) {
stacks.emplace_back(std::make_pair(sp, map_info->end));
sp_map_start = map_info->start;
}
for (const auto& frame : unwinder.frames()) {
- map_info = maps.Find(frame.sp);
+ map_info = maps->Find(frame.sp);
if (map_info != nullptr && sp_map_start != map_info->start) {
stacks.emplace_back(std::make_pair(frame.sp, map_info->end));
sp_map_start = map_info->start;
}
if (maps_by_start.count(frame.map_start) == 0) {
- map_info = maps.Find(frame.map_start);
+ map_info = maps->Find(frame.map_start);
auto info = FillInAndGetMapInfo(maps_by_start, map_info);
bool file_copied = false;
- SaveMapInformation(process_memory, info, &file_copied);
+ SaveMapInformation(unwinder.GetProcessMemory(), info, &file_copied);
// If you are using a a linker that creates two maps (one read-only, one
// read-executable), it's necessary to capture the previous map
@@ -291,7 +288,7 @@
prev_map->flags == PROT_READ && map_info->name == prev_map->name &&
maps_by_start.count(prev_map->start) == 0) {
info = FillInAndGetMapInfo(maps_by_start, prev_map);
- SaveMapInformation(process_memory, info, &file_copied);
+ SaveMapInformation(unwinder.GetProcessMemory(), info, &file_copied);
}
}
}