blob: 0f670e516772d30d3fdd8c5d4be12f8c1fcf5177 [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. */
41};
42
Petr Machata366c2f42012-02-09 19:34:36 +010043struct event_handler {
44 /* Event handler that overrides the default one. Should
45 * return NULL if the event was handled, otherwise the
46 * returned event is passed to the default handler. */
47 Event *(*on_event)(struct event_handler *self, Event *event);
48
49 /* Called when the event handler removal is requested. */
50 void (*destroy)(struct event_handler *self);
51};
52
53enum process_state {
54 STATE_ATTACHED = 0,
55 STATE_BEING_CREATED,
56 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
57};
58
Petr Machata366c2f42012-02-09 19:34:36 +010059struct callstack_element {
60 union {
61 int syscall;
62 struct library_symbol * libfunc;
63 } c_un;
64 int is_syscall;
65 void * return_addr;
66 struct timeval time_spent;
67 void * arch_ptr;
68};
69
70/* XXX We should get rid of this. */
71#define MAX_CALLDEPTH 64
72
73/* XXX We would rather have this all organized a little differently,
74 * have Process for the whole group and Task for what's there for
75 * per-thread stuff. But for now this is the less invasive way of
76 * structuring it. */
Petr Machatafd43ef72012-02-10 12:25:11 +010077typedef struct Process Process;
Petr Machata366c2f42012-02-09 19:34:36 +010078struct Process {
79 enum process_state state;
80 Process * parent; /* needed by STATE_BEING_CREATED */
81 char * filename;
82 pid_t pid;
83
84 /* Dictionary of breakpoints (which is a mapping
85 * address->breakpoint). This is NULL for non-leader
86 * processes. */
87 Dict * breakpoints;
88
89 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
90 unsigned int personality;
91 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
92
93 int callstack_depth;
94 struct callstack_element callstack[MAX_CALLDEPTH];
Petr Machata2b46cfc2012-02-18 11:17:29 +010095 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +010096
Petr Machata366c2f42012-02-09 19:34:36 +010097 /* Arch-dependent: */
98 void * debug; /* arch-dep process debug struct */
99 long debug_state; /* arch-dep debug state */
100 void * instruction_pointer;
101 void * stack_pointer; /* To get return addr, args... */
102 void * return_addr;
103 void * arch_ptr;
104 short e_machine;
Petr Machata366c2f42012-02-09 19:34:36 +0100105#ifdef __arm__
106 int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */
107#endif
108
109#if defined(HAVE_LIBUNWIND)
110 /* libunwind address space */
111 unw_addr_space_t unwind_as;
112 void *unwind_priv;
113#endif /* defined(HAVE_LIBUNWIND) */
114
115 /* Set in leader. */
116 struct event_handler *event_handler;
117
118 /**
119 * Process chaining.
120 **/
121 Process * next;
122
123 /* LEADER points to the leader thread of the POSIX.1 process.
124 If X->LEADER == X, then X is the leader thread and the
125 Process structures chained by NEXT represent other threads,
126 up until, but not including, the next leader thread.
127 LEADER may be NULL after the leader has already exited. In
128 that case this process is waiting to be collected. */
129 Process * leader;
130};
131
Petr Machata2b46cfc2012-02-18 11:17:29 +0100132int process_init(struct Process *proc,
133 const char *filename, pid_t pid, int enable_breakpoints);
134
135Process * open_program(const char *filename, pid_t pid, int enable_breakpoints);
Petr Machata366c2f42012-02-09 19:34:36 +0100136void open_pid(pid_t pid);
137Process * pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100138
139/* Clone the contents of PROC into the memory referenced by RETP.
140 * Returns 0 on success or a negative value on failure. */
141int process_clone(struct Process *retp, struct Process *proc, pid_t pid);
142
143/* Iterate through the processes that ltrace currently traces. CB is
144 * called for each process. Tasks are considered to be processes for
145 * the purpose of this iterator.
146 *
147 * Notes on this iteration interface: DATA is passed verbatim to CB.
148 * If CB returns CBS_STOP, the iteration stops and the current
149 * iterator is returned. That iterator can then be used to restart
150 * the iteration. If you don't want CB to see the same process more
151 * than once, restart with IT->next instead of just IT. NULL is
152 * returned when iteration ends.
153 *
154 * There's no provision for returning error states. Errors need to be
155 * signaled to the caller via DATA, together with any other data that
156 * the callback needs. */
Petr Machata366c2f42012-02-09 19:34:36 +0100157Process *each_process(Process *start,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100158 enum callback_status (*cb)(struct Process *proc,
159 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100160 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100161
162/* Iterate through list of tasks of given process START. Normally you
163 * start the iteration by calling this on PROC->leader, the iterator
164 * doesn't do this for you (so as to support restarts). See above for
165 * details on the iteration interface. */
Petr Machata366c2f42012-02-09 19:34:36 +0100166Process *each_task(Process *start,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100167 enum callback_status (*cb)(struct Process *proc,
168 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100169 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100170
Petr Machata366c2f42012-02-09 19:34:36 +0100171void add_process(Process *proc);
172void change_process_leader(Process *proc, Process *leader);
173void remove_process(Process *proc);
174void install_event_handler(Process *proc, struct event_handler *handler);
175void destroy_event_handler(Process *proc);
176
Petr Machata2b46cfc2012-02-18 11:17:29 +0100177/* Add a library LIB to the list of PROC's libraries. */
178void proc_add_library(struct Process *proc, struct library *lib);
179
180/* Remove LIB from list of PROC's libraries. Returns 0 if the library
181 * was found and unlinked, otherwise returns a negative value. */
182int proc_remove_library(struct Process *proc, struct library *lib);
183
184/* Iterate through the libraries of PROC. See each_process for
185 * detailed description of the iteration interface. */
186struct library *proc_each_library(struct Process *proc, struct library *start,
187 enum callback_status (*cb)(struct Process *p,
188 struct library *l,
189 void *data),
190 void *data);
191
192
Petr Machata366c2f42012-02-09 19:34:36 +0100193#endif /* _PROC_H_ */