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#ifndef DEBUG
297c478bdstevel@tonic-gate#define	NDEBUG	1
307c478bdstevel@tonic-gate#endif
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate#include <stdlib.h>
337c478bdstevel@tonic-gate#include <string.h>
347c478bdstevel@tonic-gate#include <tnf/com.h>
357c478bdstevel@tonic-gate#include <tnf/writer.h>
367c478bdstevel@tonic-gate#include <tnf/probe.h>
377c478bdstevel@tonic-gate#include <assert.h>
387c478bdstevel@tonic-gate
397c478bdstevel@tonic-gate#include "tnf_types.h"
407c478bdstevel@tonic-gate#include "tnf_trace.h"
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#ifdef TNFWB_DEBUG
437c478bdstevel@tonic-gate#ifdef _KERNEL
447c478bdstevel@tonic-gate#error TNFWB_DEBUG
457c478bdstevel@tonic-gate#else  /* _KERNEL */
467c478bdstevel@tonic-gate#include <stdio.h>
477c478bdstevel@tonic-gate#include <thread.h>
487c478bdstevel@tonic-gate#endif /* _KERNEL */
497c478bdstevel@tonic-gate#endif /* TNFW_DEBUG */
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate/*
537c478bdstevel@tonic-gate * Defines
547c478bdstevel@tonic-gate */
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate#ifdef _KERNEL
577c478bdstevel@tonic-gate#define	TNF_ASSERT(expr)	ASSERT(expr)
587c478bdstevel@tonic-gate#else
597c478bdstevel@tonic-gate#define	TNF_ASSERT(expr)	assert(expr)
607c478bdstevel@tonic-gate#endif
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gate/*
637c478bdstevel@tonic-gate * New properties need for tracing
647c478bdstevel@tonic-gate */
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gatestatic tnf_tag_data_t	**derived_tagarg_properties[] = {
677c478bdstevel@tonic-gate	&TAG_DATA(tnf_derived),
687c478bdstevel@tonic-gate	&TAG_DATA(tnf_tag_arg),
697c478bdstevel@tonic-gate	0
707c478bdstevel@tonic-gate};
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gatestatic tnf_tag_data_t	***derived_tagarg_properties_ptr =
737c478bdstevel@tonic-gate					derived_tagarg_properties;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate/*
767c478bdstevel@tonic-gate * New derived types for probes
777c478bdstevel@tonic-gate */
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gateTNF_STD_DERIVED_TAG(tnf_probe_event, tnf_tag,
807c478bdstevel@tonic-gate		derived_tagarg_properties_ptr, TNF_OPAQUE);
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gateTNF_STD_DERIVED_TAG(tnf_time_base, tnf_int64,
837c478bdstevel@tonic-gate		tnf_derived_properties, TNF_INT64);
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gateTNF_STD_DERIVED_TAG(tnf_time_delta, tnf_uint32,
867c478bdstevel@tonic-gate		tnf_derived_properties, TNF_UINT32);
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gateTNF_STD_DERIVED_TAG(tnf_pid, tnf_int32,
897c478bdstevel@tonic-gate		tnf_derived_properties, TNF_INT32);
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gateTNF_STD_DERIVED_TAG(tnf_lwpid, tnf_uint32,
927c478bdstevel@tonic-gate		tnf_derived_properties, TNF_UINT32);
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate/*
957c478bdstevel@tonic-gate * Schedule records -CAUTION- keep in sync with tnf_internal.h
967c478bdstevel@tonic-gate * Note that tnf_schedule_prototype_t has different sizes for
977c478bdstevel@tonic-gate * kernel and user.
987c478bdstevel@tonic-gate */
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gatestatic char	*user_schedule_slot_names[] = {
1017c478bdstevel@tonic-gate	TNF_N_TAG,
1027c478bdstevel@tonic-gate	TNF_N_TID,
1037c478bdstevel@tonic-gate	TNF_N_LWPID,
1047c478bdstevel@tonic-gate	TNF_N_PID,
1057c478bdstevel@tonic-gate	TNF_N_TIME_BASE,
1067c478bdstevel@tonic-gate	0};
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gatestatic tnf_tag_data_t	**user_schedule_slots[] = {
1097c478bdstevel@tonic-gate	&TAG_DATA(tnf_tag),		/* tag			*/
1107c478bdstevel@tonic-gate	&TAG_DATA(tnf_uint32),		/* tid XXX		*/
1117c478bdstevel@tonic-gate	&TAG_DATA(tnf_lwpid),		/* lwpid 		*/
1127c478bdstevel@tonic-gate	&TAG_DATA(tnf_pid),		/* pid 			*/
1137c478bdstevel@tonic-gate	&TAG_DATA(tnf_time_base),	/* time_base 		*/
1147c478bdstevel@tonic-gate	0};
1157c478bdstevel@tonic-gate
1167c478bdstevel@tonic-gateTNF_STD_STRUCT_TAG(tnf_sched_rec,
1177c478bdstevel@tonic-gate		user_schedule_slots,
1187c478bdstevel@tonic-gate		user_schedule_slot_names,
1197c478bdstevel@tonic-gate		sizeof (tnf_schedule_prototype_t));
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate/*
1227c478bdstevel@tonic-gate * Probe type record (metatag)
1237c478bdstevel@tonic-gate */
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gatestatic tnf_tag_data_t	**probe_type_slots[] = {
1267c478bdstevel@tonic-gate	&TAG_DATA(tnf_tag),
1277c478bdstevel@tonic-gate	&TAG_DATA(tnf_name),
1287c478bdstevel@tonic-gate	&TAG_DATA(tnf_properties),
1297c478bdstevel@tonic-gate	&TAG_DATA(tnf_slot_types),
1307c478bdstevel@tonic-gate	&TAG_DATA(tnf_type_size),
1317c478bdstevel@tonic-gate	&TAG_DATA(tnf_slot_names),
1327c478bdstevel@tonic-gate	&TAG_DATA(tnf_string),  	/* detail */
1337c478bdstevel@tonic-gate	0};
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gateTNF_STRUCT_TAG(tnf_probe_type,
1367c478bdstevel@tonic-gate		tnf_type_properties,
1377c478bdstevel@tonic-gate		probe_type_slots,
1387c478bdstevel@tonic-gate		0,
1397c478bdstevel@tonic-gate		sizeof (tnf_probe_prototype_t));
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate/*
1427c478bdstevel@tonic-gate * export all tags
1437c478bdstevel@tonic-gate */
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gateTAG_EXPORT(tnf_probe_event);
1467c478bdstevel@tonic-gateTAG_EXPORT(tnf_time_base);
1477c478bdstevel@tonic-gateTAG_EXPORT(tnf_time_delta);
1487c478bdstevel@tonic-gateTAG_EXPORT(tnf_pid);
1497c478bdstevel@tonic-gateTAG_EXPORT(tnf_lwpid);
1507c478bdstevel@tonic-gate
1517c478bdstevel@tonic-gateTAG_EXPORT(tnf_sched_rec);
1527c478bdstevel@tonic-gateTAG_EXPORT(tnf_probe_type);
1537c478bdstevel@tonic-gate
1547c478bdstevel@tonic-gate/*
1557c478bdstevel@tonic-gate * Write a schedule record
1567c478bdstevel@tonic-gate * Can only be written in reusable data space.
1577c478bdstevel@tonic-gate */
1587c478bdstevel@tonic-gate
1597c478bdstevel@tonic-gatetnf_record_p
1607c478bdstevel@tonic-gatetnf_schedule_write(tnf_ops_t *ops, tnf_schedule_t *sched)
1617c478bdstevel@tonic-gate{
1627c478bdstevel@tonic-gate	tnf_tag_data_t *metatag_data;
1637c478bdstevel@tonic-gate	tnf_record_p metatag_index;
1647c478bdstevel@tonic-gate	tnf_schedule_prototype_t *buffer;
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate#ifdef _TNF_VERBOSE
1677c478bdstevel@tonic-gate	fprintf(stderr, "tnf_schedule_write: \n");
1687c478bdstevel@tonic-gate#endif
1697c478bdstevel@tonic-gate	/* Cannot be called when writing into tag space */
1707c478bdstevel@tonic-gate	TNF_ASSERT(ops->mode == TNF_ALLOC_REUSABLE);
1717c478bdstevel@tonic-gate	ALLOC(ops, sizeof (*buffer), buffer, sched->record_p, ops->mode);
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate	metatag_data = TAG_DATA(tnf_sched_rec);
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gate	metatag_index = metatag_data->tag_index ?
1767c478bdstevel@tonic-gate		metatag_data->tag_index :
1777c478bdstevel@tonic-gate		metatag_data->tag_desc(ops, metatag_data);
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gate	ASSIGN(buffer,	tag, 		metatag_index);
1807c478bdstevel@tonic-gate	/* LINTED - tid is 32 bits */
1817c478bdstevel@tonic-gate	ASSIGN2(buffer, tid, 		sched->tid,		uint32);
1827c478bdstevel@tonic-gate	/* LINTED - lwpid is 32 bits */
1837c478bdstevel@tonic-gate	ASSIGN(buffer,	lwpid, 		sched->lwpid);
1847c478bdstevel@tonic-gate	/* LINTED - pid is 32 bits */
1857c478bdstevel@tonic-gate	ASSIGN(buffer,	pid, 		sched->pid);
1867c478bdstevel@tonic-gate	ASSIGN(buffer,	time_base, 	sched->time_base);
1877c478bdstevel@tonic-gate
1887c478bdstevel@tonic-gate	/*
1897c478bdstevel@tonic-gate	 * Remember schedule record generation number so the distance
1907c478bdstevel@tonic-gate	 * in virtual space can be calculated from an event record
1917c478bdstevel@tonic-gate	 */
1927c478bdstevel@tonic-gate	sched->record_gen = ((tnf_block_header_t *)
1937c478bdstevel@tonic-gate	    ((uintptr_t)buffer & TNF_BLOCK_MASK))->generation;
1947c478bdstevel@tonic-gate	/* Cannot have been written into tag space */
1957c478bdstevel@tonic-gate	TNF_ASSERT(sched->record_gen != TNF_TAG_GENERATION_NUM);
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	return ((tnf_record_p) buffer);
1987c478bdstevel@tonic-gate}
199