Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl> |
| 3 | * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl> |
| 4 | * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com> |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 5 | * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 6 | * Copyright (c) 1999-2018 The strace developers. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. The name of the author may not be used to endorse or promote products |
| 18 | * derived from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #include "defs.h" |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 33 | #include <stdarg.h> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 34 | #include <limits.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 35 | #include <fcntl.h> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 36 | #include "ptrace.h" |
Dmitry V. Levin | 0e946ab | 2015-07-17 23:56:54 +0000 | [diff] [blame] | 37 | #include <signal.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 38 | #include <sys/resource.h> |
| 39 | #include <sys/wait.h> |
| 40 | #include <sys/stat.h> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 41 | #ifdef HAVE_PATHS_H |
| 42 | # include <paths.h> |
| 43 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 44 | #include <pwd.h> |
| 45 | #include <grp.h> |
Roland McGrath | 70b0853 | 2004-04-09 00:25:21 +0000 | [diff] [blame] | 46 | #include <dirent.h> |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 47 | #include <locale.h> |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 48 | #include <sys/utsname.h> |
Dmitry V. Levin | 882478a | 2013-05-13 18:43:28 +0000 | [diff] [blame] | 49 | #ifdef HAVE_PRCTL |
| 50 | # include <sys/prctl.h> |
| 51 | #endif |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 52 | #include <asm/unistd.h> |
Dmitry V. Levin | fadf379 | 2015-02-13 00:26:38 +0000 | [diff] [blame] | 53 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 54 | #include "largefile_wrappers.h" |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 55 | #include "mmap_cache.h" |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 56 | #include "number_set.h" |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 57 | #include "scno.h" |
Dmitry V. Levin | 0e946ab | 2015-07-17 23:56:54 +0000 | [diff] [blame] | 58 | #include "printsiginfo.h" |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 59 | #include "trace_event.h" |
| 60 | #include "xstring.h" |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 61 | #include "delay.h" |
Dmitry V. Levin | fadf379 | 2015-02-13 00:26:38 +0000 | [diff] [blame] | 62 | |
Denys Vlasenko | a50d2a8 | 2012-03-15 12:49:52 +0100 | [diff] [blame] | 63 | /* In some libc, these aren't declared. Do it ourself: */ |
Denys Vlasenko | 96d5a76 | 2008-12-29 19:13:27 +0000 | [diff] [blame] | 64 | extern char **environ; |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 65 | extern int optind; |
| 66 | extern char *optarg; |
Denys Vlasenko | 96d5a76 | 2008-12-29 19:13:27 +0000 | [diff] [blame] | 67 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 68 | #ifdef ENABLE_STACKTRACE |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 69 | /* if this is true do the stack trace for every system call */ |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 70 | bool stack_trace_enabled; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 71 | #endif |
Denys Vlasenko | a50d2a8 | 2012-03-15 12:49:52 +0100 | [diff] [blame] | 72 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 73 | #define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig)) |
Denys Vlasenko | a50d2a8 | 2012-03-15 12:49:52 +0100 | [diff] [blame] | 74 | |
Denys Vlasenko | 5c9d8f4 | 2013-02-19 15:30:12 +0100 | [diff] [blame] | 75 | /* Glue for systems without a MMU that cannot provide fork() */ |
| 76 | #if !defined(HAVE_FORK) |
| 77 | # undef NOMMU_SYSTEM |
| 78 | # define NOMMU_SYSTEM 1 |
| 79 | #endif |
| 80 | #if NOMMU_SYSTEM |
| 81 | # define fork() vfork() |
| 82 | #endif |
| 83 | |
Dmitry V. Levin | 23ce9e4 | 2015-02-08 13:05:53 +0000 | [diff] [blame] | 84 | const unsigned int syscall_trap_sig = SIGTRAP | 0x80; |
| 85 | |
Denys Vlasenko | a50d2a8 | 2012-03-15 12:49:52 +0100 | [diff] [blame] | 86 | cflag_t cflag = CFLAG_NONE; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 87 | unsigned int followfork; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 88 | unsigned int ptrace_setoptions = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC |
| 89 | | PTRACE_O_TRACEEXIT; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 90 | unsigned int xflag; |
| 91 | bool debug_flag; |
| 92 | bool Tflag; |
| 93 | bool iflag; |
| 94 | bool count_wallclock; |
| 95 | unsigned int qflag; |
| 96 | static unsigned int tflag; |
| 97 | static bool rflag; |
| 98 | static bool print_pid_pfx; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 99 | |
| 100 | /* -I n */ |
| 101 | enum { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 102 | INTR_NOT_SET = 0, |
| 103 | INTR_ANYWHERE = 1, /* don't block/ignore any signals */ |
| 104 | INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */ |
| 105 | INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */ |
| 106 | INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */ |
| 107 | NUM_INTR_OPTS |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 108 | }; |
| 109 | static int opt_intr; |
| 110 | /* We play with signal mask only if this mode is active: */ |
| 111 | #define interactive (opt_intr == INTR_WHILE_WAIT) |
| 112 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 113 | /* |
| 114 | * daemonized_tracer supports -D option. |
| 115 | * With this option, strace forks twice. |
| 116 | * Unlike normal case, with -D *grandparent* process exec's, |
| 117 | * becoming a traced process. Child exits (this prevents traced process |
| 118 | * from having children it doesn't expect to have), and grandchild |
| 119 | * attaches to grandparent similarly to strace -p PID. |
| 120 | * This allows for more transparent interaction in cases |
| 121 | * when process and its parent are communicating via signals, |
| 122 | * wait() etc. Without -D, strace process gets lodged in between, |
| 123 | * disrupting parent<->child link. |
| 124 | */ |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 125 | static bool daemonized_tracer; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 126 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 127 | static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 128 | #define use_seize (post_attach_sigstop == 0) |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 129 | |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 130 | /* Sometimes we want to print only succeeding syscalls. */ |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 131 | bool not_failing_only; |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 132 | |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 133 | /* Show path associated with fd arguments */ |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 134 | unsigned int show_fd_path; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 135 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 136 | static bool detach_on_execve; |
Denys Vlasenko | 61e7aad | 2012-03-15 13:44:17 +0100 | [diff] [blame] | 137 | |
Dmitry V. Levin | b9bc216 | 2016-07-29 17:51:54 +0000 | [diff] [blame] | 138 | static int exit_code; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 139 | static int strace_child; |
| 140 | static int strace_tracer_pid; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 141 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 142 | static const char *username; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 143 | static uid_t run_uid; |
| 144 | static gid_t run_gid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 145 | |
Denys Vlasenko | a50d2a8 | 2012-03-15 12:49:52 +0100 | [diff] [blame] | 146 | unsigned int max_strlen = DEFAULT_STRLEN; |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 147 | static int acolumn = DEFAULT_ACOLUMN; |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 148 | static char *acolumn_spaces; |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 149 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 150 | /* Default output style for xlat entities */ |
| 151 | enum xlat_style xlat_verbosity = XLAT_STYLE_ABBREV; |
| 152 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 153 | static const char *outfname; |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 154 | /* If -ff, points to stderr. Else, it's our common output log */ |
| 155 | static FILE *shared_log; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 156 | static bool open_append; |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 157 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 158 | struct tcb *printing_tcp; |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 159 | static struct tcb *current_tcp; |
| 160 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 161 | struct tcb_wait_data { |
| 162 | enum trace_event te; /**< Event passed to dispatch_event() */ |
| 163 | int status; /**< status, returned by wait4() */ |
| 164 | siginfo_t si; /**< siginfo, returned by PTRACE_GETSIGINFO */ |
| 165 | }; |
| 166 | |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 167 | static struct tcb **tcbtab; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 168 | static unsigned int nprocs; |
| 169 | static size_t tcbtabsize; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 170 | |
| 171 | #ifndef HAVE_PROGRAM_INVOCATION_NAME |
| 172 | char *program_invocation_name; |
| 173 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 174 | |
Denys Vlasenko | eec8d5d | 2013-02-14 03:29:48 +0100 | [diff] [blame] | 175 | unsigned os_release; /* generated from uname()'s u.release */ |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 176 | |
Denys Vlasenko | 4a9ba98 | 2013-06-20 11:20:23 +0200 | [diff] [blame] | 177 | static void detach(struct tcb *tcp); |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 178 | static void cleanup(void); |
| 179 | static void interrupt(int sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 180 | |
| 181 | #ifdef HAVE_SIG_ATOMIC_T |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 182 | static volatile sig_atomic_t interrupted, restart_failed; |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 183 | #else |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 184 | static volatile int interrupted, restart_failed; |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 185 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 186 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 187 | static sigset_t timer_set; |
| 188 | static void timer_sighandler(int); |
| 189 | |
Denys Vlasenko | 2c4fb90 | 2012-03-15 17:24:49 +0100 | [diff] [blame] | 190 | #ifndef HAVE_STRERROR |
| 191 | |
| 192 | #if !HAVE_DECL_SYS_ERRLIST |
| 193 | extern int sys_nerr; |
| 194 | extern char *sys_errlist[]; |
Denys Vlasenko | a6d91de | 2012-03-16 12:02:22 +0100 | [diff] [blame] | 195 | #endif |
Denys Vlasenko | 2c4fb90 | 2012-03-15 17:24:49 +0100 | [diff] [blame] | 196 | |
| 197 | const char * |
| 198 | strerror(int err_no) |
| 199 | { |
| 200 | static char buf[sizeof("Unknown error %d") + sizeof(int)*3]; |
| 201 | |
| 202 | if (err_no < 1 || err_no >= sys_nerr) { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 203 | xsprintf(buf, "Unknown error %d", err_no); |
Denys Vlasenko | 2c4fb90 | 2012-03-15 17:24:49 +0100 | [diff] [blame] | 204 | return buf; |
| 205 | } |
| 206 | return sys_errlist[err_no]; |
| 207 | } |
| 208 | |
| 209 | #endif /* HAVE_STERRROR */ |
| 210 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 211 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 212 | print_version(void) |
| 213 | { |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 214 | static const char features[] = |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 215 | #ifdef ENABLE_STACKTRACE |
| 216 | " stack-trace=" USE_UNWINDER |
| 217 | #endif |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 218 | #ifdef USE_DEMANGLE |
| 219 | " stack-demangle" |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 220 | #endif |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 221 | #if SUPPORTED_PERSONALITIES > 1 |
| 222 | # if defined HAVE_M32_MPERS |
| 223 | " m32-mpers" |
| 224 | # else |
| 225 | " no-m32-mpers" |
| 226 | # endif |
| 227 | #endif /* SUPPORTED_PERSONALITIES > 1 */ |
| 228 | #if SUPPORTED_PERSONALITIES > 2 |
| 229 | # if defined HAVE_MX32_MPERS |
| 230 | " mx32-mpers" |
| 231 | # else |
| 232 | " no-mx32-mpers" |
| 233 | # endif |
| 234 | #endif /* SUPPORTED_PERSONALITIES > 2 */ |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 235 | ""; |
| 236 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 237 | printf("%s -- version %s\n" |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 238 | "Copyright (c) 1991-%s The strace developers <%s>.\n" |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 239 | "This is free software; see the source for copying conditions. There is NO\n" |
| 240 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n", |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 241 | PACKAGE_NAME, PACKAGE_VERSION, COPYRIGHT_YEAR, PACKAGE_URL); |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 242 | printf("\nOptional features enabled:%s\n", |
| 243 | features[0] ? features : " (none)"); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void |
Elvira Khabirova | 45a5a62 | 2016-06-13 01:41:33 +0300 | [diff] [blame] | 247 | usage(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 248 | { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 249 | printf("\ |
Elvira Khabirova | 3272d29 | 2015-11-26 06:25:12 +0300 | [diff] [blame] | 250 | usage: strace [-CdffhiqrtttTvVwxxy] [-I n] [-e expr]...\n\ |
Denys Vlasenko | c5ccfa4 | 2012-03-26 13:10:50 +0200 | [diff] [blame] | 251 | [-a column] [-o file] [-s strsize] [-P path]...\n\ |
| 252 | -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\ |
Elvira Khabirova | 3272d29 | 2015-11-26 06:25:12 +0300 | [diff] [blame] | 253 | or: strace -c[dfw] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\ |
Denys Vlasenko | c5ccfa4 | 2012-03-26 13:10:50 +0200 | [diff] [blame] | 254 | -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\ |
Elvira Khabirova | 3272d29 | 2015-11-26 06:25:12 +0300 | [diff] [blame] | 255 | \n\ |
| 256 | Output format:\n\ |
| 257 | -a column alignment COLUMN for printing syscall results (default %d)\n\ |
| 258 | -i print instruction pointer at time of syscall\n\ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 259 | " |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 260 | #ifdef ENABLE_STACKTRACE |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 261 | "\ |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 262 | -k obtain stack trace between each syscall\n\ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 263 | " |
| 264 | #endif |
| 265 | "\ |
Elvira Khabirova | 3272d29 | 2015-11-26 06:25:12 +0300 | [diff] [blame] | 266 | -o file send trace output to FILE instead of stderr\n\ |
| 267 | -q suppress messages about attaching, detaching, etc.\n\ |
| 268 | -r print relative timestamp\n\ |
| 269 | -s strsize limit length of print strings to STRSIZE chars (default %d)\n\ |
| 270 | -t print absolute timestamp\n\ |
| 271 | -tt print absolute timestamp with usecs\n\ |
| 272 | -T print time spent in each syscall\n\ |
| 273 | -x print non-ascii strings in hex\n\ |
| 274 | -xx print all strings in hex\n\ |
| 275 | -y print paths associated with file descriptor arguments\n\ |
Fabien Siron | e6d2b56 | 2016-05-26 15:29:41 +0000 | [diff] [blame] | 276 | -yy print protocol specific information associated with socket file descriptors\n\ |
Elvira Khabirova | 3272d29 | 2015-11-26 06:25:12 +0300 | [diff] [blame] | 277 | \n\ |
| 278 | Statistics:\n\ |
| 279 | -c count time, calls, and errors for each syscall and report summary\n\ |
| 280 | -C like -c but also print regular output\n\ |
| 281 | -O overhead set overhead for tracing syscalls to OVERHEAD usecs\n\ |
| 282 | -S sortby sort syscall counts by: time, calls, name, nothing (default %s)\n\ |
| 283 | -w summarise syscall latency (default is system time)\n\ |
| 284 | \n\ |
| 285 | Filtering:\n\ |
| 286 | -e expr a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\ |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 287 | options: trace, abbrev, verbose, raw, signal, read, write, fault, inject, kvm" |
| 288 | "\n\ |
Elvira Khabirova | 3272d29 | 2015-11-26 06:25:12 +0300 | [diff] [blame] | 289 | -P path trace accesses to path\n\ |
| 290 | \n\ |
| 291 | Tracing:\n\ |
| 292 | -b execve detach on execve syscall\n\ |
| 293 | -D run tracer process as a detached grandchild, not as parent\n\ |
| 294 | -f follow forks\n\ |
| 295 | -ff follow forks with output into separate files\n\ |
| 296 | -I interruptible\n\ |
| 297 | 1: no signals are blocked\n\ |
| 298 | 2: fatal signals are blocked while decoding syscall (default)\n\ |
| 299 | 3: fatal signals are always blocked (default if '-o FILE PROG')\n\ |
| 300 | 4: fatal signals and SIGTSTP (^Z) are always blocked\n\ |
| 301 | (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\ |
| 302 | \n\ |
| 303 | Startup:\n\ |
| 304 | -E var remove var from the environment for command\n\ |
| 305 | -E var=val put var=val in the environment for command\n\ |
| 306 | -p pid trace process with process id PID, may be repeated\n\ |
| 307 | -u username run command as username handling setuid and/or setgid\n\ |
| 308 | \n\ |
| 309 | Miscellaneous:\n\ |
| 310 | -d enable debug output to stderr\n\ |
| 311 | -v verbose mode: print unabbreviated argv, stat, termios, etc. args\n\ |
| 312 | -h print help message\n\ |
| 313 | -V print version\n\ |
Denys Vlasenko | 61e7aad | 2012-03-15 13:44:17 +0100 | [diff] [blame] | 314 | " |
Denys Vlasenko | 38e79bb | 2013-02-26 11:33:54 +0100 | [diff] [blame] | 315 | /* ancient, no one should use it |
| 316 | -F -- attempt to follow vforks (deprecated, use -f)\n\ |
| 317 | */ |
Denys Vlasenko | 61e7aad | 2012-03-15 13:44:17 +0100 | [diff] [blame] | 318 | /* this is broken, so don't document it |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 319 | -z -- print only succeeding syscalls\n\ |
Denys Vlasenko | 61e7aad | 2012-03-15 13:44:17 +0100 | [diff] [blame] | 320 | */ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 321 | , DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY); |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 322 | exit(0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 325 | void ATTRIBUTE_NORETURN |
Dmitry V. Levin | 5647cf8 | 2015-03-29 22:45:03 +0000 | [diff] [blame] | 326 | die(void) |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 327 | { |
| 328 | if (strace_tracer_pid == getpid()) { |
| 329 | cflag = 0; |
| 330 | cleanup(); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 331 | exit(1); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 332 | } |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 333 | |
| 334 | _exit(1); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 337 | static void |
| 338 | error_opt_arg(int opt, const char *arg) |
| 339 | { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 340 | error_msg_and_help("invalid -%c argument: '%s'", opt, arg); |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Dmitry V. Levin | c925151 | 2016-07-25 17:12:42 +0000 | [diff] [blame] | 343 | static const char *ptrace_attach_cmd; |
| 344 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 345 | static int |
| 346 | ptrace_attach_or_seize(int pid) |
| 347 | { |
| 348 | int r; |
| 349 | if (!use_seize) |
Dmitry V. Levin | c925151 | 2016-07-25 17:12:42 +0000 | [diff] [blame] | 350 | return ptrace_attach_cmd = "PTRACE_ATTACH", |
| 351 | ptrace(PTRACE_ATTACH, pid, 0L, 0L); |
Dmitry V. Levin | 23ce9e4 | 2015-02-08 13:05:53 +0000 | [diff] [blame] | 352 | r = ptrace(PTRACE_SEIZE, pid, 0L, (unsigned long) ptrace_setoptions); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 353 | if (r) |
Dmitry V. Levin | c925151 | 2016-07-25 17:12:42 +0000 | [diff] [blame] | 354 | return ptrace_attach_cmd = "PTRACE_SEIZE", r; |
Denys Vlasenko | c169d94 | 2013-07-10 14:33:05 +0200 | [diff] [blame] | 355 | r = ptrace(PTRACE_INTERRUPT, pid, 0L, 0L); |
Dmitry V. Levin | c925151 | 2016-07-25 17:12:42 +0000 | [diff] [blame] | 356 | return ptrace_attach_cmd = "PTRACE_INTERRUPT", r; |
Dmitry V. Levin | c925151 | 2016-07-25 17:12:42 +0000 | [diff] [blame] | 357 | } |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 358 | |
Denys Vlasenko | 852f98a | 2012-03-20 16:26:25 +0100 | [diff] [blame] | 359 | /* |
| 360 | * Used when we want to unblock stopped traced process. |
| 361 | * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL. |
| 362 | * Returns 0 on success or if error was ESRCH |
| 363 | * (presumably process was killed while we talk to it). |
| 364 | * Otherwise prints error message and returns -1. |
| 365 | */ |
| 366 | static int |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 367 | ptrace_restart(const unsigned int op, struct tcb *const tcp, unsigned int sig) |
Denys Vlasenko | 852f98a | 2012-03-20 16:26:25 +0100 | [diff] [blame] | 368 | { |
| 369 | int err; |
| 370 | const char *msg; |
| 371 | |
| 372 | errno = 0; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 373 | ptrace(op, tcp->pid, 0L, (unsigned long) sig); |
Denys Vlasenko | 852f98a | 2012-03-20 16:26:25 +0100 | [diff] [blame] | 374 | err = errno; |
Denys Vlasenko | 2350675 | 2012-03-21 10:32:49 +0100 | [diff] [blame] | 375 | if (!err) |
Denys Vlasenko | 852f98a | 2012-03-20 16:26:25 +0100 | [diff] [blame] | 376 | return 0; |
| 377 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 378 | switch (op) { |
| 379 | case PTRACE_CONT: |
| 380 | msg = "CONT"; |
| 381 | break; |
| 382 | case PTRACE_DETACH: |
| 383 | msg = "DETACH"; |
| 384 | break; |
| 385 | case PTRACE_LISTEN: |
| 386 | msg = "LISTEN"; |
| 387 | break; |
| 388 | default: |
| 389 | msg = "SYSCALL"; |
| 390 | } |
| 391 | |
Denys Vlasenko | 2350675 | 2012-03-21 10:32:49 +0100 | [diff] [blame] | 392 | /* |
| 393 | * Why curcol != 0? Otherwise sometimes we get this: |
| 394 | * |
| 395 | * 10252 kill(10253, SIGKILL) = 0 |
| 396 | * <ptrace(SYSCALL,10252):No such process>10253 ...next decode... |
| 397 | * |
| 398 | * 10252 died after we retrieved syscall exit data, |
| 399 | * but before we tried to restart it. Log looks ugly. |
| 400 | */ |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 401 | if (current_tcp && current_tcp->curcol != 0) { |
Denys Vlasenko | 2350675 | 2012-03-21 10:32:49 +0100 | [diff] [blame] | 402 | tprintf(" <ptrace(%s):%s>\n", msg, strerror(err)); |
| 403 | line_ended(); |
| 404 | } |
| 405 | if (err == ESRCH) |
| 406 | return 0; |
| 407 | errno = err; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 408 | perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%u)", msg, tcp->pid, sig); |
Denys Vlasenko | 852f98a | 2012-03-20 16:26:25 +0100 | [diff] [blame] | 409 | return -1; |
| 410 | } |
| 411 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 412 | static void |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 413 | set_cloexec_flag(int fd) |
| 414 | { |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 415 | int flags, newflags; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 416 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 417 | flags = fcntl(fd, F_GETFD); |
| 418 | if (flags < 0) { |
| 419 | /* Can happen only if fd is bad. |
| 420 | * Should never happen: if it does, we have a bug |
| 421 | * in the caller. Therefore we just abort |
| 422 | * instead of propagating the error. |
| 423 | */ |
| 424 | perror_msg_and_die("fcntl(%d, F_GETFD)", fd); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | newflags = flags | FD_CLOEXEC; |
| 428 | if (flags == newflags) |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 429 | return; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 430 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 431 | if (fcntl(fd, F_SETFD, newflags)) /* never fails */ |
| 432 | perror_msg_and_die("fcntl(%d, F_SETFD, %#x)", fd, newflags); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Elvira Khabirova | 7429dca | 2016-06-13 01:43:15 +0300 | [diff] [blame] | 435 | static void |
| 436 | kill_save_errno(pid_t pid, int sig) |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 437 | { |
| 438 | int saved_errno = errno; |
| 439 | |
| 440 | (void) kill(pid, sig); |
| 441 | errno = saved_errno; |
| 442 | } |
| 443 | |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 444 | /* |
| 445 | * When strace is setuid executable, we have to swap uids |
| 446 | * before and after filesystem and process management operations. |
| 447 | */ |
| 448 | static void |
| 449 | swap_uid(void) |
| 450 | { |
| 451 | int euid = geteuid(), uid = getuid(); |
| 452 | |
| 453 | if (euid != uid && setreuid(euid, uid) < 0) { |
| 454 | perror_msg_and_die("setreuid"); |
| 455 | } |
| 456 | } |
| 457 | |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 458 | static FILE * |
| 459 | strace_fopen(const char *path) |
| 460 | { |
| 461 | FILE *fp; |
| 462 | |
| 463 | swap_uid(); |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 464 | fp = fopen_stream(path, open_append ? "a" : "w"); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 465 | if (!fp) |
| 466 | perror_msg_and_die("Can't fopen '%s'", path); |
| 467 | swap_uid(); |
| 468 | set_cloexec_flag(fileno(fp)); |
| 469 | return fp; |
| 470 | } |
| 471 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 472 | static int popen_pid; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 473 | |
| 474 | #ifndef _PATH_BSHELL |
| 475 | # define _PATH_BSHELL "/bin/sh" |
| 476 | #endif |
| 477 | |
| 478 | /* |
| 479 | * We cannot use standard popen(3) here because we have to distinguish |
| 480 | * popen child process from other processes we trace, and standard popen(3) |
| 481 | * does not export its child's pid. |
| 482 | */ |
| 483 | static FILE * |
| 484 | strace_popen(const char *command) |
| 485 | { |
| 486 | FILE *fp; |
Denys Vlasenko | 519af5a | 2013-07-02 11:31:24 +0200 | [diff] [blame] | 487 | int pid; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 488 | int fds[2]; |
| 489 | |
| 490 | swap_uid(); |
| 491 | if (pipe(fds) < 0) |
| 492 | perror_msg_and_die("pipe"); |
| 493 | |
| 494 | set_cloexec_flag(fds[1]); /* never fails */ |
| 495 | |
Denys Vlasenko | 519af5a | 2013-07-02 11:31:24 +0200 | [diff] [blame] | 496 | pid = vfork(); |
| 497 | if (pid < 0) |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 498 | perror_msg_and_die("vfork"); |
| 499 | |
Denys Vlasenko | 519af5a | 2013-07-02 11:31:24 +0200 | [diff] [blame] | 500 | if (pid == 0) { |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 501 | /* child */ |
| 502 | close(fds[1]); |
| 503 | if (fds[0] != 0) { |
| 504 | if (dup2(fds[0], 0)) |
| 505 | perror_msg_and_die("dup2"); |
| 506 | close(fds[0]); |
| 507 | } |
| 508 | execl(_PATH_BSHELL, "sh", "-c", command, NULL); |
| 509 | perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL); |
| 510 | } |
| 511 | |
| 512 | /* parent */ |
Denys Vlasenko | 519af5a | 2013-07-02 11:31:24 +0200 | [diff] [blame] | 513 | popen_pid = pid; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 514 | close(fds[0]); |
| 515 | swap_uid(); |
| 516 | fp = fdopen(fds[1], "w"); |
| 517 | if (!fp) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 518 | perror_msg_and_die("fdopen"); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 519 | return fp; |
| 520 | } |
| 521 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 522 | static void |
| 523 | outf_perror(const struct tcb * const tcp) |
| 524 | { |
| 525 | if (tcp->outf == stderr) |
| 526 | return; |
| 527 | |
| 528 | /* This is ugly, but we don't store separate file names */ |
| 529 | if (followfork >= 2) |
| 530 | perror_msg("%s.%u", outfname, tcp->pid); |
| 531 | else |
| 532 | perror_msg("%s", outfname); |
| 533 | } |
| 534 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 535 | ATTRIBUTE_FORMAT((printf, 1, 0)) |
| 536 | static void |
| 537 | tvprintf(const char *const fmt, va_list args) |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 538 | { |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 539 | if (current_tcp) { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 540 | int n = vfprintf(current_tcp->outf, fmt, args); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 541 | if (n < 0) { |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 542 | /* very unlikely due to vfprintf buffering */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 543 | outf_perror(current_tcp); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 544 | } else |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 545 | current_tcp->curcol += n; |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 546 | } |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | void |
| 550 | tprintf(const char *fmt, ...) |
| 551 | { |
| 552 | va_list args; |
| 553 | va_start(args, fmt); |
| 554 | tvprintf(fmt, args); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 555 | va_end(args); |
| 556 | } |
| 557 | |
Dmitry V. Levin | d354130 | 2014-02-26 00:01:00 +0000 | [diff] [blame] | 558 | #ifndef HAVE_FPUTS_UNLOCKED |
| 559 | # define fputs_unlocked fputs |
| 560 | #endif |
| 561 | |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 562 | void |
| 563 | tprints(const char *str) |
| 564 | { |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 565 | if (current_tcp) { |
Denys Vlasenko | 142aee0 | 2012-04-16 18:10:15 +0200 | [diff] [blame] | 566 | int n = fputs_unlocked(str, current_tcp->outf); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 567 | if (n >= 0) { |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 568 | current_tcp->curcol += strlen(str); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 569 | return; |
| 570 | } |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 571 | /* very unlikely due to fputs_unlocked buffering */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 572 | outf_perror(current_tcp); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | void |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 577 | tprints_comment(const char *const str) |
| 578 | { |
| 579 | if (str && *str) |
| 580 | tprintf(" /* %s */", str); |
| 581 | } |
| 582 | |
| 583 | void |
| 584 | tprintf_comment(const char *fmt, ...) |
| 585 | { |
| 586 | if (!fmt || !*fmt) |
| 587 | return; |
| 588 | |
| 589 | va_list args; |
| 590 | va_start(args, fmt); |
| 591 | tprints(" /* "); |
| 592 | tvprintf(fmt, args); |
| 593 | tprints(" */"); |
| 594 | va_end(args); |
| 595 | } |
| 596 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 597 | static void |
| 598 | flush_tcp_output(const struct tcb *const tcp) |
| 599 | { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 600 | if (fflush(tcp->outf)) |
| 601 | outf_perror(tcp); |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 604 | void |
Denys Vlasenko | 7de265d | 2012-03-13 11:44:31 +0100 | [diff] [blame] | 605 | line_ended(void) |
| 606 | { |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 607 | if (current_tcp) { |
| 608 | current_tcp->curcol = 0; |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 609 | flush_tcp_output(current_tcp); |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 610 | } |
| 611 | if (printing_tcp) { |
| 612 | printing_tcp->curcol = 0; |
| 613 | printing_tcp = NULL; |
| 614 | } |
Denys Vlasenko | 7de265d | 2012-03-13 11:44:31 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | void |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 618 | set_current_tcp(const struct tcb *tcp) |
| 619 | { |
| 620 | current_tcp = (struct tcb *) tcp; |
| 621 | |
| 622 | /* Sync current_personality and stuff */ |
| 623 | if (current_tcp) |
| 624 | set_personality(current_tcp->currpers); |
| 625 | } |
| 626 | |
| 627 | void |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 628 | printleader(struct tcb *tcp) |
| 629 | { |
Denys Vlasenko | 7de265d | 2012-03-13 11:44:31 +0100 | [diff] [blame] | 630 | /* If -ff, "previous tcb we printed" is always the same as current, |
| 631 | * because we have per-tcb output files. |
| 632 | */ |
| 633 | if (followfork >= 2) |
| 634 | printing_tcp = tcp; |
| 635 | |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 636 | if (printing_tcp) { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 637 | set_current_tcp(printing_tcp); |
Denys Vlasenko | 7de265d | 2012-03-13 11:44:31 +0100 | [diff] [blame] | 638 | if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) { |
| 639 | /* |
| 640 | * case 1: we have a shared log (i.e. not -ff), and last line |
| 641 | * wasn't finished (same or different tcb, doesn't matter). |
| 642 | * case 2: split log, we are the same tcb, but our last line |
| 643 | * didn't finish ("SIGKILL nuked us after syscall entry" etc). |
| 644 | */ |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 645 | tprints(" <unfinished ...>\n"); |
Denys Vlasenko | 7de265d | 2012-03-13 11:44:31 +0100 | [diff] [blame] | 646 | printing_tcp->curcol = 0; |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | printing_tcp = tcp; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 651 | set_current_tcp(tcp); |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 652 | current_tcp->curcol = 0; |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 653 | |
| 654 | if (print_pid_pfx) |
| 655 | tprintf("%-5d ", tcp->pid); |
| 656 | else if (nprocs > 1 && !outfname) |
| 657 | tprintf("[pid %5u] ", tcp->pid); |
| 658 | |
| 659 | if (tflag) { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 660 | struct timespec ts; |
| 661 | clock_gettime(CLOCK_REALTIME, &ts); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 662 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 663 | if (tflag > 2) { |
| 664 | tprintf("%lld.%06ld ", |
| 665 | (long long) ts.tv_sec, (long) ts.tv_nsec / 1000); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 666 | } else { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 667 | time_t local = ts.tv_sec; |
| 668 | char str[MAX(sizeof("HH:MM:SS"), sizeof(ts.tv_sec) * 3)]; |
| 669 | struct tm *tm = localtime(&local); |
| 670 | |
| 671 | if (tm) |
| 672 | strftime(str, sizeof(str), "%T", tm); |
| 673 | else |
| 674 | xsprintf(str, "%lld", (long long) local); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 675 | if (tflag > 1) |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 676 | tprintf("%s.%06ld ", |
| 677 | str, (long) ts.tv_nsec / 1000); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 678 | else |
| 679 | tprintf("%s ", str); |
| 680 | } |
| 681 | } |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 682 | |
| 683 | if (rflag) { |
| 684 | struct timespec ts; |
| 685 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 686 | |
| 687 | static struct timespec ots; |
| 688 | if (ots.tv_sec == 0) |
| 689 | ots = ts; |
| 690 | |
| 691 | struct timespec dts; |
| 692 | ts_sub(&dts, &ts, &ots); |
| 693 | ots = ts; |
| 694 | |
| 695 | tprintf("%s%6ld.%06ld%s ", |
| 696 | tflag ? "(+" : "", |
| 697 | (long) dts.tv_sec, (long) dts.tv_nsec / 1000, |
| 698 | tflag ? ")" : ""); |
| 699 | } |
| 700 | |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 701 | if (iflag) |
Denys Vlasenko | 5a2483b | 2013-07-01 12:49:14 +0200 | [diff] [blame] | 702 | print_pc(tcp); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void |
| 706 | tabto(void) |
| 707 | { |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 708 | if (current_tcp->curcol < acolumn) |
| 709 | tprints(acolumn_spaces + current_tcp->curcol); |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 710 | } |
| 711 | |
Denys Vlasenko | 3db3b26 | 2012-03-16 15:15:14 +0100 | [diff] [blame] | 712 | /* Should be only called directly *after successful attach* to a tracee. |
| 713 | * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>" |
| 714 | * may create bogus empty FILE.<nonexistant_pid>, and then die. |
| 715 | */ |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 716 | static void |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 717 | after_successful_attach(struct tcb *tcp, const unsigned int flags) |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 718 | { |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 719 | tcp->flags |= TCB_ATTACHED | TCB_STARTUP | flags; |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 720 | tcp->outf = shared_log; /* if not -ff mode, the same file is for all */ |
Denys Vlasenko | 3db3b26 | 2012-03-16 15:15:14 +0100 | [diff] [blame] | 721 | if (followfork >= 2) { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 722 | char name[PATH_MAX]; |
| 723 | xsprintf(name, "%s.%u", outfname, tcp->pid); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 724 | tcp->outf = strace_fopen(name); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 725 | } |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 726 | |
| 727 | #ifdef ENABLE_STACKTRACE |
| 728 | if (stack_trace_enabled) |
| 729 | unwind_tcb_init(tcp); |
| 730 | #endif |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Denys Vlasenko | 558e512 | 2012-03-12 23:32:16 +0100 | [diff] [blame] | 733 | static void |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 734 | expand_tcbtab(void) |
| 735 | { |
Nahim El Atmani | c811fa6 | 2016-03-05 13:35:57 +0100 | [diff] [blame] | 736 | /* Allocate some (more) TCBs (and expand the table). |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 737 | We don't want to relocate the TCBs because our |
| 738 | callers have pointers and it would be a pain. |
| 739 | So tcbtab is a table of pointers. Since we never |
| 740 | free the TCBs, we allocate a single chunk of many. */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 741 | size_t old_tcbtabsize; |
Nahim El Atmani | c811fa6 | 2016-03-05 13:35:57 +0100 | [diff] [blame] | 742 | struct tcb *newtcbs; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 743 | struct tcb **tcb_ptr; |
Nahim El Atmani | c811fa6 | 2016-03-05 13:35:57 +0100 | [diff] [blame] | 744 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 745 | old_tcbtabsize = tcbtabsize; |
Nahim El Atmani | c811fa6 | 2016-03-05 13:35:57 +0100 | [diff] [blame] | 746 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 747 | tcbtab = xgrowarray(tcbtab, &tcbtabsize, sizeof(tcbtab[0])); |
| 748 | newtcbs = xcalloc(tcbtabsize - old_tcbtabsize, sizeof(newtcbs[0])); |
| 749 | |
| 750 | for (tcb_ptr = tcbtab + old_tcbtabsize; |
| 751 | tcb_ptr < tcbtab + tcbtabsize; tcb_ptr++, newtcbs++) |
| 752 | *tcb_ptr = newtcbs; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | static struct tcb * |
Denys Vlasenko | 3db3b26 | 2012-03-16 15:15:14 +0100 | [diff] [blame] | 756 | alloctcb(int pid) |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 757 | { |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 758 | unsigned int i; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 759 | struct tcb *tcp; |
| 760 | |
| 761 | if (nprocs == tcbtabsize) |
| 762 | expand_tcbtab(); |
| 763 | |
| 764 | for (i = 0; i < tcbtabsize; i++) { |
| 765 | tcp = tcbtab[i]; |
Denys Vlasenko | fadbf66 | 2013-06-26 14:14:29 +0200 | [diff] [blame] | 766 | if (!tcp->pid) { |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 767 | memset(tcp, 0, sizeof(*tcp)); |
| 768 | tcp->pid = pid; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 769 | #if SUPPORTED_PERSONALITIES > 1 |
| 770 | tcp->currpers = current_personality; |
| 771 | #endif |
| 772 | nprocs++; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 773 | debug_msg("new tcb for pid %d, active tcbs:%d", |
| 774 | tcp->pid, nprocs); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 775 | return tcp; |
| 776 | } |
| 777 | } |
Denys Vlasenko | 3db3b26 | 2012-03-16 15:15:14 +0100 | [diff] [blame] | 778 | error_msg_and_die("bug in alloctcb"); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 779 | } |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 780 | |
Patrik Jakobsson | a1546a9 | 2015-08-24 14:42:47 +0200 | [diff] [blame] | 781 | void * |
| 782 | get_tcb_priv_data(const struct tcb *tcp) |
| 783 | { |
| 784 | return tcp->_priv_data; |
| 785 | } |
| 786 | |
| 787 | int |
| 788 | set_tcb_priv_data(struct tcb *tcp, void *const priv_data, |
| 789 | void (*const free_priv_data)(void *)) |
| 790 | { |
| 791 | if (tcp->_priv_data) |
| 792 | return -1; |
| 793 | |
| 794 | tcp->_free_priv_data = free_priv_data; |
| 795 | tcp->_priv_data = priv_data; |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | void |
| 801 | free_tcb_priv_data(struct tcb *tcp) |
| 802 | { |
| 803 | if (tcp->_priv_data) { |
| 804 | if (tcp->_free_priv_data) { |
| 805 | tcp->_free_priv_data(tcp->_priv_data); |
| 806 | tcp->_free_priv_data = NULL; |
| 807 | } |
| 808 | tcp->_priv_data = NULL; |
| 809 | } |
| 810 | } |
| 811 | |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 812 | static void |
| 813 | droptcb(struct tcb *tcp) |
| 814 | { |
| 815 | if (tcp->pid == 0) |
| 816 | return; |
| 817 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 818 | int p; |
| 819 | for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) |
| 820 | free(tcp->inject_vec[p]); |
| 821 | |
Patrik Jakobsson | a1546a9 | 2015-08-24 14:42:47 +0200 | [diff] [blame] | 822 | free_tcb_priv_data(tcp); |
| 823 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 824 | #ifdef ENABLE_STACKTRACE |
| 825 | if (stack_trace_enabled) |
Masatake YAMATO | 2b09df9 | 2014-04-16 15:33:08 +0900 | [diff] [blame] | 826 | unwind_tcb_fin(tcp); |
Masatake YAMATO | 2b09df9 | 2014-04-16 15:33:08 +0900 | [diff] [blame] | 827 | #endif |
| 828 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 829 | #ifdef HAVE_LINUX_KVM_H |
| 830 | kvm_vcpu_info_free(tcp); |
| 831 | #endif |
| 832 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 833 | if (tcp->mmap_cache) |
| 834 | tcp->mmap_cache->free_fn(tcp, __func__); |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 835 | |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 836 | nprocs--; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 837 | debug_msg("dropped tcb for pid %d, %d remain", tcp->pid, nprocs); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 838 | |
| 839 | if (tcp->outf) { |
Denys Vlasenko | 8511f2a | 2012-03-22 09:35:51 +0100 | [diff] [blame] | 840 | if (followfork >= 2) { |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 841 | if (tcp->curcol != 0) |
| 842 | fprintf(tcp->outf, " <detached ...>\n"); |
| 843 | fclose(tcp->outf); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 844 | } else { |
| 845 | if (printing_tcp == tcp && tcp->curcol != 0) |
| 846 | fprintf(tcp->outf, " <detached ...>\n"); |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 847 | flush_tcp_output(tcp); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 851 | if (current_tcp == tcp) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 852 | set_current_tcp(NULL); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 853 | if (printing_tcp == tcp) |
| 854 | printing_tcp = NULL; |
| 855 | |
| 856 | memset(tcp, 0, sizeof(*tcp)); |
| 857 | } |
| 858 | |
Denys Vlasenko | f1669e7 | 2013-06-18 18:09:39 +0200 | [diff] [blame] | 859 | /* Detach traced process. |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 860 | * Never call DETACH twice on the same process as both unattached and |
| 861 | * attached-unstopped processes give the same ESRCH. For unattached process we |
| 862 | * would SIGSTOP it and wait for its SIGSTOP notification forever. |
| 863 | */ |
Denys Vlasenko | 4a9ba98 | 2013-06-20 11:20:23 +0200 | [diff] [blame] | 864 | static void |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 865 | detach(struct tcb *tcp) |
| 866 | { |
| 867 | int error; |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 868 | int status; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 869 | |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 870 | /* |
| 871 | * Linux wrongly insists the child be stopped |
| 872 | * before detaching. Arghh. We go through hoops |
| 873 | * to make a clean break of things. |
| 874 | */ |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 875 | |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 876 | if (!(tcp->flags & TCB_ATTACHED)) |
| 877 | goto drop; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 878 | |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 879 | /* We attached but possibly didn't see the expected SIGSTOP. |
| 880 | * We must catch exactly one as otherwise the detached process |
| 881 | * would be left stopped (process state T). |
| 882 | */ |
| 883 | if (tcp->flags & TCB_IGNORE_ONE_SIGSTOP) |
| 884 | goto wait_loop; |
| 885 | |
| 886 | error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0); |
| 887 | if (!error) { |
| 888 | /* On a clear day, you can see forever. */ |
| 889 | goto drop; |
| 890 | } |
| 891 | if (errno != ESRCH) { |
| 892 | /* Shouldn't happen. */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 893 | perror_func_msg("ptrace(PTRACE_DETACH,%u)", tcp->pid); |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 894 | goto drop; |
| 895 | } |
| 896 | /* ESRCH: process is either not stopped or doesn't exist. */ |
| 897 | if (my_tkill(tcp->pid, 0) < 0) { |
| 898 | if (errno != ESRCH) |
| 899 | /* Shouldn't happen. */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 900 | perror_func_msg("tkill(%u,0)", tcp->pid); |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 901 | /* else: process doesn't exist. */ |
| 902 | goto drop; |
| 903 | } |
| 904 | /* Process is not stopped, need to stop it. */ |
| 905 | if (use_seize) { |
| 906 | /* |
| 907 | * With SEIZE, tracee can be in group-stop already. |
| 908 | * In this state sending it another SIGSTOP does nothing. |
| 909 | * Need to use INTERRUPT. |
| 910 | * Testcase: trying to ^C a "strace -p <stopped_process>". |
| 911 | */ |
| 912 | error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0); |
| 913 | if (!error) |
| 914 | goto wait_loop; |
| 915 | if (errno != ESRCH) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 916 | perror_func_msg("ptrace(PTRACE_INTERRUPT,%u)", tcp->pid); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 917 | } else { |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 918 | error = my_tkill(tcp->pid, SIGSTOP); |
| 919 | if (!error) |
| 920 | goto wait_loop; |
| 921 | if (errno != ESRCH) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 922 | perror_func_msg("tkill(%u,SIGSTOP)", tcp->pid); |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 923 | } |
| 924 | /* Either process doesn't exist, or some weird error. */ |
| 925 | goto drop; |
| 926 | |
Denys Vlasenko | a2de9da | 2013-06-21 15:50:41 +0200 | [diff] [blame] | 927 | wait_loop: |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 928 | /* We end up here in three cases: |
| 929 | * 1. We sent PTRACE_INTERRUPT (use_seize case) |
| 930 | * 2. We sent SIGSTOP (!use_seize) |
| 931 | * 3. Attach SIGSTOP was already pending (TCB_IGNORE_ONE_SIGSTOP set) |
| 932 | */ |
| 933 | for (;;) { |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 934 | unsigned int sig; |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 935 | if (waitpid(tcp->pid, &status, __WALL) < 0) { |
| 936 | if (errno == EINTR) |
| 937 | continue; |
| 938 | /* |
| 939 | * if (errno == ECHILD) break; |
| 940 | * ^^^ WRONG! We expect this PID to exist, |
| 941 | * and want to emit a message otherwise: |
| 942 | */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 943 | perror_func_msg("waitpid(%u)", tcp->pid); |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 944 | break; |
| 945 | } |
| 946 | if (!WIFSTOPPED(status)) { |
| 947 | /* |
| 948 | * Tracee exited or was killed by signal. |
| 949 | * We shouldn't normally reach this place: |
| 950 | * we don't want to consume exit status. |
| 951 | * Consider "strace -p PID" being ^C-ed: |
| 952 | * we want merely to detach from PID. |
| 953 | * |
| 954 | * However, we _can_ end up here if tracee |
| 955 | * was SIGKILLed. |
| 956 | */ |
| 957 | break; |
| 958 | } |
| 959 | sig = WSTOPSIG(status); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 960 | debug_msg("detach wait: event:%d sig:%d", |
| 961 | (unsigned) status >> 16, sig); |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 962 | if (use_seize) { |
| 963 | unsigned event = (unsigned)status >> 16; |
| 964 | if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) { |
Denys Vlasenko | fdfa47a | 2013-06-20 12:10:21 +0200 | [diff] [blame] | 965 | /* |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 966 | * sig == SIGTRAP: PTRACE_INTERRUPT stop. |
| 967 | * sig == other: process was already stopped |
| 968 | * with this stopping sig (see tests/detach-stopped). |
| 969 | * Looks like re-injecting this sig is not necessary |
| 970 | * in DETACH for the tracee to remain stopped. |
Denys Vlasenko | fdfa47a | 2013-06-20 12:10:21 +0200 | [diff] [blame] | 971 | */ |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 972 | sig = 0; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 973 | } |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 974 | /* |
| 975 | * PTRACE_INTERRUPT is not guaranteed to produce |
| 976 | * the above event if other ptrace-stop is pending. |
| 977 | * See tests/detach-sleeping testcase: |
| 978 | * strace got SIGINT while tracee is sleeping. |
| 979 | * We sent PTRACE_INTERRUPT. |
| 980 | * We see syscall exit, not PTRACE_INTERRUPT stop. |
| 981 | * We won't get PTRACE_INTERRUPT stop |
| 982 | * if we would CONT now. Need to DETACH. |
| 983 | */ |
Denys Vlasenko | 69e27ef | 2013-06-19 15:31:39 +0200 | [diff] [blame] | 984 | if (sig == syscall_trap_sig) |
| 985 | sig = 0; |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 986 | /* else: not sure in which case we can be here. |
| 987 | * Signal stop? Inject it while detaching. |
| 988 | */ |
| 989 | ptrace_restart(PTRACE_DETACH, tcp, sig); |
| 990 | break; |
| 991 | } |
| 992 | /* Note: this check has to be after use_seize check */ |
| 993 | /* (else, in use_seize case SIGSTOP will be mistreated) */ |
| 994 | if (sig == SIGSTOP) { |
| 995 | /* Detach, suppressing SIGSTOP */ |
| 996 | ptrace_restart(PTRACE_DETACH, tcp, 0); |
| 997 | break; |
| 998 | } |
| 999 | if (sig == syscall_trap_sig) |
| 1000 | sig = 0; |
| 1001 | /* Can't detach just yet, may need to wait for SIGSTOP */ |
| 1002 | error = ptrace_restart(PTRACE_CONT, tcp, sig); |
| 1003 | if (error < 0) { |
| 1004 | /* Should not happen. |
| 1005 | * Note: ptrace_restart returns 0 on ESRCH, so it's not it. |
| 1006 | * ptrace_restart already emitted error message. |
| 1007 | */ |
| 1008 | break; |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Denys Vlasenko | e2567d5 | 2013-06-21 16:11:10 +0200 | [diff] [blame] | 1012 | drop: |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 1013 | if (!qflag && (tcp->flags & TCB_ATTACHED)) |
Dmitry V. Levin | 6c8ef05 | 2015-05-25 22:51:19 +0000 | [diff] [blame] | 1014 | error_msg("Process %u detached", tcp->pid); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 1015 | |
| 1016 | droptcb(tcp); |
Denys Vlasenko | 800ec8f | 2012-03-16 15:11:34 +0100 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | static void |
Denys Vlasenko | 558e512 | 2012-03-12 23:32:16 +0100 | [diff] [blame] | 1020 | process_opt_p_list(char *opt) |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1021 | { |
| 1022 | while (*opt) { |
| 1023 | /* |
| 1024 | * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`". |
| 1025 | * pidof uses space as delim, pgrep uses newline. :( |
| 1026 | */ |
| 1027 | int pid; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1028 | char *delim = opt + strcspn(opt, "\n\t ,"); |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1029 | char c = *delim; |
| 1030 | |
| 1031 | *delim = '\0'; |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 1032 | pid = string_to_uint(opt); |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1033 | if (pid <= 0) { |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 1034 | error_msg_and_die("Invalid process id: '%s'", opt); |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1035 | } |
| 1036 | if (pid == strace_tracer_pid) { |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 1037 | error_msg_and_die("I'm sorry, I can't let you do that, Dave."); |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1038 | } |
| 1039 | *delim = c; |
Denys Vlasenko | 3db3b26 | 2012-03-16 15:15:14 +0100 | [diff] [blame] | 1040 | alloctcb(pid); |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1041 | if (c == '\0') |
| 1042 | break; |
| 1043 | opt = delim + 1; |
| 1044 | } |
| 1045 | } |
| 1046 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1047 | static void |
Dmitry V. Levin | 795795a | 2016-07-25 16:25:12 +0000 | [diff] [blame] | 1048 | attach_tcb(struct tcb *const tcp) |
| 1049 | { |
Dmitry V. Levin | 795795a | 2016-07-25 16:25:12 +0000 | [diff] [blame] | 1050 | if (ptrace_attach_or_seize(tcp->pid) < 0) { |
Dmitry V. Levin | c925151 | 2016-07-25 17:12:42 +0000 | [diff] [blame] | 1051 | perror_msg("attach: ptrace(%s, %d)", |
| 1052 | ptrace_attach_cmd, tcp->pid); |
Dmitry V. Levin | 795795a | 2016-07-25 16:25:12 +0000 | [diff] [blame] | 1053 | droptcb(tcp); |
| 1054 | return; |
| 1055 | } |
| 1056 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1057 | after_successful_attach(tcp, TCB_GRABBED | post_attach_sigstop); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1058 | debug_msg("attach to pid %d (main) succeeded", tcp->pid); |
Dmitry V. Levin | 795795a | 2016-07-25 16:25:12 +0000 | [diff] [blame] | 1059 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1060 | static const char task_path[] = "/proc/%d/task"; |
| 1061 | char procdir[sizeof(task_path) + sizeof(int) * 3]; |
Dmitry V. Levin | 2ddb73d | 2016-07-25 18:48:50 +0000 | [diff] [blame] | 1062 | DIR *dir; |
| 1063 | unsigned int ntid = 0, nerr = 0; |
| 1064 | |
| 1065 | if (followfork && tcp->pid != strace_child && |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1066 | xsprintf(procdir, task_path, tcp->pid) > 0 && |
Dmitry V. Levin | 2ddb73d | 2016-07-25 18:48:50 +0000 | [diff] [blame] | 1067 | (dir = opendir(procdir)) != NULL) { |
| 1068 | struct_dirent *de; |
| 1069 | |
| 1070 | while ((de = read_dir(dir)) != NULL) { |
| 1071 | if (de->d_fileno == 0) |
| 1072 | continue; |
| 1073 | |
| 1074 | int tid = string_to_uint(de->d_name); |
| 1075 | if (tid <= 0 || tid == tcp->pid) |
| 1076 | continue; |
| 1077 | |
| 1078 | ++ntid; |
| 1079 | if (ptrace_attach_or_seize(tid) < 0) { |
| 1080 | ++nerr; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1081 | debug_perror_msg("attach: ptrace(%s, %d)", |
| 1082 | ptrace_attach_cmd, tid); |
Dmitry V. Levin | 2ddb73d | 2016-07-25 18:48:50 +0000 | [diff] [blame] | 1083 | continue; |
| 1084 | } |
Dmitry V. Levin | 2ddb73d | 2016-07-25 18:48:50 +0000 | [diff] [blame] | 1085 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1086 | after_successful_attach(alloctcb(tid), |
| 1087 | TCB_GRABBED | post_attach_sigstop); |
| 1088 | debug_msg("attach to pid %d succeeded", tid); |
Dmitry V. Levin | 2ddb73d | 2016-07-25 18:48:50 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | closedir(dir); |
| 1092 | } |
| 1093 | |
| 1094 | if (!qflag) { |
| 1095 | if (ntid > nerr) |
| 1096 | error_msg("Process %u attached" |
| 1097 | " with %u threads", |
| 1098 | tcp->pid, ntid - nerr + 1); |
| 1099 | else |
| 1100 | error_msg("Process %u attached", |
| 1101 | tcp->pid); |
| 1102 | } |
Dmitry V. Levin | 795795a | 2016-07-25 16:25:12 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static void |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1106 | startup_attach(void) |
| 1107 | { |
Dmitry V. Levin | fa8c286 | 2016-01-22 14:37:14 +0000 | [diff] [blame] | 1108 | pid_t parent_pid = strace_tracer_pid; |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 1109 | unsigned int tcbi; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1110 | struct tcb *tcp; |
| 1111 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1112 | if (daemonized_tracer) { |
| 1113 | pid_t pid = fork(); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1114 | if (pid < 0) |
| 1115 | perror_func_msg_and_die("fork"); |
| 1116 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1117 | if (pid) { /* parent */ |
| 1118 | /* |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1119 | * Wait for grandchild to attach to straced process |
| 1120 | * (grandparent). Grandchild SIGKILLs us after it attached. |
| 1121 | * Grandparent's wait() is unblocked by our death, |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1122 | * it proceeds to exec the straced program. |
| 1123 | */ |
| 1124 | pause(); |
| 1125 | _exit(0); /* paranoia */ |
| 1126 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1127 | /* grandchild */ |
| 1128 | /* We will be the tracer process. Remember our new pid: */ |
| 1129 | strace_tracer_pid = getpid(); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1132 | for (tcbi = 0; tcbi < tcbtabsize; tcbi++) { |
| 1133 | tcp = tcbtab[tcbi]; |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 1134 | |
Denys Vlasenko | fadbf66 | 2013-06-26 14:14:29 +0200 | [diff] [blame] | 1135 | if (!tcp->pid) |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1136 | continue; |
| 1137 | |
Denys Vlasenko | d116a73 | 2011-09-05 14:01:33 +0200 | [diff] [blame] | 1138 | /* Is this a process we should attach to, but not yet attached? */ |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1139 | if (tcp->flags & TCB_ATTACHED) |
| 1140 | continue; /* no, we already attached it */ |
Denys Vlasenko | d116a73 | 2011-09-05 14:01:33 +0200 | [diff] [blame] | 1141 | |
Dmitry V. Levin | fa8c286 | 2016-01-22 14:37:14 +0000 | [diff] [blame] | 1142 | if (tcp->pid == parent_pid || tcp->pid == strace_tracer_pid) { |
| 1143 | errno = EPERM; |
Dmitry V. Levin | 795795a | 2016-07-25 16:25:12 +0000 | [diff] [blame] | 1144 | perror_msg("attach: pid %d", tcp->pid); |
Dmitry V. Levin | fa8c286 | 2016-01-22 14:37:14 +0000 | [diff] [blame] | 1145 | droptcb(tcp); |
| 1146 | continue; |
| 1147 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1148 | |
Dmitry V. Levin | 795795a | 2016-07-25 16:25:12 +0000 | [diff] [blame] | 1149 | attach_tcb(tcp); |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 1150 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 1151 | if (interrupted) |
| 1152 | return; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 1153 | } /* for each tcbtab[] */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1154 | |
Dmitry V. Levin | fa8c286 | 2016-01-22 14:37:14 +0000 | [diff] [blame] | 1155 | if (daemonized_tracer) { |
| 1156 | /* |
| 1157 | * Make parent go away. |
| 1158 | * Also makes grandparent's wait() unblock. |
| 1159 | */ |
| 1160 | kill(parent_pid, SIGKILL); |
| 1161 | strace_child = 0; |
| 1162 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1165 | /* Stack-o-phobic exec helper, in the hope to work around |
| 1166 | * NOMMU + "daemonized tracer" difficulty. |
| 1167 | */ |
| 1168 | struct exec_params { |
| 1169 | int fd_to_close; |
| 1170 | uid_t run_euid; |
| 1171 | gid_t run_egid; |
| 1172 | char **argv; |
| 1173 | char *pathname; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1174 | struct sigaction child_sa; |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1175 | }; |
| 1176 | static struct exec_params params_for_tracee; |
Dmitry V. Levin | 5647cf8 | 2015-03-29 22:45:03 +0000 | [diff] [blame] | 1177 | |
| 1178 | static void ATTRIBUTE_NOINLINE ATTRIBUTE_NORETURN |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1179 | exec_or_die(void) |
| 1180 | { |
| 1181 | struct exec_params *params = ¶ms_for_tracee; |
| 1182 | |
| 1183 | if (params->fd_to_close >= 0) |
| 1184 | close(params->fd_to_close); |
| 1185 | if (!daemonized_tracer && !use_seize) { |
| 1186 | if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) { |
| 1187 | perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)"); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | if (username != NULL) { |
| 1192 | /* |
| 1193 | * It is important to set groups before we |
| 1194 | * lose privileges on setuid. |
| 1195 | */ |
| 1196 | if (initgroups(username, run_gid) < 0) { |
| 1197 | perror_msg_and_die("initgroups"); |
| 1198 | } |
| 1199 | if (setregid(run_gid, params->run_egid) < 0) { |
| 1200 | perror_msg_and_die("setregid"); |
| 1201 | } |
| 1202 | if (setreuid(run_uid, params->run_euid) < 0) { |
| 1203 | perror_msg_and_die("setreuid"); |
| 1204 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1205 | } else if (geteuid() != 0) |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1206 | if (setreuid(run_uid, run_uid) < 0) { |
| 1207 | perror_msg_and_die("setreuid"); |
| 1208 | } |
| 1209 | |
| 1210 | if (!daemonized_tracer) { |
| 1211 | /* |
| 1212 | * Induce a ptrace stop. Tracer (our parent) |
| 1213 | * will resume us with PTRACE_SYSCALL and display |
| 1214 | * the immediately following execve syscall. |
| 1215 | * Can't do this on NOMMU systems, we are after |
| 1216 | * vfork: parent is blocked, stopping would deadlock. |
| 1217 | */ |
| 1218 | if (!NOMMU_SYSTEM) |
| 1219 | kill(getpid(), SIGSTOP); |
| 1220 | } else { |
| 1221 | alarm(3); |
| 1222 | /* we depend on SIGCHLD set to SIG_DFL by init code */ |
| 1223 | /* if it happens to be SIG_IGN'ed, wait won't block */ |
| 1224 | wait(NULL); |
| 1225 | alarm(0); |
| 1226 | } |
| 1227 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1228 | if (params_for_tracee.child_sa.sa_handler != SIG_DFL) |
| 1229 | sigaction(SIGCHLD, ¶ms_for_tracee.child_sa, NULL); |
| 1230 | |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1231 | execv(params->pathname, params->argv); |
| 1232 | perror_msg_and_die("exec"); |
| 1233 | } |
| 1234 | |
Dmitry V. Levin | 559ec3e | 2016-08-13 22:04:59 +0000 | [diff] [blame] | 1235 | /* |
| 1236 | * Open a dummy descriptor for use as a placeholder. |
| 1237 | * The descriptor is O_RDONLY with FD_CLOEXEC flag set. |
| 1238 | * A read attempt from such descriptor ends with EOF, |
| 1239 | * a write attempt is rejected with EBADF. |
| 1240 | */ |
Dmitry V. Levin | 6aedd42 | 2016-02-08 17:46:58 +0000 | [diff] [blame] | 1241 | static int |
| 1242 | open_dummy_desc(void) |
Denys Vlasenko | 0736d4e | 2016-02-08 18:08:46 +0100 | [diff] [blame] | 1243 | { |
Dmitry V. Levin | 6aedd42 | 2016-02-08 17:46:58 +0000 | [diff] [blame] | 1244 | int fds[2]; |
| 1245 | |
| 1246 | if (pipe(fds)) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1247 | perror_func_msg_and_die("pipe"); |
Dmitry V. Levin | 6aedd42 | 2016-02-08 17:46:58 +0000 | [diff] [blame] | 1248 | close(fds[1]); |
Dmitry V. Levin | 559ec3e | 2016-08-13 22:04:59 +0000 | [diff] [blame] | 1249 | set_cloexec_flag(fds[0]); |
Dmitry V. Levin | 6aedd42 | 2016-02-08 17:46:58 +0000 | [diff] [blame] | 1250 | return fds[0]; |
Denys Vlasenko | 0736d4e | 2016-02-08 18:08:46 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
Dmitry V. Levin | 559ec3e | 2016-08-13 22:04:59 +0000 | [diff] [blame] | 1253 | /* placeholder fds status for stdin and stdout */ |
| 1254 | static bool fd_is_placeholder[2]; |
| 1255 | |
| 1256 | /* |
| 1257 | * Ensure that all standard file descriptors are open by opening placeholder |
| 1258 | * file descriptors for those standard file descriptors that are not open. |
| 1259 | * |
| 1260 | * The information which descriptors have been made open is saved |
| 1261 | * in fd_is_placeholder for later use. |
| 1262 | */ |
| 1263 | static void |
| 1264 | ensure_standard_fds_opened(void) |
| 1265 | { |
| 1266 | int fd; |
| 1267 | |
| 1268 | while ((fd = open_dummy_desc()) <= 2) { |
| 1269 | if (fd == 2) |
| 1270 | break; |
| 1271 | fd_is_placeholder[fd] = true; |
| 1272 | } |
| 1273 | |
| 1274 | if (fd > 2) |
| 1275 | close(fd); |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Redirect stdin and stdout unless they have been opened earlier |
| 1280 | * by ensure_standard_fds_opened as placeholders. |
| 1281 | */ |
| 1282 | static void |
| 1283 | redirect_standard_fds(void) |
| 1284 | { |
| 1285 | int i; |
| 1286 | |
| 1287 | /* |
| 1288 | * It might be a good idea to redirect stderr as well, |
| 1289 | * but we sometimes need to print error messages. |
| 1290 | */ |
| 1291 | for (i = 0; i <= 1; ++i) { |
| 1292 | if (!fd_is_placeholder[i]) { |
| 1293 | close(i); |
| 1294 | open_dummy_desc(); |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1299 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1300 | startup_child(char **argv) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1301 | { |
Dmitry V. Levin | c8938e0 | 2013-03-20 21:40:15 +0000 | [diff] [blame] | 1302 | struct_stat statbuf; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1303 | const char *filename; |
Dmitry V. Levin | 1dbd39e | 2015-02-28 14:50:09 +0000 | [diff] [blame] | 1304 | size_t filename_len; |
Dmitry V. Levin | 025b358 | 2014-11-21 22:28:34 +0000 | [diff] [blame] | 1305 | char pathname[PATH_MAX]; |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1306 | int pid; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1307 | struct tcb *tcp; |
| 1308 | |
| 1309 | filename = argv[0]; |
Dmitry V. Levin | 1dbd39e | 2015-02-28 14:50:09 +0000 | [diff] [blame] | 1310 | filename_len = strlen(filename); |
| 1311 | |
| 1312 | if (filename_len > sizeof(pathname) - 1) { |
| 1313 | errno = ENAMETOOLONG; |
| 1314 | perror_msg_and_die("exec"); |
| 1315 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1316 | if (strchr(filename, '/')) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1317 | strcpy(pathname, filename); |
| 1318 | } |
| 1319 | #ifdef USE_DEBUGGING_EXEC |
| 1320 | /* |
| 1321 | * Debuggers customarily check the current directory |
| 1322 | * first regardless of the path but doing that gives |
| 1323 | * security geeks a panic attack. |
| 1324 | */ |
Dmitry V. Levin | c8938e0 | 2013-03-20 21:40:15 +0000 | [diff] [blame] | 1325 | else if (stat_file(filename, &statbuf) == 0) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1326 | strcpy(pathname, filename); |
| 1327 | #endif /* USE_DEBUGGING_EXEC */ |
| 1328 | else { |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 1329 | const char *path; |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 1330 | size_t m, n, len; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1331 | |
| 1332 | for (path = getenv("PATH"); path && *path; path += m) { |
Denys Vlasenko | 4f3df07 | 2012-01-29 22:38:35 +0100 | [diff] [blame] | 1333 | const char *colon = strchr(path, ':'); |
| 1334 | if (colon) { |
| 1335 | n = colon - path; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1336 | m = n + 1; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1337 | } else |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1338 | m = n = strlen(path); |
| 1339 | if (n == 0) { |
Dmitry V. Levin | 025b358 | 2014-11-21 22:28:34 +0000 | [diff] [blame] | 1340 | if (!getcwd(pathname, PATH_MAX)) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1341 | continue; |
| 1342 | len = strlen(pathname); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1343 | } else if (n > sizeof(pathname) - 1) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1344 | continue; |
| 1345 | else { |
| 1346 | strncpy(pathname, path, n); |
| 1347 | len = n; |
| 1348 | } |
| 1349 | if (len && pathname[len - 1] != '/') |
| 1350 | pathname[len++] = '/'; |
Dmitry V. Levin | 1dbd39e | 2015-02-28 14:50:09 +0000 | [diff] [blame] | 1351 | if (filename_len + len > sizeof(pathname) - 1) |
| 1352 | continue; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1353 | strcpy(pathname + len, filename); |
Dmitry V. Levin | c8938e0 | 2013-03-20 21:40:15 +0000 | [diff] [blame] | 1354 | if (stat_file(pathname, &statbuf) == 0 && |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1355 | /* Accept only regular files |
| 1356 | with some execute bits set. |
| 1357 | XXX not perfect, might still fail */ |
| 1358 | S_ISREG(statbuf.st_mode) && |
| 1359 | (statbuf.st_mode & 0111)) |
| 1360 | break; |
| 1361 | } |
Dmitry V. Levin | 1dbd39e | 2015-02-28 14:50:09 +0000 | [diff] [blame] | 1362 | if (!path || !*path) |
| 1363 | pathname[0] = '\0'; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1364 | } |
Dmitry V. Levin | c8938e0 | 2013-03-20 21:40:15 +0000 | [diff] [blame] | 1365 | if (stat_file(pathname, &statbuf) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1366 | perror_msg_and_die("Can't stat '%s'", filename); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1367 | } |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1368 | |
| 1369 | params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1; |
| 1370 | params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid; |
| 1371 | params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid; |
| 1372 | params_for_tracee.argv = argv; |
| 1373 | /* |
| 1374 | * On NOMMU, can be safely freed only after execve in tracee. |
| 1375 | * It's hard to know when that happens, so we just leak it. |
| 1376 | */ |
Dmitry V. Levin | 3e9d71f | 2015-05-25 20:41:02 +0000 | [diff] [blame] | 1377 | params_for_tracee.pathname = NOMMU_SYSTEM ? xstrdup(pathname) : pathname; |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1378 | |
Dmitry V. Levin | 882478a | 2013-05-13 18:43:28 +0000 | [diff] [blame] | 1379 | #if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY |
| 1380 | if (daemonized_tracer) |
| 1381 | prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY); |
| 1382 | #endif |
| 1383 | |
Denys Vlasenko | e8681c9 | 2013-06-26 14:27:11 +0200 | [diff] [blame] | 1384 | pid = fork(); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1385 | if (pid < 0) |
| 1386 | perror_func_msg_and_die("fork"); |
| 1387 | |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1388 | if ((pid != 0 && daemonized_tracer) |
| 1389 | || (pid == 0 && !daemonized_tracer) |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1390 | ) { |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1391 | /* We are to become the tracee. Two cases: |
| 1392 | * -D: we are parent |
| 1393 | * not -D: we are child |
| 1394 | */ |
| 1395 | exec_or_die(); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1396 | } |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1397 | |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 1398 | /* We are the tracer */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1399 | |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 1400 | if (!daemonized_tracer) { |
Denys Vlasenko | e8681c9 | 2013-06-26 14:27:11 +0200 | [diff] [blame] | 1401 | strace_child = pid; |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1402 | if (!use_seize) { |
| 1403 | /* child did PTRACE_TRACEME, nothing to do in parent */ |
| 1404 | } else { |
Denys Vlasenko | 5c9d8f4 | 2013-02-19 15:30:12 +0100 | [diff] [blame] | 1405 | if (!NOMMU_SYSTEM) { |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1406 | /* Wait until child stopped itself */ |
| 1407 | int status; |
| 1408 | while (waitpid(pid, &status, WSTOPPED) < 0) { |
| 1409 | if (errno == EINTR) |
| 1410 | continue; |
| 1411 | perror_msg_and_die("waitpid"); |
| 1412 | } |
| 1413 | if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) { |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1414 | kill_save_errno(pid, SIGKILL); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1415 | perror_msg_and_die("Unexpected wait status %#x", |
| 1416 | status); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1417 | } |
| 1418 | } |
Denys Vlasenko | 5c9d8f4 | 2013-02-19 15:30:12 +0100 | [diff] [blame] | 1419 | /* Else: NOMMU case, we have no way to sync. |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1420 | * Just attach to it as soon as possible. |
| 1421 | * This means that we may miss a few first syscalls... |
| 1422 | */ |
| 1423 | |
| 1424 | if (ptrace_attach_or_seize(pid)) { |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1425 | kill_save_errno(pid, SIGKILL); |
Dmitry V. Levin | c925151 | 2016-07-25 17:12:42 +0000 | [diff] [blame] | 1426 | perror_msg_and_die("attach: ptrace(%s, %d)", |
| 1427 | ptrace_attach_cmd, pid); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1428 | } |
Denys Vlasenko | 5c9d8f4 | 2013-02-19 15:30:12 +0100 | [diff] [blame] | 1429 | if (!NOMMU_SYSTEM) |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1430 | kill(pid, SIGCONT); |
| 1431 | } |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 1432 | tcp = alloctcb(pid); |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1433 | after_successful_attach(tcp, TCB_SKIP_DETACH_ON_FIRST_EXEC |
| 1434 | | (NOMMU_SYSTEM ? 0 |
| 1435 | : (TCB_HIDE_LOG |
| 1436 | | post_attach_sigstop))); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1437 | } else { |
Dmitry V. Levin | fa8c286 | 2016-01-22 14:37:14 +0000 | [diff] [blame] | 1438 | /* With -D, we are *child* here, the tracee is our parent. */ |
| 1439 | strace_child = strace_tracer_pid; |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 1440 | strace_tracer_pid = getpid(); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1441 | tcp = alloctcb(strace_child); |
| 1442 | tcp->flags |= TCB_SKIP_DETACH_ON_FIRST_EXEC | TCB_HIDE_LOG; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1443 | /* |
| 1444 | * Attaching will be done later, by startup_attach. |
| 1445 | * Note: we don't do after_successful_attach() here either! |
| 1446 | */ |
Denys Vlasenko | 5c9d8f4 | 2013-02-19 15:30:12 +0100 | [diff] [blame] | 1447 | |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1448 | /* NOMMU BUG! -D mode is active, we (child) return, |
| 1449 | * and we will scribble over parent's stack! |
| 1450 | * When parent later unpauses, it segfaults. |
| 1451 | * |
| 1452 | * We work around it |
| 1453 | * (1) by declaring exec_or_die() NORETURN, |
| 1454 | * hopefully compiler will just jump to it |
| 1455 | * instead of call (won't push anything to stack), |
| 1456 | * (2) by trying very hard in exec_or_die() |
| 1457 | * to not use any stack, |
Dmitry V. Levin | 025b358 | 2014-11-21 22:28:34 +0000 | [diff] [blame] | 1458 | * (3) having a really big (PATH_MAX) stack object |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1459 | * in this function, which creates a "buffer" between |
| 1460 | * child's and parent's stack pointers. |
| 1461 | * This may save us if (1) and (2) failed |
| 1462 | * and compiler decided to use stack in exec_or_die() anyway |
| 1463 | * (happens on i386 because of stack parameter passing). |
Denys Vlasenko | 05f3251 | 2013-02-26 12:00:34 +0100 | [diff] [blame] | 1464 | * |
| 1465 | * A cleaner solution is to use makecontext + setcontext |
| 1466 | * to create a genuine separate stack and execute on it. |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1467 | */ |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1468 | } |
Denys Vlasenko | c9f85b3 | 2016-02-08 14:52:32 +0100 | [diff] [blame] | 1469 | /* |
| 1470 | * A case where straced process is part of a pipe: |
| 1471 | * { sleep 1; yes | head -n99999; } | strace -o/dev/null sh -c 'exec <&-; sleep 9' |
| 1472 | * If strace won't close its fd#0, closing it in tracee is not enough: |
| 1473 | * the pipe is still open, it has a reader. Thus, "head" will not get its |
| 1474 | * SIGPIPE at once, on the first write. |
| 1475 | * |
Dmitry V. Levin | 559ec3e | 2016-08-13 22:04:59 +0000 | [diff] [blame] | 1476 | * Preventing it by redirecting strace's stdin/out. |
Denys Vlasenko | c9f85b3 | 2016-02-08 14:52:32 +0100 | [diff] [blame] | 1477 | * (Don't leave fds 0 and 1 closed, this is bad practice: future opens |
| 1478 | * will reuse them, unexpectedly making a newly opened object "stdin"). |
| 1479 | */ |
Dmitry V. Levin | 559ec3e | 2016-08-13 22:04:59 +0000 | [diff] [blame] | 1480 | redirect_standard_fds(); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1483 | static void |
| 1484 | test_ptrace_seize(void) |
| 1485 | { |
| 1486 | int pid; |
| 1487 | |
Denys Vlasenko | 05f3251 | 2013-02-26 12:00:34 +0100 | [diff] [blame] | 1488 | /* Need fork for test. NOMMU has no forks */ |
Denys Vlasenko | 5c9d8f4 | 2013-02-19 15:30:12 +0100 | [diff] [blame] | 1489 | if (NOMMU_SYSTEM) { |
| 1490 | post_attach_sigstop = 0; /* this sets use_seize to 1 */ |
| 1491 | return; |
| 1492 | } |
| 1493 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1494 | pid = fork(); |
| 1495 | if (pid < 0) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1496 | perror_func_msg_and_die("fork"); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1497 | |
| 1498 | if (pid == 0) { |
| 1499 | pause(); |
| 1500 | _exit(0); |
| 1501 | } |
| 1502 | |
| 1503 | /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After |
| 1504 | * attaching tracee continues to run unless a trap condition occurs. |
| 1505 | * PTRACE_SEIZE doesn't affect signal or group stop state. |
| 1506 | */ |
Denys Vlasenko | 26bc060 | 2012-07-10 16:36:32 +0200 | [diff] [blame] | 1507 | if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) { |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1508 | post_attach_sigstop = 0; /* this sets use_seize to 1 */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1509 | } else { |
| 1510 | debug_msg("PTRACE_SEIZE doesn't work"); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | kill(pid, SIGKILL); |
| 1514 | |
| 1515 | while (1) { |
| 1516 | int status, tracee_pid; |
| 1517 | |
| 1518 | errno = 0; |
| 1519 | tracee_pid = waitpid(pid, &status, 0); |
| 1520 | if (tracee_pid <= 0) { |
| 1521 | if (errno == EINTR) |
| 1522 | continue; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1523 | perror_func_msg_and_die("unexpected wait result %d", |
| 1524 | tracee_pid); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1525 | } |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1526 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1527 | return; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1528 | |
| 1529 | error_func_msg_and_die("unexpected wait status %#x", status); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1530 | } |
| 1531 | } |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1532 | |
Denys Vlasenko | 6e0bfd1 | 2012-03-15 14:36:28 +0100 | [diff] [blame] | 1533 | static unsigned |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1534 | get_os_release(void) |
| 1535 | { |
Denys Vlasenko | 6e0bfd1 | 2012-03-15 14:36:28 +0100 | [diff] [blame] | 1536 | unsigned rel; |
| 1537 | const char *p; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1538 | struct utsname u; |
| 1539 | if (uname(&u) < 0) |
| 1540 | perror_msg_and_die("uname"); |
Denys Vlasenko | 6e0bfd1 | 2012-03-15 14:36:28 +0100 | [diff] [blame] | 1541 | /* u.release has this form: "3.2.9[-some-garbage]" */ |
| 1542 | rel = 0; |
| 1543 | p = u.release; |
| 1544 | for (;;) { |
| 1545 | if (!(*p >= '0' && *p <= '9')) |
| 1546 | error_msg_and_die("Bad OS release string: '%s'", u.release); |
| 1547 | /* Note: this open-codes KERNEL_VERSION(): */ |
| 1548 | rel = (rel << 8) | atoi(p); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1549 | if (rel >= KERNEL_VERSION(1, 0, 0)) |
Denys Vlasenko | 6e0bfd1 | 2012-03-15 14:36:28 +0100 | [diff] [blame] | 1550 | break; |
| 1551 | while (*p >= '0' && *p <= '9') |
| 1552 | p++; |
Dmitry V. Levin | 0dbc80d | 2012-05-14 23:42:10 +0000 | [diff] [blame] | 1553 | if (*p != '.') { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1554 | if (rel >= KERNEL_VERSION(0, 1, 0)) { |
Dmitry V. Levin | 0dbc80d | 2012-05-14 23:42:10 +0000 | [diff] [blame] | 1555 | /* "X.Y-something" means "X.Y.0" */ |
| 1556 | rel <<= 8; |
| 1557 | break; |
| 1558 | } |
Denys Vlasenko | 6e0bfd1 | 2012-03-15 14:36:28 +0100 | [diff] [blame] | 1559 | error_msg_and_die("Bad OS release string: '%s'", u.release); |
Dmitry V. Levin | 0dbc80d | 2012-05-14 23:42:10 +0000 | [diff] [blame] | 1560 | } |
Denys Vlasenko | 6e0bfd1 | 2012-03-15 14:36:28 +0100 | [diff] [blame] | 1561 | p++; |
| 1562 | } |
| 1563 | return rel; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1564 | } |
| 1565 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1566 | static void |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1567 | set_sighandler(int signo, void (*sighandler)(int), struct sigaction *oldact) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1568 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1569 | const struct sigaction sa = { .sa_handler = sighandler }; |
| 1570 | sigaction(signo, &sa, oldact); |
| 1571 | } |
| 1572 | |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 1573 | /* |
| 1574 | * Initialization part of main() was eating much stack (~0.5k), |
| 1575 | * which was unused after init. |
| 1576 | * We can reuse it if we move init code into a separate function. |
| 1577 | * |
| 1578 | * Don't want main() to inline us and defeat the reason |
| 1579 | * we have a separate function. |
| 1580 | */ |
Dmitry V. Levin | 5647cf8 | 2015-03-29 22:45:03 +0000 | [diff] [blame] | 1581 | static void ATTRIBUTE_NOINLINE |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 1582 | init(int argc, char *argv[]) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1583 | { |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 1584 | int c, i; |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1585 | int optF = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1586 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1587 | if (!program_invocation_name || !*program_invocation_name) { |
| 1588 | static char name[] = "strace"; |
| 1589 | program_invocation_name = |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1590 | (argc > 0 && argv[0] && *argv[0]) ? argv[0] : name; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1591 | } |
Denys Vlasenko | a509054 | 2012-03-15 17:27:49 +0100 | [diff] [blame] | 1592 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1593 | strace_tracer_pid = getpid(); |
| 1594 | |
Denys Vlasenko | 6e0bfd1 | 2012-03-15 14:36:28 +0100 | [diff] [blame] | 1595 | os_release = get_os_release(); |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1596 | |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 1597 | shared_log = stderr; |
Roland McGrath | 138c6a3 | 2006-01-12 09:50:49 +0000 | [diff] [blame] | 1598 | set_sortby(DEFAULT_SORTBY); |
| 1599 | set_personality(DEFAULT_PERSONALITY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1600 | qualify("trace=all"); |
| 1601 | qualify("abbrev=all"); |
| 1602 | qualify("verbose=all"); |
Denys Vlasenko | 74ec14f | 2013-02-21 16:13:47 +0100 | [diff] [blame] | 1603 | #if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE) |
| 1604 | # error Bug in DEFAULT_QUAL_FLAGS |
| 1605 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1606 | qualify("signal=all"); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1607 | while ((c = getopt(argc, argv, "+" |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1608 | #ifdef ENABLE_STACKTRACE |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1609 | "k" |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 1610 | #endif |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1611 | "a:Ab:cCdDe:E:fFhiI:o:O:p:P:qrs:S:tTu:vVwxX:yz")) != EOF) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1612 | switch (c) { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1613 | case 'a': |
| 1614 | acolumn = string_to_uint(optarg); |
| 1615 | if (acolumn < 0) |
| 1616 | error_opt_arg(c, optarg); |
| 1617 | break; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 1618 | case 'A': |
| 1619 | open_append = true; |
| 1620 | break; |
Denys Vlasenko | 61e7aad | 2012-03-15 13:44:17 +0100 | [diff] [blame] | 1621 | case 'b': |
Denys Vlasenko | 22efaf0 | 2013-02-27 12:15:19 +0100 | [diff] [blame] | 1622 | if (strcmp(optarg, "execve") != 0) |
| 1623 | error_msg_and_die("Syscall '%s' for -b isn't supported", |
| 1624 | optarg); |
Denys Vlasenko | 61e7aad | 2012-03-15 13:44:17 +0100 | [diff] [blame] | 1625 | detach_on_execve = 1; |
| 1626 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1627 | case 'c': |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1628 | if (cflag == CFLAG_BOTH) { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 1629 | error_msg_and_help("-c and -C are mutually exclusive"); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1630 | } |
| 1631 | cflag = CFLAG_ONLY_STATS; |
| 1632 | break; |
| 1633 | case 'C': |
| 1634 | if (cflag == CFLAG_ONLY_STATS) { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 1635 | error_msg_and_help("-c and -C are mutually exclusive"); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1636 | } |
| 1637 | cflag = CFLAG_BOTH; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1638 | break; |
| 1639 | case 'd': |
Denys Vlasenko | a50d2a8 | 2012-03-15 12:49:52 +0100 | [diff] [blame] | 1640 | debug_flag = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1641 | break; |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1642 | case 'D': |
| 1643 | daemonized_tracer = 1; |
| 1644 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1645 | case 'e': |
| 1646 | qualify(optarg); |
| 1647 | break; |
| 1648 | case 'E': |
| 1649 | if (putenv(optarg) < 0) |
| 1650 | perror_msg_and_die("putenv"); |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1651 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1652 | case 'f': |
| 1653 | followfork++; |
| 1654 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1655 | case 'F': |
| 1656 | optF = 1; |
| 1657 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1658 | case 'h': |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 1659 | usage(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1660 | break; |
| 1661 | case 'i': |
Denys Vlasenko | a50d2a8 | 2012-03-15 12:49:52 +0100 | [diff] [blame] | 1662 | iflag = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1663 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1664 | case 'I': |
| 1665 | opt_intr = string_to_uint_upto(optarg, NUM_INTR_OPTS - 1); |
| 1666 | if (opt_intr <= 0) |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 1667 | error_opt_arg(c, optarg); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1668 | break; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1669 | #ifdef ENABLE_STACKTRACE |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1670 | case 'k': |
| 1671 | stack_trace_enabled = true; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1672 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1673 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1674 | case 'o': |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 1675 | outfname = optarg; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1676 | break; |
| 1677 | case 'O': |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 1678 | i = string_to_uint(optarg); |
| 1679 | if (i < 0) |
| 1680 | error_opt_arg(c, optarg); |
| 1681 | set_overhead(i); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | case 'p': |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1684 | process_opt_p_list(optarg); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1685 | break; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1686 | case 'P': |
Denys Vlasenko | 7239dbc | 2013-03-05 15:46:34 +0100 | [diff] [blame] | 1687 | pathtrace_select(optarg); |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1688 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1689 | case 'q': |
| 1690 | qflag++; |
| 1691 | break; |
| 1692 | case 'r': |
| 1693 | rflag = 1; |
| 1694 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1695 | case 's': |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 1696 | i = string_to_uint(optarg); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1697 | if (i < 0 || (unsigned int) i > -1U / 4) |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 1698 | error_opt_arg(c, optarg); |
| 1699 | max_strlen = i; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1700 | break; |
| 1701 | case 'S': |
| 1702 | set_sortby(optarg); |
| 1703 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1704 | case 't': |
| 1705 | tflag++; |
| 1706 | break; |
| 1707 | case 'T': |
| 1708 | Tflag = 1; |
| 1709 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1710 | case 'u': |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 1711 | username = optarg; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1712 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1713 | case 'v': |
| 1714 | qualify("abbrev=none"); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 1715 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1716 | case 'V': |
| 1717 | print_version(); |
| 1718 | exit(0); |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 1719 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1720 | case 'w': |
| 1721 | count_wallclock = 1; |
| 1722 | break; |
| 1723 | case 'x': |
| 1724 | xflag++; |
| 1725 | break; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1726 | case 'X': |
| 1727 | if (!strcmp(optarg, "raw")) |
| 1728 | xlat_verbosity = XLAT_STYLE_RAW; |
| 1729 | else if (!strcmp(optarg, "abbrev")) |
| 1730 | xlat_verbosity = XLAT_STYLE_ABBREV; |
| 1731 | else if (!strcmp(optarg, "verbose")) |
| 1732 | xlat_verbosity = XLAT_STYLE_VERBOSE; |
| 1733 | else |
| 1734 | error_opt_arg(c, optarg); |
| 1735 | break; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1736 | case 'y': |
| 1737 | show_fd_path++; |
| 1738 | break; |
| 1739 | case 'z': |
| 1740 | not_failing_only = 1; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1741 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1742 | default: |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 1743 | error_msg_and_help(NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1744 | break; |
| 1745 | } |
| 1746 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1747 | |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1748 | argv += optind; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1749 | argc -= optind; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1750 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1751 | if (argc < 0 || (!nprocs && !argc)) { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 1752 | error_msg_and_help("must have PROG [ARGS] or -p PID"); |
| 1753 | } |
Roland McGrath | ce0d154 | 2003-11-11 21:24:23 +0000 | [diff] [blame] | 1754 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1755 | if (!argc && daemonized_tracer) { |
Dmitry V. Levin | fa8c286 | 2016-01-22 14:37:14 +0000 | [diff] [blame] | 1756 | error_msg_and_help("PROG [ARGS] must be specified with -D"); |
Wang Chao | d322a4b | 2010-08-05 14:30:11 +0800 | [diff] [blame] | 1757 | } |
| 1758 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1759 | if (optF) { |
| 1760 | if (followfork) { |
| 1761 | error_msg("deprecated option -F ignored"); |
| 1762 | } else { |
| 1763 | error_msg("option -F is deprecated, " |
| 1764 | "please use -f instead"); |
| 1765 | followfork = optF; |
| 1766 | } |
| 1767 | } |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1768 | |
Denys Vlasenko | 3db3b26 | 2012-03-16 15:15:14 +0100 | [diff] [blame] | 1769 | if (followfork >= 2 && cflag) { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 1770 | error_msg_and_help("(-c or -C) and -ff are mutually exclusive"); |
Roland McGrath | cb9def6 | 2006-04-25 07:48:03 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Mark Hills | e53bf23 | 2014-05-28 17:52:40 +0100 | [diff] [blame] | 1773 | if (count_wallclock && !cflag) { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 1774 | error_msg_and_help("-w must be given with (-c or -C)"); |
Mark Hills | e53bf23 | 2014-05-28 17:52:40 +0100 | [diff] [blame] | 1775 | } |
| 1776 | |
Dmitry V. Levin | 2727aae | 2014-06-03 13:20:05 +0000 | [diff] [blame] | 1777 | if (cflag == CFLAG_ONLY_STATS) { |
| 1778 | if (iflag) |
| 1779 | error_msg("-%c has no effect with -c", 'i'); |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1780 | #ifdef ENABLE_STACKTRACE |
Dmitry V. Levin | 2727aae | 2014-06-03 13:20:05 +0000 | [diff] [blame] | 1781 | if (stack_trace_enabled) |
| 1782 | error_msg("-%c has no effect with -c", 'k'); |
| 1783 | #endif |
| 1784 | if (rflag) |
| 1785 | error_msg("-%c has no effect with -c", 'r'); |
| 1786 | if (tflag) |
| 1787 | error_msg("-%c has no effect with -c", 't'); |
| 1788 | if (Tflag) |
| 1789 | error_msg("-%c has no effect with -c", 'T'); |
| 1790 | if (show_fd_path) |
| 1791 | error_msg("-%c has no effect with -c", 'y'); |
| 1792 | } |
| 1793 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1794 | acolumn_spaces = xmalloc(acolumn + 1); |
| 1795 | memset(acolumn_spaces, ' ', acolumn); |
| 1796 | acolumn_spaces[acolumn] = '\0'; |
| 1797 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1798 | set_sighandler(SIGCHLD, SIG_DFL, ¶ms_for_tracee.child_sa); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1799 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1800 | #ifdef ENABLE_STACKTRACE |
| 1801 | if (stack_trace_enabled) |
Dmitry V. Levin | 2727aae | 2014-06-03 13:20:05 +0000 | [diff] [blame] | 1802 | unwind_init(); |
| 1803 | #endif |
| 1804 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1805 | /* See if they want to run as another user. */ |
| 1806 | if (username != NULL) { |
| 1807 | struct passwd *pent; |
| 1808 | |
| 1809 | if (getuid() != 0 || geteuid() != 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1810 | error_msg_and_die("You must be root to use the -u option"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1811 | } |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1812 | pent = getpwnam(username); |
| 1813 | if (pent == NULL) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1814 | error_msg_and_die("Cannot find user '%s'", username); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1815 | } |
| 1816 | run_uid = pent->pw_uid; |
| 1817 | run_gid = pent->pw_gid; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1818 | } else { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1819 | run_uid = getuid(); |
| 1820 | run_gid = getgid(); |
| 1821 | } |
| 1822 | |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1823 | if (followfork) |
Dmitry V. Levin | 23ce9e4 | 2015-02-08 13:05:53 +0000 | [diff] [blame] | 1824 | ptrace_setoptions |= PTRACE_O_TRACECLONE | |
| 1825 | PTRACE_O_TRACEFORK | |
| 1826 | PTRACE_O_TRACEVFORK; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1827 | debug_msg("ptrace_setoptions = %#x", ptrace_setoptions); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1828 | test_ptrace_seize(); |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1829 | |
Dmitry V. Levin | 559ec3e | 2016-08-13 22:04:59 +0000 | [diff] [blame] | 1830 | /* |
| 1831 | * Is something weird with our stdin and/or stdout - |
| 1832 | * for example, may they be not open? In this case, |
| 1833 | * ensure that none of the future opens uses them. |
| 1834 | * |
| 1835 | * This was seen in the wild when /proc/sys/kernel/core_pattern |
| 1836 | * was set to "|/bin/strace -o/tmp/LOG PROG": |
| 1837 | * kernel runs coredump helper with fd#0 open but fd#1 closed (!), |
| 1838 | * therefore LOG gets opened to fd#1, and fd#1 is closed by |
| 1839 | * "don't hold up stdin/out open" code soon after. |
| 1840 | */ |
| 1841 | ensure_standard_fds_opened(); |
Denys Vlasenko | 0736d4e | 2016-02-08 18:08:46 +0100 | [diff] [blame] | 1842 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1843 | /* Check if they want to redirect the output. */ |
| 1844 | if (outfname) { |
Roland McGrath | 37b9a66 | 2003-11-07 02:26:54 +0000 | [diff] [blame] | 1845 | /* See if they want to pipe the output. */ |
| 1846 | if (outfname[0] == '|' || outfname[0] == '!') { |
| 1847 | /* |
| 1848 | * We can't do the <outfname>.PID funny business |
| 1849 | * when using popen, so prohibit it. |
| 1850 | */ |
Denys Vlasenko | 3db3b26 | 2012-03-16 15:15:14 +0100 | [diff] [blame] | 1851 | if (followfork >= 2) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1852 | error_msg_and_help("piping the output and -ff " |
| 1853 | "are mutually exclusive"); |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 1854 | shared_log = strace_popen(outfname + 1); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1855 | } else if (followfork < 2) { |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 1856 | shared_log = strace_fopen(outfname); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1857 | } else if (strlen(outfname) >= PATH_MAX - sizeof(int) * 3) { |
| 1858 | errno = ENAMETOOLONG; |
| 1859 | perror_msg_and_die("%s", outfname); |
| 1860 | } |
Denys Vlasenko | 328bf25 | 2012-03-12 23:34:13 +0100 | [diff] [blame] | 1861 | } else { |
| 1862 | /* -ff without -o FILE is the same as single -f */ |
Denys Vlasenko | 3db3b26 | 2012-03-16 15:15:14 +0100 | [diff] [blame] | 1863 | if (followfork >= 2) |
Denys Vlasenko | 328bf25 | 2012-03-12 23:34:13 +0100 | [diff] [blame] | 1864 | followfork = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1867 | if (!outfname || outfname[0] == '|' || outfname[0] == '!') { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1868 | setvbuf(shared_log, NULL, _IOLBF, 0); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1869 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1870 | |
| 1871 | /* |
| 1872 | * argv[0] -pPID -oFILE Default interactive setting |
| 1873 | * yes * 0 INTR_WHILE_WAIT |
| 1874 | * no 1 0 INTR_WHILE_WAIT |
| 1875 | * yes * 1 INTR_NEVER |
| 1876 | * no 1 1 INTR_WHILE_WAIT |
| 1877 | */ |
| 1878 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1879 | if (outfname && argc) { |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1880 | if (!opt_intr) |
| 1881 | opt_intr = INTR_NEVER; |
Dmitry V. Levin | de4a680 | 2015-06-29 14:37:26 +0000 | [diff] [blame] | 1882 | if (!qflag) |
| 1883 | qflag = 1; |
Roland McGrath | 3693105 | 2003-06-03 01:35:20 +0000 | [diff] [blame] | 1884 | } |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1885 | if (!opt_intr) |
| 1886 | opt_intr = INTR_WHILE_WAIT; |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 1887 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1888 | /* |
| 1889 | * startup_child() must be called before the signal handlers get |
Denys Vlasenko | f909c8d | 2013-02-19 16:30:31 +0100 | [diff] [blame] | 1890 | * installed below as they are inherited into the spawned process. |
| 1891 | * Also we do not need to be protected by them as during interruption |
| 1892 | * in the startup_child() mode we kill the spawned process anyway. |
| 1893 | */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1894 | if (argc) { |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1895 | startup_child(argv); |
Denys Vlasenko | 61e7aad | 2012-03-15 13:44:17 +0100 | [diff] [blame] | 1896 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1897 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1898 | set_sighandler(SIGTTOU, SIG_IGN, NULL); |
| 1899 | set_sighandler(SIGTTIN, SIG_IGN, NULL); |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1900 | if (opt_intr != INTR_ANYWHERE) { |
| 1901 | if (opt_intr == INTR_BLOCK_TSTP_TOO) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1902 | set_sighandler(SIGTSTP, SIG_IGN, NULL); |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1903 | /* |
| 1904 | * In interactive mode (if no -o OUTFILE, or -p PID is used), |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 1905 | * fatal signals are handled asynchronously and acted |
| 1906 | * when waiting for process state changes. |
| 1907 | * In non-interactive mode these signals are ignored. |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1908 | */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1909 | set_sighandler(SIGHUP, interactive ? interrupt : SIG_IGN, NULL); |
| 1910 | set_sighandler(SIGINT, interactive ? interrupt : SIG_IGN, NULL); |
| 1911 | set_sighandler(SIGQUIT, interactive ? interrupt : SIG_IGN, NULL); |
| 1912 | set_sighandler(SIGPIPE, interactive ? interrupt : SIG_IGN, NULL); |
| 1913 | set_sighandler(SIGTERM, interactive ? interrupt : SIG_IGN, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1914 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 1915 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 1916 | sigemptyset(&timer_set); |
| 1917 | sigaddset(&timer_set, SIGALRM); |
| 1918 | sigprocmask(SIG_BLOCK, &timer_set, NULL); |
| 1919 | set_sighandler(SIGALRM, timer_sighandler, NULL); |
| 1920 | |
Denys Vlasenko | fd88338 | 2012-03-09 13:03:41 +0100 | [diff] [blame] | 1921 | if (nprocs != 0 || daemonized_tracer) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1922 | startup_attach(); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1923 | |
Denys Vlasenko | fd88338 | 2012-03-09 13:03:41 +0100 | [diff] [blame] | 1924 | /* Do we want pids printed in our -o OUTFILE? |
| 1925 | * -ff: no (every pid has its own file); or |
| 1926 | * -f: yes (there can be more pids in the future); or |
| 1927 | * -p PID1,PID2: yes (there are already more than one pid) |
| 1928 | */ |
| 1929 | print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Denys Vlasenko | eebb04d | 2012-01-27 15:24:48 +0100 | [diff] [blame] | 1932 | static struct tcb * |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 1933 | pid2tcb(const int pid) |
Roland McGrath | 54e931f | 2010-09-14 18:59:20 -0700 | [diff] [blame] | 1934 | { |
Roland McGrath | 54e931f | 2010-09-14 18:59:20 -0700 | [diff] [blame] | 1935 | if (pid <= 0) |
| 1936 | return NULL; |
| 1937 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 1938 | #define PID2TCB_CACHE_SIZE 1024U |
| 1939 | #define PID2TCB_CACHE_MASK (PID2TCB_CACHE_SIZE - 1) |
| 1940 | |
| 1941 | static struct tcb *pid2tcb_cache[PID2TCB_CACHE_SIZE]; |
| 1942 | struct tcb **const ptcp = &pid2tcb_cache[pid & PID2TCB_CACHE_MASK]; |
| 1943 | struct tcb *tcp = *ptcp; |
| 1944 | |
| 1945 | if (tcp && tcp->pid == pid) |
| 1946 | return tcp; |
| 1947 | |
| 1948 | for (unsigned int i = 0; i < tcbtabsize; ++i) { |
| 1949 | tcp = tcbtab[i]; |
Denys Vlasenko | fadbf66 | 2013-06-26 14:14:29 +0200 | [diff] [blame] | 1950 | if (tcp->pid == pid) |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 1951 | return *ptcp = tcp; |
Roland McGrath | 54e931f | 2010-09-14 18:59:20 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1957 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1958 | cleanup(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1959 | { |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 1960 | unsigned int i; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1961 | struct tcb *tcp; |
Denys Vlasenko | 3521884 | 2012-01-29 21:17:56 +0100 | [diff] [blame] | 1962 | int fatal_sig; |
| 1963 | |
| 1964 | /* 'interrupted' is a volatile object, fetch it only once */ |
| 1965 | fatal_sig = interrupted; |
| 1966 | if (!fatal_sig) |
| 1967 | fatal_sig = SIGTERM; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1968 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1969 | for (i = 0; i < tcbtabsize; i++) { |
| 1970 | tcp = tcbtab[i]; |
Denys Vlasenko | fadbf66 | 2013-06-26 14:14:29 +0200 | [diff] [blame] | 1971 | if (!tcp->pid) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1972 | continue; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1973 | debug_func_msg("looking at pid %u", tcp->pid); |
Denys Vlasenko | fadbf66 | 2013-06-26 14:14:29 +0200 | [diff] [blame] | 1974 | if (tcp->pid == strace_child) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1975 | kill(tcp->pid, SIGCONT); |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 1976 | kill(tcp->pid, fatal_sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1977 | } |
Denys Vlasenko | 7de265d | 2012-03-13 11:44:31 +0100 | [diff] [blame] | 1978 | detach(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1979 | } |
| 1980 | if (cflag) |
Denys Vlasenko | 6764f8f | 2012-03-22 09:56:20 +0100 | [diff] [blame] | 1981 | call_summary(shared_log); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1985 | interrupt(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1986 | { |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 1987 | interrupted = sig; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Denys Vlasenko | d0ffdf4 | 2013-07-01 13:02:33 +0200 | [diff] [blame] | 1990 | static void |
Dmitry V. Levin | 0f4ad30 | 2015-02-22 02:13:04 +0000 | [diff] [blame] | 1991 | print_debug_info(const int pid, int status) |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 1992 | { |
| 1993 | const unsigned int event = (unsigned int) status >> 16; |
| 1994 | char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16]; |
| 1995 | char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16]; |
| 1996 | |
| 1997 | strcpy(buf, "???"); |
| 1998 | if (WIFSIGNALED(status)) |
| 1999 | #ifdef WCOREDUMP |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2000 | xsprintf(buf, "WIFSIGNALED,%ssig=%s", |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2001 | WCOREDUMP(status) ? "core," : "", |
| 2002 | signame(WTERMSIG(status))); |
| 2003 | #else |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2004 | xsprintf(buf, "WIFSIGNALED,sig=%s", |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2005 | signame(WTERMSIG(status))); |
| 2006 | #endif |
| 2007 | if (WIFEXITED(status)) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2008 | xsprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status)); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2009 | if (WIFSTOPPED(status)) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2010 | xsprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status))); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2011 | evbuf[0] = '\0'; |
| 2012 | if (event != 0) { |
| 2013 | static const char *const event_names[] = { |
| 2014 | [PTRACE_EVENT_CLONE] = "CLONE", |
| 2015 | [PTRACE_EVENT_FORK] = "FORK", |
| 2016 | [PTRACE_EVENT_VFORK] = "VFORK", |
| 2017 | [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE", |
| 2018 | [PTRACE_EVENT_EXEC] = "EXEC", |
| 2019 | [PTRACE_EVENT_EXIT] = "EXIT", |
| 2020 | /* [PTRACE_EVENT_STOP (=128)] would make biggish array */ |
| 2021 | }; |
| 2022 | const char *e = "??"; |
| 2023 | if (event < ARRAY_SIZE(event_names)) |
| 2024 | e = event_names[event]; |
| 2025 | else if (event == PTRACE_EVENT_STOP) |
| 2026 | e = "STOP"; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2027 | xsprintf(evbuf, ",EVENT_%s (%u)", e, event); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2028 | } |
Dmitry V. Levin | 6c8ef05 | 2015-05-25 22:51:19 +0000 | [diff] [blame] | 2029 | error_msg("[wait(0x%06x) = %u] %s%s", status, pid, buf, evbuf); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | static struct tcb * |
Dmitry V. Levin | 0f4ad30 | 2015-02-22 02:13:04 +0000 | [diff] [blame] | 2033 | maybe_allocate_tcb(const int pid, int status) |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2034 | { |
| 2035 | if (!WIFSTOPPED(status)) { |
Dmitry V. Levin | aa80192 | 2015-02-07 18:48:55 +0000 | [diff] [blame] | 2036 | if (detach_on_execve && pid == strace_child) { |
| 2037 | /* example: strace -bexecve sh -c 'exec true' */ |
| 2038 | strace_child = 0; |
| 2039 | return NULL; |
| 2040 | } |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2041 | /* |
| 2042 | * This can happen if we inherited an unknown child. |
Dmitry V. Levin | aa80192 | 2015-02-07 18:48:55 +0000 | [diff] [blame] | 2043 | * Example: (sleep 1 & exec strace true) |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2044 | */ |
| 2045 | error_msg("Exit of unknown pid %u ignored", pid); |
| 2046 | return NULL; |
| 2047 | } |
| 2048 | if (followfork) { |
| 2049 | /* We assume it's a fork/vfork/clone child */ |
| 2050 | struct tcb *tcp = alloctcb(pid); |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 2051 | after_successful_attach(tcp, post_attach_sigstop); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2052 | if (!qflag) |
Dmitry V. Levin | 6c8ef05 | 2015-05-25 22:51:19 +0000 | [diff] [blame] | 2053 | error_msg("Process %d attached", pid); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2054 | return tcp; |
| 2055 | } else { |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 2056 | /* |
| 2057 | * This can happen if a clone call misused CLONE_PTRACE itself. |
| 2058 | * |
| 2059 | * There used to be a dance around possible re-injection of |
| 2060 | * WSTOPSIG(status), but it was later removed as the only |
| 2061 | * observable stop here is the initial ptrace-stop. |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2062 | */ |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 2063 | ptrace(PTRACE_DETACH, pid, NULL, 0L); |
| 2064 | error_msg("Detached unknown pid %d", pid); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2065 | return NULL; |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | static struct tcb * |
| 2070 | maybe_switch_tcbs(struct tcb *tcp, const int pid) |
| 2071 | { |
| 2072 | FILE *fp; |
| 2073 | struct tcb *execve_thread; |
| 2074 | long old_pid = 0; |
| 2075 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2076 | if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, &old_pid) < 0) |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2077 | return tcp; |
| 2078 | /* Avoid truncation in pid2tcb() param passing */ |
| 2079 | if (old_pid <= 0 || old_pid == pid) |
| 2080 | return tcp; |
| 2081 | if ((unsigned long) old_pid > UINT_MAX) |
| 2082 | return tcp; |
| 2083 | execve_thread = pid2tcb(old_pid); |
| 2084 | /* It should be !NULL, but I feel paranoid */ |
| 2085 | if (!execve_thread) |
| 2086 | return tcp; |
| 2087 | |
| 2088 | if (execve_thread->curcol != 0) { |
| 2089 | /* |
| 2090 | * One case we are here is -ff: |
| 2091 | * try "strace -oLOG -ff test/threaded_execve" |
| 2092 | */ |
| 2093 | fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid); |
| 2094 | /*execve_thread->curcol = 0; - no need, see code below */ |
| 2095 | } |
| 2096 | /* Swap output FILEs (needed for -ff) */ |
| 2097 | fp = execve_thread->outf; |
| 2098 | execve_thread->outf = tcp->outf; |
| 2099 | tcp->outf = fp; |
| 2100 | /* And their column positions */ |
| 2101 | execve_thread->curcol = tcp->curcol; |
| 2102 | tcp->curcol = 0; |
| 2103 | /* Drop leader, but close execve'd thread outfile (if -ff) */ |
| 2104 | droptcb(tcp); |
| 2105 | /* Switch to the thread, reusing leader's outfile and pid */ |
| 2106 | tcp = execve_thread; |
| 2107 | tcp->pid = pid; |
| 2108 | if (cflag != CFLAG_ONLY_STATS) { |
| 2109 | printleader(tcp); |
| 2110 | tprintf("+++ superseded by execve in pid %lu +++\n", old_pid); |
| 2111 | line_ended(); |
| 2112 | tcp->flags |= TCB_REPRINT; |
| 2113 | } |
| 2114 | |
| 2115 | return tcp; |
| 2116 | } |
| 2117 | |
| 2118 | static void |
Dmitry V. Levin | 0f4ad30 | 2015-02-22 02:13:04 +0000 | [diff] [blame] | 2119 | print_signalled(struct tcb *tcp, const int pid, int status) |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2120 | { |
Dmitry V. Levin | 2cd488b | 2015-02-07 19:41:48 +0000 | [diff] [blame] | 2121 | if (pid == strace_child) { |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2122 | exit_code = 0x100 | WTERMSIG(status); |
Dmitry V. Levin | 2cd488b | 2015-02-07 19:41:48 +0000 | [diff] [blame] | 2123 | strace_child = 0; |
| 2124 | } |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2125 | |
| 2126 | if (cflag != CFLAG_ONLY_STATS |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 2127 | && is_number_in_set(WTERMSIG(status), signal_set)) { |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2128 | printleader(tcp); |
| 2129 | #ifdef WCOREDUMP |
| 2130 | tprintf("+++ killed by %s %s+++\n", |
| 2131 | signame(WTERMSIG(status)), |
| 2132 | WCOREDUMP(status) ? "(core dumped) " : ""); |
| 2133 | #else |
| 2134 | tprintf("+++ killed by %s +++\n", |
| 2135 | signame(WTERMSIG(status))); |
| 2136 | #endif |
| 2137 | line_ended(); |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | static void |
Dmitry V. Levin | 0f4ad30 | 2015-02-22 02:13:04 +0000 | [diff] [blame] | 2142 | print_exited(struct tcb *tcp, const int pid, int status) |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2143 | { |
Dmitry V. Levin | 2cd488b | 2015-02-07 19:41:48 +0000 | [diff] [blame] | 2144 | if (pid == strace_child) { |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2145 | exit_code = WEXITSTATUS(status); |
Dmitry V. Levin | 2cd488b | 2015-02-07 19:41:48 +0000 | [diff] [blame] | 2146 | strace_child = 0; |
| 2147 | } |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2148 | |
| 2149 | if (cflag != CFLAG_ONLY_STATS && |
| 2150 | qflag < 2) { |
| 2151 | printleader(tcp); |
| 2152 | tprintf("+++ exited with %d +++\n", WEXITSTATUS(status)); |
| 2153 | line_ended(); |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | static void |
| 2158 | print_stopped(struct tcb *tcp, const siginfo_t *si, const unsigned int sig) |
| 2159 | { |
| 2160 | if (cflag != CFLAG_ONLY_STATS |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2161 | && !hide_log(tcp) |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 2162 | && is_number_in_set(sig, signal_set)) { |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2163 | printleader(tcp); |
| 2164 | if (si) { |
| 2165 | tprintf("--- %s ", signame(sig)); |
Dmitry V. Levin | f9199ab | 2016-05-13 14:16:12 +0000 | [diff] [blame] | 2166 | printsiginfo(si); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2167 | tprints(" ---\n"); |
| 2168 | } else |
| 2169 | tprintf("--- stopped by %s ---\n", signame(sig)); |
| 2170 | line_ended(); |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2171 | |
| 2172 | #ifdef ENABLE_STACKTRACE |
| 2173 | if (stack_trace_enabled) |
| 2174 | unwind_tcb_print(tcp); |
| 2175 | #endif |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | |
Denys Vlasenko | 4bb2ffd | 2015-03-21 18:13:45 +0100 | [diff] [blame] | 2179 | static void |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2180 | startup_tcb(struct tcb *tcp) |
| 2181 | { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2182 | debug_msg("pid %d has TCB_STARTUP, initializing it", tcp->pid); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2183 | |
| 2184 | tcp->flags &= ~TCB_STARTUP; |
| 2185 | |
Dmitry V. Levin | 23ce9e4 | 2015-02-08 13:05:53 +0000 | [diff] [blame] | 2186 | if (!use_seize) { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2187 | debug_msg("setting opts 0x%x on pid %d", |
| 2188 | ptrace_setoptions, tcp->pid); |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2189 | if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) { |
| 2190 | if (errno != ESRCH) { |
| 2191 | /* Should never happen, really */ |
| 2192 | perror_msg_and_die("PTRACE_SETOPTIONS"); |
| 2193 | } |
| 2194 | } |
| 2195 | } |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 2196 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2197 | if ((tcp->flags & TCB_GRABBED) && (get_scno(tcp) == 1)) |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 2198 | tcp->s_prev_ent = tcp->s_ent; |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2201 | static void |
| 2202 | print_event_exit(struct tcb *tcp) |
| 2203 | { |
| 2204 | if (entering(tcp) || filtered(tcp) || hide_log(tcp) |
| 2205 | || cflag == CFLAG_ONLY_STATS) { |
| 2206 | return; |
| 2207 | } |
| 2208 | |
| 2209 | if (followfork < 2 && printing_tcp && printing_tcp != tcp |
| 2210 | && printing_tcp->curcol != 0) { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2211 | set_current_tcp(printing_tcp); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2212 | tprints(" <unfinished ...>\n"); |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 2213 | flush_tcp_output(printing_tcp); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2214 | printing_tcp->curcol = 0; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2215 | set_current_tcp(tcp); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | if ((followfork < 2 && printing_tcp != tcp) |
| 2219 | || (tcp->flags & TCB_REPRINT)) { |
| 2220 | tcp->flags &= ~TCB_REPRINT; |
| 2221 | printleader(tcp); |
| 2222 | tprintf("<... %s resumed>", tcp->s_ent->sys_name); |
| 2223 | } |
| 2224 | |
| 2225 | if (!(tcp->sys_func_rval & RVAL_DECODED)) { |
| 2226 | /* |
| 2227 | * The decoder has probably decided to print something |
| 2228 | * on exiting syscall which is not going to happen. |
| 2229 | */ |
| 2230 | tprints(" <unfinished ...>"); |
| 2231 | } |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2232 | |
| 2233 | printing_tcp = tcp; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2234 | tprints(") "); |
| 2235 | tabto(); |
| 2236 | tprints("= ?\n"); |
| 2237 | line_ended(); |
| 2238 | } |
| 2239 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2240 | static const struct tcb_wait_data * |
| 2241 | next_event(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2242 | { |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2243 | static struct tcb_wait_data wait_data; |
| 2244 | |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2245 | int pid; |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2246 | int status; |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2247 | struct tcb *tcp; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2248 | struct tcb_wait_data *wd = &wait_data; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2249 | struct rusage ru; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2250 | |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2251 | if (interrupted) |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2252 | return NULL; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2253 | |
Denys Vlasenko | 364728d | 2015-03-21 18:40:53 +0100 | [diff] [blame] | 2254 | /* |
| 2255 | * Used to exit simply when nprocs hits zero, but in this testcase: |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2256 | * int main(void) { _exit(!!fork()); } |
Denys Vlasenko | 364728d | 2015-03-21 18:40:53 +0100 | [diff] [blame] | 2257 | * under strace -f, parent sometimes (rarely) manages |
| 2258 | * to exit before we see the first stop of the child, |
| 2259 | * and we are losing track of it: |
| 2260 | * 19923 clone(...) = 19924 |
| 2261 | * 19923 exit_group(1) = ? |
| 2262 | * 19923 +++ exited with 1 +++ |
| 2263 | * Exiting only when wait() returns ECHILD works better. |
| 2264 | */ |
| 2265 | if (popen_pid != 0) { |
| 2266 | /* However, if -o|logger is in use, we can't do that. |
| 2267 | * Can work around that by double-forking the logger, |
| 2268 | * but that loses the ability to wait for its completion |
| 2269 | * on exit. Oh well... |
| 2270 | */ |
| 2271 | if (nprocs == 0) |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2272 | return NULL; |
Denys Vlasenko | 364728d | 2015-03-21 18:40:53 +0100 | [diff] [blame] | 2273 | } |
Denys Vlasenko | 725dd42 | 2013-06-20 11:06:58 +0200 | [diff] [blame] | 2274 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2275 | const bool unblock_delay_timer = is_delay_timer_armed(); |
| 2276 | |
| 2277 | /* |
| 2278 | * The window of opportunity to handle expirations |
| 2279 | * of the delay timer opens here. |
| 2280 | * |
| 2281 | * Unblock the signal handler for the delay timer |
| 2282 | * iff the delay timer is already created. |
| 2283 | */ |
| 2284 | if (unblock_delay_timer) |
| 2285 | sigprocmask(SIG_UNBLOCK, &timer_set, NULL); |
| 2286 | |
| 2287 | /* |
| 2288 | * If the delay timer has expired, then its expiration |
| 2289 | * has been handled already by the signal handler. |
| 2290 | * |
| 2291 | * If the delay timer expires during wait4(), |
| 2292 | * then the system call will be interrupted and |
| 2293 | * the expiration will be handled by the signal handler. |
| 2294 | */ |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2295 | pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL)); |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2296 | const int wait_errno = errno; |
| 2297 | |
| 2298 | /* |
| 2299 | * The window of opportunity to handle expirations |
| 2300 | * of the delay timer closes here. |
| 2301 | * |
| 2302 | * Block the signal handler for the delay timer |
| 2303 | * iff it was unblocked earlier. |
| 2304 | */ |
| 2305 | if (unblock_delay_timer) { |
| 2306 | sigprocmask(SIG_BLOCK, &timer_set, NULL); |
| 2307 | |
| 2308 | if (restart_failed) |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2309 | return NULL; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2310 | } |
Denys Vlasenko | 7c41ce2 | 2013-07-08 13:55:04 +0200 | [diff] [blame] | 2311 | |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2312 | if (pid < 0) { |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2313 | if (wait_errno == EINTR) { |
| 2314 | wd->te = TE_NEXT; |
| 2315 | return wd; |
| 2316 | } |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2317 | if (nprocs == 0 && wait_errno == ECHILD) |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2318 | return NULL; |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2319 | /* |
| 2320 | * If nprocs > 0, ECHILD is not expected, |
| 2321 | * treat it as any other error here: |
| 2322 | */ |
| 2323 | errno = wait_errno; |
| 2324 | perror_msg_and_die("wait4(__WALL)"); |
| 2325 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2326 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2327 | wd->status = status; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2328 | |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2329 | if (pid == popen_pid) { |
| 2330 | if (!WIFSTOPPED(status)) |
| 2331 | popen_pid = 0; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2332 | wd->te = TE_NEXT; |
| 2333 | return wd; |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2334 | } |
Denys Vlasenko | 725dd42 | 2013-06-20 11:06:58 +0200 | [diff] [blame] | 2335 | |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2336 | if (debug_flag) |
| 2337 | print_debug_info(pid, status); |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2338 | |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2339 | /* Look up 'pid' in our table. */ |
| 2340 | tcp = pid2tcb(pid); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2341 | |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2342 | if (!tcp) { |
| 2343 | tcp = maybe_allocate_tcb(pid, status); |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2344 | if (!tcp) { |
| 2345 | wd->te = TE_NEXT; |
| 2346 | return wd; |
| 2347 | } |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2348 | } |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2349 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2350 | clear_regs(tcp); |
Denys Vlasenko | 7de265d | 2012-03-13 11:44:31 +0100 | [diff] [blame] | 2351 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2352 | /* Set current output file */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2353 | set_current_tcp(tcp); |
Denys Vlasenko | ce7d953 | 2013-02-05 16:36:13 +0100 | [diff] [blame] | 2354 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2355 | if (cflag) { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2356 | struct timespec stime = { |
| 2357 | .tv_sec = ru.ru_stime.tv_sec, |
| 2358 | .tv_nsec = ru.ru_stime.tv_usec * 1000 |
| 2359 | }; |
| 2360 | ts_sub(&tcp->dtime, &stime, &tcp->stime); |
| 2361 | tcp->stime = stime; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2364 | if (WIFSIGNALED(status)) { |
| 2365 | wd->te = TE_SIGNALLED; |
| 2366 | return wd; |
| 2367 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2368 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2369 | if (WIFEXITED(status)) { |
| 2370 | wd->te = TE_EXITED; |
| 2371 | return wd; |
| 2372 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2373 | |
| 2374 | /* |
| 2375 | * As WCONTINUED flag has not been specified to wait4, |
| 2376 | * it cannot be WIFCONTINUED(status), so the only case |
| 2377 | * that remains is WIFSTOPPED(status). |
| 2378 | */ |
| 2379 | |
| 2380 | /* Is this the very first time we see this tracee stopped? */ |
| 2381 | if (tcp->flags & TCB_STARTUP) |
| 2382 | startup_tcb(tcp); |
| 2383 | |
| 2384 | const unsigned int sig = WSTOPSIG(status); |
| 2385 | const unsigned int event = (unsigned int) status >> 16; |
| 2386 | |
| 2387 | switch (event) { |
| 2388 | case 0: |
| 2389 | /* |
| 2390 | * Is this post-attach SIGSTOP? |
| 2391 | * Interestingly, the process may stop |
| 2392 | * with STOPSIG equal to some other signal |
| 2393 | * than SIGSTOP if we happened to attach |
| 2394 | * just before the process takes a signal. |
| 2395 | */ |
| 2396 | if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2397 | debug_func_msg("ignored SIGSTOP on pid %d", tcp->pid); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2398 | tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2399 | wd->te = TE_RESTART; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2400 | } else if (sig == syscall_trap_sig) { |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2401 | wd->te = TE_SYSCALL_STOP; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2402 | } else { |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2403 | memset(&wd->si, 0, sizeof(wd->si)); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2404 | /* |
| 2405 | * True if tracee is stopped by signal |
| 2406 | * (as opposed to "tracee received signal"). |
| 2407 | * TODO: shouldn't we check for errno == EINVAL too? |
| 2408 | * We can get ESRCH instead, you know... |
| 2409 | */ |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2410 | bool stopped = ptrace(PTRACE_GETSIGINFO, pid, 0, &wd->si) < 0; |
| 2411 | wd->te = stopped ? TE_GROUP_STOP : TE_SIGNAL_DELIVERY_STOP; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2412 | } |
| 2413 | break; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2414 | case PTRACE_EVENT_STOP: |
| 2415 | /* |
| 2416 | * PTRACE_INTERRUPT-stop or group-stop. |
| 2417 | * PTRACE_INTERRUPT-stop has sig == SIGTRAP here. |
| 2418 | */ |
| 2419 | switch (sig) { |
| 2420 | case SIGSTOP: |
| 2421 | case SIGTSTP: |
| 2422 | case SIGTTIN: |
| 2423 | case SIGTTOU: |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2424 | wd->te = TE_GROUP_STOP; |
| 2425 | break; |
| 2426 | default: |
| 2427 | wd->te = TE_RESTART; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2428 | } |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2429 | break; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2430 | case PTRACE_EVENT_EXEC: |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2431 | wd->te = TE_STOP_BEFORE_EXECVE; |
| 2432 | break; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2433 | case PTRACE_EVENT_EXIT: |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2434 | wd->te = TE_STOP_BEFORE_EXIT; |
| 2435 | break; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2436 | default: |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2437 | wd->te = TE_RESTART; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2438 | } |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2439 | |
| 2440 | return wd; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | static int |
| 2444 | trace_syscall(struct tcb *tcp, unsigned int *sig) |
| 2445 | { |
| 2446 | if (entering(tcp)) { |
| 2447 | int res = syscall_entering_decode(tcp); |
| 2448 | switch (res) { |
| 2449 | case 0: |
| 2450 | return 0; |
| 2451 | case 1: |
| 2452 | res = syscall_entering_trace(tcp, sig); |
| 2453 | } |
| 2454 | syscall_entering_finish(tcp, res); |
| 2455 | return res; |
| 2456 | } else { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2457 | struct timespec ts = {}; |
| 2458 | int res = syscall_exiting_decode(tcp, &ts); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2459 | if (res != 0) { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2460 | res = syscall_exiting_trace(tcp, &ts, res); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2461 | } |
| 2462 | syscall_exiting_finish(tcp); |
| 2463 | return res; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | /* Returns true iff the main trace loop has to continue. */ |
| 2468 | static bool |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2469 | dispatch_event(const struct tcb_wait_data *wd) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2470 | { |
| 2471 | unsigned int restart_op = PTRACE_SYSCALL; |
| 2472 | unsigned int restart_sig = 0; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2473 | enum trace_event te = wd ? wd->te : TE_BREAK; |
| 2474 | /* |
| 2475 | * Copy wd->status to a non-const variable to workaround glibc bugs |
| 2476 | * around union wait fixed by glibc commit glibc-2.24~391 |
| 2477 | */ |
| 2478 | int status = wd ? wd->status : 0; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2479 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2480 | switch (te) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2481 | case TE_BREAK: |
| 2482 | return false; |
| 2483 | |
| 2484 | case TE_NEXT: |
| 2485 | return true; |
| 2486 | |
| 2487 | case TE_RESTART: |
| 2488 | break; |
| 2489 | |
| 2490 | case TE_SYSCALL_STOP: |
| 2491 | if (trace_syscall(current_tcp, &restart_sig) < 0) { |
| 2492 | /* |
| 2493 | * ptrace() failed in trace_syscall(). |
| 2494 | * Likely a result of process disappearing mid-flight. |
| 2495 | * Observed case: exit_group() or SIGKILL terminating |
| 2496 | * all processes in thread group. |
| 2497 | * We assume that ptrace error was caused by process death. |
| 2498 | * We used to detach(current_tcp) here, but since we no |
| 2499 | * longer implement "detach before death" policy/hack, |
| 2500 | * we can let this process to report its death to us |
| 2501 | * normally, via WIFEXITED or WIFSIGNALED wait status. |
| 2502 | */ |
| 2503 | return true; |
| 2504 | } |
| 2505 | break; |
| 2506 | |
| 2507 | case TE_SIGNAL_DELIVERY_STOP: |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2508 | restart_sig = WSTOPSIG(status); |
| 2509 | print_stopped(current_tcp, &wd->si, restart_sig); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2510 | break; |
| 2511 | |
| 2512 | case TE_SIGNALLED: |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2513 | print_signalled(current_tcp, current_tcp->pid, status); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2514 | droptcb(current_tcp); |
| 2515 | return true; |
| 2516 | |
| 2517 | case TE_GROUP_STOP: |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2518 | restart_sig = WSTOPSIG(status); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2519 | print_stopped(current_tcp, NULL, restart_sig); |
| 2520 | if (use_seize) { |
| 2521 | /* |
| 2522 | * This ends ptrace-stop, but does *not* end group-stop. |
| 2523 | * This makes stopping signals work properly on straced |
| 2524 | * process (that is, process really stops. It used to |
| 2525 | * continue to run). |
| 2526 | */ |
| 2527 | restart_op = PTRACE_LISTEN; |
| 2528 | restart_sig = 0; |
| 2529 | } |
| 2530 | break; |
| 2531 | |
| 2532 | case TE_EXITED: |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2533 | print_exited(current_tcp, current_tcp->pid, status); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2534 | droptcb(current_tcp); |
| 2535 | return true; |
| 2536 | |
| 2537 | case TE_STOP_BEFORE_EXECVE: |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2538 | /* |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2539 | * Check that we are inside syscall now (next event after |
| 2540 | * PTRACE_EVENT_EXEC should be for syscall exiting). If it is |
| 2541 | * not the case, we might have a situation when we attach to a |
| 2542 | * process and the first thing we see is a PTRACE_EVENT_EXEC |
| 2543 | * and all the following syscall state tracking is screwed up |
| 2544 | * otherwise. |
| 2545 | */ |
| 2546 | if (entering(current_tcp)) { |
| 2547 | int ret; |
| 2548 | |
| 2549 | error_msg("Stray PTRACE_EVENT_EXEC from pid %d" |
| 2550 | ", trying to recover...", |
| 2551 | current_tcp->pid); |
| 2552 | |
| 2553 | current_tcp->flags |= TCB_RECOVERING; |
| 2554 | ret = trace_syscall(current_tcp, &restart_sig); |
| 2555 | current_tcp->flags &= ~TCB_RECOVERING; |
| 2556 | |
| 2557 | if (ret < 0) { |
| 2558 | /* The reason is described in TE_SYSCALL_STOP */ |
| 2559 | return true; |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | /* |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2564 | * Under Linux, execve changes pid to thread leader's pid, |
Denys Vlasenko | 8511f2a | 2012-03-22 09:35:51 +0100 | [diff] [blame] | 2565 | * and we see this changed pid on EVENT_EXEC and later, |
| 2566 | * execve sysexit. Leader "disappears" without exit |
| 2567 | * notification. Let user know that, drop leader's tcb, |
| 2568 | * and fix up pid in execve thread's tcb. |
| 2569 | * Effectively, execve thread's tcb replaces leader's tcb. |
| 2570 | * |
| 2571 | * BTW, leader is 'stuck undead' (doesn't report WIFEXITED |
| 2572 | * on exit syscall) in multithreaded programs exactly |
| 2573 | * in order to handle this case. |
| 2574 | * |
| 2575 | * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0. |
| 2576 | * On 2.6 and earlier, it can return garbage. |
| 2577 | */ |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2578 | if (os_release >= KERNEL_VERSION(3, 0, 0)) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 2579 | set_current_tcp(maybe_switch_tcbs(current_tcp, |
| 2580 | current_tcp->pid)); |
Denys Vlasenko | 8511f2a | 2012-03-22 09:35:51 +0100 | [diff] [blame] | 2581 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2582 | if (detach_on_execve) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2583 | if (current_tcp->flags & TCB_SKIP_DETACH_ON_FIRST_EXEC) { |
| 2584 | current_tcp->flags &= ~TCB_SKIP_DETACH_ON_FIRST_EXEC; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2585 | } else { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2586 | detach(current_tcp); /* do "-b execve" thingy */ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 2587 | return true; |
| 2588 | } |
Dmitry V. Levin | e690813 | 2015-02-07 17:47:53 +0000 | [diff] [blame] | 2589 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2590 | break; |
Denys Vlasenko | 8511f2a | 2012-03-22 09:35:51 +0100 | [diff] [blame] | 2591 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2592 | case TE_STOP_BEFORE_EXIT: |
| 2593 | print_event_exit(current_tcp); |
| 2594 | break; |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /* We handled quick cases, we are permitted to interrupt now. */ |
| 2598 | if (interrupted) |
| 2599 | return false; |
| 2600 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2601 | /* If the process is being delayed, do not ptrace_restart just yet */ |
| 2602 | if (syscall_delayed(current_tcp)) |
| 2603 | return true; |
| 2604 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2605 | if (ptrace_restart(restart_op, current_tcp, restart_sig) < 0) { |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2606 | /* Note: ptrace_restart emitted error message */ |
| 2607 | exit_code = 1; |
| 2608 | return false; |
| 2609 | } |
Dmitry V. Levin | 4b4ec12 | 2015-02-07 17:31:54 +0000 | [diff] [blame] | 2610 | return true; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2611 | } |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2612 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2613 | static bool |
| 2614 | restart_delayed_tcb(struct tcb *const tcp) |
| 2615 | { |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2616 | const struct tcb_wait_data wd = { .te = TE_RESTART }; |
| 2617 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2618 | debug_func_msg("pid %d", tcp->pid); |
| 2619 | |
| 2620 | tcp->flags &= ~TCB_DELAYED; |
| 2621 | |
| 2622 | struct tcb *const prev_tcp = current_tcp; |
| 2623 | current_tcp = tcp; |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2624 | bool ret = dispatch_event(&wd); |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2625 | current_tcp = prev_tcp; |
| 2626 | |
| 2627 | return ret; |
| 2628 | } |
| 2629 | |
| 2630 | static bool |
| 2631 | restart_delayed_tcbs(void) |
| 2632 | { |
| 2633 | struct tcb *tcp_next = NULL; |
| 2634 | struct timespec ts_now; |
| 2635 | |
| 2636 | clock_gettime(CLOCK_MONOTONIC, &ts_now); |
| 2637 | |
| 2638 | for (size_t i = 0; i < tcbtabsize; i++) { |
| 2639 | struct tcb *tcp = tcbtab[i]; |
| 2640 | |
| 2641 | if (tcp->pid && syscall_delayed(tcp)) { |
| 2642 | if (ts_cmp(&ts_now, &tcp->delay_expiration_time) > 0) { |
| 2643 | if (!restart_delayed_tcb(tcp)) |
| 2644 | return false; |
| 2645 | } else { |
| 2646 | /* Check whether this tcb is the next. */ |
| 2647 | if (!tcp_next || |
| 2648 | ts_cmp(&tcp_next->delay_expiration_time, |
| 2649 | &tcp->delay_expiration_time) > 0) { |
| 2650 | tcp_next = tcp; |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | if (tcp_next) |
| 2657 | arm_delay_timer(tcp_next); |
| 2658 | |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
| 2662 | /* |
| 2663 | * As this signal handler does a lot of work that is not suitable |
| 2664 | * for signal handlers, extra care must be taken to ensure that |
| 2665 | * it is enabled only in those places where it's safe. |
| 2666 | */ |
| 2667 | static void |
| 2668 | timer_sighandler(int sig) |
| 2669 | { |
| 2670 | delay_timer_expired(); |
| 2671 | |
| 2672 | if (restart_failed) |
| 2673 | return; |
| 2674 | |
| 2675 | int saved_errno = errno; |
| 2676 | |
| 2677 | if (!restart_delayed_tcbs()) |
| 2678 | restart_failed = 1; |
| 2679 | |
| 2680 | errno = saved_errno; |
| 2681 | } |
| 2682 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2683 | #ifdef ENABLE_COVERAGE_GCOV |
| 2684 | extern void __gcov_flush(void); |
| 2685 | #endif |
| 2686 | |
| 2687 | static void ATTRIBUTE_NORETURN |
| 2688 | terminate(void) |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2689 | { |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2690 | cleanup(); |
| 2691 | fflush(NULL); |
Dmitry V. Levin | cf53436 | 2012-07-12 20:54:46 +0000 | [diff] [blame] | 2692 | if (shared_log != stderr) |
| 2693 | fclose(shared_log); |
| 2694 | if (popen_pid) { |
| 2695 | while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR) |
| 2696 | ; |
| 2697 | } |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2698 | if (exit_code > 0xff) { |
| 2699 | /* Avoid potential core file clobbering. */ |
Dmitry V. Levin | c8938e0 | 2013-03-20 21:40:15 +0000 | [diff] [blame] | 2700 | struct_rlimit rlim = {0, 0}; |
| 2701 | set_rlimit(RLIMIT_CORE, &rlim); |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2702 | |
| 2703 | /* Child was killed by a signal, mimic that. */ |
| 2704 | exit_code &= 0xff; |
| 2705 | signal(exit_code, SIG_DFL); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2706 | #ifdef ENABLE_COVERAGE_GCOV |
| 2707 | __gcov_flush(); |
| 2708 | #endif |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2709 | raise(exit_code); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2710 | |
| 2711 | /* Unblock the signal. */ |
| 2712 | sigset_t mask; |
| 2713 | sigemptyset(&mask); |
| 2714 | sigaddset(&mask, exit_code); |
| 2715 | #ifdef ENABLE_COVERAGE_GCOV |
| 2716 | __gcov_flush(); |
| 2717 | #endif |
| 2718 | sigprocmask(SIG_UNBLOCK, &mask, NULL); |
| 2719 | |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2720 | /* Paranoia - what if this signal is not fatal? |
| 2721 | Exit with 128 + signo then. */ |
| 2722 | exit_code += 128; |
| 2723 | } |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2724 | exit(exit_code); |
| 2725 | } |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2726 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2727 | int |
| 2728 | main(int argc, char *argv[]) |
| 2729 | { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 2730 | setlocale(LC_ALL, ""); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2731 | init(argc, argv); |
| 2732 | |
| 2733 | exit_code = !nprocs; |
| 2734 | |
Elliott Hughes | c187376 | 2018-12-19 15:13:36 -0800 | [diff] [blame] | 2735 | while (dispatch_event(next_event())) |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 2736 | ; |
| 2737 | terminate(); |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2738 | } |