< prev index next >

src/hotspot/share/prims/stackwalk.cpp

Print this page

 29 #include "classfile/vmSymbols.hpp"
 30 #include "logging/log.hpp"
 31 #include "logging/logStream.hpp"
 32 #include "memory/oopFactory.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/klass.inline.hpp"
 35 #include "oops/oop.inline.hpp"
 36 #include "oops/objArrayOop.inline.hpp"
 37 #include "prims/stackwalk.hpp"
 38 #include "runtime/globals.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/javaCalls.hpp"
 41 #include "runtime/keepStackGCProcessed.hpp"
 42 #include "runtime/stackWatermarkSet.hpp"
 43 #include "runtime/thread.inline.hpp"
 44 #include "runtime/vframe.inline.hpp"
 45 #include "utilities/formatBuffer.hpp"
 46 #include "utilities/globalDefinitions.hpp"
 47 
 48 // setup and cleanup actions





 49 void BaseFrameStream::setup_magic_on_entry(objArrayHandle frames_array) {
 50   frames_array->obj_at_put(magic_pos, _thread->threadObj());
 51   _anchor = address_value();
 52   assert(check_magic(frames_array), "invalid magic");
 53 }
 54 
 55 bool BaseFrameStream::check_magic(objArrayHandle frames_array) {
 56   oop   m1 = frames_array->obj_at(magic_pos);
 57   jlong m2 = _anchor;
 58   if (m1 == _thread->threadObj() && m2 == address_value())  return true;
 59   return false;
 60 }
 61 
 62 bool BaseFrameStream::cleanup_magic_on_exit(objArrayHandle frames_array) {
 63   bool ok = check_magic(frames_array);
 64   frames_array->obj_at_put(magic_pos, NULL);
 65   _anchor = 0L;
 66   return ok;
 67 }
 68 
 69 JavaFrameStream::JavaFrameStream(JavaThread* thread, int mode)
 70   : BaseFrameStream(thread), _vfst(thread) {

























 71   _need_method_info = StackWalk::need_method_info(mode);
 72 }
 73 
 74 void JavaFrameStream::next() { _vfst.next();}


































 75 
 76 // Returns the BaseFrameStream for the current stack being traversed.
 77 //
 78 // Parameters:
 79 //  thread         Current Java thread.
 80 //  magic          Magic value used for each stack walking
 81 //  frames_array   User-supplied buffers.  The 0th element is reserved
 82 //                 for this BaseFrameStream to use
 83 //
 84 BaseFrameStream* BaseFrameStream::from_current(JavaThread* thread, jlong magic,
 85                                                objArrayHandle frames_array)
 86 {
 87   oop m1 = frames_array->obj_at(magic_pos);
 88   if (m1 != thread->threadObj()) return NULL;
 89   if (magic == 0L)                    return NULL;
 90   BaseFrameStream* stream = (BaseFrameStream*) (intptr_t) magic;
 91   if (!stream->is_valid_in(thread, frames_array))   return NULL;
 92   return stream;
 93 }
 94 

