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