As a sysadmin I sometimes face situations, where a program behaves abnormally, while not creating errors at all or creating nonsense error-messages.

In the past - before java came in - there were two counter-measures:

  1. If nothing else helps - RTFM ;-)
  2. If even 1. does not help - trace the system-calls and see what is happening

I usually use strace -f for this task with Linux (other OS have similar trace-tools). Now while this usually works well for any old-fashioned program, the trace gets very fuzzy when doing the same on a java-process. There are so many system-calls seemingly unrelated to any real action, that it is terrible to search through such a dump.

Are there better ways to do that (if the source-code is not available)?

asked Nov 27 '12 at 22:10
Nils

9,83232358
 

4 Answers

up vote8down voteaccepted

+50

As ckhan mentioned, jstack is great because it gives the full stack trace of all active threads in the JVM. The same can be obtained on stderr of the JVM using SIGQUIT.

Another useful tool is jmap which can grab a heap dump from the JVM process using the PID of the process:

jmap -dump:file=/tmp/heap.hprof $PID

This heap dump can be loaded in tools like visualvm (which is now part of the standard Oracle java sdk install, named jvisualvm). In addition, VisualVM can connect to the running JVM and display information about the JVM, including showing graphs of internal CPU usage, thread counts, and heap usage - great for tracking down leaks.

Another tool, jstat, can collect garbage collection statistics for the JVM over a period of time much like vmstat when run with a numeric argument (e.g. vmstat 3).

Finally, it is possible to use a Java Agent to push instrumentation on all methods of all objects at load-time. The library javassist can help to make this very easy to do. So, it is feasible to add your own tracing. The hard part with that would be finding a way to get trace output only when you wanted it and not all the time, which would likely slow the JVM to a crawl. There's a program called dtrace that works in a manner like this. I've tried it, but was not very successful. Note that agents cannot instrument all classes because the ones needed to bootstrap the JVM are loaded before the agent can instrument, and then it's too late to add instrumentation to those classes.

My Suggestion - start with VisualVM and see if that tells you what you need to know since it can show the current threads and important stats for the JVM.

answered Aug 28 '13 at 4:37
ash

2,4301810
 
    
By the way, this is an awesome question; I hope more people add answers with other ideas. When I've asked folks working with Java for many years about tracing, they gave me blank stares. Perhaps they just do not know the awesomeness of strace. – ash Aug 28 '13 at 4:41

In the same vain when debugging programs that have gone awry on a Linux system you can use similar tools to debug running JVMs on your system.

Tool #1 - jvmtop

Similar to top, you can use jvmtop to see what classes are up to within the running JVMs on your system. Once installed you invoke it like this:

$ jvmtop.sh

Its output is similarly styled to look like the tool top:

 JvmTop 0.8.0 alpha   amd64  8 cpus, Linux 2.6.32-27, load avg 0.12
http://code.google.com/p/jvmtop PID MAIN-CLASS HPCUR HPMAX NHCUR NHMAX CPU GC VM USERNAME #T DL
3370 rapperSimpleApp 165m 455m 109m 176m 0.12% 0.00% S6U37 web 21
11272 ver.resin.Resin [ERROR: Could not attach to VM]
27338 WatchdogManager 11m 28m 23m 130m 0.00% 0.00% S6U37 web 31
19187 m.jvmtop.JvmTop 20m 3544m 13m 130m 0.93% 0.47% S6U37 web 20
16733 artup.Bootstrap 159m 455m 166m 304m 0.12% 0.00% S6U37 web 46

Tool #2 - jvmmonitor

Another alternative is to use jvmmonitor. JVM Monitor is a Java profiler integrated with Eclipse to monitor CPU, threads and memory usage of Java applications. You can either use it to automatically find running JVMs on the localhost or it can connect to remote JVMs using a port@host.

Tool #3 - visualvm

visualvm is probably "the tool" to reach for when debugging issues with the JVM. Its feature set is pretty deep and you can get a very in depth look at the innards.

Profile application performance or analyze memory allocation:

Take and display thread dumps:

References

answered Aug 29 '13 at 4:02
slm

155k36265437
 

Consider jstack. Not quite a match for strace, more of a pstack-analog, but will at least give you a picture of a snapshot in time. Could string'em together to get a crude trace if you had to.

See also the suggestions at this SO article: http://stackoverflow.com/questions/1025681/call-trace-in-java

answered Nov 28 '12 at 2:39
ckhan

3,010916
 

