Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -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 | #define LOG_TAG "Corkscrew" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include "ptrace-arch.h" |
| 21 | #include <corkscrew/ptrace.h> |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <sys/ptrace.h> |
| 25 | #include <cutils/log.h> |
| 26 | |
| 27 | static const uint32_t ELF_MAGIC = 0x464C457f; // "ELF\0177" |
| 28 | |
| 29 | #ifndef PAGE_SIZE |
| 30 | #define PAGE_SIZE 4096 |
| 31 | #endif |
| 32 | |
| 33 | #ifndef PAGE_MASK |
| 34 | #define PAGE_MASK (~(PAGE_SIZE - 1)) |
| 35 | #endif |
| 36 | |
| 37 | bool try_get_word(pid_t tid, uintptr_t ptr, uint32_t* out_value) { |
| 38 | if (ptr & 3) { |
| 39 | LOGV("try_get_word: invalid pointer 0x%08x", ptr); |
| 40 | *out_value = 0; |
| 41 | return false; |
| 42 | } |
| 43 | if (tid < 0) { |
| 44 | #if 0 /*unreliable, unclear whether this is safe from a signal handler context*/ |
| 45 | // Determine whether the pointer is likely to be valid before dereferencing it. |
| 46 | unsigned char vec[1]; |
| 47 | while (mincore((void*)(ptr & PAGE_MASK), sizeof(uint32_t), vec)) { |
| 48 | if (errno != EAGAIN && errno != EINTR) { |
| 49 | LOGV("try_get_word: invalid pointer 0x%08x, mincore() errno=%d", ptr, errno); |
| 50 | *out_value = 0; |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | #endif |
| 55 | *out_value = *(uint32_t*)ptr; |
| 56 | return true; |
| 57 | } else { |
| 58 | // ptrace() returns -1 and sets errno when the operation fails. |
| 59 | // To disambiguate -1 from a valid result, we clear errno beforehand. |
| 60 | errno = 0; |
| 61 | *out_value = ptrace(PTRACE_PEEKTEXT, tid, (void*)ptr, NULL); |
| 62 | if (*out_value == 0xffffffffL && errno) { |
| 63 | LOGV("try_get_word: invalid pointer 0x%08x, ptrace() errno=%d", ptr, errno); |
| 64 | *out_value = 0; |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void load_ptrace_map_info_data(pid_t pid, map_info_t* mi) { |
| 72 | if (mi->is_executable) { |
| 73 | uint32_t elf_magic; |
| 74 | if (try_get_word(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) { |
| 75 | map_info_data_t* data = (map_info_data_t*)calloc(1, sizeof(map_info_data_t)); |
| 76 | if (data) { |
| 77 | mi->data = data; |
| 78 | if (mi->name[0]) { |
| 79 | data->symbol_table = load_symbol_table(mi->name); |
| 80 | } |
| 81 | #ifdef CORKSCREW_HAVE_ARCH |
| 82 | load_ptrace_map_info_data_arch(pid, mi, data); |
| 83 | #endif |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | ptrace_context_t* load_ptrace_context(pid_t pid) { |
| 90 | ptrace_context_t* context = |
| 91 | (ptrace_context_t*)calloc(1, sizeof(ptrace_context_t)); |
| 92 | if (context) { |
| 93 | context->map_info_list = load_map_info_list(pid); |
| 94 | for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) { |
| 95 | load_ptrace_map_info_data(pid, mi); |
| 96 | } |
| 97 | } |
| 98 | return context; |
| 99 | } |
| 100 | |
| 101 | static void free_ptrace_map_info_data(map_info_t* mi) { |
| 102 | map_info_data_t* data = (map_info_data_t*)mi->data; |
| 103 | if (data) { |
| 104 | if (data->symbol_table) { |
| 105 | free_symbol_table(data->symbol_table); |
| 106 | } |
| 107 | #ifdef CORKSCREW_HAVE_ARCH |
| 108 | free_ptrace_map_info_data_arch(mi, data); |
| 109 | #endif |
| 110 | free(data); |
| 111 | mi->data = NULL; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void free_ptrace_context(ptrace_context_t* context) { |
| 116 | for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) { |
| 117 | free_ptrace_map_info_data(mi); |
| 118 | } |
| 119 | free_map_info_list(context->map_info_list); |
| 120 | } |
| 121 | |
| 122 | void find_symbol_ptrace(const ptrace_context_t* context, |
| 123 | uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol) { |
| 124 | const map_info_t* mi = find_map_info(context->map_info_list, addr); |
| 125 | const symbol_t* symbol = NULL; |
| 126 | if (mi) { |
| 127 | const map_info_data_t* data = (const map_info_data_t*)mi->data; |
| 128 | if (data && data->symbol_table) { |
| 129 | symbol = find_symbol(data->symbol_table, addr - mi->start); |
| 130 | } |
| 131 | } |
| 132 | *out_map_info = mi; |
| 133 | *out_symbol = symbol; |
| 134 | } |