Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Abstract the process and a snapshot types with a address_space superclass
[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 <libgen.h>
14
15 #include "mc_process.h"
16 #include "mc_object_info.h"
17 #include "mc_address_space.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
20                                 "MC process information");
21
22 static void MC_process_init_memory_map_info(mc_process_t process);
23 static void MC_process_open_memory_file(mc_process_t process);
24
25 static s_mc_address_space_class_t mc_process_class = {
26   .read = (void*) &MC_process_read
27 };
28
29 void MC_process_init(mc_process_t process, pid_t pid)
30 {
31   process->address_space.address_space_class = &mc_process_class;
32   process->process_flags = MC_PROCESS_NO_FLAG;
33   process->pid = pid;
34   if (pid==getpid())
35     process->process_flags |= MC_PROCESS_SELF_FLAG;
36   process->memory_map = MC_get_memory_map(pid);
37   process->memory_file = -1;
38   process->cache_flags = 0;
39   process->heap = NULL;
40   process->heap_info = NULL;
41   MC_process_init_memory_map_info(process);
42   MC_process_open_memory_file(process);
43
44   // Read std_heap (is a struct mdesc*):
45   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
46   if (!std_heap_var)
47     xbt_die("No heap information in the target process");
48   if(!std_heap_var->address)
49     xbt_die("No constant address for this variable");
50   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
51     &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
52     MC_PROCESS_INDEX_DISABLED);
53 }
54
55 void MC_process_clear(mc_process_t process)
56 {
57   process->address_space.address_space_class = NULL;
58   process->process_flags = MC_PROCESS_NO_FLAG;
59   process->pid = 0;
60
61   MC_free_memory_map(process->memory_map);
62   process->memory_map = NULL;
63
64   process->maestro_stack_start = NULL;
65   process->maestro_stack_end = NULL;
66
67   size_t i;
68   for (i=0; i!=process->object_infos_size; ++i) {
69     MC_free_object_info(&process->object_infos[i]);
70   }
71   free(process->object_infos);
72   process->object_infos = NULL;
73   process->object_infos_size = 0;
74   if (process->memory_file >= 0) {
75     close(process->memory_file);
76   }
77
78   process->cache_flags = 0;
79
80   free(process->heap);
81   process->heap = NULL;
82
83   free(process->heap_info);
84   process->heap_info = NULL;
85 }
86
87 void MC_process_refresh_heap(mc_process_t process)
88 {
89   assert(!MC_process_is_self(process));
90   // Read/dereference/refresh the std_heap pointer:
91   if (!process->heap) {
92     xbt_mheap_t oldheap  = mmalloc_get_current_heap();
93     MC_SET_MC_HEAP;
94     process->heap = malloc(sizeof(struct mdesc));
95     mmalloc_set_current_heap(oldheap);
96   }
97   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
98     process->heap, process->heap_address, sizeof(struct mdesc),
99     MC_PROCESS_INDEX_DISABLED
100     );
101 }
102
103 void MC_process_refresh_malloc_info(mc_process_t process)
104 {
105   assert(!MC_process_is_self(process));
106   if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
107     MC_process_refresh_heap(process);
108   // Refresh process->heapinfo:
109   size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
110
111   xbt_mheap_t oldheap  = mmalloc_get_current_heap();
112   MC_SET_MC_HEAP;
113   process->heap_info = (malloc_info*) realloc(process->heap_info,
114     malloc_info_bytesize);
115   mmalloc_set_current_heap(oldheap);
116
117   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
118     process->heap_info,
119     process->heap->heapinfo, malloc_info_bytesize,
120     MC_PROCESS_INDEX_DISABLED);
121 }
122
123 #define SO_RE "\\.so[\\.0-9]*$"
124 #define VERSION_RE "-[\\.0-9]*$"
125
126 const char* FILTERED_LIBS[] = {
127   "libstdc++",
128   "libc++",
129   "libm",
130   "libgcc_s",
131   "libpthread",
132   "libunwind",
133   "libunwind-x86_64",
134   "libunwind-x86",
135   "libdw",
136   "libdl",
137   "librt",
138   "liblzma",
139   "libelf",
140   "libbz2",
141   "libz",
142   "libelf",
143   "libc",
144   "ld"
145 };
146
147 static bool MC_is_simgrid_lib(const char* libname)
148 {
149   return !strcmp(libname, "libsimgrid");
150 }
151
152 static bool MC_is_filtered_lib(const char* libname)
153 {
154   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
155   size_t i;
156   for (i=0; i!=n; ++i)
157     if (strcmp(libname, FILTERED_LIBS[i])==0)
158       return true;
159   return false;
160 }
161
162 struct s_mc_memory_map_re {
163   regex_t so_re;
164   regex_t version_re;
165 };
166
167 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
168   const char* map_basename = basename((char*) pathname);
169
170   regmatch_t match;
171   if(regexec(&res->so_re, map_basename, 1, &match, 0))
172     return NULL;
173
174   char* libname = strndup(map_basename, match.rm_so);
175
176   // Strip the version suffix:
177   if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
178     char* temp = libname;
179     libname = strndup(temp, match.rm_so);
180     free(temp);
181   }
182
183   return libname;
184 }
185
186 /** @brief Finds the range of the different memory segments and binary paths */
187 static void MC_process_init_memory_map_info(mc_process_t process)
188 {
189   XBT_INFO("Get debug information ...");
190   process->maestro_stack_start = NULL;
191   process->maestro_stack_end = NULL;
192   process->object_infos = NULL;
193   process->object_infos_size = 0;
194   process->binary_info = NULL;
195   process->libsimgrid_info = NULL;
196
197   struct s_mc_memory_map_re res;
198
199   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
200     xbt_die(".so regexp did not compile");
201
202   memory_map_t maps = process->memory_map;
203
204   const char* current_name = NULL;
205
206   size_t i = 0;
207   for (i=0; i < maps->mapsize; i++) {
208     map_region_t reg = &(maps->regions[i]);
209     const char* pathname = maps->regions[i].pathname;
210
211     // Nothing to do
212     if (maps->regions[i].pathname == NULL) {
213       current_name = NULL;
214       continue;
215     }
216
217     // [stack], [vvar], [vsyscall], [vdso] ...
218     if (pathname[0] == '[') {
219       if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
220         process->maestro_stack_start = reg->start_addr;
221         process->maestro_stack_end = reg->end_addr;
222       }
223       current_name = NULL;
224       continue;
225     }
226
227     if (current_name && strcmp(current_name, pathname)==0)
228       continue;
229
230     current_name = pathname;
231     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
232       continue;
233
234     const bool is_executable = !i;
235     char* libname = NULL;
236     if (!is_executable) {
237       libname = MC_get_lib_name(pathname, &res);
238       if(!libname)
239         continue;
240       if (MC_is_filtered_lib(libname)) {
241         free(libname);
242         continue;
243       }
244     }
245
246     mc_object_info_t info =
247       MC_find_object_info(process->memory_map, pathname, is_executable);
248     process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
249       (process->object_infos_size+1) * sizeof(mc_object_info_t*));
250     process->object_infos[process->object_infos_size] = info;
251     process->object_infos_size++;
252     if (is_executable)
253       process->binary_info = info;
254     else if (libname && MC_is_simgrid_lib(libname))
255       process->libsimgrid_info = info;
256     free(libname);
257   }
258
259   regfree(&res.so_re);
260   regfree(&res.version_re);
261
262   // Resolve time (including accress differents objects):
263   for (i=0; i!=process->object_infos_size; ++i)
264     MC_post_process_object_info(process, process->object_infos[i]);
265
266   xbt_assert(process->maestro_stack_start, "maestro_stack_start");
267   xbt_assert(process->maestro_stack_end, "maestro_stack_end");
268
269   XBT_INFO("Get debug information done !");
270 }
271
272 mc_object_info_t MC_process_find_object_info(mc_process_t process, void *ip)
273 {
274   size_t i;
275   for (i = 0; i != process->object_infos_size; ++i) {
276     if (ip >= (void *) process->object_infos[i]->start_exec
277         && ip <= (void *) process->object_infos[i]->end_exec) {
278       return process->object_infos[i];
279     }
280   }
281   return NULL;
282 }
283
284 // Functions, variables…
285
286 dw_frame_t MC_process_find_function(mc_process_t process, void *ip)
287 {
288   mc_object_info_t info = MC_process_find_object_info(process, ip);
289   if (info == NULL)
290     return NULL;
291   else
292     return MC_file_object_info_find_function(info, ip);
293 }
294
295 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
296 {
297   const size_t n = process->object_infos_size;
298   size_t i;
299   for (i=0; i!=n; ++i) {
300     mc_object_info_t info =process->object_infos[i];
301     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
302     if (var)
303       return var;
304   }
305   return NULL;
306 }
307
308 // ***** Memory access
309
310 static void MC_process_open_memory_file(mc_process_t process)
311 {
312   if (MC_process_is_self(process) || process->memory_file >= 0)
313     return;
314
315   const size_t buffer_size = 30;
316   char buffer[buffer_size];
317   int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) process->pid);
318   if (res < 0 || res>= buffer_size) {
319     XBT_ERROR("Could not open memory file descriptor for process %lli",
320       (long long) process->pid);
321     return;
322   }
323
324   int fd = open(buffer, O_RDWR);
325   if (fd<0)
326     xbt_die("Could not initialise memory access for remote process");
327   process->memory_file = fd;
328 }
329
330 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
331 {
332   char* buffer = (char*) buf;
333   ssize_t real_count = count;
334   while (count) {
335     ssize_t res = pread(fd, buffer, count, offset);
336     if (res > 0) {
337       count  -= res;
338       buffer += res;
339       offset += res;
340     } else if (res==0) {
341       return -1;
342     } else if (errno != EINTR) {
343       return -1;
344     }
345   }
346   return real_count;
347 }
348
349 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
350 {
351   const char* buffer = (const char*) buf;
352   ssize_t real_count = count;
353   while (count) {
354     ssize_t res = pwrite(fd, buffer, count, offset);
355     if (res > 0) {
356       count  -= res;
357       buffer += res;
358       offset += res;
359     } else if (res==0) {
360       return -1;
361     } else if (errno != EINTR) {
362       return -1;
363     }
364   }
365   return real_count;
366 }
367
368 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
369   void* local, const void* remote, size_t len,
370   int process_index)
371 {
372   if (process_index != MC_PROCESS_INDEX_DISABLED)
373     xbt_die("Not implemented yet");
374
375   if (MC_process_is_self(process)) {
376     if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
377       return remote;
378     else {
379       memcpy(local, remote, len);
380       return local;
381     }
382   } else {
383     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
384       xbt_die("Read from process %lli failed", (long long) process->pid);
385     return local;
386   }
387 }
388
389 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
390 {
391   if (MC_process_is_self(process)) {
392     memcpy(remote, local, len);
393   } else {
394     if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
395       xbt_die("Write to process %lli failed", (long long) process->pid);
396   }
397 }