Slide 1

Slide 1 text

Debugging Unveiled Johannes Bechberger mostlynerdless.de

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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

➜ java LineCounter.java \ lines LineCounter.java 36

Slide 5

Slide 5 text

➜ 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

Let’s look at the code

Slide 7

Slide 7 text

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 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

Any ideas?

Slide 10

Slide 10 text

Let’s use a debugger

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

... and more

Slide 17

Slide 17 text

Slight focus on IntelliJ

Slide 18

Slide 18 text

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 19

Slide 19 text

Question time

Slide 20

Slide 20 text

Any OpenJDK developers here?

Slide 21

Slide 21 text

Guessed so But still feel free to correct me.

Slide 22

Slide 22 text

Who of you used a debugger before?

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Who of you remotely debugged before?

Slide 25

Slide 25 text

Good to know my audience

Slide 26

Slide 26 text

Debugging Testing Profiling Toolbox

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Introduction Remote JDWP On Demand Architecture

Slide 30

Slide 30 text

General Architecture

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Introduction Remote JDWP On Demand Architecture

Slide 33

Slide 33 text

Remote Debugging

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Differences between remote and non-remote? None

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Little Known Gems

Slide 40

Slide 40 text

Introduction Remote JDWP On Demand Architecture

Slide 41

Slide 41 text

On Demand Debugging

Slide 42

Slide 42 text

➜ 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 43

Slide 43 text

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

Slide 44

Slide 44 text

Firewalls only dt_socket

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Let's go on a short journey

Slide 47

Slide 47 text

I went to Devoxx Belgium

Slide 48

Slide 48 text

Writing a blog post on the train

Slide 49

Slide 49 text

What a train station

Slide 50

Slide 50 text

Meeting some people loosely related to Java

Slide 51

Slide 51 text

Looking into a JDWP agent bug at the Coretto booth

Slide 52

Slide 52 text

That has been there for ages

Slide 53

Slide 53 text

A mysterious Bug

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

Introduction Remote JDWP On Demand Architecture only 120 more slides

Slide 59

Slide 59 text

JDWP From IDE to JVM calls

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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 Reply

Slide 62

Slide 62 text

And lot's of these From connection start to first suspend

Slide 63

Slide 63 text

Demo

Slide 64

Slide 64 text

JDWP From connection start to first suspend

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

Debugger JVM VM Start Event Initial thread

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

Debugger JVM Enable class prep events

Slide 69

Slide 69 text

Debugger JVM Request version VM version and name

Slide 70

Slide 70 text

Debugger JVM Request capabilities

Slide 71

Slide 71 text

Debugger JVM Request new capabilities since JDWP 1.4

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

Debugger JVM Request all threads [thread id]

Slide 74

Slide 74 text

Debugger JVM Enable thread start events

Slide 75

Slide 75 text

Debugger JVM Main and notify threads started thread ids

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

Debugger JVM Get source file of class Obtain information on class

Slide 80

Slide 80 text

Debugger JVM ... and more Obtain information on class

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

Debugger JVM Resume

Slide 83

Slide 83 text

Debugger JVM Hit breakpoint

Slide 84

Slide 84 text

Debugger JVM Hit breakpoint

Slide 85

Slide 85 text

Debugger JVM Get current frames, ...

Slide 86

Slide 86 text

Debugger JVM Resume

Slide 87

Slide 87 text

Debugger JVM ...

Slide 88

Slide 88 text

Debugger JVM VM Death

Slide 89

Slide 89 text

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 90

Slide 90 text

Debugger JVM VM Death But does the JVM have to die?

Slide 91

Slide 91 text

Who killed the JVM?

Slide 92

Slide 92 text

Debugger JVM It dies on its own or via Exit

Slide 93

Slide 93 text

So we can reattach a debugger?

Slide 94

Slide 94 text

Terminate Disconnect Eclipse

Slide 95

Slide 95 text

Close tab Stop (Re)connect IntelliJ

Slide 96

Slide 96 text

IntelliJ

Slide 97

Slide 97 text

Conditional Breakpoints

Slide 98

Slide 98 text

Any performance problems?

Slide 99

Slide 99 text

Do conditional breakpoints have JVM support?

Slide 100

Slide 100 text

defined nowhere

Slide 101

Slide 101 text

Many packages

Slide 102

Slide 102 text

No support Emulated via breakpoints and evaluation

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

Debugger JVM line.contains("//")

Slide 123

Slide 123 text

This explains the breakpoint options

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

Same...

Slide 130

Slide 130 text

Reasons for this

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

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

Slide 133

Slide 133 text

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

Slide 134

Slide 134 text

I came up with one

Slide 135

Slide 135 text

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 136

Slide 136 text

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 137

Slide 137 text

Is DAP better?

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

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

Slide 141

Slide 141 text

Extract methods ... and potentially use hotswap

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

Recompile class and hot swap

Slide 145

Slide 145 text

Add condition to code ... and potentially use hotswap

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

Question time

Slide 148

Slide 148 text

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

Slide 149

Slide 149 text

Do you know safepoints?

Slide 150

Slide 150 text

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

Slide 151

Slide 151 text

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 152

Slide 152 text

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

Slide 153

Slide 153 text

More IDE features and how they are implemented using JDWP

Slide 154

Slide 154 text

Hot Swap

Slide 155

Slide 155 text

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

Slide 156

Slide 156 text

Debugger JVM Get methods and fields

Slide 157

Slide 157 text

Set Specific Value

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

Debugger JVM Get visible classes

Slide 163

Slide 163 text

Debugger JVM Get value of args[1]

Slide 164

Slide 164 text

Debugger JVM Get methods of Path

Slide 165

Slide 165 text

Debugger JVM Invoke of method

Slide 166

Slide 166 text

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

Slide 167

Slide 167 text

Force Return

Slide 168

Slide 168 text

No content

Slide 169

Slide 169 text

No content

Slide 170

Slide 170 text

No content

Slide 171

Slide 171 text

No content

Slide 172

Slide 172 text

Debugger JVM Force early return request thread, return value

Slide 173

Slide 173 text

Does forced return, execute finalize blocks?

Slide 174

Slide 174 text

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 175

Slide 175 text

Drop/Reset Frame

Slide 176

Slide 176 text

Debugger JVM Pop frames request

Slide 177

Slide 177 text

Debugger JVM Get frames

Slide 178

Slide 178 text

Throw Exception

Slide 179

Slide 179 text

Debugger JVM Create RuntimeEx instance

Slide 180

Slide 180 text

Debugger JVM Stop request thread, throwable

Slide 181

Slide 181 text

It's arcane But better than Python and fast enough PyData 16th November DIY Python Debugger and really versatile

Slide 182

Slide 182 text

Debugging Testing Profiling Toolbox

Slide 183

Slide 183 text

No content

Slide 184

Slide 184 text

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