| 1 | /* Handle JIT code generation in the inferior for GDB, the GNU Debugger. |
| 2 | |
| 3 | Copyright (C) 2009 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | |
| 23 | #include "jit.h" |
| 24 | #include "breakpoint.h" |
| 25 | #include "gdbcore.h" |
| 26 | #include "observer.h" |
| 27 | #include "objfiles.h" |
| 28 | #include "symfile.h" |
| 29 | #include "symtab.h" |
| 30 | #include "target.h" |
| 31 | #include "gdb_stat.h" |
| 32 | |
| 33 | static const struct objfile_data *jit_objfile_data; |
| 34 | |
| 35 | static const char *const jit_break_name = "__jit_debug_register_code"; |
| 36 | |
| 37 | static const char *const jit_descriptor_name = "__jit_debug_descriptor"; |
| 38 | |
| 39 | /* This is the address of the JIT descriptor in the inferior. */ |
| 40 | |
| 41 | static CORE_ADDR jit_descriptor_addr = 0; |
| 42 | |
| 43 | /* This is a boolean indicating whether we're currently registering code. This |
| 44 | is used to avoid re-entering the registration code. We want to check for |
| 45 | new JITed every time a new object file is loaded, but we want to avoid |
| 46 | checking for new code while we're registering object files for JITed code. |
| 47 | Therefore, we flip this variable to 1 before registering new object files, |
| 48 | and set it to 0 before returning. */ |
| 49 | |
| 50 | static int registering_code = 0; |
| 51 | |
| 52 | /* Helper cleanup function to clear an integer flag like the one above. */ |
| 53 | |
| 54 | static void |
| 55 | clear_int (void *int_addr) |
| 56 | { |
| 57 | *((int *) int_addr) = 0; |
| 58 | } |
| 59 | |
| 60 | struct target_buffer |
| 61 | { |
| 62 | CORE_ADDR base; |
| 63 | size_t size; |
| 64 | }; |
| 65 | |
| 66 | /* Openning the file is a no-op. */ |
| 67 | |
| 68 | static void * |
| 69 | mem_bfd_iovec_open (struct bfd *abfd, void *open_closure) |
| 70 | { |
| 71 | return open_closure; |
| 72 | } |
| 73 | |
| 74 | /* Closing the file is just freeing the base/size pair on our side. */ |
| 75 | |
| 76 | static int |
| 77 | mem_bfd_iovec_close (struct bfd *abfd, void *stream) |
| 78 | { |
| 79 | xfree (stream); |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | /* For reading the file, we just need to pass through to target_read_memory and |
| 84 | fix up the arguments and return values. */ |
| 85 | |
| 86 | static file_ptr |
| 87 | mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf, |
| 88 | file_ptr nbytes, file_ptr offset) |
| 89 | { |
| 90 | int err; |
| 91 | struct target_buffer *buffer = (struct target_buffer *) stream; |
| 92 | |
| 93 | /* If this read will read all of the file, limit it to just the rest. */ |
| 94 | if (offset + nbytes > buffer->size) |
| 95 | nbytes = buffer->size - offset; |
| 96 | |
| 97 | /* If there are no more bytes left, we've reached EOF. */ |
| 98 | if (nbytes == 0) |
| 99 | return 0; |
| 100 | |
| 101 | err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes); |
| 102 | if (err) |
| 103 | return -1; |
| 104 | |
| 105 | return nbytes; |
| 106 | } |
| 107 | |
| 108 | /* For statting the file, we only support the st_size attribute. */ |
| 109 | |
| 110 | static int |
| 111 | mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb) |
| 112 | { |
| 113 | struct target_buffer *buffer = (struct target_buffer*) stream; |
| 114 | |
| 115 | sb->st_size = buffer->size; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* Open a BFD from the target's memory. */ |
| 120 | |
| 121 | static struct bfd * |
| 122 | bfd_open_from_target_memory (CORE_ADDR addr, size_t size, char *target) |
| 123 | { |
| 124 | const char *filename = xstrdup ("<in-memory>"); |
| 125 | struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer)); |
| 126 | |
| 127 | buffer->base = addr; |
| 128 | buffer->size = size; |
| 129 | return bfd_openr_iovec (filename, target, |
| 130 | mem_bfd_iovec_open, |
| 131 | buffer, |
| 132 | mem_bfd_iovec_pread, |
| 133 | mem_bfd_iovec_close, |
| 134 | mem_bfd_iovec_stat); |
| 135 | } |
| 136 | |
| 137 | /* Helper function for reading the global JIT descriptor from remote memory. */ |
| 138 | |
| 139 | static void |
| 140 | jit_read_descriptor (struct gdbarch *gdbarch, |
| 141 | struct jit_descriptor *descriptor) |
| 142 | { |
| 143 | int err; |
| 144 | struct type *ptr_type; |
| 145 | int ptr_size; |
| 146 | int desc_size; |
| 147 | gdb_byte *desc_buf; |
| 148 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 149 | |
| 150 | /* Figure out how big the descriptor is on the remote and how to read it. */ |
| 151 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
| 152 | ptr_size = TYPE_LENGTH (ptr_type); |
| 153 | desc_size = 8 + 2 * ptr_size; /* Two 32-bit ints and two pointers. */ |
| 154 | desc_buf = alloca (desc_size); |
| 155 | |
| 156 | /* Read the descriptor. */ |
| 157 | err = target_read_memory (jit_descriptor_addr, desc_buf, desc_size); |
| 158 | if (err) |
| 159 | error (_("Unable to read JIT descriptor from remote memory!")); |
| 160 | |
| 161 | /* Fix the endianness to match the host. */ |
| 162 | descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order); |
| 163 | descriptor->action_flag = |
| 164 | extract_unsigned_integer (&desc_buf[4], 4, byte_order); |
| 165 | descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type); |
| 166 | descriptor->first_entry = |
| 167 | extract_typed_address (&desc_buf[8 + ptr_size], ptr_type); |
| 168 | } |
| 169 | |
| 170 | /* Helper function for reading a JITed code entry from remote memory. */ |
| 171 | |
| 172 | static void |
| 173 | jit_read_code_entry (struct gdbarch *gdbarch, |
| 174 | CORE_ADDR code_addr, struct jit_code_entry *code_entry) |
| 175 | { |
| 176 | int err; |
| 177 | struct type *ptr_type; |
| 178 | int ptr_size; |
| 179 | int entry_size; |
| 180 | gdb_byte *entry_buf; |
| 181 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 182 | |
| 183 | /* Figure out how big the entry 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 | entry_size = 3 * ptr_size + 8; /* Three pointers and one 64-bit int. */ |
| 187 | entry_buf = alloca (entry_size); |
| 188 | |
| 189 | /* Read the entry. */ |
| 190 | err = target_read_memory (code_addr, entry_buf, entry_size); |
| 191 | if (err) |
| 192 | error (_("Unable to read JIT code entry from remote memory!")); |
| 193 | |
| 194 | /* Fix the endianness to match the host. */ |
| 195 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
| 196 | code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type); |
| 197 | code_entry->prev_entry = |
| 198 | extract_typed_address (&entry_buf[ptr_size], ptr_type); |
| 199 | code_entry->symfile_addr = |
| 200 | extract_typed_address (&entry_buf[2 * ptr_size], ptr_type); |
| 201 | code_entry->symfile_size = |
| 202 | extract_unsigned_integer (&entry_buf[3 * ptr_size], 8, byte_order); |
| 203 | } |
| 204 | |
| 205 | /* This function registers code associated with a JIT code entry. It uses the |
| 206 | pointer and size pair in the entry to read the symbol file from the remote |
| 207 | and then calls symbol_file_add_from_local_memory to add it as though it were |
| 208 | a symbol file added by the user. */ |
| 209 | |
| 210 | static void |
| 211 | jit_register_code (struct gdbarch *gdbarch, |
| 212 | CORE_ADDR entry_addr, struct jit_code_entry *code_entry) |
| 213 | { |
| 214 | bfd *nbfd; |
| 215 | struct section_addr_info *sai; |
| 216 | struct bfd_section *sec; |
| 217 | struct objfile *objfile; |
| 218 | struct cleanup *old_cleanups, *my_cleanups; |
| 219 | int i; |
| 220 | const struct bfd_arch_info *b; |
| 221 | CORE_ADDR *entry_addr_ptr; |
| 222 | |
| 223 | nbfd = bfd_open_from_target_memory (code_entry->symfile_addr, |
| 224 | code_entry->symfile_size, gnutarget); |
| 225 | old_cleanups = make_cleanup_bfd_close (nbfd); |
| 226 | |
| 227 | /* Check the format. NOTE: This initializes important data that GDB uses! |
| 228 | We would segfault later without this line. */ |
| 229 | if (!bfd_check_format (nbfd, bfd_object)) |
| 230 | { |
| 231 | printf_unfiltered (_("\ |
| 232 | JITed symbol file is not an object file, ignoring it.\n")); |
| 233 | do_cleanups (old_cleanups); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | /* Check bfd arch. */ |
| 238 | b = gdbarch_bfd_arch_info (gdbarch); |
| 239 | if (b->compatible (b, bfd_get_arch_info (nbfd)) != b) |
| 240 | warning (_("JITed object file architecture %s is not compatible " |
| 241 | "with target architecture %s."), bfd_get_arch_info |
| 242 | (nbfd)->printable_name, b->printable_name); |
| 243 | |
| 244 | /* Read the section address information out of the symbol file. Since the |
| 245 | file is generated by the JIT at runtime, it should all of the absolute |
| 246 | addresses that we care about. */ |
| 247 | sai = alloc_section_addr_info (bfd_count_sections (nbfd)); |
| 248 | make_cleanup_free_section_addr_info (sai); |
| 249 | i = 0; |
| 250 | for (sec = nbfd->sections; sec != NULL; sec = sec->next) |
| 251 | if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0) |
| 252 | { |
| 253 | /* We assume that these virtual addresses are absolute, and do not |
| 254 | treat them as offsets. */ |
| 255 | sai->other[i].addr = bfd_get_section_vma (nbfd, sec); |
| 256 | sai->other[i].name = (char *) bfd_get_section_name (nbfd, sec); |
| 257 | sai->other[i].sectindex = sec->index; |
| 258 | ++i; |
| 259 | } |
| 260 | |
| 261 | /* Raise this flag while we register code so we won't trigger any |
| 262 | re-registration. */ |
| 263 | registering_code = 1; |
| 264 | my_cleanups = make_cleanup (clear_int, ®istering_code); |
| 265 | |
| 266 | /* This call takes ownership of sai. */ |
| 267 | objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED); |
| 268 | |
| 269 | /* Clear the registering_code flag. */ |
| 270 | do_cleanups (my_cleanups); |
| 271 | |
| 272 | /* Remember a mapping from entry_addr to objfile. */ |
| 273 | entry_addr_ptr = xmalloc (sizeof (CORE_ADDR)); |
| 274 | *entry_addr_ptr = entry_addr; |
| 275 | set_objfile_data (objfile, jit_objfile_data, entry_addr_ptr); |
| 276 | |
| 277 | discard_cleanups (old_cleanups); |
| 278 | } |
| 279 | |
| 280 | /* This function unregisters JITed code and frees the corresponding objfile. */ |
| 281 | |
| 282 | static void |
| 283 | jit_unregister_code (struct objfile *objfile) |
| 284 | { |
| 285 | free_objfile (objfile); |
| 286 | } |
| 287 | |
| 288 | /* Look up the objfile with this code entry address. */ |
| 289 | |
| 290 | static struct objfile * |
| 291 | jit_find_objf_with_entry_addr (CORE_ADDR entry_addr) |
| 292 | { |
| 293 | struct objfile *objf; |
| 294 | CORE_ADDR *objf_entry_addr; |
| 295 | |
| 296 | ALL_OBJFILES (objf) |
| 297 | { |
| 298 | objf_entry_addr = (CORE_ADDR *) objfile_data (objf, jit_objfile_data); |
| 299 | if (objf_entry_addr != NULL && *objf_entry_addr == entry_addr) |
| 300 | return objf; |
| 301 | } |
| 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | /* (Re-)Initialize the jit breakpoint handler, and register any already |
| 306 | created translations. */ |
| 307 | |
| 308 | static void |
| 309 | jit_inferior_init (struct gdbarch *gdbarch) |
| 310 | { |
| 311 | struct minimal_symbol *reg_symbol; |
| 312 | struct minimal_symbol *desc_symbol; |
| 313 | CORE_ADDR reg_addr; |
| 314 | struct jit_descriptor descriptor; |
| 315 | struct jit_code_entry cur_entry; |
| 316 | CORE_ADDR cur_entry_addr; |
| 317 | struct cleanup *old_cleanups; |
| 318 | |
| 319 | /* When we register code, GDB resets its breakpoints in case symbols have |
| 320 | changed. That in turn calls this handler, which makes us look for new |
| 321 | code again. To avoid being re-entered, we check this flag. */ |
| 322 | if (registering_code) |
| 323 | return; |
| 324 | |
| 325 | /* Lookup the registration symbol. If it is missing, then we assume we are |
| 326 | not attached to a JIT. */ |
| 327 | reg_symbol = lookup_minimal_symbol (jit_break_name, NULL, NULL); |
| 328 | if (reg_symbol == NULL) |
| 329 | return; |
| 330 | reg_addr = SYMBOL_VALUE_ADDRESS (reg_symbol); |
| 331 | if (reg_addr == 0) |
| 332 | return; |
| 333 | |
| 334 | /* Lookup the descriptor symbol and cache the addr. If it is missing, we |
| 335 | assume we are not attached to a JIT and return early. */ |
| 336 | desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, NULL); |
| 337 | if (desc_symbol == NULL) |
| 338 | return; |
| 339 | jit_descriptor_addr = SYMBOL_VALUE_ADDRESS (desc_symbol); |
| 340 | if (jit_descriptor_addr == 0) |
| 341 | return; |
| 342 | |
| 343 | /* Read the descriptor so we can check the version number and load any already |
| 344 | JITed functions. */ |
| 345 | jit_read_descriptor (gdbarch, &descriptor); |
| 346 | |
| 347 | /* Check that the version number agrees with that we support. */ |
| 348 | if (descriptor.version != 1) |
| 349 | error (_("Unsupported JIT protocol version in descriptor!")); |
| 350 | |
| 351 | /* Put a breakpoint in the registration symbol. */ |
| 352 | create_jit_event_breakpoint (gdbarch, reg_addr); |
| 353 | |
| 354 | /* If we've attached to a running program, we need to check the descriptor to |
| 355 | register any functions that were already generated. */ |
| 356 | for (cur_entry_addr = descriptor.first_entry; |
| 357 | cur_entry_addr != 0; |
| 358 | cur_entry_addr = cur_entry.next_entry) |
| 359 | { |
| 360 | jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry); |
| 361 | |
| 362 | /* This hook may be called many times during setup, so make sure we don't |
| 363 | add the same symbol file twice. */ |
| 364 | if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL) |
| 365 | continue; |
| 366 | |
| 367 | jit_register_code (gdbarch, cur_entry_addr, &cur_entry); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* Exported routine to call when an inferior has been created. */ |
| 372 | |
| 373 | void |
| 374 | jit_inferior_created_hook (void) |
| 375 | { |
| 376 | jit_inferior_init (target_gdbarch); |
| 377 | } |
| 378 | |
| 379 | /* Exported routine to call to re-set the jit breakpoints, |
| 380 | e.g. when a program is rerun. */ |
| 381 | |
| 382 | void |
| 383 | jit_breakpoint_re_set (void) |
| 384 | { |
| 385 | jit_inferior_init (target_gdbarch); |
| 386 | } |
| 387 | |
| 388 | /* Wrapper to match the observer function pointer prototype. */ |
| 389 | |
| 390 | static void |
| 391 | jit_inferior_created_observer (struct target_ops *objfile, int from_tty) |
| 392 | { |
| 393 | jit_inferior_init (target_gdbarch); |
| 394 | } |
| 395 | |
| 396 | /* This function cleans up any code entries left over when the inferior exits. |
| 397 | We get left over code when the inferior exits without unregistering its code, |
| 398 | for example when it crashes. */ |
| 399 | |
| 400 | static void |
| 401 | jit_inferior_exit_hook (int pid) |
| 402 | { |
| 403 | struct objfile *objf; |
| 404 | struct objfile *temp; |
| 405 | |
| 406 | /* We need to reset the descriptor addr so that next time we load up the |
| 407 | inferior we look for it again. */ |
| 408 | jit_descriptor_addr = 0; |
| 409 | |
| 410 | ALL_OBJFILES_SAFE (objf, temp) |
| 411 | if (objfile_data (objf, jit_objfile_data) != NULL) |
| 412 | jit_unregister_code (objf); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | jit_event_handler (struct gdbarch *gdbarch) |
| 417 | { |
| 418 | struct jit_descriptor descriptor; |
| 419 | struct jit_code_entry code_entry; |
| 420 | CORE_ADDR entry_addr; |
| 421 | struct objfile *objf; |
| 422 | |
| 423 | /* Read the descriptor from remote memory. */ |
| 424 | jit_read_descriptor (gdbarch, &descriptor); |
| 425 | entry_addr = descriptor.relevant_entry; |
| 426 | |
| 427 | /* Do the corresponding action. */ |
| 428 | switch (descriptor.action_flag) |
| 429 | { |
| 430 | case JIT_NOACTION: |
| 431 | break; |
| 432 | case JIT_REGISTER: |
| 433 | jit_read_code_entry (gdbarch, entry_addr, &code_entry); |
| 434 | jit_register_code (gdbarch, entry_addr, &code_entry); |
| 435 | break; |
| 436 | case JIT_UNREGISTER: |
| 437 | objf = jit_find_objf_with_entry_addr (entry_addr); |
| 438 | if (objf == NULL) |
| 439 | printf_unfiltered (_("Unable to find JITed code entry at address: %s\n"), |
| 440 | paddress (gdbarch, entry_addr)); |
| 441 | else |
| 442 | jit_unregister_code (objf); |
| 443 | |
| 444 | break; |
| 445 | default: |
| 446 | error (_("Unknown action_flag value in JIT descriptor!")); |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
| 452 | |
| 453 | extern void _initialize_jit (void); |
| 454 | |
| 455 | void |
| 456 | _initialize_jit (void) |
| 457 | { |
| 458 | observer_attach_inferior_created (jit_inferior_created_observer); |
| 459 | observer_attach_inferior_exit (jit_inferior_exit_hook); |
| 460 | jit_objfile_data = register_objfile_data (); |
| 461 | } |