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. */
7 #include "mc_memory_map.h"
8 #include "mc_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
12 "Logging specific to algorithms for memory_map");
14 memory_map_t MC_get_memory_map(void)
16 FILE *fp; /* File pointer to process's proc maps file */
17 char *line = NULL; /* Temporal storage for each line that is readed */
18 ssize_t read; /* Number of bytes readed */
19 size_t n = 0; /* Amount of bytes to read by xbt_getline */
20 memory_map_t ret = NULL; /* The memory map to return */
22 /* The following variables are used during the parsing of the file "maps" */
23 s_map_region_t memreg; /* temporal map region used for creating the map */
24 char *lfields[6], *tok, *endptr;
27 /* Open the actual process's proc maps file and create the memory_map_t */
29 fp = fopen("/proc/self/maps", "r");
32 perror("fopen failed");
35 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
39 ret = xbt_new0(s_memory_map_t, 1);
41 /* Read one line at the time, parse it and add it to the memory map to be returned */
42 while ((read = xbt_getline(&line, &n, fp)) != -1) {
44 //fprintf(stderr,"%s", line);
46 /* Wipeout the new line character */
47 line[read - 1] = '\0';
49 /* Tokenize the line using spaces as delimiters and store each token */
50 /* in lfields array. We expect 5 tokens/fields */
51 lfields[0] = strtok(line, " ");
53 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
54 lfields[i] = strtok(NULL, " ");
57 /* Check to see if we got the expected amount of columns */
61 /* Ok we are good enough to try to get the info we need */
62 /* First get the start and the end address of the map */
63 tok = strtok(lfields[0], "-");
67 memreg.start_addr = (void *) strtoul(tok, &endptr, 16);
68 /* Make sure that the entire string was an hex number */
72 tok = strtok(NULL, "-");
76 memreg.end_addr = (void *) strtoul(tok, &endptr, 16);
77 /* Make sure that the entire string was an hex number */
81 /* Get the permissions flags */
82 if (strlen(lfields[1]) < 4)
87 for (i = 0; i < 3; i++){
88 switch(lfields[1][i]){
90 memreg.prot |= PROT_READ;
93 memreg.prot |= PROT_WRITE;
96 memreg.prot |= PROT_EXEC;
102 if (memreg.prot == 0)
103 memreg.prot |= PROT_NONE;
105 if (lfields[1][4] == 'p')
106 memreg.flags |= MAP_PRIVATE;
108 else if (lfields[1][4] == 's')
109 memreg.flags |= MAP_SHARED;
111 /* Get the offset value */
112 memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
113 /* Make sure that the entire string was an hex number */
117 /* Get the device major:minor bytes */
118 tok = strtok(lfields[3], ":");
122 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
123 /* Make sure that the entire string was an hex number */
127 tok = strtok(NULL, ":");
131 memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
132 /* Make sure that the entire string was an hex number */
136 /* Get the inode number and make sure that the entire string was a long int */
137 memreg.inode = strtoul(lfields[4], &endptr, 10);
141 /* And finally get the pathname */
142 memreg.pathname = xbt_strdup(lfields[5]);
144 /* Create space for a new map region in the region's array and copy the */
145 /* parsed stuff from the temporal memreg variable */
147 xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
148 memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));
160 void MC_free_memory_map(memory_map_t map){
163 for(i=0; i< map->mapsize; i++){
164 xbt_free(map->regions[i].pathname);
166 xbt_free(map->regions);