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