- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在开展一个项目,该项目运行在经过大量修改的 Linux 版本上,该版本已打补丁以能够访问 VMEbus。大部分总线处理已完成,我有一个 VMEAccess 类,它使用 mmap 写入/dev/mem 的特定地址,以便驱动程序可以提取该数据并将其推送到总线上。
当程序启动时,它不知道它正在寻找的从属板在总线上的位置所以它必须通过四处寻找来找到它:它试图一个一个地读取每个地址,如果一个设备连接在那里read 方法返回一些数据,但如果没有任何连接,则 SIGBUS 信号将发送到程序。
我尝试了几种解决方案(主要是使用信号处理),但过了一段时间,我决定使用跳转。第一个 longjmp() 调用工作正常,但对 VMEAccess::readWord() 的第二次调用给我一个总线错误,即使我的处理程序应该防止程序崩溃。
这是我的代码:
#include <iostream>
#include <string>
#include <sstream>
#include <csignal>
#include <cstdlib>
#include <csignal>
#include <csetjmp>
#include "types.h"
#include "VME_access.h"
VMEAccess *busVME;
int main(int argc, char const *argv[]);
void catch_sigbus (int sig);
void exit_function(int sig);
volatile BOOL bus_error;
volatile UDWORD offset;
jmp_buf env;
int main(int argc, char const *argv[])
{
sigemptyset(&sigBusHandler.sa_mask);
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = exit_function;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
/* */
struct sigaction sigBusHandler;
sigBusHandler.sa_handler = catch_sigbus;
sigemptyset(&sigBusHandler.sa_mask);
sigBusHandler.sa_flags = 0;
sigaction(SIGBUS, &sigBusHandler, NULL);
busVME = new VMEAccess(VME_SHORT);
offset = 0x01FE;
setjmp(env);
printf("%d\n", sigismember(&sigBusHandler.sa_mask, SIGBUS));
busVME->readWord(offset);
sleep(1);
printf("%#08x\n", offset+0xC1000000);
return 0;
}
void catch_sigbus (int sig)
{
offset++;
printf("%#08x\n", offset);
longjmp(env, 1);
}
void exit_function(int sig)
{
delete busVME;
exit(0);
}
最佳答案
如评论中所述,在信号处理程序中使用 longjmp
是个坏主意。跳出信号处理程序后,您的程序实际上仍在信号处理程序中。因此,调用非异步信号安全函数会导致未定义的行为。使用 siglongjmp
在这里并没有什么帮助,引用 man signal-safety
:
If a signal handler interrupts the execution of an unsafe function, and the handler terminates via a call to longjmp(3) or siglongjmp(3) and the program subsequently calls an unsafe function, then the behavior of the program is undefined.
例如,这个 (siglongjmp
) 在过去确实在 libcurl 代码中引起了一些问题,请参见此处:error: longjmp causes uninitialized stack frame
我建议改用常规循环并修改信号处理程序中的退出条件(无论如何都要修改那里的偏移量)。类似于以下内容(伪代码):
int had_sigbus = 0;
int main(int argc, char const *argv[])
{
...
for (offset = 0x01FE; offset is sane; ++offset) {
had_sigbus = 0;
probe(offset);
if (!had_sigbus) {
// found
break;
}
}
...
}
void catch_sigbus(int)
{
had_sigbus = 1;
}
这样一来,循环就很明显了,整个逻辑也更容易理解。并且没有跳转,所以它应该适用于多个探测器 :) 但显然 probe()
也必须在内部处理失败的调用(被 SIGBUS
中断的调用) - 并且可能返回一个错误。如果确实返回错误,则可能根本不需要使用 had_sigbus
函数。
关于c++ - 如何正确处理 SIGBUS 以便我可以继续搜索地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47866913/
我必须在 android 项目中使用 opencv。一切正常,直到我最近不得不使用 c++ exception_ptr。此后,使用std::rethrow_exception导致SIGBUS(信号SI
我的进程在同一台机器上的 2 个账户中运行良好,但是当我将进程复制到其他账户并运行进程时,我得到了核心转储。当我最后用 strace 运行这个过程时,我得到: --- SIGBUS (Bus erro
这个问题纯粹出于好奇;就我个人而言,我曾看到此信号被发出,但很少见。 我在 the C chatroom 上问过是否有可靠的方法来重现它。在这个房间里,user @Antti Haapala找到一个。
这是我写的示例代码。 #include #include #include #include #include int main() { int fd; long pages
我尝试使用 iText 5.5.0 和文档示例进行签名: private static void pdfSign(String srcFile, String dstFile) throws Exce
我的应用在 iPhone 4 iOS 7.1.3 上崩溃。在 iOS 9 和 iP6 上一切正常。 这是由使用两种泛型类型实例化相当复杂的泛型类引起的。 我可以提供有关实现的详细信息,但该类有数百行。
我的程序最近因以下堆栈而崩溃; Program terminated with signal 7, Bus error. #0 0x00007f0f323beb55 in raise () from
我的调用堆栈显示如下: --- called from signal handler with signal 10 (SIGBUS) --- 001301b8 allocate__t24__def
我的 Android 应用程序在以下代码段的第 4 行报告 SIGBUS 错误(这是一个函数序言): MOV R12, SP STMFD SP!, {R4-R12,LR,PC} SUB R11, R1
在 objective-c 中将自定义对象添加到 mutableArray 时出现内存 SIGBUS 错误。 @interface stak : NSObject { NSString *id
编辑 1:示例程序的平台是 x86_64。 编辑 2: 我正在编辑它以便更好地理解。下面是两个不同的问题。第一个问题是无效的读/写会导致 SIGBUS 吗?第二个问题是 Valgrind 对 SIGB
我目前正在开展一个项目,该项目运行在经过大量修改的 Linux 版本上,该版本已打补丁以能够访问 VMEbus。大部分总线处理已完成,我有一个 VMEAccess 类,它使用 mmap 写入/dev/
我试图在只读内存上捕获错误但无法捕获?如果我处理了错误,那么它的程序可以继续,或者只有选项是退出/中止? #include #include #include #include #include
我想为内存映射文件 io 编写一个包装器,它要么无法映射文件,要么返回一个在取消映射之前有效的映射。使用普通 mmap 时,如果底层文件在映射时被截断或删除,就会出现问题。根据 mmap 的 linu
那是在堆栈跟踪中,我不明白这个错误: 0 CoreFoundation 0x30f57648 ___CFBasicHashFindBucket_Linear_NoCollision + 92
我正在尝试通过映射一个 1 MiB 文件(与 RAM 大小相对应)来为基本 CPU 建模。我想读/写这个文件。目前,我收到 ram[2] = 1 的 SIGBUS 错误,我收集到这是由于尝试在文件范围
我的子进程正在尝试访问 PCI 地址空间。它在大多数时候都运行良好。 但是,有时子进程会进入僵尸状态。 dmesg 日志显示以下总线错误。 [ 501.134156] Caused by (from
我的一个应用程序因 SIGBUS 信号而崩溃。以下是来自 gdb 的堆栈跟踪。基本上,dlopen 调用失败了。我正在运行 CentOS 6.3 64 位。 Program received sign
以下从教程网站获取的基本 SDL2 代码引起了一些奇怪的问题: #include #include #include #include #define SCREENH 768 #define
我无法用以下汇编代码产生“总线错误”。这里我使用的内存地址不是合法的“规范地址”。那么,我该如何触发该错误? 我在带有 NASM 2.14.02 的 Ubuntu 20.04 LTS 下运行这段代码,
我是一名优秀的程序员,十分优秀!