common.h revision f48031c60b05113194ad6dc2163ce3895a6b8d80
1#ifndef COMMON_H
2#define COMMON_H
3
4#include <config.h>
5
6#include <sys/types.h>
7#include <sys/time.h>
8#include <stdio.h>
9
10#include "ltrace.h"
11#include "defs.h"
12#include "dict.h"
13#include "sysdep.h"
14#include "debug.h"
15#include "ltrace-elf.h"
16#include "read_config_file.h"
17#include "proc.h"
18
19#if defined HAVE_LIBSUPC__ || defined HAVE_LIBSTDC__
20# define USE_CXA_DEMANGLE
21#endif
22#if defined HAVE_LIBIBERTY || defined USE_CXA_DEMANGLE
23# define USE_DEMANGLE
24#endif
25
26extern char * command;
27
28extern int exiting;  /* =1 if we have to exit ASAP */
29
30enum arg_type {
31	ARGTYPE_UNKNOWN = -1,
32	ARGTYPE_VOID,
33	ARGTYPE_INT,
34	ARGTYPE_UINT,
35	ARGTYPE_LONG,
36	ARGTYPE_ULONG,
37	ARGTYPE_OCTAL,
38	ARGTYPE_CHAR,
39	ARGTYPE_SHORT,
40	ARGTYPE_USHORT,
41	ARGTYPE_FLOAT,		/* float value, may require index */
42	ARGTYPE_DOUBLE,		/* double value, may require index */
43	ARGTYPE_ADDR,
44	ARGTYPE_FILE,
45	ARGTYPE_FORMAT,		/* printf-like format */
46	ARGTYPE_STRING,		/* NUL-terminated string */
47	ARGTYPE_STRING_N,	/* String of known maxlen */
48	ARGTYPE_ARRAY,		/* Series of values in memory */
49	ARGTYPE_ENUM,		/* Enumeration */
50	ARGTYPE_STRUCT,		/* Structure of values */
51	ARGTYPE_POINTER,	/* Pointer to some other type */
52	ARGTYPE_COUNT		/* number of ARGTYPE_* values */
53};
54
55typedef struct arg_type_info_t {
56	enum arg_type type;
57	union {
58		/* ARGTYPE_ENUM */
59		struct {
60			size_t entries;
61			char ** keys;
62			int * values;
63		} enum_info;
64
65		/* ARGTYPE_ARRAY */
66		struct {
67			struct arg_type_info_t * elt_type;
68			size_t elt_size;
69			int len_spec;
70		} array_info;
71
72		/* ARGTYPE_STRING_N */
73		struct {
74			int size_spec;
75		} string_n_info;
76
77		/* ARGTYPE_STRUCT */
78		struct {
79			struct arg_type_info_t ** fields;	/* NULL-terminated */
80			size_t * offset;
81			size_t size;
82		} struct_info;
83
84		/* ARGTYPE_POINTER */
85		struct {
86			struct arg_type_info_t * info;
87		} ptr_info;
88
89		/* ARGTYPE_FLOAT */
90		struct {
91			size_t float_index;
92		} float_info;
93
94		/* ARGTYPE_DOUBLE */
95		struct {
96			size_t float_index;
97		} double_info;
98	} u;
99} arg_type_info;
100
101enum tof {
102	LT_TOF_NONE = 0,
103	LT_TOF_FUNCTION,	/* A real library function */
104	LT_TOF_FUNCTIONR,	/* Return from a real library function */
105	LT_TOF_SYSCALL,		/* A syscall */
106	LT_TOF_SYSCALLR,	/* Return from a syscall */
107	LT_TOF_STRUCT		/* Not a function; read args from struct */
108};
109
110typedef struct Function Function;
111struct Function {
112	const char * name;
113	arg_type_info * return_info;
114	int num_params;
115	arg_type_info * arg_info[MAX_ARGS];
116	int params_right;
117	Function * next;
118};
119
120extern Function * list_of_functions;
121extern char *PLTs_initialized_by_here;
122
123struct opt_c_struct {
124	int count;
125	struct timeval tv;
126};
127
128#include "options.h"
129#include "output.h"
130#ifdef USE_DEMANGLE
131#include "demangle.h"
132#endif
133
134extern Dict * dict_opt_c;
135
136enum process_status {
137	ps_invalid,	/* Failure.  */
138	ps_stop,	/* Job-control stop.  */
139	ps_tracing_stop,
140	ps_sleeping,
141	ps_zombie,
142	ps_other,	/* Necessary other states can be added as needed.  */
143};
144
145/* Events  */
146enum ecb_status {
147	ecb_cont, /* The iteration should continue.  */
148	ecb_yield, /* The iteration should stop, yielding this
149		    * event.  */
150	ecb_deque, /* Like ecb_stop, but the event should be removed
151		    * from the queue.  */
152};
153extern Event * next_event(void);
154extern Event * each_qd_event(enum ecb_status (* cb)(Event * event, void * data),
155			     void * data);
156extern void enque_event(Event * event);
157extern void handle_event(Event * event);
158
159extern pid_t execute_program(const char * command, char ** argv);
160extern int display_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info);
161extern void disable_all_breakpoints(Process * proc);
162
163extern void show_summary(void);
164extern arg_type_info * lookup_prototype(enum arg_type at);
165
166struct breakpoint;
167struct library_symbol;
168
169struct breakpoint;
170
171/* Arch-dependent stuff: */
172extern char * pid2name(pid_t pid);
173extern pid_t process_leader(pid_t pid);
174extern int process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n);
175extern int process_stopped(pid_t pid);
176extern enum process_status process_status(pid_t pid);
177extern void trace_set_options(struct Process *proc);
178extern int wait_for_proc(pid_t pid);
179extern void trace_me(void);
180extern int trace_pid(pid_t pid);
181extern void untrace_pid(pid_t pid);
182extern void get_arch_dep(Process * proc);
183extern void * get_instruction_pointer(Process * proc);
184extern void set_instruction_pointer(Process * proc, void * addr);
185extern void * get_stack_pointer(Process * proc);
186extern void * get_return_addr(Process * proc, void * stack_pointer);
187extern void set_return_addr(Process * proc, void * addr);
188extern void enable_breakpoint(struct Process *proc, struct breakpoint *sbp);
189extern void disable_breakpoint(struct Process *proc, struct breakpoint *sbp);
190extern int syscall_p(Process * proc, int status, int * sysnum);
191extern void continue_process(pid_t pid);
192extern void continue_after_signal(pid_t pid, int signum);
193extern void continue_after_syscall(Process *proc, int sysnum, int ret_p);
194extern void continue_after_breakpoint(struct Process *proc, struct breakpoint *sbp);
195extern void continue_after_vfork(Process * proc);
196extern long gimme_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info);
197extern void save_register_args(enum tof type, Process * proc);
198extern int umovestr(Process * proc, void * addr, int len, void * laddr);
199extern int umovelong (Process * proc, void * addr, long * result, arg_type_info * info);
200extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count);
201extern int ffcheck(void * maddr);
202extern void * sym2addr(Process *, struct library_symbol *);
203extern int linkmap_init(struct Process *proc, void *dyn_addr);
204extern void arch_check_dbg(Process *proc);
205extern int task_kill (pid_t pid, int sig);
206
207/* Called when trace_me or primary trace_pid fail.  This may plug in
208 * any platform-specific knowledge of why it could be so.  */
209void trace_fail_warning(pid_t pid);
210
211/* A pair of functions called to initiate a detachment request when
212 * ltrace is about to exit.  Their job is to undo any effects that
213 * tracing had and eventually detach process, perhaps by way of
214 * installing a process handler.
215 *
216 * OS_LTRACE_EXITING_SIGHANDLER is called from a signal handler
217 * context right after the signal was captured.  It returns 1 if the
218 * request was handled or 0 if it wasn't.
219 *
220 * If the call to OS_LTRACE_EXITING_SIGHANDLER didn't handle the
221 * request, OS_LTRACE_EXITING is called when the next event is
222 * generated.  Therefore it's called in "safe" context, without
223 * re-entrancy concerns, but it's only called after an event is
224 * generated.  */
225int os_ltrace_exiting_sighandler(void);
226void os_ltrace_exiting(void);
227
228int arch_elf_init(struct ltelf *lte, struct library *lib);
229void arch_elf_destroy(struct ltelf *lte);
230
231enum plt_status {
232	plt_fail,
233	plt_ok,
234	plt_default,
235};
236
237enum plt_status arch_elf_add_plt_entry(struct Process *p, struct ltelf *l,
238				       const char *n, GElf_Rela *r, size_t i,
239				       struct library_symbol **ret);
240
241int arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp);
242void arch_breakpoint_destroy(struct breakpoint *sbp);
243int arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp);
244
245void arch_library_init(struct library *lib);
246void arch_library_destroy(struct library *lib);
247void arch_library_clone(struct library *retp, struct library *lib);
248
249int arch_library_symbol_init(struct library_symbol *libsym);
250void arch_library_symbol_destroy(struct library_symbol *libsym);
251int arch_library_symbol_clone(struct library_symbol *retp,
252			      struct library_symbol *libsym);
253
254int arch_process_init(struct Process *proc);
255void arch_process_destroy(struct Process *proc);
256int arch_process_clone(struct Process *retp, struct Process *proc);
257int arch_process_exec(struct Process *proc);
258
259/* This is called after the dynamic linker is done with the
260 * process startup.  */
261void arch_dynlink_done(struct Process *proc);
262
263#endif
264