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