src/share/vm/prims/forte.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 7994
04ff2f6cd0eb
child 9852
70aa912cebe5
permissions
-rw-r--r--

Merge

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

mercurial