| 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> | 
| Elliott Hughes | 420a7fa | 2012-05-17 18:28:01 -0700 | [diff] [blame] | 24 | #include <stdlib.h> | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 25 | #include <sys/ptrace.h> | 
|  | 26 | #include <cutils/log.h> | 
|  | 27 |  | 
|  | 28 | static const uint32_t ELF_MAGIC = 0x464C457f; // "ELF\0177" | 
|  | 29 |  | 
|  | 30 | #ifndef PAGE_SIZE | 
|  | 31 | #define PAGE_SIZE 4096 | 
|  | 32 | #endif | 
|  | 33 |  | 
|  | 34 | #ifndef PAGE_MASK | 
|  | 35 | #define PAGE_MASK (~(PAGE_SIZE - 1)) | 
|  | 36 | #endif | 
|  | 37 |  | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 38 | void init_memory(memory_t* memory, const map_info_t* map_info_list) { | 
|  | 39 | memory->tid = -1; | 
|  | 40 | memory->map_info_list = map_info_list; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | void init_memory_ptrace(memory_t* memory, pid_t tid) { | 
|  | 44 | memory->tid = tid; | 
|  | 45 | memory->map_info_list = NULL; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value) { | 
| Elliott Hughes | bfec3a3 | 2012-05-24 19:03:07 -0700 | [diff] [blame] | 49 | ALOGV("try_get_word: reading word at %p", (void*) ptr); | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 50 | if (ptr & 3) { | 
| Elliott Hughes | bfec3a3 | 2012-05-24 19:03:07 -0700 | [diff] [blame] | 51 | ALOGV("try_get_word: invalid pointer %p", (void*) ptr); | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 52 | *out_value = 0xffffffffL; | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 53 | return false; | 
|  | 54 | } | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 55 | if (memory->tid < 0) { | 
|  | 56 | if (!is_readable_map(memory->map_info_list, ptr)) { | 
| Elliott Hughes | bfec3a3 | 2012-05-24 19:03:07 -0700 | [diff] [blame] | 57 | ALOGV("try_get_word: pointer %p not in a readable map", (void*) ptr); | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 58 | *out_value = 0xffffffffL; | 
|  | 59 | return false; | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 60 | } | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 61 | *out_value = *(uint32_t*)ptr; | 
|  | 62 | return true; | 
|  | 63 | } else { | 
| Elliott Hughes | bfec3a3 | 2012-05-24 19:03:07 -0700 | [diff] [blame] | 64 | #if defined(__APPLE__) | 
|  | 65 | ALOGV("no ptrace on Mac OS"); | 
|  | 66 | return false; | 
|  | 67 | #else | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 68 | // ptrace() returns -1 and sets errno when the operation fails. | 
|  | 69 | // To disambiguate -1 from a valid result, we clear errno beforehand. | 
|  | 70 | errno = 0; | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 71 | *out_value = ptrace(PTRACE_PEEKTEXT, memory->tid, (void*)ptr, NULL); | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 72 | if (*out_value == 0xffffffffL && errno) { | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 73 | ALOGV("try_get_word: invalid pointer 0x%08x reading from tid %d, " | 
|  | 74 | "ptrace() errno=%d", ptr, memory->tid, errno); | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 75 | return false; | 
|  | 76 | } | 
|  | 77 | return true; | 
| Elliott Hughes | bfec3a3 | 2012-05-24 19:03:07 -0700 | [diff] [blame] | 78 | #endif | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 79 | } | 
|  | 80 | } | 
|  | 81 |  | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 82 | bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value) { | 
|  | 83 | memory_t memory; | 
|  | 84 | init_memory_ptrace(&memory, tid); | 
|  | 85 | return try_get_word(&memory, ptr, out_value); | 
|  | 86 | } | 
|  | 87 |  | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 88 | static void load_ptrace_map_info_data(pid_t pid, map_info_t* mi) { | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 89 | if (mi->is_executable && mi->is_readable) { | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 90 | uint32_t elf_magic; | 
| Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 91 | if (try_get_word_ptrace(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) { | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 92 | map_info_data_t* data = (map_info_data_t*)calloc(1, sizeof(map_info_data_t)); | 
|  | 93 | if (data) { | 
|  | 94 | mi->data = data; | 
|  | 95 | if (mi->name[0]) { | 
|  | 96 | data->symbol_table = load_symbol_table(mi->name); | 
|  | 97 | } | 
|  | 98 | #ifdef CORKSCREW_HAVE_ARCH | 
|  | 99 | load_ptrace_map_info_data_arch(pid, mi, data); | 
|  | 100 | #endif | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | ptrace_context_t* load_ptrace_context(pid_t pid) { | 
|  | 107 | ptrace_context_t* context = | 
|  | 108 | (ptrace_context_t*)calloc(1, sizeof(ptrace_context_t)); | 
|  | 109 | if (context) { | 
|  | 110 | context->map_info_list = load_map_info_list(pid); | 
|  | 111 | for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) { | 
|  | 112 | load_ptrace_map_info_data(pid, mi); | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 | return context; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | static void free_ptrace_map_info_data(map_info_t* mi) { | 
|  | 119 | map_info_data_t* data = (map_info_data_t*)mi->data; | 
|  | 120 | if (data) { | 
|  | 121 | if (data->symbol_table) { | 
|  | 122 | free_symbol_table(data->symbol_table); | 
|  | 123 | } | 
|  | 124 | #ifdef CORKSCREW_HAVE_ARCH | 
|  | 125 | free_ptrace_map_info_data_arch(mi, data); | 
|  | 126 | #endif | 
|  | 127 | free(data); | 
|  | 128 | mi->data = NULL; | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | void free_ptrace_context(ptrace_context_t* context) { | 
|  | 133 | for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) { | 
|  | 134 | free_ptrace_map_info_data(mi); | 
|  | 135 | } | 
|  | 136 | free_map_info_list(context->map_info_list); | 
| Liangtao Gao | 0632ad4 | 2013-03-20 19:48:19 +0800 | [diff] [blame] | 137 | free(context); | 
| Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 138 | } | 
|  | 139 |  | 
|  | 140 | void find_symbol_ptrace(const ptrace_context_t* context, | 
|  | 141 | uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol) { | 
|  | 142 | const map_info_t* mi = find_map_info(context->map_info_list, addr); | 
|  | 143 | const symbol_t* symbol = NULL; | 
|  | 144 | if (mi) { | 
|  | 145 | const map_info_data_t* data = (const map_info_data_t*)mi->data; | 
|  | 146 | if (data && data->symbol_table) { | 
|  | 147 | symbol = find_symbol(data->symbol_table, addr - mi->start); | 
|  | 148 | } | 
|  | 149 | } | 
|  | 150 | *out_map_info = mi; | 
|  | 151 | *out_symbol = symbol; | 
|  | 152 | } |