1 /*
  2  * Copyright (c) 2015, 2021, 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/vmClasses.hpp"
 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 
159 // Unpacks one or more frames into user-supplied buffers.
160 // Updates the end index, and returns the number of unpacked frames.
161 // Always start with the existing vfst.method and bci.
162 // Do not call vfst.next to advance over the last returned value.
163 // In other words, do not leave any stale data in the vfst.
164 //
165 // Parameters:
166 //   mode             Restrict which frames to be decoded.
167 //   BaseFrameStream  stream of frames
168 //   max_nframes      Maximum number of frames to be filled.
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());
265   }
266 }
267 
268 // Create and return a LiveStackFrame.PrimitiveSlot (if needed) for the
269 // StackValue at the given index. 'type' is expected to be T_INT, T_LONG,
270 // T_OBJECT, or T_CONFLICT.
271 oop LiveFrameStream::create_primitive_slot_instance(StackValueCollection* values,
272                                                     int i, BasicType type, TRAPS) {
273   Klass* k = vmClasses::LiveStackFrameInfo_klass();
274   InstanceKlass* ik = InstanceKlass::cast(k);
275 
276   JavaValue result(T_OBJECT);
277   JavaCallArguments args;
278   Symbol* signature = NULL;
279 
280   // ## TODO: type is only available in LocalVariable table, if present.
281   // ## StackValue type is T_INT or T_OBJECT (or converted to T_LONG on 64-bit)
282   switch (type) {
283     case T_INT:
284       args.push_int(values->int_at(i));
285       signature = vmSymbols::asPrimitive_int_signature();
286       break;
287 
288     case T_LONG:
289       args.push_long(values->long_at(i));
290       signature = vmSymbols::asPrimitive_long_signature();
291       break;
292 
293     case T_FLOAT:
294     case T_DOUBLE:
295     case T_BYTE:
296     case T_SHORT:
297     case T_CHAR:
298     case T_BOOLEAN:
299       THROW_MSG_(vmSymbols::java_lang_InternalError(), "Unexpected StackValue type", NULL);
300 
301     case T_OBJECT:
302       return values->obj_at(i)();
303 
304     case T_CONFLICT:
305       // put a non-null slot
306       #ifdef _LP64
307         args.push_long(0);
308         signature = vmSymbols::asPrimitive_long_signature();
309       #else
310         args.push_int(0);
311         signature = vmSymbols::asPrimitive_int_signature();
312       #endif
313 
314       break;
315 
316     default: ShouldNotReachHere();
317   }
318   JavaCalls::call_static(&result,
319                          ik,
320                          vmSymbols::asPrimitive_name(),
321                          signature,
322                          &args,
323                          CHECK_NULL);
324   return (instanceOop) result.get_oop();
325 }
326 
327 objArrayHandle LiveFrameStream::values_to_object_array(StackValueCollection* values, TRAPS) {
328   objArrayHandle empty;
329   int length = values->size();
330   objArrayOop array_oop = oopFactory::new_objArray(vmClasses::Object_klass(),
331                                                    length, CHECK_(empty));
332   objArrayHandle array_h(THREAD, array_oop);
333   for (int i = 0; i < values->size(); i++) {
334     StackValue* st = values->at(i);
335     BasicType type = st->type();
336     int index = i;
337 #ifdef _LP64
338     if (type != T_OBJECT && type != T_CONFLICT) {
339         intptr_t ret = st->get_int(); // read full 64-bit slot
340         type = T_LONG;                // treat as long
341         index--;                      // undo +1 in StackValueCollection::long_at
342     }
343 #endif
344     oop obj = create_primitive_slot_instance(values, index, type, CHECK_(empty));
345     if (obj != NULL) {
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     }
387 
388     if (!locals->is_empty()) {
389       objArrayHandle locals_h = values_to_object_array(locals, CHECK);
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 
471       LogTarget(Debug, stackwalk) lt;
472       if (lt.is_enabled()) {
473         ResourceMark rm(THREAD);
474         LogStream ls(lt);
475         ls.print("  skip ");
476         stream.method()->print_short_name(&ls);
477         ls.cr();
478       }
479       stream.next();
480     }
481 
482     // stack frame has been traversed individually and resume stack walk
483     // from the stack frame at depth == skip_frames.
484     for (int n=0; n < skip_frames && !stream.at_end(); stream.next(), n++) {
485       LogTarget(Debug, stackwalk) lt;
486       if (lt.is_enabled()) {
487         ResourceMark rm(THREAD);
488         LogStream ls(lt);
489         ls.print("  skip ");
490         stream.method()->print_short_name(&ls);
491         ls.cr();
492       }
493     }
494   }
495 
496   int end_index = start_index;
497   int numFrames = 0;
498   if (!stream.at_end()) {
499     KeepStackGCProcessedMark keep_stack(THREAD);
500     numFrames = fill_in_frames(mode, stream, frame_count, start_index,
501                                frames_array, end_index, CHECK_NULL);
502     if (numFrames < 1) {
503       THROW_MSG_(vmSymbols::java_lang_InternalError(), "stack walk: decode failed", NULL);
504     }
505   }
506 
507   // JVM_CallStackWalk walks the stack and fills in stack frames, then calls to
508   // Java method java.lang.StackStreamFactory.AbstractStackWalker::doStackWalk
509   // which calls the implementation to consume the stack frames.
510   // When JVM_CallStackWalk returns, it invalidates the stack stream.
511   JavaValue result(T_OBJECT);
512   JavaCallArguments args(stackStream);
513   args.push_long(stream.address_value());
514   args.push_int(skip_frames);
515   args.push_int(frame_count);
516   args.push_int(start_index);
517   args.push_int(end_index);
518 
519   // Link the thread and vframe stream into the callee-visible object
520   stream.setup_magic_on_entry(frames_array);
521 
522   JavaCalls::call(&result, m_doStackWalk, &args, THREAD);
523 
524   // Do this before anything else happens, to disable any lingering stream objects
525   bool ok = stream.cleanup_magic_on_exit(frames_array);
526 
527   // Throw pending exception if we must
528   (void) (CHECK_NULL);
529 
530   if (!ok) {
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   }
572 
573   int count = frame_count + start_index;
574   assert (frames_array->length() >= count, "not enough space in buffers");
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 }