Slide 1

Slide 1 text

Public Debugging Unveiled Johannes Bechberger mostlynerdless.de

Slide 2

Slide 2 text

Public

Slide 3

Slide 3 text

Public If debugging is the process of removing software bugs, then programming must be the process of putting them in. — Edsger Dijkstra “

Slide 4

Slide 4 text

Public ➜ java LineCounter.java \ lines LineCounter.java 36

Slide 5

Slide 5 text

Public ➜ java LineCounter.java lines LineCounter.java Exception in thread "main" java.nio.file.NoSuchFileException: lines at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92) at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106) at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111) at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:261 at java.base/java.nio.file.Files.newByteChannel(Files.java:379) at java.base/java.nio.file.Files.newByteChannel(Files.java:431) at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420) at java.base/java.nio.file.Files.newInputStream(Files.java:159) at java.base/java.nio.file.Files.newBufferedReader(Files.java:2897) at java.base/java.nio.file.Files.readAllLines(Files.java:3392) at java.base/java.nio.file.Files.readAllLines(Files.java:3433) at LineCounter.countLines(LineCounter.java:24) at LineCounter.main(LineCounter.java:10)

Slide 6

Slide 6 text

Public Let’s look at the code

Slide 7

Slide 7 text

Public public static void main(String[] args) throws IOException { Path file = Path.of(args[0]); int count = switch (args[0]) { case "lines" -> countLines(file); case "code_lines" -> countCodeLines(file); default -> { System.err.println("Usage: java ... " + " "); System.exit(1); yield 0; } }; System.out.println(count); }

Slide 8

Slide 8 text

Public public static int countLines(Path file) { return Files.readAllLines(file).size(); } public static int countCodeLines(Path file) { int count = 0; for (String line : Files.readAllLines(file)) { if (!line.isBlank() && !line.startsWith("//")) { count++; } } return count; }

Slide 9

Slide 9 text

Public Any ideas?

Slide 10

Slide 10 text

Public Let’s use a debugger

Slide 11

Slide 11 text

Public

Slide 12

Slide 12 text

Public

Slide 13

Slide 13 text

Public

Slide 14

Slide 14 text

Public

Slide 15

Slide 15 text

Public

Slide 16

Slide 16 text

Public ... and more

Slide 17

Slide 17 text

Public Slight focus on IntelliJ...

Slide 18

Slide 18 text

Public Breakpoint properties in IntelliJ

Slide 19

Slide 19 text

Public Breakpoint properties in NetBeans

Slide 20

Slide 20 text

Public Disabling line numbers is harder in IntelliJ

Slide 21

Slide 21 text

Public Eclipse

Slide 22

Slide 22 text

Public Why debug? •Find and fix bugs! •Analyze the code •Add more logging on the fly •Change behavior on the fly •Analyze memory issues •And much more — Egor Ushakov “

Slide 23

Slide 23 text

Public Question time

Slide 24

Slide 24 text

Public Any OpenJDK developers here?

Slide 25

Slide 25 text

Public Guessed so But still feel free to correct me.

Slide 26

Slide 26 text

Public Who of you used a debugger before?

Slide 27

Slide 27 text

Public

Slide 28

Slide 28 text

Public Who of you remotely debugged before?

Slide 29

Slide 29 text

Public Good to know my audience

Slide 30

Slide 30 text

Public Debugging Testing Profiling Toolbox

Slide 31

Slide 31 text

Public

Slide 32

Slide 32 text

Public

Slide 33

Slide 33 text

Public Introduction Remote JDWP On Demand Architecture

Slide 34

Slide 34 text

Public General Architecture

Slide 35

Slide 35 text

Public IntelliJ JDI JDWP Agent JVM Java JDWP JVMTI VSCode Java Debug Server DAP Java JPDA eclipse debugger based

Slide 36

Slide 36 text

Public Introduction Remote JDWP On Demand Architecture

Slide 37

