How to view thread id of a process which has opened a socket connection?

一世执手 提交于 2020-01-21 10:57:13

问题


I have a process where multiple threads open multiple socket connection. I want to view this information and map what thread has opened which socket port. lsof -i and netstat command gives the process ID, but couldn't display thread id. Is there any command which prints this information?


回答1:


As MarkR suggested, you need to use strace from startup:

strace -fp <pid>

The above command will show you per thread system calls like open(), read(), recv() etc, along with the descriptors used:

[pid 428] close(36) 

Once you isolated the thread, you may attach to the process and find out the exact thread with

gdb attach <pid>

Or, if you have thread names set in your process, use

ps -eL

to find out the thread's friendly name.




回答2:


Unless you strace()'d the process, no.

Once the file descriptor is open, it "belongs" equally to all the threads in the process (as far as the kernel is concerned), you can't see which thread opened it.



来源:https://stackoverflow.com/questions/6044136/how-to-view-thread-id-of-a-process-which-has-opened-a-socket-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!