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