GDB (API)
/home/stan/gdb/src/gdb/windows-tdep.c
Go to the documentation of this file.
00001 /* Copyright (C) 2008-2013 Free Software Foundation, Inc.
00002 
00003    This file is part of GDB.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00017 
00018 #include "defs.h"
00019 #include "windows-tdep.h"
00020 #include "gdb_obstack.h"
00021 #include "xml-support.h"
00022 #include "gdbarch.h"
00023 #include "target.h"
00024 #include "value.h"
00025 #include "inferior.h"
00026 #include "command.h"
00027 #include "gdbcmd.h"
00028 #include "gdbthread.h"
00029 #include "objfiles.h"
00030 #include "symfile.h"
00031 #include "coff-pe-read.h"
00032 #include "gdb_bfd.h"
00033 #include "complaints.h"
00034 #include "solib.h"
00035 #include "solib-target.h"
00036 
00037 struct cmd_list_element *info_w32_cmdlist;
00038 
00039 typedef struct thread_information_block_32
00040   {
00041     uint32_t current_seh;                       /* %fs:0x0000 */
00042     uint32_t current_top_of_stack;              /* %fs:0x0004 */
00043     uint32_t current_bottom_of_stack;           /* %fs:0x0008 */
00044     uint32_t sub_system_tib;                    /* %fs:0x000c */
00045     uint32_t fiber_data;                        /* %fs:0x0010 */
00046     uint32_t arbitrary_data_slot;               /* %fs:0x0014 */
00047     uint32_t linear_address_tib;                /* %fs:0x0018 */
00048     uint32_t environment_pointer;               /* %fs:0x001c */
00049     uint32_t process_id;                        /* %fs:0x0020 */
00050     uint32_t current_thread_id;                 /* %fs:0x0024 */
00051     uint32_t active_rpc_handle;                 /* %fs:0x0028 */
00052     uint32_t thread_local_storage;              /* %fs:0x002c */
00053     uint32_t process_environment_block;         /* %fs:0x0030 */
00054     uint32_t last_error_number;                 /* %fs:0x0034 */
00055   }
00056 thread_information_32;
00057 
00058 typedef struct thread_information_block_64
00059   {
00060     uint64_t current_seh;                       /* %gs:0x0000 */
00061     uint64_t current_top_of_stack;              /* %gs:0x0008 */
00062     uint64_t current_bottom_of_stack;           /* %gs:0x0010 */
00063     uint64_t sub_system_tib;                    /* %gs:0x0018 */
00064     uint64_t fiber_data;                        /* %gs:0x0020 */
00065     uint64_t arbitrary_data_slot;               /* %gs:0x0028 */
00066     uint64_t linear_address_tib;                /* %gs:0x0030 */
00067     uint64_t environment_pointer;               /* %gs:0x0038 */
00068     uint64_t process_id;                        /* %gs:0x0040 */
00069     uint64_t current_thread_id;                 /* %gs:0x0048 */
00070     uint64_t active_rpc_handle;                 /* %gs:0x0050 */
00071     uint64_t thread_local_storage;              /* %gs:0x0058 */
00072     uint64_t process_environment_block;         /* %gs:0x0060 */
00073     uint64_t last_error_number;                 /* %gs:0x0068 */
00074   }
00075 thread_information_64;
00076 
00077 
00078 static const char* TIB_NAME[] =
00079   {
00080     " current_seh                 ",    /* %fs:0x0000 */
00081     " current_top_of_stack        ",    /* %fs:0x0004 */
00082     " current_bottom_of_stack     ",    /* %fs:0x0008 */
00083     " sub_system_tib              ",    /* %fs:0x000c */
00084     " fiber_data                  ",    /* %fs:0x0010 */
00085     " arbitrary_data_slot         ",    /* %fs:0x0014 */
00086     " linear_address_tib          ",    /* %fs:0x0018 */
00087     " environment_pointer         ",    /* %fs:0x001c */
00088     " process_id                  ",    /* %fs:0x0020 */
00089     " current_thread_id           ",    /* %fs:0x0024 */
00090     " active_rpc_handle           ",    /* %fs:0x0028 */
00091     " thread_local_storage        ",    /* %fs:0x002c */
00092     " process_environment_block   ",    /* %fs:0x0030 */
00093     " last_error_number           "     /* %fs:0x0034 */
00094   };
00095 
00096 static const int MAX_TIB32 =
00097   sizeof (thread_information_32) / sizeof (uint32_t);
00098 static const int MAX_TIB64 =
00099   sizeof (thread_information_64) / sizeof (uint64_t);
00100 static const int FULL_TIB_SIZE = 0x1000;
00101 
00102 static int maint_display_all_tib = 0;
00103 
00104 /* Define Thread Local Base pointer type.  */
00105 
00106 static struct type *
00107 windows_get_tlb_type (struct gdbarch *gdbarch)
00108 {
00109   static struct gdbarch *last_gdbarch = NULL;
00110   static struct type *last_tlb_type = NULL;
00111   struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
00112   struct type *peb_ldr_type, *peb_ldr_ptr_type;
00113   struct type *peb_type, *peb_ptr_type, *list_type, *list_ptr_type;
00114   struct type *module_list_ptr_type;
00115   struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
00116 
00117   /* Do not rebuild type if same gdbarch as last time.  */
00118   if (last_tlb_type && last_gdbarch == gdbarch)
00119     return last_tlb_type;
00120   
00121   dword_ptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
00122                                  1, "DWORD_PTR");
00123   dword32_type = arch_integer_type (gdbarch, 32,
00124                                  1, "DWORD32");
00125   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
00126 
00127   /* list entry */
00128 
00129   list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00130   TYPE_NAME (list_type) = xstrdup ("list");
00131 
00132   list_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
00133                             TYPE_LENGTH (void_ptr_type), NULL);
00134 
00135   module_list_ptr_type = void_ptr_type;
00136 
00137   append_composite_type_field (list_type, "forward_list",
00138                                module_list_ptr_type);
00139   append_composite_type_field (list_type, "backward_list",
00140                                module_list_ptr_type);
00141 
00142   /* Structured Exception Handler */
00143 
00144   seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00145   TYPE_NAME (seh_type) = xstrdup ("seh");
00146 
00147   seh_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
00148                             TYPE_LENGTH (void_ptr_type), NULL);
00149   TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;
00150 
00151   append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
00152   append_composite_type_field (seh_type, "handler",
00153                                builtin_type (gdbarch)->builtin_func_ptr);
00154 
00155   /* struct _PEB_LDR_DATA */
00156   peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00157   TYPE_NAME (peb_ldr_type) = xstrdup ("peb_ldr_data");
00158 
00159   append_composite_type_field (peb_ldr_type, "length", dword32_type);
00160   append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
00161   append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
00162   append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
00163   append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
00164   append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
00165   append_composite_type_field (peb_ldr_type, "entry_in_progress",
00166                                void_ptr_type);
00167   peb_ldr_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
00168                             TYPE_LENGTH (void_ptr_type), NULL);
00169   TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;
00170 
00171 
00172   /* struct process environment block */
00173   peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00174   TYPE_NAME (peb_type) = xstrdup ("peb");
00175 
00176   /* First bytes contain several flags.  */
00177   append_composite_type_field (peb_type, "flags", dword_ptr_type);
00178   append_composite_type_field (peb_type, "mutant", void_ptr_type);
00179   append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
00180   append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
00181   append_composite_type_field (peb_type, "process_parameters", void_ptr_type);
00182   append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
00183   append_composite_type_field (peb_type, "process_heap", void_ptr_type);
00184   append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
00185   peb_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
00186                             TYPE_LENGTH (void_ptr_type), NULL);
00187   TYPE_TARGET_TYPE (peb_ptr_type) = peb_type;
00188 
00189 
00190   /* struct thread information block */
00191   tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00192   TYPE_NAME (tib_type) = xstrdup ("tib");
00193 
00194   /* uint32_t current_seh;                      %fs:0x0000 */
00195   append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
00196   /* uint32_t current_top_of_stack;             %fs:0x0004 */
00197   append_composite_type_field (tib_type, "current_top_of_stack",
00198                                void_ptr_type);
00199   /* uint32_t current_bottom_of_stack;          %fs:0x0008 */
00200   append_composite_type_field (tib_type, "current_bottom_of_stack",
00201                                void_ptr_type);
00202   /* uint32_t sub_system_tib;                   %fs:0x000c */
00203   append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);
00204 
00205   /* uint32_t fiber_data;                       %fs:0x0010 */
00206   append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
00207   /* uint32_t arbitrary_data_slot;              %fs:0x0014 */
00208   append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
00209   /* uint32_t linear_address_tib;               %fs:0x0018 */
00210   append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
00211   /* uint32_t environment_pointer;              %fs:0x001c */
00212   append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
00213   /* uint32_t process_id;                       %fs:0x0020 */
00214   append_composite_type_field (tib_type, "process_id", dword_ptr_type);
00215   /* uint32_t current_thread_id;                %fs:0x0024 */
00216   append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
00217   /* uint32_t active_rpc_handle;                %fs:0x0028 */
00218   append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
00219   /* uint32_t thread_local_storage;             %fs:0x002c */
00220   append_composite_type_field (tib_type, "thread_local_storage",
00221                                void_ptr_type);
00222   /* uint32_t process_environment_block;        %fs:0x0030 */
00223   append_composite_type_field (tib_type, "process_environment_block",
00224                                peb_ptr_type);
00225   /* uint32_t last_error_number;                %fs:0x0034 */
00226   append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);
00227 
00228   tib_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
00229                             TYPE_LENGTH (void_ptr_type), NULL);
00230   TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;
00231 
00232   last_tlb_type = tib_ptr_type;
00233   last_gdbarch = gdbarch;
00234 
00235   return tib_ptr_type;
00236 }
00237 
00238 /* The $_tlb convenience variable is a bit special.  We don't know
00239    for sure the type of the value until we actually have a chance to
00240    fetch the data.  The type can change depending on gdbarch, so it is
00241    also dependent on which thread you have selected.  */
00242 
00243 /* This function implements the lval_computed support for reading a
00244    $_tlb value.  */
00245 
00246 static void
00247 tlb_value_read (struct value *val)
00248 {
00249   CORE_ADDR tlb;
00250   struct type *type = check_typedef (value_type (val));
00251 
00252   if (!target_get_tib_address (inferior_ptid, &tlb))
00253     error (_("Unable to read tlb"));
00254   store_typed_address (value_contents_raw (val), type, tlb);
00255 }
00256 
00257 /* This function implements the lval_computed support for writing a
00258    $_tlb value.  */
00259 
00260 static void
00261 tlb_value_write (struct value *v, struct value *fromval)
00262 {
00263   error (_("Impossible to change the Thread Local Base"));
00264 }
00265 
00266 static const struct lval_funcs tlb_value_funcs =
00267   {
00268     tlb_value_read,
00269     tlb_value_write
00270   };
00271 
00272 
00273 /* Return a new value with the correct type for the tlb object of
00274    the current thread using architecture GDBARCH.  Return a void value
00275    if there's no object available.  */
00276 
00277 static struct value *
00278 tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
00279 {
00280   if (target_has_stack && !ptid_equal (inferior_ptid, null_ptid))
00281     {
00282       struct type *type = windows_get_tlb_type (gdbarch);
00283       return allocate_computed_value (type, &tlb_value_funcs, NULL);
00284     }
00285 
00286   return allocate_value (builtin_type (gdbarch)->builtin_void);
00287 }
00288 
00289 
00290 /* Display thread information block of a given thread.  */
00291 
00292 static int
00293 display_one_tib (ptid_t ptid)
00294 {
00295   gdb_byte *tib = NULL;
00296   gdb_byte *index;
00297   CORE_ADDR thread_local_base;
00298   ULONGEST i, val, max, max_name, size, tib_size;
00299   ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch ());
00300   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00301 
00302   if (sizeof_ptr == 64)
00303     {
00304       size = sizeof (uint64_t);
00305       tib_size = sizeof (thread_information_64);
00306       max = MAX_TIB64;
00307     }
00308   else
00309     {
00310       size = sizeof (uint32_t);
00311       tib_size = sizeof (thread_information_32);
00312       max = MAX_TIB32;
00313     }
00314 
00315   max_name = max;
00316 
00317   if (maint_display_all_tib)
00318     {
00319       tib_size = FULL_TIB_SIZE;
00320       max = tib_size / size;
00321     }
00322   
00323   tib = alloca (tib_size);
00324 
00325   if (target_get_tib_address (ptid, &thread_local_base) == 0)
00326     {
00327       printf_filtered (_("Unable to get thread local base for %s\n"),
00328         target_pid_to_str (ptid));
00329       return -1;
00330     }
00331 
00332   if (target_read (&current_target, TARGET_OBJECT_MEMORY,
00333                    NULL, tib, thread_local_base, tib_size) != tib_size)
00334     {
00335       printf_filtered (_("Unable to read thread information "
00336                          "block for %s at address %s\n"),
00337         target_pid_to_str (ptid), 
00338         paddress (target_gdbarch (), thread_local_base));
00339       return -1;
00340     }
00341 
00342   printf_filtered (_("Thread Information Block %s at %s\n"),
00343                    target_pid_to_str (ptid),
00344                    paddress (target_gdbarch (), thread_local_base));
00345 
00346   index = (gdb_byte *) tib;
00347 
00348   /* All fields have the size of a pointer, this allows to iterate 
00349      using the same for loop for both layouts.  */
00350   for (i = 0; i < max; i++)
00351     {
00352       val = extract_unsigned_integer (index, size, byte_order);
00353       if (i < max_name)
00354         printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
00355       else if (val != 0)
00356         printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
00357                          phex (val, size));
00358       index += size;
00359     } 
00360   return 1;  
00361 }
00362 
00363 /* Display thread information block of a thread specified by ARGS.
00364    If ARGS is empty, display thread information block of current_thread
00365    if current_thread is non NULL.
00366    Otherwise ARGS is parsed and converted to a integer that should
00367    be the windows ThreadID (not the internal GDB thread ID).  */
00368 
00369 static void
00370 display_tib (char * args, int from_tty)
00371 {
00372   if (args)
00373     {
00374       struct thread_info *tp;
00375       int gdb_id = value_as_long (parse_and_eval (args));
00376 
00377       tp = find_thread_id (gdb_id);
00378 
00379       if (!tp)
00380         error (_("Thread ID %d not known."), gdb_id);
00381 
00382       if (!target_thread_alive (tp->ptid))
00383         error (_("Thread ID %d has terminated."), gdb_id);
00384 
00385       display_one_tib (tp->ptid);
00386     }
00387   else if (!ptid_equal (inferior_ptid, null_ptid))
00388     display_one_tib (inferior_ptid);
00389 }
00390 
00391 void
00392 windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
00393                              struct gdbarch *gdbarch, struct obstack *obstack)
00394 {
00395   char *p;
00396   struct bfd * dll;
00397   CORE_ADDR text_offset;
00398 
00399   obstack_grow_str (obstack, "<library name=\"");
00400   p = xml_escape_text (so_name);
00401   obstack_grow_str (obstack, p);
00402   xfree (p);
00403   obstack_grow_str (obstack, "\"><segment address=\"");
00404   dll = gdb_bfd_open_maybe_remote (so_name);
00405   /* The following calls are OK even if dll is NULL.
00406      The default value 0x1000 is returned by pe_text_section_offset
00407      in that case.  */
00408   text_offset = pe_text_section_offset (dll);
00409   gdb_bfd_unref (dll);
00410   obstack_grow_str (obstack, paddress (gdbarch, load_addr + text_offset));
00411   obstack_grow_str (obstack, "\"/></library>");
00412 }
00413 
00414 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
00415    method.  It searches all objfiles, starting with CURRENT_OBJFILE
00416    first (if not NULL).
00417 
00418    On Windows, the system behaves a little differently when two
00419    objfiles each define a global symbol using the same name, compared
00420    to other platforms such as GNU/Linux for instance.  On GNU/Linux,
00421    all instances of the symbol effectively get merged into a single
00422    one, but on Windows, they remain distinct.
00423 
00424    As a result, it usually makes sense to start global symbol searches
00425    with the current objfile before expanding it to all other objfiles.
00426    This helps for instance when a user debugs some code in a DLL that
00427    refers to a global variable defined inside that DLL.  When trying
00428    to print the value of that global variable, it would be unhelpful
00429    to print the value of another global variable defined with the same
00430    name, but in a different DLL.  */
00431 
00432 static void
00433 windows_iterate_over_objfiles_in_search_order
00434   (struct gdbarch *gdbarch,
00435    iterate_over_objfiles_in_search_order_cb_ftype *cb,
00436    void *cb_data, struct objfile *current_objfile)
00437 {
00438   int stop;
00439   struct objfile *objfile;
00440 
00441   if (current_objfile)
00442     {
00443       stop = cb (current_objfile, cb_data);
00444       if (stop)
00445         return;
00446     }
00447 
00448   ALL_OBJFILES (objfile)
00449     {
00450       if (objfile != current_objfile)
00451         {
00452           stop = cb (objfile, cb_data);
00453           if (stop)
00454             return;
00455         }
00456     }
00457 }
00458 
00459 static void
00460 show_maint_show_all_tib (struct ui_file *file, int from_tty,
00461                 struct cmd_list_element *c, const char *value)
00462 {
00463   fprintf_filtered (file, _("Show all non-zero elements of "
00464                             "Thread Information Block is %s.\n"), value);
00465 }
00466 
00467 static void
00468 info_w32_command (char *args, int from_tty)
00469 {
00470   help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
00471 }
00472 
00473 static int w32_prefix_command_valid = 0;
00474 void
00475 init_w32_command_list (void)
00476 {
00477   if (!w32_prefix_command_valid)
00478     {
00479       add_prefix_cmd ("w32", class_info, info_w32_command,
00480                       _("Print information specific to Win32 debugging."),
00481                       &info_w32_cmdlist, "info w32 ", 0, &infolist);
00482       w32_prefix_command_valid = 1;
00483     }
00484 }
00485 
00486 /* To be called from the various GDB_OSABI_CYGWIN handlers for the
00487    various Windows architectures and machine types.  */
00488 
00489 void
00490 windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
00491 {
00492   /* Canonical paths on this target look like
00493      `c:\Program Files\Foo App\mydll.dll', for example.  */
00494   set_gdbarch_has_dos_based_file_system (gdbarch, 1);
00495 
00496   set_gdbarch_iterate_over_objfiles_in_search_order
00497     (gdbarch, windows_iterate_over_objfiles_in_search_order);
00498 
00499   set_solib_ops (gdbarch, &solib_target_so_ops);
00500 }
00501 
00502 /* Provide a prototype to silence -Wmissing-prototypes.  */
00503 extern initialize_file_ftype _initialize_windows_tdep;
00504 
00505 /* Implementation of `tlb' variable.  */
00506 
00507 static const struct internalvar_funcs tlb_funcs =
00508 {
00509   tlb_make_value,
00510   NULL,
00511   NULL
00512 };
00513 
00514 void
00515 _initialize_windows_tdep (void)
00516 {
00517   init_w32_command_list ();
00518   add_cmd ("thread-information-block", class_info, display_tib,
00519            _("Display thread information block."),
00520            &info_w32_cmdlist);
00521   add_alias_cmd ("tib", "thread-information-block", class_info, 1,
00522                  &info_w32_cmdlist);
00523 
00524   add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
00525                            &maint_display_all_tib, _("\
00526 Set whether to display all non-zero fields of thread information block."), _("\
00527 Show whether to display all non-zero fields of thread information block."), _("\
00528 Use \"on\" to enable, \"off\" to disable.\n\
00529 If enabled, all non-zero fields of thread information block are displayed,\n\
00530 even if their meaning is unknown."),
00531                            NULL,
00532                            show_maint_show_all_tib,
00533                            &maintenance_set_cmdlist,
00534                            &maintenance_show_cmdlist);
00535 
00536   /* Explicitly create without lookup, since that tries to create a
00537      value with a void typed value, and when we get here, gdbarch
00538      isn't initialized yet.  At this point, we're quite sure there
00539      isn't another convenience variable of the same name.  */
00540   create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);
00541 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines