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