Slide 37 text

Public Remote Debugging

Slide 38

Slide 38 text

Public

Slide 39

Slide 39 text

Public -agentlib:jdwp= transport=dt_socket, server=y, suspend=y, address=*:5005 Any issues?

Slide 40

Slide 40 text

Public -agentlib:jdwp= transport=dt_socket, server=y, suspend=y, address=*:5005

Slide 41

Slide 41 text

Public Differences between remote and non-remote? None

Slide 42

Slide 42 text

Public

Slide 43

Slide 43 text

Public Little Known Gems

Slide 44

Slide 44 text

Public Introduction Remote JDWP On Demand Architecture

Slide 45

Slide 45 text

Public On Demand Debugging

Slide 46

Slide 46 text

Public ➜ java -agentlib:jdwp=transport=dt_socket,server=y, suspend=y,address=*:5005,onjcmd=y src/test/java/OnThrowAndJCmd.java & ➜ echo $! # get pid # wait some time and then start debugging on demand ➜ jcmd $! VM.start_java_debugging jcmd 97145 VM.start_java_debugging 97145: Debugging has been started. Transport : dt_socket Address : *:5005

Slide 47

Slide 47 text

Public

Slide 48

Slide 48 text

Public On Exception Debugging throw new Ex(""); start debugging

Slide 49

Slide 49 text

Public ➜ java -agentlib:jdwp=transport=dt_socket,server=y, suspend=y,address=*:5005,onthrow=Ex, launch=exit src/test/java/OnThrowAndJCmd.java # or ➜ java -agentlib:jdwp=transport=dt_socket,server=y, suspend=y,address=*:5005,onuncaught=y, launch=exit src/test/java/OnThrowAndJCmd.java

Slide 50

Slide 50 text

Public What about the launch option?

Slide 51

Slide 51 text

Public tmux_jdb.sh #!/bin/sh tmux new-session -d -s jdb -- jdb -attach $2

Slide 52

Slide 52 text

Public ➜ java “-agentlib:jdwp=transport=dt_socket,server=y, suspend=y,address=*:5005, onthrow=Ex,launch=sh tmux_jdb.sh” src/test/java/OnThrowAndJCmd.java # in another console after the exception is # thrown ➜ tmux attach -t jdb

Slide 53

Slide 53 text

Public

Slide 54

Slide 54 text

Public

Slide 55

Slide 55 text

Public Firewalls only dt_socket

Slide 56

Slide 56 text

Public

Slide 57

Slide 57 text

Public JDB + onthrow Demo

Slide 58

Slide 58 text

Public java "-agentlib:jdwp=transport=dt_socket,server=y, suspend=y,address=*:5005,onthrow=Ex,launch=exit" src/test/java/OnThrowAndJCmd.java # in other terminal jdb -attach 5005

Slide 59

Slide 59 text

Public Let's go on a short journey

Slide 60

Slide 60 text

Public I went to Devoxx Belgium

Slide 61

Slide 61 text

Public Writing a blog post on the train

Slide 62

Slide 62 text

Public What a train station

Slide 63

Slide 63 text

Public Meeting some people loosely related to Java

Slide 64

Slide 64 text

Public Looking into a JDWP agent bug at the Coretto booth

Slide 65

Slide 65 text

Public That has been there for ages

Slide 66

Slide 66 text

Public A mysterious Bug

Slide 67

Slide 67 text

Public

Slide 68

Slide 68 text

Public

Slide 69

Slide 69 text

Public Exception in thread "event-handler" java.lang.NullPointerException: Cannot invoke "com.sun.jdi.ObjectReference.referenceType()" because the return value of "com.sun.jdi.event.ExceptionEvent.exception()" is null at jdk.jdi/com.sun.tools.example.debug.tty.TTY.exceptionEvent(TT Y.java:171) at jdk.jdi/com.sun.tools.example.debug.tty.EventHandler.exceptio nEvent(EventHandler.java:295) at jdk.jdi/com.sun.tools.example.debug.tty.EventHandler.handleEv ent(EventHandler.java:133) at jdk.jdi/com.sun.tools.example.debug.tty.EventHandler.run(Even tHandler.java:78) at java.base/java.lang.Thread.run(Thread.java:1583)

