{-# OPTIONS_GHC -optc-DPROFILING #-}
{-# LINE 1 "libraries/ghc-internal/src/GHC/Internal/Stack/CCS.hsc" #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE UnboxedTuples, MagicHash, NoImplicitPrelude #-}
module GHC.Internal.Stack.CCS (
currentCallStack,
whoCreated,
CostCentreStack,
CostCentre,
getCurrentCCS,
getCCSOf,
clearCCS,
ccsCC,
ccsParent,
ccLabel,
ccModule,
ccSrcSpan,
ccsToStrings,
renderStack,
) where
import GHC.Internal.Foreign.C.String
import GHC.Internal.Foreign.C.String.Encoding as GHC
import GHC.Internal.Foreign.Storable
import GHC.Internal.Base
import GHC.Internal.Ptr
import GHC.Internal.IO.Encoding
import GHC.Internal.List ( concatMap, reverse )
data CostCentreStack
data CostCentre
getCurrentCCS :: dummy -> IO (Ptr CostCentreStack)
getCurrentCCS :: forall dummy. dummy -> IO (Ptr CostCentreStack)
getCurrentCCS dummy
dummy = (State# RealWorld -> (# State# RealWorld, Ptr CostCentreStack #))
-> IO (Ptr CostCentreStack)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Ptr CostCentreStack #))
-> IO (Ptr CostCentreStack))
-> (State# RealWorld
-> (# State# RealWorld, Ptr CostCentreStack #))
-> IO (Ptr CostCentreStack)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case dummy -> State# RealWorld -> (# State# RealWorld, Addr# #)
forall a d. a -> State# d -> (# State# d, Addr# #)
getCurrentCCS# dummy
dummy State# RealWorld
s of
(# State# RealWorld
s', Addr#
addr #) -> (# State# RealWorld
s', Addr# -> Ptr CostCentreStack
forall a. Addr# -> Ptr a
Ptr Addr#
addr #)
getCCSOf :: a -> IO (Ptr CostCentreStack)
getCCSOf :: forall dummy. dummy -> IO (Ptr CostCentreStack)
getCCSOf a
obj = (State# RealWorld -> (# State# RealWorld, Ptr CostCentreStack #))
-> IO (Ptr CostCentreStack)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Ptr CostCentreStack #))
-> IO (Ptr CostCentreStack))
-> (State# RealWorld
-> (# State# RealWorld, Ptr CostCentreStack #))
-> IO (Ptr CostCentreStack)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case a -> State# RealWorld -> (# State# RealWorld, Addr# #)
forall a d. a -> State# d -> (# State# d, Addr# #)
getCCSOf# a
obj State# RealWorld
s of
(# State# RealWorld
s', Addr#
addr #) -> (# State# RealWorld
s', Addr# -> Ptr CostCentreStack
forall a. Addr# -> Ptr a
Ptr Addr#
addr #)
clearCCS :: IO a -> IO a
clearCCS :: forall a. IO a -> IO a
clearCCS (IO State# RealWorld -> (# State# RealWorld, a #)
m) = (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, a #)) -> IO a)
-> (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s -> (State# RealWorld -> (# State# RealWorld, a #))
-> State# RealWorld -> (# State# RealWorld, a #)
forall d a.
(State# d -> (# State# d, a #)) -> State# d -> (# State# d, a #)
clearCCS# State# RealWorld -> (# State# RealWorld, a #)
m State# RealWorld
s
{-# LINE 95 "libraries/ghc-internal/src/GHC/Internal/Stack/CCS.hsc" #-}
ccsCC :: Ptr CostCentreStack -> IO (Ptr CostCentre)
ccsCC :: Ptr CostCentreStack -> IO (Ptr CostCentre)
ccsCC Ptr CostCentreStack
p = ((\Ptr CostCentreStack
hsc_ptr -> Ptr CostCentreStack -> Int -> IO (Ptr CostCentre)
forall b. Ptr b -> Int -> IO (Ptr CostCentre)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CostCentreStack
hsc_ptr Int
8)) Ptr CostCentreStack
p
{-# LINE 97 "libraries/ghc-internal/src/GHC/Internal/Stack/CCS.hsc" #-}
ccsParent :: Ptr CostCentreStack -> IO (Ptr CostCentreStack)
ccsParent :: Ptr CostCentreStack -> IO (Ptr CostCentreStack)
ccsParent Ptr CostCentreStack
p = ((\Ptr CostCentreStack
hsc_ptr -> Ptr CostCentreStack -> Int -> IO (Ptr CostCentreStack)
forall b. Ptr b -> Int -> IO (Ptr CostCentreStack)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CostCentreStack
hsc_ptr Int
16)) Ptr CostCentreStack
p
{-# LINE 101 "libraries/ghc-internal/src/GHC/Internal/Stack/CCS.hsc" #-}
ccLabel :: Ptr CostCentre -> IO CString
ccLabel :: Ptr CostCentre -> IO CString
ccLabel Ptr CostCentre
p = ((\Ptr CostCentre
hsc_ptr -> Ptr CostCentre -> Int -> IO CString
forall b. Ptr b -> Int -> IO CString
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CostCentre
hsc_ptr Int
8)) Ptr CostCentre
p
{-# LINE 105 "libraries/ghc-internal/src/GHC/Internal/Stack/CCS.hsc" #-}
ccModule :: Ptr CostCentre -> IO CString
ccModule :: Ptr CostCentre -> IO CString
ccModule Ptr CostCentre
p = ((\Ptr CostCentre
hsc_ptr -> Ptr CostCentre -> Int -> IO CString
forall b. Ptr b -> Int -> IO CString
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CostCentre
hsc_ptr Int
16)) Ptr CostCentre
p
{-# LINE 109 "libraries/ghc-internal/src/GHC/Internal/Stack/CCS.hsc" #-}
ccSrcSpan :: Ptr CostCentre -> IO CString
ccSrcSpan :: Ptr CostCentre -> IO CString
ccSrcSpan Ptr CostCentre
p = ((\Ptr CostCentre
hsc_ptr -> Ptr CostCentre -> Int -> IO CString
forall b. Ptr b -> Int -> IO CString
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CostCentre
hsc_ptr Int
24)) Ptr CostCentre
p
{-# LINE 113 "libraries/ghc-internal/src/GHC/Internal/Stack/CCS.hsc" #-}
{-# LINE 114 "libraries/ghc-internal/src/GHC/Internal/Stack/CCS.hsc" #-}
currentCallStack :: IO [String]
currentCallStack :: IO [String]
currentCallStack = Ptr CostCentreStack -> IO [String]
ccsToStrings (Ptr CostCentreStack -> IO [String])
-> IO (Ptr CostCentreStack) -> IO [String]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< () -> IO (Ptr CostCentreStack)
forall dummy. dummy -> IO (Ptr CostCentreStack)
getCurrentCCS ()
ccsToStrings :: Ptr CostCentreStack -> IO [String]
ccsToStrings :: Ptr CostCentreStack -> IO [String]
ccsToStrings Ptr CostCentreStack
ccs0 = Ptr CostCentreStack -> [String] -> IO [String]
go Ptr CostCentreStack
ccs0 []
where
go :: Ptr CostCentreStack -> [String] -> IO [String]
go Ptr CostCentreStack
ccs [String]
acc
| Ptr CostCentreStack
ccs Ptr CostCentreStack -> Ptr CostCentreStack -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr CostCentreStack
forall a. Ptr a
nullPtr = [String] -> IO [String]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return [String]
acc
| Bool
otherwise = do
cc <- Ptr CostCentreStack -> IO (Ptr CostCentre)
ccsCC Ptr CostCentreStack
ccs
lbl <- GHC.peekCString utf8 =<< ccLabel cc
mdl <- GHC.peekCString utf8 =<< ccModule cc
loc <- GHC.peekCString utf8 =<< ccSrcSpan cc
parent <- ccsParent ccs
if (mdl == "MAIN" && lbl == "MAIN")
then return acc
else go parent ((mdl ++ '.':lbl ++ ' ':'(':loc ++ ")") : acc)
whoCreated :: a -> IO [String]
whoCreated :: forall a. a -> IO [String]
whoCreated a
obj = do
ccs <- a -> IO (Ptr CostCentreStack)
forall dummy. dummy -> IO (Ptr CostCentreStack)
getCCSOf a
obj
ccsToStrings ccs
renderStack :: [String] -> String
renderStack :: [String] -> String
renderStack [String]
strs =
String
"CallStack (from -prof):" String -> String -> String
forall a. [a] -> [a] -> [a]
++ (String -> String) -> [String] -> String
forall a b. (a -> [b]) -> [a] -> [b]
concatMap (String
"\n "String -> String -> String
forall a. [a] -> [a] -> [a]
++) ([String] -> [String]
forall a. [a] -> [a]
reverse [String]
strs)