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 // setup and cleanup actions 40 void StackWalkAnchor::setup_magic_on_entry() { 41 _frames->obj_at_put(StackWalk::magic_pos, _thread->threadObj()); 42 _anchor = address_value(); 43 assert(check_magic(), "invalid magic"); 44 } 45 46 bool StackWalkAnchor::check_magic() { 47 oop m1 = _frames->obj_at(StackWalk::magic_pos); 48 jlong m2 = _anchor; 49 if (m1 == _thread->threadObj() && m2 == address_value()) return true; 50 return false; 51 } 52 53 bool StackWalkAnchor::cleanup_magic_on_exit() { 54 bool ok = check_magic(); 55 _frames->obj_at_put(StackWalk::magic_pos, NULL); 56 _anchor = 0L; 57 return ok; 58 } 59 60 /** 61 * Returns StackWalkAnchor for the current stack being traversed. 62 * 63 * @argument thread. Current Java thread. 64 * @argument magic. Magic value used for each stack walking 65 * @argument classes_array User-supplied buffers. The 0th element is reserved 66 * to this StackWalkAnchor to use 67 */ 68 StackWalkAnchor* StackWalkAnchor::from_current(JavaThread* thread, jlong magic, 69 objArrayHandle classes_array) 70 { 71 assert(thread != NULL && thread->is_Java_thread(), ""); 72 oop m1 = classes_array->obj_at(StackWalk::magic_pos); 73 if (m1 != thread->threadObj()) return NULL; 74 if (magic == 0L) return NULL; 75 StackWalkAnchor* anchor = (StackWalkAnchor*) (intptr_t) magic; 76 if (!anchor->is_valid_in(thread)) return NULL; 77 return anchor; 78 } 79 80 /* 81 * Unpacks one or more frames into user-supplied buffers. 82 * Updates the end index, and returns the number of unpacked frames. 83 * Always start with the existing vfst.method and bci. 84 * Do not call vfst.next to advance over the last returned value. 85 * In other words, do not leave any stale data in the vfst. 86 * 87 * @argument mode. Restrict which frames to be decoded. 88 * @argument decode_limit. Maximum of frames to be decoded. 89 * @argument start_index. Start index to the user-supplied buffers. 90 * @argument classes_array. Buffer to store classes in, starting at start_index. 91 * @argument frames_array. Buffer to store StackFrame in, starting at start_index. 92 * NULL if not used. 93 * @argument end_index. End index to the user-supplied buffers 94 * with unpacked frames. 95 * 96 * @return Number of frames whose information was transferred into the buffers. 97 */ 98 int StackWalkAnchor::decode_frames(jlong mode, int decode_limit, int start_index, 99 objArrayHandle classes_array, 100 objArrayHandle frames_array, 101 int& end_index, TRAPS) { 102 if (TraceStackWalk) { 103 tty->print_cr("decode_frames limit=%d start=%d frames length=%d", 104 decode_limit, start_index, classes_array->length()); 105 } 106 assert(decode_limit > 0, "invalid decode_limit"); 107 assert(start_index + decode_limit <= classes_array->length(), "oob"); 108 109 int frames_decoded = 0; 110 for (; !_vfst.at_end(); _vfst.next()) { 111 Method* method = _vfst.method(); 112 int bci = _vfst.bci(); 113 114 if (method == NULL) continue; 115 if (!ShowHiddenFrames && StackWalk::skip_hidden_frames(mode)) { 116 if (method->is_hidden()) { 117 if (TraceStackWalk) { 118 tty->print(" hidden method: "); method->print_short_name(); 119 tty->print("\n"); 120 } 121 continue; 122 } 123 } 124 125 int index = end_index++; 126 if (TraceStackWalk) { 127 tty->print(" %d: frame method: ", index); method->print_short_name(); 128 tty->print_cr(" bci=%d", bci); 129 } 130 131 classes_array->obj_at_put(index, method->method_holder()->java_mirror()); 132 // fill in StackFrameInfo and initialize MemberName 133 if (StackWalk::live_frame_info(mode)) { 134 Handle stackFrame(frames_array->obj_at(index)); 135 StackWalk::fill_live_stackframe(stackFrame, method, bci, _vfst.java_frame(), CHECK_0); 136 } else if (StackWalk::need_method_info(mode)) { 137 Handle stackFrame(frames_array->obj_at(index)); 138 StackWalk::fill_stackframe(stackFrame, method, bci); 139 } 140 if (++frames_decoded >= decode_limit) break; 141 } 142 return frames_decoded; 143 } 144 145 static oop create_primitive_value_instance(StackValueCollection* values, int i, TRAPS) { 146 Klass* k = SystemDictionary::resolve_or_null(vmSymbols::java_lang_LiveStackFrameInfo(), CHECK_NULL); 147 instanceKlassHandle ik (THREAD, k); 148 149 JavaValue result(T_OBJECT); 150 JavaCallArguments args; 151 Symbol* signature = NULL; 152 153 // ## TODO: type is only available in LocalVariable table, if present. 154 // ## StackValue type is T_INT or T_OBJECT. 155 switch (values->at(i)->type()) { 156 case T_INT: 157 args.push_int(values->int_at(i)); 158 signature = vmSymbols::asPrimitive_int_signature(); 159 break; 160 161 case T_LONG: 162 args.push_long(values->long_at(i)); 163 signature = vmSymbols::asPrimitive_long_signature(); 164 break; 165 166 case T_FLOAT: 167 args.push_float(values->float_at(i)); 168 signature = vmSymbols::asPrimitive_float_signature(); 169 break; 170 171 case T_DOUBLE: 172 args.push_double(values->double_at(i)); 173 signature = vmSymbols::asPrimitive_double_signature(); 174 break; 175 176 case T_BYTE: 177 args.push_int(values->int_at(i)); 178 signature = vmSymbols::asPrimitive_byte_signature(); 179 break; 180 181 case T_SHORT: 182 args.push_int(values->int_at(i)); 183 signature = vmSymbols::asPrimitive_short_signature(); 184 break; 185 186 case T_CHAR: 187 args.push_int(values->int_at(i)); 188 signature = vmSymbols::asPrimitive_char_signature(); 189 break; 190 191 case T_BOOLEAN: 192 args.push_int(values->int_at(i)); 193 signature = vmSymbols::asPrimitive_boolean_signature(); 194 break; 195 196 case T_OBJECT: 197 return values->obj_at(i)(); 198 199 case T_CONFLICT: 200 // put a non-null slot 201 args.push_int(0); 202 signature = vmSymbols::asPrimitive_int_signature(); 203 break; 204 205 default: ShouldNotReachHere(); 206 } 207 JavaCalls::call_static(&result, 208 ik, 209 vmSymbols::asPrimitive_name(), 210 signature, 211 &args, 212 CHECK_NULL); 213 return (instanceOop) result.get_jobject(); 214 } 215 216 static objArrayHandle values_to_object_array(StackValueCollection* values, TRAPS) { 217 objArrayHandle empty; 218 int length = values->size(); 219 objArrayOop array_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 220 length, CHECK_(empty)); 221 objArrayHandle array_h(THREAD, array_oop); 222 for (int i = 0; i < values->size(); i++) { 223 StackValue* st = values->at(i); 224 oop obj = create_primitive_value_instance(values, i, CHECK_(empty)); 225 if (obj != NULL) 226 array_h->obj_at_put(i, obj); 227 } 228 return array_h; 229 } 230 231 static objArrayHandle monitors_to_object_array(GrowableArray<MonitorInfo*>* monitors, TRAPS) { 232 int length = monitors->length(); 233 objArrayOop array_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 234 length, CHECK_(objArrayHandle())); 235 objArrayHandle array_h(THREAD, array_oop); 236 for (int i = 0; i < length; i++) { 237 MonitorInfo* monitor = monitors->at(i); 238 array_h->obj_at_put(i, monitor->owner()); 239 } 240 return array_h; 241 } 242 243 /* 244 * Fill StackFrameInfo with declaringClass and bci and initialize memberName 245 */ 246 void StackWalk::fill_stackframe(Handle stackFrame, const methodHandle& method, int bci) { 247 java_lang_StackFrameInfo::set_declaringClass(stackFrame(), method->method_holder()->java_mirror()); 248 java_lang_StackFrameInfo::set_method_and_bci(stackFrame(), method, bci); 249 } 250 251 /* 252 * Fill LiveStackFrameInfo with locals, monitors, and expressions 253 */ 254 255 void StackWalk::fill_live_stackframe(Handle stackFrame, const methodHandle& method, int bci, javaVFrame* jvf, TRAPS) { 256 fill_stackframe(stackFrame, method, bci); 257 258 if (jvf != NULL) { 259 StackValueCollection* locals = jvf->locals(); 260 StackValueCollection* expressions = jvf->expressions(); 261 GrowableArray<MonitorInfo*>* monitors = jvf->monitors(); 262 263 if (!locals->is_empty()) { 264 objArrayHandle locals_h = values_to_object_array(locals, CHECK); 265 java_lang_LiveStackFrameInfo::set_locals(stackFrame(), locals_h()); 266 } 267 if (!expressions->is_empty()) { 268 objArrayHandle expressions_h = values_to_object_array(expressions, CHECK); 269 java_lang_LiveStackFrameInfo::set_operands(stackFrame(), expressions_h()); 270 } 271 if (monitors->length() > 0) { 272 objArrayHandle monitors_h = monitors_to_object_array(monitors, CHECK); 273 java_lang_LiveStackFrameInfo::set_monitors(stackFrame(), monitors_h()); 274 } 275 } 276 } 277 278 /* 279 * Begins stack walking. 280 * 281 * @argument stackStream. StackStream object 282 * @argument mode. Stack walking mode. 283 * @argument skip_frames. Number of frames to be skipped. 284 * @argument frame_count. Number of frames to be traversed. 285 * @argument start_index. Start index to the user-supplied buffers. 286 * @argument classes_array. Buffer to store classes in, starting at start_index. 287 * @argument frames_array. Buffer to store StackFrame in, starting at start_index. 288 * NULL if not used. 289 * @return Object returned from AbstractStackWalker::doStackWalk call. 290 */ 291 Handle StackWalk::walk(Handle stackStream, jlong mode, 292 int skip_frames, int frame_count, int start_index, 293 objArrayHandle classes_array, 294 objArrayHandle frames_array, 295 TRAPS) 296 { 297 Handle empty; 298 299 JavaThread* jt = (JavaThread*)THREAD; 300 if (TraceStackWalk) { 301 tty->print_cr("Start walking: mode " JLONG_FORMAT " skip %d frames batch size %d", mode, skip_frames, frame_count); 302 } 303 304 if (need_method_info(mode)) { 305 if (frames_array.is_null()) { 306 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", empty); 307 } 308 } 309 310 Klass* stackWalker_klass = SystemDictionary::StackWalker_klass(); 311 Klass* abstractStackWalker_klass = SystemDictionary::AbstractStackWalker_klass(); 312 313 methodHandle m_doStackWalk(THREAD, Universe::stack_walker_callback_method()); 314 315 // Open up a traversable stream onto my stack. 316 // This stream will be made available by *reference* to the inner Java call. 317 StackWalkAnchor anchor(jt, classes_array); 318 vframeStream& vfst = anchor.vframe_stream(); 319 320 { 321 // Skip all methods from AbstractStackWalker and StackWalk (enclosing method) 322 if (!fill_in_stacktrace(mode)) { 323 while (!vfst.at_end()) { 324 InstanceKlass* ik = vfst.method()->method_holder(); 325 if (ik != stackWalker_klass && ik != abstractStackWalker_klass && ik->super() != abstractStackWalker_klass) { 326 break; 327 } 328 329 if (TraceStackWalk) { 330 tty->print(" skip "); vfst.method()->print_short_name(); tty->print("\n"); 331 } 332 vfst.next(); 333 } 334 } 335 336 // For exceptions, skip Throwable::fillInStackTrace and <init> methods 337 // of the exception class and superclasses 338 if (fill_in_stacktrace(mode)) { 339 bool skip_to_fillInStackTrace = false; 340 bool skip_throwableInit_check = false; 341 while (!vfst.at_end() && !skip_throwableInit_check) { 342 InstanceKlass* ik = vfst.method()->method_holder(); 343 Method* method = vfst.method(); 344 if (!skip_to_fillInStackTrace) { 345 if (ik == SystemDictionary::Throwable_klass() && 346 method->name() == vmSymbols::fillInStackTrace_name()) { 347 // this frame will be skipped 348 skip_to_fillInStackTrace = true; 349 } 350 } else if (!(ik->is_subclass_of(SystemDictionary::Throwable_klass()) && 351 method->name() == vmSymbols::object_initializer_name())) { 352 // there are none or we've seen them all - either way stop checking 353 skip_throwableInit_check = true; 354 break; 355 } 356 357 if (TraceStackWalk) { 358 tty->print("stack walk: skip "); vfst.method()->print_short_name(); tty->print("\n"); 359 } 360 vfst.next(); 361 } 362 } 363 364 // stack frame has been traversed individually and resume stack walk 365 // from the stack frame at depth == skip_frames. 366 for (int n=0; n < skip_frames && !vfst.at_end(); vfst.next(), n++) { 367 if (TraceStackWalk) { 368 tty->print(" skip "); vfst.method()->print_short_name(); 369 tty->print_cr(" frame id: " PTR_FORMAT " pc: " PTR_FORMAT, 370 p2i(vfst.frame_id()), p2i(vfst.frame_pc())); 371 } 372 } 373 } 374 375 // The Method* pointer in the vfst has a very short shelf life. Grab it now. 376 int end_index = start_index; 377 int numFrames = 0; 378 if (!vfst.at_end()) { 379 numFrames = anchor.decode_frames(mode, frame_count, start_index, classes_array, 380 frames_array, end_index, CHECK_(empty)); 381 if (numFrames < 1) { 382 THROW_MSG_(vmSymbols::java_lang_InternalError(), "stack walk: decode failed", empty); 383 } 384 } 385 386 // call StackStream::doStackWalk to do bulk traversal on the entire stack 387 JavaValue result(T_OBJECT); 388 JavaCallArguments args(stackStream); 389 args.push_long(anchor.address_value()); 390 args.push_int(skip_frames); 391 args.push_int(frame_count); 392 args.push_int(start_index); 393 args.push_int(end_index); 394 395 // Link the thread and vframe stream into the callee-visible object: 396 anchor.setup_magic_on_entry(); 397 398 JavaCalls::call(&result, m_doStackWalk, &args, THREAD); 399 400 // Do this before anything else happens, to disable any lingering stream objects: 401 bool ok = anchor.cleanup_magic_on_exit(); 402 403 // Throw pending exception if we must: 404 (void) (CHECK_(empty)); 405 406 if (!ok) { 407 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers on exit", empty); 408 } 409 410 Handle obj(THREAD, (oop) result.get_jobject()); 411 // Return normally: 412 return obj; 413 414 } 415 416 /* 417 * Walk the next batch of stack frames 418 * 419 * @argument stackStream. StackStream object 420 * @argument mode. Stack walking mode. 421 * @argument magic. Must be valid value to continue the stack walk 422 * @argument frame_count. Number of frames to be decoded. 423 * @argument start_index. Start index to the user-supplied buffers. 424 * @argument classes_array. Buffer to store classes in, starting at start_index. 425 * @argument frames_array. Buffer to store StackFrame in, starting at start_index. 426 * NULL if not used. 427 * @return End index 428 */ 429 jint StackWalk::moreFrames(Handle stackStream, jlong mode, jlong magic, 430 int frame_count, int start_index, 431 objArrayHandle classes_array, 432 objArrayHandle frames_array, 433 TRAPS) 434 { 435 JavaThread* jt = (JavaThread*)THREAD; 436 StackWalkAnchor* existing_anchor = StackWalkAnchor::from_current(jt, magic, classes_array); 437 if (existing_anchor == NULL) { 438 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers", 0L); 439 } 440 441 if ((need_method_info(mode) || live_frame_info(mode)) && frames_array.is_null()) { 442 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", 0L); 443 } 444 445 if (TraceStackWalk) { 446 tty->print_cr("StackWalk::moreFrames frame_count %d existing_anchor " PTR_FORMAT " start %d frames %d", 447 frame_count, p2i(existing_anchor), start_index, classes_array->length()); 448 } 449 int end_index = start_index; 450 if (frame_count <= 0) { 451 return end_index; // No operation. 452 } 453 454 int count = frame_count+start_index; 455 assert (classes_array->length() >= count, "not enough space in buffers"); 456 457 StackWalkAnchor& anchor = (*existing_anchor); 458 vframeStream& vfst = anchor.vframe_stream(); 459 if (!vfst.at_end()) { 460 vfst.next(); // this was the last frame decoded in the previous batch 461 if (!vfst.at_end()) { 462 int n = anchor.decode_frames(mode, frame_count, start_index, classes_array, 463 frames_array, end_index, CHECK_0); 464 if (n < 1) { 465 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: later decode failed", 0L); 466 } 467 return end_index; 468 } 469 } 470 return end_index; 471 }