gdb/windows-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Copyright (C) 2008-2015 Free Software Foundation, Inc.

  2.    This file is part of GDB.

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

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

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

  13. #include "defs.h"
  14. #include "windows-tdep.h"
  15. #include "gdb_obstack.h"
  16. #include "xml-support.h"
  17. #include "gdbarch.h"
  18. #include "target.h"
  19. #include "value.h"
  20. #include "inferior.h"
  21. #include "command.h"
  22. #include "gdbcmd.h"
  23. #include "gdbthread.h"
  24. #include "objfiles.h"
  25. #include "symfile.h"
  26. #include "coff-pe-read.h"
  27. #include "gdb_bfd.h"
  28. #include "complaints.h"
  29. #include "solib.h"
  30. #include "solib-target.h"

  31. struct cmd_list_element *info_w32_cmdlist;

  32. typedef struct thread_information_block_32
  33.   {
  34.     uint32_t current_seh;                        /* %fs:0x0000 */
  35.     uint32_t current_top_of_stack;                 /* %fs:0x0004 */
  36.     uint32_t current_bottom_of_stack;                /* %fs:0x0008 */
  37.     uint32_t sub_system_tib;                        /* %fs:0x000c */
  38.     uint32_t fiber_data;                        /* %fs:0x0010 */
  39.     uint32_t arbitrary_data_slot;                /* %fs:0x0014 */
  40.     uint32_t linear_address_tib;                /* %fs:0x0018 */
  41.     uint32_t environment_pointer;                /* %fs:0x001c */
  42.     uint32_t process_id;                        /* %fs:0x0020 */
  43.     uint32_t current_thread_id;                        /* %fs:0x0024 */
  44.     uint32_t active_rpc_handle;                        /* %fs:0x0028 */
  45.     uint32_t thread_local_storage;                /* %fs:0x002c */
  46.     uint32_t process_environment_block;                /* %fs:0x0030 */
  47.     uint32_t last_error_number;                        /* %fs:0x0034 */
  48.   }
  49. thread_information_32;

  50. typedef struct thread_information_block_64
  51.   {
  52.     uint64_t current_seh;                        /* %gs:0x0000 */
  53.     uint64_t current_top_of_stack;                 /* %gs:0x0008 */
  54.     uint64_t current_bottom_of_stack;                /* %gs:0x0010 */
  55.     uint64_t sub_system_tib;                        /* %gs:0x0018 */
  56.     uint64_t fiber_data;                        /* %gs:0x0020 */
  57.     uint64_t arbitrary_data_slot;                /* %gs:0x0028 */
  58.     uint64_t linear_address_tib;                /* %gs:0x0030 */
  59.     uint64_t environment_pointer;                /* %gs:0x0038 */
  60.     uint64_t process_id;                        /* %gs:0x0040 */
  61.     uint64_t current_thread_id;                        /* %gs:0x0048 */
  62.     uint64_t active_rpc_handle;                        /* %gs:0x0050 */
  63.     uint64_t thread_local_storage;                /* %gs:0x0058 */
  64.     uint64_t process_environment_block;                /* %gs:0x0060 */
  65.     uint64_t last_error_number;                        /* %gs:0x0068 */
  66.   }
  67. thread_information_64;


  68. static const char* TIB_NAME[] =
  69.   {
  70.     " current_seh                 ",        /* %fs:0x0000 */
  71.     " current_top_of_stack        ",         /* %fs:0x0004 */
  72.     " current_bottom_of_stack     ",        /* %fs:0x0008 */
  73.     " sub_system_tib              ",        /* %fs:0x000c */
  74.     " fiber_data                  ",        /* %fs:0x0010 */
  75.     " arbitrary_data_slot         ",        /* %fs:0x0014 */
  76.     " linear_address_tib          ",        /* %fs:0x0018 */
  77.     " environment_pointer         ",        /* %fs:0x001c */
  78.     " process_id                  ",        /* %fs:0x0020 */
  79.     " current_thread_id           ",        /* %fs:0x0024 */
  80.     " active_rpc_handle           ",        /* %fs:0x0028 */
  81.     " thread_local_storage        ",        /* %fs:0x002c */
  82.     " process_environment_block   ",        /* %fs:0x0030 */
  83.     " last_error_number           "        /* %fs:0x0034 */
  84.   };

  85. static const int MAX_TIB32 =
  86.   sizeof (thread_information_32) / sizeof (uint32_t);
  87. static const int MAX_TIB64 =
  88.   sizeof (thread_information_64) / sizeof (uint64_t);
  89. static const int FULL_TIB_SIZE = 0x1000;

  90. static int maint_display_all_tib = 0;

  91. /* Define Thread Local Base pointer type.  */

  92. static struct type *
  93. windows_get_tlb_type (struct gdbarch *gdbarch)
  94. {
  95.   static struct gdbarch *last_gdbarch = NULL;
  96.   static struct type *last_tlb_type = NULL;
  97.   struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
  98.   struct type *peb_ldr_type, *peb_ldr_ptr_type;
  99.   struct type *peb_type, *peb_ptr_type, *list_type, *list_ptr_type;
  100.   struct type *module_list_ptr_type;
  101.   struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;

  102.   /* Do not rebuild type if same gdbarch as last time.  */
  103.   if (last_tlb_type && last_gdbarch == gdbarch)
  104.     return last_tlb_type;

  105.   dword_ptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
  106.                                  1, "DWORD_PTR");
  107.   dword32_type = arch_integer_type (gdbarch, 32,
  108.                                  1, "DWORD32");
  109.   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);

  110.   /* list entry */

  111.   list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  112.   TYPE_NAME (list_type) = xstrdup ("list");

  113.   list_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
  114.                             TYPE_LENGTH (void_ptr_type), NULL);

  115.   module_list_ptr_type = void_ptr_type;

  116.   append_composite_type_field (list_type, "forward_list",
  117.                                module_list_ptr_type);
  118.   append_composite_type_field (list_type, "backward_list",
  119.                                module_list_ptr_type);

  120.   /* Structured Exception Handler */

  121.   seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  122.   TYPE_NAME (seh_type) = xstrdup ("seh");

  123.   seh_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
  124.                             TYPE_LENGTH (void_ptr_type), NULL);
  125.   TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;

  126.   append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
  127.   append_composite_type_field (seh_type, "handler",
  128.                                builtin_type (gdbarch)->builtin_func_ptr);

  129.   /* struct _PEB_LDR_DATA */
  130.   peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  131.   TYPE_NAME (peb_ldr_type) = xstrdup ("peb_ldr_data");

  132.   append_composite_type_field (peb_ldr_type, "length", dword32_type);
  133.   append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
  134.   append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
  135.   append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
  136.   append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
  137.   append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
  138.   append_composite_type_field (peb_ldr_type, "entry_in_progress",
  139.                                void_ptr_type);
  140.   peb_ldr_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
  141.                             TYPE_LENGTH (void_ptr_type), NULL);
  142.   TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;


  143.   /* struct process environment block */
  144.   peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  145.   TYPE_NAME (peb_type) = xstrdup ("peb");

  146.   /* First bytes contain several flags.  */
  147.   append_composite_type_field (peb_type, "flags", dword_ptr_type);
  148.   append_composite_type_field (peb_type, "mutant", void_ptr_type);
  149.   append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
  150.   append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
  151.   append_composite_type_field (peb_type, "process_parameters", void_ptr_type);
  152.   append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
  153.   append_composite_type_field (peb_type, "process_heap", void_ptr_type);
  154.   append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
  155.   peb_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
  156.                             TYPE_LENGTH (void_ptr_type), NULL);
  157.   TYPE_TARGET_TYPE (peb_ptr_type) = peb_type;


  158.   /* struct thread information block */
  159.   tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  160.   TYPE_NAME (tib_type) = xstrdup ("tib");

  161.   /* uint32_t current_seh;                        %fs:0x0000 */
  162.   append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
  163.   /* uint32_t current_top_of_stack;                 %fs:0x0004 */
  164.   append_composite_type_field (tib_type, "current_top_of_stack",
  165.                                void_ptr_type);
  166.   /* uint32_t current_bottom_of_stack;                %fs:0x0008 */
  167.   append_composite_type_field (tib_type, "current_bottom_of_stack",
  168.                                void_ptr_type);
  169.   /* uint32_t sub_system_tib;                        %fs:0x000c */
  170.   append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);

  171.   /* uint32_t fiber_data;                        %fs:0x0010 */
  172.   append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
  173.   /* uint32_t arbitrary_data_slot;                %fs:0x0014 */
  174.   append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
  175.   /* uint32_t linear_address_tib;                %fs:0x0018 */
  176.   append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
  177.   /* uint32_t environment_pointer;                %fs:0x001c */
  178.   append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
  179.   /* uint32_t process_id;                        %fs:0x0020 */
  180.   append_composite_type_field (tib_type, "process_id", dword_ptr_type);
  181.   /* uint32_t current_thread_id;                %fs:0x0024 */
  182.   append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
  183.   /* uint32_t active_rpc_handle;                %fs:0x0028 */
  184.   append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
  185.   /* uint32_t thread_local_storage;                %fs:0x002c */
  186.   append_composite_type_field (tib_type, "thread_local_storage",
  187.                                void_ptr_type);
  188.   /* uint32_t process_environment_block;        %fs:0x0030 */
  189.   append_composite_type_field (tib_type, "process_environment_block",
  190.                                peb_ptr_type);
  191.   /* uint32_t last_error_number;                %fs:0x0034 */
  192.   append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);

  193.   tib_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
  194.                             TYPE_LENGTH (void_ptr_type), NULL);
  195.   TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;

  196.   last_tlb_type = tib_ptr_type;
  197.   last_gdbarch = gdbarch;

  198.   return tib_ptr_type;
  199. }

  200. /* The $_tlb convenience variable is a bit special.  We don't know
  201.    for sure the type of the value until we actually have a chance to
  202.    fetch the data.  The type can change depending on gdbarch, so it is
  203.    also dependent on which thread you have selected.  */

  204. /* This function implements the lval_computed support for reading a
  205.    $_tlb value.  */

  206. static void
  207. tlb_value_read (struct value *val)
  208. {
  209.   CORE_ADDR tlb;
  210.   struct type *type = check_typedef (value_type (val));

  211.   if (!target_get_tib_address (inferior_ptid, &tlb))
  212.     error (_("Unable to read tlb"));
  213.   store_typed_address (value_contents_raw (val), type, tlb);
  214. }

  215. /* This function implements the lval_computed support for writing a
  216.    $_tlb value.  */

  217. static void
  218. tlb_value_write (struct value *v, struct value *fromval)
  219. {
  220.   error (_("Impossible to change the Thread Local Base"));
  221. }

  222. static const struct lval_funcs tlb_value_funcs =
  223.   {
  224.     tlb_value_read,
  225.     tlb_value_write
  226.   };


  227. /* Return a new value with the correct type for the tlb object of
  228.    the current thread using architecture GDBARCH.  Return a void value
  229.    if there's no object available.  */

  230. static struct value *
  231. tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
  232. {
  233.   if (target_has_stack && !ptid_equal (inferior_ptid, null_ptid))
  234.     {
  235.       struct type *type = windows_get_tlb_type (gdbarch);
  236.       return allocate_computed_value (type, &tlb_value_funcs, NULL);
  237.     }

  238.   return allocate_value (builtin_type (gdbarch)->builtin_void);
  239. }


  240. /* Display thread information block of a given thread.  */

  241. static int
  242. display_one_tib (ptid_t ptid)
  243. {
  244.   gdb_byte *tib = NULL;
  245.   gdb_byte *index;
  246.   CORE_ADDR thread_local_base;
  247.   ULONGEST i, val, max, max_name, size, tib_size;
  248.   ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch ());
  249.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());

  250.   if (sizeof_ptr == 64)
  251.     {
  252.       size = sizeof (uint64_t);
  253.       tib_size = sizeof (thread_information_64);
  254.       max = MAX_TIB64;
  255.     }
  256.   else
  257.     {
  258.       size = sizeof (uint32_t);
  259.       tib_size = sizeof (thread_information_32);
  260.       max = MAX_TIB32;
  261.     }

  262.   max_name = max;

  263.   if (maint_display_all_tib)
  264.     {
  265.       tib_size = FULL_TIB_SIZE;
  266.       max = tib_size / size;
  267.     }

  268.   tib = alloca (tib_size);

  269.   if (target_get_tib_address (ptid, &thread_local_base) == 0)
  270.     {
  271.       printf_filtered (_("Unable to get thread local base for %s\n"),
  272.         target_pid_to_str (ptid));
  273.       return -1;
  274.     }

  275.   if (target_read (&current_target, TARGET_OBJECT_MEMORY,
  276.                    NULL, tib, thread_local_base, tib_size) != tib_size)
  277.     {
  278.       printf_filtered (_("Unable to read thread information "
  279.                          "block for %s at address %s\n"),
  280.         target_pid_to_str (ptid),
  281.         paddress (target_gdbarch (), thread_local_base));
  282.       return -1;
  283.     }

  284.   printf_filtered (_("Thread Information Block %s at %s\n"),
  285.                    target_pid_to_str (ptid),
  286.                    paddress (target_gdbarch (), thread_local_base));

  287.   index = (gdb_byte *) tib;

  288.   /* All fields have the size of a pointer, this allows to iterate
  289.      using the same for loop for both layouts.  */
  290.   for (i = 0; i < max; i++)
  291.     {
  292.       val = extract_unsigned_integer (index, size, byte_order);
  293.       if (i < max_name)
  294.         printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
  295.       else if (val != 0)
  296.         printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
  297.                          phex (val, size));
  298.       index += size;
  299.     }
  300.   return 1;
  301. }

  302. /* Display thread information block of a thread specified by ARGS.
  303.    If ARGS is empty, display thread information block of current_thread
  304.    if current_thread is non NULL.
  305.    Otherwise ARGS is parsed and converted to a integer that should
  306.    be the windows ThreadID (not the internal GDB thread ID).  */

  307. static void
  308. display_tib (char * args, int from_tty)
  309. {
  310.   if (args)
  311.     {
  312.       struct thread_info *tp;
  313.       int gdb_id = value_as_long (parse_and_eval (args));

  314.       tp = find_thread_id (gdb_id);

  315.       if (!tp)
  316.         error (_("Thread ID %d not known."), gdb_id);

  317.       if (!target_thread_alive (tp->ptid))
  318.         error (_("Thread ID %d has terminated."), gdb_id);

  319.       display_one_tib (tp->ptid);
  320.     }
  321.   else if (!ptid_equal (inferior_ptid, null_ptid))
  322.     display_one_tib (inferior_ptid);
  323. }

  324. void
  325. windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
  326.                              struct gdbarch *gdbarch, struct obstack *obstack)
  327. {
  328.   char *p;
  329.   struct bfd * dll;
  330.   CORE_ADDR text_offset;

  331.   obstack_grow_str (obstack, "<library name=\"");
  332.   p = xml_escape_text (so_name);
  333.   obstack_grow_str (obstack, p);
  334.   xfree (p);
  335.   obstack_grow_str (obstack, "\"><segment address=\"");
  336.   dll = gdb_bfd_open_maybe_remote (so_name);
  337.   /* The following calls are OK even if dll is NULL.
  338.      The default value 0x1000 is returned by pe_text_section_offset
  339.      in that case.  */
  340.   text_offset = pe_text_section_offset (dll);
  341.   gdb_bfd_unref (dll);
  342.   obstack_grow_str (obstack, paddress (gdbarch, load_addr + text_offset));
  343.   obstack_grow_str (obstack, "\"/></library>");
  344. }

  345. /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
  346.    method.  It searches all objfiles, starting with CURRENT_OBJFILE
  347.    first (if not NULL).

  348.    On Windows, the system behaves a little differently when two
  349.    objfiles each define a global symbol using the same name, compared
  350.    to other platforms such as GNU/Linux for instance.  On GNU/Linux,
  351.    all instances of the symbol effectively get merged into a single
  352.    one, but on Windows, they remain distinct.

  353.    As a result, it usually makes sense to start global symbol searches
  354.    with the current objfile before expanding it to all other objfiles.
  355.    This helps for instance when a user debugs some code in a DLL that
  356.    refers to a global variable defined inside that DLL.  When trying
  357.    to print the value of that global variable, it would be unhelpful
  358.    to print the value of another global variable defined with the same
  359.    name, but in a different DLL.  */

  360. static void
  361. windows_iterate_over_objfiles_in_search_order
  362.   (struct gdbarch *gdbarch,
  363.    iterate_over_objfiles_in_search_order_cb_ftype *cb,
  364.    void *cb_data, struct objfile *current_objfile)
  365. {
  366.   int stop;
  367.   struct objfile *objfile;

  368.   if (current_objfile)
  369.     {
  370.       stop = cb (current_objfile, cb_data);
  371.       if (stop)
  372.         return;
  373.     }

  374.   ALL_OBJFILES (objfile)
  375.     {
  376.       if (objfile != current_objfile)
  377.         {
  378.           stop = cb (objfile, cb_data);
  379.           if (stop)
  380.             return;
  381.         }
  382.     }
  383. }

  384. static void
  385. show_maint_show_all_tib (struct ui_file *file, int from_tty,
  386.                 struct cmd_list_element *c, const char *value)
  387. {
  388.   fprintf_filtered (file, _("Show all non-zero elements of "
  389.                             "Thread Information Block is %s.\n"), value);
  390. }

  391. static void
  392. info_w32_command (char *args, int from_tty)
  393. {
  394.   help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
  395. }

  396. static int w32_prefix_command_valid = 0;
  397. void
  398. init_w32_command_list (void)
  399. {
  400.   if (!w32_prefix_command_valid)
  401.     {
  402.       add_prefix_cmd ("w32", class_info, info_w32_command,
  403.                       _("Print information specific to Win32 debugging."),
  404.                       &info_w32_cmdlist, "info w32 ", 0, &infolist);
  405.       w32_prefix_command_valid = 1;
  406.     }
  407. }

  408. /* To be called from the various GDB_OSABI_CYGWIN handlers for the
  409.    various Windows architectures and machine types.  */

  410. void
  411. windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  412. {
  413.   /* Canonical paths on this target look like
  414.      `c:\Program Files\Foo App\mydll.dll', for example.  */
  415.   set_gdbarch_has_dos_based_file_system (gdbarch, 1);

  416.   set_gdbarch_iterate_over_objfiles_in_search_order
  417.     (gdbarch, windows_iterate_over_objfiles_in_search_order);

  418.   set_solib_ops (gdbarch, &solib_target_so_ops);
  419. }

  420. /* Provide a prototype to silence -Wmissing-prototypes.  */
  421. extern initialize_file_ftype _initialize_windows_tdep;

  422. /* Implementation of `tlb' variable.  */

  423. static const struct internalvar_funcs tlb_funcs =
  424. {
  425.   tlb_make_value,
  426.   NULL,
  427.   NULL
  428. };

  429. void
  430. _initialize_windows_tdep (void)
  431. {
  432.   init_w32_command_list ();
  433.   add_cmd ("thread-information-block", class_info, display_tib,
  434.            _("Display thread information block."),
  435.            &info_w32_cmdlist);
  436.   add_alias_cmd ("tib", "thread-information-block", class_info, 1,
  437.                  &info_w32_cmdlist);

  438.   add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
  439.                            &maint_display_all_tib, _("\
  440. Set whether to display all non-zero fields of thread information block."), _("\
  441. Show whether to display all non-zero fields of thread information block."), _("\
  442. Use \"on\" to enable, \"off\" to disable.\n\
  443. If enabled, all non-zero fields of thread information block are displayed,\n\
  444. even if their meaning is unknown."),
  445.                            NULL,
  446.                            show_maint_show_all_tib,
  447.                            &maintenance_set_cmdlist,
  448.                            &maintenance_show_cmdlist);

  449.   /* Explicitly create without lookup, since that tries to create a
  450.      value with a void typed value, and when we get here, gdbarch
  451.      isn't initialized yet.  At this point, we're quite sure there
  452.      isn't another convenience variable of the same name.  */
  453.   create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);
  454. }