xref: /dflybsd-src/contrib/gdb-7/gdb/jit.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 2009
4*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    This file is part of GDB.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
9*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
10*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
11*5796c8dcSSimon Schubert    (at your option) any later version.
12*5796c8dcSSimon Schubert 
13*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
14*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*5796c8dcSSimon Schubert    GNU General Public License for more details.
17*5796c8dcSSimon Schubert 
18*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
19*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20*5796c8dcSSimon Schubert 
21*5796c8dcSSimon Schubert #include "defs.h"
22*5796c8dcSSimon Schubert 
23*5796c8dcSSimon Schubert #include "jit.h"
24*5796c8dcSSimon Schubert #include "breakpoint.h"
25*5796c8dcSSimon Schubert #include "gdbcore.h"
26*5796c8dcSSimon Schubert #include "observer.h"
27*5796c8dcSSimon Schubert #include "objfiles.h"
28*5796c8dcSSimon Schubert #include "symfile.h"
29*5796c8dcSSimon Schubert #include "symtab.h"
30*5796c8dcSSimon Schubert #include "target.h"
31*5796c8dcSSimon Schubert #include "gdb_stat.h"
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert static const struct objfile_data *jit_objfile_data;
34*5796c8dcSSimon Schubert 
35*5796c8dcSSimon Schubert static const char *const jit_break_name = "__jit_debug_register_code";
36*5796c8dcSSimon Schubert 
37*5796c8dcSSimon Schubert static const char *const jit_descriptor_name = "__jit_debug_descriptor";
38*5796c8dcSSimon Schubert 
39*5796c8dcSSimon Schubert /* This is the address of the JIT descriptor in the inferior.  */
40*5796c8dcSSimon Schubert 
41*5796c8dcSSimon Schubert static CORE_ADDR jit_descriptor_addr = 0;
42*5796c8dcSSimon Schubert 
43*5796c8dcSSimon Schubert /* This is a boolean indicating whether we're currently registering code.  This
44*5796c8dcSSimon Schubert    is used to avoid re-entering the registration code.  We want to check for
45*5796c8dcSSimon Schubert    new JITed every time a new object file is loaded, but we want to avoid
46*5796c8dcSSimon Schubert    checking for new code while we're registering object files for JITed code.
47*5796c8dcSSimon Schubert    Therefore, we flip this variable to 1 before registering new object files,
48*5796c8dcSSimon Schubert    and set it to 0 before returning.  */
49*5796c8dcSSimon Schubert 
50*5796c8dcSSimon Schubert static int registering_code = 0;
51*5796c8dcSSimon Schubert 
52*5796c8dcSSimon Schubert /* Helper cleanup function to clear an integer flag like the one above.  */
53*5796c8dcSSimon Schubert 
54*5796c8dcSSimon Schubert static void
55*5796c8dcSSimon Schubert clear_int (void *int_addr)
56*5796c8dcSSimon Schubert {
57*5796c8dcSSimon Schubert   *((int *) int_addr) = 0;
58*5796c8dcSSimon Schubert }
59*5796c8dcSSimon Schubert 
60*5796c8dcSSimon Schubert struct target_buffer
61*5796c8dcSSimon Schubert {
62*5796c8dcSSimon Schubert   CORE_ADDR base;
63*5796c8dcSSimon Schubert   size_t size;
64*5796c8dcSSimon Schubert };
65*5796c8dcSSimon Schubert 
66*5796c8dcSSimon Schubert /* Openning the file is a no-op.  */
67*5796c8dcSSimon Schubert 
68*5796c8dcSSimon Schubert static void *
69*5796c8dcSSimon Schubert mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
70*5796c8dcSSimon Schubert {
71*5796c8dcSSimon Schubert   return open_closure;
72*5796c8dcSSimon Schubert }
73*5796c8dcSSimon Schubert 
74*5796c8dcSSimon Schubert /* Closing the file is just freeing the base/size pair on our side.  */
75*5796c8dcSSimon Schubert 
76*5796c8dcSSimon Schubert static int
77*5796c8dcSSimon Schubert mem_bfd_iovec_close (struct bfd *abfd, void *stream)
78*5796c8dcSSimon Schubert {
79*5796c8dcSSimon Schubert   xfree (stream);
80*5796c8dcSSimon Schubert   return 1;
81*5796c8dcSSimon Schubert }
82*5796c8dcSSimon Schubert 
83*5796c8dcSSimon Schubert /* For reading the file, we just need to pass through to target_read_memory and
84*5796c8dcSSimon Schubert    fix up the arguments and return values.  */
85*5796c8dcSSimon Schubert 
86*5796c8dcSSimon Schubert static file_ptr
87*5796c8dcSSimon Schubert mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
88*5796c8dcSSimon Schubert                      file_ptr nbytes, file_ptr offset)
89*5796c8dcSSimon Schubert {
90*5796c8dcSSimon Schubert   int err;
91*5796c8dcSSimon Schubert   struct target_buffer *buffer = (struct target_buffer *) stream;
92*5796c8dcSSimon Schubert 
93*5796c8dcSSimon Schubert   /* If this read will read all of the file, limit it to just the rest.  */
94*5796c8dcSSimon Schubert   if (offset + nbytes > buffer->size)
95*5796c8dcSSimon Schubert     nbytes = buffer->size - offset;
96*5796c8dcSSimon Schubert 
97*5796c8dcSSimon Schubert   /* If there are no more bytes left, we've reached EOF.  */
98*5796c8dcSSimon Schubert   if (nbytes == 0)
99*5796c8dcSSimon Schubert     return 0;
100*5796c8dcSSimon Schubert 
101*5796c8dcSSimon Schubert   err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
102*5796c8dcSSimon Schubert   if (err)
103*5796c8dcSSimon Schubert     return -1;
104*5796c8dcSSimon Schubert 
105*5796c8dcSSimon Schubert   return nbytes;
106*5796c8dcSSimon Schubert }
107*5796c8dcSSimon Schubert 
108*5796c8dcSSimon Schubert /* For statting the file, we only support the st_size attribute.  */
109*5796c8dcSSimon Schubert 
110*5796c8dcSSimon Schubert static int
111*5796c8dcSSimon Schubert mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
112*5796c8dcSSimon Schubert {
113*5796c8dcSSimon Schubert   struct target_buffer *buffer = (struct target_buffer*) stream;
114*5796c8dcSSimon Schubert 
115*5796c8dcSSimon Schubert   sb->st_size = buffer->size;
116*5796c8dcSSimon Schubert   return 0;
117*5796c8dcSSimon Schubert }
118*5796c8dcSSimon Schubert 
119*5796c8dcSSimon Schubert /* Open a BFD from the target's memory.  */
120*5796c8dcSSimon Schubert 
121*5796c8dcSSimon Schubert static struct bfd *
122*5796c8dcSSimon Schubert bfd_open_from_target_memory (CORE_ADDR addr, size_t size, char *target)
123*5796c8dcSSimon Schubert {
124*5796c8dcSSimon Schubert   const char *filename = xstrdup ("<in-memory>");
125*5796c8dcSSimon Schubert   struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
126*5796c8dcSSimon Schubert 
127*5796c8dcSSimon Schubert   buffer->base = addr;
128*5796c8dcSSimon Schubert   buffer->size = size;
129*5796c8dcSSimon Schubert   return bfd_openr_iovec (filename, target,
130*5796c8dcSSimon Schubert                           mem_bfd_iovec_open,
131*5796c8dcSSimon Schubert                           buffer,
132*5796c8dcSSimon Schubert                           mem_bfd_iovec_pread,
133*5796c8dcSSimon Schubert                           mem_bfd_iovec_close,
134*5796c8dcSSimon Schubert                           mem_bfd_iovec_stat);
135*5796c8dcSSimon Schubert }
136*5796c8dcSSimon Schubert 
137*5796c8dcSSimon Schubert /* Helper function for reading the global JIT descriptor from remote memory.  */
138*5796c8dcSSimon Schubert 
139*5796c8dcSSimon Schubert static void
140*5796c8dcSSimon Schubert jit_read_descriptor (struct gdbarch *gdbarch,
141*5796c8dcSSimon Schubert 		     struct jit_descriptor *descriptor)
142*5796c8dcSSimon Schubert {
143*5796c8dcSSimon Schubert   int err;
144*5796c8dcSSimon Schubert   struct type *ptr_type;
145*5796c8dcSSimon Schubert   int ptr_size;
146*5796c8dcSSimon Schubert   int desc_size;
147*5796c8dcSSimon Schubert   gdb_byte *desc_buf;
148*5796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
149*5796c8dcSSimon Schubert 
150*5796c8dcSSimon Schubert   /* Figure out how big the descriptor is on the remote and how to read it.  */
151*5796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
152*5796c8dcSSimon Schubert   ptr_size = TYPE_LENGTH (ptr_type);
153*5796c8dcSSimon Schubert   desc_size = 8 + 2 * ptr_size;  /* Two 32-bit ints and two pointers.  */
154*5796c8dcSSimon Schubert   desc_buf = alloca (desc_size);
155*5796c8dcSSimon Schubert 
156*5796c8dcSSimon Schubert   /* Read the descriptor.  */
157*5796c8dcSSimon Schubert   err = target_read_memory (jit_descriptor_addr, desc_buf, desc_size);
158*5796c8dcSSimon Schubert   if (err)
159*5796c8dcSSimon Schubert     error (_("Unable to read JIT descriptor from remote memory!"));
160*5796c8dcSSimon Schubert 
161*5796c8dcSSimon Schubert   /* Fix the endianness to match the host.  */
162*5796c8dcSSimon Schubert   descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
163*5796c8dcSSimon Schubert   descriptor->action_flag =
164*5796c8dcSSimon Schubert       extract_unsigned_integer (&desc_buf[4], 4, byte_order);
165*5796c8dcSSimon Schubert   descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
166*5796c8dcSSimon Schubert   descriptor->first_entry =
167*5796c8dcSSimon Schubert       extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
168*5796c8dcSSimon Schubert }
169*5796c8dcSSimon Schubert 
170*5796c8dcSSimon Schubert /* Helper function for reading a JITed code entry from remote memory.  */
171*5796c8dcSSimon Schubert 
172*5796c8dcSSimon Schubert static void
173*5796c8dcSSimon Schubert jit_read_code_entry (struct gdbarch *gdbarch,
174*5796c8dcSSimon Schubert 		     CORE_ADDR code_addr, struct jit_code_entry *code_entry)
175*5796c8dcSSimon Schubert {
176*5796c8dcSSimon Schubert   int err;
177*5796c8dcSSimon Schubert   struct type *ptr_type;
178*5796c8dcSSimon Schubert   int ptr_size;
179*5796c8dcSSimon Schubert   int entry_size;
180*5796c8dcSSimon Schubert   gdb_byte *entry_buf;
181*5796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
182*5796c8dcSSimon Schubert 
183*5796c8dcSSimon Schubert   /* Figure out how big the entry is on the remote and how to read it.  */
184*5796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
185*5796c8dcSSimon Schubert   ptr_size = TYPE_LENGTH (ptr_type);
186*5796c8dcSSimon Schubert   entry_size = 3 * ptr_size + 8;  /* Three pointers and one 64-bit int.  */
187*5796c8dcSSimon Schubert   entry_buf = alloca (entry_size);
188*5796c8dcSSimon Schubert 
189*5796c8dcSSimon Schubert   /* Read the entry.  */
190*5796c8dcSSimon Schubert   err = target_read_memory (code_addr, entry_buf, entry_size);
191*5796c8dcSSimon Schubert   if (err)
192*5796c8dcSSimon Schubert     error (_("Unable to read JIT code entry from remote memory!"));
193*5796c8dcSSimon Schubert 
194*5796c8dcSSimon Schubert   /* Fix the endianness to match the host.  */
195*5796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
196*5796c8dcSSimon Schubert   code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
197*5796c8dcSSimon Schubert   code_entry->prev_entry =
198*5796c8dcSSimon Schubert       extract_typed_address (&entry_buf[ptr_size], ptr_type);
199*5796c8dcSSimon Schubert   code_entry->symfile_addr =
200*5796c8dcSSimon Schubert       extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
201*5796c8dcSSimon Schubert   code_entry->symfile_size =
202*5796c8dcSSimon Schubert       extract_unsigned_integer (&entry_buf[3 * ptr_size], 8, byte_order);
203*5796c8dcSSimon Schubert }
204*5796c8dcSSimon Schubert 
205*5796c8dcSSimon Schubert /* This function registers code associated with a JIT code entry.  It uses the
206*5796c8dcSSimon Schubert    pointer and size pair in the entry to read the symbol file from the remote
207*5796c8dcSSimon Schubert    and then calls symbol_file_add_from_local_memory to add it as though it were
208*5796c8dcSSimon Schubert    a symbol file added by the user.  */
209*5796c8dcSSimon Schubert 
210*5796c8dcSSimon Schubert static void
211*5796c8dcSSimon Schubert jit_register_code (struct gdbarch *gdbarch,
212*5796c8dcSSimon Schubert 		   CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
213*5796c8dcSSimon Schubert {
214*5796c8dcSSimon Schubert   bfd *nbfd;
215*5796c8dcSSimon Schubert   struct section_addr_info *sai;
216*5796c8dcSSimon Schubert   struct bfd_section *sec;
217*5796c8dcSSimon Schubert   struct objfile *objfile;
218*5796c8dcSSimon Schubert   struct cleanup *old_cleanups, *my_cleanups;
219*5796c8dcSSimon Schubert   int i;
220*5796c8dcSSimon Schubert   const struct bfd_arch_info *b;
221*5796c8dcSSimon Schubert   CORE_ADDR *entry_addr_ptr;
222*5796c8dcSSimon Schubert 
223*5796c8dcSSimon Schubert   nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
224*5796c8dcSSimon Schubert                                       code_entry->symfile_size, gnutarget);
225*5796c8dcSSimon Schubert   old_cleanups = make_cleanup_bfd_close (nbfd);
226*5796c8dcSSimon Schubert 
227*5796c8dcSSimon Schubert   /* Check the format.  NOTE: This initializes important data that GDB uses!
228*5796c8dcSSimon Schubert      We would segfault later without this line.  */
229*5796c8dcSSimon Schubert   if (!bfd_check_format (nbfd, bfd_object))
230*5796c8dcSSimon Schubert     {
231*5796c8dcSSimon Schubert       printf_unfiltered (_("\
232*5796c8dcSSimon Schubert JITed symbol file is not an object file, ignoring it.\n"));
233*5796c8dcSSimon Schubert       do_cleanups (old_cleanups);
234*5796c8dcSSimon Schubert       return;
235*5796c8dcSSimon Schubert     }
236*5796c8dcSSimon Schubert 
237*5796c8dcSSimon Schubert   /* Check bfd arch.  */
238*5796c8dcSSimon Schubert   b = gdbarch_bfd_arch_info (gdbarch);
239*5796c8dcSSimon Schubert   if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
240*5796c8dcSSimon Schubert     warning (_("JITed object file architecture %s is not compatible "
241*5796c8dcSSimon Schubert                "with target architecture %s."), bfd_get_arch_info
242*5796c8dcSSimon Schubert              (nbfd)->printable_name, b->printable_name);
243*5796c8dcSSimon Schubert 
244*5796c8dcSSimon Schubert   /* Read the section address information out of the symbol file.  Since the
245*5796c8dcSSimon Schubert      file is generated by the JIT at runtime, it should all of the absolute
246*5796c8dcSSimon Schubert      addresses that we care about.  */
247*5796c8dcSSimon Schubert   sai = alloc_section_addr_info (bfd_count_sections (nbfd));
248*5796c8dcSSimon Schubert   make_cleanup_free_section_addr_info (sai);
249*5796c8dcSSimon Schubert   i = 0;
250*5796c8dcSSimon Schubert   for (sec = nbfd->sections; sec != NULL; sec = sec->next)
251*5796c8dcSSimon Schubert     if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
252*5796c8dcSSimon Schubert       {
253*5796c8dcSSimon Schubert         /* We assume that these virtual addresses are absolute, and do not
254*5796c8dcSSimon Schubert            treat them as offsets.  */
255*5796c8dcSSimon Schubert         sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
256*5796c8dcSSimon Schubert         sai->other[i].name = (char *) bfd_get_section_name (nbfd, sec);
257*5796c8dcSSimon Schubert         sai->other[i].sectindex = sec->index;
258*5796c8dcSSimon Schubert         ++i;
259*5796c8dcSSimon Schubert       }
260*5796c8dcSSimon Schubert 
261*5796c8dcSSimon Schubert   /* Raise this flag while we register code so we won't trigger any
262*5796c8dcSSimon Schubert      re-registration.  */
263*5796c8dcSSimon Schubert   registering_code = 1;
264*5796c8dcSSimon Schubert   my_cleanups = make_cleanup (clear_int, &registering_code);
265*5796c8dcSSimon Schubert 
266*5796c8dcSSimon Schubert   /* This call takes ownership of sai.  */
267*5796c8dcSSimon Schubert   objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED);
268*5796c8dcSSimon Schubert 
269*5796c8dcSSimon Schubert   /* Clear the registering_code flag.  */
270*5796c8dcSSimon Schubert   do_cleanups (my_cleanups);
271*5796c8dcSSimon Schubert 
272*5796c8dcSSimon Schubert   /* Remember a mapping from entry_addr to objfile.  */
273*5796c8dcSSimon Schubert   entry_addr_ptr = xmalloc (sizeof (CORE_ADDR));
274*5796c8dcSSimon Schubert   *entry_addr_ptr = entry_addr;
275*5796c8dcSSimon Schubert   set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr);
276*5796c8dcSSimon Schubert 
277*5796c8dcSSimon Schubert   discard_cleanups (old_cleanups);
278*5796c8dcSSimon Schubert }
279*5796c8dcSSimon Schubert 
280*5796c8dcSSimon Schubert /* This function unregisters JITed code and frees the corresponding objfile.  */
281*5796c8dcSSimon Schubert 
282*5796c8dcSSimon Schubert static void
283*5796c8dcSSimon Schubert jit_unregister_code (struct objfile *objfile)
284*5796c8dcSSimon Schubert {
285*5796c8dcSSimon Schubert   free_objfile (objfile);
286*5796c8dcSSimon Schubert }
287*5796c8dcSSimon Schubert 
288*5796c8dcSSimon Schubert /* Look up the objfile with this code entry address.  */
289*5796c8dcSSimon Schubert 
290*5796c8dcSSimon Schubert static struct objfile *
291*5796c8dcSSimon Schubert jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
292*5796c8dcSSimon Schubert {
293*5796c8dcSSimon Schubert   struct objfile *objf;
294*5796c8dcSSimon Schubert   CORE_ADDR *objf_entry_addr;
295*5796c8dcSSimon Schubert 
296*5796c8dcSSimon Schubert   ALL_OBJFILES (objf)
297*5796c8dcSSimon Schubert     {
298*5796c8dcSSimon Schubert       objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data);
299*5796c8dcSSimon Schubert       if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr)
300*5796c8dcSSimon Schubert         return objf;
301*5796c8dcSSimon Schubert     }
302*5796c8dcSSimon Schubert   return NULL;
303*5796c8dcSSimon Schubert }
304*5796c8dcSSimon Schubert 
305*5796c8dcSSimon Schubert /* (Re-)Initialize the jit breakpoint handler, and register any already
306*5796c8dcSSimon Schubert    created translations.  */
307*5796c8dcSSimon Schubert 
308*5796c8dcSSimon Schubert static void
309*5796c8dcSSimon Schubert jit_inferior_init (struct gdbarch *gdbarch)
310*5796c8dcSSimon Schubert {
311*5796c8dcSSimon Schubert   struct minimal_symbol *reg_symbol;
312*5796c8dcSSimon Schubert   struct minimal_symbol *desc_symbol;
313*5796c8dcSSimon Schubert   CORE_ADDR reg_addr;
314*5796c8dcSSimon Schubert   struct jit_descriptor descriptor;
315*5796c8dcSSimon Schubert   struct jit_code_entry cur_entry;
316*5796c8dcSSimon Schubert   CORE_ADDR cur_entry_addr;
317*5796c8dcSSimon Schubert   struct cleanup *old_cleanups;
318*5796c8dcSSimon Schubert 
319*5796c8dcSSimon Schubert   /* When we register code, GDB resets its breakpoints in case symbols have
320*5796c8dcSSimon Schubert      changed.  That in turn calls this handler, which makes us look for new
321*5796c8dcSSimon Schubert      code again.  To avoid being re-entered, we check this flag.  */
322*5796c8dcSSimon Schubert   if (registering_code)
323*5796c8dcSSimon Schubert     return;
324*5796c8dcSSimon Schubert 
325*5796c8dcSSimon Schubert   /* Lookup the registration symbol.  If it is missing, then we assume we are
326*5796c8dcSSimon Schubert      not attached to a JIT.  */
327*5796c8dcSSimon Schubert   reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL);
328*5796c8dcSSimon Schubert   if (reg_symbol == NULL)
329*5796c8dcSSimon Schubert     return;
330*5796c8dcSSimon Schubert   reg_addr = SYMBOL_VALUE_ADDRESS (reg_symbol);
331*5796c8dcSSimon Schubert   if (reg_addr == 0)
332*5796c8dcSSimon Schubert     return;
333*5796c8dcSSimon Schubert 
334*5796c8dcSSimon Schubert   /* Lookup the descriptor symbol and cache the addr.  If it is missing, we
335*5796c8dcSSimon Schubert      assume we are not attached to a JIT and return early.  */
336*5796c8dcSSimon Schubert   desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL);
337*5796c8dcSSimon Schubert   if (desc_symbol == NULL)
338*5796c8dcSSimon Schubert     return;
339*5796c8dcSSimon Schubert   jit_descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol);
340*5796c8dcSSimon Schubert   if (jit_descriptor_addr == 0)
341*5796c8dcSSimon Schubert     return;
342*5796c8dcSSimon Schubert 
343*5796c8dcSSimon Schubert   /* Read the descriptor so we can check the version number and load any already
344*5796c8dcSSimon Schubert      JITed functions.  */
345*5796c8dcSSimon Schubert   jit_read_descriptor (gdbarch, &descriptor);
346*5796c8dcSSimon Schubert 
347*5796c8dcSSimon Schubert   /* Check that the version number agrees with that we support.  */
348*5796c8dcSSimon Schubert   if (descriptor.version != 1)
349*5796c8dcSSimon Schubert     error (_("Unsupported JIT protocol version in descriptor!"));
350*5796c8dcSSimon Schubert 
351*5796c8dcSSimon Schubert   /* Put a breakpoint in the registration symbol.  */
352*5796c8dcSSimon Schubert   create_jit_event_breakpoint (gdbarch, reg_addr);
353*5796c8dcSSimon Schubert 
354*5796c8dcSSimon Schubert   /* If we've attached to a running program, we need to check the descriptor to
355*5796c8dcSSimon Schubert      register any functions that were already generated.  */
356*5796c8dcSSimon Schubert   for (cur_entry_addr = descriptor.first_entry;
357*5796c8dcSSimon Schubert        cur_entry_addr != 0;
358*5796c8dcSSimon Schubert        cur_entry_addr = cur_entry.next_entry)
359*5796c8dcSSimon Schubert     {
360*5796c8dcSSimon Schubert       jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
361*5796c8dcSSimon Schubert 
362*5796c8dcSSimon Schubert       /* This hook may be called many times during setup, so make sure we don't
363*5796c8dcSSimon Schubert          add the same symbol file twice.  */
364*5796c8dcSSimon Schubert       if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
365*5796c8dcSSimon Schubert         continue;
366*5796c8dcSSimon Schubert 
367*5796c8dcSSimon Schubert       jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
368*5796c8dcSSimon Schubert     }
369*5796c8dcSSimon Schubert }
370*5796c8dcSSimon Schubert 
371*5796c8dcSSimon Schubert /* Exported routine to call when an inferior has been created.  */
372*5796c8dcSSimon Schubert 
373*5796c8dcSSimon Schubert void
374*5796c8dcSSimon Schubert jit_inferior_created_hook (void)
375*5796c8dcSSimon Schubert {
376*5796c8dcSSimon Schubert   jit_inferior_init (target_gdbarch);
377*5796c8dcSSimon Schubert }
378*5796c8dcSSimon Schubert 
379*5796c8dcSSimon Schubert /* Exported routine to call to re-set the jit breakpoints,
380*5796c8dcSSimon Schubert    e.g. when a program is rerun.  */
381*5796c8dcSSimon Schubert 
382*5796c8dcSSimon Schubert void
383*5796c8dcSSimon Schubert jit_breakpoint_re_set (void)
384*5796c8dcSSimon Schubert {
385*5796c8dcSSimon Schubert   jit_inferior_init (target_gdbarch);
386*5796c8dcSSimon Schubert }
387*5796c8dcSSimon Schubert 
388*5796c8dcSSimon Schubert /* Wrapper to match the observer function pointer prototype.  */
389*5796c8dcSSimon Schubert 
390*5796c8dcSSimon Schubert static void
391*5796c8dcSSimon Schubert jit_inferior_created_observer (struct target_ops *objfile, int from_tty)
392*5796c8dcSSimon Schubert {
393*5796c8dcSSimon Schubert   jit_inferior_init (target_gdbarch);
394*5796c8dcSSimon Schubert }
395*5796c8dcSSimon Schubert 
396*5796c8dcSSimon Schubert /* This function cleans up any code entries left over when the inferior exits.
397*5796c8dcSSimon Schubert    We get left over code when the inferior exits without unregistering its code,
398*5796c8dcSSimon Schubert    for example when it crashes.  */
399*5796c8dcSSimon Schubert 
400*5796c8dcSSimon Schubert static void
401*5796c8dcSSimon Schubert jit_inferior_exit_hook (int pid)
402*5796c8dcSSimon Schubert {
403*5796c8dcSSimon Schubert   struct objfile *objf;
404*5796c8dcSSimon Schubert   struct objfile *temp;
405*5796c8dcSSimon Schubert 
406*5796c8dcSSimon Schubert   /* We need to reset the descriptor addr so that next time we load up the
407*5796c8dcSSimon Schubert      inferior we look for it again.  */
408*5796c8dcSSimon Schubert   jit_descriptor_addr = 0;
409*5796c8dcSSimon Schubert 
410*5796c8dcSSimon Schubert   ALL_OBJFILES_SAFE (objf, temp)
411*5796c8dcSSimon Schubert     if (objfile_data (objf, jit_objfile_data) != NULL)
412*5796c8dcSSimon Schubert       jit_unregister_code (objf);
413*5796c8dcSSimon Schubert }
414*5796c8dcSSimon Schubert 
415*5796c8dcSSimon Schubert void
416*5796c8dcSSimon Schubert jit_event_handler (struct gdbarch *gdbarch)
417*5796c8dcSSimon Schubert {
418*5796c8dcSSimon Schubert   struct jit_descriptor descriptor;
419*5796c8dcSSimon Schubert   struct jit_code_entry code_entry;
420*5796c8dcSSimon Schubert   CORE_ADDR entry_addr;
421*5796c8dcSSimon Schubert   struct objfile *objf;
422*5796c8dcSSimon Schubert 
423*5796c8dcSSimon Schubert   /* Read the descriptor from remote memory.  */
424*5796c8dcSSimon Schubert   jit_read_descriptor (gdbarch, &descriptor);
425*5796c8dcSSimon Schubert   entry_addr = descriptor.relevant_entry;
426*5796c8dcSSimon Schubert 
427*5796c8dcSSimon Schubert   /* Do the corresponding action. */
428*5796c8dcSSimon Schubert   switch (descriptor.action_flag)
429*5796c8dcSSimon Schubert     {
430*5796c8dcSSimon Schubert     case JIT_NOACTION:
431*5796c8dcSSimon Schubert       break;
432*5796c8dcSSimon Schubert     case JIT_REGISTER:
433*5796c8dcSSimon Schubert       jit_read_code_entry (gdbarch, entry_addr, &code_entry);
434*5796c8dcSSimon Schubert       jit_register_code (gdbarch, entry_addr, &code_entry);
435*5796c8dcSSimon Schubert       break;
436*5796c8dcSSimon Schubert     case JIT_UNREGISTER:
437*5796c8dcSSimon Schubert       objf = jit_find_objf_with_entry_addr (entry_addr);
438*5796c8dcSSimon Schubert       if (objf == NULL)
439*5796c8dcSSimon Schubert 	printf_unfiltered (_("Unable to find JITed code entry at address: %s\n"),
440*5796c8dcSSimon Schubert 			   paddress (gdbarch, entry_addr));
441*5796c8dcSSimon Schubert       else
442*5796c8dcSSimon Schubert         jit_unregister_code (objf);
443*5796c8dcSSimon Schubert 
444*5796c8dcSSimon Schubert       break;
445*5796c8dcSSimon Schubert     default:
446*5796c8dcSSimon Schubert       error (_("Unknown action_flag value in JIT descriptor!"));
447*5796c8dcSSimon Schubert       break;
448*5796c8dcSSimon Schubert     }
449*5796c8dcSSimon Schubert }
450*5796c8dcSSimon Schubert 
451*5796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
452*5796c8dcSSimon Schubert 
453*5796c8dcSSimon Schubert extern void _initialize_jit (void);
454*5796c8dcSSimon Schubert 
455*5796c8dcSSimon Schubert void
456*5796c8dcSSimon Schubert _initialize_jit (void)
457*5796c8dcSSimon Schubert {
458*5796c8dcSSimon Schubert   observer_attach_inferior_created (jit_inferior_created_observer);
459*5796c8dcSSimon Schubert   observer_attach_inferior_exit (jit_inferior_exit_hook);
460*5796c8dcSSimon Schubert   jit_objfile_data = register_objfile_data ();
461*5796c8dcSSimon Schubert }
462