1 /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "extract-store-integer.h"
23 #include "jit-reader.h"
25 #include "breakpoint.h"
27 #include "dictionary.h"
28 #include "filenames.h"
29 #include "frame-unwind.h"
30 #include "cli/cli-cmds.h"
33 #include "observable.h"
39 #include "gdbsupport/gdb-dlfcn.h"
42 #include "readline/tilde.h"
43 #include "completer.h"
44 #include <forward_list>
46 static std::string jit_reader_dir
;
48 static const char jit_break_name
[] = "__jit_debug_register_code";
50 static const char jit_descriptor_name
[] = "__jit_debug_descriptor";
52 static void jit_inferior_created_hook (inferior
*inf
);
53 static void jit_inferior_exit_hook (struct inferior
*inf
);
55 /* True if we want to see trace of jit level stuff. */
57 static bool jit_debug
= false;
59 /* Print a "jit" debug statement. */
61 #define jit_debug_printf(fmt, ...) \
62 debug_prefixed_printf_cond (jit_debug, "jit", fmt, ##__VA_ARGS__)
65 show_jit_debug (struct ui_file
*file
, int from_tty
,
66 struct cmd_list_element
*c
, const char *value
)
68 gdb_printf (file
, _("JIT debugging is %s.\n"), value
);
71 /* Implementation of the "maintenance info jit" command. */
74 maint_info_jit_cmd (const char *args
, int from_tty
)
76 inferior
*inf
= current_inferior ();
77 bool printed_header
= false;
79 std::optional
<ui_out_emit_table
> table_emitter
;
81 /* Print a line for each JIT-ed objfile. */
82 for (objfile
*obj
: inf
->pspace
->objfiles ())
84 if (obj
->jited_data
== nullptr)
89 table_emitter
.emplace (current_uiout
, 3, -1, "jit-created-objfiles");
91 /* The +2 allows for the leading '0x', then one character for
93 int addr_width
= 2 + (gdbarch_ptr_bit (obj
->arch ()) / 4);
95 /* The std::max here selects between the width of an address (as
96 a string) and the width of the column header string. */
97 current_uiout
->table_header (std::max (addr_width
, 22), ui_left
,
98 "jit_code_entry-address",
99 "jit_code_entry address");
100 current_uiout
->table_header (std::max (addr_width
, 15), ui_left
,
101 "symfile-address", "symfile address");
102 current_uiout
->table_header (20, ui_left
,
103 "symfile-size", "symfile size");
104 current_uiout
->table_body ();
106 printed_header
= true;
109 ui_out_emit_tuple
tuple_emitter (current_uiout
, "jit-objfile");
111 current_uiout
->field_core_addr ("jit_code_entry-address", obj
->arch (),
112 obj
->jited_data
->addr
);
113 current_uiout
->field_core_addr ("symfile-address", obj
->arch (),
114 obj
->jited_data
->symfile_addr
);
115 current_uiout
->field_unsigned ("symfile-size",
116 obj
->jited_data
->symfile_size
);
117 current_uiout
->text ("\n");
123 jit_reader (struct gdb_reader_funcs
*f
, gdb_dlhandle_up
&&h
)
124 : functions (f
), handle (std::move (h
))
130 functions
->destroy (functions
);
133 DISABLE_COPY_AND_ASSIGN (jit_reader
);
135 struct gdb_reader_funcs
*functions
;
136 gdb_dlhandle_up handle
;
139 /* One reader that has been loaded successfully, and can potentially be used to
142 static struct jit_reader
*loaded_jit_reader
= NULL
;
144 typedef struct gdb_reader_funcs
* (reader_init_fn_type
) (void);
145 static const char reader_init_fn_sym
[] = "gdb_init_reader";
147 /* Try to load FILE_NAME as a JIT debug info reader. */
149 static struct jit_reader
*
150 jit_reader_load (const char *file_name
)
152 reader_init_fn_type
*init_fn
;
153 struct gdb_reader_funcs
*funcs
= NULL
;
155 jit_debug_printf ("Opening shared object %s", file_name
);
157 gdb_dlhandle_up so
= gdb_dlopen (file_name
);
159 init_fn
= (reader_init_fn_type
*) gdb_dlsym (so
, reader_init_fn_sym
);
161 error (_("Could not locate initialization function: %s."),
164 if (gdb_dlsym (so
, "plugin_is_GPL_compatible") == NULL
)
165 error (_("Reader not GPL compatible."));
168 if (funcs
->reader_version
!= GDB_READER_INTERFACE_VERSION
)
169 error (_("Reader version does not match GDB version."));
171 return new jit_reader (funcs
, std::move (so
));
174 /* Provides the jit-reader-load command. */
177 jit_reader_load_command (const char *args
, int from_tty
)
180 error (_("No reader name provided."));
181 gdb::unique_xmalloc_ptr
<char> file (tilde_expand (args
));
183 if (loaded_jit_reader
!= NULL
)
184 error (_("JIT reader already loaded. Run jit-reader-unload first."));
186 if (!IS_ABSOLUTE_PATH (file
.get ()))
187 file
= xstrprintf ("%s%s%s", jit_reader_dir
.c_str (),
188 SLASH_STRING
, file
.get ());
190 loaded_jit_reader
= jit_reader_load (file
.get ());
191 reinit_frame_cache ();
192 jit_inferior_created_hook (current_inferior ());
195 /* Provides the jit-reader-unload command. */
198 jit_reader_unload_command (const char *args
, int from_tty
)
200 if (!loaded_jit_reader
)
201 error (_("No JIT reader loaded."));
203 reinit_frame_cache ();
204 jit_inferior_exit_hook (current_inferior ());
206 delete loaded_jit_reader
;
207 loaded_jit_reader
= NULL
;
210 /* Destructor for jiter_objfile_data. */
212 jiter_objfile_data::~jiter_objfile_data ()
214 if (this->jit_breakpoint
!= nullptr)
215 delete_breakpoint (this->jit_breakpoint
);
218 /* Fetch the jiter_objfile_data associated with OBJF. If no data exists
219 yet, make a new structure and attach it. */
221 static jiter_objfile_data
*
222 get_jiter_objfile_data (objfile
*objf
)
224 if (objf
->jiter_data
== nullptr)
225 objf
->jiter_data
.reset (new jiter_objfile_data ());
227 return objf
->jiter_data
.get ();
230 /* Remember OBJFILE has been created for struct jit_code_entry located
231 at inferior address ENTRY. */
234 add_objfile_entry (struct objfile
*objfile
, CORE_ADDR entry
,
235 CORE_ADDR symfile_addr
, ULONGEST symfile_size
)
237 gdb_assert (objfile
->jited_data
== nullptr);
239 objfile
->jited_data
.reset (new jited_objfile_data (entry
, symfile_addr
,
243 /* Helper function for reading the global JIT descriptor from remote
244 memory. Returns true if all went well, false otherwise. */
247 jit_read_descriptor (gdbarch
*gdbarch
,
248 jit_descriptor
*descriptor
,
252 struct type
*ptr_type
;
256 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
258 gdb_assert (jiter
!= nullptr);
259 jiter_objfile_data
*objf_data
= jiter
->jiter_data
.get ();
260 gdb_assert (objf_data
!= nullptr);
262 CORE_ADDR addr
= objf_data
->descriptor
->value_address (jiter
);
264 jit_debug_printf ("descriptor_addr = %s", paddress (gdbarch
, addr
));
266 /* Figure out how big the descriptor is on the remote and how to read it. */
267 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
268 ptr_size
= ptr_type
->length ();
269 desc_size
= 8 + 2 * ptr_size
; /* Two 32-bit ints and two pointers. */
270 desc_buf
= (gdb_byte
*) alloca (desc_size
);
272 /* Read the descriptor. */
273 err
= target_read_memory (addr
, desc_buf
, desc_size
);
276 gdb_printf (gdb_stderr
, _("Unable to read JIT descriptor from "
281 /* Fix the endianness to match the host. */
282 descriptor
->version
= extract_unsigned_integer (&desc_buf
[0], 4, byte_order
);
283 descriptor
->action_flag
=
284 extract_unsigned_integer (&desc_buf
[4], 4, byte_order
);
285 descriptor
->relevant_entry
= extract_typed_address (&desc_buf
[8], ptr_type
);
286 descriptor
->first_entry
=
287 extract_typed_address (&desc_buf
[8 + ptr_size
], ptr_type
);
292 /* Helper function for reading a JITed code entry from remote memory. */
295 jit_read_code_entry (struct gdbarch
*gdbarch
,
296 CORE_ADDR code_addr
, struct jit_code_entry
*code_entry
)
299 struct type
*ptr_type
;
304 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
306 /* Figure out how big the entry is on the remote and how to read it. */
307 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
308 ptr_size
= ptr_type
->length ();
310 /* Figure out where the uint64_t value will be. */
311 align_bytes
= type_align (builtin_type (gdbarch
)->builtin_uint64
);
313 off
= (off
+ (align_bytes
- 1)) & ~(align_bytes
- 1);
315 entry_size
= off
+ 8; /* Three pointers and one 64-bit int. */
316 entry_buf
= (gdb_byte
*) alloca (entry_size
);
318 /* Read the entry. */
319 err
= target_read_memory (code_addr
, entry_buf
, entry_size
);
321 error (_("Unable to read JIT code entry from remote memory!"));
323 /* Fix the endianness to match the host. */
324 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
325 code_entry
->next_entry
= extract_typed_address (&entry_buf
[0], ptr_type
);
326 code_entry
->prev_entry
=
327 extract_typed_address (&entry_buf
[ptr_size
], ptr_type
);
328 code_entry
->symfile_addr
=
329 extract_typed_address (&entry_buf
[2 * ptr_size
], ptr_type
);
330 code_entry
->symfile_size
=
331 extract_unsigned_integer (&entry_buf
[off
], 8, byte_order
);
334 /* Proxy object for building a block. */
338 gdb_block (gdb_block
*parent
, CORE_ADDR begin
, CORE_ADDR end
,
343 name (name
!= nullptr ? xstrdup (name
) : nullptr)
346 /* The parent of this block. */
347 struct gdb_block
*parent
;
349 /* Points to the "real" block that is being built out of this
350 instance. This block will be added to a blockvector, which will
351 then be added to a symtab. */
352 struct block
*real_block
= nullptr;
354 /* The first and last code address corresponding to this block. */
355 CORE_ADDR begin
, end
;
357 /* The name of this block (if any). If this is non-NULL, the
358 FUNCTION symbol symbol is set to this value. */
359 gdb::unique_xmalloc_ptr
<char> name
;
362 /* Proxy object for building a symtab. */
366 explicit gdb_symtab (const char *file_name
)
367 : file_name (file_name
!= nullptr ? file_name
: "")
370 /* The list of blocks in this symtab. These will eventually be
371 converted to real blocks.
373 This is specifically a linked list, instead of, for example, a vector,
374 because the pointers are returned to the user's debug info reader. So
375 it's important that the objects don't change location during their
376 lifetime (which would happen with a vector of objects getting resized). */
377 std::forward_list
<gdb_block
> blocks
;
379 /* The number of blocks inserted. */
382 /* A mapping between line numbers to PC. */
383 gdb::unique_xmalloc_ptr
<struct linetable
> linetable
;
385 /* The source file for this symtab. */
386 std::string file_name
;
389 /* Proxy object for building an object. */
393 /* Symtabs of this object.
395 This is specifically a linked list, instead of, for example, a vector,
396 because the pointers are returned to the user's debug info reader. So
397 it's important that the objects don't change location during their
398 lifetime (which would happen with a vector of objects getting resized). */
399 std::forward_list
<gdb_symtab
> symtabs
;
402 /* The type of the `private' data passed around by the callback
405 struct jit_dbg_reader_data
407 /* Address of the jit_code_entry in the inferior's address space. */
408 CORE_ADDR entry_addr
;
410 /* The code entry, copied in our address space. */
411 const jit_code_entry
&entry
;
413 struct gdbarch
*gdbarch
;
416 /* The reader calls into this function to read data off the targets
419 static enum gdb_status
420 jit_target_read_impl (GDB_CORE_ADDR target_mem
, void *gdb_buf
, int len
)
422 int result
= target_read_memory ((CORE_ADDR
) target_mem
,
423 (gdb_byte
*) gdb_buf
, len
);
430 /* The reader calls into this function to create a new gdb_object
431 which it can then pass around to the other callbacks. Right now,
432 all that is required is allocating the memory. */
434 static struct gdb_object
*
435 jit_object_open_impl (struct gdb_symbol_callbacks
*cb
)
437 /* CB is not required right now, but sometime in the future we might
438 need a handle to it, and we'd like to do that without breaking
440 return new gdb_object
;
443 /* Readers call into this function to open a new gdb_symtab, which,
444 again, is passed around to other callbacks. */
446 static struct gdb_symtab
*
447 jit_symtab_open_impl (struct gdb_symbol_callbacks
*cb
,
448 struct gdb_object
*object
,
449 const char *file_name
)
451 /* CB stays unused. See comment in jit_object_open_impl. */
453 object
->symtabs
.emplace_front (file_name
);
454 return &object
->symtabs
.front ();
457 /* Called by readers to open a new gdb_block. This function also
458 inserts the new gdb_block in the correct place in the corresponding
461 static struct gdb_block
*
462 jit_block_open_impl (struct gdb_symbol_callbacks
*cb
,
463 struct gdb_symtab
*symtab
, struct gdb_block
*parent
,
464 GDB_CORE_ADDR begin
, GDB_CORE_ADDR end
, const char *name
)
466 /* Place the block at the beginning of the list, it will be sorted when the
467 symtab is finalized. */
468 symtab
->blocks
.emplace_front (parent
, begin
, end
, name
);
471 return &symtab
->blocks
.front ();
474 /* Readers call this to add a line mapping (from PC to line number) to
478 jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks
*cb
,
479 struct gdb_symtab
*stab
, int nlines
,
480 struct gdb_line_mapping
*map
)
488 alloc_len
= sizeof (struct linetable
)
489 + (nlines
- 1) * sizeof (struct linetable_entry
);
490 stab
->linetable
.reset (XNEWVAR (struct linetable
, alloc_len
));
491 stab
->linetable
->nitems
= nlines
;
492 for (i
= 0; i
< nlines
; i
++)
494 stab
->linetable
->item
[i
].set_unrelocated_pc
495 (unrelocated_addr (map
[i
].pc
));
496 stab
->linetable
->item
[i
].line
= map
[i
].line
;
497 stab
->linetable
->item
[i
].is_stmt
= true;
501 /* Called by readers to close a gdb_symtab. Does not need to do
502 anything as of now. */
505 jit_symtab_close_impl (struct gdb_symbol_callbacks
*cb
,
506 struct gdb_symtab
*stab
)
508 /* Right now nothing needs to be done here. We may need to do some
509 cleanup here in the future (again, without breaking the plugin
513 /* Transform STAB to a proper symtab, and add it it OBJFILE. */
516 finalize_symtab (struct gdb_symtab
*stab
, struct objfile
*objfile
)
518 struct compunit_symtab
*cust
;
519 size_t blockvector_size
;
520 CORE_ADDR begin
, end
;
521 struct blockvector
*bv
;
523 int actual_nblocks
= FIRST_LOCAL_BLOCK
+ stab
->nblocks
;
525 /* Sort the blocks in the order they should appear in the blockvector. */
526 stab
->blocks
.sort([] (const gdb_block
&a
, const gdb_block
&b
)
528 if (a
.begin
!= b
.begin
)
529 return a
.begin
< b
.begin
;
531 return a
.end
> b
.end
;
534 cust
= allocate_compunit_symtab (objfile
, stab
->file_name
.c_str ());
535 symtab
*filetab
= allocate_symtab (cust
, stab
->file_name
.c_str ());
536 add_compunit_symtab_to_objfile (cust
);
538 /* JIT compilers compile in memory. */
539 cust
->set_dirname (nullptr);
541 /* Copy over the linetable entry if one was provided. */
544 size_t size
= ((stab
->linetable
->nitems
- 1)
545 * sizeof (struct linetable_entry
)
546 + sizeof (struct linetable
));
547 struct linetable
*new_table
548 = (struct linetable
*) obstack_alloc (&objfile
->objfile_obstack
,
550 memcpy (new_table
, stab
->linetable
.get (), size
);
551 filetab
->set_linetable (new_table
);
554 blockvector_size
= (sizeof (struct blockvector
)
555 + (actual_nblocks
- 1) * sizeof (struct block
*));
556 bv
= (struct blockvector
*) obstack_alloc (&objfile
->objfile_obstack
,
558 cust
->set_blockvector (bv
);
560 /* At the end of this function, (begin, end) will contain the PC range this
561 entire blockvector spans. */
562 bv
->set_map (nullptr);
563 begin
= stab
->blocks
.front ().begin
;
564 end
= stab
->blocks
.front ().end
;
565 bv
->set_num_blocks (actual_nblocks
);
567 /* First run over all the gdb_block objects, creating a real block
568 object for each. Simultaneously, keep setting the real_block
570 int block_idx
= FIRST_LOCAL_BLOCK
;
571 for (gdb_block
&gdb_block_iter
: stab
->blocks
)
573 struct block
*new_block
= new (&objfile
->objfile_obstack
) block
;
574 struct symbol
*block_name
= new (&objfile
->objfile_obstack
) symbol
;
575 struct type
*block_type
= builtin_type (objfile
->arch ())->builtin_void
;
577 new_block
->set_multidict
578 (mdict_create_linear (&objfile
->objfile_obstack
, NULL
));
579 /* The address range. */
580 new_block
->set_start (gdb_block_iter
.begin
);
581 new_block
->set_end (gdb_block_iter
.end
);
584 block_name
->set_domain (FUNCTION_DOMAIN
);
585 block_name
->set_aclass_index (LOC_BLOCK
);
586 block_name
->set_symtab (filetab
);
587 block_name
->set_type (lookup_function_type (block_type
));
588 block_name
->set_value_block (new_block
);
590 block_name
->m_name
= obstack_strdup (&objfile
->objfile_obstack
,
591 gdb_block_iter
.name
.get ());
593 new_block
->set_function (block_name
);
595 bv
->set_block (block_idx
, new_block
);
596 if (begin
> new_block
->start ())
597 begin
= new_block
->start ();
598 if (end
< new_block
->end ())
599 end
= new_block
->end ();
601 gdb_block_iter
.real_block
= new_block
;
606 /* Now add the special blocks. */
607 struct block
*block_iter
= NULL
;
608 for (enum block_enum i
: { GLOBAL_BLOCK
, STATIC_BLOCK
})
610 struct block
*new_block
;
612 if (i
== GLOBAL_BLOCK
)
613 new_block
= new (&objfile
->objfile_obstack
) global_block
;
615 new_block
= new (&objfile
->objfile_obstack
) block
;
617 new_block
->set_multidict
618 (mdict_create_linear (&objfile
->objfile_obstack
, NULL
));
619 new_block
->set_superblock (block_iter
);
620 block_iter
= new_block
;
622 new_block
->set_start (begin
);
623 new_block
->set_end (end
);
625 bv
->set_block (i
, new_block
);
627 if (i
== GLOBAL_BLOCK
)
628 new_block
->as_global_block ()->set_compunit (cust
);
631 /* Fill up the superblock fields for the real blocks, using the
632 real_block fields populated earlier. */
633 for (gdb_block
&gdb_block_iter
: stab
->blocks
)
635 if (gdb_block_iter
.parent
!= NULL
)
637 /* If the plugin specifically mentioned a parent block, we
639 gdb_block_iter
.real_block
->set_superblock
640 (gdb_block_iter
.parent
->real_block
);
645 /* And if not, we set a default parent block. */
646 gdb_block_iter
.real_block
->set_superblock (bv
->static_block ());
651 /* Called when closing a gdb_objfile. Converts OBJ to a proper
655 jit_object_close_impl (struct gdb_symbol_callbacks
*cb
,
656 struct gdb_object
*obj
)
658 jit_dbg_reader_data
*priv_data
= (jit_dbg_reader_data
*) cb
->priv_data
;
659 std::string objfile_name
660 = string_printf ("<< JIT compiled code at %s >>",
661 paddress (priv_data
->gdbarch
,
662 priv_data
->entry
.symfile_addr
));
664 objfile
*objfile
= objfile::make (nullptr, current_program_space
,
665 objfile_name
.c_str (), OBJF_NOT_FILENAME
);
666 objfile
->per_bfd
->gdbarch
= priv_data
->gdbarch
;
668 for (gdb_symtab
&symtab
: obj
->symtabs
)
669 finalize_symtab (&symtab
, objfile
);
671 add_objfile_entry (objfile
, priv_data
->entry_addr
,
672 priv_data
->entry
.symfile_addr
,
673 priv_data
->entry
.symfile_size
);
678 /* Try to read CODE_ENTRY using the loaded jit reader (if any).
679 ENTRY_ADDR is the address of the struct jit_code_entry in the
680 inferior address space. */
683 jit_reader_try_read_symtab (gdbarch
*gdbarch
, jit_code_entry
*code_entry
,
684 CORE_ADDR entry_addr
)
687 jit_dbg_reader_data priv_data
693 struct gdb_reader_funcs
*funcs
;
694 struct gdb_symbol_callbacks callbacks
=
696 jit_object_open_impl
,
697 jit_symtab_open_impl
,
699 jit_symtab_close_impl
,
700 jit_object_close_impl
,
702 jit_symtab_line_mapping_add_impl
,
703 jit_target_read_impl
,
708 if (!loaded_jit_reader
)
711 gdb::byte_vector
gdb_mem (code_entry
->symfile_size
);
716 if (target_read_memory (code_entry
->symfile_addr
, gdb_mem
.data (),
717 code_entry
->symfile_size
))
720 catch (const gdb_exception_error
&e
)
727 funcs
= loaded_jit_reader
->functions
;
728 if (funcs
->read (funcs
, &callbacks
, gdb_mem
.data (),
729 code_entry
->symfile_size
)
735 jit_debug_printf ("Could not read symtab using the loaded JIT reader.");
740 /* Try to read CODE_ENTRY using BFD. ENTRY_ADDR is the address of the
741 struct jit_code_entry in the inferior address space. */
744 jit_bfd_try_read_symtab (struct jit_code_entry
*code_entry
,
745 CORE_ADDR entry_addr
,
746 struct gdbarch
*gdbarch
)
748 struct bfd_section
*sec
;
749 struct objfile
*objfile
;
750 const struct bfd_arch_info
*b
;
752 jit_debug_printf ("symfile_addr = %s, symfile_size = %s",
753 paddress (gdbarch
, code_entry
->symfile_addr
),
754 pulongest (code_entry
->symfile_size
));
756 gdb_bfd_ref_ptr
nbfd (gdb_bfd_open_from_target_memory
757 (code_entry
->symfile_addr
, code_entry
->symfile_size
, gnutarget
));
760 gdb_puts (_("Error opening JITed symbol file, ignoring it.\n"),
765 /* Check the format. NOTE: This initializes important data that GDB uses!
766 We would segfault later without this line. */
767 if (!bfd_check_format (nbfd
.get (), bfd_object
))
769 gdb_printf (gdb_stderr
, _("\
770 JITed symbol file is not an object file, ignoring it.\n"));
774 /* Check bfd arch. */
775 b
= gdbarch_bfd_arch_info (gdbarch
);
776 if (b
->compatible (b
, bfd_get_arch_info (nbfd
.get ())) != b
)
777 warning (_("JITed object file architecture %s is not compatible "
778 "with target architecture %s."),
779 bfd_get_arch_info (nbfd
.get ())->printable_name
,
782 /* Read the section address information out of the symbol file. Since the
783 file is generated by the JIT at runtime, it should contain all of the
784 absolute addresses that we care about. */
785 section_addr_info sai
;
786 for (sec
= nbfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
787 if ((bfd_section_flags (sec
) & (SEC_ALLOC
|SEC_LOAD
)) != 0)
789 /* We assume that these virtual addresses are absolute, and do not
790 treat them as offsets. */
791 sai
.emplace_back (bfd_section_vma (sec
),
792 bfd_section_name (sec
),
796 /* This call does not take ownership of SAI. */
797 objfile
= symbol_file_add_from_bfd (nbfd
,
798 bfd_get_filename (nbfd
.get ()), 0,
800 OBJF_SHARED
| OBJF_NOT_FILENAME
, NULL
);
802 add_objfile_entry (objfile
, entry_addr
, code_entry
->symfile_addr
,
803 code_entry
->symfile_size
);
806 /* This function registers code associated with a JIT code entry. It uses the
807 pointer and size pair in the entry to read the symbol file from the remote
808 and then calls symbol_file_add_from_local_memory to add it as though it were
809 a symbol file added by the user. */
812 jit_register_code (struct gdbarch
*gdbarch
,
813 CORE_ADDR entry_addr
, struct jit_code_entry
*code_entry
)
817 jit_debug_printf ("symfile_addr = %s, symfile_size = %s",
818 paddress (gdbarch
, code_entry
->symfile_addr
),
819 pulongest (code_entry
->symfile_size
));
821 success
= jit_reader_try_read_symtab (gdbarch
, code_entry
, entry_addr
);
824 jit_bfd_try_read_symtab (code_entry
, entry_addr
, gdbarch
);
827 /* Look up the objfile with this code entry address. */
829 static struct objfile
*
830 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr
)
832 for (objfile
*objf
: current_program_space
->objfiles ())
834 if (objf
->jited_data
!= nullptr && objf
->jited_data
->addr
== entry_addr
)
841 /* This is called when a breakpoint is deleted. It updates the
842 inferior's cache, if needed. */
845 jit_breakpoint_deleted (struct breakpoint
*b
)
847 if (b
->type
!= bp_jit_event
)
850 for (bp_location
&iter
: b
->locations ())
852 for (objfile
*objf
: iter
.pspace
->objfiles ())
854 jiter_objfile_data
*jiter_data
= objf
->jiter_data
.get ();
856 if (jiter_data
!= nullptr
857 && jiter_data
->jit_breakpoint
== iter
.owner
)
859 jiter_data
->cached_code_address
= 0;
860 jiter_data
->jit_breakpoint
= nullptr;
866 /* (Re-)Initialize the jit breakpoints for JIT-producing objfiles in
870 jit_breakpoint_re_set_internal (struct gdbarch
*gdbarch
, program_space
*pspace
)
872 for (objfile
*the_objfile
: pspace
->objfiles ())
874 /* Skip separate debug objects. */
875 if (the_objfile
->separate_debug_objfile_backlink
!= nullptr)
878 if (the_objfile
->skip_jit_symbol_lookup
)
881 /* Lookup the registration symbol. If it is missing, then we
882 assume we are not attached to a JIT. */
883 bound_minimal_symbol reg_symbol
884 = lookup_minimal_symbol_text (pspace
, jit_break_name
, the_objfile
);
885 if (reg_symbol
.minsym
== NULL
886 || reg_symbol
.value_address () == 0)
888 /* No need to repeat the lookup the next time. */
889 the_objfile
->skip_jit_symbol_lookup
= true;
893 bound_minimal_symbol desc_symbol
894 = lookup_minimal_symbol_linkage (jit_descriptor_name
, the_objfile
);
895 if (desc_symbol
.minsym
== NULL
896 || desc_symbol
.value_address () == 0)
898 /* No need to repeat the lookup the next time. */
899 the_objfile
->skip_jit_symbol_lookup
= true;
903 jiter_objfile_data
*objf_data
904 = get_jiter_objfile_data (the_objfile
);
905 objf_data
->register_code
= reg_symbol
.minsym
;
906 objf_data
->descriptor
= desc_symbol
.minsym
;
908 CORE_ADDR addr
= objf_data
->register_code
->value_address (the_objfile
);
909 jit_debug_printf ("breakpoint_addr = %s", paddress (gdbarch
, addr
));
911 /* Check if we need to re-create the breakpoint. */
912 if (objf_data
->cached_code_address
== addr
)
915 /* Delete the old breakpoint. */
916 if (objf_data
->jit_breakpoint
!= nullptr)
917 delete_breakpoint (objf_data
->jit_breakpoint
);
919 /* Put a breakpoint in the registration symbol. */
920 objf_data
->cached_code_address
= addr
;
921 objf_data
->jit_breakpoint
= create_jit_event_breakpoint (gdbarch
, addr
);
925 /* The private data passed around in the frame unwind callback
928 struct jit_unwind_private
930 /* Cached register values. See jit_frame_sniffer to see how this
932 std::unique_ptr
<detached_regcache
> regcache
;
934 /* The frame being unwound. */
935 frame_info_ptr this_frame
;
938 /* Sets the value of a particular register in this frame. */
941 jit_unwind_reg_set_impl (struct gdb_unwind_callbacks
*cb
, int dwarf_regnum
,
942 struct gdb_reg_value
*value
)
944 struct jit_unwind_private
*priv
;
947 priv
= (struct jit_unwind_private
*) cb
->priv_data
;
949 gdb_reg
= gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv
->this_frame
),
953 jit_debug_printf ("Could not recognize DWARF regnum %d", dwarf_regnum
);
958 priv
->regcache
->raw_supply (gdb_reg
, value
->value
);
963 reg_value_free_impl (struct gdb_reg_value
*value
)
968 /* Get the value of register REGNUM in the previous frame. */
970 static struct gdb_reg_value
*
971 jit_unwind_reg_get_impl (struct gdb_unwind_callbacks
*cb
, int regnum
)
973 struct jit_unwind_private
*priv
;
974 struct gdb_reg_value
*value
;
976 struct gdbarch
*frame_arch
;
978 priv
= (struct jit_unwind_private
*) cb
->priv_data
;
979 frame_arch
= get_frame_arch (priv
->this_frame
);
981 gdb_reg
= gdbarch_dwarf2_reg_to_regnum (frame_arch
, regnum
);
982 size
= register_size (frame_arch
, gdb_reg
);
983 value
= ((struct gdb_reg_value
*)
984 xmalloc (sizeof (struct gdb_reg_value
) + size
- 1));
985 value
->defined
= deprecated_frame_register_read (priv
->this_frame
, gdb_reg
,
988 value
->free
= reg_value_free_impl
;
992 /* gdb_reg_value has a free function, which must be called on each
993 saved register value. */
996 jit_dealloc_cache (frame_info
*this_frame
, void *cache
)
998 struct jit_unwind_private
*priv_data
= (struct jit_unwind_private
*) cache
;
1002 /* The frame sniffer for the pseudo unwinder.
1004 While this is nominally a frame sniffer, in the case where the JIT
1005 reader actually recognizes the frame, it does a lot more work -- it
1006 unwinds the frame and saves the corresponding register values in
1007 the cache. jit_frame_prev_register simply returns the saved
1011 jit_frame_sniffer (const struct frame_unwind
*self
,
1012 const frame_info_ptr
&this_frame
, void **cache
)
1014 struct jit_unwind_private
*priv_data
;
1015 struct gdb_unwind_callbacks callbacks
;
1016 struct gdb_reader_funcs
*funcs
;
1018 callbacks
.reg_get
= jit_unwind_reg_get_impl
;
1019 callbacks
.reg_set
= jit_unwind_reg_set_impl
;
1020 callbacks
.target_read
= jit_target_read_impl
;
1022 if (loaded_jit_reader
== NULL
)
1025 funcs
= loaded_jit_reader
->functions
;
1027 gdb_assert (!*cache
);
1029 priv_data
= new struct jit_unwind_private
;
1031 /* Take a snapshot of current regcache. */
1032 priv_data
->regcache
.reset
1033 (new detached_regcache (get_frame_arch (this_frame
), true));
1034 priv_data
->this_frame
= this_frame
;
1036 callbacks
.priv_data
= priv_data
;
1038 /* Try to coax the provided unwinder to unwind the stack */
1039 if (funcs
->unwind (funcs
, &callbacks
) == GDB_SUCCESS
)
1041 jit_debug_printf ("Successfully unwound frame using JIT reader.");
1045 jit_debug_printf ("Could not unwind frame using JIT reader.");
1047 jit_dealloc_cache (this_frame
.get (), *cache
);
1054 /* The frame_id function for the pseudo unwinder. Relays the call to
1055 the loaded plugin. */
1058 jit_frame_this_id (const frame_info_ptr
&this_frame
, void **cache
,
1059 struct frame_id
*this_id
)
1061 struct jit_unwind_private priv
;
1062 struct gdb_frame_id frame_id
;
1063 struct gdb_reader_funcs
*funcs
;
1064 struct gdb_unwind_callbacks callbacks
;
1066 priv
.regcache
.reset ();
1067 priv
.this_frame
= this_frame
;
1069 /* We don't expect the frame_id function to set any registers, so we
1070 set reg_set to NULL. */
1071 callbacks
.reg_get
= jit_unwind_reg_get_impl
;
1072 callbacks
.reg_set
= NULL
;
1073 callbacks
.target_read
= jit_target_read_impl
;
1074 callbacks
.priv_data
= &priv
;
1076 gdb_assert (loaded_jit_reader
);
1077 funcs
= loaded_jit_reader
->functions
;
1079 frame_id
= funcs
->get_frame_id (funcs
, &callbacks
);
1080 *this_id
= frame_id_build (frame_id
.stack_address
, frame_id
.code_address
);
1083 /* Pseudo unwinder function. Reads the previously fetched value for
1084 the register from the cache. */
1086 static struct value
*
1087 jit_frame_prev_register (const frame_info_ptr
&this_frame
, void **cache
, int reg
)
1089 struct jit_unwind_private
*priv
= (struct jit_unwind_private
*) *cache
;
1090 struct gdbarch
*gdbarch
;
1093 return frame_unwind_got_optimized (this_frame
, reg
);
1095 gdbarch
= priv
->regcache
->arch ();
1096 gdb_byte
*buf
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
));
1097 enum register_status status
= priv
->regcache
->cooked_read (reg
, buf
);
1099 if (status
== REG_VALID
)
1100 return frame_unwind_got_bytes (this_frame
, reg
, buf
);
1102 return frame_unwind_got_optimized (this_frame
, reg
);
1105 /* Relay everything back to the unwinder registered by the JIT debug
1108 static const struct frame_unwind jit_frame_unwind
=
1112 default_frame_unwind_stop_reason
,
1114 jit_frame_prev_register
,
1121 /* This is the information that is stored at jit_gdbarch_data for each
1124 struct jit_gdbarch_data_type
1126 /* Has the (pseudo) unwinder been pretended? */
1127 int unwinder_registered
= 0;
1130 /* An unwinder is registered for every gdbarch. This key is used to
1131 remember if the unwinder has been registered for a particular
1134 static const registry
<gdbarch
>::key
<jit_gdbarch_data_type
> jit_gdbarch_data
;
1136 /* Check GDBARCH and prepend the pseudo JIT unwinder if needed. */
1139 jit_prepend_unwinder (struct gdbarch
*gdbarch
)
1141 struct jit_gdbarch_data_type
*data
= jit_gdbarch_data
.get (gdbarch
);
1142 if (data
== nullptr)
1143 data
= jit_gdbarch_data
.emplace (gdbarch
);
1145 if (!data
->unwinder_registered
)
1147 frame_unwind_prepend_unwinder (gdbarch
, &jit_frame_unwind
);
1148 data
->unwinder_registered
= 1;
1152 /* Looks for the descriptor and registration symbols and breakpoints
1153 the registration function. If it finds both, it registers all the
1154 already JITed code. If it has already found the symbols, then it
1155 doesn't try again. */
1158 jit_inferior_init (inferior
*inf
)
1160 struct jit_descriptor descriptor
;
1161 struct jit_code_entry cur_entry
;
1162 CORE_ADDR cur_entry_addr
;
1163 struct gdbarch
*gdbarch
= inf
->arch ();
1164 program_space
*pspace
= inf
->pspace
;
1166 jit_debug_printf ("called");
1168 jit_prepend_unwinder (gdbarch
);
1170 jit_breakpoint_re_set_internal (gdbarch
, pspace
);
1172 for (objfile
*jiter
: pspace
->objfiles ())
1174 if (jiter
->jiter_data
== nullptr)
1177 /* Read the descriptor so we can check the version number and load
1178 any already JITed functions. */
1179 if (!jit_read_descriptor (gdbarch
, &descriptor
, jiter
))
1182 /* Check that the version number agrees with that we support. */
1183 if (descriptor
.version
!= 1)
1185 gdb_printf (gdb_stderr
,
1186 _("Unsupported JIT protocol version %ld "
1187 "in descriptor (expected 1)\n"),
1188 (long) descriptor
.version
);
1192 /* If we've attached to a running program, we need to check the
1193 descriptor to register any functions that were already
1195 for (cur_entry_addr
= descriptor
.first_entry
;
1196 cur_entry_addr
!= 0;
1197 cur_entry_addr
= cur_entry
.next_entry
)
1199 jit_read_code_entry (gdbarch
, cur_entry_addr
, &cur_entry
);
1201 /* This hook may be called many times during setup, so make sure
1202 we don't add the same symbol file twice. */
1203 if (jit_find_objf_with_entry_addr (cur_entry_addr
) != NULL
)
1206 jit_register_code (gdbarch
, cur_entry_addr
, &cur_entry
);
1211 /* inferior_created observer. */
1214 jit_inferior_created_hook (inferior
*inf
)
1216 jit_inferior_init (inf
);
1219 /* inferior_execd observer. */
1222 jit_inferior_execd_hook (inferior
*exec_inf
, inferior
*follow_inf
)
1224 jit_inferior_init (follow_inf
);
1227 /* Exported routine to call to re-set the jit breakpoints,
1228 e.g. when a program is rerun. */
1231 jit_breakpoint_re_set (void)
1233 jit_breakpoint_re_set_internal (current_inferior ()->arch (),
1234 current_program_space
);
1237 /* This function cleans up any code entries left over when the
1238 inferior exits. We get left over code when the inferior exits
1239 without unregistering its code, for example when it crashes. */
1242 jit_inferior_exit_hook (struct inferior
*inf
)
1244 for (objfile
*objf
: current_program_space
->objfiles_safe ())
1246 if (objf
->jited_data
!= nullptr && objf
->jited_data
->addr
!= 0)
1252 jit_event_handler (gdbarch
*gdbarch
, objfile
*jiter
)
1254 struct jit_descriptor descriptor
;
1256 /* If we get a JIT breakpoint event for this objfile, it is necessarily a
1258 gdb_assert (jiter
->jiter_data
!= nullptr);
1260 /* Read the descriptor from remote memory. */
1261 if (!jit_read_descriptor (gdbarch
, &descriptor
, jiter
))
1263 CORE_ADDR entry_addr
= descriptor
.relevant_entry
;
1265 /* Do the corresponding action. */
1266 switch (descriptor
.action_flag
)
1273 jit_code_entry code_entry
;
1274 jit_read_code_entry (gdbarch
, entry_addr
, &code_entry
);
1275 jit_register_code (gdbarch
, entry_addr
, &code_entry
);
1279 case JIT_UNREGISTER
:
1281 objfile
*jited
= jit_find_objf_with_entry_addr (entry_addr
);
1282 if (jited
== nullptr)
1283 gdb_printf (gdb_stderr
,
1284 _("Unable to find JITed code "
1285 "entry at address: %s\n"),
1286 paddress (gdbarch
, entry_addr
));
1294 error (_("Unknown action_flag value in JIT descriptor!"));
1299 void _initialize_jit ();
1303 jit_reader_dir
= relocate_gdb_directory (JIT_READER_DIR
,
1304 JIT_READER_DIR_RELOCATABLE
);
1305 add_setshow_boolean_cmd ("jit", class_maintenance
, &jit_debug
,
1306 _("Set JIT debugging."),
1307 _("Show JIT debugging."),
1308 _("When set, JIT debugging is enabled."),
1311 &setdebuglist
, &showdebuglist
);
1313 add_cmd ("jit", class_maintenance
, maint_info_jit_cmd
,
1314 _("Print information about JIT-ed code objects."),
1315 &maintenanceinfolist
);
1317 gdb::observers::inferior_created
.attach (jit_inferior_created_hook
, "jit");
1318 gdb::observers::inferior_execd
.attach (jit_inferior_execd_hook
, "jit");
1319 gdb::observers::inferior_exit
.attach (jit_inferior_exit_hook
, "jit");
1320 gdb::observers::breakpoint_deleted
.attach (jit_breakpoint_deleted
, "jit");
1322 if (is_dl_available ())
1324 struct cmd_list_element
*c
;
1326 c
= add_com ("jit-reader-load", no_class
, jit_reader_load_command
, _("\
1327 Load FILE as debug info reader and unwinder for JIT compiled code.\n\
1328 Usage: jit-reader-load FILE\n\
1329 Try to load file FILE as a debug info reader (and unwinder) for\n\
1330 JIT compiled code. The file is loaded from " JIT_READER_DIR
",\n\
1331 relocated relative to the GDB executable if required."));
1332 set_cmd_completer (c
, deprecated_filename_completer
);
1334 c
= add_com ("jit-reader-unload", no_class
,
1335 jit_reader_unload_command
, _("\
1336 Unload the currently loaded JIT debug info reader.\n\
1337 Usage: jit-reader-unload\n\n\
1338 Do \"help jit-reader-load\" for info on loading debug info readers."));
1339 set_cmd_completer (c
, noop_completer
);