]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/jit.c
gdb: include jit_code_entry::symfile_addr value in names of objfiles created by jit...
[thirdparty/binutils-gdb.git] / gdb / jit.c
CommitLineData
4efc6507
DE
1/* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
2
4a94e368 3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4efc6507
DE
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21
22#include "jit.h"
f997c383 23#include "jit-reader.h"
1825a88d 24#include "block.h"
4efc6507 25#include "breakpoint.h"
a255712f 26#include "command.h"
1825a88d 27#include "dictionary.h"
c9fb1240 28#include "filenames.h"
1825a88d 29#include "frame-unwind.h"
a255712f 30#include "gdbcmd.h"
4efc6507 31#include "gdbcore.h"
03673fc7 32#include "inferior.h"
76727919 33#include "observable.h"
4efc6507 34#include "objfiles.h"
3623dc3a 35#include "regcache.h"
4efc6507
DE
36#include "symfile.h"
37#include "symtab.h"
38#include "target.h"
2d41fa11 39#include "gdbsupport/gdb-dlfcn.h"
53ce3c39 40#include <sys/stat.h>
cbb099e8 41#include "gdb_bfd.h"
6571a381
TT
42#include "readline/tilde.h"
43#include "completer.h"
1b61f46d 44#include <forward_list>
4efc6507 45
f2aec7f6 46static std::string jit_reader_dir;
b8e0a31c 47
db92ac45 48static const char jit_break_name[] = "__jit_debug_register_code";
4efc6507 49
db92ac45 50static const char jit_descriptor_name[] = "__jit_debug_descriptor";
4efc6507 51
42a4fec5 52static void jit_inferior_created_hook (inferior *inf);
20aa2c60 53static void jit_inferior_exit_hook (struct inferior *inf);
3b2a0cf2 54
3623dc3a
SD
55/* An unwinder is registered for every gdbarch. This key is used to
56 remember if the unwinder has been registered for a particular
57 gdbarch. */
58
59static struct gdbarch_data *jit_gdbarch_data;
60
062eaacb 61/* True if we want to see trace of jit level stuff. */
a255712f 62
062eaacb 63static bool jit_debug = false;
a255712f 64
54ca9002
SM
65/* Print a "jit" debug statement. */
66
67#define jit_debug_printf(fmt, ...) \
68 debug_prefixed_printf_cond (jit_debug, "jit", fmt, ##__VA_ARGS__)
69
a255712f
PP
70static void
71show_jit_debug (struct ui_file *file, int from_tty,
72 struct cmd_list_element *c, const char *value)
73{
74 fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
75}
76
ff770835
SM
77/* Implementation of the "maintenance info jit" command. */
78
79static void
80maint_info_jit_cmd (const char *args, int from_tty)
81{
82 inferior *inf = current_inferior ();
83 bool printed_header = false;
84
85 /* Print a line for each JIT-ed objfile. */
86 for (objfile *obj : inf->pspace->objfiles ())
87 {
88 if (obj->jited_data == nullptr)
89 continue;
90
91 if (!printed_header)
92 {
93 printf_filtered ("Base address of known JIT-ed objfiles:\n");
94 printed_header = true;
95 }
96
97 printf_filtered (" %s\n", paddress (obj->arch (), obj->jited_data->addr));
98 }
99}
100
0e8621a0
TT
101struct jit_reader
102{
103 jit_reader (struct gdb_reader_funcs *f, gdb_dlhandle_up &&h)
104 : functions (f), handle (std::move (h))
105 {
106 }
107
108 ~jit_reader ()
109 {
110 functions->destroy (functions);
111 }
112
d6541620 113 DISABLE_COPY_AND_ASSIGN (jit_reader);
0e8621a0
TT
114
115 struct gdb_reader_funcs *functions;
116 gdb_dlhandle_up handle;
117};
118
784c47ee
SD
119/* One reader that has been loaded successfully, and can potentially be used to
120 parse debug info. */
121
0e8621a0 122static struct jit_reader *loaded_jit_reader = NULL;
784c47ee
SD
123
124typedef struct gdb_reader_funcs * (reader_init_fn_type) (void);
db92ac45 125static const char reader_init_fn_sym[] = "gdb_init_reader";
784c47ee
SD
126
127/* Try to load FILE_NAME as a JIT debug info reader. */
128
129static struct jit_reader *
130jit_reader_load (const char *file_name)
131{
784c47ee 132 reader_init_fn_type *init_fn;
784c47ee 133 struct gdb_reader_funcs *funcs = NULL;
784c47ee 134
54ca9002
SM
135 jit_debug_printf ("Opening shared object %s", file_name);
136
0e8621a0 137 gdb_dlhandle_up so = gdb_dlopen (file_name);
784c47ee 138
15cf126c 139 init_fn = (reader_init_fn_type *) gdb_dlsym (so, reader_init_fn_sym);
784c47ee
SD
140 if (!init_fn)
141 error (_("Could not locate initialization function: %s."),
3a90f266 142 reader_init_fn_sym);
784c47ee
SD
143
144 if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL)
145 error (_("Reader not GPL compatible."));
146
147 funcs = init_fn ();
148 if (funcs->reader_version != GDB_READER_INTERFACE_VERSION)
149 error (_("Reader version does not match GDB version."));
150
0e8621a0 151 return new jit_reader (funcs, std::move (so));
784c47ee
SD
152}
153
154/* Provides the jit-reader-load command. */
155
156static void
0b39b52e 157jit_reader_load_command (const char *args, int from_tty)
784c47ee 158{
784c47ee
SD
159 if (args == NULL)
160 error (_("No reader name provided."));
7c218e6c 161 gdb::unique_xmalloc_ptr<char> file (tilde_expand (args));
784c47ee
SD
162
163 if (loaded_jit_reader != NULL)
164 error (_("JIT reader already loaded. Run jit-reader-unload first."));
165
7c218e6c 166 if (!IS_ABSOLUTE_PATH (file.get ()))
8579fd13
AB
167 file = xstrprintf ("%s%s%s", jit_reader_dir.c_str (),
168 SLASH_STRING, file.get ());
784c47ee 169
7c218e6c 170 loaded_jit_reader = jit_reader_load (file.get ());
20aa2c60 171 reinit_frame_cache ();
32495661 172 jit_inferior_created_hook (current_inferior ());
784c47ee
SD
173}
174
175/* Provides the jit-reader-unload command. */
176
177static void
0b39b52e 178jit_reader_unload_command (const char *args, int from_tty)
784c47ee
SD
179{
180 if (!loaded_jit_reader)
181 error (_("No JIT reader loaded."));
182
20aa2c60
PA
183 reinit_frame_cache ();
184 jit_inferior_exit_hook (current_inferior ());
784c47ee 185
0e8621a0 186 delete loaded_jit_reader;
784c47ee
SD
187 loaded_jit_reader = NULL;
188}
189
0e74a041 190/* Destructor for jiter_objfile_data. */
03bef283 191
0e74a041 192jiter_objfile_data::~jiter_objfile_data ()
03bef283 193{
77208eb7
SM
194 if (this->jit_breakpoint != nullptr)
195 delete_breakpoint (this->jit_breakpoint);
238b5c9f 196}
03673fc7 197
0e74a041 198/* Fetch the jiter_objfile_data associated with OBJF. If no data exists
03bef283
TT
199 yet, make a new structure and attach it. */
200
0e74a041
SM
201static jiter_objfile_data *
202get_jiter_objfile_data (objfile *objf)
03bef283 203{
0e74a041 204 if (objf->jiter_data == nullptr)
c1072906 205 objf->jiter_data.reset (new jiter_objfile_data ());
03bef283 206
0e74a041 207 return objf->jiter_data.get ();
03bef283
TT
208}
209
b4264740
SD
210/* Remember OBJFILE has been created for struct jit_code_entry located
211 at inferior address ENTRY. */
1825a88d
SD
212
213static void
214add_objfile_entry (struct objfile *objfile, CORE_ADDR entry)
215{
0e74a041 216 gdb_assert (objfile->jited_data == nullptr);
1825a88d 217
0e74a041 218 objfile->jited_data.reset (new jited_objfile_data (entry));
1825a88d
SD
219}
220
1777feb0 221/* Helper function for reading the global JIT descriptor from remote
bd920864 222 memory. Returns true if all went well, false otherwise. */
4efc6507 223
bd920864 224static bool
fe053b9e
TBA
225jit_read_descriptor (gdbarch *gdbarch,
226 jit_descriptor *descriptor,
227 objfile *jiter)
4efc6507
DE
228{
229 int err;
230 struct type *ptr_type;
231 int ptr_size;
232 int desc_size;
233 gdb_byte *desc_buf;
0756c555 234 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
03bef283 235
fe053b9e 236 gdb_assert (jiter != nullptr);
8c1c720f
SM
237 jiter_objfile_data *objf_data = jiter->jiter_data.get ();
238 gdb_assert (objf_data != nullptr);
03bef283 239
2340e834
SM
240 CORE_ADDR addr = MSYMBOL_VALUE_ADDRESS (jiter, objf_data->descriptor);
241
54ca9002 242 jit_debug_printf ("descriptor_addr = %s", paddress (gdbarch, addr));
4efc6507
DE
243
244 /* Figure out how big the descriptor is on the remote and how to read it. */
0756c555 245 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4efc6507
DE
246 ptr_size = TYPE_LENGTH (ptr_type);
247 desc_size = 8 + 2 * ptr_size; /* Two 32-bit ints and two pointers. */
224c3ddb 248 desc_buf = (gdb_byte *) alloca (desc_size);
4efc6507
DE
249
250 /* Read the descriptor. */
2340e834 251 err = target_read_memory (addr, desc_buf, desc_size);
4efc6507 252 if (err)
03bef283 253 {
0fed7461
TT
254 fprintf_unfiltered (gdb_stderr, _("Unable to read JIT descriptor from "
255 "remote memory\n"));
bd920864 256 return false;
03bef283 257 }
4efc6507
DE
258
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);
03bef283 266
bd920864 267 return true;
4efc6507
DE
268}
269
270/* Helper function for reading a JITed code entry from remote memory. */
271
272static void
0756c555
DE
273jit_read_code_entry (struct gdbarch *gdbarch,
274 CORE_ADDR code_addr, struct jit_code_entry *code_entry)
4efc6507 275{
205c306f 276 int err, off;
4efc6507
DE
277 struct type *ptr_type;
278 int ptr_size;
279 int entry_size;
205c306f 280 int align_bytes;
4efc6507 281 gdb_byte *entry_buf;
0756c555 282 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4efc6507
DE
283
284 /* Figure out how big the entry is on the remote and how to read it. */
0756c555 285 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4efc6507 286 ptr_size = TYPE_LENGTH (ptr_type);
227ee7fc 287
e11fb955
TT
288 /* Figure out where the uint64_t value will be. */
289 align_bytes = type_align (builtin_type (gdbarch)->builtin_uint64);
227ee7fc
RH
290 off = 3 * ptr_size;
291 off = (off + (align_bytes - 1)) & ~(align_bytes - 1);
292
293 entry_size = off + 8; /* Three pointers and one 64-bit int. */
224c3ddb 294 entry_buf = (gdb_byte *) alloca (entry_size);
4efc6507
DE
295
296 /* Read the entry. */
297 err = target_read_memory (code_addr, entry_buf, entry_size);
298 if (err)
299 error (_("Unable to read JIT code entry from remote memory!"));
300
301 /* Fix the endianness to match the host. */
0756c555 302 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4efc6507
DE
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 =
205c306f 309 extract_unsigned_integer (&entry_buf[off], 8, byte_order);
4efc6507
DE
310}
311
1825a88d
SD
312/* Proxy object for building a block. */
313
314struct gdb_block
315{
b6112117
SM
316 gdb_block (gdb_block *parent, CORE_ADDR begin, CORE_ADDR end,
317 const char *name)
318 : parent (parent),
319 begin (begin),
320 end (end),
321 name (name != nullptr ? xstrdup (name) : nullptr)
322 {}
323
0394eed1
SM
324 /* The parent of this block. */
325 struct gdb_block *parent;
1825a88d
SD
326
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. */
b6112117 330 struct block *real_block = nullptr;
1825a88d
SD
331
332 /* The first and last code address corresponding to this block. */
333 CORE_ADDR begin, end;
334
335 /* The name of this block (if any). If this is non-NULL, the
336 FUNCTION symbol symbol is set to this value. */
b6112117 337 gdb::unique_xmalloc_ptr<char> name;
1825a88d
SD
338};
339
340/* Proxy object for building a symtab. */
341
342struct gdb_symtab
343{
89867184
SM
344 explicit gdb_symtab (const char *file_name)
345 : file_name (file_name != nullptr ? file_name : "")
346 {}
347
1825a88d 348 /* The list of blocks in this symtab. These will eventually be
0394eed1
SM
349 converted to real blocks.
350
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;
1825a88d
SD
356
357 /* The number of blocks inserted. */
89867184 358 int nblocks = 0;
1825a88d
SD
359
360 /* A mapping between line numbers to PC. */
89867184 361 gdb::unique_xmalloc_ptr<struct linetable> linetable;
1825a88d
SD
362
363 /* The source file for this symtab. */
89867184 364 std::string file_name;
1825a88d
SD
365};
366
367/* Proxy object for building an object. */
368
369struct gdb_object
370{
1b61f46d
SM
371 /* Symtabs of this object.
372
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;
1825a88d
SD
378};
379
380/* The type of the `private' data passed around by the callback
381 functions. */
382
4a620b7e
SM
383struct jit_dbg_reader_data
384{
385 /* Address of the jit_code_entry in the inferior's address space. */
386 CORE_ADDR entry_addr;
387
388 /* The code entry, copied in our address space. */
389 const jit_code_entry &entry;
390
391 struct gdbarch *gdbarch;
392};
1825a88d
SD
393
394/* The reader calls into this function to read data off the targets
395 address space. */
396
397static enum gdb_status
398jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len)
399{
cb0a2700
SM
400 int result = target_read_memory ((CORE_ADDR) target_mem,
401 (gdb_byte *) gdb_buf, len);
1825a88d
SD
402 if (result == 0)
403 return GDB_SUCCESS;
404 else
405 return GDB_FAIL;
406}
407
408/* The reader calls into this function to create a new gdb_object
409 which it can then pass around to the other callbacks. Right now,
410 all that is required is allocating the memory. */
411
412static struct gdb_object *
413jit_object_open_impl (struct gdb_symbol_callbacks *cb)
414{
415 /* CB is not required right now, but sometime in the future we might
416 need a handle to it, and we'd like to do that without breaking
417 the ABI. */
1b61f46d 418 return new gdb_object;
1825a88d
SD
419}
420
421/* Readers call into this function to open a new gdb_symtab, which,
422 again, is passed around to other callbacks. */
423
424static struct gdb_symtab *
425jit_symtab_open_impl (struct gdb_symbol_callbacks *cb,
3a90f266
SM
426 struct gdb_object *object,
427 const char *file_name)
1825a88d 428{
1825a88d
SD
429 /* CB stays unused. See comment in jit_object_open_impl. */
430
1b61f46d
SM
431 object->symtabs.emplace_front (file_name);
432 return &object->symtabs.front ();
1825a88d
SD
433}
434
1825a88d
SD
435/* Called by readers to open a new gdb_block. This function also
436 inserts the new gdb_block in the correct place in the corresponding
437 gdb_symtab. */
438
439static struct gdb_block *
440jit_block_open_impl (struct gdb_symbol_callbacks *cb,
3a90f266
SM
441 struct gdb_symtab *symtab, struct gdb_block *parent,
442 GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name)
1825a88d 443{
0394eed1
SM
444 /* Place the block at the beginning of the list, it will be sorted when the
445 symtab is finalized. */
446 symtab->blocks.emplace_front (parent, begin, end, name);
1825a88d
SD
447 symtab->nblocks++;
448
0394eed1 449 return &symtab->blocks.front ();
1825a88d
SD
450}
451
452/* Readers call this to add a line mapping (from PC to line number) to
453 a gdb_symtab. */
4efc6507
DE
454
455static void
1825a88d 456jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb,
3a90f266
SM
457 struct gdb_symtab *stab, int nlines,
458 struct gdb_line_mapping *map)
1825a88d
SD
459{
460 int i;
224c3ddb 461 int alloc_len;
1825a88d
SD
462
463 if (nlines < 1)
464 return;
465
224c3ddb
SM
466 alloc_len = sizeof (struct linetable)
467 + (nlines - 1) * sizeof (struct linetable_entry);
89867184 468 stab->linetable.reset (XNEWVAR (struct linetable, alloc_len));
1825a88d
SD
469 stab->linetable->nitems = nlines;
470 for (i = 0; i < nlines; i++)
471 {
472 stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc;
473 stab->linetable->item[i].line = map[i].line;
8c95582d 474 stab->linetable->item[i].is_stmt = 1;
1825a88d
SD
475 }
476}
477
478/* Called by readers to close a gdb_symtab. Does not need to do
479 anything as of now. */
480
481static void
482jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
3a90f266 483 struct gdb_symtab *stab)
1825a88d
SD
484{
485 /* Right now nothing needs to be done here. We may need to do some
486 cleanup here in the future (again, without breaking the plugin
487 ABI). */
488}
489
490/* Transform STAB to a proper symtab, and add it it OBJFILE. */
491
492static void
493finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
494{
43f3e411 495 struct compunit_symtab *cust;
241fd515 496 size_t blockvector_size;
1825a88d 497 CORE_ADDR begin, end;
346d1dfe 498 struct blockvector *bv;
1825a88d 499
0394eed1
SM
500 int actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks;
501
502 /* Sort the blocks in the order they should appear in the blockvector. */
503 stab->blocks.sort([] (const gdb_block &a, const gdb_block &b)
504 {
505 if (a.begin != b.begin)
506 return a.begin < b.begin;
507
508 return a.end > b.end;
509 });
1825a88d 510
89867184
SM
511 cust = allocate_compunit_symtab (objfile, stab->file_name.c_str ());
512 allocate_symtab (cust, stab->file_name.c_str ());
43f3e411
DE
513 add_compunit_symtab_to_objfile (cust);
514
1825a88d 515 /* JIT compilers compile in memory. */
43f3e411 516 COMPUNIT_DIRNAME (cust) = NULL;
1825a88d
SD
517
518 /* Copy over the linetable entry if one was provided. */
519 if (stab->linetable)
520 {
241fd515
AM
521 size_t size = ((stab->linetable->nitems - 1)
522 * sizeof (struct linetable_entry)
523 + sizeof (struct linetable));
43f3e411 524 SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust))
224c3ddb 525 = (struct linetable *) obstack_alloc (&objfile->objfile_obstack, size);
89867184
SM
526 memcpy (SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust)),
527 stab->linetable.get (), size);
1825a88d
SD
528 }
529
530 blockvector_size = (sizeof (struct blockvector)
3a90f266 531 + (actual_nblocks - 1) * sizeof (struct block *));
224c3ddb
SM
532 bv = (struct blockvector *) obstack_alloc (&objfile->objfile_obstack,
533 blockvector_size);
43f3e411 534 COMPUNIT_BLOCKVECTOR (cust) = bv;
1825a88d 535
0394eed1
SM
536 /* At the end of this function, (begin, end) will contain the PC range this
537 entire blockvector spans. */
346d1dfe 538 BLOCKVECTOR_MAP (bv) = NULL;
0394eed1
SM
539 begin = stab->blocks.front ().begin;
540 end = stab->blocks.front ().end;
346d1dfe 541 BLOCKVECTOR_NBLOCKS (bv) = actual_nblocks;
1825a88d
SD
542
543 /* First run over all the gdb_block objects, creating a real block
544 object for each. Simultaneously, keep setting the real_block
545 fields. */
0394eed1
SM
546 int block_idx = FIRST_LOCAL_BLOCK;
547 for (gdb_block &gdb_block_iter : stab->blocks)
1825a88d
SD
548 {
549 struct block *new_block = allocate_block (&objfile->objfile_obstack);
8c14c3a3 550 struct symbol *block_name = new (&objfile->objfile_obstack) symbol;
08feed99 551 struct type *block_type = arch_type (objfile->arch (),
2535757a 552 TYPE_CODE_VOID,
77b7c781 553 TARGET_CHAR_BIT,
2535757a 554 "void");
1825a88d 555
b026f593
KS
556 BLOCK_MULTIDICT (new_block)
557 = mdict_create_linear (&objfile->objfile_obstack, NULL);
1825a88d 558 /* The address range. */
0394eed1
SM
559 BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter.begin;
560 BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter.end;
1825a88d
SD
561
562 /* The name. */
1825a88d 563 SYMBOL_DOMAIN (block_name) = VAR_DOMAIN;
f1e6e072 564 SYMBOL_ACLASS_INDEX (block_name) = LOC_BLOCK;
08be3fe3 565 symbol_set_symtab (block_name, COMPUNIT_FILETABS (cust));
2535757a 566 SYMBOL_TYPE (block_name) = lookup_function_type (block_type);
1825a88d
SD
567 SYMBOL_BLOCK_VALUE (block_name) = new_block;
568
4d4eaa30
CB
569 block_name->m_name = obstack_strdup (&objfile->objfile_obstack,
570 gdb_block_iter.name.get ());
1825a88d
SD
571
572 BLOCK_FUNCTION (new_block) = block_name;
573
0394eed1 574 BLOCKVECTOR_BLOCK (bv, block_idx) = new_block;
1825a88d 575 if (begin > BLOCK_START (new_block))
3a90f266 576 begin = BLOCK_START (new_block);
1825a88d 577 if (end < BLOCK_END (new_block))
3a90f266 578 end = BLOCK_END (new_block);
1825a88d 579
0394eed1
SM
580 gdb_block_iter.real_block = new_block;
581
582 block_idx++;
1825a88d
SD
583 }
584
585 /* Now add the special blocks. */
0394eed1
SM
586 struct block *block_iter = NULL;
587 for (enum block_enum i : { GLOBAL_BLOCK, STATIC_BLOCK })
1825a88d 588 {
84a146c9
TT
589 struct block *new_block;
590
591 new_block = (i == GLOBAL_BLOCK
592 ? allocate_global_block (&objfile->objfile_obstack)
593 : allocate_block (&objfile->objfile_obstack));
b026f593
KS
594 BLOCK_MULTIDICT (new_block)
595 = mdict_create_linear (&objfile->objfile_obstack, NULL);
1825a88d
SD
596 BLOCK_SUPERBLOCK (new_block) = block_iter;
597 block_iter = new_block;
598
599 BLOCK_START (new_block) = (CORE_ADDR) begin;
600 BLOCK_END (new_block) = (CORE_ADDR) end;
601
346d1dfe 602 BLOCKVECTOR_BLOCK (bv, i) = new_block;
84a146c9
TT
603
604 if (i == GLOBAL_BLOCK)
43f3e411 605 set_block_compunit_symtab (new_block, cust);
1825a88d
SD
606 }
607
608 /* Fill up the superblock fields for the real blocks, using the
609 real_block fields populated earlier. */
0394eed1 610 for (gdb_block &gdb_block_iter : stab->blocks)
1825a88d 611 {
0394eed1 612 if (gdb_block_iter.parent != NULL)
db334a01
SD
613 {
614 /* If the plugin specifically mentioned a parent block, we
615 use that. */
0394eed1
SM
616 BLOCK_SUPERBLOCK (gdb_block_iter.real_block) =
617 gdb_block_iter.parent->real_block;
db334a01
SD
618 }
619 else
620 {
621 /* And if not, we set a default parent block. */
0394eed1 622 BLOCK_SUPERBLOCK (gdb_block_iter.real_block) =
346d1dfe 623 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
db334a01 624 }
1825a88d 625 }
1825a88d
SD
626}
627
628/* Called when closing a gdb_objfile. Converts OBJ to a proper
629 objfile. */
630
631static void
632jit_object_close_impl (struct gdb_symbol_callbacks *cb,
3a90f266 633 struct gdb_object *obj)
1825a88d 634{
4a620b7e
SM
635 jit_dbg_reader_data *priv_data = (jit_dbg_reader_data *) cb->priv_data;
636 std::string objfile_name
637 = string_printf ("<< JIT compiled code at %s >>",
638 paddress (priv_data->gdbarch,
639 priv_data->entry.symfile_addr));
1825a88d 640
4a620b7e
SM
641 objfile *objfile = objfile::make (nullptr, objfile_name.c_str (),
642 OBJF_NOT_FILENAME);
643 objfile->per_bfd->gdbarch = priv_data->gdbarch;
1825a88d 644
1b61f46d
SM
645 for (gdb_symtab &symtab : obj->symtabs)
646 finalize_symtab (&symtab, objfile);
647
4a620b7e 648 add_objfile_entry (objfile, priv_data->entry_addr);
1b61f46d
SM
649
650 delete obj;
1825a88d
SD
651}
652
744ab88c 653/* Try to read CODE_ENTRY using the loaded jit reader (if any).
b4264740
SD
654 ENTRY_ADDR is the address of the struct jit_code_entry in the
655 inferior address space. */
1825a88d
SD
656
657static int
4a620b7e 658jit_reader_try_read_symtab (gdbarch *gdbarch, jit_code_entry *code_entry,
3a90f266 659 CORE_ADDR entry_addr)
1825a88d 660{
1825a88d 661 int status;
4a620b7e
SM
662 jit_dbg_reader_data priv_data
663 {
664 entry_addr,
665 *code_entry,
666 gdbarch
667 };
1825a88d 668 struct gdb_reader_funcs *funcs;
1825a88d
SD
669 struct gdb_symbol_callbacks callbacks =
670 {
671 jit_object_open_impl,
672 jit_symtab_open_impl,
673 jit_block_open_impl,
674 jit_symtab_close_impl,
675 jit_object_close_impl,
676
677 jit_symtab_line_mapping_add_impl,
678 jit_target_read_impl,
679
680 &priv_data
681 };
682
1825a88d
SD
683 if (!loaded_jit_reader)
684 return 0;
685
7190276c 686 gdb::byte_vector gdb_mem (code_entry->symfile_size);
1825a88d
SD
687
688 status = 1;
a70b8144 689 try
492d29ea 690 {
7190276c 691 if (target_read_memory (code_entry->symfile_addr, gdb_mem.data (),
492d29ea
PA
692 code_entry->symfile_size))
693 status = 0;
694 }
230d2906 695 catch (const gdb_exception &e)
492d29ea 696 {
1825a88d 697 status = 0;
492d29ea 698 }
1825a88d
SD
699
700 if (status)
701 {
702 funcs = loaded_jit_reader->functions;
7190276c
SM
703 if (funcs->read (funcs, &callbacks, gdb_mem.data (),
704 code_entry->symfile_size)
3a90f266
SM
705 != GDB_SUCCESS)
706 status = 0;
1825a88d
SD
707 }
708
54ca9002
SM
709 if (status == 0)
710 jit_debug_printf ("Could not read symtab using the loaded JIT reader.");
711
1825a88d
SD
712 return status;
713}
714
744ab88c 715/* Try to read CODE_ENTRY using BFD. ENTRY_ADDR is the address of the
b4264740 716 struct jit_code_entry in the inferior address space. */
1825a88d
SD
717
718static void
719jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
3a90f266
SM
720 CORE_ADDR entry_addr,
721 struct gdbarch *gdbarch)
4efc6507 722{
4efc6507
DE
723 struct bfd_section *sec;
724 struct objfile *objfile;
4efc6507 725 const struct bfd_arch_info *b;
4efc6507 726
54ca9002
SM
727 jit_debug_printf ("symfile_addr = %s, symfile_size = %s",
728 paddress (gdbarch, code_entry->symfile_addr),
729 pulongest (code_entry->symfile_size));
a255712f 730
15cc148f
MS
731 gdb_bfd_ref_ptr nbfd (gdb_bfd_open_from_target_memory
732 (code_entry->symfile_addr, code_entry->symfile_size, gnutarget));
4dfb2365
JK
733 if (nbfd == NULL)
734 {
0fed7461
TT
735 fputs_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"),
736 gdb_stderr);
4dfb2365
JK
737 return;
738 }
4efc6507
DE
739
740 /* Check the format. NOTE: This initializes important data that GDB uses!
741 We would segfault later without this line. */
192b62ce 742 if (!bfd_check_format (nbfd.get (), bfd_object))
4efc6507 743 {
0fed7461 744 fprintf_unfiltered (gdb_stderr, _("\
4efc6507 745JITed symbol file is not an object file, ignoring it.\n"));
4efc6507
DE
746 return;
747 }
748
749 /* Check bfd arch. */
0756c555 750 b = gdbarch_bfd_arch_info (gdbarch);
192b62ce 751 if (b->compatible (b, bfd_get_arch_info (nbfd.get ())) != b)
4efc6507 752 warning (_("JITed object file architecture %s is not compatible "
3a90f266 753 "with target architecture %s."),
192b62ce
TT
754 bfd_get_arch_info (nbfd.get ())->printable_name,
755 b->printable_name);
4efc6507
DE
756
757 /* Read the section address information out of the symbol file. Since the
758 file is generated by the JIT at runtime, it should all of the absolute
759 addresses that we care about. */
37e136b1 760 section_addr_info sai;
4efc6507 761 for (sec = nbfd->sections; sec != NULL; sec = sec->next)
fd361982 762 if ((bfd_section_flags (sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
4efc6507 763 {
3a90f266
SM
764 /* We assume that these virtual addresses are absolute, and do not
765 treat them as offsets. */
fd361982
AM
766 sai.emplace_back (bfd_section_vma (sec),
767 bfd_section_name (sec),
37e136b1 768 sec->index);
4efc6507
DE
769 }
770
8ac244b4 771 /* This call does not take ownership of SAI. */
192b62ce 772 objfile = symbol_file_add_from_bfd (nbfd.get (),
37e136b1
TT
773 bfd_get_filename (nbfd.get ()), 0,
774 &sai,
40135bb1 775 OBJF_SHARED | OBJF_NOT_FILENAME, NULL);
4efc6507 776
744ab88c 777 add_objfile_entry (objfile, entry_addr);
1825a88d
SD
778}
779
780/* This function registers code associated with a JIT code entry. It uses the
781 pointer and size pair in the entry to read the symbol file from the remote
782 and then calls symbol_file_add_from_local_memory to add it as though it were
783 a symbol file added by the user. */
784
785static void
786jit_register_code (struct gdbarch *gdbarch,
3a90f266 787 CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
1825a88d 788{
974a734b 789 int success;
1825a88d 790
54ca9002
SM
791 jit_debug_printf ("symfile_addr = %s, symfile_size = %s",
792 paddress (gdbarch, code_entry->symfile_addr),
793 pulongest (code_entry->symfile_size));
1825a88d 794
4a620b7e 795 success = jit_reader_try_read_symtab (gdbarch, code_entry, entry_addr);
1825a88d
SD
796
797 if (!success)
744ab88c 798 jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch);
4efc6507
DE
799}
800
4efc6507
DE
801/* Look up the objfile with this code entry address. */
802
803static struct objfile *
804jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
805{
2030c079 806 for (objfile *objf : current_program_space->objfiles ())
4efc6507 807 {
0e74a041 808 if (objf->jited_data != nullptr && objf->jited_data->addr == entry_addr)
3a90f266 809 return objf;
4efc6507 810 }
238b5c9f 811
4efc6507
DE
812 return NULL;
813}
814
f25c0135
TT
815/* This is called when a breakpoint is deleted. It updates the
816 inferior's cache, if needed. */
817
818static void
819jit_breakpoint_deleted (struct breakpoint *b)
820{
f25c0135
TT
821 if (b->type != bp_jit_event)
822 return;
823
40cb8ca5 824 for (bp_location *iter : b->locations ())
8eacb197 825 {
c8474dc3 826 for (objfile *objf : iter->pspace->objfiles ())
8eacb197 827 {
77208eb7
SM
828 jiter_objfile_data *jiter_data = objf->jiter_data.get ();
829
c8474dc3
TBA
830 if (jiter_data != nullptr
831 && jiter_data->jit_breakpoint == iter->owner)
77208eb7
SM
832 {
833 jiter_data->cached_code_address = 0;
834 jiter_data->jit_breakpoint = nullptr;
835 }
8eacb197
TT
836 }
837 }
f25c0135
TT
838}
839
c8474dc3
TBA
840/* (Re-)Initialize the jit breakpoints for JIT-producing objfiles in
841 PSPACE. */
03673fc7 842
c8474dc3
TBA
843static void
844jit_breakpoint_re_set_internal (struct gdbarch *gdbarch, program_space *pspace)
03673fc7 845{
c8474dc3 846 for (objfile *the_objfile : pspace->objfiles ())
f25c0135 847 {
6d1a09b7
TV
848 /* Skip separate debug objects. */
849 if (the_objfile->separate_debug_objfile_backlink != nullptr)
850 continue;
851
a7b4ff4f
SM
852 if (the_objfile->skip_jit_symbol_lookup)
853 continue;
854
f25c0135
TT
855 /* Lookup the registration symbol. If it is missing, then we
856 assume we are not attached to a JIT. */
c8474dc3
TBA
857 bound_minimal_symbol reg_symbol
858 = lookup_minimal_symbol (jit_break_name, nullptr, the_objfile);
7cbd4a93 859 if (reg_symbol.minsym == NULL
77e371c0 860 || BMSYMBOL_VALUE_ADDRESS (reg_symbol) == 0)
a7b4ff4f
SM
861 {
862 /* No need to repeat the lookup the next time. */
863 the_objfile->skip_jit_symbol_lookup = true;
864 continue;
865 }
03bef283 866
c8474dc3
TBA
867 bound_minimal_symbol desc_symbol
868 = lookup_minimal_symbol (jit_descriptor_name, NULL, the_objfile);
3b7344d5 869 if (desc_symbol.minsym == NULL
77e371c0 870 || BMSYMBOL_VALUE_ADDRESS (desc_symbol) == 0)
a7b4ff4f
SM
871 {
872 /* No need to repeat the lookup the next time. */
873 the_objfile->skip_jit_symbol_lookup = true;
874 continue;
875 }
03bef283 876
2340e834 877 jiter_objfile_data *objf_data
6d1a09b7 878 = get_jiter_objfile_data (the_objfile);
7cbd4a93 879 objf_data->register_code = reg_symbol.minsym;
3b7344d5 880 objf_data->descriptor = desc_symbol.minsym;
03bef283 881
c8474dc3
TBA
882 CORE_ADDR addr = MSYMBOL_VALUE_ADDRESS (the_objfile,
883 objf_data->register_code);
03bef283 884
54ca9002 885 jit_debug_printf ("breakpoint_addr = %s", paddress (gdbarch, addr));
f25c0135 886
c8474dc3
TBA
887 /* Check if we need to re-create the breakpoint. */
888 if (objf_data->cached_code_address == addr)
889 continue;
03673fc7 890
c8474dc3
TBA
891 /* Delete the old breakpoint. */
892 if (objf_data->jit_breakpoint != nullptr)
893 delete_breakpoint (objf_data->jit_breakpoint);
03673fc7 894
c8474dc3
TBA
895 /* Put a breakpoint in the registration symbol. */
896 objf_data->cached_code_address = addr;
897 objf_data->jit_breakpoint = create_jit_event_breakpoint (gdbarch, addr);
898 }
03673fc7
PP
899}
900
3623dc3a
SD
901/* The private data passed around in the frame unwind callback
902 functions. */
903
904struct jit_unwind_private
905{
906 /* Cached register values. See jit_frame_sniffer to see how this
907 works. */
c8ec2f33 908 detached_regcache *regcache;
3623dc3a
SD
909
910 /* The frame being unwound. */
911 struct frame_info *this_frame;
912};
913
914/* Sets the value of a particular register in this frame. */
915
916static void
917jit_unwind_reg_set_impl (struct gdb_unwind_callbacks *cb, int dwarf_regnum,
3a90f266 918 struct gdb_reg_value *value)
3623dc3a
SD
919{
920 struct jit_unwind_private *priv;
921 int gdb_reg;
922
9a3c8263 923 priv = (struct jit_unwind_private *) cb->priv_data;
3623dc3a
SD
924
925 gdb_reg = gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv->this_frame),
3a90f266 926 dwarf_regnum);
3623dc3a
SD
927 if (gdb_reg == -1)
928 {
54ca9002 929 jit_debug_printf ("Could not recognize DWARF regnum %d", dwarf_regnum);
20aa2c60 930 value->free (value);
3623dc3a
SD
931 return;
932 }
933
c8ec2f33 934 priv->regcache->raw_supply (gdb_reg, value->value);
20aa2c60 935 value->free (value);
3623dc3a
SD
936}
937
938static void
939reg_value_free_impl (struct gdb_reg_value *value)
940{
941 xfree (value);
942}
943
944/* Get the value of register REGNUM in the previous frame. */
945
946static struct gdb_reg_value *
947jit_unwind_reg_get_impl (struct gdb_unwind_callbacks *cb, int regnum)
948{
949 struct jit_unwind_private *priv;
950 struct gdb_reg_value *value;
951 int gdb_reg, size;
952 struct gdbarch *frame_arch;
953
9a3c8263 954 priv = (struct jit_unwind_private *) cb->priv_data;
3623dc3a
SD
955 frame_arch = get_frame_arch (priv->this_frame);
956
957 gdb_reg = gdbarch_dwarf2_reg_to_regnum (frame_arch, regnum);
958 size = register_size (frame_arch, gdb_reg);
224c3ddb
SM
959 value = ((struct gdb_reg_value *)
960 xmalloc (sizeof (struct gdb_reg_value) + size - 1));
ca9d61b9
JB
961 value->defined = deprecated_frame_register_read (priv->this_frame, gdb_reg,
962 value->value);
3623dc3a
SD
963 value->size = size;
964 value->free = reg_value_free_impl;
965 return value;
966}
967
968/* gdb_reg_value has a free function, which must be called on each
969 saved register value. */
970
971static void
972jit_dealloc_cache (struct frame_info *this_frame, void *cache)
973{
9a3c8263 974 struct jit_unwind_private *priv_data = (struct jit_unwind_private *) cache;
3623dc3a 975
20aa2c60 976 gdb_assert (priv_data->regcache != NULL);
c0e383c6 977 delete priv_data->regcache;
3623dc3a
SD
978 xfree (priv_data);
979}
980
981/* The frame sniffer for the pseudo unwinder.
982
983 While this is nominally a frame sniffer, in the case where the JIT
984 reader actually recognizes the frame, it does a lot more work -- it
985 unwinds the frame and saves the corresponding register values in
986 the cache. jit_frame_prev_register simply returns the saved
987 register values. */
988
989static int
990jit_frame_sniffer (const struct frame_unwind *self,
3a90f266 991 struct frame_info *this_frame, void **cache)
3623dc3a 992{
3623dc3a 993 struct jit_unwind_private *priv_data;
3623dc3a
SD
994 struct gdb_unwind_callbacks callbacks;
995 struct gdb_reader_funcs *funcs;
996
3623dc3a
SD
997 callbacks.reg_get = jit_unwind_reg_get_impl;
998 callbacks.reg_set = jit_unwind_reg_set_impl;
999 callbacks.target_read = jit_target_read_impl;
1000
1001 if (loaded_jit_reader == NULL)
1002 return 0;
1003
1004 funcs = loaded_jit_reader->functions;
1005
1006 gdb_assert (!*cache);
1007
41bf6aca 1008 *cache = XCNEW (struct jit_unwind_private);
9a3c8263 1009 priv_data = (struct jit_unwind_private *) *cache;
c8ec2f33
YQ
1010 /* Take a snapshot of current regcache. */
1011 priv_data->regcache = new detached_regcache (get_frame_arch (this_frame),
1012 true);
3623dc3a
SD
1013 priv_data->this_frame = this_frame;
1014
1015 callbacks.priv_data = priv_data;
1016
1017 /* Try to coax the provided unwinder to unwind the stack */
1018 if (funcs->unwind (funcs, &callbacks) == GDB_SUCCESS)
1019 {
54ca9002 1020 jit_debug_printf ("Successfully unwound frame using JIT reader.");
3623dc3a
SD
1021 return 1;
1022 }
54ca9002
SM
1023
1024 jit_debug_printf ("Could not unwind frame using JIT reader.");
3623dc3a
SD
1025
1026 jit_dealloc_cache (this_frame, *cache);
1027 *cache = NULL;
1028
1029 return 0;
1030}
1031
1032
1033/* The frame_id function for the pseudo unwinder. Relays the call to
1034 the loaded plugin. */
1035
1036static void
1037jit_frame_this_id (struct frame_info *this_frame, void **cache,
3a90f266 1038 struct frame_id *this_id)
3623dc3a 1039{
fe978cb0 1040 struct jit_unwind_private priv;
3623dc3a
SD
1041 struct gdb_frame_id frame_id;
1042 struct gdb_reader_funcs *funcs;
1043 struct gdb_unwind_callbacks callbacks;
1044
20aa2c60 1045 priv.regcache = NULL;
fe978cb0 1046 priv.this_frame = this_frame;
3623dc3a
SD
1047
1048 /* We don't expect the frame_id function to set any registers, so we
1049 set reg_set to NULL. */
1050 callbacks.reg_get = jit_unwind_reg_get_impl;
1051 callbacks.reg_set = NULL;
1052 callbacks.target_read = jit_target_read_impl;
fe978cb0 1053 callbacks.priv_data = &priv;
3623dc3a
SD
1054
1055 gdb_assert (loaded_jit_reader);
1056 funcs = loaded_jit_reader->functions;
1057
1058 frame_id = funcs->get_frame_id (funcs, &callbacks);
1059 *this_id = frame_id_build (frame_id.stack_address, frame_id.code_address);
1060}
1061
1062/* Pseudo unwinder function. Reads the previously fetched value for
1063 the register from the cache. */
1064
1065static struct value *
1066jit_frame_prev_register (struct frame_info *this_frame, void **cache, int reg)
1067{
9a3c8263 1068 struct jit_unwind_private *priv = (struct jit_unwind_private *) *cache;
20aa2c60 1069 struct gdbarch *gdbarch;
3623dc3a
SD
1070
1071 if (priv == NULL)
1072 return frame_unwind_got_optimized (this_frame, reg);
1073
ac7936df 1074 gdbarch = priv->regcache->arch ();
3f5a868b
YQ
1075 gdb_byte *buf = (gdb_byte *) alloca (register_size (gdbarch, reg));
1076 enum register_status status = priv->regcache->cooked_read (reg, buf);
20aa2c60 1077
3f5a868b
YQ
1078 if (status == REG_VALID)
1079 return frame_unwind_got_bytes (this_frame, reg, buf);
3623dc3a 1080 else
3f5a868b 1081 return frame_unwind_got_optimized (this_frame, reg);
3623dc3a
SD
1082}
1083
1084/* Relay everything back to the unwinder registered by the JIT debug
1085 info reader.*/
1086
1087static const struct frame_unwind jit_frame_unwind =
1088{
a154d838 1089 "jit",
3623dc3a
SD
1090 NORMAL_FRAME,
1091 default_frame_unwind_stop_reason,
1092 jit_frame_this_id,
1093 jit_frame_prev_register,
1094 NULL,
1095 jit_frame_sniffer,
1096 jit_dealloc_cache
1097};
1098
1099
1100/* This is the information that is stored at jit_gdbarch_data for each
1101 architecture. */
1102
1103struct jit_gdbarch_data_type
1104{
1105 /* Has the (pseudo) unwinder been prepended? */
1106 int unwinder_registered;
1107};
1108
1109/* Check GDBARCH and prepend the pseudo JIT unwinder if needed. */
1110
1111static void
1112jit_prepend_unwinder (struct gdbarch *gdbarch)
1113{
1114 struct jit_gdbarch_data_type *data;
1115
9a3c8263
SM
1116 data
1117 = (struct jit_gdbarch_data_type *) gdbarch_data (gdbarch, jit_gdbarch_data);
3623dc3a
SD
1118 if (!data->unwinder_registered)
1119 {
1120 frame_unwind_prepend_unwinder (gdbarch, &jit_frame_unwind);
1121 data->unwinder_registered = 1;
1122 }
1123}
1124
03673fc7 1125/* Register any already created translations. */
0756c555
DE
1126
1127static void
32495661 1128jit_inferior_init (inferior *inf)
4efc6507 1129{
4efc6507
DE
1130 struct jit_descriptor descriptor;
1131 struct jit_code_entry cur_entry;
1132 CORE_ADDR cur_entry_addr;
32495661
SM
1133 struct gdbarch *gdbarch = inf->gdbarch;
1134 program_space *pspace = inf->pspace;
4efc6507 1135
54ca9002 1136 jit_debug_printf ("called");
a255712f 1137
3623dc3a
SD
1138 jit_prepend_unwinder (gdbarch);
1139
32495661 1140 jit_breakpoint_re_set_internal (gdbarch, pspace);
4efc6507 1141
32495661 1142 for (objfile *jiter : pspace->objfiles ())
c8474dc3
TBA
1143 {
1144 if (jiter->jiter_data == nullptr)
1145 continue;
fe053b9e 1146
c8474dc3
TBA
1147 /* Read the descriptor so we can check the version number and load
1148 any already JITed functions. */
1149 if (!jit_read_descriptor (gdbarch, &descriptor, jiter))
1150 continue;
4efc6507 1151
c8474dc3
TBA
1152 /* Check that the version number agrees with that we support. */
1153 if (descriptor.version != 1)
1154 {
0fed7461
TT
1155 fprintf_unfiltered (gdb_stderr,
1156 _("Unsupported JIT protocol version %ld "
1157 "in descriptor (expected 1)\n"),
1158 (long) descriptor.version);
c8474dc3
TBA
1159 continue;
1160 }
4efc6507 1161
c8474dc3
TBA
1162 /* If we've attached to a running program, we need to check the
1163 descriptor to register any functions that were already
1164 generated. */
1165 for (cur_entry_addr = descriptor.first_entry;
1166 cur_entry_addr != 0;
1167 cur_entry_addr = cur_entry.next_entry)
1168 {
1169 jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
4efc6507 1170
c8474dc3
TBA
1171 /* This hook may be called many times during setup, so make sure
1172 we don't add the same symbol file twice. */
1173 if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
1174 continue;
4efc6507 1175
c8474dc3
TBA
1176 jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
1177 }
4efc6507
DE
1178 }
1179}
1180
42a4fec5
SM
1181/* Looks for the descriptor and registration symbols and breakpoints
1182 the registration function. If it finds both, it registers all the
1183 already JITed code. If it has already found the symbols, then it
1184 doesn't try again. */
0756c555 1185
42a4fec5 1186static void
32495661 1187jit_inferior_created_hook (inferior *inf)
0756c555 1188{
32495661 1189 jit_inferior_init (inf);
0756c555
DE
1190}
1191
1192/* Exported routine to call to re-set the jit breakpoints,
1193 e.g. when a program is rerun. */
1194
1195void
1196jit_breakpoint_re_set (void)
1197{
c8474dc3 1198 jit_breakpoint_re_set_internal (target_gdbarch (), current_program_space);
03673fc7
PP
1199}
1200
1777feb0
MS
1201/* This function cleans up any code entries left over when the
1202 inferior exits. We get left over code when the inferior exits
1203 without unregistering its code, for example when it crashes. */
4efc6507
DE
1204
1205static void
a79b8f6e 1206jit_inferior_exit_hook (struct inferior *inf)
4efc6507 1207{
7e955d83 1208 for (objfile *objf : current_program_space->objfiles_safe ())
03bef283 1209 {
0e74a041 1210 if (objf->jited_data != nullptr && objf->jited_data->addr != 0)
268e4f09 1211 objf->unlink ();
03bef283 1212 }
03673fc7
PP
1213}
1214
4efc6507 1215void
fe053b9e 1216jit_event_handler (gdbarch *gdbarch, objfile *jiter)
4efc6507
DE
1217{
1218 struct jit_descriptor descriptor;
4efc6507 1219
8c1c720f
SM
1220 /* If we get a JIT breakpoint event for this objfile, it is necessarily a
1221 JITer. */
1222 gdb_assert (jiter->jiter_data != nullptr);
1223
4efc6507 1224 /* Read the descriptor from remote memory. */
fe053b9e 1225 if (!jit_read_descriptor (gdbarch, &descriptor, jiter))
03bef283 1226 return;
2340e834 1227 CORE_ADDR entry_addr = descriptor.relevant_entry;
4efc6507 1228
1777feb0 1229 /* Do the corresponding action. */
4efc6507
DE
1230 switch (descriptor.action_flag)
1231 {
1232 case JIT_NOACTION:
1233 break;
2340e834 1234
4efc6507 1235 case JIT_REGISTER:
2340e834
SM
1236 {
1237 jit_code_entry code_entry;
1238 jit_read_code_entry (gdbarch, entry_addr, &code_entry);
1239 jit_register_code (gdbarch, entry_addr, &code_entry);
1240 break;
1241 }
1242
4efc6507 1243 case JIT_UNREGISTER:
2340e834
SM
1244 {
1245 objfile *jited = jit_find_objf_with_entry_addr (entry_addr);
1246 if (jited == nullptr)
0fed7461
TT
1247 fprintf_unfiltered (gdb_stderr,
1248 _("Unable to find JITed code "
1249 "entry at address: %s\n"),
1250 paddress (gdbarch, entry_addr));
2340e834
SM
1251 else
1252 jited->unlink ();
1253
1254 break;
1255 }
4efc6507 1256
4efc6507
DE
1257 default:
1258 error (_("Unknown action_flag value in JIT descriptor!"));
1259 break;
1260 }
1261}
1262
3623dc3a
SD
1263/* Initialize the jit_gdbarch_data slot with an instance of struct
1264 jit_gdbarch_data_type */
1265
1266static void *
1267jit_gdbarch_data_init (struct obstack *obstack)
1268{
8d749320
SM
1269 struct jit_gdbarch_data_type *data =
1270 XOBNEW (obstack, struct jit_gdbarch_data_type);
3623dc3a 1271
3623dc3a 1272 data->unwinder_registered = 0;
8d749320 1273
3623dc3a
SD
1274 return data;
1275}
1276
6c265988 1277void _initialize_jit ();
4efc6507 1278void
6c265988 1279_initialize_jit ()
4efc6507 1280{
b8e0a31c 1281 jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR,
3a90f266 1282 JIT_READER_DIR_RELOCATABLE);
062eaacb
SM
1283 add_setshow_boolean_cmd ("jit", class_maintenance, &jit_debug,
1284 _("Set JIT debugging."),
1285 _("Show JIT debugging."),
54ca9002 1286 _("When set, JIT debugging is enabled."),
062eaacb
SM
1287 NULL,
1288 show_jit_debug,
1289 &setdebuglist, &showdebuglist);
a255712f 1290
ff770835
SM
1291 add_cmd ("jit", class_maintenance, maint_info_jit_cmd,
1292 _("Print information about JIT-ed code objects."),
1293 &maintenanceinfolist);
1294
c90e7d63
SM
1295 gdb::observers::inferior_created.attach (jit_inferior_created_hook, "jit");
1296 gdb::observers::inferior_execd.attach (jit_inferior_created_hook, "jit");
1297 gdb::observers::inferior_exit.attach (jit_inferior_exit_hook, "jit");
1298 gdb::observers::breakpoint_deleted.attach (jit_breakpoint_deleted, "jit");
f25c0135 1299
3623dc3a 1300 jit_gdbarch_data = gdbarch_data_register_pre_init (jit_gdbarch_data_init);
784c47ee
SD
1301 if (is_dl_available ())
1302 {
6571a381
TT
1303 struct cmd_list_element *c;
1304
1305 c = add_com ("jit-reader-load", no_class, jit_reader_load_command, _("\
784c47ee
SD
1306Load FILE as debug info reader and unwinder for JIT compiled code.\n\
1307Usage: jit-reader-load FILE\n\
1308Try to load file FILE as a debug info reader (and unwinder) for\n\
1309JIT compiled code. The file is loaded from " JIT_READER_DIR ",\n\
1310relocated relative to the GDB executable if required."));
6571a381
TT
1311 set_cmd_completer (c, filename_completer);
1312
1313 c = add_com ("jit-reader-unload", no_class,
1314 jit_reader_unload_command, _("\
784c47ee 1315Unload the currently loaded JIT debug info reader.\n\
6571a381 1316Usage: jit-reader-unload\n\n\
784c47ee 1317Do \"help jit-reader-load\" for info on loading debug info readers."));
6571a381 1318 set_cmd_completer (c, noop_completer);
784c47ee 1319 }
4efc6507 1320}