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

发布于 2024-11-25 08:39:57 字数 297 浏览 1 评论 0原文

我正在编写一个 C++ 异常类,它必须在抛出站点提供有限的回溯。由于我的应用程序将是多线程的,因此可能会同时引发异常。我在网上搜索了这个线程安全问题,但没有找到。

backtrace() 返回 C 字符串数组。应用程序不得释放这些 C 字符串。由于它获取信息并在运行时合成这些字符串,我担心它不是线程安全的。

dladdr() 返回一个 struct Dl_info,其中包含两个 C 字符串。也不能被应用程序释放。

哦,好吧,我想我应该阅读源代码。

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.

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

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

发布评论

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

评论(1

诗笺 2024-12-02 08:39:57

来自手册

回溯是线程中当前活动的函数调用的列表。检查程序回溯的常用方法是使用外部调试器,例如 gdb。然而,有时从程序内以编程方式获取回溯很有用,例如,出于日志记录或诊断的目的。

头文件 execinfo.h 声明了三个函数,用于获取和操作当前线程的回溯。

看起来他们正在使用线程本地存储

dladdr 返回属于已加载目标文件的不可修改字符串。这是线程安全的,因为它是只读的,并且该对象在 dlclose 之前可用。

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.

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