xref: /csrg-svn/usr.bin/ktrace/ktrace.c (revision 36426)
1*36426Smarc /*
2*36426Smarc  * Copyright (c) 1988 The Regents of the University of California.
3*36426Smarc  * All rights reserved.
4*36426Smarc  *
5*36426Smarc  * Redistribution and use in source and binary forms are permitted
6*36426Smarc  * provided that the above copyright notice and this paragraph are
7*36426Smarc  * duplicated in all such forms and that any documentation,
8*36426Smarc  * advertising materials, and other materials related to such
9*36426Smarc  * distribution and use acknowledge that the software was developed
10*36426Smarc  * by the University of California, Berkeley.  The name of the
11*36426Smarc  * University may not be used to endorse or promote products derived
12*36426Smarc  * from this software without specific prior written permission.
13*36426Smarc  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*36426Smarc  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*36426Smarc  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*36426Smarc  */
17*36426Smarc 
18*36426Smarc #ifndef lint
19*36426Smarc char copyright[] =
20*36426Smarc "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
21*36426Smarc  All rights reserved.\n";
22*36426Smarc #endif /* not lint */
23*36426Smarc 
24*36426Smarc #ifndef lint
25*36426Smarc static char sccsid[] = "@(#)ktrace.c	1.1 (Berkeley) 12/14/88";
26*36426Smarc #endif /* not lint */
27*36426Smarc 
28*36426Smarc /*#include <time.h>*/
29*36426Smarc #include <sys/param.h>
30*36426Smarc #include <sys/file.h>
31*36426Smarc #include <sys/dir.h>
32*36426Smarc #include <sys/user.h>
33*36426Smarc #include <sys/ktrace.h>
34*36426Smarc #include <stdio.h>
35*36426Smarc 
36*36426Smarc main(argc, argv)
37*36426Smarc 	char *argv[];
38*36426Smarc {
39*36426Smarc 	extern int optind;
40*36426Smarc 	extern char *optarg;
41*36426Smarc 	int ops = 0;
42*36426Smarc 	int facs = KTRFAC_SYSCALL | KTRFAC_SYSRET | KTRFAC_NAMEI;
43*36426Smarc 	int pid;
44*36426Smarc 	int ch;
45*36426Smarc 
46*36426Smarc 	while ((ch = getopt(argc,argv,"cp:g:i")) != EOF)
47*36426Smarc 		switch((char)ch) {
48*36426Smarc 			case 'c':
49*36426Smarc 				ops |= KTROP_CLEARFILE;
50*36426Smarc 				break;
51*36426Smarc 			case 'p':
52*36426Smarc 				pid = atoi(optarg);
53*36426Smarc 				break;
54*36426Smarc 			case 'g':
55*36426Smarc 				pid = -atoi(optarg);
56*36426Smarc 				break;
57*36426Smarc 			case 'i':
58*36426Smarc 				ops |= KTROP_INHERITFLAG;
59*36426Smarc 				break;
60*36426Smarc 			default:
61*36426Smarc 				fprintf(stderr,"usage: \n",*argv);
62*36426Smarc 				exit(-1);
63*36426Smarc 		}
64*36426Smarc 	argv += optind, argc -= optind;
65*36426Smarc 
66*36426Smarc 	if ((ops&0x3) == KTROP_CLEARFILE) {
67*36426Smarc 		ktrace("trace.out", ops, facs, -1);
68*36426Smarc 		exit(0);
69*36426Smarc 	}
70*36426Smarc 	open("trace.out", O_WRONLY|O_CREAT, 0777);
71*36426Smarc 	if (!*argv) {
72*36426Smarc 		if (ktrace("trace.out", ops, facs, pid) < 0) {
73*36426Smarc 			perror("ktrace");
74*36426Smarc 			exit(1);
75*36426Smarc 		}
76*36426Smarc 	} else {
77*36426Smarc 		pid = getpid();
78*36426Smarc 		if (ktrace("trace.out", ops, facs, pid) < 0) {
79*36426Smarc 			perror("ktrace");
80*36426Smarc 			exit(1);
81*36426Smarc 		}
82*36426Smarc 		execvp(argv[0], &argv[0]);
83*36426Smarc 		perror("ktrace: exec failed");
84*36426Smarc 	}
85*36426Smarc 
86*36426Smarc 	exit(0);
87*36426Smarc }
88