blob: 0d56a2bd48c806cb5c46fe9c4b69e198dc737062 [file] [log] [blame]
Michael K. Edwardsef4428a2011-03-06 20:39:18 +00001#ifndef COMMON_H
2#define COMMON_H
3
Petr Machataa06eb812011-07-08 19:23:25 +02004#include <config.h>
Joe Damatoab3b72c2010-10-31 00:21:53 -07005
Juan Cespedes3df476b2009-05-28 19:17:17 +02006#include <sys/types.h>
7#include <sys/time.h>
8#include <stdio.h>
9
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020010#include "ltrace.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020011#include "defs.h"
12#include "dict.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020013#include "sysdep.h"
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020014#include "debug.h"
Marc Kleine-Budde747c73d2010-02-03 20:23:20 +010015#include "ltrace-elf.h"
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020016#include "read_config_file.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020017
18#if defined HAVE_LIBIBERTY || defined HAVE_LIBSUPC__
19# define USE_DEMANGLE
20#endif
21
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020022extern char * command;
Juan Cespedes3df476b2009-05-28 19:17:17 +020023
24extern int exiting; /* =1 if we have to exit ASAP */
25
Juan Cespedes3df476b2009-05-28 19:17:17 +020026enum 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;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020057 char ** keys;
58 int * values;
Juan Cespedes3df476b2009-05-28 19:17:17 +020059 } enum_info;
60
61 /* ARGTYPE_ARRAY */
62 struct {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020063 struct arg_type_info_t * elt_type;
Juan Cespedes3df476b2009-05-28 19:17:17 +020064 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 {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020075 struct arg_type_info_t ** fields; /* NULL-terminated */
76 size_t * offset;
Juan Cespedes3df476b2009-05-28 19:17:17 +020077 size_t size;
78 } struct_info;
79
80 /* ARGTYPE_POINTER */
81 struct {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020082 struct arg_type_info_t * info;
Juan Cespedes3df476b2009-05-28 19:17:17 +020083 } 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 {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200108 const char * name;
109 arg_type_info * return_info;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200110 int num_params;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200111 arg_type_info * arg_info[MAX_ARGS];
Juan Cespedes3df476b2009-05-28 19:17:17 +0200112 int params_right;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200113 Function * next;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200114};
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
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200122extern Function * list_of_functions;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200123extern char *PLTs_initialized_by_here;
124
125struct library_symbol {
Petr Machataa7db59c2012-02-10 13:14:43 +0100126 const char *name;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200127 void * enter_addr;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200128 char needs_init;
129 enum toplt plt_type;
130 char is_weak;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200131 struct library_symbol * next;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200132};
133
Juan Cespedes3df476b2009-05-28 19:17:17 +0200134struct opt_c_struct {
135 int count;
136 struct timeval tv;
137};
Juan Cespedes3df476b2009-05-28 19:17:17 +0200138
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200139#include "options.h"
140#include "output.h"
141#ifdef USE_DEMANGLE
142#include "demangle.h"
143#endif
Juan Cespedes3df476b2009-05-28 19:17:17 +0200144
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200145extern Dict * dict_opt_c;
Juan Cespedes3df476b2009-05-28 19:17:17 +0200146
Petr Machata617ff0b2011-10-06 14:23:24 +0200147enum process_status {
148 ps_invalid, /* Failure. */
149 ps_stop, /* Job-control stop. */
150 ps_tracing_stop,
Petr Machatacbe29c62011-09-27 02:27:58 +0200151 ps_sleeping,
Petr Machata617ff0b2011-10-06 14:23:24 +0200152 ps_zombie,
153 ps_other, /* Necessary other states can be added as needed. */
154};
155
Petr Machata69a03e62011-07-09 11:29:12 +0200156/* 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);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200168extern void handle_event(Event * event);
Petr Machata4007d742011-07-09 11:29:42 +0200169
Petr Machata1b17dbf2011-07-08 19:22:52 +0200170extern pid_t execute_program(const char * command, char ** argv);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200171extern int display_arg(enum tof type, Process * proc, int arg_num, arg_type_info * info);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200172extern void disable_all_breakpoints(Process * proc);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200173
Juan Cespedes3df476b2009-05-28 19:17:17 +0200174extern void show_summary(void);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200175extern arg_type_info * lookup_prototype(enum arg_type at);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200176
Petr Machata1974dbc2011-08-19 18:58:01 +0200177extern int do_init_elf(struct ltelf *lte, const char *filename);
Joe Damato7a2bdf82010-11-08 15:47:41 -0800178extern 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;
Petr Machataa7db59c2012-02-10 13:14:43 +0100181extern void library_symbol_init(struct library_symbol *libsym,
182 GElf_Addr addr, const char *name,
183 enum toplt type_of_plt, int is_weak);
Joe Damato7a2bdf82010-11-08 15:47:41 -0800184extern 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
Petr Machata534e00f2011-09-27 17:58:38 +0200188extern 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
Petr Machata9294d822012-02-07 12:35:58 +0100192struct breakpoint;
193
Juan Cespedes3df476b2009-05-28 19:17:17 +0200194/* Arch-dependent stuff: */
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200195extern char * pid2name(pid_t pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200196extern 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);
Petr Machata617ff0b2011-10-06 14:23:24 +0200199extern enum process_status process_status(pid_t pid);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200200extern void trace_set_options(Process * proc, pid_t pid);
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100201extern void wait_for_proc(pid_t pid);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200202extern void trace_me(void);
203extern int trace_pid(pid_t pid);
204extern void untrace_pid(pid_t pid);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200205extern 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);
Juan Cespedes2a61d192009-07-04 11:29:27 +0200210extern void set_return_addr(Process * proc, void * addr);
Petr Machata9294d822012-02-07 12:35:58 +0100211extern void enable_breakpoint(Process * proc, struct breakpoint *sbp);
212extern void disable_breakpoint(Process * proc, struct breakpoint *sbp);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200213extern int syscall_p(Process * proc, int status, int * sysnum);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200214extern void continue_process(pid_t pid);
215extern void continue_after_signal(pid_t pid, int signum);
Petr Machata43d2fe52011-11-02 13:25:49 +0100216extern void continue_after_syscall(Process *proc, int sysnum, int ret_p);
Petr Machata9294d822012-02-07 12:35:58 +0100217extern void continue_after_breakpoint(Process * proc, struct breakpoint *sbp);
Petr Machatacbe29c62011-09-27 02:27:58 +0200218extern void continue_after_vfork(Process * proc);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200219extern 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);
Joe Damatodfa3fa32010-11-08 15:47:35 -0800223extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200224extern int ffcheck(void * maddr);
225extern void * sym2addr(Process *, struct library_symbol *);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800226extern int linkmap_init(Process *, struct ltelf *);
227extern void arch_check_dbg(Process *proc);
Petr Machata9a5420c2011-07-09 11:21:23 +0200228extern int task_kill (pid_t pid, int sig);
229
Petr Machatacec06ec2012-04-10 13:31:55 +0200230/* 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
Petr Machataffe4cd22012-04-11 18:01:44 +0200234/* 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);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200250
Joe Damatof0bd98b2010-11-08 15:47:42 -0800251extern struct ltelf main_lte;
Michael K. Edwardsef4428a2011-03-06 20:39:18 +0000252
253#endif