xref: /dflybsd-src/contrib/gdb-7/gdb/jit.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
25796c8dcSSimon Schubert 
3*cf7f2e2dSJohn Marino    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "jit.h"
235796c8dcSSimon Schubert #include "breakpoint.h"
245796c8dcSSimon Schubert #include "gdbcore.h"
255796c8dcSSimon Schubert #include "observer.h"
265796c8dcSSimon Schubert #include "objfiles.h"
275796c8dcSSimon Schubert #include "symfile.h"
285796c8dcSSimon Schubert #include "symtab.h"
295796c8dcSSimon Schubert #include "target.h"
305796c8dcSSimon Schubert #include "gdb_stat.h"
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert static const struct objfile_data *jit_objfile_data;
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert static const char *const jit_break_name = "__jit_debug_register_code";
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert static const char *const jit_descriptor_name = "__jit_debug_descriptor";
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert /* This is the address of the JIT descriptor in the inferior.  */
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert static CORE_ADDR jit_descriptor_addr = 0;
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert /* This is a boolean indicating whether we're currently registering code.  This
435796c8dcSSimon Schubert    is used to avoid re-entering the registration code.  We want to check for
445796c8dcSSimon Schubert    new JITed every time a new object file is loaded, but we want to avoid
455796c8dcSSimon Schubert    checking for new code while we're registering object files for JITed code.
465796c8dcSSimon Schubert    Therefore, we flip this variable to 1 before registering new object files,
475796c8dcSSimon Schubert    and set it to 0 before returning.  */
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert static int registering_code = 0;
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert /* Helper cleanup function to clear an integer flag like the one above.  */
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert static void
545796c8dcSSimon Schubert clear_int (void *int_addr)
555796c8dcSSimon Schubert {
565796c8dcSSimon Schubert   *((int *) int_addr) = 0;
575796c8dcSSimon Schubert }
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert struct target_buffer
605796c8dcSSimon Schubert {
615796c8dcSSimon Schubert   CORE_ADDR base;
625796c8dcSSimon Schubert   size_t size;
635796c8dcSSimon Schubert };
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert /* Openning the file is a no-op.  */
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert static void *
685796c8dcSSimon Schubert mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
695796c8dcSSimon Schubert {
705796c8dcSSimon Schubert   return open_closure;
715796c8dcSSimon Schubert }
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert /* Closing the file is just freeing the base/size pair on our side.  */
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert static int
765796c8dcSSimon Schubert mem_bfd_iovec_close (struct bfd *abfd, void *stream)
775796c8dcSSimon Schubert {
785796c8dcSSimon Schubert   xfree (stream);
795796c8dcSSimon Schubert   return 1;
805796c8dcSSimon Schubert }
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert /* For reading the file, we just need to pass through to target_read_memory and
835796c8dcSSimon Schubert    fix up the arguments and return values.  */
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert static file_ptr
865796c8dcSSimon Schubert mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
875796c8dcSSimon Schubert                      file_ptr nbytes, file_ptr offset)
885796c8dcSSimon Schubert {
895796c8dcSSimon Schubert   int err;
905796c8dcSSimon Schubert   struct target_buffer *buffer = (struct target_buffer *) stream;
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert   /* If this read will read all of the file, limit it to just the rest.  */
935796c8dcSSimon Schubert   if (offset + nbytes > buffer->size)
945796c8dcSSimon Schubert     nbytes = buffer->size - offset;
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert   /* If there are no more bytes left, we've reached EOF.  */
975796c8dcSSimon Schubert   if (nbytes == 0)
985796c8dcSSimon Schubert     return 0;
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert   err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
1015796c8dcSSimon Schubert   if (err)
1025796c8dcSSimon Schubert     return -1;
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert   return nbytes;
1055796c8dcSSimon Schubert }
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert /* For statting the file, we only support the st_size attribute.  */
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert static int
1105796c8dcSSimon Schubert mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert   struct target_buffer *buffer = (struct target_buffer*) stream;
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert   sb->st_size = buffer->size;
1155796c8dcSSimon Schubert   return 0;
1165796c8dcSSimon Schubert }
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert /* Open a BFD from the target's memory.  */
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert static struct bfd *
1215796c8dcSSimon Schubert bfd_open_from_target_memory (CORE_ADDR addr, size_t size, char *target)
1225796c8dcSSimon Schubert {
1235796c8dcSSimon Schubert   const char *filename = xstrdup ("<in-memory>");
1245796c8dcSSimon Schubert   struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert   buffer->base = addr;
1275796c8dcSSimon Schubert   buffer->size = size;
1285796c8dcSSimon Schubert   return bfd_openr_iovec (filename, target,
1295796c8dcSSimon Schubert                           mem_bfd_iovec_open,
1305796c8dcSSimon Schubert                           buffer,
1315796c8dcSSimon Schubert                           mem_bfd_iovec_pread,
1325796c8dcSSimon Schubert                           mem_bfd_iovec_close,
1335796c8dcSSimon Schubert                           mem_bfd_iovec_stat);
1345796c8dcSSimon Schubert }
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert /* Helper function for reading the global JIT descriptor from remote memory.  */
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert static void
1395796c8dcSSimon Schubert jit_read_descriptor (struct gdbarch *gdbarch,
1405796c8dcSSimon Schubert 		     struct jit_descriptor *descriptor)
1415796c8dcSSimon Schubert {
1425796c8dcSSimon Schubert   int err;
1435796c8dcSSimon Schubert   struct type *ptr_type;
1445796c8dcSSimon Schubert   int ptr_size;
1455796c8dcSSimon Schubert   int desc_size;
1465796c8dcSSimon Schubert   gdb_byte *desc_buf;
1475796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert   /* Figure out how big the descriptor is on the remote and how to read it.  */
1505796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
1515796c8dcSSimon Schubert   ptr_size = TYPE_LENGTH (ptr_type);
1525796c8dcSSimon Schubert   desc_size = 8 + 2 * ptr_size;  /* Two 32-bit ints and two pointers.  */
1535796c8dcSSimon Schubert   desc_buf = alloca (desc_size);
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert   /* Read the descriptor.  */
1565796c8dcSSimon Schubert   err = target_read_memory (jit_descriptor_addr, desc_buf, desc_size);
1575796c8dcSSimon Schubert   if (err)
1585796c8dcSSimon Schubert     error (_("Unable to read JIT descriptor from remote memory!"));
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert   /* Fix the endianness to match the host.  */
1615796c8dcSSimon Schubert   descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
1625796c8dcSSimon Schubert   descriptor->action_flag =
1635796c8dcSSimon Schubert       extract_unsigned_integer (&desc_buf[4], 4, byte_order);
1645796c8dcSSimon Schubert   descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
1655796c8dcSSimon Schubert   descriptor->first_entry =
1665796c8dcSSimon Schubert       extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
1675796c8dcSSimon Schubert }
1685796c8dcSSimon Schubert 
1695796c8dcSSimon Schubert /* Helper function for reading a JITed code entry from remote memory.  */
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert static void
1725796c8dcSSimon Schubert jit_read_code_entry (struct gdbarch *gdbarch,
1735796c8dcSSimon Schubert 		     CORE_ADDR code_addr, struct jit_code_entry *code_entry)
1745796c8dcSSimon Schubert {
1755796c8dcSSimon Schubert   int err;
1765796c8dcSSimon Schubert   struct type *ptr_type;
1775796c8dcSSimon Schubert   int ptr_size;
1785796c8dcSSimon Schubert   int entry_size;
1795796c8dcSSimon Schubert   gdb_byte *entry_buf;
1805796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert   /* Figure out how big the entry is on the remote and how to read it.  */
1835796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
1845796c8dcSSimon Schubert   ptr_size = TYPE_LENGTH (ptr_type);
1855796c8dcSSimon Schubert   entry_size = 3 * ptr_size + 8;  /* Three pointers and one 64-bit int.  */
1865796c8dcSSimon Schubert   entry_buf = alloca (entry_size);
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert   /* Read the entry.  */
1895796c8dcSSimon Schubert   err = target_read_memory (code_addr, entry_buf, entry_size);
1905796c8dcSSimon Schubert   if (err)
1915796c8dcSSimon Schubert     error (_("Unable to read JIT code entry from remote memory!"));
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert   /* Fix the endianness to match the host.  */
1945796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
1955796c8dcSSimon Schubert   code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
1965796c8dcSSimon Schubert   code_entry->prev_entry =
1975796c8dcSSimon Schubert       extract_typed_address (&entry_buf[ptr_size], ptr_type);
1985796c8dcSSimon Schubert   code_entry->symfile_addr =
1995796c8dcSSimon Schubert       extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
2005796c8dcSSimon Schubert   code_entry->symfile_size =
2015796c8dcSSimon Schubert       extract_unsigned_integer (&entry_buf[3 * ptr_size], 8, byte_order);
2025796c8dcSSimon Schubert }
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert /* This function registers code associated with a JIT code entry.  It uses the
2055796c8dcSSimon Schubert    pointer and size pair in the entry to read the symbol file from the remote
2065796c8dcSSimon Schubert    and then calls symbol_file_add_from_local_memory to add it as though it were
2075796c8dcSSimon Schubert    a symbol file added by the user.  */
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert static void
2105796c8dcSSimon Schubert jit_register_code (struct gdbarch *gdbarch,
2115796c8dcSSimon Schubert 		   CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
2125796c8dcSSimon Schubert {
2135796c8dcSSimon Schubert   bfd *nbfd;
2145796c8dcSSimon Schubert   struct section_addr_info *sai;
2155796c8dcSSimon Schubert   struct bfd_section *sec;
2165796c8dcSSimon Schubert   struct objfile *objfile;
2175796c8dcSSimon Schubert   struct cleanup *old_cleanups, *my_cleanups;
2185796c8dcSSimon Schubert   int i;
2195796c8dcSSimon Schubert   const struct bfd_arch_info *b;
2205796c8dcSSimon Schubert   CORE_ADDR *entry_addr_ptr;
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert   nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
2235796c8dcSSimon Schubert                                       code_entry->symfile_size, gnutarget);
2245796c8dcSSimon Schubert   old_cleanups = make_cleanup_bfd_close (nbfd);
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert   /* Check the format.  NOTE: This initializes important data that GDB uses!
2275796c8dcSSimon Schubert      We would segfault later without this line.  */
2285796c8dcSSimon Schubert   if (!bfd_check_format (nbfd, bfd_object))
2295796c8dcSSimon Schubert     {
2305796c8dcSSimon Schubert       printf_unfiltered (_("\
2315796c8dcSSimon Schubert JITed symbol file is not an object file, ignoring it.\n"));
2325796c8dcSSimon Schubert       do_cleanups (old_cleanups);
2335796c8dcSSimon Schubert       return;
2345796c8dcSSimon Schubert     }
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert   /* Check bfd arch.  */
2375796c8dcSSimon Schubert   b = gdbarch_bfd_arch_info (gdbarch);
2385796c8dcSSimon Schubert   if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
2395796c8dcSSimon Schubert     warning (_("JITed object file architecture %s is not compatible "
2405796c8dcSSimon Schubert                "with target architecture %s."), bfd_get_arch_info
2415796c8dcSSimon Schubert              (nbfd)->printable_name, b->printable_name);
2425796c8dcSSimon Schubert 
2435796c8dcSSimon Schubert   /* Read the section address information out of the symbol file.  Since the
2445796c8dcSSimon Schubert      file is generated by the JIT at runtime, it should all of the absolute
2455796c8dcSSimon Schubert      addresses that we care about.  */
2465796c8dcSSimon Schubert   sai = alloc_section_addr_info (bfd_count_sections (nbfd));
2475796c8dcSSimon Schubert   make_cleanup_free_section_addr_info (sai);
2485796c8dcSSimon Schubert   i = 0;
2495796c8dcSSimon Schubert   for (sec = nbfd->sections; sec != NULL; sec = sec->next)
2505796c8dcSSimon Schubert     if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
2515796c8dcSSimon Schubert       {
2525796c8dcSSimon Schubert         /* We assume that these virtual addresses are absolute, and do not
2535796c8dcSSimon Schubert            treat them as offsets.  */
2545796c8dcSSimon Schubert         sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
255*cf7f2e2dSJohn Marino         sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
2565796c8dcSSimon Schubert         sai->other[i].sectindex = sec->index;
2575796c8dcSSimon Schubert         ++i;
2585796c8dcSSimon Schubert       }
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert   /* Raise this flag while we register code so we won't trigger any
2615796c8dcSSimon Schubert      re-registration.  */
2625796c8dcSSimon Schubert   registering_code = 1;
2635796c8dcSSimon Schubert   my_cleanups = make_cleanup (clear_int, &registering_code);
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert   /* This call takes ownership of sai.  */
2665796c8dcSSimon Schubert   objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED);
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert   /* Clear the registering_code flag.  */
2695796c8dcSSimon Schubert   do_cleanups (my_cleanups);
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert   /* Remember a mapping from entry_addr to objfile.  */
2725796c8dcSSimon Schubert   entry_addr_ptr = xmalloc (sizeof (CORE_ADDR));
2735796c8dcSSimon Schubert   *entry_addr_ptr = entry_addr;
2745796c8dcSSimon Schubert   set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr);
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert   discard_cleanups (old_cleanups);
2775796c8dcSSimon Schubert }
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert /* This function unregisters JITed code and frees the corresponding objfile.  */
2805796c8dcSSimon Schubert 
2815796c8dcSSimon Schubert static void
2825796c8dcSSimon Schubert jit_unregister_code (struct objfile *objfile)
2835796c8dcSSimon Schubert {
2845796c8dcSSimon Schubert   free_objfile (objfile);
2855796c8dcSSimon Schubert }
2865796c8dcSSimon Schubert 
2875796c8dcSSimon Schubert /* Look up the objfile with this code entry address.  */
2885796c8dcSSimon Schubert 
2895796c8dcSSimon Schubert static struct objfile *
2905796c8dcSSimon Schubert jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
2915796c8dcSSimon Schubert {
2925796c8dcSSimon Schubert   struct objfile *objf;
2935796c8dcSSimon Schubert   CORE_ADDR *objf_entry_addr;
2945796c8dcSSimon Schubert 
2955796c8dcSSimon Schubert   ALL_OBJFILES (objf)
2965796c8dcSSimon Schubert     {
2975796c8dcSSimon Schubert       objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data);
2985796c8dcSSimon Schubert       if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr)
2995796c8dcSSimon Schubert         return objf;
3005796c8dcSSimon Schubert     }
3015796c8dcSSimon Schubert   return NULL;
3025796c8dcSSimon Schubert }
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert /* (Re-)Initialize the jit breakpoint handler, and register any already
3055796c8dcSSimon Schubert    created translations.  */
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert static void
3085796c8dcSSimon Schubert jit_inferior_init (struct gdbarch *gdbarch)
3095796c8dcSSimon Schubert {
3105796c8dcSSimon Schubert   struct minimal_symbol *reg_symbol;
3115796c8dcSSimon Schubert   struct minimal_symbol *desc_symbol;
3125796c8dcSSimon Schubert   CORE_ADDR reg_addr;
3135796c8dcSSimon Schubert   struct jit_descriptor descriptor;
3145796c8dcSSimon Schubert   struct jit_code_entry cur_entry;
3155796c8dcSSimon Schubert   CORE_ADDR cur_entry_addr;
3165796c8dcSSimon Schubert 
3175796c8dcSSimon Schubert   /* When we register code, GDB resets its breakpoints in case symbols have
3185796c8dcSSimon Schubert      changed.  That in turn calls this handler, which makes us look for new
3195796c8dcSSimon Schubert      code again.  To avoid being re-entered, we check this flag.  */
3205796c8dcSSimon Schubert   if (registering_code)
3215796c8dcSSimon Schubert     return;
3225796c8dcSSimon Schubert 
3235796c8dcSSimon Schubert   /* Lookup the registration symbol.  If it is missing, then we assume we are
3245796c8dcSSimon Schubert      not attached to a JIT.  */
3255796c8dcSSimon Schubert   reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL);
3265796c8dcSSimon Schubert   if (reg_symbol == NULL)
3275796c8dcSSimon Schubert     return;
3285796c8dcSSimon Schubert   reg_addr = SYMBOL_VALUE_ADDRESS (reg_symbol);
3295796c8dcSSimon Schubert   if (reg_addr == 0)
3305796c8dcSSimon Schubert     return;
3315796c8dcSSimon Schubert 
3325796c8dcSSimon Schubert   /* Lookup the descriptor symbol and cache the addr.  If it is missing, we
3335796c8dcSSimon Schubert      assume we are not attached to a JIT and return early.  */
3345796c8dcSSimon Schubert   desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL);
3355796c8dcSSimon Schubert   if (desc_symbol == NULL)
3365796c8dcSSimon Schubert     return;
3375796c8dcSSimon Schubert   jit_descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol);
3385796c8dcSSimon Schubert   if (jit_descriptor_addr == 0)
3395796c8dcSSimon Schubert     return;
3405796c8dcSSimon Schubert 
3415796c8dcSSimon Schubert   /* Read the descriptor so we can check the version number and load any already
3425796c8dcSSimon Schubert      JITed functions.  */
3435796c8dcSSimon Schubert   jit_read_descriptor (gdbarch, &descriptor);
3445796c8dcSSimon Schubert 
3455796c8dcSSimon Schubert   /* Check that the version number agrees with that we support.  */
3465796c8dcSSimon Schubert   if (descriptor.version != 1)
3475796c8dcSSimon Schubert     error (_("Unsupported JIT protocol version in descriptor!"));
3485796c8dcSSimon Schubert 
3495796c8dcSSimon Schubert   /* Put a breakpoint in the registration symbol.  */
3505796c8dcSSimon Schubert   create_jit_event_breakpoint (gdbarch, reg_addr);
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert   /* If we've attached to a running program, we need to check the descriptor to
3535796c8dcSSimon Schubert      register any functions that were already generated.  */
3545796c8dcSSimon Schubert   for (cur_entry_addr = descriptor.first_entry;
3555796c8dcSSimon Schubert        cur_entry_addr != 0;
3565796c8dcSSimon Schubert        cur_entry_addr = cur_entry.next_entry)
3575796c8dcSSimon Schubert     {
3585796c8dcSSimon Schubert       jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert       /* This hook may be called many times during setup, so make sure we don't
3615796c8dcSSimon Schubert          add the same symbol file twice.  */
3625796c8dcSSimon Schubert       if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
3635796c8dcSSimon Schubert         continue;
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert       jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
3665796c8dcSSimon Schubert     }
3675796c8dcSSimon Schubert }
3685796c8dcSSimon Schubert 
3695796c8dcSSimon Schubert /* Exported routine to call when an inferior has been created.  */
3705796c8dcSSimon Schubert 
3715796c8dcSSimon Schubert void
3725796c8dcSSimon Schubert jit_inferior_created_hook (void)
3735796c8dcSSimon Schubert {
3745796c8dcSSimon Schubert   jit_inferior_init (target_gdbarch);
3755796c8dcSSimon Schubert }
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert /* Exported routine to call to re-set the jit breakpoints,
3785796c8dcSSimon Schubert    e.g. when a program is rerun.  */
3795796c8dcSSimon Schubert 
3805796c8dcSSimon Schubert void
3815796c8dcSSimon Schubert jit_breakpoint_re_set (void)
3825796c8dcSSimon Schubert {
3835796c8dcSSimon Schubert   jit_inferior_init (target_gdbarch);
3845796c8dcSSimon Schubert }
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert /* Wrapper to match the observer function pointer prototype.  */
3875796c8dcSSimon Schubert 
3885796c8dcSSimon Schubert static void
3895796c8dcSSimon Schubert jit_inferior_created_observer (struct target_ops *objfile, int from_tty)
3905796c8dcSSimon Schubert {
3915796c8dcSSimon Schubert   jit_inferior_init (target_gdbarch);
3925796c8dcSSimon Schubert }
3935796c8dcSSimon Schubert 
3945796c8dcSSimon Schubert /* This function cleans up any code entries left over when the inferior exits.
3955796c8dcSSimon Schubert    We get left over code when the inferior exits without unregistering its code,
3965796c8dcSSimon Schubert    for example when it crashes.  */
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert static void
399*cf7f2e2dSJohn Marino jit_inferior_exit_hook (struct inferior *inf)
4005796c8dcSSimon Schubert {
4015796c8dcSSimon Schubert   struct objfile *objf;
4025796c8dcSSimon Schubert   struct objfile *temp;
4035796c8dcSSimon Schubert 
4045796c8dcSSimon Schubert   /* We need to reset the descriptor addr so that next time we load up the
4055796c8dcSSimon Schubert      inferior we look for it again.  */
4065796c8dcSSimon Schubert   jit_descriptor_addr = 0;
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert   ALL_OBJFILES_SAFE (objf, temp)
4095796c8dcSSimon Schubert     if (objfile_data (objf, jit_objfile_data) != NULL)
4105796c8dcSSimon Schubert       jit_unregister_code (objf);
4115796c8dcSSimon Schubert }
4125796c8dcSSimon Schubert 
4135796c8dcSSimon Schubert void
4145796c8dcSSimon Schubert jit_event_handler (struct gdbarch *gdbarch)
4155796c8dcSSimon Schubert {
4165796c8dcSSimon Schubert   struct jit_descriptor descriptor;
4175796c8dcSSimon Schubert   struct jit_code_entry code_entry;
4185796c8dcSSimon Schubert   CORE_ADDR entry_addr;
4195796c8dcSSimon Schubert   struct objfile *objf;
4205796c8dcSSimon Schubert 
4215796c8dcSSimon Schubert   /* Read the descriptor from remote memory.  */
4225796c8dcSSimon Schubert   jit_read_descriptor (gdbarch, &descriptor);
4235796c8dcSSimon Schubert   entry_addr = descriptor.relevant_entry;
4245796c8dcSSimon Schubert 
4255796c8dcSSimon Schubert   /* Do the corresponding action. */
4265796c8dcSSimon Schubert   switch (descriptor.action_flag)
4275796c8dcSSimon Schubert     {
4285796c8dcSSimon Schubert     case JIT_NOACTION:
4295796c8dcSSimon Schubert       break;
4305796c8dcSSimon Schubert     case JIT_REGISTER:
4315796c8dcSSimon Schubert       jit_read_code_entry (gdbarch, entry_addr, &code_entry);
4325796c8dcSSimon Schubert       jit_register_code (gdbarch, entry_addr, &code_entry);
4335796c8dcSSimon Schubert       break;
4345796c8dcSSimon Schubert     case JIT_UNREGISTER:
4355796c8dcSSimon Schubert       objf = jit_find_objf_with_entry_addr (entry_addr);
4365796c8dcSSimon Schubert       if (objf == NULL)
4375796c8dcSSimon Schubert 	printf_unfiltered (_("Unable to find JITed code entry at address: %s\n"),
4385796c8dcSSimon Schubert 			   paddress (gdbarch, entry_addr));
4395796c8dcSSimon Schubert       else
4405796c8dcSSimon Schubert         jit_unregister_code (objf);
4415796c8dcSSimon Schubert 
4425796c8dcSSimon Schubert       break;
4435796c8dcSSimon Schubert     default:
4445796c8dcSSimon Schubert       error (_("Unknown action_flag value in JIT descriptor!"));
4455796c8dcSSimon Schubert       break;
4465796c8dcSSimon Schubert     }
4475796c8dcSSimon Schubert }
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
4505796c8dcSSimon Schubert 
4515796c8dcSSimon Schubert extern void _initialize_jit (void);
4525796c8dcSSimon Schubert 
4535796c8dcSSimon Schubert void
4545796c8dcSSimon Schubert _initialize_jit (void)
4555796c8dcSSimon Schubert {
4565796c8dcSSimon Schubert   observer_attach_inferior_created (jit_inferior_created_observer);
4575796c8dcSSimon Schubert   observer_attach_inferior_exit (jit_inferior_exit_hook);
4585796c8dcSSimon Schubert   jit_objfile_data = register_objfile_data ();
4595796c8dcSSimon Schubert }
460