Logo AND Algorithmique Numérique Distribuée

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