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
16 import RtClosureInspect
21 import Var hiding ( varName )
32 import Pretty ( Mode(..), showDocWith )
36 import Control.Exception
45 #include "HsVersions.h"
47 -------------------------------------
48 -- | The :print & friends commands
49 -------------------------------------
50 pprintClosureCommand :: Session -> Bool -> Bool -> String -> IO ()
51 pprintClosureCommand session bindThings force str = do
52 tythings <- (catMaybes . concat) `liftM`
53 mapM (\w -> GHC.parseName session w >>=
54 mapM (GHC.lookupName session))
56 substs <- catMaybes `liftM` mapM (go session)
57 [id | AnId id <- tythings]
58 mapM (applySubstToEnv session . skolemSubst) substs
62 -- Do the obtainTerm--bindSuspensions-refineIdType dance
63 -- Warning! This function got a good deal of side-effects
64 go :: Session -> Id -> IO (Maybe TvSubst)
66 mb_term <- obtainTerm cms force id
67 maybe (return Nothing) `flip` mb_term $ \term -> do
68 term' <- if not bindThings then return term
69 else bindSuspensions cms term
70 showterm <- printTerm cms term'
71 unqual <- GHC.getPrintUnqual cms
72 let showSDocForUserOneLine unqual doc =
73 showDocWith LeftMode (doc (mkErrStyle unqual))
74 (putStrLn . showSDocForUserOneLine unqual) (ppr id <+> char '=' <+> showterm)
75 -- Before leaving, we compare the type obtained to see if it's more specific
76 -- Then, we extract a substitution,
77 -- mapping the old tyvars to the reconstructed types.
78 let Just reconstructed_type = termType term
79 -- tcUnifyTys doesn't look through forall's, so we drop them from
80 -- the original type, instead of sigma-typing the reconstructed type
81 mb_subst = tcUnifyTys (const BindMe) [dropForAlls$ idType id]
83 ASSERT2 (isJust mb_subst, ppr reconstructed_type $$ (ppr$ idType id))
86 applySubstToEnv :: Session -> TvSubst -> IO ()
87 applySubstToEnv cms subst | isEmptyTvSubst subst = return ()
88 applySubstToEnv cms@(Session ref) subst = do
89 hsc_env <- readIORef ref
90 inScope <- GHC.getBindings cms
91 let ictxt = hsc_IC hsc_env
92 type_env = ic_type_env ictxt
93 ids = typeEnvIds type_env
94 ids' = map (\id -> id `setIdType` substTy subst (idType id)) ids
95 type_env'= extendTypeEnvWithIds type_env ids'
96 ictxt' = ictxt { ic_type_env = type_env' }
97 writeIORef ref (hsc_env {hsc_IC = ictxt'})
99 -- | Give names, and bind in the interactive environment, to all the suspensions
100 -- included (inductively) in a term
101 bindSuspensions :: Session -> Term -> IO Term
102 bindSuspensions cms@(Session ref) t = do
103 hsc_env <- readIORef ref
104 inScope <- GHC.getBindings cms
105 let ictxt = hsc_IC hsc_env
106 type_env = ic_type_env ictxt
108 alreadyUsedNames = map (occNameString . nameOccName . getName) inScope
109 availNames = map ((prefix++) . show) [1..] \\ alreadyUsedNames
110 availNames_var <- newIORef availNames
111 (t', stuff) <- foldTerm (nameSuspensionsAndGetInfos availNames_var) t
112 let (names, tys, hvals) = unzip3 stuff
113 let tys' = map mk_skol_ty tys
114 let ids = [ mkGlobalId VanillaGlobal name ty vanillaIdInfo
115 | (name,ty) <- zip names tys']
116 new_tyvars = tyVarsOfTypes tys'
117 new_type_env = extendTypeEnvWithIds type_env ids
118 old_tyvars = ic_tyvars ictxt
119 new_ic = ictxt { ic_type_env = new_type_env,
120 ic_tyvars = old_tyvars `unionVarSet` new_tyvars }
121 extendLinkEnv (zip names hvals)
122 writeIORef ref (hsc_env {hsc_IC = new_ic })
126 -- Processing suspensions. Give names and recopilate info
127 nameSuspensionsAndGetInfos :: IORef [String] -> TermFold (IO (Term, [(Name,Type,HValue)]))
128 nameSuspensionsAndGetInfos freeNames = TermFold
130 fSuspension = doSuspension freeNames
131 , fTerm = \ty dc v tt -> do
133 let (terms,names) = unzip tt'
134 return (Term ty dc v terms, concat names)
135 , fPrim = \ty n ->return (Prim ty n,[])
137 doSuspension freeNames ct mb_ty hval Nothing = do
138 name <- atomicModifyIORef freeNames (\x->(tail x, head x))
139 n <- newGrimName cms name
140 let ty' = fromMaybe (error "unexpected") mb_ty
141 return (Suspension ct mb_ty hval (Just n), [(n,ty',hval)])
144 -- A custom Term printer to enable the use of Show instances
145 printTerm cms@(Session ref) = cPprTerm cPpr
147 cPpr = \p-> cPprShowable : cPprTermBase p
148 cPprShowable prec t@Term{ty=ty, dc=dc, val=val} = do
149 let hasType = isEmptyVarSet (tyVarsOfType ty) -- redundant
150 isEvaled = isFullyEvaluatedTerm t
151 if not isEvaled -- || not hasType
154 hsc_env <- readIORef ref
155 dflags <- GHC.getSessionDynFlags cms
157 (new_env, bname) <- bindToFreshName hsc_env ty "showme"
158 writeIORef ref (new_env)
159 let noop_log _ _ _ _ = return ()
160 expr = "show " ++ showSDoc (ppr bname)
161 GHC.setSessionDynFlags cms dflags{log_action=noop_log}
162 mb_txt <- withExtendedLinkEnv [(bname, val)]
163 (GHC.compileExpr cms expr)
164 let myprec = 9 -- TODO Infix constructors
166 Just txt -> return . Just . text . unsafeCoerce#
168 Nothing -> return Nothing
170 writeIORef ref hsc_env
171 GHC.setSessionDynFlags cms dflags
173 bindToFreshName hsc_env ty userName = do
174 name <- newGrimName cms userName
175 let ictxt = hsc_IC hsc_env
176 type_env = ic_type_env ictxt
177 id = mkGlobalId VanillaGlobal name ty vanillaIdInfo
178 new_type_env = extendTypeEnv type_env (AnId id)
179 new_ic = ictxt { ic_type_env = new_type_env }
180 return (hsc_env {hsc_IC = new_ic }, name)
182 -- Create new uniques and give them sequentially numbered names
183 -- newGrimName :: Session -> String -> IO Name
184 newGrimName cms userName = do
185 us <- mkSplitUniqSupply 'b'
186 let unique = uniqFromSupply us
187 occname = mkOccName varName userName
188 name = mkInternalName unique occname noSrcLoc
191 skolemSubst subst = subst `setTvSubstEnv`
192 mapVarEnv mk_skol_ty (getTvSubstEnv subst)
193 mk_skol_ty ty | tyvars <- varSetElems (tyVarsOfType ty)
194 , tyvars' <- map (mkTyVarTy . mk_skol_tv) tyvars
195 = substTyWith tyvars tyvars' ty
196 mk_skol_tv tv = mkTcTyVar (tyVarName tv) (tyVarKind tv)