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