我正在寻找一种方法来遍历内存并打印它(或者对它做任何事情),但是如果虚拟地址没有被分配,我将得到一个segfault。如何尝试读取程序中的任意地址而不崩溃?
这主要是为了调试,因此安全/定义的行为不是一个高优先级。例如,我可能希望转储指针指向的内存和一些周围的值,以检查是否损坏。
出于兴趣,是否有任何原因不能保证安全,例如读取的副作用?
实际的内存转储很简单,例如下面的例子。这个问题实际上是关于忽略segfault。
how to get hexdump of a structure data
https://gist.github.com/domnikl/af00cc154e3da1c5d965
编辑:
我在运行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/

10-11 19:31