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"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
17 "Logging specific to algorithms for memory_map");
19 memory_map_t MC_get_memory_map(pid_t pid)
21 /* Open the actual process's proc maps file and create the memory_map_t */
23 char* path = bprintf("/proc/%i/maps", (int) pid);
24 FILE *fp = fopen(path, "r");
27 perror("fopen failed");
29 "Cannot open %s to investigate the memory map of the process.", path);
32 memory_map_t ret = xbt_new0(s_memory_map_t, 1);
34 /* Read one line at the time, parse it and add it to the memory map to be returned */
35 ssize_t read; /* Number of bytes readed */
37 size_t n = 0; /* Amount of bytes to read by xbt_getline */
38 while ((read = xbt_getline(&line, &n, fp)) != -1) {
40 //fprintf(stderr,"%s", line);
42 /* Wipeout the new line character */
43 line[read - 1] = '\0';
45 /* Tokenize the line using spaces as delimiters and store each token */
46 /* in lfields array. We expect 5 tokens/fields */
48 lfields[0] = strtok(line, " ");
51 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
52 lfields[i] = strtok(NULL, " ");
55 /* Check to see if we got the expected amount of columns */
59 /* Ok we are good enough to try to get the info we need */
60 /* First get the start and the end address of the map */
61 char *tok = strtok(lfields[0], "-");
65 s_map_region_t memreg; /* temporal map region used for creating the map */
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 */
146 XBT_DEBUG("Found region for %s",
147 memreg.pathname ? memreg.pathname : "(null)");
148 ret->regions = (map_region_t)
149 xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
150 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);