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 | /* 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> |
Elliott Hughes | 420a7fa | 2012-05-17 18:28:01 -0700 | [diff] [blame] | 27 | #include <stdint.h> |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | /* Stores information about a process that is used for several different |
| 34 | * ptrace() based operations. */ |
| 35 | typedef struct { |
| 36 | map_info_t* map_info_list; |
| 37 | } ptrace_context_t; |
| 38 | |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 39 | /* Describes how to access memory from a process. */ |
| 40 | typedef struct { |
| 41 | pid_t tid; |
| 42 | const map_info_t* map_info_list; |
| 43 | } memory_t; |
| 44 | |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 45 | #if __i386__ |
| 46 | /* ptrace() register context. */ |
| 47 | typedef struct pt_regs_x86 { |
| 48 | uint32_t ebx; |
| 49 | uint32_t ecx; |
| 50 | uint32_t edx; |
| 51 | uint32_t esi; |
| 52 | uint32_t edi; |
| 53 | uint32_t ebp; |
| 54 | uint32_t eax; |
| 55 | uint32_t xds; |
| 56 | uint32_t xes; |
| 57 | uint32_t xfs; |
| 58 | uint32_t xgs; |
| 59 | uint32_t orig_eax; |
| 60 | uint32_t eip; |
| 61 | uint32_t xcs; |
| 62 | uint32_t eflags; |
| 63 | uint32_t esp; |
| 64 | uint32_t xss; |
| 65 | } pt_regs_x86_t; |
| 66 | #endif |
| 67 | |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 68 | #if __mips__ |
| 69 | /* ptrace() GET_REGS context. */ |
| 70 | typedef struct pt_regs_mips { |
| 71 | uint64_t regs[32]; |
| 72 | uint64_t lo; |
| 73 | uint64_t hi; |
| 74 | uint64_t cp0_epc; |
| 75 | uint64_t cp0_badvaddr; |
| 76 | uint64_t cp0_status; |
| 77 | uint64_t cp0_cause; |
| 78 | } pt_regs_mips_t; |
| 79 | #endif |
| 80 | |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 81 | /* |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 82 | * Initializes a memory structure for accessing memory from this process. |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 83 | */ |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 84 | void init_memory(memory_t* memory, const map_info_t* map_info_list); |
| 85 | |
| 86 | /* |
| 87 | * Initializes a memory structure for accessing memory from another process |
| 88 | * using ptrace(). |
| 89 | */ |
| 90 | void init_memory_ptrace(memory_t* memory, pid_t tid); |
| 91 | |
| 92 | /* |
| 93 | * Reads a word of memory safely. |
| 94 | * If the memory is local, ensures that the address is readable before dereferencing it. |
| 95 | * Returns false and a value of 0xffffffff if the word could not be read. |
| 96 | */ |
| 97 | bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value); |
| 98 | |
| 99 | /* |
| 100 | * Reads a word of memory safely using ptrace(). |
| 101 | * Returns false and a value of 0xffffffff if the word could not be read. |
| 102 | */ |
| 103 | bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Loads information needed for examining a remote process using ptrace(). |
| 107 | * The caller must already have successfully attached to the process |
| 108 | * using ptrace(). |
| 109 | * |
| 110 | * The context can be used for any threads belonging to that process |
| 111 | * assuming ptrace() is attached to them before performing the actual |
| 112 | * unwinding. The context can continue to be used to decode backtraces |
| 113 | * even after ptrace() has been detached from the process. |
| 114 | */ |
| 115 | ptrace_context_t* load_ptrace_context(pid_t pid); |
| 116 | |
| 117 | /* |
| 118 | * Frees a ptrace context. |
| 119 | */ |
| 120 | void free_ptrace_context(ptrace_context_t* context); |
| 121 | |
| 122 | /* |
| 123 | * Finds a symbol using ptrace. |
| 124 | * Returns the containing map and information about the symbol, or |
| 125 | * NULL if one or the other is not available. |
| 126 | */ |
| 127 | void find_symbol_ptrace(const ptrace_context_t* context, |
| 128 | uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol); |
| 129 | |
| 130 | #ifdef __cplusplus |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | #endif // _CORKSCREW_PTRACE_H |