Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fb91333798545ab52fc12c232b791fab8af1c4b1
[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   if(!fp)
23     xbt_abort();
24     
25   ret = xbt_new0(s_memory_map_t,1);
26   
27   /* Read one line at the time, parse it and add it to the memory map to be returned */ 
28   while((read = getline(&line, &n, fp)) != -1) {
29
30     /* Wipeout the new line character */
31     line[read - 1] = '\0';
32
33     /* Tokenize the line using spaces as delimiters and store each token */
34     /* in lfields array. We expect 5 tokens/fields */
35     lfields[0] = strtok(line, " ");
36     
37     for(i=1; i < 6 && lfields[i - 1] != NULL; i++){
38       lfields[i] = strtok(NULL, " ");  
39     }
40
41     /* Check to see if we got the expected amount of columns */
42     if(i < 6)
43       xbt_abort();
44
45     /* Ok we are good enough to try to get the info we need */
46     /* First get the start and the end address of the map   */
47     tok = strtok(lfields[0], "-");   
48     if(tok == NULL)
49       xbt_abort();
50       
51     memreg.start_addr = (void *)strtoul(tok, &endptr, 16);
52     /* Make sure that the entire string was an hex number */
53     if(*endptr != '\0')
54       xbt_abort();
55           
56     tok = strtok(NULL, "-");
57     if(tok == NULL)
58       xbt_abort();
59     
60     memreg.end_addr = (void *)strtoul(tok, &endptr, 16);
61     /* Make sure that the entire string was an hex number */
62     if(*endptr != '\0')
63       xbt_abort();
64     
65     /* Get the permissions flags */
66     if(strlen(lfields[1]) < 4)
67       xbt_abort();
68     
69     memreg.perms = 0;
70     
71     for(i=0; i<3; i++)
72       if(lfields[1][i] != '-')
73         memreg.perms |= 1 << i;
74
75     if(lfields[1][4] == 'p')
76       memreg.perms |= MAP_PRIV;
77       
78     else if (lfields[1][4] == 's')
79       memreg.perms |= MAP_SHARED;     
80             
81     /* Get the offset value */
82     memreg.offset = (void *)strtoul(lfields[2], &endptr, 16);
83     /* Make sure that the entire string was an hex number */
84     if(*endptr != '\0')
85       xbt_abort();
86       
87     /* Get the device major:minor bytes */
88     tok = strtok(lfields[3], ":");
89     if(tok == NULL)
90       xbt_abort();
91
92     memreg.dev_major = (char)strtoul(tok, &endptr, 16);    
93     /* Make sure that the entire string was an hex number */
94     if(*endptr != '\0')
95       xbt_abort();
96
97     tok = strtok(NULL, ":");
98     if(tok == NULL)
99       xbt_abort();
100
101     memreg.dev_minor = (char)strtoul(tok, &endptr, 16);    
102     /* Make sure that the entire string was an hex number */
103     if(*endptr != '\0')
104       xbt_abort();
105  
106     /* Get the inode number and make sure that the entire string was a long int*/
107     memreg.inode = strtoul(lfields[4], &endptr, 10);
108     if(*endptr != '\0')
109       xbt_abort();
110
111     /* And finally get the pathname */
112     memreg.pathname = xbt_strdup(lfields[5]);
113     
114     /* Create space for a new map region in the region's array and copy the */
115     /* parsed stuff from the temporal memreg variable */
116     ret->regions = xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
117     memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));    
118     ret->mapsize++;    
119   }
120   
121   if(line)
122     free(line);
123   
124   return ret;
125 }