Slide 70

Slide 70 text

Public cbEarlyException handler ex.class == "Ex"? initialize(env, thread, EI_EXCEPTION) yes start debugging session debugger attached set standard Exception handler send Ex exception event ignore no send Exception event 2nd Ex throw 1st Ex throw null pointers

Slide 71

Slide 71 text

Public

Slide 72

Slide 72 text

Public Introduction Remote JDWP On Demand Architecture only 120 more slides

Slide 73

Slide 73 text

Public JDWP From IDE to JVM calls

Slide 74

Slide 74 text

Public Packets length id flags (= 0) command set command data 4 bytes 4 bytes 1 byte 1 bytes 1 bytes variable Request/Event length id flags (= 0x80) error code data 4 bytes 4 bytes 1 byte 1 bytes variable Request/Event

Slide 75

Slide 75 text

Public JDWP From connection start to first suspend

Slide 76

Slide 76 text

Public Debugger JVM "JDWP-Handshake" "JDWP-Handshake"

Slide 77

Slide 77 text

Public Debugger JVM VM Start Event Initial thread

Slide 78

Slide 78 text

Public Debugger JVM ID sizes fieldID, methodID, objectID, refTypeID, frameID size

Slide 79

Slide 79 text

Public Debugger JVM Enable class prep events

Slide 80

Slide 80 text

Public Debugger JVM Request version VM version and name

Slide 81

Slide 81 text

Public Debugger JVM Request capabilities

Slide 82

Slide 82 text

Public Debugger JVM Request new capabilities since JDWP 1.4

Slide 83

Slide 83 text

Public Debugger JVM Request all classes [kind, id, signature, status] Reason for slow start of session

Slide 84

Slide 84 text

Public Debugger JVM Request all threads [thread id]

Slide 85

Slide 85 text

Public Debugger JVM Enable thread start events

Slide 86

Slide 86 text

Public Debugger JVM Main and notify threads started thread ids

Slide 87

Slide 87 text

Public Debugger JVM Prepared URLClassPath.FileLoader class thread id, kind, type id, signature, status

Slide 88

Slide 88 text

Public Debugger JVM Prepared LineCounter class thread id, kind, type id, signature, status

Slide 89

Slide 89 text

Public Debugger JVM Get line table of methods Obtain information on class [code index, line]

Slide 90

Slide 90 text

Public Debugger JVM Get source file of class Obtain information on class

Slide 91

Slide 91 text

Public Debugger JVM ... and more Obtain information on class

Slide 92

Slide 92 text

Public Debugger JVM Set breakpoint JVM supports breakpoints Do you know any runtimes that don't? Python < 3.12

Slide 93

Slide 93 text

Public Debugger JVM Resume

Slide 94

Slide 94 text

Public Debugger JVM Hit breakpoint

Slide 95

Slide 95 text

Public Debugger JVM Hit breakpoint

Slide 96

Slide 96 text

Public Debugger JVM Get current frames, ...

Slide 97

Slide 97 text

Public Debugger JVM Resume

Slide 98

Slide 98 text

Public Debugger JVM ...

Slide 99

Slide 99 text

Public Debugger JVM VM Death

Slide 100

Slide 100 text

Public Many commands are available https://docs.oracle.com/en/java/javase/17/docs/specs/jdwp/jd wp-protocol.html * 95 to be precise with JDK 20 *

Slide 101

Slide 101 text

Public Conditional Breakpoints

Slide 102

Slide 102 text

Public Any performance problems?

Slide 103

Slide 103 text

Public Do conditional breakpoints have JVM support?

Slide 104

Slide 104 text

