Logo AND Algorithmique Numérique Distribuée

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