Rodrigo Mesquita pushed to branch wip/romes/multi-threaded-debugger at Glasgow Haskell Compiler / GHC Commits: 94dd257d by Rodrigo Mesquita at 2026-10-21T12:01:14+01:00 fixes api use, kill uninterruptible mask - - - - - a64e1dbd by Rodrigo Mesquita at 2026-10-21T14:42:21+01:00 Kill redundant custom handler in GHCi.Server - - - - - e5af56d6 by Rodrigo Mesquita at 2026-10-21T14:56:07+01:00 Revert "Kill redundant custom handler in GHCi.Server" This reverts commit a64e1dbdc9f7a5d46a33c3bbafa69ffa49aee1ec. - - - - - 6e60751b by Rodrigo Mesquita at 2026-10-21T14:57:04+01:00 clean up some comments in GHCi.Signals - - - - - ad9b4a5b by Rodrigo Mesquita at 2026-10-21T17:19:46+01:00 TSO_STOP_ON_EXCEPTION -- per-thread break-on-exception - - - - - 564c6478 by Rodrigo Mesquita at 2026-10-21T17:33:49+01:00 Add redirectInterrupts again - - - - - b72f637f by Rodrigo Mesquita at 2026-10-21T17:35:27+01:00 unmask runIt only - - - - - d2f9d0c4 by Rodrigo Mesquita at 2026-10-21T17:36:45+01:00 fixup! TSO_STOP_ON_EXCEPTION -- per-thread break-on-exception - - - - - 1ea6bdc4 by Rodrigo Mesquita at 2026-10-21T17:37:52+01:00 fixup! TSO_STOP_ON_EXCEPTION -- per-thread break-on-exception - - - - - 5eb82a91 by Rodrigo Mesquita at 2026-10-21T17:50:26+01:00 Revert "Add redirectInterrupts again" This reverts commit 564c64781849dd3e681b68ae6b167779403118a6. - - - - - 978769c6 by Rodrigo Mesquita at 2026-10-21T18:00:13+01:00 Move cmmtn - - - - - 61f3a6fa by Rodrigo Mesquita at 2026-10-22T10:45:42+01:00 make T3171 not fragile (wait for started) and test ext interp too testing ext interp requires sending ^C to whole process group - - - - - 17 changed files: - compiler/GHC/Runtime/Interpreter.hs - libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc - libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingEnabled.hsc - libraries/ghc-heap/tests/parse_tso_flags.hs - libraries/ghc-internal/src/GHC/Internal/Heap/Closures.hs - libraries/ghci/GHCi/Debugger.hs - libraries/ghci/GHCi/Run.hs - libraries/ghci/GHCi/Signals.hs - rts/Exception.cmm - rts/Interpreter.c - rts/RtsSymbols.c - rts/include/rts/Constants.h - testsuite/tests/ghc-api/T8628.hs - testsuite/tests/ghc-api/T8639_api.hs - testsuite/tests/ghc-api/apirecomp001/myghc.hs - testsuite/tests/ghci/should_run/Makefile - testsuite/tests/ghci/should_run/all.T Changes: ===================================== compiler/GHC/Runtime/Interpreter.hs ===================================== @@ -494,11 +494,9 @@ lookupSymbol interp str = withSymbolCache interp str $ #endif ExternalInterp ext -> case ext of ExtIServ i -> withIServ i $ \inst -> do - uninterruptibleMask_ $ sendMessage inst (LookupSymbol (fastStringToShortByteString (interpSymbolToCLabel str))) ExtJS {} -> pprPanic "lookupSymbol not supported by the JS interpreter" (ppr str) ExtWasm i -> withWasmInterp i $ \inst -> do - uninterruptibleMask_ $ sendMessage inst (LookupSymbol (fastStringToShortByteString (interpSymbolToCLabel str))) lookupSymbolInDLL :: Interp -> RemotePtr LoadedDLL -> InterpSymbol s -> IO (Maybe (RemotePtr ())) @@ -509,8 +507,7 @@ lookupSymbolInDLL interp dll str = withSymbolCache interp str $ #endif ExternalInterp ext -> case ext of ExtIServ i -> withIServ i $ \inst -> do - uninterruptibleMask_ $ - sendMessage inst (LookupSymbolInDLL dll (fastStringToShortByteString (interpSymbolToCLabel str))) + sendMessage inst (LookupSymbolInDLL dll (fastStringToShortByteString (interpSymbolToCLabel str))) ExtJS {} -> pprPanic "lookupSymbol not supported by the JS interpreter" (ppr str) -- wasm dyld doesn't track which symbol comes from which .so ExtWasm {} -> lookupSymbol interp str ===================================== libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc ===================================== @@ -93,6 +93,7 @@ parseTsoFlags w | isSet (#const TSO_LOCKED) w = TsoLocked : parseTsoFlags (unset #if __GLASGOW_HASKELL__ >= 913 | isSet (#const TSO_STOP_NEXT_BREAKPOINT) w = TsoStopNextBreakpoint : parseTsoFlags (unset (#const TSO_STOP_NEXT_BREAKPOINT) w) | isSet (#const TSO_STOP_AFTER_RETURN) w = TsoStopAfterReturn : parseTsoFlags (unset (#const TSO_STOP_AFTER_RETURN) w) + | isSet (#const TSO_STOP_ON_EXCEPTION) w = TsoStopOnException : parseTsoFlags (unset (#const TSO_STOP_ON_EXCEPTION) w) #endif parseTsoFlags 0 = [] parseTsoFlags w = [TsoFlagsUnknownValue w] ===================================== libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingEnabled.hsc ===================================== @@ -93,6 +93,7 @@ parseTsoFlags w | isSet (#const TSO_LOCKED) w = TsoLocked : parseTsoFlags (unset #if __GLASGOW_HASKELL__ >= 913 | isSet (#const TSO_STOP_NEXT_BREAKPOINT) w = TsoStopNextBreakpoint : parseTsoFlags (unset (#const TSO_STOP_NEXT_BREAKPOINT) w) | isSet (#const TSO_STOP_AFTER_RETURN) w = TsoStopAfterReturn : parseTsoFlags (unset (#const TSO_STOP_AFTER_RETURN) w) + | isSet (#const TSO_STOP_ON_EXCEPTION) w = TsoStopOnException : parseTsoFlags (unset (#const TSO_STOP_ON_EXCEPTION) w) #endif parseTsoFlags 0 = [] parseTsoFlags w = [TsoFlagsUnknownValue w] ===================================== libraries/ghc-heap/tests/parse_tso_flags.hs ===================================== @@ -15,5 +15,6 @@ main = do assertEqual (parseTsoFlags 256) [TsoAllocLimit] assertEqual (parseTsoFlags 512) [TsoStopNextBreakpoint] assertEqual (parseTsoFlags 1024) [TsoStopAfterReturn] + assertEqual (parseTsoFlags 2048) [TsoStopOnException] assertEqual (parseTsoFlags 6) [TsoLocked, TsoBlockx] ===================================== libraries/ghc-internal/src/GHC/Internal/Heap/Closures.hs ===================================== @@ -632,6 +632,7 @@ data TsoFlags | TsoAllocLimit | TsoStopNextBreakpoint | TsoStopAfterReturn + | TsoStopOnException | TsoFlagsUnknownValue Word32 -- ^ Please report this as a bug deriving (Eq, Show, Generic, Ord) ===================================== libraries/ghci/GHCi/Debugger.hs ===================================== @@ -13,7 +13,10 @@ module GHCi.Debugger , rts_disableStopAfterReturn -- * Stop on exception - , exceptionFlag + , rts_enableStopOnException + , rts_disableStopOnException + , rts_enableStopOnExceptionAll + , rts_disableStopOnExceptionAll -- * Breakpoint Callback , BreakpointCallback @@ -23,7 +26,6 @@ module GHCi.Debugger import Prelude -- See note [Why do we import Prelude here?] import GHC.Base (ThreadId#, Addr#, Int#) -import Foreign.C (CInt) import Foreign (StablePtr, Ptr) import GHCi.RemoteTypes (HValue) @@ -60,7 +62,19 @@ foreign import ccall unsafe "rts_disableStopAfterReturn" -------------------------------------------------------------------------------- -foreign import ccall "&rts_stop_on_exception" exceptionFlag :: Ptr CInt +-- | Make this thread break when an exception is hit +foreign import ccall unsafe "rts_enableStopOnException" rts_enableStopOnException :: ThreadId# -> IO () + +-- | No longer have this thread break when an exception is hit +foreign import ccall unsafe "rts_disableStopOnException" rts_disableStopOnException :: ThreadId# -> IO () + +-- | Enables global break-on-exception for all threads +foreign import ccall unsafe "rts_enableStopOnExceptionAll" rts_enableStopOnExceptionAll :: IO () + +-- | Disables the global break-on-exception for all threads. Individual threads +-- with break-on-exception will still break on exception if the global flag is +-- disabled. +foreign import ccall unsafe "rts_disableStopOnExceptionAll" rts_disableStopOnExceptionAll :: IO () -------------------------------------------------------------------------------- ===================================== libraries/ghci/GHCi/Run.hs ===================================== @@ -338,9 +338,12 @@ sandboxIO opts io -- A solution would be to have an "initialize interpreter" command -- which sets this at the start. A different solution is to have a -- global lock and only write this once here, then lock. + -- poke breakPointIOAction globalBreakStablePtr + -- freeStablePtr stablePtr TODO: we never free the global stablePtr for + -- global action. Maybe we should have a de-init for clean up. - tid <- forkIO $ do + tid <- mask_ $ forkIOWithUnmask $ \unmask -> do takeMVar wait -- don't run anything in this thread before -- `setBreakAction` has a chance to set up. @@ -351,7 +354,7 @@ sandboxIO opts io -- We only write the thread status map in two places: -- * When the "main" thread finishes (here) -- * When any thread hits a breakpoint (global break action) - runIt >>= writeThreadEvalStatus tid + unmask runIt >>= writeThreadEvalStatus tid setBreakAction opts tid putMVar wait () @@ -393,14 +396,15 @@ sandboxIO opts io catchNoPropagate io $ \(ExceptionWithContext cx se) -> do -- If -fbreak-on-error, we break unconditionally, -- but with care of not breaking twice + ThreadId mytid <- myThreadId if breakOnError && not breakOnException - then poke exceptionFlag 1 + then rts_enableStopOnException mytid else case fromException se of -- If it is a "UserInterrupt" exception, we allow -- a possible break by way of -fbreak-on-exception Just UserInterrupt -> return () -- In any other case, we don't want to break - _ -> poke exceptionFlag 0 + _ -> rts_disableStopOnException mytid rethrowIO (ExceptionWithContext cx se) measureAlloc :: IO (EvalResult a) -> IO (EvalStatus a) @@ -420,25 +424,18 @@ setBreakAction opts tid@(ThreadId tid#) = do setBreakAction' ctx where setBreakAction' ctx = do - -- TODO: TERRIBLY NOT MULTI-THREAD FRIENDLY. Each thread needs its own RTS flag exceptionFlag, like we have for step-in or step-out. - runIf breakOnException $ \_ -> poke exceptionFlag 1 - -- Breaking on exceptions is not enabled by default, since it - -- might be a bit surprising. The exception flag is turned off - -- as soon as it is hit, or in resetBreakAction below. runIf# singleStep rts_enableStopNextBreakpoint runIf# stepOut rts_enableStopAfterReturn + -- Breaking on exceptions is not enabled by default, since it might be a + -- bit surprising. The exception flag is turned off as soon as it is hit. + runIf# breakOnException rts_enableStopOnException + if isolateThreadBreaks opts then setIsolatedCtx ctx else unsetIsolatedCtx ctx - -- freeStablePtr stablePtr TODO: we never free the global stablePtr for - -- global action. Maybe we should have a de-init for clean up. - -- TODO: don't set it in `setBreakAction` either. Should be on - -- interpreter initialization. - - runIf pred what = when (pred opts) (what tid) runIf# pred what = when (pred opts) (what tid#) -- While we're waiting for the sandbox thread to return a result, if ===================================== libraries/ghci/GHCi/Signals.hs ===================================== @@ -17,9 +17,7 @@ import GHC.ConsoleHandler #endif -- | Install standard signal handlers for catching ^C, which just throw an --- exception in the target thread. The current target thread is the --- thread at the head of the list in the MVar passed to --- installSignalHandlers. +-- exception in the target thread. #if defined(wasm32_HOST_ARCH) installSignalHandlers :: (JSVal -> IO ()) -> IO () installSignalHandlers cb_sig = do @@ -62,9 +60,7 @@ installSignalHandlers = do -- -- For native ghc with -fexternal-interpreter, when you press ^C, -- SIGINT is delivered to both ghc/iserv. iserv SIGINT handler raises --- an async UserInterrupt exception in the main thread, which might --- handle it right away, or later since it's doing I/O under --- uninterruptibleMask. +-- an async UserInterrupt exception in the main thread -- -- wasm is no different here. The node process needs to catch the same -- signals. Instead of calling process.on('SIGINT', handler) in ===================================== rts/Exception.cmm ===================================== @@ -523,14 +523,16 @@ retry_pop_stack: if (W_[no_break_on_exception] != 0) { W_[no_break_on_exception] = 0; } else { - if (TO_W_(CInt[rts_stop_on_exception]) != 0) { + if (TO_W_(CInt[rts_stop_on_exception]) != 0 || + (TO_W_(StgTSO_flags(CurrentTSO)) & TSO_STOP_ON_EXCEPTION) != 0) { W_ ioAction; // we don't want any further exceptions to be caught, // until GHCi is ready to handle them. This prevents // deadlock if an exception is raised in InteractiveUI, - // for example. Perhaps the stop_on_exception flag should - // be per-thread. + // for example. CInt[rts_stop_on_exception] = 0; + StgTSO_flags(CurrentTSO) = %lobits32( + TO_W_(StgTSO_flags(CurrentTSO)) & ~TSO_STOP_ON_EXCEPTION); ("ptr" ioAction) = ccall deRefStablePtr (W_[rts_breakpoint_io_action] "ptr"); Sp = Sp - WDS(11); Sp(10) = exception; ===================================== rts/Interpreter.c ===================================== @@ -388,8 +388,6 @@ is_ret_bco_frame(const StgPtr frame_head) { ); } -int rts_stop_on_exception = 0; - /* --------------------------------------------------------------------------- * Enabling and disabling global single step mode * ------------------------------------------------------------------------ */ @@ -473,7 +471,40 @@ A limitation of this approach is that stepping-out of a function that was tail-called will skip its caller since no stack frame is pushed for a tail call (i.e. a tail call returns directly to its caller's first non-tail caller). */ -/* -------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------- + * Enabling and disabling global break-on-exception + * ------------------------------------------------------------------------ */ + +int rts_stop_on_exception = 0; + +void rts_enableStopOnExceptionAll(void) +{ + rts_stop_on_exception = 1; +} + +void rts_disableStopOnExceptionAll(void) +{ + rts_stop_on_exception = 0; +} + +/* --------------------------------------------------------------------------- + * Enabling and disabling per-thread break-on-exception + * ------------------------------------------------------------------------ */ + +void rts_enableStopOnException(StgTSO* tso) +{ + Capability* cap = rts_unsafeGetMyCapability(); + setThreadFlag(cap, tso, TSO_STOP_ON_EXCEPTION); +} + +void rts_disableStopOnException(StgTSO* tso) +{ + Capability* cap = rts_unsafeGetMyCapability(); + unsetThreadFlag(cap, tso, TSO_STOP_ON_EXCEPTION); +} + +/* ------------------------------------------------------------------------ */ #if defined(INTERP_STATS) ===================================== rts/RtsSymbols.c ===================================== @@ -944,6 +944,10 @@ extern char **environ; SymI_NeedsProto(rts_disableStopNextBreakpoint) \ SymI_NeedsProto(rts_enableStopAfterReturn) \ SymI_NeedsProto(rts_disableStopAfterReturn) \ + SymI_NeedsProto(rts_enableStopOnException) \ + SymI_NeedsProto(rts_disableStopOnException) \ + SymI_NeedsProto(rts_enableStopOnExceptionAll) \ + SymI_NeedsProto(rts_disableStopOnExceptionAll) \ SymI_HasProto(stopTimer) \ SymI_HasDataProto(n_capabilities) \ SymI_HasDataProto(max_n_capabilities) \ ===================================== rts/include/rts/Constants.h ===================================== @@ -394,6 +394,11 @@ */ #define TSO_STOP_AFTER_RETURN 1024 +/* + * Enables break-on-exception mode for this thread + */ +#define TSO_STOP_ON_EXCEPTION 2048 + /* * The number of times we spin in a spin lock before yielding (see * #3758). To tune this value, use the benchmark in #3758: run the ===================================== testsuite/tests/ghc-api/T8628.hs ===================================== @@ -1,5 +1,6 @@ module Main where +import Control.Monad (join) import System.IO import GHC.Driver.Session import GHC @@ -25,10 +26,10 @@ main setContext [ IIDecl (simpleImportDecl pRELUDE_NAME) , IIDecl (simpleImportDecl (mkModuleNameFS (fsLit "System.IO")))] runDecls "data X = Y ()" - execStmt "print True" execOptions - MC.try $ execStmt "print (Y ())" execOptions :: GhcMonad m => m (Either SomeException ExecResult) + join $ execStmt "print True" execOptions + MC.try $ join $ execStmt "print (Y ())" execOptions :: GhcMonad m => m (Either SomeException ExecResult) runDecls "data X = Y () deriving Show" _ <- dynCompileExpr "'x'" - execStmt "print (Y ())" execOptions - execStmt "System.IO.hFlush System.IO.stdout" execOptions + join $ execStmt "print (Y ())" execOptions + join $ execStmt "System.IO.hFlush System.IO.stdout" execOptions print "done" ===================================== testsuite/tests/ghc-api/T8639_api.hs ===================================== @@ -1,5 +1,6 @@ module Main where +import Control.Monad (join) import GHC import GHC.Driver.Monad import GHC.Driver.Ppr @@ -20,8 +21,8 @@ main -- With the next line, you get an "Not in scope" exception. -- If you comment out this runStmt, it runs without error and prints the type. - execStmt "putStrLn (show 3)" execOptions - execStmt "hFlush stdout" execOptions + join $ execStmt "putStrLn (show 3)" execOptions + join $ execStmt "hFlush stdout" execOptions ty <- exprType TM_Inst "T8639_api_a.it" liftIO (putStrLn (showPpr flags ty)) ===================================== testsuite/tests/ghc-api/apirecomp001/myghc.hs ===================================== @@ -9,6 +9,7 @@ -- argument. module Main where +import Control.Monad (join) import GHC import GHC.Driver.Session import GHC.Driver.Backend @@ -50,7 +51,7 @@ main = do setContext [IIModule mod] liftIO $ hFlush stdout -- make sure things above are printed before -- interactive output - r <- execStmt "main" execOptions + r <- join $ execStmt "main" execOptions case r of ExecComplete { execResult = Right _ } -> prn "ok" ExecComplete { execResult = Left _ } -> prn "exception" ===================================== testsuite/tests/ghci/should_run/Makefile ===================================== @@ -3,10 +3,15 @@ include $(TOP)/mk/boilerplate.mk include $(TOP)/mk/test.mk # Test that threadDelay can be interrupted by ^C. -T3171: - echo "do Control.Concurrent.threadDelay 3000000; putStrLn \"threadDelay was not interrupted\"" | \ - "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) & \ - sleep 2; kill -INT $$!; wait +# Wait for the statement to start before interrupting (#15383), and signal +# the external interpreter too (the `pgrep -P $!`), as ^C in a terminal would. +T3171ext: T3171_OPTS = -fexternal-interpreter +T3171 T3171ext: + rm -f $@.started + echo 'do { writeFile "$@.started" ""; Control.Concurrent.threadDelay 100000000; putStrLn "threadDelay was not interrupted" }' | \ + "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) $(T3171_OPTS) & \ + while [ ! -e $@.started ] && kill -0 $$! 2>/dev/null; do sleep 0.1; done; \ + kill -INT $$! $$(pgrep -P $$!); wait TopEnvIface: "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) TopEnvIface -v1 -fno-hide-source-paths -e "a" -e ":q" -fwrite-if-simplified-core -fwrite-interface ===================================== testsuite/tests/ghci/should_run/all.T ===================================== @@ -12,11 +12,17 @@ test('T27287', [just_ghci, combined_output], makefile_test, []) test('T3171', [when(opsys('mingw32'),skip), - fragile(15383), req_interp, combined_output], makefile_test, []) +test('T3171ext', + [when(opsys('mingw32'),skip), + req_interp, + unless(config.have_ext_interp, skip), + combined_output], + makefile_test, []) + test('ghcirun004', just_ghci, compile_and_run, ['']) test('T8377', just_ghci, compile_and_run, ['']) test('T9914', just_ghci, ghci_script, ['T9914.script']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/2f52e43f51615541f7b4a05bb5ea697... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/2f52e43f51615541f7b4a05bb5ea697... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help