Remote debugging with Eclipse IDE when OH is started by systemd

This isn’t a question but a useful tip. I’m not sure where to put it but I’m happy to contribute to the docs if there’s a relevant place.

When doing binding development, remote debugging with Eclipse can be helpful when trying to track down issues in your code. There’s a start_debug.sh script provided, but I use the systemd unit included in the rpm to start OH. Systemd sets up the environment and runs the process as the openhab user/group. The systemd environment setup is different from what the karaf script provides, and for me, nothing works correctly when started directly with start.sh or start_debug.sh.

I found this topic which says it’s not possible to attach a remote debugger without the debug-specific configuration, but I didn’t find that to be true. It’s actually fairly easy.

In /etc/default/openhab, find the EXTRA_JAVA_OPTS line. Uncomment it if needed and set it to this, or add this to whatever you have in there already:

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

The suspend parameter controls what happens at startup. If you set it to n then OH will start normally and you can join the debugger to the running process at any time. If you use y then it will pause at startup and wait for the debugger to be attached.

There are plenty of guides on how to set up the Eclipse side of things, but the short version is: choose Run > Debug Configurations, click on Remote Java Application on the left, and click on the New Configuration button. In the Connect window, make sure the project is set to whatever you want to debug, set the Connection Type to Standard (Socket Attach), set the host accordingly, and use 5005 for the port number. Then start OH and click the Debug button to attach. Be patient, it might take a while for the connection to establish.

If you use suspend=y and set some breakpoints, you can easily debug just about anything!

5 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.