-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent_util.c
executable file
·305 lines (274 loc) · 9.37 KB
/
agent_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* @(#)agent_util.c 1.15 05/11/17
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
#include <agent_util.h>
/* ------------------------------------------------------------------- */
/* Generic C utility functions */
/* Send message to stdout or whatever the data output location is */
void
stdout_message(const char * format, ...)
{
va_list ap;
va_start(ap, format);
(void)vfprintf(stdout, format, ap);
va_end(ap);
}
/* Send message to stderr or whatever the error output location is and exit */
void
fatal_error(const char * format, ...)
{
va_list ap;
va_start(ap, format);
(void)vfprintf(stderr, format, ap);
(void)fflush(stderr);
va_end(ap);
exit(3);
}
/* Get a token from a string (strtok is not MT-safe)
* str String to scan
* seps Separation characters
* buf Place to put results
* max Size of buf
* Returns NULL if no token available or can't do the scan.
*/
char *
get_token(char *str, char *seps, char *buf, int max)
{
int len;
buf[0] = 0;
if ( str==NULL || str[0]==0 ) {
return NULL;
}
str += strspn(str, seps);
if ( str[0]==0 ) {
return NULL;
}
len = (int)strcspn(str, seps);
if ( len >= max ) {
return NULL;
}
(void)strncpy(buf, str, len);
buf[len] = 0;
return str+len;
}
/* Determines if a class/method is specified by a list item
* item String that represents a pattern to match
* If it starts with a '*', then any class is allowed
* If it ends with a '*', then any method is allowed
* cname Class name, e.g. "java.lang.Object"
* mname Method name, e.g. "<init>"
* Returns 1(true) or 0(false).
*/
static int
covered_by_list_item(char *item, char *cname, char *mname)
{
int len;
len = (int)strlen(item);
if ( item[0]=='*' ) {
if ( strncmp(mname, item+1, len-1)==0 ) {
return 1;
}
} else if ( item[len-1]=='*' ) {
if ( strncmp(cname, item, len-1)==0 ) {
return 1;
}
} else {
int cname_len;
cname_len = (int)strlen(cname);
if ( strncmp(cname, item, (len>cname_len?cname_len:len))==0 ) {
if ( cname_len >= len ) {
/* No method name supplied in item, we must have matched */
return 1;
} else {
int mname_len;
mname_len = (int)strlen(mname);
item += cname_len+1;
len -= cname_len+1;
if ( strncmp(mname, item, (len>mname_len?mname_len:len))==0 ) {
return 1;
}
}
}
}
return 0;
}
/* Determines if a class/method is specified by this list
* list String of comma separated pattern items
* cname Class name, e.g. "java.lang.Object"
* mname Method name, e.g. "<init>"
* Returns 1(true) or 0(false).
*/
static int
covered_by_list(char *list, char *cname, char *mname)
{
char token[1024];
char *next;
if ( list[0] == 0 ) {
return 0;
}
next = get_token(list, ",", token, sizeof(token));
while ( next != NULL ) {
if ( covered_by_list_item(token, cname, mname) ) {
return 1;
}
next = get_token(next, ",", token, sizeof(token));
}
return 0;
}
/* Determines which class and methods we are interested in
* cname Class name, e.g. "java.lang.Object"
* mname Method name, e.g. "<init>"
* include_list Empty or an explicit list for inclusion
* exclude_list Empty or an explicit list for exclusion
* Returns 1(true) or 0(false).
*/
int
interested(char *cname, char *mname, char *include_list, char *exclude_list)
{
if ( exclude_list!=NULL && exclude_list[0]!=0 &&
covered_by_list(exclude_list, cname, mname) ) {
return 0;
}
if ( include_list!=NULL && include_list[0]!=0 &&
!covered_by_list(include_list, cname, mname) ) {
return 0;
}
return 1;
}
/* ------------------------------------------------------------------- */
/* Generic JVMTI utility functions */
/* Every JVMTI interface returns an error code, which should be checked
* to avoid any cascading errors down the line.
* The interface GetErrorName() returns the actual enumeration constant
* name, making the error messages much easier to understand.
*/
void
check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char *str)
{
if ( errnum != JVMTI_ERROR_NONE ) {
char *errnum_str;
errnum_str = NULL;
(void)(*jvmti)->GetErrorName(jvmti, errnum, &errnum_str);
fatal_error("ERROR: JVMTI: %d(%s): %s\n", errnum,
(errnum_str==NULL?"Unknown":errnum_str),
(str==NULL?"":str));
}
}
/* All memory allocated by JVMTI must be freed by the JVMTI Deallocate
* interface.
*/
void
deallocate(jvmtiEnv *jvmti, void *ptr)
{
jvmtiError error;
error = (*jvmti)->Deallocate(jvmti, ptr);
check_jvmti_error(jvmti, error, "Cannot deallocate memory");
}
/* Allocation of JVMTI managed memory */
void *
allocate(jvmtiEnv *jvmti, jint len)
{
jvmtiError error;
void *ptr;
error = (*jvmti)->Allocate(jvmti, len, (unsigned char **)&ptr);
check_jvmti_error(jvmti, error, "Cannot allocate memory");
return ptr;
}
/* Add demo jar file to boot class path (the BCI Tracker class must be
* in the boot classpath)
*
* WARNING: This code assumes that the jar file can be found at one of:
* ${JAVA_HOME}/demo/jvmti/${DEMO_NAME}/${DEMO_NAME}.jar
* ${JAVA_HOME}/../demo/jvmti/${DEMO_NAME}/${DEMO_NAME}.jar
* where JAVA_HOME may refer to the jre directory.
* Both these values are added to the boot classpath.
* These locations are only true for these demos, installed
* in the JDK area. Platform specific code could be used to
* find the location of the DLL or .so library, and construct a
* path name to the jar file, relative to the library location.
*/
void
add_demo_jar_to_bootclasspath(jvmtiEnv *jvmti, char *demo_name)
{
jvmtiError error;
char *file_sep;
int max_len;
char *java_home;
char jar_path[FILENAME_MAX+1];
java_home = NULL;
error = (*jvmti)->GetSystemProperty(jvmti, "java.home", &java_home);
check_jvmti_error(jvmti, error, "Cannot get java.home property value");
if ( java_home == NULL || java_home[0] == 0 ) {
fatal_error("ERROR: Java home not found\n");
}
#ifdef WIN32
file_sep = "\\";
#else
file_sep = "/";
#endif
max_len = (int)(strlen(java_home) + strlen(demo_name)*2 +
strlen(file_sep)*5 +
16 /* ".." "demo" "jvmti" ".jar" NULL */ );
if ( max_len > (int)sizeof(jar_path) ) {
fatal_error("ERROR: Path to jar file too long\n");
}
(void)strcpy(jar_path, java_home);
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, "demo");
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, "jvmti");
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, demo_name);
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, demo_name);
(void)strcat(jar_path, ".jar");
error = (*jvmti)->AddToBootstrapClassLoaderSearch(jvmti, (const char*)jar_path);
check_jvmti_error(jvmti, error, "Cannot add to boot classpath");
(void)strcpy(jar_path, java_home);
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, "..");
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, "demo");
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, "jvmti");
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, demo_name);
(void)strcat(jar_path, file_sep);
(void)strcat(jar_path, demo_name);
(void)strcat(jar_path, ".jar");
error = (*jvmti)->AddToBootstrapClassLoaderSearch(jvmti, (const char*)jar_path);
check_jvmti_error(jvmti, error, "Cannot add to boot classpath");
}
/* ------------------------------------------------------------------- */