xref: /titanic_52/usr/src/cmd/streams/log/strace.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
35*7c478bd9Sstevel@tonic-gate #include <time.h>
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <stropts.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	CTLSIZE sizeof (struct log_ctl)
43*7c478bd9Sstevel@tonic-gate #define	DATSIZE 8192
44*7c478bd9Sstevel@tonic-gate #define	LOGDEV "/dev/log"
45*7c478bd9Sstevel@tonic-gate #define	MAXTID 50
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate static int errflg = 0;	/* set if error in argument parsing */
48*7c478bd9Sstevel@tonic-gate static int infile = 0;  /* set if using standard input for arguments */
49*7c478bd9Sstevel@tonic-gate static int log;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp);
52*7c478bd9Sstevel@tonic-gate static int getid(int ac, char **av, struct trace_ids *tp);
53*7c478bd9Sstevel@tonic-gate static int convarg(char *ap);
54*7c478bd9Sstevel@tonic-gate static char *getarg(void);
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #define	numeric(c) ((c <= '9') && (c >= '0'))
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static int
59*7c478bd9Sstevel@tonic-gate convarg(char *ap)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	short ids[2];
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	if (!ap)
64*7c478bd9Sstevel@tonic-gate 		return (-2);
65*7c478bd9Sstevel@tonic-gate 	if (numeric(*ap))
66*7c478bd9Sstevel@tonic-gate 		return (atoi(ap));
67*7c478bd9Sstevel@tonic-gate 	if (strcmp(ap, "all") == 0)
68*7c478bd9Sstevel@tonic-gate 		return (-1);
69*7c478bd9Sstevel@tonic-gate 	errflg = 1;
70*7c478bd9Sstevel@tonic-gate 	return (-2);
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate static char *
74*7c478bd9Sstevel@tonic-gate getarg(void)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	static char argbuf[40];
77*7c478bd9Sstevel@tonic-gate 	static eofflg = 0;
78*7c478bd9Sstevel@tonic-gate 	char *ap;
79*7c478bd9Sstevel@tonic-gate 	int c;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (eofflg) {
82*7c478bd9Sstevel@tonic-gate 		infile = 0;
83*7c478bd9Sstevel@tonic-gate 		return (NULL);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	ap = argbuf;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	/*
89*7c478bd9Sstevel@tonic-gate 	 * Scan to first significant character in standard input.
90*7c478bd9Sstevel@tonic-gate 	 * If EOF is encountered turn off standard input scanning and
91*7c478bd9Sstevel@tonic-gate 	 * return NULL
92*7c478bd9Sstevel@tonic-gate 	 */
93*7c478bd9Sstevel@tonic-gate 	while ((c = getchar()) == ' ' || c == '\n' || c == '\t')
94*7c478bd9Sstevel@tonic-gate 		;
95*7c478bd9Sstevel@tonic-gate 	if (c == EOF) {
96*7c478bd9Sstevel@tonic-gate 		infile = 0;
97*7c478bd9Sstevel@tonic-gate 		eofflg++;
98*7c478bd9Sstevel@tonic-gate 		return (NULL);
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 	/*
101*7c478bd9Sstevel@tonic-gate 	 * collect token until whitespace is encountered.  Don't do anything
102*7c478bd9Sstevel@tonic-gate 	 * with EOF here as it will be caught the next time around.
103*7c478bd9Sstevel@tonic-gate 	 */
104*7c478bd9Sstevel@tonic-gate 	while (1) {
105*7c478bd9Sstevel@tonic-gate 		*ap++ = c;
106*7c478bd9Sstevel@tonic-gate 		if ((c = getchar()) == ' ' || c == '\n' ||
107*7c478bd9Sstevel@tonic-gate 		    c == '\t' || c == EOF) {
108*7c478bd9Sstevel@tonic-gate 			if (c == EOF) eofflg++;
109*7c478bd9Sstevel@tonic-gate 			*ap = '\0';
110*7c478bd9Sstevel@tonic-gate 			return (argbuf);
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate static int
117*7c478bd9Sstevel@tonic-gate getid(int ac, char **av, struct trace_ids *tp)
118*7c478bd9Sstevel@tonic-gate {
119*7c478bd9Sstevel@tonic-gate 	static index = 1;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	/*
122*7c478bd9Sstevel@tonic-gate 	 * if inside of standard input scan take arguments from there.
123*7c478bd9Sstevel@tonic-gate 	 */
124*7c478bd9Sstevel@tonic-gate retry:
125*7c478bd9Sstevel@tonic-gate 	if (infile) {
126*7c478bd9Sstevel@tonic-gate 		tp->ti_mid = convarg(getarg());
127*7c478bd9Sstevel@tonic-gate 		tp->ti_sid = convarg(getarg());
128*7c478bd9Sstevel@tonic-gate 		tp->ti_level = convarg(getarg());
129*7c478bd9Sstevel@tonic-gate 		if (errflg)
130*7c478bd9Sstevel@tonic-gate 			return (0);
131*7c478bd9Sstevel@tonic-gate 		/*
132*7c478bd9Sstevel@tonic-gate 		 * if the previous operations encountered EOF, infile
133*7c478bd9Sstevel@tonic-gate 		 * will be set to zero.  The trace_ids structure must
134*7c478bd9Sstevel@tonic-gate 		 * then be loaded from the command line arguments.
135*7c478bd9Sstevel@tonic-gate 		 * Otherwise, the structure is now valid and should
136*7c478bd9Sstevel@tonic-gate 		 * be returned.
137*7c478bd9Sstevel@tonic-gate 		 */
138*7c478bd9Sstevel@tonic-gate 		if (infile)
139*7c478bd9Sstevel@tonic-gate 			return (1);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	/*
142*7c478bd9Sstevel@tonic-gate 	 * if we get here we are either taking arguments from the
143*7c478bd9Sstevel@tonic-gate 	 * command line argument list or we hit end of file on standard
144*7c478bd9Sstevel@tonic-gate 	 * input and should return to finish off the command line arguments
145*7c478bd9Sstevel@tonic-gate 	 */
146*7c478bd9Sstevel@tonic-gate 	if (index >= ac)
147*7c478bd9Sstevel@tonic-gate 		return (0);
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	/*
150*7c478bd9Sstevel@tonic-gate 	 * if a '-' is present, start parsing from standard input
151*7c478bd9Sstevel@tonic-gate 	 */
152*7c478bd9Sstevel@tonic-gate 	if (strcmp(av[index], "-") == 0) {
153*7c478bd9Sstevel@tonic-gate 		infile = 1;
154*7c478bd9Sstevel@tonic-gate 		index++;
155*7c478bd9Sstevel@tonic-gate 		goto retry;
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	/*
159*7c478bd9Sstevel@tonic-gate 	 * Parsing from command line, make sure there are
160*7c478bd9Sstevel@tonic-gate 	 * at least 3 arguments remaining.
161*7c478bd9Sstevel@tonic-gate 	 */
162*7c478bd9Sstevel@tonic-gate 	if ((index+2) >= ac)
163*7c478bd9Sstevel@tonic-gate 		return (0);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	tp->ti_mid = convarg(av[index++]);
166*7c478bd9Sstevel@tonic-gate 	tp->ti_sid = convarg(av[index++]);
167*7c478bd9Sstevel@tonic-gate 	tp->ti_level = convarg(av[index++]);
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if (errflg)
170*7c478bd9Sstevel@tonic-gate 		return (0);
171*7c478bd9Sstevel@tonic-gate 	return (1);
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate int
175*7c478bd9Sstevel@tonic-gate main(int ac, char *av[])
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	int  n;
178*7c478bd9Sstevel@tonic-gate 	char cbuf[CTLSIZE];
179*7c478bd9Sstevel@tonic-gate 	char dbuf[DATSIZE];
180*7c478bd9Sstevel@tonic-gate 	struct strioctl istr;
181*7c478bd9Sstevel@tonic-gate 	struct strbuf ctl, dat;
182*7c478bd9Sstevel@tonic-gate 	struct log_ctl *lp = (struct log_ctl *)cbuf;
183*7c478bd9Sstevel@tonic-gate 	struct trace_ids tid[MAXTID];
184*7c478bd9Sstevel@tonic-gate 	struct trace_ids *tp;
185*7c478bd9Sstevel@tonic-gate 	int ntid;
186*7c478bd9Sstevel@tonic-gate 	int val;
187*7c478bd9Sstevel@tonic-gate 	int flag;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	ctl.buf = cbuf;
190*7c478bd9Sstevel@tonic-gate 	ctl.maxlen = CTLSIZE;
191*7c478bd9Sstevel@tonic-gate 	dat.buf = dbuf;
192*7c478bd9Sstevel@tonic-gate 	dat.len = dat.maxlen = DATSIZE;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	log = open(LOGDEV, O_RDWR);
195*7c478bd9Sstevel@tonic-gate 	if (log < 0) {
196*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "ERROR: unable to open %s\n", LOGDEV);
197*7c478bd9Sstevel@tonic-gate 		return (1);
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	tp = tid;
201*7c478bd9Sstevel@tonic-gate 	ntid = 0;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	if (ac == 1) {
204*7c478bd9Sstevel@tonic-gate 		ntid++;
205*7c478bd9Sstevel@tonic-gate 		tid[0].ti_mid = -1;
206*7c478bd9Sstevel@tonic-gate 		tid[0].ti_sid = -1;
207*7c478bd9Sstevel@tonic-gate 		tid[0].ti_level = -1;
208*7c478bd9Sstevel@tonic-gate 	} else {
209*7c478bd9Sstevel@tonic-gate 		while (getid(ac, av, tp)) {
210*7c478bd9Sstevel@tonic-gate 			ntid++;
211*7c478bd9Sstevel@tonic-gate 			tp++;
212*7c478bd9Sstevel@tonic-gate 		}
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	if (errflg)
216*7c478bd9Sstevel@tonic-gate 		return (errflg);
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	istr.ic_cmd = I_TRCLOG;
219*7c478bd9Sstevel@tonic-gate 	istr.ic_dp = (char *)tid;
220*7c478bd9Sstevel@tonic-gate 	istr.ic_len = ntid * sizeof (struct trace_ids);
221*7c478bd9Sstevel@tonic-gate 	istr.ic_timout = 0;
222*7c478bd9Sstevel@tonic-gate 	if (ioctl(log, I_STR, &istr) < 0) {
223*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "ERROR: tracer already exists\n");
224*7c478bd9Sstevel@tonic-gate 		return (1);
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	setbuf(stdout, (char *)NULL);
228*7c478bd9Sstevel@tonic-gate 	flag = 0;
229*7c478bd9Sstevel@tonic-gate 	while (getmsg(log, &ctl, &dat, &flag) >= 0) {
230*7c478bd9Sstevel@tonic-gate 		flag = 0;
231*7c478bd9Sstevel@tonic-gate 		lp = (struct log_ctl *)cbuf;
232*7c478bd9Sstevel@tonic-gate 		prlog(stdout, lp, dbuf);
233*7c478bd9Sstevel@tonic-gate 	}
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	return (0);
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate static void
239*7c478bd9Sstevel@tonic-gate prlog(FILE *log, struct log_ctl *lp, char *dp)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	char *ts;
242*7c478bd9Sstevel@tonic-gate 	int *args;
243*7c478bd9Sstevel@tonic-gate 	char *ap;
244*7c478bd9Sstevel@tonic-gate 	time_t t = (time_t)lp->ttime;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	ts = ctime(&t);
247*7c478bd9Sstevel@tonic-gate 	ts[19] = '\0';
248*7c478bd9Sstevel@tonic-gate 	fprintf(log, "%06d %s %08x %2d %s%s%s %d %d %s\n",
249*7c478bd9Sstevel@tonic-gate 	    lp->seq_no, (ts+11), lp->ltime, lp->level,
250*7c478bd9Sstevel@tonic-gate 	    ((lp->flags & SL_FATAL) ? "F" : "."),
251*7c478bd9Sstevel@tonic-gate 	    ((lp->flags & SL_NOTIFY) ? "N" : "."),
252*7c478bd9Sstevel@tonic-gate 	    ((lp->flags & SL_ERROR) ? "E" : "."),
253*7c478bd9Sstevel@tonic-gate 	    lp->mid, lp->sid, dp);
254*7c478bd9Sstevel@tonic-gate }
255