Post

Java random stuff

openjdk is the starting point. However, IntelliJ fails to resolve symbols when open it directly. Actually, openjdk team provides some guidance on how to set up the repo in IntelliJ. See doc.

1
2
bash configure
bash bin/idea.sh

I encountered a few errors when running above two commands in a Macbook M1. In the configure step,

1
configure: error: XCode tool 'metal' neither found in path nor with xcrun

This post helps. Basically, we need to change the active developer directory of xcode. We can change it back once the configuration stage is done.

The second step requires ant: brew install ant.

However, after all these steps, it still does not work. Finally, I use a simple trick: File -> Project Structure -> Modules -> Add an existing JDK.

Debug

I am testing some Kafka Connect plugin recently. Instead of using print to debug, I really wish Java can provide some functionality similar to a dynamic language such as I insert a breakpoint() in the code, then the running process will jump to a REPL shell.

Then I realize Java provides remote debugging, or more specifically, JDWP (Java Debug Wire Protocol). Basically, when we start the Java program, we add some command line arguments to open some socket to allow remote debugger to attach. For the case of Kafka Connect, we can enable it by setting the KAFKA_DEBUG environment variable. See code.

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

server=y means that it starts a server and waits for client to connect. server=n has reverse meaning.

Then we can use jdb to connect to this process

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ jdb -attach 127.0.0.1:5005

Initializing jdb ...

> help
** command list **
connectors                -- list available connectors and transports in this VM

run [class [args]]        -- start execution of application's main class

threads [threadgroup]     -- list threads in threadgroup. Use current threadgroup if none specified.
thread <thread id>        -- set default thread
suspend [thread id(s)]    -- suspend threads (default: all)
resume [thread id(s)]     -- resume threads (default: all)
where [<thread id> | all] -- dump a thread's stack
wherei [<thread id> | all]-- dump a thread's stack, with pc info
up [n frames]             -- move up a thread's stack
down [n frames]           -- move down a thread's stack
kill <thread id> <expr>   -- kill a thread with the given exception object
interrupt <thread id>     -- interrupt a thread
...

It is very similar to gdb. You can set breakpoint, print out thread stack trace. And I think it has better support for debugging multi-thread program.

A few notes about breakpoints. First, we must use full class path such as stop at org.opensearch.cluster.ClusterState:706. Second, for inner class, we should do stop at <full_class_path>$<inner_class_name>:line_number. For example, stop at org.opensearch.cluster.ClusterState$Builder:706. You can inspect the outer class by class <full_class_path>.

Cross compilation

javac has three options related to cross compilation: -source, -target and -releaes. JEP 247 has concise description about their usage.

This post is licensed under CC BY 4.0 by the author.