caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Debugging C and OCaml
@ 2010-03-01 15:10 Jianzhou Zhao
  2010-03-01 15:19 ` [Caml-list] " Mark Shinwell
  0 siblings, 1 reply; 9+ messages in thread
From: Jianzhou Zhao @ 2010-03-01 15:10 UTC (permalink / raw)
  To: caml-list

My main program is C++. It uses C functions to call
OCaml functions, and these OCaml functions also call
C functions and wrapped C++ functions sometimes.

I can debug from C++, but it stops when it meets
an OCaml binding. Does anyone know an efficient
solution about how to debug across different languages?

Thanks in advance.
-- 
Jianzhou


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Debugging C and OCaml
  2010-03-01 15:10 Debugging C and OCaml Jianzhou Zhao
@ 2010-03-01 15:19 ` Mark Shinwell
  2010-03-01 19:24   ` rixed
  2010-03-02  0:48   ` Jianzhou Zhao
  0 siblings, 2 replies; 9+ messages in thread
From: Mark Shinwell @ 2010-03-01 15:19 UTC (permalink / raw)
  To: Jianzhou Zhao; +Cc: caml-list

On Mon, Mar 01, 2010 at 10:10:44AM -0500, Jianzhou Zhao wrote:
> My main program is C++. It uses C functions to call
> OCaml functions, and these OCaml functions also call
> C functions and wrapped C++ functions sometimes.
> 
> I can debug from C++, but it stops when it meets
> an OCaml binding. Does anyone know an efficient
> solution about how to debug across different languages?

Is this native code compilation?

If so, you should be able to use gdb to debug into the Caml bindings at the
assembly level -- does that work?  Unfortunately at the moment there is no Caml
compiler support for emitting the debug info required by gdb to debug at the
language level.  That said, ocamlopt-compiled assembly code is fairly easy to
read, and you should be able to get something resembling a backtrace using
"where".

Mark


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Debugging C and OCaml
  2010-03-01 15:19 ` [Caml-list] " Mark Shinwell
@ 2010-03-01 19:24   ` rixed
  2010-03-01 19:27     ` Mark Shinwell
  2010-03-02  0:48   ` Jianzhou Zhao
  1 sibling, 1 reply; 9+ messages in thread
From: rixed @ 2010-03-01 19:24 UTC (permalink / raw)
  To: caml-list

-[ Mon, Mar 01, 2010 at 03:19:41PM +0000, Mark Shinwell ]----
>   That said, ocamlopt-compiled assembly code is fairly easy to
> read, and you should be able to get something resembling a backtrace using
> "where".

What's "where" ?


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Debugging C and OCaml
  2010-03-01 19:24   ` rixed
@ 2010-03-01 19:27     ` Mark Shinwell
  2010-03-02  9:50       ` rixed
  0 siblings, 1 reply; 9+ messages in thread
From: Mark Shinwell @ 2010-03-01 19:27 UTC (permalink / raw)
  To: rixed; +Cc: caml-list

On Mon, Mar 01, 2010 at 08:24:32PM +0100, rixed@happyleptic.org wrote:
> -[ Mon, Mar 01, 2010 at 03:19:41PM +0000, Mark Shinwell ]----
> >   That said, ocamlopt-compiled assembly code is fairly easy to
> > read, and you should be able to get something resembling a backtrace using
> > "where".
> 
> What's "where" ?

A gdb command.

Mark


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Debugging C and OCaml
  2010-03-01 15:19 ` [Caml-list] " Mark Shinwell
  2010-03-01 19:24   ` rixed
@ 2010-03-02  0:48   ` Jianzhou Zhao
  2010-03-02  0:52     ` Jianzhou Zhao
  2010-03-02  7:42     ` Mark Shinwell
  1 sibling, 2 replies; 9+ messages in thread
From: Jianzhou Zhao @ 2010-03-02  0:48 UTC (permalink / raw)
  To: Mark Shinwell; +Cc: caml-list

On Mon, Mar 1, 2010 at 10:19 AM, Mark Shinwell <mshinwell@janestreet.com> wrote:
> On Mon, Mar 01, 2010 at 10:10:44AM -0500, Jianzhou Zhao wrote:
>> My main program is C++. It uses C functions to call
>> OCaml functions, and these OCaml functions also call
>> C functions and wrapped C++ functions sometimes.
>>
>> I can debug from C++, but it stops when it meets
>> an OCaml binding. Does anyone know an efficient
>> solution about how to debug across different languages?
>
> Is this native code compilation?

