blob: 8b77c3168ad8ed69f0a23b6f09e05fcb02d3a478 [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;
Petr Machata52dbfb12012-03-29 16:38:26 +020034struct breakpoint;
Petr Machata2b46cfc2012-02-18 11:17:29 +010035
36/* XXX Move this somewhere where it makes sense. When the mess in
37 * common.h is disentangled, that would actually be a good place for
38 * this. */
39enum callback_status {
40 CBS_STOP, /* The iteration should stop. */
41 CBS_CONT, /* The iteration should continue. */
Petr Machataef7fa372012-03-28 02:05:36 +020042 CBS_FAIL, /* There was an error. The iteration should stop
43 * and return error. */
Petr Machata2b46cfc2012-02-18 11:17:29 +010044};
45
Petr Machata366c2f42012-02-09 19:34:36 +010046struct event_handler {
47 /* Event handler that overrides the default one. Should
48 * return NULL if the event was handled, otherwise the
49 * returned event is passed to the default handler. */
50 Event *(*on_event)(struct event_handler *self, Event *event);
51
52 /* Called when the event handler removal is requested. */
53 void (*destroy)(struct event_handler *self);
54};
55
56enum process_state {
57 STATE_ATTACHED = 0,
58 STATE_BEING_CREATED,
59 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
60};
61
Petr Machata366c2f42012-02-09 19:34:36 +010062struct callstack_element {
63 union {
64 int syscall;
65 struct library_symbol * libfunc;
66 } c_un;
67 int is_syscall;
68 void * return_addr;
69 struct timeval time_spent;
70 void * arch_ptr;
71};
72
73/* XXX We should get rid of this. */
74#define MAX_CALLDEPTH 64
75
76/* XXX We would rather have this all organized a little differently,
77 * have Process for the whole group and Task for what's there for
78 * per-thread stuff. But for now this is the less invasive way of
79 * structuring it. */
Petr Machatafd43ef72012-02-10 12:25:11 +010080typedef struct Process Process;
Petr Machata366c2f42012-02-09 19:34:36 +010081struct Process {
82 enum process_state state;
83 Process * parent; /* needed by STATE_BEING_CREATED */
84 char * filename;
85 pid_t pid;
86
87 /* Dictionary of breakpoints (which is a mapping
88 * address->breakpoint). This is NULL for non-leader
Petr Machataecb082f2012-04-05 02:10:10 +020089 * processes. XXX note that we store addresses (keys) by
90 * value. That assumes that target_address_t fits in host
91 * pointer. */
Petr Machata366c2f42012-02-09 19:34:36 +010092 Dict * breakpoints;
93
94 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
95 unsigned int personality;
96 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
97
98 int callstack_depth;
99 struct callstack_element callstack[MAX_CALLDEPTH];
Petr Machata76dd9292012-04-03 13:02:06 +0200100
101 /* Linked list of libraries in backwards order of mapping.
102 * The last element is the executed binary itself. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100103 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +0100104
Petr Machata366c2f42012-02-09 19:34:36 +0100105 /* Arch-dependent: */
106 void * debug; /* arch-dep process debug struct */
Petr Machata366c2f42012-02-09 19:34:36 +0100107 void * instruction_pointer;
108 void * stack_pointer; /* To get return addr, args... */
109 void * return_addr;
110 void * arch_ptr;
111 short e_machine;
Petr Machata366c2f42012-02-09 19:34:36 +0100112#ifdef __arm__
113 int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */
114#endif
115
116#if defined(HAVE_LIBUNWIND)
117 /* libunwind address space */
118 unw_addr_space_t unwind_as;
119 void *unwind_priv;
120#endif /* defined(HAVE_LIBUNWIND) */
121
122 /* Set in leader. */
123 struct event_handler *event_handler;
124
125 /**
126 * Process chaining.
127 **/
128 Process * next;
129
130 /* LEADER points to the leader thread of the POSIX.1 process.
131 If X->LEADER == X, then X is the leader thread and the
132 Process structures chained by NEXT represent other threads,
133 up until, but not including, the next leader thread.
134 LEADER may be NULL after the leader has already exited. In
135 that case this process is waiting to be collected. */
136 Process * leader;
137};
138
Petr Machata2b46cfc2012-02-18 11:17:29 +0100139int process_init(struct Process *proc,
140 const char *filename, pid_t pid, int enable_breakpoints);
141
142Process * open_program(const char *filename, pid_t pid, int enable_breakpoints);
Petr Machata366c2f42012-02-09 19:34:36 +0100143void open_pid(pid_t pid);
144Process * pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100145
146/* Clone the contents of PROC into the memory referenced by RETP.
147 * Returns 0 on success or a negative value on failure. */
148int process_clone(struct Process *retp, struct Process *proc, pid_t pid);
149
150/* Iterate through the processes that ltrace currently traces. CB is
151 * called for each process. Tasks are considered to be processes for
152 * the purpose of this iterator.
153 *
Petr Machata74132a42012-03-16 02:46:18 +0100154 * Notes on this iteration interface: The iteration starts after the
155 * process designated by START_AFTER, or at the first process if
156 * START_AFTER is NULL. DATA is passed verbatim to CB. If CB returns
157 * CBS_STOP, the iteration stops and the current iterator is returned.
158 * That iterator can then be used to restart the iteration. NULL is
Petr Machata2b46cfc2012-02-18 11:17:29 +0100159 * returned when iteration ends.
160 *
161 * There's no provision for returning error states. Errors need to be
162 * signaled to the caller via DATA, together with any other data that
163 * the callback needs. */
Petr Machata74132a42012-03-16 02:46:18 +0100164Process *each_process(Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100165 enum callback_status (*cb)(struct Process *proc,
166 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100167 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100168
Petr Machata74132a42012-03-16 02:46:18 +0100169/* Iterate through list of tasks of given process PROC. Restarts are
170 * supported via START_AFTER (see each_process for details of
171 * iteration interface). */
172Process *each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100173 enum callback_status (*cb)(struct Process *proc,
174 void *data),
Petr Machata366c2f42012-02-09 19:34:36 +0100175 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100176
Petr Machata366c2f42012-02-09 19:34:36 +0100177void change_process_leader(Process *proc, Process *leader);
178void remove_process(Process *proc);
179void install_event_handler(Process *proc, struct event_handler *handler);
180void destroy_event_handler(Process *proc);
181
Petr Machata2b46cfc2012-02-18 11:17:29 +0100182/* Add a library LIB to the list of PROC's libraries. */
183void proc_add_library(struct Process *proc, struct library *lib);
184
185/* Remove LIB from list of PROC's libraries. Returns 0 if the library
186 * was found and unlinked, otherwise returns a negative value. */
187int proc_remove_library(struct Process *proc, struct library *lib);
188
189/* Iterate through the libraries of PROC. See each_process for
190 * detailed description of the iteration interface. */
191struct library *proc_each_library(struct Process *proc, struct library *start,
192 enum callback_status (*cb)(struct Process *p,
193 struct library *l,
194 void *data),
195 void *data);
196
Petr Machata52dbfb12012-03-29 16:38:26 +0200197/* Insert BP into PROC. */
198int proc_add_breakpoint(struct Process *proc, struct breakpoint *bp);
199
200/* Remove BP from PROC. */
201int proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100202
Petr Machatad3cc9882012-04-13 21:40:23 +0200203/* Iterate through the libraries of PROC. See each_process for
204 * detailed description of the iteration interface. */
205void *proc_each_breakpoint(struct Process *proc, void *start,
206 enum callback_status (*cb)(struct Process *proc,
207 struct breakpoint *bp,
208 void *data),
209 void *data);
210
Petr Machata366c2f42012-02-09 19:34:36 +0100211#endif /* _PROC_H_ */