check return value of unw_get_reg and unw_get_proc_name
[platform/upstream/ltrace.git] / proc.h
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2010,2011,2012,2013 Petr Machata, Red Hat Inc.
4  * Copyright (C) 2010 Joe Damato
5  * Copyright (C) 1998,2001,2008,2009 Juan Cespedes
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #ifndef _PROC_H_
24 #define _PROC_H_
25
26 #include "config.h"
27
28 #include <sys/time.h>
29 #include <stdint.h>
30
31 #if defined(HAVE_LIBUNWIND)
32 # include <libunwind.h>
33 # include <libunwind-ptrace.h>
34 #endif /* defined(HAVE_LIBUNWIND) */
35
36 #include "ltrace.h"
37 #include "dict.h"
38 #include "sysdep.h"
39 #include "callback.h"
40 #include "forward.h"
41
42 struct event_handler {
43         /* Event handler that overrides the default one.  Should
44          * return NULL if the event was handled, otherwise the
45          * returned event is passed to the default handler.  */
46         Event *(*on_event)(struct event_handler *self, Event *event);
47
48         /* Called when the event handler removal is requested.  */
49         void (*destroy)(struct event_handler *self);
50 };
51
52 enum process_state {
53         STATE_ATTACHED = 0,
54         STATE_BEING_CREATED,
55         STATE_IGNORED  /* ignore this process (it's a fork and no -f was used) */
56 };
57
58 struct output_state {
59         size_t params_left;
60         int need_delim;
61 };
62
63 struct callstack_element {
64         union {
65                 int syscall;
66                 struct library_symbol * libfunc;
67         } c_un;
68         int is_syscall;
69         arch_addr_t return_addr;
70         struct timeval enter_time;
71         struct fetch_context *fetch_context;
72         struct value_dict *arguments;
73         struct output_state out;
74 };
75
76 /* XXX We should get rid of this.  */
77 #define MAX_CALLDEPTH 64
78
79 /* XXX We would rather have this all organized a little differently,
80  * have struct process for the whole group and struct task (or struct
81  * lwp, struct thread) for what's there for per-thread stuff.  But for
82  * now this is the less invasive way of structuring it.  */
83 struct process {
84         enum process_state state;
85         struct process *parent;         /* needed by STATE_BEING_CREATED */
86         char * filename;
87         pid_t pid;
88
89         /* Dictionary of breakpoints (which is a mapping
90          * address->breakpoint).  This is NULL for non-leader
91          * processes.  */
92         struct dict *breakpoints;
93
94         int mask_32bit;           /* 1 if 64-bit ltrace is tracing 32-bit process */
95         unsigned int personality;
96         int tracesysgood;         /* signal indicating a PTRACE_SYSCALL trap */
97
98         size_t callstack_depth;
99         struct callstack_element callstack[MAX_CALLDEPTH];
100
101         /* Linked list of libraries in backwards order of mapping.
102          * The last element is the executed binary itself.  */
103         struct library *libraries;
104
105         /* Arch-dependent: */
106         void * instruction_pointer;
107         void * stack_pointer;      /* To get return addr, args... */
108         void * arch_ptr;
109
110         /* XXX We would like to replace this with a pointer to ABI
111          * object that would provide the relevant services, instead of
112          * checking the necessary flags in the back end ad
113          * nauseam.  */
114         short e_machine;
115         char e_class;
116
117 #if defined(HAVE_LIBUNWIND)
118         /* libunwind address space */
119         unw_addr_space_t unwind_as;
120         void *unwind_priv;
121 #endif /* defined(HAVE_LIBUNWIND) */
122
123         /* Set in leader.  */
124         struct event_handler *event_handler;
125
126         /**
127          * Process chaining.
128          **/
129         struct process *next;
130
131         /* LEADER points to the leader thread of the POSIX.1 process.
132            If X->LEADER == X, then X is the leader thread and the
133            process structures chained by NEXT represent other threads,
134            up until, but not including, the next leader thread.
135            LEADER may be NULL after the leader has already exited.  In
136            that case this process is waiting to be collected.  */
137         struct process *leader;
138
139         struct os_process_data os;
140         struct arch_process_data arch;
141 };
142
143 /* Initialize a process given a path to binary FILENAME, with a PID,
144  * and add the process to an internal chain of traced processes.  */
145 int process_init(struct process *proc, const char *filename, pid_t pid);
146
147 /* PROC underwent an exec.  This is a bit like process_destroy
148  * followed by process_init, except that some state is kept and the
149  * process doesn't lose it's place in the list of processes.  */
150 int process_exec(struct process *proc);
151
152 /* Release any memory allocated for PROC (but not PROC itself).  Does
153  * NOT remove PROC from internal chain.
154  *
155  * XXX clearly this init/destroy pair is different than others and
156  * should be fixed.  process_init should presumably be separate from
157  * process_add.  */
158 void process_destroy(struct process *proc);
159
160 struct process *open_program(const char *filename, pid_t pid);
161 void open_pid(pid_t pid);
162 struct process *pid2proc(pid_t pid);
163
164 /* Clone the contents of PROC into the memory referenced by RETP.
165  * Returns 0 on success or a negative value on failure.  */
166 int process_clone(struct process *retp, struct process *proc, pid_t pid);
167
168 /* Iterate through the processes that ltrace currently traces.  Tasks
169  * are considered to be processes for the purpose of this iterator.
170  * See callback.h for notes on iteration interfaces.  */
171 struct process *each_process(struct process *start_after,
172                              enum callback_status (*cb)(struct process *proc,
173                                                         void *data),
174                              void *data);
175
176 /* Iterate through list of tasks of given process PROC.  See
177  * callback.h for notes on iteration interfaces.  */
178 struct process *each_task(struct process *proc, struct process *start_after,
179                           enum callback_status (*cb)(struct process *proc,
180                                                      void *data),
181                           void *data);
182
183 void change_process_leader(struct process *proc, struct process *leader);
184
185 /* Prepare those parts of process initialization that need to be done
186  * after _start is hit (i.e. after dynamic linking was done).  */
187 void process_hit_start(struct process *proc);
188
189 /* Remove process from the list of traced processes, drop any events
190  * in the event queue, destroy it and free memory.  */
191 void remove_process(struct process *proc);
192
193 void install_event_handler(struct process *proc, struct event_handler *handler);
194 void destroy_event_handler(struct process *proc);
195
196 /* Add a library LIB to the list of PROC's libraries.  */
197 void proc_add_library(struct process *proc, struct library *lib);
198
199 /* Remove LIB from list of PROC's libraries.  Returns 0 if the library
200  * was found and unlinked, otherwise returns a negative value.  */
201 int proc_remove_library(struct process *proc, struct library *lib);
202
203 /* Clear a delayed flag.  If a symbol is neither latent, nor delayed,
204  * a breakpoint is inserted for it.  Returns 0 if the activation was
205  * successful or a negative value if it failed.  Note that if a symbol
206  * is both latent and delayed, this will not enable the corresponding
207  * breakpoint.  */
208 int proc_activate_delayed_symbol(struct process *proc,
209                                  struct library_symbol *libsym);
210
211 /* Iterate through the libraries of PROC.  See callback.h for notes on
212  * iteration interfaces.  */
213 struct library *proc_each_library(struct process *proc,
214                                   struct library *start_after,
215                                   enum callback_status (*cb)(struct process *p,
216                                                              struct library *l,
217                                                              void *data),
218                                   void *data);
219
220 /* Insert BP into PROC.  */
221 int proc_add_breakpoint(struct process *proc, struct breakpoint *bp);
222
223 /* Remove BP from PROC.  This has no reason to fail in runtime.  If it
224  * does not find BP in PROC, it's hard error guarded by assertion.  */
225 void proc_remove_breakpoint(struct process *proc, struct breakpoint *bp);
226
227 /* Iterate through the breakpoints of PROC.  See callback.h for notes
228  * on iteration interfaces.  */
229 void *proc_each_breakpoint(struct process *proc, void *start,
230                            enum callback_status (*cb)(struct process *proc,
231                                                       struct breakpoint *bp,
232                                                       void *data),
233                            void *data);
234
235 /* Iterate through the dynamic section at src_addr looking for D_TAG.
236  * If tag is found, fill it's value in RET and return 0.
237  * If tag is not found, return a negative value.  */
238 int proc_find_dynamic_entry_addr(struct process *proc, arch_addr_t src_addr,
239                                  int d_tag, arch_addr_t *ret);
240
241 /* Finds a symbol corresponding to LIBSYM in a process PROC.  Returns
242  * 0 and sets *RETLIB and *RETSYM if the corresponding pointer is
243  * non-NULL.  Returns a negative value when the symbols couldn't be
244  * found.  */
245 int proc_find_symbol(struct process *proc, struct library_symbol *sym,
246                      struct library **retlib, struct library_symbol **retsym);
247
248 /* Iterate through all symbols in all libraries of PROC.  See
249  * callback.h for notes on this interface.  */
250 struct library_symbol *proc_each_symbol
251         (struct process *proc, struct library_symbol *start_after,
252          enum callback_status (*cb)(struct library_symbol *, void *),
253          void *data);
254
255 /* Read 8, 16, 32 or 64-bit quantity located at ADDR in PROC.  The
256  * resulting value is stored in *LP.  0 is returned on success or a
257  * negative value on failure.  This uses umovebytes under the hood
258  * (see backend.h).  */
259 int proc_read_8(struct process *proc, arch_addr_t addr, uint8_t *lp);
260 int proc_read_16(struct process *proc, arch_addr_t addr, uint16_t *lp);
261 int proc_read_32(struct process *proc, arch_addr_t addr, uint32_t *lp);
262 int proc_read_64(struct process *proc, arch_addr_t addr, uint64_t *lp);
263
264 #endif /* _PROC_H_ */