Logo AND Algorithmique Numérique Distribuée

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