Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Added a \todo item for further changes.
[simgrid.git] / src / xbt / 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 #ifdef __linux__
13 # include <sys/mman.h>
14 #endif
15
16 #include <xbt/sysdep.h>
17 #include <xbt/base.h>
18 #include <xbt/file.h>
19 #include <xbt/log.h>
20
21 #include "memory_map.hpp"
22
23 extern "C" {
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_memory_map, xbt, "Logging specific to algorithms for memory_map");
25 }
26
27 namespace simgrid {
28 namespace xbt {
29
30 /**
31  * \todo This function contains many cases that do not allow for a
32  *       recovery. Currently, xbt_abort() is called but we should
33  *       much rather die with the specific reason so that it's easier
34  *       to find out what's going on.
35  */
36 XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
37 {
38 #ifdef __linux__
39   /* Open the actual process's proc maps file and create the memory_map_t */
40   /* to be returned. */
41   char* path = bprintf("/proc/%i/maps", (int) pid);
42   FILE *fp = std::fopen(path, "r");
43   if (fp == nullptr) {
44     std::perror("fopen failed");
45     xbt_die("Cannot open %s to investigate the memory map of the process.", path);
46   }
47   free(path);
48   setbuf(fp, nullptr);
49
50   std::vector<VmMap> ret;
51
52   /* Read one line at the time, parse it and add it to the memory map to be returned */
53   ssize_t read; /* Number of bytes readed */
54   char* line = nullptr;
55   std::size_t n = 0; /* Amount of bytes to read by xbt_getline */
56   while ((read = xbt_getline(&line, &n, fp)) != -1) {
57
58     //fprintf(stderr,"%s", line);
59
60     /* Wipeout the new line character */
61     line[read - 1] = '\0';
62
63     /* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens/fields */
64     char* lfields[6];
65     lfields[0] = strtok(line, " ");
66
67     int i;
68     for (i = 1; i < 6 && lfields[i - 1] != nullptr; i++) {
69       lfields[i] = std::strtok(nullptr, " ");
70     }
71
72     /* Check to see if we got the expected amount of columns */
73     if (i < 6)
74       xbt_die("The memory map apparently only supplied less than 6 columns. Recovery impossible.");
75
76     /* Ok we are good enough to try to get the info we need */
77     /* First get the start and the end address of the map   */
78     char *tok = std::strtok(lfields[0], "-");
79     if (tok == nullptr)
80       xbt_die("Start and end address of the map are not concatenated by a hyphen (-). Recovery impossible.");
81
82     VmMap memreg;
83     char *endptr;
84     memreg.start_addr = std::strtoull(tok, &endptr, 16);
85     /* Make sure that the entire string was an hex number */
86     if (*endptr != '\0')
87       xbt_abort();
88
89     tok = std::strtok(nullptr, "-");
90     if (tok == nullptr)
91       xbt_abort();
92
93     memreg.end_addr = std::strtoull(tok, &endptr, 16);
94     /* Make sure that the entire string was an hex number */
95     if (*endptr != '\0')
96       xbt_abort();
97
98     /* Get the permissions flags */
99     if (std::strlen(lfields[1]) < 4)
100       xbt_abort();
101
102     memreg.prot = 0;
103
104     for (i = 0; i < 3; i++){
105       switch(lfields[1][i]){
106         case 'r':
107           memreg.prot |= PROT_READ;
108           break;
109         case 'w':
110           memreg.prot |= PROT_WRITE;
111           break;
112         case 'x':
113           memreg.prot |= PROT_EXEC;
114           break;
115         default:
116           break;
117       }
118     }
119     if (memreg.prot == 0)
120       memreg.prot |= PROT_NONE;
121
122     if (lfields[1][4] == 'p')
123       memreg.flags |= MAP_PRIVATE;
124     else if (lfields[1][4] == 's')
125       memreg.flags |= MAP_SHARED;
126
127     /* Get the offset value */
128     memreg.offset = std::strtoull(lfields[2], &endptr, 16);
129     /* Make sure that the entire string was an hex number */
130     if (*endptr != '\0')
131       xbt_abort();
132
133     /* Get the device major:minor bytes */
134     tok = std::strtok(lfields[3], ":");
135     if (tok == nullptr)
136       xbt_abort();
137
138     memreg.dev_major = (char) strtoul(tok, &endptr, 16);
139     /* Make sure that the entire string was an hex number */
140     if (*endptr != '\0')
141       xbt_abort();
142
143     tok = std::strtok(nullptr, ":");
144     if (tok == nullptr)
145       xbt_abort();
146
147     memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
148     /* Make sure that the entire string was an hex number */
149     if (*endptr != '\0')
150       xbt_abort();
151
152     /* Get the inode number and make sure that the entire string was a long int */
153     memreg.inode = strtoul(lfields[4], &endptr, 10);
154     if (*endptr != '\0')
155       xbt_abort();
156
157     /* And finally get the pathname */
158     if (lfields[5])
159       memreg.pathname = lfields[5];
160
161     /* Create space for a new map region in the region's array and copy the */
162     /* parsed stuff from the temporal memreg variable */
163     XBT_DEBUG("Found region for %s", !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
164
165     ret.push_back(std::move(memreg));
166   }
167
168   std::free(line);
169   std::fclose(fp);
170   return ret;
171 #else
172   /* On FreeBSD, kinfo_getvmmap() could be used but mmap() support is disabled anyway. */
173   xbt_die("Could not get memory map from process %lli", (long long int) pid);
174 #endif
175 }
176
177 }
178 }