Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix formatting specification
[simgrid.git] / src / mc / mc_process.cpp
1 /* Copyright (c) 2014-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 <assert.h>
8 #include <stddef.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <errno.h>
12
13 #include <sys/types.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <regex.h>
17 #include <sys/mman.h> // PROT_*
18
19 #include <pthread.h>
20
21 #include <libgen.h>
22
23 #include <libunwind.h>
24 #include <libunwind-ptrace.h>
25
26 #include <xbt/mmalloc.h>
27
28 #include "mc_process.h"
29 #include "mc_object_info.h"
30 #include "mc_address_space.h"
31 #include "mc_unw.h"
32 #include "mc_snapshot.h"
33 #include "mc_ignore.h"
34 #include "mc_smx.h"
35 #include "mc_server.h"
36
37 extern "C" {
38
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
40                                 "MC process information");
41
42 static void MC_process_init_memory_map_info(mc_process_t process);
43 static void MC_process_open_memory_file(mc_process_t process);
44
45 // ***** Destructor callbacks
46
47 // ***** mc_address_space methods for mc_process
48
49 static mc_process_t MC_process_get_process(mc_process_t p) {
50   return p;
51 }
52
53 static const s_mc_address_space_class_t mc_process_class = {
54   (mc_address_space_class_read_callback_t) &MC_process_read,
55   (mc_address_space_class_get_process_callback_t) MC_process_get_process
56 };
57
58 bool MC_is_process(mc_address_space_t p)
59 {
60   return p->address_space_class == &mc_process_class;
61 }
62
63 // ***** mc_process
64
65 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
66 {
67   process->address_space.address_space_class = &mc_process_class;
68   process->process_flags = MC_PROCESS_NO_FLAG;
69   process->socket = sockfd;
70   process->pid = pid;
71   if (pid==getpid())
72     process->process_flags |= MC_PROCESS_SELF_FLAG;
73   process->running = true;
74   process->status = 0;
75   process->memory_map = MC_get_memory_map(pid);
76   process->memory_file = -1;
77   process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
78   process->heap = NULL;
79   process->heap_info = NULL;
80   MC_process_init_memory_map_info(process);
81   MC_process_open_memory_file(process);
82
83   // Read std_heap (is a struct mdesc*):
84   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process,
85     "__mmalloc_default_mdp");
86   if (!std_heap_var)
87     xbt_die("No heap information in the target process");
88   if(!std_heap_var->address)
89     xbt_die("No constant address for this variable");
90   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
91     &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
92     MC_PROCESS_INDEX_DISABLED);
93
94   process->smx_process_infos = MC_smx_process_info_list_new();
95   process->smx_old_process_infos = MC_smx_process_info_list_new();
96
97   process->checkpoint_ignore = MC_checkpoint_ignore_new();
98
99   process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors  , __BYTE_ORDER);
100   if (process->process_flags & MC_PROCESS_SELF_FLAG) {
101     process->unw_underlying_addr_space = unw_local_addr_space;
102     process->unw_underlying_context = NULL;
103   } else {
104     process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
105     process->unw_underlying_context = _UPT_create(pid);
106   }
107 }
108
109 void MC_process_clear(mc_process_t process)
110 {
111   process->address_space.address_space_class = NULL;
112   process->process_flags = MC_PROCESS_NO_FLAG;
113   process->pid = 0;
114
115   MC_free_memory_map(process->memory_map);
116   process->memory_map = NULL;
117
118   process->maestro_stack_start = NULL;
119   process->maestro_stack_end = NULL;
120
121   xbt_dynar_free(&process->checkpoint_ignore);
122
123   xbt_dynar_free(&process->smx_process_infos);
124   xbt_dynar_free(&process->smx_old_process_infos);
125
126   size_t i;
127   for (i=0; i!=process->object_infos_size; ++i) {
128     MC_free_object_info(&process->object_infos[i]);
129   }
130   free(process->object_infos);
131   process->object_infos = NULL;
132   process->object_infos_size = 0;
133   if (process->memory_file >= 0) {
134     close(process->memory_file);
135   }
136
137   if (process->unw_underlying_addr_space != unw_local_addr_space) {
138     unw_destroy_addr_space(process->unw_underlying_addr_space);
139     _UPT_destroy(process->unw_underlying_context);
140   }
141   process->unw_underlying_context = NULL;
142   process->unw_underlying_addr_space = NULL;
143
144   unw_destroy_addr_space(process->unw_addr_space);
145   process->unw_addr_space = NULL;
146
147   process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
148
149   free(process->heap);
150   process->heap = NULL;
151
152   free(process->heap_info);
153   process->heap_info = NULL;
154 }
155
156 void MC_process_refresh_heap(mc_process_t process)
157 {
158   xbt_assert(mc_mode == MC_MODE_SERVER);
159   xbt_assert(!MC_process_is_self(process));
160   // Read/dereference/refresh the std_heap pointer:
161   if (!process->heap) {
162     process->heap = (struct mdesc*) malloc(sizeof(struct mdesc));
163   }
164   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
165     process->heap, process->heap_address, sizeof(struct mdesc),
166     MC_PROCESS_INDEX_DISABLED
167     );
168   process->cache_flags |= MC_PROCESS_CACHE_FLAG_HEAP;
169 }
170
171 void MC_process_refresh_malloc_info(mc_process_t process)
172 {
173   xbt_assert(mc_mode == MC_MODE_SERVER);
174   xbt_assert(!MC_process_is_self(process));
175   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
176     MC_process_refresh_heap(process);
177   // Refresh process->heapinfo:
178   size_t malloc_info_bytesize =
179     (process->heap->heaplimit + 1) * sizeof(malloc_info);
180   process->heap_info = (malloc_info*) realloc(process->heap_info, malloc_info_bytesize);
181   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
182     process->heap_info,
183     process->heap->heapinfo, malloc_info_bytesize,
184     MC_PROCESS_INDEX_DISABLED);
185   process->cache_flags |= MC_PROCESS_CACHE_FLAG_MALLOC_INFO;
186 }
187
188 #define SO_RE "\\.so[\\.0-9]*$"
189 #define VERSION_RE "-[\\.0-9]*$"
190
191 const char* FILTERED_LIBS[] = {
192   "libstdc++",
193   "libc++",
194   "libm",
195   "libgcc_s",
196   "libpthread",
197   "libunwind",
198   "libunwind-x86_64",
199   "libunwind-x86",
200   "libunwind-ptrace",
201   "libdw",
202   "libdl",
203   "librt",
204   "liblzma",
205   "libelf",
206   "libbz2",
207   "libz",
208   "libelf",
209   "libc",
210   "ld"
211 };
212
213 static bool MC_is_simgrid_lib(const char* libname)
214 {
215   return !strcmp(libname, "libsimgrid");
216 }
217
218 static bool MC_is_filtered_lib(const char* libname)
219 {
220   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
221   size_t i;
222   for (i=0; i!=n; ++i)
223     if (strcmp(libname, FILTERED_LIBS[i])==0)
224       return true;
225   return false;
226 }
227
228 struct s_mc_memory_map_re {
229   regex_t so_re;
230   regex_t version_re;
231 };
232
233 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
234   const char* map_basename = basename((char*) pathname);
235
236   regmatch_t match;
237   if(regexec(&res->so_re, map_basename, 1, &match, 0))
238     return NULL;
239
240   char* libname = strndup(map_basename, match.rm_so);
241
242   // Strip the version suffix:
243   if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
244     char* temp = libname;
245     libname = strndup(temp, match.rm_so);
246     free(temp);
247   }
248
249   return libname;
250 }
251
252 /** @brief Finds the range of the different memory segments and binary paths */
253 static void MC_process_init_memory_map_info(mc_process_t process)
254 {
255   XBT_DEBUG("Get debug information ...");
256   process->maestro_stack_start = NULL;
257   process->maestro_stack_end = NULL;
258   process->object_infos = NULL;
259   process->object_infos_size = 0;
260   process->binary_info = NULL;
261   process->libsimgrid_info = NULL;
262
263   struct s_mc_memory_map_re res;
264
265   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
266     xbt_die(".so regexp did not compile");
267
268   memory_map_t maps = process->memory_map;
269
270   const char* current_name = NULL;
271
272   for (ssize_t i=0; i < maps->mapsize; i++) {
273     map_region_t reg = &(maps->regions[i]);
274     const char* pathname = maps->regions[i].pathname;
275
276     // Nothing to do
277     if (maps->regions[i].pathname == NULL) {
278       current_name = NULL;
279       continue;
280     }
281
282     // [stack], [vvar], [vsyscall], [vdso] ...
283     if (pathname[0] == '[') {
284       if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
285         process->maestro_stack_start = reg->start_addr;
286         process->maestro_stack_end = reg->end_addr;
287       }
288       current_name = NULL;
289       continue;
290     }
291
292     if (current_name && strcmp(current_name, pathname)==0)
293       continue;
294
295     current_name = pathname;
296     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
297       continue;
298
299     const bool is_executable = !i;
300     char* libname = NULL;
301     if (!is_executable) {
302       libname = MC_get_lib_name(pathname, &res);
303       if(!libname)
304         continue;
305       if (MC_is_filtered_lib(libname)) {
306         free(libname);
307         continue;
308       }
309     }
310
311     mc_object_info_t info =
312       MC_find_object_info(process->memory_map, pathname, is_executable);
313     process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
314       (process->object_infos_size+1) * sizeof(mc_object_info_t*));
315     process->object_infos[process->object_infos_size] = info;
316     process->object_infos_size++;
317     if (is_executable)
318       process->binary_info = info;
319     else if (libname && MC_is_simgrid_lib(libname))
320       process->libsimgrid_info = info;
321     free(libname);
322   }
323
324   regfree(&res.so_re);
325   regfree(&res.version_re);
326
327   // Resolve time (including accress differents objects):
328   for (size_t i=0; i!=process->object_infos_size; ++i)
329     MC_post_process_object_info(process, process->object_infos[i]);
330
331   xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
332   xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
333
334   XBT_DEBUG("Get debug information done !");
335 }
336
337 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
338 {
339   size_t i;
340   for (i = 0; i != process->object_infos_size; ++i) {
341     if (addr >= (void *) process->object_infos[i]->start
342         && addr <= (void *) process->object_infos[i]->end) {
343       return process->object_infos[i];
344     }
345   }
346   return NULL;
347 }
348
349 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
350 {
351   size_t i;
352   for (i = 0; i != process->object_infos_size; ++i) {
353     if (addr >= (void *) process->object_infos[i]->start_exec
354         && addr <= (void *) process->object_infos[i]->end_exec) {
355       return process->object_infos[i];
356     }
357   }
358   return NULL;
359 }
360
361 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
362 {
363   size_t i;
364   for (i = 0; i != process->object_infos_size; ++i) {
365     if (addr >= (void *) process->object_infos[i]->start_rw
366         && addr <= (void *) process->object_infos[i]->end_rw) {
367       return process->object_infos[i];
368     }
369   }
370   return NULL;
371 }
372
373 // Functions, variables…
374
375 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
376 {
377   mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
378   if (info == NULL)
379     return NULL;
380   else
381     return MC_file_object_info_find_function(info, ip);
382 }
383
384 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
385 {
386   const size_t n = process->object_infos_size;
387   size_t i;
388
389   // First lookup the variable in the executable shared object.
390   // A global variable used directly by the executable code from a library
391   // is reinstanciated in the executable memory .data/.bss.
392   // We need to look up the variable in the execvutable first.
393   if (process->binary_info) {
394     mc_object_info_t info = process->binary_info;
395     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
396     if (var)
397       return var;
398   }
399
400   for (i=0; i!=n; ++i) {
401     mc_object_info_t info =process->object_infos[i];
402     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
403     if (var)
404       return var;
405   }
406
407   return NULL;
408 }
409
410 void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size)
411 {
412   dw_variable_t var = MC_process_find_variable_by_name(process, name);
413   if (!var->address)
414     xbt_die("No simple location for this variable");
415   if (!var->type->full_type)
416     xbt_die("Partial type for %s, cannot check size", name);
417   if ((size_t) var->type->full_type->byte_size != size)
418     xbt_die("Unexpected size for %s (expected %zi, was %zi)",
419       name, size, (size_t) var->type->full_type->byte_size);
420   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE, target, var->address, size,
421     MC_PROCESS_INDEX_ANY);
422 }
423
424 char* MC_process_read_string(mc_process_t process, void* address)
425 {
426   if (!address)
427     return NULL;
428   if (MC_process_is_self(process))
429     return strdup((char*) address);
430
431   off_t len = 128;
432   char* res = (char*) malloc(len);
433   off_t off = 0;
434
435   while (1) {
436     ssize_t c = pread(process->memory_file, res + off, len - off, (off_t) address + off);
437     if (c == -1) {
438       if (errno == EINTR)
439         continue;
440       else
441         xbt_die("Could not read from from remote process");
442     }
443     if (c==0)
444       xbt_die("Could not read string from remote process");
445
446     void* p = memchr(res + off, '\0', c);
447     if (p)
448       return res;
449
450     off += c;
451     if (off == len) {
452       len *= 2;
453       res = (char*) realloc(res, len);
454     }
455   }
456 }
457
458 // ***** Memory access
459
460 int MC_process_vm_open(pid_t pid, int flags)
461 {
462   const size_t buffer_size = 30;
463   char buffer[buffer_size];
464   int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
465   if (res < 0 || (size_t) res >= buffer_size) {
466     errno = ENAMETOOLONG;
467     return -1;
468   }
469   return open(buffer, flags);
470 }
471
472 static void MC_process_open_memory_file(mc_process_t process)
473 {
474   if (MC_process_is_self(process) || process->memory_file >= 0)
475     return;
476
477   int fd = MC_process_vm_open(process->pid, O_RDWR);
478   if (fd<0)
479     xbt_die("Could not open file for process virtual address space");
480   process->memory_file = fd;
481 }
482
483 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
484 {
485   char* buffer = (char*) buf;
486   ssize_t real_count = count;
487   while (count) {
488     ssize_t res = pread(fd, buffer, count, offset);
489     if (res > 0) {
490       count  -= res;
491       buffer += res;
492       offset += res;
493     } else if (res==0) {
494       return -1;
495     } else if (errno != EINTR) {
496       return -1;
497     }
498   }
499   return real_count;
500 }
501
502 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
503 {
504   const char* buffer = (const char*) buf;
505   ssize_t real_count = count;
506   while (count) {
507     ssize_t res = pwrite(fd, buffer, count, offset);
508     if (res > 0) {
509       count  -= res;
510       buffer += res;
511       offset += res;
512     } else if (res==0) {
513       return -1;
514     } else if (errno != EINTR) {
515       return -1;
516     }
517   }
518   return real_count;
519 }
520
521 const void* MC_process_read(mc_process_t process, adress_space_read_flags_t flags,
522   void* local, const void* remote, size_t len,
523   int process_index)
524 {
525   if (process_index != MC_PROCESS_INDEX_DISABLED) {
526     mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
527     // Segment overlap is not handled.
528     if (MC_object_info_is_privatized(info)) {
529       if (process_index < 0)
530         xbt_die("Missing process index");
531       // Address translation in the privaization segment:
532       size_t offset = (const char*) remote - info->start_rw;
533       remote = (const char*) remote - offset;
534     }
535   }
536
537   if (MC_process_is_self(process)) {
538     if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
539       return remote;
540     else {
541       memcpy(local, remote, len);
542       return local;
543     }
544   } else {
545     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
546       xbt_die("Read from process %lli failed", (long long) process->pid);
547     return local;
548   }
549 }
550
551 const void* MC_process_read_simple(mc_process_t process,
552   void* local, const void* remote, size_t len)
553 {
554   adress_space_read_flags_t flags = MC_ADDRESS_SPACE_READ_FLAGS_NONE;
555   int index = MC_PROCESS_INDEX_ANY;
556    MC_process_read(process, flags, local, remote, len, index);
557    return local;
558 }
559
560 const void* MC_process_read_dynar_element(mc_process_t process,
561   void* local, const void* remote_dynar, size_t i, size_t len)
562 {
563   s_xbt_dynar_t d;
564   MC_process_read_simple(process, &d, remote_dynar, sizeof(d));
565   if (i >= d.used)
566     xbt_die("Out of bound index %zi/%lu", i, d.used);
567   if (len != d.elmsize)
568     xbt_die("Bad size in MC_process_read_dynar_element");
569   MC_process_read_simple(process, local, xbt_dynar_get_ptr(&d, i), len);
570   return local;
571 }
572
573 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
574 {
575   if (MC_process_is_self(process)) {
576     memcpy(remote, local, len);
577   } else {
578     if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
579       xbt_die("Write to process %lli failed", (long long) process->pid);
580   }
581 }
582
583 unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar)
584 {
585   if (!remote_dynar)
586     return 0;
587   unsigned long res;
588   MC_process_read_simple(process, &res,
589     &((xbt_dynar_t)remote_dynar)->used, sizeof(res));
590   return res;
591 }
592
593 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
594 static const void* zero_buffer;
595 static const size_t zero_buffer_size = 10 * 4096;
596
597 static void MC_zero_buffer_init(void)
598 {
599   int fd = open("/dev/zero", O_RDONLY);
600   if (fd<0)
601     xbt_die("Could not open /dev/zero");
602   zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
603   if (zero_buffer == MAP_FAILED)
604     xbt_die("Could not map the zero buffer");
605   close(fd);
606 }
607
608 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
609 {
610   if (MC_process_is_self(process)) {
611     memset(remote, 0, len);
612   } else {
613     pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
614     while (len) {
615       size_t s = len > zero_buffer_size ? zero_buffer_size : len;
616       MC_process_write(process, zero_buffer, remote, s);
617       remote = (char*) remote + s;
618       len -= s;
619     }
620   }
621 }
622
623 }