Public defined nowhere

Slide 105

Slide 105 text

Public No support Emulated via breakpoints and evaluation

Slide 106

Slide 106 text

Public Debugger JVM Handshake, ... line.contains("//")

Slide 107

Slide 107 text

Public Debugger JVM Set breakpoint at count++ line.contains("//")

Slide 108

Slide 108 text

Public Debugger JVM Suspend line.contains("//")

Slide 109

Slide 109 text

Public Debugger JVM Get value of line line.contains("//") import java.io.IOException;

Slide 110

Slide 110 text

Public Debugger JVM Create String "//" line.contains("//") string reference

Slide 111

Slide 111 text

Public Debugger JVM Get methods of String line.contains("//") method references

Slide 112

Slide 112 text

Public Debugger JVM Invoke contains method line.contains("//") false

Slide 113

Slide 113 text

Public Debugger JVM Resume line.contains("//")

Slide 114

Slide 114 text

Public Debugger JVM Invoke contains method line.contains("//") false

Slide 115

Slide 115 text

Public Debugger JVM Suspend event line.contains("//")

Slide 116

Slide 116 text

Public Debugger JVM Get value of line line.contains("//") import java.nio.file.Files;

Slide 117

Slide 117 text

Public Debugger JVM ... line.contains("//")

Slide 118

Slide 118 text

Public Debugger JVM Resume line.contains("//")

Slide 119

Slide 119 text

Public Debugger JVM ... line.contains("//")

Slide 120

Slide 120 text

Public Debugger JVM Suspend event line.contains("//")

Slide 121

Slide 121 text

Public Debugger JVM Get value of line line.contains("//") // Count lines in a file

Slide 122

Slide 122 text

Public Debugger JVM Create String "//" line.contains("//") string reference

Slide 123

Slide 123 text

Public Debugger JVM Invoke contains method line.contains("//") true

Slide 124

Slide 124 text

Public Debugger JVM Get frame and stack info line.contains("//")

Slide 125

Slide 125 text

Public Debugger JVM line.contains("//")

Slide 126

Slide 126 text

Public This explains the breakpoint options

Slide 127

Slide 127 text

Public

Slide 128

Slide 128 text

Public

Slide 129

Slide 129 text

Public

Slide 130

Slide 130 text

Public

Slide 131

Slide 131 text

Public Any guess how watches, eval expression, ... are implemented?

Slide 132

Slide 132 text

Public Same...

Slide 133

Slide 133 text

Public Reasons for this

Slide 134

Slide 134 text

Public JVMTI is a terrible API, we should get rid of it as much as possible. — Anonymous Java Architect “

Slide 135

Slide 135 text

Public Conditional Breakpoints are complex ... and slow, especially with large conditions

Slide 136

Slide 136 text

Public Any ideas of improvements? if you could control both IDE and JVM

Slide 137

Slide 137 text

Public I came up with one

Slide 138

Slide 138 text

Public Implement a Caching and Prefetching tunnel Cloud Application Developer Debugger Internet get x x get x x get y y get y y JDWP

Slide 139

Slide 139 text

Public Implement a Caching and Prefetching tunnel Cloud Application Developer Debugger Internet get x x JDWP get y y C o n get x x JDWP get y y C o n get x,y x, y get x,y x, y HTTP

Slide 140

Slide 140 text

Public

Slide 141

Slide 141 text

Public Is DAP better?

Slide 142

Slide 142 text

Public

Slide 143

Slide 143 text

Public

Slide 144

Slide 144 text

Public But... IntelliJ JDI JDWP Agent JVM Java JDWP JVMTI VSCode Java Debug Server DAP Java JPDA

Slide 145

Slide 145 text

Public Extract methods ... and potentially use hotswap

Slide 146

Slide 146 text

Public

Slide 147

Slide 147 text

Public

Slide 148

Slide 148 text

Public Recompile class and hot swap

Slide 149

