1 /* 2 * Copyright (c) 2015, 2016, 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 JavaFrameStream::setup_magic_on_entry(objArrayHandle frames_array) { 41 frames_array->obj_at_put(magic_pos, _thread->threadObj()); 42 _anchor = address_value(); 43 assert(check_magic(frames_array), "invalid magic"); 44 } 45 46 bool JavaFrameStream::check_magic(objArrayHandle frames_array) { 47 oop m1 = frames_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 JavaFrameStream::cleanup_magic_on_exit(objArrayHandle frames_array) { 54 bool ok = check_magic(frames_array); 55 frames_array->obj_at_put(magic_pos, NULL); 56 _anchor = 0L; 57 return ok; 58 } 59 60 // Returns JavaFrameStream for the current stack being traversed. 61 // 62 // Parameters: 63 // thread Current Java thread. 64 // magic Magic value used for each stack walking 65 // frames_array User-supplied buffers. The 0th element is reserved 66 // to this JavaFrameStream to use 67 // 68 JavaFrameStream* JavaFrameStream::from_current(JavaThread* thread, jlong magic, 69 objArrayHandle frames_array) 70 { 71 assert(thread != NULL && thread->is_Java_thread(), ""); 72 oop m1 = frames_array->obj_at(magic_pos); 73 if (m1 != thread->threadObj()) return NULL; 74 if (magic == 0L) return NULL; 75 JavaFrameStream* stream = (JavaFrameStream*) (intptr_t) magic; 76 if (!stream->is_valid_in(thread, frames_array)) return NULL; 77 return stream; 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 // JavaFrameStream stream of javaVFrames 89 // max_nframes Maximum number of frames to be filled. 90 // start_index Start index to the user-supplied buffers. 91 // frames_array Buffer to store Class or StackFrame in, starting at start_index. 92 // frames array is a Class<?>[] array when only getting caller 93 // reference, and a StackFrameInfo[] array (or derivative) 94 // otherwise. It should never be null. 95 // end_index End index to the user-supplied buffers with unpacked frames. 96 // 97 // Returns the number of frames whose information was transferred into the buffers. 98 // 99 int StackWalk::fill_in_frames(jlong mode, JavaFrameStream& stream, 100 int max_nframes, int start_index, 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, frames_array->length()); 106 } 107 assert(max_nframes > 0, "invalid max_nframes"); 108 assert(start_index + max_nframes <= frames_array->length(), "oob"); 109 110 int frames_decoded = 0; 111 for (; !stream.at_end(); stream.next()) { 112 Method* method = stream.method(); 113 int bci = stream.bci(); 114 115 if (method == NULL) continue; 116 117 // skip hidden frames for default StackWalker option (i.e. SHOW_HIDDEN_FRAMES 118 // not set) and when StackWalker::getCallerClass is called 119 if (!ShowHiddenFrames && (skip_hidden_frames(mode) || get_caller_class(mode))) { 120 if (method->is_hidden()) { 121 if (TraceStackWalk) { 122 tty->print(" hidden method: "); method->print_short_name(); 123 tty->print("\n"); 124 } 125 continue; 126 } 127 } 128 129 int index = end_index++; 130 if (TraceStackWalk) { 131 tty->print(" %d: frame method: ", index); method->print_short_name(); 132 tty->print_cr(" bci=%d", bci); 133 } 134 135 // fill in StackFrameInfo and initialize MemberName 136 if (live_frame_info(mode)) { 137 assert (use_frames_array(mode), "Bad mode for get live frame"); 138 Handle stackFrame(frames_array->obj_at(index)); 139 fill_live_stackframe(stackFrame, method, bci, stream.java_frame(), CHECK_0); 140 } else if (need_method_info(mode)) { 141 assert (use_frames_array(mode), "Bad mode for get stack frame"); 142 Handle stackFrame(frames_array->obj_at(index)); 143 fill_stackframe(stackFrame, method, bci); 144 } else { 145 assert (use_frames_array(mode) == false, "Bad mode for filling in Class object"); 146 if (get_caller_class(mode) && index == start_index && method->caller_sensitive()) { 147 ResourceMark rm(THREAD); 148 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), 149 err_msg("StackWalker::getCallerClass called from @CallerSensitive %s method", 150 method->name_and_sig_as_C_string())); 151 } 152 153 frames_array->obj_at_put(index, method->method_holder()->java_mirror()); 154 } 155 if (++frames_decoded >= max_nframes) break; 156 } 157 return frames_decoded; 158 } 159 160 static oop create_primitive_value_instance(StackValueCollection* values, int i, TRAPS) { 161 Klass* k = SystemDictionary::resolve_or_null(vmSymbols::java_lang_LiveStackFrameInfo(), CHECK_NULL); 162 instanceKlassHandle ik (THREAD, k); 163 164 JavaValue result(T_OBJECT); 165 JavaCallArguments args; 166 Symbol* signature = NULL; 167 168 // ## TODO: type is only available in LocalVariable table, if present. 169 // ## StackValue type is T_INT or T_OBJECT. 170 switch (values->at(i)->type()) { 171 case T_INT: 172 args.push_int(values->int_at(i)); 173 signature = vmSymbols::asPrimitive_int_signature(); 174 break; 175 176 case T_LONG: 177 args.push_long(values->long_at(i)); 178 signature = vmSymbols::asPrimitive_long_signature(); 179 break; 180 181 case T_FLOAT: 182 args.push_float(values->float_at(i)); 183 signature = vmSymbols::asPrimitive_float_signature(); 184 break; 185 186 case T_DOUBLE: 187 args.push_double(values->double_at(i)); 188 signature = vmSymbols::asPrimitive_double_signature(); 189 break; 190 191 case T_BYTE: 192 args.push_int(values->int_at(i)); 193 signature = vmSymbols::asPrimitive_byte_signature(); 194 break; 195 196 case T_SHORT: 197 args.push_int(values->int_at(i)); 198 signature = vmSymbols::asPrimitive_short_signature(); 199 break; 200 201 case T_CHAR: 202 args.push_int(values->int_at(i)); 203 signature = vmSymbols::asPrimitive_char_signature(); 204 break; 205 206 case T_BOOLEAN: 207 args.push_int(values->int_at(i)); 208 signature = vmSymbols::asPrimitive_boolean_signature(); 209 break; 210 211 case T_OBJECT: 212 return values->obj_at(i)(); 213 214 case T_CONFLICT: 215 // put a non-null slot 216 args.push_int(0); 217 signature = vmSymbols::asPrimitive_int_signature(); 218 break; 219 220 default: ShouldNotReachHere(); 221 } 222 JavaCalls::call_static(&result, 223 ik, 224 vmSymbols::asPrimitive_name(), 225 signature, 226 &args, 227 CHECK_NULL); 228 return (instanceOop) result.get_jobject(); 229 } 230 231 static objArrayHandle values_to_object_array(StackValueCollection* values, TRAPS) { 232 objArrayHandle empty; 233 int length = values->size(); 234 objArrayOop array_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 235 length, CHECK_(empty)); 236 objArrayHandle array_h(THREAD, array_oop); 237 for (int i = 0; i < values->size(); i++) { 238 StackValue* st = values->at(i); 239 oop obj = create_primitive_value_instance(values, i, CHECK_(empty)); 240 if (obj != NULL) 241 array_h->obj_at_put(i, obj); 242 } 243 return array_h; 244 } 245 246 static objArrayHandle monitors_to_object_array(GrowableArray<MonitorInfo*>* monitors, TRAPS) { 247 int length = monitors->length(); 248 objArrayOop array_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 249 length, CHECK_(objArrayHandle())); 250 objArrayHandle array_h(THREAD, array_oop); 251 for (int i = 0; i < length; i++) { 252 MonitorInfo* monitor = monitors->at(i); 253 array_h->obj_at_put(i, monitor->owner()); 254 } 255 return array_h; 256 } 257 258 // Fill StackFrameInfo with declaringClass and bci and initialize memberName 259 void StackWalk::fill_stackframe(Handle stackFrame, const methodHandle& method, int bci) { 260 java_lang_StackFrameInfo::set_declaringClass(stackFrame(), method->method_holder()->java_mirror()); 261 java_lang_StackFrameInfo::set_method_and_bci(stackFrame(), method, bci); 262 } 263 264 // Fill LiveStackFrameInfo with locals, monitors, and expressions 265 void StackWalk::fill_live_stackframe(Handle stackFrame, const methodHandle& method, 266 int bci, javaVFrame* jvf, TRAPS) { 267 fill_stackframe(stackFrame, method, bci); 268 if (jvf != NULL) { 269 StackValueCollection* locals = jvf->locals(); 270 StackValueCollection* expressions = jvf->expressions(); 271 GrowableArray<MonitorInfo*>* monitors = jvf->monitors(); 272 273 if (!locals->is_empty()) { 274 objArrayHandle locals_h = values_to_object_array(locals, CHECK); 275 java_lang_LiveStackFrameInfo::set_locals(stackFrame(), locals_h()); 276 } 277 if (!expressions->is_empty()) { 278 objArrayHandle expressions_h = values_to_object_array(expressions, CHECK); 279 java_lang_LiveStackFrameInfo::set_operands(stackFrame(), expressions_h()); 280 } 281 if (monitors->length() > 0) { 282 objArrayHandle monitors_h = monitors_to_object_array(monitors, CHECK); 283 java_lang_LiveStackFrameInfo::set_monitors(stackFrame(), monitors_h()); 284 } 285 } 286 } 287 288 // Begins stack walking. 289 // 290 // Parameters: 291 // stackStream StackStream object 292 // mode Stack walking mode. 293 // skip_frames Number of frames to be skipped. 294 // frame_count Number of frames to be traversed. 295 // start_index Start index to the user-supplied buffers. 296 // frames_array Buffer to store StackFrame in, starting at start_index. 297 // frames array is a Class<?>[] array when only getting caller 298 // reference, and a StackFrameInfo[] array (or derivative) 299 // otherwise. It should never be null. 300 // 301 // Returns Object returned from AbstractStackWalker::doStackWalk call. 302 // 303 oop StackWalk::walk(Handle stackStream, jlong mode, 304 int skip_frames, int frame_count, int start_index, 305 objArrayHandle frames_array, 306 TRAPS) { 307 ResourceMark rm(THREAD); 308 JavaThread* jt = (JavaThread*)THREAD; 309 if (TraceStackWalk) { 310 tty->print_cr("Start walking: mode " JLONG_FORMAT " skip %d frames batch size %d", 311 mode, skip_frames, frame_count); 312 } 313 314 if (frames_array.is_null()) { 315 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", NULL); 316 } 317 318 Klass* stackWalker_klass = SystemDictionary::StackWalker_klass(); 319 Klass* abstractStackWalker_klass = SystemDictionary::AbstractStackWalker_klass(); 320 321 methodHandle m_doStackWalk(THREAD, Universe::do_stack_walk_method()); 322 323 // Setup traversal onto my stack. 324 RegisterMap regMap(jt, true); 325 JavaFrameStream stream(jt, ®Map); 326 { 327 while (!stream.at_end()) { 328 InstanceKlass* ik = stream.method()->method_holder(); 329 if (ik != stackWalker_klass && 330 ik != abstractStackWalker_klass && ik->super() != abstractStackWalker_klass) { 331 break; 332 } 333 334 if (TraceStackWalk) { 335 tty->print(" skip "); stream.method()->print_short_name(); tty->print("\n"); 336 } 337 stream.next(); 338 } 339 340 // stack frame has been traversed individually and resume stack walk 341 // from the stack frame at depth == skip_frames. 342 for (int n=0; n < skip_frames && !stream.at_end(); stream.next(), n++) { 343 if (TraceStackWalk) { 344 tty->print(" skip "); stream.method()->print_short_name(); 345 tty->print_cr(" frame id: " PTR_FORMAT " pc: " PTR_FORMAT, 346 p2i(stream.java_frame()->fr().id()), 347 p2i(stream.java_frame()->fr().pc())); 348 } 349 } 350 } 351 352 int end_index = start_index; 353 int numFrames = 0; 354 if (!stream.at_end()) { 355 numFrames = fill_in_frames(mode, stream, frame_count, start_index, 356 frames_array, end_index, CHECK_NULL); 357 if (numFrames < 1) { 358 THROW_MSG_(vmSymbols::java_lang_InternalError(), "stack walk: decode failed", NULL); 359 } 360 } 361 362 // JVM_CallStackWalk walks the stack and fills in stack frames, then calls to 363 // Java method java.lang.StackStreamFactory.AbstractStackWalker::doStackWalk 364 // which calls the implementation to consume the stack frames. 365 // When JVM_CallStackWalk returns, it invalidates the stack stream. 366 JavaValue result(T_OBJECT); 367 JavaCallArguments args(stackStream); 368 args.push_long(stream.address_value()); 369 args.push_int(skip_frames); 370 args.push_int(frame_count); 371 args.push_int(start_index); 372 args.push_int(end_index); 373 374 // Link the thread and vframe stream into the callee-visible object 375 stream.setup_magic_on_entry(frames_array); 376 377 JavaCalls::call(&result, m_doStackWalk, &args, THREAD); 378 379 // Do this before anything else happens, to disable any lingering stream objects 380 bool ok = stream.cleanup_magic_on_exit(frames_array); 381 382 // Throw pending exception if we must 383 (void) (CHECK_NULL); 384 385 if (!ok) { 386 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers on exit", NULL); 387 } 388 389 // Return normally 390 return (oop)result.get_jobject(); 391 } 392 393 // Walk the next batch of stack frames 394 // 395 // Parameters: 396 // stackStream StackStream object 397 // mode Stack walking mode. 398 // magic Must be valid value to continue the stack walk 399 // frame_count Number of frames to be decoded. 400 // start_index Start index to the user-supplied buffers. 401 // frames_array Buffer to store StackFrame in, starting at start_index. 402 // 403 // Returns the end index of frame filled in the buffer. 404 // 405 jint StackWalk::moreFrames(Handle stackStream, jlong mode, jlong magic, 406 int frame_count, int start_index, 407 objArrayHandle frames_array, 408 TRAPS) 409 { 410 JavaThread* jt = (JavaThread*)THREAD; 411 JavaFrameStream* existing_stream = JavaFrameStream::from_current(jt, magic, frames_array); 412 if (existing_stream == NULL) { 413 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers", 0L); 414 } 415 416 if (frames_array.is_null()) { 417 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", 0L); 418 } 419 420 if (TraceStackWalk) { 421 tty->print_cr("StackWalk::moreFrames frame_count %d existing_stream " PTR_FORMAT " start %d frames %d", 422 frame_count, p2i(existing_stream), start_index, frames_array->length()); 423 } 424 int end_index = start_index; 425 if (frame_count <= 0) { 426 return end_index; // No operation. 427 } 428 429 int count = frame_count + start_index; 430 assert (frames_array->length() >= count, "not enough space in buffers"); 431 432 JavaFrameStream& stream = (*existing_stream); 433 if (!stream.at_end()) { 434 stream.next(); // advance past the last frame decoded in previous batch 435 if (!stream.at_end()) { 436 int n = fill_in_frames(mode, stream, frame_count, start_index, 437 frames_array, end_index, CHECK_0); 438 if (n < 1) { 439 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: later decode failed", 0L); 440 } 441 return end_index; 442 } 443 } 444 return end_index; 445 }