Skip to content

Instantly share code, notes, and snippets.

@qnikst
Created June 4, 2016 20:15
Show Gist options
  • Save qnikst/f807cdc412d6b9c3c3effeaf31d544ae to your computer and use it in GitHub Desktop.
Save qnikst/f807cdc412d6b9c3c3effeaf31d544ae to your computer and use it in GitHub Desktop.
gcc -c tc.c -g -ggdb -c
ghc T.hs tc.o -g
(gdb) bt
#0 0x00007fbbf1c0f380 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:84
#1 0x00007fbbf1ba6c08 in _IO_new_file_underflow (fp=0x7fbbf1ec64e0 <_IO_2_1_stdin_>) at fileops.c:592
#2 0x00007fbbf1ba7a6e in __GI__IO_default_uflow (fp=0x7fbbf1ec64e0 <_IO_2_1_stdin_>) at genops.c:413
#3 0x00007fbbf1b8aa73 in _IO_vfscanf_internal (s=<optimized out>, format=<optimized out>, argptr=argptr@entry=0x7fff40865458,
errp=errp@entry=0x0) at vfscanf.c:634
#4 0x00007fbbf1b98a4b in __isoc99_scanf (format=<optimized out>) at isoc99_scanf.c:37
#5 0x0000000000405d4f in another () at tc.c:6
#6 0x000000000040569d in r2nL_info ()
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign
import Foreign.C
foreign import ccall "wrapper" cwrap :: (CInt -> IO CInt) -> IO (FunPtr (CInt -> IO CInt))
foreign import ccall "inside" inside :: FunPtr (CInt -> IO CInt) -> IO ()
foreign import ccall "another" another :: IO ()
waiting :: CInt -> IO CInt
waiting i = print i >> another >> return 27
main = do
p <- cwrap waiting
inside p
#include <stdio.h>
void another() {
int i;
printf("here\n");
scanf("%i", &i);
}
void other(int (*f)(int)) {
f(42);
}
void inside(int (*f)(int)) {
other(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment