简体   繁体   English

在Linux中没有ptrace的情况下在C中读取另一个进程的内存

[英]Reading memory of another process in C without ptrace in linux

I am trying to read memory of another process and print whatever is in the memory (Heap and/or stack).我正在尝试读取另一个进程的内存并打印内存中的任何内容(堆和/或堆栈)。 I have got the range of memory addresses using /proc I have extracted address range like this .我有内存地址的使用范围/proc我已经提取的地址范围内像这样 Now I want to read the memory range of the other process like as defined.现在我想像定义的那样读取其他进程的内存范围。

5569032d2000-5569032f3000 rw-p 00000000 00:00 0 [heap] 5569032d2000-5569032f3000 rw-p 00000000 00:00 0 [堆]

I am stuck on how to access those memory addresses.我被困在如何访问这些内存地址上。 I tried something like shown below , but doesn't help much.我尝试了如下所示的方法,但没有多大帮助。

int main(int argc, char *argv[]) {

off_t offset = strtoul(argv[1], NULL, 0);
size_t len = strtoul(argv[2], NULL, 0);

// Truncate offset to a multiple of the page size, or mmap will fail.
size_t pagesize = sysconf(_SC_PAGE_SIZE);
off_t page_base = (offset / pagesize) * pagesize;
off_t page_offset = offset - page_base;

int fd = open("/dev/mem", O_SYNC);
unsigned char *mem = mmap(NULL, page_offset + len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, page_base);
if (mem == MAP_FAILED) {
    perror("Can't map memory");
    return -1;
}

size_t i;
for (i = 0; i < len; ++i)
    printf("%x ", (int)mem[page_offset + i]);
//size_t i;
return 0;}

Thanks.谢谢。

I am making like a debug tool for my embedded system.我正在为我的嵌入式系统制作调试工具。 I can't use ptrace() as it halts the running process while trying to peek into the device memory.我无法使用ptrace()因为它在尝试查看设备内存时会停止正在运行的进程。

I figured out to read the process of another process, I can use process_vm_readv() function as follow:我想通读另一个进程的进程,我可以使用process_vm_readv()函数如下:


pid_t pid; // Put value of pid in this
void *remotePtr; // Put starting address 
size_t bufferLength; // Put size of buffer in this, aka size to read
// Build iovec structs
    struct iovec local[1];
    local[0].iov_base = calloc(bufferLength, sizeof(char));
    local[0].iov_len = bufferLength;

    struct iovec remote[1];
    remote[0].iov_base = remotePtr;
    remote[0].iov_len = bufferLength;

/*Nread will contain amount of bytes of data read*/

nread = process_vm_readv(pid, local, 2, remote, 1, 0); 
    if (nread < 0) {
        switch (errno) {
        case EINVAL:
            printf("ERROR: INVALID ARGUMENTS.\n");
            break;
        case EFAULT:
            printf
                ("ERROR: UNABLE TO ACCESS TARGET MEMORY ADDRESS.\n");
            break;
        case ENOMEM:
            printf("ERROR: UNABLE TO ALLOCATE MEMORY.\n");
            break;
        case EPERM:
            printf
                ("ERROR: INSUFFICIENT PRIVILEGES TO TARGET PROCESS.\n");
            break;
        case ESRCH:
            printf("ERROR: PROCESS DOES NOT EXIST.\n");
            break;
        default:
            printf("ERROR: AN UNKNOWN ERROR HAS OCCURRED.\n");
        }

        return -1;
    }
/* To print the read data */
printf("The read text is \n %s\n", local[0].iov_base);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM