Rodrigo Mesquita pushed to branch wip/romes/step-out-11 at Glasgow Haskell Compiler / GHC Commits: 67e50cb9 by Rodrigo Mesquita at 2025-07-23T17:42:40+01:00 Allow BRK_FUNs at the start of case continuation BCOs ...and insert BRK_FUNS for case continuations: TODO: SPLIT THE TICK PARTS OUT Working on making BRK_FUNs for case cont. BCO Remove bad Beautiful Fix... More right-trackking TODO: Test step-out from a continuation which receives an unboxed tuple as an argument Fix again Beautiful again Almost there - - - - - 10 changed files: - compiler/GHC/ByteCode/Breakpoints.hs - compiler/GHC/Linker/Loader.hs - compiler/GHC/Runtime/Debugger/Breakpoints.hs - compiler/GHC/Runtime/Eval.hs - compiler/GHC/StgToByteCode.hs - ghc/GHCi/UI.hs - libraries/ghci/GHCi/Run.hs - rts/Interpreter.c - testsuite/tests/ghci.debugger/scripts/T26042g.stdout - testsuite/tests/ghci.debugger/scripts/all.T Changes: ===================================== compiler/GHC/ByteCode/Breakpoints.hs ===================================== @@ -1,4 +1,5 @@ {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE DerivingStrategies #-} -- | Breakpoint information constructed during ByteCode generation. -- @@ -15,6 +16,7 @@ module GHC.ByteCode.Breakpoints -- ** Internal breakpoint identifier , InternalBreakpointId(..), BreakInfoIndex + , InternalBreakLoc(..) -- * Operations @@ -23,7 +25,7 @@ module GHC.ByteCode.Breakpoints -- ** Source-level information operations , getBreakLoc, getBreakVars, getBreakDecls, getBreakCCS - , getBreakSourceId + , getBreakSourceId, getBreakSourceMod -- * Utils , seqInternalModBreaks @@ -165,7 +167,7 @@ data CgBreakInfo { cgb_tyvars :: ![IfaceTvBndr] -- ^ Type variables in scope at the breakpoint , cgb_vars :: ![Maybe (IfaceIdBndr, Word)] , cgb_resty :: !IfaceType - , cgb_tick_id :: !BreakpointId + , cgb_tick_id :: !(Either InternalBreakLoc BreakpointId) -- ^ This field records the original breakpoint tick identifier for this -- internal breakpoint info. It is used to convert a breakpoint -- *occurrence* index ('InternalBreakpointId') into a *definition* index @@ -173,9 +175,19 @@ data CgBreakInfo -- -- The modules of breakpoint occurrence and breakpoint definition are not -- necessarily the same: See Note [Breakpoint identifiers]. + -- + -- If there is no original tick identifier (that is, the breakpoint was + -- created during code generation), instead refer directly to the SrcSpan + -- we want to use for it. See Note [Internal Breakpoint Locations] } -- See Note [Syncing breakpoint info] in GHC.Runtime.Eval +-- | Breakpoints created during code generation don't have a source-level tick +-- location. Instead, we come up with one ourselves. +-- See Note [Internal Breakpoint Locations] +newtype InternalBreakLoc = InternalBreakLoc SrcSpan + deriving newtype (Eq, Show, NFData, Outputable) + -- | Get an internal breakpoint info by 'InternalBreakpointId' getInternalBreak :: InternalBreakpointId -> InternalModBreaks -> CgBreakInfo getInternalBreak (InternalBreakpointId mod ix) imbs = @@ -196,27 +208,36 @@ assert_modules_match ibi_mod imbs_mod = -- | Get the source module and tick index for this breakpoint -- (as opposed to the module where this breakpoint occurs, which is in 'InternalBreakpointId') -getBreakSourceId :: InternalBreakpointId -> InternalModBreaks -> BreakpointId +getBreakSourceId :: InternalBreakpointId -> InternalModBreaks -> Either InternalBreakLoc BreakpointId getBreakSourceId (InternalBreakpointId ibi_mod ibi_ix) imbs = assert_modules_match ibi_mod (imodBreaks_module imbs) $ let cgb = imodBreaks_breakInfo imbs IM.! ibi_ix in cgb_tick_id cgb +-- | Get the source module for this breakpoint (where the breakpoint is defined) +getBreakSourceMod :: InternalBreakpointId -> InternalModBreaks -> Module +getBreakSourceMod (InternalBreakpointId ibi_mod ibi_ix) imbs = + assert_modules_match ibi_mod (imodBreaks_module imbs) $ + let cgb = imodBreaks_breakInfo imbs IM.! ibi_ix + in case cgb_tick_id cgb of + Left InternalBreakLoc{} -> imodBreaks_module imbs + Right BreakpointId{bi_tick_mod} -> bi_tick_mod + -- | Get the source span for this breakpoint getBreakLoc :: (Module -> IO ModBreaks) -> InternalBreakpointId -> InternalModBreaks -> IO SrcSpan -getBreakLoc = getBreakXXX modBreaks_locs +getBreakLoc = getBreakXXX modBreaks_locs (\(InternalBreakLoc x) -> x) -- | Get the vars for this breakpoint getBreakVars :: (Module -> IO ModBreaks) -> InternalBreakpointId -> InternalModBreaks -> IO [OccName] -getBreakVars = getBreakXXX modBreaks_vars +getBreakVars = getBreakXXX modBreaks_vars (const []) -- | Get the decls for this breakpoint getBreakDecls :: (Module -> IO ModBreaks) -> InternalBreakpointId -> InternalModBreaks -> IO [String] -getBreakDecls = getBreakXXX modBreaks_decls +getBreakDecls = getBreakXXX modBreaks_decls (const []) -- | Get the decls for this breakpoint -getBreakCCS :: (Module -> IO ModBreaks) -> InternalBreakpointId -> InternalModBreaks -> IO (String, String) -getBreakCCS = getBreakXXX modBreaks_ccs +getBreakCCS :: (Module -> IO ModBreaks) -> InternalBreakpointId -> InternalModBreaks -> IO (Maybe (String, String)) +getBreakCCS = getBreakXXX (fmap Just . modBreaks_ccs) (const Nothing) -- | Internal utility to access a ModBreaks field at a particular breakpoint index -- @@ -228,14 +249,17 @@ getBreakCCS = getBreakXXX modBreaks_ccs -- 'ModBreaks'. When the tick module is different, we need to look up the -- 'ModBreaks' in the HUG for that other module. -- +-- When there is no tick module (the breakpoint was generated at codegen), use +-- the function on internal mod breaks. +-- -- To avoid cyclic dependencies, we instead receive a function that looks up -- the 'ModBreaks' given a 'Module' -getBreakXXX :: (ModBreaks -> Array BreakTickIndex a) -> (Module -> IO ModBreaks) -> InternalBreakpointId -> InternalModBreaks -> IO a -getBreakXXX view lookupModule (InternalBreakpointId ibi_mod ibi_ix) imbs = +getBreakXXX :: (ModBreaks -> Array BreakTickIndex a) -> (InternalBreakLoc -> a) -> (Module -> IO ModBreaks) -> InternalBreakpointId -> InternalModBreaks -> IO a +getBreakXXX view viewInternal lookupModule (InternalBreakpointId ibi_mod ibi_ix) imbs = assert_modules_match ibi_mod (imodBreaks_module imbs) $ do let cgb = imodBreaks_breakInfo imbs IM.! ibi_ix case cgb_tick_id cgb of - BreakpointId{bi_tick_mod, bi_tick_index} + Right BreakpointId{bi_tick_mod, bi_tick_index} | bi_tick_mod == ibi_mod -> do let these_mbs = imodBreaks_modBreaks imbs @@ -244,6 +268,8 @@ getBreakXXX view lookupModule (InternalBreakpointId ibi_mod ibi_ix) imbs = -> do other_mbs <- lookupModule bi_tick_mod return $ view other_mbs ! bi_tick_index + Left l -> + return $ viewInternal l -------------------------------------------------------------------------------- -- Instances ===================================== compiler/GHC/Linker/Loader.hs ===================================== @@ -58,6 +58,7 @@ import GHCi.RemoteTypes import GHC.Iface.Load import GHCi.Message (ConInfoTable(..), LoadedDLL) +import GHC.ByteCode.Breakpoints import GHC.ByteCode.Linker import GHC.ByteCode.Asm import GHC.ByteCode.Types @@ -1711,8 +1712,10 @@ allocateCCS interp ce mbss let count = 1 + (maybe 0 fst $ IM.lookupMax imodBreaks_breakInfo) let ccs = IM.map (\info -> - fromMaybe (toRemotePtr nullPtr) - (M.lookup (cgb_tick_id info) ccss) + case cgb_tick_id info of + Right bi -> fromMaybe (toRemotePtr nullPtr) + (M.lookup bi ccss) + Left InternalBreakLoc{} -> toRemotePtr nullPtr ) imodBreaks_breakInfo assertPpr (count == length ccs) ===================================== compiler/GHC/Runtime/Debugger/Breakpoints.hs ===================================== @@ -253,8 +253,11 @@ mkBreakpointOccurrences = do let imod = modBreaks_module $ imodBreaks_modBreaks ibrks IntMap.foldrWithKey (\info_ix cgi bmp -> do let ibi = InternalBreakpointId imod info_ix - let BreakpointId tick_mod tick_ix = cgb_tick_id cgi - extendModuleEnvWith (IntMap.unionWith (S.<>)) bmp tick_mod (IntMap.singleton tick_ix [ibi]) + case cgb_tick_id cgi of + Right (BreakpointId tick_mod tick_ix) + -> extendModuleEnvWith (IntMap.unionWith (S.<>)) bmp tick_mod (IntMap.singleton tick_ix [ibi]) + Left _ + -> bmp ) bmp0 (imodBreaks_breakInfo ibrks) -------------------------------------------------------------------------------- @@ -287,7 +290,7 @@ getCurrentBreakModule = do Nothing -> pure Nothing Just ibi -> do brks <- readIModBreaks hug ibi - return $ Just $ bi_tick_mod $ getBreakSourceId ibi brks + return $ Just $ getBreakSourceMod ibi brks ix -> Just <$> getHistoryModule hug (resumeHistory r !! (ix-1)) ===================================== compiler/GHC/Runtime/Eval.hs ===================================== @@ -151,7 +151,7 @@ getHistoryModule :: HUG.HomeUnitGraph -> History -> IO Module getHistoryModule hug hist = do let ibi = historyBreakpointId hist brks <- readIModBreaks hug ibi - return $ bi_tick_mod $ getBreakSourceId ibi brks + return $ getBreakSourceMod ibi brks getHistorySpan :: HUG.HomeUnitGraph -> History -> IO SrcSpan getHistorySpan hug hist = do ===================================== compiler/GHC/StgToByteCode.hs ===================================== @@ -63,7 +63,7 @@ import GHC.StgToCmm.Closure ( NonVoid(..), fromNonVoid, idPrimRepU, assertNonVoidIds, assertNonVoidStgArgs ) import GHC.StgToCmm.Layout import GHC.Runtime.Heap.Layout hiding (WordOff, ByteOff, wordsToBytes) -import GHC.Runtime.Interpreter ( interpreterProfiled ) +import GHC.Runtime.Interpreter ( interpreterProfiled, readIModModBreaks ) import GHC.Data.Bitmap import GHC.Data.FlatBag as FlatBag import GHC.Data.OrdList @@ -99,6 +99,7 @@ import GHC.CoreToIface import Control.Monad.IO.Class import Control.Monad.Trans.Reader (ReaderT(..)) import Control.Monad.Trans.State (StateT(..)) +import Data.Array ((!)) -- ----------------------------------------------------------------------------- -- Generating byte code for a complete module @@ -393,26 +394,30 @@ schemeR_wrk fvs nm original_body (args, body) -- | Introduce break instructions for ticked expressions. -- If no breakpoint information is available, the instruction is omitted. schemeER_wrk :: StackDepth -> BCEnv -> CgStgExpr -> BcM BCInstrList -schemeER_wrk d p (StgTick (Breakpoint tick_ty tick_id fvs) rhs) = do - code <- schemeE d 0 p rhs - mb_current_mod_breaks <- getCurrentModBreaks - case mb_current_mod_breaks of - -- if we're not generating ModBreaks for this module for some reason, we - -- can't store breakpoint occurrence information. - Nothing -> pure code - Just current_mod_breaks -> do - platform <- profilePlatform <$> getProfile - let idOffSets = getVarOffSets platform d p fvs - ty_vars = tyCoVarsOfTypesWellScoped (tick_ty:map idType fvs) - toWord :: Maybe (Id, WordOff) -> Maybe (Id, Word) - toWord = fmap (\(i, wo) -> (i, fromIntegral wo)) - breakInfo = dehydrateCgBreakInfo ty_vars (map toWord idOffSets) tick_ty tick_id - - let info_mod = modBreaks_module current_mod_breaks - infox <- newBreakInfo breakInfo +schemeER_wrk d p (StgTick bp@(Breakpoint tick_ty tick_id fvs) rhs) = do + platform <- profilePlatform <$> getProfile + + code <- case rhs of + -- When we find a tick surrounding a case expression we introduce a new BRK_FUN + -- instruction at the start of the case *continuation*, in addition to the + -- usual BRK_FUN surrounding the StgCase) + -- See Note [TODO] + StgCase scrut bndr _ alts + -> doCase d 0 p (Just bp) scrut bndr alts + _ -> schemeE d 0 p rhs + + let idOffSets = getVarOffSets platform d p fvs + ty_vars = tyCoVarsOfTypesWellScoped (tick_ty:map idType fvs) + toWord :: Maybe (Id, WordOff) -> Maybe (Id, Word) + toWord = fmap (\(i, wo) -> (i, fromIntegral wo)) + breakInfo = dehydrateCgBreakInfo ty_vars (map toWord idOffSets) tick_ty (Right tick_id) + + mibi <- newBreakInfo breakInfo + + return $ case mibi of + Nothing -> code + Just ibi -> BRK_FUN ibi `consOL` code - let breakInstr = BRK_FUN (InternalBreakpointId info_mod infox) - return $ breakInstr `consOL` code schemeER_wrk d p rhs = schemeE d 0 p rhs getVarOffSets :: Platform -> StackDepth -> BCEnv -> [Id] -> [Maybe (Id, WordOff)] @@ -614,7 +619,7 @@ schemeE d s p (StgTick _ rhs) = schemeE d s p rhs schemeE d s p (StgCase scrut _ _ []) = schemeE d s p scrut schemeE d s p (StgCase scrut bndr _ alts) - = doCase d s p scrut bndr alts + = doCase d s p Nothing scrut bndr alts {- @@ -1106,11 +1111,15 @@ doCase :: StackDepth -> Sequel -> BCEnv + -> Maybe StgTickish + -- ^ The breakpoint surrounding the full case expression, if any (only + -- source-level cases get breakpoint ticks, and those are the only we care + -- about). See Note [TODO] -> CgStgExpr -> Id -> [CgStgAlt] -> BcM BCInstrList -doCase d s p scrut bndr alts +doCase d s p m_bid scrut bndr alts = do profile <- getProfile hsc_env <- getHscEnv @@ -1325,19 +1334,35 @@ doCase d s p scrut bndr alts | ubx_tuple_frame = SLIDE 0 3 `consOL` alt_final1 | otherwise = SLIDE 0 1 `consOL` alt_final1 - -- when `BRK_FUN` in a case continuation BCO executes, - -- the stack will already have a full continuation that just - -- re-executes the BCO being stopped at (including the stg_ret and - -- stg_ctoi frames) - -- - -- right after the `BRK_FUN`, all case continuations will drop the - -- stg_ret and stg_ctoi headers (see alt_final1, alt_final2), leaving - -- the stack with the bound return values followed by the free variables - alt_final - | gopt Opt_InsertBreakpoints (hsc_dflags hsc_env) - -- See Note [Debugger: BRK_ALTS] - = BRK_ALTS False `consOL` alt_final2 - | otherwise = alt_final2 + -- when `BRK_FUN` in a case continuation BCO executes, + -- the stack will already have a full continuation that just + -- re-executes the BCO being stopped at (including the stg_ret and + -- stg_ctoi frames) + -- + -- right after the `BRK_FUN`, all case continuations will drop the + -- stg_ret and stg_ctoi headers (see alt_final1, alt_final2), leaving + -- the stack with the bound return values followed by the free variables + alt_final <- case m_bid of + Just (Breakpoint tick_ty tick_id fvs) + | gopt Opt_InsertBreakpoints (hsc_dflags hsc_env) + -- Construct an internal breakpoint to put at the start of this case + -- continuation BCO. + -- See Note [TODO] + -> do + internal_tick_loc <- makeCaseInternalBreakLoc tick_id + + -- same fvs available in the case expression are available in the case continuation + let idOffSets = getVarOffSets platform d p fvs + ty_vars = tyCoVarsOfTypesWellScoped (tick_ty:map idType fvs) + toWord :: Maybe (Id, WordOff) -> Maybe (Id, Word) + toWord = fmap (\(i, wo) -> (i, fromIntegral wo)) + breakInfo = dehydrateCgBreakInfo ty_vars (map toWord idOffSets) tick_ty (Left internal_tick_loc) + + mibi <- newBreakInfo breakInfo + return $ case mibi of + Nothing -> alt_final2 + Just ibi -> BRK_FUN ibi `consOL` alt_final2 + _ -> pure alt_final2 add_bco_name <- shouldAddBcoName let @@ -1357,6 +1382,24 @@ doCase d s p scrut bndr alts _ -> panic "schemeE(StgCase).push_alts" in return (PUSH_ALTS alt_bco scrut_rep `consOL` scrut_code) +makeCaseInternalBreakLoc :: BreakpointId -> BcM InternalBreakLoc +makeCaseInternalBreakLoc bid = do + hug <- hsc_HUG <$> getHscEnv + curr_mod <- getCurrentModule + mb_mod_brks <- getCurrentModBreaks + + -- TODO: Subtract the scrutinee loc from the case loc to get continuation loc + InternalBreakLoc <$> case bid of + BreakpointId{bi_tick_mod, bi_tick_index} + | bi_tick_mod == curr_mod + , Just these_mbs <- mb_mod_brks + -> do + return $ modBreaks_locs these_mbs ! bi_tick_index + | otherwise + -> do + other_mbs <- liftIO $ readIModModBreaks hug bi_tick_mod + return $ modBreaks_locs other_mbs ! bi_tick_index + {- Note [Debugger: BRK_ALTS] ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -2677,14 +2720,19 @@ getLabelsBc n = BcM $ \_ st -> let ctr = nextlabel st in return (coerce [ctr .. ctr+n-1], st{nextlabel = ctr+n}) -newBreakInfo :: CgBreakInfo -> BcM Int -newBreakInfo info = BcM $ \_ st -> - let ix = breakInfoIdx st - st' = st - { breakInfo = IntMap.insert ix info (breakInfo st) - , breakInfoIdx = ix + 1 - } - in return (ix, st') +newBreakInfo :: CgBreakInfo -> BcM (Maybe InternalBreakpointId) +newBreakInfo info = BcM $ \env st -> do + -- if we're not generating ModBreaks for this module for some reason, we + -- can't store breakpoint occurrence information. + case modBreaks env of + Nothing -> pure (Nothing, st) + Just modBreaks -> do + let ix = breakInfoIdx st + st' = st + { breakInfo = IntMap.insert ix info (breakInfo st) + , breakInfoIdx = ix + 1 + } + return (Just $ InternalBreakpointId (modBreaks_module modBreaks) ix, st') getCurrentModule :: BcM Module getCurrentModule = BcM $ \env st -> return (bcm_module env, st) @@ -2697,7 +2745,7 @@ tickFS = fsLit "ticked" -- Dehydrating CgBreakInfo -dehydrateCgBreakInfo :: [TyVar] -> [Maybe (Id, Word)] -> Type -> BreakpointId -> CgBreakInfo +dehydrateCgBreakInfo :: [TyVar] -> [Maybe (Id, Word)] -> Type -> Either InternalBreakLoc BreakpointId -> CgBreakInfo dehydrateCgBreakInfo ty_vars idOffSets tick_ty bid = CgBreakInfo { cgb_tyvars = map toIfaceTvBndr ty_vars ===================================== ghc/GHCi/UI.hs ===================================== @@ -45,7 +45,7 @@ import GHC.Runtime.Eval (mkTopLevEnv) import GHC.Runtime.Eval.Utils -- The GHC interface -import GHC.ByteCode.Breakpoints (imodBreaks_modBreaks, InternalBreakpointId(..), getBreakSourceId) +import GHC.ByteCode.Breakpoints (imodBreaks_modBreaks, InternalBreakpointId(..), getBreakSourceId, getBreakSourceMod) import GHC.Runtime.Interpreter import GHCi.RemoteTypes import GHCi.BreakArray( breakOn, breakOff ) @@ -1621,7 +1621,7 @@ toBreakIdAndLocation (Just inf) = do brks <- liftIO $ readIModBreaks hug inf let bi = getBreakSourceId inf brks return $ listToMaybe [ id_loc | id_loc@(_,loc) <- IntMap.assocs (breaks st), - breakId loc == bi ] + Right (breakId loc) == bi ] printStoppedAtBreakInfo :: GHC.GhcMonad m => Resume -> [Name] -> m () printStoppedAtBreakInfo res names = do @@ -3825,7 +3825,7 @@ pprStopped res = do hug <- hsc_HUG <$> GHC.getSession brks <- liftIO $ readIModBreaks hug ibi return $ Just $ moduleName $ - bi_tick_mod $ getBreakSourceId ibi brks + getBreakSourceMod ibi brks return $ text "Stopped in" <+> ((case mb_mod_name of ===================================== libraries/ghci/GHCi/Run.hs ===================================== @@ -362,6 +362,14 @@ withBreakAction opts breakMVar statusMVar mtid act info_mod_uid <- BS.packCString (Ptr info_mod_uid#) pure (Just (EvalBreakpoint info_mod info_mod_uid (I# infox#))) putMVar statusMVar $ EvalBreak apStack_r breakpoint resume_r ccs + + -- Block until this thread is resumed (by the thread which took the + -- `ResumeContext` from the `statusMVar`). + -- + -- The `onBreak` function must have been called from `rts/Interpreter.c` + -- when interpreting a `BRK_FUN`. After taking from the MVar, the function + -- returns to the continuation on the stack which is where the interpreter + -- was stopped. takeMVar breakMVar resetBreakAction stablePtr = do ===================================== rts/Interpreter.c ===================================== @@ -297,18 +297,17 @@ allocate_NONUPD (Capability *cap, int n_words) return allocate(cap, stg_max(sizeofW(StgHeader)+MIN_PAYLOAD_SIZE, n_words)); } -// STATIC_INLINE int -// is_ctoi_nontuple_frame(const StgClosure* frame) { -// const StgInfoTable* info = frame->header.info; -// return ( -// (W_)info == (W_)&stg_ctoi_R1p_info || -// (W_)info == (W_)&stg_ctoi_R1n_info || -// (W_)info == (W_)&stg_ctoi_F1_info || -// (W_)info == (W_)&stg_ctoi_D1_info || -// (W_)info == (W_)&stg_ctoi_L1_info || -// (W_)info == (W_)&stg_ctoi_V_info -// ); -// } +STATIC_INLINE int +is_ctoi_nontuple_frame(const StgPtr frame_head) { + return ( + (W_)frame_head == (W_)&stg_ctoi_R1p_info || + (W_)frame_head == (W_)&stg_ctoi_R1n_info || + (W_)frame_head == (W_)&stg_ctoi_F1_info || + (W_)frame_head == (W_)&stg_ctoi_D1_info || + (W_)frame_head == (W_)&stg_ctoi_L1_info || + (W_)frame_head == (W_)&stg_ctoi_V_info + ); +} int rts_stop_on_exception = 0; @@ -1264,7 +1263,7 @@ do_return_nonpointer: */ if(SpW(0) == (W_)&stg_ret_t_info) { - cap->r.rCCCS = (CostCentreStack*)ReadSpW(stack_frame_sizeW((StgClosure *)Sp) + 4); + cap->r.rCCCS = (CostCentreStack*)ReadSpW(offset + 4); } #endif /* Drop the RET_BCO header (next_frame), @@ -1571,7 +1570,7 @@ run_BCO: switch (bci & 0xFF) { - /* check for a breakpoint on the beginning of a let binding */ + /* check for a breakpoint on the beginning of a BCO */ case bci_BRK_FUN: { W_ arg1_brk_array, arg2_info_mod_name, arg3_info_mod_id, arg4_info_index; @@ -1624,6 +1623,20 @@ run_BCO: { breakPoints = (StgArrBytes *) BCO_PTR(arg1_brk_array); + W_ stack_head = ReadSpW(0); + + // See Note [Stack layout when entering run_BCO blah] + // When the BRK_FUN is at the start of a case continuation BCO, + // the stack contains the frame returning the value at the start. + int is_case_cont_BCO = + stack_head == (W_)&stg_ret_t_info + || stack_head == (W_)&stg_ret_v_info + || stack_head == (W_)&stg_ret_p_info + || stack_head == (W_)&stg_ret_n_info + || stack_head == (W_)&stg_ret_f_info + || stack_head == (W_)&stg_ret_d_info + || stack_head == (W_)&stg_ret_l_info; + // stop the current thread if either `stop_next_breakpoint` is // true OR if the ignore count for this particular breakpoint is zero StgInt ignore_count = ((StgInt*)breakPoints->payload)[arg4_info_index]; @@ -1632,36 +1645,83 @@ run_BCO: // decrement and write back ignore count ((StgInt*)breakPoints->payload)[arg4_info_index] = --ignore_count; } - else if (stop_next_breakpoint == true || ignore_count == 0) + else if ( + /* Doing :step (but don't stop at case continuation BCOs) */ + (stop_next_breakpoint == true && !is_case_cont_BCO) + /* Or breakpoint is explicitly enabled */ + || ignore_count == 0) { // make sure we don't automatically stop at the // next breakpoint rts_stop_next_breakpoint = 0; cap->r.rCurrentTSO->flags &= ~TSO_STOP_NEXT_BREAKPOINT; - // allocate memory for a new AP_STACK, enough to - // store the top stack frame plus an - // stg_apply_interp_info pointer and a pointer to - // the BCO - size_words = BCO_BITMAP_SIZE(obj) + 2; - new_aps = (StgAP_STACK *) allocate(cap, AP_STACK_sizeW(size_words)); - new_aps->size = size_words; - new_aps->fun = &stg_dummy_ret_closure; - - // fill in the payload of the AP_STACK - new_aps->payload[0] = (StgClosure *)&stg_apply_interp_info; - new_aps->payload[1] = (StgClosure *)obj; - - // copy the contents of the top stack frame into the AP_STACK - for (i = 2; i < size_words; i++) - { - new_aps->payload[i] = (StgClosure *)ReadSpW(i-2); + // TODO: WRITE NOTE + if (is_case_cont_BCO) { + + // TODO: WRITE NOTE + // A case cont. BCO is headed by a ret_frame with the returned value + // We need the frame here if we are going to yield to construct a well formed stack + // Then, just afterwards, we SLIDE the header off. This is generated code (see StgToByteCode) + int size_returned_frame = + (stack_head == (W_)&stg_ret_t_info) + ? 2 /* ret_t + tuple_BCO */ + + /* Sp(2) is call_info which records the offset to the next frame + * See also Note [unboxed tuple bytecodes and tuple_BCO] */ + ((ReadSpW(2) & 0xFF)) + : 2; /* ret_* + return value */ + + StgPtr cont_frame_head + = (StgPtr)(SpW(size_returned_frame)); + ASSERT(obj == UNTAG_CLOSURE((StgClosure*)ReadSpW(size_returned_frame+1))); + + // stg_ctoi_* + int size_cont_frame_head = + is_ctoi_nontuple_frame(cont_frame_head) + ? 2 // info+bco +#if defined(PROFILING) + : 5; // or info+bco+tuple_info+tuple_BCO+CCS +#else + : 4; // or info+bco+tuple_info+tuple_BCO +#endif + + // Continuation stack is already well formed, + // so just copy it whole to the AP_STACK + size_words = size_returned_frame + + size_cont_frame_head + + BCO_BITMAP_SIZE(obj) /* payload of cont_frame */; + new_aps = (StgAP_STACK *) allocate(cap, AP_STACK_sizeW(size_words)); + new_aps->size = size_words; + new_aps->fun = &stg_dummy_ret_closure; + + // (1) Fill in the payload of the AP_STACK: + for (i = 0; i < size_words; i++) { + new_aps->payload[i] = (StgClosure *)ReadSpW(i); + } + } + else { + // (1) Allocate memory for a new AP_STACK, enough to store + // the top stack frame plus an stg_apply_interp_info pointer + // and a pointer to the BCO + size_words = BCO_BITMAP_SIZE(obj) + 2; + new_aps = (StgAP_STACK *) allocate(cap, AP_STACK_sizeW(size_words)); + new_aps->size = size_words; + new_aps->fun = &stg_dummy_ret_closure; + + // (1.1) the continuation frame + new_aps->payload[0] = (StgClosure *)&stg_apply_interp_info; + new_aps->payload[1] = (StgClosure *)obj; + + // (1.2.1) copy the args/free vars of the top stack frame into the AP_STACK + for (i = 2; i < size_words; i++) { + new_aps->payload[i] = (StgClosure *)ReadSpW(i-2); + } } // No write barrier is needed here as this is a new allocation SET_HDR(new_aps,&stg_AP_STACK_info,cap->r.rCCCS); - // Arrange the stack to call the breakpoint IO action, and + // (2) Arrange the stack to call the breakpoint IO action, and // continue execution of this BCO when the IO action returns. // // ioAction :: Addr# -- the breakpoint info module @@ -1674,12 +1734,27 @@ run_BCO: ioAction = (StgClosure *) deRefStablePtr ( rts_breakpoint_io_action); - Sp_subW(13); - SpW(12) = (W_)obj; - SpW(11) = (W_)&stg_apply_interp_info; + // (2.1) Construct the continuation to which we'll return in + // this thread after the `rts_breakpoint_io_action` returns. + // + // For case continuation BCOs, the continuation that re-runs + // it is always ready at the start of the BCO. It gets + // dropped soon after if we don't stop there by SLIDEing. + // See Note [TODO] + if (!is_case_cont_BCO) { + Sp_subW(2); // stg_apply_interp_info + StgBCO* + + // (2.1.2) Write the continuation frame (above the stg_ret + // frame if one exists) + SpW(1) = (W_)obj; + SpW(0) = (W_)&stg_apply_interp_info; + } + + // (2.2) The `rts_breakpoint_io_action` call + Sp_subW(11); SpW(10) = (W_)new_aps; - SpW(9) = (W_)False_closure; // True <=> an exception - SpW(8) = (W_)&stg_ap_ppv_info; + SpW(9) = (W_)False_closure; // True <=> an exception + SpW(8) = (W_)&stg_ap_ppv_info; SpW(7) = (W_)arg4_info_index; SpW(6) = (W_)&stg_ap_n_info; SpW(5) = (W_)BCO_LIT(arg3_info_mod_id); ===================================== testsuite/tests/ghci.debugger/scripts/T26042g.stdout ===================================== @@ -6,10 +6,13 @@ x :: Int = 14 11 succ x = (-) (x - 2) (x + 1) ^^^^^^^^^^^^^^^^^^^ 12 -Stopped in T9.top, T26042g.hs:8:10-21 +Stopped in T9., T26042g.hs:(6,3)-(8,21) _result :: Int = _ +5 top = do + vv +6 case succ 14 of 7 5 -> 5 8 _ -> 6 + other 55 - ^^^^^^^^^^^^ + ^^ 9 171 ===================================== testsuite/tests/ghci.debugger/scripts/all.T ===================================== @@ -147,7 +147,7 @@ test('T25932', extra_files(['T25932.hs']), ghci_script, ['T25932.script']) # Step out tests test('T26042b', [extra_hc_opts('-O -fno-unoptimized-core-for-interpreter'), extra_files(['T26042b.hs'])], ghci_script, ['T26042b.script']) -test('T26042c', [expect_broken(26042),extra_hc_opts('-O -fno-unoptimized-core-for-interpreter'), extra_files(['T26042c.hs'])], ghci_script, ['T26042c.script']) +test('T26042c', [extra_hc_opts('-O -fno-unoptimized-core-for-interpreter'), extra_files(['T26042c.hs'])], ghci_script, ['T26042c.script']) test('T26042d', [extra_hc_opts('-O -fno-unoptimized-core-for-interpreter'), extra_files(['T26042d.hs'])], ghci_script, ['T26042d.script']) test('T26042e', extra_files(['T26042e.hs']), ghci_script, ['T26042e.script']) test('T26042f1', extra_files(['T26042f.hs', 'T26042f.script']), ghci_script, ['T26042f.script']) # >> is not inlined, so stepout has nowhere to stop View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/67e50cb9f350969d0c769d76ee6765f2... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/67e50cb9f350969d0c769d76ee6765f2... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Rodrigo Mesquita (@alt-romes)