| Michael K. Edwards | ef4428a | 2011-03-06 20:39:18 +0000 | [diff] [blame] | 1 | #ifndef COMMON_H |
| 2 | #define COMMON_H |
| 3 | |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 4 | #if defined(HAVE_LIBUNWIND) |
| 5 | #include <libunwind.h> |
| 6 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 7 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/time.h> |
| 10 | #include <stdio.h> |
| 11 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 12 | #include "ltrace.h" |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 13 | #include "defs.h" |
| 14 | #include "dict.h" |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 15 | #include "sysdep.h" |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 16 | #include "debug.h" |
| Marc Kleine-Budde | 747c73d | 2010-02-03 20:23:20 +0100 | [diff] [blame] | 17 | #include "ltrace-elf.h" |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 18 | #include "read_config_file.h" |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 19 | |
| 20 | #if defined HAVE_LIBIBERTY || defined HAVE_LIBSUPC__ |
| 21 | # define USE_DEMANGLE |
| 22 | #endif |
| 23 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 24 | extern char * command; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 25 | |
| 26 | extern int exiting; /* =1 if we have to exit ASAP */ |
| 27 | |
| 28 | typedef struct Breakpoint Breakpoint; |
| 29 | struct Breakpoint { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 30 | void * addr; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 31 | unsigned char orig_value[BREAKPOINT_LENGTH]; |
| 32 | int enabled; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 33 | struct library_symbol * libsym; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 34 | #ifdef __arm__ |
| 35 | int thumb_mode; |
| 36 | #endif |
| 37 | }; |
| 38 | |
| 39 | enum arg_type { |
| 40 | ARGTYPE_UNKNOWN = -1, |
| 41 | ARGTYPE_VOID, |
| 42 | ARGTYPE_INT, |
| 43 | ARGTYPE_UINT, |
| 44 | ARGTYPE_LONG, |
| 45 | ARGTYPE_ULONG, |
| 46 | ARGTYPE_OCTAL, |
| 47 | ARGTYPE_CHAR, |
| 48 | ARGTYPE_SHORT, |
| 49 | ARGTYPE_USHORT, |
| 50 | ARGTYPE_FLOAT, /* float value, may require index */ |
| 51 | ARGTYPE_DOUBLE, /* double value, may require index */ |
| 52 | ARGTYPE_ADDR, |
| 53 | ARGTYPE_FILE, |
| 54 | ARGTYPE_FORMAT, /* printf-like format */ |
| 55 | ARGTYPE_STRING, /* NUL-terminated string */ |
| 56 | ARGTYPE_STRING_N, /* String of known maxlen */ |
| 57 | ARGTYPE_ARRAY, /* Series of values in memory */ |
| 58 | ARGTYPE_ENUM, /* Enumeration */ |
| 59 | ARGTYPE_STRUCT, /* Structure of values */ |
| 60 | ARGTYPE_POINTER, /* Pointer to some other type */ |
| 61 | ARGTYPE_COUNT /* number of ARGTYPE_* values */ |
| 62 | }; |
| 63 | |
| 64 | typedef struct arg_type_info_t { |
| 65 | enum arg_type type; |
| 66 | union { |
| 67 | /* ARGTYPE_ENUM */ |
| 68 | struct { |
| 69 | size_t entries; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 70 | char ** keys; |
| 71 | int * values; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 72 | } enum_info; |
| 73 | |
| 74 | /* ARGTYPE_ARRAY */ |
| 75 | struct { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 76 | struct arg_type_info_t * elt_type; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 77 | size_t elt_size; |
| 78 | int len_spec; |
| 79 | } array_info; |
| 80 | |
| 81 | /* ARGTYPE_STRING_N */ |
| 82 | struct { |
| 83 | int size_spec; |
| 84 | } string_n_info; |
| 85 | |
| 86 | /* ARGTYPE_STRUCT */ |
| 87 | struct { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 88 | struct arg_type_info_t ** fields; /* NULL-terminated */ |
| 89 | size_t * offset; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 90 | size_t size; |
| 91 | } struct_info; |
| 92 | |
| 93 | /* ARGTYPE_POINTER */ |
| 94 | struct { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 95 | struct arg_type_info_t * info; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 96 | } ptr_info; |
| 97 | |
| 98 | /* ARGTYPE_FLOAT */ |
| 99 | struct { |
| 100 | size_t float_index; |
| 101 | } float_info; |
| 102 | |
| 103 | /* ARGTYPE_DOUBLE */ |
| 104 | struct { |
| 105 | size_t float_index; |
| 106 | } double_info; |
| 107 | } u; |
| 108 | } arg_type_info; |
| 109 | |
| 110 | enum tof { |
| 111 | LT_TOF_NONE = 0, |
| 112 | LT_TOF_FUNCTION, /* A real library function */ |
| 113 | LT_TOF_FUNCTIONR, /* Return from a real library function */ |
| 114 | LT_TOF_SYSCALL, /* A syscall */ |
| 115 | LT_TOF_SYSCALLR, /* Return from a syscall */ |
| 116 | LT_TOF_STRUCT /* Not a function; read args from struct */ |
| 117 | }; |
| 118 | |
| 119 | typedef struct Function Function; |
| 120 | struct Function { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 121 | const char * name; |
| 122 | arg_type_info * return_info; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 123 | int num_params; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 124 | arg_type_info * arg_info[MAX_ARGS]; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 125 | int params_right; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 126 | Function * next; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | enum toplt { |
| 130 | LS_TOPLT_NONE = 0, /* PLT not used for this symbol. */ |
| 131 | LS_TOPLT_EXEC, /* PLT for this symbol is executable. */ |
| 132 | LS_TOPLT_POINT /* PLT for this symbol is a non-executable. */ |
| 133 | }; |
| 134 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 135 | extern Function * list_of_functions; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 136 | extern char *PLTs_initialized_by_here; |
| 137 | |
| 138 | struct library_symbol { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 139 | char * name; |
| 140 | void * enter_addr; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 141 | char needs_init; |
| 142 | enum toplt plt_type; |
| 143 | char is_weak; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 144 | struct library_symbol * next; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | struct callstack_element { |
| 148 | union { |
| 149 | int syscall; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 150 | struct library_symbol * libfunc; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 151 | } c_un; |
| 152 | int is_syscall; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 153 | void * return_addr; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 154 | struct timeval time_spent; |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 155 | void * arch_ptr; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | #define MAX_CALLDEPTH 64 |
| 159 | |
| 160 | typedef enum Process_State Process_State; |
| 161 | enum Process_State { |
| 162 | STATE_ATTACHED = 0, |
| 163 | STATE_BEING_CREATED, |
| 164 | STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */ |
| 165 | }; |
| 166 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 167 | struct Process { |
| 168 | Process_State state; |
| Juan Cespedes | 2a61d19 | 2009-07-04 11:29:27 +0200 | [diff] [blame] | 169 | Process * parent; /* needed by STATE_BEING_CREATED */ |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 170 | char * filename; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 171 | pid_t pid; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 172 | Dict * breakpoints; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 173 | int breakpoints_enabled; /* -1:not enabled yet, 0:disabled, 1:enabled */ |
| 174 | int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */ |
| 175 | unsigned int personality; |
| 176 | int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */ |
| 177 | |
| 178 | int callstack_depth; |
| 179 | struct callstack_element callstack[MAX_CALLDEPTH]; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 180 | struct library_symbol * list_of_symbols; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 181 | |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 182 | int libdl_hooked; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 183 | /* Arch-dependent: */ |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 184 | void * debug; /* arch-dep process debug struct */ |
| 185 | long debug_state; /* arch-dep debug state */ |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 186 | void * instruction_pointer; |
| 187 | void * stack_pointer; /* To get return addr, args... */ |
| 188 | void * return_addr; |
| 189 | Breakpoint * breakpoint_being_enabled; |
| 190 | void * arch_ptr; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 191 | short e_machine; |
| 192 | short need_to_reinitialize_breakpoints; |
| 193 | #ifdef __arm__ |
| 194 | int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */ |
| 195 | #endif |
| 196 | |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 197 | #if defined(HAVE_LIBUNWIND) |
| 198 | /* libunwind address space */ |
| 199 | unw_addr_space_t unwind_as; |
| 200 | void *unwind_priv; |
| 201 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 202 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 203 | Process * next; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 204 | }; |
| 205 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 206 | struct opt_c_struct { |
| 207 | int count; |
| 208 | struct timeval tv; |
| 209 | }; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 210 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 211 | #include "options.h" |
| 212 | #include "output.h" |
| 213 | #ifdef USE_DEMANGLE |
| 214 | #include "demangle.h" |
| 215 | #endif |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 216 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 217 | extern Dict * dict_opt_c; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 218 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 219 | extern Process * list_of_processes; |
| 220 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 221 | extern Event * next_event(void); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 222 | extern Process * pid2proc(pid_t pid); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 223 | extern void handle_event(Event * event); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 224 | extern void execute_program(Process *, char **); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 225 | extern int display_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info); |
| 226 | extern Breakpoint * address2bpstruct(Process * proc, void * addr); |
| 227 | extern void breakpoints_init(Process * proc); |
| 228 | extern void insert_breakpoint(Process * proc, void * addr, struct library_symbol * libsym); |
| 229 | extern void delete_breakpoint(Process * proc, void * addr); |
| 230 | extern void enable_all_breakpoints(Process * proc); |
| 231 | extern void disable_all_breakpoints(Process * proc); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 232 | extern void reinitialize_breakpoints(Process *); |
| 233 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 234 | extern Process * open_program(char * filename, pid_t pid); |
| 235 | extern void open_pid(pid_t pid); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 236 | extern void show_summary(void); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 237 | extern arg_type_info * lookup_prototype(enum arg_type at); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 238 | |
| Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 239 | extern void do_init_elf(struct ltelf *lte, const char *filename); |
| 240 | extern void do_close_elf(struct ltelf *lte); |
| 241 | extern int in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym); |
| 242 | extern struct library_symbol *library_symbols; |
| 243 | extern void add_library_symbol(GElf_Addr addr, const char *name, |
| 244 | struct library_symbol **library_symbolspp, |
| 245 | enum toplt type_of_plt, int is_weak); |
| 246 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 247 | /* Arch-dependent stuff: */ |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 248 | extern char * pid2name(pid_t pid); |
| 249 | extern void trace_set_options(Process * proc, pid_t pid); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 250 | extern void trace_me(void); |
| 251 | extern int trace_pid(pid_t pid); |
| 252 | extern void untrace_pid(pid_t pid); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 253 | extern void get_arch_dep(Process * proc); |
| 254 | extern void * get_instruction_pointer(Process * proc); |
| 255 | extern void set_instruction_pointer(Process * proc, void * addr); |
| 256 | extern void * get_stack_pointer(Process * proc); |
| 257 | extern void * get_return_addr(Process * proc, void * stack_pointer); |
| Juan Cespedes | 2a61d19 | 2009-07-04 11:29:27 +0200 | [diff] [blame] | 258 | extern void set_return_addr(Process * proc, void * addr); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 259 | extern void enable_breakpoint(pid_t pid, Breakpoint * sbp); |
| 260 | extern void disable_breakpoint(pid_t pid, const Breakpoint * sbp); |
| 261 | extern int syscall_p(Process * proc, int status, int * sysnum); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 262 | extern void continue_process(pid_t pid); |
| 263 | extern void continue_after_signal(pid_t pid, int signum); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 264 | extern void continue_after_breakpoint(Process * proc, Breakpoint * sbp); |
| 265 | extern void continue_enabling_breakpoint(pid_t pid, Breakpoint * sbp); |
| 266 | extern long gimme_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info); |
| 267 | extern void save_register_args(enum tof type, Process * proc); |
| 268 | extern int umovestr(Process * proc, void * addr, int len, void * laddr); |
| 269 | extern int umovelong (Process * proc, void * addr, long * result, arg_type_info * info); |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 270 | extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 271 | extern int ffcheck(void * maddr); |
| 272 | extern void * sym2addr(Process *, struct library_symbol *); |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 273 | extern int linkmap_init(Process *, struct ltelf *); |
| 274 | extern void arch_check_dbg(Process *proc); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 275 | |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 276 | extern struct ltelf main_lte; |
| Michael K. Edwards | ef4428a | 2011-03-06 20:39:18 +0000 | [diff] [blame] | 277 | |
| 278 | #endif |