Simon Marlow simonmarhaskell at gmail.com
Tue Nov 14 05:18:05 EST 2006

Serge D. Mechveliani wrote:
> Simon P. Jones  wrote today about the  -xc  option to help with
> finding who called  fromJust Noting.
> Now, I see in the GHC Guide:
> ------------------------------------------------------------------
> -xs
> (Only available when the program is compiled for profiling.) When an 
> exception is raised in the program, this option causes the current 
> cost-centre-stack to be dumped to stderr.
> This can be particularly useful for debugging: if your program is 
> complaining about a head [] error and you haven't got a clue which bit of 
> code is causing it, compiling with -prof -auto-all and running with 
> +RTS -xc -RTS will tell you exactly the call stack at the point the error 
> was raised.
> The output contains one line for each exception raised in the program 
> (the program might raise and catch several exceptions during its 
> execution), where each line is of the form:
>   < cc1, ..., ccn >
> ...
> ---------------------------------------------------------------------
> So, I `made' the project under  -prof -auto-all   and run
>               ./Nat1  +RTS -xc -RTS.
> It prints
>   ...
>   (3, Ind n,
>    (formula1
>     [(1, IndMember,
>   <Data.Maybe.CAF>Nat1: Maybe.fromJust: Nothing
> This does not help to see who called for  fromJust  in this case.
> Maybe this is in  Nat.prof ?  And it is empty. 
> Adding  -p  does fill  Nat.prof  with a lot of statistics,
> but I guess, this statistics cannot help with the  fromJust  question.
> Can people, please, comment some more on this  -xc  usage?

-xc doesn't always work well, as you noticed.  In particular, if the exception 
is part of a CAF, then you don't get to see the call stack.  Exceptions are 
often lifted into CAFs, because in a function definition like this:

  fromJust Nothing = error "..."
  fromJust (Just a ) = a

it makes sense to transform this to

  _fail = error "..."
  fromJust Nothing = _fail
  fromJust (Just a) = a

because then we can inline fromJust without duplicating the error and the 
string.  So I suspect this is exactly what is happening.  You might be able to 
work around it by defining a local definition of fromJust, maybe adding a 
NOINLINE pragma to it.

The fact that cost-centre-stacks don't give you the call stack of a CAF is 
partly a feature - when considering the profiled costs of a CAF it would be 
wrong (arguably) to attribute them to the first call site that evaluates the CAF.

Anyway, we recognise this is a problem (and not the only problem with CCSs). 
One day we'll overhaul CCSs and make them work properly!


More information about the Glasgow-haskell-users mailing list