我正在编写JVMTI代码来剖析Java程序,这些程序通常需要使用函数AsyncGetCallTrace以固定时间间隔从随机线程获取堆栈跟踪。 因此,我可以获得CallTrace结构,每个结构都包含一个CallFrame结构数组,其中包含有关堆栈跟踪中各个帧的数据。 具体来说,这些数据由jmethodID method_id(框架所在的java方法的ID)和jint lineno(.class文件中方法的BCI,据我了解文档)组成。 我似乎无法找到一种方法来使用JVMTI框架将此“lineno”转换为相应的源代码行号(至少位于/ usr / lib / jvm / java-6-sun / include中的文件jvmti.h在Linux上)。 事实上,即使在JVMTI框架之外,到目前为止我能在网上找到的唯一东西就在这里: http : //jakarta.apache.org/bcel/apidocs/org/apache/bcel/classfile/LineNumberTable.html ,但是即使这可能不会做我想做的事,并且需要额外的安装,并且需要我使用单独的Java程序来处理由C ++ JMVTI代码生成的数据。
如果有人知道如何将BCI转换为JVMTI中的源代码行号(甚至以任何方式),请帮助!
[如果有人知道这个领域,请让我知道,因为我还有几个问题要问这个过程。]
I am writing JVMTI code to profile Java programs, which mostly entails obtaining stack traces from random threads at fixed time intervals using the function AsyncGetCallTrace. Thus, I am able to obtain CallTrace structures, each of which contains an array of CallFrame structures, which contain data about individual frames in a stack trace. Specifically, these data consist of: jmethodID method_id (the ID of the java method where the frame is located), and: jint lineno (the BCI of the method in the .class file, as far as I understand the documentation). I cannot seem to find a way to convert this "lineno" into the corresponding source code line number using the JVMTI framework (see file jvmti.h, located in /usr/lib/jvm/java-6-sun/include, at least on Linux). In fact, even outside the JVMTI framework, the only thing I could find on the web so far was here: http://jakarta.apache.org/bcel/apidocs/org/apache/bcel/classfile/LineNumberTable.html, but even this may not be doing what I would like, and requires additional installation, AND requires me to process the data, which was generated by C++ JMVTI code, using a separate Java program.
If anyone knows how to convert BCI to source code line numbers from within JVMTI (or even in any way), please help!
[If someone knows this field well, please let me know, since I have a few more questions to ask about the process.]
最满意答案
我想我有点想通了。 使用的主要方法是jvmti-> GetLineNumberTable(...),它填充了一个jvmtiLineNumberEntry数组。 给定BCI行号n(要映射到源行号),可以测试哪个int i是:jvmtiLineNumberEntryArray [i] <= n <jvmtiLineNumberEntryArray [i + 1]。 这个int就是所需的对应源代码行号。
一个问题是AsyncGetCallTrace出于某种原因始终如一地返回奇怪的BCI,因此虽然映射提供了精确的源代码行号,但它们仍不准确 ,因为原始BCI不准确。 为什么这是,我不知道。 我希望使用Sun Studio Profiler,它也使用AsyncGetCallTrace来测试返回的行号是否与我的分析器相同。 在这种情况下,AsyncGetCallTrace函数是不准确的。 但到目前为止,使用Sun Studio变成了自己的挑战。 如果有人知道如何使用此工具,请帮助!
更大的问题是Java方法通常是内联的,所以行号并不总是正确映射。 事实上,这可能是上述段落中描述的问题的原因,尽管这基于我所看到的数字似乎不太可能。 以下是有关解决内联问题的一些信息: http : //developer.amd.com/documentation/articles/pages/JVMTIEventPiggybacking.aspx
I guess I somewhat figured this out. The main method to use is jvmti->GetLineNumberTable(...), which fills up a jvmtiLineNumberEntry array. Given the BCI line number n (which is to be mapped to a source line number), one can test for which int i is: jvmtiLineNumberEntryArray[i] <= n < jvmtiLineNumberEntryArray[i + 1]. This int i is then the desired corresponding source code line number.
One catch is that AsyncGetCallTrace, for some reason, consistently returns weird BCIs, so although the mapping gives precise source code line numbers, they are still not accurate, because the original BCIs are not accurate. Why this is, I do not know. I was hoping to use the Sun Studio profiler, which also uses AsyncGetCallTrace, to test whether the returned line numbers would be the same as they are for my profiler. In that case, the AsyncGetCallTrace function is inaccurate. But so far, using the Sun Studio turned into a challenge of its own. IF ANYONE KNOWS HOW TO USE THIS TOOL, PLEASE HELP!
A bigger catch is that Java methods are often inlined, so line numbers do not always map correctly. In fact, this MIGHT be the cause of the problem described in the paragraph above, although this seems unlikely based on the numbers I've been seeing. Here is some information about solving the inlining problem: http://developer.amd.com/documentation/articles/pages/JVMTIEventPiggybacking.aspx
更多推荐
发布评论