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 switch(lfields[1][i]){
74 memreg.prot |= PROT_READ;
77 memreg.prot |= PROT_WRITE;
80 memreg.prot |= PROT_EXEC;
87 memreg.prot |= PROT_NONE;
89 if (lfields[1][4] == 'p')
90 memreg.flags |= MAP_PRIVATE;
92 else if (lfields[1][4] == 's')
93 memreg.flags |= MAP_SHARED;
95 /* Get the offset value */
96 memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
97 /* Make sure that the entire string was an hex number */
101 /* Get the device major:minor bytes */
102 tok = strtok(lfields[3], ":");
106 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
107 /* Make sure that the entire string was an hex number */
111 tok = strtok(NULL, ":");
115 memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
116 /* Make sure that the entire string was an hex number */
120 /* Get the inode number and make sure that the entire string was a long int */
121 memreg.inode = strtoul(lfields[4], &endptr, 10);
125 /* And finally get the pathname */
126 memreg.pathname = xbt_strdup(lfields[5]);
128 /* Create space for a new map region in the region's array and copy the */
129 /* parsed stuff from the temporal memreg variable */
131 xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
132 memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));