diff options
Diffstat (limited to 'libcorkscrew')
-rw-r--r-- | libcorkscrew/Android.mk | 6 | ||||
-rw-r--r-- | libcorkscrew/arch-mips/backtrace-mips.c | 194 | ||||
-rw-r--r-- | libcorkscrew/arch-mips/ptrace-mips.c | 28 | ||||
-rw-r--r-- | libcorkscrew/arch-x86/backtrace-x86.c | 6 | ||||
-rw-r--r-- | libcorkscrew/arch-x86/ptrace-x86.c | 7 | ||||
-rw-r--r-- | libcorkscrew/backtrace.c | 4 |
6 files changed, 238 insertions, 7 deletions
diff --git a/libcorkscrew/Android.mk b/libcorkscrew/Android.mk index 9019986..2786d8f 100644 --- a/libcorkscrew/Android.mk +++ b/libcorkscrew/Android.mk @@ -42,6 +42,12 @@ ifeq ($(TARGET_ARCH),x86) LOCAL_SRC_FILES += $(x86_src_files) LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH endif +ifeq ($(TARGET_ARCH),mips) +LOCAL_SRC_FILES += \ + arch-mips/backtrace-mips.c \ + arch-mips/ptrace-mips.c +LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH +endif LOCAL_SHARED_LIBRARIES += libdl libcutils libgccdemangle diff --git a/libcorkscrew/arch-mips/backtrace-mips.c b/libcorkscrew/arch-mips/backtrace-mips.c new file mode 100644 index 0000000..07d4a24 --- /dev/null +++ b/libcorkscrew/arch-mips/backtrace-mips.c @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2012 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. + */ + +/* + * Backtracing functions for mips + */ + +#define LOG_TAG "Corkscrew" +//#define LOG_NDEBUG 0 + +#include "../backtrace-arch.h" +#include "../backtrace-helper.h" +#include <corkscrew/ptrace.h> + +#include <stdlib.h> +#include <signal.h> +#include <stdbool.h> +#include <limits.h> +#include <errno.h> +#include <sys/ptrace.h> +#include <sys/exec_elf.h> +#include <cutils/log.h> + +/* For PTRACE_GETREGS */ +typedef struct { + /* FIXME: check this definition */ + uint64_t regs[32]; + uint64_t lo; + uint64_t hi; + uint64_t epc; + uint64_t badvaddr; + uint64_t status; + uint64_t cause; +} user_regs_struct; + +/* Machine context at the time a signal was raised. */ +typedef struct ucontext { + /* FIXME: use correct definition */ + uint32_t sp; + uint32_t ra; + uint32_t pc; +} ucontext_t; + +/* Unwind state. */ +typedef struct { + uint32_t sp; + uint32_t ra; + uint32_t pc; +} unwind_state_t; + +uintptr_t rewind_pc_arch(const memory_t* memory, uintptr_t pc) { + if (pc == 0) + return pc; + if ((pc & 1) == 0) + return pc-8; /* jal/bal/jalr + branch delay slot */ + return pc; +} + +static ssize_t unwind_backtrace_common(const memory_t* memory, + const map_info_t* map_info_list, + unwind_state_t* state, backtrace_frame_t* backtrace, + size_t ignore_depth, size_t max_depth) { + size_t ignored_frames = 0; + size_t returned_frames = 0; + + for (size_t index = 0; returned_frames < max_depth; index++) { + uintptr_t pc = index ? rewind_pc_arch(memory, state->pc) : state->pc; + backtrace_frame_t* frame; + uintptr_t addr; + int maxcheck = 1024; + int stack_size = 0, ra_offset = 0; + bool found_start = false; + + frame = add_backtrace_entry(pc, backtrace, ignore_depth, + max_depth, &ignored_frames, &returned_frames); + + if (frame) + frame->stack_top = state->sp; + + ALOGV("#%d: frame=%p pc=%08x sp=%08x\n", index, frame, frame->absolute_pc, frame->stack_top); + + for (addr = state->pc; maxcheck-- > 0 && !found_start; addr -= 4) { + uint32_t op; + if (!try_get_word(memory, addr, &op)) + break; + + // ALOGV("@0x%08x: 0x%08x\n", addr, op); + switch (op & 0xffff0000) { + case 0x27bd0000: // addiu sp, imm + { + // looking for stack being decremented + int32_t immediate = ((((int)op) << 16) >> 16); + if (immediate < 0) { + stack_size = -immediate; + found_start = true; + ALOGV("@0x%08x: found stack adjustment=%d\n", addr, stack_size); + } + } + break; + case 0xafbf0000: // sw ra, imm(sp) + ra_offset = ((((int)op) << 16) >> 16); + ALOGV("@0x%08x: found ra offset=%d\n", addr, ra_offset); + break; + case 0x3c1c0000: // lui gp + ALOGV("@0x%08x: found function boundary\n", addr); + found_start = true; + break; + default: + break; + } + } + + if (ra_offset) { + uint32_t next_ra; + if (!try_get_word(memory, state->sp + ra_offset, &next_ra)) + break; + state->ra = next_ra; + ALOGV("New ra: 0x%08x\n", state->ra); + } + + if (stack_size) { + if (frame) + frame->stack_size = stack_size; + state->sp += stack_size; + ALOGV("New sp: 0x%08x\n", state->sp); + } + + if (state->pc == state->ra && stack_size == 0) + break; + + if (state->ra == 0) + break; + + state->pc = state->ra; + } + + ALOGV("returning %d frames\n", returned_frames); + + return returned_frames; +} + +ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo, void* sigcontext, + const map_info_t* map_info_list, + backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { + const ucontext_t* uc = (const ucontext_t*)sigcontext; + + unwind_state_t state; + state.sp = uc->sp; + state.pc = uc->pc; + state.ra = uc->ra; + + ALOGV("unwind_backtrace_signal_arch: ignore_depth=%d max_depth=%d pc=0x%08x sp=0x%08x ra=0x%08x\n", + ignore_depth, max_depth, state.pc, state.sp, state.ra); + + memory_t memory; + init_memory(&memory, map_info_list); + return unwind_backtrace_common(&memory, map_info_list, + &state, backtrace, ignore_depth, max_depth); +} + +ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context, + backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { + + user_regs_struct regs; + if (ptrace(PTRACE_GETREGS, tid, 0, ®s)) { + return -1; + } + + unwind_state_t state; + state.sp = regs.regs[29]; + state.ra = regs.regs[31]; + state.pc = regs.epc; + + ALOGV("unwind_backtrace_ptrace_arch: ignore_depth=%d max_depth=%d pc=0x%08x sp=0x%08x ra=0x%08x\n", + ignore_depth, max_depth, state.pc, state.sp, state.ra); + + memory_t memory; + init_memory_ptrace(&memory, tid); + return unwind_backtrace_common(&memory, context->map_info_list, + &state, backtrace, ignore_depth, max_depth); +} diff --git a/libcorkscrew/arch-mips/ptrace-mips.c b/libcorkscrew/arch-mips/ptrace-mips.c new file mode 100644 index 0000000..f0ea110 --- /dev/null +++ b/libcorkscrew/arch-mips/ptrace-mips.c @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2011 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. + */ + +#define LOG_TAG "Corkscrew" +//#define LOG_NDEBUG 0 + +#include "../ptrace-arch.h" + +#include <cutils/log.h> + +void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data) { +} + +void free_ptrace_map_info_data_arch(map_info_t* mi, map_info_data_t* data) { +} diff --git a/libcorkscrew/arch-x86/backtrace-x86.c b/libcorkscrew/arch-x86/backtrace-x86.c index 01d9ed5..6cdb0c8 100644 --- a/libcorkscrew/arch-x86/backtrace-x86.c +++ b/libcorkscrew/arch-x86/backtrace-x86.c @@ -55,13 +55,13 @@ typedef struct { uint32_t esp; } unwind_state_t; -uintptr_t rewind_pc_arch(const memory_t* memory, uintptr_t pc) { +uintptr_t rewind_pc_arch(const memory_t* memory __attribute__((unused)), uintptr_t pc) { // TODO: Implement for x86. return pc; } static ssize_t unwind_backtrace_common(const memory_t* memory, - const map_info_t* map_info_list, + const map_info_t* map_info_list __attribute__((unused)), unwind_state_t* state, backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { size_t ignored_frames = 0; @@ -90,7 +90,7 @@ static ssize_t unwind_backtrace_common(const memory_t* memory, return returned_frames; } -ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo, void* sigcontext, +ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo __attribute__((unused)), void* sigcontext, const map_info_t* map_info_list, backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { const ucontext_t* uc = (const ucontext_t*)sigcontext; diff --git a/libcorkscrew/arch-x86/ptrace-x86.c b/libcorkscrew/arch-x86/ptrace-x86.c index f0ea110..07cfd3a 100644 --- a/libcorkscrew/arch-x86/ptrace-x86.c +++ b/libcorkscrew/arch-x86/ptrace-x86.c @@ -21,8 +21,11 @@ #include <cutils/log.h> -void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data) { +void load_ptrace_map_info_data_arch(pid_t pid __attribute__((unused)), + map_info_t* mi __attribute__((unused)), + map_info_data_t* data __attribute__((unused))) { } -void free_ptrace_map_info_data_arch(map_info_t* mi, map_info_data_t* data) { +void free_ptrace_map_info_data_arch(map_info_t* mi __attribute__((unused)), + map_info_data_t* data __attribute__((unused))) { } diff --git a/libcorkscrew/backtrace.c b/libcorkscrew/backtrace.c index eec53a2..3697d18 100644 --- a/libcorkscrew/backtrace.c +++ b/libcorkscrew/backtrace.c @@ -124,7 +124,7 @@ static volatile struct { size_t returned_frames; } g_unwind_signal_state; -static void unwind_backtrace_thread_signal_handler(int n, siginfo_t* siginfo, void* sigcontext) { +static void unwind_backtrace_thread_signal_handler(int n __attribute__((unused)), siginfo_t* siginfo, void* sigcontext) { if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) { g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch( siginfo, sigcontext, @@ -305,7 +305,7 @@ void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames } } -void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame, +void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame __attribute__((unused)), const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize) { const char* mapName = symbol->map_name ? symbol->map_name : "<unknown>"; const char* symbolName = symbol->demangled_name ? symbol->demangled_name : symbol->symbol_name; |