title: How to properly write a SIGPROF handler that invokes AsyncGetCallTrace?
body: <p>I am writing a short and simple profiler (in C), which is intended to print out stack traces for threads in various Java clients at regular intervals. I have to use the undocumented function <code>AsyncGetCallTrace</code> instead of <code>GetStackTrace</code> to minimize intrusion and allow for stack traces regardless of thread state. The source code for the function can be found here: <a href="http://download.java.net/openjdk/jdk6/promoted/b20/openjdk-6-src-b20-21_jun_2010.tar.gz" rel="nofollow noreferrer">http://download.java.net/openjdk/jdk6/promoted/b20/openjdk-6-src-b20-21_jun_2010.tar.gz</a>
  in <code>hotspot/src/share/vm/prims/forte.cpp</code>. I found some man pages documenting JVMTI, signal handling, and timing, as well as a blog with details on how to set up the <code>AsyncGetCallTrace</code> call: <a href="http://jeremymanson.blogspot.com/2007/05/profiling-with-jvmtijvmpi-sigprof-and.html" rel="nofollow noreferrer">http://jeremymanson.blogspot.com/2007/05/profiling-with-jvmtijvmpi-sigprof-and.html</a></p>
  <p>What this blog is missing is the code to actually invoke the function within the signal handler (the author assumes the reader can do this on his/her own). I am asking for help in doing exactly this. I am not sure how and where to create the <code>struct ASGCT_CallTrace</code> (and the internal <code>struct ASGCT_CallFrame</code>), as defined in the aforementioned file <code>forte.cpp</code>. The <code>struct ASGCT_CallTrace</code> is one of the parameters passed to <code>AsyncGetCallTrace</code>, so I do need to create it, but I don't know how to obtain the correct values for its fields: <code>JNIEnv *env_id</code>, <code>jint num_frames</code>, and <code>JVMPI_CallFrame *frames</code>. Furthermore, I do not know what the third parameter passed to <code>AsyncGetCallTrace (void* ucontext)</code> is supposed to be?</p>
  <p>The above problem is the main one I am having. However, other issues I am faced with include:</p>
  <ol>
  <li><p><code>SIGPROF</code> doesn't seem to be raised by the timer exactly at the specified intervals, but rather a bit less frequently. That is, if I set the timer to send a <code>SIGPROF</code> every second (1 sec, 0 usec), then in a 5 second run, I am getting fewer than 5 <code>SIGPROF</code> handler outputs (usually 1-3)</p>
  </li>
  <li><p><code>SIGPROF</code> handler outputs do not appear at all during a <code>Thread.sleep</code> in the Java code. So, if a <code>SIGPROF</code> is to be sent every second, and I have <code>Thread.sleep(5000);</code>, I will not get any handler outputs during the execution of that code.</p>
  </li>
  </ol>
  <p>Any help would be appreciated. Additional details (as well as parts of code and sample outputs) will be posted upon request.</p>