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