1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/javaClasses.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "memory/oopFactory.hpp" 30 #include "oops/oop.inline.hpp" 31 #include "oops/objArrayOop.inline.hpp" 32 #include "prims/stackwalk.hpp" 33 #include "runtime/globals.hpp" 34 #include "runtime/handles.inline.hpp" 35 #include "runtime/javaCalls.hpp" 36 #include "runtime/vframe.hpp" 37 #include "utilities/globalDefinitions.hpp" 38 39 Klass* StackWalk::_abstractStackWalker_klass = NULL; 40 Klass* StackWalk::_stackWalker_klass = NULL; 41 42 // setup and cleanup actions 43 void StackWalkAnchor::setup_magic_on_entry() { 44 _frames->obj_at_put(StackWalk::magic_pos, _thread->threadObj()); 45 _anchor = address_value(); 46 assert(check_magic(), "invalid magic"); 47 } 48 49 bool StackWalkAnchor::check_magic() { 50 oop m1 = _frames->obj_at(StackWalk::magic_pos); 51 jlong m2 = _anchor; 52 if (m1 == _thread->threadObj() && m2 == address_value()) return true; 53 return false; 54 } 55 56 bool StackWalkAnchor::cleanup_magic_on_exit() { 57 bool ok = check_magic(); 58 _frames->obj_at_put(StackWalk::magic_pos, NULL); 59 _anchor = 0L; 60 return ok; 61 } 62 63 /** 64 * Returns StackWalkAnchor for the current stack being traversed. 65 * 66 * @argument thread. Current Java thread. 67 * @argument magic. Magic value used for each stack walking 68 * @argument classes_array User-supplied buffers. The 0th element is reserved 69 * to this StackWalkAnchor to use 70 */ 71 StackWalkAnchor* StackWalkAnchor::from_current(JavaThread* thread, jlong magic, 72 objArrayHandle classes_array) 73 { 74 assert(thread != NULL && thread->is_Java_thread(), ""); 75 oop m1 = classes_array->obj_at(StackWalk::magic_pos); 76 if (m1 != thread->threadObj()) return NULL; 77 if (magic == 0L) return NULL; 78 StackWalkAnchor* anchor = (StackWalkAnchor*) (intptr_t) magic; 79 if (!anchor->is_valid_in(thread)) return NULL; 80 return anchor; 81 } 82 83 /* 84 * Unpacks one or more frames into user-supplied buffers. 85 * Updates the end index, and returns the number of unpacked frames. 86 * Always start with the existing vfst.method and bci. 87 * Do not call vfst.next to advance over the last returned value. 88 * In other words, do not leave any stale data in the vfst. 89 * 90 * @argument mode. Restrict which frames to be decoded. 91 * @argument decode_limit. Maximum of frames to be decoded. 92 * @argument start_index. Start index to the user-supplied buffers. 93 * @argument classes_array. Buffer to store classes in, starting at start_index. 94 * @argument frames_array. Buffer to store StackFrame in, starting at start_index. 95 * NULL if not used. 96 * @argument end_index. End index to the user-supplied buffers 97 * with unpacked frames. 98 * 99 * @return Number of frames whose information was transferred into the buffers. 100 */ 101 int StackWalkAnchor::decode_frames(jlong mode, int decode_limit, int start_index, 102 objArrayHandle classes_array, 103 objArrayHandle frames_array, 104 int& end_index, TRAPS) { 105 if (TraceStackWalk) { 106 tty->print_cr("decode_frames limit=%d start=%d frames length=%d", 107 decode_limit, start_index, classes_array->length()); 108 } 109 assert(decode_limit > 0, "invalid decode_limit"); 110 assert(start_index + decode_limit <= classes_array->length(), "oob"); 111 112 int frames_decoded = 0; 113 for (; !_vfst.at_end(); _vfst.next()) { 114 Method* method = _vfst.method(); 115 int bci = _vfst.bci(); 116 117 if (method == NULL) continue; 118 if (!ShowHiddenFrames && StackWalk::skip_hidden_frames(mode)) { 119 if (method->is_hidden()) { 120 if (TraceStackWalk) { 121 tty->print(" hidden method: "); method->print_short_name(); 122 } 123 continue; 124 } 125 } 126 127 int index = end_index++; 128 if (TraceStackWalk) { 129 tty->print(" %d: frame method: ", index); method->print_short_name(); 130 tty->print_cr(" bci=%d", bci); 131 } 132 133 classes_array->obj_at_put(index, method->method_holder()->java_mirror()); 134 // fill in StackFrameInfo and initialize MemberName 135 if (StackWalk::live_frame_info(mode)) { 136 Handle stackFrame(frames_array->obj_at(index)); 137 StackWalk::fill_live_stackframe(method, bci, stackFrame, _vfst.java_frame(), CHECK_0); 138 } else if (StackWalk::need_method_info(mode)) { 139 Handle stackFrame(frames_array->obj_at(index)); 140 StackWalk::fill_frame_buffer(method, bci, stackFrame); 141 } 142 if (++frames_decoded >= decode_limit) break; 143 } 144 return frames_decoded; 145 } 146 147 static oop create_primitive_value_instance(StackValueCollection* values, int i, TRAPS) { 148 Klass* k = SystemDictionary::resolve_or_null(vmSymbols::java_lang_LiveStackFrameInfo(), CHECK_NULL); 149 instanceKlassHandle ik (THREAD, k); 150 151 JavaValue result(T_OBJECT); 152 JavaCallArguments args; 153 Symbol* signature; 154 155 // ## TODO: type is only available in LocalVariable table, if present. 156 // ## StackValue type is T_INT or T_OBJECT. 157 switch (values->at(i)->type()) { 158 case T_INT: 159 args.push_int(values->int_at(i)); 160 signature = vmSymbols::asPrimitive_int_signature(); 161 break; 162 163 case T_LONG: 164 args.push_long(values->long_at(i)); 165 signature = vmSymbols::asPrimitive_long_signature(); 166 break; 167 168 case T_FLOAT: 169 args.push_float(values->float_at(i)); 170 signature = vmSymbols::asPrimitive_float_signature(); 171 break; 172 173 case T_DOUBLE: 174 args.push_double(values->double_at(i)); 175 signature = vmSymbols::asPrimitive_double_signature(); 176 break; 177 178 case T_BYTE: 179 args.push_int(values->int_at(i)); 180 signature = vmSymbols::asPrimitive_byte_signature(); 181 break; 182 183 case T_SHORT: 184 args.push_int(values->int_at(i)); 185 signature = vmSymbols::asPrimitive_short_signature(); 186 break; 187 188 case T_CHAR: 189 args.push_int(values->int_at(i)); 190 signature = vmSymbols::asPrimitive_char_signature(); 191 break; 192 193 case T_BOOLEAN: 194 args.push_int(values->int_at(i)); 195 signature = vmSymbols::asPrimitive_boolean_signature(); 196 break; 197 198 case T_OBJECT: 199 return values->obj_at(i)(); 200 201 case T_CONFLICT: 202 return NULL; 203 default: ShouldNotReachHere(); 204 } 205 JavaCalls::call_static(&result, 206 ik, 207 vmSymbols::asPrimitive_name(), 208 signature, 209 &args, 210 CHECK_NULL); 211 return (instanceOop) result.get_jobject(); 212 } 213 214 static objArrayHandle values_to_object_array(StackValueCollection* values, TRAPS) { 215 int length = values->size(); 216 objArrayOop array_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 217 length, CHECK_(objArrayHandle())); 218 objArrayHandle array_h(THREAD, array_oop); 219 for (int i = 0; i < values->size(); i++) { 220 StackValue* st = values->at(i); 221 oop obj = create_primitive_value_instance(values, i, CHECK_(objArrayHandle())); 222 if (obj != NULL) 223 array_h->obj_at_put(i, obj); 224 } 225 return array_h; 226 } 227 228 static objArrayHandle monitors_to_object_array(GrowableArray<MonitorInfo*>* monitors, TRAPS) { 229 int length = monitors->length(); 230 objArrayOop array_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 231 length, CHECK_(objArrayHandle())); 232 objArrayHandle array_h(THREAD, array_oop); 233 for (int i = 0; i < length; i++) { 234 MonitorInfo* monitor = monitors->at(i); 235 array_h->obj_at_put(i, monitor->owner()); 236 } 237 return array_h; 238 } 239 240 /* 241 * Initialize MemberName 242 */ 243 static void init_MemberName(oop mname, methodHandle method) { 244 InstanceKlass* ik = method->method_holder(); 245 CallInfo info(method(), ik); 246 MethodHandles::init_method_MemberName(mname, info); 247 } 248 249 /* 250 * Fill StackFrameInfo with declaringClass and bci and initialize memberName 251 */ 252 void StackWalk::fill_frame_buffer(methodHandle method, int bci, Handle stackFrame) { 253 if (TraceStackWalk) { 254 tty->print(" member name: "); method->print_short_name(); 255 } 256 257 java_lang_StackFrameInfo::set_declaringClass(stackFrame(), method->method_holder()->java_mirror()); 258 if (MemberNameInStackFrame) { 259 // ## handle redefined Method* in MemberName; does it need version? 260 init_MemberName(java_lang_StackFrameInfo::member_name(stackFrame()), method); 261 java_lang_StackFrameInfo::set_bci(stackFrame(), bci); 262 if (TraceStackWalk) { 263 tty->print_cr(" bci=%d", bci); 264 } 265 } else { 266 int mid = method->orig_method_idnum(); 267 int version = method->constants()->version(); 268 int cpref = method->name_index(); 269 270 java_lang_StackFrameInfo::set_mid(stackFrame(), BackTrace::merge_mid_and_cpref(mid, cpref)); 271 java_lang_StackFrameInfo::set_bci(stackFrame(), BackTrace::merge_bci_and_version(bci, version)); 272 if (TraceStackWalk) { 273 tty->print_cr(" mid=%d, cpref=%d bci=%d, version=%d", 274 mid, cpref, bci, version); 275 } 276 } 277 } 278 279 /* 280 * Fill LiveStackFrameInfo with locals, monitors, and expressions 281 */ 282 283 void StackWalk::fill_live_stackframe(methodHandle method, int bci, Handle stackFrame, javaVFrame* jvf, TRAPS) { 284 java_lang_StackFrameInfo::set_declaringClass(stackFrame(), method->method_holder()->java_mirror()); 285 if (MemberNameInStackFrame) { 286 init_MemberName(java_lang_StackFrameInfo::member_name(stackFrame()), method); 287 java_lang_StackFrameInfo::set_bci(stackFrame(), bci); 288 } else { 289 int mid = method->orig_method_idnum(); 290 int version = method->constants()->version(); 291 int cpref = method->name_index(); 292 293 java_lang_StackFrameInfo::set_mid(stackFrame(), BackTrace::merge_mid_and_cpref(mid, cpref)); 294 java_lang_StackFrameInfo::set_bci(stackFrame(), BackTrace::merge_bci_and_version(bci, version)); 295 } 296 297 if (jvf != NULL) { 298 StackValueCollection* locals = jvf->locals(); 299 StackValueCollection* expressions = jvf->expressions(); 300 GrowableArray<MonitorInfo*>* monitors = jvf->monitors(); 301 302 if (!locals->is_empty()) { 303 objArrayHandle locals_h = values_to_object_array(locals, CHECK); 304 java_lang_LiveStackFrameInfo::set_locals(stackFrame(), locals_h()); 305 } 306 if (!expressions->is_empty()) { 307 objArrayHandle expressions_h = values_to_object_array(expressions, CHECK); 308 java_lang_LiveStackFrameInfo::set_operands(stackFrame(), expressions_h()); 309 } 310 if (monitors->length() > 0) { 311 objArrayHandle monitors_h = monitors_to_object_array(monitors, CHECK); 312 java_lang_LiveStackFrameInfo::set_monitors(stackFrame(), monitors_h()); 313 } 314 } 315 } 316 317 Klass* StackWalk::AbstractStackWalker_klass(TRAPS) { 318 if (_abstractStackWalker_klass == NULL) { 319 _abstractStackWalker_klass = SystemDictionary::resolve_or_null(vmSymbols::java_lang_StackStreamFactory_AbstractStackWalker(), CHECK_NULL); 320 } 321 return _abstractStackWalker_klass;; 322 } 323 324 Klass* StackWalk::StackWalker_klass(TRAPS) { 325 if (_stackWalker_klass == NULL) { 326 _stackWalker_klass = SystemDictionary::resolve_or_null(vmSymbols::java_lang_StackWalker(), CHECK_NULL); 327 } 328 return _stackWalker_klass; 329 } 330 331 /* 332 * Begins stack walking. 333 * 334 * @argument stackStream. StackStream object 335 * @argument mode. Stack walking mode. 336 * @argument skip_frames. Number of frames to be skipped. 337 * @argument frame_count. Number of frames to be traversed. 338 * @argument start_index. Start index to the user-supplied buffers. 339 * @argument classes_array. Buffer to store classes in, starting at start_index. 340 * @argument frames_array. Buffer to store StackFrame in, starting at start_index. 341 * NULL if not used. 342 * @return Object returned from AbstractStackWalker::doStackWalk call. 343 */ 344 Handle StackWalk::walk(Handle stackStream, jlong mode, 345 int skip_frames, int frame_count, int start_index, 346 objArrayHandle classes_array, 347 objArrayHandle frames_array, 348 TRAPS) 349 { 350 Handle empty; 351 352 JavaThread* jt = (JavaThread*)THREAD; 353 if (TraceStackWalk) { 354 tty->print_cr("Start walking: mode " JLONG_FORMAT " skip %d frames batch size %d", mode, skip_frames, frame_count); 355 } 356 357 if (need_method_info(mode)) { 358 if (frames_array.is_null()) { 359 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", empty); 360 } 361 } 362 363 Klass* stackWalker_klass = StackWalker_klass(CHECK_(empty)); 364 Klass* abstractStackWalker_klass = AbstractStackWalker_klass(CHECK_(empty)); 365 366 methodHandle m_doStackWalk; 367 { 368 Method* m = InstanceKlass::cast(abstractStackWalker_klass)->find_method(vmSymbols::doStackWalk_name(), 369 vmSymbols::doStackWalk_signature()); 370 if (m == NULL || m->is_static()) { 371 THROW_MSG_(vmSymbols::java_lang_InternalError(), "no AbstractStackStream::doStackWalk method", empty); 372 } 373 m_doStackWalk = methodHandle(THREAD, m); 374 } 375 376 if (live_frame_info(mode)) { 377 if (frames_array.is_null()) { 378 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array argument", empty); 379 } 380 java_lang_LiveStackFrameInfo::compute_offsets(); 381 } 382 383 // Open up a traversable stream onto my stack. 384 // This stream will be made available by *reference* to the inner Java call. 385 StackWalkAnchor anchor(jt, classes_array); 386 vframeStream& vfst = anchor.vframe_stream(); 387 388 { 389 // Skip all methods from AbstractStackWalker and StackWalk (enclosing method) 390 if (!fill_in_stacktrace(mode)) { 391 while (!vfst.at_end()) { 392 InstanceKlass* ik = vfst.method()->method_holder(); 393 if (ik != stackWalker_klass && ik != abstractStackWalker_klass && ik->super() != abstractStackWalker_klass) { 394 break; 395 } 396 397 if (TraceStackWalk) { 398 tty->print(" skip "); vfst.method()->print_short_name(); tty->print("\n"); 399 } 400 vfst.next(); 401 } 402 } 403 404 // For exceptions, skip Throwable::fillInStackTrace and <init> methods 405 // of the exception class and superclasses 406 if (fill_in_stacktrace(mode)) { 407 bool skip_to_fillInStackTrace = false; 408 bool skip_throwableInit_check = false; 409 while (!vfst.at_end() && !skip_throwableInit_check) { 410 InstanceKlass* ik = vfst.method()->method_holder(); 411 Method* method = vfst.method(); 412 if (!skip_to_fillInStackTrace) { 413 if (ik == SystemDictionary::Throwable_klass() && 414 method->name() == vmSymbols::fillInStackTrace_name()) { 415 // this frame will be skipped 416 skip_to_fillInStackTrace = true; 417 } 418 } else if (!(ik->is_subclass_of(SystemDictionary::Throwable_klass()) && 419 method->name() == vmSymbols::object_initializer_name())) { 420 // there are none or we've seen them all - either way stop checking 421 skip_throwableInit_check = true; 422 break; 423 } 424 425 if (TraceStackWalk) { 426 tty->print("stack walk: skip "); vfst.method()->print_short_name(); tty->print("\n"); 427 } 428 vfst.next(); 429 } 430 } 431 432 // stack frame has been traversed individually and resume stack walk 433 // from the stack frame at depth == skip_frames. 434 for (int n=0; n < skip_frames && !vfst.at_end(); vfst.next(), n++) { 435 if (TraceStackWalk) { 436 tty->print(" skip "); vfst.method()->print_short_name(); 437 tty->print_cr(" frame id: " PTR_FORMAT " pc: " PTR_FORMAT, 438 p2i(vfst.frame_id()), p2i(vfst.frame_pc())); 439 } 440 } 441 } 442 443 // The Method* pointer in the vfst has a very short shelf life. Grab it now. 444 int end_index = start_index; 445 int numFrames = 0; 446 if (!vfst.at_end()) { 447 numFrames = anchor.decode_frames(mode, frame_count, start_index, classes_array, 448 frames_array, end_index, CHECK_(empty)); 449 if (numFrames < 1) { 450 THROW_MSG_(vmSymbols::java_lang_InternalError(), "stack walk: decode failed", empty); 451 } 452 } 453 454 // call StackStream::doStackWalk to do bulk traversal on the entire stack 455 JavaValue result(T_OBJECT); 456 JavaCallArguments args(stackStream); 457 args.push_long(anchor.address_value()); 458 args.push_int(skip_frames); 459 args.push_int(frame_count); 460 args.push_int(start_index); 461 args.push_int(end_index); 462 463 // Link the thread and vframe stream into the callee-visible object: 464 anchor.setup_magic_on_entry(); 465 466 JavaCalls::call(&result, m_doStackWalk, &args, THREAD); 467 468 // Do this before anything else happens, to disable any lingering stream objects: 469 bool ok = anchor.cleanup_magic_on_exit(); 470 471 // Throw pendiing exception if we must: 472 (void) (CHECK_(empty)); 473 474 if (!ok) { 475 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers on exit", empty); 476 } 477 478 Handle obj(THREAD, (oop) result.get_jobject()); 479 // Return normally: 480 return obj; 481 482 } 483 484 /* 485 * Walk the next batch of stack frames 486 * 487 * @argument stackStream. StackStream object 488 * @argument mode. Stack walking mode. 489 * @argument magic. Must be valid value to continue the stack walk 490 * @argument frame_count. Number of frames to be decoded. 491 * @argument start_index. Start index to the user-supplied buffers. 492 * @argument classes_array. Buffer to store classes in, starting at start_index. 493 * @argument frames_array. Buffer to store StackFrame in, starting at start_index. 494 * NULL if not used. 495 * @return End index 496 */ 497 jint StackWalk::moreFrames(Handle stackStream, jlong mode, jlong magic, 498 int frame_count, int start_index, 499 objArrayHandle classes_array, 500 objArrayHandle frames_array, 501 TRAPS) 502 { 503 JavaThread* jt = (JavaThread*)THREAD; 504 StackWalkAnchor* existing_anchor = StackWalkAnchor::from_current(jt, magic, classes_array); 505 if (existing_anchor == NULL) { 506 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers", 0L); 507 } 508 509 if ((need_method_info(mode) || live_frame_info(mode)) && frames_array.is_null()) { 510 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", 0L); 511 } 512 513 if (TraceStackWalk) { 514 tty->print_cr("StackWalk::moreFrames frame_count %d existing_anchor " PTR_FORMAT " start %d frames %d", 515 frame_count, p2i(existing_anchor), start_index, classes_array->length()); 516 } 517 int end_index = start_index; 518 if (frame_count <= 0) { 519 return end_index; // No operation. 520 } 521 522 int count = frame_count+start_index; 523 assert (classes_array->length() >= count, "not enough space in buffers"); 524 525 StackWalkAnchor& anchor = (*existing_anchor); 526 vframeStream& vfst = anchor.vframe_stream(); 527 if (!vfst.at_end()) { 528 vfst.next(); // skip java.lang.StackStream::moreStackWalk 529 if (!vfst.at_end()) { 530 int n = anchor.decode_frames(mode, frame_count, start_index, classes_array, 531 frames_array, end_index, CHECK_0); 532 if (n < 1) { 533 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: later decode failed", 0L); 534 } 535 return end_index; 536 } 537 } 538 return end_index; 539 }