C++11:pthreads、静态类函数和-pg标志的兼容性



我偶然发现了一个奇怪的错误,涉及C++11、pthreads和-pg标志。当C++库例程行mcount.C文件调用我的任何类中的静态函数时,我的线程似乎被卡住了。

Sleeping
Awakened
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7bc6148 in pthread_join (threadid=140737333020416, thread_return=0x7fffffffe4f8)
at pthread_join.c:89
89  pthread_join.c: No such file or directory.
(gdb) info threads
Id   Target Id         Frame 
17   Thread 0x7fffef3cd700 (LWP 6152) "test.o" __mcount_internal (frompc=4198422, selfpc=4206354)
at mcount.c:72
16   Thread 0x7fffefbce700 (LWP 6151) "test.o" __mcount_internal (frompc=4211225, selfpc=4212043)
at mcount.c:72
15   Thread 0x7ffff03cf700 (LWP 6150) "test.o" __mcount_internal (frompc=4211225, selfpc=4212043)
at mcount.c:72
......
at mcount.c:72
3    Thread 0x7ffff63db700 (LWP 6138) "test.o" __mcount_internal (frompc=4206451, selfpc=4211201)
at mcount.c:72
2    Thread 0x7ffff6bdc700 (LWP 6136) "test.o" __mcount_internal (frompc=4206732, selfpc=4211201)
at mcount.c:72
* 1    Thread 0x7ffff7fd6740 (LWP 6135) "test.o" 0x00007ffff7bc6148 in pthread_join (
threadid=140737333020416, thread_return=0x7fffffffe4f8) at pthread_join.c:89
(gdb) thread 17
[Switching to thread 17 (Thread 0x7fffef3cd700 (LWP 6152))]
#0  __mcount_internal (frompc=4198422, selfpc=4206354) at mcount.c:72
72  mcount.c: No such file or directory.
(gdb) bt
#0  __mcount_internal (frompc=4198422, selfpc=4206354) at mcount.c:72
#1  0x00007ffff71d0b94 in mcount () at ../sysdeps/x86_64/_mcount.S:48
#2  0x00007ffff7ff7030 in ?? ()
#3  0x000000000000001a in ?? ()
#4  0x0000000008800191 in ?? ()
#5  0x000000000000001a in ?? ()
#6  0x00007ffff7ff7030 in ?? ()
#7  0x0000000000000005 in ?? ()
#8  0x0000000000000040 in ?? ()
#9  0x0000000000402f12 in Helper::remove (vec=0x8800191, pos=0, p=0x5) at Helpers.hpp:100

线程应该在主线程打印"Awakend"后全部退出,但它们没有,当我中断程序时,它们都在mcount.c文件中。它似乎是在我调用Helper::remove和初始化Helper:中的函数变量之间调用的。

指示

#9  0x0000000000402f12 in Helper::remove (vec=0x8800191, pos=0, p=0x5) at Helpers.hpp:100

它应该保存值(vec=0x7ffff7ff7030,pos=26,p=0x8800191),最后一个变量让我怀疑我是不是有点重写堆栈。(这些值是从堆栈帧#10中检索到的)。

Helpers.hpp中的第100行只是函数声明:

static bool remove(WFVector *vec, int pos, void *p){

有人能解释一下为什么包含-pg标志会导致线程陷入静态函数中吗?

代码编译:g++-4.7 -DDEBUG=1 -g -pg -std=c++0x -mcx16 -m64 tester.cpp -o test.o -I /usr/include/boost -lpthread并用GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04进行测试

众所周知,gprof不支持多线程应用程序。查看此解决方法是否解决了问题。无论如何,大多数人只是使用另一种分析工具。

我个人更喜欢Linux内置的perf。搜索"gprof线程"会从SO中得到很多结果,并为评测工具提供各种建议。

删除-pg标志可以修复错误。我花了很长的时间才弄明白,我弄明白只是运气不好。所以我发布这个bug,以防其他人遇到这个问题。

最新更新