简体   繁体   English

GNU backtrace_symbols() 和 dladdr() 线程安全吗?

[英]Are GNU backtrace_symbols() and dladdr() thread-safe?

I am writing a C++ exception class, which has to provide limited backtrace at throw site.我正在写一个 C++ 异常 class,它必须在抛出站点提供有限的回溯。 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.我在 Internet 上搜索了这个线程安全问题,但没有找到。

backtrace() returns array of C strings. backtrace()返回 C 字符串数组。 These C strings must not be freed by the application.应用程序不得释放这些 C 字符串。 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. dladdr()返回一个struct Dl_info ,其中包含两个 C 字符串。 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 .回溯是当前在线程中活动的 function 调用的列表。 The usual way to inspect a backtrace of a program is to use an external debugger such as gdb.检查程序回溯的常用方法是使用外部调试器,例如 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 . header 文件 execinfo.h 声明了三个函数,用于获取和操作当前线程的回溯。

Looks like they're using thread-local storage .看起来他们正在使用thread-local storage

dladdr is returning non-modifiable strings which belong to the loaded object file. dladdr正在返回属于加载的 object 文件的不可修改的字符串。 This is thread-safe because it's read-only and the object is available until dlclose .这是线程安全的,因为它是只读的,并且 object 在dlclose之前可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM