19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1988, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 35ebdb213bSDavid Malone static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 400bff6132SJames Raynard #if 0 4165b386f1SPhilippe Charnier #ifndef lint 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93"; 439b50d902SRodney W. Grimes #endif /* not lint */ 4465b386f1SPhilippe Charnier #endif 4565b386f1SPhilippe Charnier 46ebdb213bSDavid Malone #include <sys/cdefs.h> 47ebdb213bSDavid Malone __FBSDID("$FreeBSD$"); 48ebdb213bSDavid Malone 499b50d902SRodney W. Grimes #include <sys/param.h> 509b50d902SRodney W. Grimes #include <sys/stat.h> 519b50d902SRodney W. Grimes #include <sys/file.h> 529b50d902SRodney W. Grimes #include <sys/time.h> 539b50d902SRodney W. Grimes #include <sys/errno.h> 549b50d902SRodney W. Grimes #include <sys/uio.h> 559b50d902SRodney W. Grimes #include <sys/ktrace.h> 560bff6132SJames Raynard 570bff6132SJames Raynard #include <err.h> 5801588bbdSMark Murray #include <stdlib.h> 599b50d902SRodney W. Grimes #include <stdio.h> 60ebdb213bSDavid Malone #include <stdlib.h> 610bff6132SJames Raynard #include <unistd.h> 620bff6132SJames Raynard 639b50d902SRodney W. Grimes #include "ktrace.h" 649b50d902SRodney W. Grimes 6501588bbdSMark Murray static char def_tracefile[] = DEF_TRACEFILE; 6601588bbdSMark Murray 67ebdb213bSDavid Malone static void no_ktrace(int); 68ebdb213bSDavid Malone static int rpid(char *); 69ebdb213bSDavid Malone static void usage(void); 70b0130767SAndreas Schulz 71ebdb213bSDavid Malone int 7201588bbdSMark Murray main(int argc, char *argv[]) 739b50d902SRodney W. Grimes { 749b50d902SRodney W. Grimes enum { NOTSET, CLEAR, CLEARALL } clear; 759b50d902SRodney W. Grimes int append, ch, fd, inherit, ops, pid, pidset, trpoints; 76ebdb213bSDavid Malone const char *tracefile; 774ac5adf0SJoerg Wunsch mode_t omask; 789bedbe6cSJoerg Wunsch struct stat sb; 799b50d902SRodney W. Grimes 809b50d902SRodney W. Grimes clear = NOTSET; 819b50d902SRodney W. Grimes append = ops = pidset = inherit = 0; 829b50d902SRodney W. Grimes trpoints = DEF_POINTS; 8301588bbdSMark Murray tracefile = def_tracefile; 841c8af878SWarner Losh while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1) 859b50d902SRodney W. Grimes switch((char)ch) { 869b50d902SRodney W. Grimes case 'a': 879b50d902SRodney W. Grimes append = 1; 889b50d902SRodney W. Grimes break; 899b50d902SRodney W. Grimes case 'C': 909b50d902SRodney W. Grimes clear = CLEARALL; 919b50d902SRodney W. Grimes pidset = 1; 929b50d902SRodney W. Grimes break; 939b50d902SRodney W. Grimes case 'c': 949b50d902SRodney W. Grimes clear = CLEAR; 959b50d902SRodney W. Grimes break; 969b50d902SRodney W. Grimes case 'd': 979b50d902SRodney W. Grimes ops |= KTRFLAG_DESCEND; 989b50d902SRodney W. Grimes break; 999b50d902SRodney W. Grimes case 'f': 1009b50d902SRodney W. Grimes tracefile = optarg; 1019b50d902SRodney W. Grimes break; 1029b50d902SRodney W. Grimes case 'g': 1039b50d902SRodney W. Grimes pid = -rpid(optarg); 1049b50d902SRodney W. Grimes pidset = 1; 1059b50d902SRodney W. Grimes break; 1069b50d902SRodney W. Grimes case 'i': 1079b50d902SRodney W. Grimes inherit = 1; 1089b50d902SRodney W. Grimes break; 1099b50d902SRodney W. Grimes case 'p': 1109b50d902SRodney W. Grimes pid = rpid(optarg); 1119b50d902SRodney W. Grimes pidset = 1; 1129b50d902SRodney W. Grimes break; 1139b50d902SRodney W. Grimes case 't': 1149b50d902SRodney W. Grimes trpoints = getpoints(optarg); 1159b50d902SRodney W. Grimes if (trpoints < 0) { 1160bff6132SJames Raynard warnx("unknown facility in %s", optarg); 1179b50d902SRodney W. Grimes usage(); 1189b50d902SRodney W. Grimes } 1199b50d902SRodney W. Grimes break; 1209b50d902SRodney W. Grimes default: 1219b50d902SRodney W. Grimes usage(); 1229b50d902SRodney W. Grimes } 1239b50d902SRodney W. Grimes argv += optind; 1249b50d902SRodney W. Grimes argc -= optind; 1259b50d902SRodney W. Grimes 126ebdb213bSDavid Malone if ((pidset && *argv) || (!pidset && !*argv)) 1279b50d902SRodney W. Grimes usage(); 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes if (inherit) 1309b50d902SRodney W. Grimes trpoints |= KTRFAC_INHERIT; 1319b50d902SRodney W. Grimes 1320bff6132SJames Raynard (void)signal(SIGSYS, no_ktrace); 1339b50d902SRodney W. Grimes if (clear != NOTSET) { 1349b50d902SRodney W. Grimes if (clear == CLEARALL) { 1359b50d902SRodney W. Grimes ops = KTROP_CLEAR | KTRFLAG_DESCEND; 1369b50d902SRodney W. Grimes trpoints = ALL_POINTS; 1379b50d902SRodney W. Grimes pid = 1; 1389b50d902SRodney W. Grimes } else 13924b9685cSMike Heffner ops |= pidset ? KTROP_CLEAR : KTROP_CLEARFILE; 1409b50d902SRodney W. Grimes 1419b50d902SRodney W. Grimes if (ktrace(tracefile, ops, trpoints, pid) < 0) 1420c4d24a7SKris Kennaway err(1, "%s", tracefile); 1439b50d902SRodney W. Grimes exit(0); 1449b50d902SRodney W. Grimes } 1459b50d902SRodney W. Grimes 1464ac5adf0SJoerg Wunsch omask = umask(S_IRWXG|S_IRWXO); 1479bedbe6cSJoerg Wunsch if (append) { 1489e92c8d4SRobert Watson if ((fd = open(tracefile, O_CREAT | O_WRONLY | O_NONBLOCK, 1499e92c8d4SRobert Watson DEFFILEMODE)) < 0) 1500c4d24a7SKris Kennaway err(1, "%s", tracefile); 1519bedbe6cSJoerg Wunsch if (fstat(fd, &sb) != 0 || sb.st_uid != getuid()) 15265b386f1SPhilippe Charnier errx(1, "refuse to append to %s not owned by you", 1539bedbe6cSJoerg Wunsch tracefile); 1549e92c8d4SRobert Watson if (!(S_ISREG(sb.st_mode))) 1559e92c8d4SRobert Watson errx(1, "%s not regular file", tracefile); 1569bedbe6cSJoerg Wunsch } else { 1579bedbe6cSJoerg Wunsch if (unlink(tracefile) == -1 && errno != ENOENT) 1589bedbe6cSJoerg Wunsch err(1, "unlink %s", tracefile); 1599bedbe6cSJoerg Wunsch if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY, 1609b50d902SRodney W. Grimes DEFFILEMODE)) < 0) 1610c4d24a7SKris Kennaway err(1, "%s", tracefile); 1629bedbe6cSJoerg Wunsch } 1634ac5adf0SJoerg Wunsch (void)umask(omask); 1649b50d902SRodney W. Grimes (void)close(fd); 1659b50d902SRodney W. Grimes 1669b50d902SRodney W. Grimes if (*argv) { 1679b50d902SRodney W. Grimes if (ktrace(tracefile, ops, trpoints, getpid()) < 0) 1680c4d24a7SKris Kennaway err(1, "%s", tracefile); 1699b50d902SRodney W. Grimes execvp(argv[0], &argv[0]); 1700bff6132SJames Raynard err(1, "exec of '%s' failed", argv[0]); 1719b50d902SRodney W. Grimes } 1729b50d902SRodney W. Grimes else if (ktrace(tracefile, ops, trpoints, pid) < 0) 1730c4d24a7SKris Kennaway err(1, "%s", tracefile); 1749b50d902SRodney W. Grimes exit(0); 1759b50d902SRodney W. Grimes } 1769b50d902SRodney W. Grimes 177ebdb213bSDavid Malone static int 17801588bbdSMark Murray rpid(char *p) 1799b50d902SRodney W. Grimes { 1809b50d902SRodney W. Grimes static int first; 1819b50d902SRodney W. Grimes 1829b50d902SRodney W. Grimes if (first++) { 18365b386f1SPhilippe Charnier warnx("only one -g or -p flag is permitted"); 1849b50d902SRodney W. Grimes usage(); 1859b50d902SRodney W. Grimes } 1869b50d902SRodney W. Grimes if (!*p) { 18765b386f1SPhilippe Charnier warnx("illegal process id"); 1889b50d902SRodney W. Grimes usage(); 1899b50d902SRodney W. Grimes } 1909b50d902SRodney W. Grimes return(atoi(p)); 1919b50d902SRodney W. Grimes } 1929b50d902SRodney W. Grimes 193ebdb213bSDavid Malone static void 19401588bbdSMark Murray usage(void) 1959b50d902SRodney W. Grimes { 1964a744ef6SPhilippe Charnier (void)fprintf(stderr, "%s\n%s\n", 19724b9685cSMike Heffner "usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t cnisuw]", 19824b9685cSMike Heffner " ktrace [-adi] [-f trfile] [-t cnisuw] command"); 1999b50d902SRodney W. Grimes exit(1); 2009b50d902SRodney W. Grimes } 2010bff6132SJames Raynard 202ebdb213bSDavid Malone static void 20301588bbdSMark Murray no_ktrace(int sig __unused) 2040bff6132SJames Raynard { 2050bff6132SJames Raynard (void)fprintf(stderr, 2060bff6132SJames Raynard "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n"); 2070bff6132SJames Raynard exit(1); 2080bff6132SJames Raynard } 209