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