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>
15 #include "mc_memory_map.h"
16 #include "mc_private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
21 "Logging specific to algorithms for memory_map");
28 XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
30 /* Open the actual process's proc maps file and create the memory_map_t */
32 char* path = bprintf("/proc/%i/maps", (int) pid);
33 FILE *fp = std::fopen(path, "r");
35 std::perror("fopen failed");
37 "Cannot open %s to investigate the memory map of the process.", path);
41 std::vector<VmMap> ret;
43 /* Read one line at the time, parse it and add it to the memory map to be returned */
44 ssize_t read; /* Number of bytes readed */
46 std::size_t n = 0; /* Amount of bytes to read by xbt_getline */
47 while ((read = xbt_getline(&line, &n, fp)) != -1) {
49 //fprintf(stderr,"%s", line);
51 /* Wipeout the new line character */
52 line[read - 1] = '\0';
54 /* Tokenize the line using spaces as delimiters and store each token */
55 /* in lfields array. We expect 5 tokens/fields */
57 lfields[0] = strtok(line, " ");
60 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
61 lfields[i] = std::strtok(NULL, " ");
64 /* Check to see if we got the expected amount of columns */
68 /* Ok we are good enough to try to get the info we need */
69 /* First get the start and the end address of the map */
70 char *tok = std::strtok(lfields[0], "-");
76 memreg.start_addr = std::strtoull(tok, &endptr, 16);
77 /* Make sure that the entire string was an hex number */
81 tok = std::strtok(NULL, "-");
85 memreg.end_addr = std::strtoull(tok, &endptr, 16);
86 /* Make sure that the entire string was an hex number */
90 /* Get the permissions flags */
91 if (std::strlen(lfields[1]) < 4)
96 for (i = 0; i < 3; i++){
97 switch(lfields[1][i]){
99 memreg.prot |= PROT_READ;
102 memreg.prot |= PROT_WRITE;
105 memreg.prot |= PROT_EXEC;
111 if (memreg.prot == 0)
112 memreg.prot |= PROT_NONE;
114 if (lfields[1][4] == 'p')
115 memreg.flags |= MAP_PRIVATE;
117 else if (lfields[1][4] == 's')
118 memreg.flags |= MAP_SHARED;
120 /* Get the offset value */
121 memreg.offset = std::strtoull(lfields[2], &endptr, 16);
122 /* Make sure that the entire string was an hex number */
126 /* Get the device major:minor bytes */
127 tok = std::strtok(lfields[3], ":");
131 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
132 /* Make sure that the entire string was an hex number */
136 tok = std::strtok(NULL, ":");
140 memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
141 /* Make sure that the entire string was an hex number */
145 /* Get the inode number and make sure that the entire string was a long int */
146 memreg.inode = strtoul(lfields[4], &endptr, 10);
150 /* And finally get the pathname */
152 memreg.pathname = lfields[5];
154 /* Create space for a new map region in the region's array and copy the */
155 /* parsed stuff from the temporal memreg variable */
156 XBT_DEBUG("Found region for %s",
157 !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
159 ret.push_back(std::move(memreg));
164 return std::move(ret);