2 * This file is part of ltrace.
3 * Copyright (C) 2010,2011,2012,2013,2014 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 1998,2001,2008,2009 Juan Cespedes
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31 #if defined(HAVE_LIBDW)
32 # include <elfutils/libdwfl.h>
35 #if defined(HAVE_LIBUNWIND)
36 # include <libunwind.h>
37 # include <libunwind-ptrace.h>
38 #endif /* defined(HAVE_LIBUNWIND) */
46 struct event_handler {
47 /* Event handler that overrides the default one. Should
48 * return NULL if the event was handled, otherwise the
49 * returned event is passed to the default handler. */
50 Event *(*on_event)(struct event_handler *self, Event *event);
52 /* Called when the event handler removal is requested. */
53 void (*destroy)(struct event_handler *self);
59 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
67 struct callstack_element {
70 struct library_symbol * libfunc;
73 arch_addr_t return_addr;
74 struct timeval enter_time;
75 struct fetch_context *fetch_context;
76 struct value_dict *arguments;
77 struct output_state out;
80 /* XXX We should get rid of this. */
81 #define MAX_CALLDEPTH 64
83 /* XXX We would rather have this all organized a little differently,
84 * have struct process for the whole group and struct task (or struct
85 * lwp, struct thread) for what's there for per-thread stuff. But for
86 * now this is the less invasive way of structuring it. */
88 enum process_state state;
89 struct process *parent; /* needed by STATE_BEING_CREATED */
93 /* Dictionary of breakpoints (which is a mapping
94 * address->breakpoint). This is NULL for non-leader
96 struct dict *breakpoints;
98 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
99 unsigned int personality;
100 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
102 size_t callstack_depth;
103 struct callstack_element callstack[MAX_CALLDEPTH];
105 /* Linked list of libraries in backwards order of mapping.
106 * The last element is the executed binary itself. */
107 struct library *libraries;
109 /* Arch-dependent: */
110 void * instruction_pointer;
111 void * stack_pointer; /* To get return addr, args... */
114 /* XXX We would like to replace this with a pointer to ABI
115 * object that would provide the relevant services, instead of
116 * checking the necessary flags in the back end ad
121 #if defined(HAVE_LIBDW)
122 /* Unwind info for leader, NULL for non-leader procs. */
125 /* Whether we still need to attach the DWARF library to this process. We
126 * try only once, and never again, regardless of whether we succeeded or
127 * not. 0 = shouldn't attach */
128 int should_attach_dwfl;
130 #endif /* defined(HAVE_LIBDW) */
132 #if defined(HAVE_LIBUNWIND)
133 /* libunwind address space */
134 unw_addr_space_t unwind_as;
136 #endif /* defined(HAVE_LIBUNWIND) */
139 struct event_handler *event_handler;
144 struct process *next;
146 /* LEADER points to the leader thread of the POSIX.1 process.
147 If X->LEADER == X, then X is the leader thread and the
148 process structures chained by NEXT represent other threads,
149 up until, but not including, the next leader thread.
150 LEADER may be NULL after the leader has already exited. In
151 that case this process is waiting to be collected. */
152 struct process *leader;
154 struct os_process_data os;
155 struct arch_process_data arch;
158 /* Initialize a process given a path to binary FILENAME, with a PID,
159 * and add the process to an internal chain of traced processes. */
160 int process_init(struct process *proc, const char *filename, pid_t pid);
162 /* PROC underwent an exec. This is a bit like process_destroy
163 * followed by process_init, except that some state is kept and the
164 * process doesn't lose it's place in the list of processes. */
165 int process_exec(struct process *proc);
167 /* Release any memory allocated for PROC (but not PROC itself). Does
168 * NOT remove PROC from internal chain.
170 * XXX clearly this init/destroy pair is different than others and
171 * should be fixed. process_init should presumably be separate from
173 void process_destroy(struct process *proc);
175 struct process *open_program(const char *filename, pid_t pid);
176 void open_pid(pid_t pid);
177 struct process *pid2proc(pid_t pid);
179 /* Clone the contents of PROC into the memory referenced by RETP.
180 * Returns 0 on success or a negative value on failure. */
181 int process_clone(struct process *retp, struct process *proc, pid_t pid);
183 /* Iterate through the processes that ltrace currently traces. Tasks
184 * are considered to be processes for the purpose of this iterator.
185 * See callback.h for notes on iteration interfaces. */
186 struct process *each_process(struct process *start_after,
187 enum callback_status (*cb)(struct process *proc,
191 /* Iterate through list of tasks of given process PROC. See
192 * callback.h for notes on iteration interfaces. */
193 struct process *each_task(struct process *proc, struct process *start_after,
194 enum callback_status (*cb)(struct process *proc,
198 void change_process_leader(struct process *proc, struct process *leader);
200 /* Prepare those parts of process initialization that need to be done
201 * after _start is hit (i.e. after dynamic linking was done). */
202 void process_hit_start(struct process *proc);
204 /* Remove process from the list of traced processes, drop any events
205 * in the event queue, destroy it and free memory. */
206 void remove_process(struct process *proc);
208 void install_event_handler(struct process *proc, struct event_handler *handler);
209 void destroy_event_handler(struct process *proc);
211 /* Add a library LIB to the list of PROC's libraries. */
212 void proc_add_library(struct process *proc, struct library *lib);
214 /* Remove LIB from list of PROC's libraries. Returns 0 if the library
215 * was found and unlinked, otherwise returns a negative value. */
216 int proc_remove_library(struct process *proc, struct library *lib);
218 /* Clear a delayed flag. If a symbol is neither latent, nor delayed,
219 * a breakpoint is inserted for it. Returns 0 if the activation was
220 * successful or a negative value if it failed. Note that if a symbol
221 * is both latent and delayed, this will not enable the corresponding
223 int proc_activate_delayed_symbol(struct process *proc,
224 struct library_symbol *libsym);
226 /* Iterate through the libraries of PROC. See callback.h for notes on
227 * iteration interfaces. */
228 struct library *proc_each_library(struct process *proc,
229 struct library *start_after,
230 enum callback_status (*cb)(struct process *p,
235 /* Insert BP into PROC. */
236 int proc_add_breakpoint(struct process *proc, struct breakpoint *bp);
238 /* Remove BP from PROC. This has no reason to fail in runtime. If it
239 * does not find BP in PROC, it's hard error guarded by assertion. */
240 void proc_remove_breakpoint(struct process *proc, struct breakpoint *bp);
242 /* Iterate through the breakpoints of PROC. See callback.h for notes
243 * on iteration interfaces. */
244 arch_addr_t *proc_each_breakpoint(struct process *proc, arch_addr_t *start,
245 enum callback_status (*cb)
246 (struct process *proc,
247 struct breakpoint *bp,
251 /* Iterate through the dynamic section at src_addr looking for D_TAG.
252 * If tag is found, fill it's value in RET and return 0.
253 * If tag is not found, return a negative value. */
254 int proc_find_dynamic_entry_addr(struct process *proc, arch_addr_t src_addr,
255 int d_tag, arch_addr_t *ret);
257 /* Finds a symbol corresponding to LIBSYM in a process PROC. Returns
258 * 0 and sets *RETLIB and *RETSYM if the corresponding pointer is
259 * non-NULL. Returns a negative value when the symbols couldn't be
261 int proc_find_symbol(struct process *proc, struct library_symbol *sym,
262 struct library **retlib, struct library_symbol **retsym);
264 /* Iterate through all symbols in all libraries of PROC. See
265 * callback.h for notes on this interface. */
266 struct library_symbol *proc_each_symbol
267 (struct process *proc, struct library_symbol *start_after,
268 enum callback_status (*cb)(struct library_symbol *, void *),
271 /* Read 8, 16, 32 or 64-bit quantity located at ADDR in PROC. The
272 * resulting value is stored in *LP. 0 is returned on success or a
273 * negative value on failure. This uses umovebytes under the hood
274 * (see backend.h). */
275 int proc_read_8(struct process *proc, arch_addr_t addr, uint8_t *lp);
276 int proc_read_16(struct process *proc, arch_addr_t addr, uint16_t *lp);
277 int proc_read_32(struct process *proc, arch_addr_t addr, uint32_t *lp);
278 int proc_read_64(struct process *proc, arch_addr_t addr, uint64_t *lp);