Skip to content

Commit 22b05c8

Browse files
meippalt-romes
authored andcommitted
Incomplete state of Debug instance visualization
1 parent 7764428 commit 22b05c8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

haskell-debugger/GHC/Debugger/Runtime.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ import GHC.Debugger.Runtime.Term.Key
1414
import GHC.Debugger.Runtime.Term.Cache
1515
import GHC.Debugger.Monad
1616

17+
import GHC.Core.TyCon
18+
import GHC.Core.Type
19+
import GHC.Types.Name
20+
import GHC.Core.Class
21+
import GHC.Core.InstEnv
22+
import Debug.Trace
23+
import qualified GHC.Linker.Loader as Loader
24+
import GHC.Driver.Env
25+
import GHC.Types.Var
26+
import GHC.Driver.Config
27+
import GHCi.Message
28+
import GHC.Runtime.Interpreter
29+
import GHC.Utils.Outputable
30+
1731
-- | Obtain the runtime 'Term' from a 'TermKey'.
1832
--
1933
-- The 'TermKey' will be looked up in the 'TermCache' to avoid recomputing the
@@ -73,4 +87,30 @@ isBoringTy :: Type -> Bool
7387
isBoringTy t = isDoubleTy t || isFloatTy t || isIntTy t || isWordTy t || isStringTy t
7488
|| isIntegerTy t || isNaturalTy t || isCharTy t
7589

90+
onDebugInstance :: Term -> Type -> Debugger Bool
91+
onDebugInstance term t = do
92+
hsc_env <- getSession
93+
instances <- getInstancesForType t
94+
95+
case filter ((== "Debug") . occNameString . occName . tyConName . classTyCon . is_cls) instances of
96+
(c:_) -> do
97+
let methods = (classOpItems . is_cls) c
98+
traceM ("Found Debug instance with methods: " ++ (show . map (occNameString . occName . fst)) methods ++ "")
99+
case filter ((== "debugDisplayTree") . occNameString . occName . fst) methods of
100+
(m:_) -> do
101+
let dfun = is_dfun c
102+
traceM $ "Dictionary function: " ++ showSDocUnsafe (ppr dfun) ++ " :: " ++ showSDocUnsafe (ppr (varType dfun))
103+
104+
let method_id = fst m :: Id
105+
traceM $ "debugDisplayTree method: " ++ showSDocUnsafe (ppr method_id) ++ " :: " ++ showSDocUnsafe (ppr (varType method_id))
106+
107+
(method_hv, _, _) <- liftIO $ Loader.loadName (hscInterp hsc_env) hsc_env (GHC.Types.Var.varName method_id)
108+
(dfun_hv, _, _) <- liftIO $ Loader.loadName (hscInterp hsc_env) hsc_env (GHC.Types.Var.varName dfun)
109+
110+
-- this call fails
111+
ev <- liftIO $ evalStmt (hscInterp hsc_env) (initEvalOpts (hsc_dflags hsc_env) EvalStepNone) (EvalApp (EvalApp (EvalThis method_hv) (EvalThis dfun_hv)) (EvalThis (val term)))
76112

113+
return True
114+
[] -> return False
115+
return False
116+
_ -> return False

haskell-debugger/GHC/Debugger/Stopped/Variables.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ termToVarInfo key term0 = do
116116
-- Pass type as value for functions since actual value is useless
117117
varValue <- if isFn
118118
then pure $ "<fn> :: " ++ varType
119-
else display =<< GHCD.showTerm (termHead term)
119+
else do
120+
_ <- onDebugInstance term ty
121+
display =<< GHCD.showTerm (termHead term)
120122
-- liftIO $ print (varName, varType, varValue, GHCI.isFullyEvaluatedTerm term)
121123

122124
-- The VarReference allows user to expand variable structure and inspect its value.

0 commit comments

Comments
 (0)