gdb/netbsd: add missing header file
[binutils-gdb.git] / gdb / dcache.c
index 20b781fa015f4cfcda410a1444166e937fe4213d..751c24fca0b87cf03e57ceec6ea3523a4c6570ad 100644 (file)
@@ -1,6 +1,6 @@
 /* Caching code for GDB, the GNU debugger.
 
-   Copyright (C) 1992-2018 Free Software Foundation, Inc.
+   Copyright (C) 1992-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -24,6 +24,7 @@
 #include "target-dcache.h"
 #include "inferior.h"
 #include "splay-tree.h"
+#include "gdbarch.h"
 
 /* Commands with a prefix of `{set,show} dcache'.  */
 static struct cmd_list_element *dcache_set_list = NULL;
@@ -65,7 +66,7 @@ static struct cmd_list_element *dcache_show_list = NULL;
    is set, etc., then the chunk is skipped.  Those chunks are handled
    in target_xfer_memory() (or target_xfer_memory_partial()).
 
-   This doesn't occur very often.  The most common occurance is when
+   This doesn't occur very often.  The most common occurrence is when
    the last bit of the .text segment and the first bit of the .data
    segment fall within the same dcache page with a ro/cacheable memory
    region defined for the .text segment and a rw/non-cacheable memory
@@ -115,6 +116,10 @@ struct dcache_struct
 
   /* The ptid of last inferior to use cache or null_ptid.  */
   ptid_t ptid;
+
+  /* The process target of last inferior to use the cache or
+     nullptr.  */
+  process_stratum_target *proc_target;
 };
 
 typedef void (block_func) (struct dcache_block *block, void *param);
@@ -125,13 +130,13 @@ static int dcache_read_line (DCACHE *dcache, struct dcache_block *db);
 
 static struct dcache_block *dcache_alloc (DCACHE *dcache, CORE_ADDR addr);
 
-static int dcache_enabled_p = 0; /* OBSOLETE */
+static bool dcache_enabled_p = false; /* OBSOLETE */
 
 static void
 show_dcache_enabled_p (struct ui_file *file, int from_tty,
                       struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file, _("Deprecated remotecache flag is %s.\n"), value);
+  gdb_printf (file, _("Deprecated remotecache flag is %s.\n"), value);
 }
 
 /* Add BLOCK to circular block list BLIST, behind the block at *BLIST.
@@ -248,6 +253,7 @@ dcache_invalidate (DCACHE *dcache)
   dcache->oldest = NULL;
   dcache->size = 0;
   dcache->ptid = null_ptid;
+  dcache->proc_target = nullptr;
 
   if (dcache->line_size != dcache_line_size)
     {
@@ -323,7 +329,7 @@ dcache_read_line (DCACHE *dcache, struct dcache_block *db)
        reg_len = region->hi - memaddr;
 
       /* Skip non-readable regions.  The cache attribute can be ignored,
-         since we may be loading this for a stack access.  */
+        since we may be loading this for a stack access.  */
       if (region->attrib.mode == MEM_WO)
        {
          memaddr += reg_len;
@@ -400,7 +406,7 @@ dcache_peek_byte (DCACHE *dcache, CORE_ADDR addr, gdb_byte *ptr)
       db = dcache_alloc (dcache, addr);
 
       if (!dcache_read_line (dcache, db))
-         return 0;
+        return 0;
     }
 
   *ptr = db->data[XFORM (dcache, addr)];
@@ -452,6 +458,7 @@ dcache_init (void)
   dcache->size = 0;
   dcache->line_size = dcache_line_size;
   dcache->ptid = null_ptid;
+  dcache->proc_target = nullptr;
 
   return dcache;
 }
