common.h revision f0bd98b3e6753d8609a3054a61f2df6f9cdac10a
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	int libdl_hooked;
180	/* Arch-dependent: */
181	void * debug;	/* arch-dep process debug struct */
182	long debug_state; /* arch-dep debug state */
183	void * instruction_pointer;
184	void * stack_pointer;      /* To get return addr, args... */
185	void * return_addr;
186	Breakpoint * breakpoint_being_enabled;
187	void * arch_ptr;
188	short e_machine;
189	short need_to_reinitialize_breakpoints;
190#ifdef __arm__
191	int thumb_mode;           /* ARM execution mode: 0: ARM, 1: Thumb */
192#endif
193
194	/* output: */
195	enum tof type_being_displayed;
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