common.h revision 4007d74c2435ce4b50305d64ffe831627f989335
1#ifndef COMMON_H
2#define COMMON_H
3
4#include <config.h>
5#if defined(HAVE_LIBUNWIND)
6#include <libunwind.h>
7#endif /* defined(HAVE_LIBUNWIND) */
8
9#include <sys/types.h>
10#include <sys/time.h>
11#include <stdio.h>
12
13#include "ltrace.h"
14#include "defs.h"
15#include "dict.h"
16#include "sysdep.h"
17#include "debug.h"
18#include "ltrace-elf.h"
19#include "read_config_file.h"
20
21#if defined HAVE_LIBIBERTY || defined HAVE_LIBSUPC__
22# define USE_DEMANGLE
23#endif
24
25extern char * command;
26
27extern int exiting;  /* =1 if we have to exit ASAP */
28
29typedef struct Breakpoint Breakpoint;
30struct Breakpoint {
31	void * addr;
32	unsigned char orig_value[BREAKPOINT_LENGTH];
33	int enabled;
34	struct library_symbol * libsym;
35#ifdef __arm__
36	int thumb_mode;
37#endif
38};
39
40enum arg_type {
41	ARGTYPE_UNKNOWN = -1,
42	ARGTYPE_VOID,
43	ARGTYPE_INT,
44	ARGTYPE_UINT,
45	ARGTYPE_LONG,
46	ARGTYPE_ULONG,
47	ARGTYPE_OCTAL,
48	ARGTYPE_CHAR,
49	ARGTYPE_SHORT,
50	ARGTYPE_USHORT,
51	ARGTYPE_FLOAT,		/* float value, may require index */
52	ARGTYPE_DOUBLE,		/* double value, may require index */
53	ARGTYPE_ADDR,
54	ARGTYPE_FILE,
55	ARGTYPE_FORMAT,		/* printf-like format */
56	ARGTYPE_STRING,		/* NUL-terminated string */
57	ARGTYPE_STRING_N,	/* String of known maxlen */
58	ARGTYPE_ARRAY,		/* Series of values in memory */
59	ARGTYPE_ENUM,		/* Enumeration */
60	ARGTYPE_STRUCT,		/* Structure of values */
61	ARGTYPE_POINTER,	/* Pointer to some other type */
62	ARGTYPE_COUNT		/* number of ARGTYPE_* values */
63};
64
65typedef struct arg_type_info_t {
66	enum arg_type type;
67	union {
68		/* ARGTYPE_ENUM */
69		struct {
70			size_t entries;
71			char ** keys;
72			int * values;
73		} enum_info;
74
75		/* ARGTYPE_ARRAY */
76		struct {
77			struct arg_type_info_t * elt_type;
78			size_t elt_size;
79			int len_spec;
80		} array_info;
81
82		/* ARGTYPE_STRING_N */
83		struct {
84			int size_spec;
85		} string_n_info;
86
87		/* ARGTYPE_STRUCT */
88		struct {
89			struct arg_type_info_t ** fields;	/* NULL-terminated */
90			size_t * offset;
91			size_t size;
92		} struct_info;
93
94		/* ARGTYPE_POINTER */
95		struct {
96			struct arg_type_info_t * info;
97		} ptr_info;
98
99		/* ARGTYPE_FLOAT */
100		struct {
101			size_t float_index;
102		} float_info;
103
104		/* ARGTYPE_DOUBLE */
105		struct {
106			size_t float_index;
107		} double_info;
108	} u;
109} arg_type_info;
110
111enum tof {
112	LT_TOF_NONE = 0,
113	LT_TOF_FUNCTION,	/* A real library function */
114	LT_TOF_FUNCTIONR,	/* Return from a real library function */
115	LT_TOF_SYSCALL,		/* A syscall */
116	LT_TOF_SYSCALLR,	/* Return from a syscall */
117	LT_TOF_STRUCT		/* Not a function; read args from struct */
118};
119
120typedef struct Function Function;
121struct Function {
122	const char * name;
123	arg_type_info * return_info;
124	int num_params;
125	arg_type_info * arg_info[MAX_ARGS];
126	int params_right;
127	Function * next;
128};
129
130enum toplt {
131	LS_TOPLT_NONE = 0,	/* PLT not used for this symbol. */
132	LS_TOPLT_EXEC,		/* PLT for this symbol is executable. */
133	LS_TOPLT_POINT		/* PLT for this symbol is a non-executable. */
134};
135
136extern Function * list_of_functions;
137extern char *PLTs_initialized_by_here;
138
139struct library_symbol {
140	char * name;
141	void * enter_addr;
142	char needs_init;
143	enum toplt plt_type;
144	char is_weak;
145	struct library_symbol * next;
146};
147
148struct callstack_element {
149	union {
150		int syscall;
151		struct library_symbol * libfunc;
152	} c_un;
153	int is_syscall;
154	void * return_addr;
155	struct timeval time_spent;
156	void * arch_ptr;
157};
158
159#define MAX_CALLDEPTH 64
160
161typedef enum Process_State Process_State;
162enum Process_State {
163	STATE_ATTACHED = 0,
164	STATE_BEING_CREATED,
165	STATE_IGNORED  /* ignore this process (it's a fork and no -f was used) */
166};
167
168typedef struct Event_Handler Event_Handler;
169struct Event_Handler {
170	/* Event handler that overrides the default one.  Should
171	 * return NULL if the event was handled, otherwise the
172	 * returned event is passed to the default handler.  */
173	Event * (* on_event)(Event_Handler * self, Event * event);
174
175	/* Called when the event handler removal is requested.  */
176	void (* destroy)(Event_Handler * self);
177};
178
179/* XXX We would rather have this all organized a little differently,
180 * have Process for the whole group and Task for what's there for
181 * per-thread stuff.  But for now this is the less invasive way of
182 * structuring it.  */
183struct Process {
184	Process_State state;
185	Process * parent;         /* needed by STATE_BEING_CREATED */
186	char * filename;
187	pid_t pid;
188
189	/* Dictionary of breakpoints (which is a mapping
190	 * address->Breakpoint).  This is NULL for non-leader
191	 * processes.  */
192	Dict * breakpoints;
193
194	int breakpoints_enabled;  /* -1:not enabled yet, 0:disabled, 1:enabled */
195	int mask_32bit;           /* 1 if 64-bit ltrace is tracing 32-bit process */
196	unsigned int personality;
197	int tracesysgood;         /* signal indicating a PTRACE_SYSCALL trap */
198
199	int callstack_depth;
200	struct callstack_element callstack[MAX_CALLDEPTH];
201	struct library_symbol * list_of_symbols;
202
203	int libdl_hooked;
204	/* Arch-dependent: */
205	void * debug;	/* arch-dep process debug struct */
206	long debug_state; /* arch-dep debug state */
207	void * instruction_pointer;
208	void * stack_pointer;      /* To get return addr, args... */
209	void * return_addr;
210	Breakpoint * breakpoint_being_enabled;
211	void * arch_ptr;
212	short e_machine;
213	short need_to_reinitialize_breakpoints;
214#ifdef __arm__
215	int thumb_mode;           /* ARM execution mode: 0: ARM, 1: Thumb */
216#endif
217
218#if defined(HAVE_LIBUNWIND)
219	/* libunwind address space */
220	unw_addr_space_t unwind_as;
221	void *unwind_priv;
222#endif /* defined(HAVE_LIBUNWIND) */
223
224	/* Set in leader.  */
225	Event_Handler * event_handler;
226
227
228	/**
229	 * Process chaining.
230	 **/
231	Process * next;
232
233	/* LEADER points to the leader thread of the POSIX.1 process.
234	   If X->LEADER == X, then X is the leader thread and the
235	   Process structures chained by NEXT represent other threads,
236	   up until, but not including, the next leader thread.
237	   LEADER may be NULL after the leader has already exited.  In
238	   that case this process is waiting to be collected.  */
239	Process * leader;
240};
241
242struct opt_c_struct {
243	int count;
244	struct timeval tv;
245};
246
247#include "options.h"
248#include "output.h"
249#ifdef USE_DEMANGLE
250#include "demangle.h"
251#endif
252
253extern Dict * dict_opt_c;
254
255enum pcb_status {
256	pcb_stop, /* The iteration should stop.  */
257	pcb_cont, /* The iteration should continue.  */
258};
259
260/* Process list  */
261extern Process * pid2proc(pid_t pid);
262extern void add_process(Process * proc);
263extern void remove_process(Process * proc);
264extern Process *each_process(Process * start,
265			     enum pcb_status (* cb)(Process * proc, void * data),
266			     void * data);
267extern Process *each_task(Process * start,
268			  enum pcb_status (* cb)(Process * proc, void * data),
269			  void * data);
270
271/* Events  */
272enum ecb_status {
273	ecb_cont, /* The iteration should continue.  */
274	ecb_yield, /* The iteration should stop, yielding this
275		    * event.  */
276	ecb_deque, /* Like ecb_stop, but the event should be removed
277		    * from the queue.  */
278};
279extern Event * next_event(void);
280extern Event * each_qd_event(enum ecb_status (* cb)(Event * event, void * data),
281			     void * data);
282extern void enque_event(Event * event);
283extern void handle_event(Event * event);
284
285extern void install_event_handler(Process * proc, Event_Handler * handler);
286extern void destroy_event_handler(Process * proc);
287
288extern pid_t execute_program(const char * command, char ** argv);
289extern int display_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info);
290extern Breakpoint * address2bpstruct(Process * proc, void * addr);
291extern void breakpoints_init(Process * proc, int enable);
292extern void insert_breakpoint(Process * proc, void * addr,
293			      struct library_symbol * libsym, int enable);
294extern void delete_breakpoint(Process * proc, void * addr);
295extern void enable_all_breakpoints(Process * proc);
296extern void disable_all_breakpoints(Process * proc);
297extern void reinitialize_breakpoints(Process *);
298
299extern Process * open_program(char * filename, pid_t pid, int init_breakpoints);
300extern void open_pid(pid_t pid);
301extern void show_summary(void);
302extern arg_type_info * lookup_prototype(enum arg_type at);
303
304extern void do_init_elf(struct ltelf *lte, const char *filename);
305extern void do_close_elf(struct ltelf *lte);
306extern int in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym);
307extern struct library_symbol *library_symbols;
308extern void add_library_symbol(GElf_Addr addr, const char *name,
309		struct library_symbol **library_symbolspp,
310		enum toplt type_of_plt, int is_weak);
311
312/* Arch-dependent stuff: */
313extern char * pid2name(pid_t pid);
314extern pid_t process_leader(pid_t pid);
315extern int process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n);
316extern int process_stopped(pid_t pid);
317extern char process_status(pid_t pid);
318extern void trace_set_options(Process * proc, pid_t pid);
319extern void trace_me(void);
320extern int trace_pid(pid_t pid);
321extern void untrace_pid(pid_t pid);
322extern void get_arch_dep(Process * proc);
323extern void * get_instruction_pointer(Process * proc);
324extern void set_instruction_pointer(Process * proc, void * addr);
325extern void * get_stack_pointer(Process * proc);
326extern void * get_return_addr(Process * proc, void * stack_pointer);
327extern void set_return_addr(Process * proc, void * addr);
328extern void enable_breakpoint(Process * proc, Breakpoint * sbp);
329extern void disable_breakpoint(Process * proc, Breakpoint * sbp);
330extern int syscall_p(Process * proc, int status, int * sysnum);
331extern void continue_process(pid_t pid);
332extern void continue_after_signal(pid_t pid, int signum);
333extern void continue_after_breakpoint(Process * proc, Breakpoint * sbp);
334extern void continue_enabling_breakpoint(Process * proc, Breakpoint * sbp);
335extern long gimme_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info);
336extern void save_register_args(enum tof type, Process * proc);
337extern int umovestr(Process * proc, void * addr, int len, void * laddr);
338extern int umovelong (Process * proc, void * addr, long * result, arg_type_info * info);
339extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count);
340extern int ffcheck(void * maddr);
341extern void * sym2addr(Process *, struct library_symbol *);
342extern int linkmap_init(Process *, struct ltelf *);
343extern void arch_check_dbg(Process *proc);
344extern int task_kill (pid_t pid, int sig);
345
346
347extern struct ltelf main_lte;
348
349#endif
350