144584Smarc /*- 236426Smarc * Copyright (c) 1988 The Regents of the University of California. 336426Smarc * All rights reserved. 436426Smarc * 544584Smarc * %sccs.include.redist.c% 636426Smarc */ 736426Smarc 836426Smarc #ifndef lint 936426Smarc char copyright[] = 1036426Smarc "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ 1136426Smarc All rights reserved.\n"; 1236426Smarc #endif /* not lint */ 1336426Smarc 1436426Smarc #ifndef lint 15*47026Smarc static char sccsid[] = "@(#)ktrace.c 5.2 (Berkeley) 03/05/91"; 1636426Smarc #endif /* not lint */ 1736426Smarc 1845987Sbostic #include <sys/param.h> 1945987Sbostic #include <sys/stat.h> 2045987Sbostic #include <sys/file.h> 2145987Sbostic #include <sys/time.h> 2245987Sbostic #include <sys/errno.h> 2345987Sbostic #include <sys/uio.h> 2445987Sbostic #include <sys/ktrace.h> 2545987Sbostic #include <stdio.h> 2636462Smarc #include "ktrace.h" 2736426Smarc 2836426Smarc main(argc, argv) 2945987Sbostic int argc; 3045987Sbostic char **argv; 3136426Smarc { 3236426Smarc extern int optind; 3336426Smarc extern char *optarg; 3445987Sbostic enum { NOTSET, CLEAR, CLEARALL } clear; 3545987Sbostic int append, ch, fd, inherit, ops, pid, pidset, trpoints; 3645987Sbostic char *tracefile; 3736426Smarc 3845987Sbostic clear = NOTSET; 3945987Sbostic append = ops = pidset = 0; 4045987Sbostic trpoints = ALL_POINTS; 4145987Sbostic tracefile = DEF_TRACEFILE; 4245987Sbostic while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF) 4336426Smarc switch((char)ch) { 4445987Sbostic case 'a': 4545987Sbostic append = 1; 4645987Sbostic break; 4740361Smarc case 'C': 4845987Sbostic clear = CLEARALL; 49*47026Smarc pidset = 1; 5040361Smarc break; 5140361Smarc case 'c': 5245987Sbostic clear = CLEAR; 5340361Smarc break; 5440361Smarc case 'd': 5540361Smarc ops |= KTRFLAG_DESCEND; 5640361Smarc break; 5745987Sbostic case 'f': 5845987Sbostic tracefile = optarg; 5940361Smarc break; 6040361Smarc case 'g': 6145987Sbostic pid = -rpid(optarg); 6245987Sbostic pidset = 1; 6340361Smarc break; 6440361Smarc case 'i': 6545987Sbostic inherit = 1; 6640361Smarc break; 6745987Sbostic case 'p': 6845987Sbostic pid = rpid(optarg); 6945987Sbostic pidset = 1; 7040361Smarc break; 7145987Sbostic case 't': 7245987Sbostic trpoints = getpoints(optarg); 7345987Sbostic if (trpoints < 0) { 7445987Sbostic (void)fprintf(stderr, 7545987Sbostic "ktrace: unknown facility in %s\n", optarg); 7645987Sbostic usage(); 7745987Sbostic } 7840361Smarc break; 7940361Smarc default: 8045987Sbostic usage(); 8136426Smarc } 8245987Sbostic argv += optind; 8345987Sbostic argc -= optind; 8436426Smarc 8545987Sbostic if (pidset && *argv || !pidset && !*argv) 8645987Sbostic usage(); 8745987Sbostic 8840361Smarc if (inherit) 8944584Smarc trpoints |= KTRFAC_INHERIT; 9045987Sbostic 9145987Sbostic if (clear != NOTSET) { 9245987Sbostic if (clear == CLEARALL) { 9340361Smarc ops = KTROP_CLEAR | KTRFLAG_DESCEND; 9440361Smarc pid = 1; 9545987Sbostic } else 9640361Smarc ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; 9745987Sbostic 9845987Sbostic if (ktrace(tracefile, ops, trpoints, pid) < 0) 9945987Sbostic error(tracefile); 10036426Smarc exit(0); 10136426Smarc } 10236462Smarc 10345987Sbostic if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC), 10445987Sbostic DEFFILEMODE)) < 0) 10545987Sbostic error(tracefile); 10645987Sbostic (void)close(fd); 10745987Sbostic 10845987Sbostic if (*argv) { 10945987Sbostic if (ktrace(tracefile, ops, trpoints, getpid()) < 0) 11045987Sbostic error(); 11145987Sbostic execvp(argv[0], &argv[0]); 11245987Sbostic error(argv[0]); 11336462Smarc exit(1); 11436462Smarc } 11545987Sbostic else if (ktrace(tracefile, ops, trpoints, pid) < 0) 11645987Sbostic error(tracefile); 11736426Smarc exit(0); 11836426Smarc } 11945987Sbostic 12045987Sbostic rpid(p) 12145987Sbostic char *p; 12245987Sbostic { 12345987Sbostic static int first; 12445987Sbostic 12545987Sbostic if (first++) { 12645987Sbostic (void)fprintf(stderr, 12745987Sbostic "ktrace: only one -g or -p flag is permitted.\n"); 12845987Sbostic usage(); 12945987Sbostic } 13045987Sbostic if (!*p) { 13145987Sbostic (void)fprintf(stderr, "ktrace: illegal process id.\n"); 13245987Sbostic usage(); 13345987Sbostic } 13445987Sbostic return(atoi(p)); 13545987Sbostic } 13645987Sbostic 13745987Sbostic error(name) 13845987Sbostic char *name; 13945987Sbostic { 14045987Sbostic (void)fprintf(stderr, "ktrace: %s: %s.\n", name, strerror(errno)); 14145987Sbostic exit(1); 14245987Sbostic } 14345987Sbostic 14445987Sbostic usage() 14545987Sbostic { 14645987Sbostic (void)fprintf(stderr, 14745987Sbostic "usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n"); 14845987Sbostic exit(1); 14945987Sbostic } 150