1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include <sys/types.h>
16 # include <mach/mach_init.h>
17 # include <mach/mach_traps.h>
18 # include <mach/mach_port.h>
19 # include <mach/mach_vm.h>
20 # include <sys/mman.h>
21 # include <sys/param.h>
23 # if __MAC_OS_X_VERSION_MIN_REQUIRED < 1050
24 # define mach_vm_address_t vm_address_t
25 # define mach_vm_size_t vm_size_t
26 # if defined __ppc64__ || defined __x86_64__
27 # define mach_vm_region vm_region64
29 # define mach_vm_region vm_region
35 # include <sys/mman.h>
38 #if defined __FreeBSD__
39 # include <sys/types.h>
40 # include <sys/mman.h>
41 # include <sys/param.h>
42 # include <sys/queue.h>
43 # include <sys/socket.h>
44 # include <sys/sysctl.h>
45 # include <sys/user.h>
46 # include <libprocstat.h>
49 #include <xbt/sysdep.h>
53 #include "memory_map.hpp"
56 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_memory_map, xbt, "Logging specific to algorithms for memory_map");
63 * \todo This function contains many cases that do not allow for a
64 * recovery. Currently, xbt_abort() is called but we should
65 * much rather die with the specific reason so that it's easier
66 * to find out what's going on.
68 XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
70 std::vector<VmMap> ret;
74 /* Request authorization to read mappings */
75 if (task_for_pid(mach_task_self(), pid, &map) != KERN_SUCCESS) {
76 std::perror("task_for_pid failed");
77 xbt_die("Cannot request authorization for kernel information access");
81 * Darwin do not give us the number of mappings, so we read entries until
82 * we get an KERN_INVALID_ADDRESS return.
84 mach_vm_address_t address = VM_MIN_ADDRESS;
87 memory_object_name_t object;
89 #if defined __ppc64__ || defined __x86_64__
90 vm_region_flavor_t flavor = VM_REGION_BASIC_INFO_64;
91 struct vm_region_basic_info_64 info;
92 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
94 vm_region_flavor_t flavor = VM_REGION_BASIC_INFO;
95 struct vm_region_basic_info info;
96 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
105 (vm_region_info_t)&info,
108 if (kr == KERN_INVALID_ADDRESS) {
111 else if (kr != KERN_SUCCESS) {
112 std::perror("mach_vm_region failed");
113 xbt_die("Cannot request authorization for kernel information access");
119 memreg.start_addr = address;
120 memreg.end_addr = address + size;
123 memreg.prot = PROT_NONE;
124 if (info.protection & VM_PROT_READ)
125 memreg.prot |= PROT_READ;
126 if (info.protection & VM_PROT_WRITE)
127 memreg.prot |= PROT_WRITE;
128 if (info.protection & VM_PROT_EXECUTE)
129 memreg.prot |= PROT_EXEC;
131 /* Private (copy-on-write) or shared? */
134 memreg.flags |= MAP_SHARED;
136 memreg.flags |= MAP_PRIVATE;
139 memreg.offset = info.offset;
141 /* Device : not sure this can be mapped to something outside of Linux? */
142 memreg.dev_major = 0;
143 memreg.dev_minor = 0;
149 char path[MAXPATHLEN];
151 pathlen = proc_regionfilename(pid, address, path, sizeof(path));
152 path[pathlen] = '\0';
153 memreg.pathname = path;
155 #if 0 /* Display mappings for debug */
157 "%#014llx - %#014llx | %c%c%c | %s\n",
158 memreg.start_addr, memreg.end_addr,
159 (memreg.prot & PROT_READ) ? 'r' : '-',
160 (memreg.prot & PROT_WRITE) ? 'w' : '-',
161 (memreg.prot & PROT_EXEC) ? 'x' : '-',
162 memreg.pathname.c_str());
165 ret.push_back(std::move(memreg));
169 mach_port_deallocate(mach_task_self(), map);
170 #elif defined __linux__
171 /* Open the actual process's proc maps file and create the memory_map_t */
172 /* to be returned. */
173 std::string path = std::string("/proc/") + std::to_string(pid) + "/maps";
175 fp.rdbuf()->pubsetbuf(0, 0);
178 std::perror("open failed");
179 xbt_die("Cannot open %s to investigate the memory map of the process.", path.c_str());
182 /* Read one line at the time, parse it and add it to the memory map to be returned */
184 while (std::getline(fp, sline)) {
186 * The lines that we read have this format: (This is just an example)
187 * 00602000-00603000 rw-p 00002000 00:28 1837264 <complete-path-to-file>
189 char* line = &sline[0];
191 /* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens for 6 fields */
192 char* saveptr = nullptr; // for strtok_r()
194 lfields[0] = strtok_r(line, " ", &saveptr);
197 for (i = 1; i < 6 && lfields[i - 1] != nullptr; i++) {
198 lfields[i] = strtok_r(nullptr, " ", &saveptr);
201 /* Check to see if we got the expected amount of columns */
203 xbt_die("The memory map apparently only supplied less than 6 columns. Recovery impossible.");
205 /* Ok we are good enough to try to get the info we need */
206 /* First get the start and the end address of the map */
207 char* tok = strtok_r(lfields[0], "-", &saveptr);
209 xbt_die("Start and end address of the map are not concatenated by a hyphen (-). Recovery impossible.");
213 memreg.start_addr = std::strtoull(tok, &endptr, 16);
214 /* Make sure that the entire string was an hex number */
218 tok = strtok_r(nullptr, "-", &saveptr);
222 memreg.end_addr = std::strtoull(tok, &endptr, 16);
223 /* Make sure that the entire string was an hex number */
227 /* Get the permissions flags */
228 if (std::strlen(lfields[1]) < 4)
232 for (i = 0; i < 3; i++){
233 switch(lfields[1][i]){
235 memreg.prot |= PROT_READ;
238 memreg.prot |= PROT_WRITE;
241 memreg.prot |= PROT_EXEC;
247 if (memreg.prot == 0)
248 memreg.prot |= PROT_NONE;
251 if (lfields[1][3] == 'p') {
252 memreg.flags |= MAP_PRIVATE;
254 memreg.flags |= MAP_SHARED;
255 if (lfields[1][3] != 's')
256 XBT_WARN("The protection is neither 'p' (private) nor 's' (shared) but '%s'. Let's assume shared, as on b0rken "
257 "win-ubuntu systems.\nFull line: %s\n",
261 /* Get the offset value */
262 memreg.offset = std::strtoull(lfields[2], &endptr, 16);
263 /* Make sure that the entire string was an hex number */
267 /* Get the device major:minor bytes */
268 tok = strtok_r(lfields[3], ":", &saveptr);
272 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
273 /* Make sure that the entire string was an hex number */
277 tok = strtok_r(nullptr, ":", &saveptr);
281 memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
282 /* Make sure that the entire string was an hex number */
286 /* Get the inode number and make sure that the entire string was a long int */
287 memreg.inode = strtoul(lfields[4], &endptr, 10);
291 /* And finally get the pathname */
293 memreg.pathname = lfields[5];
295 /* Create space for a new map region in the region's array and copy the */
296 /* parsed stuff from the temporal memreg variable */
297 XBT_DEBUG("Found region for %s", not memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
299 ret.push_back(std::move(memreg));
303 #elif defined __FreeBSD__
304 struct procstat *prstat;
305 struct kinfo_proc *proc;
306 struct kinfo_vmentry *vmentries;
309 if ((prstat = procstat_open_sysctl()) == NULL) {
310 std::perror("procstat_open_sysctl failed");
311 xbt_die("Cannot access kernel state information");
313 if ((proc = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt)) == NULL) {
314 std::perror("procstat_open_sysctl failed");
315 xbt_die("Cannot access process information");
317 if ((vmentries = procstat_getvmmap(prstat, proc, &cnt)) == NULL) {
318 std::perror("procstat_getvmmap failed");
319 xbt_die("Cannot access process memory mappings");
321 for (unsigned int i = 0; i < cnt; i++) {
325 memreg.start_addr = vmentries[i].kve_start;
326 memreg.end_addr = vmentries[i].kve_end;
329 memreg.prot = PROT_NONE;
330 if (vmentries[i].kve_protection & KVME_PROT_READ)
331 memreg.prot |= PROT_READ;
332 if (vmentries[i].kve_protection & KVME_PROT_WRITE)
333 memreg.prot |= PROT_WRITE;
334 if (vmentries[i].kve_protection & KVME_PROT_EXEC)
335 memreg.prot |= PROT_EXEC;
337 /* Private (copy-on-write) or shared? */
339 if (vmentries[i].kve_flags & KVME_FLAG_COW)
340 memreg.flags |= MAP_PRIVATE;
342 memreg.flags |= MAP_SHARED;
345 memreg.offset = vmentries[i].kve_offset;
347 /* Device : not sure this can be mapped to something outside of Linux? */
348 memreg.dev_major = 0;
349 memreg.dev_minor = 0;
352 memreg.inode = vmentries[i].kve_vn_fileid;
355 * Path. Linuxize result by giving an anonymous mapping a path from
356 * the previous mapping, provided previous is vnode and has a path,
357 * and mark the stack.
359 if (vmentries[i].kve_path[0] != '\0')
360 memreg.pathname = vmentries[i].kve_path;
361 else if (vmentries[i].kve_type == KVME_TYPE_DEFAULT && vmentries[i - 1].kve_type == KVME_TYPE_VNODE &&
362 vmentries[i - 1].kve_path[0] != '\0')
363 memreg.pathname = vmentries[i-1].kve_path;
364 else if (vmentries[i].kve_type == KVME_TYPE_DEFAULT
365 && vmentries[i].kve_flags & KVME_FLAG_GROWS_DOWN)
366 memreg.pathname = "[stack]";
369 * One last dirty modification: remove write permission from shared
370 * libraries private clean pages. This is necessary because simgrid
371 * later identifies mappings based on the permissions that are expected
372 * when running the Linux kernel.
374 if (vmentries[i].kve_type == KVME_TYPE_VNODE && not(vmentries[i].kve_flags & KVME_FLAG_NEEDS_COPY))
375 memreg.prot &= ~PROT_WRITE;
377 ret.push_back(std::move(memreg));
379 procstat_freevmmap(prstat, vmentries);
380 procstat_freeprocs(prstat, proc);
381 procstat_close(prstat);
383 xbt_die("Could not get memory map from process %lli", (long long int) pid);