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