jit.c 42.9 KB
Newer Older
1
2
/* Handle JIT code generation in the inferior for GDB, the GNU Debugger.

3
   Copyright (C) 2009-2019 Free Software Foundation, Inc.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "defs.h"

#include "jit.h"
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
23
#include "jit-reader.h"
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
24
#include "block.h"
25
#include "breakpoint.h"
Paul Pluzhnikov's avatar
Paul Pluzhnikov committed
26
#include "command.h"
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
27
#include "dictionary.h"
28
#include "filenames.h"
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
29
#include "frame-unwind.h"
Paul Pluzhnikov's avatar
Paul Pluzhnikov committed
30
#include "gdbcmd.h"
31
#include "gdbcore.h"
32
#include "inferior.h"
Tom Tromey's avatar
Tom Tromey committed
33
#include "observable.h"
34
#include "objfiles.h"
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
35
#include "regcache.h"
36
37
38
#include "symfile.h"
#include "symtab.h"
#include "target.h"
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
39
#include "gdb-dlfcn.h"
Tom Tromey's avatar
Tom Tromey committed
40
#include <sys/stat.h>
41
#include "gdb_bfd.h"
Tom Tromey's avatar
Tom Tromey committed
42
43
#include "readline/tilde.h"
#include "completer.h"
44

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
45
46
static const char *jit_reader_dir = NULL;

47
48
49
50
51
52
static const struct objfile_data *jit_objfile_data;

static const char *const jit_break_name = "__jit_debug_register_code";

static const char *const jit_descriptor_name = "__jit_debug_descriptor";

53
static const struct program_space_data *jit_program_space_data = NULL;
54

55
static void jit_inferior_init (struct gdbarch *gdbarch);
56
static void jit_inferior_exit_hook (struct inferior *inf);
57

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
58
59
60
61
62
63
/* An unwinder is registered for every gdbarch.  This key is used to
   remember if the unwinder has been registered for a particular
   gdbarch.  */

static struct gdbarch_data *jit_gdbarch_data;

Paul Pluzhnikov's avatar
Paul Pluzhnikov committed
64
65
/* Non-zero if we want to see trace of jit level stuff.  */

Yao Qi's avatar
gdb/    
Yao Qi committed
66
static unsigned int jit_debug = 0;
Paul Pluzhnikov's avatar
Paul Pluzhnikov committed
67
68
69
70
71
72
73
74

static void
show_jit_debug (struct ui_file *file, int from_tty,
		struct cmd_list_element *c, const char *value)
{
  fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
}

75
76
77
struct target_buffer
{
  CORE_ADDR base;
Paul Pluzhnikov's avatar
Paul Pluzhnikov committed
78
  ULONGEST size;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
};

/* Openning the file is a no-op.  */

static void *
mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
{
  return open_closure;
}

/* Closing the file is just freeing the base/size pair on our side.  */

static int
mem_bfd_iovec_close (struct bfd *abfd, void *stream)
{
  xfree (stream);
Jan Kratochvil's avatar
gdb/    
Jan Kratochvil committed
95
96
97

  /* Zero means success.  */
  return 0;
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
}

/* For reading the file, we just need to pass through to target_read_memory and
   fix up the arguments and return values.  */

static file_ptr
mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
                     file_ptr nbytes, file_ptr offset)
{
  int err;
  struct target_buffer *buffer = (struct target_buffer *) stream;

  /* If this read will read all of the file, limit it to just the rest.  */
  if (offset + nbytes > buffer->size)
    nbytes = buffer->size - offset;

  /* If there are no more bytes left, we've reached EOF.  */
  if (nbytes == 0)
    return 0;

  err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
  if (err)
    return -1;

  return nbytes;
}

/* For statting the file, we only support the st_size attribute.  */

static int
mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
{
  struct target_buffer *buffer = (struct target_buffer*) stream;

132
  memset (sb, 0, sizeof (struct stat));
133
134
135
136
  sb->st_size = buffer->size;
  return 0;
}

137
138
/* Open a BFD from the target's memory.  */

139
static gdb_bfd_ref_ptr
140
141
bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
{
142
  struct target_buffer *buffer = XNEW (struct target_buffer);
143
144
145

  buffer->base = addr;
  buffer->size = size;
146
147
148
149
150
151
  return gdb_bfd_openr_iovec ("<in-memory>", target,
			      mem_bfd_iovec_open,
			      buffer,
			      mem_bfd_iovec_pread,
			      mem_bfd_iovec_close,
			      mem_bfd_iovec_stat);
152
153
}

Tom Tromey's avatar
Tom Tromey committed
154
155
156
157
158
159
160
161
162
163
164
165
struct jit_reader
{
  jit_reader (struct gdb_reader_funcs *f, gdb_dlhandle_up &&h)
    : functions (f), handle (std::move (h))
  {
  }

