blob: e8032fafb21e9ad6a8af9b1585d3f0d796df4b4a [file] [log] [blame]
Petr Machata2b46cfc2012-02-18 11:17:29 +01001/*
2 * This file is part of ltrace.
Petr Machata653085a2013-01-15 17:40:40 +01003 * Copyright (C) 2010,2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata2b46cfc2012-02-18 11:17:29 +01004 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 1998,2001,2008,2009 Juan Cespedes
6 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 */
22
Petr Machata366c2f42012-02-09 19:34:36 +010023#ifndef _PROC_H_
24#define _PROC_H_
25
Petr Machata8a568dd2012-05-18 14:12:27 +020026#include "config.h"
27
Andrey Zonovd2c5dfd2012-08-05 00:16:55 +040028#include <sys/time.h>
Petr Machata653085a2013-01-15 17:40:40 +010029#include <stdint.h>
Andrey Zonovd2c5dfd2012-08-05 00:16:55 +040030
Petr Machata366c2f42012-02-09 19:34:36 +010031#if defined(HAVE_LIBUNWIND)
32# include <libunwind.h>
33#endif /* defined(HAVE_LIBUNWIND) */
34
35#include "ltrace.h"
36#include "dict.h"
Petr Machata744f2552012-04-15 04:33:18 +020037#include "sysdep.h"
Petr Machataa24021c2012-09-25 14:46:44 +020038#include "callback.h"
Petr Machata6d8aa0b2012-10-31 03:27:36 +010039#include "forward.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010040
Petr Machata366c2f42012-02-09 19:34:36 +010041struct event_handler {
42 /* Event handler that overrides the default one. Should
43 * return NULL if the event was handled, otherwise the
44 * returned event is passed to the default handler. */
45 Event *(*on_event)(struct event_handler *self, Event *event);
46
47 /* Called when the event handler removal is requested. */
48 void (*destroy)(struct event_handler *self);
49};
50
51enum process_state {
52 STATE_ATTACHED = 0,
53 STATE_BEING_CREATED,
54 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
55};
56
Petr Machataf6ec08a2012-01-06 16:58:54 +010057struct output_state {
58 size_t params_left;
59 int need_delim;
60};
61
Petr Machata366c2f42012-02-09 19:34:36 +010062struct callstack_element {
63 union {
64 int syscall;
65 struct library_symbol * libfunc;
66 } c_un;
67 int is_syscall;
Petr Machata4dcc3892013-01-30 23:11:35 +010068 arch_addr_t return_addr;
Petr Machata366c2f42012-02-09 19:34:36 +010069 struct timeval time_spent;
Petr Machataf6ec08a2012-01-06 16:58:54 +010070 struct fetch_context *fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +010071 struct value_dict *arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +010072 struct output_state out;
Petr Machata366c2f42012-02-09 19:34:36 +010073};
74
75/* XXX We should get rid of this. */
76#define MAX_CALLDEPTH 64
77
78/* XXX We would rather have this all organized a little differently,
Petr Machata929bd572012-12-17 03:20:34 +010079 * have struct process for the whole group and struct task (or struct
80 * lwp, struct thread) for what's there for per-thread stuff. But for
81 * now this is the less invasive way of structuring it. */
82struct process {
Petr Machata366c2f42012-02-09 19:34:36 +010083 enum process_state state;
Petr Machata929bd572012-12-17 03:20:34 +010084 struct process *parent; /* needed by STATE_BEING_CREATED */
Petr Machata366c2f42012-02-09 19:34:36 +010085 char * filename;
86 pid_t pid;
87
88 /* Dictionary of breakpoints (which is a mapping
89 * address->breakpoint). This is NULL for non-leader
Petr Machatad7e4ca82012-11-28 03:38:47 +010090 * processes. */
91 struct dict *breakpoints;
Petr Machata366c2f42012-02-09 19:34:36 +010092
93 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
94 unsigned int personality;
95 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
96
Petr Machataba1664b2012-04-28 14:59:05 +020097 size_t callstack_depth;
Petr Machata366c2f42012-02-09 19:34:36 +010098 struct callstack_element callstack[MAX_CALLDEPTH];
Petr Machata76dd9292012-04-03 13:02:06 +020099
100 /* Linked list of libraries in backwards order of mapping.
101 * The last element is the executed binary itself. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +0100103
Petr Machata366c2f42012-02-09 19:34:36 +0100104 /* Arch-dependent: */
Petr Machata366c2f42012-02-09 19:34:36 +0100105 void * instruction_pointer;
106 void * stack_pointer; /* To get return addr, args... */
Petr Machata366c2f42012-02-09 19:34:36 +0100107 void * arch_ptr;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400108
109 /* XXX We would like to replace this with a pointer to ABI
110 * object that would provide the relevant services, instead of
111 * checking the necessary flags in the back end ad
112 * nauseam. */
Petr Machata366c2f42012-02-09 19:34:36 +0100113 short e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400114 char e_class;
115
Petr Machata366c2f42012-02-09 19:34:36 +0100116#if defined(HAVE_LIBUNWIND)
117 /* libunwind address space */
118 unw_addr_space_t unwind_as;
119 void *unwind_priv;
120#endif /* defined(HAVE_LIBUNWIND) */
121
122 /* Set in leader. */
123 struct event_handler *event_handler;
124
125 /**
126 * Process chaining.
127 **/
Petr Machata929bd572012-12-17 03:20:34 +0100128 struct process *next;
Petr Machata366c2f42012-02-09 19:34:36 +0100129
130 /* LEADER points to the leader thread of the POSIX.1 process.
131 If X->LEADER == X, then X is the leader thread and the
Petr Machata929bd572012-12-17 03:20:34 +0100132 process structures chained by NEXT represent other threads,
Petr Machata366c2f42012-02-09 19:34:36 +0100133 up until, but not including, the next leader thread.
134 LEADER may be NULL after the leader has already exited. In
135 that case this process is waiting to be collected. */
Petr Machata929bd572012-12-17 03:20:34 +0100136 struct process *leader;
Petr Machata744f2552012-04-15 04:33:18 +0200137
Petr Machata0f6e6d92012-10-26 23:42:17 +0200138 struct os_process_data os;
Petr Machata744f2552012-04-15 04:33:18 +0200139 struct arch_process_data arch;
Petr Machata366c2f42012-02-09 19:34:36 +0100140};
141
Petr Machata75934ad2012-04-14 02:28:03 +0200142/* Initialize a process given a path to binary FILENAME, with a PID,
143 * and add the process to an internal chain of traced processes. */
Petr Machata929bd572012-12-17 03:20:34 +0100144int process_init(struct process *proc, const char *filename, pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100145
Petr Machata3d0c91c2012-04-14 02:37:38 +0200146/* PROC underwent an exec. This is a bit like process_destroy
147 * followed by process_init, except that some state is kept and the
148 * process doesn't lose it's place in the list of processes. */
Petr Machata929bd572012-12-17 03:20:34 +0100149int process_exec(struct process *proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200150
151/* Release any memory allocated for PROC (but not PROC itself). Does
152 * NOT remove PROC from internal chain.
153 *
154 * XXX clearly this init/destroy pair is different than others and
155 * should be fixed. process_init should presumably be separate from
156 * process_add. */
Petr Machata929bd572012-12-17 03:20:34 +0100157void process_destroy(struct process *proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200158
Petr Machata929bd572012-12-17 03:20:34 +0100159struct process *open_program(const char *filename, pid_t pid);
Petr Machata366c2f42012-02-09 19:34:36 +0100160void open_pid(pid_t pid);
Petr Machata929bd572012-12-17 03:20:34 +0100161struct process *pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100162
163/* Clone the contents of PROC into the memory referenced by RETP.
164 * Returns 0 on success or a negative value on failure. */
Petr Machata929bd572012-12-17 03:20:34 +0100165int process_clone(struct process *retp, struct process *proc, pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100166
Petr Machataa24021c2012-09-25 14:46:44 +0200167/* Iterate through the processes that ltrace currently traces. Tasks
168 * are considered to be processes for the purpose of this iterator.
169 * See callback.h for notes on iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100170struct process *each_process(struct process *start_after,
171 enum callback_status (*cb)(struct process *proc,
172 void *data),
173 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100174
Petr Machataa24021c2012-09-25 14:46:44 +0200175/* Iterate through list of tasks of given process PROC. See
176 * callback.h for notes on iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100177struct process *each_task(struct process *proc, struct process *start_after,
178 enum callback_status (*cb)(struct process *proc,
179 void *data),
180 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100181
Petr Machata929bd572012-12-17 03:20:34 +0100182void change_process_leader(struct process *proc, struct process *leader);
Petr Machatafd2641c2012-04-24 21:33:16 +0200183
Petr Machatadf2c88c2013-03-19 17:55:25 +0100184/* Prepare those parts of process initialization that need to be done
185 * after _start is hit (i.e. after dynamic linking was done). */
186void process_hit_start(struct process *proc);
187
Petr Machatafd2641c2012-04-24 21:33:16 +0200188/* Remove process from the list of traced processes, drop any events
189 * in the event queue, destroy it and free memory. */
Petr Machata929bd572012-12-17 03:20:34 +0100190void remove_process(struct process *proc);
Petr Machatafd2641c2012-04-24 21:33:16 +0200191
Petr Machata929bd572012-12-17 03:20:34 +0100192void install_event_handler(struct process *proc, struct event_handler *handler);
193void destroy_event_handler(struct process *proc);
Petr Machata366c2f42012-02-09 19:34:36 +0100194
Petr Machata2b46cfc2012-02-18 11:17:29 +0100195/* Add a library LIB to the list of PROC's libraries. */
Petr Machata929bd572012-12-17 03:20:34 +0100196void proc_add_library(struct process *proc, struct library *lib);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100197
198/* Remove LIB from list of PROC's libraries. Returns 0 if the library
199 * was found and unlinked, otherwise returns a negative value. */
Petr Machata929bd572012-12-17 03:20:34 +0100200int proc_remove_library(struct process *proc, struct library *lib);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100201
Petr Machataef2fd272012-09-28 00:43:01 +0200202/* Clear a delayed flag. If a symbol is neither latent, nor delayed,
203 * a breakpoint is inserted for it. Returns 0 if the activation was
204 * successful or a negative value if it failed. Note that if a symbol
205 * is both latent and delayed, this will not enable the corresponding
206 * breakpoint. */
Petr Machata929bd572012-12-17 03:20:34 +0100207int proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200208 struct library_symbol *libsym);
209
Petr Machataa24021c2012-09-25 14:46:44 +0200210/* Iterate through the libraries of PROC. See callback.h for notes on
211 * iteration interfaces. */
Petr Machata7ac04ed2012-11-23 19:00:41 +0100212struct library *proc_each_library(struct process *proc,
213 struct library *start_after,
Petr Machata929bd572012-12-17 03:20:34 +0100214 enum callback_status (*cb)(struct process *p,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215 struct library *l,
216 void *data),
217 void *data);
218
Petr Machata52dbfb12012-03-29 16:38:26 +0200219/* Insert BP into PROC. */
Petr Machata929bd572012-12-17 03:20:34 +0100220int proc_add_breakpoint(struct process *proc, struct breakpoint *bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200221
Petr Machataf7fee432012-04-19 17:00:53 +0200222/* Remove BP from PROC. This has no reason to fail in runtime. If it
223 * does not find BP in PROC, it's hard error guarded by assertion. */
Petr Machata929bd572012-12-17 03:20:34 +0100224void proc_remove_breakpoint(struct process *proc, struct breakpoint *bp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225
Petr Machataa24021c2012-09-25 14:46:44 +0200226/* Iterate through the breakpoints of PROC. See callback.h for notes
227 * on iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100228void *proc_each_breakpoint(struct process *proc, void *start,
229 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200230 struct breakpoint *bp,
231 void *data),
232 void *data);
233
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200234/* Iterate through the dynamic section at src_addr looking for D_TAG.
235 * If tag is found, fill it's value in RET and return 0.
236 * If tag is not found, return a negative value. */
Petr Machata929bd572012-12-17 03:20:34 +0100237int proc_find_dynamic_entry_addr(struct process *proc, arch_addr_t src_addr,
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200238 int d_tag, arch_addr_t *ret);
Petr Machata165b5662012-10-27 19:23:12 +0200239
240/* Finds a symbol corresponding to LIBSYM in a process PROC. Returns
241 * 0 and sets *RETLIB and *RETSYM if the corresponding pointer is
242 * non-NULL. Returns a negative value when the symbols couldn't be
243 * found. */
Petr Machata929bd572012-12-17 03:20:34 +0100244int proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +0200245 struct library **retlib, struct library_symbol **retsym);
246
Petr Machata32405542012-10-31 03:28:39 +0100247/* Iterate through all symbols in all libraries of PROC. See
248 * callback.h for notes on this interface. */
249struct library_symbol *proc_each_symbol
Petr Machata929bd572012-12-17 03:20:34 +0100250 (struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +0100251 enum callback_status (*cb)(struct library_symbol *, void *),
252 void *data);
253
Petr Machatadc70e762013-01-23 00:02:26 +0100254/* Read 8, 16, 32 or 64-bit quantity located at ADDR in PROC. The
Petr Machata653085a2013-01-15 17:40:40 +0100255 * resulting value is stored in *LP. 0 is returned on success or a
256 * negative value on failure. This uses umovebytes under the hood
257 * (see backend.h). */
Petr Machatadc70e762013-01-23 00:02:26 +0100258int proc_read_8(struct process *proc, arch_addr_t addr, uint8_t *lp);
Petr Machata653085a2013-01-15 17:40:40 +0100259int proc_read_16(struct process *proc, arch_addr_t addr, uint16_t *lp);
260int proc_read_32(struct process *proc, arch_addr_t addr, uint32_t *lp);
261int proc_read_64(struct process *proc, arch_addr_t addr, uint64_t *lp);
262
Petr Machata366c2f42012-02-09 19:34:36 +0100263#endif /* _PROC_H_ */