Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve the integration of mmalloc and mc_memory into the mess.
[simgrid.git] / src / mc / memory_map.c
1 #define _GNU_SOURCE
2 #include "private.h"
3 #include <stdlib.h>
4
5 memory_map_t get_memory_map(void)
6 {
7   FILE *fp;                  /* File pointer to process's proc maps file */
8   char *line = NULL;         /* Temporal storage for each line that is readed */
9   ssize_t read;              /* Number of bytes readed */
10   size_t n = 0;              /* Amount of bytes to read by getline */
11   memory_map_t ret = NULL;   /* The memory map to return */
12   
13 /* The following variables are used during the parsing of the file "maps" */
14   s_map_region memreg;       /* temporal map region used for creating the map */                             
15   char *lfields[6], *tok, *endptr;
16   int i;
17   
18 /* Open the actual process's proc maps file and create the memory_map_t */
19 /* to be returned. */
20   fp = fopen("/proc/self/maps","r");  
21
22   xbt_assert0(fp,"Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
23     
24   ret = xbt_new0(s_memory_map_t,1);
25   
26   /* Read one line at the time, parse it and add it to the memory map to be returned */ 
27   while((read = getline(&line, &n, fp)) != -1) {
28
29     /* Wipeout the new line character */
30     line[read - 1] = '\0';
31
32     /* Tokenize the line using spaces as delimiters and store each token */
33     /* in lfields array. We expect 5 tokens/fields */
34     lfields[0] = strtok(line, " ");
35     
36     for(i=1; i < 6 && lfields[i - 1] != NULL; i++){
37       lfields[i] = strtok(NULL, " ");  
38     }
39
40     /* Check to see if we got the expected amount of columns */
41     if(i < 6)
42       xbt_abort();
43
44     /* Ok we are good enough to try to get the info we need */
45     /* First get the start and the end address of the map   */
46     tok = strtok(lfields[0], "-");   
47     if(tok == NULL)
48       xbt_abort();
49       
50     memreg.start_addr = (void *)strtoul(tok, &endptr, 16);
51     /* Make sure that the entire string was an hex number */
52     if(*endptr != '\0')
53       xbt_abort();
54           
55     tok = strtok(NULL, "-");
56     if(tok == NULL)
57       xbt_abort();
58     
59     memreg.end_addr = (void *)strtoul(tok, &endptr, 16);
60     /* Make sure that the entire string was an hex number */
61     if(*endptr != '\0')
62       xbt_abort();
63     
64     /* Get the permissions flags */
65     if(strlen(lfields[1]) < 4)
66       xbt_abort();
67     
68     memreg.perms = 0;
69     
70     for(i=0; i<3; i++)
71       if(lfields[1][i] != '-')
72         memreg.perms |= 1 << i;
73
74     if(lfields[1][4] == 'p')
75       memreg.perms |= MAP_PRIV;
76       
77     else if (lfields[1][4] == 's')
78       memreg.perms |= MAP_SHARED;     
79             
80     /* Get the offset value */
81     memreg.offset = (void *)strtoul(lfields[2], &endptr, 16);
82     /* Make sure that the entire string was an hex number */
83     if(*endptr != '\0')
84       xbt_abort();
85       
86     /* Get the device major:minor bytes */
87     tok = strtok(lfields[3], ":");
88     if(tok == NULL)
89       xbt_abort();
90
91     memreg.dev_major = (char)strtoul(tok, &endptr, 16);    
92     /* Make sure that the entire string was an hex number */
93     if(*endptr != '\0')
94       xbt_abort();
95
96     tok = strtok(NULL, ":");
97     if(tok == NULL)
98       xbt_abort();
99
100     memreg.dev_minor = (char)strtoul(tok, &endptr, 16);    
101     /* Make sure that the entire string was an hex number */
102     if(*endptr != '\0')
103       xbt_abort();
104  
105     /* Get the inode number and make sure that the entire string was a long int*/
106     memreg.inode = strtoul(lfields[4], &endptr, 10);
107     if(*endptr != '\0')
108       xbt_abort();
109
110     /* And finally get the pathname */
111     memreg.pathname = xbt_strdup(lfields[5]);
112     
113     /* Create space for a new map region in the region's array and copy the */
114     /* parsed stuff from the temporal memreg variable */
115     ret->regions = xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
116     memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));    
117     ret->mapsize++;    
118   }
119   
120   if(line)
121     free(line);
122   
123   return ret;
124 }