xref: /titanic_41/usr/src/lib/libdtrace_jni/common/dtj_util.h (revision 28cdc3d776761766afeb198769d1b70ed7e0f2e1)
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 #ifndef	_DTJ_UTIL_H
28 #define	_DTJ_UTIL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <jni.h>
33 #include <libuutil.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 /*
40  * dtj_util.h separates functionality that is generally useful from
41  * that which is specific to the Java DTrace API.  If moved to a separate
42  * library, this functionality could be shared by other JNI wrappers.
43  */
44 
45 #ifdef JNI_VERSION_1_4
46 #define	JNI_VERSION JNI_VERSION_1_4
47 #else
48 #define	JNI_VERSION JNI_VERSION_1_2
49 #endif
50 
51 #define	CONSTRUCTOR "<init>"
52 #define	DTJ_MSG_SIZE 1024
53 #define	DTJ_INVALID_PTR ((void *)-1)
54 #define	DTJ_INVALID_STR ((const char *)-1)
55 
56 #define	WRAP_EXCEPTION(JENV)	dtj_wrap_exception((JENV), __FILE__, __LINE__)
57 
58 extern boolean_t g_dtj_util_debug;
59 
60 typedef enum dtj_status {
61 	DTJ_OK = JNI_OK,
62 	DTJ_ERR = JNI_ERR
63 } dtj_status_t;
64 
65 typedef enum dtj_type {
66 	JCLASS,
67 	JMETHOD,
68 	JMETHOD_STATIC,
69 	JFIELD,
70 	JFIELD_STATIC,
71 	DTJ_TYPE_END = -1
72 } dtj_type_t;
73 
74 /*
75  * Convenient description format for java classes, methods, and fields.  The
76  * java_class_t, java_method_t, and java_field_t structures derived from these
77  * descriptions are used to create a table of usable JNI jclass, jmethodID, and
78  * jfieldID instances.
79  */
80 typedef struct dtj_table_entry {
81 	dtj_type_t djte_type;	/* JNI type */
82 	void *djte_addr;	/* jclass, jmethodID, or jfieldID address */
83 	char *djte_name;	/* symbol name declared in Java */
84 	char *djte_desc;	/* JNI descriptor (string format) */
85 } dtj_table_entry_t;
86 
87 typedef struct dtj_java_class {
88 	jclass *djc_ptr;	/* address in user-defined structure */
89 	char *djc_name;		/* fully qualified '/' delimited class name */
90 	uu_list_t *djc_methods;	/* element type (java_method_t *) */
91 	uu_list_t *djc_fields;	/* element type (java_field_t *) */
92 	uu_list_node_t djc_node;
93 } dtj_java_class_t;
94 
95 typedef struct dtj_java_method {
96 	jmethodID *djm_ptr;	/* address in user-defined structure */
97 	char *djm_name;		/* method name in java source file */
98 	char *djm_signature;	/* javap -s method signature string */
99 	boolean_t djm_static;	/* flag indicating static qualifier */
100 	uu_list_node_t djm_node;
101 } dtj_java_method_t;
102 
103 typedef struct dtj_java_field {
104 	jfieldID *djf_ptr;	/* address in user-defined structure */
105 	char *djf_name;		/* field name in java source file */
106 	char *djf_type;		/* javap -s field type string */
107 	boolean_t djf_static;	/* flag indicating static qualifier */
108 	uu_list_node_t djf_node;
109 } dtj_java_field_t;
110 
111 /*
112  * Table of cached jclass, jmethodID, and jfieldID values usable across multiple
113  * native method calls and multiple threads.
114  *
115  * Suffix conventions:
116  *   jc  java class
117  *   jm  java method
118  *   jsm java static method
119  *   jf  java field
120  *   jsf java static field
121  */
122 
123 /* NativeException */
124 extern jclass g_nx_jc;
125 extern jmethodID g_nxinit_jm;
126 
127 /* java.io.Serializable */
128 extern jclass g_serial_jc;
129 
130 /* java.lang.Number */
131 extern jclass g_number_jc;
132 extern jmethodID g_shortval_jm;
133 extern jmethodID g_intval_jm;
134 extern jmethodID g_longval_jm;
135 
136 /* java.lang.Byte */
137 extern jclass g_byte_jc;
138 extern jmethodID g_byteinit_jm;
139 
140 /* java.lang.Character */
141 extern jclass g_char_jc;
142 extern jmethodID g_charinit_jm;
143 extern jmethodID g_charval_jm;
144 
145 /* java.lang.Short */
146 extern jclass g_short_jc;
147 extern jmethodID g_shortinit_jm;
148 
149 /* java.lang.Integer */
150 extern jclass g_int_jc;
151 extern jmethodID g_intinit_jm;
152 
153 /* java.lang.Long */
154 extern jclass g_long_jc;
155 extern jmethodID g_longinit_jm;
156 
157 /* java.lang.String */
158 extern jclass g_string_jc;
159 extern jmethodID g_strinit_bytes_jm;
160 extern jmethodID g_strbytes_jm;
161 extern jmethodID g_trim_jm;
162 
163 /* java.lang.StringBuffer */
164 extern jclass g_buf_jc;
165 extern jmethodID g_bufinit_jm;
166 extern jmethodID g_buf_append_char_jm;
167 extern jmethodID g_buf_append_int_jm;
168 extern jmethodID g_buf_append_long_jm;
169 extern jmethodID g_buf_append_str_jm;
170 extern jmethodID g_buf_append_obj_jm;
171 extern jmethodID g_buflen_jm;
172 extern jmethodID g_bufsetlen_jm;
173 
174 /* java.lang.Object */
175 extern jclass g_object_jc;
176 extern jmethodID g_tostring_jm;
177 extern jmethodID g_equals_jm;
178 
179 /* java.lang.Enum */
180 extern jclass g_enum_jc;
181 extern jmethodID g_enumname_jm;
182 
183 /* List */
184 extern jclass g_list_jc;
185 extern jmethodID g_listclear_jm;
186 extern jmethodID g_listadd_jm;
187 extern jmethodID g_listget_jm;
188 extern jmethodID g_listsize_jm;
189 
190 /*
191  * Populates the common java class references and associated method and field
192  * IDs declared in this file (above) using the dtj_cache_jni_classes() method.
193  */
194 extern dtj_status_t dtj_load_common(JNIEnv *);
195 
196 /*
197  * Populates the user-declared java class references and associated method and
198  * field IDs described in the given table.  Because the class references are
199  * created as global JNI references, the method and field IDs remain valid
200  * across multiple native method calls and across multiple threads.
201  *
202  * This function assumes that the given table of java class, method, and field
203  * descriptions is terminated by an entry with DTJ_TYPE_END, and that the
204  * method and field descriptions immediately follow the description of their
205  * containing class.
206  *
207  * Throws NoClassDefFoundError, NoSuchMethodError, or NoSuchFieldError if any
208  * dtj_table_entry_t in common_jni_table.c is incorrect.
209  */
210 extern dtj_status_t dtj_cache_jni_classes(JNIEnv *, const dtj_table_entry_t *);
211 
212 /* Common utilities */
213 
214 /*
215  * The following functions each create a pending Java Error or Exception:
216  *
217  * OutOfMemoryError
218  * NullPointerException
219  * IllegalArgumentException
220  * IllegalStateException
221  * NoSuchElementException
222  * ClassCastException
223  * AssertionError
224  * org.opensolaris.os.support.ResourceLimitException
225  *
226  * Control should be returned to Java immediately afterwards.
227  */
228 extern void dtj_throw_out_of_memory(JNIEnv *, const char *, ...);
229 extern void dtj_throw_null_pointer(JNIEnv *, const char *, ...);
230 extern void dtj_throw_illegal_argument(JNIEnv *, const char *, ...);
231 extern void dtj_throw_illegal_state(JNIEnv *, const char *, ...);
232 extern void dtj_throw_no_such_element(JNIEnv *, const char *, ...);
233 extern void dtj_throw_class_cast(JNIEnv *, const char *, ...);
234 extern void dtj_throw_assertion(JNIEnv *, const char *, ...);
235 extern void dtj_throw_resource_limit(JNIEnv *, const char *, ...);
236 
237 /*
238  * Attaches native filename and line number to the currently pending java
239  * exception, since that information is not present in the exception stack
240  * trace.
241  */
242 extern void dtj_wrap_exception(JNIEnv *, const char *, int);
243 
244 /*
245  * Calls the toString() method of the given object and prints the value to
246  * stdout (useful for debugging).  If an exception is thrown in this function,
247  * it is described on stdout and cleared.  It's guaranteed that no exception is
248  * pending when this function returns.
249  */
250 extern void dtj_print_object(JNIEnv *jenv, jobject obj);
251 
252 /*
253  * Gets a formatted String (local reference) from a format and a variable
254  * argument list of placeholder values.  Returns NULL if OutOfMemoryError is
255  * thrown.
256  */
257 extern jstring dtj_format_string(JNIEnv *jenv, const char *fmt, ...);
258 
259 /*
260  * Internationalization support.  These functions taken (not verbatim) from
261  * Section 8.2 of The Java Native Interface by Sheng Liang, The Java Series.
262  * Use these functions for locale-specific strings such as file names.
263  */
264 extern jstring dtj_NewStringNative(JNIEnv *jenv, const char *str);
265 extern char *dtj_GetStringNativeChars(JNIEnv *jenv, jstring jstr);
266 extern void dtj_ReleaseStringNativeChars(JNIEnv *jenv, jstring jstr,
267     const char *str);
268 
269 /*
270  * Converts the args array of main(String[] args) in Java into a native
271  * dynamically allocated array of strings.  The returned array must be
272  * deallocated by calling free_argv().  A java exception is pending if this
273  * function returns NULL (in that case, any allocations made up to the point of
274  * failure in get_argv() are automatically freed).
275  *
276  * Returns a NULL-terminated array that works with functions that expect a
277  * terminating NULL rather than relying on an element count.  The argc parameter
278  * is also overwritten with the number of returned array elements (not including
279  * the terminating NULL).
280  */
281 extern char **dtj_get_argv(JNIEnv *jenv, jobjectArray args, int *argc);
282 /*
283  * Tokenizes a command string to create a native dynamically allocated array of
284  * strings.  The first element of the returned array is assumed to be the name
285  * of the command, and subsequent elements are arguments to that command.
286  * Otherwise behaves exactly like get_argv() above, including requiring a
287  * subsequent call to free_argv() on the returned array.
288  * Throws NullPointerException if cmd is NULL.
289  * Throws IllegalArgumentException if cmd is empty.
290  */
291 extern char **dtj_make_argv(JNIEnv *jenv, jstring cmd, int *argc);
292 extern void dtj_free_argv(char **argv);
293 
294 
295 /* Wrappers for uu_list_t */
296 
297 /*
298  * List element destructor.
299  * params: node pointer, user arg (may be NULL)
300  */
301 typedef void dtj_value_destroy_f(void *, void *);
302 
303 /*
304  * uu_list_t generic entry type for pointers compared by pointer value, similar
305  * to Java's default Object.equals() implementation (referenced objects are
306  * equal only if they have the same address in memory).  Used with
307  * pointer_list_entry_cmp.
308  */
309 typedef struct dtj_pointer_list_entry {
310 	void *dple_ptr;
311 	uu_list_node_t dple_node;
312 } dtj_pointer_list_entry_t;
313 
314 typedef struct dtj_string_list_entry {
315 	char *dsle_value;
316 	uu_list_node_t dsle_node;
317 } dtj_string_list_entry_t;
318 
319 /* Comparison functions, uu_compare_fn_t signature */
320 extern int dtj_pointer_list_entry_cmp(const void *, const void *, void *);
321 extern int dtj_string_list_entry_cmp(const void *, const void *, void *);
322 
323 /* Constructors */
324 extern uu_list_t *dtj_pointer_list_create(void);
325 extern dtj_pointer_list_entry_t *dtj_pointer_list_entry_create(void *);
326 extern uu_list_t *dtj_string_list_create(void);
327 extern dtj_string_list_entry_t *dtj_string_list_entry_create(const char *);
328 
329 /* Destructors */
330 extern void dtj_pointer_list_entry_destroy(void *, dtj_value_destroy_f *,
331     void *);
332 extern void dtj_string_list_entry_destroy(void *, void *);
333 /*
334  * Convenience function destroys a uu_list_t and its values.
335  *
336  * param list: list to be destroyed, call is a no-op if list is NULL
337  * param value_destroy: optional destructor; if non-NULL, it is called on each
338  *	list value
339  * param arg: user argument to the optional destructor
340  */
341 extern void dtj_list_destroy(uu_list_t *, dtj_value_destroy_f *, void *);
342 extern void dtj_pointer_list_destroy(uu_list_t *, dtj_value_destroy_f *,
343     void *);
344 extern void dtj_string_list_destroy(uu_list_t *);
345 
346 /*
347  * Convenience functions clear a uu_list_t without destroying it.  Destroys all
348  * list elements and leaves the list empty.  The *_list_destroy() functions
349  * implicitly clear the list before destroying it.
350  */
351 extern void dtj_list_clear(uu_list_t *, dtj_value_destroy_f *, void *);
352 extern void dtj_pointer_list_clear(uu_list_t *, dtj_value_destroy_f *,
353     void *);
354 extern void dtj_string_list_clear(uu_list_t *);
355 
356 extern boolean_t dtj_list_empty(uu_list_t *);
357 /* Return B_TRUE if successful, B_FALSE otherwise */
358 extern boolean_t dtj_list_add(uu_list_t *, void *);
359 extern boolean_t dtj_pointer_list_add(uu_list_t *, void *);
360 extern boolean_t dtj_string_list_add(uu_list_t *, const char *);
361 /* Return INVALID_PTR if list is empty (NULL is a valid list element) */
362 extern void * dtj_pointer_list_first(uu_list_t *);
363 extern void * dtj_pointer_list_last(uu_list_t *);
364 /* Return INVALID_STR if list is empty (NULL is a valid list element) */
365 extern const char *dtj_string_list_first(uu_list_t *);
366 extern const char *dtj_string_list_last(uu_list_t *);
367 /* Return INVALID_PTR at end of list (NULL is a valid list element) */
368 extern void *dtj_pointer_list_walk_next(uu_list_walk_t *);
369 /* Return INVALID_STR at end of list (NULL is a valid list element) */
370 extern const char *dtj_string_list_walk_next(uu_list_walk_t *);
371 
372 #ifdef	__cplusplus
373 }
374 #endif
375 
376 #endif	/* _DTJ_UTIL_H */
377