Logo AND Algorithmique Numérique Distribuée

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