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 <xbt/sysdep.h>
19 #include "memory_map.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_memory_map, xbt,
24 "Logging specific to algorithms for memory_map");
31 XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
34 /* Open the actual process's proc maps file and create the memory_map_t */
36 char* path = bprintf("/proc/%i/maps", (int) pid);
37 FILE *fp = std::fopen(path, "r");
39 std::perror("fopen failed");
41 "Cannot open %s to investigate the memory map of the process.", path);
45 std::vector<VmMap> ret;
47 /* Read one line at the time, parse it and add it to the memory map to be returned */
48 ssize_t read; /* Number of bytes readed */
50 std::size_t n = 0; /* Amount of bytes to read by xbt_getline */
51 while ((read = xbt_getline(&line, &n, fp)) != -1) {
53 //fprintf(stderr,"%s", line);
55 /* Wipeout the new line character */
56 line[read - 1] = '\0';
58 /* Tokenize the line using spaces as delimiters and store each token */
59 /* in lfields array. We expect 5 tokens/fields */
61 lfields[0] = strtok(line, " ");
64 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
65 lfields[i] = std::strtok(NULL, " ");
68 /* Check to see if we got the expected amount of columns */
72 /* Ok we are good enough to try to get the info we need */
73 /* First get the start and the end address of the map */
74 char *tok = std::strtok(lfields[0], "-");
80 memreg.start_addr = std::strtoull(tok, &endptr, 16);
81 /* Make sure that the entire string was an hex number */
85 tok = std::strtok(NULL, "-");
89 memreg.end_addr = std::strtoull(tok, &endptr, 16);
90 /* Make sure that the entire string was an hex number */
94 /* Get the permissions flags */
95 if (std::strlen(lfields[1]) < 4)
100 for (i = 0; i < 3; i++){
101 switch(lfields[1][i]){
103 memreg.prot |= PROT_READ;
106 memreg.prot |= PROT_WRITE;
109 memreg.prot |= PROT_EXEC;
115 if (memreg.prot == 0)
116 memreg.prot |= PROT_NONE;
118 if (lfields[1][4] == 'p')
119 memreg.flags |= MAP_PRIVATE;
121 else if (lfields[1][4] == 's')
122 memreg.flags |= MAP_SHARED;
124 /* Get the offset value */
125 memreg.offset = std::strtoull(lfields[2], &endptr, 16);
126 /* Make sure that the entire string was an hex number */
130 /* Get the device major:minor bytes */
131 tok = std::strtok(lfields[3], ":");
135 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
136 /* Make sure that the entire string was an hex number */
140 tok = std::strtok(NULL, ":");
144 memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
145 /* Make sure that the entire string was an hex number */
149 /* Get the inode number and make sure that the entire string was a long int */
150 memreg.inode = strtoul(lfields[4], &endptr, 10);
154 /* And finally get the pathname */
156 memreg.pathname = lfields[5];
158 /* Create space for a new map region in the region's array and copy the */
159 /* parsed stuff from the temporal memreg variable */
160 XBT_DEBUG("Found region for %s",
161 !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
163 ret.push_back(std::move(memreg));
168 return std::move(ret);
170 /* On FreeBSD, kinfo_getvmmap() could be used but mmap() support is disabled
172 xbt_die("Could not get memory map from process %lli", (long long int) pid);