1 /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
3 Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "breakpoint.h"
35 static const struct objfile_data *jit_objfile_data;
37 static const char *const jit_break_name = "__jit_debug_register_code";
39 static const char *const jit_descriptor_name = "__jit_debug_descriptor";
41 static const struct inferior_data *jit_inferior_data = NULL;
43 static void jit_inferior_init (struct gdbarch *gdbarch);
45 /* Non-zero if we want to see trace of jit level stuff. */
47 static int jit_debug = 0;
50 show_jit_debug (struct ui_file *file, int from_tty,
51 struct cmd_list_element *c, const char *value)
53 fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
62 /* Openning the file is a no-op. */
65 mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
70 /* Closing the file is just freeing the base/size pair on our side. */
73 mem_bfd_iovec_close (struct bfd *abfd, void *stream)
79 /* For reading the file, we just need to pass through to target_read_memory and
80 fix up the arguments and return values. */
83 mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
84 file_ptr nbytes, file_ptr offset)
87 struct target_buffer *buffer = (struct target_buffer *) stream;
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;
93 /* If there are no more bytes left, we've reached EOF. */
97 err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
104 /* For statting the file, we only support the st_size attribute. */
107 mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
109 struct target_buffer *buffer = (struct target_buffer*) stream;
111 sb->st_size = buffer->size;
115 /* Open a BFD from the target's memory. */
118 bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
120 const char *filename = xstrdup ("<in-memory>");
121 struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
125 return bfd_openr_iovec (filename, target,
133 /* Per-inferior structure recording the addresses in the inferior. */
135 struct jit_inferior_data
137 CORE_ADDR breakpoint_addr; /* &__jit_debug_register_code() */
138 CORE_ADDR descriptor_addr; /* &__jit_debug_descriptor */
141 /* Return jit_inferior_data for current inferior. Allocate if not already
144 static struct jit_inferior_data *
145 get_jit_inferior_data (void)
147 struct inferior *inf;
148 struct jit_inferior_data *inf_data;
150 inf = current_inferior ();
151 inf_data = inferior_data (inf, jit_inferior_data);
152 if (inf_data == NULL)
154 inf_data = XZALLOC (struct jit_inferior_data);
155 set_inferior_data (inf, jit_inferior_data, inf_data);
162 jit_inferior_data_cleanup (struct inferior *inf, void *arg)
167 /* Helper function for reading the global JIT descriptor from remote
171 jit_read_descriptor (struct gdbarch *gdbarch,
172 struct jit_descriptor *descriptor,
173 CORE_ADDR descriptor_addr)
176 struct type *ptr_type;
180 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
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);
188 /* Read the descriptor. */
189 err = target_read_memory (descriptor_addr, desc_buf, desc_size);
191 error (_("Unable to read JIT descriptor from remote memory!"));
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);
202 /* Helper function for reading a JITed code entry from remote memory. */
205 jit_read_code_entry (struct gdbarch *gdbarch,
206 CORE_ADDR code_addr, struct jit_code_entry *code_entry)
209 struct type *ptr_type;
214 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
216 /* Figure out how big the entry is on the remote and how to read it. */
217 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
218 ptr_size = TYPE_LENGTH (ptr_type);
219 entry_size = 3 * ptr_size + 8; /* Three pointers and one 64-bit int. */
220 entry_buf = alloca (entry_size);
222 /* Read the entry. */
223 err = target_read_memory (code_addr, entry_buf, entry_size);
225 error (_("Unable to read JIT code entry from remote memory!"));
227 /* Fix the endianness to match the host. */
228 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
229 code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
230 code_entry->prev_entry =
231 extract_typed_address (&entry_buf[ptr_size], ptr_type);
232 code_entry->symfile_addr =
233 extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
235 align_bytes = gdbarch_long_long_align_bit (gdbarch) / 8;
237 off = (off + (align_bytes - 1)) & ~(align_bytes - 1);
239 code_entry->symfile_size =
240 extract_unsigned_integer (&entry_buf[off], 8, byte_order);
243 /* This function registers code associated with a JIT code entry. It uses the
244 pointer and size pair in the entry to read the symbol file from the remote
245 and then calls symbol_file_add_from_local_memory to add it as though it were
246 a symbol file added by the user. */
249 jit_register_code (struct gdbarch *gdbarch,
250 CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
253 struct section_addr_info *sai;
254 struct bfd_section *sec;
255 struct objfile *objfile;
256 struct cleanup *old_cleanups, *my_cleanups;
258 const struct bfd_arch_info *b;
259 CORE_ADDR *entry_addr_ptr;
262 fprintf_unfiltered (gdb_stdlog,
263 "jit_register_code, symfile_addr = %s, "
264 "symfile_size = %s\n",
265 paddress (gdbarch, code_entry->symfile_addr),
266 pulongest (code_entry->symfile_size));
268 nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
269 code_entry->symfile_size, gnutarget);
270 old_cleanups = make_cleanup_bfd_close (nbfd);
272 /* Check the format. NOTE: This initializes important data that GDB uses!
273 We would segfault later without this line. */
274 if (!bfd_check_format (nbfd, bfd_object))
276 printf_unfiltered (_("\
277 JITed symbol file is not an object file, ignoring it.\n"));
278 do_cleanups (old_cleanups);
282 /* Check bfd arch. */
283 b = gdbarch_bfd_arch_info (gdbarch);
284 if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
285 warning (_("JITed object file architecture %s is not compatible "
286 "with target architecture %s."), bfd_get_arch_info
287 (nbfd)->printable_name, b->printable_name);
289 /* Read the section address information out of the symbol file. Since the
290 file is generated by the JIT at runtime, it should all of the absolute
291 addresses that we care about. */
292 sai = alloc_section_addr_info (bfd_count_sections (nbfd));
293 make_cleanup_free_section_addr_info (sai);
295 for (sec = nbfd->sections; sec != NULL; sec = sec->next)
296 if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
298 /* We assume that these virtual addresses are absolute, and do not
299 treat them as offsets. */
300 sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
301 sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
302 sai->other[i].sectindex = sec->index;
306 /* This call takes ownership of sai. */
307 objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
309 /* Remember a mapping from entry_addr to objfile. */
310 entry_addr_ptr = xmalloc (sizeof (CORE_ADDR));
311 *entry_addr_ptr = entry_addr;
312 set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr);
314 discard_cleanups (old_cleanups);
317 /* This function unregisters JITed code and frees the corresponding
321 jit_unregister_code (struct objfile *objfile)
323 free_objfile (objfile);
326 /* Look up the objfile with this code entry address. */
328 static struct objfile *
329 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
331 struct objfile *objf;
332 CORE_ADDR *objf_entry_addr;
336 objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data);
337 if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr)
343 /* (Re-)Initialize the jit breakpoint if necessary.
344 Return 0 on success. */
347 jit_breakpoint_re_set_internal (struct gdbarch *gdbarch,
348 struct jit_inferior_data *inf_data)
350 if (inf_data->breakpoint_addr == 0)
352 struct minimal_symbol *reg_symbol;
354 /* Lookup the registration symbol. If it is missing, then we assume
355 we are not attached to a JIT. */
356 reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL);
357 if (reg_symbol == NULL)
359 inf_data->breakpoint_addr = SYMBOL_VALUE_ADDRESS (reg_symbol);
360 if (inf_data->breakpoint_addr == 0)
363 /* If we have not read the jit descriptor yet (e.g. because the JITer
364 itself is in a shared library which just got loaded), do so now. */
365 if (inf_data->descriptor_addr == 0)
366 jit_inferior_init (gdbarch);
372 fprintf_unfiltered (gdb_stdlog,
373 "jit_breakpoint_re_set_internal, "
374 "breakpoint_addr = %s\n",
375 paddress (gdbarch, inf_data->breakpoint_addr));
377 /* Put a breakpoint in the registration symbol. */
378 create_jit_event_breakpoint (gdbarch, inf_data->breakpoint_addr);
383 /* Register any already created translations. */
386 jit_inferior_init (struct gdbarch *gdbarch)
388 struct jit_descriptor descriptor;
389 struct jit_code_entry cur_entry;
390 struct jit_inferior_data *inf_data;
391 CORE_ADDR cur_entry_addr;
394 fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n");
396 inf_data = get_jit_inferior_data ();
397 if (jit_breakpoint_re_set_internal (gdbarch, inf_data) != 0)
400 if (inf_data->descriptor_addr == 0)
402 struct minimal_symbol *desc_symbol;
404 /* Lookup the descriptor symbol and cache the addr. If it is
405 missing, we assume we are not attached to a JIT and return early. */
406 desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL);
407 if (desc_symbol == NULL)
410 inf_data->descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol);
411 if (inf_data->descriptor_addr == 0)
416 fprintf_unfiltered (gdb_stdlog,
417 "jit_inferior_init, descriptor_addr = %s\n",
418 paddress (gdbarch, inf_data->descriptor_addr));
420 /* Read the descriptor so we can check the version number and load
421 any already JITed functions. */
422 jit_read_descriptor (gdbarch, &descriptor, inf_data->descriptor_addr);
424 /* Check that the version number agrees with that we support. */
425 if (descriptor.version != 1)
426 error (_("Unsupported JIT protocol version in descriptor!"));
428 /* If we've attached to a running program, we need to check the descriptor
429 to register any functions that were already generated. */
430 for (cur_entry_addr = descriptor.first_entry;
432 cur_entry_addr = cur_entry.next_entry)
434 jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
436 /* This hook may be called many times during setup, so make sure we don't
437 add the same symbol file twice. */
438 if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
441 jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
445 /* Exported routine to call when an inferior has been created. */
448 jit_inferior_created_hook (void)
450 jit_inferior_init (target_gdbarch);
453 /* Exported routine to call to re-set the jit breakpoints,
454 e.g. when a program is rerun. */
457 jit_breakpoint_re_set (void)
459 jit_breakpoint_re_set_internal (target_gdbarch,
460 get_jit_inferior_data ());
463 /* Reset inferior_data, so sybols will be looked up again, and jit_breakpoint
467 jit_reset_inferior_data_and_breakpoints (void)
469 struct jit_inferior_data *inf_data;
471 /* Force jit_inferior_init to re-lookup of jit symbol addresses. */
472 inf_data = get_jit_inferior_data ();
473 inf_data->breakpoint_addr = 0;
474 inf_data->descriptor_addr = 0;
476 /* Remove any existing JIT breakpoint(s). */
477 remove_jit_event_breakpoints ();
479 jit_inferior_init (target_gdbarch);
482 /* Wrapper to match the observer function pointer prototype. */
485 jit_inferior_created_observer (struct target_ops *objfile, int from_tty)
487 jit_reset_inferior_data_and_breakpoints ();
490 /* This function cleans up any code entries left over when the
491 inferior exits. We get left over code when the inferior exits
492 without unregistering its code, for example when it crashes. */
495 jit_inferior_exit_hook (struct inferior *inf)
497 struct objfile *objf;
498 struct objfile *temp;
500 ALL_OBJFILES_SAFE (objf, temp)
501 if (objfile_data (objf, jit_objfile_data) != NULL)
502 jit_unregister_code (objf);
506 jit_executable_changed_observer (void)
508 jit_reset_inferior_data_and_breakpoints ();
512 jit_event_handler (struct gdbarch *gdbarch)
514 struct jit_descriptor descriptor;
515 struct jit_code_entry code_entry;
516 CORE_ADDR entry_addr;
517 struct objfile *objf;
519 /* Read the descriptor from remote memory. */
520 jit_read_descriptor (gdbarch, &descriptor,
521 get_jit_inferior_data ()->descriptor_addr);
522 entry_addr = descriptor.relevant_entry;
524 /* Do the corresponding action. */
525 switch (descriptor.action_flag)
530 jit_read_code_entry (gdbarch, entry_addr, &code_entry);
531 jit_register_code (gdbarch, entry_addr, &code_entry);
534 objf = jit_find_objf_with_entry_addr (entry_addr);
536 printf_unfiltered (_("Unable to find JITed code "
537 "entry at address: %s\n"),
538 paddress (gdbarch, entry_addr));
540 jit_unregister_code (objf);
544 error (_("Unknown action_flag value in JIT descriptor!"));
549 /* Provide a prototype to silence -Wmissing-prototypes. */
551 extern void _initialize_jit (void);
554 _initialize_jit (void)
556 add_setshow_zinteger_cmd ("jit", class_maintenance, &jit_debug,
557 _("Set JIT debugging."),
558 _("Show JIT debugging."),
559 _("When non-zero, JIT debugging is enabled."),
562 &setdebuglist, &showdebuglist);
564 observer_attach_inferior_created (jit_inferior_created_observer);
565 observer_attach_inferior_exit (jit_inferior_exit_hook);
566 observer_attach_executable_changed (jit_executable_changed_observer);
567 jit_objfile_data = register_objfile_data ();
569 register_inferior_data_with_cleanup (jit_inferior_data_cleanup);