src/share/vm/prims/forte.cpp

Tue, 08 Apr 2008 12:23:15 -0400

author
sgoldman
date
Tue, 08 Apr 2008 12:23:15 -0400
changeset 542
93b6525e3b82
parent 435
a61af66fc99e
child 631
d1605aabd0a1
permissions
-rw-r--r--

6603919: Stackwalking crash on x86 -server with Sun Studio's collect -j on
Summary: Rewrite frame::safe_for_sender and friends to be safe for collector/analyzer
Reviewed-by: dcubed, kvn

duke@435 1 /*
duke@435 2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_forte.cpp.incl"
duke@435 27
sgoldman@542 28 // These name match the names reported by the forte quality kit
sgoldman@542 29 enum {
sgoldman@542 30 ticks_no_Java_frame = 0,
sgoldman@542 31 ticks_no_class_load = -1,
sgoldman@542 32 ticks_GC_active = -2,
sgoldman@542 33 ticks_unknown_not_Java = -3,
sgoldman@542 34 ticks_not_walkable_not_Java = -4,
sgoldman@542 35 ticks_unknown_Java = -5,
sgoldman@542 36 ticks_not_walkable_Java = -6,
sgoldman@542 37 ticks_unknown_state = -7,
sgoldman@542 38 ticks_thread_exit = -8,
sgoldman@542 39 ticks_deopt = -9,
sgoldman@542 40 ticks_safepoint = -10
sgoldman@542 41 };
duke@435 42
duke@435 43 //-------------------------------------------------------
duke@435 44
duke@435 45 // Native interfaces for use by Forte tools.
duke@435 46
duke@435 47
duke@435 48 #ifndef IA64
duke@435 49
duke@435 50 class vframeStreamForte : public vframeStreamCommon {
duke@435 51 public:
duke@435 52 // constructor that starts with sender of frame fr (top_frame)
duke@435 53 vframeStreamForte(JavaThread *jt, frame fr, bool stop_at_java_call_stub);
duke@435 54 void forte_next();
duke@435 55 };
duke@435 56
duke@435 57
sgoldman@542 58 static void is_decipherable_compiled_frame(frame* fr, RegisterMap* map,
duke@435 59 bool* is_compiled_p, bool* is_walkable_p);
sgoldman@542 60 static bool is_decipherable_interpreted_frame(JavaThread* thread,
sgoldman@542 61 frame* fr,
sgoldman@542 62 methodOop* method_p,
sgoldman@542 63 int* bci_p);
duke@435 64
duke@435 65
duke@435 66
duke@435 67
sgoldman@542 68 vframeStreamForte::vframeStreamForte(JavaThread *jt,
sgoldman@542 69 frame fr,
sgoldman@542 70 bool stop_at_java_call_stub) : vframeStreamCommon(jt) {
duke@435 71
sgoldman@542 72 _stop_at_java_call_stub = stop_at_java_call_stub;
sgoldman@542 73 _frame = fr;
duke@435 74
sgoldman@542 75 // We must always have a valid frame to start filling
duke@435 76
sgoldman@542 77 bool filled_in = fill_from_frame();
duke@435 78
sgoldman@542 79 assert(filled_in, "invariant");
duke@435 80
duke@435 81 }
duke@435 82
duke@435 83
duke@435 84 // Solaris SPARC Compiler1 needs an additional check on the grandparent
duke@435 85 // of the top_frame when the parent of the top_frame is interpreted and
duke@435 86 // the grandparent is compiled. However, in this method we do not know
duke@435 87 // the relationship of the current _frame relative to the top_frame so
duke@435 88 // we implement a more broad sanity check. When the previous callee is
duke@435 89 // interpreted and the current sender is compiled, we verify that the
duke@435 90 // current sender is also walkable. If it is not walkable, then we mark
duke@435 91 // the current vframeStream as at the end.
duke@435 92 void vframeStreamForte::forte_next() {
duke@435 93 // handle frames with inlining
duke@435 94 if (_mode == compiled_mode &&
duke@435 95 vframeStreamCommon::fill_in_compiled_inlined_sender()) {
duke@435 96 return;
duke@435 97 }
duke@435 98
duke@435 99 // handle general case
duke@435 100
duke@435 101 int loop_count = 0;
duke@435 102 int loop_max = MaxJavaStackTraceDepth * 2;
duke@435 103
duke@435 104
duke@435 105 do {
duke@435 106
sgoldman@542 107 loop_count++;
duke@435 108
sgoldman@542 109 // By the time we get here we should never see unsafe but better
sgoldman@542 110 // safe then segv'd
duke@435 111
sgoldman@542 112 if (loop_count > loop_max || !_frame.safe_for_sender(_thread)) {
duke@435 113 _mode = at_end_mode;
duke@435 114 return;
duke@435 115 }
duke@435 116
sgoldman@542 117 _frame = _frame.sender(&_reg_map);
duke@435 118
duke@435 119 } while (!fill_from_frame());
duke@435 120 }
duke@435 121
sgoldman@542 122 // Determine if 'fr' is a decipherable compiled frame. We are already
sgoldman@542 123 // assured that fr is for a java nmethod.
duke@435 124
sgoldman@542 125 static bool is_decipherable_compiled_frame(frame* fr) {
duke@435 126
sgoldman@542 127 assert(fr->cb() != NULL && fr->cb()->is_nmethod(), "invariant");
sgoldman@542 128 nmethod* nm = (nmethod*) fr->cb();
sgoldman@542 129 assert(nm->is_java_method(), "invariant");
duke@435 130
sgoldman@542 131 // First try and find an exact PcDesc
sgoldman@542 132
sgoldman@542 133 PcDesc* pc_desc = nm->pc_desc_at(fr->pc());
sgoldman@542 134
sgoldman@542 135 // Did we find a useful PcDesc?
sgoldman@542 136 if (pc_desc != NULL &&
sgoldman@542 137 pc_desc->scope_decode_offset() == DebugInformationRecorder::serialized_null) {
sgoldman@542 138
sgoldman@542 139 address probe_pc = fr->pc() + 1;
sgoldman@542 140 pc_desc = nm->pc_desc_near(probe_pc);
sgoldman@542 141
sgoldman@542 142 // Now do we have a useful PcDesc?
sgoldman@542 143
sgoldman@542 144 if (pc_desc != NULL &&
sgoldman@542 145 pc_desc->scope_decode_offset() == DebugInformationRecorder::serialized_null) {
sgoldman@542 146 // No debug information available for this pc
sgoldman@542 147 // vframeStream would explode if we try and walk the frames.
sgoldman@542 148 return false;
duke@435 149 }
sgoldman@542 150
sgoldman@542 151 // This PcDesc is useful however we must adjust the frame's pc
sgoldman@542 152 // so that the vframeStream lookups will use this same pc
sgoldman@542 153
sgoldman@542 154 fr->set_pc(pc_desc->real_pc(nm));
duke@435 155 }
sgoldman@542 156
sgoldman@542 157 return true;
duke@435 158 }
duke@435 159
duke@435 160 // Determine if 'fr' is a walkable interpreted frame. Returns false
duke@435 161 // if it is not. *method_p, and *bci_p are not set when false is
duke@435 162 // returned. *method_p is non-NULL if frame was executing a Java
duke@435 163 // method. *bci_p is != -1 if a valid BCI in the Java method could
duke@435 164 // be found.
duke@435 165 // Note: this method returns true when a valid Java method is found
duke@435 166 // even if a valid BCI cannot be found.
duke@435 167
sgoldman@542 168 static bool is_decipherable_interpreted_frame(JavaThread* thread,
sgoldman@542 169 frame* fr,
sgoldman@542 170 methodOop* method_p,
sgoldman@542 171 int* bci_p) {
duke@435 172 assert(fr->is_interpreted_frame(), "just checking");
duke@435 173
duke@435 174 // top frame is an interpreted frame
duke@435 175 // check if it is walkable (i.e. valid methodOop and valid bci)
sgoldman@542 176
sgoldman@542 177 // Because we may be racing a gc thread the method and/or bci
sgoldman@542 178 // of a valid interpreter frame may look bad causing us to
sgoldman@542 179 // fail the is_interpreted_frame_valid test. If the thread
sgoldman@542 180 // is in any of the following states we are assured that the
sgoldman@542 181 // frame is in fact valid and we must have hit the race.
sgoldman@542 182
sgoldman@542 183 JavaThreadState state = thread->thread_state();
sgoldman@542 184 bool known_valid = (state == _thread_in_native ||
sgoldman@542 185 state == _thread_in_vm ||
sgoldman@542 186 state == _thread_blocked );
sgoldman@542 187
sgoldman@542 188 if (known_valid || fr->is_interpreted_frame_valid(thread)) {
sgoldman@542 189
sgoldman@542 190 // The frame code should completely validate the frame so that
sgoldman@542 191 // references to methodOop and bci are completely safe to access
sgoldman@542 192 // If they aren't the frame code should be fixed not this
sgoldman@542 193 // code. However since gc isn't locked out the values could be
sgoldman@542 194 // stale. This is a race we can never completely win since we can't
sgoldman@542 195 // lock out gc so do one last check after retrieving their values
sgoldman@542 196 // from the frame for additional safety
sgoldman@542 197
sgoldman@542 198 methodOop method = fr->interpreter_frame_method();
sgoldman@542 199
sgoldman@542 200 // We've at least found a method.
sgoldman@542 201 // NOTE: there is something to be said for the approach that
sgoldman@542 202 // if we don't find a valid bci then the method is not likely
sgoldman@542 203 // a valid method. Then again we may have caught an interpreter
sgoldman@542 204 // frame in the middle of construction and the bci field is
sgoldman@542 205 // not yet valid.
sgoldman@542 206
sgoldman@542 207 *method_p = method;
sgoldman@542 208
sgoldman@542 209 // See if gc may have invalidated method since we validated frame
sgoldman@542 210
sgoldman@542 211 if (!Universe::heap()->is_valid_method(method)) return false;
sgoldman@542 212
sgoldman@542 213 intptr_t bcx = fr->interpreter_frame_bcx();
sgoldman@542 214
sgoldman@542 215 int bci = method->validate_bci_from_bcx(bcx);
sgoldman@542 216
sgoldman@542 217 // note: bci is set to -1 if not a valid bci
sgoldman@542 218 *bci_p = bci;
sgoldman@542 219 return true;
duke@435 220 }
sgoldman@542 221
duke@435 222 return false;
duke@435 223 }
duke@435 224
duke@435 225
sgoldman@542 226 // Determine if 'fr' can be used to find an initial Java frame.
sgoldman@542 227 // Return false if it can not find a fully decipherable Java frame
sgoldman@542 228 // (in other words a frame that isn't safe to use in a vframe stream).
sgoldman@542 229 // Obviously if it can't even find a Java frame false will also be returned.
duke@435 230 //
sgoldman@542 231 // If we find a Java frame decipherable or not then by definition we have
sgoldman@542 232 // identified a method and that will be returned to the caller via method_p.
sgoldman@542 233 // If we can determine a bci that is returned also. (Hmm is it possible
sgoldman@542 234 // to return a method and bci and still return false? )
sgoldman@542 235 //
sgoldman@542 236 // The initial Java frame we find (if any) is return via initial_frame_p.
sgoldman@542 237 //
duke@435 238
sgoldman@542 239 static bool find_initial_Java_frame(JavaThread* thread,
sgoldman@542 240 frame* fr,
sgoldman@542 241 frame* initial_frame_p,
sgoldman@542 242 methodOop* method_p,
sgoldman@542 243 int* bci_p) {
sgoldman@542 244
sgoldman@542 245 // It is possible that for a frame containing an nmethod
sgoldman@542 246 // we can capture the method but no bci. If we get no
sgoldman@542 247 // bci the frame isn't walkable but the method is usable.
sgoldman@542 248 // Therefore we init the returned methodOop to NULL so the
sgoldman@542 249 // caller can make the distinction.
sgoldman@542 250
sgoldman@542 251 *method_p = NULL;
sgoldman@542 252
sgoldman@542 253 // On the initial call to this method the frame we get may not be
sgoldman@542 254 // recognizable to us. This should only happen if we are in a JRT_LEAF
sgoldman@542 255 // or something called by a JRT_LEAF method.
sgoldman@542 256
sgoldman@542 257
sgoldman@542 258
sgoldman@542 259 frame candidate = *fr;
sgoldman@542 260
sgoldman@542 261 // If the starting frame we were given has no codeBlob associated with
sgoldman@542 262 // it see if we can find such a frame because only frames with codeBlobs
sgoldman@542 263 // are possible Java frames.
sgoldman@542 264
sgoldman@542 265 if (fr->cb() == NULL) {
sgoldman@542 266
sgoldman@542 267 // See if we can find a useful frame
sgoldman@542 268 int loop_count;
sgoldman@542 269 int loop_max = MaxJavaStackTraceDepth * 2;
sgoldman@542 270 RegisterMap map(thread, false);
sgoldman@542 271
sgoldman@542 272 for (loop_count = 0; loop_count < loop_max; loop_count++) {
sgoldman@542 273 if (!candidate.safe_for_sender(thread)) return false;
sgoldman@542 274 candidate = candidate.sender(&map);
sgoldman@542 275 if (candidate.cb() != NULL) break;
sgoldman@542 276 }
sgoldman@542 277 if (candidate.cb() == NULL) return false;
duke@435 278 }
duke@435 279
sgoldman@542 280 // We have a frame known to be in the codeCache
sgoldman@542 281 // We will hopefully be able to figure out something to do with it.
sgoldman@542 282 int loop_count;
sgoldman@542 283 int loop_max = MaxJavaStackTraceDepth * 2;
sgoldman@542 284 RegisterMap map(thread, false);
sgoldman@542 285
sgoldman@542 286 for (loop_count = 0; loop_count < loop_max; loop_count++) {
sgoldman@542 287
sgoldman@542 288 if (candidate.is_first_frame()) {
sgoldman@542 289 // If initial frame is frame from StubGenerator and there is no
sgoldman@542 290 // previous anchor, there are no java frames associated with a method
sgoldman@542 291 return false;
sgoldman@542 292 }
sgoldman@542 293
sgoldman@542 294 if (candidate.is_interpreted_frame()) {
sgoldman@542 295 if (is_decipherable_interpreted_frame(thread, &candidate, method_p, bci_p)) {
sgoldman@542 296 *initial_frame_p = candidate;
sgoldman@542 297 return true;
sgoldman@542 298 }
sgoldman@542 299
sgoldman@542 300 // Hopefully we got some data
sgoldman@542 301 return false;
sgoldman@542 302 }
sgoldman@542 303
sgoldman@542 304 if (candidate.cb()->is_nmethod()) {
sgoldman@542 305
sgoldman@542 306 nmethod* nm = (nmethod*) candidate.cb();
sgoldman@542 307 *method_p = nm->method();
sgoldman@542 308
sgoldman@542 309 // If the frame isn't fully decipherable then the default
sgoldman@542 310 // value for the bci is a signal that we don't have a bci.
sgoldman@542 311 // If we have a decipherable frame this bci value will
sgoldman@542 312 // not be used.
sgoldman@542 313
sgoldman@542 314 *bci_p = -1;
sgoldman@542 315
sgoldman@542 316 *initial_frame_p = candidate;
sgoldman@542 317
sgoldman@542 318 // Native wrapper code is trivial to decode by vframeStream
sgoldman@542 319
sgoldman@542 320 if (nm->is_native_method()) return true;
sgoldman@542 321
sgoldman@542 322 // If it isn't decipherable then we have found a pc that doesn't
sgoldman@542 323 // have a PCDesc that can get us a bci however we did find
sgoldman@542 324 // a method
sgoldman@542 325
sgoldman@542 326 if (!is_decipherable_compiled_frame(&candidate)) {
sgoldman@542 327 return false;
sgoldman@542 328 }
sgoldman@542 329
sgoldman@542 330 // is_decipherable_compiled_frame may modify candidate's pc
sgoldman@542 331 *initial_frame_p = candidate;
sgoldman@542 332
sgoldman@542 333 return true;
sgoldman@542 334 }
sgoldman@542 335
sgoldman@542 336 // Must be some stub frame that we don't care about
sgoldman@542 337
sgoldman@542 338 if (!candidate.safe_for_sender(thread)) return false;
sgoldman@542 339 candidate = candidate.sender(&map);
sgoldman@542 340
sgoldman@542 341 // If it isn't in the code cache something is wrong
sgoldman@542 342 // since once we find a frame in the code cache they
sgoldman@542 343 // all should be there.
sgoldman@542 344
sgoldman@542 345 if (candidate.cb() == NULL) return false;
sgoldman@542 346
duke@435 347 }
duke@435 348
sgoldman@542 349 return false;
duke@435 350
duke@435 351 }
duke@435 352
duke@435 353
duke@435 354 // call frame copied from old .h file and renamed
duke@435 355 typedef struct {
duke@435 356 jint lineno; // line number in the source file
duke@435 357 jmethodID method_id; // method executed in this frame
duke@435 358 } ASGCT_CallFrame;
duke@435 359
duke@435 360 // call trace copied from old .h file and renamed
duke@435 361 typedef struct {
duke@435 362 JNIEnv *env_id; // Env where trace was recorded
duke@435 363 jint num_frames; // number of frames in this trace
duke@435 364 ASGCT_CallFrame *frames; // frames
duke@435 365 } ASGCT_CallTrace;
duke@435 366
duke@435 367 static void forte_fill_call_trace_given_top(JavaThread* thd,
sgoldman@542 368 ASGCT_CallTrace* trace,
sgoldman@542 369 int depth,
sgoldman@542 370 frame top_frame) {
duke@435 371 NoHandleMark nhm;
duke@435 372
sgoldman@542 373 frame initial_Java_frame;
duke@435 374 methodOop method;
duke@435 375 int bci;
duke@435 376 int count;
duke@435 377
duke@435 378 count = 0;
duke@435 379 assert(trace->frames != NULL, "trace->frames must be non-NULL");
duke@435 380
sgoldman@542 381 bool fully_decipherable = find_initial_Java_frame(thd, &top_frame, &initial_Java_frame, &method, &bci);
sgoldman@542 382
sgoldman@542 383 // The frame might not be walkable but still recovered a method
sgoldman@542 384 // (e.g. an nmethod with no scope info for the pc
sgoldman@542 385
sgoldman@542 386 if (method == NULL) return;
sgoldman@542 387
sgoldman@542 388 CollectedHeap* ch = Universe::heap();
sgoldman@542 389
sgoldman@542 390 // The method is not stored GC safe so see if GC became active
sgoldman@542 391 // after we entered AsyncGetCallTrace() and before we try to
sgoldman@542 392 // use the methodOop.
sgoldman@542 393 // Yes, there is still a window after this check and before
sgoldman@542 394 // we use methodOop below, but we can't lock out GC so that
sgoldman@542 395 // has to be an acceptable risk.
sgoldman@542 396 if (!ch->is_valid_method(method)) {
sgoldman@542 397 trace->num_frames = ticks_GC_active; // -2
duke@435 398 return;
duke@435 399 }
duke@435 400
sgoldman@542 401 // We got a Java frame however it isn't fully decipherable
sgoldman@542 402 // so it won't necessarily be safe to use it for the
sgoldman@542 403 // initial frame in the vframe stream.
duke@435 404
sgoldman@542 405 if (!fully_decipherable) {
sgoldman@542 406 // Take whatever method the top-frame decoder managed to scrape up.
sgoldman@542 407 // We look further at the top frame only if non-safepoint
sgoldman@542 408 // debugging information is available.
sgoldman@542 409 count++;
sgoldman@542 410 trace->num_frames = count;
sgoldman@542 411 trace->frames[0].method_id = method->find_jmethod_id_or_null();
sgoldman@542 412 if (!method->is_native()) {
sgoldman@542 413 trace->frames[0].lineno = bci;
sgoldman@542 414 } else {
sgoldman@542 415 trace->frames[0].lineno = -3;
duke@435 416 }
duke@435 417
sgoldman@542 418 if (!initial_Java_frame.safe_for_sender(thd)) return;
sgoldman@542 419
sgoldman@542 420 RegisterMap map(thd, false);
sgoldman@542 421 initial_Java_frame = initial_Java_frame.sender(&map);
duke@435 422 }
duke@435 423
sgoldman@542 424 vframeStreamForte st(thd, initial_Java_frame, false);
duke@435 425
duke@435 426 for (; !st.at_end() && count < depth; st.forte_next(), count++) {
duke@435 427 bci = st.bci();
duke@435 428 method = st.method();
duke@435 429
duke@435 430 // The method is not stored GC safe so see if GC became active
duke@435 431 // after we entered AsyncGetCallTrace() and before we try to
duke@435 432 // use the methodOop.
duke@435 433 // Yes, there is still a window after this check and before
duke@435 434 // we use methodOop below, but we can't lock out GC so that
duke@435 435 // has to be an acceptable risk.
duke@435 436 if (!ch->is_valid_method(method)) {
duke@435 437 // we throw away everything we've gathered in this sample since
duke@435 438 // none of it is safe
sgoldman@542 439 trace->num_frames = ticks_GC_active; // -2
duke@435 440 return;
duke@435 441 }
duke@435 442
duke@435 443 trace->frames[count].method_id = method->find_jmethod_id_or_null();
duke@435 444 if (!method->is_native()) {
duke@435 445 trace->frames[count].lineno = bci;
duke@435 446 } else {
duke@435 447 trace->frames[count].lineno = -3;
duke@435 448 }
duke@435 449 }
duke@435 450 trace->num_frames = count;
duke@435 451 return;
duke@435 452 }
duke@435 453
duke@435 454
duke@435 455 // Forte Analyzer AsyncGetCallTrace() entry point. Currently supported
duke@435 456 // on Linux X86, Solaris SPARC and Solaris X86.
duke@435 457 //
duke@435 458 // Async-safe version of GetCallTrace being called from a signal handler
duke@435 459 // when a LWP gets interrupted by SIGPROF but the stack traces are filled
duke@435 460 // with different content (see below).
duke@435 461 //
duke@435 462 // This function must only be called when JVM/TI
duke@435 463 // CLASS_LOAD events have been enabled since agent startup. The enabled
duke@435 464 // event will cause the jmethodIDs to be allocated at class load time.
duke@435 465 // The jmethodIDs cannot be allocated in a signal handler because locks
duke@435 466 // cannot be grabbed in a signal handler safely.
duke@435 467 //
duke@435 468 // void (*AsyncGetCallTrace)(ASGCT_CallTrace *trace, jint depth, void* ucontext)
duke@435 469 //
duke@435 470 // Called by the profiler to obtain the current method call stack trace for
duke@435 471 // a given thread. The thread is identified by the env_id field in the
duke@435 472 // ASGCT_CallTrace structure. The profiler agent should allocate a ASGCT_CallTrace
duke@435 473 // structure with enough memory for the requested stack depth. The VM fills in
duke@435 474 // the frames buffer and the num_frames field.
duke@435 475 //
duke@435 476 // Arguments:
duke@435 477 //
duke@435 478 // trace - trace data structure to be filled by the VM.
duke@435 479 // depth - depth of the call stack trace.
duke@435 480 // ucontext - ucontext_t of the LWP
duke@435 481 //
duke@435 482 // ASGCT_CallTrace:
duke@435 483 // typedef struct {
duke@435 484 // JNIEnv *env_id;
duke@435 485 // jint num_frames;
duke@435 486 // ASGCT_CallFrame *frames;
duke@435 487 // } ASGCT_CallTrace;
duke@435 488 //
duke@435 489 // Fields:
duke@435 490 // env_id - ID of thread which executed this trace.
duke@435 491 // num_frames - number of frames in the trace.
duke@435 492 // (< 0 indicates the frame is not walkable).
duke@435 493 // frames - the ASGCT_CallFrames that make up this trace. Callee followed by callers.
duke@435 494 //
duke@435 495 // ASGCT_CallFrame:
duke@435 496 // typedef struct {
duke@435 497 // jint lineno;
duke@435 498 // jmethodID method_id;
duke@435 499 // } ASGCT_CallFrame;
duke@435 500 //
duke@435 501 // Fields:
duke@435 502 // 1) For Java frame (interpreted and compiled),
duke@435 503 // lineno - bci of the method being executed or -1 if bci is not available
duke@435 504 // method_id - jmethodID of the method being executed
duke@435 505 // 2) For native method
duke@435 506 // lineno - (-3)
duke@435 507 // method_id - jmethodID of the method being executed
duke@435 508
duke@435 509 extern "C" {
duke@435 510 void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) {
sgoldman@542 511
sgoldman@542 512 // This is if'd out because we no longer use thread suspension.
sgoldman@542 513 // However if someone wanted to backport this to a 5.0 jvm then this
sgoldman@542 514 // code would be important.
sgoldman@542 515 #if 0
duke@435 516 if (SafepointSynchronize::is_synchronizing()) {
duke@435 517 // The safepoint mechanism is trying to synchronize all the threads.
duke@435 518 // Since this can involve thread suspension, it is not safe for us
duke@435 519 // to be here. We can reduce the deadlock risk window by quickly
duke@435 520 // returning to the SIGPROF handler. However, it is still possible
duke@435 521 // for VMThread to catch us here or in the SIGPROF handler. If we
duke@435 522 // are suspended while holding a resource and another thread blocks
duke@435 523 // on that resource in the SIGPROF handler, then we will have a
duke@435 524 // three-thread deadlock (VMThread, this thread, the other thread).
sgoldman@542 525 trace->num_frames = ticks_safepoint; // -10
duke@435 526 return;
duke@435 527 }
sgoldman@542 528 #endif
duke@435 529
duke@435 530 JavaThread* thread;
duke@435 531
duke@435 532 if (trace->env_id == NULL ||
duke@435 533 (thread = JavaThread::thread_from_jni_environment(trace->env_id)) == NULL ||
duke@435 534 thread->is_exiting()) {
duke@435 535
duke@435 536 // bad env_id, thread has exited or thread is exiting
sgoldman@542 537 trace->num_frames = ticks_thread_exit; // -8
duke@435 538 return;
duke@435 539 }
duke@435 540
duke@435 541 if (thread->in_deopt_handler()) {
duke@435 542 // thread is in the deoptimization handler so return no frames
sgoldman@542 543 trace->num_frames = ticks_deopt; // -9
duke@435 544 return;
duke@435 545 }
duke@435 546
duke@435 547 assert(JavaThread::current() == thread,
duke@435 548 "AsyncGetCallTrace must be called by the current interrupted thread");
duke@435 549
duke@435 550 if (!JvmtiExport::should_post_class_load()) {
sgoldman@542 551 trace->num_frames = ticks_no_class_load; // -1
duke@435 552 return;
duke@435 553 }
duke@435 554
duke@435 555 if (Universe::heap()->is_gc_active()) {
sgoldman@542 556 trace->num_frames = ticks_GC_active; // -2
duke@435 557 return;
duke@435 558 }
duke@435 559
duke@435 560 switch (thread->thread_state()) {
duke@435 561 case _thread_new:
duke@435 562 case _thread_uninitialized:
duke@435 563 case _thread_new_trans:
duke@435 564 // We found the thread on the threads list above, but it is too
duke@435 565 // young to be useful so return that there are no Java frames.
duke@435 566 trace->num_frames = 0;
duke@435 567 break;
duke@435 568 case _thread_in_native:
duke@435 569 case _thread_in_native_trans:
duke@435 570 case _thread_blocked:
duke@435 571 case _thread_blocked_trans:
duke@435 572 case _thread_in_vm:
duke@435 573 case _thread_in_vm_trans:
duke@435 574 {
duke@435 575 frame fr;
duke@435 576
duke@435 577 // param isInJava == false - indicate we aren't in Java code
duke@435 578 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, false)) {
sgoldman@542 579 trace->num_frames = ticks_unknown_not_Java; // -3 unknown frame
sgoldman@542 580 } else {
duke@435 581 if (!thread->has_last_Java_frame()) {
sgoldman@542 582 trace->num_frames = 0; // No Java frames
duke@435 583 } else {
sgoldman@542 584 trace->num_frames = ticks_not_walkable_not_Java; // -4 non walkable frame by default
sgoldman@542 585 forte_fill_call_trace_given_top(thread, trace, depth, fr);
sgoldman@542 586
sgoldman@542 587 // This assert would seem to be valid but it is not.
sgoldman@542 588 // It would be valid if we weren't possibly racing a gc
sgoldman@542 589 // thread. A gc thread can make a valid interpreted frame
sgoldman@542 590 // look invalid. It's a small window but it does happen.
sgoldman@542 591 // The assert is left here commented out as a reminder.
sgoldman@542 592 // assert(trace->num_frames != ticks_not_walkable_not_Java, "should always be walkable");
sgoldman@542 593
duke@435 594 }
duke@435 595 }
duke@435 596 }
duke@435 597 break;
duke@435 598 case _thread_in_Java:
duke@435 599 case _thread_in_Java_trans:
duke@435 600 {
duke@435 601 frame fr;
duke@435 602
duke@435 603 // param isInJava == true - indicate we are in Java code
duke@435 604 if (!thread->pd_get_top_frame_for_signal_handler(&fr, ucontext, true)) {
sgoldman@542 605 trace->num_frames = ticks_unknown_Java; // -5 unknown frame
duke@435 606 } else {
sgoldman@542 607 trace->num_frames = ticks_not_walkable_Java; // -6, non walkable frame by default
duke@435 608 forte_fill_call_trace_given_top(thread, trace, depth, fr);
duke@435 609 }
duke@435 610 }
duke@435 611 break;
duke@435 612 default:
duke@435 613 // Unknown thread state
sgoldman@542 614 trace->num_frames = ticks_unknown_state; // -7
duke@435 615 break;
duke@435 616 }
duke@435 617 }
duke@435 618
duke@435 619
duke@435 620 #ifndef _WINDOWS
duke@435 621 // Support for the Forte(TM) Peformance Tools collector.
duke@435 622 //
duke@435 623 // The method prototype is derived from libcollector.h. For more
duke@435 624 // information, please see the libcollect man page.
duke@435 625
duke@435 626 // Method to let libcollector know about a dynamically loaded function.
duke@435 627 // Because it is weakly bound, the calls become NOP's when the library
duke@435 628 // isn't present.
duke@435 629 void collector_func_load(char* name,
duke@435 630 void* null_argument_1,
duke@435 631 void* null_argument_2,
duke@435 632 void *vaddr,
duke@435 633 int size,
duke@435 634 int zero_argument,
duke@435 635 void* null_argument_3);
duke@435 636 #pragma weak collector_func_load
duke@435 637 #define collector_func_load(x0,x1,x2,x3,x4,x5,x6) \
duke@435 638 ( collector_func_load ? collector_func_load(x0,x1,x2,x3,x4,x5,x6),0 : 0 )
duke@435 639 #endif // !_WINDOWS
duke@435 640
duke@435 641 } // end extern "C"
duke@435 642 #endif // !IA64
duke@435 643
duke@435 644 void Forte::register_stub(const char* name, address start, address end) {
duke@435 645 #if !defined(_WINDOWS) && !defined(IA64)
duke@435 646 assert(pointer_delta(end, start, sizeof(jbyte)) < INT_MAX,
duke@435 647 "Code size exceeds maximum range")
duke@435 648
duke@435 649 collector_func_load((char*)name, NULL, NULL, start,
duke@435 650 pointer_delta(end, start, sizeof(jbyte)), 0, NULL);
duke@435 651 #endif // !_WINDOWS && !IA64
duke@435 652 }

mercurial