how-to-use/cpp/run_analyze.md

1.0 KiB

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

一、Linux篇

方法1:gcore

测试源码:

#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

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 切换到指定线程(可以后续bt)