blob: 8dd3356a8d4b5d1e8be91fba64c29c6ac226c2bb [file] [log] [blame]
Petr Machata2b46cfc2012-02-18 11:17:29 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2010,2011,2012 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
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>
29
Petr Machata366c2f42012-02-09 19:34:36 +010030#if defined(HAVE_LIBUNWIND)
31# include <libunwind.h>
32#endif /* defined(HAVE_LIBUNWIND) */
33
34#include "ltrace.h"
35#include "dict.h"
Petr Machata744f2552012-04-15 04:33:18 +020036#include "sysdep.h"
Petr Machataa24021c2012-09-25 14:46:44 +020037#include "callback.h"
Petr Machata6d8aa0b2012-10-31 03:27:36 +010038#include "forward.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010039
Petr Machata366c2f42012-02-09 19:34:36 +010040struct event_handler {
41 /* Event handler that overrides the default one. Should
42 * return NULL if the event was handled, otherwise the
43 * returned event is passed to the default handler. */
44 Event *(*on_event)(struct event_handler *self, Event *event);
45
46 /* Called when the event handler removal is requested. */
47 void (*destroy)(struct event_handler *self);
48};
49
50enum process_state {
51 STATE_ATTACHED = 0,
52 STATE_BEING_CREATED,
53 STATE_IGNORED /* ignore this process (it's a fork and no -f was used) */
54};
55
Petr Machataf6ec08a2012-01-06 16:58:54 +010056struct output_state {
57 size_t params_left;
58 int need_delim;
59};
60
Petr Machata366c2f42012-02-09 19:34:36 +010061struct callstack_element {
62 union {
63 int syscall;
64 struct library_symbol * libfunc;
65 } c_un;
66 int is_syscall;
67 void * return_addr;
68 struct timeval time_spent;
Petr Machataf6ec08a2012-01-06 16:58:54 +010069 struct fetch_context *fetch_context;
Petr Machata94078ec2012-01-05 18:07:02 +010070 struct value_dict *arguments;
Petr Machataf6ec08a2012-01-06 16:58:54 +010071 struct output_state out;
Petr Machata366c2f42012-02-09 19:34:36 +010072};
73
74/* XXX We should get rid of this. */
75#define MAX_CALLDEPTH 64
76
77/* XXX We would rather have this all organized a little differently,
Petr Machata929bd572012-12-17 03:20:34 +010078 * have struct process for the whole group and struct task (or struct
79 * lwp, struct thread) for what's there for per-thread stuff. But for
80 * now this is the less invasive way of structuring it. */
81struct process {
Petr Machata366c2f42012-02-09 19:34:36 +010082 enum process_state state;
Petr Machata929bd572012-12-17 03:20:34 +010083 struct process *parent; /* needed by STATE_BEING_CREATED */
Petr Machata366c2f42012-02-09 19:34:36 +010084 char * filename;
85 pid_t pid;
86
87 /* Dictionary of breakpoints (which is a mapping
88 * address->breakpoint). This is NULL for non-leader
Petr Machataecb082f2012-04-05 02:10:10 +020089 * processes. XXX note that we store addresses (keys) by
Petr Machatabac2da52012-05-29 00:42:59 +020090 * value. That assumes that arch_addr_t fits in host
Petr Machataecb082f2012-04-05 02:10:10 +020091 * pointer. */
Petr Machata366c2f42012-02-09 19:34:36 +010092 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
Petr Machataba1664b2012-04-28 14:59:05 +020098 size_t callstack_depth;
Petr Machata366c2f42012-02-09 19:34:36 +010099 struct callstack_element callstack[MAX_CALLDEPTH];
Petr Machata76dd9292012-04-03 13:02:06 +0200100
101 /* Linked list of libraries in backwards order of mapping.
102 * The last element is the executed binary itself. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100103 struct library *libraries;
Petr Machata366c2f42012-02-09 19:34:36 +0100104
Petr Machata366c2f42012-02-09 19:34:36 +0100105 /* Arch-dependent: */
Petr Machata366c2f42012-02-09 19:34:36 +0100106 void * instruction_pointer;
107 void * stack_pointer; /* To get return addr, args... */
108 void * return_addr;
109 void * arch_ptr;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400110
111 /* XXX We would like to replace this with a pointer to ABI
112 * object that would provide the relevant services, instead of
113 * checking the necessary flags in the back end ad
114 * nauseam. */
Petr Machata366c2f42012-02-09 19:34:36 +0100115 short e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400116 char e_class;
117
118 /* XXX this shoudl go to ARM's arch_process_data. */
Petr Machata366c2f42012-02-09 19:34:36 +0100119#ifdef __arm__
120 int thumb_mode; /* ARM execution mode: 0: ARM, 1: Thumb */
121#endif
122
123#if defined(HAVE_LIBUNWIND)
124 /* libunwind address space */
125 unw_addr_space_t unwind_as;
126 void *unwind_priv;
127#endif /* defined(HAVE_LIBUNWIND) */
128
129 /* Set in leader. */
130 struct event_handler *event_handler;
131
132 /**
133 * Process chaining.
134 **/
Petr Machata929bd572012-12-17 03:20:34 +0100135 struct process *next;
Petr Machata366c2f42012-02-09 19:34:36 +0100136
137 /* LEADER points to the leader thread of the POSIX.1 process.
138 If X->LEADER == X, then X is the leader thread and the
Petr Machata929bd572012-12-17 03:20:34 +0100139 process structures chained by NEXT represent other threads,
Petr Machata366c2f42012-02-09 19:34:36 +0100140 up until, but not including, the next leader thread.
141 LEADER may be NULL after the leader has already exited. In
142 that case this process is waiting to be collected. */
Petr Machata929bd572012-12-17 03:20:34 +0100143 struct process *leader;
Petr Machata744f2552012-04-15 04:33:18 +0200144
Petr Machata0f6e6d92012-10-26 23:42:17 +0200145 struct os_process_data os;
Petr Machata744f2552012-04-15 04:33:18 +0200146 struct arch_process_data arch;
Petr Machata366c2f42012-02-09 19:34:36 +0100147};
148
Petr Machata75934ad2012-04-14 02:28:03 +0200149/* Initialize a process given a path to binary FILENAME, with a PID,
150 * and add the process to an internal chain of traced processes. */
Petr Machata929bd572012-12-17 03:20:34 +0100151int process_init(struct process *proc, const char *filename, pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100152
Petr Machata3d0c91c2012-04-14 02:37:38 +0200153/* PROC underwent an exec. This is a bit like process_destroy
154 * followed by process_init, except that some state is kept and the
155 * process doesn't lose it's place in the list of processes. */
Petr Machata929bd572012-12-17 03:20:34 +0100156int process_exec(struct process *proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200157
158/* Release any memory allocated for PROC (but not PROC itself). Does
159 * NOT remove PROC from internal chain.
160 *
161 * XXX clearly this init/destroy pair is different than others and
162 * should be fixed. process_init should presumably be separate from
163 * process_add. */
Petr Machata929bd572012-12-17 03:20:34 +0100164void process_destroy(struct process *proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200165
Petr Machata929bd572012-12-17 03:20:34 +0100166struct process *open_program(const char *filename, pid_t pid);
Petr Machata366c2f42012-02-09 19:34:36 +0100167void open_pid(pid_t pid);
Petr Machata929bd572012-12-17 03:20:34 +0100168struct process *pid2proc(pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100169
170/* Clone the contents of PROC into the memory referenced by RETP.
171 * Returns 0 on success or a negative value on failure. */
Petr Machata929bd572012-12-17 03:20:34 +0100172int process_clone(struct process *retp, struct process *proc, pid_t pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100173
Petr Machataa24021c2012-09-25 14:46:44 +0200174/* Iterate through the processes that ltrace currently traces. Tasks
175 * are considered to be processes for the purpose of this iterator.
176 * See callback.h for notes on iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100177struct process *each_process(struct process *start_after,
178 enum callback_status (*cb)(struct process *proc,
179 void *data),
180 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100181
Petr Machataa24021c2012-09-25 14:46:44 +0200182/* Iterate through list of tasks of given process PROC. See
183 * callback.h for notes on iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100184struct process *each_task(struct process *proc, struct process *start_after,
185 enum callback_status (*cb)(struct process *proc,
186 void *data),
187 void *data);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100188
Petr Machata929bd572012-12-17 03:20:34 +0100189void change_process_leader(struct process *proc, struct process *leader);
Petr Machatafd2641c2012-04-24 21:33:16 +0200190
191/* Remove process from the list of traced processes, drop any events
192 * in the event queue, destroy it and free memory. */
Petr Machata929bd572012-12-17 03:20:34 +0100193void remove_process(struct process *proc);
Petr Machatafd2641c2012-04-24 21:33:16 +0200194
Petr Machata929bd572012-12-17 03:20:34 +0100195void install_event_handler(struct process *proc, struct event_handler *handler);
196void destroy_event_handler(struct process *proc);
Petr Machata366c2f42012-02-09 19:34:36 +0100197
Petr Machata2b46cfc2012-02-18 11:17:29 +0100198/* Add a library LIB to the list of PROC's libraries. */
Petr Machata929bd572012-12-17 03:20:34 +0100199void proc_add_library(struct process *proc, struct library *lib);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100200
201/* Remove LIB from list of PROC's libraries. Returns 0 if the library
202 * was found and unlinked, otherwise returns a negative value. */
Petr Machata929bd572012-12-17 03:20:34 +0100203int proc_remove_library(struct process *proc, struct library *lib);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100204
Petr Machataef2fd272012-09-28 00:43:01 +0200205/* Clear a delayed flag. If a symbol is neither latent, nor delayed,
206 * a breakpoint is inserted for it. Returns 0 if the activation was
207 * successful or a negative value if it failed. Note that if a symbol
208 * is both latent and delayed, this will not enable the corresponding
209 * breakpoint. */
Petr Machata929bd572012-12-17 03:20:34 +0100210int proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200211 struct library_symbol *libsym);
212
Petr Machataa24021c2012-09-25 14:46:44 +0200213/* Iterate through the libraries of PROC. See callback.h for notes on
214 * iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100215struct library *proc_each_library(struct process *proc, struct library *start,
216 enum callback_status (*cb)(struct process *p,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100217 struct library *l,
218 void *data),
219 void *data);
220
Petr Machata52dbfb12012-03-29 16:38:26 +0200221/* Insert BP into PROC. */
Petr Machata929bd572012-12-17 03:20:34 +0100222int proc_add_breakpoint(struct process *proc, struct breakpoint *bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200223
Petr Machataf7fee432012-04-19 17:00:53 +0200224/* Remove BP from PROC. This has no reason to fail in runtime. If it
225 * does not find BP in PROC, it's hard error guarded by assertion. */
Petr Machata929bd572012-12-17 03:20:34 +0100226void proc_remove_breakpoint(struct process *proc, struct breakpoint *bp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100227
Petr Machataa24021c2012-09-25 14:46:44 +0200228/* Iterate through the breakpoints of PROC. See callback.h for notes
229 * on iteration interfaces. */
Petr Machata929bd572012-12-17 03:20:34 +0100230void *proc_each_breakpoint(struct process *proc, void *start,
231 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200232 struct breakpoint *bp,
233 void *data),
234 void *data);
235
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200236/* Iterate through the dynamic section at src_addr looking for D_TAG.
237 * If tag is found, fill it's value in RET and return 0.
238 * If tag is not found, return a negative value. */
Petr Machata929bd572012-12-17 03:20:34 +0100239int proc_find_dynamic_entry_addr(struct process *proc, arch_addr_t src_addr,
Edgar E. Iglesiascc77b0e2012-10-09 12:15:20 +0200240 int d_tag, arch_addr_t *ret);
Petr Machata165b5662012-10-27 19:23:12 +0200241
242/* Finds a symbol corresponding to LIBSYM in a process PROC. Returns
243 * 0 and sets *RETLIB and *RETSYM if the corresponding pointer is
244 * non-NULL. Returns a negative value when the symbols couldn't be
245 * found. */
Petr Machata929bd572012-12-17 03:20:34 +0100246int proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +0200247 struct library **retlib, struct library_symbol **retsym);
248
Petr Machata32405542012-10-31 03:28:39 +0100249/* Iterate through all symbols in all libraries of PROC. See
250 * callback.h for notes on this interface. */
251struct library_symbol *proc_each_symbol
Petr Machata929bd572012-12-17 03:20:34 +0100252 (struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +0100253 enum callback_status (*cb)(struct library_symbol *, void *),
254 void *data);
255
Petr Machata366c2f42012-02-09 19:34:36 +0100256#endif /* _PROC_H_ */