xref: /dflybsd-src/contrib/gdb-7/gdb/jit.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 2009-2012 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"
23*a45ae5f8SJohn Marino #include "jit-reader.h"
24*a45ae5f8SJohn Marino #include "block.h"
255796c8dcSSimon Schubert #include "breakpoint.h"
26c50c785cSJohn Marino #include "command.h"
27*a45ae5f8SJohn Marino #include "dictionary.h"
28*a45ae5f8SJohn Marino #include "frame-unwind.h"
29c50c785cSJohn Marino #include "gdbcmd.h"
305796c8dcSSimon Schubert #include "gdbcore.h"
31c50c785cSJohn Marino #include "inferior.h"
325796c8dcSSimon Schubert #include "observer.h"
335796c8dcSSimon Schubert #include "objfiles.h"
34*a45ae5f8SJohn Marino #include "regcache.h"
355796c8dcSSimon Schubert #include "symfile.h"
365796c8dcSSimon Schubert #include "symtab.h"
375796c8dcSSimon Schubert #include "target.h"
38*a45ae5f8SJohn Marino #include "gdb-dlfcn.h"
395796c8dcSSimon Schubert #include "gdb_stat.h"
40*a45ae5f8SJohn Marino #include "exceptions.h"
41*a45ae5f8SJohn Marino 
42*a45ae5f8SJohn Marino static const char *jit_reader_dir = NULL;
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert static const struct objfile_data *jit_objfile_data;
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert static const char *const jit_break_name = "__jit_debug_register_code";
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert static const char *const jit_descriptor_name = "__jit_debug_descriptor";
495796c8dcSSimon Schubert 
50c50c785cSJohn Marino static const struct inferior_data *jit_inferior_data = NULL;
515796c8dcSSimon Schubert 
52*a45ae5f8SJohn Marino static void jit_inferior_init (struct gdbarch *gdbarch);
53*a45ae5f8SJohn Marino 
54*a45ae5f8SJohn Marino /* An unwinder is registered for every gdbarch.  This key is used to
55*a45ae5f8SJohn Marino    remember if the unwinder has been registered for a particular
56*a45ae5f8SJohn Marino    gdbarch.  */
57*a45ae5f8SJohn Marino 
58*a45ae5f8SJohn Marino static struct gdbarch_data *jit_gdbarch_data;
59c50c785cSJohn Marino 
60c50c785cSJohn Marino /* Non-zero if we want to see trace of jit level stuff.  */
61c50c785cSJohn Marino 
62c50c785cSJohn Marino static int jit_debug = 0;
63c50c785cSJohn Marino 
64c50c785cSJohn Marino static void
65c50c785cSJohn Marino show_jit_debug (struct ui_file *file, int from_tty,
66c50c785cSJohn Marino 		struct cmd_list_element *c, const char *value)
675796c8dcSSimon Schubert {
68c50c785cSJohn Marino   fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
695796c8dcSSimon Schubert }
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert struct target_buffer
725796c8dcSSimon Schubert {
735796c8dcSSimon Schubert   CORE_ADDR base;
74c50c785cSJohn Marino   ULONGEST size;
755796c8dcSSimon Schubert };
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert /* Openning the file is a no-op.  */
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert static void *
805796c8dcSSimon Schubert mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
815796c8dcSSimon Schubert {
825796c8dcSSimon Schubert   return open_closure;
835796c8dcSSimon Schubert }
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert /* Closing the file is just freeing the base/size pair on our side.  */
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert static int
885796c8dcSSimon Schubert mem_bfd_iovec_close (struct bfd *abfd, void *stream)
895796c8dcSSimon Schubert {
905796c8dcSSimon Schubert   xfree (stream);
915796c8dcSSimon Schubert   return 1;
925796c8dcSSimon Schubert }
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert /* For reading the file, we just need to pass through to target_read_memory and
955796c8dcSSimon Schubert    fix up the arguments and return values.  */
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert static file_ptr
985796c8dcSSimon Schubert mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
995796c8dcSSimon Schubert                      file_ptr nbytes, file_ptr offset)
1005796c8dcSSimon Schubert {
1015796c8dcSSimon Schubert   int err;
1025796c8dcSSimon Schubert   struct target_buffer *buffer = (struct target_buffer *) stream;
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert   /* If this read will read all of the file, limit it to just the rest.  */
1055796c8dcSSimon Schubert   if (offset + nbytes > buffer->size)
1065796c8dcSSimon Schubert     nbytes = buffer->size - offset;
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert   /* If there are no more bytes left, we've reached EOF.  */
1095796c8dcSSimon Schubert   if (nbytes == 0)
1105796c8dcSSimon Schubert     return 0;
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert   err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
1135796c8dcSSimon Schubert   if (err)
1145796c8dcSSimon Schubert     return -1;
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert   return nbytes;
1175796c8dcSSimon Schubert }
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert /* For statting the file, we only support the st_size attribute.  */
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert static int
1225796c8dcSSimon Schubert mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
1235796c8dcSSimon Schubert {
1245796c8dcSSimon Schubert   struct target_buffer *buffer = (struct target_buffer*) stream;
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert   sb->st_size = buffer->size;
1275796c8dcSSimon Schubert   return 0;
1285796c8dcSSimon Schubert }
1295796c8dcSSimon Schubert 
130*a45ae5f8SJohn Marino /* One reader that has been loaded successfully, and can potentially be used to
131*a45ae5f8SJohn Marino    parse debug info.  */
132*a45ae5f8SJohn Marino 
133*a45ae5f8SJohn Marino static struct jit_reader
134*a45ae5f8SJohn Marino {
135*a45ae5f8SJohn Marino   struct gdb_reader_funcs *functions;
136*a45ae5f8SJohn Marino   void *handle;
137*a45ae5f8SJohn Marino } *loaded_jit_reader = NULL;
138*a45ae5f8SJohn Marino 
139*a45ae5f8SJohn Marino typedef struct gdb_reader_funcs * (reader_init_fn_type) (void);
140*a45ae5f8SJohn Marino static const char *reader_init_fn_sym = "gdb_init_reader";
141*a45ae5f8SJohn Marino 
142*a45ae5f8SJohn Marino /* Try to load FILE_NAME as a JIT debug info reader.  */
143*a45ae5f8SJohn Marino 
144*a45ae5f8SJohn Marino static struct jit_reader *
145*a45ae5f8SJohn Marino jit_reader_load (const char *file_name)
146*a45ae5f8SJohn Marino {
147*a45ae5f8SJohn Marino   void *so;
148*a45ae5f8SJohn Marino   reader_init_fn_type *init_fn;
149*a45ae5f8SJohn Marino   struct jit_reader *new_reader = NULL;
150*a45ae5f8SJohn Marino   struct gdb_reader_funcs *funcs = NULL;
151*a45ae5f8SJohn Marino   struct cleanup *old_cleanups;
152*a45ae5f8SJohn Marino 
153*a45ae5f8SJohn Marino   if (jit_debug)
154*a45ae5f8SJohn Marino     fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"),
155*a45ae5f8SJohn Marino                         file_name);
156*a45ae5f8SJohn Marino   so = gdb_dlopen (file_name);
157*a45ae5f8SJohn Marino   old_cleanups = make_cleanup_dlclose (so);
158*a45ae5f8SJohn Marino 
159*a45ae5f8SJohn Marino   init_fn = gdb_dlsym (so, reader_init_fn_sym);
160*a45ae5f8SJohn Marino   if (!init_fn)
161*a45ae5f8SJohn Marino     error (_("Could not locate initialization function: %s."),
162*a45ae5f8SJohn Marino           reader_init_fn_sym);
163*a45ae5f8SJohn Marino 
164*a45ae5f8SJohn Marino   if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL)
165*a45ae5f8SJohn Marino     error (_("Reader not GPL compatible."));
166*a45ae5f8SJohn Marino 
167*a45ae5f8SJohn Marino   funcs = init_fn ();
168*a45ae5f8SJohn Marino   if (funcs->reader_version != GDB_READER_INTERFACE_VERSION)
169*a45ae5f8SJohn Marino     error (_("Reader version does not match GDB version."));
170*a45ae5f8SJohn Marino 
171*a45ae5f8SJohn Marino   new_reader = XZALLOC (struct jit_reader);
172*a45ae5f8SJohn Marino   new_reader->functions = funcs;
173*a45ae5f8SJohn Marino   new_reader->handle = so;
174*a45ae5f8SJohn Marino 
175*a45ae5f8SJohn Marino   discard_cleanups (old_cleanups);
176*a45ae5f8SJohn Marino   return new_reader;
177*a45ae5f8SJohn Marino }
178*a45ae5f8SJohn Marino 
179*a45ae5f8SJohn Marino /* Provides the jit-reader-load command.  */
180*a45ae5f8SJohn Marino 
181*a45ae5f8SJohn Marino static void
182*a45ae5f8SJohn Marino jit_reader_load_command (char *args, int from_tty)
183*a45ae5f8SJohn Marino {
184*a45ae5f8SJohn Marino   char *so_name;
185*a45ae5f8SJohn Marino   int len;
186*a45ae5f8SJohn Marino   struct cleanup *prev_cleanup;
187*a45ae5f8SJohn Marino 
188*a45ae5f8SJohn Marino   if (args == NULL)
189*a45ae5f8SJohn Marino     error (_("No reader name provided."));
190*a45ae5f8SJohn Marino 
191*a45ae5f8SJohn Marino   if (loaded_jit_reader != NULL)
192*a45ae5f8SJohn Marino     error (_("JIT reader already loaded.  Run jit-reader-unload first."));
193*a45ae5f8SJohn Marino 
194*a45ae5f8SJohn Marino   so_name = xstrprintf ("%s/%s", jit_reader_dir, args);
195*a45ae5f8SJohn Marino   prev_cleanup = make_cleanup (xfree, so_name);
196*a45ae5f8SJohn Marino 
197*a45ae5f8SJohn Marino   loaded_jit_reader = jit_reader_load (so_name);
198*a45ae5f8SJohn Marino   do_cleanups (prev_cleanup);
199*a45ae5f8SJohn Marino }
200*a45ae5f8SJohn Marino 
201*a45ae5f8SJohn Marino /* Provides the jit-reader-unload command.  */
202*a45ae5f8SJohn Marino 
203*a45ae5f8SJohn Marino static void
204*a45ae5f8SJohn Marino jit_reader_unload_command (char *args, int from_tty)
205*a45ae5f8SJohn Marino {
206*a45ae5f8SJohn Marino   if (!loaded_jit_reader)
207*a45ae5f8SJohn Marino     error (_("No JIT reader loaded."));
208*a45ae5f8SJohn Marino 
209*a45ae5f8SJohn Marino   loaded_jit_reader->functions->destroy (loaded_jit_reader->functions);
210*a45ae5f8SJohn Marino 
211*a45ae5f8SJohn Marino   gdb_dlclose (loaded_jit_reader->handle);
212*a45ae5f8SJohn Marino   xfree (loaded_jit_reader);
213*a45ae5f8SJohn Marino   loaded_jit_reader = NULL;
214*a45ae5f8SJohn Marino }
215*a45ae5f8SJohn Marino 
2165796c8dcSSimon Schubert /* Open a BFD from the target's memory.  */
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert static struct bfd *
219c50c785cSJohn Marino bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
2205796c8dcSSimon Schubert {
2215796c8dcSSimon Schubert   const char *filename = xstrdup ("<in-memory>");
2225796c8dcSSimon Schubert   struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert   buffer->base = addr;
2255796c8dcSSimon Schubert   buffer->size = size;
2265796c8dcSSimon Schubert   return bfd_openr_iovec (filename, target,
2275796c8dcSSimon Schubert                           mem_bfd_iovec_open,
2285796c8dcSSimon Schubert                           buffer,
2295796c8dcSSimon Schubert                           mem_bfd_iovec_pread,
2305796c8dcSSimon Schubert                           mem_bfd_iovec_close,
2315796c8dcSSimon Schubert                           mem_bfd_iovec_stat);
2325796c8dcSSimon Schubert }
2335796c8dcSSimon Schubert 
234c50c785cSJohn Marino /* Per-inferior structure recording the addresses in the inferior.  */
235c50c785cSJohn Marino 
236c50c785cSJohn Marino struct jit_inferior_data
237c50c785cSJohn Marino {
238c50c785cSJohn Marino   CORE_ADDR breakpoint_addr;  /* &__jit_debug_register_code()  */
239c50c785cSJohn Marino   CORE_ADDR descriptor_addr;  /* &__jit_debug_descriptor  */
240c50c785cSJohn Marino };
241c50c785cSJohn Marino 
242*a45ae5f8SJohn Marino /* Remember OBJFILE has been created for struct jit_code_entry located
243*a45ae5f8SJohn Marino    at inferior address ENTRY.  */
244*a45ae5f8SJohn Marino 
245*a45ae5f8SJohn Marino static void
246*a45ae5f8SJohn Marino add_objfile_entry (struct objfile *objfile, CORE_ADDR entry)
247*a45ae5f8SJohn Marino {
248*a45ae5f8SJohn Marino   CORE_ADDR *entry_addr_ptr;
249*a45ae5f8SJohn Marino 
250*a45ae5f8SJohn Marino   entry_addr_ptr = xmalloc (sizeof (CORE_ADDR));
251*a45ae5f8SJohn Marino   *entry_addr_ptr = entry;
252*a45ae5f8SJohn Marino   set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr);
253*a45ae5f8SJohn Marino }
254*a45ae5f8SJohn Marino 
255c50c785cSJohn Marino /* Return jit_inferior_data for current inferior.  Allocate if not already
256c50c785cSJohn Marino    present.  */
257c50c785cSJohn Marino 
258c50c785cSJohn Marino static struct jit_inferior_data *
259c50c785cSJohn Marino get_jit_inferior_data (void)
260c50c785cSJohn Marino {
261c50c785cSJohn Marino   struct inferior *inf;
262c50c785cSJohn Marino   struct jit_inferior_data *inf_data;
263c50c785cSJohn Marino 
264c50c785cSJohn Marino   inf = current_inferior ();
265c50c785cSJohn Marino   inf_data = inferior_data (inf, jit_inferior_data);
266c50c785cSJohn Marino   if (inf_data == NULL)
267c50c785cSJohn Marino     {
268c50c785cSJohn Marino       inf_data = XZALLOC (struct jit_inferior_data);
269c50c785cSJohn Marino       set_inferior_data (inf, jit_inferior_data, inf_data);
270c50c785cSJohn Marino     }
271c50c785cSJohn Marino 
272c50c785cSJohn Marino   return inf_data;
273c50c785cSJohn Marino }
274c50c785cSJohn Marino 
275c50c785cSJohn Marino static void
276c50c785cSJohn Marino jit_inferior_data_cleanup (struct inferior *inf, void *arg)
277c50c785cSJohn Marino {
278c50c785cSJohn Marino   xfree (arg);
279c50c785cSJohn Marino }
280c50c785cSJohn Marino 
281c50c785cSJohn Marino /* Helper function for reading the global JIT descriptor from remote
282c50c785cSJohn Marino    memory.  */
2835796c8dcSSimon Schubert 
2845796c8dcSSimon Schubert static void
2855796c8dcSSimon Schubert jit_read_descriptor (struct gdbarch *gdbarch,
286c50c785cSJohn Marino 		     struct jit_descriptor *descriptor,
287c50c785cSJohn Marino 		     CORE_ADDR descriptor_addr)
2885796c8dcSSimon Schubert {
2895796c8dcSSimon Schubert   int err;
2905796c8dcSSimon Schubert   struct type *ptr_type;
2915796c8dcSSimon Schubert   int ptr_size;
2925796c8dcSSimon Schubert   int desc_size;
2935796c8dcSSimon Schubert   gdb_byte *desc_buf;
2945796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert   /* Figure out how big the descriptor is on the remote and how to read it.  */
2975796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
2985796c8dcSSimon Schubert   ptr_size = TYPE_LENGTH (ptr_type);
2995796c8dcSSimon Schubert   desc_size = 8 + 2 * ptr_size;  /* Two 32-bit ints and two pointers.  */
3005796c8dcSSimon Schubert   desc_buf = alloca (desc_size);
3015796c8dcSSimon Schubert 
3025796c8dcSSimon Schubert   /* Read the descriptor.  */
303c50c785cSJohn Marino   err = target_read_memory (descriptor_addr, desc_buf, desc_size);
3045796c8dcSSimon Schubert   if (err)
3055796c8dcSSimon Schubert     error (_("Unable to read JIT descriptor from remote memory!"));
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert   /* Fix the endianness to match the host.  */
3085796c8dcSSimon Schubert   descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
3095796c8dcSSimon Schubert   descriptor->action_flag =
3105796c8dcSSimon Schubert       extract_unsigned_integer (&desc_buf[4], 4, byte_order);
3115796c8dcSSimon Schubert   descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
3125796c8dcSSimon Schubert   descriptor->first_entry =
3135796c8dcSSimon Schubert       extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
3145796c8dcSSimon Schubert }
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert /* Helper function for reading a JITed code entry from remote memory.  */
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert static void
3195796c8dcSSimon Schubert jit_read_code_entry (struct gdbarch *gdbarch,
3205796c8dcSSimon Schubert 		     CORE_ADDR code_addr, struct jit_code_entry *code_entry)
3215796c8dcSSimon Schubert {
322*a45ae5f8SJohn Marino   int err, off;
3235796c8dcSSimon Schubert   struct type *ptr_type;
3245796c8dcSSimon Schubert   int ptr_size;
3255796c8dcSSimon Schubert   int entry_size;
326*a45ae5f8SJohn Marino   int align_bytes;
3275796c8dcSSimon Schubert   gdb_byte *entry_buf;
3285796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   /* Figure out how big the entry is on the remote and how to read it.  */
3315796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
3325796c8dcSSimon Schubert   ptr_size = TYPE_LENGTH (ptr_type);
3335796c8dcSSimon Schubert   entry_size = 3 * ptr_size + 8;  /* Three pointers and one 64-bit int.  */
3345796c8dcSSimon Schubert   entry_buf = alloca (entry_size);
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert   /* Read the entry.  */
3375796c8dcSSimon Schubert   err = target_read_memory (code_addr, entry_buf, entry_size);
3385796c8dcSSimon Schubert   if (err)
3395796c8dcSSimon Schubert     error (_("Unable to read JIT code entry from remote memory!"));
3405796c8dcSSimon Schubert 
3415796c8dcSSimon Schubert   /* Fix the endianness to match the host.  */
3425796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
3435796c8dcSSimon Schubert   code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
3445796c8dcSSimon Schubert   code_entry->prev_entry =
3455796c8dcSSimon Schubert       extract_typed_address (&entry_buf[ptr_size], ptr_type);
3465796c8dcSSimon Schubert   code_entry->symfile_addr =
3475796c8dcSSimon Schubert       extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
348*a45ae5f8SJohn Marino 
349*a45ae5f8SJohn Marino   align_bytes = gdbarch_long_long_align_bit (gdbarch) / 8;
350*a45ae5f8SJohn Marino   off = 3 * ptr_size;
351*a45ae5f8SJohn Marino   off = (off + (align_bytes - 1)) & ~(align_bytes - 1);
352*a45ae5f8SJohn Marino 
3535796c8dcSSimon Schubert   code_entry->symfile_size =
354*a45ae5f8SJohn Marino       extract_unsigned_integer (&entry_buf[off], 8, byte_order);
3555796c8dcSSimon Schubert }
3565796c8dcSSimon Schubert 
357*a45ae5f8SJohn Marino /* Proxy object for building a block.  */
358*a45ae5f8SJohn Marino 
359*a45ae5f8SJohn Marino struct gdb_block
360*a45ae5f8SJohn Marino {
361*a45ae5f8SJohn Marino   /* gdb_blocks are linked into a tree structure.  Next points to the
362*a45ae5f8SJohn Marino      next node at the same depth as this block and parent to the
363*a45ae5f8SJohn Marino      parent gdb_block.  */
364*a45ae5f8SJohn Marino   struct gdb_block *next, *parent;
365*a45ae5f8SJohn Marino 
366*a45ae5f8SJohn Marino   /* Points to the "real" block that is being built out of this
367*a45ae5f8SJohn Marino      instance.  This block will be added to a blockvector, which will
368*a45ae5f8SJohn Marino      then be added to a symtab.  */
369*a45ae5f8SJohn Marino   struct block *real_block;
370*a45ae5f8SJohn Marino 
371*a45ae5f8SJohn Marino   /* The first and last code address corresponding to this block.  */
372*a45ae5f8SJohn Marino   CORE_ADDR begin, end;
373*a45ae5f8SJohn Marino 
374*a45ae5f8SJohn Marino   /* The name of this block (if any).  If this is non-NULL, the
375*a45ae5f8SJohn Marino      FUNCTION symbol symbol is set to this value.  */
376*a45ae5f8SJohn Marino   const char *name;
377*a45ae5f8SJohn Marino };
378*a45ae5f8SJohn Marino 
379*a45ae5f8SJohn Marino /* Proxy object for building a symtab.  */
380*a45ae5f8SJohn Marino 
381*a45ae5f8SJohn Marino struct gdb_symtab
382*a45ae5f8SJohn Marino {
383*a45ae5f8SJohn Marino   /* The list of blocks in this symtab.  These will eventually be
384*a45ae5f8SJohn Marino      converted to real blocks.  */
385*a45ae5f8SJohn Marino   struct gdb_block *blocks;
386*a45ae5f8SJohn Marino 
387*a45ae5f8SJohn Marino   /* The number of blocks inserted.  */
388*a45ae5f8SJohn Marino   int nblocks;
389*a45ae5f8SJohn Marino 
390*a45ae5f8SJohn Marino   /* A mapping between line numbers to PC.  */
391*a45ae5f8SJohn Marino   struct linetable *linetable;
392*a45ae5f8SJohn Marino 
393*a45ae5f8SJohn Marino   /* The source file for this symtab.  */
394*a45ae5f8SJohn Marino   const char *file_name;
395*a45ae5f8SJohn Marino   struct gdb_symtab *next;
396*a45ae5f8SJohn Marino };
397*a45ae5f8SJohn Marino 
398*a45ae5f8SJohn Marino /* Proxy object for building an object.  */
399*a45ae5f8SJohn Marino 
400*a45ae5f8SJohn Marino struct gdb_object
401*a45ae5f8SJohn Marino {
402*a45ae5f8SJohn Marino   struct gdb_symtab *symtabs;
403*a45ae5f8SJohn Marino };
404*a45ae5f8SJohn Marino 
405*a45ae5f8SJohn Marino /* The type of the `private' data passed around by the callback
406*a45ae5f8SJohn Marino    functions.  */
407*a45ae5f8SJohn Marino 
408*a45ae5f8SJohn Marino typedef CORE_ADDR jit_dbg_reader_data;
409*a45ae5f8SJohn Marino 
410*a45ae5f8SJohn Marino /* The reader calls into this function to read data off the targets
411*a45ae5f8SJohn Marino    address space.  */
412*a45ae5f8SJohn Marino 
413*a45ae5f8SJohn Marino static enum gdb_status
414*a45ae5f8SJohn Marino jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len)
415*a45ae5f8SJohn Marino {
416*a45ae5f8SJohn Marino   int result = target_read_memory ((CORE_ADDR) target_mem, gdb_buf, len);
417*a45ae5f8SJohn Marino   if (result == 0)
418*a45ae5f8SJohn Marino     return GDB_SUCCESS;
419*a45ae5f8SJohn Marino   else
420*a45ae5f8SJohn Marino     return GDB_FAIL;
421*a45ae5f8SJohn Marino }
422*a45ae5f8SJohn Marino 
423*a45ae5f8SJohn Marino /* The reader calls into this function to create a new gdb_object
424*a45ae5f8SJohn Marino    which it can then pass around to the other callbacks.  Right now,
425*a45ae5f8SJohn Marino    all that is required is allocating the memory.  */
426*a45ae5f8SJohn Marino 
427*a45ae5f8SJohn Marino static struct gdb_object *
428*a45ae5f8SJohn Marino jit_object_open_impl (struct gdb_symbol_callbacks *cb)
429*a45ae5f8SJohn Marino {
430*a45ae5f8SJohn Marino   /* CB is not required right now, but sometime in the future we might
431*a45ae5f8SJohn Marino      need a handle to it, and we'd like to do that without breaking
432*a45ae5f8SJohn Marino      the ABI.  */
433*a45ae5f8SJohn Marino   return XZALLOC (struct gdb_object);
434*a45ae5f8SJohn Marino }
435*a45ae5f8SJohn Marino 
436*a45ae5f8SJohn Marino /* Readers call into this function to open a new gdb_symtab, which,
437*a45ae5f8SJohn Marino    again, is passed around to other callbacks.  */
438*a45ae5f8SJohn Marino 
439*a45ae5f8SJohn Marino static struct gdb_symtab *
440*a45ae5f8SJohn Marino jit_symtab_open_impl (struct gdb_symbol_callbacks *cb,
441*a45ae5f8SJohn Marino                       struct gdb_object *object,
442*a45ae5f8SJohn Marino                       const char *file_name)
443*a45ae5f8SJohn Marino {
444*a45ae5f8SJohn Marino   struct gdb_symtab *ret;
445*a45ae5f8SJohn Marino 
446*a45ae5f8SJohn Marino   /* CB stays unused.  See comment in jit_object_open_impl.  */
447*a45ae5f8SJohn Marino 
448*a45ae5f8SJohn Marino   ret = XZALLOC (struct gdb_symtab);
449*a45ae5f8SJohn Marino   ret->file_name = file_name ? xstrdup (file_name) : xstrdup ("");
450*a45ae5f8SJohn Marino   ret->next = object->symtabs;
451*a45ae5f8SJohn Marino   object->symtabs = ret;
452*a45ae5f8SJohn Marino   return ret;
453*a45ae5f8SJohn Marino }
454*a45ae5f8SJohn Marino 
455*a45ae5f8SJohn Marino /* Returns true if the block corresponding to old should be placed
456*a45ae5f8SJohn Marino    before the block corresponding to new in the final blockvector.  */
457*a45ae5f8SJohn Marino 
458*a45ae5f8SJohn Marino static int
459*a45ae5f8SJohn Marino compare_block (const struct gdb_block *const old,
460*a45ae5f8SJohn Marino                const struct gdb_block *const new)
461*a45ae5f8SJohn Marino {
462*a45ae5f8SJohn Marino   if (old == NULL)
463*a45ae5f8SJohn Marino     return 1;
464*a45ae5f8SJohn Marino   if (old->begin < new->begin)
465*a45ae5f8SJohn Marino     return 1;
466*a45ae5f8SJohn Marino   else if (old->begin == new->begin)
467*a45ae5f8SJohn Marino     {
468*a45ae5f8SJohn Marino       if (old->end > new->end)
469*a45ae5f8SJohn Marino         return 1;
470*a45ae5f8SJohn Marino       else
471*a45ae5f8SJohn Marino         return 0;
472*a45ae5f8SJohn Marino     }
473*a45ae5f8SJohn Marino   else
474*a45ae5f8SJohn Marino     return 0;
475*a45ae5f8SJohn Marino }
476*a45ae5f8SJohn Marino 
477*a45ae5f8SJohn Marino /* Called by readers to open a new gdb_block.  This function also
478*a45ae5f8SJohn Marino    inserts the new gdb_block in the correct place in the corresponding
479*a45ae5f8SJohn Marino    gdb_symtab.  */
480*a45ae5f8SJohn Marino 
481*a45ae5f8SJohn Marino static struct gdb_block *
482*a45ae5f8SJohn Marino jit_block_open_impl (struct gdb_symbol_callbacks *cb,
483*a45ae5f8SJohn Marino                      struct gdb_symtab *symtab, struct gdb_block *parent,
484*a45ae5f8SJohn Marino                      GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name)
485*a45ae5f8SJohn Marino {
486*a45ae5f8SJohn Marino   struct gdb_block *block = XZALLOC (struct gdb_block);
487*a45ae5f8SJohn Marino 
488*a45ae5f8SJohn Marino   block->next = symtab->blocks;
489*a45ae5f8SJohn Marino   block->begin = (CORE_ADDR) begin;
490*a45ae5f8SJohn Marino   block->end = (CORE_ADDR) end;
491*a45ae5f8SJohn Marino   block->name = name ? xstrdup (name) : NULL;
492*a45ae5f8SJohn Marino   block->parent = parent;
493*a45ae5f8SJohn Marino 
494*a45ae5f8SJohn Marino   /* Ensure that the blocks are inserted in the correct (reverse of
495*a45ae5f8SJohn Marino      the order expected by blockvector).  */
496*a45ae5f8SJohn Marino   if (compare_block (symtab->blocks, block))
497*a45ae5f8SJohn Marino     {
498*a45ae5f8SJohn Marino       symtab->blocks = block;
499*a45ae5f8SJohn Marino     }
500*a45ae5f8SJohn Marino   else
501*a45ae5f8SJohn Marino     {
502*a45ae5f8SJohn Marino       struct gdb_block *i = symtab->blocks;
503*a45ae5f8SJohn Marino 
504*a45ae5f8SJohn Marino       for (;; i = i->next)
505*a45ae5f8SJohn Marino         {
506*a45ae5f8SJohn Marino           /* Guaranteed to terminate, since compare_block (NULL, _)
507*a45ae5f8SJohn Marino              returns 1.  */
508*a45ae5f8SJohn Marino           if (compare_block (i->next, block))
509*a45ae5f8SJohn Marino             {
510*a45ae5f8SJohn Marino               block->next = i->next;
511*a45ae5f8SJohn Marino               i->next = block;
512*a45ae5f8SJohn Marino               break;
513*a45ae5f8SJohn Marino             }
514*a45ae5f8SJohn Marino         }
515*a45ae5f8SJohn Marino     }
516*a45ae5f8SJohn Marino   symtab->nblocks++;
517*a45ae5f8SJohn Marino 
518*a45ae5f8SJohn Marino   return block;
519*a45ae5f8SJohn Marino }
520*a45ae5f8SJohn Marino 
521*a45ae5f8SJohn Marino /* Readers call this to add a line mapping (from PC to line number) to
522*a45ae5f8SJohn Marino    a gdb_symtab.  */
5235796c8dcSSimon Schubert 
5245796c8dcSSimon Schubert static void
525*a45ae5f8SJohn Marino jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb,
526*a45ae5f8SJohn Marino                                   struct gdb_symtab *stab, int nlines,
527*a45ae5f8SJohn Marino                                   struct gdb_line_mapping *map)
528*a45ae5f8SJohn Marino {
529*a45ae5f8SJohn Marino   int i;
530*a45ae5f8SJohn Marino 
531*a45ae5f8SJohn Marino   if (nlines < 1)
532*a45ae5f8SJohn Marino     return;
533*a45ae5f8SJohn Marino 
534*a45ae5f8SJohn Marino   stab->linetable = xmalloc (sizeof (struct linetable)
535*a45ae5f8SJohn Marino                              + (nlines - 1) * sizeof (struct linetable_entry));
536*a45ae5f8SJohn Marino   stab->linetable->nitems = nlines;
537*a45ae5f8SJohn Marino   for (i = 0; i < nlines; i++)
538*a45ae5f8SJohn Marino     {
539*a45ae5f8SJohn Marino       stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc;
540*a45ae5f8SJohn Marino       stab->linetable->item[i].line = map[i].line;
541*a45ae5f8SJohn Marino     }
542*a45ae5f8SJohn Marino }
543*a45ae5f8SJohn Marino 
544*a45ae5f8SJohn Marino /* Called by readers to close a gdb_symtab.  Does not need to do
545*a45ae5f8SJohn Marino    anything as of now.  */
546*a45ae5f8SJohn Marino 
547*a45ae5f8SJohn Marino static void
548*a45ae5f8SJohn Marino jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
549*a45ae5f8SJohn Marino                        struct gdb_symtab *stab)
550*a45ae5f8SJohn Marino {
551*a45ae5f8SJohn Marino   /* Right now nothing needs to be done here.  We may need to do some
552*a45ae5f8SJohn Marino      cleanup here in the future (again, without breaking the plugin
553*a45ae5f8SJohn Marino      ABI).  */
554*a45ae5f8SJohn Marino }
555*a45ae5f8SJohn Marino 
556*a45ae5f8SJohn Marino /* Transform STAB to a proper symtab, and add it it OBJFILE.  */
557*a45ae5f8SJohn Marino 
558*a45ae5f8SJohn Marino static void
559*a45ae5f8SJohn Marino finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
560*a45ae5f8SJohn Marino {
561*a45ae5f8SJohn Marino   struct symtab *symtab;
562*a45ae5f8SJohn Marino   struct gdb_block *gdb_block_iter, *gdb_block_iter_tmp;
563*a45ae5f8SJohn Marino   struct block *block_iter;
564*a45ae5f8SJohn Marino   int actual_nblocks, i, blockvector_size;
565*a45ae5f8SJohn Marino   CORE_ADDR begin, end;
566*a45ae5f8SJohn Marino 
567*a45ae5f8SJohn Marino   actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks;
568*a45ae5f8SJohn Marino 
569*a45ae5f8SJohn Marino   symtab = allocate_symtab (stab->file_name, objfile);
570*a45ae5f8SJohn Marino   /* JIT compilers compile in memory.  */
571*a45ae5f8SJohn Marino   symtab->dirname = NULL;
572*a45ae5f8SJohn Marino 
573*a45ae5f8SJohn Marino   /* Copy over the linetable entry if one was provided.  */
574*a45ae5f8SJohn Marino   if (stab->linetable)
575*a45ae5f8SJohn Marino     {
576*a45ae5f8SJohn Marino       int size = ((stab->linetable->nitems - 1)
577*a45ae5f8SJohn Marino                   * sizeof (struct linetable_entry)
578*a45ae5f8SJohn Marino                   + sizeof (struct linetable));
579*a45ae5f8SJohn Marino       LINETABLE (symtab) = obstack_alloc (&objfile->objfile_obstack, size);
580*a45ae5f8SJohn Marino       memcpy (LINETABLE (symtab), stab->linetable, size);
581*a45ae5f8SJohn Marino     }
582*a45ae5f8SJohn Marino   else
583*a45ae5f8SJohn Marino     {
584*a45ae5f8SJohn Marino       LINETABLE (symtab) = NULL;
585*a45ae5f8SJohn Marino     }
586*a45ae5f8SJohn Marino 
587*a45ae5f8SJohn Marino   blockvector_size = (sizeof (struct blockvector)
588*a45ae5f8SJohn Marino                       + (actual_nblocks - 1) * sizeof (struct block *));
589*a45ae5f8SJohn Marino   symtab->blockvector = obstack_alloc (&objfile->objfile_obstack,
590*a45ae5f8SJohn Marino                                        blockvector_size);
591*a45ae5f8SJohn Marino 
592*a45ae5f8SJohn Marino   /* (begin, end) will contain the PC range this entire blockvector
593*a45ae5f8SJohn Marino      spans.  */
594*a45ae5f8SJohn Marino   symtab->primary = 1;
595*a45ae5f8SJohn Marino   BLOCKVECTOR_MAP (symtab->blockvector) = NULL;
596*a45ae5f8SJohn Marino   begin = stab->blocks->begin;
597*a45ae5f8SJohn Marino   end = stab->blocks->end;
598*a45ae5f8SJohn Marino   BLOCKVECTOR_NBLOCKS (symtab->blockvector) = actual_nblocks;
599*a45ae5f8SJohn Marino 
600*a45ae5f8SJohn Marino   /* First run over all the gdb_block objects, creating a real block
601*a45ae5f8SJohn Marino      object for each.  Simultaneously, keep setting the real_block
602*a45ae5f8SJohn Marino      fields.  */
603*a45ae5f8SJohn Marino   for (i = (actual_nblocks - 1), gdb_block_iter = stab->blocks;
604*a45ae5f8SJohn Marino        i >= FIRST_LOCAL_BLOCK;
605*a45ae5f8SJohn Marino        i--, gdb_block_iter = gdb_block_iter->next)
606*a45ae5f8SJohn Marino     {
607*a45ae5f8SJohn Marino       struct block *new_block = allocate_block (&objfile->objfile_obstack);
608*a45ae5f8SJohn Marino       struct symbol *block_name = obstack_alloc (&objfile->objfile_obstack,
609*a45ae5f8SJohn Marino                                                  sizeof (struct symbol));
610*a45ae5f8SJohn Marino 
611*a45ae5f8SJohn Marino       BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
612*a45ae5f8SJohn Marino                                                    NULL);
613*a45ae5f8SJohn Marino       /* The address range.  */
614*a45ae5f8SJohn Marino       BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter->begin;
615*a45ae5f8SJohn Marino       BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter->end;
616*a45ae5f8SJohn Marino 
617*a45ae5f8SJohn Marino       /* The name.  */
618*a45ae5f8SJohn Marino       memset (block_name, 0, sizeof (struct symbol));
619*a45ae5f8SJohn Marino       SYMBOL_DOMAIN (block_name) = VAR_DOMAIN;
620*a45ae5f8SJohn Marino       SYMBOL_CLASS (block_name) = LOC_BLOCK;
621*a45ae5f8SJohn Marino       SYMBOL_SYMTAB (block_name) = symtab;
622*a45ae5f8SJohn Marino       SYMBOL_BLOCK_VALUE (block_name) = new_block;
623*a45ae5f8SJohn Marino 
624*a45ae5f8SJohn Marino       block_name->ginfo.name = obsavestring (gdb_block_iter->name,
625*a45ae5f8SJohn Marino                                              strlen (gdb_block_iter->name),
626*a45ae5f8SJohn Marino                                              &objfile->objfile_obstack);
627*a45ae5f8SJohn Marino 
628*a45ae5f8SJohn Marino       BLOCK_FUNCTION (new_block) = block_name;
629*a45ae5f8SJohn Marino 
630*a45ae5f8SJohn Marino       BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
631*a45ae5f8SJohn Marino       if (begin > BLOCK_START (new_block))
632*a45ae5f8SJohn Marino         begin = BLOCK_START (new_block);
633*a45ae5f8SJohn Marino       if (end < BLOCK_END (new_block))
634*a45ae5f8SJohn Marino         end = BLOCK_END (new_block);
635*a45ae5f8SJohn Marino 
636*a45ae5f8SJohn Marino       gdb_block_iter->real_block = new_block;
637*a45ae5f8SJohn Marino     }
638*a45ae5f8SJohn Marino 
639*a45ae5f8SJohn Marino   /* Now add the special blocks.  */
640*a45ae5f8SJohn Marino   block_iter = NULL;
641*a45ae5f8SJohn Marino   for (i = 0; i < FIRST_LOCAL_BLOCK; i++)
642*a45ae5f8SJohn Marino     {
643*a45ae5f8SJohn Marino       struct block *new_block = allocate_block (&objfile->objfile_obstack);
644*a45ae5f8SJohn Marino       BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
645*a45ae5f8SJohn Marino                                                    NULL);
646*a45ae5f8SJohn Marino       BLOCK_SUPERBLOCK (new_block) = block_iter;
647*a45ae5f8SJohn Marino       block_iter = new_block;
648*a45ae5f8SJohn Marino 
649*a45ae5f8SJohn Marino       BLOCK_START (new_block) = (CORE_ADDR) begin;
650*a45ae5f8SJohn Marino       BLOCK_END (new_block) = (CORE_ADDR) end;
651*a45ae5f8SJohn Marino 
652*a45ae5f8SJohn Marino       BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
653*a45ae5f8SJohn Marino     }
654*a45ae5f8SJohn Marino 
655*a45ae5f8SJohn Marino   /* Fill up the superblock fields for the real blocks, using the
656*a45ae5f8SJohn Marino      real_block fields populated earlier.  */
657*a45ae5f8SJohn Marino   for (gdb_block_iter = stab->blocks;
658*a45ae5f8SJohn Marino        gdb_block_iter;
659*a45ae5f8SJohn Marino        gdb_block_iter = gdb_block_iter->next)
660*a45ae5f8SJohn Marino     {
661*a45ae5f8SJohn Marino       if (gdb_block_iter->parent != NULL)
662*a45ae5f8SJohn Marino         BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
663*a45ae5f8SJohn Marino           gdb_block_iter->parent->real_block;
664*a45ae5f8SJohn Marino     }
665*a45ae5f8SJohn Marino 
666*a45ae5f8SJohn Marino   /* Free memory.  */
667*a45ae5f8SJohn Marino   gdb_block_iter = stab->blocks;
668*a45ae5f8SJohn Marino 
669*a45ae5f8SJohn Marino   for (gdb_block_iter = stab->blocks, gdb_block_iter_tmp = gdb_block_iter->next;
670*a45ae5f8SJohn Marino        gdb_block_iter;
671*a45ae5f8SJohn Marino        gdb_block_iter = gdb_block_iter_tmp)
672*a45ae5f8SJohn Marino     {
673*a45ae5f8SJohn Marino       xfree ((void *) gdb_block_iter->name);
674*a45ae5f8SJohn Marino       xfree (gdb_block_iter);
675*a45ae5f8SJohn Marino     }
676*a45ae5f8SJohn Marino   xfree (stab->linetable);
677*a45ae5f8SJohn Marino   xfree ((char *) stab->file_name);
678*a45ae5f8SJohn Marino   xfree (stab);
679*a45ae5f8SJohn Marino }
680*a45ae5f8SJohn Marino 
681*a45ae5f8SJohn Marino /* Called when closing a gdb_objfile.  Converts OBJ to a proper
682*a45ae5f8SJohn Marino    objfile.  */
683*a45ae5f8SJohn Marino 
684*a45ae5f8SJohn Marino static void
685*a45ae5f8SJohn Marino jit_object_close_impl (struct gdb_symbol_callbacks *cb,
686*a45ae5f8SJohn Marino                        struct gdb_object *obj)
687*a45ae5f8SJohn Marino {
688*a45ae5f8SJohn Marino   struct gdb_symtab *i, *j;
689*a45ae5f8SJohn Marino   struct objfile *objfile;
690*a45ae5f8SJohn Marino   jit_dbg_reader_data *priv_data;
691*a45ae5f8SJohn Marino 
692*a45ae5f8SJohn Marino   priv_data = cb->priv_data;
693*a45ae5f8SJohn Marino 
694*a45ae5f8SJohn Marino   objfile = allocate_objfile (NULL, 0);
695*a45ae5f8SJohn Marino   objfile->gdbarch = target_gdbarch;
696*a45ae5f8SJohn Marino 
697*a45ae5f8SJohn Marino   objfile->msymbols = obstack_alloc (&objfile->objfile_obstack,
698*a45ae5f8SJohn Marino                                      sizeof (struct minimal_symbol));
699*a45ae5f8SJohn Marino   memset (objfile->msymbols, 0, sizeof (struct minimal_symbol));
700*a45ae5f8SJohn Marino 
701*a45ae5f8SJohn Marino   xfree (objfile->name);
702*a45ae5f8SJohn Marino   objfile->name = xstrdup ("<< JIT compiled code >>");
703*a45ae5f8SJohn Marino 
704*a45ae5f8SJohn Marino   j = NULL;
705*a45ae5f8SJohn Marino   for (i = obj->symtabs; i; i = j)
706*a45ae5f8SJohn Marino     {
707*a45ae5f8SJohn Marino       j = i->next;
708*a45ae5f8SJohn Marino       finalize_symtab (i, objfile);
709*a45ae5f8SJohn Marino     }
710*a45ae5f8SJohn Marino   add_objfile_entry (objfile, *priv_data);
711*a45ae5f8SJohn Marino   xfree (obj);
712*a45ae5f8SJohn Marino }
713*a45ae5f8SJohn Marino 
714*a45ae5f8SJohn Marino /* Try to read CODE_ENTRY using the loaded jit reader (if any).
715*a45ae5f8SJohn Marino    ENTRY_ADDR is the address of the struct jit_code_entry in the
716*a45ae5f8SJohn Marino    inferior address space.  */
717*a45ae5f8SJohn Marino 
718*a45ae5f8SJohn Marino static int
719*a45ae5f8SJohn Marino jit_reader_try_read_symtab (struct jit_code_entry *code_entry,
720*a45ae5f8SJohn Marino                             CORE_ADDR entry_addr)
721*a45ae5f8SJohn Marino {
722*a45ae5f8SJohn Marino   void *gdb_mem;
723*a45ae5f8SJohn Marino   int status;
724*a45ae5f8SJohn Marino   struct jit_dbg_reader *i;
725*a45ae5f8SJohn Marino   jit_dbg_reader_data priv_data;
726*a45ae5f8SJohn Marino   struct gdb_reader_funcs *funcs;
727*a45ae5f8SJohn Marino   volatile struct gdb_exception e;
728*a45ae5f8SJohn Marino   struct gdb_symbol_callbacks callbacks =
729*a45ae5f8SJohn Marino     {
730*a45ae5f8SJohn Marino       jit_object_open_impl,
731*a45ae5f8SJohn Marino       jit_symtab_open_impl,
732*a45ae5f8SJohn Marino       jit_block_open_impl,
733*a45ae5f8SJohn Marino       jit_symtab_close_impl,
734*a45ae5f8SJohn Marino       jit_object_close_impl,
735*a45ae5f8SJohn Marino 
736*a45ae5f8SJohn Marino       jit_symtab_line_mapping_add_impl,
737*a45ae5f8SJohn Marino       jit_target_read_impl,
738*a45ae5f8SJohn Marino 
739*a45ae5f8SJohn Marino       &priv_data
740*a45ae5f8SJohn Marino     };
741*a45ae5f8SJohn Marino 
742*a45ae5f8SJohn Marino   priv_data = entry_addr;
743*a45ae5f8SJohn Marino 
744*a45ae5f8SJohn Marino   if (!loaded_jit_reader)
745*a45ae5f8SJohn Marino     return 0;
746*a45ae5f8SJohn Marino 
747*a45ae5f8SJohn Marino   gdb_mem = xmalloc (code_entry->symfile_size);
748*a45ae5f8SJohn Marino 
749*a45ae5f8SJohn Marino   status = 1;
750*a45ae5f8SJohn Marino   TRY_CATCH (e, RETURN_MASK_ALL)
751*a45ae5f8SJohn Marino     if (target_read_memory (code_entry->symfile_addr, gdb_mem,
752*a45ae5f8SJohn Marino                             code_entry->symfile_size))
753*a45ae5f8SJohn Marino       status = 0;
754*a45ae5f8SJohn Marino   if (e.reason < 0)
755*a45ae5f8SJohn Marino     status = 0;
756*a45ae5f8SJohn Marino 
757*a45ae5f8SJohn Marino   if (status)
758*a45ae5f8SJohn Marino     {
759*a45ae5f8SJohn Marino       funcs = loaded_jit_reader->functions;
760*a45ae5f8SJohn Marino       if (funcs->read (funcs, &callbacks, gdb_mem, code_entry->symfile_size)
761*a45ae5f8SJohn Marino           != GDB_SUCCESS)
762*a45ae5f8SJohn Marino         status = 0;
763*a45ae5f8SJohn Marino     }
764*a45ae5f8SJohn Marino 
765*a45ae5f8SJohn Marino   xfree (gdb_mem);
766*a45ae5f8SJohn Marino   if (jit_debug && status == 0)
767*a45ae5f8SJohn Marino     fprintf_unfiltered (gdb_stdlog,
768*a45ae5f8SJohn Marino                         "Could not read symtab using the loaded JIT reader.\n");
769*a45ae5f8SJohn Marino   return status;
770*a45ae5f8SJohn Marino }
771*a45ae5f8SJohn Marino 
772*a45ae5f8SJohn Marino /* Try to read CODE_ENTRY using BFD.  ENTRY_ADDR is the address of the
773*a45ae5f8SJohn Marino    struct jit_code_entry in the inferior address space.  */
774*a45ae5f8SJohn Marino 
775*a45ae5f8SJohn Marino static void
776*a45ae5f8SJohn Marino jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
777*a45ae5f8SJohn Marino                          CORE_ADDR entry_addr,
778*a45ae5f8SJohn Marino                          struct gdbarch *gdbarch)
7795796c8dcSSimon Schubert {
7805796c8dcSSimon Schubert   bfd *nbfd;
7815796c8dcSSimon Schubert   struct section_addr_info *sai;
7825796c8dcSSimon Schubert   struct bfd_section *sec;
7835796c8dcSSimon Schubert   struct objfile *objfile;
784*a45ae5f8SJohn Marino   struct cleanup *old_cleanups;
7855796c8dcSSimon Schubert   int i;
7865796c8dcSSimon Schubert   const struct bfd_arch_info *b;
7875796c8dcSSimon Schubert 
788c50c785cSJohn Marino   if (jit_debug)
789c50c785cSJohn Marino     fprintf_unfiltered (gdb_stdlog,
790c50c785cSJohn Marino 			"jit_register_code, symfile_addr = %s, "
791c50c785cSJohn Marino 			"symfile_size = %s\n",
792c50c785cSJohn Marino 			paddress (gdbarch, code_entry->symfile_addr),
793c50c785cSJohn Marino 			pulongest (code_entry->symfile_size));
794c50c785cSJohn Marino 
7955796c8dcSSimon Schubert   nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
7965796c8dcSSimon Schubert                                       code_entry->symfile_size, gnutarget);
797*a45ae5f8SJohn Marino   if (nbfd == NULL)
798*a45ae5f8SJohn Marino     {
799*a45ae5f8SJohn Marino       puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
800*a45ae5f8SJohn Marino       return;
801*a45ae5f8SJohn Marino     }
8025796c8dcSSimon Schubert 
8035796c8dcSSimon Schubert   /* Check the format.  NOTE: This initializes important data that GDB uses!
8045796c8dcSSimon Schubert      We would segfault later without this line.  */
8055796c8dcSSimon Schubert   if (!bfd_check_format (nbfd, bfd_object))
8065796c8dcSSimon Schubert     {
8075796c8dcSSimon Schubert       printf_unfiltered (_("\
8085796c8dcSSimon Schubert JITed symbol file is not an object file, ignoring it.\n"));
809*a45ae5f8SJohn Marino       bfd_close (nbfd);
8105796c8dcSSimon Schubert       return;
8115796c8dcSSimon Schubert     }
8125796c8dcSSimon Schubert 
8135796c8dcSSimon Schubert   /* Check bfd arch.  */
8145796c8dcSSimon Schubert   b = gdbarch_bfd_arch_info (gdbarch);
8155796c8dcSSimon Schubert   if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
8165796c8dcSSimon Schubert     warning (_("JITed object file architecture %s is not compatible "
8175796c8dcSSimon Schubert                "with target architecture %s."), bfd_get_arch_info
8185796c8dcSSimon Schubert              (nbfd)->printable_name, b->printable_name);
8195796c8dcSSimon Schubert 
8205796c8dcSSimon Schubert   /* Read the section address information out of the symbol file.  Since the
8215796c8dcSSimon Schubert      file is generated by the JIT at runtime, it should all of the absolute
8225796c8dcSSimon Schubert      addresses that we care about.  */
8235796c8dcSSimon Schubert   sai = alloc_section_addr_info (bfd_count_sections (nbfd));
824*a45ae5f8SJohn Marino   old_cleanups = make_cleanup_free_section_addr_info (sai);
8255796c8dcSSimon Schubert   i = 0;
8265796c8dcSSimon Schubert   for (sec = nbfd->sections; sec != NULL; sec = sec->next)
8275796c8dcSSimon Schubert     if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
8285796c8dcSSimon Schubert       {
8295796c8dcSSimon Schubert         /* We assume that these virtual addresses are absolute, and do not
8305796c8dcSSimon Schubert            treat them as offsets.  */
8315796c8dcSSimon Schubert         sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
832cf7f2e2dSJohn Marino         sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
8335796c8dcSSimon Schubert         sai->other[i].sectindex = sec->index;
8345796c8dcSSimon Schubert         ++i;
8355796c8dcSSimon Schubert       }
8365796c8dcSSimon Schubert 
837*a45ae5f8SJohn Marino   /* This call takes ownership of NBFD.  It does not take ownership of SAI.  */
838*a45ae5f8SJohn Marino   objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
8395796c8dcSSimon Schubert 
840*a45ae5f8SJohn Marino   do_cleanups (old_cleanups);
841*a45ae5f8SJohn Marino   add_objfile_entry (objfile, entry_addr);
842*a45ae5f8SJohn Marino }
8435796c8dcSSimon Schubert 
844*a45ae5f8SJohn Marino /* This function registers code associated with a JIT code entry.  It uses the
845*a45ae5f8SJohn Marino    pointer and size pair in the entry to read the symbol file from the remote
846*a45ae5f8SJohn Marino    and then calls symbol_file_add_from_local_memory to add it as though it were
847*a45ae5f8SJohn Marino    a symbol file added by the user.  */
848*a45ae5f8SJohn Marino 
849*a45ae5f8SJohn Marino static void
850*a45ae5f8SJohn Marino jit_register_code (struct gdbarch *gdbarch,
851*a45ae5f8SJohn Marino                    CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
852*a45ae5f8SJohn Marino {
853*a45ae5f8SJohn Marino   int i, success;
854*a45ae5f8SJohn Marino   const struct bfd_arch_info *b;
855*a45ae5f8SJohn Marino   struct jit_inferior_data *inf_data = get_jit_inferior_data ();
856*a45ae5f8SJohn Marino 
857*a45ae5f8SJohn Marino   if (jit_debug)
858*a45ae5f8SJohn Marino     fprintf_unfiltered (gdb_stdlog,
859*a45ae5f8SJohn Marino                         "jit_register_code, symfile_addr = %s, "
860*a45ae5f8SJohn Marino                         "symfile_size = %s\n",
861*a45ae5f8SJohn Marino                         paddress (gdbarch, code_entry->symfile_addr),
862*a45ae5f8SJohn Marino                         pulongest (code_entry->symfile_size));
863*a45ae5f8SJohn Marino 
864*a45ae5f8SJohn Marino   success = jit_reader_try_read_symtab (code_entry, entry_addr);
865*a45ae5f8SJohn Marino 
866*a45ae5f8SJohn Marino   if (!success)
867*a45ae5f8SJohn Marino     jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch);
8685796c8dcSSimon Schubert }
8695796c8dcSSimon Schubert 
870c50c785cSJohn Marino /* This function unregisters JITed code and frees the corresponding
871c50c785cSJohn Marino    objfile.  */
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert static void
8745796c8dcSSimon Schubert jit_unregister_code (struct objfile *objfile)
8755796c8dcSSimon Schubert {
8765796c8dcSSimon Schubert   free_objfile (objfile);
8775796c8dcSSimon Schubert }
8785796c8dcSSimon Schubert 
8795796c8dcSSimon Schubert /* Look up the objfile with this code entry address.  */
8805796c8dcSSimon Schubert 
8815796c8dcSSimon Schubert static struct objfile *
8825796c8dcSSimon Schubert jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
8835796c8dcSSimon Schubert {
8845796c8dcSSimon Schubert   struct objfile *objf;
8855796c8dcSSimon Schubert   CORE_ADDR *objf_entry_addr;
8865796c8dcSSimon Schubert 
8875796c8dcSSimon Schubert   ALL_OBJFILES (objf)
8885796c8dcSSimon Schubert     {
8895796c8dcSSimon Schubert       objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data);
8905796c8dcSSimon Schubert       if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr)
8915796c8dcSSimon Schubert         return objf;
8925796c8dcSSimon Schubert     }
8935796c8dcSSimon Schubert   return NULL;
8945796c8dcSSimon Schubert }
8955796c8dcSSimon Schubert 
896c50c785cSJohn Marino /* (Re-)Initialize the jit breakpoint if necessary.
897c50c785cSJohn Marino    Return 0 on success.  */
898c50c785cSJohn Marino 
899c50c785cSJohn Marino static int
900c50c785cSJohn Marino jit_breakpoint_re_set_internal (struct gdbarch *gdbarch,
901c50c785cSJohn Marino 				struct jit_inferior_data *inf_data)
902c50c785cSJohn Marino {
903c50c785cSJohn Marino   if (inf_data->breakpoint_addr == 0)
904c50c785cSJohn Marino     {
905c50c785cSJohn Marino       struct minimal_symbol *reg_symbol;
906c50c785cSJohn Marino 
907c50c785cSJohn Marino       /* Lookup the registration symbol.  If it is missing, then we assume
908c50c785cSJohn Marino 	 we are not attached to a JIT.  */
909c50c785cSJohn Marino       reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL);
910c50c785cSJohn Marino       if (reg_symbol == NULL)
911c50c785cSJohn Marino 	return 1;
912c50c785cSJohn Marino       inf_data->breakpoint_addr = SYMBOL_VALUE_ADDRESS (reg_symbol);
913c50c785cSJohn Marino       if (inf_data->breakpoint_addr == 0)
914c50c785cSJohn Marino 	return 2;
915c50c785cSJohn Marino 
916c50c785cSJohn Marino       /* If we have not read the jit descriptor yet (e.g. because the JITer
917c50c785cSJohn Marino 	 itself is in a shared library which just got loaded), do so now.  */
918c50c785cSJohn Marino       if (inf_data->descriptor_addr == 0)
919c50c785cSJohn Marino 	jit_inferior_init (gdbarch);
920c50c785cSJohn Marino     }
921c50c785cSJohn Marino   else
922c50c785cSJohn Marino     return 0;
923c50c785cSJohn Marino 
924c50c785cSJohn Marino   if (jit_debug)
925c50c785cSJohn Marino     fprintf_unfiltered (gdb_stdlog,
926c50c785cSJohn Marino 			"jit_breakpoint_re_set_internal, "
927c50c785cSJohn Marino 			"breakpoint_addr = %s\n",
928c50c785cSJohn Marino 			paddress (gdbarch, inf_data->breakpoint_addr));
929c50c785cSJohn Marino 
930c50c785cSJohn Marino   /* Put a breakpoint in the registration symbol.  */
931c50c785cSJohn Marino   create_jit_event_breakpoint (gdbarch, inf_data->breakpoint_addr);
932c50c785cSJohn Marino 
933c50c785cSJohn Marino   return 0;
934c50c785cSJohn Marino }
935c50c785cSJohn Marino 
936*a45ae5f8SJohn Marino /* The private data passed around in the frame unwind callback
937*a45ae5f8SJohn Marino    functions.  */
938*a45ae5f8SJohn Marino 
939*a45ae5f8SJohn Marino struct jit_unwind_private
940*a45ae5f8SJohn Marino {
941*a45ae5f8SJohn Marino   /* Cached register values.  See jit_frame_sniffer to see how this
942*a45ae5f8SJohn Marino      works.  */
943*a45ae5f8SJohn Marino   struct gdb_reg_value **registers;
944*a45ae5f8SJohn Marino 
945*a45ae5f8SJohn Marino   /* The frame being unwound.  */
946*a45ae5f8SJohn Marino   struct frame_info *this_frame;
947*a45ae5f8SJohn Marino };
948*a45ae5f8SJohn Marino 
949*a45ae5f8SJohn Marino /* Sets the value of a particular register in this frame.  */
950*a45ae5f8SJohn Marino 
951*a45ae5f8SJohn Marino static void
952*a45ae5f8SJohn Marino jit_unwind_reg_set_impl (struct gdb_unwind_callbacks *cb, int dwarf_regnum,
953*a45ae5f8SJohn Marino                          struct gdb_reg_value *value)
954*a45ae5f8SJohn Marino {
955*a45ae5f8SJohn Marino   struct jit_unwind_private *priv;
956*a45ae5f8SJohn Marino   int gdb_reg;
957*a45ae5f8SJohn Marino 
958*a45ae5f8SJohn Marino   priv = cb->priv_data;
959*a45ae5f8SJohn Marino 
960*a45ae5f8SJohn Marino   gdb_reg = gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv->this_frame),
961*a45ae5f8SJohn Marino                                           dwarf_regnum);
962*a45ae5f8SJohn Marino   if (gdb_reg == -1)
963*a45ae5f8SJohn Marino     {
964*a45ae5f8SJohn Marino       if (jit_debug)
965*a45ae5f8SJohn Marino         fprintf_unfiltered (gdb_stdlog,
966*a45ae5f8SJohn Marino                             _("Could not recognize DWARF regnum %d"),
967*a45ae5f8SJohn Marino                             dwarf_regnum);
968*a45ae5f8SJohn Marino       return;
969*a45ae5f8SJohn Marino     }
970*a45ae5f8SJohn Marino 
971*a45ae5f8SJohn Marino   gdb_assert (priv->registers);
972*a45ae5f8SJohn Marino   priv->registers[gdb_reg] = value;
973*a45ae5f8SJohn Marino }
974*a45ae5f8SJohn Marino 
975*a45ae5f8SJohn Marino static void
976*a45ae5f8SJohn Marino reg_value_free_impl (struct gdb_reg_value *value)
977*a45ae5f8SJohn Marino {
978*a45ae5f8SJohn Marino   xfree (value);
979*a45ae5f8SJohn Marino }
980*a45ae5f8SJohn Marino 
981*a45ae5f8SJohn Marino /* Get the value of register REGNUM in the previous frame.  */
982*a45ae5f8SJohn Marino 
983*a45ae5f8SJohn Marino static struct gdb_reg_value *
984*a45ae5f8SJohn Marino jit_unwind_reg_get_impl (struct gdb_unwind_callbacks *cb, int regnum)
985*a45ae5f8SJohn Marino {
986*a45ae5f8SJohn Marino   struct jit_unwind_private *priv;
987*a45ae5f8SJohn Marino   struct gdb_reg_value *value;
988*a45ae5f8SJohn Marino   int gdb_reg, size;
989*a45ae5f8SJohn Marino   struct gdbarch *frame_arch;
990*a45ae5f8SJohn Marino 
991*a45ae5f8SJohn Marino   priv = cb->priv_data;
992*a45ae5f8SJohn Marino   frame_arch = get_frame_arch (priv->this_frame);
993*a45ae5f8SJohn Marino 
994*a45ae5f8SJohn Marino   gdb_reg = gdbarch_dwarf2_reg_to_regnum (frame_arch, regnum);
995*a45ae5f8SJohn Marino   size = register_size (frame_arch, gdb_reg);
996*a45ae5f8SJohn Marino   value = xmalloc (sizeof (struct gdb_reg_value) + size - 1);
997*a45ae5f8SJohn Marino   value->defined = frame_register_read (priv->this_frame, gdb_reg,
998*a45ae5f8SJohn Marino                                         value->value);
999*a45ae5f8SJohn Marino   value->size = size;
1000*a45ae5f8SJohn Marino   value->free = reg_value_free_impl;
1001*a45ae5f8SJohn Marino   return value;
1002*a45ae5f8SJohn Marino }
1003*a45ae5f8SJohn Marino 
1004*a45ae5f8SJohn Marino /* gdb_reg_value has a free function, which must be called on each
1005*a45ae5f8SJohn Marino    saved register value.  */
1006*a45ae5f8SJohn Marino 
1007*a45ae5f8SJohn Marino static void
1008*a45ae5f8SJohn Marino jit_dealloc_cache (struct frame_info *this_frame, void *cache)
1009*a45ae5f8SJohn Marino {
1010*a45ae5f8SJohn Marino   struct jit_unwind_private *priv_data = cache;
1011*a45ae5f8SJohn Marino   struct gdbarch *frame_arch;
1012*a45ae5f8SJohn Marino   int i;
1013*a45ae5f8SJohn Marino 
1014*a45ae5f8SJohn Marino   gdb_assert (priv_data->registers);
1015*a45ae5f8SJohn Marino   frame_arch = get_frame_arch (priv_data->this_frame);
1016*a45ae5f8SJohn Marino 
1017*a45ae5f8SJohn Marino   for (i = 0; i < gdbarch_num_regs (frame_arch); i++)
1018*a45ae5f8SJohn Marino     if (priv_data->registers[i] && priv_data->registers[i]->free)
1019*a45ae5f8SJohn Marino       priv_data->registers[i]->free (priv_data->registers[i]);
1020*a45ae5f8SJohn Marino 
1021*a45ae5f8SJohn Marino   xfree (priv_data->registers);
1022*a45ae5f8SJohn Marino   xfree (priv_data);
1023*a45ae5f8SJohn Marino }
1024*a45ae5f8SJohn Marino 
1025*a45ae5f8SJohn Marino /* The frame sniffer for the pseudo unwinder.
1026*a45ae5f8SJohn Marino 
1027*a45ae5f8SJohn Marino    While this is nominally a frame sniffer, in the case where the JIT
1028*a45ae5f8SJohn Marino    reader actually recognizes the frame, it does a lot more work -- it
1029*a45ae5f8SJohn Marino    unwinds the frame and saves the corresponding register values in
1030*a45ae5f8SJohn Marino    the cache.  jit_frame_prev_register simply returns the saved
1031*a45ae5f8SJohn Marino    register values.  */
1032*a45ae5f8SJohn Marino 
1033*a45ae5f8SJohn Marino static int
1034*a45ae5f8SJohn Marino jit_frame_sniffer (const struct frame_unwind *self,
1035*a45ae5f8SJohn Marino                    struct frame_info *this_frame, void **cache)
1036*a45ae5f8SJohn Marino {
1037*a45ae5f8SJohn Marino   struct jit_inferior_data *inf_data;
1038*a45ae5f8SJohn Marino   struct jit_unwind_private *priv_data;
1039*a45ae5f8SJohn Marino   struct jit_dbg_reader *iter;
1040*a45ae5f8SJohn Marino   struct gdb_unwind_callbacks callbacks;
1041*a45ae5f8SJohn Marino   struct gdb_reader_funcs *funcs;
1042*a45ae5f8SJohn Marino 
1043*a45ae5f8SJohn Marino   inf_data = get_jit_inferior_data ();
1044*a45ae5f8SJohn Marino 
1045*a45ae5f8SJohn Marino   callbacks.reg_get = jit_unwind_reg_get_impl;
1046*a45ae5f8SJohn Marino   callbacks.reg_set = jit_unwind_reg_set_impl;
1047*a45ae5f8SJohn Marino   callbacks.target_read = jit_target_read_impl;
1048*a45ae5f8SJohn Marino 
1049*a45ae5f8SJohn Marino   if (loaded_jit_reader == NULL)
1050*a45ae5f8SJohn Marino     return 0;
1051*a45ae5f8SJohn Marino 
1052*a45ae5f8SJohn Marino   funcs = loaded_jit_reader->functions;
1053*a45ae5f8SJohn Marino 
1054*a45ae5f8SJohn Marino   gdb_assert (!*cache);
1055*a45ae5f8SJohn Marino 
1056*a45ae5f8SJohn Marino   *cache = XZALLOC (struct jit_unwind_private);
1057*a45ae5f8SJohn Marino   priv_data = *cache;
1058*a45ae5f8SJohn Marino   priv_data->registers =
1059*a45ae5f8SJohn Marino     XCALLOC (gdbarch_num_regs (get_frame_arch (this_frame)),
1060*a45ae5f8SJohn Marino              struct gdb_reg_value *);
1061*a45ae5f8SJohn Marino   priv_data->this_frame = this_frame;
1062*a45ae5f8SJohn Marino 
1063*a45ae5f8SJohn Marino   callbacks.priv_data = priv_data;
1064*a45ae5f8SJohn Marino 
1065*a45ae5f8SJohn Marino   /* Try to coax the provided unwinder to unwind the stack */
1066*a45ae5f8SJohn Marino   if (funcs->unwind (funcs, &callbacks) == GDB_SUCCESS)
1067*a45ae5f8SJohn Marino     {
1068*a45ae5f8SJohn Marino       if (jit_debug)
1069*a45ae5f8SJohn Marino         fprintf_unfiltered (gdb_stdlog, _("Successfully unwound frame using "
1070*a45ae5f8SJohn Marino                                           "JIT reader.\n"));
1071*a45ae5f8SJohn Marino       return 1;
1072*a45ae5f8SJohn Marino     }
1073*a45ae5f8SJohn Marino   if (jit_debug)
1074*a45ae5f8SJohn Marino     fprintf_unfiltered (gdb_stdlog, _("Could not unwind frame using "
1075*a45ae5f8SJohn Marino                                       "JIT reader.\n"));
1076*a45ae5f8SJohn Marino 
1077*a45ae5f8SJohn Marino   jit_dealloc_cache (this_frame, *cache);
1078*a45ae5f8SJohn Marino   *cache = NULL;
1079*a45ae5f8SJohn Marino 
1080*a45ae5f8SJohn Marino   return 0;
1081*a45ae5f8SJohn Marino }
1082*a45ae5f8SJohn Marino 
1083*a45ae5f8SJohn Marino 
1084*a45ae5f8SJohn Marino /* The frame_id function for the pseudo unwinder.  Relays the call to
1085*a45ae5f8SJohn Marino    the loaded plugin.  */
1086*a45ae5f8SJohn Marino 
1087*a45ae5f8SJohn Marino static void
1088*a45ae5f8SJohn Marino jit_frame_this_id (struct frame_info *this_frame, void **cache,
1089*a45ae5f8SJohn Marino                    struct frame_id *this_id)
1090*a45ae5f8SJohn Marino {
1091*a45ae5f8SJohn Marino   struct jit_unwind_private private;
1092*a45ae5f8SJohn Marino   struct gdb_frame_id frame_id;
1093*a45ae5f8SJohn Marino   struct gdb_reader_funcs *funcs;
1094*a45ae5f8SJohn Marino   struct gdb_unwind_callbacks callbacks;
1095*a45ae5f8SJohn Marino 
1096*a45ae5f8SJohn Marino   private.registers = NULL;
1097*a45ae5f8SJohn Marino   private.this_frame = this_frame;
1098*a45ae5f8SJohn Marino 
1099*a45ae5f8SJohn Marino   /* We don't expect the frame_id function to set any registers, so we
1100*a45ae5f8SJohn Marino      set reg_set to NULL.  */
1101*a45ae5f8SJohn Marino   callbacks.reg_get = jit_unwind_reg_get_impl;
1102*a45ae5f8SJohn Marino   callbacks.reg_set = NULL;
1103*a45ae5f8SJohn Marino   callbacks.target_read = jit_target_read_impl;
1104*a45ae5f8SJohn Marino   callbacks.priv_data = &private;
1105*a45ae5f8SJohn Marino 
1106*a45ae5f8SJohn Marino   gdb_assert (loaded_jit_reader);
1107*a45ae5f8SJohn Marino   funcs = loaded_jit_reader->functions;
1108*a45ae5f8SJohn Marino 
1109*a45ae5f8SJohn Marino   frame_id = funcs->get_frame_id (funcs, &callbacks);
1110*a45ae5f8SJohn Marino   *this_id = frame_id_build (frame_id.stack_address, frame_id.code_address);
1111*a45ae5f8SJohn Marino }
1112*a45ae5f8SJohn Marino 
1113*a45ae5f8SJohn Marino /* Pseudo unwinder function.  Reads the previously fetched value for
1114*a45ae5f8SJohn Marino    the register from the cache.  */
1115*a45ae5f8SJohn Marino 
1116*a45ae5f8SJohn Marino static struct value *
1117*a45ae5f8SJohn Marino jit_frame_prev_register (struct frame_info *this_frame, void **cache, int reg)
1118*a45ae5f8SJohn Marino {
1119*a45ae5f8SJohn Marino   struct jit_unwind_private *priv = *cache;
1120*a45ae5f8SJohn Marino   struct gdb_reg_value *value;
1121*a45ae5f8SJohn Marino 
1122*a45ae5f8SJohn Marino   if (priv == NULL)
1123*a45ae5f8SJohn Marino     return frame_unwind_got_optimized (this_frame, reg);
1124*a45ae5f8SJohn Marino 
1125*a45ae5f8SJohn Marino   gdb_assert (priv->registers);
1126*a45ae5f8SJohn Marino   value = priv->registers[reg];
1127*a45ae5f8SJohn Marino   if (value && value->defined)
1128*a45ae5f8SJohn Marino     return frame_unwind_got_bytes (this_frame, reg, value->value);
1129*a45ae5f8SJohn Marino   else
1130*a45ae5f8SJohn Marino     return frame_unwind_got_optimized (this_frame, reg);
1131*a45ae5f8SJohn Marino }
1132*a45ae5f8SJohn Marino 
1133*a45ae5f8SJohn Marino /* Relay everything back to the unwinder registered by the JIT debug
1134*a45ae5f8SJohn Marino    info reader.*/
1135*a45ae5f8SJohn Marino 
1136*a45ae5f8SJohn Marino static const struct frame_unwind jit_frame_unwind =
1137*a45ae5f8SJohn Marino {
1138*a45ae5f8SJohn Marino   NORMAL_FRAME,
1139*a45ae5f8SJohn Marino   default_frame_unwind_stop_reason,
1140*a45ae5f8SJohn Marino   jit_frame_this_id,
1141*a45ae5f8SJohn Marino   jit_frame_prev_register,
1142*a45ae5f8SJohn Marino   NULL,
1143*a45ae5f8SJohn Marino   jit_frame_sniffer,
1144*a45ae5f8SJohn Marino   jit_dealloc_cache
1145*a45ae5f8SJohn Marino };
1146*a45ae5f8SJohn Marino 
1147*a45ae5f8SJohn Marino 
1148*a45ae5f8SJohn Marino /* This is the information that is stored at jit_gdbarch_data for each
1149*a45ae5f8SJohn Marino    architecture.  */
1150*a45ae5f8SJohn Marino 
1151*a45ae5f8SJohn Marino struct jit_gdbarch_data_type
1152*a45ae5f8SJohn Marino {
1153*a45ae5f8SJohn Marino   /* Has the (pseudo) unwinder been prepended? */
1154*a45ae5f8SJohn Marino   int unwinder_registered;
1155*a45ae5f8SJohn Marino };
1156*a45ae5f8SJohn Marino 
1157*a45ae5f8SJohn Marino /* Check GDBARCH and prepend the pseudo JIT unwinder if needed.  */
1158*a45ae5f8SJohn Marino 
1159*a45ae5f8SJohn Marino static void
1160*a45ae5f8SJohn Marino jit_prepend_unwinder (struct gdbarch *gdbarch)
1161*a45ae5f8SJohn Marino {
1162*a45ae5f8SJohn Marino   struct jit_gdbarch_data_type *data;
1163*a45ae5f8SJohn Marino 
1164*a45ae5f8SJohn Marino   data = gdbarch_data (gdbarch, jit_gdbarch_data);
1165*a45ae5f8SJohn Marino   if (!data->unwinder_registered)
1166*a45ae5f8SJohn Marino     {
1167*a45ae5f8SJohn Marino       frame_unwind_prepend_unwinder (gdbarch, &jit_frame_unwind);
1168*a45ae5f8SJohn Marino       data->unwinder_registered = 1;
1169*a45ae5f8SJohn Marino     }
1170*a45ae5f8SJohn Marino }
1171*a45ae5f8SJohn Marino 
1172c50c785cSJohn Marino /* Register any already created translations.  */
11735796c8dcSSimon Schubert 
11745796c8dcSSimon Schubert static void
11755796c8dcSSimon Schubert jit_inferior_init (struct gdbarch *gdbarch)
11765796c8dcSSimon Schubert {
11775796c8dcSSimon Schubert   struct jit_descriptor descriptor;
11785796c8dcSSimon Schubert   struct jit_code_entry cur_entry;
1179c50c785cSJohn Marino   struct jit_inferior_data *inf_data;
11805796c8dcSSimon Schubert   CORE_ADDR cur_entry_addr;
11815796c8dcSSimon Schubert 
1182c50c785cSJohn Marino   if (jit_debug)
1183c50c785cSJohn Marino     fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n");
1184c50c785cSJohn Marino 
1185*a45ae5f8SJohn Marino   jit_prepend_unwinder (gdbarch);
1186*a45ae5f8SJohn Marino 
1187c50c785cSJohn Marino   inf_data = get_jit_inferior_data ();
1188c50c785cSJohn Marino   if (jit_breakpoint_re_set_internal (gdbarch, inf_data) != 0)
11895796c8dcSSimon Schubert     return;
11905796c8dcSSimon Schubert 
1191c50c785cSJohn Marino   if (inf_data->descriptor_addr == 0)
1192c50c785cSJohn Marino     {
1193c50c785cSJohn Marino       struct minimal_symbol *desc_symbol;
11945796c8dcSSimon Schubert 
1195c50c785cSJohn Marino       /* Lookup the descriptor symbol and cache the addr.  If it is
1196c50c785cSJohn Marino 	 missing, we assume we are not attached to a JIT and return early.  */
11975796c8dcSSimon Schubert       desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL);
11985796c8dcSSimon Schubert       if (desc_symbol == NULL)
11995796c8dcSSimon Schubert 	return;
12005796c8dcSSimon Schubert 
1201c50c785cSJohn Marino       inf_data->descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol);
1202c50c785cSJohn Marino       if (inf_data->descriptor_addr == 0)
1203c50c785cSJohn Marino 	return;
1204c50c785cSJohn Marino     }
1205c50c785cSJohn Marino 
1206c50c785cSJohn Marino   if (jit_debug)
1207c50c785cSJohn Marino     fprintf_unfiltered (gdb_stdlog,
1208c50c785cSJohn Marino 			"jit_inferior_init, descriptor_addr = %s\n",
1209c50c785cSJohn Marino 			paddress (gdbarch, inf_data->descriptor_addr));
1210c50c785cSJohn Marino 
1211c50c785cSJohn Marino   /* Read the descriptor so we can check the version number and load
1212c50c785cSJohn Marino      any already JITed functions.  */
1213c50c785cSJohn Marino   jit_read_descriptor (gdbarch, &descriptor, inf_data->descriptor_addr);
12145796c8dcSSimon Schubert 
12155796c8dcSSimon Schubert   /* Check that the version number agrees with that we support.  */
12165796c8dcSSimon Schubert   if (descriptor.version != 1)
12175796c8dcSSimon Schubert     error (_("Unsupported JIT protocol version in descriptor!"));
12185796c8dcSSimon Schubert 
1219c50c785cSJohn Marino   /* If we've attached to a running program, we need to check the descriptor
1220c50c785cSJohn Marino      to register any functions that were already generated.  */
12215796c8dcSSimon Schubert   for (cur_entry_addr = descriptor.first_entry;
12225796c8dcSSimon Schubert        cur_entry_addr != 0;
12235796c8dcSSimon Schubert        cur_entry_addr = cur_entry.next_entry)
12245796c8dcSSimon Schubert     {
12255796c8dcSSimon Schubert       jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
12265796c8dcSSimon Schubert 
12275796c8dcSSimon Schubert       /* This hook may be called many times during setup, so make sure we don't
12285796c8dcSSimon Schubert          add the same symbol file twice.  */
12295796c8dcSSimon Schubert       if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
12305796c8dcSSimon Schubert         continue;
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert       jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
12335796c8dcSSimon Schubert     }
12345796c8dcSSimon Schubert }
12355796c8dcSSimon Schubert 
12365796c8dcSSimon Schubert /* Exported routine to call when an inferior has been created.  */
12375796c8dcSSimon Schubert 
12385796c8dcSSimon Schubert void
12395796c8dcSSimon Schubert jit_inferior_created_hook (void)
12405796c8dcSSimon Schubert {
12415796c8dcSSimon Schubert   jit_inferior_init (target_gdbarch);
12425796c8dcSSimon Schubert }
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert /* Exported routine to call to re-set the jit breakpoints,
12455796c8dcSSimon Schubert    e.g. when a program is rerun.  */
12465796c8dcSSimon Schubert 
12475796c8dcSSimon Schubert void
12485796c8dcSSimon Schubert jit_breakpoint_re_set (void)
12495796c8dcSSimon Schubert {
1250c50c785cSJohn Marino   jit_breakpoint_re_set_internal (target_gdbarch,
1251c50c785cSJohn Marino 				  get_jit_inferior_data ());
1252c50c785cSJohn Marino }
1253c50c785cSJohn Marino 
1254c50c785cSJohn Marino /* Reset inferior_data, so sybols will be looked up again, and jit_breakpoint
1255c50c785cSJohn Marino    will be reset.  */
1256c50c785cSJohn Marino 
1257c50c785cSJohn Marino static void
1258c50c785cSJohn Marino jit_reset_inferior_data_and_breakpoints (void)
1259c50c785cSJohn Marino {
1260c50c785cSJohn Marino   struct jit_inferior_data *inf_data;
1261c50c785cSJohn Marino 
1262c50c785cSJohn Marino   /* Force jit_inferior_init to re-lookup of jit symbol addresses.  */
1263c50c785cSJohn Marino   inf_data = get_jit_inferior_data ();
1264c50c785cSJohn Marino   inf_data->breakpoint_addr = 0;
1265c50c785cSJohn Marino   inf_data->descriptor_addr = 0;
1266c50c785cSJohn Marino 
1267c50c785cSJohn Marino   /* Remove any existing JIT breakpoint(s).  */
1268c50c785cSJohn Marino   remove_jit_event_breakpoints ();
1269c50c785cSJohn Marino 
12705796c8dcSSimon Schubert   jit_inferior_init (target_gdbarch);
12715796c8dcSSimon Schubert }
12725796c8dcSSimon Schubert 
12735796c8dcSSimon Schubert /* Wrapper to match the observer function pointer prototype.  */
12745796c8dcSSimon Schubert 
12755796c8dcSSimon Schubert static void
12765796c8dcSSimon Schubert jit_inferior_created_observer (struct target_ops *objfile, int from_tty)
12775796c8dcSSimon Schubert {
1278c50c785cSJohn Marino   jit_reset_inferior_data_and_breakpoints ();
12795796c8dcSSimon Schubert }
12805796c8dcSSimon Schubert 
1281c50c785cSJohn Marino /* This function cleans up any code entries left over when the
1282c50c785cSJohn Marino    inferior exits.  We get left over code when the inferior exits
1283c50c785cSJohn Marino    without unregistering its code, for example when it crashes.  */
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert static void
1286cf7f2e2dSJohn Marino jit_inferior_exit_hook (struct inferior *inf)
12875796c8dcSSimon Schubert {
12885796c8dcSSimon Schubert   struct objfile *objf;
12895796c8dcSSimon Schubert   struct objfile *temp;
12905796c8dcSSimon Schubert 
12915796c8dcSSimon Schubert   ALL_OBJFILES_SAFE (objf, temp)
12925796c8dcSSimon Schubert     if (objfile_data (objf, jit_objfile_data) != NULL)
12935796c8dcSSimon Schubert       jit_unregister_code (objf);
12945796c8dcSSimon Schubert }
12955796c8dcSSimon Schubert 
1296c50c785cSJohn Marino static void
1297c50c785cSJohn Marino jit_executable_changed_observer (void)
1298c50c785cSJohn Marino {
1299c50c785cSJohn Marino   jit_reset_inferior_data_and_breakpoints ();
1300c50c785cSJohn Marino }
1301c50c785cSJohn Marino 
13025796c8dcSSimon Schubert void
13035796c8dcSSimon Schubert jit_event_handler (struct gdbarch *gdbarch)
13045796c8dcSSimon Schubert {
13055796c8dcSSimon Schubert   struct jit_descriptor descriptor;
13065796c8dcSSimon Schubert   struct jit_code_entry code_entry;
13075796c8dcSSimon Schubert   CORE_ADDR entry_addr;
13085796c8dcSSimon Schubert   struct objfile *objf;
13095796c8dcSSimon Schubert 
13105796c8dcSSimon Schubert   /* Read the descriptor from remote memory.  */
1311c50c785cSJohn Marino   jit_read_descriptor (gdbarch, &descriptor,
1312c50c785cSJohn Marino 		       get_jit_inferior_data ()->descriptor_addr);
13135796c8dcSSimon Schubert   entry_addr = descriptor.relevant_entry;
13145796c8dcSSimon Schubert 
13155796c8dcSSimon Schubert   /* Do the corresponding action.  */
13165796c8dcSSimon Schubert   switch (descriptor.action_flag)
13175796c8dcSSimon Schubert     {
13185796c8dcSSimon Schubert     case JIT_NOACTION:
13195796c8dcSSimon Schubert       break;
13205796c8dcSSimon Schubert     case JIT_REGISTER:
13215796c8dcSSimon Schubert       jit_read_code_entry (gdbarch, entry_addr, &code_entry);
13225796c8dcSSimon Schubert       jit_register_code (gdbarch, entry_addr, &code_entry);
13235796c8dcSSimon Schubert       break;
13245796c8dcSSimon Schubert     case JIT_UNREGISTER:
13255796c8dcSSimon Schubert       objf = jit_find_objf_with_entry_addr (entry_addr);
13265796c8dcSSimon Schubert       if (objf == NULL)
1327c50c785cSJohn Marino 	printf_unfiltered (_("Unable to find JITed code "
1328c50c785cSJohn Marino 			     "entry at address: %s\n"),
13295796c8dcSSimon Schubert 			   paddress (gdbarch, entry_addr));
13305796c8dcSSimon Schubert       else
13315796c8dcSSimon Schubert         jit_unregister_code (objf);
13325796c8dcSSimon Schubert 
13335796c8dcSSimon Schubert       break;
13345796c8dcSSimon Schubert     default:
13355796c8dcSSimon Schubert       error (_("Unknown action_flag value in JIT descriptor!"));
13365796c8dcSSimon Schubert       break;
13375796c8dcSSimon Schubert     }
13385796c8dcSSimon Schubert }
13395796c8dcSSimon Schubert 
1340*a45ae5f8SJohn Marino /* Called to free the data allocated to the jit_inferior_data slot.  */
1341*a45ae5f8SJohn Marino 
1342*a45ae5f8SJohn Marino static void
1343*a45ae5f8SJohn Marino free_objfile_data (struct objfile *objfile, void *data)
1344*a45ae5f8SJohn Marino {
1345*a45ae5f8SJohn Marino   xfree (data);
1346*a45ae5f8SJohn Marino }
1347*a45ae5f8SJohn Marino 
1348*a45ae5f8SJohn Marino /* Initialize the jit_gdbarch_data slot with an instance of struct
1349*a45ae5f8SJohn Marino    jit_gdbarch_data_type */
1350*a45ae5f8SJohn Marino 
1351*a45ae5f8SJohn Marino static void *
1352*a45ae5f8SJohn Marino jit_gdbarch_data_init (struct obstack *obstack)
1353*a45ae5f8SJohn Marino {
1354*a45ae5f8SJohn Marino   struct jit_gdbarch_data_type *data;
1355*a45ae5f8SJohn Marino 
1356*a45ae5f8SJohn Marino   data = obstack_alloc (obstack, sizeof (struct jit_gdbarch_data_type));
1357*a45ae5f8SJohn Marino   data->unwinder_registered = 0;
1358*a45ae5f8SJohn Marino   return data;
1359*a45ae5f8SJohn Marino }
1360*a45ae5f8SJohn Marino 
13615796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert extern void _initialize_jit (void);
13645796c8dcSSimon Schubert 
13655796c8dcSSimon Schubert void
13665796c8dcSSimon Schubert _initialize_jit (void)
13675796c8dcSSimon Schubert {
1368*a45ae5f8SJohn Marino   jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR,
1369*a45ae5f8SJohn Marino                                            JIT_READER_DIR_RELOCATABLE);
1370c50c785cSJohn Marino   add_setshow_zinteger_cmd ("jit", class_maintenance, &jit_debug,
1371c50c785cSJohn Marino 			    _("Set JIT debugging."),
1372c50c785cSJohn Marino 			    _("Show JIT debugging."),
1373c50c785cSJohn Marino 			    _("When non-zero, JIT debugging is enabled."),
1374c50c785cSJohn Marino 			    NULL,
1375c50c785cSJohn Marino 			    show_jit_debug,
1376c50c785cSJohn Marino 			    &setdebuglist, &showdebuglist);
1377c50c785cSJohn Marino 
13785796c8dcSSimon Schubert   observer_attach_inferior_created (jit_inferior_created_observer);
13795796c8dcSSimon Schubert   observer_attach_inferior_exit (jit_inferior_exit_hook);
1380c50c785cSJohn Marino   observer_attach_executable_changed (jit_executable_changed_observer);
1381*a45ae5f8SJohn Marino   jit_objfile_data =
1382*a45ae5f8SJohn Marino     register_objfile_data_with_cleanup (NULL, free_objfile_data);
1383c50c785cSJohn Marino   jit_inferior_data =
1384c50c785cSJohn Marino     register_inferior_data_with_cleanup (jit_inferior_data_cleanup);
1385*a45ae5f8SJohn Marino   jit_gdbarch_data = gdbarch_data_register_pre_init (jit_gdbarch_data_init);
1386*a45ae5f8SJohn Marino   if (is_dl_available ())
1387*a45ae5f8SJohn Marino     {
1388*a45ae5f8SJohn Marino       add_com ("jit-reader-load", no_class, jit_reader_load_command, _("\
1389*a45ae5f8SJohn Marino Load FILE as debug info reader and unwinder for JIT compiled code.\n\
1390*a45ae5f8SJohn Marino Usage: jit-reader-load FILE\n\
1391*a45ae5f8SJohn Marino Try to load file FILE as a debug info reader (and unwinder) for\n\
1392*a45ae5f8SJohn Marino JIT compiled code.  The file is loaded from " JIT_READER_DIR ",\n\
1393*a45ae5f8SJohn Marino relocated relative to the GDB executable if required."));
1394*a45ae5f8SJohn Marino       add_com ("jit-reader-unload", no_class, jit_reader_unload_command, _("\
1395*a45ae5f8SJohn Marino Unload the currently loaded JIT debug info reader.\n\
1396*a45ae5f8SJohn Marino Usage: jit-reader-unload FILE\n\n\
1397*a45ae5f8SJohn Marino Do \"help jit-reader-load\" for info on loading debug info readers."));
1398*a45ae5f8SJohn Marino     }
13995796c8dcSSimon Schubert }
1400