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, showTerm, pprTypeAndContents) where
16 import RtClosureInspect
22 import Var hiding ( varName )
29 import InteractiveEval
43 -------------------------------------
44 -- | The :print & friends commands
45 -------------------------------------
46 pprintClosureCommand :: Session -> Bool -> Bool -> String -> IO ()
47 pprintClosureCommand session bindThings force str = do
48 tythings <- (catMaybes . concat) `liftM`
49 mapM (\w -> GHC.parseName session w >>=
50 mapM (GHC.lookupName session))
52 let ids = [id | AnId id <- tythings]
54 -- Obtain the terms and the recovered type information
55 (terms, substs) <- unzip `liftM` mapM (go session) ids
57 -- Apply the substitutions obtained after recovering the types
58 modifySession session $ \hsc_env ->
59 hsc_env{hsc_IC = foldr (flip substInteractiveContext)
61 (map skolemiseSubst substs)}
62 -- Finally, print the Terms
63 unqual <- GHC.getPrintUnqual session
64 docterms <- mapM (showTerm session) terms
65 (printForUser stdout unqual . vcat)
66 (zipWith (\id docterm -> ppr id <+> char '=' <+> docterm)
71 -- Do the obtainTerm--bindSuspensions-computeSubstitution dance
72 go :: Session -> Id -> IO (Term, TvSubst)
74 term_ <- GHC.obtainTerm cms force id
75 term <- tidyTermTyVars cms term_
76 term' <- if bindThings &&
77 False == isUnliftedTypeKind (termType term)
78 then bindSuspensions cms term
80 -- Before leaving, we compare the type obtained to see if it's more specific
81 -- Then, we extract a substitution,
82 -- mapping the old tyvars to the reconstructed types.
83 let reconstructed_type = termType term
84 mb_subst <- withSession cms $ \hsc_env ->
85 improveRTTIType hsc_env (idType id) (reconstructed_type)
86 return (term', fromMaybe emptyTvSubst mb_subst)
88 tidyTermTyVars :: Session -> Term -> IO Term
89 tidyTermTyVars (Session ref) t = do
90 hsc_env <- readIORef ref
91 let env_tvs = ic_tyvars (hsc_IC hsc_env)
93 tvs = env_tvs `minusVarSet` my_tvs
94 tyvarOccName = nameOccName . tyVarName
95 tidyEnv = (initTidyOccEnv (map tyvarOccName (varSetElems tvs))
96 , env_tvs `intersectVarSet` my_tvs)
97 return$ mapTermType (snd . tidyOpenType tidyEnv) t
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
107 alreadyUsedNames = map (occNameString . nameOccName . getName) inScope
108 availNames = map ((prefix++) . show) [(1::Int)..] \\ alreadyUsedNames
109 availNames_var <- newIORef availNames
110 (t', stuff) <- foldTerm (nameSuspensionsAndGetInfos availNames_var) t
111 let (names, tys, hvals) = unzip3 stuff
112 let tys' = map (fst.skolemiseTy) tys
113 let ids = [ mkGlobalId VanillaGlobal name ty vanillaIdInfo
114 | (name,ty) <- zip names tys']
115 new_tyvars = tyVarsOfTypes tys'
116 new_ic = extendInteractiveContext ictxt ids 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] ->
124 TermFold (IO (Term, [(Name,Type,HValue)]))
125 nameSuspensionsAndGetInfos freeNames = TermFold
127 fSuspension = doSuspension freeNames
128 , fTerm = \ty dc v tt -> do
130 let (terms,names) = unzip tt'
131 return (Term ty dc v terms, concat names)
132 , fPrim = \ty n ->return (Prim ty n,[])
136 return (NewtypeWrap ty dc term, names)
137 , fRefWrap = \ty t -> do
139 return (RefWrap ty term, names)
141 doSuspension freeNames ct ty hval _name = do
142 name <- atomicModifyIORef freeNames (\x->(tail x, head x))
143 n <- newGrimName name
144 return (Suspension ct ty hval (Just n), [(n,ty,hval)])
147 -- A custom Term printer to enable the use of Show instances
148 showTerm :: Session -> Term -> IO SDoc
149 showTerm cms@(Session ref) term = do
150 dflags <- GHC.getSessionDynFlags cms
151 if dopt Opt_PrintEvldWithShow dflags
152 then cPprTerm (liftM2 (++) (\_y->[cPprShowable]) cPprTermBase) term
153 else cPprTerm cPprTermBase term
155 cPprShowable prec t@Term{ty=ty, val=val} =
156 if not (isFullyEvaluatedTerm t)
159 hsc_env <- readIORef ref
160 dflags <- GHC.getSessionDynFlags cms
162 (new_env, bname) <- bindToFreshName hsc_env ty "showme"
163 writeIORef ref (new_env)
164 let noop_log _ _ _ _ = return ()
165 expr = "show " ++ showSDoc (ppr bname)
166 GHC.setSessionDynFlags cms dflags{log_action=noop_log}
167 mb_txt <- withExtendedLinkEnv [(bname, val)]
168 (GHC.compileExpr cms expr)
169 let myprec = 10 -- application precedence. TODO Infix constructors
171 Just txt_ | txt <- unsafeCoerce# txt_, not (null txt)
172 -> return $ Just$ cparen (prec >= myprec &&
177 writeIORef ref hsc_env
178 GHC.setSessionDynFlags cms dflags
179 cPprShowable prec NewtypeWrap{ty=new_ty,wrapped_term=t} =
180 cPprShowable prec t{ty=new_ty}
181 cPprShowable _ _ = return Nothing
183 needsParens ('"':_) = False -- some simple heuristics to see whether parens
184 -- are redundant in an arbitrary Show output
185 needsParens ('(':_) = False
186 needsParens txt = ' ' `elem` txt
189 bindToFreshName hsc_env ty userName = do
190 name <- newGrimName userName
191 let ictxt = hsc_IC hsc_env
192 tmp_ids = ic_tmp_ids ictxt
193 id = mkGlobalId VanillaGlobal name (sigmaType ty) vanillaIdInfo
194 new_ic = ictxt { ic_tmp_ids = id : tmp_ids }
195 return (hsc_env {hsc_IC = new_ic }, name)
197 -- Create new uniques and give them sequentially numbered names
198 newGrimName :: String -> IO Name
199 newGrimName userName = do
200 us <- mkSplitUniqSupply 'b'
201 let unique = uniqFromSupply us
202 occname = mkOccName varName userName
203 name = mkInternalName unique occname noSrcSpan
206 pprTypeAndContents :: Session -> [Id] -> IO SDoc
207 pprTypeAndContents session ids = do
208 dflags <- GHC.getSessionDynFlags session
209 let pefas = dopt Opt_PrintExplicitForalls dflags
210 pcontents = dopt Opt_PrintBindContents dflags
214 terms <- mapM (GHC.obtainTermB session depthBound False) ids
215 docs_terms <- mapM (showTerm session) terms
216 return $ vcat $ zipWith (\ty cts -> ty <+> equals <+> cts)
217 (map (pprTyThing pefas . AnId) ids)
219 else return $ vcat $ map (pprTyThing pefas . AnId) ids