add affiliation for Oz
[unix-history] / usr / src / usr.bin / ktrace / ktrace.c
CommitLineData
6c82192c 1/*-
37f7ecbb
MT
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
6c82192c 5 * %sccs.include.redist.c%
37f7ecbb
MT
6 */
7
8#ifndef lint
9char 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
7f989705 15static char sccsid[] = "@(#)ktrace.c 5.2 (Berkeley) %G%";
37f7ecbb
MT
16#endif /* not lint */
17
371824ae
KB
18#include <sys/param.h>
19#include <sys/stat.h>
20#include <sys/file.h>
21#include <sys/time.h>
22#include <sys/errno.h>
23#include <sys/uio.h>
24#include <sys/ktrace.h>
25#include <stdio.h>
f4d5acdc 26#include "ktrace.h"
37f7ecbb
MT
27
28main(argc, argv)
371824ae
KB
29 int argc;
30 char **argv;
37f7ecbb
MT
31{
32 extern int optind;
33 extern char *optarg;
371824ae
KB
34 enum { NOTSET, CLEAR, CLEARALL } clear;
35 int append, ch, fd, inherit, ops, pid, pidset, trpoints;
36 char *tracefile;
37f7ecbb 37
371824ae
KB
38 clear = NOTSET;
39 append = ops = pidset = 0;
40 trpoints = ALL_POINTS;
41 tracefile = DEF_TRACEFILE;
42 while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF)
37f7ecbb 43 switch((char)ch) {
371824ae
KB
44 case 'a':
45 append = 1;
46 break;
513e9867 47 case 'C':
371824ae 48 clear = CLEARALL;
7f989705 49 pidset = 1;
513e9867
MT
50 break;
51 case 'c':
371824ae 52 clear = CLEAR;
513e9867
MT
53 break;
54 case 'd':
55 ops |= KTRFLAG_DESCEND;
56 break;
371824ae
KB
57 case 'f':
58 tracefile = optarg;
513e9867
MT
59 break;
60 case 'g':
371824ae
KB
61 pid = -rpid(optarg);
62 pidset = 1;
513e9867
MT
63 break;
64 case 'i':
371824ae 65 inherit = 1;
513e9867 66 break;
371824ae
KB
67 case 'p':
68 pid = rpid(optarg);
69 pidset = 1;
513e9867 70 break;
371824ae
KB
71 case 't':
72 trpoints = getpoints(optarg);
73 if (trpoints < 0) {
74 (void)fprintf(stderr,
75 "ktrace: unknown facility in %s\n", optarg);
76 usage();
77 }
513e9867
MT
78 break;
79 default:
371824ae 80 usage();
37f7ecbb 81 }
371824ae
KB
82 argv += optind;
83 argc -= optind;
37f7ecbb 84
371824ae
KB
85 if (pidset && *argv || !pidset && !*argv)
86 usage();
87
513e9867 88 if (inherit)
6c82192c 89 trpoints |= KTRFAC_INHERIT;
371824ae
KB
90
91 if (clear != NOTSET) {
92 if (clear == CLEARALL) {
513e9867
MT
93 ops = KTROP_CLEAR | KTRFLAG_DESCEND;
94 pid = 1;
371824ae 95 } else
513e9867 96 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
371824ae
KB
97
98 if (ktrace(tracefile, ops, trpoints, pid) < 0)
99 error(tracefile);
37f7ecbb
MT
100 exit(0);
101 }
f4d5acdc 102
371824ae
KB
103 if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC),
104 DEFFILEMODE)) < 0)
105 error(tracefile);
106 (void)close(fd);
107
108 if (*argv) {
109 if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
110 error();
37f7ecbb 111 execvp(argv[0], &argv[0]);
371824ae
KB
112 error(argv[0]);
113 exit(1);
37f7ecbb 114 }
371824ae
KB
115 else if (ktrace(tracefile, ops, trpoints, pid) < 0)
116 error(tracefile);
37f7ecbb
MT
117 exit(0);
118}
371824ae
KB
119
120rpid(p)
121 char *p;
122{
123 static int first;
124
125 if (first++) {
126 (void)fprintf(stderr,
127 "ktrace: only one -g or -p flag is permitted.\n");
128 usage();
129 }
130 if (!*p) {
131 (void)fprintf(stderr, "ktrace: illegal process id.\n");
132 usage();
133 }
134 return(atoi(p));
135}
136
137error(name)
138 char *name;
139{
140 (void)fprintf(stderr, "ktrace: %s: %s.\n", name, strerror(errno));
141 exit(1);
142}
143
144usage()
145{
146 (void)fprintf(stderr,
147"usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n");
148 exit(1);
149}