Logo AND Algorithmique Numérique Distribuée

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