1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#include <dtrace_jni.h>
30
31/*
32 * This file creates instances of the Java class
33 * org.opensolaris.os.dtrace.InterfaceAttributes.
34 */
35
36static const char *
37dtj_stability_name(dtrace_stability_t stability)
38{
39	const char *name;
40	switch (stability) {
41	case DTRACE_STABILITY_INTERNAL:
42		name = "INTERNAL";
43		break;
44	case DTRACE_STABILITY_PRIVATE:
45		name = "PRIVATE";
46		break;
47	case DTRACE_STABILITY_OBSOLETE:
48		name = "OBSOLETE";
49		break;
50	case DTRACE_STABILITY_EXTERNAL:
51		name = "EXTERNAL";
52		break;
53	case DTRACE_STABILITY_UNSTABLE:
54		name = "UNSTABLE";
55		break;
56	case DTRACE_STABILITY_EVOLVING:
57		name = "EVOLVING";
58		break;
59	case DTRACE_STABILITY_STABLE:
60		name = "STABLE";
61		break;
62	case DTRACE_STABILITY_STANDARD:
63		name = "STANDARD";
64		break;
65	default:
66		name = NULL;
67	}
68
69	return (name);
70}
71
72static const char *
73dtj_dependency_class_name(dtrace_class_t class)
74{
75	const char *name;
76	switch (class) {
77	case DTRACE_CLASS_UNKNOWN:
78		name = "UNKNOWN";
79		break;
80	case DTRACE_CLASS_CPU:
81		name = "CPU";
82		break;
83	case DTRACE_CLASS_PLATFORM:
84		name = "PLATFORM";
85		break;
86	case DTRACE_CLASS_GROUP:
87		name = "GROUP";
88		break;
89	case DTRACE_CLASS_ISA:
90		name = "ISA";
91		break;
92	case DTRACE_CLASS_COMMON:
93		name = "COMMON";
94		break;
95	default:
96		name = NULL;
97	}
98
99	return (name);
100}
101
102jobject
103dtj_new_attribute(dtj_java_consumer_t *jc, const dtrace_attribute_t *attr)
104{
105	JNIEnv *jenv = jc->dtjj_jenv;
106
107	const char *name;
108
109	jstring jname = NULL;
110	jobject jattr = NULL; /* return value */
111
112	jattr = (*jenv)->NewObject(jenv, g_attr_jc, g_attrinit_jm);
113	if ((*jenv)->ExceptionCheck(jenv)) {
114		return (NULL);
115	}
116
117	/* name stability */
118	name = dtj_stability_name(attr->dtat_name);
119	if (!name) {
120		dtj_throw_illegal_argument(jenv,
121		    "unexpected name stability value: %d",
122		    attr->dtat_name);
123		(*jenv)->DeleteLocalRef(jenv, jattr);
124		return (NULL);
125	}
126	jname = (*jenv)->NewStringUTF(jenv, name);
127	if ((*jenv)->ExceptionCheck(jenv)) {
128		(*jenv)->DeleteLocalRef(jenv, jattr);
129		return (NULL);
130	}
131	(*jenv)->CallVoidMethod(jenv, jattr, g_attrset_name_jm, jname);
132	(*jenv)->DeleteLocalRef(jenv, jname);
133	if ((*jenv)->ExceptionCheck(jenv)) {
134		(*jenv)->DeleteLocalRef(jenv, jattr);
135		return (NULL);
136	}
137
138	/* data stability */
139	name = dtj_stability_name(attr->dtat_data);
140	if (!name) {
141		dtj_throw_illegal_argument(jenv,
142		    "unexpected data stability value: %d",
143		    attr->dtat_data);
144		(*jenv)->DeleteLocalRef(jenv, jattr);
145		return (NULL);
146	}
147	jname = (*jenv)->NewStringUTF(jenv, name);
148	if ((*jenv)->ExceptionCheck(jenv)) {
149		(*jenv)->DeleteLocalRef(jenv, jattr);
150		return (NULL);
151	}
152	(*jenv)->CallVoidMethod(jenv, jattr, g_attrset_data_jm, jname);
153	(*jenv)->DeleteLocalRef(jenv, jname);
154	if ((*jenv)->ExceptionCheck(jenv)) {
155		(*jenv)->DeleteLocalRef(jenv, jattr);
156		return (NULL);
157	}
158
159	/* dependency class */
160	name = dtj_dependency_class_name(attr->dtat_class);
161	if (!name) {
162		dtj_throw_illegal_argument(jenv,
163		    "unexpected dependency class value: %d",
164		    attr->dtat_class);
165		(*jenv)->DeleteLocalRef(jenv, jattr);
166		return (NULL);
167	}
168	jname = (*jenv)->NewStringUTF(jenv, name);
169	if ((*jenv)->ExceptionCheck(jenv)) {
170		(*jenv)->DeleteLocalRef(jenv, jattr);
171		return (NULL);
172	}
173	(*jenv)->CallVoidMethod(jenv, jattr, g_attrset_class_jm, jname);
174	(*jenv)->DeleteLocalRef(jenv, jname);
175	if ((*jenv)->ExceptionCheck(jenv)) {
176		(*jenv)->DeleteLocalRef(jenv, jattr);
177		return (NULL);
178	}
179
180	return (jattr);
181}
182