18

I have a nonterminating expression in Haskell. I want to debug and inspect the reason why it is not terminating. A technique I learned is to use the following in GHCi:

:set -fbreak-on-exception
:trace nonterminating_expression
^C
:hist 50

So I can see the instructions that are running in the infinite computation. The problem is that I would like to continue the computation with :step, ignoring the interrupt. Can I do that?

Any other solutions for debugging nonterminating computations? (History larger than 50 records or other practices to help the task.)

Boldizsár Németh
  • 1,847
  • 13
  • 20
  • Are you familiar with how to set breakpoints in arbitrary code? I would put a break somewhere in the infinite loop and use that to step through an iteration or so. – Maxander Sep 04 '13 at 01:49
  • Yes, but the problem is that I would get a lot of false calls. For example, I think I've got the location of the infinite cycle, but every function call only leads to infinite cycle in 1% of the time. – Boldizsár Németh Sep 04 '13 at 13:11

1 Answers1

1

One thing I've done in the past is use unsafePerformIO to break whenever I press a key:

Let's say t is your original expression, insert x in the expression:

import System.IO
import System.IO.Unsafe

t  i = do print i;    t  (i+1)
t2 i = do print i; x; t2 (i+1)

x = do r <- hReady stdin
       if r then do a<-hGetChar stdin
                    print a -- break on this line
            else print "" 

Then at the ghci prompt:

*Main> :break 9
*Main> t2 0

Press a keyboard key

*Main> :cont

resumes where you left off.

ja.
  • 4,245
  • 20
  • 22