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 searched 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, eg, 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.