Where is a given thread connecting?

We start by getting the PID of our running JVM…

-bash-4.1$ jps
29527 Main
3560 Jps

…then we print each lightweight process (threads in our JVM) in which we are interested. In our case, these are threads with zafu in the name, but it could be anything in which you are interested…

-bash-4.1$ for f in $(jstack 29527 | awk '$0 ~ "zafu.*nid=" {i=0;while (++i <= NF) {if ($i ~ "nid=") {split($i,t,"=");print t[2]}}}' | grep 0x); do p=$(printf "%i" $f); ps -eLf | awk -v p=$p '$4 == p'; done | awk '{print $4,$7}'
31215 04:23
22000 Jun19
21159 Jun19
20715 Jun19
20682 Jun19
20526 Jun19
20103 Jun19
20063 Jun19
19755 Jun19
19057 Jun19
18153 Jun19
27228 Jun19
25826 Jun19
6303 Jun19

...we then pick one of these, and see that it is waiting to receive some data on a network socket...

-bash-4.1$ strace -p 31215
Process 31215 attached - interrupt to quit
recvfrom(373, ^C 
Process 31215 detached

...so we get the network socket associated with this file descriptor...

-bash-4.1$ ls -lrt /proc/29527/fd | awk '$(NF - 2) == 373 {print $NF}'
socket:[833215397]

...and see the (masked) IP address to which we are connecting...

-bash-4.1$ netstat -eanp | grep 833215397
tcp        0      0 12.8.6.1:47977           4.28.7.10:2222          ESTABLISHED 11006      833215397  29527/java
-bash-4.1$

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.