Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] adding a new boolean parameter (tracing/platform) to register platform in...
[simgrid.git] / src / mc / memory_map.c
1 #define _GNU_SOURCE
2 #include "private.h"
3 #include <stdlib.h>
4
5 memory_map_t get_memory_map(void)
6 {
7   FILE *fp;                     /* File pointer to process's proc maps file */
8   char *line = NULL;            /* Temporal storage for each line that is readed */
9   ssize_t read;                 /* Number of bytes readed */
10   size_t n = 0;                 /* Amount of bytes to read by getline */
11   memory_map_t ret = NULL;      /* The memory map to return */
12
13 /* The following variables are used during the parsing of the file "maps" */
14   s_map_region memreg;          /* temporal map region used for creating the map */
15   char *lfields[6], *tok, *endptr;
16   int i;
17
18 /* Open the actual process's proc maps file and create the memory_map_t */
19 /* to be returned. */
20   fp = fopen("/proc/self/maps", "r");
21
22   xbt_assert(fp,
23               "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
24
25   ret = xbt_new0(s_memory_map_t, 1);
26
27   /* Read one line at the time, parse it and add it to the memory map to be returned */
28   while ((read = getline(&line, &n, fp)) != -1) {
29
30     /* Wipeout the new line character */
31     line[read - 1] = '\0';
32
33     /* Tokenize the line using spaces as delimiters and store each token */
34     /* in lfields array. We expect 5 tokens/fields */
35     lfields[0] = strtok(line, " ");
36
37     for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
38       lfields[i] = strtok(NULL, " ");
39     }
40
41     /* Check to see if we got the expected amount of columns */
42     if (i < 6)
43       xbt_abort();
44
45     /* Ok we are good enough to try to get the info we need */
46     /* First get the start and the end address of the map   */
47     tok = strtok(lfields[0], "-");
48     if (tok == NULL)
49       xbt_abort();
50
51     memreg.start_addr = (void *) strtoul(tok, &endptr, 16);
52     /* Make sure that the entire string was an hex number */
53     if (*endptr != '\0')
54       xbt_abort();
55
56     tok = strtok(NULL, "-");
57     if (tok == NULL)
58       xbt_abort();
59
60     memreg.end_addr = (void *) strtoul(tok, &endptr, 16);
61     /* Make sure that the entire string was an hex number */
62     if (*endptr != '\0')
63       xbt_abort();
64
65     /* Get the permissions flags */
66     if (strlen(lfields[1]) < 4)
67       xbt_abort();
68
69     memreg.prot = 0;
70
71     for (i = 0; i < 3; i++){
72       switch(lfields[1][i]){
73         case 'r':
74           memreg.prot |= PROT_READ;
75           break;
76         case 'w':
77           memreg.prot |= PROT_WRITE;
78           break;
79         case 'x':
80           memreg.prot |= PROT_EXEC;
81           break;
82         default:
83           break;
84       }
85     }
86     if (memreg.prot == 0)
87       memreg.prot |= PROT_NONE;
88
89     if (lfields[1][4] == 'p')
90       memreg.flags |= MAP_PRIVATE;
91
92     else if (lfields[1][4] == 's')
93       memreg.flags |= MAP_SHARED;
94
95     /* Get the offset value */
96     memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
97     /* Make sure that the entire string was an hex number */
98     if (*endptr != '\0')
99       xbt_abort();
100
101     /* Get the device major:minor bytes */
102     tok = strtok(lfields[3], ":");
103     if (tok == NULL)
104       xbt_abort();
105
106     memreg.dev_major = (char) strtoul(tok, &endptr, 16);
107     /* Make sure that the entire string was an hex number */
108     if (*endptr != '\0')
109       xbt_abort();
110
111     tok = strtok(NULL, ":");
112     if (tok == NULL)
113       xbt_abort();
114
115     memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
116     /* Make sure that the entire string was an hex number */
117     if (*endptr != '\0')
118       xbt_abort();
119
120     /* Get the inode number and make sure that the entire string was a long int */
121     memreg.inode = strtoul(lfields[4], &endptr, 10);
122     if (*endptr != '\0')
123       xbt_abort();
124
125     /* And finally get the pathname */
126     memreg.pathname = xbt_strdup(lfields[5]);
127
128     /* Create space for a new map region in the region's array and copy the */
129     /* parsed stuff from the temporal memreg variable */
130     ret->regions =
131         xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
132     memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));
133     ret->mapsize++;
134   }
135
136   if (line)
137     free(line);
138
139   return ret;
140 }