gdb/
[deliverable/binutils-gdb.git] / gdb / jit.c
CommitLineData
4efc6507
DE
1/* Handle JIT code generation in the inferior for GDB, the GNU Debugger.
2
7b6bb8da 3 Copyright (C) 2009, 2010, 2011 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"
4efc6507 24#include "breakpoint.h"
a255712f
PP
25#include "command.h"
26#include "gdbcmd.h"
4efc6507 27#include "gdbcore.h"
03673fc7 28#include "inferior.h"
4efc6507
DE
29#include "observer.h"
30#include "objfiles.h"
31#include "symfile.h"
32#include "symtab.h"
33#include "target.h"
34#include "gdb_stat.h"
35
b8e0a31c
SD
36static const char *jit_reader_dir = NULL;
37
4efc6507
DE
38static const struct objfile_data *jit_objfile_data;
39
40static const char *const jit_break_name = "__jit_debug_register_code";
41
42static const char *const jit_descriptor_name = "__jit_debug_descriptor";
43
03673fc7 44static const struct inferior_data *jit_inferior_data = NULL;
4efc6507 45
e2bd3b15 46static void jit_inferior_init (struct gdbarch *gdbarch);
3b2a0cf2 47
a255712f
PP
48/* Non-zero if we want to see trace of jit level stuff. */
49
50static int jit_debug = 0;
51
52static void
53show_jit_debug (struct ui_file *file, int from_tty,
54 struct cmd_list_element *c, const char *value)
55{
56 fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
57}
58
4efc6507
DE
59struct target_buffer
60{
61 CORE_ADDR base;
a255712f 62 ULONGEST size;
4efc6507
DE
63};
64
65/* Openning the file is a no-op. */
66
67static void *
68mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
69{
70 return open_closure;
71}
72
73/* Closing the file is just freeing the base/size pair on our side. */
74
75static int
76mem_bfd_iovec_close (struct bfd *abfd, void *stream)
77{
78 xfree (stream);
79 return 1;
80}
81
82/* For reading the file, we just need to pass through to target_read_memory and
83 fix up the arguments and return values. */
84
85static file_ptr
86mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
87 file_ptr nbytes, file_ptr offset)
88{
89 int err;
90 struct target_buffer *buffer = (struct target_buffer *) stream;
91
92 /* If this read will read all of the file, limit it to just the rest. */
93 if (offset + nbytes > buffer->size)
94 nbytes = buffer->size - offset;
95
96 /* If there are no more bytes left, we've reached EOF. */
97 if (nbytes == 0)
98 return 0;
99
100 err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
101 if (err)
102 return -1;
103
104 return nbytes;
105}
106
107/* For statting the file, we only support the st_size attribute. */
108
109static int
110mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
111{
112 struct target_buffer *buffer = (struct target_buffer*) stream;
113
114 sb->st_size = buffer->size;
115 return 0;
116}
117
118/* Open a BFD from the target's memory. */
119
120static struct bfd *
a255712f 121bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
4efc6507
DE
122{
123 const char *filename = xstrdup ("<in-memory>");
124 struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
125
126 buffer->base = addr;
127 buffer->size = size;
128 return bfd_openr_iovec (filename, target,
129 mem_bfd_iovec_open,
130 buffer,
131 mem_bfd_iovec_pread,
132 mem_bfd_iovec_close,
133 mem_bfd_iovec_stat);
134}
135
03673fc7
PP
136/* Per-inferior structure recording the addresses in the inferior. */
137
138struct jit_inferior_data
139{
140 CORE_ADDR breakpoint_addr; /* &__jit_debug_register_code() */
141 CORE_ADDR descriptor_addr; /* &__jit_debug_descriptor */
142};
143
144/* Return jit_inferior_data for current inferior. Allocate if not already
145 present. */
146
147static struct jit_inferior_data *
148get_jit_inferior_data (void)
149{
150 struct inferior *inf;
151 struct jit_inferior_data *inf_data;
152
153 inf = current_inferior ();
154 inf_data = inferior_data (inf, jit_inferior_data);
155 if (inf_data == NULL)
156 {
157 inf_data = XZALLOC (struct jit_inferior_data);
158 set_inferior_data (inf, jit_inferior_data, inf_data);
159 }
160
161 return inf_data;
162}
163
164static void
165jit_inferior_data_cleanup (struct inferior *inf, void *arg)
166{
167 xfree (arg);
168}
169
1777feb0
MS
170/* Helper function for reading the global JIT descriptor from remote
171 memory. */
4efc6507
DE
172
173static void
0756c555 174jit_read_descriptor (struct gdbarch *gdbarch,
03673fc7
PP
175 struct jit_descriptor *descriptor,
176 CORE_ADDR descriptor_addr)
4efc6507
DE
177{
178 int err;
179 struct type *ptr_type;
180 int ptr_size;
181 int desc_size;
182 gdb_byte *desc_buf;
0756c555 183 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4efc6507
DE
184
185 /* Figure out how big the descriptor is on the remote and how to read it. */
0756c555 186 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4efc6507
DE
187 ptr_size = TYPE_LENGTH (ptr_type);
188 desc_size = 8 + 2 * ptr_size; /* Two 32-bit ints and two pointers. */
189 desc_buf = alloca (desc_size);
190
191 /* Read the descriptor. */
03673fc7 192 err = target_read_memory (descriptor_addr, desc_buf, desc_size);
4efc6507
DE
193 if (err)
194 error (_("Unable to read JIT descriptor from remote memory!"));
195
196 /* Fix the endianness to match the host. */
197 descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
198 descriptor->action_flag =
199 extract_unsigned_integer (&desc_buf[4], 4, byte_order);
200 descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
201 descriptor->first_entry =
202 extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
203}
204
205/* Helper function for reading a JITed code entry from remote memory. */
206
207static void
0756c555
DE
208jit_read_code_entry (struct gdbarch *gdbarch,
209 CORE_ADDR code_addr, struct jit_code_entry *code_entry)
4efc6507 210{
205c306f 211 int err, off;
4efc6507
DE
212 struct type *ptr_type;
213 int ptr_size;
214 int entry_size;
205c306f 215 int align_bytes;
4efc6507 216 gdb_byte *entry_buf;
0756c555 217 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4efc6507
DE
218
219 /* Figure out how big the entry is on the remote and how to read it. */
0756c555 220 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4efc6507
DE
221 ptr_size = TYPE_LENGTH (ptr_type);
222 entry_size = 3 * ptr_size + 8; /* Three pointers and one 64-bit int. */
223 entry_buf = alloca (entry_size);
224
225 /* Read the entry. */
226 err = target_read_memory (code_addr, entry_buf, entry_size);
227 if (err)
228 error (_("Unable to read JIT code entry from remote memory!"));
229
230 /* Fix the endianness to match the host. */
0756c555 231 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4efc6507
DE
232 code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
233 code_entry->prev_entry =
234 extract_typed_address (&entry_buf[ptr_size], ptr_type);
235 code_entry->symfile_addr =
236 extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
205c306f
DM
237
238 align_bytes = gdbarch_long_long_align_bit (gdbarch) / 8;
239 off = 3 * ptr_size;
240 off = (off + (align_bytes - 1)) & ~(align_bytes - 1);
241
4efc6507 242 code_entry->symfile_size =
205c306f 243 extract_unsigned_integer (&entry_buf[off], 8, byte_order);
4efc6507
DE
244}
245
246/* This function registers code associated with a JIT code entry. It uses the
247 pointer and size pair in the entry to read the symbol file from the remote
248 and then calls symbol_file_add_from_local_memory to add it as though it were
249 a symbol file added by the user. */
250
251static void
0756c555
DE
252jit_register_code (struct gdbarch *gdbarch,
253 CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
4efc6507
DE
254{
255 bfd *nbfd;
256 struct section_addr_info *sai;
257 struct bfd_section *sec;
258 struct objfile *objfile;
4dfb2365 259 struct cleanup *old_cleanups;
4efc6507
DE
260 int i;
261 const struct bfd_arch_info *b;
262 CORE_ADDR *entry_addr_ptr;
263
a255712f
PP
264 if (jit_debug)
265 fprintf_unfiltered (gdb_stdlog,
266 "jit_register_code, symfile_addr = %s, "
267 "symfile_size = %s\n",
268 paddress (gdbarch, code_entry->symfile_addr),
269 pulongest (code_entry->symfile_size));
270
4efc6507
DE
271 nbfd = bfd_open_from_target_memory (code_entry->symfile_addr,
272 code_entry->symfile_size, gnutarget);
4dfb2365
JK
273 if (nbfd == NULL)
274 {
275 puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
276 return;
277 }
4efc6507
DE
278
279 /* Check the format. NOTE: This initializes important data that GDB uses!
280 We would segfault later without this line. */
281 if (!bfd_check_format (nbfd, bfd_object))
282 {
283 printf_unfiltered (_("\
284JITed symbol file is not an object file, ignoring it.\n"));
4dfb2365 285 bfd_close (nbfd);
4efc6507
DE
286 return;
287 }
288
289 /* Check bfd arch. */
0756c555 290 b = gdbarch_bfd_arch_info (gdbarch);
4efc6507
DE
291 if (b->compatible (b, bfd_get_arch_info (nbfd)) != b)
292 warning (_("JITed object file architecture %s is not compatible "
293 "with target architecture %s."), bfd_get_arch_info
294 (nbfd)->printable_name, b->printable_name);
295
296 /* Read the section address information out of the symbol file. Since the
297 file is generated by the JIT at runtime, it should all of the absolute
298 addresses that we care about. */
299 sai = alloc_section_addr_info (bfd_count_sections (nbfd));
4dfb2365 300 old_cleanups = make_cleanup_free_section_addr_info (sai);
4efc6507
DE
301 i = 0;
302 for (sec = nbfd->sections; sec != NULL; sec = sec->next)
303 if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
304 {
305 /* We assume that these virtual addresses are absolute, and do not
306 treat them as offsets. */
307 sai->other[i].addr = bfd_get_section_vma (nbfd, sec);
04a679b8 308 sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec));
4efc6507
DE
309 sai->other[i].sectindex = sec->index;
310 ++i;
311 }
312
4dfb2365 313 /* This call takes ownership of NBFD. It does not take ownership of SAI. */
63524580 314 objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
4efc6507 315
4efc6507
DE
316 /* Remember a mapping from entry_addr to objfile. */
317 entry_addr_ptr = xmalloc (sizeof (CORE_ADDR));
318 *entry_addr_ptr = entry_addr;
319 set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr);
320
4dfb2365 321 do_cleanups (old_cleanups);
4efc6507
DE
322}
323
1777feb0
MS
324/* This function unregisters JITed code and frees the corresponding
325 objfile. */
4efc6507
DE
326
327static void
328jit_unregister_code (struct objfile *objfile)
329{
330 free_objfile (objfile);
331}
332
333/* Look up the objfile with this code entry address. */
334
335static struct objfile *
336jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
337{
338 struct objfile *objf;
339 CORE_ADDR *objf_entry_addr;
340
341 ALL_OBJFILES (objf)
342 {
343 objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data);
344 if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr)
345 return objf;
346 }
347 return NULL;
348}
349
03673fc7
PP
350/* (Re-)Initialize the jit breakpoint if necessary.
351 Return 0 on success. */
352
353static int
354jit_breakpoint_re_set_internal (struct gdbarch *gdbarch,
355 struct jit_inferior_data *inf_data)
356{
357 if (inf_data->breakpoint_addr == 0)
358 {
359 struct minimal_symbol *reg_symbol;
360
361 /* Lookup the registration symbol. If it is missing, then we assume
362 we are not attached to a JIT. */
363 reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL);
364 if (reg_symbol == NULL)
365 return 1;
366 inf_data->breakpoint_addr = SYMBOL_VALUE_ADDRESS (reg_symbol);
367 if (inf_data->breakpoint_addr == 0)
368 return 2;
3b2a0cf2
JB
369
370 /* If we have not read the jit descriptor yet (e.g. because the JITer
371 itself is in a shared library which just got loaded), do so now. */
372 if (inf_data->descriptor_addr == 0)
373 jit_inferior_init (gdbarch);
03673fc7
PP
374 }
375 else
376 return 0;
377
378 if (jit_debug)
379 fprintf_unfiltered (gdb_stdlog,
380 "jit_breakpoint_re_set_internal, "
381 "breakpoint_addr = %s\n",
382 paddress (gdbarch, inf_data->breakpoint_addr));
383
384 /* Put a breakpoint in the registration symbol. */
385 create_jit_event_breakpoint (gdbarch, inf_data->breakpoint_addr);
386
387 return 0;
388}
389
390/* Register any already created translations. */
0756c555
DE
391
392static void
393jit_inferior_init (struct gdbarch *gdbarch)
4efc6507 394{
4efc6507
DE
395 struct jit_descriptor descriptor;
396 struct jit_code_entry cur_entry;
03673fc7 397 struct jit_inferior_data *inf_data;
4efc6507 398 CORE_ADDR cur_entry_addr;
4efc6507 399
a255712f 400 if (jit_debug)
03673fc7 401 fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n");
a255712f 402
03673fc7
PP
403 inf_data = get_jit_inferior_data ();
404 if (jit_breakpoint_re_set_internal (gdbarch, inf_data) != 0)
4efc6507
DE
405 return;
406
03673fc7
PP
407 if (inf_data->descriptor_addr == 0)
408 {
409 struct minimal_symbol *desc_symbol;
4efc6507 410
03673fc7
PP
411 /* Lookup the descriptor symbol and cache the addr. If it is
412 missing, we assume we are not attached to a JIT and return early. */
413 desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL);
414 if (desc_symbol == NULL)
415 return;
a255712f 416
03673fc7
PP
417 inf_data->descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol);
418 if (inf_data->descriptor_addr == 0)
419 return;
420 }
4efc6507 421
a255712f
PP
422 if (jit_debug)
423 fprintf_unfiltered (gdb_stdlog,
03673fc7
PP
424 "jit_inferior_init, descriptor_addr = %s\n",
425 paddress (gdbarch, inf_data->descriptor_addr));
a255712f 426
1777feb0
MS
427 /* Read the descriptor so we can check the version number and load
428 any already JITed functions. */
03673fc7 429 jit_read_descriptor (gdbarch, &descriptor, inf_data->descriptor_addr);
4efc6507
DE
430
431 /* Check that the version number agrees with that we support. */
432 if (descriptor.version != 1)
433 error (_("Unsupported JIT protocol version in descriptor!"));
434
1777feb0
MS
435 /* If we've attached to a running program, we need to check the descriptor
436 to register any functions that were already generated. */
4efc6507
DE
437 for (cur_entry_addr = descriptor.first_entry;
438 cur_entry_addr != 0;
439 cur_entry_addr = cur_entry.next_entry)
440 {
0756c555 441 jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry);
4efc6507
DE
442
443 /* This hook may be called many times during setup, so make sure we don't
444 add the same symbol file twice. */
445 if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL)
446 continue;
447
0756c555 448 jit_register_code (gdbarch, cur_entry_addr, &cur_entry);
4efc6507
DE
449 }
450}
451
0756c555
DE
452/* Exported routine to call when an inferior has been created. */
453
454void
455jit_inferior_created_hook (void)
456{
457 jit_inferior_init (target_gdbarch);
458}
459
460/* Exported routine to call to re-set the jit breakpoints,
461 e.g. when a program is rerun. */
462
463void
464jit_breakpoint_re_set (void)
465{
03673fc7
PP
466 jit_breakpoint_re_set_internal (target_gdbarch,
467 get_jit_inferior_data ());
468}
469
470/* Reset inferior_data, so sybols will be looked up again, and jit_breakpoint
471 will be reset. */
472
473static void
474jit_reset_inferior_data_and_breakpoints (void)
475{
476 struct jit_inferior_data *inf_data;
477
478 /* Force jit_inferior_init to re-lookup of jit symbol addresses. */
479 inf_data = get_jit_inferior_data ();
480 inf_data->breakpoint_addr = 0;
481 inf_data->descriptor_addr = 0;
482
483 /* Remove any existing JIT breakpoint(s). */
484 remove_jit_event_breakpoints ();
485
0756c555
DE
486 jit_inferior_init (target_gdbarch);
487}
488
4efc6507
DE
489/* Wrapper to match the observer function pointer prototype. */
490
491static void
0756c555 492jit_inferior_created_observer (struct target_ops *objfile, int from_tty)
4efc6507 493{
03673fc7 494 jit_reset_inferior_data_and_breakpoints ();
4efc6507
DE
495}
496
1777feb0
MS
497/* This function cleans up any code entries left over when the
498 inferior exits. We get left over code when the inferior exits
499 without unregistering its code, for example when it crashes. */
4efc6507
DE
500
501static void
a79b8f6e 502jit_inferior_exit_hook (struct inferior *inf)
4efc6507
DE
503{
504 struct objfile *objf;
505 struct objfile *temp;
506
4efc6507
DE
507 ALL_OBJFILES_SAFE (objf, temp)
508 if (objfile_data (objf, jit_objfile_data) != NULL)
509 jit_unregister_code (objf);
510}
511
03673fc7
PP
512static void
513jit_executable_changed_observer (void)
514{
515 jit_reset_inferior_data_and_breakpoints ();
516}
517
4efc6507 518void
0756c555 519jit_event_handler (struct gdbarch *gdbarch)
4efc6507
DE
520{
521 struct jit_descriptor descriptor;
522 struct jit_code_entry code_entry;
523 CORE_ADDR entry_addr;
524 struct objfile *objf;
525
526 /* Read the descriptor from remote memory. */
03673fc7
PP
527 jit_read_descriptor (gdbarch, &descriptor,
528 get_jit_inferior_data ()->descriptor_addr);
4efc6507
DE
529 entry_addr = descriptor.relevant_entry;
530
1777feb0 531 /* Do the corresponding action. */
4efc6507
DE
532 switch (descriptor.action_flag)
533 {
534 case JIT_NOACTION:
535 break;
536 case JIT_REGISTER:
0756c555
DE
537 jit_read_code_entry (gdbarch, entry_addr, &code_entry);
538 jit_register_code (gdbarch, entry_addr, &code_entry);
4efc6507
DE
539 break;
540 case JIT_UNREGISTER:
541 objf = jit_find_objf_with_entry_addr (entry_addr);
542 if (objf == NULL)
1777feb0
MS
543 printf_unfiltered (_("Unable to find JITed code "
544 "entry at address: %s\n"),
dfdbc9b4 545 paddress (gdbarch, entry_addr));
4efc6507
DE
546 else
547 jit_unregister_code (objf);
548
549 break;
550 default:
551 error (_("Unknown action_flag value in JIT descriptor!"));
552 break;
553 }
554}
555
556/* Provide a prototype to silence -Wmissing-prototypes. */
557
558extern void _initialize_jit (void);
559
560void
561_initialize_jit (void)
562{
b8e0a31c
SD
563 jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR,
564 JIT_READER_DIR_RELOCATABLE);
1777feb0
MS
565 add_setshow_zinteger_cmd ("jit", class_maintenance, &jit_debug,
566 _("Set JIT debugging."),
567 _("Show JIT debugging."),
568 _("When non-zero, JIT debugging is enabled."),
a255712f
PP
569 NULL,
570 show_jit_debug,
571 &setdebuglist, &showdebuglist);
572
0756c555 573 observer_attach_inferior_created (jit_inferior_created_observer);
4efc6507 574 observer_attach_inferior_exit (jit_inferior_exit_hook);
03673fc7 575 observer_attach_executable_changed (jit_executable_changed_observer);
4efc6507 576 jit_objfile_data = register_objfile_data ();
03673fc7
PP
577 jit_inferior_data =
578 register_inferior_data_with_cleanup (jit_inferior_data_cleanup);
4efc6507 579}
This page took 0.244777 seconds and 4 git commands to generate.