Cheng Shao pushed to branch wip/fix-hpc-bytecode-modname at Glasgow Haskell Compiler / GHC
Commits:
-
704164be
by Cheng Shao at 2026-05-25T13:59:10+00:00
4 changed files:
- + libraries/ghc-boot/GHC/Data/ShortByteString.hs
- libraries/ghc-boot/ghc-boot.cabal.in
- libraries/ghci/GHCi/Coverage.hs
- libraries/ghci/GHCi/Run.hs
Changes:
| 1 | +module GHC.Data.ShortByteString
|
|
| 2 | + ( newCStringFromSBS
|
|
| 3 | + ) where
|
|
| 4 | + |
|
| 5 | +import Prelude
|
|
| 6 | + |
|
| 7 | +import qualified Data.ByteString.Short as SBS
|
|
| 8 | +import Foreign
|
|
| 9 | +import Foreign.C
|
|
| 10 | + |
|
| 11 | +newCStringFromSBS :: SBS.ShortByteString -> IO CString
|
|
| 12 | +newCStringFromSBS sbs =
|
|
| 13 | + SBS.useAsCStringLen sbs $ \(src, len) -> do
|
|
| 14 | + dst <- mallocBytes (len + 1)
|
|
| 15 | + copyBytes dst src len
|
|
| 16 | + pokeByteOff dst len (0 :: Word8)
|
|
| 17 | + pure dst |
| ... | ... | @@ -51,6 +51,7 @@ Library |
| 51 | 51 | |
| 52 | 52 | exposed-modules:
|
| 53 | 53 | GHC.BaseDir
|
| 54 | + GHC.Data.ShortByteString
|
|
| 54 | 55 | GHC.Data.ShortText
|
| 55 | 56 | GHC.Data.SizedSeq
|
| 56 | 57 | GHC.Data.SmallArray
|
| ... | ... | @@ -9,9 +9,9 @@ import Prelude -- See note [Why do we import Prelude here?] |
| 9 | 9 | |
| 10 | 10 | import Control.Exception
|
| 11 | 11 | import Data.ByteString.Short (ShortByteString)
|
| 12 | -import qualified Data.ByteString.Short as SBS
|
|
| 13 | 12 | import Data.Word
|
| 14 | 13 | import Foreign
|
| 14 | +import GHC.Data.ShortByteString
|
|
| 15 | 15 | import GHC.Foreign (CString)
|
| 16 | 16 | import GHC.Utils.Encoding.UTF8 (utf8DecodeShortByteString)
|
| 17 | 17 | import GHCi.ObjLink (lookupSymbol)
|
| ... | ... | @@ -31,17 +31,19 @@ hpcAddModule :: |
| 31 | 31 | -- ^ Name of the ticks array found in the c-stub.
|
| 32 | 32 | IO ()
|
| 33 | 33 | hpcAddModule modlName ticks hash tickboxes = do
|
| 34 | - SBS.useAsCString modlName $ \modlNameLiteral -> do
|
|
| 35 | - -- we need to find the reference to the ticks array.
|
|
| 36 | - lookupSymbol tickboxes >>= \ case
|
|
| 37 | - Nothing -> do
|
|
| 38 | - -- the symbol is not found, this is a bug!
|
|
| 39 | - throwIO $ ErrorCall $ "hpcAddModule: failed to find symbol " <> utf8DecodeShortByteString tickboxes
|
|
| 40 | - Just tickBoxRef -> do
|
|
| 41 | - -- Calling 'hs_hpc_module' multiple times is safe, it will add the module only once.
|
|
| 42 | - hpc_register_module modlNameLiteral (fromIntegral ticks) (fromIntegral hash) (castPtr tickBoxRef)
|
|
| 43 | - -- calling 'hpc_startup' multiple times is safe, it will only be initialised once.
|
|
| 44 | - hpc_startup
|
|
| 34 | + -- we need to find the reference to the ticks array.
|
|
| 35 | + lookupSymbol tickboxes >>= \ case
|
|
| 36 | + Nothing -> do
|
|
| 37 | + -- the symbol is not found, this is a bug!
|
|
| 38 | + throwIO $ ErrorCall $ "hpcAddModule: failed to find symbol " <> utf8DecodeShortByteString tickboxes
|
|
| 39 | + Just tickBoxRef -> do
|
|
| 40 | + -- hs_hpc_module stores the module name pointer in the RTS hash table
|
|
| 41 | + -- until exitHpc, so pass a malloced C string.
|
|
| 42 | + modlNameLiteral <- newCStringFromSBS modlName
|
|
| 43 | + -- Calling 'hs_hpc_module' multiple times is safe, it will add the module only once.
|
|
| 44 | + hpc_register_module modlNameLiteral (fromIntegral ticks) (fromIntegral hash) (castPtr tickBoxRef)
|
|
| 45 | + -- calling 'hpc_startup' multiple times is safe, it will only be initialised once.
|
|
| 46 | + hpc_startup
|
|
| 45 | 47 | |
| 46 | 48 | foreign import ccall unsafe "hs_hpc_module"
|
| 47 | 49 | hpc_register_module :: CString -> Word32 -> Word32 -> Ptr Word64 -> IO ()
|
| ... | ... | @@ -37,6 +37,9 @@ import Control.Monad |
| 37 | 37 | import Data.ByteString (ByteString)
|
| 38 | 38 | import qualified Data.ByteString.Short.Internal as BS
|
| 39 | 39 | import qualified Data.ByteString.Unsafe as B
|
| 40 | +#if defined(PROFILING)
|
|
| 41 | +import GHC.Data.ShortByteString
|
|
| 42 | +#endif
|
|
| 40 | 43 | import GHC.Exts
|
| 41 | 44 | import qualified GHC.Exts.Heap as Heap
|
| 42 | 45 | import GHC.Stack
|
| ... | ... | @@ -447,13 +450,6 @@ mkCostCentres mod ccs = do |
| 447 | 450 | c_srcspan <- newCStringFromSBS srcspan
|
| 448 | 451 | toRemotePtr <$> c_mkCostCentre c_name c_module c_srcspan
|
| 449 | 452 | |
| 450 | - newCStringFromSBS sbs = do
|
|
| 451 | - let len = BS.length sbs
|
|
| 452 | - buf <- mallocBytes $ len + 1
|
|
| 453 | - BS.copyToPtr sbs 0 buf (fromIntegral len)
|
|
| 454 | - pokeByteOff buf len (0 :: Word8)
|
|
| 455 | - pure buf
|
|
| 456 | - |
|
| 457 | 453 | foreign import ccall unsafe "mkCostCentre"
|
| 458 | 454 | c_mkCostCentre :: Ptr CChar -> Ptr CChar -> Ptr CChar -> IO (Ptr CostCentre)
|
| 459 | 455 | #else
|