blob: 776ef69f25588f5a792506971960af99d11f0a1d [file] [log] [blame]
Jeff Brown501edd22011-10-19 20:35:35 -07001/*
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 Hughes420a7fa2012-05-17 18:28:01 -070024#include <stdlib.h>
Jeff Brown501edd22011-10-19 20:35:35 -070025#include <sys/ptrace.h>
26#include <cutils/log.h>
27
28static 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 Brownf0c58722011-11-03 17:58:44 -070038void 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
43void init_memory_ptrace(memory_t* memory, pid_t tid) {
44 memory->tid = tid;
45 memory->map_info_list = NULL;
46}
47
48bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value) {
49 ALOGV("try_get_word: reading word at 0x%08x", ptr);
Jeff Brown501edd22011-10-19 20:35:35 -070050 if (ptr & 3) {
Steve Block69f4cd72011-10-20 11:54:09 +010051 ALOGV("try_get_word: invalid pointer 0x%08x", ptr);
Jeff Brownf0c58722011-11-03 17:58:44 -070052 *out_value = 0xffffffffL;
Jeff Brown501edd22011-10-19 20:35:35 -070053 return false;
54 }
Jeff Brownf0c58722011-11-03 17:58:44 -070055 if (memory->tid < 0) {
56 if (!is_readable_map(memory->map_info_list, ptr)) {
57 ALOGV("try_get_word: pointer 0x%08x not in a readable map", ptr);
58 *out_value = 0xffffffffL;
59 return false;
Jeff Brown501edd22011-10-19 20:35:35 -070060 }
Jeff Brown501edd22011-10-19 20:35:35 -070061 *out_value = *(uint32_t*)ptr;
62 return true;
63 } else {
64 // ptrace() returns -1 and sets errno when the operation fails.
65 // To disambiguate -1 from a valid result, we clear errno beforehand.
66 errno = 0;
Jeff Brownf0c58722011-11-03 17:58:44 -070067 *out_value = ptrace(PTRACE_PEEKTEXT, memory->tid, (void*)ptr, NULL);
Jeff Brown501edd22011-10-19 20:35:35 -070068 if (*out_value == 0xffffffffL && errno) {
Jeff Brownf0c58722011-11-03 17:58:44 -070069 ALOGV("try_get_word: invalid pointer 0x%08x reading from tid %d, "
70 "ptrace() errno=%d", ptr, memory->tid, errno);
Jeff Brown501edd22011-10-19 20:35:35 -070071 return false;
72 }
73 return true;
74 }
75}
76
Jeff Brownf0c58722011-11-03 17:58:44 -070077bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value) {
78 memory_t memory;
79 init_memory_ptrace(&memory, tid);
80 return try_get_word(&memory, ptr, out_value);
81}
82
Jeff Brown501edd22011-10-19 20:35:35 -070083static void load_ptrace_map_info_data(pid_t pid, map_info_t* mi) {
Jeff Brownf0c58722011-11-03 17:58:44 -070084 if (mi->is_executable && mi->is_readable) {
Jeff Brown501edd22011-10-19 20:35:35 -070085 uint32_t elf_magic;
Jeff Brownf0c58722011-11-03 17:58:44 -070086 if (try_get_word_ptrace(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) {
Jeff Brown501edd22011-10-19 20:35:35 -070087 map_info_data_t* data = (map_info_data_t*)calloc(1, sizeof(map_info_data_t));
88 if (data) {
89 mi->data = data;
90 if (mi->name[0]) {
91 data->symbol_table = load_symbol_table(mi->name);
92 }
93#ifdef CORKSCREW_HAVE_ARCH
94 load_ptrace_map_info_data_arch(pid, mi, data);
95#endif
96 }
97 }
98 }
99}
100
101ptrace_context_t* load_ptrace_context(pid_t pid) {
102 ptrace_context_t* context =
103 (ptrace_context_t*)calloc(1, sizeof(ptrace_context_t));
104 if (context) {
105 context->map_info_list = load_map_info_list(pid);
106 for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) {
107 load_ptrace_map_info_data(pid, mi);
108 }
109 }
110 return context;
111}
112
113static void free_ptrace_map_info_data(map_info_t* mi) {
114 map_info_data_t* data = (map_info_data_t*)mi->data;
115 if (data) {
116 if (data->symbol_table) {
117 free_symbol_table(data->symbol_table);
118 }
119#ifdef CORKSCREW_HAVE_ARCH
120 free_ptrace_map_info_data_arch(mi, data);
121#endif
122 free(data);
123 mi->data = NULL;
124 }
125}
126
127void free_ptrace_context(ptrace_context_t* context) {
128 for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) {
129 free_ptrace_map_info_data(mi);
130 }
131 free_map_info_list(context->map_info_list);
Liangtao Gao0632ad42013-03-20 19:48:19 +0800132 free(context);
Jeff Brown501edd22011-10-19 20:35:35 -0700133}
134
135void find_symbol_ptrace(const ptrace_context_t* context,
136 uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol) {
137 const map_info_t* mi = find_map_info(context->map_info_list, addr);
138 const symbol_t* symbol = NULL;
139 if (mi) {
140 const map_info_data_t* data = (const map_info_data_t*)mi->data;
141 if (data && data->symbol_table) {
142 symbol = find_symbol(data->symbol_table, addr - mi->start);
143 }
144 }
145 *out_map_info = mi;
146 *out_symbol = symbol;
147}