Slide 149 text

Public Add condition to code ... and potentially use hotswap

Slide 150

Slide 150 text

Public

Slide 151

Slide 151 text

Public Question time

Slide 152

Slide 152 text

Public Are methods with breakpoints interpreted? Yes, they are deoptimized. https://github.com/openjdk/jdk/blob/edcc559f09364da3692862e1f3d0636aa8eec1d4/ src/hotspot/share/code/codeCache.cpp#L1425

Slide 153

Slide 153 text

Public Do you know safepoints?

Slide 154

Slide 154 text

Public What do they have in common with stepping? Stepping is also implemented with safepoints

Slide 155

Slide 155 text

Public Are methods interpreted during single stepping? Yes, they are deoptimized. https://github.com/openjdk/jdk/blob/d31391597433cf275fc615e0148c48c34acf6e11/ src/hotspot/share/prims/jvmtiEventController.cpp#L205

Slide 156

Slide 156 text

Public # of resources on this topic? None, just one from Apache Harmony https://harmony.apache.org/subcomponents/drlvm/breakpoints_and_ss.html 2011

Slide 157

Slide 157 text

Public More IDE features and how they are implemented using JDWP

Slide 158

Slide 158 text

Public Hot Swap

Slide 159

Slide 159 text

Public

Slide 160

Slide 160 text

Public

Slide 161

Slide 161 text

Public

Slide 162

Slide 162 text

Public Debugger JVM Redefine classes request [class id, byte code bytes]

Slide 163

Slide 163 text

Public Debugger JVM Get methods and fields

Slide 164

Slide 164 text

Public Set Specific Value

Slide 165

Slide 165 text

Public

Slide 166

Slide 166 text

Public

Slide 167

Slide 167 text

Public

Slide 168

Slide 168 text

Public

Slide 169

Slide 169 text

Public Debugger JVM Get visible classes

Slide 170

Slide 170 text

Public Debugger JVM Get value of args[1]

Slide 171

Slide 171 text

Public Debugger JVM Get methods of Path

Slide 172

Slide 172 text

Public Debugger JVM Invoke of method

Slide 173

Slide 173 text

Public Debugger JVM Set stack frame value request thread, frame, slot + slot value

Slide 174

Slide 174 text

Public https://github.com/openjdk/jdk/blo b/af8973dc509c1f326223e3ffd1773 c9e930141d8/src/hotspot/share/pri ms/jvmtiImpl.cpp#L671

Slide 175

Slide 175 text

Public Force Return

Slide 176

Slide 176 text

Public

Slide 177

Slide 177 text

Public

Slide 178

Slide 178 text

Public

Slide 179

Slide 179 text

Public

Slide 180

Slide 180 text

Public Debugger JVM Force early return request thread, return value

Slide 181

Slide 181 text

Public Does forced return, execute finalize blocks?

Slide 182

Slide 182 text

Public Does forced return, execute finalize blocks? No No further instructions are executed in the called method. Specifically, finally blocks are not executed. “ — JDWP documentation for JDK 17

Slide 183

Slide 183 text

Public Drop/Reset Frame

Slide 184

Slide 184 text

Public Debugger JVM Pop frames request

Slide 185

Slide 185 text

Public Debugger JVM Get frames

Slide 186

Slide 186 text

Public Throw Exception

Slide 187

Slide 187 text

Public

Slide 188

Slide 188 text

Public

Slide 189

Slide 189 text

Public Debugger JVM Create RuntimeEx instance

Slide 190

Slide 190 text

Public Debugger JVM Stop request thread, throwable

Slide 191

Slide 191 text

Public Method and field breakpoints

Slide 192

Slide 192 text

Public Debugging Testing Profiling Toolbox

Slide 193

Slide 193 text

Public

Slide 194

Slide 194 text

Public @parttimen3rd on Twitter parttimenerd on GitHub mostlynerdless.de @SweetSapMachine sapmachine.io