1 /*- 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1988, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #if 0 37 #ifndef lint 38 static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93"; 39 #endif /* not lint */ 40 #endif 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/file.h> 47 #include <sys/stat.h> 48 #include <sys/time.h> 49 #include <sys/uio.h> 50 #include <sys/ktrace.h> 51 52 #include <err.h> 53 #include <errno.h> 54 #include <inttypes.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <unistd.h> 58 59 #include "ktrace.h" 60 61 static char def_tracefile[] = DEF_TRACEFILE; 62 63 static enum clear { NOTSET, CLEAR, CLEARALL } clear = NOTSET; 64 static int pid; 65 66 static void no_ktrace(int); 67 static void set_pid_clear(const char *, enum clear); 68 static void usage(void); 69 70 int 71 main(int argc, char *argv[]) 72 { 73 int append, ch, fd, inherit, ops, trpoints; 74 const char *tracefile; 75 mode_t omask; 76 struct stat sb; 77 78 append = ops = inherit = 0; 79 trpoints = DEF_POINTS; 80 tracefile = def_tracefile; 81 while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1) 82 switch((char)ch) { 83 case 'a': 84 append = 1; 85 break; 86 case 'C': 87 set_pid_clear("1", CLEARALL); 88 break; 89 case 'c': 90 set_pid_clear(NULL, CLEAR); 91 break; 92 case 'd': 93 ops |= KTRFLAG_DESCEND; 94 break; 95 case 'f': 96 tracefile = optarg; 97 break; 98 case 'g': 99 set_pid_clear(optarg, NOTSET); 100 pid = -pid; 101 break; 102 case 'i': 103 inherit = 1; 104 break; 105 case 'p': 106 set_pid_clear(optarg, NOTSET); 107 break; 108 case 't': 109 trpoints = getpoints(optarg); 110 if (trpoints < 0) { 111 warnx("unknown facility in %s", optarg); 112 usage(); 113 } 114 break; 115 default: 116 usage(); 117 } 118 119 argv += optind; 120 argc -= optind; 121 122 /* must have either -[Cc], a pid or a command */ 123 if (clear == NOTSET && pid == 0 && argc == 0) 124 usage(); 125 /* can't have both a pid and a command */ 126 /* (note that -C sets pid to 1) */ 127 if (pid != 0 && argc > 0) { 128 usage(); 129 } 130 131 if (inherit) 132 trpoints |= KTRFAC_INHERIT; 133 134 (void)signal(SIGSYS, no_ktrace); 135 if (clear != NOTSET) { 136 if (clear == CLEARALL) { 137 ops = KTROP_CLEAR | KTRFLAG_DESCEND; 138 trpoints = ALL_POINTS; 139 } else { 140 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; 141 } 142 if (ktrace(tracefile, ops, trpoints, pid) < 0) 143 err(1, "%s", tracefile); 144 exit(0); 145 } 146 147 omask = umask(S_IRWXG|S_IRWXO); 148 if (append) { 149 if ((fd = open(tracefile, O_CREAT | O_WRONLY | O_NONBLOCK, 150 DEFFILEMODE)) < 0) 151 err(1, "%s", tracefile); 152 if (fstat(fd, &sb) != 0 || sb.st_uid != getuid()) 153 errx(1, "refuse to append to %s not owned by you", 154 tracefile); 155 if (!(S_ISREG(sb.st_mode))) 156 errx(1, "%s not regular file", tracefile); 157 } else { 158 if (unlink(tracefile) == -1 && errno != ENOENT) 159 err(1, "unlink %s", tracefile); 160 if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY, 161 DEFFILEMODE)) < 0) 162 err(1, "%s", tracefile); 163 } 164 (void)umask(omask); 165 (void)close(fd); 166 167 trpoints |= PROC_ABI_POINTS; 168 169 if (argc > 0) { 170 if (ktrace(tracefile, ops, trpoints, getpid()) < 0) 171 err(1, "%s", tracefile); 172 execvp(*argv, argv); 173 err(1, "exec of '%s' failed", *argv); 174 } 175 if (ktrace(tracefile, ops, trpoints, pid) < 0) 176 err(1, "%s", tracefile); 177 exit(0); 178 } 179 180 static void 181 set_pid_clear(const char *p, enum clear cl) 182 { 183 intmax_t n; 184 char *e; 185 186 if (clear != NOTSET && cl != NOTSET) { 187 /* either -c and -C or either of them twice */ 188 warnx("only one -c or -C flag is permitted"); 189 usage(); 190 } 191 if ((clear == CLEARALL && p != NULL) || (cl == CLEARALL && pid != 0)) { 192 /* both -C and a pid or pgid */ 193 warnx("the -C flag may not be combined with -g or -p"); 194 usage(); 195 } 196 if (p != NULL && pid != 0) { 197 /* either -p and -g or either of them twice */ 198 warnx("only one -g or -p flag is permitted"); 199 usage(); 200 } 201 if (p != NULL) { 202 errno = 0; 203 n = strtoimax(p, &e, 10); 204 /* 205 * 1) not a number, or outside the range of an intmax_t 206 * 2) inside the range of intmax_t but outside the range 207 * of an int, keeping in mind that the pid may be 208 * negated if it's actually a pgid. 209 */ 210 if (*e != '\0' || n < 1 || errno == ERANGE || 211 n > (intmax_t)INT_MAX || n > -(intmax_t)INT_MIN) { 212 warnx("invalid process or group id"); 213 usage(); 214 } 215 pid = n; 216 } 217 if (cl != NOTSET) 218 if ((clear = cl) == CLEARALL) 219 pid = 1; 220 } 221 222 static void 223 usage(void) 224 { 225 226 fprintf(stderr, "%s\n%s\n", 227 "usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t trstr]", 228 " ktrace [-adi] [-f trfile] [-t trstr] command"); 229 exit(1); 230 } 231 232 static void 233 no_ktrace(int sig __unused) 234 { 235 236 fprintf(stderr, "error:\t%s\n\t%s\n", 237 "ktrace() system call not supported in the running kernel", 238 "re-compile kernel with 'options KTRACE'"); 239 exit(1); 240 } 241