[libgtop] If no argument is given, defaults to self. When self-testing, try to mmap some files as an example.



commit 548f104f357c20830737e6874c0e80c800741632
Author: Benoit Dejean <bdejean gmail com>
Date:   Sun Jun 21 11:04:59 2015 +0200

    If no argument is given, defaults to self. When self-testing, try to mmap some files as an example.

 examples/procmap.c |   38 ++++++++++++++++++++++++++++++++++++--
 1 files changed, 36 insertions(+), 2 deletions(-)
---
diff --git a/examples/procmap.c b/examples/procmap.c
index 040fa57..68dcd87 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);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]