proc.h revision 4d4e1b853db0c97b7c7f023bc301e3c3eee58ce4
1/*
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
23#ifndef _PROC_H_
24#define _PROC_H_
25
26#include "config.h"
27
28#include <sys/time.h>
29
30#if defined(HAVE_LIBUNWIND)
31# include <libunwind.h>
32#endif /* defined(HAVE_LIBUNWIND) */
33
34#include "ltrace.h"
35#include "dict.h"
36#include "sysdep.h"
37
38struct library;
39struct breakpoint;
40
41/* XXX Move this somewhere where it makes sense.  When the mess in
42 * common.h is disentangled, that would actually be a good place for
43 * this.  */
44enum callback_status {
45	CBS_STOP, /* The iteration should stop.  */
46	CBS_CONT, /* The iteration should continue.  */
47	CBS_FAIL, /* There was an error.  The iteration should stop
48		   * and return error.  */
49};
50
51struct event_handler {
52	/* Event handler that overrides the default one.  Should
53	 * return NULL if the event was handled, otherwise the
54	 * returned event is passed to the default handler.  */
55	Event *(*on_event)(struct event_handler *self, Event *event);
56
57	/* Called when the event handler removal is requested.  */
58	void (*destroy)(struct event_handler *self);
59};
60
61enum process_state {
62	STATE_ATTACHED = 0,
63	STATE_BEING_CREATED,
64	STATE_IGNORED  /* ignore this process (it's a fork and no -f was used) */
65};
66
67struct output_state {
68	size_t params_left;
69	int need_delim;
70};
71
72struct callstack_element {
73	union {
74		int syscall;
75		struct library_symbol * libfunc;
76	} c_un;
77	int is_syscall;
78	void * return_addr;
79	struct timeval time_spent;
80	struct fetch_context *fetch_context;
81	struct value_dict *arguments;
82	struct output_state out;
83};
84
85/* XXX We should get rid of this.  */
86#define MAX_CALLDEPTH 64
87
88/* XXX We would rather have this all organized a little differently,
89 * have Process for the whole group and Task for what's there for
90 * per-thread stuff.  But for now this is the less invasive way of
91 * structuring it.  */
92typedef struct Process Process;
93struct Process {
94	enum process_state state;
95	Process * parent;         /* needed by STATE_BEING_CREATED */
96	char * filename;
97	pid_t pid;
98
99	/* Dictionary of breakpoints (which is a mapping
100	 * address->breakpoint).  This is NULL for non-leader
101	 * processes.  XXX note that we store addresses (keys) by
102	 * value.  That assumes that arch_addr_t fits in host
103	 * pointer.  */
104	Dict * breakpoints;
105
106	int mask_32bit;           /* 1 if 64-bit ltrace is tracing 32-bit process */
107	unsigned int personality;
108	int tracesysgood;         /* signal indicating a PTRACE_SYSCALL trap */
109
110	size_t callstack_depth;
111	struct callstack_element callstack[MAX_CALLDEPTH];
112
113	/* Linked list of libraries in backwards order of mapping.
114	 * The last element is the executed binary itself.  */
115	struct library *libraries;
116
117	/* Arch-dependent: */
118	void *debug;	/* arch-dep process debug struct XXX move to
119			 * os_process_data after it's invented.  */
120	void * instruction_pointer;
121	void * stack_pointer;      /* To get return addr, args... */
122	void * return_addr;
123	void * arch_ptr;
124
125	/* XXX We would like to replace this with a pointer to ABI
126	 * object that would provide the relevant services, instead of
127	 * checking the necessary flags in the back end ad
128	 * nauseam.  */
129	short e_machine;
130	char e_class;
131
132	/* XXX this shoudl go to ARM's arch_process_data.  */
133#ifdef __arm__
134	int thumb_mode;           /* ARM execution mode: 0: ARM, 1: Thumb */
135#endif
136
137#if defined(HAVE_LIBUNWIND)
138	/* libunwind address space */
139	unw_addr_space_t unwind_as;
140	void *unwind_priv;
141#endif /* defined(HAVE_LIBUNWIND) */
142
143	/* Set in leader.  */
144	struct event_handler *event_handler;
145
146	/**
147	 * Process chaining.
148	 **/
149	Process * next;
150
151	/* LEADER points to the leader thread of the POSIX.1 process.
152	   If X->LEADER == X, then X is the leader thread and the
153	   Process structures chained by NEXT represent other threads,
154	   up until, but not including, the next leader thread.
155	   LEADER may be NULL after the leader has already exited.  In
156	   that case this process is waiting to be collected.  */
157	Process * leader;
158
159	struct arch_process_data arch;
160};
161
162/* Initialize a process given a path to binary FILENAME, with a PID,
163 * and add the process to an internal chain of traced processes.  */
164int process_init(struct Process *proc, const char *filename, pid_t pid);
165
166/* PROC underwent an exec.  This is a bit like process_destroy
167 * followed by process_init, except that some state is kept and the
168 * process doesn't lose it's place in the list of processes.  */
169int process_exec(struct Process *proc);
170
171/* Release any memory allocated for PROC (but not PROC itself).  Does
172 * NOT remove PROC from internal chain.
173 *
174 * XXX clearly this init/destroy pair is different than others and
175 * should be fixed.  process_init should presumably be separate from
176 * process_add.  */
177void process_destroy(struct Process *proc);
178
179struct Process *open_program(const char *filename, pid_t pid);
180void open_pid(pid_t pid);
181Process * pid2proc(pid_t pid);
182
183/* Clone the contents of PROC into the memory referenced by RETP.
184 * Returns 0 on success or a negative value on failure.  */
185int process_clone(struct Process *retp, struct Process *proc, pid_t pid);
186
187/* Iterate through the processes that ltrace currently traces.  CB is
188 * called for each process.  Tasks are considered to be processes for
189 * the purpose of this iterator.
190 *
191 * Notes on this iteration interface: The iteration starts after the
192 * process designated by START_AFTER, or at the first process if
193 * START_AFTER is NULL.  DATA is passed verbatim to CB.  If CB returns
194 * CBS_STOP, the iteration stops and the current iterator is returned.
195 * That iterator can then be used to restart the iteration.  NULL is
196 * returned when iteration ends.
197 *
198 * There's no provision for returning error states.  Errors need to be
199 * signaled to the caller via DATA, together with any other data that
200 * the callback needs.  */
201Process *each_process(Process *start_after,
202		      enum callback_status (*cb)(struct Process *proc,
203						 void *data),
204		      void *data);
205
206/* Iterate through list of tasks of given process PROC.  Restarts are
207 * supported via START_AFTER (see each_process for details of
208 * iteration interface).  */
209Process *each_task(struct Process *proc, struct Process *start_after,
210		   enum callback_status (*cb)(struct Process *proc,
211					      void *data),
212		   void *data);
213
214void change_process_leader(Process *proc, Process *leader);
215
216/* Remove process from the list of traced processes, drop any events
217 * in the event queue, destroy it and free memory.  */
218void remove_process(struct Process *proc);
219
220void install_event_handler(Process *proc, struct event_handler *handler);
221void destroy_event_handler(Process *proc);
222
223/* Add a library LIB to the list of PROC's libraries.  */
224void proc_add_library(struct Process *proc, struct library *lib);
225
226/* Remove LIB from list of PROC's libraries.  Returns 0 if the library
227 * was found and unlinked, otherwise returns a negative value.  */
228int proc_remove_library(struct Process *proc, struct library *lib);
229
230/* Iterate through the libraries of PROC.  See each_process for
231 * detailed description of the iteration interface.  */
232struct library *proc_each_library(struct Process *proc, struct library *start,
233				  enum callback_status (*cb)(struct Process *p,
234							     struct library *l,
235							     void *data),
236				  void *data);
237
238/* Insert BP into PROC.  */
239int proc_add_breakpoint(struct Process *proc, struct breakpoint *bp);
240
241/* Remove BP from PROC.  This has no reason to fail in runtime.  If it
242 * does not find BP in PROC, it's hard error guarded by assertion.  */
243void proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp);
244
245/* Iterate through the libraries of PROC.  See each_process for
246 * detailed description of the iteration interface.  */
247void *proc_each_breakpoint(struct Process *proc, void *start,
248			   enum callback_status (*cb)(struct Process *proc,
249						      struct breakpoint *bp,
250						      void *data),
251			   void *data);
252
253#endif /* _PROC_H_ */
254