DsBreakpoint.lhs 7.59 KB
Newer Older
mnislaih's avatar
mnislaih committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
-----------------------------------------------------------------------------
--
-- Support code for instrumentation and expansion of the breakpoint combinator
--
-- Pepe Iborra (supported by Google SoC) 2006
--
-----------------------------------------------------------------------------

\begin{code}
module DsBreakpoint( 
                     dsAndThenMaybeInsertBreakpoint
                   , maybeInsertBreakpoint
                   , breakpoints_enabled
                   , mkBreakpointExpr
                   ) where

mnislaih's avatar
mnislaih committed
17 18 19
import IOEnv
import TysPrim
import TysWiredIn
mnislaih's avatar
mnislaih committed
20
import PrelNames        
mnislaih's avatar
mnislaih committed
21 22 23 24 25
import Module
import PackageConfig
import SrcLoc
import TyCon
import TypeRep
mnislaih's avatar
mnislaih committed
26 27
import DataCon          
import Type             
mnislaih's avatar
mnislaih committed
28 29 30 31
import MkId
import Name
import Var
import Id 
mnislaih's avatar
mnislaih committed
32

mnislaih's avatar
mnislaih committed
33 34 35
import IdInfo
import BasicTypes
import OccName
mnislaih's avatar
mnislaih committed
36 37 38

import TcRnMonad
import HsSyn            
mnislaih's avatar
mnislaih committed
39 40 41
import HsLit
import CoreSyn
import CoreUtils
mnislaih's avatar
mnislaih committed
42
import Outputable
mnislaih's avatar
mnislaih committed
43 44 45
import ErrUtils
import FastString
import DynFlags
mnislaih's avatar
mnislaih committed
46 47 48 49 50
 
