blob: 2767a94b9b8733e4119ed77fc749b9441267c960 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
Petr Machata693dfad2013-01-14 22:10:51 +01003 * Copyright (C) 2012,2013 Petr Machata, Red Hat Inc.
Petr Machata64262602012-01-07 03:41:36 +01004 *
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"
Petr Machatabac2da52012-05-29 00:42:59 +020025#include "sysdep.h"
26
Petr Machataba1664b2012-04-28 14:59:05 +020027#include <gelf.h>
28
29enum process_status {
Petr Machata6dfc5442012-12-17 03:38:36 +010030 PS_INVALID, /* Failure. */
31 PS_STOP, /* Job-control stop. */
32 PS_TRACING_STOP,
33 PS_SLEEPING,
34 PS_ZOMBIE,
35 PS_OTHER, /* Necessary other states can be added as needed. */
Petr Machataba1664b2012-04-28 14:59:05 +020036};
37
Petr Machata64262602012-01-07 03:41:36 +010038/*
39 * This file contains documentation of back end interface. Some of
40 * these may be implemented on an OS level (i.e. they are the same
41 * e.g. on all Linux architectures), some may differ per architecture
42 * on the same OS (e.g. a way to insert a breakpoint into the process
43 * image is a likely candidate).
44 */
45
46/* Convert a PID to a path to the corresponding binary. */
47char *pid2name(pid_t pid);
48
49/* Given a PID, find a leader of thread group. */
50pid_t process_leader(pid_t pid);
51
52/* Given a PID of leader thread, fill in PIDs of all the tasks. The
53 * function will initialize the pointer *RET_TASKS to a
54 * newly-allocated array, and will store number of elements in that
55 * array to *RET_N. You have to free that buffer when you don't need
56 * it anymore. */
57int process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n);
58
59/* Answer whether the process PID is stopped. Returns 0 when not
60 * stopped, 1 when stopped, or -1 when there was an error. */
61int process_stopped(pid_t pid);
62
63/* Answer a status of the task PID. See enum process_status. */
64enum process_status process_status(pid_t pid);
65
66/* Wait for PID to be ready for tracing. */
Petr Machataba1664b2012-04-28 14:59:05 +020067int wait_for_proc(pid_t pid);
Petr Machata64262602012-01-07 03:41:36 +010068
69/* Send a signal SIG to the task PID. */
70int task_kill(pid_t pid, int sig);
71
72/* Called after PID is attached, but before it is continued. */
Petr Machata929bd572012-12-17 03:20:34 +010073void trace_set_options(struct process *proc);
Petr Machata64262602012-01-07 03:41:36 +010074
75/* Called after ltrace forks. Should attach the newly created child,
76 * in whose context this function is called. */
77void trace_me(void);
78
79/* Called when ltrace needs to attach to PID, such as when it attaches
80 * to a running process, whose PID is given on the command line. */
81int trace_pid(pid_t pid);
82
83/* Stop tracing PID. */
84void untrace_pid(pid_t pid);
85
86/* The back end may need to store arbitrary data to a process. This
87 * is a place where it can initialize PROC->arch_dep. XXX this should
88 * be dropped in favor of arhc_process_init on pmachata/libs. */
Petr Machata929bd572012-12-17 03:20:34 +010089void get_arch_dep(struct process *proc);
Petr Machata64262602012-01-07 03:41:36 +010090
91/* Return current instruction pointer of PROC.
92 *
93 * XXX note that the IP must fit into an arch pointer. This prevents
94 * us to use 32-bit ltrace to trace 64-bit process, even on arches
Petr Machataba1664b2012-04-28 14:59:05 +020095 * that would otherwise support this. Above we have a definition of
Petr Machatabac2da52012-05-29 00:42:59 +020096 * arch_addr_t. This should be converted to an integral type and
Petr Machata64262602012-01-07 03:41:36 +010097 * used for target addresses throughout. */
Petr Machata929bd572012-12-17 03:20:34 +010098void *get_instruction_pointer(struct process *proc);
Petr Machata64262602012-01-07 03:41:36 +010099
100/* Set instruction pointer of PROC to ADDR. XXX see above. */
Petr Machata929bd572012-12-17 03:20:34 +0100101void set_instruction_pointer(struct process *proc, void *addr);
Petr Machata64262602012-01-07 03:41:36 +0100102
103/* Return current stack pointer of PROC. XXX see above. */
Petr Machata929bd572012-12-17 03:20:34 +0100104void *get_stack_pointer(struct process *proc);
Petr Machata64262602012-01-07 03:41:36 +0100105
106/* Find and return caller address, i.e. the address where the current
107 * function returns. */
Petr Machata929bd572012-12-17 03:20:34 +0100108void *get_return_addr(struct process *proc, void *stack_pointer);
Petr Machata64262602012-01-07 03:41:36 +0100109
110/* Adjust PROC so that when the current function returns, it returns
111 * to ADDR. */
Petr Machata929bd572012-12-17 03:20:34 +0100112void set_return_addr(struct process *proc, void *addr);
Petr Machata64262602012-01-07 03:41:36 +0100113
114/* Enable breakpoint SBP in process PROC. */
Petr Machata929bd572012-12-17 03:20:34 +0100115void enable_breakpoint(struct process *proc, struct breakpoint *sbp);
Petr Machata64262602012-01-07 03:41:36 +0100116
117/* Disable breakpoint SBP in process PROC. */
Petr Machata929bd572012-12-17 03:20:34 +0100118void disable_breakpoint(struct process *proc, struct breakpoint *sbp);
Petr Machata64262602012-01-07 03:41:36 +0100119
120/* Determine whether the event that we have just seen (and that is
121 * recorded in STATUS) was a syscall. If it was, return 1. If it was
122 * a return from syscall, return 2. In both cases, set *SYSNUM to the
123 * number of said syscall. If it wasn't a syscall, return 0. If
124 * there was an error, return -1. */
Petr Machata929bd572012-12-17 03:20:34 +0100125int syscall_p(struct process *proc, int status, int *sysnum);
Petr Machata64262602012-01-07 03:41:36 +0100126
127/* Continue execution of the process with given PID. */
128void continue_process(pid_t pid);
129
130/* Called after we received a signal SIGNUM. Should do whatever
131 * book-keeping is necessary and continue the process if
132 * necessary. */
133void continue_after_signal(pid_t pid, int signum);
134
135/* Called after we received a system call SYSNUM. RET_P is 0 if this
136 * is system call, otherwise it's return from a system call. The
137 * callback should do whatever book-keeping is necessary and continue
138 * the process if necessary. */
Petr Machata929bd572012-12-17 03:20:34 +0100139void continue_after_syscall(struct process *proc, int sysnum, int ret_p);
Petr Machata64262602012-01-07 03:41:36 +0100140
141/* Called after we hit a breakpoint SBP. Should do whatever
142 * book-keeping is necessary and then continue the process. */
Petr Machata929bd572012-12-17 03:20:34 +0100143void continue_after_breakpoint(struct process *proc, struct breakpoint *sbp);
Petr Machata64262602012-01-07 03:41:36 +0100144
145/* Called after we received a vfork. Should do whatever book-keeping
146 * is necessary and continue the process if necessary. N.B. right
147 * now, with Linux/GNU the only back end, this is not necessary. I
148 * imagine other systems may be different. */
Petr Machata929bd572012-12-17 03:20:34 +0100149void continue_after_vfork(struct process *proc);
Petr Machata64262602012-01-07 03:41:36 +0100150
Petr Machata057caa52013-01-30 23:28:47 +0100151/* Called after the process exec's. Should do whatever book-keeping
152 * is necessary and then continue the process. */
153void continue_after_exec(struct process *proc);
154
Petr Machata64262602012-01-07 03:41:36 +0100155/* Called when trace_me or primary trace_pid fail. This may plug in
156 * any platform-specific knowledge of why it could be so. */
157void trace_fail_warning(pid_t pid);
158
159/* A pair of functions called to initiate a detachment request when
160 * ltrace is about to exit. Their job is to undo any effects that
161 * tracing had and eventually detach process, perhaps by way of
162 * installing a process handler.
163 *
164 * OS_LTRACE_EXITING_SIGHANDLER is called from a signal handler
165 * context right after the signal was captured. It returns 1 if the
166 * request was handled or 0 if it wasn't.
167 *
168 * If the call to OS_LTRACE_EXITING_SIGHANDLER didn't handle the
169 * request, OS_LTRACE_EXITING is called when the next event is
170 * generated. Therefore it's called in "safe" context, without
171 * re-entrancy concerns, but it's only called after an even is
172 * generated. */
173int os_ltrace_exiting_sighandler(void);
174void os_ltrace_exiting(void);
175
176/* Should copy COUNT bytes from address ADDR of process PROC to local
177 * buffer BUF. */
Petr Machata929bd572012-12-17 03:20:34 +0100178size_t umovebytes(struct process *proc, void *addr, void *buf, size_t count);
Petr Machata64262602012-01-07 03:41:36 +0100179
180/* Find out an address of symbol SYM in process PROC, and return.
181 * Returning NULL delays breakpoint insertion and enables heaps of
182 * arch-specific black magic that we should clean up some day.
183 *
184 * XXX the same points as for get_instruction_pointer apply. */
Petr Machata929bd572012-12-17 03:20:34 +0100185void *sym2addr(struct process *proc, struct library_symbol *sym);
Petr Machata64262602012-01-07 03:41:36 +0100186
Petr Machata311358a2012-09-22 15:24:06 +0200187/* Obtain address of PLT entry corresponding to relocation RELA in
188 * file LTE. This is NDX-th PLT entry in the file.
189 *
190 * XXX should this return arch_addr_t? */
191GElf_Addr arch_plt_sym_val(struct ltelf *lte, size_t ndx, GElf_Rela *rela);
192
Petr Machata64262602012-01-07 03:41:36 +0100193/* Called at some point after we have attached to PROC. This callback
Petr Machataba1664b2012-04-28 14:59:05 +0200194 * should insert an introspection breakpoint for handling dynamic
195 * linker library loads. */
Petr Machata929bd572012-12-17 03:20:34 +0100196int linkmap_init(struct process *proc, arch_addr_t dyn_addr);
Petr Machata64262602012-01-07 03:41:36 +0100197
Petr Machata64262602012-01-07 03:41:36 +0100198/* This should produce and return the next event of one of the traced
199 * processes. The returned pointer will not be freed by the core and
200 * should be either statically allocated, or the management should be
201 * done some other way. */
202struct Event *next_event(void);
203
204/* Called when process PROC was removed. */
Petr Machata929bd572012-12-17 03:20:34 +0100205void process_removed(struct process *proc);
Petr Machata64262602012-01-07 03:41:36 +0100206
Petr Machata311358a2012-09-22 15:24:06 +0200207/* This should extract entry point address and interpreter (dynamic
208 * linker) bias if possible. Returns 0 if there were no errors, -1
209 * otherwise. Sets *ENTRYP and *INTERP_BIASP to non-zero values if
Petr Machatada69ed02012-10-18 17:27:48 +0200210 * the corresponding value is known, or zero otherwise; this is not
211 * done for pointers that are NULL. */
Petr Machata929bd572012-12-17 03:20:34 +0100212int process_get_entry(struct process *proc,
Petr Machata311358a2012-09-22 15:24:06 +0200213 arch_addr_t *entryp,
214 arch_addr_t *interp_biasp);
215
216
217/* Optional callbacks
218 *
219 * Some callbacks are only available if backend (arch.h) has a certain
220 * define. If such a define is not present, default implementation
221 * (most often doing nothing at all) us used instead. This is used
222 * for gradual extensions of ltrace, so that backends that are not
223 * fully up to date, or that don't need certain functionality, keep
224 * working, while other backends take advantage of the optional
225 * features. */
226
227/* The following callbacks have to be implemented in backend if arch.h
228 * defines ARCH_HAVE_LTELF_DATA. Those are used to init and destroy
229 * LTE->arch. arch_elf_init returns 0 on success or a negative value
230 * on failure. */
Petr Machata64262602012-01-07 03:41:36 +0100231int arch_elf_init(struct ltelf *lte, struct library *lib);
232void arch_elf_destroy(struct ltelf *lte);
233
Petr Machata311358a2012-09-22 15:24:06 +0200234/* The following callbacks have to be implemented in backend if arch.h
235 * defines ARCH_HAVE_BREAKPOINT_DATA. Those are used to init,
236 * destroy, and clone SBP->arch. arch_breakpoint_init and
237 * arch_breakpoint_clone return 0 on success or a negative value on
238 * failure. */
Petr Machata929bd572012-12-17 03:20:34 +0100239int arch_breakpoint_init(struct process *proc, struct breakpoint *sbp);
Petr Machata311358a2012-09-22 15:24:06 +0200240void arch_breakpoint_destroy(struct breakpoint *sbp);
241int arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp);
242
243/* The following callbacks have to be implemented in backend if arch.h
244 * defines ARCH_HAVE_LIBRARY_DATA. Those are used to init, destroy
245 * and clone LIB->arch. */
246void arch_library_init(struct library *lib);
247void arch_library_destroy(struct library *lib);
248void arch_library_clone(struct library *retp, struct library *lib);
249
250/* The following callbacks have to be implemented in backend if arch.h
251 * defines ARCH_HAVE_LIBRARY_SYMBOL_DATA. Those are used to init,
252 * destroy and clone LIBSYM->arch. arch_library_symbol_init and
253 * arch_library_symbol_clone return 0 on success or a negative value
254 * on failure. */
255int arch_library_symbol_init(struct library_symbol *libsym);
256void arch_library_symbol_destroy(struct library_symbol *libsym);
257int arch_library_symbol_clone(struct library_symbol *retp,
258 struct library_symbol *libsym);
259
260/* The following callbacks have to be implemented in backend if arch.h
261 * defines ARCH_HAVE_PROCESS_DATA. Those are used to init, destroy
262 * and clone PROC->arch. arch_process_exec is called to update
263 * PROC->arch in case that PROC underwent an exec. See notes at
264 * process_init, process_destroy, process_clone and process_exec in
265 * proc.h. */
Petr Machata929bd572012-12-17 03:20:34 +0100266int arch_process_init(struct process *proc);
267void arch_process_destroy(struct process *proc);
268int arch_process_clone(struct process *retp, struct process *proc);
269int arch_process_exec(struct process *proc);
Petr Machata311358a2012-09-22 15:24:06 +0200270
Petr Machata0f6e6d92012-10-26 23:42:17 +0200271/* The following callbacks have to be implemented in OS backend if
272 * os.h defines OS_HAVE_PROCESS_DATA. The protocol is same as for,
273 * respectively, arch_process_init, arch_process_destroy,
274 * arch_process_clone and arch_process_exec. */
Petr Machata929bd572012-12-17 03:20:34 +0100275int os_process_init(struct process *proc);
276void os_process_destroy(struct process *proc);
277int os_process_clone(struct process *retp, struct process *proc);
278int os_process_exec(struct process *proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200279
Edgar E. Iglesiasb5920d12012-09-27 12:07:35 +0200280/* The following callback has to be implemented in backend if arch.h
281 * defines ARCH_HAVE_GET_SYM_INFO.
282 *
283 * This is called for every PLT relocation R in ELF file LTE, that
284 * ltrace is about to add to it's internal representation of the
285 * program under trace.
286 * The corresponding PLT entry is for SYM_INDEX-th relocation in the file.
287 *
288 * The callback is responsible for initializing RELA and SYM.
289 *
290 * Return 0 if OK.
291 * Return a negative value if this symbol (SYM_INDEX) should be ignored. */
292int arch_get_sym_info(struct ltelf *lte, const char *filename,
293 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym);
294
Petr Machata64262602012-01-07 03:41:36 +0100295enum plt_status {
Petr Machataade3b972012-12-17 03:45:30 +0100296 PLT_FAIL,
297 PLT_OK,
298 PLT_DEFAULT,
Petr Machata64262602012-01-07 03:41:36 +0100299};
300
Petr Machata311358a2012-09-22 15:24:06 +0200301/* The following callback has to be implemented in backend if arch.h
302 * defines ARCH_HAVE_ADD_PLT_ENTRY.
303 *
304 * This is called for every PLT relocation R in ELF file LTE, that
305 * ltrace is about to add to a library constructed in process PROC.
306 * The corresponding PLT entry is for symbol called NAME, and it's
307 * I-th relocation in the file.
308 *
Petr Machataade3b972012-12-17 03:45:30 +0100309 * If this function returns PLT_DEFAULT, PLT address is obtained by
310 * calling arch_plt_sym_val, and symbol is allocated. If PLT_OK or
311 * PLT_DEFAULT are returned, the chain of symbols passed back in RET
Petr Machata311358a2012-09-22 15:24:06 +0200312 * is added to library under construction. */
Petr Machata929bd572012-12-17 03:20:34 +0100313enum plt_status arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
Petr Machata311358a2012-09-22 15:24:06 +0200314 const char *name, GElf_Rela *rela,
315 size_t i, struct library_symbol **ret);
Petr Machata64262602012-01-07 03:41:36 +0100316
Petr Machata311358a2012-09-22 15:24:06 +0200317/* This callback needs to be implemented if arch.h defines
318 * ARCH_HAVE_DYNLINK_DONE. It is called after the dynamic linker is
Petr Machata9af3f3a2012-12-04 17:42:14 +0100319 * done with the process start-up. */
Petr Machata929bd572012-12-17 03:20:34 +0100320void arch_dynlink_done(struct process *proc);
Petr Machata64262602012-01-07 03:41:36 +0100321
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200322/* This callback needs to be implemented if arch.h defines
323 * ARCH_HAVE_SYMBOL_RET. It is called after a traced call returns. */
Petr Machata929bd572012-12-17 03:20:34 +0100324void arch_symbol_ret(struct process *proc, struct library_symbol *libsym);
Edgar E. Iglesias3a1806d2012-09-27 17:02:40 +0200325
Edgar E. Iglesias11c3c3e2012-10-09 12:16:52 +0200326
327/* This callback needs to be implemented if arch.h defines
328 * ARCH_HAVE_FIND_DL_DEBUG.
329 * It is called by generic code to find the address of the dynamic
330 * linkers debug structure.
331 * DYN_ADDR holds the address of the dynamic section.
332 * If the debug area is found, return 0 and fill in the address in *RET.
333 * If the debug area is not found, return a negative value. */
Petr Machata929bd572012-12-17 03:20:34 +0100334int arch_find_dl_debug(struct process *proc, arch_addr_t dyn_addr,
Edgar E. Iglesias11c3c3e2012-10-09 12:16:52 +0200335 arch_addr_t *ret);
336
Petr Machata9af3f3a2012-12-04 17:42:14 +0100337/* This is called to obtain a list of directories to search when
338 * loading config files. The callback sets *RETP to a pointer to the
339 * first element of a NULL-terminated array of directory names. It's
340 * legitimate to set *RETP to NULL to indicate there are no
341 * directories. The function returns 0 on success or a negative value
342 * on a failure.
343 *
344 * If PRIVATE is set, the list in *RETP should contain only user's own
345 * directories (presumably under HOME if there's any such thing on the
346 * given OS). Otherwise only system directories should be reported.
347 *
348 * The directories don't have to exist. Directories passed in -F are
349 * handled separately by the caller and this callback shouldn't
350 * concern itself with it. */
351int os_get_config_dirs(int private, const char ***retp);
352
Petr Machata311358a2012-09-22 15:24:06 +0200353/* If arch.h defines ARCH_HAVE_FETCH_ARG, the following callbacks have
354 * to be implemented: arch_fetch_arg_init, arch_fetch_arg_clone,
355 * arch_fetch_arg_done, arch_fetch_arg_next and arch_fetch_retval.
356 * See fetch.h for details. */
357
358/* If arch.h defines both ARCH_HAVE_FETCH_ARG and
359 * ARCH_HAVE_FETCH_PACK, the following callbacks have to be
360 * implemented: arch_fetch_param_pack_start,
361 * arch_fetch_param_pack_end. See fetch.h for details. */
362
Petr Machata693dfad2013-01-14 22:10:51 +0100363enum sw_singlestep_status {
364 SWS_FAIL,
365 SWS_OK,
366 SWS_HW,
367};
368struct sw_singlestep_data;
369
370/* The following callback has to be implemented in backend if arch.h
371 * defines ARCH_HAVE_SW_SINGLESTEP.
372 *
373 * This is called before the OS backend requests hardware singlestep.
374 * arch_sw_singlestep should consider whether a singlestep needs to be
375 * done in software. If not, it returns SWS_HW. Otherwise it needs
376 * to add one or several breakpoints by calling ADD_CB. When it is
377 * done, it continues the process as appropriate, and answers either
378 * SWS_OK, or SWS_FAIL, depending on how it went.
379 *
380 * PROC is the process that should perform the singlestep, BP the
381 * breakpoint that we are singlestepping over. ADD_CB is a callback
382 * to request adding breakpoints that should trap the process after
383 * it's continued. The arguments to ADD_CB are the address where the
384 * breakpoint should be added, and DATA. ADD_CB returns 0 on success
385 * or a negative value on failure. It is expected that
386 * arch_sw_singlestep returns SWS_FAIL if ADD_CB returns error. */
387enum sw_singlestep_status arch_sw_singlestep(struct process *proc,
388 struct breakpoint *bp,
389 int (*add_cb)(arch_addr_t addr,
390 struct sw_singlestep_data *),
391 struct sw_singlestep_data *data);
392
Petr Machata64262602012-01-07 03:41:36 +0100393#endif /* BACKEND_H */