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
22 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 tythings <- (catMaybes . concat) `liftM`
59 mapM (\w -> io(GHC.parseName cms w >>=
60 mapM (GHC.lookupName cms)))
62 substs <- catMaybes `liftM` mapM (io . go cms)
63 [id | AnId id <- tythings]
64 mapM (io . applySubstToEnv cms . skolemSubst) substs
68 -- Do the obtainTerm--bindSuspensions-refineIdType dance
69 -- Warning! This function got a good deal of side-effects
70 go :: Session -> Id -> IO (Maybe TvSubst)
72 mb_term <- obtainTerm cms force id
73 maybe (return Nothing) `flip` mb_term $ \term -> do
74 term' <- if not bindThings then return term
75 else bindSuspensions cms term
76 showterm <- printTerm cms term'
77 unqual <- GHC.getPrintUnqual cms
78 let showSDocForUserOneLine unqual doc =
79 showDocWith LeftMode (doc (mkErrStyle unqual))
80 (putStrLn . showSDocForUserOneLine unqual) (ppr id <+> char '=' <+> showterm)
81 -- Before leaving, we compare the type obtained to see if it's more specific
82 -- Then, we extract a substitution,
83 -- mapping the old tyvars to the reconstructed types.
84 let Just reconstructed_type = termType term
85 mb_subst = tcUnifyTys (const BindMe) [idType id] [reconstructed_type]
86 ASSERT (isJust mb_subst) return mb_subst
88 applySubstToEnv :: Session -> TvSubst -> IO ()
89 applySubstToEnv cms subst | isEmptyTvSubst subst = return ()
90 applySubstToEnv cms@(Session ref) subst = do
91 hsc_env <- readIORef ref
92 inScope <- GHC.getBindings cms
93 let ictxt = hsc_IC hsc_env
94 type_env = ic_type_env ictxt
95 ids = typeEnvIds type_env
96 ids' = map (\id -> id `setIdType` substTy subst (idType id)) ids
97 type_env'= extendTypeEnvWithIds type_env ids'
98 ictxt' = ictxt { ic_type_env = type_env' }
99 writeIORef ref (hsc_env {hsc_IC = ictxt'})
101 -- | Give names, and bind in the interactive environment, to all the suspensions
102 -- included (inductively) in a term
103 bindSuspensions :: Session -> Term -> IO Term
104 bindSuspensions cms@(Session ref) t = do
105 hsc_env <- readIORef ref
106 inScope <- GHC.getBindings cms
107 let ictxt = hsc_IC hsc_env
108 rn_env = ic_rn_local_env ictxt
109 type_env = ic_type_env ictxt
111 alreadyUsedNames = map (occNameString . nameOccName . getName) inScope
112 availNames = map ((prefix++) . show) [1..] \\ alreadyUsedNames
113 availNames_var <- newIORef availNames
114 (t', stuff) <- foldTerm (nameSuspensionsAndGetInfos availNames_var) t
115 let (names, tys, hvals) = unzip3 stuff
116 let ids = [ mkGlobalId VanillaGlobal name (mk_skol_ty ty) vanillaIdInfo
117 | (name,ty) <- zip names tys]
118 new_type_env = extendTypeEnvWithIds type_env ids
119 new_rn_env = extendLocalRdrEnv rn_env names
120 new_ic = ictxt { ic_rn_local_env = new_rn_env,
121 ic_type_env = new_type_env }
122 extendLinkEnv (zip names hvals)
123 writeIORef ref (hsc_env {hsc_IC = new_ic })
127 -- Processing suspensions. Give names and recopilate info
128 nameSuspensionsAndGetInfos :: IORef [String] -> TermFold (IO (Term, [(Name,Type,HValue)]))
129 nameSuspensionsAndGetInfos freeNames = TermFold
131 fSuspension = doSuspension freeNames
132 , fTerm = \ty dc v tt -> do
134 let (terms,names) = unzip tt'
135 return (Term ty dc v terms, concat names)
136 , fPrim = \ty n ->return (Prim ty n,[])
138 doSuspension freeNames ct mb_ty hval Nothing = do
139 name <- atomicModifyIORef freeNames (\x->(tail x, head x))
140 n <- newGrimName cms name
141 let ty' = fromMaybe (error "unexpected") mb_ty
142 return (Suspension ct mb_ty hval (Just n), [(n,ty',hval)])
145 -- A custom Term printer to enable the use of Show instances
146 printTerm cms@(Session ref) = cPprTerm cPpr
148 cPpr = \p-> cPprShowable : cPprTermBase p
149 cPprShowable prec t@Term{ty=ty, dc=dc, val=val} = do
150 let hasType = isEmptyVarSet (tyVarsOfType ty) -- redundant
151 isEvaled = isFullyEvaluatedTerm t
152 if not isEvaled -- || not hasType
155 hsc_env <- readIORef ref
156 dflags <- GHC.getSessionDynFlags cms
158 (new_env, bname) <- bindToFreshName hsc_env ty "showme"
159 writeIORef ref (new_env)
160 let noop_log _ _ _ _ = return ()
161 expr = "show " ++ showSDoc (ppr bname)
162 GHC.setSessionDynFlags cms dflags{log_action=noop_log}
163 mb_txt <- withExtendedLinkEnv [(bname, val)]
164 (GHC.compileExpr cms expr)
165 let myprec = 9 -- TODO Infix constructors
167 Just txt -> return . Just . text . unsafeCoerce#
169 Nothing -> return Nothing
171 writeIORef ref hsc_env
172 GHC.setSessionDynFlags cms dflags
174 bindToFreshName hsc_env ty userName = do
175 name <- newGrimName cms userName
176 let ictxt = hsc_IC hsc_env
177 rn_env = ic_rn_local_env ictxt
178 type_env = ic_type_env ictxt
179 id = mkGlobalId VanillaGlobal name ty vanillaIdInfo
180 new_type_env = extendTypeEnv type_env (AnId id)
181 new_rn_env = extendLocalRdrEnv rn_env [name]
182 new_ic = ictxt { ic_rn_local_env = new_rn_env,
183 ic_type_env = new_type_env }
184 return (hsc_env {hsc_IC = new_ic }, name)
186 -- Create new uniques and give them sequentially numbered names
187 -- newGrimName :: Session -> String -> IO Name
188 newGrimName cms userName = do
189 us <- mkSplitUniqSupply 'b'
190 let unique = uniqFromSupply us
191 occname = mkOccName varName userName
192 name = mkInternalName unique occname noSrcLoc
195 skolemSubst subst = subst `setTvSubstEnv`
196 mapVarEnv mk_skol_ty (getTvSubstEnv subst)
197 mk_skol_ty ty | tyvars <- varSetElems (tyVarsOfType ty)
198 , tyvars' <- map (mkTyVarTy . mk_skol_tv) tyvars
199 = substTyWith tyvars tyvars' ty
200 mk_skol_tv tv = mkTcTyVar (tyVarName tv) (tyVarKind tv)