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 #include <dtrace_jni.h>
28 
29 /*
30  * This file creates instances of the Java class
31  * org.opensolaris.os.dtrace.InterfaceAttributes.
32  */
33 
34 static const char *
dtj_stability_name(dtrace_stability_t stability)35 dtj_stability_name(dtrace_stability_t stability)
36 {
37 	const char *name;
38 	switch (stability) {
39 	case DTRACE_STABILITY_INTERNAL:
40 		name = "INTERNAL";
41 		break;
42 	case DTRACE_STABILITY_PRIVATE:
43 		name = "PRIVATE";
44 		break;
45 	case DTRACE_STABILITY_OBSOLETE:
46 		name = "OBSOLETE";
47 		break;
48 	case DTRACE_STABILITY_EXTERNAL:
49 		name = "EXTERNAL";
50 		break;
51 	case DTRACE_STABILITY_UNSTABLE:
52 		name = "UNSTABLE";
53 		break;
54 	case DTRACE_STABILITY_EVOLVING:
55 		name = "EVOLVING";
56 		break;
57 	case DTRACE_STABILITY_STABLE:
58 		name = "STABLE";
59 		break;
60 	case DTRACE_STABILITY_STANDARD:
61 		name = "STANDARD";
62 		break;
63 	default:
64 		name = NULL;
65 	}
66 
67 	return (name);
68 }
69 
70 static const char *
dtj_dependency_class_name(dtrace_class_t class)71 dtj_dependency_class_name(dtrace_class_t class)
72 {
73 	const char *name;
74 	switch (class) {
75 	case DTRACE_CLASS_UNKNOWN:
76 		name = "UNKNOWN";
77 		break;
78 	case DTRACE_CLASS_CPU:
79 		name = "CPU";
80 		break;
81 	case DTRACE_CLASS_PLATFORM:
82 		name = "PLATFORM";
83 		break;
84 	case DTRACE_CLASS_GROUP:
85 		name = "GROUP";
86 		break;
87 	case DTRACE_CLASS_ISA:
88 		name = "ISA";
89 		break;
90 	case DTRACE_CLASS_COMMON:
91 		name = "COMMON";
92 		break;
93 	default:
94 		name = NULL;
95 	}
96 
97 	return (name);
98 }
99 
100 jobject
dtj_new_attribute(dtj_java_consumer_t * jc,const dtrace_attribute_t * attr)101 dtj_new_attribute(dtj_java_consumer_t *jc, const dtrace_attribute_t *attr)
102 {
103 	JNIEnv *jenv = jc->dtjj_jenv;
104 
105 	const char *name;
106 
107 	jstring jname = NULL;
108 	jobject jattr = NULL; /* return value */
109 
110 	jattr = (*jenv)->NewObject(jenv, g_attr_jc, g_attrinit_jm);
111 	if ((*jenv)->ExceptionCheck(jenv)) {
112 		return (NULL);
113 	}
114 
115 	/* name stability */
116 	name = dtj_stability_name(attr->dtat_name);
117 	if (!name) {
118 		dtj_throw_illegal_argument(jenv,
119 		    "unexpected name stability value: %d",
120 		    attr->dtat_name);
121 		(*jenv)->DeleteLocalRef(jenv, jattr);
122 		return (NULL);
123 	}
124 	jname = (*jenv)->NewStringUTF(jenv, name);
125 	if ((*jenv)->ExceptionCheck(jenv)) {
126 		(*jenv)->DeleteLocalRef(jenv, jattr);
127 		return (NULL);
128 	}
129 	(*jenv)->CallVoidMethod(jenv, jattr, g_attrset_name_jm, jname);
130 	(*jenv)->DeleteLocalRef(jenv, jname);
131 	if ((*jenv)->ExceptionCheck(jenv)) {
132 		(*jenv)->DeleteLocalRef(jenv, jattr);
133 		return (NULL);
134 	}
135 
136 	/* data stability */
137 	name = dtj_stability_name(attr->dtat_data);
138 	if (!name) {
139 		dtj_throw_illegal_argument(jenv,
140 		    "unexpected data stability value: %d",
141 		    attr->dtat_data);
142 		(*jenv)->DeleteLocalRef(jenv, jattr);
143 		return (NULL);
144 	}
145 	jname = (*jenv)->NewStringUTF(jenv, name);
146 	if ((*jenv)->ExceptionCheck(jenv)) {
147 		(*jenv)->DeleteLocalRef(jenv, jattr);
148 		return (NULL);
149 	}
150 	(*jenv)->CallVoidMethod(jenv, jattr, g_attrset_data_jm, jname);
151 	(*jenv)->DeleteLocalRef(jenv, jname);
152 	if ((*jenv)->ExceptionCheck(jenv)) {
153 		(*jenv)->DeleteLocalRef(jenv, jattr);
154 		return (NULL);
155 	}
156 
157 	/* dependency class */
158 	name = dtj_dependency_class_name(attr->dtat_class);
159 	if (!name) {
160 		dtj_throw_illegal_argument(jenv,
161 		    "unexpected dependency class value: %d",
162 		    attr->dtat_class);
163 		(*jenv)->DeleteLocalRef(jenv, jattr);
164 		return (NULL);
165 	}
166 	jname = (*jenv)->NewStringUTF(jenv, name);
167 	if ((*jenv)->ExceptionCheck(jenv)) {
168 		(*jenv)->DeleteLocalRef(jenv, jattr);
169 		return (NULL);
170 	}
171 	(*jenv)->CallVoidMethod(jenv, jattr, g_attrset_class_jm, jname);
172 	(*jenv)->DeleteLocalRef(jenv, jname);
173 	if ((*jenv)->ExceptionCheck(jenv)) {
174 		(*jenv)->DeleteLocalRef(jenv, jattr);
175 		return (NULL);
176 	}
177 
178 	return (jattr);
179 }
180