如何在 Java 中跟踪方法调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29382231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I trace methods calls in Java?
提问by george24
Consider the two simple Java classes below:
考虑以下两个简单的 Java 类:
First Example
第一个例子
class Computer {
Computer() {
System.out.println("Constructor of Computer class.");
}
void On() {
System.out.println("PC turning on...");
}
void working() {
System.out.println("PC working...");
}
void Off() {
System.out.println("PC shuting down...");
}
public static void main(String[] args) {
Computer my = new Computer();
Laptop your = new Laptop();
my.On();
my.working();
your.On();
your.working();
my.Off();
your.Off();
}
}
Second Example
第二个例子
class Laptop {
Laptop() {
System.out.println("Constructor of Laptop class.");
}
void On() {
System.out.println("Laptop turning on...");
}
void working() {
System.out.println("Laptop working...");
}
void Off() {
System.out.println("Laptop shuting down...");
}
}
After the program run, how do I trace (1) which object call which method (2) and how many times?
程序运行后,我如何跟踪(1)哪个对象调用哪个方法(2)以及多少次?
Just a little precision, I might have 100 classes and 1000s of objects each of them calling 100s of methods. I want to be able to trace (after I run the program), which object called which method and how many times.
稍微精确一点,我可能有 100 个类和 1000 个对象,每个对象调用 100 个方法。我希望能够跟踪(在我运行程序之后),哪个对象调用了哪个方法以及调用了多少次。
Thanks for any suggestion.
感谢您的任何建议。
采纳答案by clearlight
This prints a line for each method call of all objects in all threads:
这将为所有线程中所有对象的每个方法调用打印一行:
Runtime.traceMethodCalls() (deprecated / no-op in Java 9)
And
和
Runtime.traceInstructions (deprecated / no-op in Java 9)
You can use a call tracer like housemd or btrace or inTrace
您可以使用像housemd 或 btrace 或 inTrace这样的呼叫跟踪器
For more involved analysis, you can use a call graph utility like one of these:
对于更复杂的分析,您可以使用调用图实用程序,如以下之一:
(here is an articleon the subject)
The deprecated methods above are slated for removal, because there are now JVM-specific alternatives:
上面已弃用的方法将被删除,因为现在有特定于 JVM 的替代方法:
- Java Flight Recorder Part of JDK 7 as of build 56. Requires commercial license for use in production
- VisualVM Free/popular 3rd party
- Java Flight Recorder JDK 7 的一部分,从 build 56 开始。在生产中使用需要商业许可
- VisualVM 免费/流行的第 3 方
Both of those tools pretty easy to setup and start collecting information and have nice GUI interfaces. They attach to a running JVM process and allow for thread snapshots and various other kinds of diagnosis (Visual VM has a lot of available plugins but that can take awhile to sort through to configure and understand, if you want to go beyond default behavior, whereas JFR is instrumented with more by default).
这两个工具都非常容易设置和开始收集信息,并且具有漂亮的 GUI 界面。它们附加到正在运行的 JVM 进程并允许线程快照和各种其他类型的诊断(Visual VM 有很多可用的插件,但如果您想超越默认行为,则可能需要一段时间来整理以进行配置和理解,而JFR 默认配备更多)。
Also, don't underestimate the usefulness of JVM distributed command line utilities ($JAVA_HOME/bin
), for performing some easily accessible diagnostics.
此外,不要低估 JVM 分布式命令行实用程序 ( $JAVA_HOME/bin
) 在执行一些易于访问的诊断方面的用处。
回答by gavenkoa
$ jdb -classpath ... -sourcepath ... my.App
jdb> stop on my.App.main
jdb> run
jdb> step <... repeat until get to interesting line...>
jdb> threads
jdb> trace go methods 0x1 <... 0x1 is our main thread ID ...>
jdb> step
<...HERE you get full methods calls trace...>
jdb> quit