1 /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "breakpoint.h"
33 static const struct objfile_data
*jit_objfile_data
;
35 static const char *const jit_break_name
= "__jit_debug_register_code";
37 static const char *const jit_descriptor_name
= "__jit_debug_descriptor";
39 /* This is the address of the JIT descriptor in the inferior. */
41 static CORE_ADDR jit_descriptor_addr
= 0;
43 /* This is a boolean indicating whether we're currently registering code. This
44 is used to avoid re-entering the registration code. We want to check for
45 new JITed every time a new object file is loaded, but we want to avoid
46 checking for new code while we're registering object files for JITed code.
47 Therefore, we flip this variable to 1 before registering new object files,
48 and set it to 0 before returning. */
50 static int registering_code
= 0;
52 /* Helper cleanup function to clear an integer flag like the one above. */
55 clear_int (void *int_addr
)
57 *((int *) int_addr
) = 0;
66 /* Openning the file is a no-op. */
69 mem_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
74 /* Closing the file is just freeing the base/size pair on our side. */
77 mem_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
83 /* For reading the file, we just need to pass through to target_read_memory and
84 fix up the arguments and return values. */
87 mem_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
88 file_ptr nbytes
, file_ptr offset
)
91 struct target_buffer
*buffer
= (struct target_buffer
*) stream
;
93 /* If this read will read all of the file, limit it to just the rest. */
94 if (offset
+ nbytes
> buffer
->size
)
95 nbytes
= buffer
->size
- offset
;
97 /* If there are no more bytes left, we've reached EOF. */
101 err
= target_read_memory (buffer
->base
+ offset
, (gdb_byte
*) buf
, nbytes
);
108 /* For statting the file, we only support the st_size attribute. */
111 mem_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
113 struct target_buffer
*buffer
= (struct target_buffer
*) stream
;
115 sb
->st_size
= buffer
->size
;
119 /* Open a BFD from the target's memory. */
122 bfd_open_from_target_memory (CORE_ADDR addr
, size_t size
, char *target
)
124 const char *filename
= xstrdup ("<in-memory>");
125 struct target_buffer
*buffer
= xmalloc (sizeof (struct target_buffer
));
129 return bfd_openr_iovec (filename
, target
,
137 /* Helper function for reading the global JIT descriptor from remote memory. */
140 jit_read_descriptor (struct jit_descriptor
*descriptor
)
143 struct type
*ptr_type
;
147 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
149 /* Figure out how big the descriptor is on the remote and how to read it. */
150 ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
151 ptr_size
= TYPE_LENGTH (ptr_type
);
152 desc_size
= 8 + 2 * ptr_size
; /* Two 32-bit ints and two pointers. */
153 desc_buf
= alloca (desc_size
);
155 /* Read the descriptor. */
156 err
= target_read_memory (jit_descriptor_addr
, desc_buf
, desc_size
);
158 error (_("Unable to read JIT descriptor from remote memory!"));
160 /* Fix the endianness to match the host. */
161 descriptor
->version
= extract_unsigned_integer (&desc_buf
[0], 4, byte_order
);
162 descriptor
->action_flag
=
163 extract_unsigned_integer (&desc_buf
[4], 4, byte_order
);
164 descriptor
->relevant_entry
= extract_typed_address (&desc_buf
[8], ptr_type
);
165 descriptor
->first_entry
=
166 extract_typed_address (&desc_buf
[8 + ptr_size
], ptr_type
);
169 /* Helper function for reading a JITed code entry from remote memory. */
172 jit_read_code_entry (CORE_ADDR code_addr
, struct jit_code_entry
*code_entry
)
175 struct type
*ptr_type
;
179 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
181 /* Figure out how big the entry is on the remote and how to read it. */
182 ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
183 ptr_size
= TYPE_LENGTH (ptr_type
);
184 entry_size
= 3 * ptr_size
+ 8; /* Three pointers and one 64-bit int. */
185 entry_buf
= alloca (entry_size
);
187 /* Read the entry. */
188 err
= target_read_memory (code_addr
, entry_buf
, entry_size
);
190 error (_("Unable to read JIT code entry from remote memory!"));
192 /* Fix the endianness to match the host. */
193 ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
194 code_entry
->next_entry
= extract_typed_address (&entry_buf
[0], ptr_type
);
195 code_entry
->prev_entry
=
196 extract_typed_address (&entry_buf
[ptr_size
], ptr_type
);
197 code_entry
->symfile_addr
=
198 extract_typed_address (&entry_buf
[2 * ptr_size
], ptr_type
);
199 code_entry
->symfile_size
=
200 extract_unsigned_integer (&entry_buf
[3 * ptr_size
], 8, byte_order
);
203 /* This function registers code associated with a JIT code entry. It uses the
204 pointer and size pair in the entry to read the symbol file from the remote
205 and then calls symbol_file_add_from_local_memory to add it as though it were
206 a symbol file added by the user. */
209 jit_register_code (CORE_ADDR entry_addr
, struct jit_code_entry
*code_entry
)
212 struct section_addr_info
*sai
;
213 struct bfd_section
*sec
;
214 struct objfile
*objfile
;
215 struct cleanup
*old_cleanups
, *my_cleanups
;
217 const struct bfd_arch_info
*b
;
218 CORE_ADDR
*entry_addr_ptr
;
220 nbfd
= bfd_open_from_target_memory (code_entry
->symfile_addr
,
221 code_entry
->symfile_size
, gnutarget
);
222 old_cleanups
= make_cleanup_bfd_close (nbfd
);
224 /* Check the format. NOTE: This initializes important data that GDB uses!
225 We would segfault later without this line. */
226 if (!bfd_check_format (nbfd
, bfd_object
))
228 printf_unfiltered (_("\
229 JITed symbol file is not an object file, ignoring it.\n"));
230 do_cleanups (old_cleanups
);
234 /* Check bfd arch. */
235 b
= gdbarch_bfd_arch_info (target_gdbarch
);
236 if (b
->compatible (b
, bfd_get_arch_info (nbfd
)) != b
)
237 warning (_("JITed object file architecture %s is not compatible "
238 "with target architecture %s."), bfd_get_arch_info
239 (nbfd
)->printable_name
, b
->printable_name
);
241 /* Read the section address information out of the symbol file. Since the
242 file is generated by the JIT at runtime, it should all of the absolute
243 addresses that we care about. */
244 sai
= alloc_section_addr_info (bfd_count_sections (nbfd
));
245 make_cleanup_free_section_addr_info (sai
);
247 for (sec
= nbfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
248 if ((bfd_get_section_flags (nbfd
, sec
) & (SEC_ALLOC
|SEC_LOAD
)) != 0)
250 /* We assume that these virtual addresses are absolute, and do not
251 treat them as offsets. */
252 sai
->other
[i
].addr
= bfd_get_section_vma (nbfd
, sec
);
253 sai
->other
[i
].name
= (char *) bfd_get_section_name (nbfd
, sec
);
254 sai
->other
[i
].sectindex
= sec
->index
;
258 /* Raise this flag while we register code so we won't trigger any
260 registering_code
= 1;
261 my_cleanups
= make_cleanup (clear_int
, ®istering_code
);
263 /* This call takes ownership of sai. */
264 objfile
= symbol_file_add_from_bfd (nbfd
, 0, sai
, OBJF_SHARED
);
266 /* Clear the registering_code flag. */
267 do_cleanups (my_cleanups
);
269 /* Remember a mapping from entry_addr to objfile. */
270 entry_addr_ptr
= xmalloc (sizeof (CORE_ADDR
));
271 *entry_addr_ptr
= entry_addr
;
272 set_objfile_data (objfile
, jit_objfile_data
, entry_addr_ptr
);
274 discard_cleanups (old_cleanups
);
277 /* This function unregisters JITed code and frees the corresponding objfile. */
280 jit_unregister_code (struct objfile
*objfile
)
282 free_objfile (objfile
);
285 /* Look up the objfile with this code entry address. */
287 static struct objfile
*
288 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr
)
290 struct objfile
*objf
;
291 CORE_ADDR
*objf_entry_addr
;
295 objf_entry_addr
= (CORE_ADDR
*) objfile_data (objf
, jit_objfile_data
);
296 if (objf_entry_addr
!= NULL
&& *objf_entry_addr
== entry_addr
)
303 jit_inferior_created_hook (void)
305 struct minimal_symbol
*reg_symbol
;
306 struct minimal_symbol
*desc_symbol
;
308 struct jit_descriptor descriptor
;
309 struct jit_code_entry cur_entry
;
310 CORE_ADDR cur_entry_addr
;
311 struct cleanup
*old_cleanups
;
313 /* When we register code, GDB resets its breakpoints in case symbols have
314 changed. That in turn calls this handler, which makes us look for new
315 code again. To avoid being re-entered, we check this flag. */
316 if (registering_code
)
319 /* Lookup the registration symbol. If it is missing, then we assume we are
320 not attached to a JIT. */
321 reg_symbol
= lookup_minimal_symbol (jit_break_name
, NULL
, NULL
);
322 if (reg_symbol
== NULL
)
324 reg_addr
= SYMBOL_VALUE_ADDRESS (reg_symbol
);
328 /* Lookup the descriptor symbol and cache the addr. If it is missing, we
329 assume we are not attached to a JIT and return early. */
330 desc_symbol
= lookup_minimal_symbol (jit_descriptor_name
, NULL
, NULL
);
331 if (desc_symbol
== NULL
)
333 jit_descriptor_addr
= SYMBOL_VALUE_ADDRESS (desc_symbol
);
334 if (jit_descriptor_addr
== 0)
337 /* Read the descriptor so we can check the version number and load any already
339 jit_read_descriptor (&descriptor
);
341 /* Check that the version number agrees with that we support. */
342 if (descriptor
.version
!= 1)
343 error (_("Unsupported JIT protocol version in descriptor!"));
345 /* Put a breakpoint in the registration symbol. */
346 create_jit_event_breakpoint (target_gdbarch
, reg_addr
);
348 /* If we've attached to a running program, we need to check the descriptor to
349 register any functions that were already generated. */
350 for (cur_entry_addr
= descriptor
.first_entry
;
352 cur_entry_addr
= cur_entry
.next_entry
)
354 jit_read_code_entry (cur_entry_addr
, &cur_entry
);
356 /* This hook may be called many times during setup, so make sure we don't
357 add the same symbol file twice. */
358 if (jit_find_objf_with_entry_addr (cur_entry_addr
) != NULL
)
361 jit_register_code (cur_entry_addr
, &cur_entry
);
365 /* Wrapper to match the observer function pointer prototype. */
368 jit_inferior_created_hook1 (struct target_ops
*objfile
, int from_tty
)
370 jit_inferior_created_hook ();
373 /* This function cleans up any code entries left over when the inferior exits.
374 We get left over code when the inferior exits without unregistering its code,
375 for example when it crashes. */
378 jit_inferior_exit_hook (int pid
)
380 struct objfile
*objf
;
381 struct objfile
*temp
;
383 /* We need to reset the descriptor addr so that next time we load up the
384 inferior we look for it again. */
385 jit_descriptor_addr
= 0;
387 ALL_OBJFILES_SAFE (objf
, temp
)
388 if (objfile_data (objf
, jit_objfile_data
) != NULL
)
389 jit_unregister_code (objf
);
393 jit_event_handler (void)
395 struct jit_descriptor descriptor
;
396 struct jit_code_entry code_entry
;
397 CORE_ADDR entry_addr
;
398 struct objfile
*objf
;
400 /* Read the descriptor from remote memory. */
401 jit_read_descriptor (&descriptor
);
402 entry_addr
= descriptor
.relevant_entry
;
404 /* Do the corresponding action. */
405 switch (descriptor
.action_flag
)
410 jit_read_code_entry (entry_addr
, &code_entry
);
411 jit_register_code (entry_addr
, &code_entry
);
414 objf
= jit_find_objf_with_entry_addr (entry_addr
);
416 printf_unfiltered ("Unable to find JITed code entry at address: %p\n",
417 (void *) entry_addr
);
419 jit_unregister_code (objf
);
423 error (_("Unknown action_flag value in JIT descriptor!"));
428 /* Provide a prototype to silence -Wmissing-prototypes. */
430 extern void _initialize_jit (void);
433 _initialize_jit (void)
435 observer_attach_inferior_created (jit_inferior_created_hook1
);
436 observer_attach_inferior_exit (jit_inferior_exit_hook
);
437 jit_objfile_data
= register_objfile_data ();