Logo AND Algorithmique Numérique Distribuée

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