Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : test data in libsimgrid memory region between each state
[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   char *backup_line = NULL;
22
23 /* Open the actual process's proc maps file and create the memory_map_t */
24 /* to be returned. */
25   fp = fopen("/proc/self/maps", "r");
26
27   xbt_assert(fp,
28               "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
29
30   //XBT_DEBUG("/proc/self/maps");
31
32   ret = xbt_new0(s_memory_map_t, 1);
33
34   /* Read one line at the time, parse it and add it to the memory map to be returned */
35   while ((read = getline(&line, &n, fp)) != -1) {
36
37     //XBT_INFO("%s", line);
38     if(XBT_LOG_ISENABLED(mc_memory_map, xbt_log_priority_debug))
39       backup_line = strdup(line);
40
41     /* Wipeout the new line character */
42     line[read - 1] = '\0';
43
44     /* Tokenize the line using spaces as delimiters and store each token */
45     /* in lfields array. We expect 5 tokens/fields */
46     lfields[0] = strtok(line, " ");
47
48     for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
49       lfields[i] = strtok(NULL, " ");
50     }
51
52     /* Check to see if we got the expected amount of columns */
53     if (i < 6)
54       xbt_abort();
55
56     /* Ok we are good enough to try to get the info we need */
57     /* First get the start and the end address of the map   */
58     tok = strtok(lfields[0], "-");
59     if (tok == NULL)
60       xbt_abort();
61
62     memreg.start_addr = (void *) strtoul(tok, &endptr, 16);
63     /* Make sure that the entire string was an hex number */
64     if (*endptr != '\0')
65       xbt_abort();
66
67     tok = strtok(NULL, "-");
68     if (tok == NULL)
69       xbt_abort();
70
71     memreg.end_addr = (void *) strtoul(tok, &endptr, 16);
72     /* Make sure that the entire string was an hex number */
73     if (*endptr != '\0')
74       xbt_abort();
75
76     /* Get the permissions flags */
77     if (strlen(lfields[1]) < 4)
78       xbt_abort();
79
80     memreg.prot = 0;
81
82     for (i = 0; i < 3; i++){
83       switch(lfields[1][i]){
84         case 'r':
85           memreg.prot |= PROT_READ;
86           break;
87         case 'w':
88           memreg.prot |= PROT_WRITE;
89           break;
90         case 'x':
91           memreg.prot |= PROT_EXEC;
92           break;
93         default:
94           break;
95       }
96     }
97     if (memreg.prot == 0)
98       memreg.prot |= PROT_NONE;
99
100     if (lfields[1][4] == 'p')
101       memreg.flags |= MAP_PRIVATE;
102
103     else if (lfields[1][4] == 's')
104       memreg.flags |= MAP_SHARED;
105
106     /* Get the offset value */
107     memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
108     /* Make sure that the entire string was an hex number */
109     if (*endptr != '\0')
110       xbt_abort();
111
112     /* Get the device major:minor bytes */
113     tok = strtok(lfields[3], ":");
114     if (tok == NULL)
115       xbt_abort();
116
117     memreg.dev_major = (char) strtoul(tok, &endptr, 16);
118     /* Make sure that the entire string was an hex number */
119     if (*endptr != '\0')
120       xbt_abort();
121
122     tok = strtok(NULL, ":");
123     if (tok == NULL)
124       xbt_abort();
125
126     memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
127     /* Make sure that the entire string was an hex number */
128     if (*endptr != '\0')
129       xbt_abort();
130
131     /* Get the inode number and make sure that the entire string was a long int */
132     memreg.inode = strtoul(lfields[4], &endptr, 10);
133     if (*endptr != '\0')
134       xbt_abort();
135
136     /* And finally get the pathname */
137     memreg.pathname = xbt_strdup(lfields[5]);
138
139     /* Create space for a new map region in the region's array and copy the */
140     /* parsed stuff from the temporal memreg variable */
141     ret->regions =
142         xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
143     memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));
144     ret->mapsize++;
145
146     if(XBT_LOG_ISENABLED(mc_memory_map, xbt_log_priority_debug)){
147       if ((memreg.prot & PROT_WRITE)){
148         if (memreg.pathname == NULL){
149           if (memreg.start_addr == std_heap){ 
150             XBT_DEBUG("New region in snapshot : %s", backup_line); 
151           }
152         } else {
153           if (!memcmp(basename(memreg.pathname), "libsimgrid", 10)){
154             XBT_DEBUG("New region in snapshot : %s", backup_line); 
155           }
156         }
157       }
158     }
159
160
161   }
162
163   if (line)
164     free(line);
165
166   return ret;
167 }