如何分析代码的哪一部分创建线程?

时间:2018-10-04 09:16:58

标签: c++ debugging pthreads analysis

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

一种有效的查找方法是,代码的哪一部分在运行时创建特定线程?

谢谢!

1 个答案:

答案 0 :(得分:0)

在评论中,您提到了“跟踪器”的概念。考虑到这一点,您也许可以使用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