105 //   start_index      Start index to the user-supplied buffers.
106 //   frames_array     Buffer to store Class or StackFrame in, starting at start_index.
107 //                    frames array is a Class<?>[] array when only getting caller
108 //                    reference, and a StackFrameInfo[] array (or derivative)
109 //                    otherwise. It should never be null.
110 //   end_index        End index to the user-supplied buffers with unpacked frames.
111 //
112 // Returns the number of frames whose information was transferred into the buffers.
113 //
114 int StackWalk::fill_in_frames(jlong mode, BaseFrameStream& stream,
115                               int max_nframes, int start_index,
116                               objArrayHandle  frames_array,
117                               int& end_index, TRAPS) {
118   log_debug(stackwalk)("fill_in_frames limit=%d start=%d frames length=%d",
119                        max_nframes, start_index, frames_array->length());
120   assert(max_nframes > 0, "invalid max_nframes");
121   assert(start_index + max_nframes <= frames_array->length(), "oob");
122 
123   int frames_decoded = 0;
124   for (; !stream.at_end(); stream.next()) {

125     Method* method = stream.method();
126 
127     if (method == NULL) continue;
128 
129     // skip hidden frames for default StackWalker option (i.e. SHOW_HIDDEN_FRAMES
130     // not set) and when StackWalker::getCallerClass is called
131     if (!ShowHiddenFrames && (skip_hidden_frames(mode) || get_caller_class(mode))) {
132       if (method->is_hidden()) {
133         LogTarget(Debug, stackwalk) lt;
134         if (lt.is_enabled()) {
135           ResourceMark rm(THREAD);
136           LogStream ls(lt);
137           ls.print("  hidden method: ");
138           method->print_short_name(&ls);
139           ls.cr();
140         }
141         continue;
142       }
143     }
144 
145     int index = end_index++;
146     LogTarget(Debug, stackwalk) lt;
147     if (lt.is_enabled()) {
148       ResourceMark rm(THREAD);
149       LogStream ls(lt);
150       ls.print("  %d: frame method: ", index);
151       method->print_short_name(&ls);
152       ls.print_cr(" bci=%d", stream.bci());
153     }
154 
155     if (!need_method_info(mode) && get_caller_class(mode) &&
156           index == start_index && method->caller_sensitive()) {
157       ResourceMark rm(THREAD);
158       THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(),
159         err_msg("StackWalker::getCallerClass called from @CallerSensitive '%s' method",
160                 method->external_name()));
161     }
162     // fill in StackFrameInfo and initialize MemberName
163     stream.fill_frame(index, frames_array, methodHandle(THREAD, method), CHECK_0);
164     if (++frames_decoded >= max_nframes)  break;












165   }


166   return frames_decoded;
167 }
168 
169 // Fill in the LiveStackFrameInfo at the given index in frames_array
170 void LiveFrameStream::fill_frame(int index, objArrayHandle  frames_array,
171                                  const methodHandle& method, TRAPS) {
172   HandleMark hm(THREAD);
173   Handle stackFrame(THREAD, frames_array->obj_at(index));
174   fill_live_stackframe(stackFrame, method, CHECK);
175 }
176 
177 // Fill in the StackFrameInfo at the given index in frames_array
178 void JavaFrameStream::fill_frame(int index, objArrayHandle  frames_array,
179                                  const methodHandle& method, TRAPS) {
180   if (_need_method_info) {
181     HandleMark hm(THREAD);
182     Handle stackFrame(THREAD, frames_array->obj_at(index));
183     fill_stackframe(stackFrame, method, CHECK);
184   } else {
185     frames_array->obj_at_put(index, method->method_holder()->java_mirror());

267       array_h->obj_at_put(i, obj);
268     }
269   }
270   return array_h;
271 }
272 
273 objArrayHandle LiveFrameStream::monitors_to_object_array(GrowableArray<MonitorInfo*>* monitors, TRAPS) {
274   int length = monitors->length();
275   objArrayOop array_oop = oopFactory::new_objArray(vmClasses::Object_klass(),
276                                                    length, CHECK_(objArrayHandle()));
277   objArrayHandle array_h(THREAD, array_oop);
278   for (int i = 0; i < length; i++) {
279     MonitorInfo* monitor = monitors->at(i);
280     array_h->obj_at_put(i, monitor->owner());
281   }
282   return array_h;
283 }
284 
285 // Fill StackFrameInfo with bci and initialize memberName
286 void BaseFrameStream::fill_stackframe(Handle stackFrame, const methodHandle& method, TRAPS) {
287   java_lang_StackFrameInfo::set_method_and_bci(stackFrame, method, bci(), THREAD);
288 }
289 
290 // Fill LiveStackFrameInfo with locals, monitors, and expressions
291 void LiveFrameStream::fill_live_stackframe(Handle stackFrame,
292                                            const methodHandle& method, TRAPS) {
293   fill_stackframe(stackFrame, method, CHECK);
294   if (_jvf != NULL) {
295     ResourceMark rm(THREAD);
296     HandleMark hm(THREAD);
297 
298     StackValueCollection* locals = _jvf->locals();
299     StackValueCollection* expressions = _jvf->expressions();
300     GrowableArray<MonitorInfo*>* monitors = _jvf->monitors();
301 
302     int mode = 0;
303     if (_jvf->is_interpreted_frame()) {
304       mode = MODE_INTERPRETED;
305     } else if (_jvf->is_compiled_frame()) {
306       mode = MODE_COMPILED;
307     }

311       java_lang_LiveStackFrameInfo::set_locals(stackFrame(), locals_h());
312     }
313     if (!expressions->is_empty()) {
314       objArrayHandle expressions_h = values_to_object_array(expressions, CHECK);
315       java_lang_LiveStackFrameInfo::set_operands(stackFrame(), expressions_h());
316     }
317     if (monitors->length() > 0) {
318       objArrayHandle monitors_h = monitors_to_object_array(monitors, CHECK);
319       java_lang_LiveStackFrameInfo::set_monitors(stackFrame(), monitors_h());
320     }
321     java_lang_LiveStackFrameInfo::set_mode(stackFrame(), mode);
322   }
323 }
324 
325 // Begins stack walking.
326 //
327 // Parameters:
328 //   stackStream    StackStream object
329 //   mode           Stack walking mode.
330 //   skip_frames    Number of frames to be skipped.

