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>
14 # include <mach/mach_init.h>
15 # include <mach/mach_traps.h>
16 # include <mach/mach_port.h>
17 # include <mach/mach_vm.h>
18 # include <sys/mman.h>
19 # include <sys/param.h>
21 # if __MAC_OS_X_VERSION_MIN_REQUIRED < 1050
22 # define mach_vm_address_t vm_address_t
23 # define mach_vm_size_t vm_size_t
24 # if defined __ppc64__ || defined __x86_64__
25 # define mach_vm_region vm_region64
27 # define mach_vm_region vm_region
33 # include <sys/mman.h>
36 #if defined __FreeBSD__
37 # include <sys/types.h>
38 # include <sys/mman.h>
39 # include <sys/param.h>
40 # include <sys/queue.h>
41 # include <sys/socket.h>
42 # include <sys/sysctl.h>
43 # include <sys/user.h>
44 # include <libprocstat.h>
47 #include <xbt/sysdep.h>
52 #include "memory_map.hpp"
55 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_memory_map, xbt, "Logging specific to algorithms for memory_map");
62 * \todo This function contains many cases that do not allow for a
63 * recovery. Currently, xbt_abort() is called but we should
64 * much rather die with the specific reason so that it's easier
65 * to find out what's going on.
67 XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
69 std::vector<VmMap> ret;
73 /* Request authorization to read mappings */
74 if (task_for_pid(mach_task_self(), pid, &map) != KERN_SUCCESS) {
75 std::perror("task_for_pid failed");
76 xbt_die("Cannot request authorization for kernel information access");
80 * Darwin do not give us the number of mappings, so we read entries until
81 * we get an KERN_INVALID_ADDRESS return.
83 mach_vm_address_t address = VM_MIN_ADDRESS;
86 memory_object_name_t object;
88 #if defined __ppc64__ || defined __x86_64__
89 vm_region_flavor_t flavor = VM_REGION_BASIC_INFO_64;
90 struct vm_region_basic_info_64 info;
91 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
93 vm_region_flavor_t flavor = VM_REGION_BASIC_INFO;
94 struct vm_region_basic_info info;
95 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
104 (vm_region_info_t)&info,
107 if (kr == KERN_INVALID_ADDRESS) {
110 else if (kr != KERN_SUCCESS) {
111 std::perror("mach_vm_region failed");
112 xbt_die("Cannot request authorization for kernel information access");
118 memreg.start_addr = address;
119 memreg.end_addr = address + size - 1;
122 memreg.prot = PROT_NONE;
123 if (info.protection & VM_PROT_READ)
124 memreg.prot |= PROT_READ;
125 if (info.protection & VM_PROT_WRITE)
126 memreg.prot |= PROT_WRITE;
127 if (info.protection & VM_PROT_EXECUTE)
128 memreg.prot |= PROT_EXEC;
130 /* Private (copy-on-write) or shared? */
132 memreg.flags |= MAP_SHARED;
134 memreg.flags |= MAP_PRIVATE;
137 memreg.offset = info.offset;
139 /* Device : not sure this can be mapped to something outside of Linux? */
140 memreg.dev_major = 0;
141 memreg.dev_minor = 0;
147 char path[MAXPATHLEN];
149 pathlen = proc_regionfilename(pid, address, path, sizeof(path));
150 path[pathlen] = '\0';
151 memreg.pathname = path;
153 #if 0 /* Display mappings for debug */
155 "%#014llx - %#014llx | %c%c%c | %s\n",
156 memreg.start_addr, memreg.end_addr,
157 (memreg.prot & PROT_READ) ? 'r' : '-',
158 (memreg.prot & PROT_WRITE) ? 'w' : '-',
159 (memreg.prot & PROT_EXEC) ? 'x' : '-',
160 memreg.pathname.c_str());
163 ret.push_back(std::move(memreg));
167 mach_port_deallocate(mach_task_self(), map);
168 #elif defined __linux__
169 /* Open the actual process's proc maps file and create the memory_map_t */
170 /* to be returned. */
171 char* path = bprintf("/proc/%i/maps", (int) pid);
172 FILE *fp = std::fopen(path, "r");
174 std::perror("fopen failed");
175 xbt_die("Cannot open %s to investigate the memory map of the process.", path);
180 /* Read one line at the time, parse it and add it to the memory map to be returned */
181 ssize_t read; /* Number of bytes readed */
182 char* line = nullptr;
183 std::size_t n = 0; /* Amount of bytes to read by xbt_getline */
184 while ((read = xbt_getline(&line, &n, fp)) != -1) {
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>
190 //fprintf(stderr,"%s", line);
192 /* Wipeout the new line character */
193 line[read - 1] = '\0';
195 /* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens for 6 fields */
197 lfields[0] = strtok(line, " ");
200 for (i = 1; i < 6 && lfields[i - 1] != nullptr; i++) {
201 lfields[i] = std::strtok(nullptr, " ");
204 /* Check to see if we got the expected amount of columns */
206 xbt_die("The memory map apparently only supplied less than 6 columns. Recovery impossible.");
208 /* Ok we are good enough to try to get the info we need */
209 /* First get the start and the end address of the map */
210 char *tok = std::strtok(lfields[0], "-");
212 xbt_die("Start and end address of the map are not concatenated by a hyphen (-). Recovery impossible.");
216 memreg.start_addr = std::strtoull(tok, &endptr, 16);
217 /* Make sure that the entire string was an hex number */
221 tok = std::strtok(nullptr, "-");
225 memreg.end_addr = std::strtoull(tok, &endptr, 16);
226 /* Make sure that the entire string was an hex number */
230 /* Get the permissions flags */
231 if (std::strlen(lfields[1]) < 4)
236 for (i = 0; i < 3; i++){
237 switch(lfields[1][i]){
239 memreg.prot |= PROT_READ;
242 memreg.prot |= PROT_WRITE;
245 memreg.prot |= PROT_EXEC;
251 if (memreg.prot == 0)
252 memreg.prot |= PROT_NONE;
254 if (lfields[1][3] == 'p') {
255 memreg.flags |= MAP_PRIVATE;
257 memreg.flags |= MAP_SHARED;
258 if (lfields[1][3] != 's')
259 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",
263 /* Get the offset value */
264 memreg.offset = std::strtoull(lfields[2], &endptr, 16);
265 /* Make sure that the entire string was an hex number */
269 /* Get the device major:minor bytes */
270 tok = std::strtok(lfields[3], ":");
274 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
275 /* Make sure that the entire string was an hex number */
279 tok = std::strtok(nullptr, ":");
283 memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
284 /* Make sure that the entire string was an hex number */
288 /* Get the inode number and make sure that the entire string was a long int */
289 memreg.inode = strtoul(lfields[4], &endptr, 10);
293 /* And finally get the pathname */
295 memreg.pathname = lfields[5];
297 /* Create space for a new map region in the region's array and copy the */
298 /* parsed stuff from the temporal memreg variable */
299 XBT_DEBUG("Found region for %s", !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
301 ret.push_back(std::move(memreg));
306 #elif defined __FreeBSD__
307 struct procstat *prstat;
308 struct kinfo_proc *proc;
309 struct kinfo_vmentry *vmentries;
312 if ((prstat = procstat_open_sysctl()) == NULL) {
313 std::perror("procstat_open_sysctl failed");
314 xbt_die("Cannot access kernel state information");
316 if ((proc = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt)) == NULL) {
317 std::perror("procstat_open_sysctl failed");
318 xbt_die("Cannot access process information");
320 if ((vmentries = procstat_getvmmap(prstat, proc, &cnt)) == NULL) {
321 std::perror("procstat_getvmmap failed");
322 xbt_die("Cannot access process memory mappings");
324 for (unsigned int i = 0; i < cnt; i++) {
328 memreg.start_addr = vmentries[i].kve_start;
329 memreg.end_addr = vmentries[i].kve_end;
332 memreg.prot = PROT_NONE;
333 if (vmentries[i].kve_protection & KVME_PROT_READ)
334 memreg.prot |= PROT_READ;
335 if (vmentries[i].kve_protection & KVME_PROT_WRITE)
336 memreg.prot |= PROT_WRITE;
337 if (vmentries[i].kve_protection & KVME_PROT_EXEC)
338 memreg.prot |= PROT_EXEC;
340 /* Private (copy-on-write) or shared? */
341 if (vmentries[i].kve_flags & KVME_FLAG_COW)
342 memreg.flags |= MAP_PRIVATE;
344 memreg.flags |= MAP_SHARED;
347 memreg.offset = vmentries[i].kve_offset;
349 /* Device : not sure this can be mapped to something outside of Linux? */
350 memreg.dev_major = 0;
351 memreg.dev_minor = 0;
354 memreg.inode = vmentries[i].kve_vn_fileid;
357 * Path. Linuxize result by giving an anonymous mapping a path from
358 * the previous mapping, provided previous is vnode and has a path,
359 * and mark the stack.
361 if (vmentries[i].kve_path[0] != '\0')
362 memreg.pathname = vmentries[i].kve_path;
363 else if (vmentries[i].kve_type == KVME_TYPE_DEFAULT
364 && vmentries[i-1].kve_type == KVME_TYPE_VNODE
365 && vmentries[i-1].kve_path[0] != '\0')
366 memreg.pathname = vmentries[i-1].kve_path;
367 else if (vmentries[i].kve_type == KVME_TYPE_DEFAULT
368 && vmentries[i].kve_flags & KVME_FLAG_GROWS_DOWN)
369 memreg.pathname = "[stack]";
372 * One last dirty modification: remove write permission from shared
373 * libraries private clean pages. This is necessary because simgrid
374 * later identifies mappings based on the permissions that are expected
375 * when running the Linux kernel.
377 if (vmentries[i].kve_type == KVME_TYPE_VNODE
378 && ! (vmentries[i].kve_flags & KVME_FLAG_NEEDS_COPY))
379 memreg.prot &= ~PROT_WRITE;
381 ret.push_back(std::move(memreg));
383 procstat_freevmmap(prstat, vmentries);
384 procstat_freeprocs(prstat, proc);
385 procstat_close(prstat);
387 xbt_die("Could not get memory map from process %lli", (long long int) pid);