5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_memory_map, mc,
6 "Logging specific to algorithms for memory_map");
8 memory_map_t get_memory_map(void)
10 FILE *fp; /* File pointer to process's proc maps file */
11 char *line = NULL; /* Temporal storage for each line that is readed */
12 ssize_t read; /* Number of bytes readed */
13 size_t n = 0; /* Amount of bytes to read by getline */
14 memory_map_t ret = NULL; /* The memory map to return */
16 /* The following variables are used during the parsing of the file "maps" */
17 s_map_region memreg; /* temporal map region used for creating the map */
18 char *lfields[6], *tok, *endptr;
21 /* Open the actual process's proc maps file and create the memory_map_t */
23 fp = fopen("/proc/self/maps", "r");
26 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
28 //XBT_DEBUG("/proc/self/maps");
30 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 while ((read = getline(&line, &n, fp)) != -1) {
35 XBT_DEBUG("%s", line);
37 /* Wipeout the new line character */
38 line[read - 1] = '\0';
40 /* Tokenize the line using spaces as delimiters and store each token */
41 /* in lfields array. We expect 5 tokens/fields */
42 lfields[0] = strtok(line, " ");
44 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
45 lfields[i] = strtok(NULL, " ");
48 /* Check to see if we got the expected amount of columns */
52 /* Ok we are good enough to try to get the info we need */
53 /* First get the start and the end address of the map */
54 tok = strtok(lfields[0], "-");
58 memreg.start_addr = (void *) strtoul(tok, &endptr, 16);
59 /* Make sure that the entire string was an hex number */
63 tok = strtok(NULL, "-");
67 memreg.end_addr = (void *) strtoul(tok, &endptr, 16);
68 /* Make sure that the entire string was an hex number */
72 /* Get the permissions flags */
73 if (strlen(lfields[1]) < 4)
78 for (i = 0; i < 3; i++){
79 switch(lfields[1][i]){
81 memreg.prot |= PROT_READ;
84 memreg.prot |= PROT_WRITE;
87 memreg.prot |= PROT_EXEC;
94 memreg.prot |= PROT_NONE;
96 if (lfields[1][4] == 'p')
97 memreg.flags |= MAP_PRIVATE;
99 else if (lfields[1][4] == 's')
100 memreg.flags |= MAP_SHARED;
102 /* Get the offset value */
103 memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
104 /* Make sure that the entire string was an hex number */
108 /* Get the device major:minor bytes */
109 tok = strtok(lfields[3], ":");
113 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
114 /* Make sure that the entire string was an hex number */
118 tok = strtok(NULL, ":");
122 memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
123 /* Make sure that the entire string was an hex number */
127 /* Get the inode number and make sure that the entire string was a long int */
128 memreg.inode = strtoul(lfields[4], &endptr, 10);
132 /* And finally get the pathname */
133 memreg.pathname = xbt_strdup(lfields[5]);
135 /* Create space for a new map region in the region's array and copy the */
136 /* parsed stuff from the temporal memreg variable */
138 xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
139 memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));