Logo AND Algorithmique Numérique Distribuée

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