xref: /dflybsd-src/contrib/gdb-7/gdb/jit.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2009-2013 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"
23a45ae5f8SJohn Marino #include "jit-reader.h"
24a45ae5f8SJohn Marino #include "block.h"
255796c8dcSSimon Schubert #include "breakpoint.h"
26c50c785cSJohn Marino #include "command.h"
27a45ae5f8SJohn Marino #include "dictionary.h"
28*ef5ccd6cSJohn Marino #include "filenames.h"
29a45ae5f8SJohn Marino #include "frame-unwind.h"
30c50c785cSJohn Marino #include "gdbcmd.h"
315796c8dcSSimon Schubert #include "gdbcore.h"
32c50c785cSJohn Marino #include "inferior.h"
335796c8dcSSimon Schubert #include "observer.h"
345796c8dcSSimon Schubert #include "objfiles.h"
35a45ae5f8SJohn Marino #include "regcache.h"
365796c8dcSSimon Schubert #include "symfile.h"
375796c8dcSSimon Schubert #include "symtab.h"
385796c8dcSSimon Schubert #include "target.h"
39a45ae5f8SJohn Marino #include "gdb-dlfcn.h"
405796c8dcSSimon Schubert #include "gdb_stat.h"
41a45ae5f8SJohn Marino #include "exceptions.h"
42*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
43a45ae5f8SJohn Marino 
44a45ae5f8SJohn Marino static const char *jit_reader_dir = NULL;
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert static const struct objfile_data *jit_objfile_data;
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert static const char *const jit_break_name = "__jit_debug_register_code";
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert static const char *const jit_descriptor_name = "__jit_debug_descriptor";
515796c8dcSSimon Schubert 
52*ef5ccd6cSJohn Marino static const struct program_space_data *jit_program_space_data = NULL;
535796c8dcSSimon Schubert 
54a45ae5f8SJohn Marino static void jit_inferior_init (struct gdbarch *gdbarch);
55a45ae5f8SJohn Marino 
56a45ae5f8SJohn Marino /* An unwinder is registered for every gdbarch.  This key is used to
57a45ae5f8SJohn Marino    remember if the unwinder has been registered for a particular
58a45ae5f8SJohn Marino    gdbarch.  */
59a45ae5f8SJohn Marino 
60a45ae5f8SJohn Marino static struct gdbarch_data *jit_gdbarch_data;
61c50c785cSJohn Marino 
62c50c785cSJohn Marino /* Non-zero if we want to see trace of jit level stuff.  */
63c50c785cSJohn Marino 
64*ef5ccd6cSJohn Marino static unsigned int jit_debug = 0;
65c50c785cSJohn Marino 
66c50c785cSJohn Marino static void
show_jit_debug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)67c50c785cSJohn Marino show_jit_debug (struct ui_file *file, int from_tty,
68c50c785cSJohn Marino 		struct cmd_list_element *c, const char *value)
695796c8dcSSimon Schubert {
70c50c785cSJohn Marino   fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
715796c8dcSSimon Schubert }
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert struct target_buffer
745796c8dcSSimon Schubert {
755796c8dcSSimon Schubert   CORE_ADDR base;
76c50c785cSJohn Marino   ULONGEST size;
775796c8dcSSimon Schubert };
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert /* Openning the file is a no-op.  */
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert static void *
mem_bfd_iovec_open(struct bfd * abfd,void * open_closure)825796c8dcSSimon Schubert mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
835796c8dcSSimon Schubert {
845796c8dcSSimon Schubert   return open_closure;
855796c8dcSSimon Schubert }
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert /* Closing the file is just freeing the base/size pair on our side.  */
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert static int
mem_bfd_iovec_close(struct bfd * abfd,void * stream)905796c8dcSSimon Schubert mem_bfd_iovec_close (struct bfd *abfd, void *stream)
915796c8dcSSimon Schubert {
925796c8dcSSimon Schubert   xfree (stream);
93*ef5ccd6cSJohn Marino 
94*ef5ccd6cSJohn Marino   /* Zero means success.  */
95*ef5ccd6cSJohn Marino   return 0;
965796c8dcSSimon Schubert }
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert /* For reading the file, we just need to pass through to target_read_memory and
995796c8dcSSimon Schubert    fix up the arguments and return values.  */
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert static file_ptr
mem_bfd_iovec_pread(struct bfd * abfd,void * stream,void * buf,file_ptr nbytes,file_ptr offset)1025796c8dcSSimon Schubert mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
1035796c8dcSSimon Schubert                      file_ptr nbytes, file_ptr offset)
1045796c8dcSSimon Schubert {
1055796c8dcSSimon Schubert   int err;
1065796c8dcSSimon Schubert   struct target_buffer *buffer = (struct target_buffer *) stream;
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert   /* If this read will read all of the file, limit it to just the rest.  */
1095796c8dcSSimon Schubert   if (offset + nbytes > buffer->size)
1105796c8dcSSimon Schubert     nbytes = buffer->size - offset;
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert   /* If there are no more bytes left, we've reached EOF.  */
1135796c8dcSSimon Schubert   if (nbytes == 0)
1145796c8dcSSimon Schubert     return 0;
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert   err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
1175796c8dcSSimon Schubert   if (err)
1185796c8dcSSimon Schubert     return -1;
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert   return nbytes;
1215796c8dcSSimon Schubert }
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert /* For statting the file, we only support the st_size attribute.  */
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert static int
mem_bfd_iovec_stat(struct bfd * abfd,void * stream,struct stat * sb)1265796c8dcSSimon Schubert mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
1275796c8dcSSimon Schubert {
1285796c8dcSSimon Schubert   struct target_buffer *buffer = (struct target_buffer*) stream;
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   sb->st_size = buffer->size;
1315796c8dcSSimon Schubert   return 0;
1325796c8dcSSimon Schubert }
1335796c8dcSSimon Schubert 
134*ef5ccd6cSJohn Marino /* Open a BFD from the target's memory.  */
135*ef5ccd6cSJohn Marino 
136*ef5ccd6cSJohn Marino static struct bfd *
bfd_open_from_target_memory(CORE_ADDR addr,ULONGEST size,char * target)137*ef5ccd6cSJohn Marino bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
138*ef5ccd6cSJohn Marino {
139*ef5ccd6cSJohn Marino   struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
140*ef5ccd6cSJohn Marino 
141*ef5ccd6cSJohn Marino   buffer->base = addr;
142*ef5ccd6cSJohn Marino   buffer->size = size;
143*ef5ccd6cSJohn Marino   return gdb_bfd_openr_iovec ("<in-memory>", target,
144*ef5ccd6cSJohn Marino 			      mem_bfd_iovec_open,
145*ef5ccd6cSJohn Marino 			      buffer,
146*ef5ccd6cSJohn Marino 			      mem_bfd_iovec_pread,
147*ef5ccd6cSJohn Marino 			      mem_bfd_iovec_close,
148*ef5ccd6cSJohn Marino 			      mem_bfd_iovec_stat);
149*ef5ccd6cSJohn Marino }
150*ef5ccd6cSJohn Marino 
151a45ae5f8SJohn Marino /* One reader that has been loaded successfully, and can potentially be used to
152a45ae5f8SJohn Marino    parse debug info.  */
153a45ae5f8SJohn Marino 
154a45ae5f8SJohn Marino static struct jit_reader
155a45ae5f8SJohn Marino {
156a45ae5f8SJohn Marino   struct gdb_reader_funcs *functions;
157a45ae5f8SJohn Marino   void *handle;
158a45ae5f8SJohn Marino } *loaded_jit_reader = NULL;
159a45ae5f8SJohn Marino 
160a45ae5f8SJohn Marino typedef struct gdb_reader_funcs * (reader_init_fn_type) (void);
161a45ae5f8SJohn Marino static const char *reader_init_fn_sym = "gdb_init_reader";
162a45ae5f8SJohn Marino 
163a45ae5f8SJohn Marino /* Try to load FILE_NAME as a JIT debug info reader.  */
164a45ae5f8SJohn Marino 
165a45ae5f8SJohn Marino static struct jit_reader *
jit_reader_load(const char * file_name)166a45ae5f8SJohn Marino jit_reader_load (const char *file_name)
167a45ae5f8SJohn Marino {
168a45ae5f8SJohn Marino   void *so;
169a45ae5f8SJohn Marino   reader_init_fn_type *init_fn;
170a45ae5f8SJohn Marino   struct jit_reader *new_reader = NULL;
171a45ae5f8SJohn Marino   struct gdb_reader_funcs *funcs = NULL;
172a45ae5f8SJohn Marino   struct cleanup *old_cleanups;
173a45ae5f8SJohn Marino 
174a45ae5f8SJohn Marino   if (jit_debug)
175a45ae5f8SJohn Marino     fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"),
176a45ae5f8SJohn Marino                         file_name);
177a45ae5f8SJohn Marino   so = gdb_dlopen (file_name);
178a45ae5f8SJohn Marino   old_cleanups = make_cleanup_dlclose (so);
179a45ae5f8SJohn Marino 
180a45ae5f8SJohn Marino   init_fn = gdb_dlsym (so, reader_init_fn_sym);
181a45ae5f8SJohn Marino   if (!init_fn)
182a45ae5f8SJohn Marino     error (_("Could not locate initialization function: %s."),
183a45ae5f8SJohn Marino           reader_init_fn_sym);
184a45ae5f8SJohn Marino 
185a45ae5f8SJohn Marino   if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL)
186a45ae5f8SJohn Marino     error (_("Reader not GPL compatible."));
187a45ae5f8SJohn Marino 
188a45ae5f8SJohn Marino   funcs = init_fn ();
189a45ae5f8SJohn Marino   if (funcs->reader_version != GDB_READER_INTERFACE_VERSION)
190a45ae5f8SJohn Marino     error (_("Reader version does not match GDB version."));
191a45ae5f8SJohn Marino 
192a45ae5f8SJohn Marino   new_reader = XZALLOC (struct jit_reader);
193a45ae5f8SJohn Marino   new_reader->functions = funcs;
194a45ae5f8SJohn Marino   new_reader->handle = so;
195a45ae5f8SJohn Marino 
196a45ae5f8SJohn Marino   discard_cleanups (old_cleanups);
197a45ae5f8SJohn Marino   return new_reader;
198a45ae5f8SJohn Marino }
199a45ae5f8SJohn Marino 
200a45ae5f8SJohn Marino /* Provides the jit-reader-load command.  */
201a45ae5f8SJohn Marino 
202a45ae5f8SJohn Marino static void
jit_reader_load_command(char * args,int from_tty)203a45ae5f8SJohn Marino jit_reader_load_command (char *args, int from_tty)
204a45ae5f8SJohn Marino {
205a45ae5f8SJohn Marino   char *so_name;
206a45ae5f8SJohn Marino   struct cleanup *prev_cleanup;
207a45ae5f8SJohn Marino 
208a45ae5f8SJohn Marino   if (args == NULL)
209a45ae5f8SJohn Marino     error (_("No reader name provided."));
210a45ae5f8SJohn Marino 
211a45ae5f8SJohn Marino   if (loaded_jit_reader != NULL)
212a45ae5f8SJohn Marino     error (_("JIT reader already loaded.  Run jit-reader-unload first."));
213a45ae5f8SJohn Marino 
214*ef5ccd6cSJohn Marino   if (IS_ABSOLUTE_PATH (args))
215*ef5ccd6cSJohn Marino     so_name = xstrdup (args);
216*ef5ccd6cSJohn Marino   else
217*ef5ccd6cSJohn Marino     so_name = xstrprintf ("%s%s%s", SLASH_STRING, jit_reader_dir, args);
218a45ae5f8SJohn Marino   prev_cleanup = make_cleanup (xfree, so_name);
219a45ae5f8SJohn Marino 
220a45ae5f8SJohn Marino   loaded_jit_reader = jit_reader_load (so_name);
221a45ae5f8SJohn Marino   do_cleanups (prev_cleanup);
222a45ae5f8SJohn Marino }
223a45ae5f8SJohn Marino 
224a45ae5f8SJohn Marino /* Provides the jit-reader-unload command.  */
225a45ae5f8SJohn Marino 
226a45ae5f8SJohn Marino static void
jit_reader_unload_command(char * args,int from_tty)227a45ae5f8SJohn Marino jit_reader_unload_command (char *args, int from_tty)
228a45ae5f8SJohn Marino {
229a45ae5f8SJohn Marino   if (!loaded_jit_reader)
230a45ae5f8SJohn Marino     error (_("No JIT reader loaded."));
231a45ae5f8SJohn Marino 
232a45ae5f8SJohn Marino   loaded_jit_reader->functions->destroy (loaded_jit_reader->functions);
233a45ae5f8SJohn Marino 
234a45ae5f8SJohn Marino   gdb_dlclose (loaded_jit_reader->handle);
235a45ae5f8SJohn Marino   xfree (loaded_jit_reader);
236a45ae5f8SJohn Marino   loaded_jit_reader = NULL;
237a45ae5f8SJohn Marino }
238a45ae5f8SJohn Marino 
239*ef5ccd6cSJohn Marino /* Per-program space structure recording which objfile has the JIT
240*ef5ccd6cSJohn Marino    symbols.  */
2415796c8dcSSimon Schubert 
242*ef5ccd6cSJohn Marino struct jit_program_space_data
2435796c8dcSSimon Schubert {
244*ef5ccd6cSJohn Marino   /* The objfile.  This is NULL if no objfile holds the JIT
245*ef5ccd6cSJohn Marino      symbols.  */
2465796c8dcSSimon Schubert 
247*ef5ccd6cSJohn Marino   struct objfile *objfile;
248*ef5ccd6cSJohn Marino 
249*ef5ccd6cSJohn Marino   /* If this program space has __jit_debug_register_code, this is the
250*ef5ccd6cSJohn Marino      cached address from the minimal symbol.  This is used to detect
251*ef5ccd6cSJohn Marino      relocations requiring the breakpoint to be re-created.  */
252*ef5ccd6cSJohn Marino 
253*ef5ccd6cSJohn Marino   CORE_ADDR cached_code_address;
254*ef5ccd6cSJohn Marino 
255*ef5ccd6cSJohn Marino   /* This is the JIT event breakpoint, or NULL if it has not been
256*ef5ccd6cSJohn Marino      set.  */
257*ef5ccd6cSJohn Marino 
258*ef5ccd6cSJohn Marino   struct breakpoint *jit_breakpoint;
259*ef5ccd6cSJohn Marino };
260*ef5ccd6cSJohn Marino 
261*ef5ccd6cSJohn Marino /* Per-objfile structure recording the addresses in the program space.
262*ef5ccd6cSJohn Marino    This object serves two purposes: for ordinary objfiles, it may
263*ef5ccd6cSJohn Marino    cache some symbols related to the JIT interface; and for
264*ef5ccd6cSJohn Marino    JIT-created objfiles, it holds some information about the
265*ef5ccd6cSJohn Marino    jit_code_entry.  */
266*ef5ccd6cSJohn Marino 
267*ef5ccd6cSJohn Marino struct jit_objfile_data
268*ef5ccd6cSJohn Marino {
269*ef5ccd6cSJohn Marino   /* Symbol for __jit_debug_register_code.  */
270*ef5ccd6cSJohn Marino   struct minimal_symbol *register_code;
271*ef5ccd6cSJohn Marino 
272*ef5ccd6cSJohn Marino   /* Symbol for __jit_debug_descriptor.  */
273*ef5ccd6cSJohn Marino   struct minimal_symbol *descriptor;
274*ef5ccd6cSJohn Marino 
275*ef5ccd6cSJohn Marino   /* Address of struct jit_code_entry in this objfile.  This is only
276*ef5ccd6cSJohn Marino      non-zero for objfiles that represent code created by the JIT.  */
277*ef5ccd6cSJohn Marino   CORE_ADDR addr;
278*ef5ccd6cSJohn Marino };
279*ef5ccd6cSJohn Marino 
280*ef5ccd6cSJohn Marino /* Fetch the jit_objfile_data associated with OBJF.  If no data exists
281*ef5ccd6cSJohn Marino    yet, make a new structure and attach it.  */
282*ef5ccd6cSJohn Marino 
283*ef5ccd6cSJohn Marino static struct jit_objfile_data *
get_jit_objfile_data(struct objfile * objf)284*ef5ccd6cSJohn Marino get_jit_objfile_data (struct objfile *objf)
285*ef5ccd6cSJohn Marino {
286*ef5ccd6cSJohn Marino   struct jit_objfile_data *objf_data;
287*ef5ccd6cSJohn Marino 
288*ef5ccd6cSJohn Marino   objf_data = objfile_data (objf, jit_objfile_data);
289*ef5ccd6cSJohn Marino   if (objf_data == NULL)
290*ef5ccd6cSJohn Marino     {
291*ef5ccd6cSJohn Marino       objf_data = XZALLOC (struct jit_objfile_data);
292*ef5ccd6cSJohn Marino       set_objfile_data (objf, jit_objfile_data, objf_data);
2935796c8dcSSimon Schubert     }
2945796c8dcSSimon Schubert 
295*ef5ccd6cSJohn Marino   return objf_data;
296*ef5ccd6cSJohn Marino }
297c50c785cSJohn Marino 
298a45ae5f8SJohn Marino /* Remember OBJFILE has been created for struct jit_code_entry located
299a45ae5f8SJohn Marino    at inferior address ENTRY.  */
300a45ae5f8SJohn Marino 
301a45ae5f8SJohn Marino static void
add_objfile_entry(struct objfile * objfile,CORE_ADDR entry)302a45ae5f8SJohn Marino add_objfile_entry (struct objfile *objfile, CORE_ADDR entry)
303a45ae5f8SJohn Marino {
304*ef5ccd6cSJohn Marino   struct jit_objfile_data *objf_data;
305a45ae5f8SJohn Marino 
306*ef5ccd6cSJohn Marino   objf_data = get_jit_objfile_data (objfile);
307*ef5ccd6cSJohn Marino   objf_data->addr = entry;
308a45ae5f8SJohn Marino }
309a45ae5f8SJohn Marino 
310*ef5ccd6cSJohn Marino /* Return jit_program_space_data for current program space.  Allocate
311*ef5ccd6cSJohn Marino    if not already present.  */
312c50c785cSJohn Marino 
313*ef5ccd6cSJohn Marino static struct jit_program_space_data *
get_jit_program_space_data(void)314*ef5ccd6cSJohn Marino get_jit_program_space_data (void)
315c50c785cSJohn Marino {
316*ef5ccd6cSJohn Marino   struct jit_program_space_data *ps_data;
317c50c785cSJohn Marino 
318*ef5ccd6cSJohn Marino   ps_data = program_space_data (current_program_space, jit_program_space_data);
319*ef5ccd6cSJohn Marino   if (ps_data == NULL)
320c50c785cSJohn Marino     {
321*ef5ccd6cSJohn Marino       ps_data = XZALLOC (struct jit_program_space_data);
322*ef5ccd6cSJohn Marino       set_program_space_data (current_program_space, jit_program_space_data,
323*ef5ccd6cSJohn Marino 			      ps_data);
324c50c785cSJohn Marino     }
325c50c785cSJohn Marino 
326*ef5ccd6cSJohn Marino   return ps_data;
327c50c785cSJohn Marino }
328c50c785cSJohn Marino 
329c50c785cSJohn Marino static void
jit_program_space_data_cleanup(struct program_space * ps,void * arg)330*ef5ccd6cSJohn Marino jit_program_space_data_cleanup (struct program_space *ps, void *arg)
331c50c785cSJohn Marino {
332c50c785cSJohn Marino   xfree (arg);
333c50c785cSJohn Marino }
334c50c785cSJohn Marino 
335c50c785cSJohn Marino /* Helper function for reading the global JIT descriptor from remote
336*ef5ccd6cSJohn Marino    memory.  Returns 1 if all went well, 0 otherwise.  */
3375796c8dcSSimon Schubert 
338*ef5ccd6cSJohn Marino static int
jit_read_descriptor(struct gdbarch * gdbarch,struct jit_descriptor * descriptor,struct jit_program_space_data * ps_data)3395796c8dcSSimon Schubert jit_read_descriptor (struct gdbarch *gdbarch,
340c50c785cSJohn Marino 		     struct jit_descriptor *descriptor,
341*ef5ccd6cSJohn Marino 		     struct jit_program_space_data *ps_data)
3425796c8dcSSimon Schubert {
3435796c8dcSSimon Schubert   int err;
3445796c8dcSSimon Schubert   struct type *ptr_type;
3455796c8dcSSimon Schubert   int ptr_size;
3465796c8dcSSimon Schubert   int desc_size;
3475796c8dcSSimon Schubert   gdb_byte *desc_buf;
3485796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
349*ef5ccd6cSJohn Marino   struct jit_objfile_data *objf_data;
350*ef5ccd6cSJohn Marino 
351*ef5ccd6cSJohn Marino   if (ps_data->objfile == NULL)
352*ef5ccd6cSJohn Marino     return 0;
353*ef5ccd6cSJohn Marino   objf_data = get_jit_objfile_data (ps_data->objfile);
354*ef5ccd6cSJohn Marino   if (objf_data->descriptor == NULL)
355*ef5ccd6cSJohn Marino     return 0;
356*ef5ccd6cSJohn Marino 
357*ef5ccd6cSJohn Marino   if (jit_debug)
358*ef5ccd6cSJohn Marino     fprintf_unfiltered (gdb_stdlog,
359*ef5ccd6cSJohn Marino 			"jit_read_descriptor, descriptor_addr = %s\n",
360*ef5ccd6cSJohn Marino 			paddress (gdbarch, SYMBOL_VALUE_ADDRESS (objf_data->descriptor)));
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert   /* Figure out how big the descriptor is on the remote and how to read it.  */
3635796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
3645796c8dcSSimon Schubert   ptr_size = TYPE_LENGTH (ptr_type);
3655796c8dcSSimon Schubert   desc_size = 8 + 2 * ptr_size;  /* Two 32-bit ints and two pointers.  */
3665796c8dcSSimon Schubert   desc_buf = alloca (desc_size);
3675796c8dcSSimon Schubert 
3685796c8dcSSimon Schubert   /* Read the descriptor.  */
369*ef5ccd6cSJohn Marino   err = target_read_memory (SYMBOL_VALUE_ADDRESS (objf_data->descriptor),
370*ef5ccd6cSJohn Marino 			    desc_buf, desc_size);
3715796c8dcSSimon Schubert   if (err)
372*ef5ccd6cSJohn Marino     {
373*ef5ccd6cSJohn Marino       printf_unfiltered (_("Unable to read JIT descriptor from "
374*ef5ccd6cSJohn Marino 			   "remote memory\n"));
375*ef5ccd6cSJohn Marino       return 0;
376*ef5ccd6cSJohn Marino     }
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert   /* Fix the endianness to match the host.  */
3795796c8dcSSimon Schubert   descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
3805796c8dcSSimon Schubert   descriptor->action_flag =
3815796c8dcSSimon Schubert       extract_unsigned_integer (&desc_buf[4], 4, byte_order);
3825796c8dcSSimon Schubert   descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
3835796c8dcSSimon Schubert   descriptor->first_entry =
3845796c8dcSSimon Schubert       extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
385*ef5ccd6cSJohn Marino 
386*ef5ccd6cSJohn Marino   return 1;
3875796c8dcSSimon Schubert }
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert /* Helper function for reading a JITed code entry from remote memory.  */
3905796c8dcSSimon Schubert 
3915796c8dcSSimon Schubert static void
jit_read_code_entry(struct gdbarch * gdbarch,CORE_ADDR code_addr,struct jit_code_entry * code_entry)3925796c8dcSSimon Schubert jit_read_code_entry (struct gdbarch *gdbarch,
3935796c8dcSSimon Schubert 		     CORE_ADDR code_addr, struct jit_code_entry *code_entry)
3945796c8dcSSimon Schubert {
395a45ae5f8SJohn Marino   int err, off;
3965796c8dcSSimon Schubert   struct type *ptr_type;
3975796c8dcSSimon Schubert   int ptr_size;
3985796c8dcSSimon Schubert   int entry_size;
399a45ae5f8SJohn Marino   int align_bytes;
4005796c8dcSSimon Schubert   gdb_byte *entry_buf;
4015796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4025796c8dcSSimon Schubert 
4035796c8dcSSimon Schubert   /* Figure out how big the entry is on the remote and how to read it.  */
4045796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4055796c8dcSSimon Schubert   ptr_size = TYPE_LENGTH (ptr_type);
406*ef5ccd6cSJohn Marino 
407*ef5ccd6cSJohn Marino   /* Figure out where the longlong value will be.  */
408*ef5ccd6cSJohn Marino   align_bytes = gdbarch_long_long_align_bit (gdbarch) / 8;
409*ef5ccd6cSJohn Marino   off = 3 * ptr_size;
410*ef5ccd6cSJohn Marino   off = (off + (align_bytes - 1)) & ~(align_bytes - 1);
411*ef5ccd6cSJohn Marino 
412*ef5ccd6cSJohn Marino   entry_size = off + 8;  /* Three pointers and one 64-bit int.  */
4135796c8dcSSimon Schubert   entry_buf = alloca (entry_size);
4145796c8dcSSimon Schubert 
4155796c8dcSSimon Schubert   /* Read the entry.  */
4165796c8dcSSimon Schubert   err = target_read_memory (code_addr, entry_buf, entry_size);
4175796c8dcSSimon Schubert   if (err)
4185796c8dcSSimon Schubert     error (_("Unable to read JIT code entry from remote memory!"));
4195796c8dcSSimon Schubert 
4205796c8dcSSimon Schubert   /* Fix the endianness to match the host.  */
4215796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4225796c8dcSSimon Schubert   code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
4235796c8dcSSimon Schubert   code_entry->prev_entry =
4245796c8dcSSimon Schubert       extract_typed_address (&entry_buf[ptr_size], ptr_type);
4255796c8dcSSimon Schubert   code_entry->symfile_addr =
4265796c8dcSSimon Schubert       extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
4275796c8dcSSimon Schubert   code_entry->symfile_size =
428a45ae5f8SJohn Marino       extract_unsigned_integer (&entry_buf[off], 8, byte_order);
4295796c8dcSSimon Schubert }
4305796c8dcSSimon Schubert 
431a45ae5f8SJohn Marino /* Proxy object for building a block.  */
432a45ae5f8SJohn Marino 
433a45ae5f8SJohn Marino struct gdb_block
434a45ae5f8SJohn Marino {
435a45ae5f8SJohn Marino   /* gdb_blocks are linked into a tree structure.  Next points to the
436a45ae5f8SJohn Marino      next node at the same depth as this block and parent to the
437a45ae5f8SJohn Marino      parent gdb_block.  */
438a45ae5f8SJohn Marino   struct gdb_block *next, *parent;
439a45ae5f8SJohn Marino 
440a45ae5f8SJohn Marino   /* Points to the "real" block that is being built out of this
441a45ae5f8SJohn Marino      instance.  This block will be added to a blockvector, which will
442a45ae5f8SJohn Marino      then be added to a symtab.  */
443a45ae5f8SJohn Marino   struct block *real_block;
444a45ae5f8SJohn Marino 
445a45ae5f8SJohn Marino   /* The first and last code address corresponding to this block.  */
446a45ae5f8SJohn Marino   CORE_ADDR begin, end;
447a45ae5f8SJohn Marino 
448a45ae5f8SJohn Marino   /* The name of this block (if any).  If this is non-NULL, the
449a45ae5f8SJohn Marino      FUNCTION symbol symbol is set to this value.  */
450a45ae5f8SJohn Marino   const char *name;
451a45ae5f8SJohn Marino };
452a45ae5f8SJohn Marino 
453a45ae5f8SJohn Marino /* Proxy object for building a symtab.  */
454a45ae5f8SJohn Marino 
455a45ae5f8SJohn Marino struct gdb_symtab
456a45ae5f8SJohn Marino {
457a45ae5f8SJohn Marino   /* The list of blocks in this symtab.  These will eventually be
458a45ae5f8SJohn Marino      converted to real blocks.  */
459a45ae5f8SJohn Marino   struct gdb_block *blocks;
460a45ae5f8SJohn Marino 
461a45ae5f8SJohn Marino   /* The number of blocks inserted.  */
462a45ae5f8SJohn Marino   int nblocks;
463a45ae5f8SJohn Marino 
464a45ae5f8SJohn Marino   /* A mapping between line numbers to PC.  */
465a45ae5f8SJohn Marino   struct linetable *linetable;
466a45ae5f8SJohn Marino 
467a45ae5f8SJohn Marino   /* The source file for this symtab.  */
468a45ae5f8SJohn Marino   const char *file_name;
469a45ae5f8SJohn Marino   struct gdb_symtab *next;
470a45ae5f8SJohn Marino };
471a45ae5f8SJohn Marino 
472a45ae5f8SJohn Marino /* Proxy object for building an object.  */
473a45ae5f8SJohn Marino 
474a45ae5f8SJohn Marino struct gdb_object
475a45ae5f8SJohn Marino {
476a45ae5f8SJohn Marino   struct gdb_symtab *symtabs;
477a45ae5f8SJohn Marino };
478a45ae5f8SJohn Marino 
479a45ae5f8SJohn Marino /* The type of the `private' data passed around by the callback
480a45ae5f8SJohn Marino    functions.  */
481a45ae5f8SJohn Marino 
482a45ae5f8SJohn Marino typedef CORE_ADDR jit_dbg_reader_data;
483a45ae5f8SJohn Marino 
484a45ae5f8SJohn Marino /* The reader calls into this function to read data off the targets
485a45ae5f8SJohn Marino    address space.  */
486a45ae5f8SJohn Marino 
487a45ae5f8SJohn Marino static enum gdb_status
jit_target_read_impl(GDB_CORE_ADDR target_mem,void * gdb_buf,int len)488a45ae5f8SJohn Marino jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len)
489a45ae5f8SJohn Marino {
490a45ae5f8SJohn Marino   int result = target_read_memory ((CORE_ADDR) target_mem, gdb_buf, len);
491a45ae5f8SJohn Marino   if (result == 0)
492a45ae5f8SJohn Marino     return GDB_SUCCESS;
493a45ae5f8SJohn Marino   else
494a45ae5f8SJohn Marino     return GDB_FAIL;
495a45ae5f8SJohn Marino }
496a45ae5f8SJohn Marino 
497a45ae5f8SJohn Marino /* The reader calls into this function to create a new gdb_object
498a45ae5f8SJohn Marino    which it can then pass around to the other callbacks.  Right now,
499a45ae5f8SJohn Marino    all that is required is allocating the memory.  */
500a45ae5f8SJohn Marino 
501a45ae5f8SJohn Marino static struct gdb_object *
jit_object_open_impl(struct gdb_symbol_callbacks * cb)502a45ae5f8SJohn Marino jit_object_open_impl (struct gdb_symbol_callbacks *cb)
503a45ae5f8SJohn Marino {
504a45ae5f8SJohn Marino   /* CB is not required right now, but sometime in the future we might
505a45ae5f8SJohn Marino      need a handle to it, and we'd like to do that without breaking
506a45ae5f8SJohn Marino      the ABI.  */
507a45ae5f8SJohn Marino   return XZALLOC (struct gdb_object);
508a45ae5f8SJohn Marino }
509a45ae5f8SJohn Marino 
510a45ae5f8SJohn Marino /* Readers call into this function to open a new gdb_symtab, which,
511a45ae5f8SJohn Marino    again, is passed around to other callbacks.  */
512a45ae5f8SJohn Marino 
513a45ae5f8SJohn Marino static struct gdb_symtab *
jit_symtab_open_impl(struct gdb_symbol_callbacks * cb,struct gdb_object * object,const char * file_name)514a45ae5f8SJohn Marino jit_symtab_open_impl (struct gdb_symbol_callbacks *cb,
515a45ae5f8SJohn Marino                       struct gdb_object *object,
516a45ae5f8SJohn Marino                       const char *file_name)
517a45ae5f8SJohn Marino {
518a45ae5f8SJohn Marino   struct gdb_symtab *ret;
519a45ae5f8SJohn Marino 
520a45ae5f8SJohn Marino   /* CB stays unused.  See comment in jit_object_open_impl.  */
521a45ae5f8SJohn Marino 
522a45ae5f8SJohn Marino   ret = XZALLOC (struct gdb_symtab);
523a45ae5f8SJohn Marino   ret->file_name = file_name ? xstrdup (file_name) : xstrdup ("");
524a45ae5f8SJohn Marino   ret->next = object->symtabs;
525a45ae5f8SJohn Marino   object->symtabs = ret;
526a45ae5f8SJohn Marino   return ret;
527a45ae5f8SJohn Marino }
528a45ae5f8SJohn Marino 
529a45ae5f8SJohn Marino /* Returns true if the block corresponding to old should be placed
530a45ae5f8SJohn Marino    before the block corresponding to new in the final blockvector.  */
531a45ae5f8SJohn Marino 
532a45ae5f8SJohn Marino static int
compare_block(const struct gdb_block * const old,const struct gdb_block * const new)533a45ae5f8SJohn Marino compare_block (const struct gdb_block *const old,
534a45ae5f8SJohn Marino                const struct gdb_block *const new)
535a45ae5f8SJohn Marino {
536a45ae5f8SJohn Marino   if (old == NULL)
537a45ae5f8SJohn Marino     return 1;
538a45ae5f8SJohn Marino   if (old->begin < new->begin)
539a45ae5f8SJohn Marino     return 1;
540a45ae5f8SJohn Marino   else if (old->begin == new->begin)
541a45ae5f8SJohn Marino     {
542a45ae5f8SJohn Marino       if (old->end > new->end)
543a45ae5f8SJohn Marino         return 1;
544a45ae5f8SJohn Marino       else
545a45ae5f8SJohn Marino         return 0;
546a45ae5f8SJohn Marino     }
547a45ae5f8SJohn Marino   else
548a45ae5f8SJohn Marino     return 0;
549a45ae5f8SJohn Marino }
550a45ae5f8SJohn Marino 
551a45ae5f8SJohn Marino /* Called by readers to open a new gdb_block.  This function also
552a45ae5f8SJohn Marino    inserts the new gdb_block in the correct place in the corresponding
553a45ae5f8SJohn Marino    gdb_symtab.  */
554a45ae5f8SJohn Marino 
555a45ae5f8SJohn Marino static struct gdb_block *
jit_block_open_impl(struct gdb_symbol_callbacks * cb,struct gdb_symtab * symtab,struct gdb_block * parent,GDB_CORE_ADDR begin,GDB_CORE_ADDR end,const char * name)556a45ae5f8SJohn Marino jit_block_open_impl (struct gdb_symbol_callbacks *cb,
557a45ae5f8SJohn Marino                      struct gdb_symtab *symtab, struct gdb_block *parent,
558a45ae5f8SJohn Marino                      GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name)
559a45ae5f8SJohn Marino {
560a45ae5f8SJohn Marino   struct gdb_block *block = XZALLOC (struct gdb_block);
561a45ae5f8SJohn Marino 
562a45ae5f8SJohn Marino   block->next = symtab->blocks;
563a45ae5f8SJohn Marino   block->begin = (CORE_ADDR) begin;
564a45ae5f8SJohn Marino   block->end = (CORE_ADDR) end;
565a45ae5f8SJohn Marino   block->name = name ? xstrdup (name) : NULL;
566a45ae5f8SJohn Marino   block->parent = parent;
567a45ae5f8SJohn Marino 
568a45ae5f8SJohn Marino   /* Ensure that the blocks are inserted in the correct (reverse of
569a45ae5f8SJohn Marino      the order expected by blockvector).  */
570a45ae5f8SJohn Marino   if (compare_block (symtab->blocks, block))
571a45ae5f8SJohn Marino     {
572a45ae5f8SJohn Marino       symtab->blocks = block;
573a45ae5f8SJohn Marino     }
574a45ae5f8SJohn Marino   else
575a45ae5f8SJohn Marino     {
576a45ae5f8SJohn Marino       struct gdb_block *i = symtab->blocks;
577a45ae5f8SJohn Marino 
578a45ae5f8SJohn Marino       for (;; i = i->next)
579a45ae5f8SJohn Marino         {
580a45ae5f8SJohn Marino           /* Guaranteed to terminate, since compare_block (NULL, _)
581a45ae5f8SJohn Marino              returns 1.  */
582a45ae5f8SJohn Marino           if (compare_block (i->next, block))
583a45ae5f8SJohn Marino             {
584a45ae5f8SJohn Marino               block->next = i->next;
585a45ae5f8SJohn Marino               i->next = block;
586a45ae5f8SJohn Marino               break;
587a45ae5f8SJohn Marino             }
588a45ae5f8SJohn Marino         }
589a45ae5f8SJohn Marino     }
590a45ae5f8SJohn Marino   symtab->nblocks++;
591a45ae5f8SJohn Marino 
592a45ae5f8SJohn Marino   return block;
593a45ae5f8SJohn Marino }
594a45ae5f8SJohn Marino 
595a45ae5f8SJohn Marino /* Readers call this to add a line mapping (from PC to line number) to
596a45ae5f8SJohn Marino    a gdb_symtab.  */
5975796c8dcSSimon Schubert 
5985796c8dcSSimon Schubert static void
jit_symtab_line_mapping_add_impl(struct gdb_symbol_callbacks * cb,struct gdb_symtab * stab,int nlines,struct gdb_line_mapping * map)599a45ae5f8SJohn Marino jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb,
600a45ae5f8SJohn Marino                                   struct gdb_symtab *stab, int nlines,
601a45ae5f8SJohn Marino                                   struct gdb_line_mapping *map)
602a45ae5f8SJohn Marino {
603a45ae5f8SJohn Marino   int i;
604a45ae5f8SJohn Marino 
605a45ae5f8SJohn Marino   if (nlines < 1)
606a45ae5f8SJohn Marino     return;
607a45ae5f8SJohn Marino 
608a45ae5f8SJohn Marino   stab->linetable = xmalloc (sizeof (struct linetable)
609a45ae5f8SJohn Marino                              + (nlines - 1) * sizeof (struct linetable_entry));
610a45ae5f8SJohn Marino   stab->linetable->nitems = nlines;
611a45ae5f8SJohn Marino   for (i = 0; i < nlines; i++)
612a45ae5f8SJohn Marino     {
613a45ae5f8SJohn Marino       stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc;
614a45ae5f8SJohn Marino       stab->linetable->item[i].line = map[i].line;
615a45ae5f8SJohn Marino     }
616a45ae5f8SJohn Marino }
617a45ae5f8SJohn Marino 
618a45ae5f8SJohn Marino /* Called by readers to close a gdb_symtab.  Does not need to do
619a45ae5f8SJohn Marino    anything as of now.  */
620a45ae5f8SJohn Marino 
621a45ae5f8SJohn Marino static void
jit_symtab_close_impl(struct gdb_symbol_callbacks * cb,struct gdb_symtab * stab)622a45ae5f8SJohn Marino jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
623a45ae5f8SJohn Marino                        struct gdb_symtab *stab)
624a45ae5f8SJohn Marino {
625a45ae5f8SJohn Marino   /* Right now nothing needs to be done here.  We may need to do some
626a45ae5f8SJohn Marino      cleanup here in the future (again, without breaking the plugin
627a45ae5f8SJohn Marino      ABI).  */
628a45ae5f8SJohn Marino }
629a45ae5f8SJohn Marino 
630a45ae5f8SJohn Marino /* Transform STAB to a proper symtab, and add it it OBJFILE.  */
631a45ae5f8SJohn Marino 
632a45ae5f8SJohn Marino static void
finalize_symtab(struct gdb_symtab * stab,struct objfile * objfile)633a45ae5f8SJohn Marino finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
634a45ae5f8SJohn Marino {
635a45ae5f8SJohn Marino   struct symtab *symtab;
636a45ae5f8SJohn Marino   struct gdb_block *gdb_block_iter, *gdb_block_iter_tmp;
637a45ae5f8SJohn Marino   struct block *block_iter;
638a45ae5f8SJohn Marino   int actual_nblocks, i, blockvector_size;
639a45ae5f8SJohn Marino   CORE_ADDR begin, end;
640a45ae5f8SJohn Marino 
641a45ae5f8SJohn Marino   actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks;
642a45ae5f8SJohn Marino 
643a45ae5f8SJohn Marino   symtab = allocate_symtab (stab->file_name, objfile);
644a45ae5f8SJohn Marino   /* JIT compilers compile in memory.  */
645a45ae5f8SJohn Marino   symtab->dirname = NULL;
646a45ae5f8SJohn Marino 
647a45ae5f8SJohn Marino   /* Copy over the linetable entry if one was provided.  */
648a45ae5f8SJohn Marino   if (stab->linetable)
649a45ae5f8SJohn Marino     {
650a45ae5f8SJohn Marino       int size = ((stab->linetable->nitems - 1)
651a45ae5f8SJohn Marino                   * sizeof (struct linetable_entry)
652a45ae5f8SJohn Marino                   + sizeof (struct linetable));
653a45ae5f8SJohn Marino       LINETABLE (symtab) = obstack_alloc (&objfile->objfile_obstack, size);
654a45ae5f8SJohn Marino       memcpy (LINETABLE (symtab), stab->linetable, size);
655a45ae5f8SJohn Marino     }
656a45ae5f8SJohn Marino   else
657a45ae5f8SJohn Marino     {
658a45ae5f8SJohn Marino       LINETABLE (symtab) = NULL;
659a45ae5f8SJohn Marino     }
660a45ae5f8SJohn Marino 
661a45ae5f8SJohn Marino   blockvector_size = (sizeof (struct blockvector)
662a45ae5f8SJohn Marino                       + (actual_nblocks - 1) * sizeof (struct block *));
663a45ae5f8SJohn Marino   symtab->blockvector = obstack_alloc (&objfile->objfile_obstack,
664a45ae5f8SJohn Marino                                        blockvector_size);
665a45ae5f8SJohn Marino 
666a45ae5f8SJohn Marino   /* (begin, end) will contain the PC range this entire blockvector
667a45ae5f8SJohn Marino      spans.  */
668a45ae5f8SJohn Marino   symtab->primary = 1;
669a45ae5f8SJohn Marino   BLOCKVECTOR_MAP (symtab->blockvector) = NULL;
670a45ae5f8SJohn Marino   begin = stab->blocks->begin;
671a45ae5f8SJohn Marino   end = stab->blocks->end;
672a45ae5f8SJohn Marino   BLOCKVECTOR_NBLOCKS (symtab->blockvector) = actual_nblocks;
673a45ae5f8SJohn Marino 
674a45ae5f8SJohn Marino   /* First run over all the gdb_block objects, creating a real block
675a45ae5f8SJohn Marino      object for each.  Simultaneously, keep setting the real_block
676a45ae5f8SJohn Marino      fields.  */
677a45ae5f8SJohn Marino   for (i = (actual_nblocks - 1), gdb_block_iter = stab->blocks;
678a45ae5f8SJohn Marino        i >= FIRST_LOCAL_BLOCK;
679a45ae5f8SJohn Marino        i--, gdb_block_iter = gdb_block_iter->next)
680a45ae5f8SJohn Marino     {
681a45ae5f8SJohn Marino       struct block *new_block = allocate_block (&objfile->objfile_obstack);
682a45ae5f8SJohn Marino       struct symbol *block_name = obstack_alloc (&objfile->objfile_obstack,
683a45ae5f8SJohn Marino                                                  sizeof (struct symbol));
684*ef5ccd6cSJohn Marino       struct type *block_type = arch_type (get_objfile_arch (objfile),
685*ef5ccd6cSJohn Marino 					   TYPE_CODE_VOID,
686*ef5ccd6cSJohn Marino 					   1,
687*ef5ccd6cSJohn Marino 					   "void");
688a45ae5f8SJohn Marino 
689a45ae5f8SJohn Marino       BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
690a45ae5f8SJohn Marino                                                    NULL);
691a45ae5f8SJohn Marino       /* The address range.  */
692a45ae5f8SJohn Marino       BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter->begin;
693a45ae5f8SJohn Marino       BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter->end;
694a45ae5f8SJohn Marino 
695a45ae5f8SJohn Marino       /* The name.  */
696a45ae5f8SJohn Marino       memset (block_name, 0, sizeof (struct symbol));
697a45ae5f8SJohn Marino       SYMBOL_DOMAIN (block_name) = VAR_DOMAIN;
698a45ae5f8SJohn Marino       SYMBOL_CLASS (block_name) = LOC_BLOCK;
699a45ae5f8SJohn Marino       SYMBOL_SYMTAB (block_name) = symtab;
700*ef5ccd6cSJohn Marino       SYMBOL_TYPE (block_name) = lookup_function_type (block_type);
701a45ae5f8SJohn Marino       SYMBOL_BLOCK_VALUE (block_name) = new_block;
702a45ae5f8SJohn Marino 
703*ef5ccd6cSJohn Marino       block_name->ginfo.name = obstack_copy0 (&objfile->objfile_obstack,
704*ef5ccd6cSJohn Marino 					      gdb_block_iter->name,
705*ef5ccd6cSJohn Marino 					      strlen (gdb_block_iter->name));
706a45ae5f8SJohn Marino 
707a45ae5f8SJohn Marino       BLOCK_FUNCTION (new_block) = block_name;
708a45ae5f8SJohn Marino 
709a45ae5f8SJohn Marino       BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
710a45ae5f8SJohn Marino       if (begin > BLOCK_START (new_block))
711a45ae5f8SJohn Marino         begin = BLOCK_START (new_block);
712a45ae5f8SJohn Marino       if (end < BLOCK_END (new_block))
713a45ae5f8SJohn Marino         end = BLOCK_END (new_block);
714a45ae5f8SJohn Marino 
715a45ae5f8SJohn Marino       gdb_block_iter->real_block = new_block;
716a45ae5f8SJohn Marino     }
717a45ae5f8SJohn Marino 
718a45ae5f8SJohn Marino   /* Now add the special blocks.  */
719a45ae5f8SJohn Marino   block_iter = NULL;
720a45ae5f8SJohn Marino   for (i = 0; i < FIRST_LOCAL_BLOCK; i++)
721a45ae5f8SJohn Marino     {
722*ef5ccd6cSJohn Marino       struct block *new_block;
723*ef5ccd6cSJohn Marino 
724*ef5ccd6cSJohn Marino       new_block = (i == GLOBAL_BLOCK
725*ef5ccd6cSJohn Marino 		   ? allocate_global_block (&objfile->objfile_obstack)
726*ef5ccd6cSJohn Marino 		   : allocate_block (&objfile->objfile_obstack));
727a45ae5f8SJohn Marino       BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
728a45ae5f8SJohn Marino                                                    NULL);
729a45ae5f8SJohn Marino       BLOCK_SUPERBLOCK (new_block) = block_iter;
730a45ae5f8SJohn Marino       block_iter = new_block;
731a45ae5f8SJohn Marino 
732a45ae5f8SJohn Marino       BLOCK_START (new_block) = (CORE_ADDR) begin;
733a45ae5f8SJohn Marino       BLOCK_END (new_block) = (CORE_ADDR) end;
734a45ae5f8SJohn Marino 
735a45ae5f8SJohn Marino       BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
736*ef5ccd6cSJohn Marino 
737*ef5ccd6cSJohn Marino       if (i == GLOBAL_BLOCK)
738*ef5ccd6cSJohn Marino 	set_block_symtab (new_block, symtab);
739a45ae5f8SJohn Marino     }
740a45ae5f8SJohn Marino 
741a45ae5f8SJohn Marino   /* Fill up the superblock fields for the real blocks, using the
742a45ae5f8SJohn Marino      real_block fields populated earlier.  */
743a45ae5f8SJohn Marino   for (gdb_block_iter = stab->blocks;
744a45ae5f8SJohn Marino        gdb_block_iter;
745a45ae5f8SJohn Marino        gdb_block_iter = gdb_block_iter->next)
746a45ae5f8SJohn Marino     {
747a45ae5f8SJohn Marino       if (gdb_block_iter->parent != NULL)
748*ef5ccd6cSJohn Marino 	{
749*ef5ccd6cSJohn Marino 	  /* If the plugin specifically mentioned a parent block, we
750*ef5ccd6cSJohn Marino 	     use that.  */
751a45ae5f8SJohn Marino 	  BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
752a45ae5f8SJohn Marino 	    gdb_block_iter->parent->real_block;
753a45ae5f8SJohn Marino 	}
754*ef5ccd6cSJohn Marino       else
755*ef5ccd6cSJohn Marino 	{
756*ef5ccd6cSJohn Marino 	  /* And if not, we set a default parent block.  */
757*ef5ccd6cSJohn Marino 	  BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
758*ef5ccd6cSJohn Marino 	    BLOCKVECTOR_BLOCK (symtab->blockvector, STATIC_BLOCK);
759*ef5ccd6cSJohn Marino 	}
760*ef5ccd6cSJohn Marino     }
761a45ae5f8SJohn Marino 
762a45ae5f8SJohn Marino   /* Free memory.  */
763a45ae5f8SJohn Marino   gdb_block_iter = stab->blocks;
764a45ae5f8SJohn Marino 
765a45ae5f8SJohn Marino   for (gdb_block_iter = stab->blocks, gdb_block_iter_tmp = gdb_block_iter->next;
766a45ae5f8SJohn Marino        gdb_block_iter;
767a45ae5f8SJohn Marino        gdb_block_iter = gdb_block_iter_tmp)
768a45ae5f8SJohn Marino     {
769a45ae5f8SJohn Marino       xfree ((void *) gdb_block_iter->name);
770a45ae5f8SJohn Marino       xfree (gdb_block_iter);
771a45ae5f8SJohn Marino     }
772a45ae5f8SJohn Marino   xfree (stab->linetable);
773a45ae5f8SJohn Marino   xfree ((char *) stab->file_name);
774a45ae5f8SJohn Marino   xfree (stab);
775a45ae5f8SJohn Marino }
776a45ae5f8SJohn Marino 
777a45ae5f8SJohn Marino /* Called when closing a gdb_objfile.  Converts OBJ to a proper
778a45ae5f8SJohn Marino    objfile.  */
779a45ae5f8SJohn Marino 
780a45ae5f8SJohn Marino static void
jit_object_close_impl(struct gdb_symbol_callbacks * cb,struct gdb_object * obj)781a45ae5f8SJohn Marino jit_object_close_impl (struct gdb_symbol_callbacks *cb,
782a45ae5f8SJohn Marino                        struct gdb_object *obj)
783a45ae5f8SJohn Marino {
784a45ae5f8SJohn Marino   struct gdb_symtab *i, *j;
785a45ae5f8SJohn Marino   struct objfile *objfile;
786a45ae5f8SJohn Marino   jit_dbg_reader_data *priv_data;
787a45ae5f8SJohn Marino 
788a45ae5f8SJohn Marino   priv_data = cb->priv_data;
789a45ae5f8SJohn Marino 
790a45ae5f8SJohn Marino   objfile = allocate_objfile (NULL, 0);
791*ef5ccd6cSJohn Marino   objfile->gdbarch = target_gdbarch ();
792a45ae5f8SJohn Marino 
793*ef5ccd6cSJohn Marino   terminate_minimal_symbol_table (objfile);
794a45ae5f8SJohn Marino 
795*ef5ccd6cSJohn Marino   objfile->name = "<< JIT compiled code >>";
796a45ae5f8SJohn Marino 
797a45ae5f8SJohn Marino   j = NULL;
798a45ae5f8SJohn Marino   for (i = obj->symtabs; i; i = j)
799a45ae5f8SJohn Marino     {
800a45ae5f8SJohn Marino       j = i->next;
801a45ae5f8SJohn Marino       finalize_symtab (i, objfile);
802a45ae5f8SJohn Marino     }
803a45ae5f8SJohn Marino   add_objfile_entry (objfile, *priv_data);
804a45ae5f8SJohn Marino   xfree (obj);
805a45ae5f8SJohn Marino }
806a45ae5f8SJohn Marino 
807a45ae5f8SJohn Marino /* Try to read CODE_ENTRY using the loaded jit reader (if any).
808a45ae5f8SJohn Marino    ENTRY_ADDR is the address of the struct jit_code_entry in the
809a45ae5f8SJohn Marino    inferior address space.  */
810a45ae5f8SJohn Marino 
811a45ae5f8SJohn Marino static int
jit_reader_try_read_symtab(struct jit_code_entry * code_entry,CORE_ADDR entry_addr)812a45ae5f8SJohn Marino jit_reader_try_read_symtab (struct jit_code_entry *code_entry,
813a45ae5f8SJohn Marino                             CORE_ADDR entry_addr)
814a45ae5f8SJohn Marino {
815a45ae5f8SJohn Marino   void *gdb_mem;
816a45ae5f8SJohn Marino   int status;
817a45ae5f8SJohn Marino   jit_dbg_reader_data priv_data;
818a45ae5f8SJohn Marino   struct gdb_reader_funcs *funcs;
819a45ae5f8SJohn Marino   volatile struct gdb_exception e;
820a45ae5f8SJohn Marino   struct gdb_symbol_callbacks callbacks =
821a45ae5f8SJohn Marino     {
822a45ae5f8SJohn Marino       jit_object_open_impl,
823a45ae5f8SJohn Marino       jit_symtab_open_impl,
824a45ae5f8SJohn Marino       jit_block_open_impl,
825a45ae5f8SJohn Marino       jit_symtab_close_impl,
826a45ae5f8SJohn Marino       jit_object_close_impl,
827a45ae5f8SJohn Marino 
828a45ae5f8SJohn Marino       jit_symtab_line_mapping_add_impl,
829a45ae5f8SJohn Marino       jit_target_read_impl,
830a45ae5f8SJohn Marino 
831a45ae5f8SJohn Marino       &priv_data
832a45ae5f8SJohn Marino     };
833a45ae5f8SJohn Marino 
834a45ae5f8SJohn Marino   priv_data = entry_addr;
835a45ae5f8SJohn Marino 
836a45ae5f8SJohn Marino   if (!loaded_jit_reader)
837a45ae5f8SJohn Marino     return 0;
838a45ae5f8SJohn Marino 
839a45ae5f8SJohn Marino   gdb_mem = xmalloc (code_entry->symfile_size);
840a45ae5f8SJohn Marino 
841a45ae5f8SJohn Marino   status = 1;
842a45ae5f8SJohn Marino   TRY_CATCH (e, RETURN_MASK_ALL)
843a45ae5f8SJohn Marino     if (target_read_memory (code_entry->symfile_addr, gdb_mem,
844a45ae5f8SJohn Marino                             code_entry->symfile_size))
845a45ae5f8SJohn Marino       status = 0;
846a45ae5f8SJohn Marino   if (e.reason < 0)
847a45ae5f8SJohn Marino     status = 0;
848a45ae5f8SJohn Marino 
849a45ae5f8SJohn Marino   if (status)
850a45ae5f8SJohn Marino     {
851a45ae5f8SJohn Marino       funcs = loaded_jit_reader->functions;
852a45ae5f8SJohn Marino       if (funcs->read (funcs, &callbacks, gdb_mem, code_entry->symfile_size)
853a45ae5f8SJohn Marino           != GDB_SUCCESS)
854a45ae5f8SJohn Marino         status = 0;
855a45ae5f8SJohn Marino     }
856a45ae5f8SJohn Marino 
857a45ae5f8SJohn Marino   xfree (gdb_mem);
858a45ae5f8SJohn Marino   if (jit_debug && status == 0)
859a45ae5f8SJohn Marino     fprintf_unfiltered (gdb_stdlog,
860a45ae5f8SJohn Marino                         "Could not read symtab using the loaded JIT reader.\n");
861a45ae5f8SJohn Marino   return status;
862a45ae5f8SJohn Marino }
863a45ae5f8SJohn Marino 
864a45ae5f8SJohn Marino /* Try to read CODE_ENTRY using BFD.  ENTRY_ADDR is the address of the
865a45ae5f8SJohn Marino    struct jit_code_entry in the inferior address space.  */
866a45ae5f8SJohn Marino 
867a45ae5f8SJohn Marino static void
jit_bfd_try_read_symtab(struct jit_code_entry * code_entry,CORE_ADDR entry_addr,struct gdbarch * gdbarch)868a45ae5f8SJohn Marino jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
869a45ae5f8SJohn Marino                          CORE_ADDR entry_addr,
870a45ae5f8SJohn Marino                          struct gdbarch *gdbarch)
8715796c8dcSSimon Schubert {
8725796c8dcSSimon Schubert   bfd *nbfd;
8735796c8dcSSimon Schubert   struct section_addr_info *sai;
8745796c8dcSSimon Schubert   struct bfd_section *sec;
8755796c8dcSSimon Schubert   struct objfile *objfile;
876a45ae5f8SJohn Marino   struct cleanup *old_cleanups;
8775796c8dcSSimon Schubert   int i;
8785796c8dcSSimon Schubert   const struct bfd_arch_info *b;
8795796c8dcSSimon Schubert 
880c50c785cSJohn Marino   if (jit_debug)
881c50c785cSJohn Marino     fprintf_unfiltered (gdb_stdlog,
882c50c785cSJohn Marino 			"jit_register_code, symfile_addr = %s, "
883c50c785cSJohn Marino 			"symfile_size = %s\n",
884c50c785cSJohn Marino 			paddress (gdbarch, code_entry->symfile_addr),
885c50c785cSJohn Marino 			pulongest (code_entry->symfile_size));
886c50c785cSJohn Marino 
8875796c8dcSSimon Schubert   nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
8885796c8dcSSimon Schubert                                       code_entry->symfile_size, gnutarget);
889a45ae5f8SJohn Marino   if (nbfd == NULL)
890a45ae5f8SJohn Marino     {
891a45ae5f8SJohn Marino       puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
892a45ae5f8SJohn Marino       return;
893a45ae5f8SJohn Marino     }
8945796c8dcSSimon Schubert 
8955796c8dcSSimon Schubert   /* Check the format.  NOTE: This initializes important data that GDB uses!
8965796c8dcSSimon Schubert      We would segfault later without this line.  */
8975796c8dcSSimon Schubert   if (!bfd_check_format (nbfd, bfd_object))
8985796c8dcSSimon Schubert     {
8995796c8dcSSimon Schubert       printf_unfiltered (_("\
9005796c8dcSSimon Schubert JITed symbol file is not an object file, ignoring it.\n"));
901*ef5ccd6cSJohn Marino       gdb_bfd_unref (nbfd);
9025796c8dcSSimon Schubert       return;
9035796c8dcSSimon Schubert     }
9045796c8dcSSimon Schubert 
9055796c8dcSSimon Schubert   /* Check bfd arch.  */
9065796c8dcSSimon Schubert   b = gdbarch_bfd_arch_info (gdbarch);
9075796c8dcSSimon Schubert   if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
9085796c8dcSSimon Schubert     warning (_("JITed object file architecture %s is not compatible "
9095796c8dcSSimon Schubert                "with target architecture %s."), bfd_get_arch_info
9105796c8dcSSimon Schubert              (nbfd)->printable_name, b->printable_name);
9115796c8dcSSimon Schubert 
9125796c8dcSSimon Schubert   /* Read the section address information out of the symbol file.  Since the
9135796c8dcSSimon Schubert      file is generated by the JIT at runtime, it should all of the absolute
9145796c8dcSSimon Schubert      addresses that we care about.  */
9155796c8dcSSimon Schubert   sai = alloc_section_addr_info (bfd_count_sections (nbfd));
916a45ae5f8SJohn Marino   old_cleanups = make_cleanup_free_section_addr_info (sai);
9175796c8dcSSimon Schubert   i = 0;
9185796c8dcSSimon Schubert   for (sec = nbfd->sections; sec != NULL; sec = sec->next)
9195796c8dcSSimon Schubert     if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
9205796c8dcSSimon Schubert       {
9215796c8dcSSimon Schubert         /* We assume that these virtual addresses are absolute, and do not
9225796c8dcSSimon Schubert            treat them as offsets.  */
9235796c8dcSSimon Schubert         sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
924cf7f2e2dSJohn Marino         sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
9255796c8dcSSimon Schubert         sai->other[i].sectindex = sec->index;
9265796c8dcSSimon Schubert         ++i;
9275796c8dcSSimon Schubert       }
9285796c8dcSSimon Schubert 
929*ef5ccd6cSJohn Marino   /* This call does not take ownership of SAI.  */
930*ef5ccd6cSJohn Marino   make_cleanup_bfd_unref (nbfd);
931a45ae5f8SJohn Marino   objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
9325796c8dcSSimon Schubert 
933a45ae5f8SJohn Marino   do_cleanups (old_cleanups);
934a45ae5f8SJohn Marino   add_objfile_entry (objfile, entry_addr);
935a45ae5f8SJohn Marino }
9365796c8dcSSimon Schubert 
937a45ae5f8SJohn Marino /* This function registers code associated with a JIT code entry.  It uses the
938a45ae5f8SJohn Marino    pointer and size pair in the entry to read the symbol file from the remote
939a45ae5f8SJohn Marino    and then calls symbol_file_add_from_local_memory to add it as though it were
940a45ae5f8SJohn Marino    a symbol file added by the user.  */
941a45ae5f8SJohn Marino 
942a45ae5f8SJohn Marino static void
jit_register_code(struct gdbarch * gdbarch,CORE_ADDR entry_addr,struct jit_code_entry * code_entry)943a45ae5f8SJohn Marino jit_register_code (struct gdbarch *gdbarch,
944a45ae5f8SJohn Marino                    CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
945a45ae5f8SJohn Marino {
946*ef5ccd6cSJohn Marino   int success;
947a45ae5f8SJohn Marino 
948a45ae5f8SJohn Marino   if (jit_debug)
949a45ae5f8SJohn Marino     fprintf_unfiltered (gdb_stdlog,
950a45ae5f8SJohn Marino                         "jit_register_code, symfile_addr = %s, "
951a45ae5f8SJohn Marino                         "symfile_size = %s\n",
952a45ae5f8SJohn Marino                         paddress (gdbarch, code_entry->symfile_addr),
953a45ae5f8SJohn Marino                         pulongest (code_entry->symfile_size));
954a45ae5f8SJohn Marino 
955a45ae5f8SJohn Marino   success = jit_reader_try_read_symtab (code_entry, entry_addr);
956a45ae5f8SJohn Marino 
957a45ae5f8SJohn Marino   if (!success)
958a45ae5f8SJohn Marino     jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch);
9595796c8dcSSimon Schubert }
9605796c8dcSSimon Schubert 
961c50c785cSJohn Marino /* This function unregisters JITed code and frees the corresponding
962c50c785cSJohn Marino    objfile.  */
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert static void
jit_unregister_code(struct objfile * objfile)9655796c8dcSSimon Schubert jit_unregister_code (struct objfile *objfile)
9665796c8dcSSimon Schubert {
9675796c8dcSSimon Schubert   free_objfile (objfile);
9685796c8dcSSimon Schubert }
9695796c8dcSSimon Schubert 
9705796c8dcSSimon Schubert /* Look up the objfile with this code entry address.  */
9715796c8dcSSimon Schubert 
9725796c8dcSSimon Schubert static struct objfile *
jit_find_objf_with_entry_addr(CORE_ADDR entry_addr)9735796c8dcSSimon Schubert jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
9745796c8dcSSimon Schubert {
9755796c8dcSSimon Schubert   struct objfile *objf;
9765796c8dcSSimon Schubert 
9775796c8dcSSimon Schubert   ALL_OBJFILES (objf)
9785796c8dcSSimon Schubert     {
979*ef5ccd6cSJohn Marino       struct jit_objfile_data *objf_data;
980*ef5ccd6cSJohn Marino 
981*ef5ccd6cSJohn Marino       objf_data = objfile_data (objf, jit_objfile_data);
982*ef5ccd6cSJohn Marino       if (objf_data != NULL && objf_data->addr == entry_addr)
9835796c8dcSSimon Schubert         return objf;
9845796c8dcSSimon Schubert     }
9855796c8dcSSimon Schubert   return NULL;
9865796c8dcSSimon Schubert }
9875796c8dcSSimon Schubert 
988*ef5ccd6cSJohn Marino /* This is called when a breakpoint is deleted.  It updates the
989*ef5ccd6cSJohn Marino    inferior's cache, if needed.  */
990*ef5ccd6cSJohn Marino 
991*ef5ccd6cSJohn Marino static void
jit_breakpoint_deleted(struct breakpoint * b)992*ef5ccd6cSJohn Marino jit_breakpoint_deleted (struct breakpoint *b)
993*ef5ccd6cSJohn Marino {
994*ef5ccd6cSJohn Marino   struct bp_location *iter;
995*ef5ccd6cSJohn Marino 
996*ef5ccd6cSJohn Marino   if (b->type != bp_jit_event)
997*ef5ccd6cSJohn Marino     return;
998*ef5ccd6cSJohn Marino 
999*ef5ccd6cSJohn Marino   for (iter = b->loc; iter != NULL; iter = iter->next)
1000*ef5ccd6cSJohn Marino     {
1001*ef5ccd6cSJohn Marino       struct jit_program_space_data *ps_data;
1002*ef5ccd6cSJohn Marino 
1003*ef5ccd6cSJohn Marino       ps_data = program_space_data (iter->pspace, jit_program_space_data);
1004*ef5ccd6cSJohn Marino       if (ps_data != NULL && ps_data->jit_breakpoint == iter->owner)
1005*ef5ccd6cSJohn Marino 	{
1006*ef5ccd6cSJohn Marino 	  ps_data->cached_code_address = 0;
1007*ef5ccd6cSJohn Marino 	  ps_data->jit_breakpoint = NULL;
1008*ef5ccd6cSJohn Marino 	}
1009*ef5ccd6cSJohn Marino     }
1010*ef5ccd6cSJohn Marino }
1011*ef5ccd6cSJohn Marino 
1012c50c785cSJohn Marino /* (Re-)Initialize the jit breakpoint if necessary.
1013c50c785cSJohn Marino    Return 0 on success.  */
1014c50c785cSJohn Marino 
1015c50c785cSJohn Marino static int
jit_breakpoint_re_set_internal(struct gdbarch * gdbarch,struct jit_program_space_data * ps_data)1016c50c785cSJohn Marino jit_breakpoint_re_set_internal (struct gdbarch *gdbarch,
1017*ef5ccd6cSJohn Marino 				struct jit_program_space_data *ps_data)
1018c50c785cSJohn Marino {
1019*ef5ccd6cSJohn Marino   struct minimal_symbol *reg_symbol, *desc_symbol;
1020*ef5ccd6cSJohn Marino   struct objfile *objf;
1021*ef5ccd6cSJohn Marino   struct jit_objfile_data *objf_data;
1022*ef5ccd6cSJohn Marino   CORE_ADDR addr;
1023c50c785cSJohn Marino 
1024*ef5ccd6cSJohn Marino   if (ps_data->objfile == NULL)
1025*ef5ccd6cSJohn Marino     {
1026*ef5ccd6cSJohn Marino       /* Lookup the registration symbol.  If it is missing, then we
1027*ef5ccd6cSJohn Marino 	 assume we are not attached to a JIT.  */
1028*ef5ccd6cSJohn Marino       reg_symbol = lookup_minimal_symbol_and_objfile (jit_break_name, &objf);
1029*ef5ccd6cSJohn Marino       if (reg_symbol == NULL || SYMBOL_VALUE_ADDRESS (reg_symbol) == 0)
1030c50c785cSJohn Marino 	return 1;
1031c50c785cSJohn Marino 
1032*ef5ccd6cSJohn Marino       desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, objf);
1033*ef5ccd6cSJohn Marino       if (desc_symbol == NULL || SYMBOL_VALUE_ADDRESS (desc_symbol) == 0)
1034*ef5ccd6cSJohn Marino 	return 1;
1035*ef5ccd6cSJohn Marino 
1036*ef5ccd6cSJohn Marino       objf_data = get_jit_objfile_data (objf);
1037*ef5ccd6cSJohn Marino       objf_data->register_code = reg_symbol;
1038*ef5ccd6cSJohn Marino       objf_data->descriptor = desc_symbol;
1039*ef5ccd6cSJohn Marino 
1040*ef5ccd6cSJohn Marino       ps_data->objfile = objf;
1041c50c785cSJohn Marino     }
1042c50c785cSJohn Marino   else
1043*ef5ccd6cSJohn Marino     objf_data = get_jit_objfile_data (ps_data->objfile);
1044*ef5ccd6cSJohn Marino 
1045*ef5ccd6cSJohn Marino   addr = SYMBOL_VALUE_ADDRESS (objf_data->register_code);
1046c50c785cSJohn Marino 
1047c50c785cSJohn Marino   if (jit_debug)
1048c50c785cSJohn Marino     fprintf_unfiltered (gdb_stdlog,
1049c50c785cSJohn Marino 			"jit_breakpoint_re_set_internal, "
1050c50c785cSJohn Marino 			"breakpoint_addr = %s\n",
1051*ef5ccd6cSJohn Marino 			paddress (gdbarch, addr));
1052*ef5ccd6cSJohn Marino 
1053*ef5ccd6cSJohn Marino   if (ps_data->cached_code_address == addr)
1054*ef5ccd6cSJohn Marino     return 1;
1055*ef5ccd6cSJohn Marino 
1056*ef5ccd6cSJohn Marino   /* Delete the old breakpoint.  */
1057*ef5ccd6cSJohn Marino   if (ps_data->jit_breakpoint != NULL)
1058*ef5ccd6cSJohn Marino     delete_breakpoint (ps_data->jit_breakpoint);
1059c50c785cSJohn Marino 
1060c50c785cSJohn Marino   /* Put a breakpoint in the registration symbol.  */
1061*ef5ccd6cSJohn Marino   ps_data->cached_code_address = addr;
1062*ef5ccd6cSJohn Marino   ps_data->jit_breakpoint = create_jit_event_breakpoint (gdbarch, addr);
1063c50c785cSJohn Marino 
1064c50c785cSJohn Marino   return 0;
1065c50c785cSJohn Marino }
1066c50c785cSJohn Marino 
1067a45ae5f8SJohn Marino /* The private data passed around in the frame unwind callback
1068a45ae5f8SJohn Marino    functions.  */
1069a45ae5f8SJohn Marino 
1070a45ae5f8SJohn Marino struct jit_unwind_private
1071a45ae5f8SJohn Marino {
1072a45ae5f8SJohn Marino   /* Cached register values.  See jit_frame_sniffer to see how this
1073a45ae5f8SJohn Marino      works.  */
1074a45ae5f8SJohn Marino   struct gdb_reg_value **registers;
1075a45ae5f8SJohn Marino 
1076a45ae5f8SJohn Marino   /* The frame being unwound.  */
1077a45ae5f8SJohn Marino   struct frame_info *this_frame;
1078a45ae5f8SJohn Marino };
1079a45ae5f8SJohn Marino 
1080a45ae5f8SJohn Marino /* Sets the value of a particular register in this frame.  */
1081a45ae5f8SJohn Marino 
1082a45ae5f8SJohn Marino static void
jit_unwind_reg_set_impl(struct gdb_unwind_callbacks * cb,int dwarf_regnum,struct gdb_reg_value * value)1083a45ae5f8SJohn Marino jit_unwind_reg_set_impl (struct gdb_unwind_callbacks *cb, int dwarf_regnum,
1084a45ae5f8SJohn Marino                          struct gdb_reg_value *value)
1085a45ae5f8SJohn Marino {
1086a45ae5f8SJohn Marino   struct jit_unwind_private *priv;
1087a45ae5f8SJohn Marino   int gdb_reg;
1088a45ae5f8SJohn Marino 
1089a45ae5f8SJohn Marino   priv = cb->priv_data;
1090a45ae5f8SJohn Marino 
1091a45ae5f8SJohn Marino   gdb_reg = gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv->this_frame),
1092a45ae5f8SJohn Marino                                           dwarf_regnum);
1093a45ae5f8SJohn Marino   if (gdb_reg == -1)
1094a45ae5f8SJohn Marino     {
1095a45ae5f8SJohn Marino       if (jit_debug)
1096a45ae5f8SJohn Marino         fprintf_unfiltered (gdb_stdlog,
1097a45ae5f8SJohn Marino                             _("Could not recognize DWARF regnum %d"),
1098a45ae5f8SJohn Marino                             dwarf_regnum);
1099a45ae5f8SJohn Marino       return;
1100a45ae5f8SJohn Marino     }
1101a45ae5f8SJohn Marino 
1102a45ae5f8SJohn Marino   gdb_assert (priv->registers);
1103a45ae5f8SJohn Marino   priv->registers[gdb_reg] = value;
1104a45ae5f8SJohn Marino }
1105a45ae5f8SJohn Marino 
1106a45ae5f8SJohn Marino static void
reg_value_free_impl(struct gdb_reg_value * value)1107a45ae5f8SJohn Marino reg_value_free_impl (struct gdb_reg_value *value)
1108a45ae5f8SJohn Marino {
1109a45ae5f8SJohn Marino   xfree (value);
1110a45ae5f8SJohn Marino }
1111a45ae5f8SJohn Marino 
1112a45ae5f8SJohn Marino /* Get the value of register REGNUM in the previous frame.  */
1113a45ae5f8SJohn Marino 
1114a45ae5f8SJohn Marino static struct gdb_reg_value *
jit_unwind_reg_get_impl(struct gdb_unwind_callbacks * cb,int regnum)1115a45ae5f8SJohn Marino jit_unwind_reg_get_impl (struct gdb_unwind_callbacks *cb, int regnum)
1116a45ae5f8SJohn Marino {
1117a45ae5f8SJohn Marino   struct jit_unwind_private *priv;
1118a45ae5f8SJohn Marino   struct gdb_reg_value *value;
1119a45ae5f8SJohn Marino   int gdb_reg, size;
1120a45ae5f8SJohn Marino   struct gdbarch *frame_arch;
1121a45ae5f8SJohn Marino 
1122a45ae5f8SJohn Marino   priv = cb->priv_data;
1123a45ae5f8SJohn Marino   frame_arch = get_frame_arch (priv->this_frame);
1124a45ae5f8SJohn Marino 
1125a45ae5f8SJohn Marino   gdb_reg = gdbarch_dwarf2_reg_to_regnum (frame_arch, regnum);
1126a45ae5f8SJohn Marino   size = register_size (frame_arch, gdb_reg);
1127a45ae5f8SJohn Marino   value = xmalloc (sizeof (struct gdb_reg_value) + size - 1);
1128*ef5ccd6cSJohn Marino   value->defined = deprecated_frame_register_read (priv->this_frame, gdb_reg,
1129a45ae5f8SJohn Marino 						   value->value);
1130a45ae5f8SJohn Marino   value->size = size;
1131a45ae5f8SJohn Marino   value->free = reg_value_free_impl;
1132a45ae5f8SJohn Marino   return value;
1133a45ae5f8SJohn Marino }
1134a45ae5f8SJohn Marino 
1135a45ae5f8SJohn Marino /* gdb_reg_value has a free function, which must be called on each
1136a45ae5f8SJohn Marino    saved register value.  */
1137a45ae5f8SJohn Marino 
1138a45ae5f8SJohn Marino static void
jit_dealloc_cache(struct frame_info * this_frame,void * cache)1139a45ae5f8SJohn Marino jit_dealloc_cache (struct frame_info *this_frame, void *cache)
1140a45ae5f8SJohn Marino {
1141a45ae5f8SJohn Marino   struct jit_unwind_private *priv_data = cache;
1142a45ae5f8SJohn Marino   struct gdbarch *frame_arch;
1143a45ae5f8SJohn Marino   int i;
1144a45ae5f8SJohn Marino 
1145a45ae5f8SJohn Marino   gdb_assert (priv_data->registers);
1146a45ae5f8SJohn Marino   frame_arch = get_frame_arch (priv_data->this_frame);
1147a45ae5f8SJohn Marino 
1148a45ae5f8SJohn Marino   for (i = 0; i < gdbarch_num_regs (frame_arch); i++)
1149a45ae5f8SJohn Marino     if (priv_data->registers[i] && priv_data->registers[i]->free)
1150a45ae5f8SJohn Marino       priv_data->registers[i]->free (priv_data->registers[i]);
1151a45ae5f8SJohn Marino 
1152a45ae5f8SJohn Marino   xfree (priv_data->registers);
1153a45ae5f8SJohn Marino   xfree (priv_data);
1154a45ae5f8SJohn Marino }
1155a45ae5f8SJohn Marino 
1156a45ae5f8SJohn Marino /* The frame sniffer for the pseudo unwinder.
1157a45ae5f8SJohn Marino 
1158a45ae5f8SJohn Marino    While this is nominally a frame sniffer, in the case where the JIT
1159a45ae5f8SJohn Marino    reader actually recognizes the frame, it does a lot more work -- it
1160a45ae5f8SJohn Marino    unwinds the frame and saves the corresponding register values in
1161a45ae5f8SJohn Marino    the cache.  jit_frame_prev_register simply returns the saved
1162a45ae5f8SJohn Marino    register values.  */
1163a45ae5f8SJohn Marino 
1164a45ae5f8SJohn Marino static int
jit_frame_sniffer(const struct frame_unwind * self,struct frame_info * this_frame,void ** cache)1165a45ae5f8SJohn Marino jit_frame_sniffer (const struct frame_unwind *self,
1166a45ae5f8SJohn Marino                    struct frame_info *this_frame, void **cache)
1167a45ae5f8SJohn Marino {
1168a45ae5f8SJohn Marino   struct jit_unwind_private *priv_data;
1169a45ae5f8SJohn Marino   struct gdb_unwind_callbacks callbacks;
1170a45ae5f8SJohn Marino   struct gdb_reader_funcs *funcs;
1171a45ae5f8SJohn Marino 
1172a45ae5f8SJohn Marino   callbacks.reg_get = jit_unwind_reg_get_impl;
1173a45ae5f8SJohn Marino   callbacks.reg_set = jit_unwind_reg_set_impl;
1174a45ae5f8SJohn Marino   callbacks.target_read = jit_target_read_impl;
1175a45ae5f8SJohn Marino 
1176a45ae5f8SJohn Marino   if (loaded_jit_reader == NULL)
1177a45ae5f8SJohn Marino     return 0;
1178a45ae5f8SJohn Marino 
1179a45ae5f8SJohn Marino   funcs = loaded_jit_reader->functions;
1180a45ae5f8SJohn Marino 
1181a45ae5f8SJohn Marino   gdb_assert (!*cache);
1182a45ae5f8SJohn Marino 
1183a45ae5f8SJohn Marino   *cache = XZALLOC (struct jit_unwind_private);
1184a45ae5f8SJohn Marino   priv_data = *cache;
1185a45ae5f8SJohn Marino   priv_data->registers =
1186a45ae5f8SJohn Marino     XCALLOC (gdbarch_num_regs (get_frame_arch (this_frame)),
1187a45ae5f8SJohn Marino              struct gdb_reg_value *);
1188a45ae5f8SJohn Marino   priv_data->this_frame = this_frame;
1189a45ae5f8SJohn Marino 
1190a45ae5f8SJohn Marino   callbacks.priv_data = priv_data;
1191a45ae5f8SJohn Marino 
1192a45ae5f8SJohn Marino   /* Try to coax the provided unwinder to unwind the stack */
1193a45ae5f8SJohn Marino   if (funcs->unwind (funcs, &callbacks) == GDB_SUCCESS)
1194a45ae5f8SJohn Marino     {
1195a45ae5f8SJohn Marino       if (jit_debug)
1196a45ae5f8SJohn Marino         fprintf_unfiltered (gdb_stdlog, _("Successfully unwound frame using "
1197a45ae5f8SJohn Marino                                           "JIT reader.\n"));
1198a45ae5f8SJohn Marino       return 1;
1199a45ae5f8SJohn Marino     }
1200a45ae5f8SJohn Marino   if (jit_debug)
1201a45ae5f8SJohn Marino     fprintf_unfiltered (gdb_stdlog, _("Could not unwind frame using "
1202a45ae5f8SJohn Marino                                       "JIT reader.\n"));
1203a45ae5f8SJohn Marino 
1204a45ae5f8SJohn Marino   jit_dealloc_cache (this_frame, *cache);
1205a45ae5f8SJohn Marino   *cache = NULL;
1206a45ae5f8SJohn Marino 
1207a45ae5f8SJohn Marino   return 0;
1208a45ae5f8SJohn Marino }
1209a45ae5f8SJohn Marino 
1210a45ae5f8SJohn Marino 
1211a45ae5f8SJohn Marino /* The frame_id function for the pseudo unwinder.  Relays the call to
1212a45ae5f8SJohn Marino    the loaded plugin.  */
1213a45ae5f8SJohn Marino 
1214a45ae5f8SJohn Marino static void
jit_frame_this_id(struct frame_info * this_frame,void ** cache,struct frame_id * this_id)1215a45ae5f8SJohn Marino jit_frame_this_id (struct frame_info *this_frame, void **cache,
1216a45ae5f8SJohn Marino                    struct frame_id *this_id)
1217a45ae5f8SJohn Marino {
1218a45ae5f8SJohn Marino   struct jit_unwind_private private;
1219a45ae5f8SJohn Marino   struct gdb_frame_id frame_id;
1220a45ae5f8SJohn Marino   struct gdb_reader_funcs *funcs;
1221a45ae5f8SJohn Marino   struct gdb_unwind_callbacks callbacks;
1222a45ae5f8SJohn Marino 
1223a45ae5f8SJohn Marino   private.registers = NULL;
1224a45ae5f8SJohn Marino   private.this_frame = this_frame;
1225a45ae5f8SJohn Marino 
1226a45ae5f8SJohn Marino   /* We don't expect the frame_id function to set any registers, so we
1227a45ae5f8SJohn Marino      set reg_set to NULL.  */
1228a45ae5f8SJohn Marino   callbacks.reg_get = jit_unwind_reg_get_impl;
1229a45ae5f8SJohn Marino   callbacks.reg_set = NULL;
1230a45ae5f8SJohn Marino   callbacks.target_read = jit_target_read_impl;
1231a45ae5f8SJohn Marino   callbacks.priv_data = &private;
1232a45ae5f8SJohn Marino 
1233a45ae5f8SJohn Marino   gdb_assert (loaded_jit_reader);
1234a45ae5f8SJohn Marino   funcs = loaded_jit_reader->functions;
1235a45ae5f8SJohn Marino 
1236a45ae5f8SJohn Marino   frame_id = funcs->get_frame_id (funcs, &callbacks);
1237a45ae5f8SJohn Marino   *this_id = frame_id_build (frame_id.stack_address, frame_id.code_address);
1238a45ae5f8SJohn Marino }
1239a45ae5f8SJohn Marino 
1240a45ae5f8SJohn Marino /* Pseudo unwinder function.  Reads the previously fetched value for
1241a45ae5f8SJohn Marino    the register from the cache.  */
1242a45ae5f8SJohn Marino 
1243a45ae5f8SJohn Marino static struct value *
jit_frame_prev_register(struct frame_info * this_frame,void ** cache,int reg)1244a45ae5f8SJohn Marino jit_frame_prev_register (struct frame_info *this_frame, void **cache, int reg)
1245a45ae5f8SJohn Marino {
1246a45ae5f8SJohn Marino   struct jit_unwind_private *priv = *cache;
1247a45ae5f8SJohn Marino   struct gdb_reg_value *value;
1248a45ae5f8SJohn Marino 
1249a45ae5f8SJohn Marino   if (priv == NULL)
1250a45ae5f8SJohn Marino     return frame_unwind_got_optimized (this_frame, reg);
1251a45ae5f8SJohn Marino 
1252a45ae5f8SJohn Marino   gdb_assert (priv->registers);
1253a45ae5f8SJohn Marino   value = priv->registers[reg];
1254a45ae5f8SJohn Marino   if (value && value->defined)
1255a45ae5f8SJohn Marino     return frame_unwind_got_bytes (this_frame, reg, value->value);
1256a45ae5f8SJohn Marino   else
1257a45ae5f8SJohn Marino     return frame_unwind_got_optimized (this_frame, reg);
1258a45ae5f8SJohn Marino }
1259a45ae5f8SJohn Marino 
1260a45ae5f8SJohn Marino /* Relay everything back to the unwinder registered by the JIT debug
1261a45ae5f8SJohn Marino    info reader.*/
1262a45ae5f8SJohn Marino 
1263a45ae5f8SJohn Marino static const struct frame_unwind jit_frame_unwind =
1264a45ae5f8SJohn Marino {
1265a45ae5f8SJohn Marino   NORMAL_FRAME,
1266a45ae5f8SJohn Marino   default_frame_unwind_stop_reason,
1267a45ae5f8SJohn Marino   jit_frame_this_id,
1268a45ae5f8SJohn Marino   jit_frame_prev_register,
1269a45ae5f8SJohn Marino   NULL,
1270a45ae5f8SJohn Marino   jit_frame_sniffer,
1271a45ae5f8SJohn Marino   jit_dealloc_cache
1272a45ae5f8SJohn Marino };
1273a45ae5f8SJohn Marino 
1274a45ae5f8SJohn Marino 
1275a45ae5f8SJohn Marino /* This is the information that is stored at jit_gdbarch_data for each
1276a45ae5f8SJohn Marino    architecture.  */
1277a45ae5f8SJohn Marino 
1278a45ae5f8SJohn Marino struct jit_gdbarch_data_type
1279a45ae5f8SJohn Marino {
1280a45ae5f8SJohn Marino   /* Has the (pseudo) unwinder been prepended? */
1281a45ae5f8SJohn Marino   int unwinder_registered;
1282a45ae5f8SJohn Marino };
1283a45ae5f8SJohn Marino 
1284a45ae5f8SJohn Marino /* Check GDBARCH and prepend the pseudo JIT unwinder if needed.  */
1285a45ae5f8SJohn Marino 
1286a45ae5f8SJohn Marino static void
jit_prepend_unwinder(struct gdbarch * gdbarch)1287a45ae5f8SJohn Marino jit_prepend_unwinder (struct gdbarch *gdbarch)
1288a45ae5f8SJohn Marino {
1289a45ae5f8SJohn Marino   struct jit_gdbarch_data_type *data;
1290a45ae5f8SJohn Marino 
1291a45ae5f8SJohn Marino   data = gdbarch_data (gdbarch, jit_gdbarch_data);
1292a45ae5f8SJohn Marino   if (!data->unwinder_registered)
1293a45ae5f8SJohn Marino     {
1294a45ae5f8SJohn Marino       frame_unwind_prepend_unwinder (gdbarch, &jit_frame_unwind);
1295a45ae5f8SJohn Marino       data->unwinder_registered = 1;
1296a45ae5f8SJohn Marino     }
1297a45ae5f8SJohn Marino }
1298a45ae5f8SJohn Marino 
1299c50c785cSJohn Marino /* Register any already created translations.  */
13005796c8dcSSimon Schubert 
13015796c8dcSSimon Schubert static void
jit_inferior_init(struct gdbarch * gdbarch)13025796c8dcSSimon Schubert jit_inferior_init (struct gdbarch *gdbarch)
13035796c8dcSSimon Schubert {
13045796c8dcSSimon Schubert   struct jit_descriptor descriptor;
13055796c8dcSSimon Schubert   struct jit_code_entry cur_entry;
1306*ef5ccd6cSJohn Marino   struct jit_program_space_data *ps_data;
13075796c8dcSSimon Schubert   CORE_ADDR cur_entry_addr;
13085796c8dcSSimon Schubert 
1309c50c785cSJohn Marino   if (jit_debug)
1310c50c785cSJohn Marino     fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n");
1311c50c785cSJohn Marino 
1312a45ae5f8SJohn Marino   jit_prepend_unwinder (gdbarch);
1313a45ae5f8SJohn Marino 
1314*ef5ccd6cSJohn Marino   ps_data = get_jit_program_space_data ();
1315*ef5ccd6cSJohn Marino   if (jit_breakpoint_re_set_internal (gdbarch, ps_data) != 0)
13165796c8dcSSimon Schubert     return;
13175796c8dcSSimon Schubert 
1318c50c785cSJohn Marino   /* Read the descriptor so we can check the version number and load
1319c50c785cSJohn Marino      any already JITed functions.  */
1320*ef5ccd6cSJohn Marino   if (!jit_read_descriptor (gdbarch, &descriptor, ps_data))
1321*ef5ccd6cSJohn Marino     return;
13225796c8dcSSimon Schubert 
13235796c8dcSSimon Schubert   /* Check that the version number agrees with that we support.  */
13245796c8dcSSimon Schubert   if (descriptor.version != 1)
1325*ef5ccd6cSJohn Marino     {
1326*ef5ccd6cSJohn Marino       printf_unfiltered (_("Unsupported JIT protocol version %ld "
1327*ef5ccd6cSJohn Marino 			   "in descriptor (expected 1)\n"),
1328*ef5ccd6cSJohn Marino 			 (long) descriptor.version);
1329*ef5ccd6cSJohn Marino       return;
1330*ef5ccd6cSJohn Marino     }
13315796c8dcSSimon Schubert 
1332c50c785cSJohn Marino   /* If we've attached to a running program, we need to check the descriptor
1333c50c785cSJohn Marino      to register any functions that were already generated.  */
13345796c8dcSSimon Schubert   for (cur_entry_addr = descriptor.first_entry;
13355796c8dcSSimon Schubert        cur_entry_addr != 0;
13365796c8dcSSimon Schubert        cur_entry_addr = cur_entry.next_entry)
13375796c8dcSSimon Schubert     {
13385796c8dcSSimon Schubert       jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
13395796c8dcSSimon Schubert 
13405796c8dcSSimon Schubert       /* This hook may be called many times during setup, so make sure we don't
13415796c8dcSSimon Schubert          add the same symbol file twice.  */
13425796c8dcSSimon Schubert       if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
13435796c8dcSSimon Schubert         continue;
13445796c8dcSSimon Schubert 
13455796c8dcSSimon Schubert       jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
13465796c8dcSSimon Schubert     }
13475796c8dcSSimon Schubert }
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert /* Exported routine to call when an inferior has been created.  */
13505796c8dcSSimon Schubert 
13515796c8dcSSimon Schubert void
jit_inferior_created_hook(void)13525796c8dcSSimon Schubert jit_inferior_created_hook (void)
13535796c8dcSSimon Schubert {
1354*ef5ccd6cSJohn Marino   jit_inferior_init (target_gdbarch ());
13555796c8dcSSimon Schubert }
13565796c8dcSSimon Schubert 
13575796c8dcSSimon Schubert /* Exported routine to call to re-set the jit breakpoints,
13585796c8dcSSimon Schubert    e.g. when a program is rerun.  */
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert void
jit_breakpoint_re_set(void)13615796c8dcSSimon Schubert jit_breakpoint_re_set (void)
13625796c8dcSSimon Schubert {
1363*ef5ccd6cSJohn Marino   jit_breakpoint_re_set_internal (target_gdbarch (),
1364*ef5ccd6cSJohn Marino 				  get_jit_program_space_data ());
13655796c8dcSSimon Schubert }
13665796c8dcSSimon Schubert 
1367c50c785cSJohn Marino /* This function cleans up any code entries left over when the
1368c50c785cSJohn Marino    inferior exits.  We get left over code when the inferior exits
1369c50c785cSJohn Marino    without unregistering its code, for example when it crashes.  */
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert static void
jit_inferior_exit_hook(struct inferior * inf)1372cf7f2e2dSJohn Marino jit_inferior_exit_hook (struct inferior *inf)
13735796c8dcSSimon Schubert {
13745796c8dcSSimon Schubert   struct objfile *objf;
13755796c8dcSSimon Schubert   struct objfile *temp;
13765796c8dcSSimon Schubert 
13775796c8dcSSimon Schubert   ALL_OBJFILES_SAFE (objf, temp)
1378*ef5ccd6cSJohn Marino     {
1379*ef5ccd6cSJohn Marino       struct jit_objfile_data *objf_data = objfile_data (objf,
1380*ef5ccd6cSJohn Marino 							 jit_objfile_data);
1381*ef5ccd6cSJohn Marino 
1382*ef5ccd6cSJohn Marino       if (objf_data != NULL && objf_data->addr != 0)
13835796c8dcSSimon Schubert 	jit_unregister_code (objf);
13845796c8dcSSimon Schubert     }
1385c50c785cSJohn Marino }
1386c50c785cSJohn Marino 
13875796c8dcSSimon Schubert void
jit_event_handler(struct gdbarch * gdbarch)13885796c8dcSSimon Schubert jit_event_handler (struct gdbarch *gdbarch)
13895796c8dcSSimon Schubert {
13905796c8dcSSimon Schubert   struct jit_descriptor descriptor;
13915796c8dcSSimon Schubert   struct jit_code_entry code_entry;
13925796c8dcSSimon Schubert   CORE_ADDR entry_addr;
13935796c8dcSSimon Schubert   struct objfile *objf;
13945796c8dcSSimon Schubert 
13955796c8dcSSimon Schubert   /* Read the descriptor from remote memory.  */
1396*ef5ccd6cSJohn Marino   if (!jit_read_descriptor (gdbarch, &descriptor,
1397*ef5ccd6cSJohn Marino 			    get_jit_program_space_data ()))
1398*ef5ccd6cSJohn Marino     return;
13995796c8dcSSimon Schubert   entry_addr = descriptor.relevant_entry;
14005796c8dcSSimon Schubert 
14015796c8dcSSimon Schubert   /* Do the corresponding action.  */
14025796c8dcSSimon Schubert   switch (descriptor.action_flag)
14035796c8dcSSimon Schubert     {
14045796c8dcSSimon Schubert     case JIT_NOACTION:
14055796c8dcSSimon Schubert       break;
14065796c8dcSSimon Schubert     case JIT_REGISTER:
14075796c8dcSSimon Schubert       jit_read_code_entry (gdbarch, entry_addr, &code_entry);
14085796c8dcSSimon Schubert       jit_register_code (gdbarch, entry_addr, &code_entry);
14095796c8dcSSimon Schubert       break;
14105796c8dcSSimon Schubert     case JIT_UNREGISTER:
14115796c8dcSSimon Schubert       objf = jit_find_objf_with_entry_addr (entry_addr);
14125796c8dcSSimon Schubert       if (objf == NULL)
1413c50c785cSJohn Marino 	printf_unfiltered (_("Unable to find JITed code "
1414c50c785cSJohn Marino 			     "entry at address: %s\n"),
14155796c8dcSSimon Schubert 			   paddress (gdbarch, entry_addr));
14165796c8dcSSimon Schubert       else
14175796c8dcSSimon Schubert         jit_unregister_code (objf);
14185796c8dcSSimon Schubert 
14195796c8dcSSimon Schubert       break;
14205796c8dcSSimon Schubert     default:
14215796c8dcSSimon Schubert       error (_("Unknown action_flag value in JIT descriptor!"));
14225796c8dcSSimon Schubert       break;
14235796c8dcSSimon Schubert     }
14245796c8dcSSimon Schubert }
14255796c8dcSSimon Schubert 
1426*ef5ccd6cSJohn Marino /* Called to free the data allocated to the jit_program_space_data slot.  */
1427a45ae5f8SJohn Marino 
1428a45ae5f8SJohn Marino static void
free_objfile_data(struct objfile * objfile,void * data)1429a45ae5f8SJohn Marino free_objfile_data (struct objfile *objfile, void *data)
1430a45ae5f8SJohn Marino {
1431*ef5ccd6cSJohn Marino   struct jit_objfile_data *objf_data = data;
1432*ef5ccd6cSJohn Marino 
1433*ef5ccd6cSJohn Marino   if (objf_data->register_code != NULL)
1434*ef5ccd6cSJohn Marino     {
1435*ef5ccd6cSJohn Marino       struct jit_program_space_data *ps_data;
1436*ef5ccd6cSJohn Marino 
1437*ef5ccd6cSJohn Marino       ps_data = program_space_data (objfile->pspace, jit_program_space_data);
1438*ef5ccd6cSJohn Marino       if (ps_data != NULL && ps_data->objfile == objfile)
1439*ef5ccd6cSJohn Marino 	ps_data->objfile = NULL;
1440*ef5ccd6cSJohn Marino     }
1441*ef5ccd6cSJohn Marino 
1442a45ae5f8SJohn Marino   xfree (data);
1443a45ae5f8SJohn Marino }
1444a45ae5f8SJohn Marino 
1445a45ae5f8SJohn Marino /* Initialize the jit_gdbarch_data slot with an instance of struct
1446a45ae5f8SJohn Marino    jit_gdbarch_data_type */
1447a45ae5f8SJohn Marino 
1448a45ae5f8SJohn Marino static void *
jit_gdbarch_data_init(struct obstack * obstack)1449a45ae5f8SJohn Marino jit_gdbarch_data_init (struct obstack *obstack)
1450a45ae5f8SJohn Marino {
1451a45ae5f8SJohn Marino   struct jit_gdbarch_data_type *data;
1452a45ae5f8SJohn Marino 
1453a45ae5f8SJohn Marino   data = obstack_alloc (obstack, sizeof (struct jit_gdbarch_data_type));
1454a45ae5f8SJohn Marino   data->unwinder_registered = 0;
1455a45ae5f8SJohn Marino   return data;
1456a45ae5f8SJohn Marino }
1457a45ae5f8SJohn Marino 
14585796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
14595796c8dcSSimon Schubert 
14605796c8dcSSimon Schubert extern void _initialize_jit (void);
14615796c8dcSSimon Schubert 
14625796c8dcSSimon Schubert void
_initialize_jit(void)14635796c8dcSSimon Schubert _initialize_jit (void)
14645796c8dcSSimon Schubert {
1465a45ae5f8SJohn Marino   jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR,
1466a45ae5f8SJohn Marino                                            JIT_READER_DIR_RELOCATABLE);
1467*ef5ccd6cSJohn Marino   add_setshow_zuinteger_cmd ("jit", class_maintenance, &jit_debug,
1468c50c785cSJohn Marino 			     _("Set JIT debugging."),
1469c50c785cSJohn Marino 			     _("Show JIT debugging."),
1470c50c785cSJohn Marino 			     _("When non-zero, JIT debugging is enabled."),
1471c50c785cSJohn Marino 			     NULL,
1472c50c785cSJohn Marino 			     show_jit_debug,
1473c50c785cSJohn Marino 			     &setdebuglist, &showdebuglist);
1474c50c785cSJohn Marino 
14755796c8dcSSimon Schubert   observer_attach_inferior_exit (jit_inferior_exit_hook);
1476*ef5ccd6cSJohn Marino   observer_attach_breakpoint_deleted (jit_breakpoint_deleted);
1477*ef5ccd6cSJohn Marino 
1478a45ae5f8SJohn Marino   jit_objfile_data =
1479a45ae5f8SJohn Marino     register_objfile_data_with_cleanup (NULL, free_objfile_data);
1480*ef5ccd6cSJohn Marino   jit_program_space_data =
1481*ef5ccd6cSJohn Marino     register_program_space_data_with_cleanup (NULL,
1482*ef5ccd6cSJohn Marino 					      jit_program_space_data_cleanup);
1483a45ae5f8SJohn Marino   jit_gdbarch_data = gdbarch_data_register_pre_init (jit_gdbarch_data_init);
1484a45ae5f8SJohn Marino   if (is_dl_available ())
1485a45ae5f8SJohn Marino     {
1486a45ae5f8SJohn Marino       add_com ("jit-reader-load", no_class, jit_reader_load_command, _("\
1487a45ae5f8SJohn Marino Load FILE as debug info reader and unwinder for JIT compiled code.\n\
1488a45ae5f8SJohn Marino Usage: jit-reader-load FILE\n\
1489a45ae5f8SJohn Marino Try to load file FILE as a debug info reader (and unwinder) for\n\
1490a45ae5f8SJohn Marino JIT compiled code.  The file is loaded from " JIT_READER_DIR ",\n\
1491a45ae5f8SJohn Marino relocated relative to the GDB executable if required."));
1492a45ae5f8SJohn Marino       add_com ("jit-reader-unload", no_class, jit_reader_unload_command, _("\
1493a45ae5f8SJohn Marino Unload the currently loaded JIT debug info reader.\n\
1494a45ae5f8SJohn Marino Usage: jit-reader-unload FILE\n\n\
1495a45ae5f8SJohn Marino Do \"help jit-reader-load\" for info on loading debug info readers."));
1496a45ae5f8SJohn Marino     }
14975796c8dcSSimon Schubert }
1498