# Understanding Java’s Debugging Internals

- Speaker: [Liran Haimovitch](/wiki/speakers/liran-haimovitch.md)
- Event: [Reversim Summit 2021](/wiki/years/2021.md)
- Video: https://www.youtube.com/watch?v=0EgZrL7hxSM
- Transcript: [/transcripts/2021/14-0EgZrL7hxSM.md](/transcripts/2021/14-0EgZrL7hxSM.md)
- Language: Hebrew · Type: talk · Level: intermediate
- Topics: [Performance](/wiki/topics/performance.md), [Java](/wiki/topics/java.md), [Debugging](/wiki/topics/debugging.md)

> Written by an LLM from machine transcripts. It can contain errors. Check the transcript before you quote it.

## Summary

This talk explores the inner workings of Java debugging, contrasting traditional black-box debugging with deep white-box approaches. The speaker explains the core JVM execution lifecycle, including building, loading, and compilation phases. Additionally, the audience learns about low-level instrumentation mechanisms like JVMTI and Java Agents, which enable runtime code manipulation for building production debuggers and profilers.

## Key points

- [1:07](https://www.youtube.com/watch?v=0EgZrL7hxSM&t=67s) Debugging can be approached through black-box methods focusing on inputs and outputs, or white-box methods involving step-by-step code inspection.
- [3:13](https://www.youtube.com/watch?v=0EgZrL7hxSM&t=193s) The JVM executes code through three main phases: build, load, and execution.
- [5:25](https://www.youtube.com/watch?v=0EgZrL7hxSM&t=325s) During execution, the JVM starts with an interpreter and later utilizes C1 and C2 compilers to optimize frequently called functions.
- [7:46](https://www.youtube.com/watch?v=0EgZrL7hxSM&t=466s) Traditional Java debugging relies on JDWP and JVMTI, but they present significant complexity, security, and performance challenges in production environments.
- [13:59](https://www.youtube.com/watch?v=0EgZrL7hxSM&t=839s) Java Agents and the java.lang.instrument API provide a safer, purely Java-based mechanism to transform classes and modify bytecode at runtime.
- [22:40](https://www.youtube.com/watch?v=0EgZrL7hxSM&t=1360s) Generating stack traces efficiently requires bypassing standard exception conversion overhead, utilizing internal secrets in Java 8 or the newer StackWalker API in Java 9.
- [24:40](https://www.youtube.com/watch?v=0EgZrL7hxSM&t=1480s) Because Java does not preserve local variable names at runtime, debuggers must use bytecode visitors during instrumentation to capture variable values.

## Takeaways

- Understand the trade-offs between black-box and white-box debugging techniques.
- Utilize Java Agents and the java.lang.instrument API to safely intercept and modify bytecode at runtime.
- Optimize stack trace generation performance by choosing version-appropriate APIs like StackWalker.

## Questions this talk answers

- How do Java Agents allow runtime modification of classes without restarting the application?
- What are the performance differences between using standard exceptions and the StackWalker API for generating stack traces?
- How can a developer inspect local variables at runtime given that Java discards variable names during compilation?

## Technologies

Java, JVM

## Tags

bytecode, debugging, instrumentation, java, jvm, performance
