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