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