blob: ed2bb7010b6ce9c8f52354fb60e712a3d5801c81 [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
26#if defined(HAVE_LIBUNWIND)
27# include <libunwind.h>
28#endif /* defined(HAVE_LIBUNWIND) */
29
30#include "ltrace.h"
31#include "dict.h"
Petr Machata744f2552012-04-15 04:33:18 +020032#include "sysdep.h"
Petr Machata366c2f42012-02-09 19:34:36 +010033
Petr Machata2b46cfc2012-02-18 11:17:29 +010034struct library;
Petr Machata52dbfb12012-03-29 16:38:26 +020035struct breakpoint;
Petr Machata2b46cfc2012-02-18 11:17:29 +010036
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. */
Petr Machataef7fa372012-03-28 02:05:36 +020043 CBS_FAIL, /* There was an error. The iteration should stop
44 * and return error. */
Petr Machata2b46cfc2012-02-18 11:17:29 +010045};
46
Petr Machata366c2f42012-02-09 19:34:36 +010047struct 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
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;
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. */
Petr Machatafd43ef72012-02-10 12:25:11 +010081typedef struct Process Process;
Petr Machata366c2f42012-02-09 19:34:36 +010082struct 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
Petr Machataecb082f2012-04-05 02:10:10 +020090 * processes. XXX note that we store addresses (keys) by
91 * value. That assumes that target_address_t fits in host
92 * pointer. */
Petr Machata366c2f42012-02-09 19:34:36 +010093 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];
Petr Machata76dd9292012-04-03 13:02:06 +0200101
102 /* Linked list of libraries in backwards order of mapping.
103 * The last element is the executed binary itself. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100104 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +0100105
Petr Machata366c2f42012-02-09 19:34:36 +0100106 /* Arch-dependent: */
Petr Machata744f2552012-04-15 04:33:18 +0200107 void *debug; /* arch-dep process debug struct XXX move to
108 * os_process_data after it's invented. */
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;
113 short e_machine;
Petr Machata366c2f42012-02-09 19:34:36 +0100114#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;
Petr Machata744f2552012-04-15 04:33:18 +0200139
140 struct arch_process_data arch;
Petr Machata366c2f42012-02-09 19:34:36 +0100141};
142
Petr Machata75934ad2012-04-14 02:28:03 +0200143/* 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);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100146
Petr Machata3d0c91c2012-04-14 02:37:38 +0200147/* 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
Petr Machata75934ad2012-04-14 02:28:03 +0200160struct Process *open_program(const char *filename, pid_t pid);
Petr Machata366c2f42012-02-09 19:34:36 +0100161void open_pid(pid_t pid);
162Process * pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100163
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 *
Petr Machata74132a42012-03-16 02:46:18 +0100172 * 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
Petr Machata2b46cfc2012-02-18 11:17:29 +0100177 * 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. */
Petr Machata74132a42012-03-16 02:46:18 +0100182Process *each_process(Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100183 enum callback_status (*cb)(struct Process *proc,
184 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100185 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100186
Petr Machata74132a42012-03-16 02:46:18 +0100187/* 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,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100191 enum callback_status (*cb)(struct Process *proc,
192 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100193 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100194
Petr Machata366c2f42012-02-09 19:34:36 +0100195void change_process_leader(Process *proc, Process *leader);
196void remove_process(Process *proc);
197void install_event_handler(Process *proc, struct event_handler *handler);
198void destroy_event_handler(Process *proc);
199
Petr Machata2b46cfc2012-02-18 11:17:29 +0100200/* Add a library LIB to the list of PROC's libraries. */
201void proc_add_library(struct Process *proc, struct library *lib);
202
203/* Remove LIB from list of PROC's libraries. Returns 0 if the library
204 * was found and unlinked, otherwise returns a negative value. */
205int proc_remove_library(struct Process *proc, struct library *lib);
206
207/* Iterate through the libraries of PROC. See each_process for
208 * detailed description of the iteration interface. */
209struct library *proc_each_library(struct Process *proc, struct library *start,
210 enum callback_status (*cb)(struct Process *p,
211 struct library *l,
212 void *data),
213 void *data);
214
Petr Machata52dbfb12012-03-29 16:38:26 +0200215/* Insert BP into PROC. */
216int proc_add_breakpoint(struct Process *proc, struct breakpoint *bp);
217
Petr Machataf7fee432012-04-19 17:00:53 +0200218/* Remove BP from PROC. This has no reason to fail in runtime. If it
219 * does not find BP in PROC, it's hard error guarded by assertion. */
220void proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100221
Petr Machatad3cc9882012-04-13 21:40:23 +0200222/* Iterate through the libraries of PROC. See each_process for
223 * detailed description of the iteration interface. */
224void *proc_each_breakpoint(struct Process *proc, void *start,
225 enum callback_status (*cb)(struct Process *proc,
226 struct breakpoint *bp,
227 void *data),
228 void *data);
229
Petr Machata366c2f42012-02-09 19:34:36 +0100230#endif /* _PROC_H_ */