gdb: add inferior-specific breakpoints
[binutils-gdb.git] / gdb / guile / scm-block.c
index 05263a4202bfa2f5672bfa8d751e6d6ab09f9fbd..02b7a48be37648f6320411e3ec27ba3d158bfc95 100644 (file)
@@ -1,6 +1,6 @@
 /* Scheme interface to blocks.
 
-   Copyright (C) 2008-2021 Free Software Foundation, Inc.
+   Copyright (C) 2008-2023 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -76,7 +76,35 @@ static scm_t_bits block_syms_progress_smob_tag;
 /* The "next!" block syms iterator method.  */
 static SCM bkscm_next_symbol_x_proc;
 
-static const struct objfile_data *bkscm_objfile_data_key;
+/* This is called when an objfile is about to be freed.
+   Invalidate the block as further actions on the block would result
+   in bad data.  All access to b_smob->block should be gated by
+   checks to ensure the block is (still) valid.  */
+struct bkscm_deleter
+{
+  /* Helper function for bkscm_del_objfile_blocks to mark the block
+     as invalid.  */
+
+  static int
+  bkscm_mark_block_invalid (void **slot, void *info)
+  {
+    block_smob *b_smob = (block_smob *) *slot;
+
+    b_smob->block = NULL;
+    b_smob->objfile = NULL;
+    return 1;
+  }
+
+  void operator() (htab_t htab)
+  {
+    gdb_assert (htab != nullptr);
+    htab_traverse_noresize (htab, bkscm_mark_block_invalid, NULL);
+    htab_delete (htab);
+  }
+};
+
+static const registry<objfile>::key<htab, bkscm_deleter>
+     bkscm_objfile_data_key;
 \f
 /* Administrivia for block smobs.  */
 
@@ -108,13 +136,13 @@ bkscm_eq_block_smob (const void *ap, const void *bp)
 static htab_t
 bkscm_objfile_block_map (struct objfile *objfile)
 {
-  htab_t htab = (htab_t) objfile_data (objfile, bkscm_objfile_data_key);
+  htab_t htab = bkscm_objfile_data_key.get (objfile);
 
   if (htab == NULL)
     {
       htab = gdbscm_create_eqable_gsmob_ptr_map (bkscm_hash_block_smob,
                                                 bkscm_eq_block_smob);
-      set_objfile_data (objfile, bkscm_objfile_data_key, htab);
+      bkscm_objfile_data_key.set (objfile, htab);
     }
 
   return htab;
@@ -151,16 +179,16 @@ bkscm_print_block_smob (SCM self, SCM port, scm_print_state *pstate)
 
   gdbscm_printf (port, "#<%s", block_smob_name);
 
-  if (BLOCK_SUPERBLOCK (b) == NULL)
+  if (b->superblock () == NULL)
     gdbscm_printf (port, " global");
-  else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (b)) == NULL)
+  else if (b->superblock ()->superblock () == NULL)
     gdbscm_printf (port, " static");
 
-  if (BLOCK_FUNCTION (b) != NULL)
-    gdbscm_printf (port, " %s", BLOCK_FUNCTION (b)->print_name ());
+  if (b->function () != NULL)
+    gdbscm_printf (port, " %s", b->function ()->print_name ());
 
   gdbscm_printf (port, " %s-%s",
-                hex_string (BLOCK_START (b)), hex_string (BLOCK_END (b)));
+                hex_string (b->start ()), hex_string (b->end ()));
 
   scm_puts (">", port);
 
@@ -326,35 +354,6 @@ bkscm_scm_to_block (SCM block_scm, int arg_pos, const char *func_name,
   return NULL;
 }
 
-/* Helper function for bkscm_del_objfile_blocks to mark the block
-   as invalid.  */
-
-static int
-bkscm_mark_block_invalid (void **slot, void *info)
-{
-  block_smob *b_smob = (block_smob *) *slot;
-
-  b_smob->block = NULL;
-  b_smob->objfile = NULL;
-  return 1;
-}
-
-/* This function is called when an objfile is about to be freed.
-   Invalidate the block as further actions on the block would result
-   in bad data.  All access to b_smob->block should be gated by
-   checks to ensure the block is (still) valid.  */
-
-static void
-bkscm_del_objfile_blocks (struct objfile *objfile, void *datum)
-{
-  htab_t htab = (htab_t) datum;
-
-  if (htab != NULL)
-    {
-      htab_traverse_noresize (htab, bkscm_mark_block_invalid, NULL);
-      htab_delete (htab);
-    }
-}
 \f
 /* Block methods.  */
 
@@ -379,7 +378,7 @@ gdbscm_block_start (SCM self)
     = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
   const struct block *block = b_smob->block;
 
-  return gdbscm_scm_from_ulongest (BLOCK_START (block));
+  return gdbscm_scm_from_ulongest (block->start ());
 }
 
 /* (block-end <gdb:block>) -> address */
@@ -391,7 +390,7 @@ gdbscm_block_end (SCM self)
     = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
   const struct block *block = b_smob->block;
 
