1 /* 2 * Copyright (c) 1988 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 char copyright[] = 20 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ 21 All rights reserved.\n"; 22 #endif /* not lint */ 23 24 #ifndef lint 25 static char sccsid[] = "@(#)ktrace.c 1.4 (Berkeley) 03/07/90"; 26 #endif /* not lint */ 27 28 #include "ktrace.h" 29 30 #define USAGE \ 31 "usage: ktrace [-acid] [-f trfile] [-t trops] [-p pid] [-g pgid]\n\ 32 trops: c = syscalls, n = namei, g = generic-i/o, a = everything\n\ 33 ktrace -C (clear everthing)\n" 34 35 36 char *tracefile = DEF_TRACEFILE; 37 int append, clear, descend, inherit; 38 39 main(argc, argv) 40 char *argv[]; 41 { 42 extern int optind; 43 extern char *optarg; 44 int facs = ALL_FACS; 45 int ops = 0; 46 int pid = 0; 47 int ch; 48 49 while ((ch = getopt(argc,argv,"Cacdp:g:if:t:")) != EOF) 50 switch((char)ch) { 51 case 'C': 52 clear = 2; 53 break; 54 case 'c': 55 clear = 1; 56 break; 57 case 'd': 58 ops |= KTRFLAG_DESCEND; 59 break; 60 case 't': 61 facs = getfacs(optarg); 62 if (facs < 0) { 63 fprintf(stderr, 64 "ktrace: unknown facility in %s\n", 65 optarg); 66 exit(1); 67 } 68 break; 69 case 'p': 70 pid = atoi(optarg); 71 break; 72 case 'g': 73 pid = -atoi(optarg); 74 break; 75 case 'i': 76 inherit++; 77 break; 78 case 'f': 79 tracefile = optarg; 80 break; 81 case 'a': 82 append++; 83 break; 84 default: 85 fprintf(stderr,"usage: \n",*argv); 86 exit(-1); 87 } 88 argv += optind, argc -= optind; 89 90 if (inherit) 91 facs |= KTRFAC_INHERIT; 92 if (clear) { /* untrace something */ 93 if (clear == 2) { /* -C */ 94 ops = KTROP_CLEAR | KTRFLAG_DESCEND; 95 pid = 1; 96 } else { 97 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; 98 } 99 if (ktrace(tracefile, ops, facs, pid) < 0) { 100 perror("ktrace"); 101 exit(1); 102 } 103 exit(0); 104 } 105 106 if (pid == 0 && !*argv) { /* nothing to trace */ 107 fprintf(stderr, USAGE); 108 exit(1); 109 } 110 111 close(open(tracefile, O_WRONLY | O_CREAT, 0666)); 112 if (!append) 113 close(open(tracefile, O_WRONLY | O_TRUNC)); 114 if (!*argv) { 115 if (ktrace(tracefile, ops, facs, pid) < 0) { 116 perror("ktrace"); 117 exit(1); 118 } 119 } else { 120 pid = getpid(); 121 if (ktrace(tracefile, ops, facs, pid) < 0) { 122 perror("ktrace"); 123 exit(1); 124 } 125 execvp(argv[0], &argv[0]); 126 perror("ktrace: exec failed"); 127 } 128 exit(0); 129 } 130