@@ -469,13 +476,15 @@ dcache_read_memory_partial (struct target_ops *ops, DCACHE *dcache,
 {
   ULONGEST i;
 
-  /* If this is a different inferior from what we've recorded,
-     flush the cache.  */
+  /* If this is a different thread from what we've recorded, flush the
+     cache.  */
 
-  if (! ptid_equal (inferior_ptid, dcache->ptid))
+  process_stratum_target *proc_target = current_inferior ()->process_target ();
+  if (proc_target != dcache->proc_target || inferior_ptid != dcache->ptid)
     {
       dcache_invalidate (dcache);
       dcache->ptid = inferior_ptid;
+      dcache->proc_target = proc_target;
     }
 
   for (i = 0; i < len; i++)
@@ -545,7 +554,7 @@ dcache_print_line (DCACHE *dcache, int index)
 
   if (dcache == NULL)
     {
-      printf_filtered (_("No data cache available.\n"));
+      gdb_printf (_("No data cache available.\n"));
       return;
     }
 
@@ -560,24 +569,24 @@ dcache_print_line (DCACHE *dcache, int index)
 
   if (!n)
     {
-      printf_filtered (_("No such cache line exists.\n"));
+      gdb_printf (_("No such cache line exists.\n"));
       return;
     }
     
   db = (struct dcache_block *) n->value;
 
-  printf_filtered (_("Line %d: address %s [%d hits]\n"),
-                  index, paddress (target_gdbarch (), db->addr), db->refs);
+  gdb_printf (_("Line %d: address %s [%d hits]\n"),
+             index, paddress (target_gdbarch (), db->addr), db->refs);
 
   for (j = 0; j < dcache->line_size; j++)
     {
-      printf_filtered ("%02x ", db->data[j]);
+      gdb_printf ("%02x ", db->data[j]);
 
       /* Print a newline every 16 bytes (48 characters).  */
       if ((j % 16 == 15) && (j != dcache->line_size - 1))
-       printf_filtered ("\n");
+       gdb_printf ("\n");
     }
-  printf_filtered ("\n");
+  gdb_printf ("\n");
 }
 
 /* Parse EXP and show the info about DCACHE.  */
@@ -595,27 +604,27 @@ dcache_info_1 (DCACHE *dcache, const char *exp)
       i = strtol (exp, &linestart, 10);
       if (linestart == exp || i < 0)
        {
-         printf_filtered (_("Usage: info dcache [LINENUMBER]\n"));
-          return;
+         gdb_printf (_("Usage: info dcache [LINENUMBER]\n"));
+         return;
        }
 
       dcache_print_line (dcache, i);
       return;
     }
 
-  printf_filtered (_("Dcache %u lines of %u bytes each.\n"),
-                  dcache_size,
-                  dcache ? (unsigned) dcache->line_size
-                  : dcache_line_size);
+  gdb_printf (_("Dcache %u lines of %u bytes each.\n"),
+             dcache_size,
+             dcache ? (unsigned) dcache->line_size
+             : dcache_line_size);
 
-  if (dcache == NULL || ptid_equal (dcache->ptid, null_ptid))
+  if (dcache == NULL || dcache->ptid == null_ptid)
     {
-      printf_filtered (_("No data cache available.\n"));
+      gdb_printf (_("No data cache available.\n"));
       return;
     }
 
-  printf_filtered (_("Contains data for %s\n"),
-                  target_pid_to_str (dcache->ptid));
+  gdb_printf (_("Contains data for %s\n"),
+             target_pid_to_str (dcache->ptid).c_str ());
 
   refcount = 0;
 
@@ -626,15 +635,15 @@ dcache_info_1 (DCACHE *dcache, const char *exp)
     {
       struct dcache_block *db = (struct dcache_block *) n->value;
 
-      printf_filtered (_("Line %d: address %s [%d hits]\n"),
-                      i, paddress (target_gdbarch (), db->addr), db->refs);
+      gdb_printf (_("Line %d: address %s [%d hits]\n"),
+                 i, paddress (target_gdbarch (), db->addr), db->refs);
       i++;
       refcount += db->refs;
 
       n = splay_tree_successor (dcache->tree, n->key);
     }
 
-  printf_filtered (_("Cache state: %d active lines, %d hits\n"), i, refcount);
+  gdb_printf (_("Cache state: %d active lines, %d hits\n"), i, refcount);
 }
 
 static void
@@ -669,22 +678,9 @@ set_dcache_line_size (const char *args, int from_tty,
   target_dcache_invalidate ();
 }
 
-static void
-set_dcache_command (const char *arg, int from_tty)
-{
-  printf_unfiltered (
-     "\"set dcache\" must be followed by the name of a subcommand.\n");
-  help_list (dcache_set_list, "set dcache ", all_commands, gdb_stdout);
-}
-
-static void
-show_dcache_command (const char *args, int from_tty)
-{
-  cmd_show_list (dcache_show_list, from_tty, "");
-}
-
+void _initialize_dcache ();
 void
-_initialize_dcache (void)
+_initialize_dcache ()
 {
   add_setshow_boolean_cmd ("remotecache", class_support,
                           &dcache_enabled_p, _("\
@@ -706,12 +702,12 @@ With no arguments, this command prints the cache configuration and a\n\
 summary of each line in the cache.  With an argument, dump\"\n\
 the contents of the given line."));
 
-  add_prefix_cmd ("dcache", class_obscure, set_dcache_command, _("\
+  add_setshow_prefix_cmd ("dcache", class_obscure,
+                         _("\
 Use this command to set number of lines in dcache and line-size."),
-                 &dcache_set_list, "set dcache ", /*allow_unknown*/0, &setlist);
-  add_prefix_cmd ("dcache", class_obscure, show_dcache_command, _("\
-Show dcachesettings."),
-                 &dcache_show_list, "show dcache ", /*allow_unknown*/0, &showlist);
+                         ("Show dcache settings."),
+                         &dcache_set_list, &dcache_show_list,
+                         &setlist, &showlist);
 
   add_setshow_zuinteger_cmd ("line-size", class_obscure,
                             &dcache_line_size, _("\