blob: c6c7317aa536866ceeae97f774f245e1096cdb4a [file] [log] [blame]
Michael K. Edwardsef4428a2011-03-06 20:39:18 +00001#ifndef COMMON_H
2#define COMMON_H
3
Petr Machataa06eb812011-07-08 19:23:25 +02004#include <config.h>
Joe Damatoab3b72c2010-10-31 00:21:53 -07005#if defined(HAVE_LIBUNWIND)
6#include <libunwind.h>
7#endif /* defined(HAVE_LIBUNWIND) */
8
Juan Cespedes3df476b2009-05-28 19:17:17 +02009#include <sys/types.h>
10#include <sys/time.h>
11#include <stdio.h>
12
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020013#include "ltrace.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020014#include "defs.h"
15#include "dict.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020016#include "sysdep.h"
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020017#include "debug.h"
Marc Kleine-Budde747c73d2010-02-03 20:23:20 +010018#include "ltrace-elf.h"
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020019#include "read_config_file.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020020
21#if defined HAVE_LIBIBERTY || defined HAVE_LIBSUPC__
22# define USE_DEMANGLE
23#endif
24
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020025extern char * command;
Juan Cespedes3df476b2009-05-28 19:17:17 +020026
27extern int exiting; /* =1 if we have to exit ASAP */
28
Juan Cespedes3df476b2009-05-28 19:17:17 +020029enum arg_type {
30 ARGTYPE_UNKNOWN = -1,
31 ARGTYPE_VOID,
32 ARGTYPE_INT,
33 ARGTYPE_UINT,
34 ARGTYPE_LONG,
35 ARGTYPE_ULONG,
36 ARGTYPE_OCTAL,
37 ARGTYPE_CHAR,
38 ARGTYPE_SHORT,
39 ARGTYPE_USHORT,
40 ARGTYPE_FLOAT, /* float value, may require index */
41 ARGTYPE_DOUBLE, /* double value, may require index */
42 ARGTYPE_ADDR,
43 ARGTYPE_FILE,
44 ARGTYPE_FORMAT, /* printf-like format */
45 ARGTYPE_STRING, /* NUL-terminated string */
46 ARGTYPE_STRING_N, /* String of known maxlen */
47 ARGTYPE_ARRAY, /* Series of values in memory */
48 ARGTYPE_ENUM, /* Enumeration */
49 ARGTYPE_STRUCT, /* Structure of values */
50 ARGTYPE_POINTER, /* Pointer to some other type */
51 ARGTYPE_COUNT /* number of ARGTYPE_* values */
52};
53
54typedef struct arg_type_info_t {
55 enum arg_type type;
56 union {
57 /* ARGTYPE_ENUM */
58 struct {
59 size_t entries;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020060 char ** keys;
61 int * values;
Juan Cespedes3df476b2009-05-28 19:17:17 +020062 } enum_info;
63
64 /* ARGTYPE_ARRAY */
65 struct {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020066 struct arg_type_info_t * elt_type;
Juan Cespedes3df476b2009-05-28 19:17:17 +020067 size_t elt_size;
68 int len_spec;
69 } array_info;
70
71 /* ARGTYPE_STRING_N */
72 struct {
73 int size_spec;
74 } string_n_info;
75
76 /* ARGTYPE_STRUCT */
77 struct {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020078 struct arg_type_info_t ** fields; /* NULL-terminated */
79 size_t * offset;
Juan Cespedes3df476b2009-05-28 19:17:17 +020080 size_t size;
81 } struct_info;
82
83 /* ARGTYPE_POINTER */
84 struct {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020085 struct arg_type_info_t * info;
Juan Cespedes3df476b2009-05-28 19:17:17 +020086 } ptr_info;
87
88 /* ARGTYPE_FLOAT */
89 struct {
90 size_t float_index;
91 } float_info;
92
93 /* ARGTYPE_DOUBLE */
94 struct {
95 size_t float_index;
96 } double_info;
97 } u;
98} arg_type_info;
99
100enum tof {
101 LT_TOF_NONE = 0,
102 LT_TOF_FUNCTION, /* A real library function */
103 LT_TOF_FUNCTIONR, /* Return from a real library function */
104 LT_TOF_SYSCALL, /* A syscall */
105 LT_TOF_SYSCALLR, /* Return from a syscall */
106 LT_TOF_STRUCT /* Not a function; read args from struct */
107};
108
109typedef struct Function Function;
110struct Function {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200111 const char * name;
112 arg_type_info * return_info;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200113 int num_params;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200114 arg_type_info * arg_info[MAX_ARGS];
Juan Cespedes3df476b2009-05-28 19:17:17 +0200115 int params_right;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200116 Function * next;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200117};
118
119enum toplt {
120 LS_TOPLT_NONE = 0, /* PLT not used for this symbol. */
121 LS_TOPLT_EXEC, /* PLT for this symbol is executable. */
122 LS_TOPLT_POINT /* PLT for this symbol is a non-executable. */
123};
124
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200125extern Function * list_of_functions;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200126extern char *PLTs_initialized_by_here;
127
128struct library_symbol {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200129 char * name;
130 void * enter_addr;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200131 char needs_init;
132 enum toplt plt_type;
133 char is_weak;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200134 struct library_symbol * next;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200135};
136
137struct callstack_element {
138 union {
139 int syscall;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200140 struct library_symbol * libfunc;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200141 } c_un;
142 int is_syscall;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200143 void * return_addr;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200144 struct timeval time_spent;
Petr Machata211f0882010-11-03 18:42:18 +0100145 void * arch_ptr;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200146};
147
148#define MAX_CALLDEPTH 64
149
150typedef enum Process_State Process_State;
151enum Process_State {
152 STATE_ATTACHED = 0,
153 STATE_BEING_CREATED,
154 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
155};
156
Petr Machata4007d742011-07-09 11:29:42 +0200157typedef struct Event_Handler Event_Handler;
158struct Event_Handler {
159 /* Event handler that overrides the default one. Should
160 * return NULL if the event was handled, otherwise the
161 * returned event is passed to the default handler. */
162 Event * (* on_event)(Event_Handler * self, Event * event);
163
164 /* Called when the event handler removal is requested. */
165 void (* destroy)(Event_Handler * self);
166};
167
Petr Machata9a5420c2011-07-09 11:21:23 +0200168/* XXX We would rather have this all organized a little differently,
169 * have Process for the whole group and Task for what's there for
170 * per-thread stuff. But for now this is the less invasive way of
171 * structuring it. */
Juan Cespedes3df476b2009-05-28 19:17:17 +0200172struct Process {
173 Process_State state;
Juan Cespedes2a61d192009-07-04 11:29:27 +0200174 Process * parent; /* needed by STATE_BEING_CREATED */
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200175 char * filename;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200176 pid_t pid;
Petr Machata9a5420c2011-07-09 11:21:23 +0200177
178 /* Dictionary of breakpoints (which is a mapping
Petr Machata9294d822012-02-07 12:35:58 +0100179 * address->breakpoint). This is NULL for non-leader
Petr Machata9a5420c2011-07-09 11:21:23 +0200180 * processes. */
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200181 Dict * breakpoints;
Petr Machata9a5420c2011-07-09 11:21:23 +0200182
Juan Cespedes3df476b2009-05-28 19:17:17 +0200183 int breakpoints_enabled; /* -1:not enabled yet, 0:disabled, 1:enabled */
184 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
185 unsigned int personality;
186 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
187
188 int callstack_depth;
189 struct callstack_element callstack[MAX_CALLDEPTH];
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200190 struct library_symbol * list_of_symbols;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200191
Joe Damatof0bd98b2010-11-08 15:47:42 -0800192 int libdl_hooked;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200193 /* Arch-dependent: */
Joe Damatof0bd98b2010-11-08 15:47:42 -0800194 void * debug; /* arch-dep process debug struct */
195 long debug_state; /* arch-dep debug state */
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200196 void * instruction_pointer;
197 void * stack_pointer; /* To get return addr, args... */
198 void * return_addr;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200199 void * arch_ptr;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200200 short e_machine;
201 short need_to_reinitialize_breakpoints;
202#ifdef __arm__
203 int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */
204#endif
205
Joe Damatoab3b72c2010-10-31 00:21:53 -0700206#if defined(HAVE_LIBUNWIND)
207 /* libunwind address space */
208 unw_addr_space_t unwind_as;
209 void *unwind_priv;
210#endif /* defined(HAVE_LIBUNWIND) */
211
Petr Machata4007d742011-07-09 11:29:42 +0200212 /* Set in leader. */
213 Event_Handler * event_handler;
214
215
Petr Machata9a5420c2011-07-09 11:21:23 +0200216 /**
217 * Process chaining.
218 **/
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200219 Process * next;
Petr Machata9a5420c2011-07-09 11:21:23 +0200220
221 /* LEADER points to the leader thread of the POSIX.1 process.
222 If X->LEADER == X, then X is the leader thread and the
223 Process structures chained by NEXT represent other threads,
224 up until, but not including, the next leader thread.
225 LEADER may be NULL after the leader has already exited. In
226 that case this process is waiting to be collected. */
227 Process * leader;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200228};
229
Juan Cespedes3df476b2009-05-28 19:17:17 +0200230struct opt_c_struct {
231 int count;
232 struct timeval tv;
233};
Juan Cespedes3df476b2009-05-28 19:17:17 +0200234
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200235#include "options.h"
236#include "output.h"
237#ifdef USE_DEMANGLE
238#include "demangle.h"
239#endif
Juan Cespedes3df476b2009-05-28 19:17:17 +0200240
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200241extern Dict * dict_opt_c;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200242
Petr Machata617ff0b2011-10-06 14:23:24 +0200243enum process_status {
244 ps_invalid, /* Failure. */
245 ps_stop, /* Job-control stop. */
246 ps_tracing_stop,
Petr Machatacbe29c62011-09-27 02:27:58 +0200247 ps_sleeping,
Petr Machata617ff0b2011-10-06 14:23:24 +0200248 ps_zombie,
249 ps_other, /* Necessary other states can be added as needed. */
250};
251
Petr Machatacebb8842011-07-09 11:14:11 +0200252enum pcb_status {
253 pcb_stop, /* The iteration should stop. */
254 pcb_cont, /* The iteration should continue. */
255};
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200256
Petr Machata4007d742011-07-09 11:29:42 +0200257/* Process list */
Juan Cespedes3df476b2009-05-28 19:17:17 +0200258extern Process * pid2proc(pid_t pid);
Petr Machatacebb8842011-07-09 11:14:11 +0200259extern void add_process(Process * proc);
260extern void remove_process(Process * proc);
Petr Machatacbe29c62011-09-27 02:27:58 +0200261extern void change_process_leader(Process * proc, Process * leader);
Petr Machatacebb8842011-07-09 11:14:11 +0200262extern Process *each_process(Process * start,
263 enum pcb_status (* cb)(Process * proc, void * data),
264 void * data);
Petr Machata9a5420c2011-07-09 11:21:23 +0200265extern Process *each_task(Process * start,
266 enum pcb_status (* cb)(Process * proc, void * data),
267 void * data);
268
Petr Machata69a03e62011-07-09 11:29:12 +0200269/* Events */
270enum ecb_status {
271 ecb_cont, /* The iteration should continue. */
272 ecb_yield, /* The iteration should stop, yielding this
273 * event. */
274 ecb_deque, /* Like ecb_stop, but the event should be removed
275 * from the queue. */
276};
277extern Event * next_event(void);
278extern Event * each_qd_event(enum ecb_status (* cb)(Event * event, void * data),
279 void * data);
280extern void enque_event(Event * event);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200281extern void handle_event(Event * event);
Petr Machata4007d742011-07-09 11:29:42 +0200282
283extern void install_event_handler(Process * proc, Event_Handler * handler);
284extern void destroy_event_handler(Process * proc);
285
Petr Machata1b17dbf2011-07-08 19:22:52 +0200286extern pid_t execute_program(const char * command, char ** argv);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200287extern int display_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200288extern void disable_all_breakpoints(Process * proc);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200289
Petr Machatac7585b62011-07-08 22:58:12 +0200290extern Process * open_program(char * filename, pid_t pid, int init_breakpoints);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200291extern void open_pid(pid_t pid);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200292extern void show_summary(void);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200293extern arg_type_info * lookup_prototype(enum arg_type at);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200294
Petr Machata1974dbc2011-08-19 18:58:01 +0200295extern int do_init_elf(struct ltelf *lte, const char *filename);
Joe Damato7a2bdf82010-11-08 15:47:41 -0800296extern void do_close_elf(struct ltelf *lte);
297extern int in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym);
298extern struct library_symbol *library_symbols;
299extern void add_library_symbol(GElf_Addr addr, const char *name,
300 struct library_symbol **library_symbolspp,
301 enum toplt type_of_plt, int is_weak);
302
Petr Machata534e00f2011-09-27 17:58:38 +0200303extern struct library_symbol * clone_library_symbol(struct library_symbol * s);
304extern void destroy_library_symbol(struct library_symbol * s);
305extern void destroy_library_symbol_chain(struct library_symbol * chain);
306
Petr Machata9294d822012-02-07 12:35:58 +0100307struct breakpoint;
308
Juan Cespedes3df476b2009-05-28 19:17:17 +0200309/* Arch-dependent stuff: */
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200310extern char * pid2name(pid_t pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200311extern pid_t process_leader(pid_t pid);
312extern int process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n);
313extern int process_stopped(pid_t pid);
Petr Machata617ff0b2011-10-06 14:23:24 +0200314extern enum process_status process_status(pid_t pid);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200315extern void trace_set_options(Process * proc, pid_t pid);
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100316extern void wait_for_proc(pid_t pid);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200317extern void trace_me(void);
318extern int trace_pid(pid_t pid);
319extern void untrace_pid(pid_t pid);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200320extern void get_arch_dep(Process * proc);
321extern void * get_instruction_pointer(Process * proc);
322extern void set_instruction_pointer(Process * proc, void * addr);
323extern void * get_stack_pointer(Process * proc);
324extern void * get_return_addr(Process * proc, void * stack_pointer);
Juan Cespedes2a61d192009-07-04 11:29:27 +0200325extern void set_return_addr(Process * proc, void * addr);
Petr Machata9294d822012-02-07 12:35:58 +0100326extern void enable_breakpoint(Process * proc, struct breakpoint *sbp);
327extern void disable_breakpoint(Process * proc, struct breakpoint *sbp);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200328extern int syscall_p(Process * proc, int status, int * sysnum);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200329extern void continue_process(pid_t pid);
330extern void continue_after_signal(pid_t pid, int signum);
Petr Machata43d2fe52011-11-02 13:25:49 +0100331extern void continue_after_syscall(Process *proc, int sysnum, int ret_p);
Petr Machata9294d822012-02-07 12:35:58 +0100332extern void continue_after_breakpoint(Process * proc, struct breakpoint *sbp);
Petr Machatacbe29c62011-09-27 02:27:58 +0200333extern void continue_after_vfork(Process * proc);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200334extern long gimme_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info);
335extern void save_register_args(enum tof type, Process * proc);
336extern int umovestr(Process * proc, void * addr, int len, void * laddr);
337extern int umovelong (Process * proc, void * addr, long * result, arg_type_info * info);
Joe Damatodfa3fa32010-11-08 15:47:35 -0800338extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200339extern int ffcheck(void * maddr);
340extern void * sym2addr(Process *, struct library_symbol *);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800341extern int linkmap_init(Process *, struct ltelf *);
342extern void arch_check_dbg(Process *proc);
Petr Machata9a5420c2011-07-09 11:21:23 +0200343extern int task_kill (pid_t pid, int sig);
344
Petr Machatacec06ec2012-04-10 13:31:55 +0200345/* Called when trace_me or primary trace_pid fail. This may plug in
346 * any platform-specific knowledge of why it could be so. */
347void trace_fail_warning(pid_t pid);
348
Petr Machataffe4cd22012-04-11 18:01:44 +0200349/* A pair of functions called to initiate a detachment request when
350 * ltrace is about to exit. Their job is to undo any effects that
351 * tracing had and eventually detach process, perhaps by way of
352 * installing a process handler.
353 *
354 * OS_LTRACE_EXITING_SIGHANDLER is called from a signal handler
355 * context right after the signal was captured. It returns 1 if the
356 * request was handled or 0 if it wasn't.
357 *
358 * If the call to OS_LTRACE_EXITING_SIGHANDLER didn't handle the
359 * request, OS_LTRACE_EXITING is called when the next event is
360 * generated. Therefore it's called in "safe" context, without
361 * re-entrancy concerns, but it's only called after an even is
362 * generated. */
363int os_ltrace_exiting_sighandler(void);
364void os_ltrace_exiting(void);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200365
Joe Damatof0bd98b2010-11-08 15:47:42 -0800366extern struct ltelf main_lte;
Michael K. Edwardsef4428a2011-03-06 20:39:18 +0000367
368#endif