xref: /titanic_52/usr/src/cmd/streams/log/strace.c (revision ae35285a4e0fe9ad6a7ffd73227903966dab4c59)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*ae35285aSmeem  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*ae35285aSmeem  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <time.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <stropts.h>
407c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	CTLSIZE sizeof (struct log_ctl)
437c478bd9Sstevel@tonic-gate #define	DATSIZE 8192
447c478bd9Sstevel@tonic-gate #define	LOGDEV "/dev/log"
457c478bd9Sstevel@tonic-gate #define	MAXTID 50
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static int errflg = 0;	/* set if error in argument parsing */
487c478bd9Sstevel@tonic-gate static int infile = 0;  /* set if using standard input for arguments */
497c478bd9Sstevel@tonic-gate static int log;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp);
527c478bd9Sstevel@tonic-gate static int getid(int ac, char **av, struct trace_ids *tp);
537c478bd9Sstevel@tonic-gate static int convarg(char *ap);
547c478bd9Sstevel@tonic-gate static char *getarg(void);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	numeric(c) ((c <= '9') && (c >= '0'))
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static int
597c478bd9Sstevel@tonic-gate convarg(char *ap)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	short ids[2];
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (!ap)
647c478bd9Sstevel@tonic-gate 		return (-2);
657c478bd9Sstevel@tonic-gate 	if (numeric(*ap))
667c478bd9Sstevel@tonic-gate 		return (atoi(ap));
677c478bd9Sstevel@tonic-gate 	if (strcmp(ap, "all") == 0)
687c478bd9Sstevel@tonic-gate 		return (-1);
697c478bd9Sstevel@tonic-gate 	errflg = 1;
707c478bd9Sstevel@tonic-gate 	return (-2);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static char *
747c478bd9Sstevel@tonic-gate getarg(void)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	static char argbuf[40];
77*ae35285aSmeem 	static int eofflg = 0;
787c478bd9Sstevel@tonic-gate 	char *ap;
797c478bd9Sstevel@tonic-gate 	int c;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (eofflg) {
827c478bd9Sstevel@tonic-gate 		infile = 0;
837c478bd9Sstevel@tonic-gate 		return (NULL);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	ap = argbuf;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * Scan to first significant character in standard input.
907c478bd9Sstevel@tonic-gate 	 * If EOF is encountered turn off standard input scanning and
917c478bd9Sstevel@tonic-gate 	 * return NULL
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	while ((c = getchar()) == ' ' || c == '\n' || c == '\t')
947c478bd9Sstevel@tonic-gate 		;
957c478bd9Sstevel@tonic-gate 	if (c == EOF) {
967c478bd9Sstevel@tonic-gate 		infile = 0;
977c478bd9Sstevel@tonic-gate 		eofflg++;
987c478bd9Sstevel@tonic-gate 		return (NULL);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * collect token until whitespace is encountered.  Don't do anything
1027c478bd9Sstevel@tonic-gate 	 * with EOF here as it will be caught the next time around.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	while (1) {
1057c478bd9Sstevel@tonic-gate 		*ap++ = c;
1067c478bd9Sstevel@tonic-gate 		if ((c = getchar()) == ' ' || c == '\n' ||
1077c478bd9Sstevel@tonic-gate 		    c == '\t' || c == EOF) {
1087c478bd9Sstevel@tonic-gate 			if (c == EOF) eofflg++;
1097c478bd9Sstevel@tonic-gate 			*ap = '\0';
1107c478bd9Sstevel@tonic-gate 			return (argbuf);
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static int
1177c478bd9Sstevel@tonic-gate getid(int ac, char **av, struct trace_ids *tp)
1187c478bd9Sstevel@tonic-gate {
119*ae35285aSmeem 	static int index = 1;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * if inside of standard input scan take arguments from there.
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate retry:
1257c478bd9Sstevel@tonic-gate 	if (infile) {
1267c478bd9Sstevel@tonic-gate 		tp->ti_mid = convarg(getarg());
1277c478bd9Sstevel@tonic-gate 		tp->ti_sid = convarg(getarg());
1287c478bd9Sstevel@tonic-gate 		tp->ti_level = convarg(getarg());
1297c478bd9Sstevel@tonic-gate 		if (errflg)
1307c478bd9Sstevel@tonic-gate 			return (0);
1317c478bd9Sstevel@tonic-gate 		/*
1327c478bd9Sstevel@tonic-gate 		 * if the previous operations encountered EOF, infile
1337c478bd9Sstevel@tonic-gate 		 * will be set to zero.  The trace_ids structure must
1347c478bd9Sstevel@tonic-gate 		 * then be loaded from the command line arguments.
1357c478bd9Sstevel@tonic-gate 		 * Otherwise, the structure is now valid and should
1367c478bd9Sstevel@tonic-gate 		 * be returned.
1377c478bd9Sstevel@tonic-gate 		 */
1387c478bd9Sstevel@tonic-gate 		if (infile)
1397c478bd9Sstevel@tonic-gate 			return (1);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * if we get here we are either taking arguments from the
1437c478bd9Sstevel@tonic-gate 	 * command line argument list or we hit end of file on standard
1447c478bd9Sstevel@tonic-gate 	 * input and should return to finish off the command line arguments
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	if (index >= ac)
1477c478bd9Sstevel@tonic-gate 		return (0);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * if a '-' is present, start parsing from standard input
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	if (strcmp(av[index], "-") == 0) {
1537c478bd9Sstevel@tonic-gate 		infile = 1;
1547c478bd9Sstevel@tonic-gate 		index++;
1557c478bd9Sstevel@tonic-gate 		goto retry;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * Parsing from command line, make sure there are
1607c478bd9Sstevel@tonic-gate 	 * at least 3 arguments remaining.
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	if ((index+2) >= ac)
1637c478bd9Sstevel@tonic-gate 		return (0);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	tp->ti_mid = convarg(av[index++]);
1667c478bd9Sstevel@tonic-gate 	tp->ti_sid = convarg(av[index++]);
1677c478bd9Sstevel@tonic-gate 	tp->ti_level = convarg(av[index++]);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (errflg)
1707c478bd9Sstevel@tonic-gate 		return (0);
1717c478bd9Sstevel@tonic-gate 	return (1);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate int
1757c478bd9Sstevel@tonic-gate main(int ac, char *av[])
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	int  n;
1787c478bd9Sstevel@tonic-gate 	char cbuf[CTLSIZE];
1797c478bd9Sstevel@tonic-gate 	char dbuf[DATSIZE];
1807c478bd9Sstevel@tonic-gate 	struct strioctl istr;
1817c478bd9Sstevel@tonic-gate 	struct strbuf ctl, dat;
1827c478bd9Sstevel@tonic-gate 	struct log_ctl *lp = (struct log_ctl *)cbuf;
1837c478bd9Sstevel@tonic-gate 	struct trace_ids tid[MAXTID];
1847c478bd9Sstevel@tonic-gate 	struct trace_ids *tp;
1857c478bd9Sstevel@tonic-gate 	int ntid;
1867c478bd9Sstevel@tonic-gate 	int val;
1877c478bd9Sstevel@tonic-gate 	int flag;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	ctl.buf = cbuf;
1907c478bd9Sstevel@tonic-gate 	ctl.maxlen = CTLSIZE;
1917c478bd9Sstevel@tonic-gate 	dat.buf = dbuf;
1927c478bd9Sstevel@tonic-gate 	dat.len = dat.maxlen = DATSIZE;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	log = open(LOGDEV, O_RDWR);
1957c478bd9Sstevel@tonic-gate 	if (log < 0) {
1967c478bd9Sstevel@tonic-gate 		fprintf(stderr, "ERROR: unable to open %s\n", LOGDEV);
1977c478bd9Sstevel@tonic-gate 		return (1);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	tp = tid;
2017c478bd9Sstevel@tonic-gate 	ntid = 0;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (ac == 1) {
2047c478bd9Sstevel@tonic-gate 		ntid++;
2057c478bd9Sstevel@tonic-gate 		tid[0].ti_mid = -1;
2067c478bd9Sstevel@tonic-gate 		tid[0].ti_sid = -1;
2077c478bd9Sstevel@tonic-gate 		tid[0].ti_level = -1;
2087c478bd9Sstevel@tonic-gate 	} else {
2097c478bd9Sstevel@tonic-gate 		while (getid(ac, av, tp)) {
2107c478bd9Sstevel@tonic-gate 			ntid++;
2117c478bd9Sstevel@tonic-gate 			tp++;
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (errflg)
2167c478bd9Sstevel@tonic-gate 		return (errflg);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	istr.ic_cmd = I_TRCLOG;
2197c478bd9Sstevel@tonic-gate 	istr.ic_dp = (char *)tid;
2207c478bd9Sstevel@tonic-gate 	istr.ic_len = ntid * sizeof (struct trace_ids);
2217c478bd9Sstevel@tonic-gate 	istr.ic_timout = 0;
2227c478bd9Sstevel@tonic-gate 	if (ioctl(log, I_STR, &istr) < 0) {
2237c478bd9Sstevel@tonic-gate 		fprintf(stderr, "ERROR: tracer already exists\n");
2247c478bd9Sstevel@tonic-gate 		return (1);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	setbuf(stdout, (char *)NULL);
2287c478bd9Sstevel@tonic-gate 	flag = 0;
2297c478bd9Sstevel@tonic-gate 	while (getmsg(log, &ctl, &dat, &flag) >= 0) {
2307c478bd9Sstevel@tonic-gate 		flag = 0;
2317c478bd9Sstevel@tonic-gate 		lp = (struct log_ctl *)cbuf;
2327c478bd9Sstevel@tonic-gate 		prlog(stdout, lp, dbuf);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	return (0);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate static void
2397c478bd9Sstevel@tonic-gate prlog(FILE *log, struct log_ctl *lp, char *dp)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	char *ts;
2427c478bd9Sstevel@tonic-gate 	int *args;
2437c478bd9Sstevel@tonic-gate 	char *ap;
2447c478bd9Sstevel@tonic-gate 	time_t t = (time_t)lp->ttime;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	ts = ctime(&t);
2477c478bd9Sstevel@tonic-gate 	ts[19] = '\0';
2487c478bd9Sstevel@tonic-gate 	fprintf(log, "%06d %s %08x %2d %s%s%s %d %d %s\n",
2497c478bd9Sstevel@tonic-gate 	    lp->seq_no, (ts+11), lp->ltime, lp->level,
2507c478bd9Sstevel@tonic-gate 	    ((lp->flags & SL_FATAL) ? "F" : "."),
2517c478bd9Sstevel@tonic-gate 	    ((lp->flags & SL_NOTIFY) ? "N" : "."),
2527c478bd9Sstevel@tonic-gate 	    ((lp->flags & SL_ERROR) ? "E" : "."),
2537c478bd9Sstevel@tonic-gate 	    lp->mid, lp->sid, dp);
2547c478bd9Sstevel@tonic-gate }
255