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 perror("fopen failed");
29 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
31 //XBT_DEBUG("/proc/self/maps");
33 ret = xbt_new0(s_memory_map_t, 1);
35 /* Read one line at the time, parse it and add it to the memory map to be returned */
36 while ((read = getline(&line, &n, fp)) != -1) {
38 XBT_DEBUG("%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 */
45 lfields[0] = strtok(line, " ");
47 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
48 lfields[i] = strtok(NULL, " ");
51 /* Check to see if we got the expected amount of columns */
55 /* Ok we are good enough to try to get the info we need */
56 /* First get the start and the end address of the map */
57 tok = strtok(lfields[0], "-");
61 memreg.start_addr = (void *) strtoul(tok, &endptr, 16);
62 /* Make sure that the entire string was an hex number */
66 tok = strtok(NULL, "-");
70 memreg.end_addr = (void *) strtoul(tok, &endptr, 16);
71 /* Make sure that the entire string was an hex number */
75 /* Get the permissions flags */
76 if (strlen(lfields[1]) < 4)
81 for (i = 0; i < 3; i++){
82 switch(lfields[1][i]){
84 memreg.prot |= PROT_READ;
87 memreg.prot |= PROT_WRITE;
90 memreg.prot |= PROT_EXEC;
97 memreg.prot |= PROT_NONE;
99 if (lfields[1][4] == 'p')
100 memreg.flags |= MAP_PRIVATE;
102 else if (lfields[1][4] == 's')
103 memreg.flags |= MAP_SHARED;
105 /* Get the offset value */
106 memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
107 /* Make sure that the entire string was an hex number */
111 /* Get the device major:minor bytes */
112 tok = strtok(lfields[3], ":");
116 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
117 /* Make sure that the entire string was an hex number */
121 tok = strtok(NULL, ":");
125 memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
126 /* Make sure that the entire string was an hex number */
130 /* Get the inode number and make sure that the entire string was a long int */
131 memreg.inode = strtoul(lfields[4], &endptr, 10);
135 /* And finally get the pathname */
136 memreg.pathname = xbt_strdup(lfields[5]);
138 /* Create space for a new map region in the region's array and copy the */
139 /* parsed stuff from the temporal memreg variable */
141 xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
142 memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));