17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate *	Copyright (c) 1994, by Sun Microsytems, Inc.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * Includes
307c478bdstevel@tonic-gate */
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate#ifndef DEBUG
337c478bdstevel@tonic-gate#define	NDEBUG	1
347c478bdstevel@tonic-gate#endif
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate#include <assert.h>
377c478bdstevel@tonic-gate#include <limits.h>
387c478bdstevel@tonic-gate#include <values.h>
397c478bdstevel@tonic-gate#include <stdio.h>
407c478bdstevel@tonic-gate#include <string.h>
417c478bdstevel@tonic-gate#include <unistd.h>
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate#include <tnf/probe.h>
447c478bdstevel@tonic-gate#include "tnf_trace.h"
457c478bdstevel@tonic-gate#include "tnf_args.h"
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate/*
487c478bdstevel@tonic-gate * tnf_probe_debug() - a debug final function
497c478bdstevel@tonic-gate */
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate#define	BUF_LIMIT	1024
527c478bdstevel@tonic-gate#define	NAME_LIMIT	32
537c478bdstevel@tonic-gate#define	ATTR_LIMIT	128
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate/*
567c478bdstevel@tonic-gate * code coverage comment out
577c478bdstevel@tonic-gate * #pragma covcc !instr
587c478bdstevel@tonic-gate */
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gatevoid
617c478bdstevel@tonic-gatetnf_probe_debug(tnf_probe_setup_t *set_p)
627c478bdstevel@tonic-gate{
637c478bdstevel@tonic-gate	char 		tmp_buf[BUF_LIMIT];
647c478bdstevel@tonic-gate	char		*buf_p;
657c478bdstevel@tonic-gate	tnf_probe_control_t *probe_p;
667c478bdstevel@tonic-gate	const char 	*attr_start, *name_start, *name_end;
677c478bdstevel@tonic-gate	ulong_t		attr_len;
687c478bdstevel@tonic-gate	int 		num_args, i, str_len, name_len;
697c478bdstevel@tonic-gate	void 		*arg_position;
707c478bdstevel@tonic-gate	tnf_arg_kind_t	arg_type;
717c478bdstevel@tonic-gate	void 		*buffer;
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate	buf_p = tmp_buf;
747c478bdstevel@tonic-gate	probe_p = set_p->probe_p;
757c478bdstevel@tonic-gate	buffer = set_p->buffer_p;
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate	/* get the name of the probe */
787c478bdstevel@tonic-gate	attr_start = tnf_probe_get_value(probe_p, "name", &attr_len);
797c478bdstevel@tonic-gate	assert(attr_start);
807c478bdstevel@tonic-gate	attr_len = (attr_len > (NAME_LIMIT - 1)) ? (NAME_LIMIT - 1) : attr_len;
817c478bdstevel@tonic-gate	str_len = sprintf(buf_p, "probe %.*s; ", attr_len, attr_start);
827c478bdstevel@tonic-gate	buf_p += str_len;
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate	/* get the sunw%debug attribute */
857c478bdstevel@tonic-gate	attr_start = tnf_probe_get_value(probe_p, "sunw%debug", &attr_len);
867c478bdstevel@tonic-gate	if (attr_start) {
877c478bdstevel@tonic-gate	    attr_len = (attr_len > (ATTR_LIMIT - 1)) ?
887c478bdstevel@tonic-gate				(ATTR_LIMIT - 1) : attr_len;
897c478bdstevel@tonic-gate	    str_len = sprintf(buf_p, "sunw%%debug \"%.*s\"; ",
907c478bdstevel@tonic-gate				attr_len, attr_start);
917c478bdstevel@tonic-gate	    buf_p += str_len;
927c478bdstevel@tonic-gate	}
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate	/* number of args ? we are done if there are only standard args */
957c478bdstevel@tonic-gate	num_args = tnf_probe_get_num_args(probe_p);
967c478bdstevel@tonic-gate	if (num_args <= 2) {
977c478bdstevel@tonic-gate	    (void) sprintf(buf_p, "\n");
987c478bdstevel@tonic-gate	    (void) write(STDERR_FILENO, tmp_buf, strlen(tmp_buf));
997c478bdstevel@tonic-gate	    return;
1007c478bdstevel@tonic-gate	}
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate	/* get the slot names */
1037c478bdstevel@tonic-gate	name_start = tnf_probe_get_value(probe_p, "slots", &attr_len);
1047c478bdstevel@tonic-gate	assert(name_start);
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gate	num_args = tnf_probe_get_num_args(probe_p);
1077c478bdstevel@tonic-gate	if (num_args <= 2)
1087c478bdstevel@tonic-gate	    return;
1097c478bdstevel@tonic-gate	/* print each of the arguments to the probe */
1107c478bdstevel@tonic-gate	for (i = 2; i < num_args; i++) {
1117c478bdstevel@tonic-gate	    /* find slot names - number of spaces is equal to number of args */
1127c478bdstevel@tonic-gate	    name_end = strchr(name_start, VAL_SEPARATOR);
1137c478bdstevel@tonic-gate	    /* LINTED - result is <= string length */
1147c478bdstevel@tonic-gate	    name_len = name_end - name_start;
1157c478bdstevel@tonic-gate	    name_len = (name_len > (NAME_LIMIT - 1)) ?
1167c478bdstevel@tonic-gate				(NAME_LIMIT - 1) : name_len;
1177c478bdstevel@tonic-gate	    str_len = sprintf(buf_p, "%.*s=", name_len, name_start);
1187c478bdstevel@tonic-gate	    buf_p += str_len;
1197c478bdstevel@tonic-gate	    name_start = name_end + 1;
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate	    arg_position = tnf_probe_get_arg_indexed(probe_p, i, buffer);
1227c478bdstevel@tonic-gate	    arg_type = tnf_probe_get_type_indexed(probe_p, i);
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate	    switch (arg_type) {
1257c478bdstevel@tonic-gate	    case TNF_UNKNOWN:
1267c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "<unknown>; ");
1277c478bdstevel@tonic-gate		buf_p += str_len;
1287c478bdstevel@tonic-gate		break;
1297c478bdstevel@tonic-gate	    case TNF_INT32:
1307c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "%ld; ",
1317c478bdstevel@tonic-gate					tnf_probe_get_int(arg_position));
1327c478bdstevel@tonic-gate		buf_p += str_len;
1337c478bdstevel@tonic-gate		break;
1347c478bdstevel@tonic-gate	    case TNF_UINT32:
1357c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "%lu; ",
1367c478bdstevel@tonic-gate					tnf_probe_get_uint(arg_position));
1377c478bdstevel@tonic-gate		buf_p += str_len;
1387c478bdstevel@tonic-gate		break;
1397c478bdstevel@tonic-gate	    case TNF_INT64:
1407c478bdstevel@tonic-gate		/* LINTED malformed format string */
1417c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "%lld; ",
1427c478bdstevel@tonic-gate					tnf_probe_get_longlong(arg_position));
1437c478bdstevel@tonic-gate		buf_p += str_len;
1447c478bdstevel@tonic-gate		break;
1457c478bdstevel@tonic-gate	    case TNF_UINT64:
1467c478bdstevel@tonic-gate		/* LINTED malformed format string */
1477c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "%llu; ",
1487c478bdstevel@tonic-gate					tnf_probe_get_ulonglong(arg_position));
1497c478bdstevel@tonic-gate		buf_p += str_len;
1507c478bdstevel@tonic-gate		break;
1517c478bdstevel@tonic-gate	    case TNF_FLOAT32:
1527c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "%f; ",
1537c478bdstevel@tonic-gate					tnf_probe_get_float(arg_position));
1547c478bdstevel@tonic-gate		buf_p += str_len;
1557c478bdstevel@tonic-gate		break;
1567c478bdstevel@tonic-gate	    case TNF_FLOAT64:
1577c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "%f; ",
1587c478bdstevel@tonic-gate					tnf_probe_get_double(arg_position));
1597c478bdstevel@tonic-gate		buf_p += str_len;
1607c478bdstevel@tonic-gate		break;
1617c478bdstevel@tonic-gate	    case TNF_STRING:
1627c478bdstevel@tonic-gate		attr_start = tnf_probe_get_chars(arg_position);
1637c478bdstevel@tonic-gate		attr_len = strlen(attr_start);
1647c478bdstevel@tonic-gate		attr_len = (attr_len > (ATTR_LIMIT - 1)) ? (ATTR_LIMIT - 1) :
1657c478bdstevel@tonic-gate					attr_len;
1667c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "\"%.*s\"; ", attr_len, attr_start);
1677c478bdstevel@tonic-gate		buf_p += str_len;
1687c478bdstevel@tonic-gate		break;
1697c478bdstevel@tonic-gate	    case TNF_ARRAY:
1707c478bdstevel@tonic-gate		/* no break */
1717c478bdstevel@tonic-gate	    case TNF_STRUCT:
1727c478bdstevel@tonic-gate		/* no break */
1737c478bdstevel@tonic-gate	    case TNF_OPAQUE:
1747c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "0x%lx; ",
1757c478bdstevel@tonic-gate					tnf_probe_get_ulong(arg_position));
1767c478bdstevel@tonic-gate		buf_p += str_len;
1777c478bdstevel@tonic-gate		break;
1787c478bdstevel@tonic-gate	    default:
1797c478bdstevel@tonic-gate		str_len = sprintf(buf_p, "<error>; ");
1807c478bdstevel@tonic-gate		buf_p += str_len;
1817c478bdstevel@tonic-gate		break;
1827c478bdstevel@tonic-gate	    }
1837c478bdstevel@tonic-gate	}
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate	(void) sprintf(buf_p, "\n");
1867c478bdstevel@tonic-gate	(void) write(STDERR_FILENO, tmp_buf, strlen(tmp_buf));
1877c478bdstevel@tonic-gate
1887c478bdstevel@tonic-gate	return;
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gate}   /* end tnf_probe_debug */
1917c478bdstevel@tonic-gate
1927c478bdstevel@tonic-gate
1937c478bdstevel@tonic-gate/*
1947c478bdstevel@tonic-gate * code coverage comment out
1957c478bdstevel@tonic-gate * #pragma covcc instr
1967c478bdstevel@tonic-gate */
1977c478bdstevel@tonic-gate
1987c478bdstevel@tonic-gate#ifdef TESTING
1997c478bdstevel@tonic-gate/*
2007c478bdstevel@tonic-gate * tnf_probe_empty() - an empty final function
2017c478bdstevel@tonic-gate */
2027c478bdstevel@tonic-gate
2037c478bdstevel@tonic-gate/*ARGSUSED0*/
2047c478bdstevel@tonic-gatevoid
2057c478bdstevel@tonic-gatetnf_probe_empty(tnf_probe_setup_t *set_p)
2067c478bdstevel@tonic-gate{
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate	return;
2097c478bdstevel@tonic-gate
2107c478bdstevel@tonic-gate}   /* end tnf_probe_empty */
2117c478bdstevel@tonic-gate#endif
212