[llvm-dev] How to add custom instrumentation?

Martin J. O'Riordan via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 13 15:08:55 PDT 2017


A numeric ID is difficult (or impossible) at compile-time, because at the time of compilation, nothing is known about the functions compiled in other translation units.  So the usual trick is to use a map from the address of the function (which is unique after linking) to other meta-data - the most common being the Dwarf meta-data.  I would usually correlate the IP address of the function to the information in the Dwarf data to give me the whole picture.

The other address - the "call site" - is a bit misleading, as it is usually the address to which the function will "return" rather than the address at which it was called (the return address is easily found).  So for example, given the following:

  void foo () { ... }
  void bar () { ... foo(); ... }

The instrumentation for 'foo' will show the address to which it will return to in 'bar' and not the exact call-site to 'foo'.  Again, by cross correlating this IP information against the address data in the Dwarf information, you can easily determine where the call was made with reasonable accuracy.  A target knowledgeable tool (e.g. debugger) might even be able to further refine this to determine the actual call address with knowledge of the ABI.

I would usually use '-finstrument-functions' with '-g' to get the corresponding address/location information.  Since the IP address of the function is unique, the mapping to a unique ordinal (i.e. ID) is pretty straightforward.  After all, why invent a custom map when there is already a standard definition for in in Dwarf.

Regarding the BB's; not necessarily.  The BB ordering may be changed by the scheduler - for example, in our target we prioritise the BBs in a "trace" based on most probable execution path.  I am not 100% sure where the '__cyg_*' hooks are added in the IR, though I suspect that they occur in the front-end (CLang).  Somebody else can answer this more accurately.  I don't in principal see why the "exit" cannot occur more than once though, so there is no reason why it would be tied to a particular BB, though the "enter" probably is.

In any event, the semantics are the same, the instrumentation 'exit' hook will still tell you when the function exits, what function it is exiting, and where it is returning to regardless of whether it is consolidated to only one BB or replicated across many BBs.

	MartinO

-----Original Message-----
From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Alex Denisov via llvm-dev
Sent: 13 July 2017 21:46
To: llvm-dev <llvm-dev at lists.llvm.org>
Subject: Re: [llvm-dev] How to add custom instrumentation?

Thanks for the hint, I didn’t know about this option. That’s a great reference!

However, I am trying to be a compiler/language agnostic.
Also (for whatever reasons) I need a numeric ID of a function rather then its address.
So the question is still opened.

May I assume that the following always holds:
The first basic block in a function is an entry point and the last basic block in a function is an exit point ?

> On 13. Jul 2017, at 10:57, ORiordan, Martin <martin.oriordan at intel.com> wrote:
> 
> The '-finstrument-functions' option may already be sufficient for your needs.
> 
> When selected this inserts the following two calls on entry-to and exit-from a function:
> 
>  __cyg_profile_func_enter(void* this_fn, void* call_site)
>  __cyg_profile_func_exit(void* this_fn, void* call_site)
> 
> You can then provide a custom implementation of these calls to perform the analysis tasks that you require.
> 
> The two parameters are the address of the function into which the instrumentation is inserted, and the address of the call site to the instrumentation function.
> 
> There is an "Execution Trace" implementation of these instrumentation hooks on GitHub called 'eTrace' that may guide you, though I can't recall the link.
> 
> All the best,
> 
>  MartinO
> 
> -----Original Message-----
> From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of 
> Alex Denisov via llvm-dev
> Sent: Thursday, July 13, 2017 8:57 AM
> To: llvm-dev <llvm-dev at lists.llvm.org>
> Subject: [llvm-dev] How to add custom instrumentation?
> 
> Hi everyone,
> 
> I run some functions using ORC JIT, now I need to add custom instrumentation.
> I want to add two callbacks to each function: ‘enterFunction' at the beginning and ‘leaveFunction' at the end.
> Intuition says that I could ‘just' insert CallInst's to the first and the last basic blocks in the function.
> 
> Am I correct? Are there any other/better way to do this? Is there anything special I need to be aware of?
> 
> Thank you.
> --
> AlexDenisov
> Software Engineer, https://lowlevelbits.org
> 
> --------------------------------------------------------------
> Intel Research and Development Ireland Limited Registered in Ireland 
> Registered Office: Collinstown Industrial Park, Leixlip, County 
> Kildare Registered Number: 308263
> 
> 
> This e-mail and any attachments may contain confidential material for 
> the sole use of the intended recipient(s). Any review or distribution 
> by others is strictly prohibited. If you are not the intended 
> recipient, please contact the sender and delete all copies.

--
AlexDenisov
Software Engineer, https://lowlevelbits.org




More information about the llvm-dev mailing list