【发布时间】:2020-06-07 15:35:03
【问题描述】:
当抛出异常时,我有以下代码来获取当前堆栈帧位置的行号,但我通过实验而不是通过规范找到了它。我只留下了相关代码。
int max_frame_count = 50;
jvmtiFrameInfo frames[max_frame_count];
jint count;
(*jvmti_env)->GetStackTrace(jvmti_env, thread, 0, max_frame_count, frames, &count);
for (int loop = 0; loop < count; loop++) {
jvmtiFrameInfo frame = frames[loop];
jmethodID currentMethod = frame.method;
(*jvmti_env)->GetMethodName(jvmti_env, currentMethod, &name_ptr, &signature_ptr, &generic_ptr);
(*jvmti_env)->GetLineNumberTable(jvmti_env, currentMethod, &entry_count_ptr, &table_ptr);
jint lineNumber = -1;
int lineNumberCount;
jlocation prevLocationId = -1;
if (frame.location != -1) {
for (int lineNumberLoop = entry_count_ptr - 1; lineNumberLoop >= 0; lineNumberLoop--) {
jvmtiLineNumberEntry lineNumberEntry = table_ptr[lineNumberLoop];
if (frame.location >= lineNumberEntry.start_location) {
lineNumber = lineNumberEntry.line_number;
break;
}
}
}
}
GetLineNumberTable 为currentMethod 的所有行返回line_number 和对应的start_location。现在我的困惑从这里开始:为什么我不能将frame.location 与GetLineNumberTable 返回的start_location 匹配?有没有规范这些如何匹配?虽然我的代码似乎有效,但我无法相信这是一个始终有效的解决方案。它向后搜索大于或等于lineNumberEntry.start_location 的第一个frame.location。
感谢任何提示和指点!
【问题讨论】:
标签: java jvm java-native-interface jvmti