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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
/*
* author Tomas Hurka
* Ian Formanek
* Misha Dmitriev
*/
#ifdef WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#include <fcntl.h>
#include <time.h>
#endif
#ifdef SOLARIS
#define _STRUCTURED_PROC 1
#include <sys/procfs.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "jni.h"
#include "jvmti.h"
#include "org_netbeans_lib_profiler_server_system_Stacks.h"
#include "common_functions.h"
#include "Threads.h"
#define NEEDS_CONVERSION (sizeof(jmethodID)!=sizeof(jint))
#define NO_OF_BASE_BITS 2
#define NO_OF_MASK_BITS (32-NO_OF_BASE_BITS)
#define NO_OF_BASE_ADDRESS (1<<NO_OF_BASE_BITS)
#define OFFSET_MASK ((1L<<NO_OF_MASK_BITS)-1)
#define BASE_ADDRESS_MASK (~OFFSET_MASK)
#define MAX_FRAMES 100
static jvmtiFrameInfo *_stack_frames_buffer = NULL;
static jint *_stack_id_buffer = NULL;
static jclass threadType = NULL;
static jclass intArrType = NULL;
static long base_addresses[NO_OF_BASE_ADDRESS]={-1L,-1L,-1L,-1L};
static jint convert_jmethodID_to_jint(jmethodID jmethod) {
if (NEEDS_CONVERSION) {
long base_address=(long)jmethod&BASE_ADDRESS_MASK;
unsigned int i;
for (i=0;i<NO_OF_BASE_ADDRESS;i++) {
long ba = base_addresses[i];
if (ba == -1L) {
base_addresses[i] = base_address;
//fprintf(stderr,"Profiler Agent: Registering new base %lx\n",base_address);
}
if (base_addresses[i]==base_address) {
jint offset = (long)jmethod&OFFSET_MASK;
offset |= i<<NO_OF_MASK_BITS;
//fprintf(stderr,"M %p -> %x\n",jmethod,offset);
return offset;
}
}
fprintf(stderr,"Profiler Agent Warning: Cannot convert %p\n",jmethod);
return 0;
} else {
return (jint)jmethod;
}
}
static jmethodID convert_jint_to_jmethodID(jint method) {
if (NEEDS_CONVERSION) {
int offset = method&OFFSET_MASK;
int base_id = ((unsigned int)method)>>NO_OF_MASK_BITS;
jmethodID jmethod = (jmethodID)(base_addresses[base_id]|offset);
//fprintf(stderr,"X %x -> %p\n",method,jmethod);
//fflush(stderr);
return jmethod;
} else {
return (jmethodID)method;
}
}
/*
* Class: org_netbeans_lib_profiler_server_system_Stacks
* Method: getCurrentJavaStackDepth
* Signature: (Ljava/lang/Thread;)I
*/
JNIEXPORT jint JNICALL Java_org_netbeans_lib_profiler_server_system_Stacks_getCurrentJavaStackDepth
(JNIEnv *env, jclass clz, jobject jni_thread)
{
jint count;
(*_jvmti)->GetFrameCount(_jvmti, jni_thread, &count);
return count;
}
/*
* Class: org_netbeans_lib_profiler_server_system_Stacks
* Method: createNativeStackFrameBuffer
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_org_netbeans_lib_profiler_server_system_Stacks_createNativeStackFrameBuffer
(JNIEnv *env, jclass clz, jint sizeInFrames)
{
if (_stack_frames_buffer != NULL) {
Java_org_netbeans_lib_profiler_server_system_Stacks_clearNativeStackFrameBuffer(env, clz);
}
_stack_frames_buffer = calloc(sizeInFrames, sizeof(jvmtiFrameInfo));
_stack_id_buffer = calloc(sizeInFrames, sizeof(jint));
}
/*
* Class: org_netbeans_lib_profiler_server_system_Stacks
* Method: clearNativeStackFrameBuffer
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_netbeans_lib_profiler_server_system_Stacks_clearNativeStackFrameBuffer
(JNIEnv *env, jclass clz)
{
if (_stack_frames_buffer != NULL) {
free(_stack_frames_buffer);
}
if (_stack_id_buffer != NULL) {
free(_stack_id_buffer);
}
_stack_frames_buffer = NULL;
_stack_id_buffer = NULL;
}
/*
* Class: org_netbeans_lib_profiler_server_system_Stacks
* Method: getCurrentStackFrameIds
* Signature: (Ljava/lang/Thread;I[I)I
*/
JNIEXPORT jint JNICALL Java_org_netbeans_lib_profiler_server_system_Stacks_getCurrentStackFrameIds
(JNIEnv *env, jclass clz, jthread jni_thread, jint depth, jintArray ret)
{
jint i, count;
if (_stack_frames_buffer == NULL) {
/* Can happen if profiling stopped concurrently */
return 0;
}
(*_jvmti)->GetStackTrace(_jvmti, jni_thread, 0, depth, _stack_frames_buffer, &count);
for (i = 0; i < count; i++) {
_stack_id_buffer[i] = convert_jmethodID_to_jint(_stack_frames_buffer[i].method);
}
(*env)->SetIntArrayRegion(env, ret, 0, count, _stack_id_buffer);
return count;
}
static jbyte *byteData;
static jint *strOffsets;
static int byteDataLen, dataOfs, ofsIdx;
static void copy_into_data_array(char *s) {
int len = strlen(s);
if (dataOfs + len > byteDataLen) {
jbyte *oldByteData = byteData;
int newLen = byteDataLen * 2;
if (newLen < dataOfs + len) {
newLen = dataOfs+len;
}
byteData = malloc(newLen);
memcpy(byteData, oldByteData, dataOfs);
free(oldByteData);
byteDataLen = newLen;
}
strncpy((char*)(byteData + dataOfs), s, len);
strOffsets[ofsIdx++] = dataOfs;
dataOfs += len;
}
static void copy_dummy_names_into_data_array() {
copy_into_data_array("<unknown class>");
copy_into_data_array("<unknown method>");
copy_into_data_array("()V");
}
/*
* Class: org_netbeans_lib_profiler_server_system_Stacks
* Method: getMethodNamesForJMethodIds
* Signature: (I[I[I)[B
*/
JNIEXPORT jbyteArray JNICALL Java_org_netbeans_lib_profiler_server_system_Stacks_getMethodNamesForJMethodIds
(JNIEnv *env, jclass clz, jint nMethods, jintArray jmethodIds, jintArray packedArrayOffsets)
{
jvmtiError res;
int i, len;
jint *methodIds;
jbyteArray ret;
// fprintf (stderr, "1");
methodIds = (jint*) malloc(sizeof(jint) * nMethods);
(*env)->GetIntArrayRegion(env, jmethodIds, 0, nMethods, methodIds);
strOffsets = (jint*) malloc(sizeof(jint) * nMethods * 3);
byteDataLen = nMethods * 3 * 10; /* The initial size for the packed strings array */
byteData = (jbyte*) malloc(byteDataLen);
// fprintf (stderr, "2");
dataOfs = ofsIdx = 0;
for (i = 0; i < nMethods; i++) {
jclass declaringClass;
char *className, *genericSignature, *methodName, *methodSig, *genericMethodSig;
jmethodID methodID = convert_jint_to_jmethodID(methodIds[i]);
//fprintf (stderr, "Going to call GetMethodDeclaringClass for methodId = %d\n", *(int*)methodID);
res = (*_jvmti)->GetMethodDeclaringClass(_jvmti, methodID, &declaringClass);
if (res != JVMTI_ERROR_NONE || declaringClass == NULL || *((int*)declaringClass) == 0) { /* Also a bug workaround */
fprintf(stderr, "Profiler Agent Warning: Invalid declaringClass obtained from jmethodID\n");
fprintf(stderr, "Profiler Agent Warning: mId = %p, *mId = %d\n", methodID, *(int*)methodID);
fprintf(stderr, "Profiler Agent Warning: dCl = %p", declaringClass);
if (declaringClass != NULL) {
fprintf(stderr, ", *dCl = %d\n", *((int*)declaringClass));
} else {
fprintf(stderr, "\n");
}
// fprintf(stderr, "*** res = %d", res);
copy_dummy_names_into_data_array();
continue;
}
// fprintf (stderr, "Going to call GetClassSignature for methodId = %d, last res = %d, declaring class: %d\n", *(int*)methodID, res, *((int*)declaringClass));
res = (*_jvmti)->GetClassSignature(_jvmti, declaringClass, &className, &genericSignature);
if (res != JVMTI_ERROR_NONE) {
fprintf(stderr, "Profiler Agent Warning: Couldn't obtain name of declaringClass = %p\n", declaringClass);
copy_dummy_names_into_data_array();
continue;
}
// fprintf (stderr, "Going to call GetMethodName for methodId = %d, last res = %d, signature: %s\n", *(int*)methodID, res, genericSignature);
res = (*_jvmti)->GetMethodName(_jvmti, methodID, &methodName, &methodSig, &genericMethodSig);
if (res != JVMTI_ERROR_NONE) {
fprintf(stderr, "Profiler Agent Warning: Couldn't obtain name for methodID = %p\n", methodID);
copy_dummy_names_into_data_array();
continue;
}
// fprintf (stderr, "Going to copy results, last res = %d, method name: %s, sig: %s, genSig: %s\n", res, methodName, methodSig, genericMethodSig);
len = strlen(className);
if (className[0] == 'L' && className[len-1] == ';') {
className[len-1] = 0;
copy_into_data_array(className+1);
} else {
copy_into_data_array(className);
}
copy_into_data_array(methodName);
copy_into_data_array(methodSig);
(*_jvmti)->Deallocate(_jvmti, (void*)className);
if (genericSignature != NULL) {
(*_jvmti)->Deallocate(_jvmti, (void*)genericSignature);
}
(*_jvmti)->Deallocate(_jvmti, (void*)methodName);
(*_jvmti)->Deallocate(_jvmti, (void*)methodSig);
if (genericMethodSig != NULL) {
(*_jvmti)->Deallocate(_jvmti, (void*)genericMethodSig);
}
}
// fprintf (stderr, "3");
free(methodIds);
ret = (*env)->NewByteArray(env, dataOfs);
(*env)->SetByteArrayRegion(env, ret, 0, dataOfs, byteData);
(*env)->SetIntArrayRegion(env, packedArrayOffsets, 0, nMethods*3, strOffsets);
// fprintf (stderr, "4");
free(strOffsets);
free(byteData);
return ret;
}
/*
* Class: org_netbeans_lib_profiler_server_system_Stacks
* Method: getAllStackTraces
* Signature: ([[Ljava/lang/Thread;[[I[[[I)V
*/
JNIEXPORT void JNICALL Java_org_netbeans_lib_profiler_server_system_Stacks_getAllStackTraces
(JNIEnv *env, jclass clz, jobjectArray threads, jobjectArray states, jobjectArray frames)
{
jobjectArray jthreadArr;
jobjectArray statesArr;
jobjectArray methodIdArrArr;
jvmtiStackInfo *stack_info;
jint *state_buffer;
jint thread_count;
int ti;
jvmtiError err;
err = (*_jvmti)->GetAllStackTraces(_jvmti, MAX_FRAMES, &stack_info, &thread_count);
if (err != JVMTI_ERROR_NONE) {
return;
}
if (threadType == NULL) {
threadType = (*env)->FindClass(env, "java/lang/Thread");
threadType = (*env)->NewGlobalRef(env, threadType);
}
if (intArrType == NULL) {
intArrType = (*env)->FindClass(env, "[I");
intArrType = (*env)->NewGlobalRef(env, intArrType);
}
jthreadArr = (*env)->NewObjectArray(env, thread_count, threadType, NULL);
(*env)->SetObjectArrayElement(env, threads, 0, jthreadArr);
statesArr = (*env)->NewIntArray(env, thread_count);
(*env)->SetObjectArrayElement(env, states, 0, statesArr);
methodIdArrArr = (*env)->NewObjectArray(env, thread_count, intArrType, NULL);
(*env)->SetObjectArrayElement(env, frames, 0, methodIdArrArr);
state_buffer = calloc(thread_count, sizeof(jint));
for (ti = 0; ti < thread_count; ti++) {
jvmtiStackInfo *infop = &stack_info[ti];
jthread thread = infop->thread;
jint state = infop->state;
jvmtiFrameInfo *frames = infop->frame_buffer;
jobjectArray jmethodIdArr;
jint *id_buffer;
int fi;
(*env)->SetObjectArrayElement(env, jthreadArr, ti, thread);
state_buffer[ti] = convert_JVMTI_thread_status_to_jfluid_status(state);
jmethodIdArr = (*env)->NewIntArray(env, infop->frame_count);
(*env)->SetObjectArrayElement(env, methodIdArrArr, ti, jmethodIdArr);
id_buffer = calloc(infop->frame_count, sizeof(jint));
for (fi = 0; fi < infop->frame_count; fi++) {
id_buffer[fi] = convert_jmethodID_to_jint(frames[fi].method);
}
(*env)->SetIntArrayRegion(env, jmethodIdArr, 0, infop->frame_count, id_buffer);
free(id_buffer);
}
(*env)->SetIntArrayRegion(env, statesArr, 0, thread_count, state_buffer);
/* this one Deallocate call frees all data allocated by GetAllStackTraces */
err = (*_jvmti)->Deallocate(_jvmti, (unsigned char*)stack_info);
assert(err == JVMTI_ERROR_NONE);
free(state_buffer);
}
|