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