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