gdb: remove BLOCK_NAMESPACE macro
[binutils-gdb.git] / gdb / windows-tdep.c
index 086038af0d49817061dbd03f4141888c966a63f2..9049f1e17f5db3c3e8423be8514d85a085466b33 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2008-2020 Free Software Foundation, Inc.
+/* Copyright (C) 2008-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -17,7 +17,7 @@
 
 #include "defs.h"
 #include "windows-tdep.h"
-#include "gdb_obstack.h"
+#include "gdbsupport/gdb_obstack.h"
 #include "xml-support.h"
 #include "gdbarch.h"
 #include "target.h"
@@ -30,7 +30,6 @@
 #include "symfile.h"
 #include "coff-pe-read.h"
 #include "gdb_bfd.h"
-#include "complaints.h"
 #include "solib.h"
 #include "solib-target.h"
 #include "gdbcore.h"
@@ -105,6 +104,10 @@ enum
   CYGWIN_SIGUSR2 = 31,
 };
 
+/* These constants are defined by Cygwin's core_dump.h */
+static constexpr unsigned int NOTE_INFO_MODULE = 3;
+static constexpr unsigned int NOTE_INFO_MODULE64 = 4;
+
 struct cmd_list_element *info_w32_cmdlist;
 
 typedef struct thread_information_block_32
@@ -405,7 +408,7 @@ tlb_value_read (struct value *val)
 
   if (!target_get_tib_address (inferior_ptid, &tlb))
     error (_("Unable to read tlb"));
-  store_typed_address (value_contents_raw (val), type, tlb);
+  store_typed_address (value_contents_raw (val).data (), type, tlb);
 }
 
 /* This function implements the lval_computed support for writing a
@@ -431,7 +434,7 @@ static const struct lval_funcs tlb_value_funcs =
 static struct value *
 tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
 {
-  if (target_has_stack && inferior_ptid != null_ptid)
+  if (target_has_stack () && inferior_ptid != null_ptid)
     {
       struct type *type = windows_get_tlb_type (gdbarch);
       return allocate_computed_value (type, &tlb_value_funcs, NULL);
@@ -478,24 +481,24 @@ display_one_tib (ptid_t ptid)
 
   if (target_get_tib_address (ptid, &thread_local_base) == 0)
     {
-      printf_filtered (_("Unable to get thread local base for %s\n"),
-                      target_pid_to_str (ptid).c_str ());
+      gdb_printf (_("Unable to get thread local base for %s\n"),
+                 target_pid_to_str (ptid).c_str ());
       return -1;
     }
 
-  if (target_read (current_top_target (), TARGET_OBJECT_MEMORY,
+  if (target_read (current_inferior ()->top_target (), TARGET_OBJECT_MEMORY,
                   NULL, tib, thread_local_base, tib_size) != tib_size)
     {
-      printf_filtered (_("Unable to read thread information "
-                        "block for %s at address %s\n"),
-                      target_pid_to_str (ptid).c_str (), 
-                      paddress (target_gdbarch (), thread_local_base));
+      gdb_printf (_("Unable to read thread information "
+                   "block for %s at address %s\n"),
+                 target_pid_to_str (ptid).c_str (), 
+                 paddress (target_gdbarch (), thread_local_base));
       return -1;
     }
 
-  printf_filtered (_("Thread Information Block %s at %s\n"),
-                  target_pid_to_str (ptid).c_str (),
-                  paddress (target_gdbarch (), thread_local_base));
+  gdb_printf (_("Thread Information Block %s at %s\n"),
+             target_pid_to_str (ptid).c_str (),
+             paddress (target_gdbarch (), thread_local_base));
 
   index = (gdb_byte *) tib;
 
@@ -505,10 +508,10 @@ display_one_tib (ptid_t ptid)
     {
       val = extract_unsigned_integer (index, size, byte_order);
       if (i < max_name)
-       printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
+       gdb_printf (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
       else if (val != 0)
-       printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
-                        phex (val, size));
+       gdb_printf (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
+                   phex (val, size));
       index += size;
     } 
   return 1;  
@@ -537,7 +540,7 @@ windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
 
   if (!text_offset)
     {
-      gdb_bfd_ref_ptr dll (gdb_bfd_open (so_name, gnutarget, -1));
+      gdb_bfd_ref_ptr dll (gdb_bfd_open (so_name, gnutarget));
       /* The following calls are OK even if dll is NULL.
         The default value 0x1000 is returned by pe_text_section_offset
         in that case.  */
