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