栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

linux下抓取C++ throw调用栈

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux下抓取C++ throw调用栈

使用API钩子技术,抓取throw调用的调用堆栈。

1. hook_cxa_throw-lys.hpp

#pragma once

#include 
#include 
#include 
#include 
#include 
#include 
#include 


#ifdef __GNUC__
typedef void __attribute__((__noreturn__)) (*cxa_throw_type)(void*, void*, void(*)(void*));
#elif defined(__clang__)
typedef void __attribute__((__noreturn__)) (*cxa_throw_type)(void*, std::type_info*, void (_GLIBCXX_CDTOR_CALLABI*) (void*));
#endif
cxa_throw_type orig_cxa_throw = nullptr;

void load_orig_throw_code()
{
    orig_cxa_throw = (cxa_throw_type) dlsym(RTLD_NEXT, "__cxa_throw");
}

extern "C"
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

#ifdef __clang__
void __cxa_throw(void* thrown_exception, std::type_info* typeifo, void (_GLIBCXX_CDTOR_CALLABI* dest)(void*))
#elif defined(__GNUC__)
void __cxa_throw(void* thrown_exception, void* pvtinfo, void (*dest)(void *))
#endif
{
    if (orig_cxa_throw == nullptr)
        load_orig_throw_code();

	int nptrs;
	void *buffer[32];
	char **strings;

	nptrs = backtrace(buffer, 32);
	

	strings = backtrace_symbols(buffer, nptrs);

	if (strings != NULL)
	{
		for (int j = 0; j < nptrs; j++)
		{
			size_t p = 0;
			while(strings[j][p] != '(' && strings[j][p] != ' ' && strings[j][p] != 0)
			++p;

			char syscom[256];

			
			sprintf(syscom,"addr2line %p -e %.*s", buffer[j], p, strings[j]);

			//last parameter is the file name of the symbol
			system(syscom);
			printf("%sn", strings[j]);
		}

		free(strings);
	}
#ifdef __clang__
    orig_cxa_throw(thrown_exception, typeifo, dest);
#elif defined(__GNUC__)
    orig_cxa_throw(thrown_exception, pvtinfo, dest);
#endif
}

2. main.cpp

#include 
#include 
#include "hook_cxa_throw-lys.hpp"

int mytest()
{
	try
	{
		throw "mythrow char strings";
	}
	catch(const char* sz)
	{
		std::cout << sz << "n";
	}

	return 0;
}


int main(int argc, char* argv[])
{
	printf("hello, worldn");
	mytest();
	return 0;
}

3. 编译

g++ -g -std=c++0x -pthread -ldl -rdynamic main.cpp -o testThrow.out

4. 运行: 

./testThrow.out

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号