ltrace.h revision 1dec217e47f998c03c642561d98753c32683985c
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
115struct function {
116	const char *name;
117	arg_type_info *return_info;
118	int num_params;
119	arg_type_info *arg_info[MAX_ARGS];
120	int params_right;
121	struct function *next;
122};
123
124enum toplt {
125	LS_TOPLT_NONE = 0,	/* PLT not used for this symbol. */
126	LS_TOPLT_EXEC,		/* PLT for this symbol is executable. */
127	LS_TOPLT_POINT		/* PLT for this symbol is a non-executable. */
128};
129
130
131extern struct 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,
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
198struct event {
199	Process *proc;
200	enum {
201		EVENT_NONE,
202		EVENT_SIGNAL,
203		EVENT_EXIT,
204		EVENT_EXIT_SIGNAL,
205		EVENT_SYSCALL,
206		EVENT_SYSRET,
207		EVENT_ARCH_SYSCALL,
208		EVENT_ARCH_SYSRET,
209		EVENT_FORK,
210		EVENT_CLONE, /* Like FORK, but parent and child share memory */
211		EVENT_EXEC,
212		EVENT_BREAKPOINT
213	} thing;
214	union {
215		int ret_val;	/* EVENT_EXIT */
216		int signum;     /* EVENT_SIGNAL, EVENT_EXIT_SIGNAL */
217		int sysnum;     /* EVENT_SYSCALL, EVENT_SYSRET */
218		void *brk_addr;	/* EVENT_BREAKPOINT */
219		int newpid;     /* EVENT_FORK, EVENT_CLONE */
220	} e_un;
221};
222
223struct opt_c_struct {
224	int count;
225	struct timeval tv;
226};
227extern struct dict *dict_opt_c;
228
229extern Process *list_of_processes;
230
231extern void *instruction_pointer;
232
233extern struct event *next_event(void);
234extern Process * pid2proc(pid_t pid);
235extern void process_event(struct event *event);
236extern void execute_program(Process *, char **);
237extern int display_arg(enum tof type, Process *proc, int arg_num, arg_type_info *info);
238extern Breakpoint *address2bpstruct(Process *proc, void *addr);
239extern void breakpoints_init(Process *proc);
240extern void insert_breakpoint(Process *proc, void *addr, struct library_symbol *libsym);
241extern void delete_breakpoint(Process *proc, void *addr);
242extern void enable_all_breakpoints(Process *proc);
243extern void disable_all_breakpoints(Process *proc);
244extern void reinitialize_breakpoints(Process *);
245
246extern Process *open_program(char *filename, pid_t pid);
247extern void open_pid(pid_t pid, int verbose);
248extern void show_summary(void);
249extern arg_type_info *lookup_prototype(enum arg_type at);
250
251/* Arch-dependent stuff: */
252extern char *pid2name(pid_t pid);
253extern void trace_set_options(Process *proc, pid_t pid);
254extern void trace_me(void);
255extern int trace_pid(pid_t pid);
256extern void untrace_pid(pid_t pid);
257extern void get_arch_dep(Process *proc);
258extern void *get_instruction_pointer(Process *proc);
259extern void set_instruction_pointer(Process *proc, void *addr);
260extern void *get_stack_pointer(Process *proc);
261extern void *get_return_addr(Process *proc, void *stack_pointer);
262extern void enable_breakpoint(pid_t pid, Breakpoint *sbp);
263extern void disable_breakpoint(pid_t pid, const Breakpoint *sbp);
264extern int fork_p(Process *proc, int sysnum);
265extern int exec_p(Process *proc, int sysnum);
266extern int was_exec(Process *proc, int status);
267extern int syscall_p(Process *proc, int status, int *sysnum);
268extern void continue_process(pid_t pid);
269extern void continue_after_signal(pid_t pid, int signum);
270extern void continue_after_breakpoint(Process *proc, Breakpoint *sbp);
271extern void continue_enabling_breakpoint(pid_t pid, Breakpoint *sbp);
272extern long gimme_arg(enum tof type, Process *proc, int arg_num, arg_type_info *info);
273extern void save_register_args(enum tof type, Process *proc);
274extern int umovestr(Process *proc, void *addr, int len, void *laddr);
275extern int umovelong (Process *proc, void *addr, long *result, arg_type_info *info);
276extern int ffcheck(void *maddr);
277extern void *sym2addr(Process *, struct library_symbol *);
278
279#if 0				/* not yet */
280extern int umoven(Process *proc, void *addr, int len, void *laddr);
281#endif
282
283#endif
284