@@ -598,8 +601,8 @@ static void
 show_maint_show_all_tib (struct ui_file *file, int from_tty,
                struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file, _("Show all non-zero elements of "
-                           "Thread Information Block is %s.\n"), value);
+  gdb_printf (file, _("Show all non-zero elements of "
+                     "Thread Information Block is %s.\n"), value);
 }
 
 
@@ -612,7 +615,7 @@ init_w32_command_list (void)
       add_basic_prefix_cmd
        ("w32", class_info,
         _("Print information specific to Win32 debugging."),
-        &info_w32_cmdlist, "info w32 ", 0, &infolist);
+        &info_w32_cmdlist, 0, &infolist);
       w32_prefix_command_valid = 1;
     }
 }
@@ -751,16 +754,16 @@ create_enum (struct gdbarch *gdbarch, int bit, const char *name,
   int i;
 
   type = arch_type (gdbarch, TYPE_CODE_ENUM, bit, name);
-  TYPE_NFIELDS (type) = count;
-  TYPE_FIELDS (type) = (struct field *)
-    TYPE_ZALLOC (type, sizeof (struct field) * count);
-  TYPE_UNSIGNED (type) = 1;
+  type->set_num_fields (count);
+  type->set_fields
+    ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * count));
+  type->set_is_unsigned (true);
 
   for (i = 0; i < count; i++)
-  {
-    TYPE_FIELD_NAME (type, i) = values[i].name;
-    SET_FIELD_ENUMVAL (TYPE_FIELD (type, i), values[i].value);
-  }
+    {
+      type->field (i).set_name (values[i].name);
+      type->field (i).set_loc_enumval (values[i].value);
+    }
 
   return type;
 }
@@ -898,7 +901,7 @@ windows_solib_create_inferior_hook (int from_tty)
     }
   CORE_ADDR tlb;
   gdb_byte buf[8];
-  if (target_has_execution
+  if (target_has_execution ()
       && target_get_tib_address (inferior_ptid, &tlb)
       && !target_read_memory (tlb + peb_offset, buf, ptr_bytes))
     {
@@ -908,11 +911,13 @@ windows_solib_create_inferior_hook (int from_tty)
     }
 
   /* Rebase executable if the base address changed because of ASLR.  */
-  if (symfile_objfile != nullptr && exec_base != 0)
+  if (current_program_space->symfile_object_file != nullptr && exec_base != 0)
     {
-      CORE_ADDR vmaddr = pe_data (exec_bfd)->pe_opthdr.ImageBase;
+      CORE_ADDR vmaddr
+       = pe_data (current_program_space->exec_bfd ())->pe_opthdr.ImageBase;
       if (vmaddr != exec_base)
-       objfile_rebase (symfile_objfile, exec_base - vmaddr);
+       objfile_rebase (current_program_space->symfile_object_file,
+                       exec_base - vmaddr);
     }
 }
 
