Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
Petr Machata | 653085a | 2013-01-15 17:40:40 +0100 | [diff] [blame] | 3 | * Copyright (C) 2010,2011,2012,2013 Petr Machata, Red Hat Inc. |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 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> |
Petr Machata | 653085a | 2013-01-15 17:40:40 +0100 | [diff] [blame] | 29 | #include <stdint.h> |
Andrey Zonov | d2c5dfd | 2012-08-05 00:16:55 +0400 | [diff] [blame] | 30 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 31 | #if defined(HAVE_LIBUNWIND) |
| 32 | # include <libunwind.h> |
| 33 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 34 | |
| 35 | #include "ltrace.h" |
| 36 | #include "dict.h" |
Petr Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame] | 37 | #include "sysdep.h" |
Petr Machata | a24021c | 2012-09-25 14:46:44 +0200 | [diff] [blame] | 38 | #include "callback.h" |
Petr Machata | 6d8aa0b | 2012-10-31 03:27:36 +0100 | [diff] [blame] | 39 | #include "forward.h" |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 40 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 41 | struct event_handler { |
| 42 | /* Event handler that overrides the default one. Should |
| 43 | * return NULL if the event was handled, otherwise the |
| 44 | * returned event is passed to the default handler. */ |
| 45 | Event *(*on_event)(struct event_handler *self, Event *event); |
| 46 | |
| 47 | /* Called when the event handler removal is requested. */ |
| 48 | void (*destroy)(struct event_handler *self); |
| 49 | }; |
| 50 | |
| 51 | enum process_state { |
| 52 | STATE_ATTACHED = 0, |
| 53 | STATE_BEING_CREATED, |
| 54 | STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */ |
| 55 | }; |
| 56 | |
Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 57 | struct output_state { |
| 58 | size_t params_left; |
| 59 | int need_delim; |
| 60 | }; |
| 61 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 62 | struct 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; |
Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 70 | struct fetch_context *fetch_context; |
Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 71 | struct value_dict *arguments; |
Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 72 | struct output_state out; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /* XXX We should get rid of this. */ |
| 76 | #define MAX_CALLDEPTH 64 |
| 77 | |
| 78 | /* XXX We would rather have this all organized a little differently, |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 79 | * have struct process for the whole group and struct task (or struct |
| 80 | * lwp, struct thread) for what's there for per-thread stuff. But for |
| 81 | * now this is the less invasive way of structuring it. */ |
| 82 | struct process { |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 83 | enum process_state state; |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 84 | struct process *parent; /* needed by STATE_BEING_CREATED */ |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 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 | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 90 | * processes. */ |
| 91 | struct dict *breakpoints; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 92 | |
| 93 | int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */ |
| 94 | unsigned int personality; |
| 95 | int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */ |
| 96 | |
Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 97 | size_t callstack_depth; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 98 | struct callstack_element callstack[MAX_CALLDEPTH]; |
Petr Machata | 76dd929 | 2012-04-03 13:02:06 +0200 | [diff] [blame] | 99 | |
| 100 | /* Linked list of libraries in backwards order of mapping. |
| 101 | * The last element is the executed binary itself. */ |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 102 | struct library *libraries; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 103 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 104 | /* Arch-dependent: */ |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 105 | void * instruction_pointer; |
| 106 | void * stack_pointer; /* To get return addr, args... */ |
| 107 | void * return_addr; |
| 108 | void * arch_ptr; |
Petr Machata | 4d4e1b8 | 2012-05-30 11:08:39 -0400 | [diff] [blame] | 109 | |
| 110 | /* XXX We would like to replace this with a pointer to ABI |
| 111 | * object that would provide the relevant services, instead of |
| 112 | * checking the necessary flags in the back end ad |
| 113 | * nauseam. */ |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 114 | short e_machine; |
Petr Machata | 4d4e1b8 | 2012-05-30 11:08:39 -0400 | [diff] [blame] | 115 | char e_class; |
| 116 | |
| 117 | /* XXX this shoudl go to ARM's arch_process_data. */ |
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 | **/ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 134 | struct process *next; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 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 |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 138 | process structures chained by NEXT represent other threads, |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 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. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 142 | struct process *leader; |
Petr Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame] | 143 | |
Petr Machata | 0f6e6d9 | 2012-10-26 23:42:17 +0200 | [diff] [blame] | 144 | struct os_process_data os; |
Petr Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame] | 145 | struct arch_process_data arch; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
Petr Machata | 75934ad | 2012-04-14 02:28:03 +0200 | [diff] [blame] | 148 | /* Initialize a process given a path to binary FILENAME, with a PID, |
| 149 | * and add the process to an internal chain of traced processes. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 150 | int process_init(struct process *proc, const char *filename, pid_t pid); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 151 | |
Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 152 | /* PROC underwent an exec. This is a bit like process_destroy |
| 153 | * followed by process_init, except that some state is kept and the |
| 154 | * process doesn't lose it's place in the list of processes. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 155 | int process_exec(struct process *proc); |
Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 156 | |
| 157 | /* Release any memory allocated for PROC (but not PROC itself). Does |
| 158 | * NOT remove PROC from internal chain. |
| 159 | * |
| 160 | * XXX clearly this init/destroy pair is different than others and |
| 161 | * should be fixed. process_init should presumably be separate from |
| 162 | * process_add. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 163 | void process_destroy(struct process *proc); |
Petr Machata | 3d0c91c | 2012-04-14 02:37:38 +0200 | [diff] [blame] | 164 | |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 165 | struct process *open_program(const char *filename, pid_t pid); |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 166 | void open_pid(pid_t pid); |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 167 | struct process *pid2proc(pid_t pid); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 168 | |
| 169 | /* Clone the contents of PROC into the memory referenced by RETP. |
| 170 | * Returns 0 on success or a negative value on failure. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 171 | int process_clone(struct process *retp, struct process *proc, pid_t pid); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 172 | |
Petr Machata | a24021c | 2012-09-25 14:46:44 +0200 | [diff] [blame] | 173 | /* Iterate through the processes that ltrace currently traces. Tasks |
| 174 | * are considered to be processes for the purpose of this iterator. |
| 175 | * See callback.h for notes on iteration interfaces. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 176 | struct process *each_process(struct process *start_after, |
| 177 | enum callback_status (*cb)(struct process *proc, |
| 178 | void *data), |
| 179 | void *data); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 180 | |
Petr Machata | a24021c | 2012-09-25 14:46:44 +0200 | [diff] [blame] | 181 | /* Iterate through list of tasks of given process PROC. See |
| 182 | * callback.h for notes on iteration interfaces. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 183 | struct process *each_task(struct process *proc, struct process *start_after, |
| 184 | enum callback_status (*cb)(struct process *proc, |
| 185 | void *data), |
| 186 | void *data); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 187 | |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 188 | void change_process_leader(struct process *proc, struct process *leader); |
Petr Machata | fd2641c | 2012-04-24 21:33:16 +0200 | [diff] [blame] | 189 | |
| 190 | /* Remove process from the list of traced processes, drop any events |
| 191 | * in the event queue, destroy it and free memory. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 192 | void remove_process(struct process *proc); |
Petr Machata | fd2641c | 2012-04-24 21:33:16 +0200 | [diff] [blame] | 193 | |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 194 | void install_event_handler(struct process *proc, struct event_handler *handler); |
| 195 | void destroy_event_handler(struct process *proc); |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 196 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 197 | /* Add a library LIB to the list of PROC's libraries. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 198 | void proc_add_library(struct process *proc, struct library *lib); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 199 | |
| 200 | /* Remove LIB from list of PROC's libraries. Returns 0 if the library |
| 201 | * was found and unlinked, otherwise returns a negative value. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 202 | int proc_remove_library(struct process *proc, struct library *lib); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 203 | |
Petr Machata | ef2fd27 | 2012-09-28 00:43:01 +0200 | [diff] [blame] | 204 | /* Clear a delayed flag. If a symbol is neither latent, nor delayed, |
| 205 | * a breakpoint is inserted for it. Returns 0 if the activation was |
| 206 | * successful or a negative value if it failed. Note that if a symbol |
| 207 | * is both latent and delayed, this will not enable the corresponding |
| 208 | * breakpoint. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 209 | int proc_activate_delayed_symbol(struct process *proc, |
Petr Machata | ef2fd27 | 2012-09-28 00:43:01 +0200 | [diff] [blame] | 210 | struct library_symbol *libsym); |
| 211 | |
Petr Machata | a24021c | 2012-09-25 14:46:44 +0200 | [diff] [blame] | 212 | /* Iterate through the libraries of PROC. See callback.h for notes on |
| 213 | * iteration interfaces. */ |
Petr Machata | 7ac04ed | 2012-11-23 19:00:41 +0100 | [diff] [blame] | 214 | struct library *proc_each_library(struct process *proc, |
| 215 | struct library *start_after, |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 216 | enum callback_status (*cb)(struct process *p, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 217 | struct library *l, |
| 218 | void *data), |
| 219 | void *data); |
| 220 | |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 221 | /* Insert BP into PROC. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 222 | int proc_add_breakpoint(struct process *proc, struct breakpoint *bp); |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 223 | |
Petr Machata | f7fee43 | 2012-04-19 17:00:53 +0200 | [diff] [blame] | 224 | /* Remove BP from PROC. This has no reason to fail in runtime. If it |
| 225 | * does not find BP in PROC, it's hard error guarded by assertion. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 226 | void proc_remove_breakpoint(struct process *proc, struct breakpoint *bp); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 227 | |
Petr Machata | a24021c | 2012-09-25 14:46:44 +0200 | [diff] [blame] | 228 | /* Iterate through the breakpoints of PROC. See callback.h for notes |
| 229 | * on iteration interfaces. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 230 | void *proc_each_breakpoint(struct process *proc, void *start, |
| 231 | enum callback_status (*cb)(struct process *proc, |
Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 232 | struct breakpoint *bp, |
| 233 | void *data), |
| 234 | void *data); |
| 235 | |
Edgar E. Iglesias | cc77b0e | 2012-10-09 12:15:20 +0200 | [diff] [blame] | 236 | /* Iterate through the dynamic section at src_addr looking for D_TAG. |
| 237 | * If tag is found, fill it's value in RET and return 0. |
| 238 | * If tag is not found, return a negative value. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 239 | int proc_find_dynamic_entry_addr(struct process *proc, arch_addr_t src_addr, |
Edgar E. Iglesias | cc77b0e | 2012-10-09 12:15:20 +0200 | [diff] [blame] | 240 | int d_tag, arch_addr_t *ret); |
Petr Machata | 165b566 | 2012-10-27 19:23:12 +0200 | [diff] [blame] | 241 | |
| 242 | /* Finds a symbol corresponding to LIBSYM in a process PROC. Returns |
| 243 | * 0 and sets *RETLIB and *RETSYM if the corresponding pointer is |
| 244 | * non-NULL. Returns a negative value when the symbols couldn't be |
| 245 | * found. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 246 | int proc_find_symbol(struct process *proc, struct library_symbol *sym, |
Petr Machata | 165b566 | 2012-10-27 19:23:12 +0200 | [diff] [blame] | 247 | struct library **retlib, struct library_symbol **retsym); |
| 248 | |
Petr Machata | 3240554 | 2012-10-31 03:28:39 +0100 | [diff] [blame] | 249 | /* Iterate through all symbols in all libraries of PROC. See |
| 250 | * callback.h for notes on this interface. */ |
| 251 | struct library_symbol *proc_each_symbol |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 252 | (struct process *proc, struct library_symbol *start_after, |
Petr Machata | 3240554 | 2012-10-31 03:28:39 +0100 | [diff] [blame] | 253 | enum callback_status (*cb)(struct library_symbol *, void *), |
| 254 | void *data); |
| 255 | |
Petr Machata | dc70e76 | 2013-01-23 00:02:26 +0100 | [diff] [blame^] | 256 | /* Read 8, 16, 32 or 64-bit quantity located at ADDR in PROC. The |
Petr Machata | 653085a | 2013-01-15 17:40:40 +0100 | [diff] [blame] | 257 | * resulting value is stored in *LP. 0 is returned on success or a |
| 258 | * negative value on failure. This uses umovebytes under the hood |
| 259 | * (see backend.h). */ |
Petr Machata | dc70e76 | 2013-01-23 00:02:26 +0100 | [diff] [blame^] | 260 | int proc_read_8(struct process *proc, arch_addr_t addr, uint8_t *lp); |
Petr Machata | 653085a | 2013-01-15 17:40:40 +0100 | [diff] [blame] | 261 | int proc_read_16(struct process *proc, arch_addr_t addr, uint16_t *lp); |
| 262 | int proc_read_32(struct process *proc, arch_addr_t addr, uint32_t *lp); |
| 263 | int proc_read_64(struct process *proc, arch_addr_t addr, uint64_t *lp); |
| 264 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 265 | #endif /* _PROC_H_ */ |