Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand "/proc/[pid]/stack"?

According to proc manual:

/proc/[pid]/stack (since Linux 2.6.29)

This file provides a symbolic trace of the function calls in this process's kernel stack. This file is provided only if the kernel was built with the CONFIG_STACKTRACE configuration option.

So I write a program to test:

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>

void *thread_func(void *p_arg)
{
        pid_t pid = fork();
        if (pid > 0) {
            wait(NULL);
            return 0;
        } else if (pid == 0) {
            sleep(1000);
            return 0;
        }
        return NULL;
}
int main(void)
{
        pthread_t t1, t2;

        pthread_create(&t1, NULL, thread_func, "Thread 1");
        pthread_create(&t2, NULL, thread_func, "Thread 2");

        sleep(1000);
        return 0;
}

After running, use pstack to check the threads of progress:

linux-uibj:~ # pstack 24976
Thread 3 (Thread 0x7fd6e4ed5700 (LWP 24977)):
#0  0x00007fd6e528d3f4 in wait () from /lib64/libpthread.so.0
#1  0x0000000000400744 in thread_func ()
#2  0x00007fd6e52860a4 in start_thread () from /lib64/libpthread.so.0
#3  0x00007fd6e4fbb7fd in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7fd6e46d4700 (LWP 24978)):
#0  0x00007fd6e528d3f4 in wait () from /lib64/libpthread.so.0
#1  0x0000000000400744 in thread_func ()
#2  0x00007fd6e52860a4 in start_thread () from /lib64/libpthread.so.0
#3  0x00007fd6e4fbb7fd in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7fd6e569f700 (LWP 24976)):
#0  0x00007fd6e4f8d6cd in nanosleep () from /lib64/libc.so.6
#1  0x00007fd6e4f8d564 in sleep () from /lib64/libc.so.6
#2  0x00000000004007b1 in main ()

At the same time, check /proc/24976/stack:

linux-uibj:~ # cat /proc/24976/stack
[<ffffffff804ba1a7>] system_call_fastpath+0x16/0x1b
[<00007fd6e4f8d6cd>] 0x7fd6e4f8d6cd
[<ffffffffffffffff>] 0xffffffffffffffff

The 24976 process has 3 threads, and they all block on system call(nanosleep and wait), so all 3 threads now work in kernel space, and turn into kernel threads now, right? If this is true, there should be 3 stacks in /proc/[pid]/stack file. But it seems there is only 1 stack in /proc/[pid]/stack file.

How should I understand /proc/[pid]/stack?

like image 843
Nan Xiao Avatar asked Oct 30 '15 05:10

Nan Xiao


People also ask

How do you read proc PID stack?

It corresponds to the /proc/[pid]/task/[tid]/path. Which seems to be what you are looking for. After your hints, I get the answer: on Linux, thread is actually a process, so /proc/[tid]/stack will get the thread's kernel stack info, or use /proc/[pid]/task/[tid]/stack .

How do I know if a process is stuck Linux?

strace -p $process_id - if there is not continuous output, that means it is waiting for something. If it waits too long, you could consider that "hung". you could also use the timeout command to set a limit on a command. most people know if x amount of time goes by, it is too much.

What is proc PID MEM?

/proc/PID/mem gives access to the memory of the process as it is mapped in the process: accessing the Nth byte of this file is equivalent to accessing (*unsigned char)N inside the process in C, or ptrace(PTRACE_PEEKDATA, PID, (void*)N, NULL) from another process (or PTRACE_POKEDATA for writing, and with the difference ...


2 Answers

How should I understand /proc/[pid]/stack ?

Taken from the man pages for proc:

There are additional helpful pseudo-paths:

[stack] The initial process's (also known as the main thread's) stack.

Just below this, you can find:

[stack:[tid]] (since Linux 3.4)

A thread's stack (where the [tid] is a thread ID). It corresponds to the /proc/[pid]/task/[tid]/path.

Which seems to be what you are looking for.

like image 97
Joe Watkins Avatar answered Sep 28 '22 09:09

Joe Watkins


Nan Xiao is right.
Thread kernel mode stack is under /proc/[PID]/task/[TID]/stack.

you are checking /proc/[PID]/stack, that's the main thread stack so you have only 1. Others are under task folder.

like image 28
hguo Avatar answered Sep 28 '22 10:09

hguo