1 /*- 2 * Copyright (c) 1988 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)ktrace.c 1.5 (Berkeley) 06/29/90"; 16 #endif /* not lint */ 17 18 #include "ktrace.h" 19 20 #define USAGE \ 21 "usage: ktrace [-acid] [-f trfile] [-t trpoints] [-p pid] [-g pgid]\n\ 22 trops: c = syscalls, n = namei, g = generic-i/o, a = everything\n\ 23 ktrace -C (clear everthing)\n" 24 25 26 char *tracefile = DEF_TRACEFILE; 27 int append, clear, descend, inherit; 28 29 main(argc, argv) 30 char *argv[]; 31 { 32 extern int optind; 33 extern char *optarg; 34 int trpoints = ALL_POINTS; 35 int ops = 0; 36 int pid = 0; 37 int ch; 38 39 while ((ch = getopt(argc,argv,"Cacdp:g:if:t:")) != EOF) 40 switch((char)ch) { 41 case 'C': 42 clear = 2; 43 break; 44 case 'c': 45 clear = 1; 46 break; 47 case 'd': 48 ops |= KTRFLAG_DESCEND; 49 break; 50 case 't': 51 trpoints = getpoints(optarg); 52 if (trpoints < 0) { 53 fprintf(stderr, 54 "ktrace: unknown facility in %s\n", 55 optarg); 56 exit(1); 57 } 58 break; 59 case 'p': 60 pid = atoi(optarg); 61 break; 62 case 'g': 63 pid = -atoi(optarg); 64 break; 65 case 'i': 66 inherit++; 67 break; 68 case 'f': 69 tracefile = optarg; 70 break; 71 case 'a': 72 append++; 73 break; 74 default: 75 fprintf(stderr,"usage: \n",*argv); 76 exit(-1); 77 } 78 argv += optind, argc -= optind; 79 80 if (inherit) 81 trpoints |= KTRFAC_INHERIT; 82 if (clear) { /* untrace something */ 83 if (clear == 2) { /* -C */ 84 ops = KTROP_CLEAR | KTRFLAG_DESCEND; 85 pid = 1; 86 } else { 87 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; 88 } 89 if (ktrace(tracefile, ops, trpoints, pid) < 0) { 90 perror("ktrace"); 91 exit(1); 92 } 93 exit(0); 94 } 95 96 if (pid == 0 && !*argv) { /* nothing to trace */ 97 fprintf(stderr, USAGE); 98 exit(1); 99 } 100 101 close(open(tracefile, O_WRONLY | O_CREAT, 0666)); 102 if (!append) 103 close(open(tracefile, O_WRONLY | O_TRUNC)); 104 if (!*argv) { 105 if (ktrace(tracefile, ops, trpoints, pid) < 0) { 106 perror("ktrace"); 107 exit(1); 108 } 109 } else { 110 pid = getpid(); 111 if (ktrace(tracefile, ops, trpoints, pid) < 0) { 112 perror("ktrace"); 113 exit(1); 114 } 115 execvp(argv[0], &argv[0]); 116 perror("ktrace: exec failed"); 117 } 118 exit(0); 119 } 120