blob: 9f0e8e0c75c29924f9b2b01ed3d2c1affdf518b5 [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
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
Michael K. Edwardsef4428a2011-03-06 20:39:18 +000023#ifndef COMMON_H
24#define COMMON_H
25
Petr Machataa06eb812011-07-08 19:23:25 +020026#include <config.h>
Joe Damatoab3b72c2010-10-31 00:21:53 -070027
Juan Cespedes3df476b2009-05-28 19:17:17 +020028#include <sys/types.h>
29#include <sys/time.h>
30#include <stdio.h>
31
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020032#include "ltrace.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020033#include "defs.h"
34#include "dict.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020035#include "sysdep.h"
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020036#include "debug.h"
Marc Kleine-Budde747c73d2010-02-03 20:23:20 +010037#include "ltrace-elf.h"
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020038#include "read_config_file.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010039#include "proc.h"
Petr Machata000e3112012-01-03 17:03:39 +010040#include "forward.h"
Juan Cespedes3df476b2009-05-28 19:17:17 +020041
Petr Machatacdd17b82012-06-01 19:35:24 +020042#if defined HAVE_LIBSUPC__ || defined HAVE_LIBSTDC__
43# define USE_CXA_DEMANGLE
44#endif
45#if defined HAVE_LIBIBERTY || defined USE_CXA_DEMANGLE
Juan Cespedes3df476b2009-05-28 19:17:17 +020046# define USE_DEMANGLE
47#endif
48
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020049extern char * command;
Juan Cespedes3df476b2009-05-28 19:17:17 +020050
51extern int exiting; /* =1 if we have to exit ASAP */
52
Juan Cespedes3df476b2009-05-28 19:17:17 +020053typedef struct Function Function;
54struct Function {
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020055 const char * name;
Petr Machata865303f2012-01-06 18:40:38 +010056 struct param *params;
Petr Machata000e3112012-01-03 17:03:39 +010057 struct arg_type_info *return_info;
Petr Machata865303f2012-01-06 18:40:38 +010058 size_t num_params;
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020059 Function * next;
Juan Cespedes3df476b2009-05-28 19:17:17 +020060};
61
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020062extern Function * list_of_functions;
Juan Cespedes3df476b2009-05-28 19:17:17 +020063extern char *PLTs_initialized_by_here;
64
Juan Cespedes3df476b2009-05-28 19:17:17 +020065struct opt_c_struct {
66 int count;
67 struct timeval tv;
68};
Juan Cespedes3df476b2009-05-28 19:17:17 +020069
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020070#include "options.h"
71#include "output.h"
72#ifdef USE_DEMANGLE
73#include "demangle.h"
74#endif
Juan Cespedes3df476b2009-05-28 19:17:17 +020075
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020076extern Dict * dict_opt_c;
Juan Cespedes3df476b2009-05-28 19:17:17 +020077
Petr Machata617ff0b2011-10-06 14:23:24 +020078enum process_status {
79 ps_invalid, /* Failure. */
80 ps_stop, /* Job-control stop. */
81 ps_tracing_stop,
Petr Machatacbe29c62011-09-27 02:27:58 +020082 ps_sleeping,
Petr Machata617ff0b2011-10-06 14:23:24 +020083 ps_zombie,
84 ps_other, /* Necessary other states can be added as needed. */
85};
86
Petr Machata69a03e62011-07-09 11:29:12 +020087/* Events */
88enum ecb_status {
89 ecb_cont, /* The iteration should continue. */
90 ecb_yield, /* The iteration should stop, yielding this
91 * event. */
92 ecb_deque, /* Like ecb_stop, but the event should be removed
93 * from the queue. */
94};
95extern Event * next_event(void);
96extern Event * each_qd_event(enum ecb_status (* cb)(Event * event, void * data),
97 void * data);
98extern void enque_event(Event * event);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020099extern void handle_event(Event * event);
Petr Machata4007d742011-07-09 11:29:42 +0200100
Petr Machata1b17dbf2011-07-08 19:22:52 +0200101extern pid_t execute_program(const char * command, char ** argv);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200102
Juan Cespedes3df476b2009-05-28 19:17:17 +0200103extern void show_summary(void);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200104
Petr Machata9294d822012-02-07 12:35:58 +0100105struct breakpoint;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100106struct library_symbol;
Petr Machata9294d822012-02-07 12:35:58 +0100107
Petr Machataf48031c2012-02-07 12:35:58 +0100108struct breakpoint;
109
Juan Cespedes3df476b2009-05-28 19:17:17 +0200110/* Arch-dependent stuff: */
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200111extern char * pid2name(pid_t pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200112extern pid_t process_leader(pid_t pid);
113extern int process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n);
114extern int process_stopped(pid_t pid);
Petr Machata617ff0b2011-10-06 14:23:24 +0200115extern enum process_status process_status(pid_t pid);
Petr Machata3ed2a422012-04-06 17:18:55 +0200116extern void trace_set_options(struct Process *proc);
Petr Machatac805c622012-03-02 00:10:37 +0100117extern int wait_for_proc(pid_t pid);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200118extern void trace_me(void);
119extern int trace_pid(pid_t pid);
120extern void untrace_pid(pid_t pid);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200121extern void get_arch_dep(Process * proc);
122extern void * get_instruction_pointer(Process * proc);
123extern void set_instruction_pointer(Process * proc, void * addr);
124extern void * get_stack_pointer(Process * proc);
125extern void * get_return_addr(Process * proc, void * stack_pointer);
Juan Cespedes2a61d192009-07-04 11:29:27 +0200126extern void set_return_addr(Process * proc, void * addr);
Petr Machatad7b63322012-04-26 16:08:42 +0200127extern void enable_breakpoint(struct Process *proc, struct breakpoint *sbp);
128extern void disable_breakpoint(struct Process *proc, struct breakpoint *sbp);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200129extern int syscall_p(Process * proc, int status, int * sysnum);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200130extern void continue_process(pid_t pid);
131extern void continue_after_signal(pid_t pid, int signum);
Petr Machata43d2fe52011-11-02 13:25:49 +0100132extern void continue_after_syscall(Process *proc, int sysnum, int ret_p);
Petr Machatad7b63322012-04-26 16:08:42 +0200133extern void continue_after_breakpoint(struct Process *proc, struct breakpoint *sbp);
Petr Machatacbe29c62011-09-27 02:27:58 +0200134extern void continue_after_vfork(Process * proc);
Joe Damatodfa3fa32010-11-08 15:47:35 -0800135extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count);
Juan Cespedes8d1b92b2009-07-03 10:39:34 +0200136extern int ffcheck(void * maddr);
137extern void * sym2addr(Process *, struct library_symbol *);
Petr Machata52dbfb12012-03-29 16:38:26 +0200138extern int linkmap_init(struct Process *proc, void *dyn_addr);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800139extern void arch_check_dbg(Process *proc);
Petr Machata9a5420c2011-07-09 11:21:23 +0200140extern int task_kill (pid_t pid, int sig);
141
Petr Machatacec06ec2012-04-10 13:31:55 +0200142/* Called when trace_me or primary trace_pid fail. This may plug in
143 * any platform-specific knowledge of why it could be so. */
144void trace_fail_warning(pid_t pid);
145
Petr Machataffe4cd22012-04-11 18:01:44 +0200146/* A pair of functions called to initiate a detachment request when
147 * ltrace is about to exit. Their job is to undo any effects that
148 * tracing had and eventually detach process, perhaps by way of
149 * installing a process handler.
150 *
151 * OS_LTRACE_EXITING_SIGHANDLER is called from a signal handler
152 * context right after the signal was captured. It returns 1 if the
153 * request was handled or 0 if it wasn't.
154 *
155 * If the call to OS_LTRACE_EXITING_SIGHANDLER didn't handle the
156 * request, OS_LTRACE_EXITING is called when the next event is
157 * generated. Therefore it's called in "safe" context, without
Petr Machatae67635d2012-03-21 03:37:39 +0100158 * re-entrancy concerns, but it's only called after an event is
Petr Machataffe4cd22012-04-11 18:01:44 +0200159 * generated. */
160int os_ltrace_exiting_sighandler(void);
161void os_ltrace_exiting(void);
Juan Cespedes3df476b2009-05-28 19:17:17 +0200162
Petr Machatae0615ab2012-04-17 05:17:48 +0200163int arch_elf_init(struct ltelf *lte, struct library *lib);
Petr Machata4d9a91c2012-03-24 04:55:03 +0100164void arch_elf_destroy(struct ltelf *lte);
165
Petr Machatae6523e62012-03-24 04:54:06 +0100166enum plt_status {
167 plt_fail,
168 plt_ok,
169 plt_default,
170};
171
172enum plt_status arch_elf_add_plt_entry(struct Process *p, struct ltelf *l,
173 const char *n, GElf_Rela *r, size_t i,
174 struct library_symbol **ret);
Petr Machatae67635d2012-03-21 03:37:39 +0100175
Petr Machata8cce1192012-03-25 01:37:19 +0100176int arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp);
177void arch_breakpoint_destroy(struct breakpoint *sbp);
Petr Machatad3cc9882012-04-13 21:40:23 +0200178int arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp);
Petr Machata8cce1192012-03-25 01:37:19 +0100179
Petr Machata994ad6d2012-04-17 03:07:04 +0200180void arch_library_init(struct library *lib);
181void arch_library_destroy(struct library *lib);
182void arch_library_clone(struct library *retp, struct library *lib);
183
Petr Machata24c6e9d2012-04-15 04:31:34 +0200184int arch_library_symbol_init(struct library_symbol *libsym);
185void arch_library_symbol_destroy(struct library_symbol *libsym);
186int arch_library_symbol_clone(struct library_symbol *retp,
187 struct library_symbol *libsym);
188
Petr Machata744f2552012-04-15 04:33:18 +0200189int arch_process_init(struct Process *proc);
190void arch_process_destroy(struct Process *proc);
191int arch_process_clone(struct Process *retp, struct Process *proc);
192int arch_process_exec(struct Process *proc);
193
Petr Machata93d95df2012-04-17 05:16:19 +0200194/* This is called after the dynamic linker is done with the
195 * process startup. */
196void arch_dynlink_done(struct Process *proc);
197
Petr Machata94078ec2012-01-05 18:07:02 +0100198/* Format VALUE into STREAM. The dictionary of all arguments is given
199 * for purposes of evaluating array lengths and other dynamic
200 * expressions. Returns number of characters outputted, -1 in case of
201 * failure. */
202int format_argument(FILE *stream, struct value *value,
203 struct value_dict *arguments);
204
Michael K. Edwardsef4428a2011-03-06 20:39:18 +0000205#endif