我正在寻找一种遍历内存并打印(或对其进行任何处理)的方法,但是如果未分配虚拟地址,则会出现段错误。我该如何尝试读取程序中的任意地址而不会崩溃?
这主要是用于调试,因此安全/已定义的行为不是高优先级。例如,我可能想转储指针指向的内存和一些周围的值以检查是否损坏。
出于兴趣,是否有任何理由不能使其安全,例如读的副作用?
实际的内存转储很简单,例如以下内容。这个问题实际上是关于忽略段错误的。
编辑:
我正在运行ubuntu 18.04
答案 0 :(得分:2)
sigaction
捕获诸如SIGBUS
和SIGSEGV
之类的信号siglongjmp
安全退出处理程序SA_NODEFER
以能够再次捕获信号基于以下答案:Catching Segmentation Violations and Getting on with Life
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <setjmp.h>
sigjmp_buf badAddressRead;
void badAddressReadHandler(int signo) {
siglongjmp(badAddressRead, signo);
}
void hexDump(const char *desc, void *addr, int len)
{
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char*)addr;
struct sigaction segvActionOld;
struct sigaction busActionOld;
struct sigaction readAction = {};
readAction.sa_handler = badAddressReadHandler;
sigemptyset(&readAction.sa_mask);
readAction.sa_flags = SA_NODEFER;
sigaction(SIGBUS, &readAction, &segvActionOld);
sigaction(SIGSEGV, &readAction, &busActionOld);
// Output description if given.
if (desc != NULL)
printf("%s:\n", desc);
printf("address = %p\n", addr);
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf(" %s\n", buff);
// Output the offset.
printf(" %04x ", i);
}
// Attempt to read memory that may not be accessible
unsigned char byte;
int caughtSignal;
if ((caughtSignal = sigsetjmp(badAddressRead, 0)) == 0) {
// Now the hex code for the specific character.
byte = pc[i];
printf(" %02x", byte);
} else {
byte = 0;
if (caughtSignal == SIGSEGV) {
printf(" SV");
} else if (caughtSignal == SIGBUS) {
printf(" BS");
} else {
printf(" ??");
}
}
// And store a printable ASCII character for later.
if ((byte < 0x20) || (byte > 0x7e)) {
buff[i % 16] = '.';
} else {
buff[i % 16] = pc[i];
}
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf(" ");
i++;
}
// And print the final ASCII bit.
printf(" %s\n", buff);
sigaction(SIGBUS, &busActionOld, NULL);
sigaction(SIGSEGV, &segvActionOld, NULL);
}
int main(void) {
int test = 123;
hexDump("test", &test, 8000); // dump 'test' and a whole bunch more after it
return 0;
}
(已从https://gist.github.com/domnikl/af00cc154e3da1c5d965修改的十六进制转储代码)