  ~jit_reader ()
  {
    functions->destroy (functions);
  }

Yao Qi's avatar
Yao Qi committed
166
  DISABLE_COPY_AND_ASSIGN (jit_reader);
Tom Tromey's avatar
Tom Tromey committed
167
168
169
170
171

  struct gdb_reader_funcs *functions;
  gdb_dlhandle_up handle;
};

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
172
173
174
/* One reader that has been loaded successfully, and can potentially be used to
   parse debug info.  */

Tom Tromey's avatar
Tom Tromey committed
175
static struct jit_reader *loaded_jit_reader = NULL;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

typedef struct gdb_reader_funcs * (reader_init_fn_type) (void);
static const char *reader_init_fn_sym = "gdb_init_reader";

/* Try to load FILE_NAME as a JIT debug info reader.  */

static struct jit_reader *
jit_reader_load (const char *file_name)
{
  reader_init_fn_type *init_fn;
  struct gdb_reader_funcs *funcs = NULL;

  if (jit_debug)
    fprintf_unfiltered (gdb_stdlog, _("Opening shared object %s.\n"),
                        file_name);
Tom Tromey's avatar
Tom Tromey committed
191
  gdb_dlhandle_up so = gdb_dlopen (file_name);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
192

Simon Marchi's avatar
Simon Marchi committed
193
  init_fn = (reader_init_fn_type *) gdb_dlsym (so, reader_init_fn_sym);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
194
195
196
197
198
199
200
201
202
203
204
  if (!init_fn)
    error (_("Could not locate initialization function: %s."),
          reader_init_fn_sym);

  if (gdb_dlsym (so, "plugin_is_GPL_compatible") == NULL)
    error (_("Reader not GPL compatible."));

  funcs = init_fn ();
  if (funcs->reader_version != GDB_READER_INTERFACE_VERSION)
    error (_("Reader version does not match GDB version."));

Tom Tromey's avatar
Tom Tromey committed
205
  return new jit_reader (funcs, std::move (so));
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
206
207
208
209
210
}

/* Provides the jit-reader-load command.  */

static void
Tom Tromey's avatar
Tom Tromey committed
211
jit_reader_load_command (const char *args, int from_tty)
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
212
213
214
{
  if (args == NULL)
    error (_("No reader name provided."));
Tom Tromey's avatar
Tom Tromey committed
215
  gdb::unique_xmalloc_ptr<char> file (tilde_expand (args));
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
216
217
218
219

  if (loaded_jit_reader != NULL)
    error (_("JIT reader already loaded.  Run jit-reader-unload first."));

Tom Tromey's avatar
Tom Tromey committed
220
221
222
  if (!IS_ABSOLUTE_PATH (file.get ()))
    file.reset (xstrprintf ("%s%s%s", jit_reader_dir, SLASH_STRING,
			    file.get ()));
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
223

Tom Tromey's avatar
Tom Tromey committed
224
  loaded_jit_reader = jit_reader_load (file.get ());
225
226
  reinit_frame_cache ();
  jit_inferior_created_hook ();
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
227
228
229
230
231
}

/* Provides the jit-reader-unload command.  */

static void
Tom Tromey's avatar
Tom Tromey committed
232
jit_reader_unload_command (const char *args, int from_tty)
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
233
234
235
236
{
  if (!loaded_jit_reader)
    error (_("No JIT reader loaded."));

237
238
  reinit_frame_cache ();
  jit_inferior_exit_hook (current_inferior ());
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
239

Tom Tromey's avatar
Tom Tromey committed
240
  delete loaded_jit_reader;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
241
242
243
  loaded_jit_reader = NULL;
}

244
/* Per-program space structure recording which objfile has the JIT
Tom Tromey's avatar
Tom Tromey committed
245
   symbols.  */
246

247
struct jit_program_space_data
248
{
Tom Tromey's avatar
Tom Tromey committed
249
250
251
252
  /* The objfile.  This is NULL if no objfile holds the JIT
     symbols.  */

  struct objfile *objfile;
Tom Tromey's avatar
Tom Tromey committed
253

254
  /* If this program space has __jit_debug_register_code, this is the
Tom Tromey's avatar
Tom Tromey committed
255
256
257
258
259
260
261
262
263
     cached address from the minimal symbol.  This is used to detect
     relocations requiring the breakpoint to be re-created.  */

  CORE_ADDR cached_code_address;

  /* This is the JIT event breakpoint, or NULL if it has not been
     set.  */

  struct breakpoint *jit_breakpoint;
Tom Tromey's avatar
Tom Tromey committed
264
265
};

266
/* Per-objfile structure recording the addresses in the program space.
Tom Tromey's avatar
Tom Tromey committed
267
268
269
270
   This object serves two purposes: for ordinary objfiles, it may
   cache some symbols related to the JIT interface; and for
   JIT-created objfiles, it holds some information about the
   jit_code_entry.  */
