如何使用C/C ++语言在Linux/UNIX中查找进程的所有读写内存地址? [英] How to find all read-write memory address of a process in Linux/UNIX with C/C++ language?

查看:112
本文介绍了如何使用C/C ++语言在Linux/UNIX中查找进程的所有读写内存地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过/proc文件系统,可以使用/proc/PID_PROCESS/maps读取内存映射,但是在C/C ++中是否有专门用于此功能的本机API?

Through /proc file system , it's probable to read memory mappings with /proc/PID_PROCESS/maps , but is there any native APIs that dedicated for this function in C/C++ ?

即找出可写和可读的内存地址,以使用PID 9322进行处理:

i.e to find out memory address that are writable and readable for process with PID 9322:

%> awk -F "-| " '$3 ~ /rw/ { print $1 " " $2}' /proc/9322/maps
0804e000 0804f000
085ed000 0860e000
b7707000 b7708000
b7864000 b7865000
b7865000 b7868000
b7897000 b7898000
b78b6000 b78b7000
bfd2e000 bfd50000

这些地址被传递到我的程序中,但是现在我想将此函数直接集成到我的C ++程序中.

And those address are passed into my program , but now i want to integrate this function directly into my C++ program.

为了最有效,如果我想支持其他* BSD系统,我将无法利用/proc系统,并且我认为应该有一些方法可以直接生成例如/proc/1/maps而无需阅读他们在那里再次,如果我错了,请更正^ _ ^

For most effectiveness , if i want to support for other *BSD system , i would not be able to take advantage of /proc system , and i think there should some method to generate e.g /proc/1/maps directly without reading them again there , correct if i'm wrong ^_^

推荐答案

读取proc文件就像读取普通文件一样.

Read the proc file like you read normal file.

例如

  FILE *filep = fopen("/proc/9322/maps","r");
  char ch;
  while (ch != EOF){
    ch = fgetc(filep);
    printf("%c", ch);
  }

这篇关于如何使用C/C ++语言在Linux/UNIX中查找进程的所有读写内存地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