1 /* Copyright (c) 2008-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "mc_memory_map.h"
12 #include "mc_private.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
17 "Logging specific to algorithms for memory_map");
24 std::vector<VmMap> get_memory_map(pid_t pid)
26 /* Open the actual process's proc maps file and create the memory_map_t */
28 char* path = bprintf("/proc/%i/maps", (int) pid);
29 FILE *fp = fopen(path, "r");
32 perror("fopen failed");
34 "Cannot open %s to investigate the memory map of the process.", path);
37 std::vector<VmMap> ret;
39 /* Read one line at the time, parse it and add it to the memory map to be returned */
40 ssize_t read; /* Number of bytes readed */
42 size_t n = 0; /* Amount of bytes to read by xbt_getline */
43 while ((read = xbt_getline(&line, &n, fp)) != -1) {
45 //fprintf(stderr,"%s", line);
47 /* Wipeout the new line character */
48 line[read - 1] = '\0';
50 /* Tokenize the line using spaces as delimiters and store each token */
51 /* in lfields array. We expect 5 tokens/fields */
53 lfields[0] = strtok(line, " ");
56 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
57 lfields[i] = strtok(NULL, " ");
60 /* Check to see if we got the expected amount of columns */
64 /* Ok we are good enough to try to get the info we need */
65 /* First get the start and the end address of the map */
66 char *tok = strtok(lfields[0], "-");
72 memreg.start_addr = strtoull(tok, &endptr, 16);
73 /* Make sure that the entire string was an hex number */
77 tok = strtok(NULL, "-");
81 memreg.end_addr = strtoull(tok, &endptr, 16);
82 /* Make sure that the entire string was an hex number */
86 /* Get the permissions flags */
87 if (strlen(lfields[1]) < 4)
92 for (i = 0; i < 3; i++){
93 switch(lfields[1][i]){
95 memreg.prot |= PROT_READ;
98 memreg.prot |= PROT_WRITE;
101 memreg.prot |= PROT_EXEC;
107 if (memreg.prot == 0)
108 memreg.prot |= PROT_NONE;
110 if (lfields[1][4] == 'p')
111 memreg.flags |= MAP_PRIVATE;
113 else if (lfields[1][4] == 's')
114 memreg.flags |= MAP_SHARED;
116 /* Get the offset value */
117 memreg.offset = strtoull(lfields[2], &endptr, 16);
118 /* Make sure that the entire string was an hex number */
122 /* Get the device major:minor bytes */
123 tok = strtok(lfields[3], ":");
127 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
128 /* Make sure that the entire string was an hex number */
132 tok = strtok(NULL, ":");
136 memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
137 /* Make sure that the entire string was an hex number */
141 /* Get the inode number and make sure that the entire string was a long int */
142 memreg.inode = strtoul(lfields[4], &endptr, 10);
146 /* And finally get the pathname */
148 memreg.pathname = lfields[5];
150 /* Create space for a new map region in the region's array and copy the */
151 /* parsed stuff from the temporal memreg variable */
152 XBT_DEBUG("Found region for %s",
153 !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
155 ret.push_back(std::move(memreg));
160 return std::move(ret);