Logo AND Algorithmique Numérique Distribuée

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