How to find out how many threads a Linux binary creates without a source?

Suppose I have a shared binary without a source, and I want to determine if it works sequentially or generates multiple threads.

Is there a way to do this from the linux command line?

+4
source share
3 answers

You can use ps for this. From man ps :

 -L Show threads, possibly with LWP and NLWP columns. 

So you can do:

 $ ps -L <pid> 

and he will show you something like this:

  PID LWP TTY STAT TIME COMMAND 4112 4112 ? Sl 65:35 /usr/lib/firefox/firefox 4112 4116 ? Sl 0:04 /usr/lib/firefox/firefox 

Each line of output corresponds to one stream. This, of course, only works for a specific point in time. Use strace to track thread spawning, as suggested by Jonathan Reinhart.

An alternative to strace is, of course, gdb . For more on thread management in gdb, see this question . You can also read the section of the gdb manual section . Brief introduction:

 $ gdb /usr/lib/firefox/firefox <pid> [... initialization output ...] > info threads # lists threads > thread <nr> # switch to thread <nr> 

Your comment:

How can I determine where to set the breakpoint at the instruction level if the program only takes seconds?

This answer may help you here, as it shows how to break up thread creation (using pthread_create ) using gdb. Therefore, every time a thread is created, execution stops and you can explore.

+4
source

Install strace first.

 $ yum install strace 

Run the program with strace and find the clone or fork system calls. Here is a short example with a program that I wrote that simply calls fork and returns.

 $ strace ./a.out execve("./a.out", ["./a.out"], [/* 43 vars */]) = 0 brk(0) = 0x74f000 ... clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fb22b16da10) = 6567 exit_group(1) = ? +++ exited with 1 +++ 
+3
source

Just run: cat /proc/<pid>/stat | awk '{print $20}' cat /proc/<pid>/stat | awk '{print $20}' to get the number of threads of the running process.

proc manpage

+1
source

Source: https://habr.com/ru/post/1481257/


All Articles