1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include <sys/types.h>
13 # include <sys/mman.h>
14 #elif defined __FreeBSD__
15 # include <sys/types.h>
16 # include <sys/mman.h>
17 # include <sys/param.h>
18 # include <sys/queue.h>
19 # include <sys/socket.h>
20 # include <sys/sysctl.h>
21 # include <sys/user.h>
22 # include <libprocstat.h>
25 #include <xbt/sysdep.h>
30 #include "memory_map.hpp"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_memory_map, xbt, "Logging specific to algorithms for memory_map");
40 * \todo This function contains many cases that do not allow for a
41 * recovery. Currently, xbt_abort() is called but we should
42 * much rather die with the specific reason so that it's easier
43 * to find out what's going on.
45 XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
47 std::vector<VmMap> ret;
49 /* Open the actual process's proc maps file and create the memory_map_t */
51 char* path = bprintf("/proc/%i/maps", (int) pid);
52 FILE *fp = std::fopen(path, "r");
54 std::perror("fopen failed");
55 xbt_die("Cannot open %s to investigate the memory map of the process.", path);
60 /* Read one line at the time, parse it and add it to the memory map to be returned */
61 ssize_t read; /* Number of bytes readed */
63 std::size_t n = 0; /* Amount of bytes to read by xbt_getline */
64 while ((read = xbt_getline(&line, &n, fp)) != -1) {
66 * The lines that we read have this format: (This is just an example)
67 * 00602000-00603000 rw-p 00002000 00:28 1837264 <complete-path-to-file>
70 //fprintf(stderr,"%s", line);
72 /* Wipeout the new line character */
73 line[read - 1] = '\0';
75 /* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens for 6 fields */
77 lfields[0] = strtok(line, " ");
80 for (i = 1; i < 6 && lfields[i - 1] != nullptr; i++) {
81 lfields[i] = std::strtok(nullptr, " ");
84 /* Check to see if we got the expected amount of columns */
86 xbt_die("The memory map apparently only supplied less than 6 columns. Recovery impossible.");
88 /* Ok we are good enough to try to get the info we need */
89 /* First get the start and the end address of the map */
90 char *tok = std::strtok(lfields[0], "-");
92 xbt_die("Start and end address of the map are not concatenated by a hyphen (-). Recovery impossible.");
96 memreg.start_addr = std::strtoull(tok, &endptr, 16);
97 /* Make sure that the entire string was an hex number */
101 tok = std::strtok(nullptr, "-");
105 memreg.end_addr = std::strtoull(tok, &endptr, 16);
106 /* Make sure that the entire string was an hex number */
110 /* Get the permissions flags */
111 if (std::strlen(lfields[1]) < 4)
116 for (i = 0; i < 3; i++){
117 switch(lfields[1][i]){
119 memreg.prot |= PROT_READ;
122 memreg.prot |= PROT_WRITE;
125 memreg.prot |= PROT_EXEC;
131 if (memreg.prot == 0)
132 memreg.prot |= PROT_NONE;
134 if (lfields[1][3] == 'p') {
135 memreg.flags |= MAP_PRIVATE;
137 memreg.flags |= MAP_SHARED;
138 if (lfields[1][3] != 's')
139 XBT_WARN("The protection is neither 'p' (private) nor 's' (shared) but '%s'. Let's assume shared, as on b0rken win-ubuntu systems.\nFull line: %s\n",
143 /* Get the offset value */
144 memreg.offset = std::strtoull(lfields[2], &endptr, 16);
145 /* Make sure that the entire string was an hex number */
149 /* Get the device major:minor bytes */
150 tok = std::strtok(lfields[3], ":");
154 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
155 /* Make sure that the entire string was an hex number */
159 tok = std::strtok(nullptr, ":");
163 memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
164 /* Make sure that the entire string was an hex number */
168 /* Get the inode number and make sure that the entire string was a long int */
169 memreg.inode = strtoul(lfields[4], &endptr, 10);
173 /* And finally get the pathname */
175 memreg.pathname = lfields[5];
177 /* Create space for a new map region in the region's array and copy the */
178 /* parsed stuff from the temporal memreg variable */
179 XBT_DEBUG("Found region for %s", !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
181 ret.push_back(std::move(memreg));
186 #elif defined __FreeBSD__
187 struct procstat *prstat;
188 struct kinfo_proc *proc;
189 struct kinfo_vmentry *vmentries;
192 if ((prstat = procstat_open_sysctl()) == NULL) {
193 std::perror("procstat_open_sysctl failed");
194 xbt_die("Cannot access kernel state information");
196 if ((proc = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt)) == NULL) {
197 std::perror("procstat_open_sysctl failed");
198 xbt_die("Cannot access process information");
200 if ((vmentries = procstat_getvmmap(prstat, proc, &cnt)) == NULL) {
201 std::perror("procstat_getvmmap failed");
202 xbt_die("Cannot access process memory mappings");
204 for (unsigned int i = 0; i < cnt; i++) {
208 memreg.start_addr = vmentries[i].kve_start;
209 memreg.end_addr = vmentries[i].kve_end;
212 memreg.prot = PROT_NONE;
213 if (vmentries[i].kve_protection & KVME_PROT_READ)
214 memreg.prot |= PROT_READ;
215 if (vmentries[i].kve_protection & KVME_PROT_WRITE)
216 memreg.prot |= PROT_WRITE;
217 if (vmentries[i].kve_protection & KVME_PROT_EXEC)
218 memreg.prot |= PROT_EXEC;
220 /* Private (copy-on-write) or shared? */
221 if (vmentries[i].kve_flags & KVME_FLAG_COW)
222 memreg.flags |= MAP_PRIVATE;
224 memreg.flags |= MAP_SHARED;
227 memreg.offset = vmentries[i].kve_offset;
229 /* Device : not sure this can be mapped to something outside of Linux? */
230 memreg.dev_major = 0;
231 memreg.dev_minor = 0;
234 memreg.inode = vmentries[i].kve_vn_fileid;
237 * Path. Linuxize result by giving an anonymous mapping a path from
238 * the previous mapping, provided previous is vnode and has a path,
239 * and mark the stack.
241 if (vmentries[i].kve_path[0] != '\0')
242 memreg.pathname = vmentries[i].kve_path;
243 else if (vmentries[i].kve_type == KVME_TYPE_DEFAULT
244 && vmentries[i-1].kve_type == KVME_TYPE_VNODE
245 && vmentries[i-1].kve_path[0] != '\0')
246 memreg.pathname = vmentries[i-1].kve_path;
247 else if (vmentries[i].kve_type == KVME_TYPE_DEFAULT
248 && vmentries[i].kve_flags & KVME_FLAG_GROWS_DOWN)
249 memreg.pathname = "[stack]";
252 * One last dirty modification: remove write permission from shared
253 * libraries private clean pages. This is necessary because simgrid
254 * later identifies mappings based on the permissions that are expected
255 * when running the Linux kernel.
257 if (vmentries[i].kve_type == KVME_TYPE_VNODE
258 && ! (vmentries[i].kve_flags & KVME_FLAG_NEEDS_COPY))
259 memreg.prot &= ~PROT_WRITE;
261 ret.push_back(std::move(memreg));
263 procstat_freevmmap(prstat, vmentries);
264 procstat_freeprocs(prstat, proc);
265 procstat_close(prstat);
267 xbt_die("Could not get memory map from process %lli", (long long int) pid);