ptrace.c revision f0c5872637a63e28e3cd314cfc915c07f76df9c6
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 27static 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 37void init_memory(memory_t* memory, const map_info_t* map_info_list) { 38 memory->tid = -1; 39 memory->map_info_list = map_info_list; 40} 41 42void init_memory_ptrace(memory_t* memory, pid_t tid) { 43 memory->tid = tid; 44 memory->map_info_list = NULL; 45} 46 47bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value) { 48 ALOGV("try_get_word: reading word at 0x%08x", ptr); 49 if (ptr & 3) { 50 ALOGV("try_get_word: invalid pointer 0x%08x", ptr); 51 *out_value = 0xffffffffL; 52 return false; 53 } 54 if (memory->tid < 0) { 55 if (!is_readable_map(memory->map_info_list, ptr)) { 56 ALOGV("try_get_word: pointer 0x%08x not in a readable map", ptr); 57 *out_value = 0xffffffffL; 58 return false; 59 } 60 *out_value = *(uint32_t*)ptr; 61 return true; 62 } else { 63 // ptrace() returns -1 and sets errno when the operation fails. 64 // To disambiguate -1 from a valid result, we clear errno beforehand. 65 errno = 0; 66 *out_value = ptrace(PTRACE_PEEKTEXT, memory->tid, (void*)ptr, NULL); 67 if (*out_value == 0xffffffffL && errno) { 68 ALOGV("try_get_word: invalid pointer 0x%08x reading from tid %d, " 69 "ptrace() errno=%d", ptr, memory->tid, errno); 70 return false; 71 } 72 return true; 73 } 74} 75 76bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value) { 77 memory_t memory; 78 init_memory_ptrace(&memory, tid); 79 return try_get_word(&memory, ptr, out_value); 80} 81 82static void load_ptrace_map_info_data(pid_t pid, map_info_t* mi) { 83 if (mi->is_executable && mi->is_readable) { 84 uint32_t elf_magic; 85 if (try_get_word_ptrace(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) { 86 map_info_data_t* data = (map_info_data_t*)calloc(1, sizeof(map_info_data_t)); 87 if (data) { 88 mi->data = data; 89 if (mi->name[0]) { 90 data->symbol_table = load_symbol_table(mi->name); 91 } 92#ifdef CORKSCREW_HAVE_ARCH 93 load_ptrace_map_info_data_arch(pid, mi, data); 94#endif 95 } 96 } 97 } 98} 99 100ptrace_context_t* load_ptrace_context(pid_t pid) { 101 ptrace_context_t* context = 102 (ptrace_context_t*)calloc(1, sizeof(ptrace_context_t)); 103 if (context) { 104 context->map_info_list = load_map_info_list(pid); 105 for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) { 106 load_ptrace_map_info_data(pid, mi); 107 } 108 } 109 return context; 110} 111 112static void free_ptrace_map_info_data(map_info_t* mi) { 113 map_info_data_t* data = (map_info_data_t*)mi->data; 114 if (data) { 115 if (data->symbol_table) { 116 free_symbol_table(data->symbol_table); 117 } 118#ifdef CORKSCREW_HAVE_ARCH 119 free_ptrace_map_info_data_arch(mi, data); 120#endif 121 free(data); 122 mi->data = NULL; 123 } 124} 125 126void free_ptrace_context(ptrace_context_t* context) { 127 for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) { 128 free_ptrace_map_info_data(mi); 129 } 130 free_map_info_list(context->map_info_list); 131} 132 133void find_symbol_ptrace(const ptrace_context_t* context, 134 uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol) { 135 const map_info_t* mi = find_map_info(context->map_info_list, addr); 136 const symbol_t* symbol = NULL; 137 if (mi) { 138 const map_info_data_t* data = (const map_info_data_t*)mi->data; 139 if (data && data->symbol_table) { 140 symbol = find_symbol(data->symbol_table, addr - mi->start); 141 } 142 } 143 *out_map_info = mi; 144 *out_symbol = symbol; 145} 146