blob: 68a969437932d670d4774231a8d13d8bb45b999d [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2012 Petr Machata, Red Hat Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 */
20
21#ifndef BACKEND_H
22#define BACKEND_H
23
24#include "forward.h"
25
26/*
27 * This file contains documentation of back end interface. Some of
28 * these may be implemented on an OS level (i.e. they are the same
29 * e.g. on all Linux architectures), some may differ per architecture
30 * on the same OS (e.g. a way to insert a breakpoint into the process
31 * image is a likely candidate).
32 */
33
34/* Convert a PID to a path to the corresponding binary. */
35char *pid2name(pid_t pid);
36
37/* Given a PID, find a leader of thread group. */
38pid_t process_leader(pid_t pid);
39
40/* Given a PID of leader thread, fill in PIDs of all the tasks. The
41 * function will initialize the pointer *RET_TASKS to a
42 * newly-allocated array, and will store number of elements in that
43 * array to *RET_N. You have to free that buffer when you don't need
44 * it anymore. */
45int process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n);
46
47/* Answer whether the process PID is stopped. Returns 0 when not
48 * stopped, 1 when stopped, or -1 when there was an error. */
49int process_stopped(pid_t pid);
50
51/* Answer a status of the task PID. See enum process_status. */
52enum process_status process_status(pid_t pid);
53
54/* Wait for PID to be ready for tracing. */
55void wait_for_proc(pid_t pid);
56
57/* Send a signal SIG to the task PID. */
58int task_kill(pid_t pid, int sig);
59
60/* Called after PID is attached, but before it is continued. */
61void trace_set_options(struct Process *proc, pid_t pid);
62
63/* Called after ltrace forks. Should attach the newly created child,
64 * in whose context this function is called. */
65void trace_me(void);
66
67/* Called when ltrace needs to attach to PID, such as when it attaches
68 * to a running process, whose PID is given on the command line. */
69int trace_pid(pid_t pid);
70
71/* Stop tracing PID. */
72void untrace_pid(pid_t pid);
73
74/* The back end may need to store arbitrary data to a process. This
75 * is a place where it can initialize PROC->arch_dep. XXX this should
76 * be dropped in favor of arhc_process_init on pmachata/libs. */
77void get_arch_dep(struct Process *proc);
78
79/* Return current instruction pointer of PROC.
80 *
81 * XXX note that the IP must fit into an arch pointer. This prevents
82 * us to use 32-bit ltrace to trace 64-bit process, even on arches
83 * that would otherwise support this. Below we have a definition of
84 * target_address_t. This should be converted to an integral type and
85 * used for target addresses throughout. */
86void *get_instruction_pointer(struct Process *proc);
87
88/* Set instruction pointer of PROC to ADDR. XXX see above. */
89void set_instruction_pointer(struct Process *proc, void *addr);
90
91/* Return current stack pointer of PROC. XXX see above. */
92void *get_stack_pointer(struct Process *proc);
93
94/* Find and return caller address, i.e. the address where the current
95 * function returns. */
96void *get_return_addr(struct Process *proc, void *stack_pointer);
97
98/* Adjust PROC so that when the current function returns, it returns
99 * to ADDR. */
100void set_return_addr(struct Process *proc, void *addr);
101
102/* Enable breakpoint SBP in process PROC. */
103void enable_breakpoint(struct Process *proc, struct breakpoint *sbp);
104
105/* Disable breakpoint SBP in process PROC. */
106void disable_breakpoint(struct Process *proc, struct breakpoint *sbp);
107
108/* Determine whether the event that we have just seen (and that is
109 * recorded in STATUS) was a syscall. If it was, return 1. If it was
110 * a return from syscall, return 2. In both cases, set *SYSNUM to the
111 * number of said syscall. If it wasn't a syscall, return 0. If
112 * there was an error, return -1. */
113int syscall_p(struct Process *proc, int status, int *sysnum);
114
115/* Continue execution of the process with given PID. */
116void continue_process(pid_t pid);
117
118/* Called after we received a signal SIGNUM. Should do whatever
119 * book-keeping is necessary and continue the process if
120 * necessary. */
121void continue_after_signal(pid_t pid, int signum);
122
123/* Called after we received a system call SYSNUM. RET_P is 0 if this
124 * is system call, otherwise it's return from a system call. The
125 * callback should do whatever book-keeping is necessary and continue
126 * the process if necessary. */
127void continue_after_syscall(struct Process *proc, int sysnum, int ret_p);
128
129/* Called after we hit a breakpoint SBP. Should do whatever
130 * book-keeping is necessary and then continue the process. */
131void continue_after_breakpoint(struct Process *proc, struct breakpoint *sbp);
132
133/* Called after we received a vfork. Should do whatever book-keeping
134 * is necessary and continue the process if necessary. N.B. right
135 * now, with Linux/GNU the only back end, this is not necessary. I
136 * imagine other systems may be different. */
137void continue_after_vfork(struct Process *proc);
138
139/* Called when trace_me or primary trace_pid fail. This may plug in
140 * any platform-specific knowledge of why it could be so. */
141void trace_fail_warning(pid_t pid);
142
143/* A pair of functions called to initiate a detachment request when
144 * ltrace is about to exit. Their job is to undo any effects that
145 * tracing had and eventually detach process, perhaps by way of
146 * installing a process handler.
147 *
148 * OS_LTRACE_EXITING_SIGHANDLER is called from a signal handler
149 * context right after the signal was captured. It returns 1 if the
150 * request was handled or 0 if it wasn't.
151 *
152 * If the call to OS_LTRACE_EXITING_SIGHANDLER didn't handle the
153 * request, OS_LTRACE_EXITING is called when the next event is
154 * generated. Therefore it's called in "safe" context, without
155 * re-entrancy concerns, but it's only called after an even is
156 * generated. */
157int os_ltrace_exiting_sighandler(void);
158void os_ltrace_exiting(void);
159
160/* Should copy COUNT bytes from address ADDR of process PROC to local
161 * buffer BUF. */
162size_t umovebytes (struct Process *proc, void *addr, void *buf, size_t count);
163
164/* Find out an address of symbol SYM in process PROC, and return.
165 * Returning NULL delays breakpoint insertion and enables heaps of
166 * arch-specific black magic that we should clean up some day.
167 *
168 * XXX the same points as for get_instruction_pointer apply. */
169void *sym2addr(struct Process *proc, struct library_symbol *sym);
170
171/* Called at some point after we have attached to PROC. This callback
172 * should insert an introspection breakpoint for artificial symbol
173 * named "" (empty string). When this breakpoint is hit,
174 * arch_check_dbg is called. */
175int linkmap_init(struct Process *proc, struct ltelf *elf);
176
177/* Called for breakpoints defined over an artificial symbol "". This
178 * can be used (like it is on Linux/GNU) to add more breakpoints
179 * because a dlopen'ed library was mapped in.
180 *
181 * XXX we should somehow clean up this interface. For starters,
182 * breakpoints should have their own handler callbacks, so that we can
183 * generalize this to e.g. systemtap SDT probes. linkmap_init could
184 * perhaps be rolled into some other process init callback. */
185void arch_check_dbg(struct Process *proc);
186
187/* This should produce and return the next event of one of the traced
188 * processes. The returned pointer will not be freed by the core and
189 * should be either statically allocated, or the management should be
190 * done some other way. */
191struct Event *next_event(void);
192
193/* Called when process PROC was removed. */
194void process_removed(struct Process *proc);
195
196int arch_elf_init(struct ltelf *lte, struct library *lib);
197void arch_elf_destroy(struct ltelf *lte);
198
199enum plt_status {
200 plt_fail,
201 plt_ok,
202 plt_default,
203};
204
205enum plt_status arch_elf_add_plt_entry(struct Process *p, struct ltelf *l,
206 const char *n, GElf_Rela *r, size_t i,
207 struct library_symbol **ret);
208
209int arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp);
210void arch_breakpoint_destroy(struct breakpoint *sbp);
211int arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp);
212
213void arch_library_init(struct library *lib);
214void arch_library_destroy(struct library *lib);
215void arch_library_clone(struct library *retp, struct library *lib);
216
217int arch_library_symbol_init(struct library_symbol *libsym);
218void arch_library_symbol_destroy(struct library_symbol *libsym);
219int arch_library_symbol_clone(struct library_symbol *retp,
220 struct library_symbol *libsym);
221
222int arch_process_init(struct Process *proc);
223void arch_process_destroy(struct Process *proc);
224int arch_process_clone(struct Process *retp, struct Process *proc);
225int arch_process_exec(struct Process *proc);
226
227typedef void *target_address_t;
228
229/* This should extract entry point address and interpreter (dynamic
230 * linker) bias if possible. Returns 0 if there were no errors, -1
231 * otherwise. Sets *ENTRYP and *INTERP_BIASP to non-zero values if
232 * the corresponding value is known. Unknown values are set to 0. */
233int process_get_entry(struct Process *proc,
234 target_address_t *entryp,
235 target_address_t *interp_biasp);
236
237/* This is called after the dynamic linker is done with the
238 * process startup. */
239void arch_dynlink_done(struct Process *proc);
240
241#endif /* BACKEND_H */