331 //   frame_count    Number of frames to be traversed.
332 //   start_index    Start index to the user-supplied buffers.
333 //   frames_array   Buffer to store StackFrame in, starting at start_index.
334 //                  frames array is a Class<?>[] array when only getting caller
335 //                  reference, and a StackFrameInfo[] array (or derivative)
336 //                  otherwise. It should never be null.
337 //
338 // Returns Object returned from AbstractStackWalker::doStackWalk call.
339 //
340 oop StackWalk::walk(Handle stackStream, jlong mode,
341                     int skip_frames, int frame_count, int start_index,
342                     objArrayHandle frames_array,
343                     TRAPS) {
344   ResourceMark rm(THREAD);


345   JavaThread* jt = THREAD;
346   log_debug(stackwalk)("Start walking: mode " JLONG_FORMAT " skip %d frames batch size %d",
347                        mode, skip_frames, frame_count);







348 
349   if (frames_array.is_null()) {
350     THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", NULL);
351   }
352 
353   // Setup traversal onto my stack.
354   if (live_frame_info(mode)) {
355     assert (use_frames_array(mode), "Bad mode for get live frame");
356     RegisterMap regMap(jt, true);
357     LiveFrameStream stream(jt, &regMap);

358     return fetchFirstBatch(stream, stackStream, mode, skip_frames, frame_count,
359                            start_index, frames_array, THREAD);
360   } else {
361     JavaFrameStream stream(jt, mode);
362     return fetchFirstBatch(stream, stackStream, mode, skip_frames, frame_count,
363                            start_index, frames_array, THREAD);
364   }
365 }
366 
367 oop StackWalk::fetchFirstBatch(BaseFrameStream& stream, Handle stackStream,
368                                jlong mode, int skip_frames, int frame_count,
369                                int start_index, objArrayHandle frames_array, TRAPS) {
370   methodHandle m_doStackWalk(THREAD, Universe::do_stack_walk_method());
371 
372   {
373     Klass* stackWalker_klass = vmClasses::StackWalker_klass();
374     Klass* abstractStackWalker_klass = vmClasses::AbstractStackWalker_klass();
375     while (!stream.at_end()) {
376       InstanceKlass* ik = stream.method()->method_holder();
377       if (ik != stackWalker_klass &&
378             ik != abstractStackWalker_klass && ik->super() != abstractStackWalker_klass)  {
379         break;
380       }
381 

442     THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers on exit", NULL);
443   }
444 
445   // Return normally
446   return result.get_oop();
447 }
448 
449 // Walk the next batch of stack frames
450 //
451 // Parameters:
452 //   stackStream    StackStream object
453 //   mode           Stack walking mode.
454 //   magic          Must be valid value to continue the stack walk
455 //   frame_count    Number of frames to be decoded.
456 //   start_index    Start index to the user-supplied buffers.
457 //   frames_array   Buffer to store StackFrame in, starting at start_index.
458 //
459 // Returns the end index of frame filled in the buffer.
460 //
461 jint StackWalk::fetchNextBatch(Handle stackStream, jlong mode, jlong magic,
462                                int frame_count, int start_index,
463                                objArrayHandle frames_array,
464                                TRAPS)
465 {
466   JavaThread* jt = THREAD;
467   BaseFrameStream* existing_stream = BaseFrameStream::from_current(jt, magic, frames_array);
468   if (existing_stream == NULL) {
469     THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers", 0L);
470   }
471 
472   if (frames_array.is_null()) {
473     THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", 0L);
474   }
475 
476   log_debug(stackwalk)("StackWalk::fetchNextBatch frame_count %d existing_stream "
477                        PTR_FORMAT " start %d frames %d",
478                        frame_count, p2i(existing_stream), start_index, frames_array->length());
479   int end_index = start_index;
480   if (frame_count <= 0) {
481     return end_index;        // No operation.
482   }

486 
487   BaseFrameStream& stream = (*existing_stream);
488   if (!stream.at_end()) {
489     // If we have to get back here for even more frames, then 1) the user did not supply
490     // an accurate hint suggesting the depth of the stack walk, and 2) we are not just
491     // peeking  at a few frames. Take the cost of flushing out any pending deferred GC
492     // processing of the stack.
493     KeepStackGCProcessedMark keep_stack(jt);
494     stream.next(); // advance past the last frame decoded in previous batch
495     if (!stream.at_end()) {
496       int n = fill_in_frames(mode, stream, frame_count, start_index,
497                              frames_array, end_index, CHECK_0);
498       if (n < 1) {
499         THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: later decode failed", 0L);
500       }
501       return end_index;
502     }
503   }
504   return end_index;
505 }
















 29 #include "classfile/vmSymbols.hpp"
 30 #include "logging/log.hpp"
 31 #include "logging/logStream.hpp"
 32 #include "memory/oopFactory.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/klass.inline.hpp"
 35 #include "oops/oop.inline.hpp"
 36 #include "oops/objArrayOop.inline.hpp"
 37 #include "prims/stackwalk.hpp"
 38 #include "runtime/globals.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/javaCalls.hpp"
 41 #include "runtime/keepStackGCProcessed.hpp"
 42 #include "runtime/stackWatermarkSet.hpp"
 43 #include "runtime/thread.inline.hpp"
 44 #include "runtime/vframe.inline.hpp"
 45 #include "utilities/formatBuffer.hpp"
 46 #include "utilities/globalDefinitions.hpp"
 47 
 48 // setup and cleanup actions
 49 BaseFrameStream::BaseFrameStream(JavaThread* thread, Handle continuation) 
 50   : _thread(thread), _continuation(continuation), _anchor(0L) {
 51     assert (thread != NULL, "");
 52 }
 53 
 54 void BaseFrameStream::setup_magic_on_entry(objArrayHandle frames_array) {
 55   frames_array->obj_at_put(magic_pos, _thread->threadObj());
 56   _anchor = address_value();
 57   assert(check_magic(frames_array), "invalid magic");
 58 }
 59 
 60 bool BaseFrameStream::check_magic(objArrayHandle frames_array) {
 61   oop   m1 = frames_array->obj_at(magic_pos);
 62   jlong m2 = _anchor;
 63   if (m1 == _thread->threadObj() && m2 == address_value())  return true;
 64   return false;
 65 }
 66 
 67 bool BaseFrameStream::cleanup_magic_on_exit(objArrayHandle frames_array) {
 68   bool ok = check_magic(frames_array);
 69   frames_array->obj_at_put(magic_pos, NULL);
 70   _anchor = 0L;
 71   return ok;
 72 }
 73 
 74 void BaseFrameStream::set_continuation(Handle cont) {
 75   // ensure that the lifetime of the handle is that of the entire walk
 76   // This actually also sets a copy of the handle in the RegisterMap,
 77   // but that's OK, because we want them to be the same, anyway.
 78   // (although we don't rely on this sharing, and set the other copy again)
 79   // tty->print_cr("-- BaseFrameStream::set_continuation: %p", (oopDesc*)cont());
 80   *(_continuation.raw_value()) = cont();
 81 }
 82 
 83 // static inline Handle continuation_of(Handle cont_or_scope) {
 84 //   return (cont_or_scope.not_null() && cont_or_scope()->is_a(SystemDictionary::Continuation_klass()))
 85 //             ? cont_or_scope
 86 //             : Handle();
 87 // }
 88 
 89 // static inline Handle continuationScope_of(JavaThread* thread, Handle cont_or_scope) {
 90 //   if (cont_or_scope.is_null() || cont_or_scope()->is_a(SystemDictionary::ContinuationScope_klass()))
 91 //     return cont_or_scope;
 92 //   assert (cont_or_scope()->is_a(SystemDictionary::Continuation_klass()), "must be");
 93 //   return Handle(thread, Continuation::continuation_scope(cont_or_scope()));
 94 // }
 95 
 96 JavaFrameStream::JavaFrameStream(JavaThread* thread, int mode, Handle cont_scope, Handle cont)
 97   : BaseFrameStream(thread, cont), 
 98    _vfst(cont.is_null()
 99       ? vframeStream(thread, cont_scope)
100       : vframeStream(cont(), cont_scope)) {
101   _need_method_info = StackWalk::need_method_info(mode);
102 }
103 
104 LiveFrameStream::LiveFrameStream(JavaThread* thread, RegisterMap* rm, Handle cont_scope, Handle cont)
105    : BaseFrameStream(thread, cont), _cont_scope(cont_scope) {
106      
107     _map = rm;
108     if (cont.is_null()) {
109       _jvf  = thread->last_java_vframe(rm);
110       _cont = thread->last_continuation();
111     } else {
112       _jvf  = Continuation::last_java_vframe(cont, rm);
113       _cont = NULL;
114     }
115 }
116 
117 void JavaFrameStream::next() { 
118   _vfst.next(); 
119   if (_vfst.method()->is_continuation_enter_intrinsic()) 
120     _vfst.next();
121 }
122 
123 void LiveFrameStream::next() {
124   assert (_cont_scope.is_null() || cont() != (oop)NULL, "must be");
125 
126   oop cont = this->cont();
127   if (cont != (oop)NULL && Continuation::is_continuation_entry_frame(_jvf->fr(), _jvf->register_map())) {    
128     oop scope = jdk_internal_vm_Continuation::scope(cont);
129     if (_cont_scope.not_null() && scope == _cont_scope()) {
130       _jvf = NULL;
131       return;
132     }
133     _cont = _cont->parent();
134   }
135   assert (!Continuation::is_scope_bottom(_cont_scope(), _jvf->fr(), _jvf->register_map()), "");
136   
137   _jvf = _jvf->java_sender();
138 }
139 
140 // Returns the BaseFrameStream for the current stack being traversed.
141 //
142 // Parameters:
143 //  thread         Current Java thread.
144 //  magic          Magic value used for each stack walking
145 //  frames_array   User-supplied buffers.  The 0th element is reserved
146 //                 for this BaseFrameStream to use
147 //
148 BaseFrameStream* BaseFrameStream::from_current(JavaThread* thread, jlong magic,
149                                                objArrayHandle frames_array)
150 {
151   oop m1 = frames_array->obj_at(magic_pos);
152   if (m1 != thread->threadObj()) return NULL;
153   if (magic == 0L)                    return NULL;
154   BaseFrameStream* stream = (BaseFrameStream*) (intptr_t) magic;
155   if (!stream->is_valid_in(thread, frames_array))   return NULL;
156   return stream;
157 }
158 

169 //   start_index      Start index to the user-supplied buffers.
170 //   frames_array     Buffer to store Class or StackFrame in, starting at start_index.
171 //                    frames array is a Class<?>[] array when only getting caller
172 //                    reference, and a StackFrameInfo[] array (or derivative)
173 //                    otherwise. It should never be null.
174 //   end_index        End index to the user-supplied buffers with unpacked frames.
175 //
176 // Returns the number of frames whose information was transferred into the buffers.
177 //
178 int StackWalk::fill_in_frames(jlong mode, BaseFrameStream& stream,
179                               int max_nframes, int start_index,
180                               objArrayHandle  frames_array,
181                               int& end_index, TRAPS) {
182   log_debug(stackwalk)("fill_in_frames limit=%d start=%d frames length=%d",
183                        max_nframes, start_index, frames_array->length());
184   assert(max_nframes > 0, "invalid max_nframes");
185   assert(start_index + max_nframes <= frames_array->length(), "oob");
186 
187   int frames_decoded = 0;
188   for (; !stream.at_end(); stream.next()) {
189     assert (stream.continuation() == NULL || stream.continuation() == stream.reg_map()->cont(), "");
190     Method* method = stream.method();
191 
192     if (method == NULL) continue;
193 
194     // skip hidden frames for default StackWalker option (i.e. SHOW_HIDDEN_FRAMES
195     // not set) and when StackWalker::getCallerClass is called
196     if (!ShowHiddenFrames && (skip_hidden_frames(mode) || get_caller_class(mode))) {
197       if (method->is_hidden()) {
198         LogTarget(Debug, stackwalk) lt;
199         if (lt.is_enabled()) {
200           ResourceMark rm(THREAD);
201           LogStream ls(lt);
202           ls.print("  hidden method: ");
203           method->print_short_name(&ls);
204           ls.cr();
205         }
206         continue;
207       }
208     }
209 
210     int index = end_index++;
211     LogTarget(Debug, stackwalk) lt;
212     if (lt.is_enabled()) {
213       ResourceMark rm(THREAD);
214       LogStream ls(lt);
215       ls.print("  %d: frame method: ", index);
216       method->print_short_name(&ls);
217       ls.print_cr(" bci=%d", stream.bci());
218     }
219 
220     if (!need_method_info(mode) && get_caller_class(mode) &&
221           index == start_index && method->caller_sensitive()) {
222       ResourceMark rm(THREAD);
223       THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(),
224         err_msg("StackWalker::getCallerClass called from @CallerSensitive '%s' method",
225                 method->external_name()));
226     }
227     // fill in StackFrameInfo and initialize MemberName
228     stream.fill_frame(index, frames_array, methodHandle(THREAD, method), CHECK_0);
229 
230     if (lt.is_enabled()) {
231       ResourceMark rm(THREAD);
232       LogStream ls(lt);
233       ls.print("  %d: done frame method: ", index);
234       method->print_short_name(&ls);
235     }
236     frames_decoded++;
237 
238     // We end a batch on continuation bottom to let the Java side skip top frames of the next one
239     if (stream.continuation() != NULL && method->intrinsic_id() == vmIntrinsics::_Continuation_enter) break;
240 
241     if (frames_decoded >= max_nframes)  break;
242   }
243   log_debug(stackwalk)("fill_in_frames done frames_decoded=%d at_end=%d", frames_decoded, stream.at_end());
244 
245   return frames_decoded;
246 }
247 
248 // Fill in the LiveStackFrameInfo at the given index in frames_array
249 void LiveFrameStream::fill_frame(int index, objArrayHandle  frames_array,
250                                  const methodHandle& method, TRAPS) {
251   HandleMark hm(THREAD);
252   Handle stackFrame(THREAD, frames_array->obj_at(index));
253   fill_live_stackframe(stackFrame, method, CHECK);
254 }
255 
256 // Fill in the StackFrameInfo at the given index in frames_array
257 void JavaFrameStream::fill_frame(int index, objArrayHandle  frames_array,
258                                  const methodHandle& method, TRAPS) {
259   if (_need_method_info) {
260     HandleMark hm(THREAD);
261     Handle stackFrame(THREAD, frames_array->obj_at(index));
262     fill_stackframe(stackFrame, method, CHECK);
263   } else {
264     frames_array->obj_at_put(index, method->method_holder()->java_mirror());

346       array_h->obj_at_put(i, obj);
347     }
348   }
349   return array_h;
350 }
351 
352 objArrayHandle LiveFrameStream::monitors_to_object_array(GrowableArray<MonitorInfo*>* monitors, TRAPS) {
353   int length = monitors->length();
354   objArrayOop array_oop = oopFactory::new_objArray(vmClasses::Object_klass(),
355                                                    length, CHECK_(objArrayHandle()));
356   objArrayHandle array_h(THREAD, array_oop);
357   for (int i = 0; i < length; i++) {
358     MonitorInfo* monitor = monitors->at(i);
359     array_h->obj_at_put(i, monitor->owner());
360   }
361   return array_h;
362 }
363 
364 // Fill StackFrameInfo with bci and initialize memberName
365 void BaseFrameStream::fill_stackframe(Handle stackFrame, const methodHandle& method, TRAPS) {
366   java_lang_StackFrameInfo::set_method_and_bci(stackFrame, method, bci(), cont(), THREAD);
367 }
368 
369 // Fill LiveStackFrameInfo with locals, monitors, and expressions
370 void LiveFrameStream::fill_live_stackframe(Handle stackFrame,
371                                            const methodHandle& method, TRAPS) {
372   fill_stackframe(stackFrame, method, CHECK);
373   if (_jvf != NULL) {
374     ResourceMark rm(THREAD);
375     HandleMark hm(THREAD);
376 
377     StackValueCollection* locals = _jvf->locals();
378     StackValueCollection* expressions = _jvf->expressions();
379     GrowableArray<MonitorInfo*>* monitors = _jvf->monitors();
380 
381     int mode = 0;
382     if (_jvf->is_interpreted_frame()) {
383       mode = MODE_INTERPRETED;
384     } else if (_jvf->is_compiled_frame()) {
385       mode = MODE_COMPILED;
386     }

390       java_lang_LiveStackFrameInfo::set_locals(stackFrame(), locals_h());
391     }
392     if (!expressions->is_empty()) {
393       objArrayHandle expressions_h = values_to_object_array(expressions, CHECK);
394       java_lang_LiveStackFrameInfo::set_operands(stackFrame(), expressions_h());
395     }
396     if (monitors->length() > 0) {
397       objArrayHandle monitors_h = monitors_to_object_array(monitors, CHECK);
398       java_lang_LiveStackFrameInfo::set_monitors(stackFrame(), monitors_h());
399     }
400     java_lang_LiveStackFrameInfo::set_mode(stackFrame(), mode);
401   }
402 }
403 
404 // Begins stack walking.
405 //
406 // Parameters:
407 //   stackStream    StackStream object
408 //   mode           Stack walking mode.
409 //   skip_frames    Number of frames to be skipped.
410 //   cont_scope     Continuation scope to walk (if not in this scope, we'll walk all the way).
411 //   frame_count    Number of frames to be traversed.
412 //   start_index    Start index to the user-supplied buffers.
413 //   frames_array   Buffer to store StackFrame in, starting at start_index.
414 //                  frames array is a Class<?>[] array when only getting caller
415 //                  reference, and a StackFrameInfo[] array (or derivative)
416 //                  otherwise. It should never be null.
417 //
418 // Returns Object returned from AbstractStackWalker::doStackWalk call.
419 //
420 oop StackWalk::walk(Handle stackStream, jlong mode, int skip_frames, Handle cont_scope, Handle cont, 
421                     int frame_count, int start_index, objArrayHandle frames_array,

422                     TRAPS) {
423   ResourceMark rm(THREAD);
424   HandleMark hm(THREAD); // needed to store a continuation in the RegisterMap
425 
426   JavaThread* jt = THREAD;
427   log_debug(stackwalk)("Start walking: mode " JLONG_FORMAT " skip %d frames batch size %d", mode, skip_frames, frame_count);
428   LogTarget(Debug, stackwalk) lt;
429   if (lt.is_enabled()) {
430     ResourceMark rm(THREAD);
431     LogStream ls(lt);
432     ls.print("cont_scope: ");
433     cont_scope()->print_on(&ls);
434     ls.cr();
435   }
436 
437   if (frames_array.is_null()) {
438     THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", NULL);
439   }
440 
441   // Setup traversal onto my stack.
442   if (live_frame_info(mode)) {
443     assert (use_frames_array(mode), "Bad mode for get live frame");
444     RegisterMap regMap = cont.is_null() ? RegisterMap(jt, true, true, true)
445                                         : RegisterMap(cont(), true);
446     LiveFrameStream stream(jt, &regMap, cont_scope, cont);
447     return fetchFirstBatch(stream, stackStream, mode, skip_frames, frame_count,
448                            start_index, frames_array, THREAD);
449   } else {
450     JavaFrameStream stream(jt, mode, cont_scope, cont);
451     return fetchFirstBatch(stream, stackStream, mode, skip_frames, frame_count,
452                            start_index, frames_array, THREAD);
453   }
454 }
455 
456 oop StackWalk::fetchFirstBatch(BaseFrameStream& stream, Handle stackStream,
457                                jlong mode, int skip_frames, int frame_count,
458                                int start_index, objArrayHandle frames_array, TRAPS) {
459   methodHandle m_doStackWalk(THREAD, Universe::do_stack_walk_method());
460 
461   {
462     Klass* stackWalker_klass = vmClasses::StackWalker_klass();
463     Klass* abstractStackWalker_klass = vmClasses::AbstractStackWalker_klass();
464     while (!stream.at_end()) {
465       InstanceKlass* ik = stream.method()->method_holder();
466       if (ik != stackWalker_klass &&
467             ik != abstractStackWalker_klass && ik->super() != abstractStackWalker_klass)  {
468         break;
469       }
470 

531     THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers on exit", NULL);
532   }
533 
534   // Return normally
535   return result.get_oop();
536 }
537 
538 // Walk the next batch of stack frames
539 //
540 // Parameters:
541 //   stackStream    StackStream object
542 //   mode           Stack walking mode.
543 //   magic          Must be valid value to continue the stack walk
544 //   frame_count    Number of frames to be decoded.
545 //   start_index    Start index to the user-supplied buffers.
546 //   frames_array   Buffer to store StackFrame in, starting at start_index.
547 //
548 // Returns the end index of frame filled in the buffer.
549 //
550 jint StackWalk::fetchNextBatch(Handle stackStream, jlong mode, jlong magic,
551                                int frame_count, int start_index, 
552                                objArrayHandle frames_array,
553                                TRAPS)
554 {
555   JavaThread* jt = THREAD;
556   BaseFrameStream* existing_stream = BaseFrameStream::from_current(jt, magic, frames_array);
557   if (existing_stream == NULL) {
558     THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers", 0L);
559   }
560 
561   if (frames_array.is_null()) {
562     THROW_MSG_(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL", 0L);
563   }
564 
565   log_debug(stackwalk)("StackWalk::fetchNextBatch frame_count %d existing_stream "
566                        PTR_FORMAT " start %d frames %d",
567                        frame_count, p2i(existing_stream), start_index, frames_array->length());
568   int end_index = start_index;
569   if (frame_count <= 0) {
570     return end_index;        // No operation.
571   }

575 
576   BaseFrameStream& stream = (*existing_stream);
577   if (!stream.at_end()) {
578     // If we have to get back here for even more frames, then 1) the user did not supply
579     // an accurate hint suggesting the depth of the stack walk, and 2) we are not just
580     // peeking  at a few frames. Take the cost of flushing out any pending deferred GC
581     // processing of the stack.
582     KeepStackGCProcessedMark keep_stack(jt);
583     stream.next(); // advance past the last frame decoded in previous batch
584     if (!stream.at_end()) {
585       int n = fill_in_frames(mode, stream, frame_count, start_index,
586                              frames_array, end_index, CHECK_0);
587       if (n < 1) {
588         THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: later decode failed", 0L);
589       }
590       return end_index;
591     }
592   }
593   return end_index;
594 }
595 
596 void StackWalk::setContinuation(Handle stackStream, jlong magic, objArrayHandle frames_array, Handle cont, TRAPS) {
597   JavaThread* jt = JavaThread::cast(THREAD);
598 
599   if (frames_array.is_null()) {
600     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "frames_array is NULL");
601   }
602 
603   BaseFrameStream* existing_stream = BaseFrameStream::from_current(jt, magic, frames_array);
604   if (existing_stream == NULL) {
605     THROW_MSG(vmSymbols::java_lang_InternalError(), "doStackWalk: corrupted buffers");
606   }
607 
608   existing_stream->set_continuation(cont);
609 }
< prev index next >