Linux 环境用C++获取TEB线程块信息

请教一下大家有过在Linux环境下获取TEB线程块的信息吗?我使用AT&T语法一直获取不成功,大家知道使用TLS库可以获取到吗?

我的主要目的是读取TEB线程块中的debug标志位来判断程序是否正在被调试。

望采纳,谢谢

// 方法1
#include <sys/syscall.h>
syscall(__NR_gettid)

// 方法2
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)

// 方法3
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)

 

#include <iostream>
#include <unistd.h>
#include <sstream>

using namespace std;

template<typename T> inline string toString(const T& var) {
	stringstream ss;
	ss << var;
	return ss.str();
}

//KiB
int getMem(pid_t pid) {
	string file_name = "/proc/" + toString(pid) + "/status";
	FILE *fd = fopen(file_name.c_str(), "r");
	char key_value[1024];
	string key;
	int value = -1;
	stringstream ss;
	while(fgets(key_value, sizeof(key_value), fd)) {
		ss.str("");
		ss.clear();
		ss << key_value;
		ss >> key >> value;
		if(key.compare("RssAnon:") == 0)
			break;
	}
	fclose(fd);
	return value;
}

int main() {
	cout << "pid:" << getpid() << " mem:" << getMem(getpid()) << "KiB" << endl;
}