blob: 659c78661fa35964764a60766215bf63989f307e [file] [log] [blame]
Petr Machata2b46cfc2012-02-18 11:17:29 +01001/*
2 * This file is part of ltrace.
Petr Machata6bcc0922014-01-09 23:50:07 +01003 * Copyright (C) 2010,2011,2012,2013,2014 Petr Machata, Red Hat Inc.
Petr Machata2b46cfc2012-02-18 11:17:29 +01004 * 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
Petr Machata366c2f42012-02-09 19:34:36 +010023#ifndef _PROC_H_
24#define _PROC_H_
25
Petr Machata8a568dd2012-05-18 14:12:27 +020026#include "config.h"
27
Andrey Zonovd2c5dfd2012-08-05 00:16:55 +040028#include <sys/time.h>
Petr Machata653085a2013-01-15 17:40:40 +010029#include <stdint.h>
Andrey Zonovd2c5dfd2012-08-05 00:16:55 +040030
Mark Wielaarddfefa9f2014-01-07 21:00:44 +010031#if defined(HAVE_LIBDW)
32# include <elfutils/libdwfl.h>
33#endif
34
Petr Machata366c2f42012-02-09 19:34:36 +010035#if defined(HAVE_LIBUNWIND)
36# include <libunwind.h>
Luca Clementiaf452c62014-01-03 22:05:03 -080037# include <libunwind-ptrace.h>
Petr Machata366c2f42012-02-09 19:34:36 +010038#endif /* defined(HAVE_LIBUNWIND) */
39
40#include "ltrace.h"
41#include "dict.h"
Petr Machata744f2552012-04-15 04:33:18 +020042#include "sysdep.h"
Petr Machataa24021c2012-09-25 14:46:44 +020043#include "callback.h"
Petr Machata6d8aa0b2012-10-31 03:27:36 +010044#include "forward.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010045
Petr Machata366c2f42012-02-09 19:34:36 +010046struct event_handler {
47 /* Event handler that overrides the default one. Should
48 * return NULL if the event was handled, otherwise the
49 * returned event is passed to the default handler. */
50 Event *(*on_event)(struct event_handler *self, Event *event);
51
52 /* Called when the event handler removal is requested. */
53 void (*destroy)(struct event_handler *self);
54};
55
56enum process_state {
57 STATE_ATTACHED = 0,
58 STATE_BEING_CREATED,
59 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
60};
61
Petr Machataf6ec08a2012-01-06 16:58:54 +010062struct output_state {
63 size_t params_left;
64 int need_delim;
65};
66
Petr Machata366c2f42012-02-09 19:34:36 +010067struct callstack_element {
68 union {
69 int syscall;
70 struct library_symbol * libfunc;
71 } c_un;
72 int is_syscall;
Petr Machata4dcc3892013-01-30 23:11:35 +010073 arch_addr_t return_addr;
Petr Machata8a730f32013-11-21 20:43:51 +010074 struct timeval enter_time;
Petr Machataf6ec08a2012-01-06 16:58:54 +010075 struct fetch_context *fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +010076 struct value_dict *arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +010077 struct output_state out;
Petr Machata366c2f42012-02-09 19:34:36 +010078};
79
80/* XXX We should get rid of this. */
81#define MAX_CALLDEPTH 64
82
83/* XXX We would rather have this all organized a little differently,
Petr Machata929bd572012-12-17 03:20:34 +010084 * have struct process for the whole group and struct task (or struct
85 * lwp, struct thread) for what's there for per-thread stuff. But for
86 * now this is the less invasive way of structuring it. */
87struct process {
Petr Machata366c2f42012-02-09 19:34:36 +010088 enum process_state state;
Petr Machata929bd572012-12-17 03:20:34 +010089 struct process *parent; /* needed by STATE_BEING_CREATED */
Petr Machata366c2f42012-02-09 19:34:36 +010090 char * filename;
91 pid_t pid;
92
93 /* Dictionary of breakpoints (which is a mapping
94 * address->breakpoint). This is NULL for non-leader
Petr Machatad7e4ca82012-11-28 03:38:47 +010095 * processes. */
96 struct dict *breakpoints;
Petr Machata366c2f42012-02-09 19:34:36 +010097
98 int mask_32bit; /* 1 if 64-bit ltrace is tracing 32-bit process */
99 unsigned int personality;
100 int tracesysgood; /* signal indicating a PTRACE_SYSCALL trap */
101
Petr Machataba1664b2012-04-28 14:59:05 +0200102 size_t callstack_depth;
Petr Machata366c2f42012-02-09 19:34:36 +0100103 struct callstack_element callstack[MAX_CALLDEPTH];
Petr Machata76dd9292012-04-03 13:02:06 +0200104
105 /* Linked list of libraries in backwards order of mapping.
106 * The last element is the executed binary itself. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100107 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +0100108
Petr Machata366c2f42012-02-09 19:34:36 +0100109 /* Arch-dependent: */
Petr Machata366c2f42012-02-09 19:34:36 +0100110 void * instruction_pointer;
111 void * stack_pointer; /* To get return addr, args... */
Petr Machata366c2f42012-02-09 19:34:36 +0100112 void * arch_ptr;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400113
114 /* XXX We would like to replace this with a pointer to ABI
115 * object that would provide the relevant services, instead of
116 * checking the necessary flags in the back end ad
117 * nauseam. */
Petr Machata366c2f42012-02-09 19:34:36 +0100118 short e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400119 char e_class;
120
Mark Wielaarddfefa9f2014-01-07 21:00:44 +0100121#if defined(HAVE_LIBDW)
122 /* Unwind info for leader, NULL for non-leader procs. */
123 Dwfl *dwfl;
124#endif /* defined(HAVE_LIBDW) */
125
Petr Machata366c2f42012-02-09 19:34:36 +0100126#if defined(HAVE_LIBUNWIND)
127 /* libunwind address space */
128 unw_addr_space_t unwind_as;
129 void *unwind_priv;
130#endif /* defined(HAVE_LIBUNWIND) */
131
132 /* Set in leader. */
133 struct event_handler *event_handler;
134
135 /**
136 * Process chaining.
137 **/
Petr Machata929bd572012-12-17 03:20:34 +0100138 struct process *next;
Petr Machata366c2f42012-02-09 19:34:36 +0100139
140 /* LEADER points to the leader thread of the POSIX.1 process.
141 If X->LEADER == X, then X is the leader thread and the
Petr Machata929bd572012-12-17 03:20:34 +0100142 process structures chained by NEXT represent other threads,
Petr Machata366c2f42012-02-09 19:34:36 +0100143 up until, but not including, the next leader thread.
144 LEADER may be NULL after the leader has already exited. In
145 that case this process is waiting to be collected. */
Petr Machata929bd572012-12-17 03:20:34 +0100146 struct process *leader;
Petr Machata744f2552012-04-15 04:33:18 +0200147
Petr Machata0f6e6d92012-10-26 23:42:17 +0200148 struct os_process_data os;
Petr Machata744f2552012-04-15 04:33:18 +0200149 struct arch_process_data arch;
Petr Machata366c2f42012-02-09 19:34:36 +0100150};
151
Petr Machata75934ad2012-04-14 02:28:03 +0200152/* Initialize a process given a path to binary FILENAME, with a PID,
153 * and add the process to an internal chain of traced processes. */
Petr Machata929bd572012-12-17 03:20:34 +0100154int process_init(struct process *proc, const char *filename, pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100155
Petr Machata3d0c91c2012-04-14 02:37:38 +0200156/* PROC underwent an exec. This is a bit like process_destroy
157 * followed by process_init, except that some state is kept and the
158 * process doesn't lose it's place in the list of processes. */
Petr Machata929bd572012-12-17 03:20:34 +0100159int process_exec(struct process *proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200160
161/* Release any memory allocated for PROC (but not PROC itself). Does
162 * NOT remove PROC from internal chain.
163 *
164 * XXX clearly this init/destroy pair is different than others and
165 * should be fixed. process_init should presumably be separate from
166 * process_add. */
Petr Machata929bd572012-12-17 03:20:34 +0100167void process_destroy(struct process *proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200168
Petr Machata929bd572012-12-17 03:20:34 +0100169struct process *open_program(const char *filename, pid_t pid);
Petr Machata366c2f42012-02-09 19:34:36 +0100170void open_pid(pid_t pid);
Petr Machata929bd572012-12-17 03:20:34 +0100171struct process *pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100172
173/* Clone the contents of PROC into the memory referenced by RETP.
174 * Returns 0 on success or a negative value on failure. */
Petr Machata929bd572012-12-17 03:20:34 +0100175int process_clone(struct process *retp, struct process *proc, pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100176
Petr Machataa24021c2012-09-25 14:46:44 +0200177/* Iterate through the processes that ltrace currently traces. Tasks
178 * are considered to be processes for the purpose of this iterator.
179 * See callback.h for notes on iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100180struct process *each_process(struct process *start_after,
181 enum callback_status (*cb)(struct process *proc,
182 void *data),
183 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100184
Petr Machataa24021c2012-09-25 14:46:44 +0200185/* Iterate through list of tasks of given process PROC. See
186 * callback.h for notes on iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100187struct process *each_task(struct process *proc, struct process *start_after,
188 enum callback_status (*cb)(struct process *proc,
189 void *data),
190 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100191
Petr Machata929bd572012-12-17 03:20:34 +0100192void change_process_leader(struct process *proc, struct process *leader);
Petr Machatafd2641c2012-04-24 21:33:16 +0200193
Petr Machatadf2c88c2013-03-19 17:55:25 +0100194/* Prepare those parts of process initialization that need to be done
195 * after _start is hit (i.e. after dynamic linking was done). */
196void process_hit_start(struct process *proc);
197
Petr Machatafd2641c2012-04-24 21:33:16 +0200198/* Remove process from the list of traced processes, drop any events
199 * in the event queue, destroy it and free memory. */
Petr Machata929bd572012-12-17 03:20:34 +0100200void remove_process(struct process *proc);
Petr Machatafd2641c2012-04-24 21:33:16 +0200201
Petr Machata929bd572012-12-17 03:20:34 +0100202void install_event_handler(struct process *proc, struct event_handler *handler);
203void destroy_event_handler(struct process *proc);
Petr Machata366c2f42012-02-09 19:34:36 +0100204
Petr Machata2b46cfc2012-02-18 11:17:29 +0100205/* Add a library LIB to the list of PROC's libraries. */
Petr Machata929bd572012-12-17 03:20:34 +0100206void proc_add_library(struct process *proc, struct library *lib);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100207
208/* Remove LIB from list of PROC's libraries. Returns 0 if the library
209 * was found and unlinked, otherwise returns a negative value. */
Petr Machata929bd572012-12-17 03:20:34 +0100210int proc_remove_library(struct process *proc, struct library *lib);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100211
Petr Machataef2fd272012-09-28 00:43:01 +0200212/* Clear a delayed flag. If a symbol is neither latent, nor delayed,
213 * a breakpoint is inserted for it. Returns 0 if the activation was
214 * successful or a negative value if it failed. Note that if a symbol
215 * is both latent and delayed, this will not enable the corresponding
216 * breakpoint. */
Petr Machata929bd572012-12-17 03:20:34 +0100217int proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200218 struct library_symbol *libsym);
219
Petr Machataa24021c2012-09-25 14:46:44 +0200220/* Iterate through the libraries of PROC. See callback.h for notes on
221 * iteration interfaces. */
Petr Machata7ac04ed2012-11-23 19:00:41 +0100222struct library *proc_each_library(struct process *proc,
223 struct library *start_after,
Petr Machata929bd572012-12-17 03:20:34 +0100224 enum callback_status (*cb)(struct process *p,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225 struct library *l,
226 void *data),
227 void *data);
228
Petr Machata52dbfb12012-03-29 16:38:26 +0200229/* Insert BP into PROC. */
Petr Machata929bd572012-12-17 03:20:34 +0100230int proc_add_breakpoint(struct process *proc, struct breakpoint *bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200231
Petr Machataf7fee432012-04-19 17:00:53 +0200232/* Remove BP from PROC. This has no reason to fail in runtime. If it
233 * does not find BP in PROC, it's hard error guarded by assertion. */
Petr Machata929bd572012-12-17 03:20:34 +0100234void proc_remove_breakpoint(struct process *proc, struct breakpoint *bp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100235
Petr Machataa24021c2012-09-25 14:46:44 +0200236/* Iterate through the breakpoints of PROC. See callback.h for notes
237 * on iteration interfaces. */
Petr Machata6bcc0922014-01-09 23:50:07 +0100238arch_addr_t *proc_each_breakpoint(struct process *proc, arch_addr_t *start,
239 enum callback_status (*cb)
240 (struct process *proc,
241 struct breakpoint *bp,
242 void *data),
243 void *data);
Petr Machatad3cc9882012-04-13 21:40:23 +0200244
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200245/* Iterate through the dynamic section at src_addr looking for D_TAG.
246 * If tag is found, fill it's value in RET and return 0.
247 * If tag is not found, return a negative value. */
Petr Machata929bd572012-12-17 03:20:34 +0100248int proc_find_dynamic_entry_addr(struct process *proc, arch_addr_t src_addr,
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200249 int d_tag, arch_addr_t *ret);
Petr Machata165b5662012-10-27 19:23:12 +0200250
251/* Finds a symbol corresponding to LIBSYM in a process PROC. Returns
252 * 0 and sets *RETLIB and *RETSYM if the corresponding pointer is
253 * non-NULL. Returns a negative value when the symbols couldn't be
254 * found. */
Petr Machata929bd572012-12-17 03:20:34 +0100255int proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +0200256 struct library **retlib, struct library_symbol **retsym);
257
Petr Machata32405542012-10-31 03:28:39 +0100258/* Iterate through all symbols in all libraries of PROC. See
259 * callback.h for notes on this interface. */
260struct library_symbol *proc_each_symbol
Petr Machata929bd572012-12-17 03:20:34 +0100261 (struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +0100262 enum callback_status (*cb)(struct library_symbol *, void *),
263 void *data);
264
Petr Machatadc70e762013-01-23 00:02:26 +0100265/* Read 8, 16, 32 or 64-bit quantity located at ADDR in PROC. The
Petr Machata653085a2013-01-15 17:40:40 +0100266 * resulting value is stored in *LP. 0 is returned on success or a
267 * negative value on failure. This uses umovebytes under the hood
268 * (see backend.h). */
Petr Machatadc70e762013-01-23 00:02:26 +0100269int proc_read_8(struct process *proc, arch_addr_t addr, uint8_t *lp);
Petr Machata653085a2013-01-15 17:40:40 +0100270int proc_read_16(struct process *proc, arch_addr_t addr, uint16_t *lp);
271int proc_read_32(struct process *proc, arch_addr_t addr, uint32_t *lp);
272int proc_read_64(struct process *proc, arch_addr_t addr, uint64_t *lp);
273
Petr Machata366c2f42012-02-09 19:34:36 +0100274#endif /* _PROC_H_ */