-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decorate exceptions with backtrace information #330
Conversation
6fa2aeb
to
74ed8ba
Compare
I love this. |
@bgamari FWIW I had attempted some benchmarks a long time ago:
|
With such a type we can easily write a variant of ``throwIO`` that, for | ||
instance, attaches a DWARF backtrace: :: | ||
|
||
-- | Throws an exception with a 'HasCallStack' backtrace. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this a HasCallStackBacktrace
, not a ExecutionBacktrace
as implied above? Also it's not clear how the Backtrace type enables this, at least not without a definition for throw
.
Thanks for doing this work! Can you lead with the UX story and clarify a bit? E.g.: I have a web app with various layers of exception handling with a topmost handler that logs anything that reaches there. Occasionally I get a mysterious If this is only really useful with a local profiling build, why not just I already don't use |
How much overhead would it bring to introduce a Also, would I ever want to have backtraces from multiple mechanisms for the same exception? My understanding is that As a side note, I feel like the most significant feature suggested here is exposing to Haskell the stack trace powered by the DWARF debug information (Don't get me wrong, the change to P.S: Just a personal note; There have been times when I would sell my soul to the devil on the spot just to have the slightest bit of context for a given exception thrown in production. Any hint whatsoever... |
foo someExc = do
SomeException exc <- someExc will desugar using Note, exceptions are also thrown in mtl style programs, where |
An alternative to the pattern synonym: leave One also needs to keep data SomeExceptionWithLocation where
SomeExceptionWithLocation
:: forall e. Exception e
=> Maybe Backtrace -- ^ backtrace, if available
-> e -- ^ the exception
-> SomeExceptionWithLocation
-- Two new members are added to the Exception type class, with default implementations.
class Exception e where
toExceptionWithLocation :: e -> SomeExceptionWithLocation
toExceptionWithLocation = SomeExceptionWithLocation Nothing
fromExceptionWithLocation :: SomeExceptionWithLocation -> Maybe e
fromExceptionWithLocation (SomeExceptionWithLocation _ e) = fromException e
-- we keep the old members for backwards compatibility
toException :: e -> SomeException
fromException :: SomeException -> Maybe e
{- NOTE changing the default implementations of toException and fromException to be
defined via the WithLocation variants will loop for fully default instances.
toException e = case toExceptionWithLocation e of
SomeExceptionWithLocation _ someE -> SomeException someE
fromException e = fromExceptionWithLocation (SomeExceptionWithLocation Nothing e)
-}
instance Exception SomeExceptionWithLocation where
toExceptionWithLocation = id
fromExceptionWithLocation = Just
instance Exception SomeException where
toException = id
fromException = Just
-- Minor changes are needed to 'throw' and 'catch'
throw = raise# . toExceptionWithLocation
catch = ... |
Another problem with |
I think it's nice to point out that the extra state lives purely in base using regular synchronization primitives? If one was really worried they could alwaysrebuild base to statically force one sort of backtrace and remove the state. |
is this a ghc proposal or a base / core libraries proposal? this seems to be strictly libraries based ( cc @chessai ) |
another question: why are we assuming/presuming that you'll only provide one of the several types of backtrace? what obstacles or reasons for/against having an n-tuple of possible subset products of these various backtraces? |
I agree about concerns around pattern synonyms. But maybe using one in this way would be a nice forcing function to get GHC to fix weaknesses around them. I'm still a little unsure of what this all means, though. Here are a few questions:
Don't get me wrong -- I love the idea of having stack traces on exceptions. I just don't really understand how this will work in practice. |
As someone has pointed out on reddit¹ there's combinators in base that might need adjustment to preserve backtraces when they re-throw (e.g. tryJust²). 1: https://reddit.com/r/haskell/comments/gfg5ac/ghc_proposal_decorate_exceptions_with_backtrace/fpvtxe8/ |
Co-authored-by: Tommy Bidne <tbidne@protonmail.com>
Co-authored-by: Tommy Bidne <tbidne@protonmail.com>
@int-index, as far as I can tell there are no major objections to the proposed handling of rethrowing. I would consequently like to submit the proposal for consideration by the committee. |
I hereby declare the proposal to be accepted. |
I took the liberty to squash the commits, so the GitHub interface didn't detect the merge. Nevertheless, you can find the merged document at https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0330-exception-backtraces.rst |
Yeah, I usually don’t bother squashing. Thanks for merging! |
I know this is closed, but I didn't want to open another thread somewhere else... I was just taking another look at this and noticed that with the current design if we catch and rethrow a |
Here we introduce the `ExceptionContext` type and `ExceptionAnnotation` class, allowing dynamically-typed user-defined annotations to be attached to exceptions. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330
Here we introduce the `Backtraces` type and associated machinery for attaching these via `ExceptionContext`. These has a few compile-time regressions (`T15703` and `T9872d`) due to the additional dependencies in the exception machinery. As well, there is a surprisingly large regression in the `size_hello_artifact` test. This appears to be due to various `Integer` and `Read` bits now being reachable at link-time. I believe it should be possible to avoid this but I have accepted the change for now to get the feature merged. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330 Metric Increase: T15703 T9872d size_hello_artifact
Here we introduce the `ExceptionContext` type and `ExceptionAnnotation` class, allowing dynamically-typed user-defined annotations to be attached to exceptions. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330
Here we introduce the `Backtraces` type and associated machinery for attaching these via `ExceptionContext`. These has a few compile-time regressions (`T15703` and `T9872d`) due to the additional dependencies in the exception machinery. As well, there is a surprisingly large regression in the `size_hello_artifact` test. This appears to be due to various `Integer` and `Read` bits now being reachable at link-time. I believe it should be possible to avoid this but I have accepted the change for now to get the feature merged. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330 Metric Increase: T15703 T9872d size_hello_artifact
Here we introduce the `ExceptionContext` type and `ExceptionAnnotation` class, allowing dynamically-typed user-defined annotations to be attached to exceptions. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330
Here we introduce the `Backtraces` type and associated machinery for attaching these via `ExceptionContext`. These has a few compile-time regressions (`T15703` and `T9872d`) due to the additional dependencies in the exception machinery. As well, there is a surprisingly large regression in the `size_hello_artifact` test. This appears to be due to various `Integer` and `Read` bits now being reachable at link-time. I believe it should be possible to avoid this but I have accepted the change for now to get the feature merged. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330 Metric Increase: T15703 T9872d size_hello_artifact
Here we introduce the `ExceptionContext` type and `ExceptionAnnotation` class, allowing dynamically-typed user-defined annotations to be attached to exceptions. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330
Here we introduce the `Backtraces` type and associated machinery for attaching these via `ExceptionContext`. These has a few compile-time regressions (`T15703` and `T9872d`) due to the additional dependencies in the exception machinery. As well, there is a surprisingly large regression in the `size_hello_artifact` test. This appears to be due to various `Integer` and `Read` bits now being reachable at link-time. I believe it should be possible to avoid this but I have accepted the change for now to get the feature merged. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330 Metric Increase: T15703 T9872d size_hello_artifact
Here we introduce the `ExceptionContext` type and `ExceptionAnnotation` class, allowing dynamically-typed user-defined annotations to be attached to exceptions. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330
Here we introduce the `Backtraces` type and associated machinery for attaching these via `ExceptionContext`. These has a few compile-time regressions (`T15703` and `T9872d`) due to the additional dependencies in the exception machinery. As well, there is a surprisingly large regression in the `size_hello_artifact` test. This appears to be due to various `Integer` and `Read` bits now being reachable at link-time. I believe it should be possible to avoid this but I have accepted the change for now to get the feature merged. CLC Proposal: haskell/core-libraries-committee#199 GHC Proposal: ghc-proposals/ghc-proposals#330 Metric Increase: T15703 T9872d size_hello_artifact
From the Proposal:
(Some text in the quote above has been bolded by me) So What about IPE backtraces ? How can we qualify the qualify of the backtraces on that ? Precise ? Imprecise ? As precise as |
@sidkshatriya I (sort of) asked this question on discourse. I have not received a response, but perhaps my examples are useful to you. For me the answer seems to be "fairly imprecise", but I would love to learn that I have done something wrong. |
Thanks @tbidne ! @bgamari Sorry for the ping -- Your input on #330 (comment) would be much appreciated too ! |
The proposal has been accepted; the following discussion is mostly of historic interest.