common.h revision cc947516b7a76aa21095d8c3563a19a399bf628c
1#ifndef COMMON_H 2#define COMMON_H 3 4#if defined(HAVE_LIBUNWIND) 5#include <libunwind.h> 6#endif /* defined(HAVE_LIBUNWIND) */ 7 8#include <sys/types.h> 9#include <sys/time.h> 10#include <stdio.h> 11 12#include "ltrace.h" 13#include "defs.h" 14#include "dict.h" 15#include "sysdep.h" 16#include "debug.h" 17#include "ltrace-elf.h" 18#include "read_config_file.h" 19 20#if defined HAVE_LIBIBERTY || defined HAVE_LIBSUPC__ 21# define USE_DEMANGLE 22#endif 23 24extern char * command; 25 26extern int exiting; /* =1 if we have to exit ASAP */ 27 28typedef struct Breakpoint Breakpoint; 29struct Breakpoint { 30 void * addr; 31 unsigned char orig_value[BREAKPOINT_LENGTH]; 32 int enabled; 33 struct library_symbol * libsym; 34#ifdef __arm__ 35 int thumb_mode; 36#endif 37}; 38 39enum 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 64typedef struct arg_type_info_t { 65 enum arg_type type; 66 union { 67 /* ARGTYPE_ENUM */ 68 struct { 69 size_t entries; 70 char ** keys; 71 int * values; 72 } enum_info; 73 74 /* ARGTYPE_ARRAY */ 75 struct { 76 struct arg_type_info_t * elt_type; 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 { 88 struct arg_type_info_t ** fields; /* NULL-terminated */ 89 size_t * offset; 90 size_t size; 91 } struct_info; 92 93 /* ARGTYPE_POINTER */ 94 struct { 95 struct arg_type_info_t * info; 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 110enum 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 119typedef struct Function Function; 120struct Function { 121 const char * name; 122 arg_type_info * return_info; 123 int num_params; 124 arg_type_info * arg_info[MAX_ARGS]; 125 int params_right; 126 Function * next; 127}; 128 129enum 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 135extern Function * list_of_functions; 136extern char *PLTs_initialized_by_here; 137 138struct library_symbol { 139 char * name; 140 void * enter_addr; 141 char needs_init; 142 enum toplt plt_type; 143 char is_weak; 144 struct library_symbol * next; 145}; 146 147struct callstack_element { 148 union { 149 int syscall; 150 struct library_symbol * libfunc; 151 } c_un; 152 int is_syscall; 153 void * return_addr; 154 struct timeval time_spent; 155 void * arch_ptr; 156}; 157 158#define MAX_CALLDEPTH 64 159 160typedef enum Process_State Process_State; 161enum 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 167struct Process { 168 Process_State state; 169 Process * parent; /* needed by STATE_BEING_CREATED */ 170 char * filename; 171 pid_t pid; 172 Dict * breakpoints; 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]; 180 struct library_symbol * list_of_symbols; 181 182 int libdl_hooked; 183 /* Arch-dependent: */ 184 void * debug; /* arch-dep process debug struct */ 185 long debug_state; /* arch-dep debug state */ 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; 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 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 203 Process * next; 204}; 205 206struct opt_c_struct { 207 int count; 208 struct timeval tv; 209}; 210 211#include "options.h" 212#include "output.h" 213#ifdef USE_DEMANGLE 214#include "demangle.h" 215#endif 216 217extern Dict * dict_opt_c; 218 219extern Process * list_of_processes; 220 221extern Event * next_event(void); 222extern Process * pid2proc(pid_t pid); 223extern void handle_event(Event * event); 224extern void execute_program(Process *, char **); 225extern int display_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info); 226extern Breakpoint * address2bpstruct(Process * proc, void * addr); 227extern void breakpoints_init(Process * proc); 228extern void insert_breakpoint(Process * proc, void * addr, struct library_symbol * libsym); 229extern void delete_breakpoint(Process * proc, void * addr); 230extern void enable_all_breakpoints(Process * proc); 231extern void disable_all_breakpoints(Process * proc); 232extern void reinitialize_breakpoints(Process *); 233 234extern Process * open_program(char * filename, pid_t pid); 235extern void open_pid(pid_t pid); 236extern void show_summary(void); 237extern arg_type_info * lookup_prototype(enum arg_type at); 238 239extern void do_init_elf(struct ltelf *lte, const char *filename); 240extern void do_close_elf(struct ltelf *lte); 241extern int in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym); 242extern struct library_symbol *library_symbols; 243extern 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 247/* Arch-dependent stuff: */ 248extern char * pid2name(pid_t pid); 249extern void trace_set_options(Process * proc, pid_t pid); 250extern void trace_me(void); 251extern int trace_pid(pid_t pid); 252extern void untrace_pid(pid_t pid); 253extern void get_arch_dep(Process * proc); 254extern void * get_instruction_pointer(Process * proc); 255extern void set_instruction_pointer(Process * proc, void * addr); 256extern void * get_stack_pointer(Process * proc); 257extern void * get_return_addr(Process * proc, void * stack_pointer); 258extern void set_return_addr(Process * proc, void * addr); 259extern void enable_breakpoint(pid_t pid, Breakpoint * sbp); 260extern void disable_breakpoint(pid_t pid, const Breakpoint * sbp); 261extern int syscall_p(Process * proc, int status, int * sysnum); 262extern void continue_process(pid_t pid); 263extern void continue_after_signal(pid_t pid, int signum); 264extern void continue_after_breakpoint(Process * proc, Breakpoint * sbp); 265extern void continue_enabling_breakpoint(pid_t pid, Breakpoint * sbp); 266extern long gimme_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info); 267extern void save_register_args(enum tof type, Process * proc); 268extern int umovestr(Process * proc, void * addr, int len, void * laddr); 269extern int umovelong (Process * proc, void * addr, long * result, arg_type_info * info); 270extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count); 271extern int ffcheck(void * maddr); 272extern void * sym2addr(Process *, struct library_symbol *); 273extern int linkmap_init(Process *, struct ltelf *); 274extern void arch_check_dbg(Process *proc); 275 276extern struct ltelf main_lte; 277 278#endif 279