Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 772
kumar Avatar asked May 18 '11 11:05

kumar


2 Answers

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.

like image 146
Alex C Avatar answered Oct 30 '22 20:10

Alex C


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.

like image 41
MarkR Avatar answered Oct 30 '22 20:10

MarkR