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");
22 xbt_assert0(fp,"Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
24 ret = xbt_new0(s_memory_map_t,1);
26 /* Read one line at the time, parse it and add it to the memory map to be returned */
27 while((read = getline(&line, &n, fp)) != -1) {
29 /* Wipeout the new line character */
30 line[read - 1] = '\0';
32 /* Tokenize the line using spaces as delimiters and store each token */
33 /* in lfields array. We expect 5 tokens/fields */
34 lfields[0] = strtok(line, " ");
36 for(i=1; i < 6 && lfields[i - 1] != NULL; i++){
37 lfields[i] = strtok(NULL, " ");
40 /* Check to see if we got the expected amount of columns */
44 /* Ok we are good enough to try to get the info we need */
45 /* First get the start and the end address of the map */
46 tok = strtok(lfields[0], "-");
50 memreg.start_addr = (void *)strtoul(tok, &endptr, 16);
51 /* Make sure that the entire string was an hex number */
55 tok = strtok(NULL, "-");
59 memreg.end_addr = (void *)strtoul(tok, &endptr, 16);
60 /* Make sure that the entire string was an hex number */
64 /* Get the permissions flags */
65 if(strlen(lfields[1]) < 4)
71 if(lfields[1][i] != '-')
72 memreg.perms |= 1 << i;
74 if(lfields[1][4] == 'p')
75 memreg.perms |= MAP_PRIV;
77 else if (lfields[1][4] == 's')
78 memreg.perms |= MAP_SHARED;
80 /* Get the offset value */
81 memreg.offset = (void *)strtoul(lfields[2], &endptr, 16);
82 /* Make sure that the entire string was an hex number */
86 /* Get the device major:minor bytes */
87 tok = strtok(lfields[3], ":");
91 memreg.dev_major = (char)strtoul(tok, &endptr, 16);
92 /* Make sure that the entire string was an hex number */
96 tok = strtok(NULL, ":");
100 memreg.dev_minor = (char)strtoul(tok, &endptr, 16);
101 /* Make sure that the entire string was an hex number */
105 /* Get the inode number and make sure that the entire string was a long int*/
106 memreg.inode = strtoul(lfields[4], &endptr, 10);
110 /* And finally get the pathname */
111 memreg.pathname = xbt_strdup(lfields[5]);
113 /* Create space for a new map region in the region's array and copy the */
114 /* parsed stuff from the temporal memreg variable */
115 ret->regions = xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
116 memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));