blob: 68054b33d16b1b6e6b4545f1c8536f87e55fb37f [file] [log] [blame]
Petr Machata2b46cfc2012-02-18 11:17:29 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2010,2011,2012 Petr Machata, Red Hat Inc.
4 * 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>
29
Petr Machata366c2f42012-02-09 19:34:36 +010030#if defined(HAVE_LIBUNWIND)
31# include <libunwind.h>
32#endif /* defined(HAVE_LIBUNWIND) */
33
34#include "ltrace.h"
35#include "dict.h"
Petr Machata744f2552012-04-15 04:33:18 +020036#include "sysdep.h"
Petr Machataa24021c2012-09-25 14:46:44 +020037#include "callback.h"
Petr Machata366c2f42012-02-09 19:34:36 +010038
Petr Machata2b46cfc2012-02-18 11:17:29 +010039struct library;
Petr Machata52dbfb12012-03-29 16:38:26 +020040struct breakpoint;
Petr Machata2b46cfc2012-02-18 11:17:29 +010041
Petr Machata366c2f42012-02-09 19:34:36 +010042struct event_handler {
43 /* Event handler that overrides the default one. Should
44 * return NULL if the event was handled, otherwise the
45 * returned event is passed to the default handler. */
46 Event *(*on_event)(struct event_handler *self, Event *event);
47
48 /* Called when the event handler removal is requested. */
49 void (*destroy)(struct event_handler *self);
50};
51
52enum process_state {
53 STATE_ATTACHED = 0,
54 STATE_BEING_CREATED,
55 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
56};
57
Petr Machataf6ec08a2012-01-06 16:58:54 +010058struct output_state {
59 size_t params_left;
60 int need_delim;
61};
62
Petr Machata366c2f42012-02-09 19:34:36 +010063struct callstack_element {
64 union {
65 int syscall;
66 struct library_symbol * libfunc;
67 } c_un;
68 int is_syscall;
69 void * return_addr;
70 struct timeval time_spent;
Petr Machataf6ec08a2012-01-06 16:58:54 +010071 struct fetch_context *fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +010072 struct value_dict *arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +010073 struct output_state out;
Petr Machata366c2f42012-02-09 19:34:36 +010074};
75
76/* XXX We should get rid of this. */
77#define MAX_CALLDEPTH 64
78
79/* XXX We would rather have this all organized a little differently,
80 * have Process for the whole group and Task for what's there for
81 * per-thread stuff. But for now this is the less invasive way of
82 * structuring it. */
Petr Machatafd43ef72012-02-10 12:25:11 +010083typedef struct Process Process;
Petr Machata366c2f42012-02-09 19:34:36 +010084struct Process {
85 enum process_state state;
86 Process * parent; /* needed by STATE_BEING_CREATED */
87 char * filename;
88 pid_t pid;
89
90 /* Dictionary of breakpoints (which is a mapping
91 * address->breakpoint). This is NULL for non-leader
Petr Machataecb082f2012-04-05 02:10:10 +020092 * processes. XXX note that we store addresses (keys) by
Petr Machatabac2da52012-05-29 00:42:59 +020093 * value. That assumes that arch_addr_t fits in host
Petr Machataecb082f2012-04-05 02:10:10 +020094 * pointer. */
Petr Machata366c2f42012-02-09 19:34:36 +010095 Dict * breakpoints;
96
97 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
98 unsigned int personality;
99 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
100
Petr Machataba1664b2012-04-28 14:59:05 +0200101 size_t callstack_depth;
Petr Machata366c2f42012-02-09 19:34:36 +0100102 struct callstack_element callstack[MAX_CALLDEPTH];
Petr Machata76dd9292012-04-03 13:02:06 +0200103
104 /* Linked list of libraries in backwards order of mapping.
105 * The last element is the executed binary itself. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100106 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +0100107
Petr Machata366c2f42012-02-09 19:34:36 +0100108 /* Arch-dependent: */
Petr Machata366c2f42012-02-09 19:34:36 +0100109 void * instruction_pointer;
110 void * stack_pointer; /* To get return addr, args... */
111 void * return_addr;
112 void * arch_ptr;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400113
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
117 * nauseam. */
Petr Machata366c2f42012-02-09 19:34:36 +0100118 short e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400119 char e_class;
120
121 /* XXX this shoudl go to ARM's arch_process_data. */
Petr Machata366c2f42012-02-09 19:34:36 +0100122#ifdef __arm__
123 int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */
124#endif
125
126#if defined(HAVE_LIBUNWIND)
127 /* libunwind address space */
128 unw_addr_space_t unwind_as;
129 void *unwind_priv;
130#endif /* defined(HAVE_LIBUNWIND) */
131
132 /* Set in leader. */
133 struct event_handler *event_handler;
134
135 /**
136 * Process chaining.
137 **/
138 Process * next;
139
140 /* LEADER points to the leader thread of the POSIX.1 process.
141 If X->LEADER == X, then X is the leader thread and the
142 Process structures chained by NEXT represent other threads,
143 up until, but not including, the next leader thread.
144 LEADER may be NULL after the leader has already exited. In
145 that case this process is waiting to be collected. */
146 Process * leader;
Petr Machata744f2552012-04-15 04:33:18 +0200147
Petr Machata0f6e6d92012-10-26 23:42:17 +0200148 struct os_process_data os;
Petr Machata744f2552012-04-15 04:33:18 +0200149 struct arch_process_data arch;
Petr Machata366c2f42012-02-09 19:34:36 +0100150};
151
Petr Machata75934ad2012-04-14 02:28:03 +0200152/* Initialize a process given a path to binary FILENAME, with a PID,
153 * and add the process to an internal chain of traced processes. */
154int process_init(struct Process *proc, const char *filename, pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100155
Petr Machata3d0c91c2012-04-14 02:37:38 +0200156/* PROC underwent an exec. This is a bit like process_destroy
157 * followed by process_init, except that some state is kept and the
158 * process doesn't lose it's place in the list of processes. */
159int process_exec(struct Process *proc);
160
161/* Release any memory allocated for PROC (but not PROC itself). Does
162 * NOT remove PROC from internal chain.
163 *
164 * XXX clearly this init/destroy pair is different than others and
165 * should be fixed. process_init should presumably be separate from
166 * process_add. */
167void process_destroy(struct Process *proc);
168
Petr Machata75934ad2012-04-14 02:28:03 +0200169struct Process *open_program(const char *filename, pid_t pid);
Petr Machata366c2f42012-02-09 19:34:36 +0100170void open_pid(pid_t pid);
171Process * pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100172
173/* Clone the contents of PROC into the memory referenced by RETP.
174 * Returns 0 on success or a negative value on failure. */
175int process_clone(struct Process *retp, struct Process *proc, pid_t pid);
176
Petr Machataa24021c2012-09-25 14:46:44 +0200177/* Iterate through the processes that ltrace currently traces. Tasks
178 * are considered to be processes for the purpose of this iterator.
179 * See callback.h for notes on iteration interfaces. */
Petr Machata74132a42012-03-16 02:46:18 +0100180Process *each_process(Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100181 enum callback_status (*cb)(struct Process *proc,
182 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100183 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100184
Petr Machataa24021c2012-09-25 14:46:44 +0200185/* Iterate through list of tasks of given process PROC. See
186 * callback.h for notes on iteration interfaces. */
Petr Machata74132a42012-03-16 02:46:18 +0100187Process *each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100188 enum callback_status (*cb)(struct Process *proc,
189 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100190 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100191
Petr Machata366c2f42012-02-09 19:34:36 +0100192void change_process_leader(Process *proc, Process *leader);
Petr Machatafd2641c2012-04-24 21:33:16 +0200193
194/* Remove process from the list of traced processes, drop any events
195 * in the event queue, destroy it and free memory. */
196void remove_process(struct Process *proc);
197
Petr Machata366c2f42012-02-09 19:34:36 +0100198void install_event_handler(Process *proc, struct event_handler *handler);
199void destroy_event_handler(Process *proc);
200
Petr Machata2b46cfc2012-02-18 11:17:29 +0100201/* Add a library LIB to the list of PROC's libraries. */
202void proc_add_library(struct Process *proc, struct library *lib);
203
204/* Remove LIB from list of PROC's libraries. Returns 0 if the library
205 * was found and unlinked, otherwise returns a negative value. */
206int proc_remove_library(struct Process *proc, struct library *lib);
207
Petr Machataef2fd272012-09-28 00:43:01 +0200208/* Clear a delayed flag. If a symbol is neither latent, nor delayed,
209 * a breakpoint is inserted for it. Returns 0 if the activation was
210 * successful or a negative value if it failed. Note that if a symbol
211 * is both latent and delayed, this will not enable the corresponding
212 * breakpoint. */
213int proc_activate_delayed_symbol(struct Process *proc,
214 struct library_symbol *libsym);
215
Petr Machataa24021c2012-09-25 14:46:44 +0200216/* Iterate through the libraries of PROC. See callback.h for notes on
217 * iteration interfaces. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100218struct library *proc_each_library(struct Process *proc, struct library *start,
219 enum callback_status (*cb)(struct Process *p,
220 struct library *l,
221 void *data),
222 void *data);
223
Petr Machata52dbfb12012-03-29 16:38:26 +0200224/* Insert BP into PROC. */
225int proc_add_breakpoint(struct Process *proc, struct breakpoint *bp);
226
Petr Machataf7fee432012-04-19 17:00:53 +0200227/* Remove BP from PROC. This has no reason to fail in runtime. If it
228 * does not find BP in PROC, it's hard error guarded by assertion. */
229void proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100230
Petr Machataa24021c2012-09-25 14:46:44 +0200231/* Iterate through the breakpoints of PROC. See callback.h for notes
232 * on iteration interfaces. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200233void *proc_each_breakpoint(struct Process *proc, void *start,
234 enum callback_status (*cb)(struct Process *proc,
235 struct breakpoint *bp,
236 void *data),
237 void *data);
238
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200239/* Iterate through the dynamic section at src_addr looking for D_TAG.
240 * If tag is found, fill it's value in RET and return 0.
241 * If tag is not found, return a negative value. */
242int proc_find_dynamic_entry_addr(struct Process *proc, arch_addr_t src_addr,
243 int d_tag, arch_addr_t *ret);
Petr Machata165b5662012-10-27 19:23:12 +0200244
245/* Finds a symbol corresponding to LIBSYM in a process PROC. Returns
246 * 0 and sets *RETLIB and *RETSYM if the corresponding pointer is
247 * non-NULL. Returns a negative value when the symbols couldn't be
248 * found. */
249int proc_find_symbol(struct Process *proc, struct library_symbol *sym,
250 struct library **retlib, struct library_symbol **retsym);
251
Petr Machata366c2f42012-02-09 19:34:36 +0100252#endif /* _PROC_H_ */