1 /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
3 Copyright (C) 2009-2021 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/>. */
23 #include "jit-reader.h"
25 #include "breakpoint.h"
27 #include "dictionary.h"
28 #include "filenames.h"
29 #include "frame-unwind.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 /* An unwinder is registered for every gdbarch. This key is used to
56 remember if the unwinder has been registered for a particular
59 static struct gdbarch_data
*jit_gdbarch_data
;
61 /* True if we want to see trace of jit level stuff. */
63 static bool jit_debug
= false;
65 /* Print a "jit" debug statement. */
67 #define jit_debug_printf(fmt, ...) \
68 debug_prefixed_printf_cond (jit_debug, "jit", fmt, ##__VA_ARGS__)
71 show_jit_debug (struct ui_file
*file
, int from_tty
,
72 struct cmd_list_element
*c
, const char *value
)
74 fprintf_filtered (file
, _("JIT debugging is %s.\n"), value
);
77 /* Implementation of the "maintenance info jit" command. */
80 maint_info_jit_cmd (const char *args
, int from_tty
)
82 inferior
*inf
= current_inferior ();
83 bool printed_header
= false;
85 /* Print a line for each JIT-ed objfile. */
86 for (objfile
*obj
: inf
->pspace
->objfiles ())
88 if (obj
->jited_data
== nullptr)
93 printf_filtered ("Base address of known JIT-ed objfiles:\n");
94 printed_header
= true;
97 printf_filtered (" %s\n", paddress (obj
->arch (), obj
->jited_data
->addr
));
103 jit_reader (struct gdb_reader_funcs
*f
, gdb_dlhandle_up
&&h
)
104 : functions (f
), handle (std::move (h
))
110 functions
->destroy (functions
);
113 DISABLE_COPY_AND_ASSIGN (jit_reader
);
115 struct gdb_reader_funcs
*functions
;
116 gdb_dlhandle_up handle
;
119 /* One reader that has been loaded successfully, and can potentially be used to
122 static struct jit_reader
*loaded_jit_reader
= NULL
;
124 typedef struct gdb_reader_funcs
* (reader_init_fn_type
) (void);
125 static const char reader_init_fn_sym
[] = "gdb_init_reader";
127 /* Try to load FILE_NAME as a JIT debug info reader. */
129 static struct jit_reader
*
130 jit_reader_load (const char *file_name
)
132 reader_init_fn_type
*init_fn
;
133 struct gdb_reader_funcs
*funcs
= NULL
;
135 jit_debug_printf ("Opening shared object %s", file_name
);
137 gdb_dlhandle_up so
= gdb_dlopen (file_name
);
139 init_fn
= (reader_init_fn_type
*) gdb_dlsym (so
, reader_init_fn_sym
);
141 error (_("Could not locate initialization function: %s."),
144 if (gdb_dlsym (so
, "plugin_is_GPL_compatible") == NULL
)
145 error (_("Reader not GPL compatible."));
148 if (funcs
->reader_version
!= GDB_READER_INTERFACE_VERSION
)
149 error (_("Reader version does not match GDB version."));
151 return new jit_reader (funcs
, std::move (so
));
154 /* Provides the jit-reader-load command. */
157 jit_reader_load_command (const char *args
, int from_tty
)
160 error (_("No reader name provided."));
161 gdb::unique_xmalloc_ptr
<char> file (tilde_expand (args
));
163 if (loaded_jit_reader
!= NULL
)
164 error (_("JIT reader already loaded. Run jit-reader-unload first."));
166 if (!IS_ABSOLUTE_PATH (file
.get ()))
167 file
= xstrprintf ("%s%s%s", jit_reader_dir
.c_str (),
168 SLASH_STRING
, file
.get ());
170 loaded_jit_reader
= jit_reader_load (file
.get ());
171 reinit_frame_cache ();
172 jit_inferior_created_hook (current_inferior ());
175 /* Provides the jit-reader-unload command. */
178 jit_reader_unload_command (const char *args
, int from_tty
)
180 if (!loaded_jit_reader
)
181 error (_("No JIT reader loaded."));
183 reinit_frame_cache ();
184 jit_inferior_exit_hook (current_inferior ());
186 delete loaded_jit_reader
;
187 loaded_jit_reader
= NULL
;
190 /* Destructor for jiter_objfile_data. */
192 jiter_objfile_data::~jiter_objfile_data ()
194 if (this->jit_breakpoint
!= nullptr)
195 delete_breakpoint (this->jit_breakpoint
);
198 /* Fetch the jiter_objfile_data associated with OBJF. If no data exists
199 yet, make a new structure and attach it. */
201 static jiter_objfile_data
*
202 get_jiter_objfile_data (objfile
*objf
)
204 if (objf
->jiter_data
== nullptr)
205 objf
->jiter_data
.reset (new jiter_objfile_data ());
207 return objf
->jiter_data
.get ();
210 /* Remember OBJFILE has been created for struct jit_code_entry located
211 at inferior address ENTRY. */
214 add_objfile_entry (struct objfile
*objfile
, CORE_ADDR entry
)
216 gdb_assert (objfile
->jited_data
== nullptr);
218 objfile
->jited_data
.reset (new jited_objfile_data (entry
));
221 /* Helper function for reading the global JIT descriptor from remote
222 memory. Returns true if all went well, false otherwise. */
225 jit_read_descriptor (gdbarch
*gdbarch
,
226 jit_descriptor
*descriptor
,
230 struct type
*ptr_type
;
234 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
236 gdb_assert (jiter
!= nullptr);
237 jiter_objfile_data
*objf_data
= jiter
->jiter_data
.get ();
238 gdb_assert (objf_data
!= nullptr);
240 CORE_ADDR addr
= MSYMBOL_VALUE_ADDRESS (jiter
, objf_data
->descriptor
);
242 jit_debug_printf ("descriptor_addr = %s", paddress (gdbarch
, addr
));
244 /* Figure out how big the descriptor is on the remote and how to read it. */
245 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
246 ptr_size
= TYPE_LENGTH (ptr_type
);
247 desc_size
= 8 + 2 * ptr_size
; /* Two 32-bit ints and two pointers. */
248 desc_buf
= (gdb_byte
*) alloca (desc_size
);
250 /* Read the descriptor. */
251 err
= target_read_memory (addr
, desc_buf
, desc_size
);
254 printf_unfiltered (_("Unable to read JIT descriptor from "
259 /* Fix the endianness to match the host. */
260 descriptor
->version
= extract_unsigned_integer (&desc_buf
[0], 4, byte_order
);
261 descriptor
->action_flag
=
262 extract_unsigned_integer (&desc_buf
[4], 4, byte_order
);
263 descriptor
->relevant_entry
= extract_typed_address (&desc_buf
[8], ptr_type
);
264 descriptor
->first_entry
=
265 extract_typed_address (&desc_buf
[8 + ptr_size
], ptr_type
);
270 /* Helper function for reading a JITed code entry from remote memory. */
273 jit_read_code_entry (struct gdbarch
*gdbarch
,
274 CORE_ADDR code_addr
, struct jit_code_entry
*code_entry
)
277 struct type
*ptr_type
;
282 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
284 /* Figure out how big the entry is on the remote and how to read it. */
285 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
286 ptr_size
= TYPE_LENGTH (ptr_type
);
288 /* Figure out where the uint64_t value will be. */
289 align_bytes
= type_align (builtin_type (gdbarch
)->builtin_uint64
);
291 off
= (off
+ (align_bytes
- 1)) & ~(align_bytes
- 1);
293 entry_size
= off
+ 8; /* Three pointers and one 64-bit int. */
294 entry_buf
= (gdb_byte
*) alloca (entry_size
);
296 /* Read the entry. */
297 err
= target_read_memory (code_addr
, entry_buf
, entry_size
);
299 error (_("Unable to read JIT code entry from remote memory!"));
301 /* Fix the endianness to match the host. */
302 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
303 code_entry
->next_entry
= extract_typed_address (&entry_buf
[0], ptr_type
);
304 code_entry
->prev_entry
=
305 extract_typed_address (&entry_buf
[ptr_size
], ptr_type
);
306 code_entry
->symfile_addr
=
307 extract_typed_address (&entry_buf
[2 * ptr_size
], ptr_type
);
308 code_entry
->symfile_size
=
309 extract_unsigned_integer (&entry_buf
[off
], 8, byte_order
);
312 /* Proxy object for building a block. */
316 gdb_block (gdb_block
*parent
, CORE_ADDR begin
, CORE_ADDR end
,
321 name (name
!= nullptr ? xstrdup (name
) : nullptr)
324 /* The parent of this block. */
325 struct gdb_block
*parent
;
327 /* Points to the "real" block that is being built out of this
328 instance. This block will be added to a blockvector, which will
329 then be added to a symtab. */
330 struct block
*real_block
= nullptr;
332 /* The first and last code address corresponding to this block. */
333 CORE_ADDR begin
, end
;
335 /* The name of this block (if any). If this is non-NULL, the
336 FUNCTION symbol symbol is set to this value. */
337 gdb::unique_xmalloc_ptr
<char> name
;
340 /* Proxy object for building a symtab. */
344 explicit gdb_symtab (const char *file_name
)
345 : file_name (file_name
!= nullptr ? file_name
: "")
348 /* The list of blocks in this symtab. These will eventually be
349 converted to real blocks.
351 This is specifically a linked list, instead of, for example, a vector,
352 because the pointers are returned to the user's debug info reader. So
353 it's important that the objects don't change location during their
354 lifetime (which would happen with a vector of objects getting resized). */
355 std::forward_list
<gdb_block
> blocks
;
357 /* The number of blocks inserted. */
360 /* A mapping between line numbers to PC. */
361 gdb::unique_xmalloc_ptr
<struct linetable
> linetable
;
363 /* The source file for this symtab. */
364 std::string file_name
;
367 /* Proxy object for building an object. */
371 /* Symtabs of this object.
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_symtab
> symtabs
;
380 /* The type of the `private' data passed around by the callback
383 typedef CORE_ADDR jit_dbg_reader_data
;
385 /* The reader calls into this function to read data off the targets
388 static enum gdb_status
389 jit_target_read_impl (GDB_CORE_ADDR target_mem
, void *gdb_buf
, int len
)
391 int result
= target_read_memory ((CORE_ADDR
) target_mem
,
392 (gdb_byte
*) gdb_buf
, len
);
399 /* The reader calls into this function to create a new gdb_object
400 which it can then pass around to the other callbacks. Right now,
401 all that is required is allocating the memory. */
403 static struct gdb_object
*
404 jit_object_open_impl (struct gdb_symbol_callbacks
*cb
)
406 /* CB is not required right now, but sometime in the future we might
407 need a handle to it, and we'd like to do that without breaking
409 return new gdb_object
;
412 /* Readers call into this function to open a new gdb_symtab, which,
413 again, is passed around to other callbacks. */
415 static struct gdb_symtab
*
416 jit_symtab_open_impl (struct gdb_symbol_callbacks
*cb
,
417 struct gdb_object
*object
,
418 const char *file_name
)
420 /* CB stays unused. See comment in jit_object_open_impl. */
422 object
->symtabs
.emplace_front (file_name
);
423 return &object
->symtabs
.front ();
426 /* Called by readers to open a new gdb_block. This function also
427 inserts the new gdb_block in the correct place in the corresponding
430 static struct gdb_block
*
431 jit_block_open_impl (struct gdb_symbol_callbacks
*cb
,
432 struct gdb_symtab
*symtab
, struct gdb_block
*parent
,
433 GDB_CORE_ADDR begin
, GDB_CORE_ADDR end
, const char *name
)
435 /* Place the block at the beginning of the list, it will be sorted when the
436 symtab is finalized. */
437 symtab
->blocks
.emplace_front (parent
, begin
, end
, name
);
440 return &symtab
->blocks
.front ();
443 /* Readers call this to add a line mapping (from PC to line number) to
447 jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks
*cb
,
448 struct gdb_symtab
*stab
, int nlines
,
449 struct gdb_line_mapping
*map
)
457 alloc_len
= sizeof (struct linetable
)
458 + (nlines
- 1) * sizeof (struct linetable_entry
);
459 stab
->linetable
.reset (XNEWVAR (struct linetable
, alloc_len
));
460 stab
->linetable
->nitems
= nlines
;
461 for (i
= 0; i
< nlines
; i
++)
463 stab
->linetable
->item
[i
].pc
= (CORE_ADDR
) map
[i
].pc
;
464 stab
->linetable
->item
[i
].line
= map
[i
].line
;
465 stab
->linetable
->item
[i
].is_stmt
= 1;
469 /* Called by readers to close a gdb_symtab. Does not need to do
470 anything as of now. */
473 jit_symtab_close_impl (struct gdb_symbol_callbacks
*cb
,
474 struct gdb_symtab
*stab
)
476 /* Right now nothing needs to be done here. We may need to do some
477 cleanup here in the future (again, without breaking the plugin
481 /* Transform STAB to a proper symtab, and add it it OBJFILE. */
484 finalize_symtab (struct gdb_symtab
*stab
, struct objfile
*objfile
)
486 struct compunit_symtab
*cust
;
487 size_t blockvector_size
;
488 CORE_ADDR begin
, end
;
489 struct blockvector
*bv
;
491 int actual_nblocks
= FIRST_LOCAL_BLOCK
+ stab
->nblocks
;
493 /* Sort the blocks in the order they should appear in the blockvector. */
494 stab
->blocks
.sort([] (const gdb_block
&a
, const gdb_block
&b
)
496 if (a
.begin
!= b
.begin
)
497 return a
.begin
< b
.begin
;
499 return a
.end
> b
.end
;
502 cust
= allocate_compunit_symtab (objfile
, stab
->file_name
.c_str ());
503 allocate_symtab (cust
, stab
->file_name
.c_str ());
504 add_compunit_symtab_to_objfile (cust
);
506 /* JIT compilers compile in memory. */
507 COMPUNIT_DIRNAME (cust
) = NULL
;
509 /* Copy over the linetable entry if one was provided. */
512 size_t size
= ((stab
->linetable
->nitems
- 1)
513 * sizeof (struct linetable_entry
)
514 + sizeof (struct linetable
));
515 SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust
))
516 = (struct linetable
*) obstack_alloc (&objfile
->objfile_obstack
, size
);
517 memcpy (SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust
)),
518 stab
->linetable
.get (), size
);
521 blockvector_size
= (sizeof (struct blockvector
)
522 + (actual_nblocks
- 1) * sizeof (struct block
*));
523 bv
= (struct blockvector
*) obstack_alloc (&objfile
->objfile_obstack
,
525 COMPUNIT_BLOCKVECTOR (cust
) = bv
;
527 /* At the end of this function, (begin, end) will contain the PC range this
528 entire blockvector spans. */
529 BLOCKVECTOR_MAP (bv
) = NULL
;
530 begin
= stab
->blocks
.front ().begin
;
531 end
= stab
->blocks
.front ().end
;
532 BLOCKVECTOR_NBLOCKS (bv
) = actual_nblocks
;
534 /* First run over all the gdb_block objects, creating a real block
535 object for each. Simultaneously, keep setting the real_block
537 int block_idx
= FIRST_LOCAL_BLOCK
;
538 for (gdb_block
&gdb_block_iter
: stab
->blocks
)
540 struct block
*new_block
= allocate_block (&objfile
->objfile_obstack
);
541 struct symbol
*block_name
= new (&objfile
->objfile_obstack
) symbol
;
542 struct type
*block_type
= arch_type (objfile
->arch (),
547 BLOCK_MULTIDICT (new_block
)
548 = mdict_create_linear (&objfile
->objfile_obstack
, NULL
);
549 /* The address range. */
550 BLOCK_START (new_block
) = (CORE_ADDR
) gdb_block_iter
.begin
;
551 BLOCK_END (new_block
) = (CORE_ADDR
) gdb_block_iter
.end
;
554 SYMBOL_DOMAIN (block_name
) = VAR_DOMAIN
;
555 SYMBOL_ACLASS_INDEX (block_name
) = LOC_BLOCK
;
556 symbol_set_symtab (block_name
, COMPUNIT_FILETABS (cust
));
557 SYMBOL_TYPE (block_name
) = lookup_function_type (block_type
);
558 SYMBOL_BLOCK_VALUE (block_name
) = new_block
;
560 block_name
->m_name
= obstack_strdup (&objfile
->objfile_obstack
,
561 gdb_block_iter
.name
.get ());
563 BLOCK_FUNCTION (new_block
) = block_name
;
565 BLOCKVECTOR_BLOCK (bv
, block_idx
) = new_block
;
566 if (begin
> BLOCK_START (new_block
))
567 begin
= BLOCK_START (new_block
);
568 if (end
< BLOCK_END (new_block
))
569 end
= BLOCK_END (new_block
);
571 gdb_block_iter
.real_block
= new_block
;
576 /* Now add the special blocks. */
577 struct block
*block_iter
= NULL
;
578 for (enum block_enum i
: { GLOBAL_BLOCK
, STATIC_BLOCK
})
580 struct block
*new_block
;
582 new_block
= (i
== GLOBAL_BLOCK
583 ? allocate_global_block (&objfile
->objfile_obstack
)
584 : allocate_block (&objfile
->objfile_obstack
));
585 BLOCK_MULTIDICT (new_block
)
586 = mdict_create_linear (&objfile
->objfile_obstack
, NULL
);
587 BLOCK_SUPERBLOCK (new_block
) = block_iter
;
588 block_iter
= new_block
;
590 BLOCK_START (new_block
) = (CORE_ADDR
) begin
;
591 BLOCK_END (new_block
) = (CORE_ADDR
) end
;
593 BLOCKVECTOR_BLOCK (bv
, i
) = new_block
;
595 if (i
== GLOBAL_BLOCK
)
596 set_block_compunit_symtab (new_block
, cust
);
599 /* Fill up the superblock fields for the real blocks, using the
600 real_block fields populated earlier. */
601 for (gdb_block
&gdb_block_iter
: stab
->blocks
)
603 if (gdb_block_iter
.parent
!= NULL
)
605 /* If the plugin specifically mentioned a parent block, we
607 BLOCK_SUPERBLOCK (gdb_block_iter
.real_block
) =
608 gdb_block_iter
.parent
->real_block
;
612 /* And if not, we set a default parent block. */
613 BLOCK_SUPERBLOCK (gdb_block_iter
.real_block
) =
614 BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
619 /* Called when closing a gdb_objfile. Converts OBJ to a proper
623 jit_object_close_impl (struct gdb_symbol_callbacks
*cb
,
624 struct gdb_object
*obj
)
626 struct objfile
*objfile
;
627 jit_dbg_reader_data
*priv_data
;
629 priv_data
= (jit_dbg_reader_data
*) cb
->priv_data
;
631 objfile
= objfile::make (nullptr, "<< JIT compiled code >>",
633 objfile
->per_bfd
->gdbarch
= target_gdbarch ();
635 for (gdb_symtab
&symtab
: obj
->symtabs
)
636 finalize_symtab (&symtab
, objfile
);
638 add_objfile_entry (objfile
, *priv_data
);
643 /* Try to read CODE_ENTRY using the loaded jit reader (if any).
644 ENTRY_ADDR is the address of the struct jit_code_entry in the
645 inferior address space. */
648 jit_reader_try_read_symtab (struct jit_code_entry
*code_entry
,
649 CORE_ADDR entry_addr
)
652 jit_dbg_reader_data priv_data
;
653 struct gdb_reader_funcs
*funcs
;
654 struct gdb_symbol_callbacks callbacks
=
656 jit_object_open_impl
,
657 jit_symtab_open_impl
,
659 jit_symtab_close_impl
,
660 jit_object_close_impl
,
662 jit_symtab_line_mapping_add_impl
,
663 jit_target_read_impl
,
668 priv_data
= entry_addr
;
670 if (!loaded_jit_reader
)
673 gdb::byte_vector
gdb_mem (code_entry
->symfile_size
);
678 if (target_read_memory (code_entry
->symfile_addr
, gdb_mem
.data (),
679 code_entry
->symfile_size
))
682 catch (const gdb_exception
&e
)
689 funcs
= loaded_jit_reader
->functions
;
690 if (funcs
->read (funcs
, &callbacks
, gdb_mem
.data (),
691 code_entry
->symfile_size
)
697 jit_debug_printf ("Could not read symtab using the loaded JIT reader.");
702 /* Try to read CODE_ENTRY using BFD. ENTRY_ADDR is the address of the
703 struct jit_code_entry in the inferior address space. */
706 jit_bfd_try_read_symtab (struct jit_code_entry
*code_entry
,
707 CORE_ADDR entry_addr
,
708 struct gdbarch
*gdbarch
)
710 struct bfd_section
*sec
;
711 struct objfile
*objfile
;
712 const struct bfd_arch_info
*b
;
714 jit_debug_printf ("symfile_addr = %s, symfile_size = %s",
715 paddress (gdbarch
, code_entry
->symfile_addr
),
716 pulongest (code_entry
->symfile_size
));
718 gdb_bfd_ref_ptr
nbfd (gdb_bfd_open_from_target_memory
719 (code_entry
->symfile_addr
, code_entry
->symfile_size
, gnutarget
));
722 puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
726 /* Check the format. NOTE: This initializes important data that GDB uses!
727 We would segfault later without this line. */
728 if (!bfd_check_format (nbfd
.get (), bfd_object
))
730 printf_unfiltered (_("\
731 JITed symbol file is not an object file, ignoring it.\n"));
735 /* Check bfd arch. */
736 b
= gdbarch_bfd_arch_info (gdbarch
);
737 if (b
->compatible (b
, bfd_get_arch_info (nbfd
.get ())) != b
)
738 warning (_("JITed object file architecture %s is not compatible "
739 "with target architecture %s."),
740 bfd_get_arch_info (nbfd
.get ())->printable_name
,
743 /* Read the section address information out of the symbol file. Since the
744 file is generated by the JIT at runtime, it should all of the absolute
745 addresses that we care about. */
746 section_addr_info sai
;
747 for (sec
= nbfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
748 if ((bfd_section_flags (sec
) & (SEC_ALLOC
|SEC_LOAD
)) != 0)
750 /* We assume that these virtual addresses are absolute, and do not
751 treat them as offsets. */
752 sai
.emplace_back (bfd_section_vma (sec
),
753 bfd_section_name (sec
),
757 /* This call does not take ownership of SAI. */
758 objfile
= symbol_file_add_from_bfd (nbfd
.get (),
759 bfd_get_filename (nbfd
.get ()), 0,
761 OBJF_SHARED
| OBJF_NOT_FILENAME
, NULL
);
763 add_objfile_entry (objfile
, entry_addr
);
766 /* This function registers code associated with a JIT code entry. It uses the
767 pointer and size pair in the entry to read the symbol file from the remote
768 and then calls symbol_file_add_from_local_memory to add it as though it were
769 a symbol file added by the user. */
772 jit_register_code (struct gdbarch
*gdbarch
,
773 CORE_ADDR entry_addr
, struct jit_code_entry
*code_entry
)
777 jit_debug_printf ("symfile_addr = %s, symfile_size = %s",
778 paddress (gdbarch
, code_entry
->symfile_addr
),
779 pulongest (code_entry
->symfile_size
));
781 success
= jit_reader_try_read_symtab (code_entry
, entry_addr
);
784 jit_bfd_try_read_symtab (code_entry
, entry_addr
, gdbarch
);
787 /* Look up the objfile with this code entry address. */
789 static struct objfile
*
790 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr
)
792 for (objfile
*objf
: current_program_space
->objfiles ())
794 if (objf
->jited_data
!= nullptr && objf
->jited_data
->addr
== entry_addr
)
801 /* This is called when a breakpoint is deleted. It updates the
802 inferior's cache, if needed. */
805 jit_breakpoint_deleted (struct breakpoint
*b
)
807 if (b
->type
!= bp_jit_event
)
810 for (bp_location
*iter
: b
->locations ())
812 for (objfile
*objf
: iter
->pspace
->objfiles ())
814 jiter_objfile_data
*jiter_data
= objf
->jiter_data
.get ();
816 if (jiter_data
!= nullptr
817 && jiter_data
->jit_breakpoint
== iter
->owner
)
819 jiter_data
->cached_code_address
= 0;
820 jiter_data
->jit_breakpoint
= nullptr;
826 /* (Re-)Initialize the jit breakpoints for JIT-producing objfiles in
830 jit_breakpoint_re_set_internal (struct gdbarch
*gdbarch
, program_space
*pspace
)
832 for (objfile
*the_objfile
: pspace
->objfiles ())
834 /* Skip separate debug objects. */
835 if (the_objfile
->separate_debug_objfile_backlink
!= nullptr)
838 if (the_objfile
->skip_jit_symbol_lookup
)
841 /* Lookup the registration symbol. If it is missing, then we
842 assume we are not attached to a JIT. */
843 bound_minimal_symbol reg_symbol
844 = lookup_minimal_symbol (jit_break_name
, nullptr, the_objfile
);
845 if (reg_symbol
.minsym
== NULL
846 || BMSYMBOL_VALUE_ADDRESS (reg_symbol
) == 0)
848 /* No need to repeat the lookup the next time. */
849 the_objfile
->skip_jit_symbol_lookup
= true;
853 bound_minimal_symbol desc_symbol
854 = lookup_minimal_symbol (jit_descriptor_name
, NULL
, the_objfile
);
855 if (desc_symbol
.minsym
== NULL
856 || BMSYMBOL_VALUE_ADDRESS (desc_symbol
) == 0)
858 /* No need to repeat the lookup the next time. */
859 the_objfile
->skip_jit_symbol_lookup
= true;
863 jiter_objfile_data
*objf_data
864 = get_jiter_objfile_data (the_objfile
);
865 objf_data
->register_code
= reg_symbol
.minsym
;
866 objf_data
->descriptor
= desc_symbol
.minsym
;
868 CORE_ADDR addr
= MSYMBOL_VALUE_ADDRESS (the_objfile
,
869 objf_data
->register_code
);
871 jit_debug_printf ("breakpoint_addr = %s", paddress (gdbarch
, addr
));
873 /* Check if we need to re-create the breakpoint. */
874 if (objf_data
->cached_code_address
== addr
)
877 /* Delete the old breakpoint. */
878 if (objf_data
->jit_breakpoint
!= nullptr)
879 delete_breakpoint (objf_data
->jit_breakpoint
);
881 /* Put a breakpoint in the registration symbol. */
882 objf_data
->cached_code_address
= addr
;
883 objf_data
->jit_breakpoint
= create_jit_event_breakpoint (gdbarch
, addr
);
887 /* The private data passed around in the frame unwind callback
890 struct jit_unwind_private
892 /* Cached register values. See jit_frame_sniffer to see how this
894 detached_regcache
*regcache
;
896 /* The frame being unwound. */
897 struct frame_info
*this_frame
;
900 /* Sets the value of a particular register in this frame. */
903 jit_unwind_reg_set_impl (struct gdb_unwind_callbacks
*cb
, int dwarf_regnum
,
904 struct gdb_reg_value
*value
)
906 struct jit_unwind_private
*priv
;
909 priv
= (struct jit_unwind_private
*) cb
->priv_data
;
911 gdb_reg
= gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv
->this_frame
),
915 jit_debug_printf ("Could not recognize DWARF regnum %d", dwarf_regnum
);
920 priv
->regcache
->raw_supply (gdb_reg
, value
->value
);
925 reg_value_free_impl (struct gdb_reg_value
*value
)
930 /* Get the value of register REGNUM in the previous frame. */
932 static struct gdb_reg_value
*
933 jit_unwind_reg_get_impl (struct gdb_unwind_callbacks
*cb
, int regnum
)
935 struct jit_unwind_private
*priv
;
936 struct gdb_reg_value
*value
;
938 struct gdbarch
*frame_arch
;
940 priv
= (struct jit_unwind_private
*) cb
->priv_data
;
941 frame_arch
= get_frame_arch (priv
->this_frame
);
943 gdb_reg
= gdbarch_dwarf2_reg_to_regnum (frame_arch
, regnum
);
944 size
= register_size (frame_arch
, gdb_reg
);
945 value
= ((struct gdb_reg_value
*)
946 xmalloc (sizeof (struct gdb_reg_value
) + size
- 1));
947 value
->defined
= deprecated_frame_register_read (priv
->this_frame
, gdb_reg
,
950 value
->free
= reg_value_free_impl
;
954 /* gdb_reg_value has a free function, which must be called on each
955 saved register value. */
958 jit_dealloc_cache (struct frame_info
*this_frame
, void *cache
)
960 struct jit_unwind_private
*priv_data
= (struct jit_unwind_private
*) cache
;
962 gdb_assert (priv_data
->regcache
!= NULL
);
963 delete priv_data
->regcache
;
967 /* The frame sniffer for the pseudo unwinder.
969 While this is nominally a frame sniffer, in the case where the JIT
970 reader actually recognizes the frame, it does a lot more work -- it
971 unwinds the frame and saves the corresponding register values in
972 the cache. jit_frame_prev_register simply returns the saved
976 jit_frame_sniffer (const struct frame_unwind
*self
,
977 struct frame_info
*this_frame
, void **cache
)
979 struct jit_unwind_private
*priv_data
;
980 struct gdb_unwind_callbacks callbacks
;
981 struct gdb_reader_funcs
*funcs
;
983 callbacks
.reg_get
= jit_unwind_reg_get_impl
;
984 callbacks
.reg_set
= jit_unwind_reg_set_impl
;
985 callbacks
.target_read
= jit_target_read_impl
;
987 if (loaded_jit_reader
== NULL
)
990 funcs
= loaded_jit_reader
->functions
;
992 gdb_assert (!*cache
);
994 *cache
= XCNEW (struct jit_unwind_private
);
995 priv_data
= (struct jit_unwind_private
*) *cache
;
996 /* Take a snapshot of current regcache. */
997 priv_data
->regcache
= new detached_regcache (get_frame_arch (this_frame
),
999 priv_data
->this_frame
= this_frame
;
1001 callbacks
.priv_data
= priv_data
;
1003 /* Try to coax the provided unwinder to unwind the stack */
1004 if (funcs
->unwind (funcs
, &callbacks
) == GDB_SUCCESS
)
1006 jit_debug_printf ("Successfully unwound frame using JIT reader.");
1010 jit_debug_printf ("Could not unwind frame using JIT reader.");
1012 jit_dealloc_cache (this_frame
, *cache
);
1019 /* The frame_id function for the pseudo unwinder. Relays the call to
1020 the loaded plugin. */
1023 jit_frame_this_id (struct frame_info
*this_frame
, void **cache
,
1024 struct frame_id
*this_id
)
1026 struct jit_unwind_private priv
;
1027 struct gdb_frame_id frame_id
;
1028 struct gdb_reader_funcs
*funcs
;
1029 struct gdb_unwind_callbacks callbacks
;
1031 priv
.regcache
= NULL
;
1032 priv
.this_frame
= this_frame
;
1034 /* We don't expect the frame_id function to set any registers, so we
1035 set reg_set to NULL. */
1036 callbacks
.reg_get
= jit_unwind_reg_get_impl
;
1037 callbacks
.reg_set
= NULL
;
1038 callbacks
.target_read
= jit_target_read_impl
;
1039 callbacks
.priv_data
= &priv
;
1041 gdb_assert (loaded_jit_reader
);
1042 funcs
= loaded_jit_reader
->functions
;
1044 frame_id
= funcs
->get_frame_id (funcs
, &callbacks
);
1045 *this_id
= frame_id_build (frame_id
.stack_address
, frame_id
.code_address
);
1048 /* Pseudo unwinder function. Reads the previously fetched value for
1049 the register from the cache. */
1051 static struct value
*
1052 jit_frame_prev_register (struct frame_info
*this_frame
, void **cache
, int reg
)
1054 struct jit_unwind_private
*priv
= (struct jit_unwind_private
*) *cache
;
1055 struct gdbarch
*gdbarch
;
1058 return frame_unwind_got_optimized (this_frame
, reg
);
1060 gdbarch
= priv
->regcache
->arch ();
1061 gdb_byte
*buf
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
));
1062 enum register_status status
= priv
->regcache
->cooked_read (reg
, buf
);
1064 if (status
== REG_VALID
)
1065 return frame_unwind_got_bytes (this_frame
, reg
, buf
);
1067 return frame_unwind_got_optimized (this_frame
, reg
);
1070 /* Relay everything back to the unwinder registered by the JIT debug
1073 static const struct frame_unwind jit_frame_unwind
=
1077 default_frame_unwind_stop_reason
,
1079 jit_frame_prev_register
,
1086 /* This is the information that is stored at jit_gdbarch_data for each
1089 struct jit_gdbarch_data_type
1091 /* Has the (pseudo) unwinder been prepended? */
1092 int unwinder_registered
;
1095 /* Check GDBARCH and prepend the pseudo JIT unwinder if needed. */
1098 jit_prepend_unwinder (struct gdbarch
*gdbarch
)
1100 struct jit_gdbarch_data_type
*data
;
1103 = (struct jit_gdbarch_data_type
*) gdbarch_data (gdbarch
, jit_gdbarch_data
);
1104 if (!data
->unwinder_registered
)
1106 frame_unwind_prepend_unwinder (gdbarch
, &jit_frame_unwind
);
1107 data
->unwinder_registered
= 1;
1111 /* Register any already created translations. */
1114 jit_inferior_init (inferior
*inf
)
1116 struct jit_descriptor descriptor
;
1117 struct jit_code_entry cur_entry
;
1118 CORE_ADDR cur_entry_addr
;
1119 struct gdbarch
*gdbarch
= inf
->gdbarch
;
1120 program_space
*pspace
= inf
->pspace
;
1122 jit_debug_printf ("called");
1124 jit_prepend_unwinder (gdbarch
);
1126 jit_breakpoint_re_set_internal (gdbarch
, pspace
);
1128 for (objfile
*jiter
: pspace
->objfiles ())
1130 if (jiter
->jiter_data
== nullptr)
1133 /* Read the descriptor so we can check the version number and load
1134 any already JITed functions. */
1135 if (!jit_read_descriptor (gdbarch
, &descriptor
, jiter
))
1138 /* Check that the version number agrees with that we support. */
1139 if (descriptor
.version
!= 1)
1141 printf_unfiltered (_("Unsupported JIT protocol version %ld "
1142 "in descriptor (expected 1)\n"),
1143 (long) descriptor
.version
);
1147 /* If we've attached to a running program, we need to check the
1148 descriptor to register any functions that were already
1150 for (cur_entry_addr
= descriptor
.first_entry
;
1151 cur_entry_addr
!= 0;
1152 cur_entry_addr
= cur_entry
.next_entry
)
1154 jit_read_code_entry (gdbarch
, cur_entry_addr
, &cur_entry
);
1156 /* This hook may be called many times during setup, so make sure
1157 we don't add the same symbol file twice. */
1158 if (jit_find_objf_with_entry_addr (cur_entry_addr
) != NULL
)
1161 jit_register_code (gdbarch
, cur_entry_addr
, &cur_entry
);
1166 /* Looks for the descriptor and registration symbols and breakpoints
1167 the registration function. If it finds both, it registers all the
1168 already JITed code. If it has already found the symbols, then it
1169 doesn't try again. */
1172 jit_inferior_created_hook (inferior
*inf
)
1174 jit_inferior_init (inf
);
1177 /* Exported routine to call to re-set the jit breakpoints,
1178 e.g. when a program is rerun. */
1181 jit_breakpoint_re_set (void)
1183 jit_breakpoint_re_set_internal (target_gdbarch (), current_program_space
);
1186 /* This function cleans up any code entries left over when the
1187 inferior exits. We get left over code when the inferior exits
1188 without unregistering its code, for example when it crashes. */
1191 jit_inferior_exit_hook (struct inferior
*inf
)
1193 for (objfile
*objf
: current_program_space
->objfiles_safe ())
1195 if (objf
->jited_data
!= nullptr && objf
->jited_data
->addr
!= 0)
1201 jit_event_handler (gdbarch
*gdbarch
, objfile
*jiter
)
1203 struct jit_descriptor descriptor
;
1205 /* If we get a JIT breakpoint event for this objfile, it is necessarily a
1207 gdb_assert (jiter
->jiter_data
!= nullptr);
1209 /* Read the descriptor from remote memory. */
1210 if (!jit_read_descriptor (gdbarch
, &descriptor
, jiter
))
1212 CORE_ADDR entry_addr
= descriptor
.relevant_entry
;
1214 /* Do the corresponding action. */
1215 switch (descriptor
.action_flag
)
1222 jit_code_entry code_entry
;
1223 jit_read_code_entry (gdbarch
, entry_addr
, &code_entry
);
1224 jit_register_code (gdbarch
, entry_addr
, &code_entry
);
1228 case JIT_UNREGISTER
:
1230 objfile
*jited
= jit_find_objf_with_entry_addr (entry_addr
);
1231 if (jited
== nullptr)
1232 printf_unfiltered (_("Unable to find JITed code "
1233 "entry at address: %s\n"),
1234 paddress (gdbarch
, entry_addr
));
1242 error (_("Unknown action_flag value in JIT descriptor!"));
1247 /* Initialize the jit_gdbarch_data slot with an instance of struct
1248 jit_gdbarch_data_type */
1251 jit_gdbarch_data_init (struct obstack
*obstack
)
1253 struct jit_gdbarch_data_type
*data
=
1254 XOBNEW (obstack
, struct jit_gdbarch_data_type
);
1256 data
->unwinder_registered
= 0;
1261 void _initialize_jit ();
1265 jit_reader_dir
= relocate_gdb_directory (JIT_READER_DIR
,
1266 JIT_READER_DIR_RELOCATABLE
);
1267 add_setshow_boolean_cmd ("jit", class_maintenance
, &jit_debug
,
1268 _("Set JIT debugging."),
1269 _("Show JIT debugging."),
1270 _("When set, JIT debugging is enabled."),
1273 &setdebuglist
, &showdebuglist
);
1275 add_cmd ("jit", class_maintenance
, maint_info_jit_cmd
,
1276 _("Print information about JIT-ed code objects."),
1277 &maintenanceinfolist
);
1279 gdb::observers::inferior_created
.attach (jit_inferior_created_hook
, "jit");
1280 gdb::observers::inferior_execd
.attach (jit_inferior_created_hook
, "jit");
1281 gdb::observers::inferior_exit
.attach (jit_inferior_exit_hook
, "jit");
1282 gdb::observers::breakpoint_deleted
.attach (jit_breakpoint_deleted
, "jit");
1284 jit_gdbarch_data
= gdbarch_data_register_pre_init (jit_gdbarch_data_init
);
1285 if (is_dl_available ())
1287 struct cmd_list_element
*c
;
1289 c
= add_com ("jit-reader-load", no_class
, jit_reader_load_command
, _("\
1290 Load FILE as debug info reader and unwinder for JIT compiled code.\n\
1291 Usage: jit-reader-load FILE\n\
1292 Try to load file FILE as a debug info reader (and unwinder) for\n\
1293 JIT compiled code. The file is loaded from " JIT_READER_DIR
",\n\
1294 relocated relative to the GDB executable if required."));
1295 set_cmd_completer (c
, filename_completer
);
1297 c
= add_com ("jit-reader-unload", no_class
,
1298 jit_reader_unload_command
, _("\
1299 Unload the currently loaded JIT debug info reader.\n\
1300 Usage: jit-reader-unload\n\n\
1301 Do \"help jit-reader-load\" for info on loading debug info readers."));
1302 set_cmd_completer (c
, noop_completer
);