Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[xbt] Documentation and cleanup
[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      * The lines that we read have this format: (This is just an example)
59      * 00602000-00603000 rw-p 00002000 00:28 1837264                            <complete-path-to-file>
60      */
61
62     //fprintf(stderr,"%s", line);
63
64     /* Wipeout the new line character */
65     line[read - 1] = '\0';
66
67     /* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens for 6 fields */
68     char* lfields[6];
69     lfields[0] = strtok(line, " ");
70
71     int i;
72     for (i = 1; i < 6 && lfields[i - 1] != nullptr; i++) {
73       lfields[i] = std::strtok(nullptr, " ");
74     }
75
76     /* Check to see if we got the expected amount of columns */
77     if (i < 6)
78       xbt_die("The memory map apparently only supplied less than 6 columns. Recovery impossible.");
79
80     /* Ok we are good enough to try to get the info we need */
81     /* First get the start and the end address of the map   */
82     char *tok = std::strtok(lfields[0], "-");
83     if (tok == nullptr)
84       xbt_die("Start and end address of the map are not concatenated by a hyphen (-). Recovery impossible.");
85
86     VmMap memreg;
87     char *endptr;
88     memreg.start_addr = std::strtoull(tok, &endptr, 16);
89     /* Make sure that the entire string was an hex number */
90     if (*endptr != '\0')
91       xbt_abort();
92
93     tok = std::strtok(nullptr, "-");
94     if (tok == nullptr)
95       xbt_abort();
96
97     memreg.end_addr = std::strtoull(tok, &endptr, 16);
98     /* Make sure that the entire string was an hex number */
99     if (*endptr != '\0')
100       xbt_abort();
101
102     /* Get the permissions flags */
103     if (std::strlen(lfields[1]) < 4)
104       xbt_abort();
105
106     memreg.prot = 0;
107
108     for (i = 0; i < 3; i++){
109       switch(lfields[1][i]){
110         case 'r':
111           memreg.prot |= PROT_READ;
112           break;
113         case 'w':
114           memreg.prot |= PROT_WRITE;
115           break;
116         case 'x':
117           memreg.prot |= PROT_EXEC;
118           break;
119         default:
120           break;
121       }
122     }
123     if (memreg.prot == 0)
124       memreg.prot |= PROT_NONE;
125
126     if (lfields[1][3] == 'p')
127       memreg.flags |= MAP_PRIVATE;
128     else if (lfields[1][3] == 's')
129       memreg.flags |= MAP_SHARED;
130     else {
131       xbt_die("Flag was neither 'p' (private) nor 's' (shared). This should have never happened! Instead, the permissions column was set to: %s\n"
132               "This was the whole line that caused the trouble: %s", lfields[1], line);
133     }
134
135     /* Get the offset value */
136     memreg.offset = std::strtoull(lfields[2], &endptr, 16);
137     /* Make sure that the entire string was an hex number */
138     if (*endptr != '\0')
139       xbt_abort();
140
141     /* Get the device major:minor bytes */
142     tok = std::strtok(lfields[3], ":");
143     if (tok == nullptr)
144       xbt_abort();
145
146     memreg.dev_major = (char) strtoul(tok, &endptr, 16);
147     /* Make sure that the entire string was an hex number */
148     if (*endptr != '\0')
149       xbt_abort();
150
151     tok = std::strtok(nullptr, ":");
152     if (tok == nullptr)
153       xbt_abort();
154
155     memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
156     /* Make sure that the entire string was an hex number */
157     if (*endptr != '\0')
158       xbt_abort();
159
160     /* Get the inode number and make sure that the entire string was a long int */
161     memreg.inode = strtoul(lfields[4], &endptr, 10);
162     if (*endptr != '\0')
163       xbt_abort();
164
165     /* And finally get the pathname */
166     if (lfields[5])
167       memreg.pathname = lfields[5];
168
169     /* Create space for a new map region in the region's array and copy the */
170     /* parsed stuff from the temporal memreg variable */
171     XBT_DEBUG("Found region for %s", !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
172
173     ret.push_back(std::move(memreg));
174   }
175
176   std::free(line);
177   std::fclose(fp);
178   return ret;
179 #else
180   /* On FreeBSD, kinfo_getvmmap() could be used but mmap() support is disabled anyway. */
181   xbt_die("Could not get memory map from process %lli", (long long int) pid);
182 #endif
183 }
184
185 }
186 }