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 /* Useful ptrace() utility functions. */ 18 19 #ifndef _CORKSCREW_PTRACE_H 20 #define _CORKSCREW_PTRACE_H 21 22 #include <corkscrew/map_info.h> 23 #include <corkscrew/symbol_table.h> 24 25 #include <sys/types.h> 26 #include <stdbool.h> 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif 31 32 /* Stores information about a process that is used for several different 33 * ptrace() based operations. */ 34 typedef struct { 35 map_info_t* map_info_list; 36 } ptrace_context_t; 37 38 /* Describes how to access memory from a process. */ 39 typedef struct { 40 pid_t tid; 41 const map_info_t* map_info_list; 42 } memory_t; 43 44 #if __i386__ 45 /* ptrace() register context. */ 46 typedef struct pt_regs_x86 { 47 uint32_t ebx; 48 uint32_t ecx; 49 uint32_t edx; 50 uint32_t esi; 51 uint32_t edi; 52 uint32_t ebp; 53 uint32_t eax; 54 uint32_t xds; 55 uint32_t xes; 56 uint32_t xfs; 57 uint32_t xgs; 58 uint32_t orig_eax; 59 uint32_t eip; 60 uint32_t xcs; 61 uint32_t eflags; 62 uint32_t esp; 63 uint32_t xss; 64 } pt_regs_x86_t; 65 #endif 66 67 /* 68 * Initializes a memory structure for accessing memory from this process. 69 */ 70 void init_memory(memory_t* memory, const map_info_t* map_info_list); 71 72 /* 73 * Initializes a memory structure for accessing memory from another process 74 * using ptrace(). 75 */ 76 void init_memory_ptrace(memory_t* memory, pid_t tid); 77 78 /* 79 * Reads a word of memory safely. 80 * If the memory is local, ensures that the address is readable before dereferencing it. 81 * Returns false and a value of 0xffffffff if the word could not be read. 82 */ 83 bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value); 84 85 /* 86 * Reads a word of memory safely using ptrace(). 87 * Returns false and a value of 0xffffffff if the word could not be read. 88 */ 89 bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value); 90 91 /* 92 * Loads information needed for examining a remote process using ptrace(). 93 * The caller must already have successfully attached to the process 94 * using ptrace(). 95 * 96 * The context can be used for any threads belonging to that process 97 * assuming ptrace() is attached to them before performing the actual 98 * unwinding. The context can continue to be used to decode backtraces 99 * even after ptrace() has been detached from the process. 100 */ 101 ptrace_context_t* load_ptrace_context(pid_t pid); 102 103 /* 104 * Frees a ptrace context. 105 */ 106 void free_ptrace_context(ptrace_context_t* context); 107 108 /* 109 * Finds a symbol using ptrace. 110 * Returns the containing map and information about the symbol, or 111 * NULL if one or the other is not available. 112 */ 113 void find_symbol_ptrace(const ptrace_context_t* context, 114 uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol); 115 116 #ifdef __cplusplus 117 } 118 #endif 119 120 #endif // _CORKSCREW_PTRACE_H 121