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