1 /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
3 Copyright (C) 2009-2020 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 *const jit_break_name
= "__jit_debug_register_code";
50 static const char *const jit_descriptor_name
= "__jit_debug_descriptor";
52 static void jit_inferior_init (struct gdbarch
*gdbarch
);
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 /* Non-zero if we want to see trace of jit level stuff. */
63 static unsigned int jit_debug
= 0;
66 show_jit_debug (struct ui_file
*file
, int from_tty
,
67 struct cmd_list_element
*c
, const char *value
)
69 fprintf_filtered (file
, _("JIT debugging is %s.\n"), value
);
78 /* Opening the file is a no-op. */
81 mem_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
86 /* Closing the file is just freeing the base/size pair on our side. */
89 mem_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
93 /* Zero means success. */
97 /* For reading the file, we just need to pass through to target_read_memory and
98 fix up the arguments and return values. */
101 mem_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
102 file_ptr nbytes
, file_ptr offset
)
105 struct target_buffer
*buffer
= (struct target_buffer
*) stream
;
107 /* If this read will read all of the file, limit it to just the rest. */
108 if (offset
+ nbytes
> buffer
->size
)
109 nbytes
= buffer
->size
- offset
;
111 /* If there are no more bytes left, we've reached EOF. */
115 err
= target_read_memory (buffer
->base
+ offset
, (gdb_byte
*) buf
, nbytes
);
122 /* For statting the file, we only support the st_size attribute. */
125 mem_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
127 struct target_buffer
*buffer
= (struct target_buffer
*) stream
;
129 memset (sb
, 0, sizeof (struct stat
));
130 sb
->st_size
= buffer
->size
;
134 /* Open a BFD from the target's memory. */
136 static gdb_bfd_ref_ptr
137 bfd_open_from_target_memory (CORE_ADDR addr
, ULONGEST size
,
140 struct target_buffer
*buffer
= XNEW (struct target_buffer
);
144 return gdb_bfd_openr_iovec ("<in-memory>", target
,
154 jit_reader (struct gdb_reader_funcs
*f
, gdb_dlhandle_up
&&h
)
155 : functions (f
), handle (std::move (h
))
161 functions
->destroy (functions
);
164 DISABLE_COPY_AND_ASSIGN (jit_reader
);
166 struct gdb_reader_funcs
*functions
;
167 gdb_dlhandle_up handle
;
170 /* One reader that has been loaded successfully, and can potentially be used to
173 static struct jit_reader
*loaded_jit_reader
= NULL
;
175 typedef struct gdb_reader_funcs
* (reader_init_fn_type
) (void);
176 static const char *reader_init_fn_sym
= "gdb_init_reader";
178 /* Try to load FILE_NAME as a JIT debug info reader. */
180 static struct jit_reader
*
181 jit_reader_load (const char *file_name
)
183 reader_init_fn_type
*init_fn
;
184 struct gdb_reader_funcs
*funcs
= NULL
;
187 fprintf_unfiltered (gdb_stdlog
, _("Opening shared object %s.\n"),
189 gdb_dlhandle_up so
= gdb_dlopen (file_name
);
191 init_fn
= (reader_init_fn_type
*) gdb_dlsym (so
, reader_init_fn_sym
);
193 error (_("Could not locate initialization function: %s."),
196 if (gdb_dlsym (so
, "plugin_is_GPL_compatible") == NULL
)
197 error (_("Reader not GPL compatible."));
200 if (funcs
->reader_version
!= GDB_READER_INTERFACE_VERSION
)
201 error (_("Reader version does not match GDB version."));
203 return new jit_reader (funcs
, std::move (so
));
206 /* Provides the jit-reader-load command. */
209 jit_reader_load_command (const char *args
, int from_tty
)
212 error (_("No reader name provided."));
213 gdb::unique_xmalloc_ptr
<char> file (tilde_expand (args
));
215 if (loaded_jit_reader
!= NULL
)
216 error (_("JIT reader already loaded. Run jit-reader-unload first."));
218 if (!IS_ABSOLUTE_PATH (file
.get ()))
219 file
.reset (xstrprintf ("%s%s%s", jit_reader_dir
.c_str (), SLASH_STRING
,
222 loaded_jit_reader
= jit_reader_load (file
.get ());
223 reinit_frame_cache ();
224 jit_inferior_created_hook ();
227 /* Provides the jit-reader-unload command. */
230 jit_reader_unload_command (const char *args
, int from_tty
)
232 if (!loaded_jit_reader
)
233 error (_("No JIT reader loaded."));
235 reinit_frame_cache ();
236 jit_inferior_exit_hook (current_inferior ());
238 delete loaded_jit_reader
;
239 loaded_jit_reader
= NULL
;
242 /* Destructor for jiter_objfile_data. */
244 jiter_objfile_data::~jiter_objfile_data ()
246 if (this->jit_breakpoint
!= nullptr)
247 delete_breakpoint (this->jit_breakpoint
);
250 /* Fetch the jiter_objfile_data associated with OBJF. If no data exists
251 yet, make a new structure and attach it. */
253 static jiter_objfile_data
*
254 get_jiter_objfile_data (objfile
*objf
)
256 if (objf
->jiter_data
== nullptr)
257 objf
->jiter_data
.reset (new jiter_objfile_data (objf
));
259 return objf
->jiter_data
.get ();
262 /* Remember OBJFILE has been created for struct jit_code_entry located
263 at inferior address ENTRY. */
266 add_objfile_entry (struct objfile
*objfile
, CORE_ADDR entry
)
268 gdb_assert (objfile
->jited_data
== nullptr);
270 objfile
->jited_data
.reset (new jited_objfile_data (entry
));
273 /* Helper function for reading the global JIT descriptor from remote
274 memory. Returns true if all went well, false otherwise. */
277 jit_read_descriptor (gdbarch
*gdbarch
,
278 jit_descriptor
*descriptor
,
282 struct type
*ptr_type
;
286 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
288 gdb_assert (jiter
!= nullptr);
289 jiter_objfile_data
*objf_data
= jiter
->jiter_data
.get ();
290 gdb_assert (objf_data
!= nullptr);
293 fprintf_unfiltered (gdb_stdlog
,
294 "jit_read_descriptor, descriptor_addr = %s\n",
295 paddress (gdbarch
, MSYMBOL_VALUE_ADDRESS (jiter
,
296 objf_data
->descriptor
)));
298 /* Figure out how big the descriptor is on the remote and how to read it. */
299 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
300 ptr_size
= TYPE_LENGTH (ptr_type
);
301 desc_size
= 8 + 2 * ptr_size
; /* Two 32-bit ints and two pointers. */
302 desc_buf
= (gdb_byte
*) alloca (desc_size
);
304 /* Read the descriptor. */
305 err
= target_read_memory (MSYMBOL_VALUE_ADDRESS (jiter
,
306 objf_data
->descriptor
),
307 desc_buf
, desc_size
);
310 printf_unfiltered (_("Unable to read JIT descriptor from "
315 /* Fix the endianness to match the host. */
316 descriptor
->version
= extract_unsigned_integer (&desc_buf
[0], 4, byte_order
);
317 descriptor
->action_flag
=
318 extract_unsigned_integer (&desc_buf
[4], 4, byte_order
);
319 descriptor
->relevant_entry
= extract_typed_address (&desc_buf
[8], ptr_type
);
320 descriptor
->first_entry
=
321 extract_typed_address (&desc_buf
[8 + ptr_size
], ptr_type
);
326 /* Helper function for reading a JITed code entry from remote memory. */
329 jit_read_code_entry (struct gdbarch
*gdbarch
,
330 CORE_ADDR code_addr
, struct jit_code_entry
*code_entry
)
333 struct type
*ptr_type
;
338 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
340 /* Figure out how big the entry is on the remote and how to read it. */
341 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
342 ptr_size
= TYPE_LENGTH (ptr_type
);
344 /* Figure out where the uint64_t value will be. */
345 align_bytes
= type_align (builtin_type (gdbarch
)->builtin_uint64
);
347 off
= (off
+ (align_bytes
- 1)) & ~(align_bytes
- 1);
349 entry_size
= off
+ 8; /* Three pointers and one 64-bit int. */
350 entry_buf
= (gdb_byte
*) alloca (entry_size
);
352 /* Read the entry. */
353 err
= target_read_memory (code_addr
, entry_buf
, entry_size
);
355 error (_("Unable to read JIT code entry from remote memory!"));
357 /* Fix the endianness to match the host. */
358 ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
359 code_entry
->next_entry
= extract_typed_address (&entry_buf
[0], ptr_type
);
360 code_entry
->prev_entry
=
361 extract_typed_address (&entry_buf
[ptr_size
], ptr_type
);
362 code_entry
->symfile_addr
=
363 extract_typed_address (&entry_buf
[2 * ptr_size
], ptr_type
);
364 code_entry
->symfile_size
=
365 extract_unsigned_integer (&entry_buf
[off
], 8, byte_order
);
368 /* Proxy object for building a block. */
372 gdb_block (gdb_block
*parent
, CORE_ADDR begin
, CORE_ADDR end
,
377 name (name
!= nullptr ? xstrdup (name
) : nullptr)
380 /* The parent of this block. */
381 struct gdb_block
*parent
;
383 /* Points to the "real" block that is being built out of this
384 instance. This block will be added to a blockvector, which will
385 then be added to a symtab. */
386 struct block
*real_block
= nullptr;
388 /* The first and last code address corresponding to this block. */
389 CORE_ADDR begin
, end
;
391 /* The name of this block (if any). If this is non-NULL, the
392 FUNCTION symbol symbol is set to this value. */
393 gdb::unique_xmalloc_ptr
<char> name
;
396 /* Proxy object for building a symtab. */
400 explicit gdb_symtab (const char *file_name
)
401 : file_name (file_name
!= nullptr ? file_name
: "")
404 /* The list of blocks in this symtab. These will eventually be
405 converted to real blocks.
407 This is specifically a linked list, instead of, for example, a vector,
408 because the pointers are returned to the user's debug info reader. So
409 it's important that the objects don't change location during their
410 lifetime (which would happen with a vector of objects getting resized). */
411 std::forward_list
<gdb_block
> blocks
;
413 /* The number of blocks inserted. */
416 /* A mapping between line numbers to PC. */
417 gdb::unique_xmalloc_ptr
<struct linetable
> linetable
;
419 /* The source file for this symtab. */
420 std::string file_name
;
423 /* Proxy object for building an object. */
427 /* Symtabs of this object.
429 This is specifically a linked list, instead of, for example, a vector,
430 because the pointers are returned to the user's debug info reader. So
431 it's important that the objects don't change location during their
432 lifetime (which would happen with a vector of objects getting resized). */
433 std::forward_list
<gdb_symtab
> symtabs
;
436 /* The type of the `private' data passed around by the callback
439 typedef CORE_ADDR jit_dbg_reader_data
;
441 /* The reader calls into this function to read data off the targets
444 static enum gdb_status
445 jit_target_read_impl (GDB_CORE_ADDR target_mem
, void *gdb_buf
, int len
)
447 int result
= target_read_memory ((CORE_ADDR
) target_mem
,
448 (gdb_byte
*) gdb_buf
, len
);
455 /* The reader calls into this function to create a new gdb_object
456 which it can then pass around to the other callbacks. Right now,
457 all that is required is allocating the memory. */
459 static struct gdb_object
*
460 jit_object_open_impl (struct gdb_symbol_callbacks
*cb
)
462 /* CB is not required right now, but sometime in the future we might
463 need a handle to it, and we'd like to do that without breaking
465 return new gdb_object
;
468 /* Readers call into this function to open a new gdb_symtab, which,
469 again, is passed around to other callbacks. */
471 static struct gdb_symtab
*
472 jit_symtab_open_impl (struct gdb_symbol_callbacks
*cb
,
473 struct gdb_object
*object
,
474 const char *file_name
)
476 /* CB stays unused. See comment in jit_object_open_impl. */
478 object
->symtabs
.emplace_front (file_name
);
479 return &object
->symtabs
.front ();
482 /* Called by readers to open a new gdb_block. This function also
483 inserts the new gdb_block in the correct place in the corresponding
486 static struct gdb_block
*
487 jit_block_open_impl (struct gdb_symbol_callbacks
*cb
,
488 struct gdb_symtab
*symtab
, struct gdb_block
*parent
,
489 GDB_CORE_ADDR begin
, GDB_CORE_ADDR end
, const char *name
)
491 /* Place the block at the beginning of the list, it will be sorted when the
492 symtab is finalized. */
493 symtab
->blocks
.emplace_front (parent
, begin
, end
, name
);
496 return &symtab
->blocks
.front ();
499 /* Readers call this to add a line mapping (from PC to line number) to
503 jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks
*cb
,
504 struct gdb_symtab
*stab
, int nlines
,
505 struct gdb_line_mapping
*map
)
513 alloc_len
= sizeof (struct linetable
)
514 + (nlines
- 1) * sizeof (struct linetable_entry
);
515 stab
->linetable
.reset (XNEWVAR (struct linetable
, alloc_len
));
516 stab
->linetable
->nitems
= nlines
;
517 for (i
= 0; i
< nlines
; i
++)
519 stab
->linetable
->item
[i
].pc
= (CORE_ADDR
) map
[i
].pc
;
520 stab
->linetable
->item
[i
].line
= map
[i
].line
;
521 stab
->linetable
->item
[i
].is_stmt
= 1;
525 /* Called by readers to close a gdb_symtab. Does not need to do
526 anything as of now. */
529 jit_symtab_close_impl (struct gdb_symbol_callbacks
*cb
,
530 struct gdb_symtab
*stab
)
532 /* Right now nothing needs to be done here. We may need to do some
533 cleanup here in the future (again, without breaking the plugin
537 /* Transform STAB to a proper symtab, and add it it OBJFILE. */
540 finalize_symtab (struct gdb_symtab
*stab
, struct objfile
*objfile
)
542 struct compunit_symtab
*cust
;
543 size_t blockvector_size
;
544 CORE_ADDR begin
, end
;
545 struct blockvector
*bv
;
547 int actual_nblocks
= FIRST_LOCAL_BLOCK
+ stab
->nblocks
;
549 /* Sort the blocks in the order they should appear in the blockvector. */
550 stab
->blocks
.sort([] (const gdb_block
&a
, const gdb_block
&b
)
552 if (a
.begin
!= b
.begin
)
553 return a
.begin
< b
.begin
;
555 return a
.end
> b
.end
;
558 cust
= allocate_compunit_symtab (objfile
, stab
->file_name
.c_str ());
559 allocate_symtab (cust
, stab
->file_name
.c_str ());
560 add_compunit_symtab_to_objfile (cust
);
562 /* JIT compilers compile in memory. */
563 COMPUNIT_DIRNAME (cust
) = NULL
;
565 /* Copy over the linetable entry if one was provided. */
568 size_t size
= ((stab
->linetable
->nitems
- 1)
569 * sizeof (struct linetable_entry
)
570 + sizeof (struct linetable
));
571 SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust
))
572 = (struct linetable
*) obstack_alloc (&objfile
->objfile_obstack
, size
);
573 memcpy (SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust
)),
574 stab
->linetable
.get (), size
);
577 blockvector_size
= (sizeof (struct blockvector
)
578 + (actual_nblocks
- 1) * sizeof (struct block
*));
579 bv
= (struct blockvector
*) obstack_alloc (&objfile
->objfile_obstack
,
581 COMPUNIT_BLOCKVECTOR (cust
) = bv
;
583 /* At the end of this function, (begin, end) will contain the PC range this
584 entire blockvector spans. */
585 BLOCKVECTOR_MAP (bv
) = NULL
;
586 begin
= stab
->blocks
.front ().begin
;
587 end
= stab
->blocks
.front ().end
;
588 BLOCKVECTOR_NBLOCKS (bv
) = actual_nblocks
;
590 /* First run over all the gdb_block objects, creating a real block
591 object for each. Simultaneously, keep setting the real_block
593 int block_idx
= FIRST_LOCAL_BLOCK
;
594 for (gdb_block
&gdb_block_iter
: stab
->blocks
)
596 struct block
*new_block
= allocate_block (&objfile
->objfile_obstack
);
597 struct symbol
*block_name
= new (&objfile
->objfile_obstack
) symbol
;
598 struct type
*block_type
= arch_type (objfile
->arch (),
603 BLOCK_MULTIDICT (new_block
)
604 = mdict_create_linear (&objfile
->objfile_obstack
, NULL
);
605 /* The address range. */
606 BLOCK_START (new_block
) = (CORE_ADDR
) gdb_block_iter
.begin
;
607 BLOCK_END (new_block
) = (CORE_ADDR
) gdb_block_iter
.end
;
610 SYMBOL_DOMAIN (block_name
) = VAR_DOMAIN
;
611 SYMBOL_ACLASS_INDEX (block_name
) = LOC_BLOCK
;
612 symbol_set_symtab (block_name
, COMPUNIT_FILETABS (cust
));
613 SYMBOL_TYPE (block_name
) = lookup_function_type (block_type
);
614 SYMBOL_BLOCK_VALUE (block_name
) = new_block
;
616 block_name
->m_name
= obstack_strdup (&objfile
->objfile_obstack
,
617 gdb_block_iter
.name
.get ());
619 BLOCK_FUNCTION (new_block
) = block_name
;
621 BLOCKVECTOR_BLOCK (bv
, block_idx
) = new_block
;
622 if (begin
> BLOCK_START (new_block
))
623 begin
= BLOCK_START (new_block
);
624 if (end
< BLOCK_END (new_block
))
625 end
= BLOCK_END (new_block
);
627 gdb_block_iter
.real_block
= new_block
;
632 /* Now add the special blocks. */
633 struct block
*block_iter
= NULL
;
634 for (enum block_enum i
: { GLOBAL_BLOCK
, STATIC_BLOCK
})
636 struct block
*new_block
;
638 new_block
= (i
== GLOBAL_BLOCK
639 ? allocate_global_block (&objfile
->objfile_obstack
)
640 : allocate_block (&objfile
->objfile_obstack
));
641 BLOCK_MULTIDICT (new_block
)
642 = mdict_create_linear (&objfile
->objfile_obstack
, NULL
);
643 BLOCK_SUPERBLOCK (new_block
) = block_iter
;
644 block_iter
= new_block
;
646 BLOCK_START (new_block
) = (CORE_ADDR
) begin
;
647 BLOCK_END (new_block
) = (CORE_ADDR
) end
;
649 BLOCKVECTOR_BLOCK (bv
, i
) = new_block
;
651 if (i
== GLOBAL_BLOCK
)
652 set_block_compunit_symtab (new_block
, cust
);
655 /* Fill up the superblock fields for the real blocks, using the
656 real_block fields populated earlier. */
657 for (gdb_block
&gdb_block_iter
: stab
->blocks
)
659 if (gdb_block_iter
.parent
!= NULL
)
661 /* If the plugin specifically mentioned a parent block, we
663 BLOCK_SUPERBLOCK (gdb_block_iter
.real_block
) =
664 gdb_block_iter
.parent
->real_block
;
668 /* And if not, we set a default parent block. */
669 BLOCK_SUPERBLOCK (gdb_block_iter
.real_block
) =
670 BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
675 /* Called when closing a gdb_objfile. Converts OBJ to a proper
679 jit_object_close_impl (struct gdb_symbol_callbacks
*cb
,
680 struct gdb_object
*obj
)
682 struct objfile
*objfile
;
683 jit_dbg_reader_data
*priv_data
;
685 priv_data
= (jit_dbg_reader_data
*) cb
->priv_data
;
687 objfile
= objfile::make (nullptr, "<< JIT compiled code >>",
689 objfile
->per_bfd
->gdbarch
= target_gdbarch ();
691 for (gdb_symtab
&symtab
: obj
->symtabs
)
692 finalize_symtab (&symtab
, objfile
);
694 add_objfile_entry (objfile
, *priv_data
);
699 /* Try to read CODE_ENTRY using the loaded jit reader (if any).
700 ENTRY_ADDR is the address of the struct jit_code_entry in the
701 inferior address space. */
704 jit_reader_try_read_symtab (struct jit_code_entry
*code_entry
,
705 CORE_ADDR entry_addr
)
708 jit_dbg_reader_data priv_data
;
709 struct gdb_reader_funcs
*funcs
;
710 struct gdb_symbol_callbacks callbacks
=
712 jit_object_open_impl
,
713 jit_symtab_open_impl
,
715 jit_symtab_close_impl
,
716 jit_object_close_impl
,
718 jit_symtab_line_mapping_add_impl
,
719 jit_target_read_impl
,
724 priv_data
= entry_addr
;
726 if (!loaded_jit_reader
)
729 gdb::byte_vector
gdb_mem (code_entry
->symfile_size
);
734 if (target_read_memory (code_entry
->symfile_addr
, gdb_mem
.data (),
735 code_entry
->symfile_size
))
738 catch (const gdb_exception
&e
)
745 funcs
= loaded_jit_reader
->functions
;
746 if (funcs
->read (funcs
, &callbacks
, gdb_mem
.data (),
747 code_entry
->symfile_size
)
752 if (jit_debug
&& status
== 0)
753 fprintf_unfiltered (gdb_stdlog
,
754 "Could not read symtab using the loaded JIT reader.\n");
758 /* Try to read CODE_ENTRY using BFD. ENTRY_ADDR is the address of the
759 struct jit_code_entry in the inferior address space. */
762 jit_bfd_try_read_symtab (struct jit_code_entry
*code_entry
,
763 CORE_ADDR entry_addr
,
764 struct gdbarch
*gdbarch
)
766 struct bfd_section
*sec
;
767 struct objfile
*objfile
;
768 const struct bfd_arch_info
*b
;
771 fprintf_unfiltered (gdb_stdlog
,
772 "jit_bfd_try_read_symtab, symfile_addr = %s, "
773 "symfile_size = %s\n",
774 paddress (gdbarch
, code_entry
->symfile_addr
),
775 pulongest (code_entry
->symfile_size
));
777 gdb_bfd_ref_ptr
nbfd (bfd_open_from_target_memory (code_entry
->symfile_addr
,
778 code_entry
->symfile_size
,
782 puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
786 /* Check the format. NOTE: This initializes important data that GDB uses!
787 We would segfault later without this line. */
788 if (!bfd_check_format (nbfd
.get (), bfd_object
))
790 printf_unfiltered (_("\
791 JITed symbol file is not an object file, ignoring it.\n"));
795 /* Check bfd arch. */
796 b
= gdbarch_bfd_arch_info (gdbarch
);
797 if (b
->compatible (b
, bfd_get_arch_info (nbfd
.get ())) != b
)
798 warning (_("JITed object file architecture %s is not compatible "
799 "with target architecture %s."),
800 bfd_get_arch_info (nbfd
.get ())->printable_name
,
803 /* Read the section address information out of the symbol file. Since the
804 file is generated by the JIT at runtime, it should all of the absolute
805 addresses that we care about. */
806 section_addr_info sai
;
807 for (sec
= nbfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
808 if ((bfd_section_flags (sec
) & (SEC_ALLOC
|SEC_LOAD
)) != 0)
810 /* We assume that these virtual addresses are absolute, and do not
811 treat them as offsets. */
812 sai
.emplace_back (bfd_section_vma (sec
),
813 bfd_section_name (sec
),
817 /* This call does not take ownership of SAI. */
818 objfile
= symbol_file_add_from_bfd (nbfd
.get (),
819 bfd_get_filename (nbfd
.get ()), 0,
821 OBJF_SHARED
| OBJF_NOT_FILENAME
, NULL
);
823 add_objfile_entry (objfile
, entry_addr
);
826 /* This function registers code associated with a JIT code entry. It uses the
827 pointer and size pair in the entry to read the symbol file from the remote
828 and then calls symbol_file_add_from_local_memory to add it as though it were
829 a symbol file added by the user. */
832 jit_register_code (struct gdbarch
*gdbarch
,
833 CORE_ADDR entry_addr
, struct jit_code_entry
*code_entry
)
838 fprintf_unfiltered (gdb_stdlog
,
839 "jit_register_code, symfile_addr = %s, "
840 "symfile_size = %s\n",
841 paddress (gdbarch
, code_entry
->symfile_addr
),
842 pulongest (code_entry
->symfile_size
));
844 success
= jit_reader_try_read_symtab (code_entry
, entry_addr
);
847 jit_bfd_try_read_symtab (code_entry
, entry_addr
, gdbarch
);
850 /* Look up the objfile with this code entry address. */
852 static struct objfile
*
853 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr
)
855 for (objfile
*objf
: current_program_space
->objfiles ())
857 if (objf
->jited_data
!= nullptr && objf
->jited_data
->addr
== entry_addr
)
864 /* This is called when a breakpoint is deleted. It updates the
865 inferior's cache, if needed. */
868 jit_breakpoint_deleted (struct breakpoint
*b
)
870 struct bp_location
*iter
;
872 if (b
->type
!= bp_jit_event
)
875 for (iter
= b
->loc
; iter
!= NULL
; iter
= iter
->next
)
877 for (objfile
*objf
: iter
->pspace
->objfiles ())
879 jiter_objfile_data
*jiter_data
= objf
->jiter_data
.get ();
881 if (jiter_data
!= nullptr
882 && jiter_data
->jit_breakpoint
== iter
->owner
)
884 jiter_data
->cached_code_address
= 0;
885 jiter_data
->jit_breakpoint
= nullptr;
891 /* (Re-)Initialize the jit breakpoints for JIT-producing objfiles in
895 jit_breakpoint_re_set_internal (struct gdbarch
*gdbarch
, program_space
*pspace
)
897 jiter_objfile_data
*objf_data
;
899 for (objfile
*the_objfile
: pspace
->objfiles ())
901 /* Lookup the registration symbol. If it is missing, then we
902 assume we are not attached to a JIT. */
903 bound_minimal_symbol reg_symbol
904 = lookup_minimal_symbol (jit_break_name
, nullptr, the_objfile
);
905 if (reg_symbol
.minsym
== NULL
906 || BMSYMBOL_VALUE_ADDRESS (reg_symbol
) == 0)
909 bound_minimal_symbol desc_symbol
910 = lookup_minimal_symbol (jit_descriptor_name
, NULL
, the_objfile
);
911 if (desc_symbol
.minsym
== NULL
912 || BMSYMBOL_VALUE_ADDRESS (desc_symbol
) == 0)
915 objf_data
= get_jiter_objfile_data (reg_symbol
.objfile
);
916 objf_data
->register_code
= reg_symbol
.minsym
;
917 objf_data
->descriptor
= desc_symbol
.minsym
;
919 CORE_ADDR addr
= MSYMBOL_VALUE_ADDRESS (the_objfile
,
920 objf_data
->register_code
);
923 fprintf_unfiltered (gdb_stdlog
,
924 "jit_breakpoint_re_set_internal, "
925 "breakpoint_addr = %s\n",
926 paddress (gdbarch
, addr
));
928 /* Check if we need to re-create the breakpoint. */
929 if (objf_data
->cached_code_address
== addr
)
932 /* Delete the old breakpoint. */
933 if (objf_data
->jit_breakpoint
!= nullptr)
934 delete_breakpoint (objf_data
->jit_breakpoint
);
936 /* Put a breakpoint in the registration symbol. */
937 objf_data
->cached_code_address
= addr
;
938 objf_data
->jit_breakpoint
= create_jit_event_breakpoint (gdbarch
, addr
);
942 /* The private data passed around in the frame unwind callback
945 struct jit_unwind_private
947 /* Cached register values. See jit_frame_sniffer to see how this
949 detached_regcache
*regcache
;
951 /* The frame being unwound. */
952 struct frame_info
*this_frame
;
955 /* Sets the value of a particular register in this frame. */
958 jit_unwind_reg_set_impl (struct gdb_unwind_callbacks
*cb
, int dwarf_regnum
,
959 struct gdb_reg_value
*value
)
961 struct jit_unwind_private
*priv
;
964 priv
= (struct jit_unwind_private
*) cb
->priv_data
;
966 gdb_reg
= gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv
->this_frame
),
971 fprintf_unfiltered (gdb_stdlog
,
972 _("Could not recognize DWARF regnum %d"),
978 priv
->regcache
->raw_supply (gdb_reg
, value
->value
);
983 reg_value_free_impl (struct gdb_reg_value
*value
)
988 /* Get the value of register REGNUM in the previous frame. */
990 static struct gdb_reg_value
*
991 jit_unwind_reg_get_impl (struct gdb_unwind_callbacks
*cb
, int regnum
)
993 struct jit_unwind_private
*priv
;
994 struct gdb_reg_value
*value
;
996 struct gdbarch
*frame_arch
;
998 priv
= (struct jit_unwind_private
*) cb
->priv_data
;
999 frame_arch
= get_frame_arch (priv
->this_frame
);
1001 gdb_reg
= gdbarch_dwarf2_reg_to_regnum (frame_arch
, regnum
);
1002 size
= register_size (frame_arch
, gdb_reg
);
1003 value
= ((struct gdb_reg_value
*)
1004 xmalloc (sizeof (struct gdb_reg_value
) + size
- 1));
1005 value
->defined
= deprecated_frame_register_read (priv
->this_frame
, gdb_reg
,
1008 value
->free
= reg_value_free_impl
;
1012 /* gdb_reg_value has a free function, which must be called on each
1013 saved register value. */
1016 jit_dealloc_cache (struct frame_info
*this_frame
, void *cache
)
1018 struct jit_unwind_private
*priv_data
= (struct jit_unwind_private
*) cache
;
1020 gdb_assert (priv_data
->regcache
!= NULL
);
1021 delete priv_data
->regcache
;
1025 /* The frame sniffer for the pseudo unwinder.
1027 While this is nominally a frame sniffer, in the case where the JIT
1028 reader actually recognizes the frame, it does a lot more work -- it
1029 unwinds the frame and saves the corresponding register values in
1030 the cache. jit_frame_prev_register simply returns the saved
1034 jit_frame_sniffer (const struct frame_unwind
*self
,
1035 struct frame_info
*this_frame
, void **cache
)
1037 struct jit_unwind_private
*priv_data
;
1038 struct gdb_unwind_callbacks callbacks
;
1039 struct gdb_reader_funcs
*funcs
;
1041 callbacks
.reg_get
= jit_unwind_reg_get_impl
;
1042 callbacks
.reg_set
= jit_unwind_reg_set_impl
;
1043 callbacks
.target_read
= jit_target_read_impl
;
1045 if (loaded_jit_reader
== NULL
)
1048 funcs
= loaded_jit_reader
->functions
;
1050 gdb_assert (!*cache
);
1052 *cache
= XCNEW (struct jit_unwind_private
);
1053 priv_data
= (struct jit_unwind_private
*) *cache
;
1054 /* Take a snapshot of current regcache. */
1055 priv_data
->regcache
= new detached_regcache (get_frame_arch (this_frame
),
1057 priv_data
->this_frame
= this_frame
;
1059 callbacks
.priv_data
= priv_data
;
1061 /* Try to coax the provided unwinder to unwind the stack */
1062 if (funcs
->unwind (funcs
, &callbacks
) == GDB_SUCCESS
)
1065 fprintf_unfiltered (gdb_stdlog
, _("Successfully unwound frame using "
1070 fprintf_unfiltered (gdb_stdlog
, _("Could not unwind frame using "
1073 jit_dealloc_cache (this_frame
, *cache
);
1080 /* The frame_id function for the pseudo unwinder. Relays the call to
1081 the loaded plugin. */
1084 jit_frame_this_id (struct frame_info
*this_frame
, void **cache
,
1085 struct frame_id
*this_id
)
1087 struct jit_unwind_private priv
;
1088 struct gdb_frame_id frame_id
;
1089 struct gdb_reader_funcs
*funcs
;
1090 struct gdb_unwind_callbacks callbacks
;
1092 priv
.regcache
= NULL
;
1093 priv
.this_frame
= this_frame
;
1095 /* We don't expect the frame_id function to set any registers, so we
1096 set reg_set to NULL. */
1097 callbacks
.reg_get
= jit_unwind_reg_get_impl
;
1098 callbacks
.reg_set
= NULL
;
1099 callbacks
.target_read
= jit_target_read_impl
;
1100 callbacks
.priv_data
= &priv
;
1102 gdb_assert (loaded_jit_reader
);
1103 funcs
= loaded_jit_reader
->functions
;
1105 frame_id
= funcs
->get_frame_id (funcs
, &callbacks
);
1106 *this_id
= frame_id_build (frame_id
.stack_address
, frame_id
.code_address
);
1109 /* Pseudo unwinder function. Reads the previously fetched value for
1110 the register from the cache. */
1112 static struct value
*
1113 jit_frame_prev_register (struct frame_info
*this_frame
, void **cache
, int reg
)
1115 struct jit_unwind_private
*priv
= (struct jit_unwind_private
*) *cache
;
1116 struct gdbarch
*gdbarch
;
1119 return frame_unwind_got_optimized (this_frame
, reg
);
1121 gdbarch
= priv
->regcache
->arch ();
1122 gdb_byte
*buf
= (gdb_byte
*) alloca (register_size (gdbarch
, reg
));
1123 enum register_status status
= priv
->regcache
->cooked_read (reg
, buf
);
1125 if (status
== REG_VALID
)
1126 return frame_unwind_got_bytes (this_frame
, reg
, buf
);
1128 return frame_unwind_got_optimized (this_frame
, reg
);
1131 /* Relay everything back to the unwinder registered by the JIT debug
1134 static const struct frame_unwind jit_frame_unwind
=
1137 default_frame_unwind_stop_reason
,
1139 jit_frame_prev_register
,
1146 /* This is the information that is stored at jit_gdbarch_data for each
1149 struct jit_gdbarch_data_type
1151 /* Has the (pseudo) unwinder been prepended? */
1152 int unwinder_registered
;
1155 /* Check GDBARCH and prepend the pseudo JIT unwinder if needed. */
1158 jit_prepend_unwinder (struct gdbarch
*gdbarch
)
1160 struct jit_gdbarch_data_type
*data
;
1163 = (struct jit_gdbarch_data_type
*) gdbarch_data (gdbarch
, jit_gdbarch_data
);
1164 if (!data
->unwinder_registered
)
1166 frame_unwind_prepend_unwinder (gdbarch
, &jit_frame_unwind
);
1167 data
->unwinder_registered
= 1;
1171 /* Register any already created translations. */
1174 jit_inferior_init (struct gdbarch
*gdbarch
)
1176 struct jit_descriptor descriptor
;
1177 struct jit_code_entry cur_entry
;
1178 CORE_ADDR cur_entry_addr
;
1181 fprintf_unfiltered (gdb_stdlog
, "jit_inferior_init\n");
1183 jit_prepend_unwinder (gdbarch
);
1185 jit_breakpoint_re_set_internal (gdbarch
, current_program_space
);
1187 for (objfile
*jiter
: current_program_space
->objfiles ())
1189 if (jiter
->jiter_data
== nullptr)
1192 /* Read the descriptor so we can check the version number and load
1193 any already JITed functions. */
1194 if (!jit_read_descriptor (gdbarch
, &descriptor
, jiter
))
1197 /* Check that the version number agrees with that we support. */
1198 if (descriptor
.version
!= 1)
1200 printf_unfiltered (_("Unsupported JIT protocol version %ld "
1201 "in descriptor (expected 1)\n"),
1202 (long) descriptor
.version
);
1206 /* If we've attached to a running program, we need to check the
1207 descriptor to register any functions that were already
1209 for (cur_entry_addr
= descriptor
.first_entry
;
1210 cur_entry_addr
!= 0;
1211 cur_entry_addr
= cur_entry
.next_entry
)
1213 jit_read_code_entry (gdbarch
, cur_entry_addr
, &cur_entry
);
1215 /* This hook may be called many times during setup, so make sure
1216 we don't add the same symbol file twice. */
1217 if (jit_find_objf_with_entry_addr (cur_entry_addr
) != NULL
)
1220 jit_register_code (gdbarch
, cur_entry_addr
, &cur_entry
);
1225 /* inferior_created observer. */
1228 jit_inferior_created (struct target_ops
*ops
, int from_tty
)
1230 jit_inferior_created_hook ();
1233 /* Exported routine to call when an inferior has been created. */
1236 jit_inferior_created_hook (void)
1238 jit_inferior_init (target_gdbarch ());
1241 /* Exported routine to call to re-set the jit breakpoints,
1242 e.g. when a program is rerun. */
1245 jit_breakpoint_re_set (void)
1247 jit_breakpoint_re_set_internal (target_gdbarch (), current_program_space
);
1250 /* This function cleans up any code entries left over when the
1251 inferior exits. We get left over code when the inferior exits
1252 without unregistering its code, for example when it crashes. */
1255 jit_inferior_exit_hook (struct inferior
*inf
)
1257 for (objfile
*objf
: current_program_space
->objfiles_safe ())
1259 if (objf
->jited_data
!= nullptr && objf
->jited_data
->addr
!= 0)
1265 jit_event_handler (gdbarch
*gdbarch
, objfile
*jiter
)
1267 struct jit_descriptor descriptor
;
1268 struct jit_code_entry code_entry
;
1269 CORE_ADDR entry_addr
;
1270 struct objfile
*objf
;
1272 /* If we get a JIT breakpoint event for this objfile, it is necessarily a
1274 gdb_assert (jiter
->jiter_data
!= nullptr);
1276 /* Read the descriptor from remote memory. */
1277 if (!jit_read_descriptor (gdbarch
, &descriptor
, jiter
))
1279 entry_addr
= descriptor
.relevant_entry
;
1281 /* Do the corresponding action. */
1282 switch (descriptor
.action_flag
)
1287 jit_read_code_entry (gdbarch
, entry_addr
, &code_entry
);
1288 jit_register_code (gdbarch
, entry_addr
, &code_entry
);
1290 case JIT_UNREGISTER
:
1291 objf
= jit_find_objf_with_entry_addr (entry_addr
);
1293 printf_unfiltered (_("Unable to find JITed code "
1294 "entry at address: %s\n"),
1295 paddress (gdbarch
, entry_addr
));
1301 error (_("Unknown action_flag value in JIT descriptor!"));
1306 /* Initialize the jit_gdbarch_data slot with an instance of struct
1307 jit_gdbarch_data_type */
1310 jit_gdbarch_data_init (struct obstack
*obstack
)
1312 struct jit_gdbarch_data_type
*data
=
1313 XOBNEW (obstack
, struct jit_gdbarch_data_type
);
1315 data
->unwinder_registered
= 0;
1320 void _initialize_jit ();
1324 jit_reader_dir
= relocate_gdb_directory (JIT_READER_DIR
,
1325 JIT_READER_DIR_RELOCATABLE
);
1326 add_setshow_zuinteger_cmd ("jit", class_maintenance
, &jit_debug
,
1327 _("Set JIT debugging."),
1328 _("Show JIT debugging."),
1329 _("When non-zero, JIT debugging is enabled."),
1332 &setdebuglist
, &showdebuglist
);
1334 gdb::observers::inferior_created
.attach (jit_inferior_created
);
1335 gdb::observers::inferior_exit
.attach (jit_inferior_exit_hook
);
1336 gdb::observers::breakpoint_deleted
.attach (jit_breakpoint_deleted
);
1338 jit_gdbarch_data
= gdbarch_data_register_pre_init (jit_gdbarch_data_init
);
1339 if (is_dl_available ())
1341 struct cmd_list_element
*c
;
1343 c
= add_com ("jit-reader-load", no_class
, jit_reader_load_command
, _("\
1344 Load FILE as debug info reader and unwinder for JIT compiled code.\n\
1345 Usage: jit-reader-load FILE\n\
1346 Try to load file FILE as a debug info reader (and unwinder) for\n\
1347 JIT compiled code. The file is loaded from " JIT_READER_DIR
",\n\
1348 relocated relative to the GDB executable if required."));
1349 set_cmd_completer (c
, filename_completer
);
1351 c
= add_com ("jit-reader-unload", no_class
,
1352 jit_reader_unload_command
, _("\
1353 Unload the currently loaded JIT debug info reader.\n\
1354 Usage: jit-reader-unload\n\n\
1355 Do \"help jit-reader-load\" for info on loading debug info readers."));
1356 set_cmd_completer (c
, noop_completer
);