import DsMonad 
import {-#SOURCE#-}DsExpr ( dsLExpr ) 
import Control.Monad
import Data.IORef
mnislaih's avatar
mnislaih committed
51 52
import Foreign.StablePtr
import GHC.Exts
mnislaih's avatar
mnislaih committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

mkBreakpointExpr :: SrcSpan -> Id -> DsM (LHsExpr Id)
mkBreakpointExpr loc bkptFuncId = do
        scope' <- getLocalBindsDs
        mod  <- getModuleDs
        let scope = filter (isValidType .idType ) scope'
            mod_name = moduleNameFS$ moduleName mod
        if null scope && instrumenting
         then return (l$ HsVar lazyId) 
         else do
          when (not instrumenting) $
              warnDs (text "Extracted ids:" <+> (ppr scope $$ 
                                                   ppr (map idType scope)))
          stablePtr <- ioToIOEnv $ newStablePtr scope
          site <- if instrumenting
                   then recordBkpt (srcSpanStart loc)
                   else return 0
          ATyCon opaqueTyCon <- dsLookupGlobal opaqueTyConName
          jumpFuncId <- mkJumpFunc bkptFuncId
          let [opaqueDataCon] = tyConDataCons opaqueTyCon
              opaqueId = dataConWrapId opaqueDataCon
              opaqueTy = mkTyConApp opaqueTyCon []
              wrapInOpaque id = 
                  l(HsApp (l(HsWrap (WpTyApp (idType id)) (HsVar opaqueId)))
                          (l(HsVar id)))
           -- Yes, I know... I'm gonna burn in hell.
              Ptr addr# = castStablePtrToPtr stablePtr
              hvals = ExplicitList opaqueTy (map wrapInOpaque scope)
              locInfo = nlTuple [ HsLit (HsString (packageIdFS$ modulePackageId mod))
                                , HsLit (HsString mod_name)
                                , HsLit (HsInt (fromIntegral site))]
              
              funE  = l$ HsVar jumpFuncId
              ptrE  = l (HsLit (HsInt (fromIntegral (I# (addr2Int# addr#)))))
              hvalE = l hvals
              locE  = l locInfo
              msgE  = l (srcSpanLit loc)
          return$  l(l(l(l(funE `HsApp` ptrE) `HsApp` hvalE) `HsApp` locE) `HsApp` msgE)
    where l = L loc
          nlTuple exps = ExplicitTuple (map noLoc exps) Boxed
--          isValidType (PredTy p `FunTy` ty ) = False -- TODO: Too restrictive ? 
          isValidType (FunTy a b) = isValidType a && isValidType b
          isValidType (NoteTy _ t) = isValidType t
          isValidType (AppTy a b) = isValidType a && isValidType b
          isValidType (TyConApp con ts) = not (isUnLiftedTyCon con) && all isValidType ts
          isValidType _ = True
          srcSpanLit :: SrcSpan -> HsExpr Id
          srcSpanLit span = HsLit (HsString (mkFastString (showSDoc (ppr span))))
          instrumenting = idName bkptFuncId == breakpointAutoName

mnislaih's avatar
mnislaih committed
103
debug_enabled :: DsM Bool
104
#if defined(GHCI) && defined(DEBUGGER)
mnislaih's avatar
mnislaih committed
105 106 107 108
debug_enabled = do
    debugging      <- doptDs Opt_Debugging
    b_enabled      <- breakpoints_enabled
    return (debugging && b_enabled)
109 110 111
#else
debug_enabled = return False
#endif
mnislaih's avatar
mnislaih committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

maybeInsertBreakpoint :: LHsExpr Id -> Type ->  DsM (LHsExpr Id)
--maybeInsertBreakpoint e | pprTrace("insertBreakpoint at" (ppr e) False = undefined

isInstrumentationSpot (L loc e) = do
  ghcmode   <- getGhcModeDs
  instrumenting <- debug_enabled 
  return$ instrumenting     
          && isGoodSrcSpan loc          -- Avoids 'derived' code
          && (not$ isRedundant e)

isRedundant HsLet  {} = True
isRedundant HsDo   {} = True
isRedundant HsCase {} = True
isRedundant     _     = False

dynBreakpoint :: SrcSpan -> DsM (LHsExpr Id)
dynBreakpoint loc | not (isGoodSrcSpan loc) = 
                         pprPanic "dynBreakpoint" (ppr loc)
dynBreakpoint loc = do 
mnislaih's avatar
mnislaih committed
132
    let autoBreakpoint = Id.mkGlobalId VanillaGlobal breakpointAutoName 
mnislaih's avatar
mnislaih committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                         breakpointAutoTy vanillaIdInfo
    dflags <- getDOptsDs 
    ioToIOEnv$ debugTraceMsg dflags 3 (text "Breakpoint inserted at " <> ppr loc)
    return$ L loc (HsVar autoBreakpoint)
  where breakpointAutoTy = (ForAllTy alphaTyVar
                                (FunTy (TyVarTy  alphaTyVar)
                                 (TyVarTy alphaTyVar)))

-- Records a breakpoint site and returns the site number
recordBkpt :: SrcLoc -> DsM (Int)
--recordBkpt | trace "recordBkpt" False = undefined
recordBkpt loc = do
    sites_var <- getBkptSitesDs
    sites     <- ioToIOEnv$ readIORef sites_var
    let site   = length sites + 1
    let coords = (srcLocLine loc, srcLocCol loc)
    ioToIOEnv$ writeIORef sites_var ((site, coords) : sites) 
    return site

mnislaih's avatar
mnislaih committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
mkJumpFunc :: Id -> DsM Id  
mkJumpFunc bkptFuncId
    | idName bkptFuncId == breakpointName 
    = build breakpointJumpName id
    | idName bkptFuncId == breakpointCondName 
    = build breakpointCondJumpName (FunTy boolTy)
    | idName bkptFuncId == breakpointAutoName 
    = build breakpointAutoJumpName id
  where 
        tyvar = alphaTyVar
        basicType extra opaqueTy = 
                           (FunTy intTy
                            (FunTy (mkListTy opaqueTy)
                             (FunTy (mkTupleType [stringTy, stringTy, intTy])
                              (FunTy stringTy
                          (ForAllTy tyvar
                               (extra
                                (FunTy (TyVarTy tyvar)
                                 (TyVarTy tyvar))))))))
        build name extra  = do 
            ATyCon opaqueTyCon <- dsLookupGlobal opaqueTyConName
mnislaih's avatar
mnislaih committed
173
            return$ Id.mkGlobalId VanillaGlobal name
mnislaih's avatar
mnislaih committed
174 175 176
                      (basicType extra (mkTyConApp opaqueTyCon [])) vanillaIdInfo
        mkTupleType tys = mkTupleTy Boxed (length tys) tys

mnislaih's avatar
mnislaih committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
breakpoints_enabled :: DsM Bool
dsAndThenMaybeInsertBreakpoint :: LHsExpr Id -> DsM CoreExpr

#ifdef GHCI
maybeInsertBreakpoint lhsexpr@(L loc _) ty = do 
  instrumenting <- isInstrumentationSpot lhsexpr
  if instrumenting
         then do L _ dynBkpt <- dynBreakpoint loc 
--                 return (l (HsApp (l$ TyApp dynBkpt [ty]) lhsexpr))
                 return$ l(HsApp (l$ HsWrap (WpTyApp ty) dynBkpt) lhsexpr)
         else return lhsexpr
  where l = L loc

dsAndThenMaybeInsertBreakpoint expr@(L loc _) = do
  coreExpr  <- dsLExpr expr
  instrumenting <- isInstrumentationSpot expr
  if instrumenting
         then do L _ dynBkpt<- dynBreakpoint loc
                 bkptCore   <- dsLExpr (l$ HsWrap (WpTyApp (exprType coreExpr)) dynBkpt)
                 return (bkptCore `App` coreExpr)
         else return coreExpr
  where l = L loc

breakpoints_enabled = do
    ghcMode            <- getGhcModeDs
    currentModule      <- getModuleDs
    ignore_breakpoints <- doptDs Opt_IgnoreBreakpoints
    return ( not ignore_breakpoints 
          && ghcMode == Interactive 
          && currentModule /= iNTERACTIVE )
mnislaih's avatar
mnislaih committed
207 208 209
#else
maybeInsertBreakpoint expr _ = return expr
dsAndThenMaybeInsertBreakpoint coreExpr = dsLExpr coreExpr
mnislaih's avatar
mnislaih committed
210
breakpoints_enabled = return False
mnislaih's avatar
mnislaih committed
211 212
#endif
\end{code}