gdb/
[binutils-gdb.git] / gdb / jit.c
1 /* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
2
3 Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
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"
23 #include "jit-reader.h"
24 #include "block.h"
25 #include "breakpoint.h"
26 #include "command.h"
27 #include "dictionary.h"
28 #include "frame-unwind.h"
29 #include "gdbcmd.h"
30 #include "gdbcore.h"
31 #include "inferior.h"
32 #include "observer.h"
33 #include "objfiles.h"
34 #include "symfile.h"
35 #include "symtab.h"
36 #include "target.h"
37 #include "gdb-dlfcn.h"
38 #include "gdb_stat.h"
39 #include "exceptions.h"
40
41 static const char *jit_reader_dir = NULL;
42
43 static const struct objfile_data *jit_objfile_data;
44
45 static const char *const jit_break_name = "__jit_debug_register_code";
46
47 static const char *const jit_descriptor_name = "__jit_debug_descriptor";
48
49 static const struct inferior_data *jit_inferior_data = NULL;
50
51 static void jit_inferior_init (struct gdbarch *gdbarch);
52
53 /* Non-zero if we want to see trace of jit level stuff. */
54
55 static int jit_debug = 0;
56
57 static void
58 show_jit_debug (struct ui_file *file, int from_tty,
59 struct cmd_list_element *c, const char *value)
60 {
61 fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
62 }
63
64 struct target_buffer
65 {
66 CORE_ADDR base;
67 ULONGEST size;
68 };
69
70 /* Openning the file is a no-op. */
71
72 static void *
73 mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
74 {
75 return open_closure;
76 }
77
78 /* Closing the file is just freeing the base/size pair on our side. */
79
80 static int
81 mem_bfd_iovec_close (struct bfd *abfd, void *stream)
82 {
83 xfree (stream);
84 return 1;
85 }
86
87 /* For reading the file, we just need to pass through to target_read_memory and
88 fix up the arguments and return values. */
89
90 static file_ptr
91 mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
92 file_ptr nbytes, file_ptr offset)
93 {
94 int err;
95 struct target_buffer *buffer = (struct target_buffer *) stream;
96
97 /* If this read will read all of the file, limit it to just the rest. */
98 if (offset + nbytes > buffer->size)
99 nbytes = buffer->size - offset;
100
101 /* If there are no more bytes left, we've reached EOF. */
102 if (nbytes == 0)
103 return 0;
104
105 err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
106 if (err)
107 return -1;
108
109 return nbytes;
110 }
111
112 /* For statting the file, we only support the st_size attribute. */
113
114 static int
115 mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
116 {
117 struct target_buffer *buffer = (struct target_buffer*) stream;
118
119 sb->st_size = buffer->size;
120 return 0;
121 }
122
123 /* One reader that has been loaded successfully, and can potentially be used to
124 parse debug info. */
125
126 static struct jit_reader
127 {
128 struct gdb_reader_funcs *functions;
129 void *handle;
130 } *loaded_jit_reader = NULL;
131
132 typedef struct gdb_reader_funcs * (reader_init_fn_type) (void);
133 static const char *reader_init_fn_sym = "gdb_init_reader";
134
135 /* Try to load FILE_NAME as a JIT debug info reader. */
136
137 static struct jit_reader *
138 jit_reader_load (const char *file_name)
139 {
140 void *so;
141 reader_init_fn_type *init_fn;
142 struct jit_reader *new_reader = NULL;
143 struct gdb_reader_funcs *funcs = NULL;
144 struct cleanup *old_cleanups;
145
146 if (jit_debug)
147 fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"),
148 file_name);
149 so = gdb_dlopen (file_name);
150 old_cleanups = make_cleanup_dlclose (so);
151
152 init_fn = gdb_dlsym (so, reader_init_fn_sym);
153 if (!init_fn)
154 error (_("Could not locate initialization function: %s."),
155 reader_init_fn_sym);
156
157 if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL)
158 error (_("Reader not GPL compatible."));
159
160 funcs = init_fn ();
161 if (funcs->reader_version != GDB_READER_INTERFACE_VERSION)
162 error (_("Reader version does not match GDB version."));
163
164 new_reader = XZALLOC (struct jit_reader);
165 new_reader->functions = funcs;
166 new_reader->handle = so;
167
168 discard_cleanups (old_cleanups);
169 return new_reader;
170 }
171
172 /* Provides the jit-reader-load command. */
173
174 static void
175 jit_reader_load_command (char *args, int from_tty)
176 {
177 char *so_name;
178 int len;
179 struct cleanup *prev_cleanup;
180
181 if (args == NULL)
182 error (_("No reader name provided."));
183
184 if (loaded_jit_reader != NULL)
185 error (_("JIT reader already loaded. Run jit-reader-unload first."));
186
187 so_name = xstrprintf ("%s/%s", jit_reader_dir, args);
188 prev_cleanup = make_cleanup (xfree, so_name);
189
190 loaded_jit_reader = jit_reader_load (so_name);
191 do_cleanups (prev_cleanup);
192 }
193
194 /* Provides the jit-reader-unload command. */
195
196 static void
197 jit_reader_unload_command (char *args, int from_tty)
198 {
199 if (!loaded_jit_reader)
200 error (_("No JIT reader loaded."));
201
202 loaded_jit_reader->functions->destroy (loaded_jit_reader->functions);
203
204 gdb_dlclose (loaded_jit_reader->handle);
205 xfree (loaded_jit_reader);
206 loaded_jit_reader = NULL;
207 }
208
209 /* Open a BFD from the target's memory. */
210
211 static struct bfd *
212 bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
213 {
214 const char *filename = xstrdup ("<in-memory>");
215 struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
216
217 buffer->base = addr;
218 buffer->size = size;
219 return bfd_openr_iovec (filename, target,
220 mem_bfd_iovec_open,
221 buffer,
222 mem_bfd_iovec_pread,
223 mem_bfd_iovec_close,
224 mem_bfd_iovec_stat);
225 }
226
227 /* Per-inferior structure recording the addresses in the inferior. */
228
229 struct jit_inferior_data
230 {
231 CORE_ADDR breakpoint_addr; /* &__jit_debug_register_code() */
232 CORE_ADDR descriptor_addr; /* &__jit_debug_descriptor */
233 };
234
235 /* Remember a mapping from entry_addr to objfile. */
236
237 static void
238 add_objfile_entry (struct objfile *objfile, CORE_ADDR entry)
239 {
240 CORE_ADDR *entry_addr_ptr;
241
242 entry_addr_ptr = xmalloc (sizeof (CORE_ADDR));
243 *entry_addr_ptr = entry;
244 set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr);
245 }
246
247 /* Return jit_inferior_data for current inferior. Allocate if not already
248 present. */
249
250 static struct jit_inferior_data *
251 get_jit_inferior_data (void)
252 {
253 struct inferior *inf;
254 struct jit_inferior_data *inf_data;
255
256 inf = current_inferior ();
257 inf_data = inferior_data (inf, jit_inferior_data);
258 if (inf_data == NULL)
259 {
260 inf_data = XZALLOC (struct jit_inferior_data);
261 set_inferior_data (inf, jit_inferior_data, inf_data);
262 }
263
264 return inf_data;
265 }
266
267 static void
268 jit_inferior_data_cleanup (struct inferior *inf, void *arg)
269 {
270 xfree (arg);
271 }
272
273 /* Helper function for reading the global JIT descriptor from remote
274 memory. */
275
276 static void
277 jit_read_descriptor (struct gdbarch *gdbarch,
278 struct jit_descriptor *descriptor,
279 CORE_ADDR descriptor_addr)
280 {
281 int err;
282 struct type *ptr_type;
283 int ptr_size;
284 int desc_size;
285 gdb_byte *desc_buf;
286 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
287
288 /* Figure out how big the descriptor is on the remote and how to read it. */
289 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
290 ptr_size = TYPE_LENGTH (ptr_type);
291 desc_size = 8 + 2 * ptr_size; /* Two 32-bit ints and two pointers. */
292 desc_buf = alloca (desc_size);
293
294 /* Read the descriptor. */
295 err = target_read_memory (descriptor_addr, desc_buf, desc_size);
296 if (err)
297 error (_("Unable to read JIT descriptor from remote memory!"));
298
299 /* Fix the endianness to match the host. */
300 descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
301 descriptor->action_flag =
302 extract_unsigned_integer (&desc_buf[4], 4, byte_order);
303 descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
304 descriptor->first_entry =
305 extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
306 }
307
308 /* Helper function for reading a JITed code entry from remote memory. */
309
310 static void
311 jit_read_code_entry (struct gdbarch *gdbarch,
312 CORE_ADDR code_addr, struct jit_code_entry *code_entry)
313 {
314 int err, off;
315 struct type *ptr_type;
316 int ptr_size;
317 int entry_size;
318 int align_bytes;
319 gdb_byte *entry_buf;
320 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
321
322 /* Figure out how big the entry is on the remote and how to read it. */
323 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
324 ptr_size = TYPE_LENGTH (ptr_type);
325 entry_size = 3 * ptr_size + 8; /* Three pointers and one 64-bit int. */
326 entry_buf = alloca (entry_size);
327
328 /* Read the entry. */
329 err = target_read_memory (code_addr, entry_buf, entry_size);
330 if (err)
331 error (_("Unable to read JIT code entry from remote memory!"));
332
333 /* Fix the endianness to match the host. */
334 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
335 code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
336 code_entry->prev_entry =
337 extract_typed_address (&entry_buf[ptr_size], ptr_type);
338 code_entry->symfile_addr =
339 extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
340
341 align_bytes = gdbarch_long_long_align_bit (gdbarch) / 8;
342 off = 3 * ptr_size;
343 off = (off + (align_bytes - 1)) & ~(align_bytes - 1);
344
345 code_entry->symfile_size =
346 extract_unsigned_integer (&entry_buf[off], 8, byte_order);
347 }
348
349 /* Proxy object for building a block. */
350
351 struct gdb_block
352 {
353 /* gdb_blocks are linked into a tree structure. Next points to the
354 next node at the same depth as this block and parent to the
355 parent gdb_block. */
356 struct gdb_block *next, *parent;
357
358 /* Points to the "real" block that is being built out of this
359 instance. This block will be added to a blockvector, which will
360 then be added to a symtab. */
361 struct block *real_block;
362
363 /* The first and last code address corresponding to this block. */
364 CORE_ADDR begin, end;
365
366 /* The name of this block (if any). If this is non-NULL, the
367 FUNCTION symbol symbol is set to this value. */
368 const char *name;
369 };
370
371 /* Proxy object for building a symtab. */
372
373 struct gdb_symtab
374 {
375 /* The list of blocks in this symtab. These will eventually be
376 converted to real blocks. */
377 struct gdb_block *blocks;
378
379 /* The number of blocks inserted. */
380 int nblocks;
381
382 /* A mapping between line numbers to PC. */
383 struct linetable *linetable;
384
385 /* The source file for this symtab. */
386 const char *file_name;
387 struct gdb_symtab *next;
388 };
389
390 /* Proxy object for building an object. */
391
392 struct gdb_object
393 {
394 struct gdb_symtab *symtabs;
395 };
396
397 /* The type of the `private' data passed around by the callback
398 functions. */
399
400 typedef CORE_ADDR jit_dbg_reader_data;
401
402 /* The reader calls into this function to read data off the targets
403 address space. */
404
405 static enum gdb_status
406 jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len)
407 {
408 int result = target_read_memory ((CORE_ADDR) target_mem, gdb_buf, len);
409 if (result == 0)
410 return GDB_SUCCESS;
411 else
412 return GDB_FAIL;
413 }
414
415 /* The reader calls into this function to create a new gdb_object
416 which it can then pass around to the other callbacks. Right now,
417 all that is required is allocating the memory. */
418
419 static struct gdb_object *
420 jit_object_open_impl (struct gdb_symbol_callbacks *cb)
421 {
422 /* CB is not required right now, but sometime in the future we might
423 need a handle to it, and we'd like to do that without breaking
424 the ABI. */
425 return XZALLOC (struct gdb_object);
426 }
427
428 /* Readers call into this function to open a new gdb_symtab, which,
429 again, is passed around to other callbacks. */
430
431 static struct gdb_symtab *
432 jit_symtab_open_impl (struct gdb_symbol_callbacks *cb,
433 struct gdb_object *object,
434 const char *file_name)
435 {
436 struct gdb_symtab *ret;
437
438 /* CB stays unused. See comment in jit_object_open_impl. */
439
440 ret = XZALLOC (struct gdb_symtab);
441 ret->file_name = file_name ? xstrdup (file_name) : xstrdup ("");
442 ret->next = object->symtabs;
443 object->symtabs = ret;
444 return ret;
445 }
446
447 /* Returns true if the block corresponding to old should be placed
448 before the block corresponding to new in the final blockvector. */
449
450 static int
451 compare_block (const struct gdb_block *const old,
452 const struct gdb_block *const new)
453 {
454 if (old == NULL)
455 return 1;
456 if (old->begin < new->begin)
457 return 1;
458 else if (old->begin == new->begin)
459 {
460 if (old->end > new->end)
461 return 1;
462 else
463 return 0;
464 }
465 else
466 return 0;
467 }
468
469 /* Called by readers to open a new gdb_block. This function also
470 inserts the new gdb_block in the correct place in the corresponding
471 gdb_symtab. */
472
473 static struct gdb_block *
474 jit_block_open_impl (struct gdb_symbol_callbacks *cb,
475 struct gdb_symtab *symtab, struct gdb_block *parent,
476 GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name)
477 {
478 struct gdb_block *block = XZALLOC (struct gdb_block);
479
480 block->next = symtab->blocks;
481 block->begin = (CORE_ADDR) begin;
482 block->end = (CORE_ADDR) end;
483 block->name = name ? xstrdup (name) : NULL;
484 block->parent = parent;
485
486 /* Ensure that the blocks are inserted in the correct (reverse of
487 the order expected by blockvector). */
488 if (compare_block (symtab->blocks, block))
489 {
490 symtab->blocks = block;
491 }
492 else
493 {
494 struct gdb_block *i = symtab->blocks;
495
496 for (;; i = i->next)
497 {
498 /* Guaranteed to terminate, since compare_block (NULL, _)
499 returns 1. */
500 if (compare_block (i->next, block))
501 {
502 block->next = i->next;
503 i->next = block;
504 break;
505 }
506 }
507 }
508 symtab->nblocks++;
509
510 return block;
511 }
512
513 /* Readers call this to add a line mapping (from PC to line number) to
514 a gdb_symtab. */
515
516 static void
517 jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb,
518 struct gdb_symtab *stab, int nlines,
519 struct gdb_line_mapping *map)
520 {
521 int i;
522
523 if (nlines < 1)
524 return;
525
526 stab->linetable = xmalloc (sizeof (struct linetable)
527 + (nlines - 1) * sizeof (struct linetable_entry));
528 stab->linetable->nitems = nlines;
529 for (i = 0; i < nlines; i++)
530 {
531 stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc;
532 stab->linetable->item[i].line = map[i].line;
533 }
534 }
535
536 /* Called by readers to close a gdb_symtab. Does not need to do
537 anything as of now. */
538
539 static void
540 jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
541 struct gdb_symtab *stab)
542 {
543 /* Right now nothing needs to be done here. We may need to do some
544 cleanup here in the future (again, without breaking the plugin
545 ABI). */
546 }
547
548 /* Transform STAB to a proper symtab, and add it it OBJFILE. */
549
550 static void
551 finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
552 {
553 struct symtab *symtab;
554 struct gdb_block *gdb_block_iter, *gdb_block_iter_tmp;
555 struct block *block_iter;
556 int actual_nblocks, i, blockvector_size;
557 CORE_ADDR begin, end;
558
559 actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks;
560
561 symtab = allocate_symtab (stab->file_name, objfile);
562 /* JIT compilers compile in memory. */
563 symtab->dirname = NULL;
564
565 /* Copy over the linetable entry if one was provided. */
566 if (stab->linetable)
567 {
568 int size = ((stab->linetable->nitems - 1)
569 * sizeof (struct linetable_entry)
570 + sizeof (struct linetable));
571 LINETABLE (symtab) = obstack_alloc (&objfile->objfile_obstack, size);
572 memcpy (LINETABLE (symtab), stab->linetable, size);
573 }
574 else
575 {
576 LINETABLE (symtab) = NULL;
577 }
578
579 blockvector_size = (sizeof (struct blockvector)
580 + (actual_nblocks - 1) * sizeof (struct block *));
581 symtab->blockvector = obstack_alloc (&objfile->objfile_obstack,
582 blockvector_size);
583
584 /* (begin, end) will contain the PC range this entire blockvector
585 spans. */
586 symtab->primary = 1;
587 BLOCKVECTOR_MAP (symtab->blockvector) = NULL;
588 begin = stab->blocks->begin;
589 end = stab->blocks->end;
590 BLOCKVECTOR_NBLOCKS (symtab->blockvector) = actual_nblocks;
591
592 /* First run over all the gdb_block objects, creating a real block
593 object for each. Simultaneously, keep setting the real_block
594 fields. */
595 for (i = (actual_nblocks - 1), gdb_block_iter = stab->blocks;
596 i >= FIRST_LOCAL_BLOCK;
597 i--, gdb_block_iter = gdb_block_iter->next)
598 {
599 struct block *new_block = allocate_block (&objfile->objfile_obstack);
600 struct symbol *block_name = obstack_alloc (&objfile->objfile_obstack,
601 sizeof (struct symbol));
602
603 BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
604 NULL);
605 /* The address range. */
606 BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter->begin;
607 BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter->end;
608
609 /* The name. */
610 memset (block_name, 0, sizeof (struct symbol));
611 SYMBOL_DOMAIN (block_name) = VAR_DOMAIN;
612 SYMBOL_CLASS (block_name) = LOC_BLOCK;
613 SYMBOL_SYMTAB (block_name) = symtab;
614 SYMBOL_BLOCK_VALUE (block_name) = new_block;
615
616 block_name->ginfo.name = obsavestring (gdb_block_iter->name,
617 strlen (gdb_block_iter->name),
618 &objfile->objfile_obstack);
619
620 BLOCK_FUNCTION (new_block) = block_name;
621
622 BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
623 if (begin > BLOCK_START (new_block))
624 begin = BLOCK_START (new_block);
625 if (end < BLOCK_END (new_block))
626 end = BLOCK_END (new_block);
627
628 gdb_block_iter->real_block = new_block;
629 }
630
631 /* Now add the special blocks. */
632 block_iter = NULL;
633 for (i = 0; i < FIRST_LOCAL_BLOCK; i++)
634 {
635 struct block *new_block = allocate_block (&objfile->objfile_obstack);
636 BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack,
637 NULL);
638 BLOCK_SUPERBLOCK (new_block) = block_iter;
639 block_iter = new_block;
640
641 BLOCK_START (new_block) = (CORE_ADDR) begin;
642 BLOCK_END (new_block) = (CORE_ADDR) end;
643
644 BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block;
645 }
646
647 /* Fill up the superblock fields for the real blocks, using the
648 real_block fields populated earlier. */
649 for (gdb_block_iter = stab->blocks;
650 gdb_block_iter;
651 gdb_block_iter = gdb_block_iter->next)
652 {
653 if (gdb_block_iter->parent != NULL)
654 BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
655 gdb_block_iter->parent->real_block;
656 }
657
658 /* Free memory. */
659 gdb_block_iter = stab->blocks;
660
661 for (gdb_block_iter = stab->blocks, gdb_block_iter_tmp = gdb_block_iter->next;
662 gdb_block_iter;
663 gdb_block_iter = gdb_block_iter_tmp)
664 {
665 xfree ((void *) gdb_block_iter->name);
666 xfree (gdb_block_iter);
667 }
668 xfree (stab->linetable);
669 xfree ((char *) stab->file_name);
670 xfree (stab);
671 }
672
673 /* Called when closing a gdb_objfile. Converts OBJ to a proper
674 objfile. */
675
676 static void
677 jit_object_close_impl (struct gdb_symbol_callbacks *cb,
678 struct gdb_object *obj)
679 {
680 struct gdb_symtab *i, *j;
681 struct objfile *objfile;
682 jit_dbg_reader_data *priv_data;
683
684 priv_data = cb->priv_data;
685
686 objfile = allocate_objfile (NULL, 0);
687 objfile->gdbarch = target_gdbarch;
688
689 objfile->msymbols = obstack_alloc (&objfile->objfile_obstack,
690 sizeof (struct minimal_symbol));
691 memset (objfile->msymbols, 0, sizeof (struct minimal_symbol));
692
693 xfree (objfile->name);
694 objfile->name = xstrdup ("<< JIT compiled code >>");
695
696 j = NULL;
697 for (i = obj->symtabs; i; i = j)
698 {
699 j = i->next;
700 finalize_symtab (i, objfile);
701 }
702 add_objfile_entry (objfile, *priv_data);
703 xfree (obj);
704 }
705
706 /* Try to read CODE_ENTRY using the loaded jit reader (if any). */
707
708 static int
709 jit_reader_try_read_symtab (struct jit_code_entry *code_entry)
710 {
711 void *gdb_mem;
712 int status;
713 struct jit_dbg_reader *i;
714 jit_dbg_reader_data priv_data;
715 struct gdb_reader_funcs *funcs;
716 volatile struct gdb_exception e;
717 struct gdb_symbol_callbacks callbacks =
718 {
719 jit_object_open_impl,
720 jit_symtab_open_impl,
721 jit_block_open_impl,
722 jit_symtab_close_impl,
723 jit_object_close_impl,
724
725 jit_symtab_line_mapping_add_impl,
726 jit_target_read_impl,
727
728 &priv_data
729 };
730
731 priv_data = code_entry->symfile_addr;
732
733 if (!loaded_jit_reader)
734 return 0;
735
736 gdb_mem = xmalloc (code_entry->symfile_size);
737
738 status = 1;
739 TRY_CATCH (e, RETURN_MASK_ALL)
740 if (target_read_memory (code_entry->symfile_addr, gdb_mem,
741 code_entry->symfile_size))
742 status = 0;
743 if (e.reason < 0)
744 status = 0;
745
746 if (status)
747 {
748 funcs = loaded_jit_reader->functions;
749 if (funcs->read (funcs, &callbacks, gdb_mem, code_entry->symfile_size)
750 != GDB_SUCCESS)
751 status = 0;
752 }
753
754 xfree (gdb_mem);
755 if (jit_debug && status == 0)
756 fprintf_unfiltered (gdb_stdlog,
757 "Could not read symtab using the loaded JIT reader.\n");
758 return status;
759 }
760
761 /* Try to read CODE_ENTRY using BFD. */
762
763 static void
764 jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
765 struct gdbarch *gdbarch)
766 {
767 bfd *nbfd;
768 struct section_addr_info *sai;
769 struct bfd_section *sec;
770 struct objfile *objfile;
771 struct cleanup *old_cleanups;
772 int i;
773 const struct bfd_arch_info *b;
774
775 if (jit_debug)
776 fprintf_unfiltered (gdb_stdlog,
777 "jit_register_code, symfile_addr = %s, "
778 "symfile_size = %s\n",
779 paddress (gdbarch, code_entry->symfile_addr),
780 pulongest (code_entry->symfile_size));
781
782 nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
783 code_entry->symfile_size, gnutarget);
784 if (nbfd == NULL)
785 {
786 puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
787 return;
788 }
789
790 /* Check the format. NOTE: This initializes important data that GDB uses!
791 We would segfault later without this line. */
792 if (!bfd_check_format (nbfd, bfd_object))
793 {
794 printf_unfiltered (_("\
795 JITed symbol file is not an object file, ignoring it.\n"));
796 bfd_close (nbfd);
797 return;
798 }
799
800 /* Check bfd arch. */
801 b = gdbarch_bfd_arch_info (gdbarch);
802 if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
803 warning (_("JITed object file architecture %s is not compatible "
804 "with target architecture %s."), bfd_get_arch_info
805 (nbfd)->printable_name, b->printable_name);
806
807 /* Read the section address information out of the symbol file. Since the
808 file is generated by the JIT at runtime, it should all of the absolute
809 addresses that we care about. */
810 sai = alloc_section_addr_info (bfd_count_sections (nbfd));
811 old_cleanups = make_cleanup_free_section_addr_info (sai);
812 i = 0;
813 for (sec = nbfd->sections; sec != NULL; sec = sec->next)
814 if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
815 {
816 /* We assume that these virtual addresses are absolute, and do not
817 treat them as offsets. */
818 sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
819 sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
820 sai->other[i].sectindex = sec->index;
821 ++i;
822 }
823
824 /* This call takes ownership of NBFD. It does not take ownership of SAI. */
825 objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
826
827 do_cleanups (old_cleanups);
828 add_objfile_entry (objfile, code_entry->symfile_addr);
829 }
830
831 /* This function registers code associated with a JIT code entry. It uses the
832 pointer and size pair in the entry to read the symbol file from the remote
833 and then calls symbol_file_add_from_local_memory to add it as though it were
834 a symbol file added by the user. */
835
836 static void
837 jit_register_code (struct gdbarch *gdbarch,
838 CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
839 {
840 int i, success;
841 const struct bfd_arch_info *b;
842 struct jit_inferior_data *inf_data = get_jit_inferior_data ();
843
844 if (jit_debug)
845 fprintf_unfiltered (gdb_stdlog,
846 "jit_register_code, symfile_addr = %s, "
847 "symfile_size = %s\n",
848 paddress (gdbarch, code_entry->symfile_addr),
849 pulongest (code_entry->symfile_size));
850
851 success = jit_reader_try_read_symtab (code_entry);
852
853 if (!success)
854 jit_bfd_try_read_symtab (code_entry, gdbarch);
855 }
856
857 /* This function unregisters JITed code and frees the corresponding
858 objfile. */
859
860 static void
861 jit_unregister_code (struct objfile *objfile)
862 {
863 free_objfile (objfile);
864 }
865
866 /* Look up the objfile with this code entry address. */
867
868 static struct objfile *
869 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
870 {
871 struct objfile *objf;
872 CORE_ADDR *objf_entry_addr;
873
874 ALL_OBJFILES (objf)
875 {
876 objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data);
877 if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr)
878 return objf;
879 }
880 return NULL;
881 }
882
883 /* (Re-)Initialize the jit breakpoint if necessary.
884 Return 0 on success. */
885
886 static int
887 jit_breakpoint_re_set_internal (struct gdbarch *gdbarch,
888 struct jit_inferior_data *inf_data)
889 {
890 if (inf_data->breakpoint_addr == 0)
891 {
892 struct minimal_symbol *reg_symbol;
893
894 /* Lookup the registration symbol. If it is missing, then we assume
895 we are not attached to a JIT. */
896 reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL);
897 if (reg_symbol == NULL)
898 return 1;
899 inf_data->breakpoint_addr = SYMBOL_VALUE_ADDRESS (reg_symbol);
900 if (inf_data->breakpoint_addr == 0)
901 return 2;
902
903 /* If we have not read the jit descriptor yet (e.g. because the JITer
904 itself is in a shared library which just got loaded), do so now. */
905 if (inf_data->descriptor_addr == 0)
906 jit_inferior_init (gdbarch);
907 }
908 else
909 return 0;
910
911 if (jit_debug)
912 fprintf_unfiltered (gdb_stdlog,
913 "jit_breakpoint_re_set_internal, "
914 "breakpoint_addr = %s\n",
915 paddress (gdbarch, inf_data->breakpoint_addr));
916
917 /* Put a breakpoint in the registration symbol. */
918 create_jit_event_breakpoint (gdbarch, inf_data->breakpoint_addr);
919
920 return 0;
921 }
922
923 /* Register any already created translations. */
924
925 static void
926 jit_inferior_init (struct gdbarch *gdbarch)
927 {
928 struct jit_descriptor descriptor;
929 struct jit_code_entry cur_entry;
930 struct jit_inferior_data *inf_data;
931 CORE_ADDR cur_entry_addr;
932
933 if (jit_debug)
934 fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n");
935
936 inf_data = get_jit_inferior_data ();
937 if (jit_breakpoint_re_set_internal (gdbarch, inf_data) != 0)
938 return;
939
940 if (inf_data->descriptor_addr == 0)
941 {
942 struct minimal_symbol *desc_symbol;
943
944 /* Lookup the descriptor symbol and cache the addr. If it is
945 missing, we assume we are not attached to a JIT and return early. */
946 desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL);
947 if (desc_symbol == NULL)
948 return;
949
950 inf_data->descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol);
951 if (inf_data->descriptor_addr == 0)
952 return;
953 }
954
955 if (jit_debug)
956 fprintf_unfiltered (gdb_stdlog,
957 "jit_inferior_init, descriptor_addr = %s\n",
958 paddress (gdbarch, inf_data->descriptor_addr));
959
960 /* Read the descriptor so we can check the version number and load
961 any already JITed functions. */
962 jit_read_descriptor (gdbarch, &descriptor, inf_data->descriptor_addr);
963
964 /* Check that the version number agrees with that we support. */
965 if (descriptor.version != 1)
966 error (_("Unsupported JIT protocol version in descriptor!"));
967
968 /* If we've attached to a running program, we need to check the descriptor
969 to register any functions that were already generated. */
970 for (cur_entry_addr = descriptor.first_entry;
971 cur_entry_addr != 0;
972 cur_entry_addr = cur_entry.next_entry)
973 {
974 jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
975
976 /* This hook may be called many times during setup, so make sure we don't
977 add the same symbol file twice. */
978 if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
979 continue;
980
981 jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
982 }
983 }
984
985 /* Exported routine to call when an inferior has been created. */
986
987 void
988 jit_inferior_created_hook (void)
989 {
990 jit_inferior_init (target_gdbarch);
991 }
992
993 /* Exported routine to call to re-set the jit breakpoints,
994 e.g. when a program is rerun. */
995
996 void
997 jit_breakpoint_re_set (void)
998 {
999 jit_breakpoint_re_set_internal (target_gdbarch,
1000 get_jit_inferior_data ());
1001 }
1002
1003 /* Reset inferior_data, so sybols will be looked up again, and jit_breakpoint
1004 will be reset. */
1005
1006 static void
1007 jit_reset_inferior_data_and_breakpoints (void)
1008 {
1009 struct jit_inferior_data *inf_data;
1010
1011 /* Force jit_inferior_init to re-lookup of jit symbol addresses. */
1012 inf_data = get_jit_inferior_data ();
1013 inf_data->breakpoint_addr = 0;
1014 inf_data->descriptor_addr = 0;
1015
1016 /* Remove any existing JIT breakpoint(s). */
1017 remove_jit_event_breakpoints ();
1018
1019 jit_inferior_init (target_gdbarch);
1020 }
1021
1022 /* Wrapper to match the observer function pointer prototype. */
1023
1024 static void
1025 jit_inferior_created_observer (struct target_ops *objfile, int from_tty)
1026 {
1027 jit_reset_inferior_data_and_breakpoints ();
1028 }
1029
1030 /* This function cleans up any code entries left over when the
1031 inferior exits. We get left over code when the inferior exits
1032 without unregistering its code, for example when it crashes. */
1033
1034 static void
1035 jit_inferior_exit_hook (struct inferior *inf)
1036 {
1037 struct objfile *objf;
1038 struct objfile *temp;
1039
1040 ALL_OBJFILES_SAFE (objf, temp)
1041 if (objfile_data (objf, jit_objfile_data) != NULL)
1042 jit_unregister_code (objf);
1043 }
1044
1045 static void
1046 jit_executable_changed_observer (void)
1047 {
1048 jit_reset_inferior_data_and_breakpoints ();
1049 }
1050
1051 void
1052 jit_event_handler (struct gdbarch *gdbarch)
1053 {
1054 struct jit_descriptor descriptor;
1055 struct jit_code_entry code_entry;
1056 CORE_ADDR entry_addr;
1057 struct objfile *objf;
1058
1059 /* Read the descriptor from remote memory. */
1060 jit_read_descriptor (gdbarch, &descriptor,
1061 get_jit_inferior_data ()->descriptor_addr);
1062 entry_addr = descriptor.relevant_entry;
1063
1064 /* Do the corresponding action. */
1065 switch (descriptor.action_flag)
1066 {
1067 case JIT_NOACTION:
1068 break;
1069 case JIT_REGISTER:
1070 jit_read_code_entry (gdbarch, entry_addr, &code_entry);
1071 jit_register_code (gdbarch, entry_addr, &code_entry);
1072 break;
1073 case JIT_UNREGISTER:
1074 objf = jit_find_objf_with_entry_addr (entry_addr);
1075 if (objf == NULL)
1076 printf_unfiltered (_("Unable to find JITed code "
1077 "entry at address: %s\n"),
1078 paddress (gdbarch, entry_addr));
1079 else
1080 jit_unregister_code (objf);
1081
1082 break;
1083 default:
1084 error (_("Unknown action_flag value in JIT descriptor!"));
1085 break;
1086 }
1087 }
1088
1089 /* Called to free the data allocated to the jit_inferior_data slot. */
1090
1091 static void
1092 free_objfile_data (struct objfile *objfile, void *data)
1093 {
1094 xfree (data);
1095 }
1096
1097 /* Provide a prototype to silence -Wmissing-prototypes. */
1098
1099 extern void _initialize_jit (void);
1100
1101 void
1102 _initialize_jit (void)
1103 {
1104 jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR,
1105 JIT_READER_DIR_RELOCATABLE);
1106 add_setshow_zinteger_cmd ("jit", class_maintenance, &jit_debug,
1107 _("Set JIT debugging."),
1108 _("Show JIT debugging."),
1109 _("When non-zero, JIT debugging is enabled."),
1110 NULL,
1111 show_jit_debug,
1112 &setdebuglist, &showdebuglist);
1113
1114 observer_attach_inferior_created (jit_inferior_created_observer);
1115 observer_attach_inferior_exit (jit_inferior_exit_hook);
1116 observer_attach_executable_changed (jit_executable_changed_observer);
1117 jit_objfile_data =
1118 register_objfile_data_with_cleanup (NULL, free_objfile_data);
1119 jit_inferior_data =
1120 register_inferior_data_with_cleanup (jit_inferior_data_cleanup);
1121 if (is_dl_available ())
1122 {
1123 add_com ("jit-reader-load", no_class, jit_reader_load_command, _("\
1124 Load FILE as debug info reader and unwinder for JIT compiled code.\n\
1125 Usage: jit-reader-load FILE\n\
1126 Try to load file FILE as a debug info reader (and unwinder) for\n\
1127 JIT compiled code. The file is loaded from " JIT_READER_DIR ",\n\
1128 relocated relative to the GDB executable if required."));
1129 add_com ("jit-reader-unload", no_class, jit_reader_unload_command, _("\
1130 Unload the currently loaded JIT debug info reader.\n\
1131 Usage: jit-reader-unload FILE\n\n\
1132 Do \"help jit-reader-load\" for info on loading debug info readers."));
1133 }
1134 }