[racket] Help debugging a ffi crash

From: Eric Dobson (eric.n.dobson at gmail.com)
Date: Sun Sep 28 14:15:33 EDT 2014

That is almost surely it, thanks for the second pair of eyes. I had
similar issues with a different type that I thought was a pointer but
was actually a struct, but that one I couldn't even get a single call
to work so it was much more obvious something was up.

On Sun, Sep 28, 2014 at 11:08 AM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> Looking at
>   http://clang.llvm.org/doxygen/CXString_8h_source.html
> it seems that CXString as returned by clang_getCursorSpelling() is not
> a pointer:
>  typedef struct {
>    const void *data;
>    unsigned private_flags;
>  } CXString;
> If that's right, I'm a little surprised that `cursor-spelling` works
> --- but when you get representation wrong, strange things can happen,
> including something working when it shouldn't.
> Am I looking at the right library/definitions?
> At Sun, 28 Sep 2014 10:48:06 -0700, Eric Dobson wrote:
>> I'm trying to debug an FFI crash that I'm seeing, and because it is
>> dealing with C code the error just presents as a segfault. I believe I
>> have tracked down what is causing the problem, but don't understand
>> how it could be doing so.
>> I have two racket functions which take a "cursor" (the foreign
>> libraries object) and return a string representation of it, which I'm
>> trying to use for debugging.
>> (define raw-clang-get-cstring
>>   (get-ffi-obj "clang_getCString" lib-clang
>>     (_fun _pointer -> _string)))
>> (define raw-cursor-spelling
>>  (get-ffi-obj "clang_getCursorSpelling" lib-clang
>>    (_fun _CXCursor -> _pointer)))
>> (define (cursor-spelling c)
>>   (raw-clang-get-cstring (raw-cursor-spelling c)))
>> (define cursor-spelling2
>>  (get-ffi-obj "clang_getCursorSpelling" lib-clang
>>    (_fun _CXCursor -> (make-ctype _pointer values (λ (v)
>> (raw-clang-get-cstring v))))))
>> If I use cursor-spelling, I have not been able to trigger a crash. But
>> if I use cursor-spelling2 I can reliably trigger a crash.
>> Is there anything obvious on how these functions are different?
>> Because they look to me like they should be doing the same thing. If
>> it would be helpful I can try to get my code in a portable enough
>> shape so that it will work/crash on another machine.
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users

Posted on the users mailing list.