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 )
33 import Pretty ( Mode(..), showDocWith )
37 import Control.Exception
46 #include "HsVersions.h"
48 -------------------------------------
49 -- | The :print & friends commands
50 -------------------------------------
51 pprintClosureCommand :: Session -> Bool -> Bool -> String -> IO ()
52 pprintClosureCommand session bindThings force str = do
53 tythings <- (catMaybes . concat) `liftM`
54 mapM (\w -> GHC.parseName session w >>=
55 mapM (GHC.lookupName session))
57 substs <- catMaybes `liftM` mapM (go session)
58 [id | AnId id <- tythings]
59 mapM (applySubstToEnv session . skolemSubst) substs
63 -- Do the obtainTerm--bindSuspensions-computeSubstitution dance
64 go :: Session -> Id -> IO (Maybe TvSubst)
66 term_ <- obtainTerm cms force id
67 term <- tidyTermTyVars cms term_
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
80 -- tcUnifyTys doesn't look through forall's, so we drop them from
81 -- the original type, instead of sigma-typing the reconstructed type
82 -- In addition, we strip newtypes too, since the reconstructed type might
83 -- not have recovered them all
84 mb_subst = tcUnifyTys (const BindMe)
85 [repType' $ dropForAlls$ idType id]
86 [repType' $ reconstructed_type]
88 ASSERT2 (isJust mb_subst, ppr reconstructed_type $$ (ppr$ idType id))
91 applySubstToEnv :: Session -> TvSubst -> IO ()
92 applySubstToEnv cms subst | isEmptyTvSubst subst = return ()
93 applySubstToEnv cms@(Session ref) subst = do
94 hsc_env <- readIORef ref
95 inScope <- GHC.getBindings cms
96 let ictxt = hsc_IC hsc_env
97 ids = ic_tmp_ids ictxt
98 ids' = map (\id -> id `setIdType` substTy subst (idType id)) ids
99 subst_dom= varEnvKeys$ getTvSubstEnv subst
100 subst_ran= varEnvElts$ getTvSubstEnv subst
101 new_tvs = [ tv | Just tv <- map getTyVar_maybe subst_ran]
102 ic_tyvars'= (`delVarSetListByKey` subst_dom)
103 . (`extendVarSetList` new_tvs)
105 ictxt' = ictxt { ic_tmp_ids = ids'
106 , ic_tyvars = ic_tyvars' }
107 writeIORef ref (hsc_env {hsc_IC = ictxt'})
109 where delVarSetListByKey = foldl' delVarSetByKey
111 tidyTermTyVars :: Session -> Term -> IO Term
112 tidyTermTyVars (Session ref) t = do
113 hsc_env <- readIORef ref
114 let env_tvs = ic_tyvars (hsc_IC hsc_env)
115 my_tvs = termTyVars t
116 tvs = env_tvs `minusVarSet` my_tvs
117 tyvarOccName = nameOccName . tyVarName
118 tidyEnv = (initTidyOccEnv (map tyvarOccName (varSetElems tvs))
119 , env_tvs `intersectVarSet` my_tvs)
120 return$ mapTermType (snd . tidyOpenType tidyEnv) t
122 -- | Give names, and bind in the interactive environment, to all the suspensions
123 -- included (inductively) in a term
124 bindSuspensions :: Session -> Term -> IO Term
125 bindSuspensions cms@(Session ref) t = do
126 hsc_env <- readIORef ref
127 inScope <- GHC.getBindings cms
128 let ictxt = hsc_IC hsc_env
130 alreadyUsedNames = map (occNameString . nameOccName . getName) inScope
131 availNames = map ((prefix++) . show) [1..] \\ alreadyUsedNames
132 availNames_var <- newIORef availNames
133 (t', stuff) <- foldTerm (nameSuspensionsAndGetInfos availNames_var) t
134 let (names, tys, hvals) = unzip3 stuff
135 let tys' = map mk_skol_ty tys
136 let ids = [ mkGlobalId VanillaGlobal name ty vanillaIdInfo
137 | (name,ty) <- zip names tys']
138 new_tyvars = tyVarsOfTypes tys'
139 new_ic = extendInteractiveContext ictxt ids new_tyvars
140 extendLinkEnv (zip names hvals)
141 writeIORef ref (hsc_env {hsc_IC = new_ic })
145 -- Processing suspensions. Give names and recopilate info
146 nameSuspensionsAndGetInfos :: IORef [String] -> TermFold (IO (Term, [(Name,Type,HValue)]))
147 nameSuspensionsAndGetInfos freeNames = TermFold
149 fSuspension = doSuspension freeNames
150 , fTerm = \ty dc v tt -> do
152 let (terms,names) = unzip tt'
153 return (Term ty dc v terms, concat names)
154 , fPrim = \ty n ->return (Prim ty n,[])
156 doSuspension freeNames ct mb_ty hval Nothing = do
157 name <- atomicModifyIORef freeNames (\x->(tail x, head x))
158 n <- newGrimName cms name
159 let ty' = fromMaybe (error "unexpected") mb_ty
160 return (Suspension ct mb_ty hval (Just n), [(n,ty',hval)])
163 -- A custom Term printer to enable the use of Show instances
164 printTerm cms@(Session ref) = cPprTerm cPpr
166 cPpr = \p-> cPprShowable : cPprTermBase p
167 cPprShowable prec t@Term{ty=ty, dc=dc, val=val} = do
168 let hasType = isEmptyVarSet (tyVarsOfType ty) -- redundant
169 isEvaled = isFullyEvaluatedTerm t
170 if not isEvaled -- || not hasType
173 hsc_env <- readIORef ref
174 dflags <- GHC.getSessionDynFlags cms
176 (new_env, bname) <- bindToFreshName hsc_env ty "showme"
177 writeIORef ref (new_env)
178 let noop_log _ _ _ _ = return ()
179 expr = "show " ++ showSDoc (ppr bname)
180 GHC.setSessionDynFlags cms dflags{log_action=noop_log}
181 mb_txt <- withExtendedLinkEnv [(bname, val)]
182 (GHC.compileExpr cms expr)
183 let myprec = 9 -- TODO Infix constructors
185 Just txt -> return . Just . text . unsafeCoerce#
187 Nothing -> return Nothing
189 writeIORef ref hsc_env
190 GHC.setSessionDynFlags cms dflags
192 bindToFreshName hsc_env ty userName = do
193 name <- newGrimName cms userName
194 let ictxt = hsc_IC hsc_env
195 tmp_ids = ic_tmp_ids ictxt
196 id = mkGlobalId VanillaGlobal name ty vanillaIdInfo
197 new_ic = ictxt { ic_tmp_ids = id : tmp_ids }
198 return (hsc_env {hsc_IC = new_ic }, name)
200 -- Create new uniques and give them sequentially numbered names
201 -- newGrimName :: Session -> String -> IO Name
202 newGrimName cms userName = do
203 us <- mkSplitUniqSupply 'b'
204 let unique = uniqFromSupply us
205 occname = mkOccName varName userName
206 name = mkInternalName unique occname noSrcLoc
209 skolemSubst subst = subst `setTvSubstEnv`
210 mapVarEnv mk_skol_ty (getTvSubstEnv subst)
211 mk_skol_ty ty | tyvars <- varSetElems (tyVarsOfType ty)
212 , tyvars' <- map (mkTyVarTy . mk_skol_tv) tyvars
213 = substTyWith tyvars tyvars' ty
214 mk_skol_tv tv = mkTcTyVar (tyVarName tv) (tyVarKind tv)
215 (SkolemTv RuntimeUnkSkol)