Tom Tromey's avatar
Tom Tromey committed
271
272
273
274
275
276
277
278
279

struct jit_objfile_data
{
  /* Symbol for __jit_debug_register_code.  */
  struct minimal_symbol *register_code;

  /* Symbol for __jit_debug_descriptor.  */
  struct minimal_symbol *descriptor;

Tom Tromey's avatar
Tom Tromey committed
280
281
  /* Address of struct jit_code_entry in this objfile.  This is only
     non-zero for objfiles that represent code created by the JIT.  */
Tom Tromey's avatar
Tom Tromey committed
282
  CORE_ADDR addr;
283
284
};

Tom Tromey's avatar
Tom Tromey committed
285
286
287
288
289
290
291
292
/* Fetch the jit_objfile_data associated with OBJF.  If no data exists
   yet, make a new structure and attach it.  */

static struct jit_objfile_data *
get_jit_objfile_data (struct objfile *objf)
{
  struct jit_objfile_data *objf_data;

Simon Marchi's avatar
Simon Marchi committed
293
  objf_data = (struct jit_objfile_data *) objfile_data (objf, jit_objfile_data);
Tom Tromey's avatar
Tom Tromey committed
294
295
  if (objf_data == NULL)
    {
Tom Tromey's avatar
Tom Tromey committed
296
      objf_data = XCNEW (struct jit_objfile_data);
Tom Tromey's avatar
Tom Tromey committed
297
298
299
300
301
302
      set_objfile_data (objf, jit_objfile_data, objf_data);
    }

  return objf_data;
}

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
303
304
/* Remember OBJFILE has been created for struct jit_code_entry located
   at inferior address ENTRY.  */
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
305
306
307
308

static void
add_objfile_entry (struct objfile *objfile, CORE_ADDR entry)
{
Tom Tromey's avatar
Tom Tromey committed
309
  struct jit_objfile_data *objf_data;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
310

Tom Tromey's avatar
Tom Tromey committed
311
312
  objf_data = get_jit_objfile_data (objfile);
  objf_data->addr = entry;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
313
314
}

315
316
/* Return jit_program_space_data for current program space.  Allocate
   if not already present.  */
317

318
319
static struct jit_program_space_data *
get_jit_program_space_data (void)
320
{
321
  struct jit_program_space_data *ps_data;
322

Simon Marchi's avatar
Simon Marchi committed
323
324
325
  ps_data
    = ((struct jit_program_space_data *)
       program_space_data (current_program_space, jit_program_space_data));
326
  if (ps_data == NULL)
327
    {
Tom Tromey's avatar
Tom Tromey committed
328
      ps_data = XCNEW (struct jit_program_space_data);
329
330
      set_program_space_data (current_program_space, jit_program_space_data,
			      ps_data);
331
332
    }

333
  return ps_data;
334
335
336
}

static void
337
jit_program_space_data_cleanup (struct program_space *ps, void *arg)
338
339
340
341
{
  xfree (arg);
}

342
/* Helper function for reading the global JIT descriptor from remote
Tom Tromey's avatar
Tom Tromey committed
343
   memory.  Returns 1 if all went well, 0 otherwise.  */
344

Tom Tromey's avatar
Tom Tromey committed
345
static int
346
jit_read_descriptor (struct gdbarch *gdbarch,
347
		     struct jit_descriptor *descriptor,
348
		     struct jit_program_space_data *ps_data)
349
350
351
352
353
354
{
  int err;
  struct type *ptr_type;
  int ptr_size;
  int desc_size;
  gdb_byte *desc_buf;
355
  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
Tom Tromey's avatar
Tom Tromey committed
356
357
  struct jit_objfile_data *objf_data;

358
  if (ps_data->objfile == NULL)
Tom Tromey's avatar
Tom Tromey committed
359
    return 0;
360
  objf_data = get_jit_objfile_data (ps_data->objfile);
Tom Tromey's avatar
Tom Tromey committed
361
362
363
364
365
366
  if (objf_data->descriptor == NULL)
    return 0;

  if (jit_debug)
    fprintf_unfiltered (gdb_stdlog,
			"jit_read_descriptor, descriptor_addr = %s\n",
367
368
			paddress (gdbarch, MSYMBOL_VALUE_ADDRESS (ps_data->objfile,
								  objf_data->descriptor)));
369
370

  /* Figure out how big the descriptor is on the remote and how to read it.  */
371
  ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
372
373
  ptr_size = TYPE_LENGTH (ptr_type);
  desc_size = 8 + 2 * ptr_size;  /* Two 32-bit ints and two pointers.  */
374
  desc_buf = (gdb_byte *) alloca (desc_size);
375
376

  /* Read the descriptor.  */
377
378
  err = target_read_memory (MSYMBOL_VALUE_ADDRESS (ps_data->objfile,
						   objf_data->descriptor),
Tom Tromey's avatar
Tom Tromey committed
379
			    desc_buf, desc_size);
380
  if (err)
Tom Tromey's avatar
Tom Tromey committed
381
382
383
384
385
    {
      printf_unfiltered (_("Unable to read JIT descriptor from "
			   "remote memory\n"));
      return 0;
    }
386
387
388
389
390
391
392
393

  /* Fix the endianness to match the host.  */
  descriptor->version = extract_unsigned_integer (&desc_buf[0], 4, byte_order);
  descriptor->action_flag =
      extract_unsigned_integer (&desc_buf[4], 4, byte_order);
  descriptor->relevant_entry = extract_typed_address (&desc_buf[8], ptr_type);
  descriptor->first_entry =
      extract_typed_address (&desc_buf[8 + ptr_size], ptr_type);
Tom Tromey's avatar
Tom Tromey committed
394
395

  return 1;
396
397
398
399
400
}