-  return gdbscm_scm_from_ulongest (BLOCK_END (block));
+  return gdbscm_scm_from_ulongest (block->end ());
 }
 
 /* (block-function <gdb:block>) -> <gdb:symbol> */
@@ -404,7 +403,7 @@ gdbscm_block_function (SCM self)
   const struct block *block = b_smob->block;
   struct symbol *sym;
 
-  sym = BLOCK_FUNCTION (block);
+  sym = block->function ();
 
   if (sym != NULL)
     return syscm_scm_from_symbol (sym);
@@ -421,7 +420,7 @@ gdbscm_block_superblock (SCM self)
   const struct block *block = b_smob->block;
   const struct block *super_block;
 
-  super_block = BLOCK_SUPERBLOCK (block);
+  super_block = block->superblock ();
 
   if (super_block)
     return bkscm_scm_from_block (super_block, b_smob->objfile);
@@ -439,7 +438,7 @@ gdbscm_block_global_block (SCM self)
   const struct block *block = b_smob->block;
   const struct block *global_block;
 
-  global_block = block_global_block (block);
+  global_block = block->global_block ();
 
   return bkscm_scm_from_block (global_block, b_smob->objfile);
 }
@@ -456,10 +455,10 @@ gdbscm_block_static_block (SCM self)
   const struct block *block = b_smob->block;
   const struct block *static_block;
 
-  if (BLOCK_SUPERBLOCK (block) == NULL)
+  if (block->superblock () == NULL)
     return SCM_BOOL_F;
 
-  static_block = block_static_block (block);
+  static_block = block->static_block ();
 
   return bkscm_scm_from_block (static_block, b_smob->objfile);
 }
@@ -474,7 +473,7 @@ gdbscm_block_global_p (SCM self)
     = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
   const struct block *block = b_smob->block;
 
-  return scm_from_bool (BLOCK_SUPERBLOCK (block) == NULL);
+  return scm_from_bool (block->superblock () == NULL);
 }
 
 /* (block-static? <gdb:block>) -> boolean
@@ -487,8 +486,8 @@ gdbscm_block_static_p (SCM self)
     = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
   const struct block *block = b_smob->block;
 
-  if (BLOCK_SUPERBLOCK (block) != NULL
-      && BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
+  if (block->superblock () != NULL
+      && block->superblock ()->superblock () == NULL)
     return SCM_BOOL_T;
   return SCM_BOOL_F;
 }
@@ -502,20 +501,15 @@ gdbscm_block_symbols (SCM self)
   block_smob *b_smob
     = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
   const struct block *block = b_smob->block;
-  struct block_iterator iter;
-  struct symbol *sym;
   SCM result;
 
   result = SCM_EOL;
 
-  sym = block_iterator_first (block, &iter);
-
-  while (sym != NULL)
+  for (struct symbol *sym : block_iterator_range (block))
     {
       SCM s_scm = syscm_scm_from_symbol (sym);
 
       result = scm_cons (s_scm, result);
-      sym = block_iterator_next (&iter);
     }
 
   return scm_reverse_x (result, SCM_EOL);
@@ -554,7 +548,7 @@ bkscm_print_block_syms_progress_smob (SCM self, SCM port,
                    : i_smob->iter.d.compunit_symtab->includes[i_smob->iter.idx]);
            gdbscm_printf (port, " %s",
                           symtab_to_filename_for_display
-                            (compunit_primary_filetab (cust)));
+                            (cust->primary_filetab ()));
            break;
          }
        case FIRST_LOCAL_BLOCK:
@@ -685,7 +679,7 @@ gdbscm_lookup_block (SCM pc_scm)
     {
       cust = find_pc_compunit_symtab (pc);
 
-      if (cust != NULL && COMPUNIT_OBJFILE (cust) != NULL)
+      if (cust != NULL && cust->objfile () != NULL)
        block = block_for_pc (pc);
     }
   catch (const gdb_exception &except)
@@ -694,14 +688,14 @@ gdbscm_lookup_block (SCM pc_scm)
     }
 
   GDBSCM_HANDLE_GDB_EXCEPTION (exc);
-  if (cust == NULL || COMPUNIT_OBJFILE (cust) == NULL)
+  if (cust == NULL || cust->objfile () == NULL)
     {
       gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, pc_scm,
                                 _("cannot locate object file for block"));
     }
 
   if (block != NULL)
-    return bkscm_scm_from_block (block, COMPUNIT_OBJFILE (cust));
+    return bkscm_scm_from_block (block, cust->objfile ());
   return SCM_BOOL_F;
 }
 \f
@@ -799,9 +793,4 @@ gdbscm_initialize_blocks (void)
                                gdbscm_documentation_symbol,
                                gdbscm_scm_from_c_string ("\
 Internal function to assist the block symbols iterator."));
-
-  /* Register an objfile "free" callback so we can properly
-     invalidate blocks when an object file is about to be deleted.  */
-  bkscm_objfile_data_key
-    = register_objfile_data_with_cleanup (NULL, bkscm_del_objfile_blocks);
 }