If you are using RHEL OpenJDK (or similiar, the point is that it is not Oracle's JDK), you may use SystemTap for that.

Some probes are enabled by using java command line options -XX:+DTraceMethodProbes-XX:+DTraceAllocProbes-XX:+DTraceMonitorProbes. Note that enabling these probes will significally affect program performance.

Here is example SystemTap Script:

#!/usr/bin/stap

probe hotspot.class_loaded {
printf("%12s [???] %s\n", name, class);
} probe hotspot.method_entry,
hotspot.method_return {
printf("%12s [%3d] %s.%s\n", name, thread_id, class, method);
} probe hotspot.thread_start,
hotspot.thread_stop {
printf("%12s [%3d] %s\n", name, id, thread_name);
} probe hotspot.monitor_contended_enter,
hotspot.monitor_contended_exit {
printf("%12s [%3d] %s\n", name, thread_id, class);
}

You can also use jstack() to get Java stack of the process, but it will only work if you start SystemTap before JVM.


Note that SystemTap will trace every method. It is also not able to get method's arguments. Another option is to use JVM own capabilities of tracing which is called JVMTI. One of the most famous JVMTI implementations is BTrace.

How to trace a java-program的更多相关文章

  1. Core Java Volume I — 3.1. A Simple Java Program

    Let’s look more closely at one of the simplest Java programs you can have—one that simply prints a m ...

  2. Java program to find the largest element in array

    Java program to find the largest element in array Given an array of numbers, write a java program to ...

  3. jstack(Stack Trace for Java)

    功能   用于生成虚拟机当前时刻的线程快照(一般称为threaddump或javacore文件).线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间 ...

  4. 2013.11.7-21:15_My first Java program

  5. Caused by: java.lang.NoSuchFieldError: TRACE

    Caused by: java.lang.NoSuchFieldError: TRACE at org.slf4j.impl.Log4jLoggerAdapter.trace(Log4jLoggerA ...

  6. How to Create a Java Concurrent Program

    In this Document   Goal   Solution   Overview   Steps in writing Java Concurrent Program   Template ...

  7. Spark-HBase集成错误之 java.lang.NoClassDefFoundError: org/htrace/Trace

    在进行Spark与HBase 集成的过程中遇到以下问题: java.lang.IllegalArgumentException: Error while instantiating 'org.apac ...

  8. 编写一个应用程序,利用数组或者集合, 求出"HELLO",“JAVA”,“PROGRAM”,“EXCEPTION”四个字符串的平均长度以及字符出现重复次数最多的字符串。

    public class Number { public static void main(String[] args) { String[] arr = { "HELLO", & ...

  9. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  10. [Java Basics] Stack, Heap, Constructor, I/O, Immutable, ClassLoader

    Good about Java: friendly syntax, memory management[GC can collect unreferenced memory resources], o ...

随机推荐

  1. 《无懈可击的Web设计》_灵活的文字

    常见的方法 body{ font-size:12px; } 使用像素单位的优点: 不管使用哪种浏览器或者设备来查看,文字看起来都大小一致. 为什么这么做不是无懈可击的? 以像素为单位设定文字大小之后, ...

  2. 降维(二)----Laplacian Eigenmaps

    降维(二)----Laplacian Eigenmaps 降维系列: 降维(一)----说说主成分分析(PCA)的源头 降维(二)----Laplacian Eigenmaps ----------- ...

  3. 【整理】各种Python的IDE(集成开发环境)的总结和对比

    原地址:http://www.tuicool.com/articles/rMVJNn 原文  http://www.crifan.com/summary_common_python_ide_pyscr ...

  4. 又爱又恨的BOOTSTRAP

    搞本书,看了一天,确实,,UIKIT比它好用... 但,艺多不压身吧. 今天自己抄了个大概的,不用其它插件,,但那手风琴,真的找了很多,没有中意的... <!DOCTYPE html> & ...

  5. bzoj2705

    一个常用的结论(方法) 只要知道gcd(i,n)=L 的i的个数s,我们就能很轻易得出答案 gcd(i,n)=L gcd(i/L,n/L)=1 不难得到这样的s=与n/L互质的个数=phi(n/L) ...

  6. js 弹出页面传值

    有页面a和页面b,页面a中有一个文本框和一个按钮,点按钮弹出页面b,页面b也有一个文本框,在文本框中输入值,在不经过后台用js实现把页面b的文本框的值传到页面a,赋给页面a的文本框 a页面代码< ...

  7. Windows Azure案例分析: 选择虚拟机或云服务?

    作者 王枫 发布于2013年6月27日 随着云计算技术和市场的日渐成熟,企业在考虑IT管理和运维时的选择也更加多样化,应用也从传统部署方式,发展为私有云.公有云.和混合云等部署方式.作为微软核心的公有 ...

  8. 【转】调整mac电脑鼠标移动速度

    原文网址:https://www.zhihu.com/question/19579075 作者:陆猛链接:https://www.zhihu.com/question/19579075/answer/ ...

  9. (转载)顺序栈c++实现

    (转载)http://myswirl.blog.163.com/blog/static/51318642200882310239324/ SqStack.h********************** ...

  10. android学习——必学基础组件

    android基础组件是一个Android的开发人员必须要了解,且深刻理解的东西: 1.应用程序基础 2.应用程序组件 2.1.活动(Activities) 2.2.服务(Services) 2.3. ...