From b5d7a875128d58924832ab9b97b72f9e37af9b7a Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Wed, 12 Jul 2017 15:40:52 -0700 Subject: [PATCH] Move tools to separate directory. Bug: 23762183 Test: Ran unit tests, ran new tools. Change-Id: Icc7cbb4102a68042f4683d6dd622f21bc5e74deb --- libunwindstack/Android.bp | 34 ++++- libunwindstack/tools/unwind.cpp | 160 +++++++++++++++++++++ libunwindstack/{ => tools}/unwind_info.cpp | 6 +- libunwindstack/tools/unwind_symbols.cpp | 97 +++++++++++++ 4 files changed, 286 insertions(+), 11 deletions(-) create mode 100644 libunwindstack/tools/unwind.cpp rename libunwindstack/{ => tools}/unwind_info.cpp (98%) create mode 100644 libunwindstack/tools/unwind_symbols.cpp diff --git a/libunwindstack/Android.bp b/libunwindstack/Android.bp index a6044c88b..e6d3bffa0 100644 --- a/libunwindstack/Android.bp +++ b/libunwindstack/Android.bp @@ -150,10 +150,10 @@ cc_test { } //------------------------------------------------------------------------- -// Utility Executables +// Tools //------------------------------------------------------------------------- cc_defaults { - name: "libunwindstack_executables", + name: "libunwindstack_tools", defaults: ["libunwindstack_flags"], shared_libs: [ @@ -161,18 +161,40 @@ cc_defaults { "libbase", "liblzma", ], +} - static_libs: [ - "liblog", +cc_binary { + name: "unwind", + defaults: ["libunwindstack_tools"], + + srcs: [ + "tools/unwind.cpp", ], + + target: { + linux: { + host_ldlibs: [ + "-lrt", + ], + }, + }, } cc_binary { name: "unwind_info", - defaults: ["libunwindstack_executables"], + defaults: ["libunwindstack_tools"], srcs: [ - "unwind_info.cpp", + "tools/unwind_info.cpp", + ], +} + +cc_binary { + name: "unwind_symbols", + defaults: ["libunwindstack_tools"], + + srcs: [ + "tools/unwind_symbols.cpp", ], } diff --git a/libunwindstack/tools/unwind.cpp b/libunwindstack/tools/unwind.cpp new file mode 100644 index 000000000..34cd1ce5e --- /dev/null +++ b/libunwindstack/tools/unwind.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "Elf.h" +#include "Maps.h" +#include "Memory.h" +#include "Regs.h" + +static bool Attach(pid_t pid) { + if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) { + return false; + } + + // Allow at least 1 second to attach properly. + for (size_t i = 0; i < 1000; i++) { + siginfo_t si; + if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) { + return true; + } + usleep(1000); + } + printf("%d: Failed to stop.\n", pid); + return false; +} + +static bool Detach(pid_t pid) { + return ptrace(PTRACE_DETACH, pid, 0, 0) == 0; +} + +void DoUnwind(pid_t pid) { + RemoteMaps remote_maps(pid); + if (!remote_maps.Parse()) { + printf("Failed to parse map data.\n"); + return; + } + + uint32_t machine_type; + Regs* regs = Regs::RemoteGet(pid, &machine_type); + if (regs == nullptr) { + printf("Unable to get remote reg data\n"); + return; + } + + bool bits32 = true; + printf("ABI: "); + switch (machine_type) { + case EM_ARM: + printf("arm"); + break; + case EM_386: + printf("x86"); + break; + case EM_AARCH64: + printf("arm64"); + bits32 = false; + break; + case EM_X86_64: + printf("x86_64"); + bits32 = false; + break; + default: + printf("unknown\n"); + return; + } + printf("\n"); + + MemoryRemote remote_memory(pid); + for (size_t frame_num = 0; frame_num < 64; frame_num++) { + if (regs->pc() == 0) { + break; + } + MapInfo* map_info = remote_maps.Find(regs->pc()); + if (map_info == nullptr) { + printf("Failed to find map data for the pc\n"); + break; + } + + Elf* elf = map_info->GetElf(pid, true); + + uint64_t rel_pc = regs->GetRelPc(elf, map_info); + uint64_t adjusted_rel_pc = rel_pc; + // Don't need to adjust the first frame pc. + if (frame_num != 0) { + adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf); + } + + std::string name; + if (bits32) { + printf(" #%02zu pc %08" PRIx64, frame_num, adjusted_rel_pc); + } else { + printf(" #%02zu pc %016" PRIx64, frame_num, adjusted_rel_pc); + } + if (!map_info->name.empty()) { + printf(" %s", map_info->name.c_str()); + if (map_info->elf_offset != 0) { + printf(" (offset 0x%" PRIx64 ")", map_info->elf_offset); + } + } else { + printf(" ", map_info->offset); + } + uint64_t func_offset; + if (elf->GetFunctionName(adjusted_rel_pc, &name, &func_offset)) { + printf(" (%s", name.c_str()); + if (func_offset != 0) { + printf("+%" PRId64, func_offset); + } + printf(")"); + } + printf("\n"); + + if (!elf->Step(rel_pc + map_info->elf_offset, regs, &remote_memory)) { + break; + } + } +} + +int main(int argc, char** argv) { + if (argc != 2) { + printf("Usage: unwind \n"); + return 1; + } + + pid_t pid = atoi(argv[1]); + if (!Attach(pid)) { + printf("Failed to attach to pid %d: %s\n", pid, strerror(errno)); + return 1; + } + + DoUnwind(pid); + + Detach(pid); + + return 0; +} diff --git a/libunwindstack/unwind_info.cpp b/libunwindstack/tools/unwind_info.cpp similarity index 98% rename from libunwindstack/unwind_info.cpp rename to libunwindstack/tools/unwind_info.cpp index da1522b3d..e889f8c32 100644 --- a/libunwindstack/unwind_info.cpp +++ b/libunwindstack/tools/unwind_info.cpp @@ -21,8 +21,8 @@ #include #include #include -#include #include +#include #include #include "ArmExidx.h" @@ -115,10 +115,6 @@ int main(int argc, char** argv) { printf("%s is not a regular file.\n", argv[1]); return 1; } - if (S_ISDIR(st.st_mode)) { - printf("%s is a directory.\n", argv[1]); - return 1; - } // Send all log messages to stdout. log_to_stdout(true); diff --git a/libunwindstack/tools/unwind_symbols.cpp b/libunwindstack/tools/unwind_symbols.cpp new file mode 100644 index 000000000..c010dfcb1 --- /dev/null +++ b/libunwindstack/tools/unwind_symbols.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "Elf.h" +#include "ElfInterface.h" +#include "Log.h" + +int main(int argc, char** argv) { + if (argc != 2) { + printf("Need to pass the name of an elf file to the program.\n"); + return 1; + } + + struct stat st; + if (stat(argv[1], &st) == -1) { + printf("Cannot stat %s: %s\n", argv[1], strerror(errno)); + return 1; + } + if (!S_ISREG(st.st_mode)) { + printf("%s is not a regular file.\n", argv[1]); + return 1; + } + + // Send all log messages to stdout. + log_to_stdout(true); + + MemoryFileAtOffset* memory = new MemoryFileAtOffset; + if (!memory->Init(argv[1], 0)) { + printf("Failed to init\n"); + return 1; + } + + Elf elf(memory); + if (!elf.Init() || !elf.valid()) { + printf("%s is not a valid elf file.\n", argv[1]); + return 1; + } + + switch (elf.machine_type()) { + case EM_ARM: + printf("ABI: arm\n"); + break; + case EM_AARCH64: + printf("ABI: arm64\n"); + break; + case EM_386: + printf("ABI: x86\n"); + break; + case EM_X86_64: + printf("ABI: x86_64\n"); + break; + default: + printf("ABI: unknown\n"); + return 1; + } + + // This is a crude way to get the symbols in order. + std::string name; + uint64_t load_bias = elf.interface()->load_bias(); + for (const auto& entry : elf.interface()->pt_loads()) { + uint64_t start = entry.second.offset + load_bias; + uint64_t end = entry.second.table_size + load_bias; + for (uint64_t addr = start; addr < end; addr += 4) { + std::string cur_name; + uint64_t func_offset; + if (elf.GetFunctionName(addr, &cur_name, &func_offset)) { + if (cur_name != name) { + printf("<0x%" PRIx64 "> Function: %s\n", addr - func_offset, cur_name.c_str()); + } + name = cur_name; + } + } + } + + return 0; +}