Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 1 | /* |
| 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 Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 23 | #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 Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame^] | 32 | #include "sysdep.h" |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 33 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 34 | struct library; |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 35 | struct breakpoint; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 36 | |
| 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. */ |
| 40 | enum callback_status { |
| 41 | CBS_STOP, /* The iteration should stop. */ |
| 42 | CBS_CONT, /* The iteration should continue. */ |
Petr Machata | ef7fa37 | 2012-03-28 02:05:36 +0200 | [diff] [blame] | 43 | CBS_FAIL, /* There was an error. The iteration should stop |
| 44 | * and return error. */ |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 47 | struct 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 | |
| 57 | enum 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 Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 63 | struct 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 Machata | fd43ef7 | 2012-02-10 12:25:11 +0100 | [diff] [blame] | 81 | typedef struct Process Process; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 82 | struct 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 Machata | ecb082f | 2012-04-05 02:10:10 +0200 | [diff] [blame] | 90 | * processes. XXX note that we store addresses (keys) by |
| 91 | * value. That assumes that target_address_t fits in host |
| 92 | * pointer. */ |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 93 | 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 Machata | 76dd929 | 2012-04-03 13:02:06 +0200 | [diff] [blame] | 101 | |
| 102 | /* Linked list of libraries in backwards order of mapping. |
| 103 | * The last element is the executed binary itself. */ |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 104 | struct library *libraries; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 105 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 106 | /* Arch-dependent: */ |
Petr Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame^] | 107 | void *debug; /* arch-dep process debug struct XXX move to |
| 108 | * os_process_data after it's invented. */ |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 109 | 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 Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 114 | #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 Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame^] | 139 | |
| 140 | struct arch_process_data arch; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
Petr Machata | 75934ad | 2012-04-14 02:28:03 +0200 | [diff] [blame] | 143 | /* Initialize a process given a path to binary FILENAME, with a PID, |
| 144 | * and add the process to an internal chain of traced processes. */ |
| 145 | int process_init(struct Process *proc, const char *filename, pid_t pid); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 146 | |
Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 147 | /* 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. */ |
| 150 | int 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. */ |
| 158 | void process_destroy(struct Process *proc); |
| 159 | |
Petr Machata | 75934ad | 2012-04-14 02:28:03 +0200 | [diff] [blame] | 160 | struct Process *open_program(const char *filename, pid_t pid); |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 161 | void open_pid(pid_t pid); |
| 162 | Process * pid2proc(pid_t pid); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 163 | |
| 164 | /* Clone the contents of PROC into the memory referenced by RETP. |
| 165 | * Returns 0 on success or a negative value on failure. */ |
| 166 | int 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 Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 172 | * 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 Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 177 | * 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 Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 182 | Process *each_process(Process *start_after, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 183 | enum callback_status (*cb)(struct Process *proc, |
| 184 | void *data), |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 185 | void *data); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 186 | |
Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 187 | /* 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). */ |
| 190 | Process *each_task(struct Process *proc, struct Process *start_after, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 191 | enum callback_status (*cb)(struct Process *proc, |
| 192 | void *data), |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 193 | void *data); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 194 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 195 | void change_process_leader(Process *proc, Process *leader); |
| 196 | void remove_process(Process *proc); |
| 197 | void install_event_handler(Process *proc, struct event_handler *handler); |
| 198 | void destroy_event_handler(Process *proc); |
| 199 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 200 | /* Add a library LIB to the list of PROC's libraries. */ |
| 201 | void 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. */ |
| 205 | int 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. */ |
| 209 | struct 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 Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 215 | /* Insert BP into PROC. */ |
| 216 | int proc_add_breakpoint(struct Process *proc, struct breakpoint *bp); |
| 217 | |
| 218 | /* Remove BP from PROC. */ |
| 219 | int proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 220 | |
Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 221 | /* Iterate through the libraries of PROC. See each_process for |
| 222 | * detailed description of the iteration interface. */ |
| 223 | void *proc_each_breakpoint(struct Process *proc, void *start, |
| 224 | enum callback_status (*cb)(struct Process *proc, |
| 225 | struct breakpoint *bp, |
| 226 | void *data), |
| 227 | void *data); |
| 228 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 229 | #endif /* _PROC_H_ */ |