/* Helper function for reading a JITed code entry from remote memory.  */

static void
401
402
jit_read_code_entry (struct gdbarch *gdbarch,
		     CORE_ADDR code_addr, struct jit_code_entry *code_entry)
403
{
404
  int err, off;
405
406
407
  struct type *ptr_type;
  int ptr_size;
  int entry_size;
408
  int align_bytes;
409
  gdb_byte *entry_buf;
410
  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
411
412

  /* Figure out how big the entry is on the remote and how to read it.  */
413
  ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
414
  ptr_size = TYPE_LENGTH (ptr_type);
415

416
417
  /* Figure out where the uint64_t value will be.  */
  align_bytes = type_align (builtin_type (gdbarch)->builtin_uint64);
418
419
420
421
  off = 3 * ptr_size;
  off = (off + (align_bytes - 1)) & ~(align_bytes - 1);

  entry_size = off + 8;  /* Three pointers and one 64-bit int.  */
422
  entry_buf = (gdb_byte *) alloca (entry_size);
423
424
425
426
427
428
429

  /* Read the entry.  */
  err = target_read_memory (code_addr, entry_buf, entry_size);
  if (err)
    error (_("Unable to read JIT code entry from remote memory!"));

  /* Fix the endianness to match the host.  */
430
  ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
431
432
433
434
435
436
  code_entry->next_entry = extract_typed_address (&entry_buf[0], ptr_type);
  code_entry->prev_entry =
      extract_typed_address (&entry_buf[ptr_size], ptr_type);
  code_entry->symfile_addr =
      extract_typed_address (&entry_buf[2 * ptr_size], ptr_type);
  code_entry->symfile_size =
437
      extract_unsigned_integer (&entry_buf[off], 8, byte_order);
438
439
}

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* Proxy object for building a block.  */

struct gdb_block
{
  /* gdb_blocks are linked into a tree structure.  Next points to the
     next node at the same depth as this block and parent to the
     parent gdb_block.  */
  struct gdb_block *next, *parent;

  /* Points to the "real" block that is being built out of this
     instance.  This block will be added to a blockvector, which will
     then be added to a symtab.  */
  struct block *real_block;

  /* The first and last code address corresponding to this block.  */
  CORE_ADDR begin, end;

  /* The name of this block (if any).  If this is non-NULL, the
     FUNCTION symbol symbol is set to this value.  */
  const char *name;
};

/* Proxy object for building a symtab.  */

struct gdb_symtab
{
  /* The list of blocks in this symtab.  These will eventually be
     converted to real blocks.  */
  struct gdb_block *blocks;

  /* The number of blocks inserted.  */
  int nblocks;

  /* A mapping between line numbers to PC.  */
  struct linetable *linetable;

  /* The source file for this symtab.  */
  const char *file_name;
  struct gdb_symtab *next;
};

/* Proxy object for building an object.  */

struct gdb_object
{
  struct gdb_symtab *symtabs;
};

/* The type of the `private' data passed around by the callback
   functions.  */

typedef CORE_ADDR jit_dbg_reader_data;

/* The reader calls into this function to read data off the targets
   address space.  */

static enum gdb_status
jit_target_read_impl (GDB_CORE_ADDR target_mem, void *gdb_buf, int len)
{
499
500
  int result = target_read_memory ((CORE_ADDR) target_mem,
				   (gdb_byte *) gdb_buf, len);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  if (result == 0)
    return GDB_SUCCESS;
  else
    return GDB_FAIL;
}

/* The reader calls into this function to create a new gdb_object
   which it can then pass around to the other callbacks.  Right now,
   all that is required is allocating the memory.  */

static struct gdb_object *
jit_object_open_impl (struct gdb_symbol_callbacks *cb)
{
  /* CB is not required right now, but sometime in the future we might
     need a handle to it, and we'd like to do that without breaking
     the ABI.  */
Tom Tromey's avatar
Tom Tromey committed
517
  return XCNEW (struct gdb_object);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
}