@@ -965,7 +970,6 @@ static const struct internalvar_funcs tlb_funcs =
 {
   tlb_make_value,
   NULL,
-  NULL
 };
 
 /* Layout of an element of a PE's Import Directory Table.  Based on:
@@ -1060,7 +1064,7 @@ section's range [0x%" BFD_VMA_FMT "x, 0x%" BFD_VMA_FMT "x[."),
       bfd_vma name_va = dir_entry->name_rva;
 
       /* If the name's virtual address is smaller than the section's virtual
-         address, there's a problem.  */
+        address, there's a problem.  */
       if (name_va < idata_section_va || name_va >= idata_section_end_va)
        {
          warning (_("\
@@ -1088,6 +1092,112 @@ range [0x%" BFD_VMA_FMT "x, 0x%" BFD_VMA_FMT "x[."),
   return false;
 }
 
+struct cpms_data
+{
+  struct gdbarch *gdbarch;
+  struct obstack *obstack;
+  int module_count;
+};
+
+static void
+core_process_module_section (bfd *abfd, asection *sect, void *obj)
+{
+  struct cpms_data *data = (struct cpms_data *) obj;
+  enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);
+
+  unsigned int data_type;
+  char *module_name;
+  size_t module_name_size;
+  size_t module_name_offset;
+  CORE_ADDR base_addr;
+
+  if (!startswith (sect->name, ".module"))
+    return;
+
+  gdb::byte_vector buf (bfd_section_size (sect) + 1);
+  if (!bfd_get_section_contents (abfd, sect,
+                                buf.data (), 0, bfd_section_size (sect)))
+    return;
+  /* We're going to treat part of the buffer as a string, so make sure
+     it is NUL-terminated.  */
+  buf.back () = 0;
+
+  /* A DWORD (data_type) followed by struct windows_core_module_info.  */
+  if (bfd_section_size (sect) < 4)
+    return;
+  data_type = extract_unsigned_integer (buf.data (), 4, byte_order);
+
+  if (data_type == NOTE_INFO_MODULE)
+    {
+      module_name_offset = 12;
+      if (bfd_section_size (sect) < module_name_offset)
+       return;
+      base_addr = extract_unsigned_integer (&buf[4], 4, byte_order);
+      module_name_size = extract_unsigned_integer (&buf[8], 4, byte_order);
+    }
+  else if (data_type == NOTE_INFO_MODULE64)
+    {
+      module_name_offset = 16;
+      if (bfd_section_size (sect) < module_name_offset)
+       return;
+      base_addr = extract_unsigned_integer (&buf[4], 8, byte_order);
+      module_name_size = extract_unsigned_integer (&buf[12], 4, byte_order);
+    }
+  else
+    return;
+
+  if (module_name_offset + module_name_size > bfd_section_size (sect))
+    return;
+  module_name = (char *) buf.data () + module_name_offset;
+
+  /* The first module is the .exe itself.  */
+  if (data->module_count != 0)
+    windows_xfer_shared_library (module_name, base_addr,
+                                NULL, data->gdbarch, data->obstack);
+  data->module_count++;
+}
+
+ULONGEST
+windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
+                                 gdb_byte *readbuf,
+                                 ULONGEST offset, ULONGEST len)
+{
+  struct obstack obstack;
+  const char *buf;
+  ULONGEST len_avail;
+  struct cpms_data data = { gdbarch, &obstack, 0 };
+
+  obstack_init (&obstack);
+  obstack_grow_str (&obstack, "<library-list>\n");
+  bfd_map_over_sections (core_bfd,
+                        core_process_module_section,
+                        &data);
+  obstack_grow_str0 (&obstack, "</library-list>\n");
+
+  buf = (const char *) obstack_finish (&obstack);
+  len_avail = strlen (buf);
+  if (offset >= len_avail)
+    return 0;
+
+  if (len > len_avail - offset)
+    len = len_avail - offset;
+  memcpy (readbuf, buf + offset, len);
+
+  obstack_free (&obstack, NULL);
+  return len;
+}
+
+/* This is how we want PTIDs from core files to be printed.  */
+
+std::string
+windows_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
+{
+  if (ptid.lwp () != 0)
+    return string_printf ("Thread 0x%lx", ptid.lwp ());
+
+  return normal_pid_to_str (ptid);
+}
+
 void _initialize_windows_tdep ();
 void
 _initialize_windows_tdep ()
@@ -1096,10 +1206,11 @@ _initialize_windows_tdep ()
     = gdbarch_data_register_post_init (init_windows_gdbarch_data);
 
   init_w32_command_list ();
-  add_cmd ("thread-information-block", class_info, display_tib,
-          _("Display thread information block."),
-          &info_w32_cmdlist);
-  add_alias_cmd ("tib", "thread-information-block", class_info, 1,
+  cmd_list_element *info_w32_thread_information_block_cmd
+    = add_cmd ("thread-information-block", class_info, display_tib,
+              _("Display thread information block."),
+              &info_w32_cmdlist);
+  add_alias_cmd ("tib", info_w32_thread_information_block_cmd, class_info, 1,
                 &info_w32_cmdlist);
 
   add_setshow_boolean_cmd ("show-all-tib", class_maintenance,