- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个具有实时约束的软件项目,主要用 C++ 编写,但使用了许多 C 库,在 POSIX 操作系统中运行。为了满足实时限制,我们已将几乎所有的文本移出 stderr
管道并进入共享内存环形缓冲区。
我们现在遇到的问题是,当旧代码或 C 库调用 assert
时,消息最终位于 stderr
而不是与其余日志一起存在于我们的环形缓冲区中。我们想找到一种方法来重定向 assert
的输出.
我考虑了三种基本方法:
1.) 创建我们自己的断言宏——基本上,不要使用 #include <cassert>
,给出我们自己的 assert
的定义。这可行,但是修补我们正在使用该调用 assert
的所有库将非常困难。包含不同的 header 。
2.) 修补 libc -- 修改 __assert_fail
的 libc 实现。这可行,但在实践中会非常尴尬,因为这意味着如果不构建日志基础设施,我们就无法构建 libc。我们可以这样做,以便在运行时,我们可以将函数指针传递给 libc
这就是“断言处理程序”——这是我们可以考虑的。问题是是否有比这更简单/更少侵入性的解决方案。
3.) 修补 libc header ,以便 __assert_fail
标有__attribute__((weak))
。这意味着我们可以在链接时使用自定义实现覆盖它,但如果我们的自定义实现未链接,那么我们将链接到常规 libc 实现。实际上我希望这个函数已经被标记为 __attribute__((weak))
我惊讶地发现事实并非如此。
我的主要问题是:选项(3)可能有哪些缺点——修补 libc 以使这一行:https://github.com/lattera/glibc/blob/master/assert/assert.h#L67
extern void __assert_fail (const char *__assertion, const char *__file,
unsigned int __line, const char *__function)
__THROW __attribute__ ((__noreturn__));
标有 __attribute__((weak))
还有?
libc
对于我们来说已经是一个共享库,我认为动态链接的成本应该淹没任何有关系统在加载时必须执行的弱解析与强解析的案例分析?glibc 中的一些函数,特别是 strtod
和malloc
,都标有特殊的 gcc 属性 __attribute__((weak))
。这是一个链接器指令——它告诉 gcc 这些符号应该被标记为“弱符号”,这意味着如果在链接时找到该符号的两个版本,则选择“强”版本而不是弱版本。
维基百科上描述了这样做的动机:
Weak symbols can be used as a mechanism to provide default implementations of functions that can be replaced by more specialized (e.g. optimized) ones at link-time. The default implementation is then declared as weak, and, on certain targets, object files with strongly declared symbols are added to the linker command line.
If a library defines a symbol as weak, a program that links that library is free to provide a strong one for, say, customization purposes.
Another use case for weak symbols is the maintenance of binary backward compatibility.
但是,在 glibc 和 musl libc 中,在我看来 __assert_fail
函数(assert.h
宏转发到的函数)未标记为弱符号。
https://github.com/lattera/glibc/blob/master/assert/assert.h
https://github.com/lattera/glibc/blob/master/assert/assert.c
https://github.com/cloudius-systems/musl/blob/master/include/assert.h
最佳答案
您不需要 glibc 中的符号 __assert_fail
上的 attribute((weak))
。只需在程序中编写您自己的 __assert_fail 实现,链接器应该使用您的实现,例如 example :
#include <stdio.h>
#include <assert.h>
void __assert_fail(const char * assertion, const char * file, unsigned int line, const char * function)
{
fprintf(stderr, "My custom message\n");
abort();
}
int main()
{
assert(0);
printf("Hello World");
return 0;
}
这是因为当链接器解析符号时,__assert_fail
符号已由您的程序定义,因此链接器不应选择 libc 定义的符号。
如果您确实需要将 __assert_fail 定义为 libc 内的弱符号,为什么不直接 objcopy --weaken-symbol=__assert_fail/lib/libc.so/lib/libc_with_weak_assert_fail.so
。我认为您不需要为此从源代码重建 libc。
关于c - 重定向断言失败消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915274/
我正在尝试使用谷歌浏览器的 Trace Event Profiling Tool分析我正在运行的 Node.js 应用程序。选择点样本后,我可以在三种 View 之间进行选择: 自上而下(树) 自上而
对于一个可能是菜鸟的问题,我们深表歉意,但尽管在 SO 上研究了大量教程和其他问题,但仍找不到答案。 我想做的很简单:显示一个包含大量数据库存储字符串的 Android ListView。我所说的“很
我已经开始了一个新元素的工作,并决定给 Foundation 5 一个 bash,看看它是什么样的。在创建带有水平字段的表单时,我在文档中注意到的第一件事是它们使用大量 div 来设置样式。所以我在下
我有一个 Windows 窗体用户控件,其中包含一个使用 BeginInvoke 委托(delegate)调用从单独线程更新的第 3 方图像显示控件。 在繁重的 CPU 负载下,UI 会锁定。当我附加
我有一堆严重依赖dom元素的JS代码。我目前使用的测试解决方案依赖于 Selenium ,但 AFAIK 无法正确评估 js 错误(addScript 错误不会导致您的测试失败,而 getEval 会
我正在制作一款基于滚动 2D map /图 block 的游戏。每个图 block (存储为图 block [21][11] - 每个 map 总共 231 个图 block )最多可以包含 21 个
考虑到以下情况,我是前端初学者: 某个 HTML 页面应该包含一个沉重的图像(例如 - 动画 gif),但我不想强制客户缓慢地等待它完全下载才能享受一个漂亮的页面,而是我更愿意给他看一个轻量级图像(例
我正在设计一个小软件,其中包括: 在互联网上获取资源, 一些用户交互(资源的快速编辑), 一些处理。 我想使用许多资源(它们都列在列表中)来这样做。每个都独立于其他。由于编辑部分很累,我想让用户(可能
我想比较两个理论场景。为了问题的目的,我简化了案例。但基本上它是您典型的生产者消费者场景。 (我关注的是消费者)。 我有一个很大的Queue dataQueue我必须将其传输给多个客户端。 那么让我们
我有一个二元分类问题,标签 0 和 1(少数)存在巨大不平衡。由于测试集带有标签 1 的行太少,因此我将训练测试设置为至少 70-30 或 60-40,因此仍然有重要的观察结果。由于我没有过多地衡量准
我是一名优秀的程序员,十分优秀!