通过cabal repl调试/步入包模块

3

我有以下来自Preventing caching of computation in Criterion benchmark的代码,我的目标是能够直接从main步进到Criterion.Main中的defaultMain函数:

{-# OPTIONS -fno-full-laziness #-}
{-# OPTIONS_GHC -fno-cse #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import Criterion.Main
import Data.List

num :: Int
num = 100000000

lst :: a -> [Int]
lst _ = [1,2..num]

myadd :: Int -> Int -> Int
myadd !x !y = let !result = x + y in
  result

mysum = foldl' myadd 0

main :: IO ()
main = defaultMain [
  bgroup "summation" 
    [bench "mysum" $ whnf (mysum . lst) ()]
  ]

而 cabal 文件是:

name:                test
version:             0.1.0.0
build-type:          Simple
cabal-version:       >=1.10

executable test
  main-is:             Main.hs
  build-depends:       base >=4.8 && <4.9,
                       criterion==1.1.0.0
  default-language:    Haskell2010
  ghc-options:         "-O3"

(使用ghc 7.10.1和cabal 1.22.0.0)。
如果我在cabal repl中尝试在标准中设置断点,则会出现以下错误:
*Main> :break Criterion.Main.defaultMain
cannot set breakpoint on defaultMain: module Criterion.Main is not interpreted

此外,如果我尝试添加此软件包,则会出现以下错误:
*Main> :add *Criterion

<no location info>: module ‘Criterion’ is a package module
Failed, modules loaded: Main.

如果我在目录中执行 git clone https://github.com/bos/criterion 命令,然后在 cabal 文件中添加以下两行:
other-modules:       Criterion
hs-source-dirs:      .
                 ./criterion

然后我执行cabal build时,出现以下错误:

criterion/Criterion/IO.hs:23:0:
     error: missing binary operator before token "("
     #if MIN_VERSION_binary(0, 6, 3)

我怀疑我必须对准则小组的cabal文件进行全面合并,这感觉有些过度。是否有更简单的方法让我在Criterion中设置断点,以便我可以直接从我的源代码步进(在cabal repl / ghci中进行调试)到Criterion的源代码?谢谢。 附:有一个相关问题: Debugging IO in a package module inside GHCi ,但不幸的是它没有帮助。

1
我不知道cabal文件部分的答案,但据我所知,GHCi只能调试其内部字节码格式的代码。特别是,您要避免使用cabal build进行本地编译,因为生成的.o文件将默认用于字节码。 - Ørjan Johansen
@ØrjanJohansen 谢谢,我之前通过“合并” cabal 文件成功调试了 cassava,这使得我可以无缝地从我的应用程序进入 cassava。然而,使用相同的方法尝试调试 criterion 时出现了 criterion-1.1.0.0/cbits/time-posix.o: relocation R_X86_64_PC32 against undefined symbol clock_gettime@@GLIBC_2.2.5 can not be used when making a shared object; recompile with -fPIC 的错误,这是我目前正在尝试解决的问题! - artella
@ØrjanJohansen:嗯,解决上面评论中的time-posix.o错误的方法似乎是添加选项cc-options: -fPIC。因此,我可以通过合并cabal文件并将cc-options: -fPIC添加到生成的cabal文件中来实现所需的行为。 - artella
太好了!这听起来像是一个普遍有用的提示。如果你想的话,可以发布一个回答来描述你是如何解决它的。 - Ørjan Johansen
@ØrjanJohansen:我已经发布了下面的方法。谢谢。 - artella
1个回答

2

这是我实现能够(在 cabal repl 中)从我的代码步进到 criterion 源代码的期望目标的方法:

  1. First do :

    mkdir /tmp/testCrit
    cd /tmp/testCrit
    
  2. Download criterion-1.1.0.0.tar.gz

  3. Unzip into /tmp/testCrit, so we should have /tmp/testCrit/criterion-1.1.0.0. In this directory we have Criterion.hs etc.

  4. Then jump into the folder containing the criterion source and do :

    cd /tmp/testCrit/criterion-1.1.0.0
    cabal sandbox init
    cabal install -j
    

    Note that this creates a directory : /tmp/testCrit/criterion-1.1.0.0/dist/dist-sandbox-782e42f0/build/autogen which we shall use later

  5. Back in /tmp/testCrit create a Main.hs file containing the benchmark code above and also the cabal file above, but merge it with the criterion cabal file contained in /tmp/testCrit/criterion-1.1.0.0 in the following way. Note the main new additions are the lines :

    cc-options: -fPIC
    

    which allows one to run it in cabal repl, and the following lines :

    hs-source-dirs: 
      ./
      ./criterion-1.1.0.0
      ./criterion-1.1.0.0/dist/dist-sandbox-782e42f0/build/autogen
    

    The full cabal file should then look like :

      name:                test
      version:             0.1.0.0
      build-type:          Simple
      cabal-version:       >=1.10
    
      executable test
        main-is:             Main.hs
        build-depends:       
          base >=4.8 && <4.9,
          aeson >= 0.8,
          ansi-wl-pprint >= 0.6.7.2,
          base >= 4.5 && < 5,
          binary >= 0.5.1.0,
          bytestring >= 0.9 && < 1.0,
          cassava >= 0.3.0.0,
          containers,
          deepseq >= 1.1.0.0,
          directory,
          filepath,
          Glob >= 0.7.2,
          hastache >= 0.6.0,
          mtl >= 2,
          mwc-random >= 0.8.0.3,
          optparse-applicative >= 0.11,
          parsec >= 3.1.0,
          statistics >= 0.13.2.1,
          text >= 0.11,
          time,
          transformers,
          transformers-compat >= 0.4,
          vector >= 0.7.1,
          vector-algorithms >= 0.4
        default-language:    Haskell2010
        ghc-options:         "-O3"
        c-sources: 
          ./criterion-1.1.0.0/cbits/cycles.c
          ./criterion-1.1.0.0/cbits/time-posix.c
        hs-source-dirs: 
          ./
          ./criterion-1.1.0.0
          ./criterion-1.1.0.0/dist/dist-sandbox-782e42f0/build/autogen
        cc-options: -fPIC
    
  6. Then in the main directory do :

    cd /tmp/testCrit/
    cabal sandbox init
    cabal install -j
    
  7. Then we can spin up a cabal repl and step directly into criterion from our Main.hs code :

    *Main> :break Criterion.Main.defaultMain
    Breakpoint 0 activated at criterion-1.1.0.0/Criterion/Main.hs:79:15-43
    *Main> main
    Stopped at criterion-1.1.0.0/Criterion/Main.hs:79:15-43
    _result :: [Benchmark] -> IO () = _
    [criterion-1.1.0.0/Criterion/Main.hs:79:15-43] *Main> :step
    Stopped at criterion-1.1.0.0/Criterion/Main.hs:(131,1)-(147,39)
    _result :: IO () = _
    [criterion-1.1.0.0/Criterion/Main.hs:(131,1)-(147,39)] *Main> :step
    Stopped at criterion-1.1.0.0/Criterion/Main.hs:(131,29)-(147,39)
    _result :: IO () = _
    bs :: [Benchmark] = [_]
    defCfg :: Criterion.Types.Config = _
    [criterion-1.1.0.0/Criterion/Main.hs:(131,29)-(147,39)] *Main> :step
    Stopped at criterion-1.1.0.0/Criterion/Main.hs:132:10-37
    _result :: IO Criterion.Main.Options.Mode = _
    defCfg :: Criterion.Types.Config = _
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接