Pramod Subramanyan
2008-10-16 15:03:41 UTC
Hi,
I want to dump the virtual memory map of my process, and it seems like
I can get this from the /proc/<pid>/maps file. The trouble is, when I
try to loop over this file until EOF, I never get EOF and if I try to
calculate the length of the file, its turning out to be zero!
Here is a small snippet that demonstrates the problem with calculating
the filelength.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
int main(int argc, char* argv[])
{
char filename[1024];
sprintf(filename, "/proc/%s/maps", argv[1]);
printf("filename is %s\n", filename);
FILE* fp = fopen(filename, "r");
assert(fp);
fseek(fp, 0, SEEK_END);
long pos = ftell(fp);
assert(pos);
char* buf = malloc(pos);
fseek(fp, 0, SEEK_SET);
fread(buf, pos, 1, fp);
puts(buf);
fclose(fp);
return 0;
}
This one fails the assertion for pos.
Any ideas on what I'm doing wrong? Am I missing something here?
Thanks in advance,
pramod
I want to dump the virtual memory map of my process, and it seems like
I can get this from the /proc/<pid>/maps file. The trouble is, when I
try to loop over this file until EOF, I never get EOF and if I try to
calculate the length of the file, its turning out to be zero!
Here is a small snippet that demonstrates the problem with calculating
the filelength.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
int main(int argc, char* argv[])
{
char filename[1024];
sprintf(filename, "/proc/%s/maps", argv[1]);
printf("filename is %s\n", filename);
FILE* fp = fopen(filename, "r");
assert(fp);
fseek(fp, 0, SEEK_END);
long pos = ftell(fp);
assert(pos);
char* buf = malloc(pos);
fseek(fp, 0, SEEK_SET);
fread(buf, pos, 1, fp);
puts(buf);
fclose(fp);
return 0;
}
This one fails the assertion for pos.
Any ideas on what I'm doing wrong? Am I missing something here?
Thanks in advance,
pramod