GDB (API)
/home/stan/gdb/src/gdb/jit.c
Go to the documentation of this file.
00001 /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
00002 
00003    Copyright (C) 2009-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 
00022 #include "jit.h"
00023 #include "jit-reader.h"
00024 #include "block.h"
00025 #include "breakpoint.h"
00026 #include "command.h"
00027 #include "dictionary.h"
00028 #include "filenames.h"
00029 #include "frame-unwind.h"
00030 #include "gdbcmd.h"
00031 #include "gdbcore.h"
00032 #include "inferior.h"
00033 #include "observer.h"
00034 #include "objfiles.h"
00035 #include "regcache.h"
00036 #include "symfile.h"
00037 #include "symtab.h"
00038 #include "target.h"
00039 #include "gdb-dlfcn.h"
00040 #include "gdb_stat.h"
00041 #include "exceptions.h"
00042 #include "gdb_bfd.h"
00043 
00044 static const char *jit_reader_dir = NULL;
00045 
00046 static const struct objfile_data *jit_objfile_data;
00047 
00048 static const char *const jit_break_name = "__jit_debug_register_code";
00049 
00050 static const char *const jit_descriptor_name = "__jit_debug_descriptor";
00051 
00052 static const struct program_space_data *jit_program_space_data = NULL;
00053 
00054 static void jit_inferior_init (struct gdbarch *gdbarch);
00055 
00056 /* An unwinder is registered for every gdbarch.  This key is used to
00057    remember if the unwinder has been registered for a particular
00058    gdbarch.  */
00059 
00060 static struct gdbarch_data *jit_gdbarch_data;
00061 
00062 /* Non-zero if we want to see trace of jit level stuff.  */
00063 
00064 static unsigned int jit_debug = 0;
00065 
00066 static void
00067 show_jit_debug (struct ui_file *file, int from_tty,
00068                 struct cmd_list_element *c, const char *value)
00069 {
00070   fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
00071 }
00072 
00073 struct target_buffer
00074 {
00075   CORE_ADDR base;
00076   ULONGEST size;
00077 };
00078 
00079 /* Openning the file is a no-op.  */
00080 
00081 static void *
00082 mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
00083 {
00084   return open_closure;
00085 }
00086 
00087 /* Closing the file is just freeing the base/size pair on our side.  */
00088 
00089 static int
00090 mem_bfd_iovec_close (struct bfd *abfd, void *stream)
00091 {
00092   xfree (stream);
00093 
00094   /* Zero means success.  */
00095   return 0;
00096 }
00097 
00098 /* For reading the file, we just need to pass through to target_read_memory and
00099    fix up the arguments and return values.  */
00100 
00101 static file_ptr
00102 mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
00103                      file_ptr nbytes, file_ptr offset)
00104 {
00105   int err;
00106   struct target_buffer *buffer = (struct target_buffer *) stream;
00107 
00108   /* If this read will read all of the file, limit it to just the rest.  */
00109   if (offset + nbytes > buffer->size)
00110     nbytes = buffer->size - offset;
00111 
00112   /* If there are no more bytes left, we've reached EOF.  */
00113   if (nbytes == 0)
00114     return 0;
00115 
00116   err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
00117   if (err)
00118     return -1;
00119 
00120   return nbytes;
00121 }
00122 
00123 /* For statting the file, we only support the st_size attribute.  */
00124 
00125 static int
00126 mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
00127 {
00128   struct target_buffer *buffer = (struct target_buffer*) stream;
00129 
00130   sb->st_size = buffer->size;
00131   return 0;
00132 }
00133 
00134 /* Open a BFD from the target's memory.  */
00135 
00136 static struct bfd *
00137 bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
00138 {
00139   struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
00140 
00141   buffer->base = addr;
00142   buffer->size = size;
00143   return gdb_bfd_openr_iovec ("<in-memory>", target,
00144                               mem_bfd_iovec_open,
00145                               buffer,
00146                               mem_bfd_iovec_pread,
00147                               mem_bfd_iovec_close,
00148                               mem_bfd_iovec_stat);
00149 }
00150 
00151 /* One reader that has been loaded successfully, and can potentially be used to
00152    parse debug info.  */
00153 
00154 static struct jit_reader
00155 {
00156   struct gdb_reader_funcs *functions;
00157   void *handle;
00158 } *loaded_jit_reader = NULL;
00159 
00160 typedef struct gdb_reader_funcs * (reader_init_fn_type) (void);
00161 static const char *reader_init_fn_sym = "gdb_init_reader";
00162 
00163 /* Try to load FILE_NAME as a JIT debug info reader.  */
00164 
00165 static struct jit_reader *
00166 jit_reader_load (const char *file_name)
00167 {
00168   void *so;
00169   reader_init_fn_type *init_fn;
00170   struct jit_reader *new_reader = NULL;
00171   struct gdb_reader_funcs *funcs = NULL;
00172   struct cleanup *old_cleanups;
00173 
00174   if (jit_debug)
00175     fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"),
00176                         file_name);
00177   so = gdb_dlopen (file_name);
00178   old_cleanups = make_cleanup_dlclose (so);
00179 
00180   init_fn = gdb_dlsym (so, reader_init_fn_sym);
00181   if (!init_fn)
00182     error (_("Could not locate initialization function: %s."),
00183           reader_init_fn_sym);
00184 
00185   if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL)
00186     error (_("Reader not GPL compatible."));
00187 
00188   funcs = init_fn ();
00189   if (funcs->reader_version != GDB_READER_INTERFACE_VERSION)
00190     error (_("Reader version does not match GDB version."));
00191 
00192   new_reader = XZALLOC (struct jit_reader);
00193   new_reader->functions = funcs;
00194   new_reader->handle = so;
00195 
00196   discard_cleanups (old_cleanups);
00197   return new_reader;
00198 }
00199 
00200 /* Provides the jit-reader-load command.  */
00201 
00202 static void
00203 jit_reader_load_command (char *args, int from_tty)
00204 {
00205   char *so_name;
00206   struct cleanup *prev_cleanup;
00207 
00208   if (args == NULL)
00209     error (_("No reader name provided."));
00210 
00211   if (loaded_jit_reader != NULL)
00212     error (_("JIT reader already loaded.  Run jit-reader-unload first."));
00213 
00214   if (IS_ABSOLUTE_PATH (args))
00215     so_name = xstrdup (args);
00216   else
00217     so_name = xstrprintf ("%s%s%s", SLASH_STRING, jit_reader_dir, args);
00218   prev_cleanup = make_cleanup (xfree, so_name);
00219 
00220   loaded_jit_reader = jit_reader_load (so_name);
00221   do_cleanups (prev_cleanup);
00222 }
00223 
00224 /* Provides the jit-reader-unload command.  */
00225 
00226 static void
00227 jit_reader_unload_command (char *args, int from_tty)
00228 {
00229   if (!loaded_jit_reader)
00230     error (_("No JIT reader loaded."));
00231 
00232   loaded_jit_reader->functions->destroy (loaded_jit_reader->functions);
00233 
00234   gdb_dlclose (loaded_jit_reader->handle);
00235   xfree (loaded_jit_reader);
00236   loaded_jit_reader = NULL;
00237 }
00238 
00239 /* Per-program space structure recording which objfile has the JIT
00240    symbols.  */
00241 
00242 struct jit_program_space_data
00243 {
00244   /* The objfile.  This is NULL if no objfile holds the JIT
00245      symbols.  */
00246 
00247   struct objfile *objfile;
00248 
00249   /* If this program space has __jit_debug_register_code, this is the
00250      cached address from the minimal symbol.  This is used to detect
00251      relocations requiring the breakpoint to be re-created.  */
00252 
00253   CORE_ADDR cached_code_address;
00254 
00255   /* This is the JIT event breakpoint, or NULL if it has not been
00256      set.  */
00257 
00258   struct breakpoint *jit_breakpoint;
00259 };
00260 
00261 /* Per-objfile structure recording the addresses in the program space.
00262    This object serves two purposes: for ordinary objfiles, it may
00263    cache some symbols related to the JIT interface; and for
00264    JIT-created objfiles, it holds some information about the
00265    jit_code_entry.  */
00266 
00267 struct jit_objfile_data
00268 {
00269   /* Symbol for __jit_debug_register_code.  */
00270   struct minimal_symbol *register_code;
00271 
00272   /* Symbol for __jit_debug_descriptor.  */
00273   struct minimal_symbol *descriptor;
00274 
00275   /* Address of struct jit_code_entry in this objfile.  This is only
00276      non-zero for objfiles that represent code created by the JIT.  */
00277   CORE_ADDR addr;
00278 };
00279 
00280 /* Fetch the jit_objfile_data associated with OBJF.  If no data exists
00281    yet, make a new structure and attach it.  */
00282 
00283 static struct jit_objfile_data *
00284 get_jit_objfile_data (struct objfile *objf)
00285 {
00286   struct jit_objfile_data *objf_data;
00287 
00288   objf_data = objfile_data (objf, jit_objfile_data);
00289   if (objf_data == NULL)
00290     {
00291       objf_data = XZALLOC (struct jit_objfile_data);
00292       set_objfile_data (objf, jit_objfile_data, objf_data);
00293     }
00294 
00295   return objf_data;
00296 }
00297 
00298 /* Remember OBJFILE has been created for struct jit_code_entry located
00299    at inferior address ENTRY.  */
00300 
00301 static void
00302 add_objfile_entry (struct objfile *objfile, CORE_ADDR entry)
00303 {
00304   struct jit_objfile_data *objf_data;
00305 
00306   objf_data = get_jit_objfile_data (objfile);
00307   objf_data->addr = entry;
00308 }
00309 
00310 /* Return jit_program_space_data for current program space.  Allocate
00311    if not already present.  */
00312 
00313 static struct jit_program_space_data *
00314 get_jit_program_space_data (void)
00315 {
00316   struct jit_program_space_data *ps_data;
00317 
00318   ps_data = program_space_data (current_program_space, jit_program_space_data);
00319   if (ps_data == NULL)
00320     {
00321       ps_data = XZALLOC (struct jit_program_space_data);
00322       set_program_space_data (current_program_space, jit_program_space_data,
00323                               ps_data);
00324     }
00325 
00326   return ps_data;
00327 }
00328 
00329 static void
00330 jit_program_space_data_cleanup (struct program_space *ps, void *arg)
00331 {
00332   xfree (arg);
00333 }
00334 
00335 /* Helper function for reading the global JIT descriptor from remote
00336    memory.  Returns 1 if all went well, 0 otherwise.  */
00337 
00338 static int
00339 jit_read_descriptor (struct gdbarch *gdbarch,
00340                      struct jit_descriptor *descriptor,
00341                      struct jit_program_space_data *ps_data)
00342 {
00343   int err;
00344   struct type *ptr_type;
00345   int ptr_size;
00346   int desc_size;
00347   gdb_byte *desc_buf;
00348   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00349   struct jit_objfile_data *objf_data;
00350 
00351   if (ps_data->objfile == NULL)
00352     return 0;
00353   objf_data = get_jit_objfile_data (ps_data->objfile);
00354   if (objf_data->descriptor == NULL)
00355     return 0;
00356 
00357   if (jit_debug)
00358     fprintf_unfiltered (gdb_stdlog,
00359                         "jit_read_descriptor, descriptor_addr = %s\n",
00360                         paddress (gdbarch, SYMBOL_VALUE_ADDRESS (objf_data->descriptor)));
00361 
00362   /* Figure out how big the descriptor is on the remote and how to read it.  */
00363   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
00364   ptr_size = TYPE_LENGTH (ptr_type);
00365   desc_size = 8 + 2 * ptr_size;  /* Two 32-bit ints and two pointers.  */
00366   desc_buf = alloca (desc_size);
00367 
00368   /* Read the descriptor.  */
00369   err = target_read_memory (SYMBOL_VALUE_ADDRESS (objf_data->descriptor),
00370                             desc_buf, desc_size);
00371   if (err)
00372     {
00373       printf_unfiltered (_("Unable to read JIT descriptor from "
00374                            "remote memory\n"));
00375       return 0;
00376     }
00377 
00378   /* Fix the endianness to match the host.  */
00379   descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
00380   descriptor->action_flag =
00381       extract_unsigned_integer (&desc_buf[4], 4, byte_order);
00382   descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
00383   descriptor->first_entry =
00384       extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
00385 
00386   return 1;
00387 }
00388 
00389 /* Helper function for reading a JITed code entry from remote memory.  */
00390 
00391 static void
00392 jit_read_code_entry (struct gdbarch *gdbarch,
00393                      CORE_ADDR code_addr, struct jit_code_entry *code_entry)
00394 {
00395   int err, off;
00396   struct type *ptr_type;
00397   int ptr_size;
00398   int entry_size;
00399   int align_bytes;
00400   gdb_byte *entry_buf;
00401   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00402 
00403   /* Figure out how big the entry is on the remote and how to read it.  */
00404   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
00405   ptr_size = TYPE_LENGTH (ptr_type);
00406 
00407   /* Figure out where the longlong value will be.  */
00408   align_bytes = gdbarch_long_long_align_bit (gdbarch) / 8;
00409   off = 3 * ptr_size;
00410   off = (off + (align_bytes - 1)) & ~(align_bytes - 1);
00411 
00412   entry_size = off + 8;  /* Three pointers and one 64-bit int.  */
00413   entry_buf = alloca (entry_size);
00414 
00415   /* Read the entry.  */
00416   err = target_read_memory (code_addr, entry_buf, entry_size);
00417   if (err)
00418     error (_("Unable to read JIT code entry from remote memory!"));
00419 
00420   /* Fix the endianness to match the host.  */
00421   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
00422   code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
00423   code_entry->prev_entry =
00424       extract_typed_address (&entry_buf[ptr_size], ptr_type);
00425   code_entry->symfile_addr =
00426       extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
00427   code_entry->symfile_size =
00428       extract_unsigned_integer (&entry_buf[off], 8, byte_order);
00429 }
00430 
00431 /* Proxy object for building a block.  */
00432 
00433 struct gdb_block
00434 {
00435   /* gdb_blocks are linked into a tree structure.  Next points to the
00436      next node at the same depth as this block and parent to the
00437      parent gdb_block.  */
00438   struct gdb_block *next, *parent;
00439 
00440   /* Points to the "real" block that is being built out of this
00441      instance.  This block will be added to a blockvector, which will
00442      then be added to a symtab.  */
00443   struct block *real_block;
00444 
00445   /* The first and last code address corresponding to this block.  */
00446   CORE_ADDR begin, end;
00447 
00448   /* The name of this block (if any).  If this is non-NULL, the
00449      FUNCTION symbol symbol is set to this value.  */
00450   const char *name;
00451 };
00452 
00453 /* Proxy object for building a symtab.  */
00454 
00455 struct gdb_symtab
00456 {
00457   /* The list of blocks in this symtab.  These will eventually be
00458      converted to real blocks.  */
00459   struct gdb_block *blocks;
00460 
00461   /* The number of blocks inserted.  */
00462   int nblocks;
00463 
00464   /* A mapping between line numbers to PC.  */
00465   struct linetable *linetable;
00466 
00467   /* The source file for this symtab.  */
00468   const char *file_name;
00469   struct gdb_symtab *next;
00470 };
00471 
00472 /* Proxy object for building an object.  */
00473 
00474 struct gdb_object
00475 {
00476   struct gdb_symtab *symtabs;
00477 };
00478 
00479 /* The type of the `private' data passed around by the callback
00480    functions.  */
00481 
00482 typedef CORE_ADDR jit_dbg_reader_data;
00483 
00484 /* The reader calls into this function to read data off the targets
00485    address space.  */
00486 
00487 static enum gdb_status
00488 jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len)
00489 {
00490   int result = target_read_memory ((CORE_ADDR) target_mem, gdb_buf, len);
00491   if (result == 0)
00492     return GDB_SUCCESS;
00493   else
00494     return GDB_FAIL;
00495 }
00496 
00497 /* The reader calls into this function to create a new gdb_object
00498    which it can then pass around to the other callbacks.  Right now,
00499    all that is required is allocating the memory.  */
00500 
00501 static struct gdb_object *
00502 jit_object_open_impl (struct gdb_symbol_callbacks *cb)
00503 {
00504   /* CB is not required right now, but sometime in the future we might
00505      need a handle to it, and we'd like to do that without breaking
00506      the ABI.  */
00507   return XZALLOC (struct gdb_object);
00508 }
00509 
00510 /* Readers call into this function to open a new gdb_symtab, which,
00511    again, is passed around to other callbacks.  */
00512 
00513 static struct gdb_symtab *
00514 jit_symtab_open_impl (struct gdb_symbol_callbacks *cb,
00515                       struct gdb_object *object,
00516                       const char *file_name)
00517 {
00518   struct gdb_symtab *ret;
00519 
00520   /* CB stays unused.  See comment in jit_object_open_impl.  */
00521 
00522   ret = XZALLOC (struct gdb_symtab);
00523   ret->file_name = file_name ? xstrdup (file_name) : xstrdup ("");
00524   ret->next = object->symtabs;
00525   object->symtabs = ret;
00526   return ret;
00527 }
00528 
00529 /* Returns true if the block corresponding to old should be placed
00530    before the block corresponding to new in the final blockvector.  */
00531 
00532 static int
00533 compare_block (const struct gdb_block *const old,
00534                const struct gdb_block *const new)
00535 {
00536   if (old == NULL)
00537     return 1;
00538   if (old->begin < new->begin)
00539     return 1;
00540   else if (old->begin == new->begin)
00541     {
00542       if (old->end > new->end)
00543         return 1;
00544       else
00545         return 0;
00546     }
00547   else
00548     return 0;
00549 }
00550 
00551 /* Called by readers to open a new gdb_block.  This function also
00552    inserts the new gdb_block in the correct place in the corresponding
00553    gdb_symtab.  */
00554 
00555 static struct gdb_block *
00556 jit_block_open_impl (struct gdb_symbol_callbacks *cb,
00557                      struct gdb_symtab *symtab, struct gdb_block *parent,
00558                      GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name)
00559 {
00560   struct gdb_block *block = XZALLOC (struct gdb_block);
00561 
00562   block->next = symtab->blocks;
00563   block->begin = (CORE_ADDR) begin;
00564   block->end = (CORE_ADDR) end;
00565   block->name = name ? xstrdup (name) : NULL;
00566   block->parent = parent;
00567 
00568   /* Ensure that the blocks are inserted in the correct (reverse of
00569      the order expected by blockvector).  */
00570   if (compare_block (symtab->blocks, block))
00571     {
00572       symtab->blocks = block;
00573     }
00574   else
00575     {
00576       struct gdb_block *i = symtab->blocks;
00577 
00578       for (;; i = i->next)
00579         {
00580           /* Guaranteed to terminate, since compare_block (NULL, _)
00581              returns 1.  */
00582           if (compare_block (i->next, block))
00583             {
00584               block->next = i->next;
00585               i->next = block;
00586               break;
00587             }
00588         }
00589     }
00590   symtab->nblocks++;
00591 
00592   return block;
00593 }
00594 
00595 /* Readers call this to add a line mapping (from PC to line number) to
00596    a gdb_symtab.  */
00597 
00598 static void
00599 jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb,
00600                                   struct gdb_symtab *stab, int nlines,
00601                                   struct gdb_line_mapping *map)
00602 {
00603   int i;
00604 
00605   if (nlines < 1)
00606     return;
00607 
00608   stab->linetable = xmalloc (sizeof (struct linetable)
00609                              + (nlines - 1) * sizeof (struct linetable_entry));
00610   stab->linetable->nitems = nlines;
00611   for (i = 0; i < nlines; i++)
00612     {
00613       stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc;
00614       stab->linetable->item[i].line = map[i].line;
00615     }
00616 }
00617 
00618 /* Called by readers to close a gdb_symtab.  Does not need to do
00619    anything as of now.  */
00620 
00621 static void
00622 jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
00623                        struct gdb_symtab *stab)
00624 {
00625   /* Right now nothing needs to be done here.  We may need to do some
00626      cleanup here in the future (again, without breaking the plugin
00627      ABI).  */
00628 }
00629 
00630 /* Transform STAB to a proper symtab, and add it it OBJFILE.  */
00631 
00632 static void
00633 finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
00634 {
00635   struct symtab *symtab;
00636   struct gdb_block *gdb_block_iter, *gdb_block_iter_tmp;
00637   struct block *block_iter;
00638   int actual_nblocks, i, blockvector_size;
00639   CORE_ADDR begin, end;
00640 
00641   actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks;
00642 
00643   symtab = allocate_symtab (stab->file_name, objfile);
00644   /* JIT compilers compile in memory.  */
00645   symtab->dirname = NULL;
00646 
00647   /* Copy over the linetable entry if one was provided.  */
00648   if (stab->linetable)
00649     {
00650       int size = ((stab->linetable->nitems - 1)
00651                   * sizeof (struct linetable_entry)
00652                   + sizeof (struct linetable));
00653       LINETABLE (symtab) = obstack_alloc (&objfile->objfile_obstack, size);
00654       memcpy (LINETABLE (symtab), stab->linetable, size);
00655     }
00656   else
00657     {
00658       LINETABLE (symtab) = NULL;
00659     }
00660 
00661   blockvector_size = (sizeof (struct blockvector)
00662                       + (actual_nblocks - 1) * sizeof (struct block *));
00663   symtab->blockvector = obstack_alloc (&objfile->objfile_obstack,
00664                                        blockvector_size);
00665 
00666   /* (begin, end) will contain the PC range this entire blockvector
00667      spans.  */
00668   symtab->primary = 1;
00669   BLOCKVECTOR_MAP (symtab->blockvector) = NULL;
00670   begin = stab->blocks->begin;
00671   end = stab->blocks->end;
00672   BLOCKVECTOR_NBLOCKS (symtab->blockvector) = actual_nblocks;
00673 
00674   /* First run over all the gdb_block objects, creating a real block
00675      object for each.  Simultaneously, keep setting the real_block
00676      fields.  */
00677   for (i = (actual_nblocks - 1), gdb_block_iter = stab->blocks;
00678        i >= FIRST_LOCAL_BLOCK;
00679        i--, gdb_block_iter = gdb_block_iter->next)
00680     {
00681       struct block *new_block = allocate_block (&objfile->objfile_obstack);
00682       struct symbol *block_name = allocate_symbol (objfile);
00683       struct type *block_type = arch_type (get_objfile_arch (objfile),
00684                                            TYPE_CODE_VOID,
00685                                            1,
00686                                            "void");
00687 
00688       BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
00689                                                    NULL);
00690       /* The address range.  */
00691       BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter->begin;
00692       BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter->end;
00693 
00694       /* The name.  */
00695       SYMBOL_DOMAIN (block_name) = VAR_DOMAIN;
00696       SYMBOL_ACLASS_INDEX (block_name) = LOC_BLOCK;
00697       SYMBOL_SYMTAB (block_name) = symtab;
00698       SYMBOL_TYPE (block_name) = lookup_function_type (block_type);
00699       SYMBOL_BLOCK_VALUE (block_name) = new_block;
00700 
00701       block_name->ginfo.name = obstack_copy0 (&objfile->objfile_obstack,
00702                                               gdb_block_iter->name,
00703                                               strlen (gdb_block_iter->name));
00704 
00705       BLOCK_FUNCTION (new_block) = block_name;
00706 
00707       BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
00708       if (begin > BLOCK_START (new_block))
00709         begin = BLOCK_START (new_block);
00710       if (end < BLOCK_END (new_block))
00711         end = BLOCK_END (new_block);
00712 
00713       gdb_block_iter->real_block = new_block;
00714     }
00715 
00716   /* Now add the special blocks.  */
00717   block_iter = NULL;
00718   for (i = 0; i < FIRST_LOCAL_BLOCK; i++)
00719     {
00720       struct block *new_block;
00721 
00722       new_block = (i == GLOBAL_BLOCK
00723                    ? allocate_global_block (&objfile->objfile_obstack)
00724                    : allocate_block (&objfile->objfile_obstack));
00725       BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
00726                                                    NULL);
00727       BLOCK_SUPERBLOCK (new_block) = block_iter;
00728       block_iter = new_block;
00729 
00730       BLOCK_START (new_block) = (CORE_ADDR) begin;
00731       BLOCK_END (new_block) = (CORE_ADDR) end;
00732 
00733       BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
00734 
00735       if (i == GLOBAL_BLOCK)
00736         set_block_symtab (new_block, symtab);
00737     }
00738 
00739   /* Fill up the superblock fields for the real blocks, using the
00740      real_block fields populated earlier.  */
00741   for (gdb_block_iter = stab->blocks;
00742        gdb_block_iter;
00743        gdb_block_iter = gdb_block_iter->next)
00744     {
00745       if (gdb_block_iter->parent != NULL)
00746         {
00747           /* If the plugin specifically mentioned a parent block, we
00748              use that.  */
00749           BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
00750             gdb_block_iter->parent->real_block;
00751         }
00752       else
00753         {
00754           /* And if not, we set a default parent block.  */
00755           BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
00756             BLOCKVECTOR_BLOCK (symtab->blockvector, STATIC_BLOCK);
00757         }
00758     }
00759 
00760   /* Free memory.  */
00761   gdb_block_iter = stab->blocks;
00762 
00763   for (gdb_block_iter = stab->blocks, gdb_block_iter_tmp = gdb_block_iter->next;
00764        gdb_block_iter;
00765        gdb_block_iter = gdb_block_iter_tmp)
00766     {
00767       xfree ((void *) gdb_block_iter->name);
00768       xfree (gdb_block_iter);
00769     }
00770   xfree (stab->linetable);
00771   xfree ((char *) stab->file_name);
00772   xfree (stab);
00773 }
00774 
00775 /* Called when closing a gdb_objfile.  Converts OBJ to a proper
00776    objfile.  */
00777 
00778 static void
00779 jit_object_close_impl (struct gdb_symbol_callbacks *cb,
00780                        struct gdb_object *obj)
00781 {
00782   struct gdb_symtab *i, *j;
00783   struct objfile *objfile;
00784   jit_dbg_reader_data *priv_data;
00785 
00786   priv_data = cb->priv_data;
00787 
00788   objfile = allocate_objfile (NULL, "<< JIT compiled code >>",
00789                               OBJF_NOT_FILENAME);
00790   objfile->per_bfd->gdbarch = target_gdbarch ();
00791 
00792   terminate_minimal_symbol_table (objfile);
00793 
00794   j = NULL;
00795   for (i = obj->symtabs; i; i = j)
00796     {
00797       j = i->next;
00798       finalize_symtab (i, objfile);
00799     }
00800   add_objfile_entry (objfile, *priv_data);
00801   xfree (obj);
00802 }
00803 
00804 /* Try to read CODE_ENTRY using the loaded jit reader (if any).
00805    ENTRY_ADDR is the address of the struct jit_code_entry in the
00806    inferior address space.  */
00807 
00808 static int
00809 jit_reader_try_read_symtab (struct jit_code_entry *code_entry,
00810                             CORE_ADDR entry_addr)
00811 {
00812   void *gdb_mem;
00813   int status;
00814   jit_dbg_reader_data priv_data;
00815   struct gdb_reader_funcs *funcs;
00816   volatile struct gdb_exception e;
00817   struct gdb_symbol_callbacks callbacks =
00818     {
00819       jit_object_open_impl,
00820       jit_symtab_open_impl,
00821       jit_block_open_impl,
00822       jit_symtab_close_impl,
00823       jit_object_close_impl,
00824 
00825       jit_symtab_line_mapping_add_impl,
00826       jit_target_read_impl,
00827 
00828       &priv_data
00829     };
00830 
00831   priv_data = entry_addr;
00832 
00833   if (!loaded_jit_reader)
00834     return 0;
00835 
00836   gdb_mem = xmalloc (code_entry->symfile_size);
00837 
00838   status = 1;
00839   TRY_CATCH (e, RETURN_MASK_ALL)
00840     if (target_read_memory (code_entry->symfile_addr, gdb_mem,
00841                             code_entry->symfile_size))
00842       status = 0;
00843   if (e.reason < 0)
00844     status = 0;
00845 
00846   if (status)
00847     {
00848       funcs = loaded_jit_reader->functions;
00849       if (funcs->read (funcs, &callbacks, gdb_mem, code_entry->symfile_size)
00850           != GDB_SUCCESS)
00851         status = 0;
00852     }
00853 
00854   xfree (gdb_mem);
00855   if (jit_debug && status == 0)
00856     fprintf_unfiltered (gdb_stdlog,
00857                         "Could not read symtab using the loaded JIT reader.\n");
00858   return status;
00859 }
00860 
00861 /* Try to read CODE_ENTRY using BFD.  ENTRY_ADDR is the address of the
00862    struct jit_code_entry in the inferior address space.  */
00863 
00864 static void
00865 jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
00866                          CORE_ADDR entry_addr,
00867                          struct gdbarch *gdbarch)
00868 {
00869   bfd *nbfd;
00870   struct section_addr_info *sai;
00871   struct bfd_section *sec;
00872   struct objfile *objfile;
00873   struct cleanup *old_cleanups;
00874   int i;
00875   const struct bfd_arch_info *b;
00876 
00877   if (jit_debug)
00878     fprintf_unfiltered (gdb_stdlog,
00879                         "jit_register_code, symfile_addr = %s, "
00880                         "symfile_size = %s\n",
00881                         paddress (gdbarch, code_entry->symfile_addr),
00882                         pulongest (code_entry->symfile_size));
00883 
00884   nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
00885                                       code_entry->symfile_size, gnutarget);
00886   if (nbfd == NULL)
00887     {
00888       puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
00889       return;
00890     }
00891 
00892   /* Check the format.  NOTE: This initializes important data that GDB uses!
00893      We would segfault later without this line.  */
00894   if (!bfd_check_format (nbfd, bfd_object))
00895     {
00896       printf_unfiltered (_("\
00897 JITed symbol file is not an object file, ignoring it.\n"));
00898       gdb_bfd_unref (nbfd);
00899       return;
00900     }
00901 
00902   /* Check bfd arch.  */
00903   b = gdbarch_bfd_arch_info (gdbarch);
00904   if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
00905     warning (_("JITed object file architecture %s is not compatible "
00906                "with target architecture %s."), bfd_get_arch_info
00907              (nbfd)->printable_name, b->printable_name);
00908 
00909   /* Read the section address information out of the symbol file.  Since the
00910      file is generated by the JIT at runtime, it should all of the absolute
00911      addresses that we care about.  */
00912   sai = alloc_section_addr_info (bfd_count_sections (nbfd));
00913   old_cleanups = make_cleanup_free_section_addr_info (sai);
00914   i = 0;
00915   for (sec = nbfd->sections; sec != NULL; sec = sec->next)
00916     if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
00917       {
00918         /* We assume that these virtual addresses are absolute, and do not
00919            treat them as offsets.  */
00920         sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
00921         sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
00922         sai->other[i].sectindex = sec->index;
00923         ++i;
00924       }
00925   sai->num_sections = i;
00926 
00927   /* This call does not take ownership of SAI.  */
00928   make_cleanup_bfd_unref (nbfd);
00929   objfile = symbol_file_add_from_bfd (nbfd, bfd_get_filename (nbfd), 0, sai,
00930                                       OBJF_SHARED | OBJF_NOT_FILENAME, NULL);
00931 
00932   do_cleanups (old_cleanups);
00933   add_objfile_entry (objfile, entry_addr);
00934 }
00935 
00936 /* This function registers code associated with a JIT code entry.  It uses the
00937    pointer and size pair in the entry to read the symbol file from the remote
00938    and then calls symbol_file_add_from_local_memory to add it as though it were
00939    a symbol file added by the user.  */
00940 
00941 static void
00942 jit_register_code (struct gdbarch *gdbarch,
00943                    CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
00944 {
00945   int success;
00946 
00947   if (jit_debug)
00948     fprintf_unfiltered (gdb_stdlog,
00949                         "jit_register_code, symfile_addr = %s, "
00950                         "symfile_size = %s\n",
00951                         paddress (gdbarch, code_entry->symfile_addr),
00952                         pulongest (code_entry->symfile_size));
00953 
00954   success = jit_reader_try_read_symtab (code_entry, entry_addr);
00955 
00956   if (!success)
00957     jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch);
00958 }
00959 
00960 /* This function unregisters JITed code and frees the corresponding
00961    objfile.  */
00962 
00963 static void
00964 jit_unregister_code (struct objfile *objfile)
00965 {
00966   free_objfile (objfile);
00967 }
00968 
00969 /* Look up the objfile with this code entry address.  */
00970 
00971 static struct objfile *
00972 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
00973 {
00974   struct objfile *objf;
00975 
00976   ALL_OBJFILES (objf)
00977     {
00978       struct jit_objfile_data *objf_data;
00979 
00980       objf_data = objfile_data (objf, jit_objfile_data);
00981       if (objf_data != NULL && objf_data->addr == entry_addr)
00982         return objf;
00983     }
00984   return NULL;
00985 }
00986 
00987 /* This is called when a breakpoint is deleted.  It updates the
00988    inferior's cache, if needed.  */
00989 
00990 static void
00991 jit_breakpoint_deleted (struct breakpoint *b)
00992 {
00993   struct bp_location *iter;
00994 
00995   if (b->type != bp_jit_event)
00996     return;
00997 
00998   for (iter = b->loc; iter != NULL; iter = iter->next)
00999     {
01000       struct jit_program_space_data *ps_data;
01001 
01002       ps_data = program_space_data (iter->pspace, jit_program_space_data);
01003       if (ps_data != NULL && ps_data->jit_breakpoint == iter->owner)
01004         {
01005           ps_data->cached_code_address = 0;
01006           ps_data->jit_breakpoint = NULL;
01007         }
01008     }
01009 }
01010 
01011 /* (Re-)Initialize the jit breakpoint if necessary.
01012    Return 0 on success.  */
01013 
01014 static int
01015 jit_breakpoint_re_set_internal (struct gdbarch *gdbarch,
01016                                 struct jit_program_space_data *ps_data)
01017 {
01018   struct bound_minimal_symbol reg_symbol;
01019   struct minimal_symbol *desc_symbol;
01020   struct jit_objfile_data *objf_data;
01021   CORE_ADDR addr;
01022 
01023   if (ps_data->objfile == NULL)
01024     {
01025       /* Lookup the registration symbol.  If it is missing, then we
01026          assume we are not attached to a JIT.  */
01027       reg_symbol = lookup_minimal_symbol_and_objfile (jit_break_name);
01028       if (reg_symbol.minsym == NULL
01029           || SYMBOL_VALUE_ADDRESS (reg_symbol.minsym) == 0)
01030         return 1;
01031 
01032       desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL,
01033                                            reg_symbol.objfile);
01034       if (desc_symbol == NULL || SYMBOL_VALUE_ADDRESS (desc_symbol) == 0)
01035         return 1;
01036 
01037       objf_data = get_jit_objfile_data (reg_symbol.objfile);
01038       objf_data->register_code = reg_symbol.minsym;
01039       objf_data->descriptor = desc_symbol;
01040 
01041       ps_data->objfile = reg_symbol.objfile;
01042     }
01043   else
01044     objf_data = get_jit_objfile_data (ps_data->objfile);
01045 
01046   addr = SYMBOL_VALUE_ADDRESS (objf_data->register_code);
01047 
01048   if (jit_debug)
01049     fprintf_unfiltered (gdb_stdlog,
01050                         "jit_breakpoint_re_set_internal, "
01051                         "breakpoint_addr = %s\n",
01052                         paddress (gdbarch, addr));
01053 
01054   if (ps_data->cached_code_address == addr)
01055     return 1;
01056 
01057   /* Delete the old breakpoint.  */
01058   if (ps_data->jit_breakpoint != NULL)
01059     delete_breakpoint (ps_data->jit_breakpoint);
01060 
01061   /* Put a breakpoint in the registration symbol.  */
01062   ps_data->cached_code_address = addr;
01063   ps_data->jit_breakpoint = create_jit_event_breakpoint (gdbarch, addr);
01064 
01065   return 0;
01066 }
01067 
01068 /* The private data passed around in the frame unwind callback
01069    functions.  */
01070 
01071 struct jit_unwind_private
01072 {
01073   /* Cached register values.  See jit_frame_sniffer to see how this
01074      works.  */
01075   struct gdb_reg_value **registers;
01076 
01077   /* The frame being unwound.  */
01078   struct frame_info *this_frame;
01079 };
01080 
01081 /* Sets the value of a particular register in this frame.  */
01082 
01083 static void
01084 jit_unwind_reg_set_impl (struct gdb_unwind_callbacks *cb, int dwarf_regnum,
01085                          struct gdb_reg_value *value)
01086 {
01087   struct jit_unwind_private *priv;
01088   int gdb_reg;
01089 
01090   priv = cb->priv_data;
01091 
01092   gdb_reg = gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv->this_frame),
01093                                           dwarf_regnum);
01094   if (gdb_reg == -1)
01095     {
01096       if (jit_debug)
01097         fprintf_unfiltered (gdb_stdlog,
01098                             _("Could not recognize DWARF regnum %d"),
01099                             dwarf_regnum);
01100       return;
01101     }
01102 
01103   gdb_assert (priv->registers);
01104   priv->registers[gdb_reg] = value;
01105 }
01106 
01107 static void
01108 reg_value_free_impl (struct gdb_reg_value *value)
01109 {
01110   xfree (value);
01111 }
01112 
01113 /* Get the value of register REGNUM in the previous frame.  */
01114 
01115 static struct gdb_reg_value *
01116 jit_unwind_reg_get_impl (struct gdb_unwind_callbacks *cb, int regnum)
01117 {
01118   struct jit_unwind_private *priv;
01119   struct gdb_reg_value *value;
01120   int gdb_reg, size;
01121   struct gdbarch *frame_arch;
01122 
01123   priv = cb->priv_data;
01124   frame_arch = get_frame_arch (priv->this_frame);
01125 
01126   gdb_reg = gdbarch_dwarf2_reg_to_regnum (frame_arch, regnum);
01127   size = register_size (frame_arch, gdb_reg);
01128   value = xmalloc (sizeof (struct gdb_reg_value) + size - 1);
01129   value->defined = deprecated_frame_register_read (priv->this_frame, gdb_reg,
01130                                                    value->value);
01131   value->size = size;
01132   value->free = reg_value_free_impl;
01133   return value;
01134 }
01135 
01136 /* gdb_reg_value has a free function, which must be called on each
01137    saved register value.  */
01138 
01139 static void
01140 jit_dealloc_cache (struct frame_info *this_frame, void *cache)
01141 {
01142   struct jit_unwind_private *priv_data = cache;
01143   struct gdbarch *frame_arch;
01144   int i;
01145 
01146   gdb_assert (priv_data->registers);
01147   frame_arch = get_frame_arch (priv_data->this_frame);
01148 
01149   for (i = 0; i < gdbarch_num_regs (frame_arch); i++)
01150     if (priv_data->registers[i] && priv_data->registers[i]->free)
01151       priv_data->registers[i]->free (priv_data->registers[i]);
01152 
01153   xfree (priv_data->registers);
01154   xfree (priv_data);
01155 }
01156 
01157 /* The frame sniffer for the pseudo unwinder.
01158 
01159    While this is nominally a frame sniffer, in the case where the JIT
01160    reader actually recognizes the frame, it does a lot more work -- it
01161    unwinds the frame and saves the corresponding register values in
01162    the cache.  jit_frame_prev_register simply returns the saved
01163    register values.  */
01164 
01165 static int
01166 jit_frame_sniffer (const struct frame_unwind *self,
01167                    struct frame_info *this_frame, void **cache)
01168 {
01169   struct jit_unwind_private *priv_data;
01170   struct gdb_unwind_callbacks callbacks;
01171   struct gdb_reader_funcs *funcs;
01172 
01173   callbacks.reg_get = jit_unwind_reg_get_impl;
01174   callbacks.reg_set = jit_unwind_reg_set_impl;
01175   callbacks.target_read = jit_target_read_impl;
01176 
01177   if (loaded_jit_reader == NULL)
01178     return 0;
01179 
01180   funcs = loaded_jit_reader->functions;
01181 
01182   gdb_assert (!*cache);
01183 
01184   *cache = XZALLOC (struct jit_unwind_private);
01185   priv_data = *cache;
01186   priv_data->registers =
01187     XCALLOC (gdbarch_num_regs (get_frame_arch (this_frame)),
01188              struct gdb_reg_value *);
01189   priv_data->this_frame = this_frame;
01190 
01191   callbacks.priv_data = priv_data;
01192 
01193   /* Try to coax the provided unwinder to unwind the stack */
01194   if (funcs->unwind (funcs, &callbacks) == GDB_SUCCESS)
01195     {
01196       if (jit_debug)
01197         fprintf_unfiltered (gdb_stdlog, _("Successfully unwound frame using "
01198                                           "JIT reader.\n"));
01199       return 1;
01200     }
01201   if (jit_debug)
01202     fprintf_unfiltered (gdb_stdlog, _("Could not unwind frame using "
01203                                       "JIT reader.\n"));
01204 
01205   jit_dealloc_cache (this_frame, *cache);
01206   *cache = NULL;
01207 
01208   return 0;
01209 }
01210 
01211 
01212 /* The frame_id function for the pseudo unwinder.  Relays the call to
01213    the loaded plugin.  */
01214 
01215 static void
01216 jit_frame_this_id (struct frame_info *this_frame, void **cache,
01217                    struct frame_id *this_id)
01218 {
01219   struct jit_unwind_private private;
01220   struct gdb_frame_id frame_id;
01221   struct gdb_reader_funcs *funcs;
01222   struct gdb_unwind_callbacks callbacks;
01223 
01224   private.registers = NULL;
01225   private.this_frame = this_frame;
01226 
01227   /* We don't expect the frame_id function to set any registers, so we
01228      set reg_set to NULL.  */
01229   callbacks.reg_get = jit_unwind_reg_get_impl;
01230   callbacks.reg_set = NULL;
01231   callbacks.target_read = jit_target_read_impl;
01232   callbacks.priv_data = &private;
01233 
01234   gdb_assert (loaded_jit_reader);
01235   funcs = loaded_jit_reader->functions;
01236 
01237   frame_id = funcs->get_frame_id (funcs, &callbacks);
01238   *this_id = frame_id_build (frame_id.stack_address, frame_id.code_address);
01239 }
01240 
01241 /* Pseudo unwinder function.  Reads the previously fetched value for
01242    the register from the cache.  */
01243 
01244 static struct value *
01245 jit_frame_prev_register (struct frame_info *this_frame, void **cache, int reg)
01246 {
01247   struct jit_unwind_private *priv = *cache;
01248   struct gdb_reg_value *value;
01249 
01250   if (priv == NULL)
01251     return frame_unwind_got_optimized (this_frame, reg);
01252 
01253   gdb_assert (priv->registers);
01254   value = priv->registers[reg];
01255   if (value && value->defined)
01256     return frame_unwind_got_bytes (this_frame, reg, value->value);
01257   else
01258     return frame_unwind_got_optimized (this_frame, reg);
01259 }
01260 
01261 /* Relay everything back to the unwinder registered by the JIT debug
01262    info reader.*/
01263 
01264 static const struct frame_unwind jit_frame_unwind =
01265 {
01266   NORMAL_FRAME,
01267   default_frame_unwind_stop_reason,
01268   jit_frame_this_id,
01269   jit_frame_prev_register,
01270   NULL,
01271   jit_frame_sniffer,
01272   jit_dealloc_cache
01273 };
01274 
01275 
01276 /* This is the information that is stored at jit_gdbarch_data for each
01277    architecture.  */
01278 
01279 struct jit_gdbarch_data_type
01280 {
01281   /* Has the (pseudo) unwinder been prepended? */
01282   int unwinder_registered;
01283 };
01284 
01285 /* Check GDBARCH and prepend the pseudo JIT unwinder if needed.  */
01286 
01287 static void
01288 jit_prepend_unwinder (struct gdbarch *gdbarch)
01289 {
01290   struct jit_gdbarch_data_type *data;
01291 
01292   data = gdbarch_data (gdbarch, jit_gdbarch_data);
01293   if (!data->unwinder_registered)
01294     {
01295       frame_unwind_prepend_unwinder (gdbarch, &jit_frame_unwind);
01296       data->unwinder_registered = 1;
01297     }
01298 }
01299 
01300 /* Register any already created translations.  */
01301 
01302 static void
01303 jit_inferior_init (struct gdbarch *gdbarch)
01304 {
01305   struct jit_descriptor descriptor;
01306   struct jit_code_entry cur_entry;
01307   struct jit_program_space_data *ps_data;
01308   CORE_ADDR cur_entry_addr;
01309 
01310   if (jit_debug)
01311     fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n");
01312 
01313   jit_prepend_unwinder (gdbarch);
01314 
01315   ps_data = get_jit_program_space_data ();
01316   if (jit_breakpoint_re_set_internal (gdbarch, ps_data) != 0)
01317     return;
01318 
01319   /* Read the descriptor so we can check the version number and load
01320      any already JITed functions.  */
01321   if (!jit_read_descriptor (gdbarch, &descriptor, ps_data))
01322     return;
01323 
01324   /* Check that the version number agrees with that we support.  */
01325   if (descriptor.version != 1)
01326     {
01327       printf_unfiltered (_("Unsupported JIT protocol version %ld "
01328                            "in descriptor (expected 1)\n"),
01329                          (long) descriptor.version);
01330       return;
01331     }
01332 
01333   /* If we've attached to a running program, we need to check the descriptor
01334      to register any functions that were already generated.  */
01335   for (cur_entry_addr = descriptor.first_entry;
01336        cur_entry_addr != 0;
01337        cur_entry_addr = cur_entry.next_entry)
01338     {
01339       jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
01340 
01341       /* This hook may be called many times during setup, so make sure we don't
01342          add the same symbol file twice.  */
01343       if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
01344         continue;
01345 
01346       jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
01347     }
01348 }
01349 
01350 /* Exported routine to call when an inferior has been created.  */
01351 
01352 void
01353 jit_inferior_created_hook (void)
01354 {
01355   jit_inferior_init (target_gdbarch ());
01356 }
01357 
01358 /* Exported routine to call to re-set the jit breakpoints,
01359    e.g. when a program is rerun.  */
01360 
01361 void
01362 jit_breakpoint_re_set (void)
01363 {
01364   jit_breakpoint_re_set_internal (target_gdbarch (),
01365                                   get_jit_program_space_data ());
01366 }
01367 
01368 /* This function cleans up any code entries left over when the
01369    inferior exits.  We get left over code when the inferior exits
01370    without unregistering its code, for example when it crashes.  */
01371 
01372 static void
01373 jit_inferior_exit_hook (struct inferior *inf)
01374 {
01375   struct objfile *objf;
01376   struct objfile *temp;
01377 
01378   ALL_OBJFILES_SAFE (objf, temp)
01379     {
01380       struct jit_objfile_data *objf_data = objfile_data (objf,
01381                                                          jit_objfile_data);
01382 
01383       if (objf_data != NULL && objf_data->addr != 0)
01384         jit_unregister_code (objf);
01385     }
01386 }
01387 
01388 void
01389 jit_event_handler (struct gdbarch *gdbarch)
01390 {
01391   struct jit_descriptor descriptor;
01392   struct jit_code_entry code_entry;
01393   CORE_ADDR entry_addr;
01394   struct objfile *objf;
01395 
01396   /* Read the descriptor from remote memory.  */
01397   if (!jit_read_descriptor (gdbarch, &descriptor,
01398                             get_jit_program_space_data ()))
01399     return;
01400   entry_addr = descriptor.relevant_entry;
01401 
01402   /* Do the corresponding action.  */
01403   switch (descriptor.action_flag)
01404     {
01405     case JIT_NOACTION:
01406       break;
01407     case JIT_REGISTER:
01408       jit_read_code_entry (gdbarch, entry_addr, &code_entry);
01409       jit_register_code (gdbarch, entry_addr, &code_entry);
01410       break;
01411     case JIT_UNREGISTER:
01412       objf = jit_find_objf_with_entry_addr (entry_addr);
01413       if (objf == NULL)
01414         printf_unfiltered (_("Unable to find JITed code "
01415                              "entry at address: %s\n"),
01416                            paddress (gdbarch, entry_addr));
01417       else
01418         jit_unregister_code (objf);
01419 
01420       break;
01421     default:
01422       error (_("Unknown action_flag value in JIT descriptor!"));
01423       break;
01424     }
01425 }
01426 
01427 /* Called to free the data allocated to the jit_program_space_data slot.  */
01428 
01429 static void
01430 free_objfile_data (struct objfile *objfile, void *data)
01431 {
01432   struct jit_objfile_data *objf_data = data;
01433 
01434   if (objf_data->register_code != NULL)
01435     {
01436       struct jit_program_space_data *ps_data;
01437 
01438       ps_data = program_space_data (objfile->pspace, jit_program_space_data);
01439       if (ps_data != NULL && ps_data->objfile == objfile)
01440         ps_data->objfile = NULL;
01441     }
01442 
01443   xfree (data);
01444 }
01445 
01446 /* Initialize the jit_gdbarch_data slot with an instance of struct
01447    jit_gdbarch_data_type */
01448 
01449 static void *
01450 jit_gdbarch_data_init (struct obstack *obstack)
01451 {
01452   struct jit_gdbarch_data_type *data;
01453 
01454   data = obstack_alloc (obstack, sizeof (struct jit_gdbarch_data_type));
01455   data->unwinder_registered = 0;
01456   return data;
01457 }
01458 
01459 /* Provide a prototype to silence -Wmissing-prototypes.  */
01460 
01461 extern void _initialize_jit (void);
01462 
01463 void
01464 _initialize_jit (void)
01465 {
01466   jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR,
01467                                            JIT_READER_DIR_RELOCATABLE);
01468   add_setshow_zuinteger_cmd ("jit", class_maintenance, &jit_debug,
01469                              _("Set JIT debugging."),
01470                              _("Show JIT debugging."),
01471                              _("When non-zero, JIT debugging is enabled."),
01472                              NULL,
01473                              show_jit_debug,
01474                              &setdebuglist, &showdebuglist);
01475 
01476   observer_attach_inferior_exit (jit_inferior_exit_hook);
01477   observer_attach_breakpoint_deleted (jit_breakpoint_deleted);
01478 
01479   jit_objfile_data =
01480     register_objfile_data_with_cleanup (NULL, free_objfile_data);
01481   jit_program_space_data =
01482     register_program_space_data_with_cleanup (NULL,
01483                                               jit_program_space_data_cleanup);
01484   jit_gdbarch_data = gdbarch_data_register_pre_init (jit_gdbarch_data_init);
01485   if (is_dl_available ())
01486     {
01487       add_com ("jit-reader-load", no_class, jit_reader_load_command, _("\
01488 Load FILE as debug info reader and unwinder for JIT compiled code.\n\
01489 Usage: jit-reader-load FILE\n\
01490 Try to load file FILE as a debug info reader (and unwinder) for\n\
01491 JIT compiled code.  The file is loaded from " JIT_READER_DIR ",\n\
01492 relocated relative to the GDB executable if required."));
01493       add_com ("jit-reader-unload", no_class, jit_reader_unload_command, _("\
01494 Unload the currently loaded JIT debug info reader.\n\
01495 Usage: jit-reader-unload FILE\n\n\
01496 Do \"help jit-reader-load\" for info on loading debug info readers."));
01497     }
01498 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines