19

When gdb is used for debugging purposes in Java:

  • What's its practical use?
  • What are its limitations?
  • How is it compared to other debuggers?
0

4 Answers 4

16

I would say gdb is used for debugging Java when the programmer is coming from a different language and is already familiar with gdb. Otherwise, it seems like a strange choice given that there are more popular alternatives for Java: jdb, JSwat, eclipse, netbeans, etc.

Here is a tutorial for debugging Java with gdb.

Sign up to request clarification or add additional context in comments.

2 Comments

Only more popular or better? Could you be more specific? From what I've read it has limitations (or maybe other uses). See Debugging with gdb.
<speculation> I tend to agree with the article you've linked. gdb is probably more limited for debugging pure java code than other popular options. It might be more useful for debugging the actual JVM though. I could see it being helpful for debugging alternative JVM languages like JRuby, Clojure, or Scala as well. My gdb experience is limited to C and C++ though, so I don't feel qualified to comment any further. </speculation>
4

GDB is 99% of the time is not useful for debugging Java, but it can help you find native memory leaks which the java debuggers can't.

Comments

3

gdb can be useful to take heap dumps fast.

Normally one would dump the heap by using jmap or similar tools. But when using this tools, the JVM performs garbage collection before dumping, which can take hours on a hanging process. With the steps below you can dump fast, restart the process and convert the dump afterwards.

gdb --pid <PID>
gcore /tmp/gdbdump.core
detach
quit

Afterwards you need to convert the core file to a hprof file to analyze it with Java tools:

jmap -dump:format=b,file=/tmp/javadump.hprof /path/tojdk/java /tmp/gdbdump.core

Be aware:

  1. You need to use the same version of the JDK like the JRE/JDK version the process was running with.
  2. The conversion can take some hours.
  3. For Windows: There seems to be similar process: https://stackoverflow.com/a/35822193/4248273

Comments

1

GDB can be used in java programs to debug/trace JNI (java native interface). JNI is usually used to access code in shared libraries. We can put breakpoints in the shared libraries, which can be triggered when jvm executes the JNI methods

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.