@@ -240,6 +240,12 @@ private static class StepState {
240240 }
241241 }
242242
243+ /**
244+ * Cached reflection method for DebuggerFrame.getLine().
245+ * Initialized lazily on first use in getStackDepth().
246+ */
247+ private static volatile java .lang .reflect .Method debuggerFrameGetLineMethod = null ;
248+
243249 public static void setNativeMode (boolean enabled ) {
244250 nativeMode = enabled ;
245251 Log .info ("Native mode: " + enabled );
@@ -604,12 +610,29 @@ public static void stopStepping(long threadId) {
604610 /**
605611 * Get the current stack depth for a PageContext.
606612 * Uses reflection to get debugger frames.
613+ * Only counts frames with line > 0 to match NativeDebugFrame.getNativeFrames() filtering.
607614 */
608615 public static int getStackDepth (PageContext pc ) {
609616 try {
610617 java .lang .reflect .Method getFrames = pc .getClass ().getMethod ("getDebuggerFrames" );
611618 Object [] frames = (Object []) getFrames .invoke (pc );
612- return frames != null ? frames .length : 0 ;
619+ if (frames == null || frames .length == 0 ) return 0 ;
620+
621+ // Cache the getLine method on first use
622+ java .lang .reflect .Method getLine = debuggerFrameGetLineMethod ;
623+ if (getLine == null ) {
624+ getLine = frames [0 ].getClass ().getMethod ("getLine" );
625+ debuggerFrameGetLineMethod = getLine ;
626+ }
627+
628+ // Count only frames with line > 0 (matching getNativeFrames filtering)
629+ // Frames start with line=0 before first ExecutionLog.start() call
630+ int count = 0 ;
631+ for (Object frame : frames ) {
632+ int line = (int ) getLine .invoke (frame );
633+ if (line > 0 ) count ++;
634+ }
635+ return count ;
613636 } catch (Exception e ) {
614637 // Log error - silent failure could cause incorrect step behavior
615638 Log .error ("Error getting stack depth: " + e .getMessage ());
0 commit comments