Logo AND Algorithmique Numérique Distribuée

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