blob: 0078030a7536f547df40fa3ed4cda4b591db7030 [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 Machata366c2f42012-02-09 19:34:36 +010037
Petr Machata2b46cfc2012-02-18 11:17:29 +010038struct library;
Petr Machata52dbfb12012-03-29 16:38:26 +020039struct breakpoint;
Petr Machata2b46cfc2012-02-18 11:17:29 +010040
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. */
Petr Machataef7fa372012-03-28 02:05:36 +020047 CBS_FAIL, /* There was an error. The iteration should stop
48 * and return error. */
Petr Machata2b46cfc2012-02-18 11:17:29 +010049};
50
Petr Machata366c2f42012-02-09 19:34:36 +010051struct 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
Petr Machata366c2f42012-02-09 19:34:36 +010067struct callstack_element {
68 union {
69 int syscall;
70 struct library_symbol * libfunc;
71 } c_un;
72 int is_syscall;
73 void * return_addr;
74 struct timeval time_spent;
75 void * arch_ptr;
Petr Machata94078ec2012-01-05 18:07:02 +010076 struct value_dict *arguments;
Petr Machata366c2f42012-02-09 19:34:36 +010077};
78
79/* XXX We should get rid of this. */
80#define MAX_CALLDEPTH 64
81
82/* XXX We would rather have this all organized a little differently,
83 * have Process for the whole group and Task for what's there for
84 * per-thread stuff. But for now this is the less invasive way of
85 * structuring it. */
Petr Machatafd43ef72012-02-10 12:25:11 +010086typedef struct Process Process;
Petr Machata366c2f42012-02-09 19:34:36 +010087struct Process {
88 enum process_state state;
89 Process * parent; /* needed by STATE_BEING_CREATED */
90 char * filename;
91 pid_t pid;
92
93 /* Dictionary of breakpoints (which is a mapping
94 * address->breakpoint). This is NULL for non-leader
Petr Machataecb082f2012-04-05 02:10:10 +020095 * processes. XXX note that we store addresses (keys) by
96 * value. That assumes that target_address_t fits in host
97 * pointer. */
Petr Machata366c2f42012-02-09 19:34:36 +010098 Dict * breakpoints;
99
100 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
101 unsigned int personality;
102 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
103
104 int callstack_depth;
105 struct callstack_element callstack[MAX_CALLDEPTH];
Petr Machata76dd9292012-04-03 13:02:06 +0200106
107 /* Linked list of libraries in backwards order of mapping.
108 * The last element is the executed binary itself. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100109 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +0100110
Petr Machata366c2f42012-02-09 19:34:36 +0100111 /* Arch-dependent: */
Petr Machata744f2552012-04-15 04:33:18 +0200112 void *debug; /* arch-dep process debug struct XXX move to
113 * os_process_data after it's invented. */
Petr Machata366c2f42012-02-09 19:34:36 +0100114 void * instruction_pointer;
115 void * stack_pointer; /* To get return addr, args... */
116 void * return_addr;
117 void * arch_ptr;
118 short e_machine;
Petr Machata366c2f42012-02-09 19:34:36 +0100119#ifdef __arm__
120 int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */
121#endif
122
123#if defined(HAVE_LIBUNWIND)
124 /* libunwind address space */
125 unw_addr_space_t unwind_as;
126 void *unwind_priv;
127#endif /* defined(HAVE_LIBUNWIND) */
128
129 /* Set in leader. */
130 struct event_handler *event_handler;
131
132 /**
133 * Process chaining.
134 **/
135 Process * next;
136
137 /* LEADER points to the leader thread of the POSIX.1 process.
138 If X->LEADER == X, then X is the leader thread and the
139 Process structures chained by NEXT represent other threads,
140 up until, but not including, the next leader thread.
141 LEADER may be NULL after the leader has already exited. In
142 that case this process is waiting to be collected. */
143 Process * leader;
Petr Machata744f2552012-04-15 04:33:18 +0200144
145 struct arch_process_data arch;
Petr Machata366c2f42012-02-09 19:34:36 +0100146};
147
Petr Machata75934ad2012-04-14 02:28:03 +0200148/* Initialize a process given a path to binary FILENAME, with a PID,
149 * and add the process to an internal chain of traced processes. */
150int process_init(struct Process *proc, const char *filename, pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100151
Petr Machata3d0c91c2012-04-14 02:37:38 +0200152/* PROC underwent an exec. This is a bit like process_destroy
153 * followed by process_init, except that some state is kept and the
154 * process doesn't lose it's place in the list of processes. */
155int process_exec(struct Process *proc);
156
157/* Release any memory allocated for PROC (but not PROC itself). Does
158 * NOT remove PROC from internal chain.
159 *
160 * XXX clearly this init/destroy pair is different than others and
161 * should be fixed. process_init should presumably be separate from
162 * process_add. */
163void process_destroy(struct Process *proc);
164
Petr Machata75934ad2012-04-14 02:28:03 +0200165struct Process *open_program(const char *filename, pid_t pid);
Petr Machata366c2f42012-02-09 19:34:36 +0100166void open_pid(pid_t pid);
167Process * pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100168
169/* Clone the contents of PROC into the memory referenced by RETP.
170 * Returns 0 on success or a negative value on failure. */
171int process_clone(struct Process *retp, struct Process *proc, pid_t pid);
172
173/* Iterate through the processes that ltrace currently traces. CB is
174 * called for each process. Tasks are considered to be processes for
175 * the purpose of this iterator.
176 *
Petr Machata74132a42012-03-16 02:46:18 +0100177 * Notes on this iteration interface: The iteration starts after the
178 * process designated by START_AFTER, or at the first process if
179 * START_AFTER is NULL. DATA is passed verbatim to CB. If CB returns
180 * CBS_STOP, the iteration stops and the current iterator is returned.
181 * That iterator can then be used to restart the iteration. NULL is
Petr Machata2b46cfc2012-02-18 11:17:29 +0100182 * returned when iteration ends.
183 *
184 * There's no provision for returning error states. Errors need to be
185 * signaled to the caller via DATA, together with any other data that
186 * the callback needs. */
Petr Machata74132a42012-03-16 02:46:18 +0100187Process *each_process(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 Machata74132a42012-03-16 02:46:18 +0100192/* Iterate through list of tasks of given process PROC. Restarts are
193 * supported via START_AFTER (see each_process for details of
194 * iteration interface). */
195Process *each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100196 enum callback_status (*cb)(struct Process *proc,
197 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100198 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100199
Petr Machata366c2f42012-02-09 19:34:36 +0100200void change_process_leader(Process *proc, Process *leader);
Petr Machatafd2641c2012-04-24 21:33:16 +0200201
202/* Remove process from the list of traced processes, drop any events
203 * in the event queue, destroy it and free memory. */
204void remove_process(struct Process *proc);
205
Petr Machata366c2f42012-02-09 19:34:36 +0100206void install_event_handler(Process *proc, struct event_handler *handler);
207void destroy_event_handler(Process *proc);
208
Petr Machata2b46cfc2012-02-18 11:17:29 +0100209/* Add a library LIB to the list of PROC's libraries. */
210void proc_add_library(struct Process *proc, struct library *lib);
211
212/* Remove LIB from list of PROC's libraries. Returns 0 if the library
213 * was found and unlinked, otherwise returns a negative value. */
214int proc_remove_library(struct Process *proc, struct library *lib);
215
216/* Iterate through the libraries of PROC. See each_process for
217 * detailed description of the iteration interface. */
218struct 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 Machatad3cc9882012-04-13 21:40:23 +0200231/* Iterate through the libraries of PROC. See each_process for
232 * detailed description of the iteration interface. */
233void *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
Petr Machata366c2f42012-02-09 19:34:36 +0100239#endif /* _PROC_H_ */