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