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