common.h revision 37b73c0ab8cbcab4729df6d12f2dc846d4652310
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 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 COMMON_H
24#define COMMON_H
25
26#include <config.h>
27
28#include <sys/types.h>
29#include <sys/time.h>
30#include <stdio.h>
31
32#include "ltrace.h"
33#include "defs.h"
34#include "dict.h"
35#include "sysdep.h"
36#include "debug.h"
37#include "ltrace-elf.h"
38#include "read_config_file.h"
39#include "proc.h"
40#include "forward.h"
41
42#if defined HAVE_LIBSUPC__ || defined HAVE_LIBSTDC__
43# define USE_CXA_DEMANGLE
44#endif
45#if defined HAVE_LIBIBERTY || defined USE_CXA_DEMANGLE
46# define USE_DEMANGLE
47#endif
48
49extern char * command;
50
51extern int exiting;  /* =1 if we have to exit ASAP */
52
53enum tof {
54	LT_TOF_FUNCTION,	/* A real library function */
55	LT_TOF_FUNCTIONR,	/* Return from a real library function */
56	LT_TOF_SYSCALL,		/* A syscall */
57	LT_TOF_SYSCALLR,	/* Return from a syscall */
58};
59
60typedef struct Function Function;
61struct Function {
62	const char * name;
63	struct arg_type_info *return_info;
64	int num_params;
65	struct arg_type_info *arg_info[MAX_ARGS];
66	int params_right;
67	Function * next;
68};
69
70extern Function * list_of_functions;
71extern char *PLTs_initialized_by_here;
72
73struct opt_c_struct {
74	int count;
75	struct timeval tv;
76};
77
78#include "options.h"
79#include "output.h"
80#ifdef USE_DEMANGLE
81#include "demangle.h"
82#endif
83
84extern Dict * dict_opt_c;
85
86enum process_status {
87	ps_invalid,	/* Failure.  */
88	ps_stop,	/* Job-control stop.  */
89	ps_tracing_stop,
90	ps_sleeping,
91	ps_zombie,
92	ps_other,	/* Necessary other states can be added as needed.  */
93};
94
95/* Events  */
96enum ecb_status {
97	ecb_cont, /* The iteration should continue.  */
98	ecb_yield, /* The iteration should stop, yielding this
99		    * event.  */
100	ecb_deque, /* Like ecb_stop, but the event should be removed
101		    * from the queue.  */
102};
103extern Event * next_event(void);
104extern Event * each_qd_event(enum ecb_status (* cb)(Event * event, void * data),
105			     void * data);
106extern void enque_event(Event * event);
107extern void handle_event(Event * event);
108
109extern pid_t execute_program(const char * command, char ** argv);
110
111extern void show_summary(void);
112
113struct breakpoint;
114struct library_symbol;
115
116struct breakpoint;
117
118/* Arch-dependent stuff: */
119extern char * pid2name(pid_t pid);
120extern pid_t process_leader(pid_t pid);
121extern int process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n);
122extern int process_stopped(pid_t pid);
123extern enum process_status process_status(pid_t pid);
124extern void trace_set_options(struct Process *proc);
125extern int wait_for_proc(pid_t pid);
126extern void trace_me(void);
127extern int trace_pid(pid_t pid);
128extern void untrace_pid(pid_t pid);
129extern void get_arch_dep(Process * proc);
130extern void * get_instruction_pointer(Process * proc);
131extern void set_instruction_pointer(Process * proc, void * addr);
132extern void * get_stack_pointer(Process * proc);
133extern void * get_return_addr(Process * proc, void * stack_pointer);
134extern void set_return_addr(Process * proc, void * addr);
135extern void enable_breakpoint(struct Process *proc, struct breakpoint *sbp);
136extern void disable_breakpoint(struct Process *proc, struct breakpoint *sbp);
137extern int syscall_p(Process * proc, int status, int * sysnum);
138extern void continue_process(pid_t pid);
139extern void continue_after_signal(pid_t pid, int signum);
140extern void continue_after_syscall(Process *proc, int sysnum, int ret_p);
141extern void continue_after_breakpoint(struct Process *proc, struct breakpoint *sbp);
142extern void continue_after_vfork(Process * proc);
143extern long gimme_arg(enum tof type, Process *proc, int arg_num,
144		      struct arg_type_info *info);
145extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count);
146extern int ffcheck(void * maddr);
147extern void * sym2addr(Process *, struct library_symbol *);
148extern int linkmap_init(struct Process *proc, void *dyn_addr);
149extern void arch_check_dbg(Process *proc);
150extern int task_kill (pid_t pid, int sig);
151
152/* Called when trace_me or primary trace_pid fail.  This may plug in
153 * any platform-specific knowledge of why it could be so.  */
154void trace_fail_warning(pid_t pid);
155
156/* A pair of functions called to initiate a detachment request when
157 * ltrace is about to exit.  Their job is to undo any effects that
158 * tracing had and eventually detach process, perhaps by way of
159 * installing a process handler.
160 *
161 * OS_LTRACE_EXITING_SIGHANDLER is called from a signal handler
162 * context right after the signal was captured.  It returns 1 if the
163 * request was handled or 0 if it wasn't.
164 *
165 * If the call to OS_LTRACE_EXITING_SIGHANDLER didn't handle the
166 * request, OS_LTRACE_EXITING is called when the next event is
167 * generated.  Therefore it's called in "safe" context, without
168 * re-entrancy concerns, but it's only called after an event is
169 * generated.  */
170int os_ltrace_exiting_sighandler(void);
171void os_ltrace_exiting(void);
172
173int arch_elf_init(struct ltelf *lte, struct library *lib);
174void arch_elf_destroy(struct ltelf *lte);
175
176enum plt_status {
177	plt_fail,
178	plt_ok,
179	plt_default,
180};
181
182enum plt_status arch_elf_add_plt_entry(struct Process *p, struct ltelf *l,
183				       const char *n, GElf_Rela *r, size_t i,
184				       struct library_symbol **ret);
185
186int arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp);
187void arch_breakpoint_destroy(struct breakpoint *sbp);
188int arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp);
189
190void arch_library_init(struct library *lib);
191void arch_library_destroy(struct library *lib);
192void arch_library_clone(struct library *retp, struct library *lib);
193
194int arch_library_symbol_init(struct library_symbol *libsym);
195void arch_library_symbol_destroy(struct library_symbol *libsym);
196int arch_library_symbol_clone(struct library_symbol *retp,
197			      struct library_symbol *libsym);
198
199int arch_process_init(struct Process *proc);
200void arch_process_destroy(struct Process *proc);
201int arch_process_clone(struct Process *retp, struct Process *proc);
202int arch_process_exec(struct Process *proc);
203
204/* This is called after the dynamic linker is done with the
205 * process startup.  */
206void arch_dynlink_done(struct Process *proc);
207
208/* Format VALUE into STREAM.  The dictionary of all arguments is given
209 * for purposes of evaluating array lengths and other dynamic
210 * expressions.  Returns number of characters outputted, -1 in case of
211 * failure.  */
212int format_argument(FILE *stream, struct value *value,
213		    struct value_dict *arguments);
214
215#endif
216