#include <sys/cdefs.h>
#ifndef lint
__unused static char copyright[] =
"@(#) Copyright (c) 1988, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
#if 0
__unused static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93";
#endif
__unused static const char rcsid[] =
"$FreeBSD: src/usr.bin/ktrace/ktrace.c,v 1.12.2.3 2001/07/11 00:29:27 mikeh Exp $";
#endif
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/ktrace.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ktrace.h"
void no_ktrace __P((int));
void usage __P((void));
int rpid(char *p);
int
main(int argc, char *argv[])
{
enum { NOTSET, CLEAR, CLEARALL } clear;
int append;
int ch;
int fd;
int inherit;
int ops;
int pid = 1;
int pidset;
int trpoints;
char *tracefile;
mode_t omask;
struct stat sb;
clear = NOTSET;
append = ops = pidset = inherit = 0;
trpoints = DEF_POINTS;
tracefile = DEF_TRACEFILE;
while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
switch((char)ch) {
case 'a':
append = 1;
break;
case 'C':
clear = CLEARALL;
pidset = 1;
break;
case 'c':
clear = CLEAR;
break;
case 'd':
ops |= KTRFLAG_DESCEND;
break;
case 'f':
tracefile = optarg;
break;
case 'g':
pid = -rpid(optarg);
pidset = 1;
break;
case 'i':
inherit = 1;
break;
case 'p':
pid = rpid(optarg);
pidset = 1;
break;
case 't':
trpoints = getpoints(optarg);
if (trpoints < 0) {
warnx("unknown facility in %s", optarg);
usage();
}
break;
default:
usage();
}
argv += optind;
argc -= optind;
if (pidset && *argv || !pidset && !*argv)
usage();
if (inherit)
trpoints |= KTRFAC_INHERIT;
(void)signal(SIGSYS, no_ktrace);
if (clear != NOTSET) {
if (clear == CLEARALL) {
ops = KTROP_CLEAR | KTRFLAG_DESCEND;
trpoints = ALL_POINTS;
pid = 1;
} else
ops |= pidset ? KTROP_CLEAR : KTROP_CLEARFILE;
if (ktrace(tracefile, ops, trpoints, pid) < 0)
err(1, "%s", tracefile);
exit(0);
}
omask = umask(S_IRWXG|S_IRWXO);
if (append) {
if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) < 0)
err(1, "%s", tracefile);
if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
errx(1, "Refuse to append to %s not owned by you.",
tracefile);
} else {
if (unlink(tracefile) == -1 && errno != ENOENT)
err(1, "unlink %s", tracefile);
if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
DEFFILEMODE)) < 0)
err(1, "%s", tracefile);
}
(void)umask(omask);
(void)close(fd);
if (*argv) {
if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
err(1, "%s", tracefile);
execvp(argv[0], &argv[0]);
err(1, "exec of '%s' failed", argv[0]);
}
else if (ktrace(tracefile, ops, trpoints, pid) < 0)
err(1, "%s", tracefile);
exit(0);
}
int
rpid(char *p)
{
static int first;
if (first++) {
warnx("only one -g or -p flag is permitted.");
usage();
}
if (!*p) {
warnx("illegal process id.");
usage();
}
return(atoi(p));
}
void
usage(void)
{
(void)fprintf(stderr, "%s\n%s\n",
"usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t cnisuw]",
" ktrace [-adi] [-f trfile] [-t cnisuw] command");
exit(1);
}
void
no_ktrace(int sig)
{
(void)fprintf(stderr,
"error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
exit(1);
}