Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Implements privatization support for MC_process_read
[simgrid.git] / src / mc / mc_process.c
1 #include <assert.h>
2 #include <stddef.h>
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <errno.h>
6
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <regex.h>
11 #include <sys/mman.h> // PROT_*
12
13 #include <libgen.h>
14
15 #include "mc_process.h"
16 #include "mc_object_info.h"
17 #include "mc_address_space.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
20                                 "MC process information");
21
22 static void MC_process_init_memory_map_info(mc_process_t process);
23 static void MC_process_open_memory_file(mc_process_t process);
24
25 static s_mc_address_space_class_t mc_process_class = {
26   .read = (void*) &MC_process_read
27 };
28
29 void MC_process_init(mc_process_t process, pid_t pid)
30 {
31   process->address_space.address_space_class = &mc_process_class;
32   process->process_flags = MC_PROCESS_NO_FLAG;
33   process->pid = pid;
34   if (pid==getpid())
35     process->process_flags |= MC_PROCESS_SELF_FLAG;
36   process->memory_map = MC_get_memory_map(pid);
37   process->memory_file = -1;
38   process->cache_flags = 0;
39   process->heap = NULL;
40   process->heap_info = NULL;
41   MC_process_init_memory_map_info(process);
42   MC_process_open_memory_file(process);
43
44   // Read std_heap (is a struct mdesc*):
45   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
46   if (!std_heap_var)
47     xbt_die("No heap information in the target process");
48   if(!std_heap_var->address)
49     xbt_die("No constant address for this variable");
50   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
51     &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
52     MC_PROCESS_INDEX_DISABLED);
53 }
54
55 void MC_process_clear(mc_process_t process)
56 {
57   process->address_space.address_space_class = NULL;
58   process->process_flags = MC_PROCESS_NO_FLAG;
59   process->pid = 0;
60
61   MC_free_memory_map(process->memory_map);
62   process->memory_map = NULL;
63
64   process->maestro_stack_start = NULL;
65   process->maestro_stack_end = NULL;
66
67   size_t i;
68   for (i=0; i!=process->object_infos_size; ++i) {
69     MC_free_object_info(&process->object_infos[i]);
70   }
71   free(process->object_infos);
72   process->object_infos = NULL;
73   process->object_infos_size = 0;
74   if (process->memory_file >= 0) {
75     close(process->memory_file);
76   }
77
78   process->cache_flags = 0;
79
80   free(process->heap);
81   process->heap = NULL;
82
83   free(process->heap_info);
84   process->heap_info = NULL;
85 }
86
87 void MC_process_refresh_heap(mc_process_t process)
88 {
89   assert(!MC_process_is_self(process));
90   // Read/dereference/refresh the std_heap pointer:
91   if (!process->heap) {
92     xbt_mheap_t oldheap  = mmalloc_get_current_heap();
93     MC_SET_MC_HEAP;
94     process->heap = malloc(sizeof(struct mdesc));
95     mmalloc_set_current_heap(oldheap);
96   }
97   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
98     process->heap, process->heap_address, sizeof(struct mdesc),
99     MC_PROCESS_INDEX_DISABLED
100     );
101 }
102
103 void MC_process_refresh_malloc_info(mc_process_t process)
104 {
105   assert(!MC_process_is_self(process));
106   if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
107     MC_process_refresh_heap(process);
108   // Refresh process->heapinfo:
109   size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
110
111   xbt_mheap_t oldheap  = mmalloc_get_current_heap();
112   MC_SET_MC_HEAP;
113   process->heap_info = (malloc_info*) realloc(process->heap_info,
114     malloc_info_bytesize);
115   mmalloc_set_current_heap(oldheap);
116
117   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
118     process->heap_info,
119     process->heap->heapinfo, malloc_info_bytesize,
120     MC_PROCESS_INDEX_DISABLED);
121 }
122
123 #define SO_RE "\\.so[\\.0-9]*$"
124 #define VERSION_RE "-[\\.0-9]*$"
125
126 const char* FILTERED_LIBS[] = {
127   "libstdc++",
128   "libc++",
129   "libm",
130   "libgcc_s",
131   "libpthread",
132   "libunwind",
133   "libunwind-x86_64",
134   "libunwind-x86",
135   "libdw",
136   "libdl",
137   "librt",
138   "liblzma",
139   "libelf",
140   "libbz2",
141   "libz",
142   "libelf",
143   "libc",
144   "ld"
145 };
146
147 static bool MC_is_simgrid_lib(const char* libname)
148 {
149   return !strcmp(libname, "libsimgrid");
150 }
151
152 static bool MC_is_filtered_lib(const char* libname)
153 {
154   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
155   size_t i;
156   for (i=0; i!=n; ++i)
157     if (strcmp(libname, FILTERED_LIBS[i])==0)
158       return true;
159   return false;
160 }
161
162 struct s_mc_memory_map_re {
163   regex_t so_re;
164   regex_t version_re;
165 };
166
167 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
168   const char* map_basename = basename((char*) pathname);
169
170   regmatch_t match;
171   if(regexec(&res->so_re, map_basename, 1, &match, 0))
172     return NULL;
173
174   char* libname = strndup(map_basename, match.rm_so);
175
176   // Strip the version suffix:
177   if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
178     char* temp = libname;
179     libname = strndup(temp, match.rm_so);
180     free(temp);
181   }
182
183   return libname;
184 }
185
186 /** @brief Finds the range of the different memory segments and binary paths */
187 static void MC_process_init_memory_map_info(mc_process_t process)
188 {
189   XBT_INFO("Get debug information ...");
190   process->maestro_stack_start = NULL;
191   process->maestro_stack_end = NULL;
192   process->object_infos = NULL;
193   process->object_infos_size = 0;
194   process->binary_info = NULL;
195   process->libsimgrid_info = NULL;
196
197   struct s_mc_memory_map_re res;
198
199   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
200     xbt_die(".so regexp did not compile");
201
202   memory_map_t maps = process->memory_map;
203
204   const char* current_name = NULL;
205
206   size_t i = 0;
207   for (i=0; i < maps->mapsize; i++) {
208     map_region_t reg = &(maps->regions[i]);
209     const char* pathname = maps->regions[i].pathname;
210
211     // Nothing to do
212     if (maps->regions[i].pathname == NULL) {
213       current_name = NULL;
214       continue;
215     }
216
217     // [stack], [vvar], [vsyscall], [vdso] ...
218     if (pathname[0] == '[') {
219       if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
220         process->maestro_stack_start = reg->start_addr;
221         process->maestro_stack_end = reg->end_addr;
222       }
223       current_name = NULL;
224       continue;
225     }
226
227     if (current_name && strcmp(current_name, pathname)==0)
228       continue;
229
230     current_name = pathname;
231     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
232       continue;
233
234     const bool is_executable = !i;
235     char* libname = NULL;
236     if (!is_executable) {
237       libname = MC_get_lib_name(pathname, &res);
238       if(!libname)
239         continue;
240       if (MC_is_filtered_lib(libname)) {
241         free(libname);
242         continue;
243       }
244     }
245
246     mc_object_info_t info =
247       MC_find_object_info(process->memory_map, pathname, is_executable);
248     process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
249       (process->object_infos_size+1) * sizeof(mc_object_info_t*));
250     process->object_infos[process->object_infos_size] = info;
251     process->object_infos_size++;
252     if (is_executable)
253       process->binary_info = info;
254     else if (libname && MC_is_simgrid_lib(libname))
255       process->libsimgrid_info = info;
256     free(libname);
257   }
258
259   regfree(&res.so_re);
260   regfree(&res.version_re);
261
262   // Resolve time (including accress differents objects):
263   for (i=0; i!=process->object_infos_size; ++i)
264     MC_post_process_object_info(process, process->object_infos[i]);
265
266   xbt_assert(process->maestro_stack_start, "maestro_stack_start");
267   xbt_assert(process->maestro_stack_end, "maestro_stack_end");
268
269   XBT_INFO("Get debug information done !");
270 }
271
272 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
273 {
274   size_t i;
275   for (i = 0; i != process->object_infos_size; ++i) {
276     if (addr >= (void *) process->object_infos[i]->start
277         && addr <= (void *) process->object_infos[i]->end) {
278       return process->object_infos[i];
279     }
280   }
281   return NULL;
282 }
283
284 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
285 {
286   size_t i;
287   for (i = 0; i != process->object_infos_size; ++i) {
288     if (addr >= (void *) process->object_infos[i]->start_exec
289         && addr <= (void *) process->object_infos[i]->end_exec) {
290       return process->object_infos[i];
291     }
292   }
293   return NULL;
294 }
295
296 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
297 {
298   size_t i;
299   for (i = 0; i != process->object_infos_size; ++i) {
300     if (addr >= (void *) process->object_infos[i]->start_rw
301         && addr <= (void *) process->object_infos[i]->end_rw) {
302       return process->object_infos[i];
303     }
304   }
305   return NULL;
306 }
307
308 // Functions, variables…
309
310 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
311 {
312   mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
313   if (info == NULL)
314     return NULL;
315   else
316     return MC_file_object_info_find_function(info, ip);
317 }
318
319 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
320 {
321   const size_t n = process->object_infos_size;
322   size_t i;
323   for (i=0; i!=n; ++i) {
324     mc_object_info_t info =process->object_infos[i];
325     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
326     if (var)
327       return var;
328   }
329   return NULL;
330 }
331
332 // ***** Memory access
333
334 static void MC_process_open_memory_file(mc_process_t process)
335 {
336   if (MC_process_is_self(process) || process->memory_file >= 0)
337     return;
338
339   const size_t buffer_size = 30;
340   char buffer[buffer_size];
341   int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) process->pid);
342   if (res < 0 || res>= buffer_size) {
343     XBT_ERROR("Could not open memory file descriptor for process %lli",
344       (long long) process->pid);
345     return;
346   }
347
348   int fd = open(buffer, O_RDWR);
349   if (fd<0)
350     xbt_die("Could not initialise memory access for remote process");
351   process->memory_file = fd;
352 }
353
354 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
355 {
356   char* buffer = (char*) buf;
357   ssize_t real_count = count;
358   while (count) {
359     ssize_t res = pread(fd, buffer, count, offset);
360     if (res > 0) {
361       count  -= res;
362       buffer += res;
363       offset += res;
364     } else if (res==0) {
365       return -1;
366     } else if (errno != EINTR) {
367       return -1;
368     }
369   }
370   return real_count;
371 }
372
373 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
374 {
375   const char* buffer = (const char*) buf;
376   ssize_t real_count = count;
377   while (count) {
378     ssize_t res = pwrite(fd, buffer, count, offset);
379     if (res > 0) {
380       count  -= res;
381       buffer += res;
382       offset += res;
383     } else if (res==0) {
384       return -1;
385     } else if (errno != EINTR) {
386       return -1;
387     }
388   }
389   return real_count;
390 }
391
392 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
393   void* local, const void* remote, size_t len,
394   int process_index)
395 {
396   if (process_index != MC_PROCESS_INDEX_DISABLED) {
397     mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
398     // Segment overlap is not handled.
399     if (MC_object_info_is_privatized(info)) {
400       if (process_index < 0)
401         xbt_die("Missing process index");
402       // Address translation in the privaization segment:
403       size_t offset = (const char*) remote - info->start_rw;
404       remote = (const char*) remote - offset;
405     }
406   }
407
408   if (MC_process_is_self(process)) {
409     if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
410       return remote;
411     else {
412       memcpy(local, remote, len);
413       return local;
414     }
415   } else {
416     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
417       xbt_die("Read from process %lli failed", (long long) process->pid);
418     return local;
419   }
420 }
421
422 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
423 {
424   if (MC_process_is_self(process)) {
425     memcpy(remote, local, len);
426   } else {
427     if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
428       xbt_die("Write to process %lli failed", (long long) process->pid);
429   }
430 }