fstream无法读取页面地图?

时间:2018-07-31 18:45:13

标签: c++ linux fstream

我正在尝试为进程解码/ proc / [pid] / pagemap文件,但是以下使用fstream的代码将buffer的值输出为0:

int main() {

  const uint64_t addr       = 0x1396010;
  const uint8_t  offsetBits = 12; 
  const uint8_t  entryBytes = 8;

  unsigned long long offset = (addr >> offsetBits) * entryBytes;
  unsigned long long buffer = 0;

  std::ifstream file ("/proc/19710/pagemap", std::ifstream::binary);

 if (file) {
   std::cout << "Got the file" << std::endl;
 }

 file.seekg(offset, file.beg);
 file.read((char*)(&buffer), 8); 

 std::cout << "Offset:: " << offset << std::endl; 

 std::cout << buffer << std::endl;
}

同一程序使用C文件功能时,输出正确的val值:

int main() {

    const uint64_t addr       = 0x1396010;
    const uint8_t  offsetBits = 12;
    const uint8_t  entryBytes = 8;

    uint64_t offset = (addr >> offsetBits) * entryBytes;

    std::cout << "Offset: " << offset << std::endl;

    char pagemap[256];

    sprintf(pagemap, "%s", "/proc/19710/pagemap");

    FILE* file = fopen(pagemap, "rb");

    fseek(file, offset, SEEK_SET);

    uint64_t val = 0;

    fread(&val, entryBytes, 1, file);

    std::cout << val << std::endl;
}

两个程序都使用sudo命令运行。我不知道为什么第一个程序不能使用pagemap文件,但是可以正确使用任何其他二进制文件。

0 个答案:

没有答案