Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Add AddressSpace::process() method
[simgrid.git] / src / mc / memory_map.cpp
1 /* Copyright (c) 2008-2015. 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 <cstdlib>
8 #include <cstdio>
9 #include <cstring>
10
11 #include <sys/types.h>
12
13 #include <xbt/base.h>
14
15 #include "mc_memory_map.h"
16 #include "mc_private.h"
17
18 extern "C" {
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
21                                 "Logging specific to algorithms for memory_map");
22
23 }
24
25 namespace simgrid {
26 namespace mc {
27
28 XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
29 {
30   /* Open the actual process's proc maps file and create the memory_map_t */
31   /* to be returned. */
32   char* path = bprintf("/proc/%i/maps", (int) pid);
33   FILE *fp = std::fopen(path, "r");
34   if(fp == NULL)
35     std::perror("fopen failed");
36   xbt_assert(fp,
37     "Cannot open %s to investigate the memory map of the process.", path);
38   free(path);
39   setbuf(fp, NULL);
40
41   std::vector<VmMap> ret;
42
43   /* Read one line at the time, parse it and add it to the memory map to be returned */
44   ssize_t read; /* Number of bytes readed */
45   char* line = NULL;
46   std::size_t n = 0; /* Amount of bytes to read by xbt_getline */
47   while ((read = xbt_getline(&line, &n, fp)) != -1) {
48
49     //fprintf(stderr,"%s", line);
50
51     /* Wipeout the new line character */
52     line[read - 1] = '\0';
53
54     /* Tokenize the line using spaces as delimiters and store each token */
55     /* in lfields array. We expect 5 tokens/fields */
56     char* lfields[6];
57     lfields[0] = strtok(line, " ");
58
59     int i;
60     for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
61       lfields[i] = std::strtok(NULL, " ");
62     }
63
64     /* Check to see if we got the expected amount of columns */
65     if (i < 6)
66       xbt_abort();
67
68     /* Ok we are good enough to try to get the info we need */
69     /* First get the start and the end address of the map   */
70     char *tok = std::strtok(lfields[0], "-");
71     if (tok == NULL)
72       xbt_abort();
73
74     VmMap memreg;
75     char *endptr;
76     memreg.start_addr = std::strtoull(tok, &endptr, 16);
77     /* Make sure that the entire string was an hex number */
78     if (*endptr != '\0')
79       xbt_abort();
80
81     tok = std::strtok(NULL, "-");
82     if (tok == NULL)
83       xbt_abort();
84
85     memreg.end_addr = std::strtoull(tok, &endptr, 16);
86     /* Make sure that the entire string was an hex number */
87     if (*endptr != '\0')
88       xbt_abort();
89
90     /* Get the permissions flags */
91     if (std::strlen(lfields[1]) < 4)
92       xbt_abort();
93
94     memreg.prot = 0;
95
96     for (i = 0; i < 3; i++){
97       switch(lfields[1][i]){
98         case 'r':
99           memreg.prot |= PROT_READ;
100           break;
101         case 'w':
102           memreg.prot |= PROT_WRITE;
103           break;
104         case 'x':
105           memreg.prot |= PROT_EXEC;
106           break;
107         default:
108           break;
109       }
110     }
111     if (memreg.prot == 0)
112       memreg.prot |= PROT_NONE;
113
114     if (lfields[1][4] == 'p')
115       memreg.flags |= MAP_PRIVATE;
116
117     else if (lfields[1][4] == 's')
118       memreg.flags |= MAP_SHARED;
119
120     /* Get the offset value */
121     memreg.offset = std::strtoull(lfields[2], &endptr, 16);
122     /* Make sure that the entire string was an hex number */
123     if (*endptr != '\0')
124       xbt_abort();
125
126     /* Get the device major:minor bytes */
127     tok = std::strtok(lfields[3], ":");
128     if (tok == NULL)
129       xbt_abort();
130
131     memreg.dev_major = (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     tok = std::strtok(NULL, ":");
137     if (tok == NULL)
138       xbt_abort();
139
140     memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
141     /* Make sure that the entire string was an hex number */
142     if (*endptr != '\0')
143       xbt_abort();
144
145     /* Get the inode number and make sure that the entire string was a long int */
146     memreg.inode = strtoul(lfields[4], &endptr, 10);
147     if (*endptr != '\0')
148       xbt_abort();
149
150     /* And finally get the pathname */
151     if (lfields[5])
152       memreg.pathname = lfields[5];
153
154     /* Create space for a new map region in the region's array and copy the */
155     /* parsed stuff from the temporal memreg variable */
156     XBT_DEBUG("Found region for %s",
157       !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
158
159     ret.push_back(std::move(memreg));
160   }
161
162   std::free(line);
163   std::fclose(fp);
164   return std::move(ret);
165 }
166
167 }
168 }