| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 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 | |
| Michael K. Edwards | ef4428a | 2011-03-06 20:39:18 +0000 | [diff] [blame] | 23 | #ifndef COMMON_H |
| 24 | #define COMMON_H |
| 25 | |
| Petr Machata | a06eb81 | 2011-07-08 19:23:25 +0200 | [diff] [blame] | 26 | #include <config.h> |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 27 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <stdio.h> |
| 31 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 32 | #include "ltrace.h" |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 33 | #include "defs.h" |
| 34 | #include "dict.h" |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 35 | #include "sysdep.h" |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 36 | #include "debug.h" |
| Marc Kleine-Budde | 747c73d | 2010-02-03 20:23:20 +0100 | [diff] [blame] | 37 | #include "ltrace-elf.h" |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 38 | #include "read_config_file.h" |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 39 | #include "proc.h" |
| Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 40 | #include "forward.h" |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 41 | |
| Petr Machata | cdd17b8 | 2012-06-01 19:35:24 +0200 | [diff] [blame] | 42 | #if defined HAVE_LIBSUPC__ || defined HAVE_LIBSTDC__ |
| 43 | # define USE_CXA_DEMANGLE |
| 44 | #endif |
| 45 | #if defined HAVE_LIBIBERTY || defined USE_CXA_DEMANGLE |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 46 | # define USE_DEMANGLE |
| 47 | #endif |
| 48 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 49 | extern char * command; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 50 | |
| 51 | extern int exiting; /* =1 if we have to exit ASAP */ |
| 52 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 53 | typedef struct Function Function; |
| 54 | struct Function { |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 55 | const char * name; |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 56 | struct param *params; |
| Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 57 | struct arg_type_info *return_info; |
| Petr Machata | 3a9bf6d | 2012-01-06 21:44:10 +0100 | [diff] [blame^] | 58 | int own_return_info; |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 59 | size_t num_params; |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 60 | Function * next; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 63 | extern Function * list_of_functions; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 64 | extern char *PLTs_initialized_by_here; |
| 65 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 66 | struct opt_c_struct { |
| 67 | int count; |
| 68 | struct timeval tv; |
| 69 | }; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 70 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 71 | #include "options.h" |
| 72 | #include "output.h" |
| 73 | #ifdef USE_DEMANGLE |
| 74 | #include "demangle.h" |
| 75 | #endif |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 76 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 77 | extern Dict * dict_opt_c; |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 78 | |
| Petr Machata | 617ff0b | 2011-10-06 14:23:24 +0200 | [diff] [blame] | 79 | enum process_status { |
| 80 | ps_invalid, /* Failure. */ |
| 81 | ps_stop, /* Job-control stop. */ |
| 82 | ps_tracing_stop, |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 83 | ps_sleeping, |
| Petr Machata | 617ff0b | 2011-10-06 14:23:24 +0200 | [diff] [blame] | 84 | ps_zombie, |
| 85 | ps_other, /* Necessary other states can be added as needed. */ |
| 86 | }; |
| 87 | |
| Petr Machata | 69a03e6 | 2011-07-09 11:29:12 +0200 | [diff] [blame] | 88 | /* Events */ |
| 89 | enum ecb_status { |
| 90 | ecb_cont, /* The iteration should continue. */ |
| 91 | ecb_yield, /* The iteration should stop, yielding this |
| 92 | * event. */ |
| 93 | ecb_deque, /* Like ecb_stop, but the event should be removed |
| 94 | * from the queue. */ |
| 95 | }; |
| 96 | extern Event * next_event(void); |
| 97 | extern Event * each_qd_event(enum ecb_status (* cb)(Event * event, void * data), |
| 98 | void * data); |
| 99 | extern void enque_event(Event * event); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 100 | extern void handle_event(Event * event); |
| Petr Machata | 4007d74 | 2011-07-09 11:29:42 +0200 | [diff] [blame] | 101 | |
| Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 102 | extern pid_t execute_program(const char * command, char ** argv); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 103 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 104 | extern void show_summary(void); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 105 | |
| Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 106 | struct breakpoint; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 107 | struct library_symbol; |
| Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 108 | |
| Petr Machata | f48031c | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 109 | struct breakpoint; |
| 110 | |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 111 | /* Arch-dependent stuff: */ |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 112 | extern char * pid2name(pid_t pid); |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 113 | extern pid_t process_leader(pid_t pid); |
| 114 | extern int process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n); |
| 115 | extern int process_stopped(pid_t pid); |
| Petr Machata | 617ff0b | 2011-10-06 14:23:24 +0200 | [diff] [blame] | 116 | extern enum process_status process_status(pid_t pid); |
| Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 117 | extern void trace_set_options(struct Process *proc); |
| Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 118 | extern int wait_for_proc(pid_t pid); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 119 | extern void trace_me(void); |
| 120 | extern int trace_pid(pid_t pid); |
| 121 | extern void untrace_pid(pid_t pid); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 122 | extern void get_arch_dep(Process * proc); |
| 123 | extern void * get_instruction_pointer(Process * proc); |
| 124 | extern void set_instruction_pointer(Process * proc, void * addr); |
| 125 | extern void * get_stack_pointer(Process * proc); |
| 126 | extern void * get_return_addr(Process * proc, void * stack_pointer); |
| Juan Cespedes | 2a61d19 | 2009-07-04 11:29:27 +0200 | [diff] [blame] | 127 | extern void set_return_addr(Process * proc, void * addr); |
| Petr Machata | d7b6332 | 2012-04-26 16:08:42 +0200 | [diff] [blame] | 128 | extern void enable_breakpoint(struct Process *proc, struct breakpoint *sbp); |
| 129 | extern void disable_breakpoint(struct Process *proc, struct breakpoint *sbp); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 130 | extern int syscall_p(Process * proc, int status, int * sysnum); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 131 | extern void continue_process(pid_t pid); |
| 132 | extern void continue_after_signal(pid_t pid, int signum); |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 133 | extern void continue_after_syscall(Process *proc, int sysnum, int ret_p); |
| Petr Machata | d7b6332 | 2012-04-26 16:08:42 +0200 | [diff] [blame] | 134 | extern void continue_after_breakpoint(struct Process *proc, struct breakpoint *sbp); |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 135 | extern void continue_after_vfork(Process * proc); |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 136 | extern size_t umovebytes (Process *proc, void * addr, void * laddr, size_t count); |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 137 | extern int ffcheck(void * maddr); |
| 138 | extern void * sym2addr(Process *, struct library_symbol *); |
| Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 139 | extern int linkmap_init(struct Process *proc, void *dyn_addr); |
| Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 140 | extern void arch_check_dbg(Process *proc); |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 141 | extern int task_kill (pid_t pid, int sig); |
| 142 | |
| Petr Machata | cec06ec | 2012-04-10 13:31:55 +0200 | [diff] [blame] | 143 | /* Called when trace_me or primary trace_pid fail. This may plug in |
| 144 | * any platform-specific knowledge of why it could be so. */ |
| 145 | void trace_fail_warning(pid_t pid); |
| 146 | |
| Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 147 | /* A pair of functions called to initiate a detachment request when |
| 148 | * ltrace is about to exit. Their job is to undo any effects that |
| 149 | * tracing had and eventually detach process, perhaps by way of |
| 150 | * installing a process handler. |
| 151 | * |
| 152 | * OS_LTRACE_EXITING_SIGHANDLER is called from a signal handler |
| 153 | * context right after the signal was captured. It returns 1 if the |
| 154 | * request was handled or 0 if it wasn't. |
| 155 | * |
| 156 | * If the call to OS_LTRACE_EXITING_SIGHANDLER didn't handle the |
| 157 | * request, OS_LTRACE_EXITING is called when the next event is |
| 158 | * generated. Therefore it's called in "safe" context, without |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 159 | * re-entrancy concerns, but it's only called after an event is |
| Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 160 | * generated. */ |
| 161 | int os_ltrace_exiting_sighandler(void); |
| 162 | void os_ltrace_exiting(void); |
| Juan Cespedes | 3df476b | 2009-05-28 19:17:17 +0200 | [diff] [blame] | 163 | |
| Petr Machata | e0615ab | 2012-04-17 05:17:48 +0200 | [diff] [blame] | 164 | int arch_elf_init(struct ltelf *lte, struct library *lib); |
| Petr Machata | 4d9a91c | 2012-03-24 04:55:03 +0100 | [diff] [blame] | 165 | void arch_elf_destroy(struct ltelf *lte); |
| 166 | |
| Petr Machata | e6523e6 | 2012-03-24 04:54:06 +0100 | [diff] [blame] | 167 | enum plt_status { |
| 168 | plt_fail, |
| 169 | plt_ok, |
| 170 | plt_default, |
| 171 | }; |
| 172 | |
| 173 | enum plt_status arch_elf_add_plt_entry(struct Process *p, struct ltelf *l, |
| 174 | const char *n, GElf_Rela *r, size_t i, |
| 175 | struct library_symbol **ret); |
| Petr Machata | e67635d | 2012-03-21 03:37:39 +0100 | [diff] [blame] | 176 | |
| Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 177 | int arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp); |
| 178 | void arch_breakpoint_destroy(struct breakpoint *sbp); |
| Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 179 | int arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp); |
| Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 180 | |
| Petr Machata | 994ad6d | 2012-04-17 03:07:04 +0200 | [diff] [blame] | 181 | void arch_library_init(struct library *lib); |
| 182 | void arch_library_destroy(struct library *lib); |
| 183 | void arch_library_clone(struct library *retp, struct library *lib); |
| 184 | |
| Petr Machata | 24c6e9d | 2012-04-15 04:31:34 +0200 | [diff] [blame] | 185 | int arch_library_symbol_init(struct library_symbol *libsym); |
| 186 | void arch_library_symbol_destroy(struct library_symbol *libsym); |
| 187 | int arch_library_symbol_clone(struct library_symbol *retp, |
| 188 | struct library_symbol *libsym); |
| 189 | |
| Petr Machata | 744f255 | 2012-04-15 04:33:18 +0200 | [diff] [blame] | 190 | int arch_process_init(struct Process *proc); |
| 191 | void arch_process_destroy(struct Process *proc); |
| 192 | int arch_process_clone(struct Process *retp, struct Process *proc); |
| 193 | int arch_process_exec(struct Process *proc); |
| 194 | |
| Petr Machata | 93d95df | 2012-04-17 05:16:19 +0200 | [diff] [blame] | 195 | /* This is called after the dynamic linker is done with the |
| 196 | * process startup. */ |
| 197 | void arch_dynlink_done(struct Process *proc); |
| 198 | |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 199 | /* Format VALUE into STREAM. The dictionary of all arguments is given |
| 200 | * for purposes of evaluating array lengths and other dynamic |
| 201 | * expressions. Returns number of characters outputted, -1 in case of |
| 202 | * failure. */ |
| 203 | int format_argument(FILE *stream, struct value *value, |
| 204 | struct value_dict *arguments); |
| 205 | |
| Michael K. Edwards | ef4428a | 2011-03-06 20:39:18 +0000 | [diff] [blame] | 206 | #endif |