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 "mc_memory_map.h"
14 #include "mc_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
19 "Logging specific to algorithms for memory_map");
26 std::vector<VmMap> get_memory_map(pid_t pid)
28 /* Open the actual process's proc maps file and create the memory_map_t */
30 char* path = bprintf("/proc/%i/maps", (int) pid);
31 FILE *fp = std::fopen(path, "r");
33 std::perror("fopen failed");
35 "Cannot open %s to investigate the memory map of the process.", path);
39 std::vector<VmMap> ret;
41 /* Read one line at the time, parse it and add it to the memory map to be returned */
42 ssize_t read; /* Number of bytes readed */
44 std::size_t n = 0; /* Amount of bytes to read by xbt_getline */
45 while ((read = xbt_getline(&line, &n, fp)) != -1) {
47 //fprintf(stderr,"%s", line);
49 /* Wipeout the new line character */
50 line[read - 1] = '\0';
52 /* Tokenize the line using spaces as delimiters and store each token */
53 /* in lfields array. We expect 5 tokens/fields */
55 lfields[0] = strtok(line, " ");
58 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
59 lfields[i] = std::strtok(NULL, " ");
62 /* Check to see if we got the expected amount of columns */
66 /* Ok we are good enough to try to get the info we need */
67 /* First get the start and the end address of the map */
68 char *tok = std::strtok(lfields[0], "-");
74 memreg.start_addr = std::strtoull(tok, &endptr, 16);
75 /* Make sure that the entire string was an hex number */
79 tok = std::strtok(NULL, "-");
83 memreg.end_addr = std::strtoull(tok, &endptr, 16);
84 /* Make sure that the entire string was an hex number */
88 /* Get the permissions flags */
89 if (std::strlen(lfields[1]) < 4)
94 for (i = 0; i < 3; i++){
95 switch(lfields[1][i]){
97 memreg.prot |= PROT_READ;
100 memreg.prot |= PROT_WRITE;
103 memreg.prot |= PROT_EXEC;
109 if (memreg.prot == 0)
110 memreg.prot |= PROT_NONE;
112 if (lfields[1][4] == 'p')
113 memreg.flags |= MAP_PRIVATE;
115 else if (lfields[1][4] == 's')
116 memreg.flags |= MAP_SHARED;
118 /* Get the offset value */
119 memreg.offset = std::strtoull(lfields[2], &endptr, 16);
120 /* Make sure that the entire string was an hex number */
124 /* Get the device major:minor bytes */
125 tok = std::strtok(lfields[3], ":");
129 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
130 /* Make sure that the entire string was an hex number */
134 tok = std::strtok(NULL, ":");
138 memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
139 /* Make sure that the entire string was an hex number */
143 /* Get the inode number and make sure that the entire string was a long int */
144 memreg.inode = strtoul(lfields[4], &endptr, 10);
148 /* And finally get the pathname */
150 memreg.pathname = lfields[5];
152 /* Create space for a new map region in the region's array and copy the */
153 /* parsed stuff from the temporal memreg variable */
154 XBT_DEBUG("Found region for %s",
155 !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
157 ret.push_back(std::move(memreg));
162 return std::move(ret);