5 memory_map_t get_memory_map(void)
7 FILE *fp; /* File pointer to process's proc maps file */
8 char *line = NULL; /* Temporal storage for each line that is readed */
9 ssize_t read; /* Number of bytes readed */
10 size_t n = 0; /* Amount of bytes to read by getline */
11 memory_map_t ret = NULL; /* The memory map to return */
13 /* The following variables are used during the parsing of the file "maps" */
14 s_map_region memreg; /* temporal map region used for creating the map */
15 char *lfields[6], *tok, *endptr;
18 /* Open the actual process's proc maps file and create the memory_map_t */
20 fp = fopen("/proc/self/maps", "r");
23 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
25 ret = xbt_new0(s_memory_map_t, 1);
27 /* Read one line at the time, parse it and add it to the memory map to be returned */
28 while ((read = getline(&line, &n, fp)) != -1) {
30 /* Wipeout the new line character */
31 line[read - 1] = '\0';
33 /* Tokenize the line using spaces as delimiters and store each token */
34 /* in lfields array. We expect 5 tokens/fields */
35 lfields[0] = strtok(line, " ");
37 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
38 lfields[i] = strtok(NULL, " ");
41 /* Check to see if we got the expected amount of columns */
45 /* Ok we are good enough to try to get the info we need */
46 /* First get the start and the end address of the map */
47 tok = strtok(lfields[0], "-");
51 memreg.start_addr = (void *) strtoul(tok, &endptr, 16);
52 /* Make sure that the entire string was an hex number */
56 tok = strtok(NULL, "-");
60 memreg.end_addr = (void *) strtoul(tok, &endptr, 16);
61 /* Make sure that the entire string was an hex number */
65 /* Get the permissions flags */
66 if (strlen(lfields[1]) < 4)
71 for (i = 0; i < 3; i++)
72 if (lfields[1][i] != '-')
73 memreg.perms |= 1 << i;
75 if (lfields[1][4] == 'p')
76 memreg.perms |= MAP_PRIV;
78 else if (lfields[1][4] == 's')
79 memreg.perms |= MAP_SHARED;
81 /* Get the offset value */
82 memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
83 /* Make sure that the entire string was an hex number */
87 /* Get the device major:minor bytes */
88 tok = strtok(lfields[3], ":");
92 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
93 /* Make sure that the entire string was an hex number */
97 tok = strtok(NULL, ":");
101 memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
102 /* Make sure that the entire string was an hex number */
106 /* Get the inode number and make sure that the entire string was a long int */
107 memreg.inode = strtoul(lfields[4], &endptr, 10);
111 /* And finally get the pathname */
112 memreg.pathname = xbt_strdup(lfields[5]);
114 /* Create space for a new map region in the region's array and copy the */
115 /* parsed stuff from the temporal memreg variable */
117 xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
118 memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));