]>
Commit | Line | Data |
---|---|---|
4efc6507 DE |
1 | /* Handle JIT code generation in the inferior for GDB, the GNU Debugger. |
2 | ||
28e7fd62 | 3 | Copyright (C) 2009-2013 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" |
1825a88d | 24 | #include "block.h" |
4efc6507 | 25 | #include "breakpoint.h" |
a255712f | 26 | #include "command.h" |
1825a88d | 27 | #include "dictionary.h" |
c9fb1240 | 28 | #include "filenames.h" |
1825a88d | 29 | #include "frame-unwind.h" |
a255712f | 30 | #include "gdbcmd.h" |
4efc6507 | 31 | #include "gdbcore.h" |
03673fc7 | 32 | #include "inferior.h" |
4efc6507 DE |
33 | #include "observer.h" |
34 | #include "objfiles.h" | |
3623dc3a | 35 | #include "regcache.h" |
4efc6507 DE |
36 | #include "symfile.h" |
37 | #include "symtab.h" | |
38 | #include "target.h" | |
784c47ee | 39 | #include "gdb-dlfcn.h" |
4efc6507 | 40 | #include "gdb_stat.h" |
1825a88d | 41 | #include "exceptions.h" |
cbb099e8 | 42 | #include "gdb_bfd.h" |
4efc6507 | 43 | |
b8e0a31c SD |
44 | static const char *jit_reader_dir = NULL; |
45 | ||
4efc6507 DE |
46 | static const struct objfile_data *jit_objfile_data; |
47 | ||
48 | static const char *const jit_break_name = "__jit_debug_register_code"; | |
49 | ||
50 | static const char *const jit_descriptor_name = "__jit_debug_descriptor"; | |
51 | ||
03673fc7 | 52 | static const struct inferior_data *jit_inferior_data = NULL; |
4efc6507 | 53 | |
e2bd3b15 | 54 | static void jit_inferior_init (struct gdbarch *gdbarch); |
3b2a0cf2 | 55 | |
3623dc3a SD |
56 | /* An unwinder is registered for every gdbarch. This key is used to |
57 | remember if the unwinder has been registered for a particular | |
58 | gdbarch. */ | |
59 | ||
60 | static struct gdbarch_data *jit_gdbarch_data; | |
61 | ||
a255712f PP |
62 | /* Non-zero if we want to see trace of jit level stuff. */ |
63 | ||
ccce17b0 | 64 | static unsigned int jit_debug = 0; |
a255712f PP |
65 | |
66 | static void | |
67 | show_jit_debug (struct ui_file *file, int from_tty, | |
68 | struct cmd_list_element *c, const char *value) | |
69 | { | |
70 | fprintf_filtered (file, _("JIT debugging is %s.\n"), value); | |
71 | } | |
72 | ||
4efc6507 DE |
73 | struct target_buffer |
74 | { | |
75 | CORE_ADDR base; | |
a255712f | 76 | ULONGEST size; |
4efc6507 DE |
77 | }; |
78 | ||
79 | /* Openning the file is a no-op. */ | |
80 | ||
81 | static void * | |
82 | mem_bfd_iovec_open (struct bfd *abfd, void *open_closure) | |
83 | { | |
84 | return open_closure; | |
85 | } | |
86 | ||
87 | /* Closing the file is just freeing the base/size pair on our side. */ | |
88 | ||
89 | static int | |
90 | mem_bfd_iovec_close (struct bfd *abfd, void *stream) | |
91 | { | |
92 | xfree (stream); | |
93 | return 1; | |
94 | } | |
95 | ||
96 | /* For reading the file, we just need to pass through to target_read_memory and | |
97 | fix up the arguments and return values. */ | |
98 | ||
99 | static file_ptr | |
100 | mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf, | |
101 | file_ptr nbytes, file_ptr offset) | |
102 | { | |
103 | int err; | |
104 | struct target_buffer *buffer = (struct target_buffer *) stream; | |
105 | ||
106 | /* If this read will read all of the file, limit it to just the rest. */ | |
107 | if (offset + nbytes > buffer->size) | |
108 | nbytes = buffer->size - offset; | |
109 | ||
110 | /* If there are no more bytes left, we've reached EOF. */ | |
111 | if (nbytes == 0) | |
112 | return 0; | |
113 | ||
114 | err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes); | |
115 | if (err) | |
116 | return -1; | |
117 | ||
118 | return nbytes; | |
119 | } | |
120 | ||
121 | /* For statting the file, we only support the st_size attribute. */ | |
122 | ||
123 | static int | |
124 | mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb) | |
125 | { | |
126 | struct target_buffer *buffer = (struct target_buffer*) stream; | |
127 | ||
128 | sb->st_size = buffer->size; | |
129 | return 0; | |
130 | } | |
131 | ||
f0bbc364 TT |
132 | /* Open a BFD from the target's memory. */ |
133 | ||
134 | static struct bfd * | |
135 | bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target) | |
136 | { | |
f0bbc364 TT |
137 | struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer)); |
138 | ||
139 | buffer->base = addr; | |
140 | buffer->size = size; | |
64c31149 TT |
141 | return gdb_bfd_openr_iovec ("<in-memory>", target, |
142 | mem_bfd_iovec_open, | |
143 | buffer, | |
144 | mem_bfd_iovec_pread, | |
145 | mem_bfd_iovec_close, | |
146 | mem_bfd_iovec_stat); | |
f0bbc364 TT |
147 | } |
148 | ||
784c47ee SD |
149 | /* One reader that has been loaded successfully, and can potentially be used to |
150 | parse debug info. */ | |
151 | ||
152 | static struct jit_reader | |
153 | { | |
154 | struct gdb_reader_funcs *functions; | |
155 | void *handle; | |
156 | } *loaded_jit_reader = NULL; | |
157 | ||
158 | typedef struct gdb_reader_funcs * (reader_init_fn_type) (void); | |
159 | static const char *reader_init_fn_sym = "gdb_init_reader"; | |
160 | ||
161 | /* Try to load FILE_NAME as a JIT debug info reader. */ | |
162 | ||
163 | static struct jit_reader * | |
164 | jit_reader_load (const char *file_name) | |
165 | { | |
166 | void *so; | |
167 | reader_init_fn_type *init_fn; | |
168 | struct jit_reader *new_reader = NULL; | |
169 | struct gdb_reader_funcs *funcs = NULL; | |
170 | struct cleanup *old_cleanups; | |
171 | ||
172 | if (jit_debug) | |
173 | fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"), | |
174 | file_name); | |
175 | so = gdb_dlopen (file_name); | |
176 | old_cleanups = make_cleanup_dlclose (so); | |
177 | ||
178 | init_fn = gdb_dlsym (so, reader_init_fn_sym); | |
179 | if (!init_fn) | |
180 | error (_("Could not locate initialization function: %s."), | |
181 | reader_init_fn_sym); | |
182 | ||
183 | if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL) | |
184 | error (_("Reader not GPL compatible.")); | |
185 | ||
186 | funcs = init_fn (); | |
187 | if (funcs->reader_version != GDB_READER_INTERFACE_VERSION) | |
188 | error (_("Reader version does not match GDB version.")); | |
189 | ||
190 | new_reader = XZALLOC (struct jit_reader); | |
191 | new_reader->functions = funcs; | |
192 | new_reader->handle = so; | |
193 | ||
194 | discard_cleanups (old_cleanups); | |
195 | return new_reader; | |
196 | } | |
197 | ||
198 | /* Provides the jit-reader-load command. */ | |
199 | ||
200 | static void | |
201 | jit_reader_load_command (char *args, int from_tty) | |
202 | { | |
203 | char *so_name; | |
784c47ee SD |
204 | struct cleanup *prev_cleanup; |
205 | ||
206 | if (args == NULL) | |
207 | error (_("No reader name provided.")); | |
208 | ||
209 | if (loaded_jit_reader != NULL) | |
210 | error (_("JIT reader already loaded. Run jit-reader-unload first.")); | |
211 | ||
c9fb1240 SD |
212 | if (IS_ABSOLUTE_PATH (args)) |
213 | so_name = xstrdup (args); | |
214 | else | |
215 | so_name = xstrprintf ("%s%s%s", SLASH_STRING, jit_reader_dir, args); | |
784c47ee SD |
216 | prev_cleanup = make_cleanup (xfree, so_name); |
217 | ||
218 | loaded_jit_reader = jit_reader_load (so_name); | |
219 | do_cleanups (prev_cleanup); | |
220 | } | |
221 | ||
222 | /* Provides the jit-reader-unload command. */ | |
223 | ||
224 | static void | |
225 | jit_reader_unload_command (char *args, int from_tty) | |
226 | { | |
227 | if (!loaded_jit_reader) | |
228 | error (_("No JIT reader loaded.")); | |
229 | ||
230 | loaded_jit_reader->functions->destroy (loaded_jit_reader->functions); | |
231 | ||
232 | gdb_dlclose (loaded_jit_reader->handle); | |
233 | xfree (loaded_jit_reader); | |
234 | loaded_jit_reader = NULL; | |
235 | } | |
236 | ||
03bef283 TT |
237 | /* Per-inferior structure recording which objfile has the JIT |
238 | symbols. */ | |
03673fc7 PP |
239 | |
240 | struct jit_inferior_data | |
241 | { | |
03bef283 TT |
242 | /* The objfile. This is NULL if no objfile holds the JIT |
243 | symbols. */ | |
244 | ||
245 | struct objfile *objfile; | |
246 | }; | |
247 | ||
248 | /* Per-objfile structure recording the addresses in the inferior. */ | |
249 | ||
250 | struct jit_objfile_data | |
251 | { | |
252 | /* Symbol for __jit_debug_register_code. */ | |
253 | struct minimal_symbol *register_code; | |
254 | ||
255 | /* Symbol for __jit_debug_descriptor. */ | |
256 | struct minimal_symbol *descriptor; | |
257 | ||
258 | /* Address of struct jit_code_entry in this objfile. */ | |
259 | CORE_ADDR addr; | |
03673fc7 PP |
260 | }; |
261 | ||
03bef283 TT |
262 | /* Fetch the jit_objfile_data associated with OBJF. If no data exists |
263 | yet, make a new structure and attach it. */ | |
264 | ||
265 | static struct jit_objfile_data * | |
266 | get_jit_objfile_data (struct objfile *objf) | |
267 | { | |
268 | struct jit_objfile_data *objf_data; | |
269 | ||
270 | objf_data = objfile_data (objf, jit_objfile_data); | |
271 | if (objf_data == NULL) | |
272 | { | |
273 | objf_data = XZALLOC (struct jit_objfile_data); | |
274 | set_objfile_data (objf, jit_objfile_data, objf_data); | |
275 | } | |
276 | ||
277 | return objf_data; | |
278 | } | |
279 | ||
b4264740 SD |
280 | /* Remember OBJFILE has been created for struct jit_code_entry located |
281 | at inferior address ENTRY. */ | |
1825a88d SD |
282 | |
283 | static void | |
284 | add_objfile_entry (struct objfile *objfile, CORE_ADDR entry) | |
285 | { | |
03bef283 | 286 | struct jit_objfile_data *objf_data; |
1825a88d | 287 | |
03bef283 TT |
288 | objf_data = get_jit_objfile_data (objfile); |
289 | objf_data->addr = entry; | |
1825a88d SD |
290 | } |
291 | ||
03673fc7 PP |
292 | /* Return jit_inferior_data for current inferior. Allocate if not already |
293 | present. */ | |
294 | ||
295 | static struct jit_inferior_data * | |
296 | get_jit_inferior_data (void) | |
297 | { | |
298 | struct inferior *inf; | |
299 | struct jit_inferior_data *inf_data; | |
300 | ||
301 | inf = current_inferior (); | |
302 | inf_data = inferior_data (inf, jit_inferior_data); | |
303 | if (inf_data == NULL) | |
304 | { | |
305 | inf_data = XZALLOC (struct jit_inferior_data); | |
306 | set_inferior_data (inf, jit_inferior_data, inf_data); | |
307 | } | |
308 | ||
309 | return inf_data; | |
310 | } | |
311 | ||
312 | static void | |
313 | jit_inferior_data_cleanup (struct inferior *inf, void *arg) | |
314 | { | |
315 | xfree (arg); | |
316 | } | |
317 | ||
1777feb0 | 318 | /* Helper function for reading the global JIT descriptor from remote |
03bef283 | 319 | memory. Returns 1 if all went well, 0 otherwise. */ |
4efc6507 | 320 | |
03bef283 | 321 | static int |
0756c555 | 322 | jit_read_descriptor (struct gdbarch *gdbarch, |
03673fc7 | 323 | struct jit_descriptor *descriptor, |
03bef283 | 324 | struct jit_inferior_data *inf_data) |
4efc6507 DE |
325 | { |
326 | int err; | |
327 | struct type *ptr_type; | |
328 | int ptr_size; | |
329 | int desc_size; | |
330 | gdb_byte *desc_buf; | |
0756c555 | 331 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
03bef283 TT |
332 | struct jit_objfile_data *objf_data; |
333 | ||
334 | if (inf_data->objfile == NULL) | |
335 | return 0; | |
336 | objf_data = get_jit_objfile_data (inf_data->objfile); | |
337 | if (objf_data->descriptor == NULL) | |
338 | return 0; | |
339 | ||
340 | if (jit_debug) | |
341 | fprintf_unfiltered (gdb_stdlog, | |
342 | "jit_read_descriptor, descriptor_addr = %s\n", | |
343 | paddress (gdbarch, SYMBOL_VALUE_ADDRESS (objf_data->descriptor))); | |
4efc6507 DE |
344 | |
345 | /* Figure out how big the descriptor is on the remote and how to read it. */ | |
0756c555 | 346 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
4efc6507 DE |
347 | ptr_size = TYPE_LENGTH (ptr_type); |
348 | desc_size = 8 + 2 * ptr_size; /* Two 32-bit ints and two pointers. */ | |
349 | desc_buf = alloca (desc_size); | |
350 | ||
351 | /* Read the descriptor. */ | |
03bef283 TT |
352 | err = target_read_memory (SYMBOL_VALUE_ADDRESS (objf_data->descriptor), |
353 | desc_buf, desc_size); | |
4efc6507 | 354 | if (err) |
03bef283 TT |
355 | { |
356 | printf_unfiltered (_("Unable to read JIT descriptor from " | |
357 | "remote memory\n")); | |
358 | return 0; | |
359 | } | |
4efc6507 DE |
360 | |
361 | /* Fix the endianness to match the host. */ | |
362 | descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order); | |
363 | descriptor->action_flag = | |
364 | extract_unsigned_integer (&desc_buf[4], 4, byte_order); | |
365 | descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type); | |
366 | descriptor->first_entry = | |
367 | extract_typed_address (&desc_buf[8 + ptr_size], ptr_type); | |
03bef283 TT |
368 | |
369 | return 1; | |
4efc6507 DE |
370 | } |
371 | ||
372 | /* Helper function for reading a JITed code entry from remote memory. */ | |
373 | ||
374 | static void | |
0756c555 DE |
375 | jit_read_code_entry (struct gdbarch *gdbarch, |
376 | CORE_ADDR code_addr, struct jit_code_entry *code_entry) | |
4efc6507 | 377 | { |
205c306f | 378 | int err, off; |
4efc6507 DE |
379 | struct type *ptr_type; |
380 | int ptr_size; | |
381 | int entry_size; | |
205c306f | 382 | int align_bytes; |
4efc6507 | 383 | gdb_byte *entry_buf; |
0756c555 | 384 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
4efc6507 DE |
385 | |
386 | /* Figure out how big the entry is on the remote and how to read it. */ | |
0756c555 | 387 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
4efc6507 | 388 | ptr_size = TYPE_LENGTH (ptr_type); |
227ee7fc RH |
389 | |
390 | /* Figure out where the longlong value will be. */ | |
391 | align_bytes = gdbarch_long_long_align_bit (gdbarch) / 8; | |
392 | off = 3 * ptr_size; | |
393 | off = (off + (align_bytes - 1)) & ~(align_bytes - 1); | |
394 | ||
395 | entry_size = off + 8; /* Three pointers and one 64-bit int. */ | |
4efc6507 DE |
396 | entry_buf = alloca (entry_size); |
397 | ||
398 | /* Read the entry. */ | |
399 | err = target_read_memory (code_addr, entry_buf, entry_size); | |
400 | if (err) | |
401 | error (_("Unable to read JIT code entry from remote memory!")); | |
402 | ||
403 | /* Fix the endianness to match the host. */ | |
0756c555 | 404 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
4efc6507 DE |
405 | code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type); |
406 | code_entry->prev_entry = | |
407 | extract_typed_address (&entry_buf[ptr_size], ptr_type); | |
408 | code_entry->symfile_addr = | |
409 | extract_typed_address (&entry_buf[2 * ptr_size], ptr_type); | |
410 | code_entry->symfile_size = | |
205c306f | 411 | extract_unsigned_integer (&entry_buf[off], 8, byte_order); |
4efc6507 DE |
412 | } |
413 | ||
1825a88d SD |
414 | /* Proxy object for building a block. */ |
415 | ||
416 | struct gdb_block | |
417 | { | |
418 | /* gdb_blocks are linked into a tree structure. Next points to the | |
419 | next node at the same depth as this block and parent to the | |
420 | parent gdb_block. */ | |
421 | struct gdb_block *next, *parent; | |
422 | ||
423 | /* Points to the "real" block that is being built out of this | |
424 | instance. This block will be added to a blockvector, which will | |
425 | then be added to a symtab. */ | |
426 | struct block *real_block; | |
427 | ||
428 | /* The first and last code address corresponding to this block. */ | |
429 | CORE_ADDR begin, end; | |
430 | ||
431 | /* The name of this block (if any). If this is non-NULL, the | |
432 | FUNCTION symbol symbol is set to this value. */ | |
433 | const char *name; | |
434 | }; | |
435 | ||
436 | /* Proxy object for building a symtab. */ | |
437 | ||
438 | struct gdb_symtab | |
439 | { | |
440 | /* The list of blocks in this symtab. These will eventually be | |
441 | converted to real blocks. */ | |
442 | struct gdb_block *blocks; | |
443 | ||
444 | /* The number of blocks inserted. */ | |
445 | int nblocks; | |
446 | ||
447 | /* A mapping between line numbers to PC. */ | |
448 | struct linetable *linetable; | |
449 | ||
450 | /* The source file for this symtab. */ | |
451 | const char *file_name; | |
452 | struct gdb_symtab *next; | |
453 | }; | |
454 | ||
455 | /* Proxy object for building an object. */ | |
456 | ||
457 | struct gdb_object | |
458 | { | |
459 | struct gdb_symtab *symtabs; | |
460 | }; | |
461 | ||
462 | /* The type of the `private' data passed around by the callback | |
463 | functions. */ | |
464 | ||
465 | typedef CORE_ADDR jit_dbg_reader_data; | |
466 | ||
467 | /* The reader calls into this function to read data off the targets | |
468 | address space. */ | |
469 | ||
470 | static enum gdb_status | |
471 | jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len) | |
472 | { | |
473 | int result = target_read_memory ((CORE_ADDR) target_mem, gdb_buf, len); | |
474 | if (result == 0) | |
475 | return GDB_SUCCESS; | |
476 | else | |
477 | return GDB_FAIL; | |
478 | } | |
479 | ||
480 | /* The reader calls into this function to create a new gdb_object | |
481 | which it can then pass around to the other callbacks. Right now, | |
482 | all that is required is allocating the memory. */ | |
483 | ||
484 | static struct gdb_object * | |
485 | jit_object_open_impl (struct gdb_symbol_callbacks *cb) | |
486 | { | |
487 | /* CB is not required right now, but sometime in the future we might | |
488 | need a handle to it, and we'd like to do that without breaking | |
489 | the ABI. */ | |
490 | return XZALLOC (struct gdb_object); | |
491 | } | |
492 | ||
493 | /* Readers call into this function to open a new gdb_symtab, which, | |
494 | again, is passed around to other callbacks. */ | |
495 | ||
496 | static struct gdb_symtab * | |
497 | jit_symtab_open_impl (struct gdb_symbol_callbacks *cb, | |
498 | struct gdb_object *object, | |
499 | const char *file_name) | |
500 | { | |
501 | struct gdb_symtab *ret; | |
502 | ||
503 | /* CB stays unused. See comment in jit_object_open_impl. */ | |
504 | ||
505 | ret = XZALLOC (struct gdb_symtab); | |
506 | ret->file_name = file_name ? xstrdup (file_name) : xstrdup (""); | |
507 | ret->next = object->symtabs; | |
508 | object->symtabs = ret; | |
509 | return ret; | |
510 | } | |
511 | ||
512 | /* Returns true if the block corresponding to old should be placed | |
513 | before the block corresponding to new in the final blockvector. */ | |
514 | ||
515 | static int | |
516 | compare_block (const struct gdb_block *const old, | |
517 | const struct gdb_block *const new) | |
518 | { | |
519 | if (old == NULL) | |
520 | return 1; | |
521 | if (old->begin < new->begin) | |
522 | return 1; | |
523 | else if (old->begin == new->begin) | |
524 | { | |
525 | if (old->end > new->end) | |
526 | return 1; | |
527 | else | |
528 | return 0; | |
529 | } | |
530 | else | |
531 | return 0; | |
532 | } | |
533 | ||
534 | /* Called by readers to open a new gdb_block. This function also | |
535 | inserts the new gdb_block in the correct place in the corresponding | |
536 | gdb_symtab. */ | |
537 | ||
538 | static struct gdb_block * | |
539 | jit_block_open_impl (struct gdb_symbol_callbacks *cb, | |
540 | struct gdb_symtab *symtab, struct gdb_block *parent, | |
541 | GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name) | |
542 | { | |
543 | struct gdb_block *block = XZALLOC (struct gdb_block); | |
544 | ||
545 | block->next = symtab->blocks; | |
546 | block->begin = (CORE_ADDR) begin; | |
547 | block->end = (CORE_ADDR) end; | |
548 | block->name = name ? xstrdup (name) : NULL; | |
549 | block->parent = parent; | |
550 | ||
551 | /* Ensure that the blocks are inserted in the correct (reverse of | |
552 | the order expected by blockvector). */ | |
553 | if (compare_block (symtab->blocks, block)) | |
554 | { | |
555 | symtab->blocks = block; | |
556 | } | |
557 | else | |
558 | { | |
559 | struct gdb_block *i = symtab->blocks; | |
560 | ||
561 | for (;; i = i->next) | |
562 | { | |
563 | /* Guaranteed to terminate, since compare_block (NULL, _) | |
564 | returns 1. */ | |
565 | if (compare_block (i->next, block)) | |
566 | { | |
567 | block->next = i->next; | |
568 | i->next = block; | |
569 | break; | |
570 | } | |
571 | } | |
572 | } | |
573 | symtab->nblocks++; | |
574 | ||
575 | return block; | |
576 | } | |
577 | ||
578 | /* Readers call this to add a line mapping (from PC to line number) to | |
579 | a gdb_symtab. */ | |
4efc6507 DE |
580 | |
581 | static void | |
1825a88d SD |
582 | jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb, |
583 | struct gdb_symtab *stab, int nlines, | |
584 | struct gdb_line_mapping *map) | |
585 | { | |
586 | int i; | |
587 | ||
588 | if (nlines < 1) | |
589 | return; | |
590 | ||
591 | stab->linetable = xmalloc (sizeof (struct linetable) | |
592 | + (nlines - 1) * sizeof (struct linetable_entry)); | |
593 | stab->linetable->nitems = nlines; | |
594 | for (i = 0; i < nlines; i++) | |
595 | { | |
596 | stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc; | |
597 | stab->linetable->item[i].line = map[i].line; | |
598 | } | |
599 | } | |
600 | ||
601 | /* Called by readers to close a gdb_symtab. Does not need to do | |
602 | anything as of now. */ | |
603 | ||
604 | static void | |
605 | jit_symtab_close_impl (struct gdb_symbol_callbacks *cb, | |
606 | struct gdb_symtab *stab) | |
607 | { | |
608 | /* Right now nothing needs to be done here. We may need to do some | |
609 | cleanup here in the future (again, without breaking the plugin | |
610 | ABI). */ | |
611 | } | |
612 | ||
613 | /* Transform STAB to a proper symtab, and add it it OBJFILE. */ | |
614 | ||
615 | static void | |
616 | finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile) | |
617 | { | |
618 | struct symtab *symtab; | |
619 | struct gdb_block *gdb_block_iter, *gdb_block_iter_tmp; | |
620 | struct block *block_iter; | |
621 | int actual_nblocks, i, blockvector_size; | |
622 | CORE_ADDR begin, end; | |
623 | ||
624 | actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks; | |
625 | ||
626 | symtab = allocate_symtab (stab->file_name, objfile); | |
627 | /* JIT compilers compile in memory. */ | |
628 | symtab->dirname = NULL; | |
629 | ||
630 | /* Copy over the linetable entry if one was provided. */ | |
631 | if (stab->linetable) | |
632 | { | |
633 | int size = ((stab->linetable->nitems - 1) | |
634 | * sizeof (struct linetable_entry) | |
635 | + sizeof (struct linetable)); | |
636 | LINETABLE (symtab) = obstack_alloc (&objfile->objfile_obstack, size); | |
637 | memcpy (LINETABLE (symtab), stab->linetable, size); | |
638 | } | |
639 | else | |
640 | { | |
641 | LINETABLE (symtab) = NULL; | |
642 | } | |
643 | ||
644 | blockvector_size = (sizeof (struct blockvector) | |
645 | + (actual_nblocks - 1) * sizeof (struct block *)); | |
646 | symtab->blockvector = obstack_alloc (&objfile->objfile_obstack, | |
647 | blockvector_size); | |
648 | ||
649 | /* (begin, end) will contain the PC range this entire blockvector | |
650 | spans. */ | |
651 | symtab->primary = 1; | |
652 | BLOCKVECTOR_MAP (symtab->blockvector) = NULL; | |
653 | begin = stab->blocks->begin; | |
654 | end = stab->blocks->end; | |
655 | BLOCKVECTOR_NBLOCKS (symtab->blockvector) = actual_nblocks; | |
656 | ||
657 | /* First run over all the gdb_block objects, creating a real block | |
658 | object for each. Simultaneously, keep setting the real_block | |
659 | fields. */ | |
660 | for (i = (actual_nblocks - 1), gdb_block_iter = stab->blocks; | |
661 | i >= FIRST_LOCAL_BLOCK; | |
662 | i--, gdb_block_iter = gdb_block_iter->next) | |
663 | { | |
664 | struct block *new_block = allocate_block (&objfile->objfile_obstack); | |
665 | struct symbol *block_name = obstack_alloc (&objfile->objfile_obstack, | |
666 | sizeof (struct symbol)); | |
2535757a TT |
667 | struct type *block_type = arch_type (get_objfile_arch (objfile), |
668 | TYPE_CODE_VOID, | |
669 | 1, | |
670 | "void"); | |
1825a88d SD |
671 | |
672 | BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack, | |
673 | NULL); | |
674 | /* The address range. */ | |
675 | BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter->begin; | |
676 | BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter->end; | |
677 | ||
678 | /* The name. */ | |
679 | memset (block_name, 0, sizeof (struct symbol)); | |
680 | SYMBOL_DOMAIN (block_name) = VAR_DOMAIN; | |
681 | SYMBOL_CLASS (block_name) = LOC_BLOCK; | |
682 | SYMBOL_SYMTAB (block_name) = symtab; | |
2535757a | 683 | SYMBOL_TYPE (block_name) = lookup_function_type (block_type); |
1825a88d SD |
684 | SYMBOL_BLOCK_VALUE (block_name) = new_block; |
685 | ||
10f0c4bb TT |
686 | block_name->ginfo.name = obstack_copy0 (&objfile->objfile_obstack, |
687 | gdb_block_iter->name, | |
688 | strlen (gdb_block_iter->name)); | |
1825a88d SD |
689 | |
690 | BLOCK_FUNCTION (new_block) = block_name; | |
691 | ||
692 | BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block; | |
693 | if (begin > BLOCK_START (new_block)) | |
694 | begin = BLOCK_START (new_block); | |
695 | if (end < BLOCK_END (new_block)) | |
696 | end = BLOCK_END (new_block); | |
697 | ||
698 | gdb_block_iter->real_block = new_block; | |
699 | } | |
700 | ||
701 | /* Now add the special blocks. */ | |
702 | block_iter = NULL; | |
703 | for (i = 0; i < FIRST_LOCAL_BLOCK; i++) | |
704 | { | |
84a146c9 TT |
705 | struct block *new_block; |
706 | ||
707 | new_block = (i == GLOBAL_BLOCK | |
708 | ? allocate_global_block (&objfile->objfile_obstack) | |
709 | : allocate_block (&objfile->objfile_obstack)); | |
1825a88d SD |
710 | BLOCK_DICT (new_block) = dict_create_linear (&objfile->objfile_obstack, |
711 | NULL); | |
712 | BLOCK_SUPERBLOCK (new_block) = block_iter; | |
713 | block_iter = new_block; | |
714 | ||
715 | BLOCK_START (new_block) = (CORE_ADDR) begin; | |
716 | BLOCK_END (new_block) = (CORE_ADDR) end; | |
717 | ||
718 | BLOCKVECTOR_BLOCK (symtab->blockvector, i) = new_block; | |
84a146c9 TT |
719 | |
720 | if (i == GLOBAL_BLOCK) | |
721 | set_block_symtab (new_block, symtab); | |
1825a88d SD |
722 | } |
723 | ||
724 | /* Fill up the superblock fields for the real blocks, using the | |
725 | real_block fields populated earlier. */ | |
726 | for (gdb_block_iter = stab->blocks; | |
727 | gdb_block_iter; | |
728 | gdb_block_iter = gdb_block_iter->next) | |
729 | { | |
730 | if (gdb_block_iter->parent != NULL) | |
db334a01 SD |
731 | { |
732 | /* If the plugin specifically mentioned a parent block, we | |
733 | use that. */ | |
734 | BLOCK_SUPERBLOCK (gdb_block_iter->real_block) = | |
735 | gdb_block_iter->parent->real_block; | |
736 | } | |
737 | else | |
738 | { | |
739 | /* And if not, we set a default parent block. */ | |
740 | BLOCK_SUPERBLOCK (gdb_block_iter->real_block) = | |
741 | BLOCKVECTOR_BLOCK (symtab->blockvector, STATIC_BLOCK); | |
742 | } | |
1825a88d SD |
743 | } |
744 | ||
745 | /* Free memory. */ | |
746 | gdb_block_iter = stab->blocks; | |
747 | ||
748 | for (gdb_block_iter = stab->blocks, gdb_block_iter_tmp = gdb_block_iter->next; | |
749 | gdb_block_iter; | |
750 | gdb_block_iter = gdb_block_iter_tmp) | |
751 | { | |
752 | xfree ((void *) gdb_block_iter->name); | |
753 | xfree (gdb_block_iter); | |
754 | } | |
755 | xfree (stab->linetable); | |
756 | xfree ((char *) stab->file_name); | |
757 | xfree (stab); | |
758 | } | |
759 | ||
760 | /* Called when closing a gdb_objfile. Converts OBJ to a proper | |
761 | objfile. */ | |
762 | ||
763 | static void | |
764 | jit_object_close_impl (struct gdb_symbol_callbacks *cb, | |
765 | struct gdb_object *obj) | |
766 | { | |
767 | struct gdb_symtab *i, *j; | |
768 | struct objfile *objfile; | |
769 | jit_dbg_reader_data *priv_data; | |
770 | ||
771 | priv_data = cb->priv_data; | |
772 | ||
773 | objfile = allocate_objfile (NULL, 0); | |
f5656ead | 774 | objfile->gdbarch = target_gdbarch (); |
1825a88d | 775 | |
a5bd37c3 | 776 | terminate_minimal_symbol_table (objfile); |
1825a88d | 777 | |
e1507e95 | 778 | objfile->name = "<< JIT compiled code >>"; |
1825a88d SD |
779 | |
780 | j = NULL; | |
781 | for (i = obj->symtabs; i; i = j) | |
782 | { | |
783 | j = i->next; | |
784 | finalize_symtab (i, objfile); | |
785 | } | |
786 | add_objfile_entry (objfile, *priv_data); | |
787 | xfree (obj); | |
788 | } | |
789 | ||
744ab88c | 790 | /* Try to read CODE_ENTRY using the loaded jit reader (if any). |
b4264740 SD |
791 | ENTRY_ADDR is the address of the struct jit_code_entry in the |
792 | inferior address space. */ | |
1825a88d SD |
793 | |
794 | static int | |
744ab88c SD |
795 | jit_reader_try_read_symtab (struct jit_code_entry *code_entry, |
796 | CORE_ADDR entry_addr) | |
1825a88d SD |
797 | { |
798 | void *gdb_mem; | |
799 | int status; | |
1825a88d SD |
800 | jit_dbg_reader_data priv_data; |
801 | struct gdb_reader_funcs *funcs; | |
802 | volatile struct gdb_exception e; | |
803 | struct gdb_symbol_callbacks callbacks = | |
804 | { | |
805 | jit_object_open_impl, | |
806 | jit_symtab_open_impl, | |
807 | jit_block_open_impl, | |
808 | jit_symtab_close_impl, | |
809 | jit_object_close_impl, | |
810 | ||
811 | jit_symtab_line_mapping_add_impl, | |
812 | jit_target_read_impl, | |
813 | ||
814 | &priv_data | |
815 | }; | |
816 | ||
744ab88c | 817 | priv_data = entry_addr; |
1825a88d SD |
818 | |
819 | if (!loaded_jit_reader) | |
820 | return 0; | |
821 | ||
822 | gdb_mem = xmalloc (code_entry->symfile_size); | |
823 | ||
824 | status = 1; | |
825 | TRY_CATCH (e, RETURN_MASK_ALL) | |
826 | if (target_read_memory (code_entry->symfile_addr, gdb_mem, | |
827 | code_entry->symfile_size)) | |
828 | status = 0; | |
829 | if (e.reason < 0) | |
830 | status = 0; | |
831 | ||
832 | if (status) | |
833 | { | |
834 | funcs = loaded_jit_reader->functions; | |
835 | if (funcs->read (funcs, &callbacks, gdb_mem, code_entry->symfile_size) | |
836 | != GDB_SUCCESS) | |
837 | status = 0; | |
838 | } | |
839 | ||
840 | xfree (gdb_mem); | |
841 | if (jit_debug && status == 0) | |
842 | fprintf_unfiltered (gdb_stdlog, | |
843 | "Could not read symtab using the loaded JIT reader.\n"); | |
844 | return status; | |
845 | } | |
846 | ||
744ab88c | 847 | /* Try to read CODE_ENTRY using BFD. ENTRY_ADDR is the address of the |
b4264740 | 848 | struct jit_code_entry in the inferior address space. */ |
1825a88d SD |
849 | |
850 | static void | |
851 | jit_bfd_try_read_symtab (struct jit_code_entry *code_entry, | |
744ab88c | 852 | CORE_ADDR entry_addr, |
1825a88d | 853 | struct gdbarch *gdbarch) |
4efc6507 DE |
854 | { |
855 | bfd *nbfd; | |
856 | struct section_addr_info *sai; | |
857 | struct bfd_section *sec; | |
858 | struct objfile *objfile; | |
4dfb2365 | 859 | struct cleanup *old_cleanups; |
4efc6507 DE |
860 | int i; |
861 | const struct bfd_arch_info *b; | |
4efc6507 | 862 | |
a255712f PP |
863 | if (jit_debug) |
864 | fprintf_unfiltered (gdb_stdlog, | |
865 | "jit_register_code, symfile_addr = %s, " | |
866 | "symfile_size = %s\n", | |
867 | paddress (gdbarch, code_entry->symfile_addr), | |
868 | pulongest (code_entry->symfile_size)); | |
869 | ||
4efc6507 DE |
870 | nbfd = bfd_open_from_target_memory (code_entry->symfile_addr, |
871 | code_entry->symfile_size, gnutarget); | |
4dfb2365 JK |
872 | if (nbfd == NULL) |
873 | { | |
874 | puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n")); | |
875 | return; | |
876 | } | |
4efc6507 DE |
877 | |
878 | /* Check the format. NOTE: This initializes important data that GDB uses! | |
879 | We would segfault later without this line. */ | |
880 | if (!bfd_check_format (nbfd, bfd_object)) | |
881 | { | |
882 | printf_unfiltered (_("\ | |
883 | JITed symbol file is not an object file, ignoring it.\n")); | |
cbb099e8 | 884 | gdb_bfd_unref (nbfd); |
4efc6507 DE |
885 | return; |
886 | } | |
887 | ||
888 | /* Check bfd arch. */ | |
0756c555 | 889 | b = gdbarch_bfd_arch_info (gdbarch); |
4efc6507 DE |
890 | if (b->compatible (b, bfd_get_arch_info (nbfd)) != b) |
891 | warning (_("JITed object file architecture %s is not compatible " | |
892 | "with target architecture %s."), bfd_get_arch_info | |
893 | (nbfd)->printable_name, b->printable_name); | |
894 | ||
895 | /* Read the section address information out of the symbol file. Since the | |
896 | file is generated by the JIT at runtime, it should all of the absolute | |
897 | addresses that we care about. */ | |
898 | sai = alloc_section_addr_info (bfd_count_sections (nbfd)); | |
4dfb2365 | 899 | old_cleanups = make_cleanup_free_section_addr_info (sai); |
4efc6507 DE |
900 | i = 0; |
901 | for (sec = nbfd->sections; sec != NULL; sec = sec->next) | |
902 | if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0) | |
903 | { | |
904 | /* We assume that these virtual addresses are absolute, and do not | |
905 | treat them as offsets. */ | |
906 | sai->other[i].addr = bfd_get_section_vma (nbfd, sec); | |
04a679b8 | 907 | sai->other[i].name = xstrdup (bfd_get_section_name (nbfd, sec)); |
4efc6507 DE |
908 | sai->other[i].sectindex = sec->index; |
909 | ++i; | |
910 | } | |
911 | ||
8ac244b4 TT |
912 | /* This call does not take ownership of SAI. */ |
913 | make_cleanup_bfd_unref (nbfd); | |
63524580 | 914 | objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL); |
4efc6507 | 915 | |
4dfb2365 | 916 | do_cleanups (old_cleanups); |
744ab88c | 917 | add_objfile_entry (objfile, entry_addr); |
1825a88d SD |
918 | } |
919 | ||
920 | /* This function registers code associated with a JIT code entry. It uses the | |
921 | pointer and size pair in the entry to read the symbol file from the remote | |
922 | and then calls symbol_file_add_from_local_memory to add it as though it were | |
923 | a symbol file added by the user. */ | |
924 | ||
925 | static void | |
926 | jit_register_code (struct gdbarch *gdbarch, | |
927 | CORE_ADDR entry_addr, struct jit_code_entry *code_entry) | |
928 | { | |
974a734b | 929 | int success; |
1825a88d SD |
930 | |
931 | if (jit_debug) | |
932 | fprintf_unfiltered (gdb_stdlog, | |
933 | "jit_register_code, symfile_addr = %s, " | |
934 | "symfile_size = %s\n", | |
935 | paddress (gdbarch, code_entry->symfile_addr), | |
936 | pulongest (code_entry->symfile_size)); | |
937 | ||
744ab88c | 938 | success = jit_reader_try_read_symtab (code_entry, entry_addr); |
1825a88d SD |
939 | |
940 | if (!success) | |
744ab88c | 941 | jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch); |
4efc6507 DE |
942 | } |
943 | ||
1777feb0 MS |
944 | /* This function unregisters JITed code and frees the corresponding |
945 | objfile. */ | |
4efc6507 DE |
946 | |
947 | static void | |
948 | jit_unregister_code (struct objfile *objfile) | |
949 | { | |
950 | free_objfile (objfile); | |
951 | } | |
952 | ||
953 | /* Look up the objfile with this code entry address. */ | |
954 | ||
955 | static struct objfile * | |
956 | jit_find_objf_with_entry_addr (CORE_ADDR entry_addr) | |
957 | { | |
958 | struct objfile *objf; | |
4efc6507 DE |
959 | |
960 | ALL_OBJFILES (objf) | |
961 | { | |
03bef283 TT |
962 | struct jit_objfile_data *objf_data; |
963 | ||
964 | objf_data = objfile_data (objf, jit_objfile_data); | |
965 | if (objf_data != NULL && objf_data->addr == entry_addr) | |
4efc6507 DE |
966 | return objf; |
967 | } | |
968 | return NULL; | |
969 | } | |
970 | ||
03673fc7 PP |
971 | /* (Re-)Initialize the jit breakpoint if necessary. |
972 | Return 0 on success. */ | |
973 | ||
974 | static int | |
975 | jit_breakpoint_re_set_internal (struct gdbarch *gdbarch, | |
976 | struct jit_inferior_data *inf_data) | |
977 | { | |
03bef283 TT |
978 | struct minimal_symbol *reg_symbol, *desc_symbol; |
979 | struct objfile *objf; | |
980 | struct jit_objfile_data *objf_data; | |
981 | ||
982 | if (inf_data->objfile != NULL) | |
03673fc7 PP |
983 | return 0; |
984 | ||
03bef283 TT |
985 | /* Lookup the registration symbol. If it is missing, then we assume |
986 | we are not attached to a JIT. */ | |
987 | reg_symbol = lookup_minimal_symbol_and_objfile (jit_break_name, &objf); | |
988 | if (reg_symbol == NULL || SYMBOL_VALUE_ADDRESS (reg_symbol) == 0) | |
989 | return 1; | |
990 | ||
991 | desc_symbol = lookup_minimal_symbol (jit_descriptor_name, NULL, objf); | |
992 | if (desc_symbol == NULL || SYMBOL_VALUE_ADDRESS (desc_symbol) == 0) | |
993 | return 1; | |
994 | ||
995 | objf_data = get_jit_objfile_data (objf); | |
996 | objf_data->register_code = reg_symbol; | |
997 | objf_data->descriptor = desc_symbol; | |
998 | ||
999 | inf_data->objfile = objf; | |
1000 | ||
1001 | jit_inferior_init (gdbarch); | |
1002 | ||
03673fc7 PP |
1003 | if (jit_debug) |
1004 | fprintf_unfiltered (gdb_stdlog, | |
1005 | "jit_breakpoint_re_set_internal, " | |
1006 | "breakpoint_addr = %s\n", | |
03bef283 | 1007 | paddress (gdbarch, SYMBOL_VALUE_ADDRESS (reg_symbol))); |
03673fc7 PP |
1008 | |
1009 | /* Put a breakpoint in the registration symbol. */ | |
03bef283 | 1010 | create_jit_event_breakpoint (gdbarch, SYMBOL_VALUE_ADDRESS (reg_symbol)); |
03673fc7 PP |
1011 | |
1012 | return 0; | |
1013 | } | |
1014 | ||
3623dc3a SD |
1015 | /* The private data passed around in the frame unwind callback |
1016 | functions. */ | |
1017 | ||
1018 | struct jit_unwind_private | |
1019 | { | |
1020 | /* Cached register values. See jit_frame_sniffer to see how this | |
1021 | works. */ | |
1022 | struct gdb_reg_value **registers; | |
1023 | ||
1024 | /* The frame being unwound. */ | |
1025 | struct frame_info *this_frame; | |
1026 | }; | |
1027 | ||
1028 | /* Sets the value of a particular register in this frame. */ | |
1029 | ||
1030 | static void | |
1031 | jit_unwind_reg_set_impl (struct gdb_unwind_callbacks *cb, int dwarf_regnum, | |
1032 | struct gdb_reg_value *value) | |
1033 | { | |
1034 | struct jit_unwind_private *priv; | |
1035 | int gdb_reg; | |
1036 | ||
1037 | priv = cb->priv_data; | |
1038 | ||
1039 | gdb_reg = gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv->this_frame), | |
1040 | dwarf_regnum); | |
1041 | if (gdb_reg == -1) | |
1042 | { | |
1043 | if (jit_debug) | |
1044 | fprintf_unfiltered (gdb_stdlog, | |
1045 | _("Could not recognize DWARF regnum %d"), | |
1046 | dwarf_regnum); | |
1047 | return; | |
1048 | } | |
1049 | ||
1050 | gdb_assert (priv->registers); | |
1051 | priv->registers[gdb_reg] = value; | |
1052 | } | |
1053 | ||
1054 | static void | |
1055 | reg_value_free_impl (struct gdb_reg_value *value) | |
1056 | { | |
1057 | xfree (value); | |
1058 | } | |
1059 | ||
1060 | /* Get the value of register REGNUM in the previous frame. */ | |
1061 | ||
1062 | static struct gdb_reg_value * | |
1063 | jit_unwind_reg_get_impl (struct gdb_unwind_callbacks *cb, int regnum) | |
1064 | { | |
1065 | struct jit_unwind_private *priv; | |
1066 | struct gdb_reg_value *value; | |
1067 | int gdb_reg, size; | |
1068 | struct gdbarch *frame_arch; | |
1069 | ||
1070 | priv = cb->priv_data; | |
1071 | frame_arch = get_frame_arch (priv->this_frame); | |
1072 | ||
1073 | gdb_reg = gdbarch_dwarf2_reg_to_regnum (frame_arch, regnum); | |
1074 | size = register_size (frame_arch, gdb_reg); | |
1075 | value = xmalloc (sizeof (struct gdb_reg_value) + size - 1); | |
ca9d61b9 JB |
1076 | value->defined = deprecated_frame_register_read (priv->this_frame, gdb_reg, |
1077 | value->value); | |
3623dc3a SD |
1078 | value->size = size; |
1079 | value->free = reg_value_free_impl; | |
1080 | return value; | |
1081 | } | |
1082 | ||
1083 | /* gdb_reg_value has a free function, which must be called on each | |
1084 | saved register value. */ | |
1085 | ||
1086 | static void | |
1087 | jit_dealloc_cache (struct frame_info *this_frame, void *cache) | |
1088 | { | |
1089 | struct jit_unwind_private *priv_data = cache; | |
1090 | struct gdbarch *frame_arch; | |
1091 | int i; | |
1092 | ||
1093 | gdb_assert (priv_data->registers); | |
1094 | frame_arch = get_frame_arch (priv_data->this_frame); | |
1095 | ||
1096 | for (i = 0; i < gdbarch_num_regs (frame_arch); i++) | |
1097 | if (priv_data->registers[i] && priv_data->registers[i]->free) | |
1098 | priv_data->registers[i]->free (priv_data->registers[i]); | |
1099 | ||
1100 | xfree (priv_data->registers); | |
1101 | xfree (priv_data); | |
1102 | } | |
1103 | ||
1104 | /* The frame sniffer for the pseudo unwinder. | |
1105 | ||
1106 | While this is nominally a frame sniffer, in the case where the JIT | |
1107 | reader actually recognizes the frame, it does a lot more work -- it | |
1108 | unwinds the frame and saves the corresponding register values in | |
1109 | the cache. jit_frame_prev_register simply returns the saved | |
1110 | register values. */ | |
1111 | ||
1112 | static int | |
1113 | jit_frame_sniffer (const struct frame_unwind *self, | |
1114 | struct frame_info *this_frame, void **cache) | |
1115 | { | |
3623dc3a | 1116 | struct jit_unwind_private *priv_data; |
3623dc3a SD |
1117 | struct gdb_unwind_callbacks callbacks; |
1118 | struct gdb_reader_funcs *funcs; | |
1119 | ||
3623dc3a SD |
1120 | callbacks.reg_get = jit_unwind_reg_get_impl; |
1121 | callbacks.reg_set = jit_unwind_reg_set_impl; | |
1122 | callbacks.target_read = jit_target_read_impl; | |
1123 | ||
1124 | if (loaded_jit_reader == NULL) | |
1125 | return 0; | |
1126 | ||
1127 | funcs = loaded_jit_reader->functions; | |
1128 | ||
1129 | gdb_assert (!*cache); | |
1130 | ||
1131 | *cache = XZALLOC (struct jit_unwind_private); | |
1132 | priv_data = *cache; | |
1133 | priv_data->registers = | |
1134 | XCALLOC (gdbarch_num_regs (get_frame_arch (this_frame)), | |
1135 | struct gdb_reg_value *); | |
1136 | priv_data->this_frame = this_frame; | |
1137 | ||
1138 | callbacks.priv_data = priv_data; | |
1139 | ||
1140 | /* Try to coax the provided unwinder to unwind the stack */ | |
1141 | if (funcs->unwind (funcs, &callbacks) == GDB_SUCCESS) | |
1142 | { | |
1143 | if (jit_debug) | |
1144 | fprintf_unfiltered (gdb_stdlog, _("Successfully unwound frame using " | |
1145 | "JIT reader.\n")); | |
1146 | return 1; | |
1147 | } | |
1148 | if (jit_debug) | |
1149 | fprintf_unfiltered (gdb_stdlog, _("Could not unwind frame using " | |
1150 | "JIT reader.\n")); | |
1151 | ||
1152 | jit_dealloc_cache (this_frame, *cache); | |
1153 | *cache = NULL; | |
1154 | ||
1155 | return 0; | |
1156 | } | |
1157 | ||
1158 | ||
1159 | /* The frame_id function for the pseudo unwinder. Relays the call to | |
1160 | the loaded plugin. */ | |
1161 | ||
1162 | static void | |
1163 | jit_frame_this_id (struct frame_info *this_frame, void **cache, | |
1164 | struct frame_id *this_id) | |
1165 | { | |
1166 | struct jit_unwind_private private; | |
1167 | struct gdb_frame_id frame_id; | |
1168 | struct gdb_reader_funcs *funcs; | |
1169 | struct gdb_unwind_callbacks callbacks; | |
1170 | ||
1171 | private.registers = NULL; | |
1172 | private.this_frame = this_frame; | |
1173 | ||
1174 | /* We don't expect the frame_id function to set any registers, so we | |
1175 | set reg_set to NULL. */ | |
1176 | callbacks.reg_get = jit_unwind_reg_get_impl; | |
1177 | callbacks.reg_set = NULL; | |
1178 | callbacks.target_read = jit_target_read_impl; | |
1179 | callbacks.priv_data = &private; | |
1180 | ||
1181 | gdb_assert (loaded_jit_reader); | |
1182 | funcs = loaded_jit_reader->functions; | |
1183 | ||
1184 | frame_id = funcs->get_frame_id (funcs, &callbacks); | |
1185 | *this_id = frame_id_build (frame_id.stack_address, frame_id.code_address); | |
1186 | } | |
1187 | ||
1188 | /* Pseudo unwinder function. Reads the previously fetched value for | |
1189 | the register from the cache. */ | |
1190 | ||
1191 | static struct value * | |
1192 | jit_frame_prev_register (struct frame_info *this_frame, void **cache, int reg) | |
1193 | { | |
1194 | struct jit_unwind_private *priv = *cache; | |
1195 | struct gdb_reg_value *value; | |
1196 | ||
1197 | if (priv == NULL) | |
1198 | return frame_unwind_got_optimized (this_frame, reg); | |
1199 | ||
1200 | gdb_assert (priv->registers); | |
1201 | value = priv->registers[reg]; | |
1202 | if (value && value->defined) | |
1203 | return frame_unwind_got_bytes (this_frame, reg, value->value); | |
1204 | else | |
1205 | return frame_unwind_got_optimized (this_frame, reg); | |
1206 | } | |
1207 | ||
1208 | /* Relay everything back to the unwinder registered by the JIT debug | |
1209 | info reader.*/ | |
1210 | ||
1211 | static const struct frame_unwind jit_frame_unwind = | |
1212 | { | |
1213 | NORMAL_FRAME, | |
1214 | default_frame_unwind_stop_reason, | |
1215 | jit_frame_this_id, | |
1216 | jit_frame_prev_register, | |
1217 | NULL, | |
1218 | jit_frame_sniffer, | |
1219 | jit_dealloc_cache | |
1220 | }; | |
1221 | ||
1222 | ||
1223 | /* This is the information that is stored at jit_gdbarch_data for each | |
1224 | architecture. */ | |
1225 | ||
1226 | struct jit_gdbarch_data_type | |
1227 | { | |
1228 | /* Has the (pseudo) unwinder been prepended? */ | |
1229 | int unwinder_registered; | |
1230 | }; | |
1231 | ||
1232 | /* Check GDBARCH and prepend the pseudo JIT unwinder if needed. */ | |
1233 | ||
1234 | static void | |
1235 | jit_prepend_unwinder (struct gdbarch *gdbarch) | |
1236 | { | |
1237 | struct jit_gdbarch_data_type *data; | |
1238 | ||
1239 | data = gdbarch_data (gdbarch, jit_gdbarch_data); | |
1240 | if (!data->unwinder_registered) | |
1241 | { | |
1242 | frame_unwind_prepend_unwinder (gdbarch, &jit_frame_unwind); | |
1243 | data->unwinder_registered = 1; | |
1244 | } | |
1245 | } | |
1246 | ||
03673fc7 | 1247 | /* Register any already created translations. */ |
0756c555 DE |
1248 | |
1249 | static void | |
1250 | jit_inferior_init (struct gdbarch *gdbarch) | |
4efc6507 | 1251 | { |
4efc6507 DE |
1252 | struct jit_descriptor descriptor; |
1253 | struct jit_code_entry cur_entry; | |
03673fc7 | 1254 | struct jit_inferior_data *inf_data; |
4efc6507 | 1255 | CORE_ADDR cur_entry_addr; |
4efc6507 | 1256 | |
a255712f | 1257 | if (jit_debug) |
03673fc7 | 1258 | fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n"); |
a255712f | 1259 | |
3623dc3a SD |
1260 | jit_prepend_unwinder (gdbarch); |
1261 | ||
03673fc7 PP |
1262 | inf_data = get_jit_inferior_data (); |
1263 | if (jit_breakpoint_re_set_internal (gdbarch, inf_data) != 0) | |
4efc6507 DE |
1264 | return; |
1265 | ||
1777feb0 MS |
1266 | /* Read the descriptor so we can check the version number and load |
1267 | any already JITed functions. */ | |
03bef283 TT |
1268 | if (!jit_read_descriptor (gdbarch, &descriptor, inf_data)) |
1269 | return; | |
4efc6507 DE |
1270 | |
1271 | /* Check that the version number agrees with that we support. */ | |
1272 | if (descriptor.version != 1) | |
03bef283 TT |
1273 | { |
1274 | printf_unfiltered (_("Unsupported JIT protocol version %ld " | |
1275 | "in descriptor (expected 1)\n"), | |
1276 | (long) descriptor.version); | |
1277 | return; | |
1278 | } | |
4efc6507 | 1279 | |
1777feb0 MS |
1280 | /* If we've attached to a running program, we need to check the descriptor |
1281 | to register any functions that were already generated. */ | |
4efc6507 DE |
1282 | for (cur_entry_addr = descriptor.first_entry; |
1283 | cur_entry_addr != 0; | |
1284 | cur_entry_addr = cur_entry.next_entry) | |
1285 | { | |
0756c555 | 1286 | jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry); |
4efc6507 DE |
1287 | |
1288 | /* This hook may be called many times during setup, so make sure we don't | |
1289 | add the same symbol file twice. */ | |
1290 | if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL) | |
1291 | continue; | |
1292 | ||
0756c555 | 1293 | jit_register_code (gdbarch, cur_entry_addr, &cur_entry); |
4efc6507 DE |
1294 | } |
1295 | } | |
1296 | ||
0756c555 DE |
1297 | /* Exported routine to call when an inferior has been created. */ |
1298 | ||
1299 | void | |
1300 | jit_inferior_created_hook (void) | |
1301 | { | |
f5656ead | 1302 | jit_inferior_init (target_gdbarch ()); |
0756c555 DE |
1303 | } |
1304 | ||
1305 | /* Exported routine to call to re-set the jit breakpoints, | |
1306 | e.g. when a program is rerun. */ | |
1307 | ||
1308 | void | |
1309 | jit_breakpoint_re_set (void) | |
1310 | { | |
f5656ead | 1311 | jit_breakpoint_re_set_internal (target_gdbarch (), |
03673fc7 PP |
1312 | get_jit_inferior_data ()); |
1313 | } | |
1314 | ||
1777feb0 MS |
1315 | /* This function cleans up any code entries left over when the |
1316 | inferior exits. We get left over code when the inferior exits | |
1317 | without unregistering its code, for example when it crashes. */ | |
4efc6507 DE |
1318 | |
1319 | static void | |
a79b8f6e | 1320 | jit_inferior_exit_hook (struct inferior *inf) |
4efc6507 DE |
1321 | { |
1322 | struct objfile *objf; | |
1323 | struct objfile *temp; | |
1324 | ||
4efc6507 | 1325 | ALL_OBJFILES_SAFE (objf, temp) |
03bef283 TT |
1326 | { |
1327 | struct jit_objfile_data *objf_data = objfile_data (objf, | |
1328 | jit_objfile_data); | |
4efc6507 | 1329 | |
03bef283 TT |
1330 | if (objf_data != NULL && objf_data->addr != 0) |
1331 | jit_unregister_code (objf); | |
1332 | } | |
03673fc7 PP |
1333 | } |
1334 | ||
4efc6507 | 1335 | void |
0756c555 | 1336 | jit_event_handler (struct gdbarch *gdbarch) |
4efc6507 DE |
1337 | { |
1338 | struct jit_descriptor descriptor; | |
1339 | struct jit_code_entry code_entry; | |
1340 | CORE_ADDR entry_addr; | |
1341 | struct objfile *objf; | |
1342 | ||
1343 | /* Read the descriptor from remote memory. */ | |
03bef283 TT |
1344 | if (!jit_read_descriptor (gdbarch, &descriptor, get_jit_inferior_data ())) |
1345 | return; | |
4efc6507 DE |
1346 | entry_addr = descriptor.relevant_entry; |
1347 | ||
1777feb0 | 1348 | /* Do the corresponding action. */ |
4efc6507 DE |
1349 | switch (descriptor.action_flag) |
1350 | { | |
1351 | case JIT_NOACTION: | |
1352 | break; | |
1353 | case JIT_REGISTER: | |
0756c555 DE |
1354 | jit_read_code_entry (gdbarch, entry_addr, &code_entry); |
1355 | jit_register_code (gdbarch, entry_addr, &code_entry); | |
4efc6507 DE |
1356 | break; |
1357 | case JIT_UNREGISTER: | |
1358 | objf = jit_find_objf_with_entry_addr (entry_addr); | |
1359 | if (objf == NULL) | |
1777feb0 MS |
1360 | printf_unfiltered (_("Unable to find JITed code " |
1361 | "entry at address: %s\n"), | |
dfdbc9b4 | 1362 | paddress (gdbarch, entry_addr)); |
4efc6507 DE |
1363 | else |
1364 | jit_unregister_code (objf); | |
1365 | ||
1366 | break; | |
1367 | default: | |
1368 | error (_("Unknown action_flag value in JIT descriptor!")); | |
1369 | break; | |
1370 | } | |
1371 | } | |
1372 | ||
1825a88d SD |
1373 | /* Called to free the data allocated to the jit_inferior_data slot. */ |
1374 | ||
1375 | static void | |
1376 | free_objfile_data (struct objfile *objfile, void *data) | |
1377 | { | |
03bef283 TT |
1378 | struct jit_objfile_data *objf_data = data; |
1379 | ||
1380 | if (objf_data->register_code != NULL) | |
1381 | { | |
1382 | struct jit_inferior_data *inf_data = get_jit_inferior_data (); | |
1383 | ||
1384 | if (inf_data->objfile == objfile) | |
1385 | inf_data->objfile = NULL; | |
1386 | } | |
1387 | ||
1825a88d SD |
1388 | xfree (data); |
1389 | } | |
1390 | ||
3623dc3a SD |
1391 | /* Initialize the jit_gdbarch_data slot with an instance of struct |
1392 | jit_gdbarch_data_type */ | |
1393 | ||
1394 | static void * | |
1395 | jit_gdbarch_data_init (struct obstack *obstack) | |
1396 | { | |
1397 | struct jit_gdbarch_data_type *data; | |
1398 | ||
1399 | data = obstack_alloc (obstack, sizeof (struct jit_gdbarch_data_type)); | |
1400 | data->unwinder_registered = 0; | |
1401 | return data; | |
1402 | } | |
1403 | ||
4efc6507 DE |
1404 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1405 | ||
1406 | extern void _initialize_jit (void); | |
1407 | ||
1408 | void | |
1409 | _initialize_jit (void) | |
1410 | { | |
b8e0a31c SD |
1411 | jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR, |
1412 | JIT_READER_DIR_RELOCATABLE); | |
ccce17b0 YQ |
1413 | add_setshow_zuinteger_cmd ("jit", class_maintenance, &jit_debug, |
1414 | _("Set JIT debugging."), | |
1415 | _("Show JIT debugging."), | |
1416 | _("When non-zero, JIT debugging is enabled."), | |
1417 | NULL, | |
1418 | show_jit_debug, | |
1419 | &setdebuglist, &showdebuglist); | |
a255712f | 1420 | |
4efc6507 | 1421 | observer_attach_inferior_exit (jit_inferior_exit_hook); |
1825a88d SD |
1422 | jit_objfile_data = |
1423 | register_objfile_data_with_cleanup (NULL, free_objfile_data); | |
03673fc7 | 1424 | jit_inferior_data = |
8e260fc0 | 1425 | register_inferior_data_with_cleanup (NULL, jit_inferior_data_cleanup); |
3623dc3a | 1426 | jit_gdbarch_data = gdbarch_data_register_pre_init (jit_gdbarch_data_init); |
784c47ee SD |
1427 | if (is_dl_available ()) |
1428 | { | |
1429 | add_com ("jit-reader-load", no_class, jit_reader_load_command, _("\ | |
1430 | Load FILE as debug info reader and unwinder for JIT compiled code.\n\ | |
1431 | Usage: jit-reader-load FILE\n\ | |
1432 | Try to load file FILE as a debug info reader (and unwinder) for\n\ | |
1433 | JIT compiled code. The file is loaded from " JIT_READER_DIR ",\n\ | |
1434 | relocated relative to the GDB executable if required.")); | |
1435 | add_com ("jit-reader-unload", no_class, jit_reader_unload_command, _("\ | |
1436 | Unload the currently loaded JIT debug info reader.\n\ | |
1437 | Usage: jit-reader-unload FILE\n\n\ | |
1438 | Do \"help jit-reader-load\" for info on loading debug info readers.")); | |
1439 | } | |
4efc6507 | 1440 | } |