/* Readers call into this function to open a new gdb_symtab, which,
   again, is passed around to other callbacks.  */

static struct gdb_symtab *
jit_symtab_open_impl (struct gdb_symbol_callbacks *cb,
                      struct gdb_object *object,
                      const char *file_name)
{
  struct gdb_symtab *ret;

  /* CB stays unused.  See comment in jit_object_open_impl.  */

Tom Tromey's avatar
Tom Tromey committed
532
  ret = XCNEW (struct gdb_symtab);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
533
534
535
536
537
538
539
540
541
542
543
  ret->file_name = file_name ? xstrdup (file_name) : xstrdup ("");
  ret->next = object->symtabs;
  object->symtabs = ret;
  return ret;
}

/* Returns true if the block corresponding to old should be placed
   before the block corresponding to new in the final blockvector.  */

static int
compare_block (const struct gdb_block *const old,
544
               const struct gdb_block *const newobj)
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
545
546
547
{
  if (old == NULL)
    return 1;
548
  if (old->begin < newobj->begin)
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
549
    return 1;
550
  else if (old->begin == newobj->begin)
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
551
    {
552
      if (old->end > newobj->end)
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
        return 1;
      else
        return 0;
    }
  else
    return 0;
}

/* Called by readers to open a new gdb_block.  This function also
   inserts the new gdb_block in the correct place in the corresponding
   gdb_symtab.  */

static struct gdb_block *
jit_block_open_impl (struct gdb_symbol_callbacks *cb,
                     struct gdb_symtab *symtab, struct gdb_block *parent,
                     GDB_CORE_ADDR begin, GDB_CORE_ADDR end, const char *name)
{
Tom Tromey's avatar
Tom Tromey committed
570
  struct gdb_block *block = XCNEW (struct gdb_block);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606

  block->next = symtab->blocks;
  block->begin = (CORE_ADDR) begin;
  block->end = (CORE_ADDR) end;
  block->name = name ? xstrdup (name) : NULL;
  block->parent = parent;

  /* Ensure that the blocks are inserted in the correct (reverse of
     the order expected by blockvector).  */
  if (compare_block (symtab->blocks, block))
    {
      symtab->blocks = block;
    }
  else
    {
      struct gdb_block *i = symtab->blocks;

      for (;; i = i->next)
        {
          /* Guaranteed to terminate, since compare_block (NULL, _)
             returns 1.  */
          if (compare_block (i->next, block))
            {
              block->next = i->next;
              i->next = block;
              break;
            }
        }
    }
  symtab->nblocks++;

  return block;
}

/* Readers call this to add a line mapping (from PC to line number) to
   a gdb_symtab.  */
607
608

static void
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
609
610
611
612
613
jit_symtab_line_mapping_add_impl (struct gdb_symbol_callbacks *cb,
                                  struct gdb_symtab *stab, int nlines,
                                  struct gdb_line_mapping *map)
{
  int i;
614
  int alloc_len;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
615
616
617
618

  if (nlines < 1)
    return;

619
620
621
  alloc_len = sizeof (struct linetable)
	      + (nlines - 1) * sizeof (struct linetable_entry);
  stab->linetable = (struct linetable *) xmalloc (alloc_len);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
  stab->linetable->nitems = nlines;
  for (i = 0; i < nlines; i++)
    {
      stab->linetable->item[i].pc = (CORE_ADDR) map[i].pc;
      stab->linetable->item[i].line = map[i].line;
    }
}

/* Called by readers to close a gdb_symtab.  Does not need to do
   anything as of now.  */

static void
jit_symtab_close_impl (struct gdb_symbol_callbacks *cb,
                       struct gdb_symtab *stab)
{
  /* Right now nothing needs to be done here.  We may need to do some
     cleanup here in the future (again, without breaking the plugin
     ABI).  */
}

/* Transform STAB to a proper symtab, and add it it OBJFILE.  */

static void
finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
{
647
  struct compunit_symtab *cust;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
648
649
  struct gdb_block *gdb_block_iter, *gdb_block_iter_tmp;
  struct block *block_iter;
Alan Modra's avatar
Alan Modra committed
650
651
  int actual_nblocks, i;
  size_t blockvector_size;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
652
  CORE_ADDR begin, end;
Tom Tromey's avatar
Tom Tromey committed
653
  struct blockvector *bv;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
654
655
656

  actual_nblocks = FIRST_LOCAL_BLOCK + stab->nblocks;

657
658
659
660
  cust = allocate_compunit_symtab (objfile, stab->file_name);
  allocate_symtab (cust, stab->file_name);
  add_compunit_symtab_to_objfile (cust);

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
661
  /* JIT compilers compile in memory.  */
662
  COMPUNIT_DIRNAME (cust) = NULL;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
663
664
665
666

  /* Copy over the linetable entry if one was provided.  */
  if (stab->linetable)
    {
Alan Modra's avatar
Alan Modra committed
667
668
669
      size_t size = ((stab->linetable->nitems - 1)
		     * sizeof (struct linetable_entry)
		     + sizeof (struct linetable));
670
      SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust))
