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" |
| 32 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 33 | struct library; |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame^] | 34 | struct breakpoint; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 35 | |
| 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. */ |
| 39 | enum callback_status { |
| 40 | CBS_STOP, /* The iteration should stop. */ |
| 41 | CBS_CONT, /* The iteration should continue. */ |
Petr Machata | ef7fa37 | 2012-03-28 02:05:36 +0200 | [diff] [blame] | 42 | CBS_FAIL, /* There was an error. The iteration should stop |
| 43 | * and return error. */ |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 46 | struct 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 | |
| 56 | enum 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 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; |
| 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 Machata | fd43ef7 | 2012-02-10 12:25:11 +0100 | [diff] [blame] | 80 | typedef struct Process Process; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 81 | struct 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 |
| 89 | * processes. */ |
| 90 | Dict * breakpoints; |
| 91 | |
| 92 | int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */ |
| 93 | unsigned int personality; |
| 94 | int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */ |
| 95 | |
| 96 | int callstack_depth; |
| 97 | struct callstack_element callstack[MAX_CALLDEPTH]; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 98 | struct library *libraries; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 99 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 100 | /* Arch-dependent: */ |
| 101 | void * debug; /* arch-dep process debug struct */ |
| 102 | long debug_state; /* arch-dep debug state */ |
| 103 | void * instruction_pointer; |
| 104 | void * stack_pointer; /* To get return addr, args... */ |
| 105 | void * return_addr; |
| 106 | void * arch_ptr; |
| 107 | short e_machine; |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 108 | #ifdef __arm__ |
| 109 | int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */ |
| 110 | #endif |
| 111 | |
| 112 | #if defined(HAVE_LIBUNWIND) |
| 113 | /* libunwind address space */ |
| 114 | unw_addr_space_t unwind_as; |
| 115 | void *unwind_priv; |
| 116 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 117 | |
| 118 | /* Set in leader. */ |
| 119 | struct event_handler *event_handler; |
| 120 | |
| 121 | /** |
| 122 | * Process chaining. |
| 123 | **/ |
| 124 | Process * next; |
| 125 | |
| 126 | /* LEADER points to the leader thread of the POSIX.1 process. |
| 127 | If X->LEADER == X, then X is the leader thread and the |
| 128 | Process structures chained by NEXT represent other threads, |
| 129 | up until, but not including, the next leader thread. |
| 130 | LEADER may be NULL after the leader has already exited. In |
| 131 | that case this process is waiting to be collected. */ |
| 132 | Process * leader; |
| 133 | }; |
| 134 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 135 | int process_init(struct Process *proc, |
| 136 | const char *filename, pid_t pid, int enable_breakpoints); |
| 137 | |
| 138 | Process * open_program(const char *filename, pid_t pid, int enable_breakpoints); |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 139 | void open_pid(pid_t pid); |
| 140 | Process * pid2proc(pid_t pid); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 141 | |
| 142 | /* Clone the contents of PROC into the memory referenced by RETP. |
| 143 | * Returns 0 on success or a negative value on failure. */ |
| 144 | int process_clone(struct Process *retp, struct Process *proc, pid_t pid); |
| 145 | |
| 146 | /* Iterate through the processes that ltrace currently traces. CB is |
| 147 | * called for each process. Tasks are considered to be processes for |
| 148 | * the purpose of this iterator. |
| 149 | * |
Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 150 | * Notes on this iteration interface: The iteration starts after the |
| 151 | * process designated by START_AFTER, or at the first process if |
| 152 | * START_AFTER is NULL. DATA is passed verbatim to CB. If CB returns |
| 153 | * CBS_STOP, the iteration stops and the current iterator is returned. |
| 154 | * That iterator can then be used to restart the iteration. NULL is |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 155 | * returned when iteration ends. |
| 156 | * |
| 157 | * There's no provision for returning error states. Errors need to be |
| 158 | * signaled to the caller via DATA, together with any other data that |
| 159 | * the callback needs. */ |
Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 160 | Process *each_process(Process *start_after, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 161 | enum callback_status (*cb)(struct Process *proc, |
| 162 | void *data), |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 163 | void *data); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 164 | |
Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 165 | /* Iterate through list of tasks of given process PROC. Restarts are |
| 166 | * supported via START_AFTER (see each_process for details of |
| 167 | * iteration interface). */ |
| 168 | Process *each_task(struct Process *proc, struct Process *start_after, |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 169 | enum callback_status (*cb)(struct Process *proc, |
| 170 | void *data), |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 171 | void *data); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 172 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 173 | void add_process(Process *proc); |
| 174 | void change_process_leader(Process *proc, Process *leader); |
| 175 | void remove_process(Process *proc); |
| 176 | void install_event_handler(Process *proc, struct event_handler *handler); |
| 177 | void destroy_event_handler(Process *proc); |
| 178 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 179 | /* Add a library LIB to the list of PROC's libraries. */ |
| 180 | void proc_add_library(struct Process *proc, struct library *lib); |
| 181 | |
| 182 | /* Remove LIB from list of PROC's libraries. Returns 0 if the library |
| 183 | * was found and unlinked, otherwise returns a negative value. */ |
| 184 | int proc_remove_library(struct Process *proc, struct library *lib); |
| 185 | |
| 186 | /* Iterate through the libraries of PROC. See each_process for |
| 187 | * detailed description of the iteration interface. */ |
| 188 | struct library *proc_each_library(struct Process *proc, struct library *start, |
| 189 | enum callback_status (*cb)(struct Process *p, |
| 190 | struct library *l, |
| 191 | void *data), |
| 192 | void *data); |
| 193 | |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame^] | 194 | /* Insert BP into PROC. */ |
| 195 | int proc_add_breakpoint(struct Process *proc, struct breakpoint *bp); |
| 196 | |
| 197 | /* Remove BP from PROC. */ |
| 198 | int proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 199 | |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 200 | #endif /* _PROC_H_ */ |