Open Source Debugging for Java 1.4.0

4,106 views

Published on

Open Source Debugging for Java given at the Dallas MUG

Published in: Education, Technology
1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total views
4,106
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
253
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

Open Source Debugging for Java 1.4.0

  1. 1. Open Source Java Debugging Tools by Matthew McCullough of Ambient Ideas, LLC
  2. 2. Code Examples http://github.com/matthewmccullough Twitter @matthewmccull Email matthewm@ambientideas.com Blog http://www.ambientideas.com/blog sidebar has my social networking profile links
  3. 3. http://delicious.com/matthew.mccullough/opensource+debugging
  4. 4. Not
  5. 5. Ins tead
  6. 6. jhat jstat jmap jps jstack VisualVM Eclipse TPTP MAT TOD jstatd
  7. 7. Hotspot JVMs
  8. 8. jps process list
  9. 9. What JVM options did we use when we started up the app server?
  10. 10. jps
  11. 11. jps -l Sh ow fu ll pa ck ag e na m e
  12. 12. Sh ow jps -l -v Sh fu ow ll pa JV ck M ag e ar gu nam m e en ts
  13. 13. Sh ow Sh fu ow ll pa JV ck M ag jps -l -v -m e Sh ar ow gu nam m e m en ts ain () ar gu m en ts
  14. 14. VIDEO JPS
  15. 15. only local?
  16. 16. jps 10.15.25.32
  17. 17. How?
  18. 18. Run jstatd daemon
  19. 19. Allow-all policy
  20. 20. y lic Po ur Yo jstatd -J-Djava.security.policy=allowall.pol
  21. 21. grant codebase "file:${java.home}/../lib/tools.jar" { permission java.security.AllPermission; };
  22. 22. grant codebase "file:${java.home}/../lib/tools.jar" { permission java.net.SocketPermission "<YourDesktopHostnameOrIP>:1099", "accept, connect, listen, resolve"; };
  23. 23. let’s start up the jstatd daemon
  24. 24. VIDEO JSTATD
  25. 25. jps 10.15.25.32
  26. 26. Windows let’s run JPS from across the network to a Mac
  27. 27. VIDEO JPS REMOTE
  28. 28. jstat process info
  29. 29. Sh ow ga rb ag e jstat -gcutil <pid> co lle ct io n
  30. 30. es pl m sa n ee ns tw io tit be pe s re m jstat -gcutil <pid> 500 999
  31. 31. es lin X y er ev r e ad he ow Sh jstat -gcutil -h5 <pid> 500 999
  32. 32. VIDEO JSTAT REMOTE
  33. 33. jstack stack dumps
  34. 34. jstack <pid>
  35. 35. jstack -F <pid>
  36. 36. of in ck lo e os rb Ve jstack -l <pid>
  37. 37. Direct this output to a file with a tdump extension Use TDA on VisualVM to analyze it
  38. 38. javap class file disassembler
  39. 39. We added a new method to a class. Is it on the server-deployed copy of the app?
  40. 40. javap classfile
  41. 41. e os rb Ve javap -v classfile
  42. 42. major version: 48 = Java 1.4 major version: 49 = Java 5 major version: 50 = Java 6
  43. 43. In te rn al ty pe javap -s classfile si gn at ur es
  44. 44. ds ho et m te iva Pr ow Sh javap -private classfile
  45. 45. Survey
  46. 46. StackOverflow.com
  47. 47. Your favorite Open Source Debugging Tool?
  48. 48. ❸ VisualVM
  49. 49. ❷ Eclipse
  50. 50. ❶ System.out.println(brokenobj) System.out.println(broke System.out.println(brokenobj); nobj) System.out.println(brokenobj); System.out.p rintln(brokenobj); System.out.pr intln(brokenobj); System.out.println(brokenobj); System.out.println(brokenobj); System.out.println(brokenobj); System.out.println(brokenobj); ystem.out.println(broken obj); System.out.println(brokenob System.out.println(brokenobj); System.out.p rintln(brokenobj) System.out.println(brokenobj);
  51. 51. I’ve got the only tool I need
  52. 52. jmap memory maps
  53. 53. jmap histogram
  54. 54. jmap -histo <pid>
  55. 55. jmap -histo:live <pid>
  56. 56. jmap heap dump
  57. 57. jmap -dump:file=myfile.hprof <pid>
  58. 58. java -XX:+HeapDumpOnOutOfMemoryError MyApp
  59. 59. jinfo -flag +HeapDumpOnOutMemoryError <pid>
  60. 60. OutOfMemoryError Drive in to office!
  61. 61. jhat heap analysis
  62. 62. jhat -J-Xmx512m <yourdump.hprof>
  63. 63. jhat -J-Xmx512m <yourdump.hprof>
  64. 64. jhat -J-Xmx512m <yourdump.hprof>
  65. 65. http://localhost:7000
  66. 66. OQL
  67. 67. select z from java.lang.String z
  68. 68. select z from java.lang.String z where z.count >= 50
  69. 69. http://blogs.sun.com/sundararajan/date/200509
  70. 70. eclipse memory analyzer M.A.T.
  71. 71. EclipseMemoryAnalyzer Purpose Investigate memory usage. Useful for Visualizing memory footprint. Finding memory leaks. Querying through (OQL) allocated memory. Discovering heavy memory allocation sources.
  72. 72. EclipseMemoryAnalyzer Gotcha Doesn’t create heap dumps, just analyzes. Available as a plugin or standalone RCP app.
  73. 73. java.util.HashMap java.lang.String java.lang.String java.lang.String char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[]
  74. 74. Shallow Heap java.util.HashMap java.lang.String java.lang.String java.lang.String char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[]
  75. 75. Retained Heap java.util.HashMap java.lang.String java.lang.String java.lang.String char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[] char[]
  76. 76. Load a heap dump in MAT
  77. 77. VIDEO ECLIPSE MAT
  78. 78. Run some OQL on it
  79. 79. VIDEO ECLIPSE MAT OQL
  80. 80. btrace
  81. 81. Can you put a few debugging “sysouts” into that running production app?
  82. 82. BTrace Tracing
  83. 83. BTrace Tracing
  84. 84. BTrace Tracing Production-time Operates on deployed code Uses debugging hooks to re-deploy class files Runtime injection of tracing statements
  85. 85. BTrace Tracing Code Tracing scripts written in Java. Limited to a catalog of static methods. Compiled at design time or tracing time. Unsafe mode
  86. 86. BTrace Tracing Code Tracing scripts written in Java. Limited to a catalog of static methods. Compiled at design time or tracing time. Unsafe mode
  87. 87. import com.sun.btrace.annotations.*; import static com.sun.btrace.BTraceUtils.*; import com.sun.btrace.AnyType; import java.lang.management.MemoryUsage; import java.util.concurrent.atomic.AtomicInteger; import java.util.Map; @BTrace public class TraceAllMethodCalls { /** * METHOD CALL TRACEPOINT. */ @OnMethod( clazz="com.ambientideas.HelloWorldJava", method="print", location=@Location(value=Kind.CALL, clazz="/.*/", method="/.*/") ) public static void m(AnyType[] args) { //System.out.println("Hello"); println(strcat("****** WE'VE ENTERED A METHOD ON CLASS: ", name(probeClass ()))); println(strcat("****** WE'VE ENTERED METHOD: ", probeMethod())); } @OnMethod( clazz="/.*/",
  88. 88. import java.lang.management.MemoryUsage; import java.util.concurrent.atomic.AtomicInteger; import java.util.Map; @BTrace public class TraceAllMethodCalls { /** * METHOD CALL TRACEPOINT. */ @OnMethod( clazz="com.ambientideas.HelloWorldJava", method="print", location=@Location(value=Kind.CALL, clazz="/.*/", method="/.*/") ) public static void m(AnyType[] args) { //System.out.println("Hello"); println(strcat("****** WE'VE ENTERED A METHOD ON CLASS: ", name(probeClass ()))); println(strcat("****** WE'VE ENTERED METHOD: ", probeMethod())); } @OnMethod( clazz="/.*/", method="println" ) public static void m2(AnyType[] args) { println(strcat("****** WE'VE ENTERED A METHOD ON CLASS: ", name(probeClass ()))); println(strcat("****** WE'VE ENTERED METHOD: ", probeMethod())); } /** * ONEXIT TRACEPOINT. CALLED WHEN A BTRACE METHOD CALLS EXIT(INT).
  89. 89. ) public static void m(AnyType[] args) { //System.out.println("Hello"); println(strcat("****** WE'VE ENTERED A METHOD ON CLASS: ", name(probeClass ()))); println(strcat("****** WE'VE ENTERED METHOD: ", probeMethod())); } @OnMethod( clazz="/.*/", method="println" ) public static void m2(AnyType[] args) { println(strcat("****** WE'VE ENTERED A METHOD ON CLASS: ", name(probeClass ()))); println(strcat("****** WE'VE ENTERED METHOD: ", probeMethod())); } /** * ONEXIT TRACEPOINT. CALLED WHEN A BTRACE METHOD CALLS EXIT(INT). */ @OnExit public static void onexit(int code) { println("****** THE BTRACE PROGRAM IS EXITING."); } /** * LOW MEMORY TRACE POINT. */ @OnLowMemory( pool = "Tenured Gen", threshold=58720250
  90. 90. public static void m2(AnyType[] args) { println(strcat("****** WE'VE ENTERED A METHOD ON CLASS: ", name(probeClass ()))); println(strcat("****** WE'VE ENTERED METHOD: ", probeMethod())); } /** * ONEXIT TRACEPOINT. CALLED WHEN A BTRACE METHOD CALLS EXIT(INT). */ @OnExit public static void onexit(int code) { println("****** THE BTRACE PROGRAM IS EXITING."); } /** * LOW MEMORY TRACE POINT. */ @OnLowMemory( pool = "Tenured Gen", threshold=58720250 ) public static void onLowMem(MemoryUsage mu) { println("******"); println(mu); } /** * MEMORY HISTOGRAM TRACE POINT. */ private static Map<String, AtomicInteger> histo = newHashMap();
  91. 91. /** * ONEXIT TRACEPOINT. CALLED WHEN A BTRACE METHOD CALLS EXIT(INT). */ @OnExit public static void onexit(int code) { println("****** THE BTRACE PROGRAM IS EXITING."); } /** * LOW MEMORY TRACE POINT. */ @OnLowMemory( pool = "Tenured Gen", threshold=58720250 ) public static void onLowMem(MemoryUsage mu) { println("******"); println(mu); } /** * MEMORY HISTOGRAM TRACE POINT. */ private static Map<String, AtomicInteger> histo = newHashMap(); @OnTimer(4000) public static void print() { if (size(histo) != 0) { printNumberMap(strcat("******", "Component Histogram"), histo); } } }
  92. 92. * LOW MEMORY TRACE POINT. */ @OnLowMemory( pool = "Tenured Gen", threshold=58720250 ) public static void onLowMem(MemoryUsage mu) { println("******"); println(mu); } /** * MEMORY HISTOGRAM TRACE POINT. */ private static Map<String, AtomicInteger> histo = newHashMap(); @OnTimer(4000) public static void print() { if (size(histo) != 0) { printNumberMap(strcat("******", "Component Histogram"), histo); } } }
  93. 93. visualvm
  94. 94. Useful for ‣ Discovering the GC cycles of your app. ‣ Finding the largest memory usage culprits. ‣ Diffing memory snapshots. ‣ Injecting btrace code. ‣ Snapping heapdumps.
  95. 95. Gotcha Download version 1.2 even if your JDK includes 1.0. Standalone version always ahead of JVM version.
  96. 96. VisualVM in action
  97. 97. BTrace in action
  98. 98. omniscient debuggers
  99. 99. Luke, you can destroy the bug! He has foreseen this. It is your destiny.
  100. 100. TOD (by Guillaume Pothier)
  101. 101. TOD ✴ Eclipse-integrated omniscient debugger ✴ observe all memory changes ✴ execute once, review infinitely ✴ rewind and step through call stacks, loops ✴ Windows-only (DLL)
  102. 102. VIDEO TOD
  103. 103. comprehension tools
  104. 104. Goal: Comprehend the application runtime call sequence better.
  105. 105. amida
  106. 106. amida http://sel.ist.osaka-u.ac.jp/~ishio/amida/
  107. 107. jtracert
  108. 108. jtracert http://code.google.com/p/jtracert/
  109. 109. Eclipse TPTP
  110. 110. Eclipse TPTP ✴ profile an application’s performance ✴ render call sequence diagrams ✴ almost ready for Mac OS
  111. 111. VIDEO TPTP
  112. 112. decompiling tools
  113. 113. cavaj
  114. 114. JODE
  115. 115. JD-Eclipse
  116. 116. System.out.println()
  117. 117. ancient weapons are no match...
  118. 118. bring a better weapon to the fight
  119. 119. if a bug strikes you down...
  120. 120. You work the weekend solving it.
  121. 121. Matthew McCullough matthewm@ambientideas.com
  122. 122. May the debugging force be with you. Matthew McCullough matthewm@ambientideas.com
  123. 123. Than ks in advance for your complete d evals!
  124. 124. Code Examples http://github.com/matthewmccullough Twitter @matthewmccull Email matthewm@ambientideas.com Blog http://www.ambientideas.com/blog sidebar has my social networking profile links
  125. 125. Resources Eclipse Memory Analyzer http://www.eclipse.org/mat/ VisualVM https://visualvm.dev.java.net/ GCHisto https://gchisto.dev.java.net/ BTrace https://btrace.dev.java.net/ DTrace, XRay, Instruments http://en.wikipedia.org/wiki/DTrace
  126. 126. Resources TOD http://pleiad.cl/tod/ JTracert http://code.google.com/p/jtracert/ Amida http://sel.ist.osaka-u.ac.jp/~ishio/amida/ TPTP http://www.eclipse.org/tptp/ JD-Eclipse http://java.decompiler.free.fr/
  127. 127. Image Credits ‣ http://www.ambientideasphotography.com ‣ http://upload.wikimedia.org/wikipedia/commons/a/a4/ BernardMadoff.jpg ‣ http://flickr.com/photos/triller/2226679393/ ‣ http://flickr.com/photos/ektogamat/2687444500/ ‣ http://flickr.com/photos/bfionline/2380398799/ ‣ http://flickr.com/photos/symphoney/76513801/ ‣ http://flickr.com/photos/foxypar4/2124673642/ ‣ http://flickr.com/photos/morberg/3146874095/ ‣ http://flickr.com/photos/triller/2226679393/

×