I compiled OCaml code into *.o by 'ocamlc -custom -output-obj...',
and then linked it with *.o from C and C++ code.
I think this isnt a native compilation.

My main C++ program is eventually compiled into a shared library.
If I do 'ocamlopt', ld reports:
/usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../x86_64-suse-linux/bin/ld:
/mnt/eclipse/acg/users/jianzhou/tools/ocaml-3.11.2//lib/ocaml/libasmrun.a(amd64.o):
relocation R_X86_64_PC32 against symbol `caml_last_return_address' can
not be used when making a shared object; recompile with -fPIC

So I am doing ''ocamlc -custom' for the time being.

>
> If so, you should be able to use gdb to debug into the Caml bindings at the
> assembly level -- does that work?  Unfortunately at the moment there is no Caml
> compiler support for emitting the debug info required by gdb to debug at the
> language level.  That said, ocamlopt-compiled assembly code is fairly easy to
> read, and you should be able to get something resembling a backtrace using
> "where".

Now I can debug into OCaml assembly from C, and its stack frames.

#0  0x00007ffff6cad3f4 in caml_interprete ()
#1  0x00007ffff6caa54f in caml_callbackN_exn ()
#2  0x00007ffff6caa64a in caml_callback_exn ()
#3  0x00007ffff6caa658 in caml_callback ()
...

It seems that OCaml runtime is interpreting via 'caml_interprete' the
OCaml function which C
calls at runtime. If this is true, we cannot really debug that OCaml
function, but we can
see how it is interpreted. But I guess we can debug the real assembly if I do
native compilation.

>
> Mark
>



-- 
Jianzhou


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Debugging C and OCaml
  2010-03-02  0:48   ` Jianzhou Zhao
@ 2010-03-02  0:52     ` Jianzhou Zhao
  2010-03-02  7:36       ` Mark Shinwell
  2010-03-02  7:42     ` Mark Shinwell
  1 sibling, 1 reply; 9+ messages in thread
From: Jianzhou Zhao @ 2010-03-02  0:52 UTC (permalink / raw)
  To: Mark Shinwell; +Cc: caml-list

On Mon, Mar 1, 2010 at 7:48 PM, Jianzhou Zhao <jianzhou@seas.upenn.edu> wrote:
> On Mon, Mar 1, 2010 at 10:19 AM, Mark Shinwell <mshinwell@janestreet.com> wrote:
>> On Mon, Mar 01, 2010 at 10:10:44AM -0500, Jianzhou Zhao wrote:
>>> My main program is C++. It uses C functions to call
>>> OCaml functions, and these OCaml functions also call
>>> C functions and wrapped C++ functions sometimes.
>>>
>>> I can debug from C++, but it stops when it meets
>>> an OCaml binding. Does anyone know an efficient
>>> solution about how to debug across different languages?
>>
>> Is this native code compilation?
>
> I compiled OCaml code into *.o by 'ocamlc -custom -output-obj...',
> and then linked it with *.o from C and C++ code.
> I think this isnt a native compilation.
>
> My main C++ program is eventually compiled into a shared library.
> If I do 'ocamlopt', ld reports:
> /usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../x86_64-suse-linux/bin/ld:
> /mnt/eclipse/acg/users/jianzhou/tools/ocaml-3.11.2//lib/ocaml/libasmrun.a(amd64.o):
> relocation R_X86_64_PC32 against symbol `caml_last_return_address' can
> not be used when making a shared object; recompile with -fPIC
>
> So I am doing ''ocamlc -custom' for the time being.
>
>>
>> If so, you should be able to use gdb to debug into the Caml bindings at the
>> assembly level -- does that work?  Unfortunately at the moment there is no Caml
>> compiler support for emitting the debug info required by gdb to debug at the
>> language level.  That said, ocamlopt-compiled assembly code is fairly easy to
>> read, and you should be able to get something resembling a backtrace using
>> "where".
>
> Now I can debug into OCaml assembly from C, and its stack frames.
>
> #0  0x00007ffff6cad3f4 in caml_interprete ()
> #1  0x00007ffff6caa54f in caml_callbackN_exn ()
> #2  0x00007ffff6caa64a in caml_callback_exn ()
> #3  0x00007ffff6caa658 in caml_callback ()
> ...
>
> It seems that OCaml runtime is interpreting via 'caml_interprete' the
> OCaml function which C
> calls at runtime. If this is true, we cannot really debug that OCaml
> function, but we can
> see how it is interpreted. But I guess we can debug the real assembly if I do
> native compilation.

