[Haskell-cafe] GHC API question

Thomas Schilling nominolo at googlemail.com
Tue Aug 30 01:33:06 CEST 2011

OK, I guess I misunderstood you.  I don't know how SafeHaskell works,
so I don't know whether there might be some interaction.  I know that
profiling is a static flag which must be set when you initialise the
session and cannot be changed afterwards.  I assume you are doing

I checked the source code for getHValue (in 7.0.4) and it calls
linkDependencies if the name is external (not 100 percent sure what
that means).  There is an interesting comment in linkDependencies,

	-- The interpreter and dynamic linker can only handle object code built
	-- the "normal" way, i.e. no non-std ways like profiling or ticky-ticky.
	-- So here we check the build tag: if we're building a non-standard way
	-- then we need to find & link object files built the "normal" way.

This is what I've was referring to in my previous mail. Even though
you're compiling to machine code, you are using the in-memory linker
(i.e., the GHCi linker).  It seems like that this is a fundamental
limitation of the internal linker. You may be using it in a way that
doesn't trigger the sanity check and end up causing a panic.  I
suggest you pose this question on the glasgow-haskell-users mailing

On 28 August 2011 17:57, Chris Smith <cdsmith at gmail.com> wrote:
> On Sun, 2011-08-28 at 17:47 +0100, Thomas Schilling wrote:
>> I don't think you can link GHCi with binaries compiled in profiling
>> mode.  You'll have to build an executable.
> Okay... sorry to be obtuse, but what exactly does this mean?  I'm not
> using GHCi at all: I *am* in an executable built with profiling info.
> I'm doing this:
>        dflags <- GHC.getSessionDynFlags
>        let dflags' = dflags {
>            GHC.ghcMode = GHC.CompManager,
>            GHC.ghcLink = GHC.LinkInMemory,
>            GHC.hscTarget = GHC.HscAsm,
>            GHC.optLevel = 2,
>            GHC.safeHaskell = GHC.Sf_Safe,
>            GHC.packageFlags = [GHC.TrustPackage "gloss" ],
>            GHC.log_action = addErrorTo codeErrors
>            }
>        GHC.setSessionDynFlags dflags'
>        target <- GHC.guessTarget filename Nothing
>        GHC.setTargets [target]
>        r      <- fmap GHC.succeeded (GHC.load GHC.LoadAllTargets)
> and then if r is true:
>        mods <- GHC.getModuleGraph
>        let mainMod = GHC.ms_mod (head mods)
>        Just mi <- GHC.getModuleInfo mainMod
>        let tyThings = GHC.modInfoTyThings mi
>        let var = chooseTopLevel varname tyThings
>        session <- GHC.getSession
>        v       <- GHC.liftIO $ GHC.getHValue session (GHC.varName var)
>        return (unsafeCoerce# v)
> Here, I know that chooseTopLevel is working, but the getHValue part only
> works without profiling.  So is this still hopeless, or do I just need
> to find the right additional flags to add to dflags'?
> --
> Chris Smith

Push the envelope. Watch it bend.

More information about the Haskell-Cafe mailing list