Commit | Line | Data |
---|---|---|
4efc6507 DE |
1 | /* Handle JIT code generation in the inferior for GDB, the GNU Debugger. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2009-2020 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" |
76727919 | 33 | #include "observable.h" |
4efc6507 | 34 | #include "objfiles.h" |
3623dc3a | 35 | #include "regcache.h" |
4efc6507 DE |
36 | #include "symfile.h" |
37 | #include "symtab.h" | |
38 | #include "target.h" | |
2d41fa11 | 39 | #include "gdbsupport/gdb-dlfcn.h" |
53ce3c39 | 40 | #include <sys/stat.h> |
cbb099e8 | 41 | #include "gdb_bfd.h" |
6571a381 TT |
42 | #include "readline/tilde.h" |
43 | #include "completer.h" | |
1b61f46d | 44 | #include <forward_list> |
4efc6507 | 45 | |
f2aec7f6 | 46 | static std::string jit_reader_dir; |
b8e0a31c | 47 | |
db92ac45 | 48 | static const char jit_break_name[] = "__jit_debug_register_code"; |
4efc6507 | 49 | |
db92ac45 | 50 | static const char jit_descriptor_name[] = "__jit_debug_descriptor"; |
4efc6507 | 51 | |
42a4fec5 | 52 | static void jit_inferior_created_hook (inferior *inf); |
20aa2c60 | 53 | static void jit_inferior_exit_hook (struct inferior *inf); |
3b2a0cf2 | 54 | |
3623dc3a SD |
55 | /* An unwinder is registered for every gdbarch. This key is used to |
56 | remember if the unwinder has been registered for a particular | |
57 | gdbarch. */ | |
58 | ||
59 | static struct gdbarch_data *jit_gdbarch_data; | |
60 | ||
a255712f PP |
61 | /* Non-zero if we want to see trace of jit level stuff. */ |
62 | ||
ccce17b0 | 63 | static unsigned int jit_debug = 0; |
a255712f PP |
64 | |
65 | static void | |
66 | show_jit_debug (struct ui_file *file, int from_tty, | |
67 | struct cmd_list_element *c, const char *value) | |
68 | { | |
69 | fprintf_filtered (file, _("JIT debugging is %s.\n"), value); | |
70 | } | |
71 | ||
0e8621a0 TT |
72 | struct jit_reader |
73 | { | |
74 | jit_reader (struct gdb_reader_funcs *f, gdb_dlhandle_up &&h) | |
75 | : functions (f), handle (std::move (h)) | |
76 | { | |
77 | } | |
78 | ||
79 | ~jit_reader () | |
80 | { | |
81 | functions->destroy (functions); | |
82 | } | |
83 | ||
d6541620 | 84 | DISABLE_COPY_AND_ASSIGN (jit_reader); |
0e8621a0 TT |
85 | |
86 | struct gdb_reader_funcs *functions; | |
87 | gdb_dlhandle_up handle; | |
88 | }; | |
89 | ||
784c47ee SD |
90 | /* One reader that has been loaded successfully, and can potentially be used to |
91 | parse debug info. */ | |
92 | ||
0e8621a0 | 93 | static struct jit_reader *loaded_jit_reader = NULL; |
784c47ee SD |
94 | |
95 | typedef struct gdb_reader_funcs * (reader_init_fn_type) (void); | |
db92ac45 | 96 | static const char reader_init_fn_sym[] = "gdb_init_reader"; |
784c47ee SD |
97 | |
98 | /* Try to load FILE_NAME as a JIT debug info reader. */ | |
99 | ||
100 | static struct jit_reader * | |
101 | jit_reader_load (const char *file_name) | |
102 | { | |
784c47ee | 103 | reader_init_fn_type *init_fn; |
784c47ee | 104 | struct gdb_reader_funcs *funcs = NULL; |
784c47ee SD |
105 | |
106 | if (jit_debug) | |
107 | fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"), | |
3a90f266 | 108 | file_name); |
0e8621a0 | 109 | gdb_dlhandle_up so = gdb_dlopen (file_name); |
784c47ee | 110 | |
15cf126c | 111 | init_fn = (reader_init_fn_type *) gdb_dlsym (so, reader_init_fn_sym); |
784c47ee SD |
112 | if (!init_fn) |
113 | error (_("Could not locate initialization function: %s."), | |
3a90f266 | 114 | reader_init_fn_sym); |
784c47ee SD |
115 | |
116 | if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL) | |
117 | error (_("Reader not GPL compatible.")); | |
118 | ||
119 | funcs = init_fn (); | |
120 | if (funcs->reader_version != GDB_READER_INTERFACE_VERSION) | |
121 | error (_("Reader version does not match GDB version.")); | |
122 | ||
0e8621a0 | 123 | return new jit_reader (funcs, std::move (so)); |
784c47ee SD |
124 | } |
125 | ||
126 | /* Provides the jit-reader-load command. */ | |
127 | ||
128 | static void | |
0b39b52e | 129 | jit_reader_load_command (const char *args, int from_tty) |
784c47ee | 130 | { |
784c47ee SD |
131 | if (args == NULL) |
132 | error (_("No reader name provided.")); | |
7c218e6c | 133 | gdb::unique_xmalloc_ptr<char> file (tilde_expand (args)); |
784c47ee SD |
134 | |
135 | if (loaded_jit_reader != NULL) | |
136 | error (_("JIT reader already loaded. Run jit-reader-unload first.")); | |
137 | ||
7c218e6c | 138 | if (!IS_ABSOLUTE_PATH (file.get ())) |
f2aec7f6 | 139 | file.reset (xstrprintf ("%s%s%s", jit_reader_dir.c_str (), SLASH_STRING, |
7c218e6c | 140 | file.get ())); |
784c47ee | 141 | |
7c218e6c | 142 | loaded_jit_reader = jit_reader_load (file.get ()); |
20aa2c60 | 143 | reinit_frame_cache (); |
32495661 | 144 | jit_inferior_created_hook (current_inferior ()); |
784c47ee SD |
145 | } |
146 | ||
147 | /* Provides the jit-reader-unload command. */ | |
148 | ||
149 | static void | |
0b39b52e | 150 | jit_reader_unload_command (const char *args, int from_tty) |
784c47ee SD |
151 | { |
152 | if (!loaded_jit_reader) | |
153 | error (_("No JIT reader loaded.")); | |
154 | ||
20aa2c60 PA |
155 | reinit_frame_cache (); |
156 | jit_inferior_exit_hook (current_inferior ()); | |
784c47ee | 157 | |
0e8621a0 | 158 | delete loaded_jit_reader; |
784c47ee SD |
159 | loaded_jit_reader = NULL; |
160 | } | |
161 | ||
0e74a041 | 162 | /* Destructor for jiter_objfile_data. */ |
03bef283 | 163 | |
0e74a041 | 164 | jiter_objfile_data::~jiter_objfile_data () |
03bef283 | 165 | { |
77208eb7 SM |
166 | if (this->jit_breakpoint != nullptr) |
167 | delete_breakpoint (this->jit_breakpoint); | |
238b5c9f | 168 | } |
03673fc7 | 169 | |
0e74a041 | 170 | /* Fetch the jiter_objfile_data associated with OBJF. If no data exists |
03bef283 TT |
171 | yet, make a new structure and attach it. */ |
172 | ||
0e74a041 SM |
173 | static jiter_objfile_data * |
174 | get_jiter_objfile_data (objfile *objf) | |
03bef283 | 175 | { |
0e74a041 | 176 | if (objf->jiter_data == nullptr) |
c1072906 | 177 | objf->jiter_data.reset (new jiter_objfile_data ()); |
03bef283 | 178 | |
0e74a041 | 179 | return objf->jiter_data.get (); |
03bef283 TT |
180 | } |
181 | ||
b4264740 SD |
182 | /* Remember OBJFILE has been created for struct jit_code_entry located |
183 | at inferior address ENTRY. */ | |
1825a88d SD |
184 | |
185 | static void | |
186 | add_objfile_entry (struct objfile *objfile, CORE_ADDR entry) | |
187 | { | |
0e74a041 | 188 | gdb_assert (objfile->jited_data == nullptr); |
1825a88d | 189 | |
0e74a041 | 190 | objfile->jited_data.reset (new jited_objfile_data (entry)); |
1825a88d SD |
191 | } |
192 | ||
1777feb0 | 193 | /* Helper function for reading the global JIT descriptor from remote |
bd920864 | 194 | memory. Returns true if all went well, false otherwise. */ |
4efc6507 | 195 | |
bd920864 | 196 | static bool |
fe053b9e TBA |
197 | jit_read_descriptor (gdbarch *gdbarch, |
198 | jit_descriptor *descriptor, | |
199 | objfile *jiter) | |
4efc6507 DE |
200 | { |
201 | int err; | |
202 | struct type *ptr_type; | |
203 | int ptr_size; | |
204 | int desc_size; | |
205 | gdb_byte *desc_buf; | |
0756c555 | 206 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
03bef283 | 207 | |
fe053b9e | 208 | gdb_assert (jiter != nullptr); |
8c1c720f SM |
209 | jiter_objfile_data *objf_data = jiter->jiter_data.get (); |
210 | gdb_assert (objf_data != nullptr); | |
03bef283 | 211 | |
2340e834 SM |
212 | CORE_ADDR addr = MSYMBOL_VALUE_ADDRESS (jiter, objf_data->descriptor); |
213 | ||
03bef283 TT |
214 | if (jit_debug) |
215 | fprintf_unfiltered (gdb_stdlog, | |
216 | "jit_read_descriptor, descriptor_addr = %s\n", | |
2340e834 | 217 | paddress (gdbarch, addr)); |
4efc6507 DE |
218 | |
219 | /* Figure out how big the descriptor is on the remote and how to read it. */ | |
0756c555 | 220 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
4efc6507 DE |
221 | ptr_size = TYPE_LENGTH (ptr_type); |
222 | desc_size = 8 + 2 * ptr_size; /* Two 32-bit ints and two pointers. */ | |
224c3ddb | 223 | desc_buf = (gdb_byte *) alloca (desc_size); |
4efc6507 DE |
224 | |
225 | /* Read the descriptor. */ | |
2340e834 | 226 | err = target_read_memory (addr, desc_buf, desc_size); |
4efc6507 | 227 | if (err) |
03bef283 TT |
228 | { |
229 | printf_unfiltered (_("Unable to read JIT descriptor from " | |
230 | "remote memory\n")); | |
bd920864 | 231 | return false; |
03bef283 | 232 | } |
4efc6507 DE |
233 | |
234 | /* Fix the endianness to match the host. */ | |
235 | descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order); | |
236 | descriptor->action_flag = | |
237 | extract_unsigned_integer (&desc_buf[4], 4, byte_order); | |
238 | descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type); | |
239 | descriptor->first_entry = | |
240 | extract_typed_address (&desc_buf[8 + ptr_size], ptr_type); | |
03bef283 | 241 | |
bd920864 | 242 | return true; |
4efc6507 DE |
243 | } |
244 | ||
245 | /* Helper function for reading a JITed code entry from remote memory. */ | |
246 | ||
247 | static void | |
0756c555 DE |
248 | jit_read_code_entry (struct gdbarch *gdbarch, |
249 | CORE_ADDR code_addr, struct jit_code_entry *code_entry) | |
4efc6507 | 250 | { |
205c306f | 251 | int err, off; |
4efc6507 DE |
252 | struct type *ptr_type; |
253 | int ptr_size; | |
254 | int entry_size; | |
205c306f | 255 | int align_bytes; |
4efc6507 | 256 | gdb_byte *entry_buf; |
0756c555 | 257 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
4efc6507 DE |
258 | |
259 | /* Figure out how big the entry is on the remote and how to read it. */ | |
0756c555 | 260 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
4efc6507 | 261 | ptr_size = TYPE_LENGTH (ptr_type); |
227ee7fc | 262 | |
e11fb955 TT |
263 | /* Figure out where the uint64_t value will be. */ |
264 | align_bytes = type_align (builtin_type (gdbarch)->builtin_uint64); | |
227ee7fc RH |
265 | off = 3 * ptr_size; |
266 | off = (off + (align_bytes - 1)) & ~(align_bytes - 1); | |
267 | ||
268 | entry_size = off + 8; /* Three pointers and one 64-bit int. */ | |
224c3ddb | 269 | entry_buf = (gdb_byte *) alloca (entry_size); |
4efc6507 DE |
270 | |
271 | /* Read the entry. */ | |
272 | err = target_read_memory (code_addr, entry_buf, entry_size); | |
273 | if (err) | |
274 | error (_("Unable to read JIT code entry from remote memory!")); | |
275 | ||
276 | /* Fix the endianness to match the host. */ | |
0756c555 | 277 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; |
4efc6507 DE |
278 | code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type); |
279 | code_entry->prev_entry = | |
280 | extract_typed_address (&entry_buf[ptr_size], ptr_type); | |
281 | code_entry->symfile_addr = | |
282 | extract_typed_address (&entry_buf[2 * ptr_size], ptr_type); | |
283 | code_entry->symfile_size = | |
205c306f | 284 | extract_unsigned_integer (&entry_buf[off], 8, byte_order); |
4efc6507 DE |
285 | } |
286 | ||
1825a88d SD |
287 | /* Proxy object for building a block. */ |
288 | ||
289 | struct gdb_block | |
290 | { | |
b6112117 SM |
291 | gdb_block (gdb_block *parent, CORE_ADDR begin, CORE_ADDR end, |
292 | const char *name) | |
293 | : parent (parent), | |
294 | begin (begin), | |
295 | end (end), | |
296 | name (name != nullptr ? xstrdup (name) : nullptr) | |
297 | {} | |
298 | ||
0394eed1 SM |
299 | /* The parent of this block. */ |
300 | struct gdb_block *parent; | |
1825a88d SD |
301 | |
302 | /* Points to the "real" block that is being built out of this | |
303 | instance. This block will be added to a blockvector, which will | |
304 | then be added to a symtab. */ | |
b6112117 | 305 | struct block *real_block = nullptr; |
1825a88d SD |
306 | |
307 | /* The first and last code address corresponding to this block. */ | |
308 | CORE_ADDR begin, end; | |
309 | ||
310 | /* The name of this block (if any). If this is non-NULL, the | |
311 | FUNCTION symbol symbol is set to this value. */ | |
b6112117 | 312 | gdb::unique_xmalloc_ptr<char> name; |
1825a88d SD |
313 | }; |
314 | ||
315 | /* Proxy object for building a symtab. */ | |
316 | ||
317 | struct gdb_symtab | |
318 | { | |
89867184 SM |
319 | explicit gdb_symtab (const char *file_name) |
320 | : file_name (file_name != nullptr ? file_name : "") | |
321 | {} | |
322 | ||
1825a88d | 323 | /* The list of blocks in this symtab. These will eventually be |
0394eed1 SM |
324 | converted to real blocks. |
325 | ||
326 | This is specifically a linked list, instead of, for example, a vector, | |
327 | because the pointers are returned to the user's debug info reader. So | |
328 | it's important that the objects don't change location during their | |
329 | lifetime (which would happen with a vector of objects getting resized). */ | |
330 | std::forward_list<gdb_block> blocks; | |
1825a88d SD |
331 | |
332 | /* The number of blocks inserted. */ | |
89867184 | 333 | int nblocks = 0; |
1825a88d SD |
334 | |
335 | /* A mapping between line numbers to PC. */ | |
89867184 | 336 | gdb::unique_xmalloc_ptr<struct linetable> linetable; |
1825a88d SD |
337 | |
338 | /* The source file for this symtab. */ | |
89867184 | 339 | std::string file_name; |
1825a88d SD |
340 | }; |
341 | ||
342 | /* Proxy object for building an object. */ | |
343 | ||
344 | struct gdb_object | |
345 | { | |
1b61f46d SM |
346 | /* Symtabs of this object. |
347 | ||
348 | This is specifically a linked list, instead of, for example, a vector, | |
349 | because the pointers are returned to the user's debug info reader. So | |
350 | it's important that the objects don't change location during their | |
351 | lifetime (which would happen with a vector of objects getting resized). */ | |
352 | std::forward_list<gdb_symtab> symtabs; | |
1825a88d SD |
353 | }; |
354 | ||
355 | /* The type of the `private' data passed around by the callback | |
356 | functions. */ | |
357 | ||
358 | typedef CORE_ADDR jit_dbg_reader_data; | |
359 | ||
360 | /* The reader calls into this function to read data off the targets | |
361 | address space. */ | |
362 | ||
363 | static enum gdb_status | |
364 | jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len) | |
365 | { | |
cb0a2700 SM |
366 | int result = target_read_memory ((CORE_ADDR) target_mem, |
367 | (gdb_byte *) gdb_buf, len); | |
1825a88d SD |
368 | if (result == 0) |
369 | return GDB_SUCCESS; | |
370 | else | |
371 | return GDB_FAIL; | |
372 | } | |
373 | ||
374 | /* The reader calls into this function to create a new gdb_object | |
375 | which it can then pass around to the other callbacks. Right now, | |
376 | all that is required is allocating the memory. */ | |
377 | ||
378 | static struct gdb_object * | |
379 | jit_object_open_impl (struct gdb_symbol_callbacks *cb) | |
380 | { | |
381 | /* CB is not required right now, but sometime in the future we might | |
382 | need a handle to it, and we'd like to do that without breaking | |
383 | the ABI. */ | |
1b61f46d | 384 | return new gdb_object; |
1825a88d SD |
385 | } |
386 | ||
387 | /* Readers call into this function to open a new gdb_symtab, which, | |
388 | again, is passed around to other callbacks. */ | |
389 | ||
390 | static struct gdb_symtab * | |
391 | jit_symtab_open_impl (struct gdb_symbol_callbacks *cb, | |
3a90f266 SM |
392 | struct gdb_object *object, |
393 | const char *file_name) | |
1825a88d | 394 | { |
1825a88d SD |
395 | /* CB stays unused. See comment in jit_object_open_impl. */ |
396 | ||
1b61f46d SM |
397 | object->symtabs.emplace_front (file_name); |
398 | return &object->symtabs.front (); | |
1825a88d SD |
399 | } |
400 | ||
1825a88d SD |
401 | /* Called by readers to open a new gdb_block. This function also |
402 | inserts the new gdb_block in the correct place in the corresponding | |
403 | gdb_symtab. */ | |
404 | ||
405 | static struct gdb_block * | |
406 | jit_block_open_impl (struct gdb_symbol_callbacks *cb, | |
3a90f266 SM |
407 | struct gdb_symtab *symtab, struct gdb_block *parent, |
408 | GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name) | |
1825a88d | 409 | { |
0394eed1 SM |
410 | /* Place the block at the beginning of the list, it will be sorted when the |
411 | symtab is finalized. */ | |
412 | symtab->blocks.emplace_front (parent, begin, end, name); | |
1825a88d SD |
413 | symtab->nblocks++; |
414 | ||
0394eed1 | 415 | return &symtab->blocks.front (); |
1825a88d SD |
416 | } |
417 | ||
418 | /* Readers call this to add a line mapping (from PC to line number) to | |
419 | a gdb_symtab. */ | |
4efc6507 DE |
420 | |
421 | static void | |
1825a88d | 422 | jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb, |
3a90f266 SM |
423 | struct gdb_symtab *stab, int nlines, |
424 | struct gdb_line_mapping *map) | |
1825a88d SD |
425 | { |
426 | int i; | |
224c3ddb | 427 | int alloc_len; |
1825a88d SD |
428 | |
429 | if (nlines < 1) | |
430 | return; | |
431 | ||
224c3ddb SM |
432 | alloc_len = sizeof (struct linetable) |
433 | + (nlines - 1) * sizeof (struct linetable_entry); | |
89867184 | 434 | stab->linetable.reset (XNEWVAR (struct linetable, alloc_len)); |
1825a88d SD |
435 | stab->linetable->nitems = nlines; |
436 | for (i = 0; i < nlines; i++) | |
437 | { | |
438 | stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc; | |
439 | stab->linetable->item[i].line = map[i].line; | |
8c95582d | 440 | stab->linetable->item[i].is_stmt = 1; |
1825a88d SD |
441 | } |
442 | } | |
443 | ||
444 | /* Called by readers to close a gdb_symtab. Does not need to do | |
445 | anything as of now. */ | |
446 | ||
447 | static void | |
448 | jit_symtab_close_impl (struct gdb_symbol_callbacks *cb, | |
3a90f266 | 449 | struct gdb_symtab *stab) |
1825a88d SD |
450 | { |
451 | /* Right now nothing needs to be done here. We may need to do some | |
452 | cleanup here in the future (again, without breaking the plugin | |
453 | ABI). */ | |
454 | } | |
455 | ||
456 | /* Transform STAB to a proper symtab, and add it it OBJFILE. */ | |
457 | ||
458 | static void | |
459 | finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile) | |
460 | { | |
43f3e411 | 461 | struct compunit_symtab *cust; |
241fd515 | 462 | size_t blockvector_size; |
1825a88d | 463 | CORE_ADDR begin, end; |
346d1dfe | 464 | struct blockvector *bv; |
1825a88d | 465 | |
0394eed1 SM |
466 | int actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks; |
467 | ||
468 | /* Sort the blocks in the order they should appear in the blockvector. */ | |
469 | stab->blocks.sort([] (const gdb_block &a, const gdb_block &b) | |
470 | { | |
471 | if (a.begin != b.begin) | |
472 | return a.begin < b.begin; | |
473 | ||
474 | return a.end > b.end; | |
475 | }); | |
1825a88d | 476 | |
89867184 SM |
477 | cust = allocate_compunit_symtab (objfile, stab->file_name.c_str ()); |
478 | allocate_symtab (cust, stab->file_name.c_str ()); | |
43f3e411 DE |
479 | add_compunit_symtab_to_objfile (cust); |
480 | ||
1825a88d | 481 | /* JIT compilers compile in memory. */ |
43f3e411 | 482 | COMPUNIT_DIRNAME (cust) = NULL; |
1825a88d SD |
483 | |
484 | /* Copy over the linetable entry if one was provided. */ | |
485 | if (stab->linetable) | |
486 | { | |
241fd515 AM |
487 | size_t size = ((stab->linetable->nitems - 1) |
488 | * sizeof (struct linetable_entry) | |
489 | + sizeof (struct linetable)); | |
43f3e411 | 490 | SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust)) |
224c3ddb | 491 | = (struct linetable *) obstack_alloc (&objfile->objfile_obstack, size); |
89867184 SM |
492 | memcpy (SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust)), |
493 | stab->linetable.get (), size); | |
1825a88d SD |
494 | } |
495 | ||
496 | blockvector_size = (sizeof (struct blockvector) | |
3a90f266 | 497 | + (actual_nblocks - 1) * sizeof (struct block *)); |
224c3ddb SM |
498 | bv = (struct blockvector *) obstack_alloc (&objfile->objfile_obstack, |
499 | blockvector_size); | |
43f3e411 | 500 | COMPUNIT_BLOCKVECTOR (cust) = bv; |
1825a88d | 501 | |
0394eed1 SM |
502 | /* At the end of this function, (begin, end) will contain the PC range this |
503 | entire blockvector spans. */ | |
346d1dfe | 504 | BLOCKVECTOR_MAP (bv) = NULL; |
0394eed1 SM |
505 | begin = stab->blocks.front ().begin; |
506 | end = stab->blocks.front ().end; | |
346d1dfe | 507 | BLOCKVECTOR_NBLOCKS (bv) = actual_nblocks; |
1825a88d SD |
508 | |
509 | /* First run over all the gdb_block objects, creating a real block | |
510 | object for each. Simultaneously, keep setting the real_block | |
511 | fields. */ | |
0394eed1 SM |
512 | int block_idx = FIRST_LOCAL_BLOCK; |
513 | for (gdb_block &gdb_block_iter : stab->blocks) | |
1825a88d SD |
514 | { |
515 | struct block *new_block = allocate_block (&objfile->objfile_obstack); | |
8c14c3a3 | 516 | struct symbol *block_name = new (&objfile->objfile_obstack) symbol; |
08feed99 | 517 | struct type *block_type = arch_type (objfile->arch (), |
2535757a | 518 | TYPE_CODE_VOID, |
77b7c781 | 519 | TARGET_CHAR_BIT, |
2535757a | 520 | "void"); |
1825a88d | 521 | |
b026f593 KS |
522 | BLOCK_MULTIDICT (new_block) |
523 | = mdict_create_linear (&objfile->objfile_obstack, NULL); | |
1825a88d | 524 | /* The address range. */ |
0394eed1 SM |
525 | BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter.begin; |
526 | BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter.end; | |
1825a88d SD |
527 | |
528 | /* The name. */ | |
1825a88d | 529 | SYMBOL_DOMAIN (block_name) = VAR_DOMAIN; |
f1e6e072 | 530 | SYMBOL_ACLASS_INDEX (block_name) = LOC_BLOCK; |
08be3fe3 | 531 | symbol_set_symtab (block_name, COMPUNIT_FILETABS (cust)); |
2535757a | 532 | SYMBOL_TYPE (block_name) = lookup_function_type (block_type); |
1825a88d SD |
533 | SYMBOL_BLOCK_VALUE (block_name) = new_block; |
534 | ||
4d4eaa30 CB |
535 | block_name->m_name = obstack_strdup (&objfile->objfile_obstack, |
536 | gdb_block_iter.name.get ()); | |
1825a88d SD |
537 | |
538 | BLOCK_FUNCTION (new_block) = block_name; | |
539 | ||
0394eed1 | 540 | BLOCKVECTOR_BLOCK (bv, block_idx) = new_block; |
1825a88d | 541 | if (begin > BLOCK_START (new_block)) |
3a90f266 | 542 | begin = BLOCK_START (new_block); |
1825a88d | 543 | if (end < BLOCK_END (new_block)) |
3a90f266 | 544 | end = BLOCK_END (new_block); |
1825a88d | 545 | |
0394eed1 SM |
546 | gdb_block_iter.real_block = new_block; |
547 | ||
548 | block_idx++; | |
1825a88d SD |
549 | } |
550 | ||
551 | /* Now add the special blocks. */ | |
0394eed1 SM |
552 | struct block *block_iter = NULL; |
553 | for (enum block_enum i : { GLOBAL_BLOCK, STATIC_BLOCK }) | |
1825a88d | 554 | { |
84a146c9 TT |
555 | struct block *new_block; |
556 | ||
557 | new_block = (i == GLOBAL_BLOCK | |
558 | ? allocate_global_block (&objfile->objfile_obstack) | |
559 | : allocate_block (&objfile->objfile_obstack)); | |
b026f593 KS |
560 | BLOCK_MULTIDICT (new_block) |
561 | = mdict_create_linear (&objfile->objfile_obstack, NULL); | |
1825a88d SD |
562 | BLOCK_SUPERBLOCK (new_block) = block_iter; |
563 | block_iter = new_block; | |
564 | ||
565 | BLOCK_START (new_block) = (CORE_ADDR) begin; | |
566 | BLOCK_END (new_block) = (CORE_ADDR) end; | |
567 | ||
346d1dfe | 568 | BLOCKVECTOR_BLOCK (bv, i) = new_block; |
84a146c9 TT |
569 | |
570 | if (i == GLOBAL_BLOCK) | |
43f3e411 | 571 | set_block_compunit_symtab (new_block, cust); |
1825a88d SD |
572 | } |
573 | ||
574 | /* Fill up the superblock fields for the real blocks, using the | |
575 | real_block fields populated earlier. */ | |
0394eed1 | 576 | for (gdb_block &gdb_block_iter : stab->blocks) |
1825a88d | 577 | { |
0394eed1 | 578 | if (gdb_block_iter.parent != NULL) |
db334a01 SD |
579 | { |
580 | /* If the plugin specifically mentioned a parent block, we | |
581 | use that. */ | |
0394eed1 SM |
582 | BLOCK_SUPERBLOCK (gdb_block_iter.real_block) = |
583 | gdb_block_iter.parent->real_block; | |
db334a01 SD |
584 | } |
585 | else | |
586 | { | |
587 | /* And if not, we set a default parent block. */ | |
0394eed1 | 588 | BLOCK_SUPERBLOCK (gdb_block_iter.real_block) = |
346d1dfe | 589 | BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK); |
db334a01 | 590 | } |
1825a88d | 591 | } |
1825a88d SD |
592 | } |
593 | ||
594 | /* Called when closing a gdb_objfile. Converts OBJ to a proper | |
595 | objfile. */ | |
596 | ||
597 | static void | |
598 | jit_object_close_impl (struct gdb_symbol_callbacks *cb, | |
3a90f266 | 599 | struct gdb_object *obj) |
1825a88d | 600 | { |
1825a88d SD |
601 | struct objfile *objfile; |
602 | jit_dbg_reader_data *priv_data; | |
603 | ||
9a3c8263 | 604 | priv_data = (jit_dbg_reader_data *) cb->priv_data; |
1825a88d | 605 | |
bda13cdc TT |
606 | objfile = objfile::make (nullptr, "<< JIT compiled code >>", |
607 | OBJF_NOT_FILENAME); | |
df6d5441 | 608 | objfile->per_bfd->gdbarch = target_gdbarch (); |
1825a88d | 609 | |
1b61f46d SM |
610 | for (gdb_symtab &symtab : obj->symtabs) |
611 | finalize_symtab (&symtab, objfile); | |
612 | ||
1825a88d | 613 | add_objfile_entry (objfile, *priv_data); |
1b61f46d SM |
614 | |
615 | delete obj; | |
1825a88d SD |
616 | } |
617 | ||
744ab88c | 618 | /* Try to read CODE_ENTRY using the loaded jit reader (if any). |
b4264740 SD |
619 | ENTRY_ADDR is the address of the struct jit_code_entry in the |
620 | inferior address space. */ | |
1825a88d SD |
621 | |
622 | static int | |
744ab88c | 623 | jit_reader_try_read_symtab (struct jit_code_entry *code_entry, |
3a90f266 | 624 | CORE_ADDR entry_addr) |
1825a88d | 625 | { |
1825a88d | 626 | int status; |
1825a88d SD |
627 | jit_dbg_reader_data priv_data; |
628 | struct gdb_reader_funcs *funcs; | |
1825a88d SD |
629 | struct gdb_symbol_callbacks callbacks = |
630 | { | |
631 | jit_object_open_impl, | |
632 | jit_symtab_open_impl, | |
633 | jit_block_open_impl, | |
634 | jit_symtab_close_impl, | |
635 | jit_object_close_impl, | |
636 | ||
637 | jit_symtab_line_mapping_add_impl, | |
638 | jit_target_read_impl, | |
639 | ||
640 | &priv_data | |
641 | }; | |
642 | ||
744ab88c | 643 | priv_data = entry_addr; |
1825a88d SD |
644 | |
645 | if (!loaded_jit_reader) | |
646 | return 0; | |
647 | ||
7190276c | 648 | gdb::byte_vector gdb_mem (code_entry->symfile_size); |
1825a88d SD |
649 | |
650 | status = 1; | |
a70b8144 | 651 | try |
492d29ea | 652 | { |
7190276c | 653 | if (target_read_memory (code_entry->symfile_addr, gdb_mem.data (), |
492d29ea PA |
654 | code_entry->symfile_size)) |
655 | status = 0; | |
656 | } | |
230d2906 | 657 | catch (const gdb_exception &e) |
492d29ea | 658 | { |
1825a88d | 659 | status = 0; |
492d29ea | 660 | } |
1825a88d SD |
661 | |
662 | if (status) | |
663 | { | |
664 | funcs = loaded_jit_reader->functions; | |
7190276c SM |
665 | if (funcs->read (funcs, &callbacks, gdb_mem.data (), |
666 | code_entry->symfile_size) | |
3a90f266 SM |
667 | != GDB_SUCCESS) |
668 | status = 0; | |
1825a88d SD |
669 | } |
670 | ||
1825a88d SD |
671 | if (jit_debug && status == 0) |
672 | fprintf_unfiltered (gdb_stdlog, | |
3a90f266 | 673 | "Could not read symtab using the loaded JIT reader.\n"); |
1825a88d SD |
674 | return status; |
675 | } | |
676 | ||
744ab88c | 677 | /* Try to read CODE_ENTRY using BFD. ENTRY_ADDR is the address of the |
b4264740 | 678 | struct jit_code_entry in the inferior address space. */ |
1825a88d SD |
679 | |
680 | static void | |
681 | jit_bfd_try_read_symtab (struct jit_code_entry *code_entry, | |
3a90f266 SM |
682 | CORE_ADDR entry_addr, |
683 | struct gdbarch *gdbarch) | |
4efc6507 | 684 | { |
4efc6507 DE |
685 | struct bfd_section *sec; |
686 | struct objfile *objfile; | |
4efc6507 | 687 | const struct bfd_arch_info *b; |
4efc6507 | 688 | |
a255712f PP |
689 | if (jit_debug) |
690 | fprintf_unfiltered (gdb_stdlog, | |
38b49e22 | 691 | "jit_bfd_try_read_symtab, symfile_addr = %s, " |
a255712f PP |
692 | "symfile_size = %s\n", |
693 | paddress (gdbarch, code_entry->symfile_addr), | |
694 | pulongest (code_entry->symfile_size)); | |
695 | ||
15cc148f MS |
696 | gdb_bfd_ref_ptr nbfd (gdb_bfd_open_from_target_memory |
697 | (code_entry->symfile_addr, code_entry->symfile_size, gnutarget)); | |
4dfb2365 JK |
698 | if (nbfd == NULL) |
699 | { | |
700 | puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n")); | |
701 | return; | |
702 | } | |
4efc6507 DE |
703 | |
704 | /* Check the format. NOTE: This initializes important data that GDB uses! | |
705 | We would segfault later without this line. */ | |
192b62ce | 706 | if (!bfd_check_format (nbfd.get (), bfd_object)) |
4efc6507 DE |
707 | { |
708 | printf_unfiltered (_("\ | |
709 | JITed symbol file is not an object file, ignoring it.\n")); | |
4efc6507 DE |
710 | return; |
711 | } | |
712 | ||
713 | /* Check bfd arch. */ | |
0756c555 | 714 | b = gdbarch_bfd_arch_info (gdbarch); |
192b62ce | 715 | if (b->compatible (b, bfd_get_arch_info (nbfd.get ())) != b) |
4efc6507 | 716 | warning (_("JITed object file architecture %s is not compatible " |
3a90f266 | 717 | "with target architecture %s."), |
192b62ce TT |
718 | bfd_get_arch_info (nbfd.get ())->printable_name, |
719 | b->printable_name); | |
4efc6507 DE |
720 | |
721 | /* Read the section address information out of the symbol file. Since the | |
722 | file is generated by the JIT at runtime, it should all of the absolute | |
723 | addresses that we care about. */ | |
37e136b1 | 724 | section_addr_info sai; |
4efc6507 | 725 | for (sec = nbfd->sections; sec != NULL; sec = sec->next) |
fd361982 | 726 | if ((bfd_section_flags (sec) & (SEC_ALLOC|SEC_LOAD)) != 0) |
4efc6507 | 727 | { |
3a90f266 SM |
728 | /* We assume that these virtual addresses are absolute, and do not |
729 | treat them as offsets. */ | |
fd361982 AM |
730 | sai.emplace_back (bfd_section_vma (sec), |
731 | bfd_section_name (sec), | |
37e136b1 | 732 | sec->index); |
4efc6507 DE |
733 | } |
734 | ||
8ac244b4 | 735 | /* This call does not take ownership of SAI. */ |
192b62ce | 736 | objfile = symbol_file_add_from_bfd (nbfd.get (), |
37e136b1 TT |
737 | bfd_get_filename (nbfd.get ()), 0, |
738 | &sai, | |
40135bb1 | 739 | OBJF_SHARED | OBJF_NOT_FILENAME, NULL); |
4efc6507 | 740 | |
744ab88c | 741 | add_objfile_entry (objfile, entry_addr); |
1825a88d SD |
742 | } |
743 | ||
744 | /* This function registers code associated with a JIT code entry. It uses the | |
745 | pointer and size pair in the entry to read the symbol file from the remote | |
746 | and then calls symbol_file_add_from_local_memory to add it as though it were | |
747 | a symbol file added by the user. */ | |
748 | ||
749 | static void | |
750 | jit_register_code (struct gdbarch *gdbarch, | |
3a90f266 | 751 | CORE_ADDR entry_addr, struct jit_code_entry *code_entry) |
1825a88d | 752 | { |
974a734b | 753 | int success; |
1825a88d SD |
754 | |
755 | if (jit_debug) | |
756 | fprintf_unfiltered (gdb_stdlog, | |
3a90f266 SM |
757 | "jit_register_code, symfile_addr = %s, " |
758 | "symfile_size = %s\n", | |
759 | paddress (gdbarch, code_entry->symfile_addr), | |
760 | pulongest (code_entry->symfile_size)); | |
1825a88d | 761 | |
744ab88c | 762 | success = jit_reader_try_read_symtab (code_entry, entry_addr); |
1825a88d SD |
763 | |
764 | if (!success) | |
744ab88c | 765 | jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch); |
4efc6507 DE |
766 | } |
767 | ||
4efc6507 DE |
768 | /* Look up the objfile with this code entry address. */ |
769 | ||
770 | static struct objfile * | |
771 | jit_find_objf_with_entry_addr (CORE_ADDR entry_addr) | |
772 | { | |
2030c079 | 773 | for (objfile *objf : current_program_space->objfiles ()) |
4efc6507 | 774 | { |
0e74a041 | 775 | if (objf->jited_data != nullptr && objf->jited_data->addr == entry_addr) |
3a90f266 | 776 | return objf; |
4efc6507 | 777 | } |
238b5c9f | 778 | |
4efc6507 DE |
779 | return NULL; |
780 | } | |
781 | ||
f25c0135 TT |
782 | /* This is called when a breakpoint is deleted. It updates the |
783 | inferior's cache, if needed. */ | |
784 | ||
785 | static void | |
786 | jit_breakpoint_deleted (struct breakpoint *b) | |
787 | { | |
f25c0135 TT |
788 | if (b->type != bp_jit_event) |
789 | return; | |
790 | ||
2340e834 | 791 | for (bp_location *iter = b->loc; iter != nullptr; iter = iter->next) |
8eacb197 | 792 | { |
c8474dc3 | 793 | for (objfile *objf : iter->pspace->objfiles ()) |
8eacb197 | 794 | { |
77208eb7 SM |
795 | jiter_objfile_data *jiter_data = objf->jiter_data.get (); |
796 | ||
c8474dc3 TBA |
797 | if (jiter_data != nullptr |
798 | && jiter_data->jit_breakpoint == iter->owner) | |
77208eb7 SM |
799 | { |
800 | jiter_data->cached_code_address = 0; | |
801 | jiter_data->jit_breakpoint = nullptr; | |
802 | } | |
8eacb197 TT |
803 | } |
804 | } | |
f25c0135 TT |
805 | } |
806 | ||
c8474dc3 TBA |
807 | /* (Re-)Initialize the jit breakpoints for JIT-producing objfiles in |
808 | PSPACE. */ | |
03673fc7 | 809 | |
c8474dc3 TBA |
810 | static void |
811 | jit_breakpoint_re_set_internal (struct gdbarch *gdbarch, program_space *pspace) | |
03673fc7 | 812 | { |
c8474dc3 | 813 | for (objfile *the_objfile : pspace->objfiles ()) |
f25c0135 | 814 | { |
a7b4ff4f SM |
815 | if (the_objfile->skip_jit_symbol_lookup) |
816 | continue; | |
817 | ||
f25c0135 TT |
818 | /* Lookup the registration symbol. If it is missing, then we |
819 | assume we are not attached to a JIT. */ | |
c8474dc3 TBA |
820 | bound_minimal_symbol reg_symbol |
821 | = lookup_minimal_symbol (jit_break_name, nullptr, the_objfile); | |
7cbd4a93 | 822 | if (reg_symbol.minsym == NULL |
77e371c0 | 823 | || BMSYMBOL_VALUE_ADDRESS (reg_symbol) == 0) |
a7b4ff4f SM |
824 | { |
825 | /* No need to repeat the lookup the next time. */ | |
826 | the_objfile->skip_jit_symbol_lookup = true; | |
827 | continue; | |
828 | } | |
03bef283 | 829 | |
c8474dc3 TBA |
830 | bound_minimal_symbol desc_symbol |
831 | = lookup_minimal_symbol (jit_descriptor_name, NULL, the_objfile); | |
3b7344d5 | 832 | if (desc_symbol.minsym == NULL |
77e371c0 | 833 | || BMSYMBOL_VALUE_ADDRESS (desc_symbol) == 0) |
a7b4ff4f SM |
834 | { |
835 | /* No need to repeat the lookup the next time. */ | |
836 | the_objfile->skip_jit_symbol_lookup = true; | |
837 | continue; | |
838 | } | |
03bef283 | 839 | |
2340e834 SM |
840 | jiter_objfile_data *objf_data |
841 | = get_jiter_objfile_data (reg_symbol.objfile); | |
7cbd4a93 | 842 | objf_data->register_code = reg_symbol.minsym; |
3b7344d5 | 843 | objf_data->descriptor = desc_symbol.minsym; |
03bef283 | 844 | |
c8474dc3 TBA |
845 | CORE_ADDR addr = MSYMBOL_VALUE_ADDRESS (the_objfile, |
846 | objf_data->register_code); | |
03bef283 | 847 | |
c8474dc3 TBA |
848 | if (jit_debug) |
849 | fprintf_unfiltered (gdb_stdlog, | |
850 | "jit_breakpoint_re_set_internal, " | |
851 | "breakpoint_addr = %s\n", | |
852 | paddress (gdbarch, addr)); | |
f25c0135 | 853 | |
c8474dc3 TBA |
854 | /* Check if we need to re-create the breakpoint. */ |
855 | if (objf_data->cached_code_address == addr) | |
856 | continue; | |
03673fc7 | 857 | |
c8474dc3 TBA |
858 | /* Delete the old breakpoint. */ |
859 | if (objf_data->jit_breakpoint != nullptr) | |
860 | delete_breakpoint (objf_data->jit_breakpoint); | |
03673fc7 | 861 | |
c8474dc3 TBA |
862 | /* Put a breakpoint in the registration symbol. */ |
863 | objf_data->cached_code_address = addr; | |
864 | objf_data->jit_breakpoint = create_jit_event_breakpoint (gdbarch, addr); | |
865 | } | |
03673fc7 PP |
866 | } |
867 | ||
3623dc3a SD |
868 | /* The private data passed around in the frame unwind callback |
869 | functions. */ | |
870 | ||
871 | struct jit_unwind_private | |
872 | { | |
873 | /* Cached register values. See jit_frame_sniffer to see how this | |
874 | works. */ | |
c8ec2f33 | 875 | detached_regcache *regcache; |
3623dc3a SD |
876 | |
877 | /* The frame being unwound. */ | |
878 | struct frame_info *this_frame; | |
879 | }; | |
880 | ||
881 | /* Sets the value of a particular register in this frame. */ | |
882 | ||
883 | static void | |
884 | jit_unwind_reg_set_impl (struct gdb_unwind_callbacks *cb, int dwarf_regnum, | |
3a90f266 | 885 | struct gdb_reg_value *value) |
3623dc3a SD |
886 | { |
887 | struct jit_unwind_private *priv; | |
888 | int gdb_reg; | |
889 | ||
9a3c8263 | 890 | priv = (struct jit_unwind_private *) cb->priv_data; |
3623dc3a SD |
891 | |
892 | gdb_reg = gdbarch_dwarf2_reg_to_regnum (get_frame_arch (priv->this_frame), | |
3a90f266 | 893 | dwarf_regnum); |
3623dc3a SD |
894 | if (gdb_reg == -1) |
895 | { | |
896 | if (jit_debug) | |
3a90f266 SM |
897 | fprintf_unfiltered (gdb_stdlog, |
898 | _("Could not recognize DWARF regnum %d"), | |
899 | dwarf_regnum); | |
20aa2c60 | 900 | value->free (value); |
3623dc3a SD |
901 | return; |
902 | } | |
903 | ||
c8ec2f33 | 904 | priv->regcache->raw_supply (gdb_reg, value->value); |
20aa2c60 | 905 | value->free (value); |
3623dc3a SD |
906 | } |
907 | ||
908 | static void | |
909 | reg_value_free_impl (struct gdb_reg_value *value) | |
910 | { | |
911 | xfree (value); | |
912 | } | |
913 | ||
914 | /* Get the value of register REGNUM in the previous frame. */ | |
915 | ||
916 | static struct gdb_reg_value * | |
917 | jit_unwind_reg_get_impl (struct gdb_unwind_callbacks *cb, int regnum) | |
918 | { | |
919 | struct jit_unwind_private *priv; | |
920 | struct gdb_reg_value *value; | |
921 | int gdb_reg, size; | |
922 | struct gdbarch *frame_arch; | |
923 | ||
9a3c8263 | 924 | priv = (struct jit_unwind_private *) cb->priv_data; |
3623dc3a SD |
925 | frame_arch = get_frame_arch (priv->this_frame); |
926 | ||
927 | gdb_reg = gdbarch_dwarf2_reg_to_regnum (frame_arch, regnum); | |
928 | size = register_size (frame_arch, gdb_reg); | |
224c3ddb SM |
929 | value = ((struct gdb_reg_value *) |
930 | xmalloc (sizeof (struct gdb_reg_value) + size - 1)); | |
ca9d61b9 JB |
931 | value->defined = deprecated_frame_register_read (priv->this_frame, gdb_reg, |
932 | value->value); | |
3623dc3a SD |
933 | value->size = size; |
934 | value->free = reg_value_free_impl; | |
935 | return value; | |
936 | } | |
937 | ||
938 | /* gdb_reg_value has a free function, which must be called on each | |
939 | saved register value. */ | |
940 | ||
941 | static void | |
942 | jit_dealloc_cache (struct frame_info *this_frame, void *cache) | |
943 | { | |
9a3c8263 | 944 | struct jit_unwind_private *priv_data = (struct jit_unwind_private *) cache; |
3623dc3a | 945 | |
20aa2c60 | 946 | gdb_assert (priv_data->regcache != NULL); |
c0e383c6 | 947 | delete priv_data->regcache; |
3623dc3a SD |
948 | xfree (priv_data); |
949 | } | |
950 | ||
951 | /* The frame sniffer for the pseudo unwinder. | |
952 | ||
953 | While this is nominally a frame sniffer, in the case where the JIT | |
954 | reader actually recognizes the frame, it does a lot more work -- it | |
955 | unwinds the frame and saves the corresponding register values in | |
956 | the cache. jit_frame_prev_register simply returns the saved | |
957 | register values. */ | |
958 | ||
959 | static int | |
960 | jit_frame_sniffer (const struct frame_unwind *self, | |
3a90f266 | 961 | struct frame_info *this_frame, void **cache) |
3623dc3a | 962 | { |
3623dc3a | 963 | struct jit_unwind_private *priv_data; |
3623dc3a SD |
964 | struct gdb_unwind_callbacks callbacks; |
965 | struct gdb_reader_funcs *funcs; | |
966 | ||
3623dc3a SD |
967 | callbacks.reg_get = jit_unwind_reg_get_impl; |
968 | callbacks.reg_set = jit_unwind_reg_set_impl; | |
969 | callbacks.target_read = jit_target_read_impl; | |
970 | ||
971 | if (loaded_jit_reader == NULL) | |
972 | return 0; | |
973 | ||
974 | funcs = loaded_jit_reader->functions; | |
975 | ||
976 | gdb_assert (!*cache); | |
977 | ||
41bf6aca | 978 | *cache = XCNEW (struct jit_unwind_private); |
9a3c8263 | 979 | priv_data = (struct jit_unwind_private *) *cache; |
c8ec2f33 YQ |
980 | /* Take a snapshot of current regcache. */ |
981 | priv_data->regcache = new detached_regcache (get_frame_arch (this_frame), | |
982 | true); | |
3623dc3a SD |
983 | priv_data->this_frame = this_frame; |
984 | ||
985 | callbacks.priv_data = priv_data; | |
986 | ||
987 | /* Try to coax the provided unwinder to unwind the stack */ | |
988 | if (funcs->unwind (funcs, &callbacks) == GDB_SUCCESS) | |
989 | { | |
990 | if (jit_debug) | |
3a90f266 SM |
991 | fprintf_unfiltered (gdb_stdlog, _("Successfully unwound frame using " |
992 | "JIT reader.\n")); | |
3623dc3a SD |
993 | return 1; |
994 | } | |
995 | if (jit_debug) | |
996 | fprintf_unfiltered (gdb_stdlog, _("Could not unwind frame using " | |
3a90f266 | 997 | "JIT reader.\n")); |
3623dc3a SD |
998 | |
999 | jit_dealloc_cache (this_frame, *cache); | |
1000 | *cache = NULL; | |
1001 | ||
1002 | return 0; | |
1003 | } | |
1004 | ||
1005 | ||
1006 | /* The frame_id function for the pseudo unwinder. Relays the call to | |
1007 | the loaded plugin. */ | |
1008 | ||
1009 | static void | |
1010 | jit_frame_this_id (struct frame_info *this_frame, void **cache, | |
3a90f266 | 1011 | struct frame_id *this_id) |
3623dc3a | 1012 | { |
fe978cb0 | 1013 | struct jit_unwind_private priv; |
3623dc3a SD |
1014 | struct gdb_frame_id frame_id; |
1015 | struct gdb_reader_funcs *funcs; | |
1016 | struct gdb_unwind_callbacks callbacks; | |
1017 | ||
20aa2c60 | 1018 | priv.regcache = NULL; |
fe978cb0 | 1019 | priv.this_frame = this_frame; |
3623dc3a SD |
1020 | |
1021 | /* We don't expect the frame_id function to set any registers, so we | |
1022 | set reg_set to NULL. */ | |
1023 | callbacks.reg_get = jit_unwind_reg_get_impl; | |
1024 | callbacks.reg_set = NULL; | |
1025 | callbacks.target_read = jit_target_read_impl; | |
fe978cb0 | 1026 | callbacks.priv_data = &priv; |
3623dc3a SD |
1027 | |
1028 | gdb_assert (loaded_jit_reader); | |
1029 | funcs = loaded_jit_reader->functions; | |
1030 | ||
1031 | frame_id = funcs->get_frame_id (funcs, &callbacks); | |
1032 | *this_id = frame_id_build (frame_id.stack_address, frame_id.code_address); | |
1033 | } | |
1034 | ||
1035 | /* Pseudo unwinder function. Reads the previously fetched value for | |
1036 | the register from the cache. */ | |
1037 | ||
1038 | static struct value * | |
1039 | jit_frame_prev_register (struct frame_info *this_frame, void **cache, int reg) | |
1040 | { | |
9a3c8263 | 1041 | struct jit_unwind_private *priv = (struct jit_unwind_private *) *cache; |
20aa2c60 | 1042 | struct gdbarch *gdbarch; |
3623dc3a SD |
1043 | |
1044 | if (priv == NULL) | |
1045 | return frame_unwind_got_optimized (this_frame, reg); | |
1046 | ||
ac7936df | 1047 | gdbarch = priv->regcache->arch (); |
3f5a868b YQ |
1048 | gdb_byte *buf = (gdb_byte *) alloca (register_size (gdbarch, reg)); |
1049 | enum register_status status = priv->regcache->cooked_read (reg, buf); | |
20aa2c60 | 1050 | |
3f5a868b YQ |
1051 | if (status == REG_VALID) |
1052 | return frame_unwind_got_bytes (this_frame, reg, buf); | |
3623dc3a | 1053 | else |
3f5a868b | 1054 | return frame_unwind_got_optimized (this_frame, reg); |
3623dc3a SD |
1055 | } |
1056 | ||
1057 | /* Relay everything back to the unwinder registered by the JIT debug | |
1058 | info reader.*/ | |
1059 | ||
1060 | static const struct frame_unwind jit_frame_unwind = | |
1061 | { | |
1062 | NORMAL_FRAME, | |
1063 | default_frame_unwind_stop_reason, | |
1064 | jit_frame_this_id, | |
1065 | jit_frame_prev_register, | |
1066 | NULL, | |
1067 | jit_frame_sniffer, | |
1068 | jit_dealloc_cache | |
1069 | }; | |
1070 | ||
1071 | ||
1072 | /* This is the information that is stored at jit_gdbarch_data for each | |
1073 | architecture. */ | |
1074 | ||
1075 | struct jit_gdbarch_data_type | |
1076 | { | |
1077 | /* Has the (pseudo) unwinder been prepended? */ | |
1078 | int unwinder_registered; | |
1079 | }; | |
1080 | ||
1081 | /* Check GDBARCH and prepend the pseudo JIT unwinder if needed. */ | |
1082 | ||
1083 | static void | |
1084 | jit_prepend_unwinder (struct gdbarch *gdbarch) | |
1085 | { | |
1086 | struct jit_gdbarch_data_type *data; | |
1087 | ||
9a3c8263 SM |
1088 | data |
1089 | = (struct jit_gdbarch_data_type *) gdbarch_data (gdbarch, jit_gdbarch_data); | |
3623dc3a SD |
1090 | if (!data->unwinder_registered) |
1091 | { | |
1092 | frame_unwind_prepend_unwinder (gdbarch, &jit_frame_unwind); | |
1093 | data->unwinder_registered = 1; | |
1094 | } | |
1095 | } | |
1096 | ||
03673fc7 | 1097 | /* Register any already created translations. */ |
0756c555 DE |
1098 | |
1099 | static void | |
32495661 | 1100 | jit_inferior_init (inferior *inf) |
4efc6507 | 1101 | { |
4efc6507 DE |
1102 | struct jit_descriptor descriptor; |
1103 | struct jit_code_entry cur_entry; | |
1104 | CORE_ADDR cur_entry_addr; | |
32495661 SM |
1105 | struct gdbarch *gdbarch = inf->gdbarch; |
1106 | program_space *pspace = inf->pspace; | |
4efc6507 | 1107 | |
a255712f | 1108 | if (jit_debug) |
03673fc7 | 1109 | fprintf_unfiltered (gdb_stdlog, "jit_inferior_init\n"); |
a255712f | 1110 | |
3623dc3a SD |
1111 | jit_prepend_unwinder (gdbarch); |
1112 | ||
32495661 | 1113 | jit_breakpoint_re_set_internal (gdbarch, pspace); |
4efc6507 | 1114 | |
32495661 | 1115 | for (objfile *jiter : pspace->objfiles ()) |
c8474dc3 TBA |
1116 | { |
1117 | if (jiter->jiter_data == nullptr) | |
1118 | continue; | |
fe053b9e | 1119 | |
c8474dc3 TBA |
1120 | /* Read the descriptor so we can check the version number and load |
1121 | any already JITed functions. */ | |
1122 | if (!jit_read_descriptor (gdbarch, &descriptor, jiter)) | |
1123 | continue; | |
4efc6507 | 1124 | |
c8474dc3 TBA |
1125 | /* Check that the version number agrees with that we support. */ |
1126 | if (descriptor.version != 1) | |
1127 | { | |
1128 | printf_unfiltered (_("Unsupported JIT protocol version %ld " | |
1129 | "in descriptor (expected 1)\n"), | |
1130 | (long) descriptor.version); | |
1131 | continue; | |
1132 | } | |
4efc6507 | 1133 | |
c8474dc3 TBA |
1134 | /* If we've attached to a running program, we need to check the |
1135 | descriptor to register any functions that were already | |
1136 | generated. */ | |
1137 | for (cur_entry_addr = descriptor.first_entry; | |
1138 | cur_entry_addr != 0; | |
1139 | cur_entry_addr = cur_entry.next_entry) | |
1140 | { | |
1141 | jit_read_code_entry (gdbarch, cur_entry_addr, &cur_entry); | |
4efc6507 | 1142 | |
c8474dc3 TBA |
1143 | /* This hook may be called many times during setup, so make sure |
1144 | we don't add the same symbol file twice. */ | |
1145 | if (jit_find_objf_with_entry_addr (cur_entry_addr) != NULL) | |
1146 | continue; | |
4efc6507 | 1147 | |
c8474dc3 TBA |
1148 | jit_register_code (gdbarch, cur_entry_addr, &cur_entry); |
1149 | } | |
4efc6507 DE |
1150 | } |
1151 | } | |
1152 | ||
42a4fec5 SM |
1153 | /* Looks for the descriptor and registration symbols and breakpoints |
1154 | the registration function. If it finds both, it registers all the | |
1155 | already JITed code. If it has already found the symbols, then it | |
1156 | doesn't try again. */ | |
0756c555 | 1157 | |
42a4fec5 | 1158 | static void |
32495661 | 1159 | jit_inferior_created_hook (inferior *inf) |
0756c555 | 1160 | { |
32495661 | 1161 | jit_inferior_init (inf); |
0756c555 DE |
1162 | } |
1163 | ||
1164 | /* Exported routine to call to re-set the jit breakpoints, | |
1165 | e.g. when a program is rerun. */ | |
1166 | ||
1167 | void | |
1168 | jit_breakpoint_re_set (void) | |
1169 | { | |
c8474dc3 | 1170 | jit_breakpoint_re_set_internal (target_gdbarch (), current_program_space); |
03673fc7 PP |
1171 | } |
1172 | ||
1777feb0 MS |
1173 | /* This function cleans up any code entries left over when the |
1174 | inferior exits. We get left over code when the inferior exits | |
1175 | without unregistering its code, for example when it crashes. */ | |
4efc6507 DE |
1176 | |
1177 | static void | |
a79b8f6e | 1178 | jit_inferior_exit_hook (struct inferior *inf) |
4efc6507 | 1179 | { |
7e955d83 | 1180 | for (objfile *objf : current_program_space->objfiles_safe ()) |
03bef283 | 1181 | { |
0e74a041 | 1182 | if (objf->jited_data != nullptr && objf->jited_data->addr != 0) |
268e4f09 | 1183 | objf->unlink (); |
03bef283 | 1184 | } |
03673fc7 PP |
1185 | } |
1186 | ||
4efc6507 | 1187 | void |
fe053b9e | 1188 | jit_event_handler (gdbarch *gdbarch, objfile *jiter) |
4efc6507 DE |
1189 | { |
1190 | struct jit_descriptor descriptor; | |
4efc6507 | 1191 | |
8c1c720f SM |
1192 | /* If we get a JIT breakpoint event for this objfile, it is necessarily a |
1193 | JITer. */ | |
1194 | gdb_assert (jiter->jiter_data != nullptr); | |
1195 | ||
4efc6507 | 1196 | /* Read the descriptor from remote memory. */ |
fe053b9e | 1197 | if (!jit_read_descriptor (gdbarch, &descriptor, jiter)) |
03bef283 | 1198 | return; |
2340e834 | 1199 | CORE_ADDR entry_addr = descriptor.relevant_entry; |
4efc6507 | 1200 | |
1777feb0 | 1201 | /* Do the corresponding action. */ |
4efc6507 DE |
1202 | switch (descriptor.action_flag) |
1203 | { | |
1204 | case JIT_NOACTION: | |
1205 | break; | |
2340e834 | 1206 | |
4efc6507 | 1207 | case JIT_REGISTER: |
2340e834 SM |
1208 | { |
1209 | jit_code_entry code_entry; | |
1210 | jit_read_code_entry (gdbarch, entry_addr, &code_entry); | |
1211 | jit_register_code (gdbarch, entry_addr, &code_entry); | |
1212 | break; | |
1213 | } | |
1214 | ||
4efc6507 | 1215 | case JIT_UNREGISTER: |
2340e834 SM |
1216 | { |
1217 | objfile *jited = jit_find_objf_with_entry_addr (entry_addr); | |
1218 | if (jited == nullptr) | |
1219 | printf_unfiltered (_("Unable to find JITed code " | |
1220 | "entry at address: %s\n"), | |
1221 | paddress (gdbarch, entry_addr)); | |
1222 | else | |
1223 | jited->unlink (); | |
1224 | ||
1225 | break; | |
1226 | } | |
4efc6507 | 1227 | |
4efc6507 DE |
1228 | default: |
1229 | error (_("Unknown action_flag value in JIT descriptor!")); | |
1230 | break; | |
1231 | } | |
1232 | } | |
1233 | ||
3623dc3a SD |
1234 | /* Initialize the jit_gdbarch_data slot with an instance of struct |
1235 | jit_gdbarch_data_type */ | |
1236 | ||
1237 | static void * | |
1238 | jit_gdbarch_data_init (struct obstack *obstack) | |
1239 | { | |
8d749320 SM |
1240 | struct jit_gdbarch_data_type *data = |
1241 | XOBNEW (obstack, struct jit_gdbarch_data_type); | |
3623dc3a | 1242 | |
3623dc3a | 1243 | data->unwinder_registered = 0; |
8d749320 | 1244 | |
3623dc3a SD |
1245 | return data; |
1246 | } | |
1247 | ||
6c265988 | 1248 | void _initialize_jit (); |
4efc6507 | 1249 | void |
6c265988 | 1250 | _initialize_jit () |
4efc6507 | 1251 | { |
b8e0a31c | 1252 | jit_reader_dir = relocate_gdb_directory (JIT_READER_DIR, |
3a90f266 | 1253 | JIT_READER_DIR_RELOCATABLE); |
ccce17b0 YQ |
1254 | add_setshow_zuinteger_cmd ("jit", class_maintenance, &jit_debug, |
1255 | _("Set JIT debugging."), | |
1256 | _("Show JIT debugging."), | |
1257 | _("When non-zero, JIT debugging is enabled."), | |
1258 | NULL, | |
1259 | show_jit_debug, | |
1260 | &setdebuglist, &showdebuglist); | |
a255712f | 1261 | |
32495661 | 1262 | gdb::observers::inferior_created.attach (jit_inferior_created_hook); |
42a4fec5 | 1263 | gdb::observers::inferior_execd.attach (jit_inferior_created_hook); |
76727919 TT |
1264 | gdb::observers::inferior_exit.attach (jit_inferior_exit_hook); |
1265 | gdb::observers::breakpoint_deleted.attach (jit_breakpoint_deleted); | |
f25c0135 | 1266 | |
3623dc3a | 1267 | jit_gdbarch_data = gdbarch_data_register_pre_init (jit_gdbarch_data_init); |
784c47ee SD |
1268 | if (is_dl_available ()) |
1269 | { | |
6571a381 TT |
1270 | struct cmd_list_element *c; |
1271 | ||
1272 | c = add_com ("jit-reader-load", no_class, jit_reader_load_command, _("\ | |
784c47ee SD |
1273 | Load FILE as debug info reader and unwinder for JIT compiled code.\n\ |
1274 | Usage: jit-reader-load FILE\n\ | |
1275 | Try to load file FILE as a debug info reader (and unwinder) for\n\ | |
1276 | JIT compiled code. The file is loaded from " JIT_READER_DIR ",\n\ | |
1277 | relocated relative to the GDB executable if required.")); | |
6571a381 TT |
1278 | set_cmd_completer (c, filename_completer); |
1279 | ||
1280 | c = add_com ("jit-reader-unload", no_class, | |
1281 | jit_reader_unload_command, _("\ | |
784c47ee | 1282 | Unload the currently loaded JIT debug info reader.\n\ |
6571a381 | 1283 | Usage: jit-reader-unload\n\n\ |
784c47ee | 1284 | Do \"help jit-reader-load\" for info on loading debug info readers.")); |
6571a381 | 1285 | set_cmd_completer (c, noop_completer); |
784c47ee | 1286 | } |
4efc6507 | 1287 | } |