gdb/i386-cygwin-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for Cygwin running on i386's, for GDB.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "osabi.h"
  16. #include "i386-tdep.h"
  17. #include "windows-tdep.h"
  18. #include "regset.h"
  19. #include "gdb_obstack.h"
  20. #include "xml-support.h"
  21. #include "gdbcore.h"
  22. #include "inferior.h"

  23. /* Core file support.  */

  24. /* This vector maps GDB's idea of a register's number into an address
  25.    in the windows exception context vector.  */

  26. static int i386_windows_gregset_reg_offset[] =
  27. {
  28.   176, /* eax */
  29.   172, /* ecx */
  30.   168, /* edx */
  31.   164, /* ebx */

  32.   196, /* esp */
  33.   180, /* ebp */
  34.   160, /* esi */
  35.   156, /* edi */

  36.   184, /* eip */
  37.   192, /* eflags */
  38.   188, /* cs */
  39.   200, /* ss */

  40.   152, /* ds */
  41.   148, /* es */
  42.   144, /* fs */
  43.   140, /* gs */

  44.   56, /* FloatSave.RegisterArea[0 * 10] */
  45.   66, /* FloatSave.RegisterArea[1 * 10] */
  46.   76, /* FloatSave.RegisterArea[2 * 10] */
  47.   86, /* FloatSave.RegisterArea[3 * 10] */
  48.   96, /* FloatSave.RegisterArea[4 * 10] */
  49.   106, /* FloatSave.RegisterArea[5 * 10] */
  50.   116, /* FloatSave.RegisterArea[6 * 10] */
  51.   126, /* FloatSave.RegisterArea[7 * 10] */

  52.   28, /* FloatSave.ControlWord */
  53.   32, /* FloatSave.StatusWord */
  54.   36, /* FloatSave.TagWord */
  55.   44, /* FloatSave.ErrorSelector */
  56.   40, /* FloatSave.ErrorOffset */
  57.   52, /* FloatSave.DataSelector */
  58.   48, /* FloatSave.DataOffset */
  59.   44, /* FloatSave.ErrorSelector */

  60.   /* XMM0-7 */
  61.   364, /* ExtendedRegisters[10*16] */
  62.   380, /* ExtendedRegisters[11*16] */
  63.   396, /* ExtendedRegisters[12*16] */
  64.   412, /* ExtendedRegisters[13*16] */
  65.   428, /* ExtendedRegisters[14*16] */
  66.   444, /* ExtendedRegisters[15*16] */
  67.   460, /* ExtendedRegisters[16*16] */
  68.   476, /* ExtendedRegisters[17*16] */

  69.   /* MXCSR */
  70.   228 /* ExtendedRegisters[24] */
  71. };

  72. #define I386_WINDOWS_SIZEOF_GREGSET 716

  73. struct cpms_data
  74. {
  75.   struct gdbarch *gdbarch;
  76.   struct obstack *obstack;
  77.   int module_count;
  78. };

  79. static void
  80. core_process_module_section (bfd *abfd, asection *sect, void *obj)
  81. {
  82.   struct cpms_data *data = obj;
  83.   enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);

  84.   char *module_name;
  85.   size_t module_name_size;
  86.   CORE_ADDR base_addr;

  87.   gdb_byte *buf = NULL;

  88.   if (strncmp (sect->name, ".module", 7) != 0)
  89.     return;

  90.   buf = xmalloc (bfd_get_section_size (sect) + 1);
  91.   if (!buf)
  92.     {
  93.       printf_unfiltered ("memory allocation failed for %s\n", sect->name);
  94.       goto out;
  95.     }
  96.   if (!bfd_get_section_contents (abfd, sect,
  97.                                  buf, 0, bfd_get_section_size (sect)))
  98.     goto out;



  99.   /* A DWORD (data_type) followed by struct windows_core_module_info.  */

  100.   base_addr =
  101.     extract_unsigned_integer (buf + 4, 4, byte_order);

  102.   module_name_size =
  103.     extract_unsigned_integer (buf + 8, 4, byte_order);

  104.   if (12 + module_name_size > bfd_get_section_size (sect))
  105.     goto out;
  106.   module_name = (char *) buf + 12;

  107.   /* The first module is the .exe itself.  */
  108.   if (data->module_count != 0)
  109.     windows_xfer_shared_library (module_name, base_addr,
  110.                                  data->gdbarch, data->obstack);
  111.   data->module_count++;

  112. out:
  113.   if (buf)
  114.     xfree (buf);
  115.   return;
  116. }

  117. static ULONGEST
  118. windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
  119.                                   gdb_byte *readbuf,
  120.                                   ULONGEST offset, ULONGEST len)
  121. {
  122.   struct obstack obstack;
  123.   const char *buf;
  124.   ULONGEST len_avail;
  125.   struct cpms_data data = { gdbarch, &obstack, 0 };

  126.   obstack_init (&obstack);
  127.   obstack_grow_str (&obstack, "<library-list>\n");
  128.   bfd_map_over_sections (core_bfd,
  129.                          core_process_module_section,
  130.                          &data);
  131.   obstack_grow_str0 (&obstack, "</library-list>\n");

  132.   buf = obstack_finish (&obstack);
  133.   len_avail = strlen (buf);
  134.   if (offset >= len_avail)
  135.     return 0;

  136.   if (len > len_avail - offset)
  137.     len = len_avail - offset;
  138.   memcpy (readbuf, buf + offset, len);

  139.   obstack_free (&obstack, NULL);
  140.   return len;
  141. }

  142. /* This is how we want PTIDs from core files to be printed.  */

  143. static char *
  144. i386_windows_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
  145. {
  146.   static char buf[80];

  147.   if (ptid_get_lwp (ptid) != 0)
  148.     {
  149.       snprintf (buf, sizeof (buf), "Thread 0x%lx", ptid_get_lwp (ptid));
  150.       return buf;
  151.     }

  152.   return normal_pid_to_str (ptid);
  153. }

  154. static CORE_ADDR
  155. i386_cygwin_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  156. {
  157.   return i386_pe_skip_trampoline_code (frame, pc, NULL);
  158. }

  159. static const char *
  160. i386_cygwin_auto_wide_charset (void)
  161. {
  162.   return "UTF-16";
  163. }

  164. static void
  165. i386_cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  166. {
  167.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  168.   windows_init_abi (info, gdbarch);

  169.   set_gdbarch_skip_trampoline_code (gdbarch, i386_cygwin_skip_trampoline_code);

  170.   set_gdbarch_skip_main_prologue (gdbarch, i386_skip_main_prologue);

  171.   tdep->struct_return = reg_struct_return;

  172.   tdep->gregset_reg_offset = i386_windows_gregset_reg_offset;
  173.   tdep->gregset_num_regs = ARRAY_SIZE (i386_windows_gregset_reg_offset);
  174.   tdep->sizeof_gregset = I386_WINDOWS_SIZEOF_GREGSET;

  175.   tdep->sizeof_fpregset = 0;

  176.   /* Core file support.  */
  177.   set_gdbarch_core_xfer_shared_libraries
  178.     (gdbarch, windows_core_xfer_shared_libraries);
  179.   set_gdbarch_core_pid_to_str (gdbarch, i386_windows_core_pid_to_str);

  180.   set_gdbarch_auto_wide_charset (gdbarch, i386_cygwin_auto_wide_charset);
  181. }

  182. static enum gdb_osabi
  183. i386_cygwin_osabi_sniffer (bfd *abfd)
  184. {
  185.   char *target_name = bfd_get_target (abfd);

  186.   if (strcmp (target_name, "pei-i386") == 0)
  187.     return GDB_OSABI_CYGWIN;

  188.   /* Cygwin uses elf core dumps.  Do not claim all ELF executables,
  189.      check whether there is a .reg section of proper size.  */
  190.   if (strcmp (target_name, "elf32-i386") == 0)
  191.     {
  192.       asection *section = bfd_get_section_by_name (abfd, ".reg");
  193.       if (section
  194.           && bfd_section_size (abfd, section) == I386_WINDOWS_SIZEOF_GREGSET)
  195.         return GDB_OSABI_CYGWIN;
  196.     }

  197.   return GDB_OSABI_UNKNOWN;
  198. }

  199. /* Provide a prototype to silence -Wmissing-prototypes.  */
  200. void _initialize_i386_cygwin_tdep (void);

  201. void
  202. _initialize_i386_cygwin_tdep (void)
  203. {
  204.   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
  205.                                   i386_cygwin_osabi_sniffer);

  206.   /* Cygwin uses elf core dumps.  */
  207.   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour,
  208.                                   i386_cygwin_osabi_sniffer);

  209.   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_CYGWIN,
  210.                           i386_cygwin_init_abi);
  211. }