{-# LINE 1 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}

module GHC.Exts.Heap.FFIClosures_ProfilingDisabled where

-- See [hsc and CPP workaround]




import Prelude
import Foreign
import GHC.Exts
import GHC.Exts.Heap.ProfInfo.PeekProfInfo
import GHC.Exts.Heap.ProfInfo.Types
import GHC.Exts.Heap.Closures(WhatNext(..), WhyBlocked(..), TsoFlags(..))

data TSOFields = TSOFields {
    TSOFields -> WhatNext
tso_what_next :: WhatNext,
    TSOFields -> WhyBlocked
tso_why_blocked :: WhyBlocked,
    TSOFields -> [TsoFlags]
tso_flags :: [TsoFlags],
-- Unfortunately block_info is a union without clear discriminator.
--    block_info :: TDB,
    TSOFields -> Word64
tso_threadId :: Word64,
    TSOFields -> Word32
tso_saved_errno :: Word32,
    TSOFields -> Word32
tso_dirty:: Word32,
    TSOFields -> Int64
tso_alloc_limit :: Int64,
    TSOFields -> Word32
tso_tot_stack_size :: Word32,
    TSOFields -> Maybe StgTSOProfInfo
tso_prof :: Maybe StgTSOProfInfo
}

-- | Get non-pointer fields from @StgTSO_@ (@TSO.h@)
peekTSOFields :: (Ptr a -> IO (Maybe CostCentreStack)) -> Ptr tsoPtr -> IO TSOFields
peekTSOFields :: forall a tsoPtr.
(Ptr a -> IO (Maybe CostCentreStack)) -> Ptr tsoPtr -> IO TSOFields
peekTSOFields Ptr a -> IO (Maybe CostCentreStack)
decodeCCS Ptr tsoPtr
ptr = do
    what_next' <- ((\Ptr tsoPtr
hsc_ptr -> Ptr tsoPtr -> Int -> IO Word16
forall b. Ptr b -> Int -> IO Word16
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr tsoPtr
hsc_ptr Int
32)) Ptr tsoPtr
ptr
{-# LINE 36 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    why_blocked' <- ((\hsc_ptr -> peekByteOff hsc_ptr 40)) ptr
{-# LINE 37 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    flags' <- ((\hsc_ptr -> peekByteOff hsc_ptr 36)) ptr
{-# LINE 38 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    threadId' <- ((\hsc_ptr -> peekByteOff hsc_ptr 56)) ptr
{-# LINE 39 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    saved_errno' <- ((\hsc_ptr -> peekByteOff hsc_ptr 64)) ptr
{-# LINE 40 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    dirty' <- ((\hsc_ptr -> peekByteOff hsc_ptr 68)) ptr
{-# LINE 41 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    alloc_limit' <- ((\hsc_ptr -> peekByteOff hsc_ptr 120)) ptr
{-# LINE 42 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    tot_stack_size' <- ((\hsc_ptr -> peekByteOff hsc_ptr 128)) ptr
{-# LINE 43 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    tso_prof' <- peekStgTSOProfInfo decodeCCS ptr

    return TSOFields {
        tso_what_next = parseWhatNext what_next',
        tso_why_blocked = parseWhyBlocked why_blocked',
        tso_flags = parseTsoFlags flags',
        tso_threadId = threadId',
        tso_saved_errno = saved_errno',
        tso_dirty = dirty',
        tso_alloc_limit = alloc_limit',
        tso_tot_stack_size = tot_stack_size',
        tso_prof = tso_prof'
    }

parseWhatNext :: Word16 -> WhatNext
parseWhatNext :: Word16 -> WhatNext
parseWhatNext Word16
w = case Word16
w of
                    (Word16
1) -> WhatNext
ThreadRunGHC
{-# LINE 60 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                    (Word16
2) -> WhatNext
ThreadInterpret
{-# LINE 61 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                    (Word16
3) -> WhatNext
ThreadKilled
{-# LINE 62 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                    (Word16
4) -> WhatNext
ThreadComplete
{-# LINE 63 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                    Word16
_ -> Word16 -> WhatNext
WhatNextUnknownValue Word16
w

parseWhyBlocked :: Word16 -> WhyBlocked
parseWhyBlocked :: Word16 -> WhyBlocked
parseWhyBlocked Word16
w = case Word16
w of
                        (Word16
0) -> WhyBlocked
NotBlocked
{-# LINE 68 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
1) -> WhyBlocked
BlockedOnMVar
{-# LINE 69 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
14) -> WhyBlocked
BlockedOnMVarRead
{-# LINE 70 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
2) -> WhyBlocked
BlockedOnBlackHole
{-# LINE 71 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
3) -> WhyBlocked
BlockedOnRead
{-# LINE 72 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
4) -> WhyBlocked
BlockedOnWrite
{-# LINE 73 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
5) -> WhyBlocked
BlockedOnDelay
{-# LINE 74 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
6) -> WhyBlocked
BlockedOnSTM
{-# LINE 75 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
7) -> WhyBlocked
BlockedOnDoProc
{-# LINE 76 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
10) -> WhyBlocked
BlockedOnCCall
{-# LINE 77 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
11) -> WhyBlocked
BlockedOnCCall_Interruptible
{-# LINE 78 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
12) -> WhyBlocked
BlockedOnMsgThrowTo
{-# LINE 79 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        (Word16
13) -> WhyBlocked
ThreadMigrating
{-# LINE 80 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                        Word16
_ -> Word16 -> WhyBlocked
WhyBlockedUnknownValue Word16
w

parseTsoFlags :: Word32 -> [TsoFlags]
parseTsoFlags :: Word32 -> [TsoFlags]
parseTsoFlags Word32
w | Word32 -> Word32 -> Bool
isSet (Word32
2) Word32
w = TsoFlags
TsoLocked TsoFlags -> [TsoFlags] -> [TsoFlags]
forall a. a -> [a] -> [a]
: Word32 -> [TsoFlags]
parseTsoFlags (Word32 -> Word32 -> Word32
unset (Word32
2) Word32
w)
{-# LINE 84 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                | Word32 -> Word32 -> Bool
isSet (Word32
4) Word32
w = TsoFlags
TsoBlockx TsoFlags -> [TsoFlags] -> [TsoFlags]
forall a. a -> [a] -> [a]
: Word32 -> [TsoFlags]
parseTsoFlags (Word32 -> Word32 -> Word32
unset (Word32
4) Word32
w)
{-# LINE 85 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                | Word32 -> Word32 -> Bool
isSet (Word32
8) Word32
w = TsoFlags
TsoInterruptible TsoFlags -> [TsoFlags] -> [TsoFlags]
forall a. a -> [a] -> [a]
: Word32 -> [TsoFlags]
parseTsoFlags (Word32 -> Word32 -> Word32
unset (Word32
8) Word32
w)
{-# LINE 86 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                | Word32 -> Word32 -> Bool
isSet (Word32
16) Word32
w = TsoFlags
TsoStoppedOnBreakpoint TsoFlags -> [TsoFlags] -> [TsoFlags]
forall a. a -> [a] -> [a]
: Word32 -> [TsoFlags]
parseTsoFlags (Word32 -> Word32 -> Word32
unset (Word32
16) Word32
w)
{-# LINE 87 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                | Word32 -> Word32 -> Bool
isSet (Word32
64) Word32
w = TsoFlags
TsoMarked TsoFlags -> [TsoFlags] -> [TsoFlags]
forall a. a -> [a] -> [a]
: Word32 -> [TsoFlags]
parseTsoFlags (Word32 -> Word32 -> Word32
unset (Word32
64) Word32
w)
{-# LINE 88 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                | Word32 -> Word32 -> Bool
isSet (Word32
128) Word32
w = TsoFlags
TsoSqueezed TsoFlags -> [TsoFlags] -> [TsoFlags]
forall a. a -> [a] -> [a]
: Word32 -> [TsoFlags]
parseTsoFlags (Word32 -> Word32 -> Word32
unset (Word32
128) Word32
w)
{-# LINE 89 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
                | Word32 -> Word32 -> Bool
isSet (Word32
256) Word32
w = TsoFlags
TsoAllocLimit TsoFlags -> [TsoFlags] -> [TsoFlags]
forall a. a -> [a] -> [a]
: Word32 -> [TsoFlags]
parseTsoFlags (Word32 -> Word32 -> Word32
unset (Word32
256) Word32
w)
{-# LINE 90 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
parseTsoFlags 0 = []
parseTsoFlags Word32
w = [Word32 -> TsoFlags
TsoFlagsUnknownValue Word32
w]

isSet :: Word32 -> Word32 -> Bool
isSet :: Word32 -> Word32 -> Bool
isSet Word32
bitMask Word32
w = Word32
w Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32
bitMask Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word32
0

unset :: Word32 -> Word32 -> Word32
unset :: Word32 -> Word32 -> Word32
unset Word32
bitMask Word32
w = Word32
w Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
`xor` Word32
bitMask

data StackFields = StackFields {
    StackFields -> Word32
stack_size :: Word32,
    StackFields -> Word8
stack_dirty :: Word8,
    StackFields -> Word8
stack_marking :: Word8,
    StackFields -> Addr#
stack_sp :: Addr#
}

-- | Get non-closure fields from @StgStack_@ (@TSO.h@)
peekStackFields :: Ptr a -> IO StackFields
peekStackFields :: forall a. Ptr a -> IO StackFields
peekStackFields Ptr a
ptr = do
    stack_size' <- ((\Ptr a
hsc_ptr -> Ptr a -> Int -> IO Word32
forall b. Ptr b -> Int -> IO Word32
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr a
hsc_ptr Int
8)) Ptr a
ptr ::IO Word32
{-# LINE 110 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    dirty' <- ((\hsc_ptr -> peekByteOff hsc_ptr 12)) ptr
{-# LINE 111 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    marking' <- ((\hsc_ptr -> peekByteOff hsc_ptr 13)) ptr
{-# LINE 112 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}
    Ptr sp' <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) ptr
{-# LINE 113 "libraries/ghc-heap/GHC/Exts/Heap/FFIClosures_ProfilingDisabled.hsc" #-}

    -- TODO decode the stack.

    return StackFields {
        stack_size = stack_size',
        stack_dirty = dirty',
        stack_marking = marking',
        stack_sp = sp'
    }