Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
29640c0d266fb074c533be8ccc5f46d259074848
[simgrid.git] / src / xbt / memory_map.cpp
1 /* Copyright (c) 2008-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <cstdlib>
8 #include <cstdio>
9 #include <cstring>
10
11 #include <sys/types.h>
12 #ifdef __linux__
13 # include <sys/mman.h>
14 #endif
15
16 #include <xbt/sysdep.h>
17 #include <xbt/base.h>
18 #include <xbt/file.h>
19 #include <xbt/log.h>
20
21 #include "memory_map.hpp"
22
23 extern "C" {
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_memory_map, xbt,
26                                 "Logging specific to algorithms for memory_map");
27
28 }
29
30 namespace simgrid {
31 namespace xbt {
32
33 XBT_PRIVATE std::vector<VmMap> get_memory_map(pid_t pid)
34 {
35 #ifdef __linux__
36   /* Open the actual process's proc maps file and create the memory_map_t */
37   /* to be returned. */
38   char* path = bprintf("/proc/%i/maps", (int) pid);
39   FILE *fp = std::fopen(path, "r");
40   if(fp == NULL)
41     std::perror("fopen failed");
42   xbt_assert(fp,
43     "Cannot open %s to investigate the memory map of the process.", path);
44   free(path);
45   setbuf(fp, NULL);
46
47   std::vector<VmMap> ret;
48
49   /* Read one line at the time, parse it and add it to the memory map to be returned */
50   ssize_t read; /* Number of bytes readed */
51   char* line = NULL;
52   std::size_t n = 0; /* Amount of bytes to read by xbt_getline */
53   while ((read = xbt_getline(&line, &n, fp)) != -1) {
54
55     //fprintf(stderr,"%s", line);
56
57     /* Wipeout the new line character */
58     line[read - 1] = '\0';
59
60     /* Tokenize the line using spaces as delimiters and store each token */
61     /* in lfields array. We expect 5 tokens/fields */
62     char* lfields[6];
63     lfields[0] = strtok(line, " ");
64
65     int i;
66     for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
67       lfields[i] = std::strtok(NULL, " ");
68     }
69
70     /* Check to see if we got the expected amount of columns */
71     if (i < 6)
72       xbt_abort();
73
74     /* Ok we are good enough to try to get the info we need */
75     /* First get the start and the end address of the map   */
76     char *tok = std::strtok(lfields[0], "-");
77     if (tok == NULL)
78       xbt_abort();
79
80     VmMap memreg;
81     char *endptr;
82     memreg.start_addr = std::strtoull(tok, &endptr, 16);
83     /* Make sure that the entire string was an hex number */
84     if (*endptr != '\0')
85       xbt_abort();
86
87     tok = std::strtok(NULL, "-");
88     if (tok == NULL)
89       xbt_abort();
90
91     memreg.end_addr = std::strtoull(tok, &endptr, 16);
92     /* Make sure that the entire string was an hex number */
93     if (*endptr != '\0')
94       xbt_abort();
95
96     /* Get the permissions flags */
97     if (std::strlen(lfields[1]) < 4)
98       xbt_abort();
99
100     memreg.prot = 0;
101
102     for (i = 0; i < 3; i++){
103       switch(lfields[1][i]){
104         case 'r':
105           memreg.prot |= PROT_READ;
106           break;
107         case 'w':
108           memreg.prot |= PROT_WRITE;
109           break;
110         case 'x':
111           memreg.prot |= PROT_EXEC;
112           break;
113         default:
114           break;
115       }
116     }
117     if (memreg.prot == 0)
118       memreg.prot |= PROT_NONE;
119
120     if (lfields[1][4] == 'p')
121       memreg.flags |= MAP_PRIVATE;
122
123     else if (lfields[1][4] == 's')
124       memreg.flags |= MAP_SHARED;
125
126     /* Get the offset value */
127     memreg.offset = std::strtoull(lfields[2], &endptr, 16);
128     /* Make sure that the entire string was an hex number */
129     if (*endptr != '\0')
130       xbt_abort();
131
132     /* Get the device major:minor bytes */
133     tok = std::strtok(lfields[3], ":");
134     if (tok == NULL)
135       xbt_abort();
136
137     memreg.dev_major = (char) strtoul(tok, &endptr, 16);
138     /* Make sure that the entire string was an hex number */
139     if (*endptr != '\0')
140       xbt_abort();
141
142     tok = std::strtok(NULL, ":");
143     if (tok == NULL)
144       xbt_abort();
145
146     memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
147     /* Make sure that the entire string was an hex number */
148     if (*endptr != '\0')
149       xbt_abort();
150
151     /* Get the inode number and make sure that the entire string was a long int */
152     memreg.inode = strtoul(lfields[4], &endptr, 10);
153     if (*endptr != '\0')
154       xbt_abort();
155
156     /* And finally get the pathname */
157     if (lfields[5])
158       memreg.pathname = lfields[5];
159
160     /* Create space for a new map region in the region's array and copy the */
161     /* parsed stuff from the temporal memreg variable */
162     XBT_DEBUG("Found region for %s",
163       !memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
164
165     ret.push_back(std::move(memreg));
166   }
167
168   std::free(line);
169   std::fclose(fp);
170   return std::move(ret);
171 #else
172   /* On FreeBSD, kinfo_getvmmap() could be used but mmap() support is disabled
173      anyway. */
174   xbt_die("Could not get memory map from process %lli", (long long int) pid);
175 #endif
176 }
177
178 }
179 }