If we do native compilation, will each OCaml function be assembled to
a function in assemly with a readable name? That will help a lot while
reading the assemly code.

>
>>
>> Mark
>>
>
>
>
> --
> Jianzhou
>



-- 
Jianzhou


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Debugging C and OCaml
  2010-03-02  0:52     ` Jianzhou Zhao
@ 2010-03-02  7:36       ` Mark Shinwell
  0 siblings, 0 replies; 9+ messages in thread
From: Mark Shinwell @ 2010-03-02  7:36 UTC (permalink / raw)
  To: Jianzhou Zhao; +Cc: caml-list

On Mon, Mar 01, 2010 at 07:52:01PM -0500, Jianzhou Zhao wrote:
> > It seems that OCaml runtime is interpreting via 'caml_interprete' the
> > OCaml function which C
> > calls at runtime. If this is true, we cannot really debug that OCaml
> > function, but we can
> > see how it is interpreted. But I guess we can debug the real assembly if I do
> > native compilation.
> 
> If we do native compilation, will each OCaml function be assembled to
> a function in assemly with a readable name? That will help a lot while
> reading the assemly code.

The names are mangled, but it's pretty easy to work out what they correspond
to, except in the case of anonymous functions where you may need to do some
detective work.

Mark


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Debugging C and OCaml
  2010-03-02  0:48   ` Jianzhou Zhao
  2010-03-02  0:52     ` Jianzhou Zhao
@ 2010-03-02  7:42     ` Mark Shinwell
  1 sibling, 0 replies; 9+ messages in thread
From: Mark Shinwell @ 2010-03-02  7:42 UTC (permalink / raw)
  To: Jianzhou Zhao; +Cc: caml-list

On Mon, Mar 01, 2010 at 07:48:56PM -0500, Jianzhou Zhao wrote:
> On Mon, Mar 1, 2010 at 10:19 AM, Mark Shinwell <mshinwell@janestreet.com> wrote:
> I compiled OCaml code into *.o by 'ocamlc -custom -output-obj...',
> and then linked it with *.o from C and C++ code.
> I think this isnt a native compilation.

Agreed.

> My main C++ program is eventually compiled into a shared library.
> If I do 'ocamlopt', ld reports:
> /usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../x86_64-suse-linux/bin/ld:
> /mnt/eclipse/acg/users/jianzhou/tools/ocaml-3.11.2//lib/ocaml/libasmrun.a(amd64.o):
> relocation R_X86_64_PC32 against symbol `caml_last_return_address' can
> not be used when making a shared object; recompile with -fPIC

You need to build the runtime as position-independent code.  Unfortunately
this is non-trivial.  I suggest you try looking at this thread which may
help you:

http://caml.inria.fr/pub/ml-archives/caml-list/2010/01/9894615c02692ca5b86e2a891d8cf324.en.html

Mark


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Debugging C and OCaml
  2010-03-01 19:27     ` Mark Shinwell
@ 2010-03-02  9:50       ` rixed
  0 siblings, 0 replies; 9+ messages in thread
From: rixed @ 2010-03-02  9:50 UTC (permalink / raw)
  To: caml-list

-[ Mon, Mar 01, 2010 at 07:27:03PM +0000, Mark Shinwell ]----
> > What's "where" ?
> 
> A gdb command.

Sorry for the noise I failed to get info for this command.
Knew it under name "bt" BTW.



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-03-02  9:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-01 15:10 Debugging C and OCaml Jianzhou Zhao
2010-03-01 15:19 ` [Caml-list] " Mark Shinwell
2010-03-01 19:24   ` rixed
2010-03-01 19:27     ` Mark Shinwell
2010-03-02  9:50       ` rixed
2010-03-02  0:48   ` Jianzhou Zhao
2010-03-02  0:52     ` Jianzhou Zhao
2010-03-02  7:36       ` Mark Shinwell
2010-03-02  7:42     ` Mark Shinwell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).