Logo AND Algorithmique Numérique Distribuée

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