Logo AND Algorithmique Numérique Distribuée

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