# 关于运行中的进程运行状态分析

## 一、Linux篇

### 方法1:gcore

测试源码:

```c++
#include <iostream>
#include <thread>

static int g_Mark = 1;

void demo()
{
	int i = 60;
	while (--i) {
		std::this_thread::sleep_for(std::chrono::seconds(1));
	}
	g_Mark = 0;
}

int main()
{
	std::thread t(demo);

	t.join();
	return 0;
}
```

运行起来,然后查找进程`ID`:

```shell
yun@ubuntu:~/Documents$ ps -ef | grep a.out
yun         2259    1990  0 04:44 pts/0    00:00:00 ./a.out
yun         2262    2243  0 04:45 pts/2    00:00:00 grep --color=auto a.out
```

查找到进程的进程`ID`,使用:`sudo gcore -o core.dump 2259`

使用`gdb a.out core.dump.2259`启动调试,以下是基本的指令:

| 指令                | 功能                         |
| ------------------- | ---------------------------- |
| bt                  | 打印当前线程的调用栈         |
| thread apply all bt | 打印所有线程的调用栈         |
| thread <ID>         | 切换到指定线程(可以后续bt) |