671
	= (struct linetable *) obstack_alloc (&objfile->objfile_obstack, size);
672
673
      memcpy (SYMTAB_LINETABLE (COMPUNIT_FILETABS (cust)), stab->linetable,
	      size);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
674
675
676
677
    }

  blockvector_size = (sizeof (struct blockvector)
                      + (actual_nblocks - 1) * sizeof (struct block *));
678
679
  bv = (struct blockvector *) obstack_alloc (&objfile->objfile_obstack,
					     blockvector_size);
680
  COMPUNIT_BLOCKVECTOR (cust) = bv;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
681
682
683

  /* (begin, end) will contain the PC range this entire blockvector
     spans.  */
Tom Tromey's avatar
Tom Tromey committed
684
  BLOCKVECTOR_MAP (bv) = NULL;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
685
686
  begin = stab->blocks->begin;
  end = stab->blocks->end;
Tom Tromey's avatar
Tom Tromey committed
687
  BLOCKVECTOR_NBLOCKS (bv) = actual_nblocks;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
688
689
690
691
692
693
694
695
696

  /* First run over all the gdb_block objects, creating a real block
     object for each.  Simultaneously, keep setting the real_block
     fields.  */
  for (i = (actual_nblocks - 1), gdb_block_iter = stab->blocks;
       i >= FIRST_LOCAL_BLOCK;
       i--, gdb_block_iter = gdb_block_iter->next)
    {
      struct block *new_block = allocate_block (&objfile->objfile_obstack);
697
      struct symbol *block_name = allocate_symbol (objfile);
698
699
      struct type *block_type = arch_type (get_objfile_arch (objfile),
					   TYPE_CODE_VOID,
700
					   TARGET_CHAR_BIT,
701
					   "void");
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
702

703
704
      BLOCK_MULTIDICT (new_block)
	= mdict_create_linear (&objfile->objfile_obstack, NULL);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
705
706
707
708
709
710
      /* The address range.  */
      BLOCK_START (new_block) = (CORE_ADDR) gdb_block_iter->begin;
      BLOCK_END (new_block) = (CORE_ADDR) gdb_block_iter->end;

      /* The name.  */
      SYMBOL_DOMAIN (block_name) = VAR_DOMAIN;
711
      SYMBOL_ACLASS_INDEX (block_name) = LOC_BLOCK;
712
      symbol_set_symtab (block_name, COMPUNIT_FILETABS (cust));
713
      SYMBOL_TYPE (block_name) = lookup_function_type (block_type);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
714
715
      SYMBOL_BLOCK_VALUE (block_name) = new_block;

716
717
718
719
      block_name->ginfo.name
	= (const char *) obstack_copy0 (&objfile->objfile_obstack,
					gdb_block_iter->name,
					strlen (gdb_block_iter->name));
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
720
721
722

      BLOCK_FUNCTION (new_block) = block_name;

Tom Tromey's avatar
Tom Tromey committed
723
      BLOCKVECTOR_BLOCK (bv, i) = new_block;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
724
725
726
727
728
729
730
731
732
733
734
735
      if (begin > BLOCK_START (new_block))
        begin = BLOCK_START (new_block);
      if (end < BLOCK_END (new_block))
        end = BLOCK_END (new_block);

      gdb_block_iter->real_block = new_block;
    }

  /* Now add the special blocks.  */
  block_iter = NULL;
  for (i = 0; i < FIRST_LOCAL_BLOCK; i++)
    {
736
737
738
739
740
      struct block *new_block;

      new_block = (i == GLOBAL_BLOCK
		   ? allocate_global_block (&objfile->objfile_obstack)
		   : allocate_block (&objfile->objfile_obstack));
741
742
      BLOCK_MULTIDICT (new_block)
	= mdict_create_linear (&objfile->objfile_obstack, NULL);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
743
744
745
746
747
748
      BLOCK_SUPERBLOCK (new_block) = block_iter;
      block_iter = new_block;

      BLOCK_START (new_block) = (CORE_ADDR) begin;
      BLOCK_END (new_block) = (CORE_ADDR) end;

Tom Tromey's avatar
Tom Tromey committed
749
      BLOCKVECTOR_BLOCK (bv, i) = new_block;
750
751

      if (i == GLOBAL_BLOCK)
752
	set_block_compunit_symtab (new_block, cust);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
753
754
755
756
757
758
759
760
761
    }

  /* Fill up the superblock fields for the real blocks, using the
     real_block fields populated earlier.  */
  for (gdb_block_iter = stab->blocks;
       gdb_block_iter;
       gdb_block_iter = gdb_block_iter->next)
    {
      if (gdb_block_iter->parent != NULL)
Sanjoy Das's avatar
Sanjoy Das committed
762
763
764
765
766
767
768
769
770
771
	{
	  /* If the plugin specifically mentioned a parent block, we
	     use that.  */
	  BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
	    gdb_block_iter->parent->real_block;
	}
      else
	{
	  /* And if not, we set a default parent block.  */
	  BLOCK_SUPERBLOCK (gdb_block_iter->real_block) =
Tom Tromey's avatar
Tom Tromey committed
772
	    BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
Sanjoy Das's avatar
Sanjoy Das committed
773
	}
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
    }

  /* Free memory.  */
  gdb_block_iter = stab->blocks;

  for (gdb_block_iter = stab->blocks, gdb_block_iter_tmp = gdb_block_iter->next;
       gdb_block_iter;
       gdb_block_iter = gdb_block_iter_tmp)
    {
      xfree ((void *) gdb_block_iter->name);
      xfree (gdb_block_iter);
    }
  xfree (stab->linetable);
  xfree ((char *) stab->file_name);
  xfree (stab);
}

