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 char *backup_line = NULL;
23 /* Open the actual process's proc maps file and create the memory_map_t */
25 fp = fopen("/proc/self/maps", "r");
28 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
30 //XBT_DEBUG("/proc/self/maps");
32 ret = xbt_new0(s_memory_map_t, 1);
34 /* Read one line at the time, parse it and add it to the memory map to be returned */
35 while ((read = getline(&line, &n, fp)) != -1) {
37 //XBT_INFO("%s", line);
38 if(XBT_LOG_ISENABLED(mc_memory_map, xbt_log_priority_debug))
39 backup_line = strdup(line);
41 /* Wipeout the new line character */
42 line[read - 1] = '\0';
44 /* Tokenize the line using spaces as delimiters and store each token */
45 /* in lfields array. We expect 5 tokens/fields */
46 lfields[0] = strtok(line, " ");
48 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
49 lfields[i] = strtok(NULL, " ");
52 /* Check to see if we got the expected amount of columns */
56 /* Ok we are good enough to try to get the info we need */
57 /* First get the start and the end address of the map */
58 tok = strtok(lfields[0], "-");
62 memreg.start_addr = (void *) strtoul(tok, &endptr, 16);
63 /* Make sure that the entire string was an hex number */
67 tok = strtok(NULL, "-");
71 memreg.end_addr = (void *) strtoul(tok, &endptr, 16);
72 /* Make sure that the entire string was an hex number */
76 /* Get the permissions flags */
77 if (strlen(lfields[1]) < 4)
82 for (i = 0; i < 3; i++){
83 switch(lfields[1][i]){
85 memreg.prot |= PROT_READ;
88 memreg.prot |= PROT_WRITE;
91 memreg.prot |= PROT_EXEC;
98 memreg.prot |= PROT_NONE;
100 if (lfields[1][4] == 'p')
101 memreg.flags |= MAP_PRIVATE;
103 else if (lfields[1][4] == 's')
104 memreg.flags |= MAP_SHARED;
106 /* Get the offset value */
107 memreg.offset = (void *) strtoul(lfields[2], &endptr, 16);
108 /* Make sure that the entire string was an hex number */
112 /* Get the device major:minor bytes */
113 tok = strtok(lfields[3], ":");
117 memreg.dev_major = (char) strtoul(tok, &endptr, 16);
118 /* Make sure that the entire string was an hex number */
122 tok = strtok(NULL, ":");
126 memreg.dev_minor = (char) strtoul(tok, &endptr, 16);
127 /* Make sure that the entire string was an hex number */
131 /* Get the inode number and make sure that the entire string was a long int */
132 memreg.inode = strtoul(lfields[4], &endptr, 10);
136 /* And finally get the pathname */
137 memreg.pathname = xbt_strdup(lfields[5]);
139 /* Create space for a new map region in the region's array and copy the */
140 /* parsed stuff from the temporal memreg variable */
142 xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1));
143 memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg));
146 if(XBT_LOG_ISENABLED(mc_memory_map, xbt_log_priority_debug)){
147 if ((memreg.prot & PROT_WRITE)){
148 if (memreg.pathname == NULL){
149 if (memreg.start_addr == std_heap){
150 XBT_DEBUG("New region in snapshot : %s", backup_line);
153 if (!memcmp(basename(memreg.pathname), "libsimgrid", 10)){
154 XBT_DEBUG("New region in snapshot : %s", backup_line);