Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Backtracing functions for x86. |
| 19 | */ |
| 20 | |
| 21 | #define LOG_TAG "Corkscrew" |
| 22 | //#define LOG_NDEBUG 0 |
| 23 | |
| 24 | #include "../backtrace-arch.h" |
| 25 | #include "../backtrace-helper.h" |
| 26 | #include <corkscrew/ptrace.h> |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <signal.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <limits.h> |
| 32 | #include <errno.h> |
| 33 | #include <sys/ptrace.h> |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 34 | #include <cutils/log.h> |
| 35 | |
Elliott Hughes | 6b3bab3 | 2012-05-21 13:59:52 -0700 | [diff] [blame^] | 36 | #if defined(__BIONIC__) |
| 37 | |
| 38 | // Bionic offers the Linux kernel headers. |
| 39 | #include <asm/sigcontext.h> |
| 40 | #include <asm/ucontext.h> |
| 41 | typedef struct ucontext ucontext_t; |
| 42 | |
| 43 | #else |
| 44 | |
| 45 | // glibc has its own renaming of the Linux kernel's structures. |
Elliott Hughes | 71363a8 | 2012-05-18 11:56:17 -0700 | [diff] [blame] | 46 | #define __USE_GNU // For REG_EBP, REG_ESP, and REG_EIP. |
| 47 | #include <ucontext.h> |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | 6b3bab3 | 2012-05-21 13:59:52 -0700 | [diff] [blame^] | 49 | #endif |
| 50 | |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 51 | /* Unwind state. */ |
| 52 | typedef struct { |
| 53 | uint32_t ebp; |
| 54 | uint32_t eip; |
| 55 | uint32_t esp; |
| 56 | } unwind_state_t; |
| 57 | |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 58 | uintptr_t rewind_pc_arch(const memory_t* memory, uintptr_t pc) { |
| 59 | // TODO: Implement for x86. |
| 60 | return pc; |
| 61 | } |
| 62 | |
| 63 | static ssize_t unwind_backtrace_common(const memory_t* memory, |
| 64 | const map_info_t* map_info_list, |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 65 | unwind_state_t* state, backtrace_frame_t* backtrace, |
| 66 | size_t ignore_depth, size_t max_depth) { |
| 67 | size_t ignored_frames = 0; |
| 68 | size_t returned_frames = 0; |
| 69 | |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 70 | for (size_t index = 0; state->ebp && returned_frames < max_depth; index++) { |
| 71 | backtrace_frame_t* frame = add_backtrace_entry( |
| 72 | index ? rewind_pc_arch(memory, state->eip) : state->eip, |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 73 | backtrace, ignore_depth, max_depth, |
| 74 | &ignored_frames, &returned_frames); |
| 75 | uint32_t next_esp = state->ebp + 8; |
| 76 | if (frame) { |
| 77 | frame->stack_top = state->esp; |
| 78 | if (state->esp < next_esp) { |
| 79 | frame->stack_size = next_esp - state->esp; |
| 80 | } |
| 81 | } |
| 82 | state->esp = next_esp; |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 83 | if (!try_get_word(memory, state->ebp + 4, &state->eip) |
| 84 | || !try_get_word(memory, state->ebp, &state->ebp) |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 85 | || !state->eip) { |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return returned_frames; |
| 91 | } |
| 92 | |
| 93 | ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo, void* sigcontext, |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 94 | const map_info_t* map_info_list, |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 95 | backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { |
| 96 | const ucontext_t* uc = (const ucontext_t*)sigcontext; |
| 97 | |
| 98 | unwind_state_t state; |
Elliott Hughes | 6b3bab3 | 2012-05-21 13:59:52 -0700 | [diff] [blame^] | 99 | #if defined(__BIONIC__) |
| 100 | state.ebp = uc->uc_mcontext.ebp; |
| 101 | state.esp = uc->uc_mcontext.esp; |
| 102 | state.eip = uc->uc_mcontext.eip; |
| 103 | #else |
Elliott Hughes | 71363a8 | 2012-05-18 11:56:17 -0700 | [diff] [blame] | 104 | state.ebp = uc->uc_mcontext.gregs[REG_EBP]; |
| 105 | state.esp = uc->uc_mcontext.gregs[REG_ESP]; |
| 106 | state.eip = uc->uc_mcontext.gregs[REG_EIP]; |
Elliott Hughes | 6b3bab3 | 2012-05-21 13:59:52 -0700 | [diff] [blame^] | 107 | #endif |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 108 | |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 109 | memory_t memory; |
| 110 | init_memory(&memory, map_info_list); |
| 111 | return unwind_backtrace_common(&memory, map_info_list, |
| 112 | &state, backtrace, ignore_depth, max_depth); |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context, |
| 116 | backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { |
| 117 | pt_regs_x86_t regs; |
| 118 | if (ptrace(PTRACE_GETREGS, tid, 0, ®s)) { |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | unwind_state_t state; |
| 123 | state.ebp = regs.ebp; |
| 124 | state.eip = regs.eip; |
| 125 | state.esp = regs.esp; |
| 126 | |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 127 | memory_t memory; |
| 128 | init_memory_ptrace(&memory, tid); |
| 129 | return unwind_backtrace_common(&memory, context->map_info_list, |
| 130 | &state, backtrace, ignore_depth, max_depth); |
Jeff Brown | 10484a0 | 2011-10-21 12:07:49 -0700 | [diff] [blame] | 131 | } |