java中的调用跟踪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1025681/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:23:07  来源:igfitidea点击:

Call trace in java

java

提问by JavaRocky

Is there a way to output a call trace for a particular thread in java?

有没有办法在java中输出特定线程的调用跟踪?

I do not want a stack trace. I would like a sequence of calls on each object for tracing.

我不想要堆栈跟踪。我想要对每个对象进行一系列调用以进行跟踪。

采纳答案by Thorbj?rn Ravn Andersen

I think you might find this interesting. It is a java agent which adds entry and exit logging to methods, using the slf4j framework to actually log the output. Then it is a matter of configuring the logging framework to only print out that thread you are interested in.

我想你可能会觉得这很有趣。它是一个 java 代理,它向方法添加入口和出口日志记录,使用 slf4j 框架来实际记录输出。然后是配置日志框架以仅打印出您感兴趣的线程的问题。

http://www.slf4j.org/extensions.html#javaagent

http://www.slf4j.org/extensions.html#javaagent

(just to be explicit: 1) I wrote it, 2) it works for me :) )

(只是明确地说:1)我写的,2)它对我有用:))

回答by joe

Runtime.traceMethodCalls(), which prints a line for each method call of all objects in all threads

Runtime.traceMethodCalls(),它为所有线程中所有对象的每个方法调用打印一行

回答by Jon Skeet

You'd basically want aspect-oriented programming of some form, where the aspect could determine whether the current thread was meant to be logging calls. (The alternative is to explicitly put the logging in every method yourself, which would be painful.)

您基本上需要某种形式的面向方面的编程,其中方面可以确定当前线程是否打算记录调用。(另一种方法是自己明确地将日志记录放入每个方法中,这会很痛苦。)

There are plenty of AOP frameworks in Java, such as AspectJand Spring(Spring is a lot more than AOP of course).

Java中有很多AOP框架,比如AspectJSpring(当然Spring远不止AOP)。

Applying aspects to everymethod call in the system could be tricky though... I don't have any experience with AspectJ, but Spring AOP is basically designed to enable AOP around the components. Classes which are effectively unknown to Spring can't be changed easily. Mind you, it's been a while since I've used even Spring AOP - things may have come on since then :)

将方面应用于系统中的每个方法调用可能会很棘手……我没有任何使用 AspectJ 的经验,但 Spring AOP 基本上旨在启用围绕组件的 AOP。Spring实际上不知道的类不能轻易改变。请注意,我已经有一段时间没有使用 Spring AOP - 从那时起可能发生了一些事情:)

回答by Veera

We could create a new Throwable object which will have the call trace. And using some string manipulations, we can get the call stack.

我们可以创建一个具有调用跟踪的新 Throwable 对象。并使用一些字符串操作,我们可以获得调用堆栈。

Throwable t = new Throwable();
System.out.println(t.getStackTrace()[1].toString());

I'm not sure retrieving the info this way , is a good practice or not. :)

我不确定以这种方式检索信息,是否是一个好习惯。:)

回答by Smar

There is btrace, that can be used for this kind of action.

btrace,可用于此类操作。

回答by mchr

If you want to trace the execution of your Java code you can use a tool called InTrace.

如果要跟踪 Java 代码的执行情况,可以使用名为InTrace的工具。

NOTE: InTrace is a free and open source tool which I have written.

注意:InTrace 是我编写的免费开源工具。

回答by zhongl

The more easier way than Btrace is using HouseMD, you can get the from here

比 Btrace 更简单的方法是使用HouseMD,你可以从这里得到

回答by parrotHyman

If you're using IntelliJ, there is plugin called Flameviewer. Please find more details on the below link,

如果您使用的是 IntelliJ,则有一个名为 Flameviewer 的插件。请在以下链接中找到更多详细信息,

https://github.com/kornilova-l/FlameViewer#uploading-file-to-flameviewer

https://github.com/kornilova-l/FlameViewer#uploading-file-to-flameviewer