+ sprintf(execname, "vmmap -interleaved %d", pid);
+ map = popen(execname, "r");
+
+ if (!map) {
+ perror("popen");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Wait for correct part */
+ while (fgets(buf, sizeof(buf), map)) {
+ if (strstr(buf, "==== regions for process"))
+ break;
+ }
+
+ while (fgets(buf, sizeof(buf), map)) {
+ char regionname[200] = "";
+ char type[23];
+ char smstr[23];
+ char r, w, x;
+ char mr, mw, mx;
+ int size;
+ void *begin, *end;
+
+ //Skip out at the end of the section
+ if (buf[0] == '\n')
+ break;
+
+ sscanf(buf, "%22s %p-%p [%5dK] %c%c%c/%c%c%c SM=%3s %200s\n", type, &begin, &end, &size, &r, &w, &x, &mr, &mw, &mx, smstr, regionname);
+
+ if (w == 'w' && strstr(regionname, MYBINARYNAME)) {
+ size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
+ if (len != 0)
+ snapshot_add_memory_region(begin, len);
+ }
+ }
+ pclose(map);
+}
+#else
+
+static void get_binary_name(char *buf, size_t len)
+{
+ ssize_t size = readlink("/proc/self/exe", buf, len);
+ if (size < 0) {
+ perror("readlink");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Terminate string */
+ if ((size_t)size > len)
+ size = len;
+ buf[size] = '\0';
+}
+
+/** The SnapshotGlobalSegments function computes the memory regions
+ * that may contain globals and then configures the snapshotting
+ * library to snapshot them.
+ */
+static void SnapshotGlobalSegments()
+{
+ char buf[9000];
+ char binary_name[800];
+ FILE *map;
+
+ map = fopen(MAPFILE, "r");