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 "jit-reader.h"
25 #include "breakpoint.h"
27 #include "dictionary.h"
28 #include "frame-unwind.h"
37 #include "gdb-dlfcn.h"
39 #include "exceptions.h"
41 static const char *jit_reader_dir = NULL;
43 static const struct objfile_data *jit_objfile_data;
45 static const char *const jit_break_name = "__jit_debug_register_code";
47 static const char *const jit_descriptor_name = "__jit_debug_descriptor";
49 static const struct inferior_data *jit_inferior_data = NULL;
51 static void jit_inferior_init (struct gdbarch *gdbarch);
53 /* Non-zero if we want to see trace of jit level stuff. */
55 static int jit_debug = 0;
58 show_jit_debug (struct ui_file *file, int from_tty,
59 struct cmd_list_element *c, const char *value)
61 fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
70 /* Openning the file is a no-op. */
73 mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
78 /* Closing the file is just freeing the base/size pair on our side. */
81 mem_bfd_iovec_close (struct bfd *abfd, void *stream)
87 /* For reading the file, we just need to pass through to target_read_memory and
88 fix up the arguments and return values. */
91 mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
92 file_ptr nbytes, file_ptr offset)
95 struct target_buffer *buffer = (struct target_buffer *) stream;
97 /* If this read will read all of the file, limit it to just the rest. */
98 if (offset + nbytes > buffer->size)
99 nbytes = buffer->size - offset;
101 /* If there are no more bytes left, we've reached EOF. */
105 err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
112 /* For statting the file, we only support the st_size attribute. */
115 mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
117 struct target_buffer *buffer = (struct target_buffer*) stream;
119 sb->st_size = buffer->size;
123 /* One reader that has been loaded successfully, and can potentially be used to
126 static struct jit_reader
128 struct gdb_reader_funcs *functions;
130 } *loaded_jit_reader = NULL;
132 typedef struct gdb_reader_funcs * (reader_init_fn_type) (void);
133 static const char *reader_init_fn_sym = "gdb_init_reader";
135 /* Try to load FILE_NAME as a JIT debug info reader. */
137 static struct jit_reader *
138 jit_reader_load (const char *file_name)
141 reader_init_fn_type *init_fn;
142 struct jit_reader *new_reader = NULL;
143 struct gdb_reader_funcs *funcs = NULL;
144 struct cleanup *old_cleanups;
147 fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"),
149 so = gdb_dlopen (file_name);
150 old_cleanups = make_cleanup_dlclose (so);
152 init_fn = gdb_dlsym (so, reader_init_fn_sym);
154 error (_("Could not locate initialization function: %s."),
157 if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL)
158 error (_("Reader not GPL compatible."));
161 if (funcs->reader_version != GDB_READER_INTERFACE_VERSION)
162 error (_("Reader version does not match GDB version."));
164 new_reader = XZALLOC (struct jit_reader);
165 new_reader->functions = funcs;
166 new_reader->handle = so;
168 discard_cleanups (old_cleanups);
172 /* Provides the jit-reader-load command. */
175 jit_reader_load_command (char *args, int from_tty)
179 struct cleanup *prev_cleanup;
182 error (_("No reader name provided."));
184 if (loaded_jit_reader != NULL)
185 error (_("JIT reader already loaded. Run jit-reader-unload first."));
187 so_name = xstrprintf ("%s/%s", jit_reader_dir, args);
188 prev_cleanup = make_cleanup (xfree, so_name);
190 loaded_jit_reader = jit_reader_load (so_name);
191 do_cleanups (prev_cleanup);
194 /* Provides the jit-reader-unload command. */
197 jit_reader_unload_command (char *args, int from_tty)
199 if (!loaded_jit_reader)
200 error (_("No JIT reader loaded."));
202 loaded_jit_reader->functions->destroy (loaded_jit_reader->functions);
204 gdb_dlclose (loaded_jit_reader->handle);
205 xfree (loaded_jit_reader);
206 loaded_jit_reader = NULL;
209 /* Open a BFD from the target's memory. */
212 bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
214 const char *filename = xstrdup ("<in-memory>");
215 struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
219 return bfd_openr_iovec (filename, target,
227 /* Per-inferior structure recording the addresses in the inferior. */
229 struct jit_inferior_data
231 CORE_ADDR breakpoint_addr; /* &__jit_debug_register_code() */
232 CORE_ADDR descriptor_addr; /* &__jit_debug_descriptor */
235 /* Remember a mapping from entry_addr to objfile. */
238 add_objfile_entry (struct objfile *objfile, CORE_ADDR entry)
240 CORE_ADDR *entry_addr_ptr;
242 entry_addr_ptr = xmalloc (sizeof (CORE_ADDR));
243 *entry_addr_ptr = entry;
244 set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr);
247 /* Return jit_inferior_data for current inferior. Allocate if not already
250 static struct jit_inferior_data *
251 get_jit_inferior_data (void)
253 struct inferior *inf;
254 struct jit_inferior_data *inf_data;
256 inf = current_inferior ();
257 inf_data = inferior_data (inf, jit_inferior_data);
258 if (inf_data == NULL)
260 inf_data = XZALLOC (struct jit_inferior_data);
261 set_inferior_data (inf, jit_inferior_data, inf_data);
268 jit_inferior_data_cleanup (struct inferior *inf, void *arg)
273 /* Helper function for reading the global JIT descriptor from remote
277 jit_read_descriptor (struct gdbarch *gdbarch,
278 struct jit_descriptor *descriptor,
279 CORE_ADDR descriptor_addr)
282 struct type *ptr_type;
286 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
288 /* Figure out how big the descriptor is on the remote and how to read it. */
289 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
290 ptr_size = TYPE_LENGTH (ptr_type);
291 desc_size = 8 + 2 * ptr_size; /* Two 32-bit ints and two pointers. */
292 desc_buf = alloca (desc_size);
294 /* Read the descriptor. */
295 err = target_read_memory (descriptor_addr, desc_buf, desc_size);
297 error (_("Unable to read JIT descriptor from remote memory!"));
299 /* Fix the endianness to match the host. */
300 descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
301 descriptor->action_flag =
302 extract_unsigned_integer (&desc_buf[4], 4, byte_order);
303 descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
304 descriptor->first_entry =
305 extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
308 /* Helper function for reading a JITed code entry from remote memory. */
311 jit_read_code_entry (struct gdbarch *gdbarch,
312 CORE_ADDR code_addr, struct jit_code_entry *code_entry)
315 struct type *ptr_type;
320 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
322 /* Figure out how big the entry is on the remote and how to read it. */
323 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
324 ptr_size = TYPE_LENGTH (ptr_type);
325 entry_size = 3 * ptr_size + 8; /* Three pointers and one 64-bit int. */
326 entry_buf = alloca (entry_size);
328 /* Read the entry. */
329 err = target_read_memory (code_addr, entry_buf, entry_size);
331 error (_("Unable to read JIT code entry from remote memory!"));
333 /* Fix the endianness to match the host. */
334 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
335 code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
336 code_entry->prev_entry =
337 extract_typed_address (&entry_buf[ptr_size], ptr_type);
338 code_entry->symfile_addr =
339 extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
341 align_bytes = gdbarch_long_long_align_bit (gdbarch) / 8;
343 off = (off + (align_bytes - 1)) & ~(align_bytes - 1);
345 code_entry->symfile_size =
346 extract_unsigned_integer (&entry_buf[off], 8, byte_order);
349 /* Proxy object for building a block. */
353 /* gdb_blocks are linked into a tree structure. Next points to the
354 next node at the same depth as this block and parent to the
356 struct gdb_block *next, *parent;
358 /* Points to the "real" block that is being built out of this
359 instance. This block will be added to a blockvector, which will
360 then be added to a symtab. */
361 struct block *real_block;
363 /* The first and last code address corresponding to this block. */
364 CORE_ADDR begin, end;
366 /* The name of this block (if any). If this is non-NULL, the
367 FUNCTION symbol symbol is set to this value. */
371 /* Proxy object for building a symtab. */
375 /* The list of blocks in this symtab. These will eventually be
376 converted to real blocks. */
377 struct gdb_block *blocks;
379 /* The number of blocks inserted. */
382 /* A mapping between line numbers to PC. */
383 struct linetable *linetable;
385 /* The source file for this symtab. */
386 const char *file_name;
387 struct gdb_symtab *next;
390 /* Proxy object for building an object. */
394 struct gdb_symtab *symtabs;
397 /* The type of the `private' data passed around by the callback
400 typedef CORE_ADDR jit_dbg_reader_data;
402 /* The reader calls into this function to read data off the targets
405 static enum gdb_status
406 jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len)
408 int result = target_read_memory ((CORE_ADDR) target_mem, gdb_buf, len);
415 /* The reader calls into this function to create a new gdb_object
416 which it can then pass around to the other callbacks. Right now,
417 all that is required is allocating the memory. */
419 static struct gdb_object *
420 jit_object_open_impl (struct gdb_symbol_callbacks *cb)
422 /* CB is not required right now, but sometime in the future we might
423 need a handle to it, and we'd like to do that without breaking
425 return XZALLOC (struct gdb_object);
428 /* Readers call into this function to open a new gdb_symtab, which,
429 again, is passed around to other callbacks. */
431 static struct gdb_symtab *
432 jit_symtab_open_impl (struct gdb_symbol_callbacks *cb,
433 struct gdb_object *object,
434 const char *file_name)
436 struct gdb_symtab *ret;
438 /* CB stays unused. See comment in jit_object_open_impl. */
440 ret = XZALLOC (struct gdb_symtab);
441 ret->file_name = file_name ? xstrdup (file_name) : xstrdup ("");
442 ret->next = object->symtabs;
443 object->symtabs = ret;
447 /* Returns true if the block corresponding to old should be placed
448 before the block corresponding to new in the final blockvector. */
451 compare_block (const struct gdb_block *const old,
452 const struct gdb_block *const new)
456 if (old->begin < new->begin)
458 else if (old->begin == new->begin)
460 if (old->end > new->end)
469 /* Called by readers to open a new gdb_block. This function also
470 inserts the new gdb_block in the correct place in the corresponding
473 static struct gdb_block *
474 jit_block_open_impl (struct gdb_symbol_callbacks *cb,
475 struct gdb_symtab *symtab, struct gdb_block *parent,
476 GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name)
478 struct gdb_block *block = XZALLOC (struct gdb_block);
480 block->next = symtab->blocks;
481 block->begin = (CORE_ADDR) begin;
482 block->end = (CORE_ADDR) end;
483 block->name = name ? xstrdup (name) : NULL;
484 block->parent = parent;
486 /* Ensure that the blocks are inserted in the correct (reverse of
487 the order expected by blockvector). */
488 if (compare_block (symtab->blocks, block))
490 symtab->blocks = block;
494 struct gdb_block *i = symtab->blocks;
498 /* Guaranteed to terminate, since compare_block (NULL, _)
500 if (compare_block (i->next, block))
502 block->next = i->next;
513 /* Readers call this to add a line mapping (from PC to line number) to
517 jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb,
518 struct gdb_symtab *stab, int nlines,
519 struct gdb_line_mapping *map)
526 stab->linetable = xmalloc (sizeof (struct linetable)
527 + (nlines - 1) * sizeof (struct linetable_entry));
528 stab->linetable->nitems = nlines;
529 for (i = 0; i < nlines; i++)
531 stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc;
532 stab->linetable->item[i].line = map[i].line;
536 /* Called by readers to close a gdb_symtab. Does not need to do
537 anything as of now. */
540 jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
541 struct gdb_symtab *stab)
543 /* Right now nothing needs to be done here. We may need to do some
544 cleanup here in the future (again, without breaking the plugin
548 /* Transform STAB to a proper symtab, and add it it OBJFILE. */
551 finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
553 struct symtab *symtab;
554 struct gdb_block *gdb_block_iter, *gdb_block_iter_tmp;
555 struct block *block_iter;
556 int actual_nblocks, i, blockvector_size;
557 CORE_ADDR begin, end;
559 actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks;
561 symtab = allocate_symtab (stab->file_name, objfile);
562 /* JIT compilers compile in memory. */
563 symtab->dirname = NULL;
565 /* Copy over the linetable entry if one was provided. */
568 int size = ((stab->linetable->nitems - 1)
569 * sizeof (struct linetable_entry)
570 + sizeof (struct linetable));
571 LINETABLE (symtab) = obstack_alloc (&objfile->objfile_obstack, size);
572 memcpy (LINETABLE (symtab), stab->linetable, size);
576 LINETABLE (symtab) = NULL;
579 blockvector_size = (sizeof (struct blockvector)
580 + (actual_nblocks - 1) * sizeof (struct block *));
581 symtab->blockvector = obstack_alloc (&objfile->objfile_obstack,
584 /* (begin, end) will contain the PC range this entire blockvector
587 BLOCKVECTOR_MAP (symtab->blockvector) = NULL;
588 begin = stab->blocks->begin;
589 end = stab->blocks->end;
590 BLOCKVECTOR_NBLOCKS (symtab->blockvector) = actual_nblocks;
592 /* First run over all the gdb_block objects, creating a real block
593 object for each. Simultaneously, keep setting the real_block
595 for (i = (actual_nblocks - 1), gdb_block_iter = stab->blocks;
596 i >= FIRST_LOCAL_BLOCK;
597 i--, gdb_block_iter = gdb_block_iter->next)
599 struct block *new_block = allocate_block (&objfile->objfile_obstack);
600 struct symbol *block_name = obstack_alloc (&objfile->objfile_obstack,
601 sizeof (struct symbol));
603 BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
605 /* The address range. */
606 BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter->begin;
607 BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter->end;
610 memset (block_name, 0, sizeof (struct symbol));
611 SYMBOL_DOMAIN (block_name) = VAR_DOMAIN;
612 SYMBOL_CLASS (block_name) = LOC_BLOCK;
613 SYMBOL_SYMTAB (block_name) = symtab;
614 SYMBOL_BLOCK_VALUE (block_name) = new_block;
616 block_name->ginfo.name = obsavestring (gdb_block_iter->name,
617 strlen (gdb_block_iter->name),
618 &objfile->objfile_obstack);
620 BLOCK_FUNCTION (new_block) = block_name;
622 BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
623 if (begin > BLOCK_START (new_block))
624 begin = BLOCK_START (new_block);
625 if (end < BLOCK_END (new_block))
626 end = BLOCK_END (new_block);
628 gdb_block_iter->real_block = new_block;
631 /* Now add the special blocks. */
633 for (i = 0; i < FIRST_LOCAL_BLOCK; i++)
635 struct block *new_block = allocate_block (&objfile->objfile_obstack);
636 BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
638 BLOCK_SUPERBLOCK (new_block) = block_iter;
639 block_iter = new_block;
641 BLOCK_START (new_block) = (CORE_ADDR) begin;
642 BLOCK_END (new_block) = (CORE_ADDR) end;
644 BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
647 /* Fill up the superblock fields for the real blocks, using the
648 real_block fields populated earlier. */
649 for (gdb_block_iter = stab->blocks;
651 gdb_block_iter = gdb_block_iter->next)
653 if (gdb_block_iter->parent != NULL)
654 BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
655 gdb_block_iter->parent->real_block;
659 gdb_block_iter = stab->blocks;
661 for (gdb_block_iter = stab->blocks, gdb_block_iter_tmp = gdb_block_iter->next;
663 gdb_block_iter = gdb_block_iter_tmp)
665 xfree ((void *) gdb_block_iter->name);
666 xfree (gdb_block_iter);
668 xfree (stab->linetable);
669 xfree ((char *) stab->file_name);
673 /* Called when closing a gdb_objfile. Converts OBJ to a proper
677 jit_object_close_impl (struct gdb_symbol_callbacks *cb,
678 struct gdb_object *obj)
680 struct gdb_symtab *i, *j;
681 struct objfile *objfile;
682 jit_dbg_reader_data *priv_data;
684 priv_data = cb->priv_data;
686 objfile = allocate_objfile (NULL, 0);
687 objfile->gdbarch = target_gdbarch;
689 objfile->msymbols = obstack_alloc (&objfile->objfile_obstack,
690 sizeof (struct minimal_symbol));
691 memset (objfile->msymbols, 0, sizeof (struct minimal_symbol));
693 xfree (objfile->name);
694 objfile->name = xstrdup ("<< JIT compiled code >>");
697 for (i = obj->symtabs; i; i = j)
700 finalize_symtab (i, objfile);
702 add_objfile_entry (objfile, *priv_data);
706 /* Try to read CODE_ENTRY using the loaded jit reader (if any). */
709 jit_reader_try_read_symtab (struct jit_code_entry *code_entry)
713 struct jit_dbg_reader *i;
714 jit_dbg_reader_data priv_data;
715 struct gdb_reader_funcs *funcs;
716 volatile struct gdb_exception e;
717 struct gdb_symbol_callbacks callbacks =
719 jit_object_open_impl,
720 jit_symtab_open_impl,
722 jit_symtab_close_impl,
723 jit_object_close_impl,
725 jit_symtab_line_mapping_add_impl,
726 jit_target_read_impl,
731 priv_data = code_entry->symfile_addr;
733 if (!loaded_jit_reader)
736 gdb_mem = xmalloc (code_entry->symfile_size);
739 TRY_CATCH (e, RETURN_MASK_ALL)
740 if (target_read_memory (code_entry->symfile_addr, gdb_mem,
741 code_entry->symfile_size))
748 funcs = loaded_jit_reader->functions;
749 if (funcs->read (funcs, &callbacks, gdb_mem, code_entry->symfile_size)
755 if (jit_debug && status == 0)
756 fprintf_unfiltered (gdb_stdlog,
757 "Could not read symtab using the loaded JIT reader.\n");
761 /* Try to read CODE_ENTRY using BFD. */
764 jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
765 struct gdbarch *gdbarch)
768 struct section_addr_info *sai;
769 struct bfd_section *sec;
770 struct objfile *objfile;
771 struct cleanup *old_cleanups;
773 const struct bfd_arch_info *b;
776 fprintf_unfiltered (gdb_stdlog,
777 "jit_register_code, symfile_addr = %s, "
778 "symfile_size = %s\n",
779 paddress (gdbarch, code_entry->symfile_addr),
780 pulongest (code_entry->symfile_size));
782 nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
783 code_entry->symfile_size, gnutarget);
786 puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
790 /* Check the format. NOTE: This initializes important data that GDB uses!
791 We would segfault later without this line. */
792 if (!bfd_check_format (nbfd, bfd_object))
794 printf_unfiltered (_("\
795 JITed symbol file is not an object file, ignoring it.\n"));
800 /* Check bfd arch. */
801 b = gdbarch_bfd_arch_info (gdbarch);
802 if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
803 warning (_("JITed object file architecture %s is not compatible "
804 "with target architecture %s."), bfd_get_arch_info
805 (nbfd)->printable_name, b->printable_name);
807 /* Read the section address information out of the symbol file. Since the
808 file is generated by the JIT at runtime, it should all of the absolute
809 addresses that we care about. */
810 sai = alloc_section_addr_info (bfd_count_sections (nbfd));
811 old_cleanups = make_cleanup_free_section_addr_info (sai);
813 for (sec = nbfd->sections; sec != NULL; sec = sec->next)
814 if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
816 /* We assume that these virtual addresses are absolute, and do not
817 treat them as offsets. */
818 sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
819 sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
820 sai->other[i].sectindex = sec->index;
824 /* This call takes ownership of NBFD. It does not take ownership of SAI. */
825 objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
827 do_cleanups (old_cleanups);
828 add_objfile_entry (objfile, code_entry->symfile_addr);
831 /* This function registers code associated with a JIT code entry. It uses the
832 pointer and size pair in the entry to read the symbol file from the remote
833 and then calls symbol_file_add_from_local_memory to add it as though it were
834 a symbol file added by the user. */
837 jit_register_code (struct gdbarch *gdbarch,
838 CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
841 const struct bfd_arch_info *b;
842 struct jit_inferior_data *inf_data = get_jit_inferior_data ();
845 fprintf_unfiltered (gdb_stdlog,
846 "jit_register_code, symfile_addr = %s, "
847 "symfile_size = %s\n",
848 paddress (gdbarch, code_entry->symfile_addr),
849 pulongest (code_entry->symfile_size));
851 success = jit_reader_try_read_symtab (code_entry);
854 jit_bfd_try_read_symtab (code_entry, gdbarch);
857 /* This function unregisters JITed code and frees the corresponding
861 jit_unregister_code (struct objfile *objfile)
863 free_objfile (objfile);
866 /* Look up the objfile with this code entry address. */
868 static struct objfile *
869 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
871 struct objfile *objf;
872 CORE_ADDR *objf_entry_addr;
876 objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data);
877 if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr)
883 /* (Re-)Initialize the jit breakpoint if necessary.
884 Return 0 on success. */
887 jit_breakpoint_re_set_internal (struct gdbarch *gdbarch,
888 struct jit_inferior_data *inf_data)
890 if (inf_data->breakpoint_addr == 0)
892 struct minimal_symbol *reg_symbol;
894 /* Lookup the registration symbol. If it is missing, then we assume
895 we are not attached to a JIT. */
896 reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL);
897 if (reg_symbol == NULL)
899 inf_data->breakpoint_addr = SYMBOL_VALUE_ADDRESS (reg_symbol);
900 if (inf_data->breakpoint_addr == 0)
903 /* If we have not read the jit descriptor yet (e.g. because the JITer
904 itself is in a shared library which just got loaded), do so now. */
905 if (inf_data->descriptor_addr == 0)
906 jit_inferior_init (gdbarch);
912 fprintf_unfiltered (gdb_stdlog,
913 "jit_breakpoint_re_set_internal, "
914 "breakpoint_addr = %s\n",
915 paddress (gdbarch, inf_data->breakpoint_addr));
917 /* Put a breakpoint in the registration symbol. */
918 create_jit_event_breakpoint (gdbarch, inf_data->breakpoint_addr);
923 /* Register any already created translations. */
926 jit_inferior_init (struct gdbarch *gdbarch)
928 struct jit_descriptor descriptor;
929 struct jit_code_entry cur_entry;
930 struct jit_inferior_data *inf_data;
931 CORE_ADDR cur_entry_addr;
934 fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n");
936 inf_data = get_jit_inferior_data ();
937 if (jit_breakpoint_re_set_internal (gdbarch, inf_data) != 0)
940 if (inf_data->descriptor_addr == 0)
942 struct minimal_symbol *desc_symbol;
944 /* Lookup the descriptor symbol and cache the addr. If it is
945 missing, we assume we are not attached to a JIT and return early. */
946 desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL);
947 if (desc_symbol == NULL)
950 inf_data->descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol);
951 if (inf_data->descriptor_addr == 0)
956 fprintf_unfiltered (gdb_stdlog,
957 "jit_inferior_init, descriptor_addr = %s\n",
958 paddress (gdbarch, inf_data->descriptor_addr));
960 /* Read the descriptor so we can check the version number and load
961 any already JITed functions. */
962 jit_read_descriptor (gdbarch, &descriptor, inf_data->descriptor_addr);
964 /* Check that the version number agrees with that we support. */
965 if (descriptor.version != 1)
966 error (_("Unsupported JIT protocol version in descriptor!"));
968 /* If we've attached to a running program, we need to check the descriptor
969 to register any functions that were already generated. */
970 for (cur_entry_addr = descriptor.first_entry;
972 cur_entry_addr = cur_entry.next_entry)
974 jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
976 /* This hook may be called many times during setup, so make sure we don't
977 add the same symbol file twice. */
978 if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
981 jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
985 /* Exported routine to call when an inferior has been created. */
988 jit_inferior_created_hook (void)
990 jit_inferior_init (target_gdbarch);
993 /* Exported routine to call to re-set the jit breakpoints,
994 e.g. when a program is rerun. */
997 jit_breakpoint_re_set (void)
999 jit_breakpoint_re_set_internal (target_gdbarch,
1000 get_jit_inferior_data ());
1003 /* Reset inferior_data, so sybols will be looked up again, and jit_breakpoint
1007 jit_reset_inferior_data_and_breakpoints (void)
1009 struct jit_inferior_data *inf_data;
1011 /* Force jit_inferior_init to re-lookup of jit symbol addresses. */
1012 inf_data = get_jit_inferior_data ();
1013 inf_data->breakpoint_addr = 0;
1014 inf_data->descriptor_addr = 0;
1016 /* Remove any existing JIT breakpoint(s). */
1017 remove_jit_event_breakpoints ();
1019 jit_inferior_init (target_gdbarch);
1022 /* Wrapper to match the observer function pointer prototype. */
1025 jit_inferior_created_observer (struct target_ops *objfile, int from_tty)
1027 jit_reset_inferior_data_and_breakpoints ();
1030 /* This function cleans up any code entries left over when the
1031 inferior exits. We get left over code when the inferior exits
1032 without unregistering its code, for example when it crashes. */
1035 jit_inferior_exit_hook (struct inferior *inf)
1037 struct objfile *objf;
1038 struct objfile *temp;
1040 ALL_OBJFILES_SAFE (objf, temp)
1041 if (objfile_data (objf, jit_objfile_data) != NULL)
1042 jit_unregister_code (objf);
1046 jit_executable_changed_observer (void)
1048 jit_reset_inferior_data_and_breakpoints ();
1052 jit_event_handler (struct gdbarch *gdbarch)
1054 struct jit_descriptor descriptor;
1055 struct jit_code_entry code_entry;
1056 CORE_ADDR entry_addr;
1057 struct objfile *objf;
1059 /* Read the descriptor from remote memory. */
1060 jit_read_descriptor (gdbarch, &descriptor,
1061 get_jit_inferior_data ()->descriptor_addr);
1062 entry_addr = descriptor.relevant_entry;
1064 /* Do the corresponding action. */
1065 switch (descriptor.action_flag)
1070 jit_read_code_entry (gdbarch, entry_addr, &code_entry);
1071 jit_register_code (gdbarch, entry_addr, &code_entry);
1073 case JIT_UNREGISTER:
1074 objf = jit_find_objf_with_entry_addr (entry_addr);
1076 printf_unfiltered (_("Unable to find JITed code "
1077 "entry at address: %s\n"),
1078 paddress (gdbarch, entry_addr));
1080 jit_unregister_code (objf);
1084 error (_("Unknown action_flag value in JIT descriptor!"));
1089 /* Called to free the data allocated to the jit_inferior_data slot. */
1092 free_objfile_data (struct objfile *objfile, void *data)
1097 /* Provide a prototype to silence -Wmissing-prototypes. */
1099 extern void _initialize_jit (void);
1102 _initialize_jit (void)
1104 jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR,
1105 JIT_READER_DIR_RELOCATABLE);
1106 add_setshow_zinteger_cmd ("jit", class_maintenance, &jit_debug,
1107 _("Set JIT debugging."),
1108 _("Show JIT debugging."),
1109 _("When non-zero, JIT debugging is enabled."),
1112 &setdebuglist, &showdebuglist);
1114 observer_attach_inferior_created (jit_inferior_created_observer);
1115 observer_attach_inferior_exit (jit_inferior_exit_hook);
1116 observer_attach_executable_changed (jit_executable_changed_observer);
1118 register_objfile_data_with_cleanup (NULL, free_objfile_data);
1120 register_inferior_data_with_cleanup (jit_inferior_data_cleanup);
1121 if (is_dl_available ())
1123 add_com ("jit-reader-load", no_class, jit_reader_load_command, _("\
1124 Load FILE as debug info reader and unwinder for JIT compiled code.\n\
1125 Usage: jit-reader-load FILE\n\
1126 Try to load file FILE as a debug info reader (and unwinder) for\n\
1127 JIT compiled code. The file is loaded from " JIT_READER_DIR ",\n\
1128 relocated relative to the GDB executable if required."));
1129 add_com ("jit-reader-unload", no_class, jit_reader_unload_command, _("\
1130 Unload the currently loaded JIT debug info reader.\n\
1131 Usage: jit-reader-unload FILE\n\n\
1132 Do \"help jit-reader-load\" for info on loading debug info readers."));