Mac OS X 端口在 glibstdc 中的 pthread_setspecific 中崩溃++ vsnprintf - 如何排除故障?

发布于 2024-08-10 12:52:53 字数 3150 浏览 1 评论 0原文

我正在测试多线程服务器的 Mac OS X 端口。它启动了,但在第一个客户端请求被工作线程处理后不久,它就在 vsnprintf 中终止。

看起来 vsnprintf 正在尝试使用 pthread_setspecific 操作一些线程本地内存。这取消了对错误指针的引用。 然后,gdb 捕获 dlopen 调用,收到错误,并在尝试格式化其自己的错误消息时终止。 因为,要格式化错误,需要设置一些线程本地内存!

在此之前,我自己的代码成功地使用了 pthread_create_key、pthread_getspecic 和 pthread_setspecic。我仔细记录了自己的访问,我认为它们没有破坏任何东西。

是否有可能glibstdc++中的某些static没有按时初始化?我怎么知道?

另外,我使用 g++ -pthread 进行编译和链接,但我在可执行清单中没有看到 libpthread。

    otool -L myExecutable

libboost_thread-xgcc40-mt-1_39.dylib (compatibility version 0.0.0, current version 0.0.0)
/Users/eolson//lib/libgsl.0.dylib (compatibility version 14.0.0, current version 14.0.0)
/Users/eolson//lib/libgslcblas.0.dylib (compatibility version 1.0.0, current version 1.0.0)
/Users/eolson/mico-2.3.12/lib/libmico2.3.12.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/local/lib/libModelsCorba.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/local/lib/libModelsBigLibrary.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0)
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)

有谁知道如何进一步调试这个?

堆栈跟踪:

[Switching to process 37784]
Program received signal:  “EXC_BAD_ACCESS”.
[Switching to process 37784]
sharedlibrary apply-load-rules all
Data Formatters temporarily unavailable, will re-try after a 'continue'. (The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on"
Evaluation of the expression containing the function (dlopen) will be abandoned.)
(gdb) where

#0  0x9232f03b in pthread_setspecific ()
#1  0x9232efe6 in getPerThreadBufferFor_dlerror ()
#2  0x8fe0b0cd in __dyld_dlopen ()
#3  0x9232ef48 in dlopen ()
#4  <function called from gdb>
#5  0x9232f03b in pthread_setspecific ()
#6  0x9233ed64 in __Balloc_D2A ()
#7  0x9233eb92 in __d2b_D2A ()
#8  0x9233dc5e in __dtoa ()
#9  0x92335975 in __vfprintf ()
#10 0x92355886 in vsnprintf ()
#11 0x96eb526b in std::__convert_from_v ()
#12 0x96eaeb5e in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double> ()
#13 0x96eaedb4 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put ()
#14 0x96ea9583 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::put ()
#15 0x96eb79dd in std::ostream::_M_insert<double> ()
#16 0x012db6a8 in MyCode ...

触发崩溃的代码:

std::ostringstream buf;
buf << myObjectWithOutputOperator << endl;
double x = 1;
buf << "x: " << x << endl; // crashes during __vfprintf

编辑:我相信这与 XCode 3.2 DEBUG 配置的 ostringstream 中的错误有关。请参阅c++ 中 int 的 ostringstream 问题

I'm testing a Mac OS X port of my multithreaded server. It starts up, but it dies in vsnprintf soon after the first client request is taken by a worker thread.

It seems that vsnprintf is trying to manipulate some thread local memory with pthread_setspecific. This dereferences a bad pointer.
Then, gdb traps a dlopen call, gets an error, and dies trying to format its own error message.
Because, to format the error, it needs to set up some thread local memory!

Prior to this, my own code successfully used pthread_create_key, pthread_getspecific, and pthread_setspecific. I logged my own accesses carefully and I don't think they are corrupting anything.

Is it possible that some static in glibstdc++ has not been initialized on time? How can I tell?

Also, I used the g++ -pthread for compiling and linking, but I don't see a libpthread in my executable manifest.

    otool -L myExecutable

libboost_thread-xgcc40-mt-1_39.dylib (compatibility version 0.0.0, current version 0.0.0)
/Users/eolson//lib/libgsl.0.dylib (compatibility version 14.0.0, current version 14.0.0)
/Users/eolson//lib/libgslcblas.0.dylib (compatibility version 1.0.0, current version 1.0.0)
/Users/eolson/mico-2.3.12/lib/libmico2.3.12.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/local/lib/libModelsCorba.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/local/lib/libModelsBigLibrary.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0)
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)

Does anyone have an idea how to debug this further?

Stack trace:

[Switching to process 37784]
Program received signal:  “EXC_BAD_ACCESS”.
[Switching to process 37784]
sharedlibrary apply-load-rules all
Data Formatters temporarily unavailable, will re-try after a 'continue'. (The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on"
Evaluation of the expression containing the function (dlopen) will be abandoned.)
(gdb) where

#0  0x9232f03b in pthread_setspecific ()
#1  0x9232efe6 in getPerThreadBufferFor_dlerror ()
#2  0x8fe0b0cd in __dyld_dlopen ()
#3  0x9232ef48 in dlopen ()
#4  <function called from gdb>
#5  0x9232f03b in pthread_setspecific ()
#6  0x9233ed64 in __Balloc_D2A ()
#7  0x9233eb92 in __d2b_D2A ()
#8  0x9233dc5e in __dtoa ()
#9  0x92335975 in __vfprintf ()
#10 0x92355886 in vsnprintf ()
#11 0x96eb526b in std::__convert_from_v ()
#12 0x96eaeb5e in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double> ()
#13 0x96eaedb4 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put ()
#14 0x96ea9583 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::put ()
#15 0x96eb79dd in std::ostream::_M_insert<double> ()
#16 0x012db6a8 in MyCode ...

Code that triggered the crash:

std::ostringstream buf;
buf << myObjectWithOutputOperator << endl;
double x = 1;
buf << "x: " << x << endl; // crashes during __vfprintf

EDIT: I believe this is related to the bug in ostringstream with XCode 3.2 DEBUG configuration. See ostringstream problem with int in c++

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-08-17 12:52:53

Mac OS X 不使用单独的libpthread。所有 pthread 函数都在 libSystem 中:

$ nm -g /usr/lib/libSystem.dylib | grep ' _pthread_' | wc -l
     113

按照编辑中链接的问题的建议,在没有定义 _GLICXX_DEBUG=1 的情况下进行编译是否可以解决问题?

Mac OS X doesn't use a separate libpthread. All the pthread functions are in libSystem:

$ nm -g /usr/lib/libSystem.dylib | grep ' _pthread_' | wc -l
     113

Does compiling without _GLICXX_DEBUG=1 defined fix the problem, as suggested by the question linked in your edit?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文