Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Java Debugging Internals
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Yarden Laifenfeld
November 22, 2022
Programming
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Java Debugging Internals
Yarden Laifenfeld
November 22, 2022
More Decks by Yarden Laifenfeld
See All by Yarden Laifenfeld
When Bits Hit the TurboFan
yardenlaif
0
270
OpenFeature: The Future of Feature Flags
yardenlaif
0
210
Go Sync or Go Home: Advanced Concurrency Techniques for Better Go Programs
yardenlaif
0
230
Go Stack: Should it be Moving Like That?
yardenlaif
0
220
Java Debugging Internals - JPrime 2023
yardenlaif
0
150
Debugging, the Developer Way
yardenlaif
0
260
Ruby & JVM: A (JRuby) Love Story - RubyConfTh
yardenlaif
0
220
Ruby & JVM: A (JRuby) Love Story
yardenlaif
0
320
Other Decks in Programming
See All in Programming
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
8
3k
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
270
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
2
900
継続モナドとリアクティブプログラミング
yukikurage
3
620
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
390
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
230
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
150
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
yield再入門 #phpcon
o0h
PRO
0
700
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
140
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
150
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
630
Featured
See All Featured
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
390
The Invisible Side of Design
smashingmag
301
52k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
HDC tutorial
michielstock
2
750
The Art of Programming - Codeland 2020
erikaheidi
57
14k
[SF Ruby Conf 2025] Rails X
palkan
2
1.2k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
310
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
300
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
270
Testing 201, or: Great Expectations
jmmastey
46
8.2k
Prompt Engineering for Job Search
mfonobong
0
380
Transcript
Java Debugging Internals @yardenlaif
2 Who Am I? • Software Engineer at Rookout •
Background in low level C programming in linux IOT environments • Java, Go, Ruby • C#, Python, JavaScript, C++
Non Breaking Breakpoints 3
Non Breaking Breakpoints 4
jdb -attach 8000 java -agentlib:jdwp= transport=dt_socket,server=y, suspend=n,address=*:8000 myApp JDB -
The Java Debugger 5 JVM MyApp Debugger CLI
JPDA - Java Platform Debugging Architecture The Java Platform Debugger
Architecture (JPDA) consists of three interfaces designed for use by debuggers in development environments for desktop systems. - Oracle Documentation 6
JPDA - Java Platform Debugging Architecture • JDI - Java
Debug Interface • JDWP - Java Debug Wire Protocol • JVMTI - Java Virtual Machine Tool Interface 7
JDI - Java Debug Interface • High level Java API
• Inspect & Control • Connect remotely 8
JDI - Java Debug Interface • Add breakpoint ◦ createBreakpointRequest
• Collect data ◦ visibleVariables ◦ getArgumentValues ◦ frames • Continue program ◦ resume 9
Using JDI • No availability during collection • Zero control
or visibility • Easy to make mistakes • Single instance debugging 10
JPDA - Java Platform Debugging Architecture • JDI - Java
Debug Interface • JDWP - Java Debug Wire Protocol • JVMTI - Java Virtual Machine Tool Interface 11
JDWP - Java Debug Wire Protocol • A protocol •
Used for communication between a debugger and the JVM which it debugs 12 JVM MyApp Debugger CLI JDWP
Using JDWP • No availability during collection • Zero control
or visibility • Easy to make mistakes • Single instance debugging • Managing multiple clients and connecting to multiple servers 13
JPDA - Java Platform Debugging Architecture • JDI - Java
Debug Interface • JDWP - Java Debug Wire Protocol • JVMTI - Java Virtual Machine Tool Interface 14
JVMTI - Java Virtual Machine Tool Interface • Native programming
interface • Interacts directly with the JVM • Inspect & Control 15
JVMTI - Java Virtual Machine Tool Interface • SetBreakpoint •
SetEventNotificationMode ◦ Breakpoint • GetLocalObject ◦ jobject 16
JVMTI - Get Stack Trace /* Get Stack Trace */
err = (*jvmti)->GetStackTrace(jvmti, thr, 0, 5, &frames, &count); if (err != JVMTI_ERROR_NONE) { printf("(GetThreadInfo) Error expected: %d, got: %d\n", JVMTI_ERROR_NONE, err); describe(err); printf("\n"); } printf("Number of records filled: %d\n", count); if (err == JVMTI_ERROR_NONE && count >=1) { char *methodName; methodName = "yet_to_call()"; char *declaringClassName; jclass declaring_class; int i=0; printf("Exception Stack Trace\n"); printf("=====================\n"); printf("Stack Trace Depth: %d\n", count); for ( i=0; i < count; i++) { err = (*jvmti)->GetMethodName(jvmti, frames[i].method, &methodName, NULL, NULL); if (err == JVMTI_ERROR_NONE) { err = (*jvmti)->GetMethodDeclaringClass(jvmti, frames[i].method, &declaring_class); err = (*jvmti)->GetClassSignature(jvmti, declaring_class, &declaringClassName, NULL); if (err == JVMTI_ERROR_NONE) { printf("at method %s() in class %s\n", methodName, declaringClassName); } } } } 17 *A lot of code*
Java - Get Stack Trace Thread.currentThread().getStackTrace() 18
Using JVMTI • Incredibly Complex • Native vulnerabilities • Not
portable • Bugs are catastrophic 19
20 Newer? Simpler? Full control? In-process? Unbreakable?
Requirements 21 • Don’t break • Predictability • Availability •
Performance • Security and compliance • Multi-instance debugging
Thread 2: Client Connection public Response Process(Request req) { String
id = req.GetID(); if (isExistingUser(id)) { return new Response("User exists!"); } // Create new user... } Requirements 22 • Don’t break • Availability Thread 1: Server Client Connection Thread 3: Client Connection public Response Process(Request req) { String id = req.GetID(); if (isExistingUser(id)) { return new Response("User exists!"); } // Create new user... } Thread 2: Client Connection public Response Process(Request req) { String id = req.GetID(); if (isExistingUser(id)) { return new Response("User exists!"); } // Create new user... }
-agentlib:jdwp address=*:8000 transport=dt_socket server=y suspend=n Break It Down java -agentlib:jdwp=transport=dt_socket,server=y,
suspend=n,address=*:8000 myApp 23
Java Agents 24
Java Agents 25 JVM MyApp Java Agent java.lang.Instrument API
Sample Program 26 1| public class HelloWorld { 2| public
static void main(String[] args) { 3| while(true) { 4| System.out.println("Hello, World!"); 5| } 6| } 7| }
Sample Program 27 • Build HelloWorld to JAR • Run:
java -jar HelloWorld.jar • Output: Hello, World! Hello, World! ...
Java Agent - Premain 28 public class JavaAgent { public
static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { System.out.println("Hello, JavaSummit!"); } }
Java Agent - Premain 29 public class JavaAgent { public
static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { System.out.println("Hello, JavaSummit!"); } }
Java Agent - Premain 30 public class JavaAgent { public
static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { System.out.println("Hello, JavaSummit!"); } }
Java Agent - Premain 31 public class JavaAgent { public
static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { System.out.println("Hello, JavaSummit!"); } }
Java Agent - Premain 32 • Build JavaAgent to JAR
• Run with java agent: java -javaagent:JavaAgent.jar -jar HelloWorld.jar • Output: Hello, JavaSummit! Hello, World! ...
Java Agent - ClassFileTransformer 33 JVM Compiler Code Bytecode
Java Agent - ClassFileTransformer 34 JVM 32 | … 33
| new Item() 34 | … Class Loader Get Item class bytecode Return Item class bytecode
Java Agent - ClassFileTransformer 35 JVM 32 | … 33
| new Item() 34 | … Class Loader Get Item class bytecode Return Item class bytecode Class File Transformer Return transformed Item class bytecode
Java Agent - ClassFileTransformer 36 public class JavaAgent { public
static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { System.out.println("Hello, JavaSummit!"); } } • void addTransformer(ClassFileTransformer transformer);
Java Agent - ClassFileTransformer 37 public class DebugTransformer implements ClassFileTransformer
{ @Override public byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
Java Agent - ClassFileTransformer 38 public class DebugTransformer implements ClassFileTransformer
{ @Override public byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
Java Agent - ClassFileTransformer 39 public class DebugTransformer implements ClassFileTransformer
{ @Override public byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
Open Source Bytecode Manipulation Tools BCEL ASM 40
Bytecode Abstraction 41 • AbstractInsnNode ◦ JumpInsnNode ◦ MethodInsnNode ◦
VarInsnNode • InsnList instructions
Bytecode Manipulation 42 byte[] addPrintCall(byte[] classfileBuffer) { // Add call
to System.out.println("Transformed!") // Return transformed bytecode }
Java Agent - ClassFileTransformer 43 public class DebugTransformer implements ClassFileTransformer
{ @Override public byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { if (className.contains("HelloWorld")) { return addPrintCall(classfileBuffer); } return classfileBuffer; } }
Java Agent - ClassFileTransformer 44 public class DebugTransformer implements ClassFileTransformer
{ @Override public byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { if (className.contains("HelloWorld")) { return addPrintCall(classfileBuffer); } return classfileBuffer; } }
Java Agent - Premain 45 public class JavaAgent { public
static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { DebugTransformer transformer = new DebugTransformer(); instrumentation.addTransformer(transformer); } }
Java Agent - Premain 46 public class JavaAgent { public
static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { DebugTransformer transformer = new DebugTransformer(); instrumentation.addTransformer(transformer); } }
Java Agent - Premain 47 • Build JavaAgent to JAR
• Run with java agent: java -javaagent:JavaAgent.jar -jar HelloWorld.jar • Output: Transformed! Hello, World! Hello, World! ...
Non-Breaking Breakpoints!
Profiling Interpreter C2 C1 Profiling Profiling Interpreter C2 C1 Profiling
JVM Execution Engine 49
Add Breakpoint 50 public void Breakpoint(Object[] locals) { System.out.println("Breakpoint hit!");
for (Object local : locals) { System.out.println(local); } }
Add Breakpoint 51 byte[] addBreakpoint(byte[] classfileBuffer, int lineno) { //
Find where to insert Breakpoint call // Push local variables onto stack // Insert call to Breakpoint // Return transformed bytecode }
Add Breakpoint 52 byte[] addBreakpoint(byte[] classfileBuffer, int lineno) { //
Find where to insert Breakpoint call // Push local variables onto stack // Insert call to Breakpoint // Return transformed bytecode }
Add Breakpoint 53 byte[] addBreakpoint(byte[] classfileBuffer, int lineno) { //
Find where to insert Breakpoint call // Push local variables onto stack // Insert call to Breakpoint // Return transformed bytecode }
Add Breakpoint 54 byte[] addBreakpoint(byte[] classfileBuffer, int lineno) { //
Find where to insert Breakpoint call // Push local variables onto stack // Insert call to Breakpoint // Return transformed bytecode }
Add Breakpoint 55 byte[] addBreakpoint(byte[] classfileBuffer, int lineno) { //
Find where to insert Breakpoint call // Push local variables onto stack // Insert call to Breakpoint // Return transformed bytecode }
Sample Program 56 1| public class HelloWorld { 2| public
static void main(String[] args) { 3| while(true) { 4| System.out.println("Hello, World!"); 5| } 6| } 7| }
Java Agent - ClassFileTransformer 57 public class DebugTransformer implements ClassFileTransformer
{ @Override public byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { if (className.contains("HelloWorld")) { return addBreakpoint(classfileBuffer, 4); } return classfileBuffer; } }
Java Agent - Premain 58 • Build JavaAgent to JAR
• Run with java agent: java -javaagent:JavaAgent.jar -jar HelloWorld.jar • Output: Breakpoint hit! [Ljava.lang.Object;@3d075dc0 Hello, World! ...
59 What if the class has already been loaded?
Magic Instrumentation 60 void redefineClasses(ClassDefinition... definitions) void retransformClasses(Class<?>... classes)
Putting it all together 61 • Transform on demand •
Add call to breakpoint function • Access locals as objects
62 Putting it all together
Thank You @yardenlaif rookout.com