{"action":"created","issue":{"url":"https://api.github.com/repos/tada/pljava/issues/127","repository_url":"https://api.github.com/repos/tada/pljava","labels_url":"https://api.github.com/repos/tada/pljava/issues/127/labels{/name}","comments_url":"https://api.github.com/repos/tada/pljava/issues/127/comments","events_url":"https://api.github.com/repos/tada/pljava/issues/127/events","html_url":"https://github.com/tada/pljava/issues/127","id":237539877,"number":127,"title":"Debugging \"My\" Pl/Java Code","user":{"login":"TasMot","id":477660,"avatar_url":"https://avatars1.githubusercontent.com/u/477660?v=3","gravatar_id":"","url":"https://api.github.com/users/TasMot","html_url":"https://github.com/TasMot","followers_url":"https://api.github.com/users/TasMot/followers","following_url":"https://api.github.com/users/TasMot/following{/other_user}","gists_url":"https://api.github.com/users/TasMot/gists{/gist_id}","starred_url":"https://api.github.com/users/TasMot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TasMot/subscriptions","organizations_url":"https://api.github.com/users/TasMot/orgs","repos_url":"https://api.github.com/users/TasMot/repos","events_url":"https://api.github.com/users/TasMot/events{/privacy}","received_events_url":"https://api.github.com/users/TasMot/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-06-21T13:45:55Z","updated_at":"2017-06-22T00:11:01Z","closed_at":null,"body":"I don't know where else to stick this so that it can be found by others that may need it, so I'm sticking it here. My Pl/Java code wasn't working (big surprise of course). So I wanted to do some remote debugging from Eclipse. It involved 2 steps. First though, this is using :\r\n\r\n```\r\nEclipse IDE for Java Developers\r\nVersion: Neon.3 Release (4.6.3)\r\nBuild id: 20170314-1500\r\n\r\nPostgresSQL\r\nd:\\pljava>psql --version\r\npsql (PostgreSQL) 9.5.7\r\n```\r\n\r\nand\r\n```\r\nd:\\pljava>java -version\r\njava version \"1.8.0_131\"\r\nJava(TM) SE Runtime Environment (build 1.8.0_131-b11)\r\nJava HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)\r\n```\r\n\r\nIn postgresql.conf in D:\\data\\pg95\\ directory (my Postgres data directory) at the bottom add:\r\n\r\n```\r\n#------------------------------------------------------------------------------\r\n# CUSTOMIZED OPTIONS\r\n#------------------------------------------------------------------------------\r\n\r\n# Add settings for extensions here\r\n# Turn on Elcipse Debugging of the Java code:\r\npljava.vmoptions='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000'\r\n\r\n# end debug options\r\n```\r\n\r\nin Eclipse create a remote debugging profile:\r\n\r\nChoose the project to Debug,\r\n\r\n```\r\nConnection Type is \"Standard (Socket Attach)\"\r\n\r\nConnection Properties\r\nHost: localhost \r\nPort: 8000\r\n```\r\n\r\nIn my case, it is a local copy of Postgres - since this is debugging inside the main Postgres process, please realize that if you set this up on a shared development database, Postgres ONLY does the debugging and all other processing is suspended while the debug session is in process.\r\n\r\nFinally, the firewall has to allow the connection. In my case it was \"Windows Advanced Firewall\" but your firewall may be different. If the connection keeps getting refused, this may be the problem.\r\n\r\nPlease note that this is the \"current\" at the time of this writing for how to specify the debugging option for Java 8. It was different for prior versions of Java (it changed around Java 5 or 6).\r\n\r\nIt took me 2 days of Googling to put this all together and I've lost all the various references so I would like to thank those that I found bits of information from and Thank You, all of you.\r\n\r\nI hope this will help save someone else all the time it took me to get this working.\r\n\r\nTom \r\n\r\n"},"comment":{"url":"https://api.github.com/repos/tada/pljava/issues/comments/310239105","html_url":"https://github.com/tada/pljava/issues/127#issuecomment-310239105","issue_url":"https://api.github.com/repos/tada/pljava/issues/127","id":310239105,"user":{"login":"jcflack","id":3670696,"avatar_url":"https://avatars1.githubusercontent.com/u/3670696?v=3","gravatar_id":"","url":"https://api.github.com/users/jcflack","html_url":"https://github.com/jcflack","followers_url":"https://api.github.com/users/jcflack/followers","following_url":"https://api.github.com/users/jcflack/following{/other_user}","gists_url":"https://api.github.com/users/jcflack/gists{/gist_id}","starred_url":"https://api.github.com/users/jcflack/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jcflack/subscriptions","organizations_url":"https://api.github.com/users/jcflack/orgs","repos_url":"https://api.github.com/users/jcflack/repos","events_url":"https://api.github.com/users/jcflack/events{/privacy}","received_events_url":"https://api.github.com/users/jcflack/received_events","type":"User","site_admin":false},"created_at":"2017-06-22T00:11:00Z","updated_at":"2017-06-22T00:11:00Z","body":"Thanks for the report. I'm thinking the best place I should add this info is right in the [Debugging your Java code][dyjc] wiki page, which has `jdb` and `dbx` info so far, but not Eclipse.\r\n\r\nMy suggestion would be to leave `postgresql.conf` alone, and just do\r\n\r\n    SET pljava.vmoptions TO '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000';\r\n\r\nin the session you want to debug, before it does anything with PL/Java. That saves you having to restart or `SIGHUP` the server, and only affects the session you are debugging.\r\n\r\n> please realize that if you set this up on a shared development database, Postgres ONLY does the debugging and all other processing is suspended while the debug session is in process.\r\n\r\nThat's another reason to avoid putting debug options into `postgresql.conf`. Because PostgreSQL has a backend-process-per-session design (and a backend using PL/Java starts its own JVM), if you just `SET pljava.vmoptions` with debug settings in one session, only that session's JVM will start with debug enabled.\r\n\r\nNow, you could still use the debugger to set a breakpoint in your code, and if that's inside a transaction or holding a lock, you could end up blocking other database sessions that way. But if you put the options in `postgresql.conf`, you're going to have _every session_ that uses PL/Java trying to listen for a debug connection (and if you specifically assign a port number as in your example, they'll all try to listen on that port, which only one will be able to get).\r\n\r\nIt's also possible to leave off the `,address=8000` and then the system just picks an unused port and tells you the number it picked. Well, it doesn't really tell _you_, it writes the message to `stdout`, so it's not as convenient as it could be to find out the port it picked. You can look in the server log file if you have access.\r\n\r\nIf you'll be debugging on the same host, it turns out you don't need to find out the port that was picked. Instead of `Standard (Socket Attach)`, there should be a `Process Attach` option, and you just specify the PID, which you can get with `SELECT pg_backend_pid();` in the session you are debugging. I'm not sure exactly how that looks in Eclipse, but it looks like this using `jdb`:\r\n\r\n    jdb -attach 39663 # if you found out the port number, which is simply shorthand for\r\n    jdb -connect com.sun.jdi.SocketAttach:port=39663\r\n    # but if you only know the PID:\r\n    jdb -connect com.sun.jdi.ProcessAttach:pid=8887\r\n\r\nUnder the hood, the `ProcessAttach` just examines the process with PID 8887 to find out what port it's using, then does a `SocketAttach`. That saves you having to dig for the port number yourself, but it only works on the same host. If you want to connect your debugger from another box, you have to know the port.\r\n\r\nThere's a StackOverflow answer giving [sample code to determine the port that was picked][stof] if you know the PID (in other words, basically what `ProcessAttach` is doing under the hood).\r\n\r\nOne caution is when you run with `server=y` debugging options, the JVM will listen for a debug connection on _any_ network interface (possibly including from the wild wooly internet). You can also specify `address=localhost:8000` to ensure it only listens for connections from the local machine. (Or, `address=localhost:` to let it choose the port number; notice the colon!)\r\n\r\nIt would be ideal to combine `localhost` listening with `ProcessAttach`, which only works on the local host anyway, but `ProcessAttach` seems to be too stupid: it examines the process, finds out the port being used, then tries to connect to that port on the network interface instead of `localhost`. Ooops...\r\n\r\nThe place to read about all these transport options is [here][jpda].\r\n\r\nNow a question for you: I think I tried this with Eclipse at one time, got it to attach with no problem, but had trouble getting it to find the source files and show source (I had not developed the code inside an Eclipse project). Have you found the secret to that?\r\n\r\n[dyjc]: https://github.com/tada/pljava/wiki/Debugging-your-java-code\r\n[stof]: https://stackoverflow.com/questions/28745065/\r\n[jpda]: http://docs.oracle.com/javase/8/docs/technotes/guides/jpda/conninv.html"}}
{"actor":{"display_login":"jcflack"}}