1 /* Copyright (c) 2008-2014. 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 "mc_memory_map.h"
12 #include "mc_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
15 "Logging specific to algorithms for memory_map");
17 memory_map_t MC_get_memory_map(pid_t pid)
19 /* Open the actual process's proc maps file and create the memory_map_t */
21 char* path = bprintf("/proc/%i/maps", (int) pid);
22 FILE *fp = fopen(path, "r");
25 perror("fopen failed");
27 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
30 memory_map_t ret = xbt_new0(s_memory_map_t, 1);
32 /* Read one line at the time, parse it and add it to the memory map to be returned */
33 ssize_t read; /* Number of bytes readed */
35 size_t n = 0; /* Amount of bytes to read by xbt_getline */
36 while ((read = xbt_getline(&line, &n, fp)) != -1) {
38 //fprintf(stderr,"%s", line);
40 /* Wipeout the new line character */
41 line[read - 1] = '\0';
43 /* Tokenize the line using spaces as delimiters and store each token */
44 /* in lfields array. We expect 5 tokens/fields */
46 lfields[0] = strtok(line, " ");
49 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
50 lfields[i] = strtok(NULL, " ");
53 /* Check to see if we got the expected amount of columns */
57 /* Ok we are good enough to try to get the info we need */
58 /* First get the start and the end address of the map */
59 char *tok = strtok(lfields[0], "-");
63 s_map_region_t memreg; /* temporal map region used for creating the map */
65 memreg.start_addr = (void *) strtoul(tok, &endptr, 16);
66 /* Make sure that the entire string was an hex number */
70 tok = strtok(NULL, "-");
74 memreg.end_addr = (void *) strtoul(tok, &endptr, 16);
75 /* Make sure that the entire string was an hex number */
79 /* Get the permissions flags */
80 if (strlen(lfields[1]) < 4)
85 for (i = 0; i < 3; i++){
86 switch(lfields[1][i]){
88 memreg.prot |= PROT_READ;
91 memreg.prot |= PROT_WRITE;
94 memreg.prot |= PROT_EXEC;
100 if (memreg.prot == 0)
101 memreg.prot |= PROT_NONE;
103 if (lfields[1][4] == 'p')
104 memreg.flags |= MAP_PRIVATE;
106 else if (lfields[1][4] == 's')
107 memreg.flags |= MAP_SHARED;
109 /* Get the offset value */
110 memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
111 /* Make sure that the entire string was an hex number */
115 /* Get the device major:minor bytes */
116 tok = strtok(lfields[3], ":");
120 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
121 /* Make sure that the entire string was an hex number */
125 tok = strtok(NULL, ":");
129 memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
130 /* Make sure that the entire string was an hex number */
134 /* Get the inode number and make sure that the entire string was a long int */
135 memreg.inode = strtoul(lfields[4], &endptr, 10);
139 /* And finally get the pathname */
140 memreg.pathname = xbt_strdup(lfields[5]);
142 /* Create space for a new map region in the region's array and copy the */
143 /* parsed stuff from the temporal memreg variable */
145 xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
146 memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));
156 void MC_free_memory_map(memory_map_t map){
159 for(i=0; i< map->mapsize; i++){
160 xbt_free(map->regions[i].pathname);
162 xbt_free(map->regions);