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 "breakpoint.h" 24 #include "command.h" 25 #include "gdbcmd.h" 26 #include "gdbcore.h" 27 #include "inferior.h" 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 35 static const struct objfile_data *jit_objfile_data; 36 37 static const char *const jit_break_name = "__jit_debug_register_code"; 38 39 static const char *const jit_descriptor_name = "__jit_debug_descriptor"; 40 41 static const struct inferior_data *jit_inferior_data = NULL; 42 43 static void 44 jit_inferior_init (struct gdbarch *gdbarch); 45 46 /* Non-zero if we want to see trace of jit level stuff. */ 47 48 static int jit_debug = 0; 49 50 static void 51 show_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 57 struct target_buffer 58 { 59 CORE_ADDR base; 60 ULONGEST size; 61 }; 62 63 /* Openning the file is a no-op. */ 64 65 static void * 66 mem_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 73 static int 74 mem_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 83 static file_ptr 84 mem_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 107 static int 108 mem_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 118 static struct bfd * 119 bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target) 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 134 /* Per-inferior structure recording the addresses in the inferior. */ 135 136 struct 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 145 static struct jit_inferior_data * 146 get_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 162 static void 163 jit_inferior_data_cleanup (struct inferior *inf, void *arg) 164 { 165 xfree (arg); 166 } 167 168 /* Helper function for reading the global JIT descriptor from remote 169 memory. */ 170 171 static void 172 jit_read_descriptor (struct gdbarch *gdbarch, 173 struct jit_descriptor *descriptor, 174 CORE_ADDR descriptor_addr) 175 { 176 int err; 177 struct type *ptr_type; 178 int ptr_size; 179 int desc_size; 180 gdb_byte *desc_buf; 181 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 182 183 /* Figure out how big the descriptor is on the remote and how to read it. */ 184 ptr_type = builtin_type (gdbarch)->builtin_data_ptr; 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. */ 190 err = target_read_memory (descriptor_addr, desc_buf, desc_size); 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 205 static void 206 jit_read_code_entry (struct gdbarch *gdbarch, 207 CORE_ADDR code_addr, struct jit_code_entry *code_entry) 208 { 209 int err; 210 struct type *ptr_type; 211 int ptr_size; 212 int entry_size; 213 gdb_byte *entry_buf; 214 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 215 216 /* Figure out how big the entry is on the remote and how to read it. */ 217 ptr_type = builtin_type (gdbarch)->builtin_data_ptr; 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. */ 228 ptr_type = builtin_type (gdbarch)->builtin_data_ptr; 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); 234 code_entry->symfile_size = 235 extract_unsigned_integer (&entry_buf[3 * ptr_size], 8, byte_order); 236 } 237 238 /* This function registers code associated with a JIT code entry. It uses the 239 pointer and size pair in the entry to read the symbol file from the remote 240 and then calls symbol_file_add_from_local_memory to add it as though it were 241 a symbol file added by the user. */ 242 243 static void 244 jit_register_code (struct gdbarch *gdbarch, 245 CORE_ADDR entry_addr, struct jit_code_entry *code_entry) 246 { 247 bfd *nbfd; 248 struct section_addr_info *sai; 249 struct bfd_section *sec; 250 struct objfile *objfile; 251 struct cleanup *old_cleanups, *my_cleanups; 252 int i; 253 const struct bfd_arch_info *b; 254 CORE_ADDR *entry_addr_ptr; 255 256 if (jit_debug) 257 fprintf_unfiltered (gdb_stdlog, 258 "jit_register_code, symfile_addr = %s, " 259 "symfile_size = %s\n", 260 paddress (gdbarch, code_entry->symfile_addr), 261 pulongest (code_entry->symfile_size)); 262 263 nbfd = bfd_open_from_target_memory (code_entry->symfile_addr, 264 code_entry->symfile_size, gnutarget); 265 old_cleanups = make_cleanup_bfd_close (nbfd); 266 267 /* Check the format. NOTE: This initializes important data that GDB uses! 268 We would segfault later without this line. */ 269 if (!bfd_check_format (nbfd, bfd_object)) 270 { 271 printf_unfiltered (_("\ 272 JITed symbol file is not an object file, ignoring it.\n")); 273 do_cleanups (old_cleanups); 274 return; 275 } 276 277 /* Check bfd arch. */ 278 b = gdbarch_bfd_arch_info (gdbarch); 279 if (b->compatible (b, bfd_get_arch_info (nbfd)) != b) 280 warning (_("JITed object file architecture %s is not compatible " 281 "with target architecture %s."), bfd_get_arch_info 282 (nbfd)->printable_name, b->printable_name); 283 284 /* Read the section address information out of the symbol file. Since the 285 file is generated by the JIT at runtime, it should all of the absolute 286 addresses that we care about. */ 287 sai = alloc_section_addr_info (bfd_count_sections (nbfd)); 288 make_cleanup_free_section_addr_info (sai); 289 i = 0; 290 for (sec = nbfd->sections; sec != NULL; sec = sec->next) 291 if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0) 292 { 293 /* We assume that these virtual addresses are absolute, and do not 294 treat them as offsets. */ 295 sai->other[i].addr = bfd_get_section_vma (nbfd, sec); 296 sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec)); 297 sai->other[i].sectindex = sec->index; 298 ++i; 299 } 300 301 /* This call takes ownership of sai. */ 302 objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED); 303 304 /* Remember a mapping from entry_addr to objfile. */ 305 entry_addr_ptr = xmalloc (sizeof (CORE_ADDR)); 306 *entry_addr_ptr = entry_addr; 307 set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr); 308 309 discard_cleanups (old_cleanups); 310 } 311 312 /* This function unregisters JITed code and frees the corresponding 313 objfile. */ 314 315 static void 316 jit_unregister_code (struct objfile *objfile) 317 { 318 free_objfile (objfile); 319 } 320 321 /* Look up the objfile with this code entry address. */ 322 323 static struct objfile * 324 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr) 325 { 326 struct objfile *objf; 327 CORE_ADDR *objf_entry_addr; 328 329 ALL_OBJFILES (objf) 330 { 331 objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data); 332 if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr) 333 return objf; 334 } 335 return NULL; 336 } 337 338 /* (Re-)Initialize the jit breakpoint if necessary. 339 Return 0 on success. */ 340 341 static int 342 jit_breakpoint_re_set_internal (struct gdbarch *gdbarch, 343 struct jit_inferior_data *inf_data) 344 { 345 if (inf_data->breakpoint_addr == 0) 346 { 347 struct minimal_symbol *reg_symbol; 348 349 /* Lookup the registration symbol. If it is missing, then we assume 350 we are not attached to a JIT. */ 351 reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL); 352 if (reg_symbol == NULL) 353 return 1; 354 inf_data->breakpoint_addr = SYMBOL_VALUE_ADDRESS (reg_symbol); 355 if (inf_data->breakpoint_addr == 0) 356 return 2; 357 358 /* If we have not read the jit descriptor yet (e.g. because the JITer 359 itself is in a shared library which just got loaded), do so now. */ 360 if (inf_data->descriptor_addr == 0) 361 jit_inferior_init (gdbarch); 362 } 363 else 364 return 0; 365 366 if (jit_debug) 367 fprintf_unfiltered (gdb_stdlog, 368 "jit_breakpoint_re_set_internal, " 369 "breakpoint_addr = %s\n", 370 paddress (gdbarch, inf_data->breakpoint_addr)); 371 372 /* Put a breakpoint in the registration symbol. */ 373 create_jit_event_breakpoint (gdbarch, inf_data->breakpoint_addr); 374 375 return 0; 376 } 377 378 /* Register any already created translations. */ 379 380 static void 381 jit_inferior_init (struct gdbarch *gdbarch) 382 { 383 struct jit_descriptor descriptor; 384 struct jit_code_entry cur_entry; 385 struct jit_inferior_data *inf_data; 386 CORE_ADDR cur_entry_addr; 387 388 if (jit_debug) 389 fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n"); 390 391 inf_data = get_jit_inferior_data (); 392 if (jit_breakpoint_re_set_internal (gdbarch, inf_data) != 0) 393 return; 394 395 if (inf_data->descriptor_addr == 0) 396 { 397 struct minimal_symbol *desc_symbol; 398 399 /* Lookup the descriptor symbol and cache the addr. If it is 400 missing, we assume we are not attached to a JIT and return early. */ 401 desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL); 402 if (desc_symbol == NULL) 403 return; 404 405 inf_data->descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol); 406 if (inf_data->descriptor_addr == 0) 407 return; 408 } 409 410 if (jit_debug) 411 fprintf_unfiltered (gdb_stdlog, 412 "jit_inferior_init, descriptor_addr = %s\n", 413 paddress (gdbarch, inf_data->descriptor_addr)); 414 415 /* Read the descriptor so we can check the version number and load 416 any already JITed functions. */ 417 jit_read_descriptor (gdbarch, &descriptor, inf_data->descriptor_addr); 418 419 /* Check that the version number agrees with that we support. */ 420 if (descriptor.version != 1) 421 error (_("Unsupported JIT protocol version in descriptor!")); 422 423 /* If we've attached to a running program, we need to check the descriptor 424 to register any functions that were already generated. */ 425 for (cur_entry_addr = descriptor.first_entry; 426 cur_entry_addr != 0; 427 cur_entry_addr = cur_entry.next_entry) 428 { 429 jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry); 430 431 /* This hook may be called many times during setup, so make sure we don't 432 add the same symbol file twice. */ 433 if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL) 434 continue; 435 436 jit_register_code (gdbarch, cur_entry_addr, &cur_entry); 437 } 438 } 439 440 /* Exported routine to call when an inferior has been created. */ 441 442 void 443 jit_inferior_created_hook (void) 444 { 445 jit_inferior_init (target_gdbarch); 446 } 447 448 /* Exported routine to call to re-set the jit breakpoints, 449 e.g. when a program is rerun. */ 450 451 void 452 jit_breakpoint_re_set (void) 453 { 454 jit_breakpoint_re_set_internal (target_gdbarch, 455 get_jit_inferior_data ()); 456 } 457 458 /* Reset inferior_data, so sybols will be looked up again, and jit_breakpoint 459 will be reset. */ 460 461 static void 462 jit_reset_inferior_data_and_breakpoints (void) 463 { 464 struct jit_inferior_data *inf_data; 465 466 /* Force jit_inferior_init to re-lookup of jit symbol addresses. */ 467 inf_data = get_jit_inferior_data (); 468 inf_data->breakpoint_addr = 0; 469 inf_data->descriptor_addr = 0; 470 471 /* Remove any existing JIT breakpoint(s). */ 472 remove_jit_event_breakpoints (); 473 474 jit_inferior_init (target_gdbarch); 475 } 476 477 /* Wrapper to match the observer function pointer prototype. */ 478 479 static void 480 jit_inferior_created_observer (struct target_ops *objfile, int from_tty) 481 { 482 jit_reset_inferior_data_and_breakpoints (); 483 } 484 485 /* This function cleans up any code entries left over when the 486 inferior exits. We get left over code when the inferior exits 487 without unregistering its code, for example when it crashes. */ 488 489 static void 490 jit_inferior_exit_hook (struct inferior *inf) 491 { 492 struct objfile *objf; 493 struct objfile *temp; 494 495 ALL_OBJFILES_SAFE (objf, temp) 496 if (objfile_data (objf, jit_objfile_data) != NULL) 497 jit_unregister_code (objf); 498 } 499 500 static void 501 jit_executable_changed_observer (void) 502 { 503 jit_reset_inferior_data_and_breakpoints (); 504 } 505 506 void 507 jit_event_handler (struct gdbarch *gdbarch) 508 { 509 struct jit_descriptor descriptor; 510 struct jit_code_entry code_entry; 511 CORE_ADDR entry_addr; 512 struct objfile *objf; 513 514 /* Read the descriptor from remote memory. */ 515 jit_read_descriptor (gdbarch, &descriptor, 516 get_jit_inferior_data ()->descriptor_addr); 517 entry_addr = descriptor.relevant_entry; 518 519 /* Do the corresponding action. */ 520 switch (descriptor.action_flag) 521 { 522 case JIT_NOACTION: 523 break; 524 case JIT_REGISTER: 525 jit_read_code_entry (gdbarch, entry_addr, &code_entry); 526 jit_register_code (gdbarch, entry_addr, &code_entry); 527 break; 528 case JIT_UNREGISTER: 529 objf = jit_find_objf_with_entry_addr (entry_addr); 530 if (objf == NULL) 531 printf_unfiltered (_("Unable to find JITed code " 532 "entry at address: %s\n"), 533 paddress (gdbarch, entry_addr)); 534 else 535 jit_unregister_code (objf); 536 537 break; 538 default: 539 error (_("Unknown action_flag value in JIT descriptor!")); 540 break; 541 } 542 } 543 544 /* Provide a prototype to silence -Wmissing-prototypes. */ 545 546 extern void _initialize_jit (void); 547 548 void 549 _initialize_jit (void) 550 { 551 add_setshow_zinteger_cmd ("jit", class_maintenance, &jit_debug, 552 _("Set JIT debugging."), 553 _("Show JIT debugging."), 554 _("When non-zero, JIT debugging is enabled."), 555 NULL, 556 show_jit_debug, 557 &setdebuglist, &showdebuglist); 558 559 observer_attach_inferior_created (jit_inferior_created_observer); 560 observer_attach_inferior_exit (jit_inferior_exit_hook); 561 observer_attach_executable_changed (jit_executable_changed_observer); 562 jit_objfile_data = register_objfile_data (); 563 jit_inferior_data = 564 register_inferior_data_with_cleanup (jit_inferior_data_cleanup); 565 } 566