Run --gc-sections tests only if supported.
[deliverable/binutils-gdb.git] / gdb / jit.c
1 /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
2
3 Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21
22 #include "jit.h"
23 #include "breakpoint.h"
24 #include "command.h"
25 #include "gdbcmd.h"
26 #include "gdbcore.h"
27 #include "inferior.h"
28 #include "observer.h"
29 #include "objfiles.h"
30 #include "symfile.h"
31 #include "symtab.h"
32 #include "target.h"
33 #include "gdb_stat.h"
34
35 static const struct objfile_data *jit_objfile_data;
36
37 static const char *const jit_break_name = "__jit_debug_register_code";
38
39 static const char *const jit_descriptor_name = "__jit_debug_descriptor";
40
41 static const struct inferior_data *jit_inferior_data = NULL;
42
43 static void jit_inferior_init (struct gdbarch *gdbarch);
44
45 /* Non-zero if we want to see trace of jit level stuff. */
46
47 static int jit_debug = 0;
48
49 static void
50 show_jit_debug (struct ui_file *file, int from_tty,
51 struct cmd_list_element *c, const char *value)
52 {
53 fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
54 }
55
56 struct target_buffer
57 {
58 CORE_ADDR base;
59 ULONGEST size;
60 };
61
62 /* Openning the file is a no-op. */
63
64 static void *
65 mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
66 {
67 return open_closure;
68 }
69
70 /* Closing the file is just freeing the base/size pair on our side. */
71
72 static int
73 mem_bfd_iovec_close (struct bfd *abfd, void *stream)
74 {
75 xfree (stream);
76 return 1;
77 }
78
79 /* For reading the file, we just need to pass through to target_read_memory and
80 fix up the arguments and return values. */
81
82 static file_ptr
83 mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
84 file_ptr nbytes, file_ptr offset)
85 {
86 int err;
87 struct target_buffer *buffer = (struct target_buffer *) stream;
88
89 /* If this read will read all of the file, limit it to just the rest. */
90 if (offset + nbytes > buffer->size)
91 nbytes = buffer->size - offset;
92
93 /* If there are no more bytes left, we've reached EOF. */
94 if (nbytes == 0)
95 return 0;
96
97 err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
98 if (err)
99 return -1;
100
101 return nbytes;
102 }
103
104 /* For statting the file, we only support the st_size attribute. */
105
106 static int
107 mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
108 {
109 struct target_buffer *buffer = (struct target_buffer*) stream;
110
111 sb->st_size = buffer->size;
112 return 0;
113 }
114
115 /* Open a BFD from the target's memory. */
116
117 static struct bfd *
118 bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
119 {
120 const char *filename = xstrdup ("<in-memory>");
121 struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
122
123 buffer->base = addr;
124 buffer->size = size;
125 return bfd_openr_iovec (filename, target,
126 mem_bfd_iovec_open,
127 buffer,
128 mem_bfd_iovec_pread,
129 mem_bfd_iovec_close,
130 mem_bfd_iovec_stat);
131 }
132
133 /* Per-inferior structure recording the addresses in the inferior. */
134
135 struct jit_inferior_data
136 {
137 CORE_ADDR breakpoint_addr; /* &__jit_debug_register_code() */
138 CORE_ADDR descriptor_addr; /* &__jit_debug_descriptor */
139 };
140
141 /* Return jit_inferior_data for current inferior. Allocate if not already
142 present. */
143
144 static struct jit_inferior_data *
145 get_jit_inferior_data (void)
146 {
147 struct inferior *inf;
148 struct jit_inferior_data *inf_data;
149
150 inf = current_inferior ();
151 inf_data = inferior_data (inf, jit_inferior_data);
152 if (inf_data == NULL)
153 {
154 inf_data = XZALLOC (struct jit_inferior_data);
155 set_inferior_data (inf, jit_inferior_data, inf_data);
156 }
157
158 return inf_data;
159 }
160
161 static void
162 jit_inferior_data_cleanup (struct inferior *inf, void *arg)
163 {
164 xfree (arg);
165 }
166
167 /* Helper function for reading the global JIT descriptor from remote
168 memory. */
169
170 static void
171 jit_read_descriptor (struct gdbarch *gdbarch,
172 struct jit_descriptor *descriptor,
173 CORE_ADDR descriptor_addr)
174 {
175 int err;
176 struct type *ptr_type;
177 int ptr_size;
178 int desc_size;
179 gdb_byte *desc_buf;
180 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
181
182 /* Figure out how big the descriptor is on the remote and how to read it. */
183 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
184 ptr_size = TYPE_LENGTH (ptr_type);
185 desc_size = 8 + 2 * ptr_size; /* Two 32-bit ints and two pointers. */
186 desc_buf = alloca (desc_size);
187
188 /* Read the descriptor. */
189 err = target_read_memory (descriptor_addr, desc_buf, desc_size);
190 if (err)
191 error (_("Unable to read JIT descriptor from remote memory!"));
192
193 /* Fix the endianness to match the host. */
194 descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
195 descriptor->action_flag =
196 extract_unsigned_integer (&desc_buf[4], 4, byte_order);
197 descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
198 descriptor->first_entry =
199 extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
200 }
201
202 /* Helper function for reading a JITed code entry from remote memory. */
203
204 static void
205 jit_read_code_entry (struct gdbarch *gdbarch,
206 CORE_ADDR code_addr, struct jit_code_entry *code_entry)
207 {
208 int err;
209 struct type *ptr_type;
210 int ptr_size;
211 int entry_size;
212 gdb_byte *entry_buf;
213 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
214
215 /* Figure out how big the entry is on the remote and how to read it. */
216 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
217 ptr_size = TYPE_LENGTH (ptr_type);
218 entry_size = 3 * ptr_size + 8; /* Three pointers and one 64-bit int. */
219 entry_buf = alloca (entry_size);
220
221 /* Read the entry. */
222 err = target_read_memory (code_addr, entry_buf, entry_size);
223 if (err)
224 error (_("Unable to read JIT code entry from remote memory!"));
225
226 /* Fix the endianness to match the host. */
227 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
228 code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
229 code_entry->prev_entry =
230 extract_typed_address (&entry_buf[ptr_size], ptr_type);
231 code_entry->symfile_addr =
232 extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
233 code_entry->symfile_size =
234 extract_unsigned_integer (&entry_buf[3 * ptr_size], 8, byte_order);
235 }
236
237 /* This function registers code associated with a JIT code entry. It uses the
238 pointer and size pair in the entry to read the symbol file from the remote
239 and then calls symbol_file_add_from_local_memory to add it as though it were
240 a symbol file added by the user. */
241
242 static void
243 jit_register_code (struct gdbarch *gdbarch,
244 CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
245 {
246 bfd *nbfd;
247 struct section_addr_info *sai;
248 struct bfd_section *sec;
249 struct objfile *objfile;
250 struct cleanup *old_cleanups, *my_cleanups;
251 int i;
252 const struct bfd_arch_info *b;
253 CORE_ADDR *entry_addr_ptr;
254
255 if (jit_debug)
256 fprintf_unfiltered (gdb_stdlog,
257 "jit_register_code, symfile_addr = %s, "
258 "symfile_size = %s\n",
259 paddress (gdbarch, code_entry->symfile_addr),
260 pulongest (code_entry->symfile_size));
261
262 nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
263 code_entry->symfile_size, gnutarget);
264 old_cleanups = make_cleanup_bfd_close (nbfd);
265
266 /* Check the format. NOTE: This initializes important data that GDB uses!
267 We would segfault later without this line. */
268 if (!bfd_check_format (nbfd, bfd_object))
269 {
270 printf_unfiltered (_("\
271 JITed symbol file is not an object file, ignoring it.\n"));
272 do_cleanups (old_cleanups);
273 return;
274 }
275
276 /* Check bfd arch. */
277 b = gdbarch_bfd_arch_info (gdbarch);
278 if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
279 warning (_("JITed object file architecture %s is not compatible "
280 "with target architecture %s."), bfd_get_arch_info
281 (nbfd)->printable_name, b->printable_name);
282
283 /* Read the section address information out of the symbol file. Since the
284 file is generated by the JIT at runtime, it should all of the absolute
285 addresses that we care about. */
286 sai = alloc_section_addr_info (bfd_count_sections (nbfd));
287 make_cleanup_free_section_addr_info (sai);
288 i = 0;
289 for (sec = nbfd->sections; sec != NULL; sec = sec->next)
290 if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
291 {
292 /* We assume that these virtual addresses are absolute, and do not
293 treat them as offsets. */
294 sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
295 sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
296 sai->other[i].sectindex = sec->index;
297 ++i;
298 }
299
300 /* This call takes ownership of sai. */
301 objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
302
303 /* Remember a mapping from entry_addr to objfile. */
304 entry_addr_ptr = xmalloc (sizeof (CORE_ADDR));
305 *entry_addr_ptr = entry_addr;
306 set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr);
307
308 discard_cleanups (old_cleanups);
309 }
310
311 /* This function unregisters JITed code and frees the corresponding
312 objfile. */
313
314 static void
315 jit_unregister_code (struct objfile *objfile)
316 {
317 free_objfile (objfile);
318 }
319
320 /* Look up the objfile with this code entry address. */
321
322 static struct objfile *
323 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
324 {
325 struct objfile *objf;
326 CORE_ADDR *objf_entry_addr;
327
328 ALL_OBJFILES (objf)
329 {
330 objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data);
331 if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr)
332 return objf;
333 }
334 return NULL;
335 }
336
337 /* (Re-)Initialize the jit breakpoint if necessary.
338 Return 0 on success. */
339
340 static int
341 jit_breakpoint_re_set_internal (struct gdbarch *gdbarch,
342 struct jit_inferior_data *inf_data)
343 {
344 if (inf_data->breakpoint_addr == 0)
345 {
346 struct minimal_symbol *reg_symbol;
347
348 /* Lookup the registration symbol. If it is missing, then we assume
349 we are not attached to a JIT. */
350 reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL);
351 if (reg_symbol == NULL)
352 return 1;
353 inf_data->breakpoint_addr = SYMBOL_VALUE_ADDRESS (reg_symbol);
354 if (inf_data->breakpoint_addr == 0)
355 return 2;
356
357 /* If we have not read the jit descriptor yet (e.g. because the JITer
358 itself is in a shared library which just got loaded), do so now. */
359 if (inf_data->descriptor_addr == 0)
360 jit_inferior_init (gdbarch);
361 }
362 else
363 return 0;
364
365 if (jit_debug)
366 fprintf_unfiltered (gdb_stdlog,
367 "jit_breakpoint_re_set_internal, "
368 "breakpoint_addr = %s\n",
369 paddress (gdbarch, inf_data->breakpoint_addr));
370
371 /* Put a breakpoint in the registration symbol. */
372 create_jit_event_breakpoint (gdbarch, inf_data->breakpoint_addr);
373
374 return 0;
375 }
376
377 /* Register any already created translations. */
378
379 static void
380 jit_inferior_init (struct gdbarch *gdbarch)
381 {
382 struct jit_descriptor descriptor;
383 struct jit_code_entry cur_entry;
384 struct jit_inferior_data *inf_data;
385 CORE_ADDR cur_entry_addr;
386
387 if (jit_debug)
388 fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n");
389
390 inf_data = get_jit_inferior_data ();
391 if (jit_breakpoint_re_set_internal (gdbarch, inf_data) != 0)
392 return;
393
394 if (inf_data->descriptor_addr == 0)
395 {
396 struct minimal_symbol *desc_symbol;
397
398 /* Lookup the descriptor symbol and cache the addr. If it is
399 missing, we assume we are not attached to a JIT and return early. */
400 desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL);
401 if (desc_symbol == NULL)
402 return;
403
404 inf_data->descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol);
405 if (inf_data->descriptor_addr == 0)
406 return;
407 }
408
409 if (jit_debug)
410 fprintf_unfiltered (gdb_stdlog,
411 "jit_inferior_init, descriptor_addr = %s\n",
412 paddress (gdbarch, inf_data->descriptor_addr));
413
414 /* Read the descriptor so we can check the version number and load
415 any already JITed functions. */
416 jit_read_descriptor (gdbarch, &descriptor, inf_data->descriptor_addr);
417
418 /* Check that the version number agrees with that we support. */
419 if (descriptor.version != 1)
420 error (_("Unsupported JIT protocol version in descriptor!"));
421
422 /* If we've attached to a running program, we need to check the descriptor
423 to register any functions that were already generated. */
424 for (cur_entry_addr = descriptor.first_entry;
425 cur_entry_addr != 0;
426 cur_entry_addr = cur_entry.next_entry)
427 {
428 jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
429
430 /* This hook may be called many times during setup, so make sure we don't
431 add the same symbol file twice. */
432 if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
433 continue;
434
435 jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
436 }
437 }
438
439 /* Exported routine to call when an inferior has been created. */
440
441 void
442 jit_inferior_created_hook (void)
443 {
444 jit_inferior_init (target_gdbarch);
445 }
446
447 /* Exported routine to call to re-set the jit breakpoints,
448 e.g. when a program is rerun. */
449
450 void
451 jit_breakpoint_re_set (void)
452 {
453 jit_breakpoint_re_set_internal (target_gdbarch,
454 get_jit_inferior_data ());
455 }
456
457 /* Reset inferior_data, so sybols will be looked up again, and jit_breakpoint
458 will be reset. */
459
460 static void
461 jit_reset_inferior_data_and_breakpoints (void)
462 {
463 struct jit_inferior_data *inf_data;
464
465 /* Force jit_inferior_init to re-lookup of jit symbol addresses. */
466 inf_data = get_jit_inferior_data ();
467 inf_data->breakpoint_addr = 0;
468 inf_data->descriptor_addr = 0;
469
470 /* Remove any existing JIT breakpoint(s). */
471 remove_jit_event_breakpoints ();
472
473 jit_inferior_init (target_gdbarch);
474 }
475
476 /* Wrapper to match the observer function pointer prototype. */
477
478 static void
479 jit_inferior_created_observer (struct target_ops *objfile, int from_tty)
480 {
481 jit_reset_inferior_data_and_breakpoints ();
482 }
483
484 /* This function cleans up any code entries left over when the
485 inferior exits. We get left over code when the inferior exits
486 without unregistering its code, for example when it crashes. */
487
488 static void
489 jit_inferior_exit_hook (struct inferior *inf)
490 {
491 struct objfile *objf;
492 struct objfile *temp;
493
494 ALL_OBJFILES_SAFE (objf, temp)
495 if (objfile_data (objf, jit_objfile_data) != NULL)
496 jit_unregister_code (objf);
497 }
498
499 static void
500 jit_executable_changed_observer (void)
501 {
502 jit_reset_inferior_data_and_breakpoints ();
503 }
504
505 void
506 jit_event_handler (struct gdbarch *gdbarch)
507 {
508 struct jit_descriptor descriptor;
509 struct jit_code_entry code_entry;
510 CORE_ADDR entry_addr;
511 struct objfile *objf;
512
513 /* Read the descriptor from remote memory. */
514 jit_read_descriptor (gdbarch, &descriptor,
515 get_jit_inferior_data ()->descriptor_addr);
516 entry_addr = descriptor.relevant_entry;
517
518 /* Do the corresponding action. */
519 switch (descriptor.action_flag)
520 {
521 case JIT_NOACTION:
522 break;
523 case JIT_REGISTER:
524 jit_read_code_entry (gdbarch, entry_addr, &code_entry);
525 jit_register_code (gdbarch, entry_addr, &code_entry);
526 break;
527 case JIT_UNREGISTER:
528 objf = jit_find_objf_with_entry_addr (entry_addr);
529 if (objf == NULL)
530 printf_unfiltered (_("Unable to find JITed code "
531 "entry at address: %s\n"),
532 paddress (gdbarch, entry_addr));
533 else
534 jit_unregister_code (objf);
535
536 break;
537 default:
538 error (_("Unknown action_flag value in JIT descriptor!"));
539 break;
540 }
541 }
542
543 /* Provide a prototype to silence -Wmissing-prototypes. */
544
545 extern void _initialize_jit (void);
546
547 void
548 _initialize_jit (void)
549 {
550 add_setshow_zinteger_cmd ("jit", class_maintenance, &jit_debug,
551 _("Set JIT debugging."),
552 _("Show JIT debugging."),
553 _("When non-zero, JIT debugging is enabled."),
554 NULL,
555 show_jit_debug,
556 &setdebuglist, &showdebuglist);
557
558 observer_attach_inferior_created (jit_inferior_created_observer);
559 observer_attach_inferior_exit (jit_inferior_exit_hook);
560 observer_attach_executable_changed (jit_executable_changed_observer);
561 jit_objfile_data = register_objfile_data ();
562 jit_inferior_data =
563 register_inferior_data_with_cleanup (jit_inferior_data_cleanup);
564 }
This page took 0.05426 seconds and 4 git commands to generate.