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 mb_subst = tcUnifyTys (const BindMe) [idType id] [reconstructed_type]
80 ASSERT (isJust mb_subst) return mb_subst
82 applySubstToEnv :: Session -> TvSubst -> IO ()
83 applySubstToEnv cms subst | isEmptyTvSubst subst = return ()
84 applySubstToEnv cms@(Session ref) subst = do
85 hsc_env <- readIORef ref
86 inScope <- GHC.getBindings cms
87 let ictxt = hsc_IC hsc_env
88 type_env = ic_type_env ictxt
89 ids = typeEnvIds type_env
90 ids' = map (\id -> id `setIdType` substTy subst (idType id)) ids
91 type_env'= extendTypeEnvWithIds type_env ids'
92 ictxt' = ictxt { ic_type_env = type_env' }
93 writeIORef ref (hsc_env {hsc_IC = ictxt'})
95 -- | Give names, and bind in the interactive environment, to all the suspensions
96 -- included (inductively) in a term
97 bindSuspensions :: Session -> Term -> IO Term
98 bindSuspensions cms@(Session ref) t = do
99 hsc_env <- readIORef ref
100 inScope <- GHC.getBindings cms
101 let ictxt = hsc_IC hsc_env
102 type_env = ic_type_env ictxt
104 alreadyUsedNames = map (occNameString . nameOccName . getName) inScope
105 availNames = map ((prefix++) . show) [1..] \\ alreadyUsedNames
106 availNames_var <- newIORef availNames
107 (t', stuff) <- foldTerm (nameSuspensionsAndGetInfos availNames_var) t
108 let (names, tys, hvals) = unzip3 stuff
109 let tys' = map mk_skol_ty tys
110 let ids = [ mkGlobalId VanillaGlobal name ty vanillaIdInfo
111 | (name,ty) <- zip names tys']
112 new_tyvars = tyVarsOfTypes tys'
113 new_type_env = extendTypeEnvWithIds type_env ids
114 old_tyvars = ic_tyvars ictxt
115 new_ic = ictxt { ic_type_env = new_type_env,
116 ic_tyvars = old_tyvars `unionVarSet` new_tyvars }
117 extendLinkEnv (zip names hvals)
118 writeIORef ref (hsc_env {hsc_IC = new_ic })
122 -- Processing suspensions. Give names and recopilate info
123 nameSuspensionsAndGetInfos :: IORef [String] -> TermFold (IO (Term, [(Name,Type,HValue)]))
124 nameSuspensionsAndGetInfos freeNames = TermFold
126 fSuspension = doSuspension freeNames
127 , fTerm = \ty dc v tt -> do
129 let (terms,names) = unzip tt'
130 return (Term ty dc v terms, concat names)
131 , fPrim = \ty n ->return (Prim ty n,[])
133 doSuspension freeNames ct mb_ty hval Nothing = do
134 name <- atomicModifyIORef freeNames (\x->(tail x, head x))
135 n <- newGrimName cms name
136 let ty' = fromMaybe (error "unexpected") mb_ty
137 return (Suspension ct mb_ty hval (Just n), [(n,ty',hval)])
140 -- A custom Term printer to enable the use of Show instances
141 printTerm cms@(Session ref) = cPprTerm cPpr
143 cPpr = \p-> cPprShowable : cPprTermBase p
144 cPprShowable prec t@Term{ty=ty, dc=dc, val=val} = do
145 let hasType = isEmptyVarSet (tyVarsOfType ty) -- redundant
146 isEvaled = isFullyEvaluatedTerm t
147 if not isEvaled -- || not hasType
150 hsc_env <- readIORef ref
151 dflags <- GHC.getSessionDynFlags cms
153 (new_env, bname) <- bindToFreshName hsc_env ty "showme"
154 writeIORef ref (new_env)
155 let noop_log _ _ _ _ = return ()
156 expr = "show " ++ showSDoc (ppr bname)
157 GHC.setSessionDynFlags cms dflags{log_action=noop_log}
158 mb_txt <- withExtendedLinkEnv [(bname, val)]
159 (GHC.compileExpr cms expr)
160 let myprec = 9 -- TODO Infix constructors
162 Just txt -> return . Just . text . unsafeCoerce#
164 Nothing -> return Nothing
166 writeIORef ref hsc_env
167 GHC.setSessionDynFlags cms dflags
169 bindToFreshName hsc_env ty userName = do
170 name <- newGrimName cms userName
171 let ictxt = hsc_IC hsc_env
172 type_env = ic_type_env ictxt
173 id = mkGlobalId VanillaGlobal name ty vanillaIdInfo
174 new_type_env = extendTypeEnv type_env (AnId id)
175 new_ic = ictxt { ic_type_env = new_type_env }
176 return (hsc_env {hsc_IC = new_ic }, name)
178 -- Create new uniques and give them sequentially numbered names
179 -- newGrimName :: Session -> String -> IO Name
180 newGrimName cms userName = do
181 us <- mkSplitUniqSupply 'b'
182 let unique = uniqFromSupply us
183 occname = mkOccName varName userName
184 name = mkInternalName unique occname noSrcLoc
187 skolemSubst subst = subst `setTvSubstEnv`
188 mapVarEnv mk_skol_ty (getTvSubstEnv subst)
189 mk_skol_ty ty | tyvars <- varSetElems (tyVarsOfType ty)
190 , tyvars' <- map (mkTyVarTy . mk_skol_tv) tyvars
191 = substTyWith tyvars tyvars' ty
192 mk_skol_tv tv = mkTcTyVar (tyVarName tv) (tyVarKind tv)