1 -----------------------------------------------------------------------------
3 -- GHCi Interactive debugging commands
5 -- Pepe Iborra (supported by Google SoC) 2006
7 -- ToDo: lots of violation of layering here. This module should
8 -- decide whether it is above the GHC API (import GHC and nothing
11 -----------------------------------------------------------------------------
13 module Debugger (pprintClosureCommand) where
15 import qualified DebuggerTys
17 import RtClosureInspect
23 import Var hiding ( varName )
37 import Pretty ( Mode(..), showDocWith )
41 import Control.Exception
50 #include "HsVersions.h"
52 -------------------------------------
53 -- | The :print & friends commands
54 -------------------------------------
55 pprintClosureCommand :: Bool -> Bool -> String -> GHCi ()
56 pprintClosureCommand bindThings force str = do
58 newvarsNames <- io$ do
59 uniques <- liftM uniqsFromSupply (mkSplitUniqSupply 'q')
60 return$ map (\u-> (mkSysTvName u (mkFastString "a"))) uniques
61 mb_ids <- io$ mapM (cleanUp cms newvarsNames) (words str)
62 mb_new_ids <- mapM (io . go cms) (catMaybes mb_ids)
63 io$ updateIds cms (catMaybes mb_new_ids)
65 -- Find the Id, clean up 'Unknowns'
66 cleanUp :: Session -> [Name] -> String -> IO (Maybe Id)
67 cleanUp cms newNames str = do
68 tythings <- GHC.parseName cms str >>= mapM (GHC.lookupName cms)
69 return$ listToMaybe (map (stripUnknowns newNames)
70 [ i | Just (AnId i) <- tythings])
72 -- Do the obtainTerm--bindSuspensions-refineIdType dance
73 -- Warning! This function got a good deal of side-effects
74 go :: Session -> Id -> IO (Maybe Id)
76 mb_term <- obtainTerm cms force id
77 maybe (return Nothing) `flip` mb_term $ \term -> do
78 term' <- if not bindThings then return term
79 else bindSuspensions cms term
80 showterm <- pprTerm cms term'
81 unqual <- GHC.getPrintUnqual cms
82 let showSDocForUserOneLine unqual doc =
83 showDocWith LeftMode (doc (mkErrStyle unqual))
84 (putStrLn . showSDocForUserOneLine unqual) (ppr id <+> char '=' <+> showterm)
85 -- Before leaving, we compare the type obtained to see if it's more specific
86 -- Note how we need the Unknown-clear type returned by obtainTerm
87 let Just reconstructedType = termType term
88 new_type <- instantiateTyVarsToUnknown cms
89 (mostSpecificType (idType id) reconstructedType)
90 return . Just $ setIdType id new_type
92 updateIds :: Session -> [Id] -> IO ()
93 updateIds (Session ref) new_ids = do
94 hsc_env <- readIORef ref
95 let ictxt = hsc_IC hsc_env
96 type_env = ic_type_env ictxt
97 filtered_type_env = delListFromNameEnv type_env (map idName new_ids)
98 new_type_env = extendTypeEnvWithIds filtered_type_env new_ids
99 new_ic = ictxt {ic_type_env = new_type_env }
100 writeIORef ref (hsc_env {hsc_IC = new_ic })
102 isMoreSpecificThan :: Type -> Type -> Bool
103 ty `isMoreSpecificThan` ty1
104 | Just subst <- tcUnifyTys bindOnlyTy1 [repType' ty] [repType' ty1]
105 , substFiltered <- filter (not.isTyVarTy) . varEnvElts . getTvSubstEnv $ subst
106 , not . null $ substFiltered
107 , all (flip notElemTvSubst subst) ty_vars
110 where bindOnlyTy1 tyv | tyv `elem` ty_vars = AvoidMe
112 ty_vars = varSetElems$ tyVarsOfType ty
114 mostSpecificType ty1 ty2 | ty1 `isMoreSpecificThan` ty2 = ty1
117 -- | Give names, and bind in the interactive environment, to all the suspensions
118 -- included (inductively) in a term
119 bindSuspensions :: Session -> Term -> IO Term
120 bindSuspensions cms@(Session ref) t = do
121 hsc_env <- readIORef ref
122 inScope <- GHC.getBindings cms
123 let ictxt = hsc_IC hsc_env
124 rn_env = ic_rn_local_env ictxt
125 type_env = ic_type_env ictxt
127 alreadyUsedNames = map (occNameString . nameOccName . getName) inScope
128 availNames = map ((prefix++) . show) [1..] \\ alreadyUsedNames
129 availNames_var <- newIORef availNames
130 (t', stuff) <- foldTerm (nameSuspensionsAndGetInfos availNames_var) t
131 let (names, tys, hvals) = unzip3 stuff
132 concrete_tys <- mapM (instantiateTyVarsToUnknown cms) tys
133 let ids = [ mkGlobalId VanillaGlobal name ty vanillaIdInfo
134 | (name,ty) <- zip names concrete_tys]
135 new_type_env = extendTypeEnvWithIds type_env ids
136 new_rn_env = extendLocalRdrEnv rn_env names
137 new_ic = ictxt { ic_rn_local_env = new_rn_env,
138 ic_type_env = new_type_env }
139 extendLinkEnv (zip names hvals)
140 writeIORef ref (hsc_env {hsc_IC = new_ic })
144 -- Processing suspensions. Give names and recopilate info
145 nameSuspensionsAndGetInfos :: IORef [String] -> TermFold (IO (Term, [(Name,Type,HValue)]))
146 nameSuspensionsAndGetInfos freeNames = TermFold
148 fSuspension = doSuspension freeNames
149 , fTerm = \ty dc v tt -> do
151 let (terms,names) = unzip tt'
152 return (Term ty dc v terms, concat names)
153 , fPrim = \ty n ->return (Prim ty n,[])
155 doSuspension freeNames ct mb_ty hval Nothing = do
156 name <- atomicModifyIORef freeNames (\x->(tail x, head x))
157 n <- newGrimName cms name
158 let ty' = fromMaybe (error "unexpected") mb_ty
159 return (Suspension ct mb_ty hval (Just n), [(n,ty',hval)])
162 -- A custom Term printer to enable the use of Show instances
163 pprTerm cms@(Session ref) = customPrintTerm customPrint
165 customPrint = \p-> customPrintShowable : customPrintTermBase p
166 customPrintShowable t@Term{ty=ty, dc=dc, val=val} = do
167 let hasType = isEmptyVarSet (tyVarsOfType ty) -- redundant
168 isEvaled = isFullyEvaluatedTerm t
169 if not isEvaled -- || not hasType
172 hsc_env <- readIORef ref
173 dflags <- GHC.getSessionDynFlags cms
175 (new_env, bname) <- bindToFreshName hsc_env ty "showme"
176 writeIORef ref (new_env)
177 let noop_log _ _ _ _ = return ()
178 expr = "show " ++ showSDoc (ppr bname)
179 GHC.setSessionDynFlags cms dflags{log_action=noop_log}
180 mb_txt <- withExtendedLinkEnv [(bname, val)]
181 (GHC.compileExpr cms expr)
183 Just txt -> return . Just . text . unsafeCoerce# $ txt
184 Nothing -> return Nothing
186 writeIORef ref hsc_env
187 GHC.setSessionDynFlags cms dflags
189 bindToFreshName hsc_env ty userName = do
190 name <- newGrimName cms userName
191 let ictxt = hsc_IC hsc_env
192 rn_env = ic_rn_local_env ictxt
193 type_env = ic_type_env ictxt
194 id = mkGlobalId VanillaGlobal name ty vanillaIdInfo
195 new_type_env = extendTypeEnv type_env (AnId id)
196 new_rn_env = extendLocalRdrEnv rn_env [name]
197 new_ic = ictxt { ic_rn_local_env = new_rn_env,
198 ic_type_env = new_type_env }
199 return (hsc_env {hsc_IC = new_ic }, name)
201 -- Create new uniques and give them sequentially numbered names
202 -- newGrimName :: Session -> String -> IO Name
203 newGrimName cms userName = do
204 us <- mkSplitUniqSupply 'b'
205 let unique = uniqFromSupply us
206 occname = mkOccName varName userName
207 name = mkInternalName unique occname noSrcLoc
210 -- | The inverse function. Strip the GHC.Base.Unknowns in the type of the id, they correspond to tyvars. The caller must provide an infinite list of fresh names
211 stripUnknowns :: [Name] -> Id -> Id
212 stripUnknowns names id = setIdType id . fst . go names . idType
215 go tyvarsNames@(v:vv) ty
216 | Just (ty1,ty2) <- splitFunTy_maybe ty = let
217 (ty1',vv') = go tyvarsNames ty1
218 (ty2',vv'')= go vv' ty2
219 in (mkFunTy ty1' ty2', vv'')
220 | Just (ty1,ty2) <- splitAppTy_maybe ty = let
221 (ty1',vv') = go tyvarsNames ty1
222 (ty2',vv'')= go vv' ty2
223 in (mkAppTy ty1' ty2', vv'')
224 | Just (tycon, args) <- splitTyConApp_maybe ty
225 , Just (tycon', vv') <- (fixTycon tycon tyvarsNames)
226 , (args',vv'') <- foldr (\arg (aa,vv) -> let (arg',vv') = go vv' arg
229 = (mkAppTys tycon' args',vv'')
230 | Just (tycon, args) <- splitTyConApp_maybe ty
231 , (args',vv') <- foldr (\arg (aa,vv) -> let (arg',vv') = go vv' arg
233 ([],tyvarsNames) args
234 = (mkTyConApp tycon args',vv')
235 | otherwise = (ty, tyvarsNames)
236 where fixTycon tycon (v:vv) = do
237 k <- lookup (tyConName tycon) kinds
238 return (mkTyVarTy$ mkTyVar v k, vv)
239 kinds = [ (unknownTyConName, liftedTypeKind)
240 , (unknown1TyConName, kind1)
241 , (unknown2TyConName, kind2)
242 , (unknown3TyConName, kind3)]
243 kind1 = mkArrowKind liftedTypeKind liftedTypeKind
244 kind2 = mkArrowKind kind1 liftedTypeKind
245 kind3 = mkArrowKind kind2 liftedTypeKind
247 instantiateTyVarsToUnknown :: Session -> Type -> IO Type
248 instantiateTyVarsToUnknown (Session ref) ty
249 = do hsc_env <- readIORef ref
250 DebuggerTys.instantiateTyVarsToUnknown hsc_env ty