Skip to content

Instantly share code, notes, and snippets.

@iamahuman
Last active July 16, 2017 12:04
Show Gist options
  • Save iamahuman/a930ff5932b0d9890c9bdc0370b5881d to your computer and use it in GitHub Desktop.
Save iamahuman/a930ff5932b0d9890c9bdc0370b5881d to your computer and use it in GitHub Desktop.
Linux: show the memory map of current process
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int fd, fail;
ssize_t nread, nwritten;
size_t acc;
char buffer[1024];
fd = open("/proc/self/maps", O_RDONLY);
if (fd < 0) {
perror("open");
return EXIT_FAILURE;
}
while ((nread = read(fd, buffer, sizeof(buffer))) > 0)
{
fail = 0;
for (acc = 0; acc < sizeof(buffer) && acc < nread; ) {
nwritten = write(STDOUT_FILENO, buffer + acc, nread - acc);
if (nwritten <= 0)
{
fail = 1;
break;
}
acc += nwritten;
}
if (fail)
{
if (nwritten < 0)
{
perror("write");
close(fd);
return EXIT_FAILURE;
}
break;
}
}
close(fd);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment