gpt4 book ai didi

c++ - 如何分析哪部分代码创建了线程?

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:58 24 4
gpt4 key购买 nike

我必须在线程创建方面分析一个非常大的 C++ 项目。这不是我的项目,线程没有标记。线程的数量和选择因输入而异。

找出哪部分代码在运行时创建特定线程的有效方法是什么?

提前致谢!

最佳答案

在评论中,您提到了“示踪剂”的概念。考虑到这一点,您也许可以使用 LD_PRELOAD .

考虑以下代码...

#include <iostream>
#include <dlfcn.h>
#include <pthread.h>
#include <boost/stacktrace.hpp>

typedef int (*real_pthread_create_t)(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

static real_pthread_create_t real_pthread_create = NULL;
static unsigned thread_count = 0;

extern "C"
int
pthread_create (pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
{

/*
* If real_pthread_create hasn't been fixed up yet then do it now.
*/
if (!real_pthread_create)
real_pthread_create = (real_pthread_create_t)dlsym(RTLD_NEXT, "pthread_create");

/*
* Increment the thread count.
*/
++thread_count;

/*
* Dump some stats and the call stack to cerr.
*/
std::cerr << "\ncreated thread #" << thread_count
<< "\nStack trace...\n"
<< boost::stacktrace::stacktrace();

/*
* Delegate to the real pthread_create and simply forward its return value.
*/
return real_pthread_create(thread, attr, start_routine, arg);
}

将上面的代码编译成一个共享对象,类似...

g++ -shared -fPIC -o pthread_catcher.so pthread_catcher.cpp

然后将其用作...

LD_PRELOAD=pthread_catcher.so ./path-to-main-application

对一些自制代码的快速测试显示输出如...

created thread #7
Stack trace...
0# pthread_create in build/objs/how-to-analyze-which-part-of-code-creates-a-thread.so
1# 0x00007F311A7EF91C in /usr/lib64/dri/nouveau_dri.so
2# 0x00007F311A7EFD75 in /usr/lib64/dri/nouveau_dri.so
3# 0x00007F311A7E7E22 in /usr/lib64/dri/nouveau_dri.so
4# 0x00007F311AA08FF4 in /usr/lib64/dri/nouveau_dri.so
5# 0x00007F311AA09379 in /usr/lib64/dri/nouveau_dri.so
6# 0x00007F311AA9BC73 in /usr/lib64/dri/nouveau_dri.so
7# nouveau_drm_screen_create in /usr/lib64/dri/nouveau_dri.so
8# 0x00007F311A52FBC6 in /usr/lib64/dri/nouveau_dri.so
9# 0x00007F311AA00DB8 in /usr/lib64/dri/nouveau_dri.so
10# 0x00007F311A8D6042 in /usr/lib64/dri/nouveau_dri.so
11# 0x00007F311A8D144E in /usr/lib64/dri/nouveau_dri.so
12# 0x00007F311B6688FE in /usr/lib64/libGLX_mesa.so.0
13# 0x00007F311B640BA3 in /usr/lib64/libGLX_mesa.so.0
14# 0x00007F311B63C284 in /usr/lib64/libGLX_mesa.so.0
15# 0x00007F311B63CC9D in /usr/lib64/libGLX_mesa.so.0
16# 0x00007F3121BEBA50 in /usr/lib64/qt5/plugins/xcbglintegrations/libqxcb-glx-integration.so
17# QXcbWindow::create() in /usr/lib64/libQt5XcbQpa.so.5
18# QXcbIntegration::createPlatformWindow(QWindow*) const in /usr/lib64/libQt5XcbQpa.so.5
19# QWindowPrivate::create(bool, unsigned long long) in /usr/lib64/libQt5Gui.so.5
20# QWidgetPrivate::create_sys(unsigned long long, bool, bool) in /usr/lib64/libQt5Widgets.so.5
21# QWidget::create(unsigned long long, bool, bool) in /usr/lib64/libQt5Widgets.so.5
22# QWidget::setVisible(bool) in /usr/lib64/libQt5Widgets.so.5
23# main in ./build/applications/vnet/test00005
24# __libc_start_main in /lib64/libc.so.6
25# _start in ./build/applications/vnet/test00005

关于c++ - 如何分析哪部分代码创建了线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52643186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com