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