title: Current function address - x64
body: <p>I am working on this small project where I'd like to generate the call graph of an application - I am not planning to do anything complex, it is mainly for fun/experience. I am working on x64 platform.</p>
  
  <p>The first goal I set myself is to be able to measure the time spent in each function of my test application. So far my strategy has been to use __penter()_ and __pexit()_ - __penter()_ is a function that will get called at the start of every method or function and conversely __pexit()_ will get called at the end of every method or function.</p>
  
  <p>With these two functions I can record each function call as well as the time spent in each of them. What I'd like to do next is get the address of each function being called.<br>
  For example if we consider the following callstack (very simplified):  </p>
  
  <pre><code>
  main()  
  ....myFunction()  
  ........_penter()
  </code> </pre>
  
  <p>I am in __penter_ and I want to get the address of the calling function, <em>myFunction()</em>. I already found a way to do it in the case of non-leaf functions, I simply use <a href="http://msdn.microsoft.com/en-us/library/ms680597.aspx" rel="nofollow noreferrer">RtlLookupFunctionEntry</a>. However this solution doesn't seem to work for leaf functions because they don't provide any unwind data.</p>
  
  <p>One thing I was thinking about is to go up one more level in the callstack, in <em>main()</em>, and decode the CALL procedure manually - that would involve getting a pointer to the instruction calling <em>myFunction()</em>.</p>
  
  <p>I was wondering if any of you would know how to get the address of the current function in the case of leaf functions. I have this gut feeling that my current approach is a bit overcomplicated.</p>
  
  <p>Thanks,</p>
  
  <p>Clem</p>