136426Smarc /* 236426Smarc * Copyright (c) 1988 The Regents of the University of California. 336426Smarc * All rights reserved. 436426Smarc * 536426Smarc * Redistribution and use in source and binary forms are permitted 636426Smarc * provided that the above copyright notice and this paragraph are 736426Smarc * duplicated in all such forms and that any documentation, 836426Smarc * advertising materials, and other materials related to such 936426Smarc * distribution and use acknowledge that the software was developed 1036426Smarc * by the University of California, Berkeley. The name of the 1136426Smarc * University may not be used to endorse or promote products derived 1236426Smarc * from this software without specific prior written permission. 1336426Smarc * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1436426Smarc * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1536426Smarc * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1636426Smarc */ 1736426Smarc 1836426Smarc #ifndef lint 1936426Smarc char copyright[] = 2036426Smarc "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ 2136426Smarc All rights reserved.\n"; 2236426Smarc #endif /* not lint */ 2336426Smarc 2436426Smarc #ifndef lint 25*40361Smarc static char sccsid[] = "@(#)ktrace.c 1.4 (Berkeley) 03/07/90"; 2636426Smarc #endif /* not lint */ 2736426Smarc 2836462Smarc #include "ktrace.h" 2936426Smarc 3036462Smarc #define USAGE \ 31*40361Smarc "usage: ktrace [-acid] [-f trfile] [-t trops] [-p pid] [-g pgid]\n\ 32*40361Smarc trops: c = syscalls, n = namei, g = generic-i/o, a = everything\n\ 33*40361Smarc ktrace -C (clear everthing)\n" 3436462Smarc 3536427Smarc 3636462Smarc char *tracefile = DEF_TRACEFILE; 37*40361Smarc int append, clear, descend, inherit; 3836427Smarc 3936426Smarc main(argc, argv) 4036426Smarc char *argv[]; 4136426Smarc { 4236426Smarc extern int optind; 4336426Smarc extern char *optarg; 44*40361Smarc int facs = ALL_FACS; 4536426Smarc int ops = 0; 4636462Smarc int pid = 0; 4736426Smarc int ch; 4836426Smarc 49*40361Smarc while ((ch = getopt(argc,argv,"Cacdp:g:if:t:")) != EOF) 5036426Smarc switch((char)ch) { 51*40361Smarc case 'C': 52*40361Smarc clear = 2; 53*40361Smarc break; 54*40361Smarc case 'c': 55*40361Smarc clear = 1; 56*40361Smarc break; 57*40361Smarc case 'd': 58*40361Smarc ops |= KTRFLAG_DESCEND; 59*40361Smarc break; 60*40361Smarc case 't': 61*40361Smarc facs = getfacs(optarg); 62*40361Smarc if (facs < 0) { 63*40361Smarc fprintf(stderr, 64*40361Smarc "ktrace: unknown facility in %s\n", 65*40361Smarc optarg); 66*40361Smarc exit(1); 67*40361Smarc } 68*40361Smarc break; 69*40361Smarc case 'p': 70*40361Smarc pid = atoi(optarg); 71*40361Smarc break; 72*40361Smarc case 'g': 73*40361Smarc pid = -atoi(optarg); 74*40361Smarc break; 75*40361Smarc case 'i': 76*40361Smarc inherit++; 77*40361Smarc break; 78*40361Smarc case 'f': 79*40361Smarc tracefile = optarg; 80*40361Smarc break; 81*40361Smarc case 'a': 82*40361Smarc append++; 83*40361Smarc break; 84*40361Smarc default: 85*40361Smarc fprintf(stderr,"usage: \n",*argv); 86*40361Smarc exit(-1); 8736426Smarc } 8836426Smarc argv += optind, argc -= optind; 8936426Smarc 90*40361Smarc if (inherit) 91*40361Smarc facs |= KTRFAC_INHERIT; 9236462Smarc if (clear) { /* untrace something */ 93*40361Smarc if (clear == 2) { /* -C */ 94*40361Smarc ops = KTROP_CLEAR | KTRFLAG_DESCEND; 95*40361Smarc pid = 1; 96*40361Smarc } else { 97*40361Smarc ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; 98*40361Smarc } 9936462Smarc if (ktrace(tracefile, ops, facs, pid) < 0) { 10036462Smarc perror("ktrace"); 10136462Smarc exit(1); 10236462Smarc } 10336426Smarc exit(0); 10436426Smarc } 10536462Smarc 10636462Smarc if (pid == 0 && !*argv) { /* nothing to trace */ 10736462Smarc fprintf(stderr, USAGE); 10836462Smarc exit(1); 10936462Smarc } 11036462Smarc 11136462Smarc close(open(tracefile, O_WRONLY | O_CREAT, 0666)); 11236427Smarc if (!append) 11336427Smarc close(open(tracefile, O_WRONLY | O_TRUNC)); 11436426Smarc if (!*argv) { 11536427Smarc if (ktrace(tracefile, ops, facs, pid) < 0) { 11636426Smarc perror("ktrace"); 11736426Smarc exit(1); 11836426Smarc } 11936426Smarc } else { 12036426Smarc pid = getpid(); 12136427Smarc if (ktrace(tracefile, ops, facs, pid) < 0) { 12236426Smarc perror("ktrace"); 12336426Smarc exit(1); 12436426Smarc } 12536426Smarc execvp(argv[0], &argv[0]); 12636426Smarc perror("ktrace: exec failed"); 12736426Smarc } 12836426Smarc exit(0); 12936426Smarc } 130