/* Called when closing a gdb_objfile.  Converts OBJ to a proper
   objfile.  */

static void
jit_object_close_impl (struct gdb_symbol_callbacks *cb,
                       struct gdb_object *obj)
{
  struct gdb_symtab *i, *j;
  struct objfile *objfile;
  jit_dbg_reader_data *priv_data;

Simon Marchi's avatar
Simon Marchi committed
802
  priv_data = (jit_dbg_reader_data *) cb->priv_data;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
803

Tom Tromey's avatar
Tom Tromey committed
804
805
  objfile = new struct objfile (NULL, "<< JIT compiled code >>",
				OBJF_NOT_FILENAME);
806
  objfile->per_bfd->gdbarch = target_gdbarch ();
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
807

808
  terminate_minimal_symbol_table (objfile);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
809
810
811
812
813
814
815
816
817
818
819

  j = NULL;
  for (i = obj->symtabs; i; i = j)
    {
      j = i->next;
      finalize_symtab (i, objfile);
    }
  add_objfile_entry (objfile, *priv_data);
  xfree (obj);
}

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
820
/* Try to read CODE_ENTRY using the loaded jit reader (if any).
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
821
822
   ENTRY_ADDR is the address of the struct jit_code_entry in the
   inferior address space.  */
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
823
824

static int
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
825
826
jit_reader_try_read_symtab (struct jit_code_entry *code_entry,
                            CORE_ADDR entry_addr)
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
827
{
Simon Marchi's avatar
Simon Marchi committed
828
  gdb_byte *gdb_mem;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
  int status;
  jit_dbg_reader_data priv_data;
  struct gdb_reader_funcs *funcs;
  struct gdb_symbol_callbacks callbacks =
    {
      jit_object_open_impl,
      jit_symtab_open_impl,
      jit_block_open_impl,
      jit_symtab_close_impl,
      jit_object_close_impl,

      jit_symtab_line_mapping_add_impl,
      jit_target_read_impl,

      &priv_data
    };

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
846
  priv_data = entry_addr;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
847
848
849
850

  if (!loaded_jit_reader)
    return 0;

Simon Marchi's avatar
Simon Marchi committed
851
  gdb_mem = (gdb_byte *) xmalloc (code_entry->symfile_size);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
852
853

  status = 1;
Pedro Alves's avatar
Pedro Alves committed
854
855
856
857
858
859
860
861
  TRY
    {
      if (target_read_memory (code_entry->symfile_addr, gdb_mem,
			      code_entry->symfile_size))
	status = 0;
    }
  CATCH (e, RETURN_MASK_ALL)
    {
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
862
      status = 0;
Pedro Alves's avatar
Pedro Alves committed
863
864
    }
  END_CATCH
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880

  if (status)
    {
      funcs = loaded_jit_reader->functions;
      if (funcs->read (funcs, &callbacks, gdb_mem, code_entry->symfile_size)
          != GDB_SUCCESS)
        status = 0;
    }

  xfree (gdb_mem);
  if (jit_debug && status == 0)
    fprintf_unfiltered (gdb_stdlog,
                        "Could not read symtab using the loaded JIT reader.\n");
  return status;
}

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
881
/* Try to read CODE_ENTRY using BFD.  ENTRY_ADDR is the address of the
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
882
   struct jit_code_entry in the inferior address space.  */
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
883
884
885

static void
jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
886
                         CORE_ADDR entry_addr,
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
887
                         struct gdbarch *gdbarch)
