blob: 0040c2229b10f7abee65c75eb521a4e8299046be [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/* 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 Hughes420a7fa2012-05-17 18:28:01 -070027#include <stdint.h>
Jeff Brown501edd22011-10-19 20:35:35 -070028
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/* Stores information about a process that is used for several different
34 * ptrace() based operations. */
35typedef struct {
36 map_info_t* map_info_list;
37} ptrace_context_t;
38
Jeff Brownf0c58722011-11-03 17:58:44 -070039/* Describes how to access memory from a process. */
40typedef struct {
41 pid_t tid;
42 const map_info_t* map_info_list;
43} memory_t;
44
Jeff Brown501edd22011-10-19 20:35:35 -070045#if __i386__
46/* ptrace() register context. */
47typedef 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
68/*
Jeff Brownf0c58722011-11-03 17:58:44 -070069 * Initializes a memory structure for accessing memory from this process.
Jeff Brown501edd22011-10-19 20:35:35 -070070 */
Jeff Brownf0c58722011-11-03 17:58:44 -070071void init_memory(memory_t* memory, const map_info_t* map_info_list);
72
73/*
74 * Initializes a memory structure for accessing memory from another process
75 * using ptrace().
76 */
77void init_memory_ptrace(memory_t* memory, pid_t tid);
78
79/*
80 * Reads a word of memory safely.
81 * If the memory is local, ensures that the address is readable before dereferencing it.
82 * Returns false and a value of 0xffffffff if the word could not be read.
83 */
84bool try_get_word(const memory_t* memory, uintptr_t ptr, uint32_t* out_value);
85
86/*
87 * Reads a word of memory safely using ptrace().
88 * Returns false and a value of 0xffffffff if the word could not be read.
89 */
90bool try_get_word_ptrace(pid_t tid, uintptr_t ptr, uint32_t* out_value);
Jeff Brown501edd22011-10-19 20:35:35 -070091
92/*
93 * Loads information needed for examining a remote process using ptrace().
94 * The caller must already have successfully attached to the process
95 * using ptrace().
96 *
97 * The context can be used for any threads belonging to that process
98 * assuming ptrace() is attached to them before performing the actual
99 * unwinding. The context can continue to be used to decode backtraces
100 * even after ptrace() has been detached from the process.
101 */
102ptrace_context_t* load_ptrace_context(pid_t pid);
103
104/*
105 * Frees a ptrace context.
106 */
107void free_ptrace_context(ptrace_context_t* context);
108
109/*
110 * Finds a symbol using ptrace.
111 * Returns the containing map and information about the symbol, or
112 * NULL if one or the other is not available.
113 */
114void find_symbol_ptrace(const ptrace_context_t* context,
115 uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol);
116
117#ifdef __cplusplus
118}
119#endif
120
121#endif // _CORKSCREW_PTRACE_H