关于linux:如何理解“/ proc / [pid] / stack”?

How to understand “/proc/[pid]/stack”?

根据proc手册:

/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.

所以我写了一个测试程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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;
}

运行后,使用pstack检查进度的线程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 ()

同时,检查/proc/24976/stack

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

24976进程有3个线程,它们都在系统调用(nanosleepwait)上阻塞,因此所有3线程现在都在kernel空间中工作,现在转为内核线程,对? 如果是这样,/proc/[pid]/stack文件中应该有3个堆栈。 但似乎/proc/[pid]/stack文件中只有1堆栈。

我应该如何理解/proc/[pid]/stack


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

摘自proc的手册页:

There are additional helpful pseudo-paths:

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

在这之下,您可以找到:

[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.

这似乎是你在寻找什么。


南萧是对的。
线程内核模式堆栈位于/ proc / [PID] / task / [TID] / stack下。

你正在检查/ proc / [PID] / stack,这是主线程堆栈所以你只有1.其他人在任务文件夹下。