Getting location of closure/function value

Hi.

I am not sure if this is possible or even makes sense: Assuming I have build with -g and I have, either in a C stub or in OCaml code, a value which is a closure/function. Can I somehow poll/extract the “location” (source code location) of that closure/function from the value?

The use case is, from code which operates on sequences of closures/functions, to log what is being called, without having to insert this logging in the actual underlying closures/functions.

Thanks for any input.

You can play with these magic values. But they are a bit limited and noisy to use.

One pattern which I first saw used in eio is to have it as optional variables, so you can selectively use them.

let f ?__POS__ f x = …

let () = f 3 (* untraced *) 
let () = f 3 ~__POS__ (* traced *) 

In the future you may get nicer things, but it looks stalled at the moment.

1 Like

Note that this will provide you with the location of the callsite, while the OP is more interested by the location of the function definition, if I understand correctly.

Not easily. But see GitHub - let-def/owee: OCaml library to work with DWARF format for one approach.

Cheers,
Nicolas

Well that’s easier. You just use __POS__ inside your function definition. (But the title of the post is about callsites)

Of course, but the OP seems to be after a solution that does not require modifying the called function.

Indeed, it is a bit confusing…

Cheers,
Nicolas

Sorry, that is my fault. I need to know the location of the called closure/function - I will update the title.

Thank you both for your input, I will look into the DWARF link.