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