From 7325b75d5ba6eebc1c60384b9641d3326d8687f1 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 19 Oct 2024 18:47:03 -1000 Subject: [PATCH] imp: rename ghcdebug build flag to debug, and enable stack traces with it Builds made with ghc 9.10+ and the 'debug' build flag, will show (some kind of, partial) stack traces when the program ends with an error. (And also will have ghc-debug support enabled.) The stack traces will probably improve in due course. --- Justfile | 8 ++++---- hledger-lib/Hledger/Utils/Debug.hs | 10 +++++----- hledger-lib/package.yaml | 6 +++--- hledger-ui/Hledger/UI/Main.hs | 11 ++++++++--- hledger-ui/Hledger/UI/UIOptions.hs | 2 +- hledger-ui/package.yaml | 6 +++--- hledger-web/Hledger/Web/Main.hs | 8 ++++++++ hledger-web/Hledger/Web/WebOptions.hs | 2 +- hledger-web/package.yaml | 6 +++--- hledger/Hledger/Cli.hs | 12 ++++++++---- hledger/Hledger/Cli/CliOptions.hs | 2 +- hledger/Hledger/Cli/Version.hs | 6 +++--- hledger/package.yaml | 8 ++++---- stack.yaml | 2 +- stack9.8.yaml | 2 +- 15 files changed, 54 insertions(+), 37 deletions(-) diff --git a/Justfile b/Justfile index 39d509cef..18bba4213 100644 --- a/Justfile +++ b/Justfile @@ -556,14 +556,14 @@ INSTALLING: $STACK install --local-bin-path bin/old for e in hledger hledger-ui hledger-web ; do mv bin/old/$e bin/old/$e-{{ VER }}; echo "bin/old/$e-{{ VER }}"; done -# install hledger with ghc-debug support as bin/hledger*-dbg +# install hledger with stack traces and ghc-debug support enabled, as bin/hledger*-dbg @installasdbg *STACKARGS: - $STACK install --local-bin-path bin --flag '*:ghcdebug' {{ STACKARGS }} hledger + $STACK install --local-bin-path bin --flag '*:debug' {{ STACKARGS }} hledger for e in hledger ; do mv bin/$e bin/$e-dbg; echo "bin/$e-dbg"; done -# install all hledger executables with ghc-debug support as bin/hledger*-dbg +# install all hledger executables with stack traces and ghc-debug support enabled, as bin/hledger*-dbg @installallasdbg *STACKARGS: - $STACK install --local-bin-path bin --flag '*:ghcdebug' {{ STACKARGS }} + $STACK install --local-bin-path bin --flag '*:debug' {{ STACKARGS }} for e in hledger hledger-ui hledger-web ; do mv bin/$e bin/$e-dbg; echo "bin/$e-dbg"; done # # make must be GNU Make 4.3+ diff --git a/hledger-lib/Hledger/Utils/Debug.hs b/hledger-lib/Hledger/Utils/Debug.hs index 448805e9b..5b4f68d7f 100644 --- a/hledger-lib/Hledger/Utils/Debug.hs +++ b/hledger-lib/Hledger/Utils/Debug.hs @@ -184,7 +184,7 @@ import Control.Monad.IO.Class (MonadIO, liftIO) import Data.List hiding (uncons) -- import Debug.Breakpoint import Debug.Trace (trace, traceIO, traceShowId) -#ifdef GHCDEBUG +#ifdef DEBUG import GHC.Debug.Stub (pause, withGhcDebug) #endif import Safe (readDef) @@ -224,7 +224,7 @@ debugLevel = case dropWhile (/="--debug") progArgs of _ -> 0 -- | Whether ghc-debug support is included in this build, and if so, how it will behave. --- When hledger is built with the ghcdebug cabal build flag (normally disabled), +-- When hledger is built with the @debug@ cabal flag (off by default), -- it can listen (on unix ?) for connections from ghc-debug clients like ghc-debug-brick, -- for pausing/resuming the program and inspecting memory usage and profile information. -- @@ -244,7 +244,7 @@ data GhcDebugMode = -- | Is the hledger-lib package built with ghc-debug support ? ghcDebugSupportedInLib :: Bool ghcDebugSupportedInLib = -#ifdef GHCDEBUG +#ifdef DEBUG True #else False @@ -265,7 +265,7 @@ ghcDebugMode = -- | When ghc-debug support has been built into the program and enabled at runtime with --debug=-N, -- this calls ghc-debug's withGhcDebug; otherwise it's a no-op. withGhcDebug' = -#ifdef GHCDEBUG +#ifdef DEBUG if ghcDebugMode > GDDisabled then withGhcDebug else id #else id @@ -274,7 +274,7 @@ withGhcDebug' = -- | When ghc-debug support has been built into the program, this calls ghc-debug's pause, otherwise it's a no-op. ghcDebugPause' :: IO () ghcDebugPause' = -#ifdef GHCDEBUG +#ifdef DEBUG pause #else return () diff --git a/hledger-lib/package.yaml b/hledger-lib/package.yaml index 21a17b7cb..10b7a71c6 100644 --- a/hledger-lib/package.yaml +++ b/hledger-lib/package.yaml @@ -96,7 +96,7 @@ ghc-options: - -Wno-unused-do-bind flags: - ghcdebug: + debug: description: Build with support for attaching a ghc-debug client default: false manual: true @@ -105,8 +105,8 @@ when: - condition: (!(os(windows))) dependencies: - pager >=0.1.1.0 -- condition: (flag(ghcdebug)) - cpp-options: -DGHCDEBUG +- condition: (flag(debug)) + cpp-options: -DDEBUG dependencies: - ghc-debug-stub >=0.6.0.0 && <0.7 diff --git a/hledger-ui/Hledger/UI/Main.hs b/hledger-ui/Hledger/UI/Main.hs index 5434b4af6..0c5a6e72e 100644 --- a/hledger-ui/Hledger/UI/Main.hs +++ b/hledger-ui/Hledger/UI/Main.hs @@ -67,10 +67,15 @@ hledgerUiMain = withGhcDebug' $ withProgName "hledger-ui.log" $ do -- force Hle #if MIN_VERSION_base(4,20,0) -- Control ghc 9.10+'s stack traces. - -- Strangely only hledger-ui has been showing them (when command line processing fails), - -- even though hledger and hledger-web process it in just the same way. - -- Disable them here. + -- CostCentreBacktrace - collect cost-centre stack backtraces (only available when built with profiling) + -- HasCallStackBacktrace - collect HasCallStack backtraces + -- ExecutionBacktrace - collect backtraces from native execution stack unwinding + -- IPEBacktrace - collect backtraces from Info Table Provenance Entries +#ifdef DEBUG + setBacktraceMechanismState HasCallStackBacktrace True +#else setBacktraceMechanismState HasCallStackBacktrace False +#endif #endif traceLogAtIO 1 "\n\n\n\n==== hledger-ui start" diff --git a/hledger-ui/Hledger/UI/UIOptions.hs b/hledger-ui/Hledger/UI/UIOptions.hs index d04653c0f..3417ef65b 100644 --- a/hledger-ui/Hledger/UI/UIOptions.hs +++ b/hledger-ui/Hledger/UI/UIOptions.hs @@ -33,7 +33,7 @@ prognameandversion :: String prognameandversion = versionStringWith $$tGitInfoCwdTry -#ifdef GHCDEBUG +#ifdef DEBUG True #else False diff --git a/hledger-ui/package.yaml b/hledger-ui/package.yaml index b7ca44267..f31507749 100644 --- a/hledger-ui/package.yaml +++ b/hledger-ui/package.yaml @@ -37,7 +37,7 @@ flags: description: Build with support for multithreaded execution default: true manual: false - ghcdebug: + debug: description: Build with support for attaching a ghc-debug client default: false manual: true @@ -56,8 +56,8 @@ dependencies: - base >=4.14 && <4.21 when: -- condition: (flag(ghcdebug)) - cpp-options: -DGHCDEBUG +- condition: (flag(debug)) + cpp-options: -DDEBUG dependencies: - ghc-debug-stub >=0.6.0.0 && <0.7 diff --git a/hledger-web/Hledger/Web/Main.hs b/hledger-web/Hledger/Web/Main.hs index bd9588e2a..1a9faf857 100644 --- a/hledger-web/Hledger/Web/Main.hs +++ b/hledger-web/Hledger/Web/Main.hs @@ -55,7 +55,15 @@ hledgerWebMain = withGhcDebug' $ do #if MIN_VERSION_base(4,20,0) -- Control ghc 9.10+'s stack traces. + -- CostCentreBacktrace - collect cost-centre stack backtraces (only available when built with profiling) + -- HasCallStackBacktrace - collect HasCallStack backtraces + -- ExecutionBacktrace - collect backtraces from native execution stack unwinding + -- IPEBacktrace - collect backtraces from Info Table Provenance Entries +#ifdef DEBUG + setBacktraceMechanismState HasCallStackBacktrace True +#else setBacktraceMechanismState HasCallStackBacktrace False +#endif #endif -- try to encourage user's $PAGER to properly display ANSI (in command line help) diff --git a/hledger-web/Hledger/Web/WebOptions.hs b/hledger-web/Hledger/Web/WebOptions.hs index 5a0375e1b..18aae9bd8 100644 --- a/hledger-web/Hledger/Web/WebOptions.hs +++ b/hledger-web/Hledger/Web/WebOptions.hs @@ -43,7 +43,7 @@ prognameandversion :: String prognameandversion = versionStringWith $$tGitInfoCwdTry -#ifdef GHCDEBUG +#ifdef DEBUG True #else False diff --git a/hledger-web/package.yaml b/hledger-web/package.yaml index c0baad255..13a3d9d7c 100644 --- a/hledger-web/package.yaml +++ b/hledger-web/package.yaml @@ -59,7 +59,7 @@ flags: description: Build with support for multithreaded execution. default: true manual: false - ghcdebug: + debug: description: Build with support for attaching a ghc-debug client default: false manual: true @@ -80,8 +80,8 @@ when: # 'ghc-options: -O0' is not needed. Use the --disable-optimization configure flag. - condition: flag(dev) ghc-options: -O0 -- condition: (flag(ghcdebug)) - cpp-options: -DGHCDEBUG +- condition: (flag(debug)) + cpp-options: -DDEBUG dependencies: - ghc-debug-stub >=0.6.0.0 && <0.7 diff --git a/hledger/Hledger/Cli.hs b/hledger/Hledger/Cli.hs index 9654ad5ab..e224e5963 100644 --- a/hledger/Hledger/Cli.hs +++ b/hledger/Hledger/Cli.hs @@ -196,11 +196,15 @@ main = withGhcDebug' $ do #if MIN_VERSION_base(4,20,0) -- Control ghc 9.10+'s stack traces. + -- CostCentreBacktrace - collect cost-centre stack backtraces (only available when built with profiling) + -- HasCallStackBacktrace - collect HasCallStack backtraces + -- ExecutionBacktrace - collect backtraces from native execution stack unwinding + -- IPEBacktrace - collect backtraces from Info Table Provenance Entries +#ifdef DEBUG + setBacktraceMechanismState HasCallStackBacktrace True +#else setBacktraceMechanismState HasCallStackBacktrace False - -- CostCentreBacktrace - collect cost-centre stack backtraces (only available when built with profiling) - -- HasCallStackBacktrace - collect HasCallStack backtraces - -- ExecutionBacktrace - collect backtraces from native execution stack unwinding - -- IPEBacktrace - collect backtraces from Info Table Provenance Entries +#endif #endif -- 0. let's go! diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 23710637f..e0c03e020 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -134,7 +134,7 @@ prognameandversion :: String prognameandversion = versionStringWith $$tGitInfoCwdTry -#ifdef GHCDEBUG +#ifdef DEBUG True #else False diff --git a/hledger/Hledger/Cli/Version.hs b/hledger/Hledger/Cli/Version.hs index 363b64a06..71bd985ee 100644 --- a/hledger/Hledger/Cli/Version.hs +++ b/hledger/Hledger/Cli/Version.hs @@ -39,7 +39,7 @@ packagemajorversion :: PackageVersion packagemajorversion = intercalate "." $ take 2 $ splitAtElement '.' packageversion -- | Given possible git state info from the build directory (or a git error, which is ignored), --- and the ghcdebug build flag, executable name and package version for the package being built, +-- and the debug build flag, executable name and package version for the package being built, -- make the best version string we can. Here is the logic: -- -- * Program name, OS and architecture are always shown. @@ -50,7 +50,7 @@ packagemajorversion = intercalate "." $ take 2 $ splitAtElement '.' packageversi -- * (TODO, requires adding --match support to githash: -- If there are tags matching THISPKG-[0-9]*, the latest one is used to calculate patch level -- (number of commits since tag), and if non-zero, it and the branch name are shown.) --- * If the ghcdebug build flag was enabled for the package being built, and for hledger-lib (both are needed), +-- * If the debug build flag was enabled for the package being built, and for hledger-lib (both are needed), -- "ghc-debug support" is shown. -- -- Some example outputs: @@ -64,7 +64,7 @@ packagemajorversion = intercalate "." $ take 2 $ splitAtElement '.' packageversi -- -- The GitInfo if any, fetched by template haskell, is passed down from -- a top-level module, reducing wasteful recompilation. --- The status of the ghcdebug build flag is also passed down, since it is +-- The status of the debug build flag is also passed down, since it is -- specific to each hledger package. -- -- This is used indirectly by at least hledger, hledger-ui, and hledger-web, diff --git a/hledger/package.yaml b/hledger/package.yaml index ab80b9506..0f517b54f 100644 --- a/hledger/package.yaml +++ b/hledger/package.yaml @@ -99,8 +99,8 @@ flags: description: Build with support for multithreaded execution default: true manual: false - ghcdebug: - description: Build with support for attaching a ghc-debug client + debug: + description: Build with GHC 9.10+'s stack traces enabled, and with support for attaching a ghc-debug client default: false manual: true @@ -155,8 +155,8 @@ when: - condition: (!(os(windows))) && (flag(terminfo)) dependencies: - terminfo -- condition: (flag(ghcdebug)) - cpp-options: -DGHCDEBUG +- condition: (flag(debug)) + cpp-options: -DDEBUG dependencies: - ghc-debug-stub >=0.6.0.0 && <0.7 diff --git a/stack.yaml b/stack.yaml index 50f555e83..ba3c86805 100644 --- a/stack.yaml +++ b/stack.yaml @@ -34,7 +34,7 @@ extra-deps: # to silence a warning - wizards-1.0.3@rev:3 -# for dev builds when the ghcdebug flag is enabled: +# for dev builds when the debug flag is enabled: - ghc-debug-convention-0.6.0.0 - ghc-debug-stub-0.6.0.0 diff --git a/stack9.8.yaml b/stack9.8.yaml index 68387ac52..383169390 100644 --- a/stack9.8.yaml +++ b/stack9.8.yaml @@ -9,7 +9,7 @@ packages: - hledger-web extra-deps: -# needed only for dev builds when the ghcdebug flag is enabled: +# needed only for dev builds when the debug flag is enabled: - ghc-debug-convention-0.6.0.0 - ghc-debug-stub-0.6.0.0