Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make a std::vector of Process::checkpoint_ignore
[simgrid.git] / src / mc / memory_map.cpp
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 <stdlib.h>
8
9 #include <sys/types.h>
10
11 #include "mc_memory_map.h"
12 #include "mc_private.h"
13
14 extern "C" {
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
17                                 "Logging specific to algorithms for memory_map");
18
19 memory_map_t MC_get_memory_map(pid_t pid)
20 {
21   /* Open the actual process's proc maps file and create the memory_map_t */
22   /* to be returned. */
23   char* path = bprintf("/proc/%i/maps", (int) pid);
24   FILE *fp = fopen(path, "r");
25   free(path);
26   if(fp == NULL)
27     perror("fopen failed");
28   xbt_assert(fp,
29     "Cannot open %s to investigate the memory map of the process.", path);
30   setbuf(fp, NULL);
31
32   memory_map_t ret = xbt_new0(s_memory_map_t, 1);
33
34   /* Read one line at the time, parse it and add it to the memory map to be returned */
35   ssize_t read; /* Number of bytes readed */
36   char* line = NULL;
37   size_t n = 0; /* Amount of bytes to read by xbt_getline */
38   while ((read = xbt_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     char* lfields[6];
48     lfields[0] = strtok(line, " ");
49
50     int i;
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     char *tok = strtok(lfields[0], "-");
62     if (tok == NULL)
63       xbt_abort();
64
65     s_map_region_t memreg;          /* temporal map region used for creating the map */
66     char *endptr;
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     XBT_DEBUG("Found region for %s",
147       memreg.pathname ? memreg.pathname : "(null)");
148     ret->regions = (map_region_t)
149         xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
150     memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));
151     ret->mapsize++;
152
153   }
154
155   free(line);
156   fclose(fp);
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 }
169
170 }