【问题标题】:Using ghci debugger in a wai/warp app在 wai/warp 应用程序中使用 ghci 调试器
【发布时间】:2015-01-05 19:14:16
【问题描述】:

这是一个简单的wai/warp 程序,因此我可以了解ghci 调试器的实际工作原理:-

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types (status200)
import Network.Wai.Handler.Warp (run)
import Debug.Trace (trace)

myApp req respond = do 
    let x = 1 {- line 8 -}
    putStrLn "processing request" {- line 9 -}
    putStrLn "Hoooray!" {- line 10 -}
    respond $ responseLBS status200 [("Content-Type", "text/plain")] "Hello World" 

main = run 3000 myApp 

ghci,我首先加载这个程序(例如:load hellowai.hs)。

然后,我在第 9 行和第 10 行设置断点

:break 9
:break 10

然后,在ghci 中,我执行main

然后我在浏览器上或使用 curl(当然没关系)运行 localhost:3000,我的程序在第 9 行按预期中断。

如何打印(内省)x 以及如何内省 req

我尝试使用 :printghci 只是抱怨“不在范围内”。

Stopped at hellowai.hs:9:5-33
_result :: IO () = _
[hellowai.hs:9:5-33] *Main> :print x

Top level: Not in scope: ‘x’
[hellowai.hs:9:5-33] *Main> :print req

Top level:
    Not in scope: ‘req’
    Perhaps you meant one of these:
      ‘rem’ (imported from Prelude), ‘seq’ (imported from Prelude)
[hellowai.hs:9:5-33] *Main>

【问题讨论】:

    标签: haskell ghci haskell-warp


    【解决方案1】:

    使用 GHCi 命令:print ...(或:sprint ...),您可以打印出范围内的变量。然而,这将只打印评估值(回想一下 Haskell 是惰性的)。

    要评估和打印,只需直接使用变量的名称。您可以使用:show bindings 命令获取范围内的变量列表。

    如果您在范围内看不到变量,则它们可能已被编译器优化掉。在您的代码中,您不使用x,因此它可能会在编译期间被删除。另外,代码如

    foo = do
      let x = 3
      print "aa"
      print "bb"
      print x
    

    可能被处理为(除了行号)

    foo = do
      print "aa"
      print "bb"
      let x = 3
      print x
    

    因此,直到最后一行,您才会在范围内看到 x。在这种情况下,请使用:step 稍微提前执行,直到看到x 出现。

    【讨论】:

    • 这就是我尝试过的 - :print x,我得到了 Top level: Not in scope: ‘x’(上面已编辑以反映我确实尝试过使用 :print
    • 我明白了。当我在代码中明确使用这些变量 xreq 时,它会起作用。感谢你的回答。这确实有效。我设法得到Request {requestMethod = "GET", httpVersion = HTTP/1.1, rawPathInfo = "/", rawQueryString = "", requestHeaders = [("User-Agent","curl/7.39.0"),("Host","127.0.0.1:3000"),("Accept","*/*")], isSecure = False, remoteHost = 127.0.0.1:62615, pathInfo = [], queryString = [], requestBody = <IO ByteString>, vault = <Vault>, requestBodyLength = KnownLength 0, requestHeaderHost = Just "127.0.0.1:3000", requestHeaderRange = Nothing} Stopped at hellowai.hs:12:5-23
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多