888
889
890
891
892
{
  struct bfd_section *sec;
  struct objfile *objfile;
  const struct bfd_arch_info *b;

Paul Pluzhnikov's avatar
Paul Pluzhnikov committed
893
894
895
896
897
898
899
  if (jit_debug)
    fprintf_unfiltered (gdb_stdlog,
			"jit_register_code, symfile_addr = %s, "
			"symfile_size = %s\n",
			paddress (gdbarch, code_entry->symfile_addr),
			pulongest (code_entry->symfile_size));

900
901
902
  gdb_bfd_ref_ptr nbfd (bfd_open_from_target_memory (code_entry->symfile_addr,
						     code_entry->symfile_size,
						     gnutarget));
Jan Kratochvil's avatar
gdb/    
Jan Kratochvil committed
903
904
905
906
907
  if (nbfd == NULL)
    {
      puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
      return;
    }
908
909
910

  /* Check the format.  NOTE: This initializes important data that GDB uses!
     We would segfault later without this line.  */
911
  if (!bfd_check_format (nbfd.get (), bfd_object))
912
913
914
915
916
917
918
    {
      printf_unfiltered (_("\
JITed symbol file is not an object file, ignoring it.\n"));
      return;
    }

  /* Check bfd arch.  */
919
  b = gdbarch_bfd_arch_info (gdbarch);
920
  if (b->compatible (b, bfd_get_arch_info (nbfd.get ())) != b)
921
    warning (_("JITed object file architecture %s is not compatible "
922
923
924
               "with target architecture %s."),
	     bfd_get_arch_info (nbfd.get ())->printable_name,
	     b->printable_name);
925
926
927
928

  /* Read the section address information out of the symbol file.  Since the
     file is generated by the JIT at runtime, it should all of the absolute
     addresses that we care about.  */
929
  section_addr_info sai;
930
  for (sec = nbfd->sections; sec != NULL; sec = sec->next)
931
    if ((bfd_get_section_flags (nbfd.get (), sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
932
933
934
      {
        /* We assume that these virtual addresses are absolute, and do not
           treat them as offsets.  */
935
936
937
	sai.emplace_back (bfd_get_section_vma (nbfd.get (), sec),
			  bfd_get_section_name (nbfd.get (), sec),
			  sec->index);
938
939
      }

940
  /* This call does not take ownership of SAI.  */
941
  objfile = symbol_file_add_from_bfd (nbfd.get (),
942
943
				      bfd_get_filename (nbfd.get ()), 0,
				      &sai,
Jan Kratochvil's avatar
Jan Kratochvil committed
944
				      OBJF_SHARED | OBJF_NOT_FILENAME, NULL);
945

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
946
  add_objfile_entry (objfile, entry_addr);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
947
948
949
950
951
952
953
954
955
956
957
}

/* This function registers code associated with a JIT code entry.  It uses the
   pointer and size pair in the entry to read the symbol file from the remote
   and then calls symbol_file_add_from_local_memory to add it as though it were
   a symbol file added by the user.  */

static void
jit_register_code (struct gdbarch *gdbarch,
                   CORE_ADDR entry_addr, struct jit_code_entry *code_entry)
{
958
  int success;
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
959
960
961
962
963
964
965
966

  if (jit_debug)
    fprintf_unfiltered (gdb_stdlog,
                        "jit_register_code, symfile_addr = %s, "
                        "symfile_size = %s\n",
                        paddress (gdbarch, code_entry->symfile_addr),
                        pulongest (code_entry->symfile_size));

Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
967
  success = jit_reader_try_read_symtab (code_entry, entry_addr);
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
968
969

  if (!success)
Sanjoy Das's avatar
gdb/    
Sanjoy Das committed
970
    jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch);
971
972
}

973
974
/* This function unregisters JITed code and frees the corresponding
   objfile.  */
975
976
977
978

static void
jit_unregister_code (struct objfile *objfile)
{
Tom Tromey's avatar
Tom Tromey committed
979
  delete objfile;
980
981
982
983
984
985
986
}

/* Look up the objfile with this code entry address.  */

static struct objfile *
jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
{
987
  for (objfile *objf : current_program_space->objfiles ())
988
    {
Tom Tromey's avatar
Tom Tromey committed
989
990
      struct jit_objfile_data *objf_data;

Simon Marchi's avatar
Simon Marchi committed
991
992
      objf_data
	= (struct jit_objfile_data *) objfile_data (objf, jit_objfile_data);
Tom Tromey's avatar
Tom Tromey committed
993
      if (objf_data != NULL && objf_data->addr == entry_addr)
994
995
996
997
998
        return objf;
    }
  return NULL;
}

Tom Tromey's avatar
Tom Tromey committed
999
1000
/* This is called when a breakpoint is deleted.  It updates the
   inferior's cache, if needed.  */
For faster browsing, not all history is shown. View entire blame