ptrace.h revision 231e3c83a3a70b11160fb0da108ebf2e0e7470e2
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 29extern "C" { 30#endif 31 32/* Stores information about a process that is used for several different 33 * ptrace() based operations. */ 34typedef struct { 35 map_info_t* map_info_list; 36} ptrace_context_t; 37 38/* Describes how to access memory from a process. */ 39typedef struct { 40 pid_t tid; 41 const map_info_t* map_info_list; 42} memory_t; 43 44#if __i386__ 45/* ptrace() register context. */ 46typedef 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#if __mips__ 68/* ptrace() GET_REGS context. */ 69typedef struct pt_regs_mips { 70 uint64_t regs[32]; 71 uint64_t lo; 72 uint64_t hi; 73 uint64_t cp0_epc; 74 uint64_t cp0_badvaddr; 75 uint64_t cp0_status; 76 uint64_t cp0_cause; 77} pt_regs_mips_t; 78#endif 79 80/* 81 * Initializes a memory structure for accessing memory from this process. 82 */ 83void init_memory(memory_t* memory, const map_info_t* map_info_list); 84 85/* 86 * Initializes a memory structure for accessing memory from another process 87 * using ptrace(). 88 */ 89void init_memory_ptrace(memory_t* memory, pid_t tid); 90 91/* 92 * Reads a word of memory safely. 93 * If the memory is local, ensures that the address is readable before dereferencing it. 94 * Returns false and a value of 0xffffffff if the word could not be read. 95 */ 96bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value); 97 98/* 99 * Reads a word of memory safely using ptrace(). 100 * Returns false and a value of 0xffffffff if the word could not be read. 101 */ 102bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value); 103 104/* 105 * Loads information needed for examining a remote process using ptrace(). 106 * The caller must already have successfully attached to the process 107 * using ptrace(). 108 * 109 * The context can be used for any threads belonging to that process 110 * assuming ptrace() is attached to them before performing the actual 111 * unwinding. The context can continue to be used to decode backtraces 112 * even after ptrace() has been detached from the process. 113 */ 114ptrace_context_t* load_ptrace_context(pid_t pid); 115 116/* 117 * Frees a ptrace context. 118 */ 119void free_ptrace_context(ptrace_context_t* context); 120 121/* 122 * Finds a symbol using ptrace. 123 * Returns the containing map and information about the symbol, or 124 * NULL if one or the other is not available. 125 */ 126void find_symbol_ptrace(const ptrace_context_t* context, 127 uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol); 128 129#ifdef __cplusplus 130} 131#endif 132 133#endif // _CORKSCREW_PTRACE_H 134