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 | |
Petr Machata | 8a568dd | 2012-05-18 14:12:27 +0200 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Andrey Zonov | d2c5dfd | 2012-08-05 00:16:55 +0400 | [diff] [blame^] | 28 | #include <sys/time.h> |
| 29 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 30 | #if defined(HAVE_LIBUNWIND) |
| 31 | # include <libunwind.h> |
| 32 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 33 | |
| 34 | #include "ltrace.h" |
| 35 | #include "dict.h" |
Petr Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame] | 36 | #include "sysdep.h" |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 37 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 38 | struct library; |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 39 | struct breakpoint; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 40 | |
| 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. */ |
| 44 | enum callback_status { |
| 45 | CBS_STOP, /* The iteration should stop. */ |
| 46 | CBS_CONT, /* The iteration should continue. */ |
Petr Machata | ef7fa37 | 2012-03-28 02:05:36 +0200 | [diff] [blame] | 47 | CBS_FAIL, /* There was an error. The iteration should stop |
| 48 | * and return error. */ |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 51 | struct 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 | |
| 61 | enum 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 Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 67 | struct 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; |
| 76 | }; |
| 77 | |
| 78 | /* XXX We should get rid of this. */ |
| 79 | #define MAX_CALLDEPTH 64 |
| 80 | |
| 81 | /* XXX We would rather have this all organized a little differently, |
| 82 | * have Process for the whole group and Task for what's there for |
| 83 | * per-thread stuff. But for now this is the less invasive way of |
| 84 | * structuring it. */ |
Petr Machata | fd43ef7 | 2012-02-10 12:25:11 +0100 | [diff] [blame] | 85 | typedef struct Process Process; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 86 | struct Process { |
| 87 | enum process_state state; |
| 88 | Process * parent; /* needed by STATE_BEING_CREATED */ |
| 89 | char * filename; |
| 90 | pid_t pid; |
| 91 | |
| 92 | /* Dictionary of breakpoints (which is a mapping |
| 93 | * address->breakpoint). This is NULL for non-leader |
Petr Machata | ecb082f | 2012-04-05 02:10:10 +0200 | [diff] [blame] | 94 | * processes. XXX note that we store addresses (keys) by |
| 95 | * value. That assumes that target_address_t fits in host |
| 96 | * pointer. */ |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 97 | Dict * breakpoints; |
| 98 | |
| 99 | int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */ |
| 100 | unsigned int personality; |
| 101 | int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */ |
| 102 | |
| 103 | int callstack_depth; |
| 104 | struct callstack_element callstack[MAX_CALLDEPTH]; |
Petr Machata | 76dd929 | 2012-04-03 13:02:06 +0200 | [diff] [blame] | 105 | |
| 106 | /* Linked list of libraries in backwards order of mapping. |
| 107 | * The last element is the executed binary itself. */ |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 108 | struct library *libraries; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 109 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 110 | /* Arch-dependent: */ |
Petr Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame] | 111 | void *debug; /* arch-dep process debug struct XXX move to |
| 112 | * os_process_data after it's invented. */ |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 113 | void * instruction_pointer; |
| 114 | void * stack_pointer; /* To get return addr, args... */ |
| 115 | void * return_addr; |
| 116 | void * arch_ptr; |
| 117 | short e_machine; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 118 | #ifdef __arm__ |
| 119 | int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */ |
| 120 | #endif |
| 121 | |
| 122 | #if defined(HAVE_LIBUNWIND) |
| 123 | /* libunwind address space */ |
| 124 | unw_addr_space_t unwind_as; |
| 125 | void *unwind_priv; |
| 126 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 127 | |
| 128 | /* Set in leader. */ |
| 129 | struct event_handler *event_handler; |
| 130 | |
| 131 | /** |
| 132 | * Process chaining. |
| 133 | **/ |
| 134 | Process * next; |
| 135 | |
| 136 | /* LEADER points to the leader thread of the POSIX.1 process. |
| 137 | If X->LEADER == X, then X is the leader thread and the |
| 138 | Process structures chained by NEXT represent other threads, |
| 139 | up until, but not including, the next leader thread. |
| 140 | LEADER may be NULL after the leader has already exited. In |
| 141 | that case this process is waiting to be collected. */ |
| 142 | Process * leader; |
Petr Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame] | 143 | |
| 144 | struct arch_process_data arch; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
Petr Machata | 75934ad | 2012-04-14 02:28:03 +0200 | [diff] [blame] | 147 | /* Initialize a process given a path to binary FILENAME, with a PID, |
| 148 | * and add the process to an internal chain of traced processes. */ |
| 149 | int process_init(struct Process *proc, const char *filename, pid_t pid); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 150 | |
Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 151 | /* PROC underwent an exec. This is a bit like process_destroy |
| 152 | * followed by process_init, except that some state is kept and the |
| 153 | * process doesn't lose it's place in the list of processes. */ |
| 154 | int process_exec(struct Process *proc); |
| 155 | |
| 156 | /* Release any memory allocated for PROC (but not PROC itself). Does |
| 157 | * NOT remove PROC from internal chain. |
| 158 | * |
| 159 | * XXX clearly this init/destroy pair is different than others and |
| 160 | * should be fixed. process_init should presumably be separate from |
| 161 | * process_add. */ |
| 162 | void process_destroy(struct Process *proc); |
| 163 | |
Petr Machata | 75934ad | 2012-04-14 02:28:03 +0200 | [diff] [blame] | 164 | struct Process *open_program(const char *filename, pid_t pid); |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 165 | void open_pid(pid_t pid); |
| 166 | Process * pid2proc(pid_t pid); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 167 | |
| 168 | /* Clone the contents of PROC into the memory referenced by RETP. |
| 169 | * Returns 0 on success or a negative value on failure. */ |
| 170 | int process_clone(struct Process *retp, struct Process *proc, pid_t pid); |
| 171 | |
| 172 | /* Iterate through the processes that ltrace currently traces. CB is |
| 173 | * called for each process. Tasks are considered to be processes for |
| 174 | * the purpose of this iterator. |
| 175 | * |
Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 176 | * Notes on this iteration interface: The iteration starts after the |
| 177 | * process designated by START_AFTER, or at the first process if |
| 178 | * START_AFTER is NULL. DATA is passed verbatim to CB. If CB returns |
| 179 | * CBS_STOP, the iteration stops and the current iterator is returned. |
| 180 | * That iterator can then be used to restart the iteration. NULL is |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 181 | * returned when iteration ends. |
| 182 | * |
| 183 | * There's no provision for returning error states. Errors need to be |
| 184 | * signaled to the caller via DATA, together with any other data that |
| 185 | * the callback needs. */ |
Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 186 | Process *each_process(Process *start_after, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 187 | enum callback_status (*cb)(struct Process *proc, |
| 188 | void *data), |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 189 | void *data); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 190 | |
Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 191 | /* Iterate through list of tasks of given process PROC. Restarts are |
| 192 | * supported via START_AFTER (see each_process for details of |
| 193 | * iteration interface). */ |
| 194 | Process *each_task(struct Process *proc, struct Process *start_after, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 195 | enum callback_status (*cb)(struct Process *proc, |
| 196 | void *data), |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 197 | void *data); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 198 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 199 | void change_process_leader(Process *proc, Process *leader); |
Petr Machata | fd2641c | 2012-04-24 21:33:16 +0200 | [diff] [blame] | 200 | |
| 201 | /* Remove process from the list of traced processes, drop any events |
| 202 | * in the event queue, destroy it and free memory. */ |
| 203 | void remove_process(struct Process *proc); |
| 204 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 205 | void install_event_handler(Process *proc, struct event_handler *handler); |
| 206 | void destroy_event_handler(Process *proc); |
| 207 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 208 | /* Add a library LIB to the list of PROC's libraries. */ |
| 209 | void proc_add_library(struct Process *proc, struct library *lib); |
| 210 | |
| 211 | /* Remove LIB from list of PROC's libraries. Returns 0 if the library |
| 212 | * was found and unlinked, otherwise returns a negative value. */ |
| 213 | int proc_remove_library(struct Process *proc, struct library *lib); |
| 214 | |
| 215 | /* Iterate through the libraries of PROC. See each_process for |
| 216 | * detailed description of the iteration interface. */ |
| 217 | struct library *proc_each_library(struct Process *proc, struct library *start, |
| 218 | enum callback_status (*cb)(struct Process *p, |
| 219 | struct library *l, |
| 220 | void *data), |
| 221 | void *data); |
| 222 | |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 223 | /* Insert BP into PROC. */ |
| 224 | int proc_add_breakpoint(struct Process *proc, struct breakpoint *bp); |
| 225 | |
Petr Machata | f7fee43 | 2012-04-19 17:00:53 +0200 | [diff] [blame] | 226 | /* Remove BP from PROC. This has no reason to fail in runtime. If it |
| 227 | * does not find BP in PROC, it's hard error guarded by assertion. */ |
| 228 | void proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 229 | |
Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 230 | /* Iterate through the libraries of PROC. See each_process for |
| 231 | * detailed description of the iteration interface. */ |
| 232 | void *proc_each_breakpoint(struct Process *proc, void *start, |
| 233 | enum callback_status (*cb)(struct Process *proc, |
| 234 | struct breakpoint *bp, |
| 235 | void *data), |
| 236 | void *data); |
| 237 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 238 | #endif /* _PROC_H_ */ |