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*36462Smarc static char sccsid[] = "@(#)ktrace.c 1.3 (Berkeley) 12/21/88"; 2636426Smarc #endif /* not lint */ 2736426Smarc 28*36462Smarc #include "ktrace.h" 2936426Smarc 30*36462Smarc #define USAGE \ 31*36462Smarc "usage: ktrace [-aci] [-f tracefile] [-t facilitystring] [-p pid] [-g pgid]\n\ 32*36462Smarc facilities: c = syscalls, n = namei, g = generic-i/o, a = everything\n" 33*36462Smarc 3436427Smarc 35*36462Smarc char *tracefile = DEF_TRACEFILE; 36*36462Smarc int append, clear; 3736427Smarc 3836426Smarc main(argc, argv) 3936426Smarc char *argv[]; 4036426Smarc { 4136426Smarc extern int optind; 4236426Smarc extern char *optarg; 43*36462Smarc int facs = DEF_FACS; 4436426Smarc int ops = 0; 45*36462Smarc int pid = 0; 4636426Smarc int ch; 4736426Smarc 48*36462Smarc while ((ch = getopt(argc,argv,"acp:g:if:t:")) != EOF) 4936426Smarc switch((char)ch) { 5036426Smarc case 'c': 51*36462Smarc clear = 1; 5236426Smarc break; 53*36462Smarc case 't': 54*36462Smarc facs = getfacs(optarg); 55*36462Smarc if (facs < 0) { 56*36462Smarc fprintf(stderr, 57*36462Smarc "ktrace: unknown facility in %s\n", 58*36462Smarc optarg); 59*36462Smarc exit(1); 60*36462Smarc } 61*36462Smarc break; 6236426Smarc case 'p': 6336426Smarc pid = atoi(optarg); 6436426Smarc break; 6536426Smarc case 'g': 6636426Smarc pid = -atoi(optarg); 6736426Smarc break; 6836426Smarc case 'i': 6936426Smarc ops |= KTROP_INHERITFLAG; 7036426Smarc break; 7136427Smarc case 'f': 7236427Smarc tracefile = optarg; 7336427Smarc break; 7436427Smarc case 'a': 7536427Smarc append++; 7636427Smarc break; 7736426Smarc default: 7836426Smarc fprintf(stderr,"usage: \n",*argv); 7936426Smarc exit(-1); 8036426Smarc } 8136426Smarc argv += optind, argc -= optind; 8236426Smarc 83*36462Smarc if (clear) { /* untrace something */ 84*36462Smarc ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; 85*36462Smarc if (ktrace(tracefile, ops, facs, pid) < 0) { 86*36462Smarc perror("ktrace"); 87*36462Smarc exit(1); 88*36462Smarc } 8936426Smarc exit(0); 9036426Smarc } 91*36462Smarc 92*36462Smarc if (pid == 0 && !*argv) { /* nothing to trace */ 93*36462Smarc fprintf(stderr, USAGE); 94*36462Smarc exit(1); 95*36462Smarc } 96*36462Smarc 97*36462Smarc close(open(tracefile, O_WRONLY | O_CREAT, 0666)); 9836427Smarc if (!append) 9936427Smarc close(open(tracefile, O_WRONLY | O_TRUNC)); 10036426Smarc if (!*argv) { 10136427Smarc if (ktrace(tracefile, ops, facs, pid) < 0) { 10236426Smarc perror("ktrace"); 10336426Smarc exit(1); 10436426Smarc } 10536426Smarc } else { 10636426Smarc pid = getpid(); 10736427Smarc if (ktrace(tracefile, ops, facs, pid) < 0) { 10836426Smarc perror("ktrace"); 10936426Smarc exit(1); 11036426Smarc } 11136426Smarc execvp(argv[0], &argv[0]); 11236426Smarc perror("ktrace: exec failed"); 11336426Smarc } 11436426Smarc exit(0); 11536426Smarc } 116