开发者

Are GNU backtrace_symbols() and dladdr() thread-safe?

开发者 https://www.devze.com 2023-03-22 08:48 出处:网络
I am writing a C++ exception class, which has to provide limited backtrace at throw site. Since my application will be multi-threaded, exceptions might be thrown at the same time. I searc开发者_JAVA技

I am writing a C++ exception class, which has to provide limited backtrace at throw site. Since my application will be multi-threaded, exceptions might be thrown at the same time. I searc开发者_JAVA技巧hed the Internet for this thread-safety issue, but found none.

backtrace() returns array of C strings. These C strings must not be freed by the application. Since it gets its information and composites these strings at runtime, I fear that it is not thread safe.

dladdr() returns a struct Dl_info, with two C strings in it. Also must not be freed by the application.

Oh well, I guess I should just read the source code.


From the manual

A backtrace is a list of the function calls that are currently active in a thread. The usual way to inspect a backtrace of a program is to use an external debugger such as gdb. However, sometimes it is useful to obtain a backtrace programmatically from within a program, e.g., for the purposes of logging or diagnostics.

The header file execinfo.h declares three functions that obtain and manipulate backtraces of the current thread.

Looks like they're using thread-local storage.

dladdr is returning non-modifiable strings which belong to the loaded object file. This is thread-safe because it's read-only and the object is available until dlclose.

0

精彩评论

暂无评论...
验证码 换一张
取 消