diff options
author | Benoit Dejean <bdejean@gmail.com> | 2015-06-21 11:04:59 +0200 |
---|---|---|
committer | Robert Roth <robert.roth.off@gmail.com> | 2015-06-26 02:57:41 +0300 |
commit | 548f104f357c20830737e6874c0e80c800741632 (patch) | |
tree | 07a1609a0efc9a73414ef4db77856adb2e3b8d45 /examples/procmap.c | |
parent | 4808aac8ef347c42a28119736a57dec5b4c6b4b3 (diff) | |
download | libgtop-548f104f357c20830737e6874c0e80c800741632.tar.gz |
If no argument is given, defaults to self. When self-testing, try to mmap some files as an example.
Diffstat (limited to 'examples/procmap.c')
-rw-r--r-- | examples/procmap.c | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/examples/procmap.c b/examples/procmap.c index 040fa57d..68dcd872 100644 --- a/examples/procmap.c +++ b/examples/procmap.c @@ -43,6 +43,30 @@ #define PROFILE_COUNT 1 #endif +static void +try_mmap(const char *path) +{ + struct stat buf; + int fd; + + if ((fd = open(path, O_RDONLY)) < 0) + goto out; + + if (fstat(fd, &buf) < 0) + goto out; + + if (mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0) == MAP_FAILED) + goto out; + + close(fd); + + return; + +out: + fprintf(stderr, "Failed to test mmap with '%s'\n", path); +} + + int main (int argc, char *argv []) { @@ -78,8 +102,18 @@ main (int argc, char *argv []) glibtop_init_r (&glibtop_global_server, 0, 0); - if ((argc != 2) || (sscanf (argv [1], "%d", (int *) &pid) != 1)) - g_error ("Usage: %s pid", argv [0]); + if (argc == 1) { + pid = getpid(); + } + else if ((argc != 2) || (sscanf (argv [1], "%d", (int *) &pid) != 1)) + g_error ("Usage: %s [pid]", argv [0]); + + if (pid == getpid()) { + /* let's map something for a try */ + try_mmap("/etc/passwd"); + try_mmap("/etc/resolv.conf"); + try_mmap(argv[0]); + } fprintf (stderr, "Getting memory maps for pid %d.\n\n", (int) pid); |