c - 如何从任意虚拟地址转储内存,忽略 SIGSEGV

标签 c memory dump virtual-address-space page-fault

我正在寻找一种方法来遍历内存并打印它(或者用它做任何事情),但是如果虚拟地址没有被分配,我会得到一个段错误。我怎样才能尝试读取程序中的任意地址而不崩溃?

这主要用于调试,因此安全/定义的行为不是高优先级。例如,我可能想转储指针指向的内存和一些周围的值以检查损坏。

出于兴趣,是否有任何理由不能使其安全,例如读取的副作用?

实际的内存转储很简单,例如下面的。这个问题实际上是关于忽略段错误。

编辑:
我正在运行 ubuntu 18.04

最佳答案

  • 使用sigaction 捕捉SIGBUSSIGSEGV 等信号
  • 使用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 修改的 hexdump 代码)

关于c - 如何从任意虚拟地址转储内存,忽略 SIGSEGV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452872/

相关文章:

C - 如何将结构保存到内存的 malloc 部分?

java - 等到 DOS 命令执行完成 - Java

Java 8 : Better to extend class of static methods or better to call static methods directly?

java - Java中的序列化对象大小与内存对象大小

neo4j - 加载 Neo4j 数据库转储 (neo4j-shell)

mysql - 将 SQL 插入从 PHPmyAdmin 拆分到 Adminer

c - UDP源端口什么时候设置?

c - 在 C 中使用 argv 传递变量

c - NxN 棋盘上的 N 个皇后

java - 在批处理模式下运行 matlab 时如何增加 java 堆内存大小