ptrace.h revision 501edd29b823ce1301d2effdd3a9e4b6e2b20b76
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#if __i386__
39/* ptrace() register context. */
40typedef struct pt_regs_x86 {
41    uint32_t ebx;
42    uint32_t ecx;
43    uint32_t edx;
44    uint32_t esi;
45    uint32_t edi;
46    uint32_t ebp;
47    uint32_t eax;
48    uint32_t xds;
49    uint32_t xes;
50    uint32_t xfs;
51    uint32_t xgs;
52    uint32_t orig_eax;
53    uint32_t eip;
54    uint32_t xcs;
55    uint32_t eflags;
56    uint32_t esp;
57    uint32_t xss;
58} pt_regs_x86_t;
59#endif
60
61/*
62 * Reads a word of memory safely.
63 * Uses ptrace() if tid >= 0, local memory otherwise.
64 * Returns false if the word could not be read.
65 */
66bool try_get_word(pid_t tid, uintptr_t ptr, uint32_t* out_value);
67
68/*
69 * Loads information needed for examining a remote process using ptrace().
70 * The caller must already have successfully attached to the process
71 * using ptrace().
72 *
73 * The context can be used for any threads belonging to that process
74 * assuming ptrace() is attached to them before performing the actual
75 * unwinding.  The context can continue to be used to decode backtraces
76 * even after ptrace() has been detached from the process.
77 */
78ptrace_context_t* load_ptrace_context(pid_t pid);
79
80/*
81 * Frees a ptrace context.
82 */
83void free_ptrace_context(ptrace_context_t* context);
84
85/*
86 * Finds a symbol using ptrace.
87 * Returns the containing map and information about the symbol, or
88 * NULL if one or the other is not available.
89 */
90void find_symbol_ptrace(const ptrace_context_t* context,
91        uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol);
92
93#ifdef __cplusplus
94}
95#endif
96
97#endif // _CORKSCREW_PTRACE_H
98