blob: 84241bf5eb60317d534def7613d3c265d4304550 [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"
32
Petr Machata2b46cfc2012-02-18 11:17:29 +010033struct library;
34
35/* XXX Move this somewhere where it makes sense. When the mess in
36 * common.h is disentangled, that would actually be a good place for
37 * this. */
38enum callback_status {
39 CBS_STOP, /* The iteration should stop. */
40 CBS_CONT, /* The iteration should continue. */
Petr Machataef7fa372012-03-28 02:05:36 +020041 CBS_FAIL, /* There was an error. The iteration should stop
42 * and return error. */
Petr Machata2b46cfc2012-02-18 11:17:29 +010043};
44
Petr Machata366c2f42012-02-09 19:34:36 +010045struct event_handler {
46 /* Event handler that overrides the default one. Should
47 * return NULL if the event was handled, otherwise the
48 * returned event is passed to the default handler. */
49 Event *(*on_event)(struct event_handler *self, Event *event);
50
51 /* Called when the event handler removal is requested. */
52 void (*destroy)(struct event_handler *self);
53};
54
55enum process_state {
56 STATE_ATTACHED = 0,
57 STATE_BEING_CREATED,
58 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
59};
60
Petr Machata366c2f42012-02-09 19:34:36 +010061struct callstack_element {
62 union {
63 int syscall;
64 struct library_symbol * libfunc;
65 } c_un;
66 int is_syscall;
67 void * return_addr;
68 struct timeval time_spent;
69 void * arch_ptr;
70};
71
72/* XXX We should get rid of this. */
73#define MAX_CALLDEPTH 64
74
75/* XXX We would rather have this all organized a little differently,
76 * have Process for the whole group and Task for what's there for
77 * per-thread stuff. But for now this is the less invasive way of
78 * structuring it. */
Petr Machatafd43ef72012-02-10 12:25:11 +010079typedef struct Process Process;
Petr Machata366c2f42012-02-09 19:34:36 +010080struct Process {
81 enum process_state state;
82 Process * parent; /* needed by STATE_BEING_CREATED */
83 char * filename;
84 pid_t pid;
85
86 /* Dictionary of breakpoints (which is a mapping
87 * address->breakpoint). This is NULL for non-leader
88 * processes. */
89 Dict * breakpoints;
90
91 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
92 unsigned int personality;
93 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
94
95 int callstack_depth;
96 struct callstack_element callstack[MAX_CALLDEPTH];
Petr Machata2b46cfc2012-02-18 11:17:29 +010097 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +010098
Petr Machata366c2f42012-02-09 19:34:36 +010099 /* Arch-dependent: */
100 void * debug; /* arch-dep process debug struct */
101 long debug_state; /* arch-dep debug state */
102 void * instruction_pointer;
103 void * stack_pointer; /* To get return addr, args... */
104 void * return_addr;
105 void * arch_ptr;
106 short e_machine;
Petr Machata366c2f42012-02-09 19:34:36 +0100107#ifdef __arm__
108 int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */
109#endif
110
111#if defined(HAVE_LIBUNWIND)
112 /* libunwind address space */
113 unw_addr_space_t unwind_as;
114 void *unwind_priv;
115#endif /* defined(HAVE_LIBUNWIND) */
116
117 /* Set in leader. */
118 struct event_handler *event_handler;
119
120 /**
121 * Process chaining.
122 **/
123 Process * next;
124
125 /* LEADER points to the leader thread of the POSIX.1 process.
126 If X->LEADER == X, then X is the leader thread and the
127 Process structures chained by NEXT represent other threads,
128 up until, but not including, the next leader thread.
129 LEADER may be NULL after the leader has already exited. In
130 that case this process is waiting to be collected. */
131 Process * leader;
132};
133
Petr Machata2b46cfc2012-02-18 11:17:29 +0100134int process_init(struct Process *proc,
135 const char *filename, pid_t pid, int enable_breakpoints);
136
137Process * open_program(const char *filename, pid_t pid, int enable_breakpoints);
Petr Machata366c2f42012-02-09 19:34:36 +0100138void open_pid(pid_t pid);
139Process * pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100140
141/* Clone the contents of PROC into the memory referenced by RETP.
142 * Returns 0 on success or a negative value on failure. */
143int process_clone(struct Process *retp, struct Process *proc, pid_t pid);
144
145/* Iterate through the processes that ltrace currently traces. CB is
146 * called for each process. Tasks are considered to be processes for
147 * the purpose of this iterator.
148 *
Petr Machata74132a42012-03-16 02:46:18 +0100149 * Notes on this iteration interface: The iteration starts after the
150 * process designated by START_AFTER, or at the first process if
151 * START_AFTER is NULL. DATA is passed verbatim to CB. If CB returns
152 * CBS_STOP, the iteration stops and the current iterator is returned.
153 * That iterator can then be used to restart the iteration. NULL is
Petr Machata2b46cfc2012-02-18 11:17:29 +0100154 * returned when iteration ends.
155 *
156 * There's no provision for returning error states. Errors need to be
157 * signaled to the caller via DATA, together with any other data that
158 * the callback needs. */
Petr Machata74132a42012-03-16 02:46:18 +0100159Process *each_process(Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100160 enum callback_status (*cb)(struct Process *proc,
161 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100162 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100163
Petr Machata74132a42012-03-16 02:46:18 +0100164/* Iterate through list of tasks of given process PROC. Restarts are
165 * supported via START_AFTER (see each_process for details of
166 * iteration interface). */
167Process *each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100168 enum callback_status (*cb)(struct Process *proc,
169 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100170 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100171
Petr Machata366c2f42012-02-09 19:34:36 +0100172void add_process(Process *proc);
173void change_process_leader(Process *proc, Process *leader);
174void remove_process(Process *proc);
175void install_event_handler(Process *proc, struct event_handler *handler);
176void destroy_event_handler(Process *proc);
177
Petr Machata2b46cfc2012-02-18 11:17:29 +0100178/* Add a library LIB to the list of PROC's libraries. */
179void proc_add_library(struct Process *proc, struct library *lib);
180
181/* Remove LIB from list of PROC's libraries. Returns 0 if the library
182 * was found and unlinked, otherwise returns a negative value. */
183int proc_remove_library(struct Process *proc, struct library *lib);
184
185/* Iterate through the libraries of PROC. See each_process for
186 * detailed description of the iteration interface. */
187struct library *proc_each_library(struct Process *proc, struct library *start,
188 enum callback_status (*cb)(struct Process *p,
189 struct library *l,
190 void *data),
191 void *data);
192
193
Petr Machata366c2f42012-02-09 19:34:36 +0100194#endif /* _PROC_H_ */