1 /* Copyright (C) 2008-2016 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "windows-tdep.h"
20 #include "gdb_obstack.h"
21 #include "xml-support.h"
28 #include "gdbthread.h"
31 #include "coff-pe-read.h"
33 #include "complaints.h"
35 #include "solib-target.h"
38 struct cmd_list_element *info_w32_cmdlist;
40 typedef struct thread_information_block_32
42 uint32_t current_seh; /* %fs:0x0000 */
43 uint32_t current_top_of_stack; /* %fs:0x0004 */
44 uint32_t current_bottom_of_stack; /* %fs:0x0008 */
45 uint32_t sub_system_tib; /* %fs:0x000c */
46 uint32_t fiber_data; /* %fs:0x0010 */
47 uint32_t arbitrary_data_slot; /* %fs:0x0014 */
48 uint32_t linear_address_tib; /* %fs:0x0018 */
49 uint32_t environment_pointer; /* %fs:0x001c */
50 uint32_t process_id; /* %fs:0x0020 */
51 uint32_t current_thread_id; /* %fs:0x0024 */
52 uint32_t active_rpc_handle; /* %fs:0x0028 */
53 uint32_t thread_local_storage; /* %fs:0x002c */
54 uint32_t process_environment_block; /* %fs:0x0030 */
55 uint32_t last_error_number; /* %fs:0x0034 */
57 thread_information_32;
59 typedef struct thread_information_block_64
61 uint64_t current_seh; /* %gs:0x0000 */
62 uint64_t current_top_of_stack; /* %gs:0x0008 */
63 uint64_t current_bottom_of_stack; /* %gs:0x0010 */
64 uint64_t sub_system_tib; /* %gs:0x0018 */
65 uint64_t fiber_data; /* %gs:0x0020 */
66 uint64_t arbitrary_data_slot; /* %gs:0x0028 */
67 uint64_t linear_address_tib; /* %gs:0x0030 */
68 uint64_t environment_pointer; /* %gs:0x0038 */
69 uint64_t process_id; /* %gs:0x0040 */
70 uint64_t current_thread_id; /* %gs:0x0048 */
71 uint64_t active_rpc_handle; /* %gs:0x0050 */
72 uint64_t thread_local_storage; /* %gs:0x0058 */
73 uint64_t process_environment_block; /* %gs:0x0060 */
74 uint64_t last_error_number; /* %gs:0x0068 */
76 thread_information_64;
79 static const char* TIB_NAME[] =
81 " current_seh ", /* %fs:0x0000 */
82 " current_top_of_stack ", /* %fs:0x0004 */
83 " current_bottom_of_stack ", /* %fs:0x0008 */
84 " sub_system_tib ", /* %fs:0x000c */
85 " fiber_data ", /* %fs:0x0010 */
86 " arbitrary_data_slot ", /* %fs:0x0014 */
87 " linear_address_tib ", /* %fs:0x0018 */
88 " environment_pointer ", /* %fs:0x001c */
89 " process_id ", /* %fs:0x0020 */
90 " current_thread_id ", /* %fs:0x0024 */
91 " active_rpc_handle ", /* %fs:0x0028 */
92 " thread_local_storage ", /* %fs:0x002c */
93 " process_environment_block ", /* %fs:0x0030 */
94 " last_error_number " /* %fs:0x0034 */
97 static const int MAX_TIB32 =
98 sizeof (thread_information_32) / sizeof (uint32_t);
99 static const int MAX_TIB64 =
100 sizeof (thread_information_64) / sizeof (uint64_t);
101 static const int FULL_TIB_SIZE = 0x1000;
103 static int maint_display_all_tib = 0;
105 /* Define Thread Local Base pointer type. */
108 windows_get_tlb_type (struct gdbarch *gdbarch)
110 static struct gdbarch *last_gdbarch = NULL;
111 static struct type *last_tlb_type = NULL;
112 struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
113 struct type *peb_ldr_type, *peb_ldr_ptr_type;
114 struct type *peb_type, *peb_ptr_type, *list_type, *list_ptr_type;
115 struct type *module_list_ptr_type;
116 struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
118 /* Do not rebuild type if same gdbarch as last time. */
119 if (last_tlb_type && last_gdbarch == gdbarch)
120 return last_tlb_type;
122 dword_ptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
124 dword32_type = arch_integer_type (gdbarch, 32,
126 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
130 list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
131 TYPE_NAME (list_type) = xstrdup ("list");
133 list_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
134 TYPE_LENGTH (void_ptr_type), NULL);
136 module_list_ptr_type = void_ptr_type;
138 append_composite_type_field (list_type, "forward_list",
139 module_list_ptr_type);
140 append_composite_type_field (list_type, "backward_list",
141 module_list_ptr_type);
143 /* Structured Exception Handler */
145 seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
146 TYPE_NAME (seh_type) = xstrdup ("seh");
148 seh_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
149 TYPE_LENGTH (void_ptr_type), NULL);
150 TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;
152 append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
153 append_composite_type_field (seh_type, "handler",
154 builtin_type (gdbarch)->builtin_func_ptr);
156 /* struct _PEB_LDR_DATA */
157 peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
158 TYPE_NAME (peb_ldr_type) = xstrdup ("peb_ldr_data");
160 append_composite_type_field (peb_ldr_type, "length", dword32_type);
161 append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
162 append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
163 append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
164 append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
165 append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
166 append_composite_type_field (peb_ldr_type, "entry_in_progress",
168 peb_ldr_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
169 TYPE_LENGTH (void_ptr_type), NULL);
170 TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;
173 /* struct process environment block */
174 peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
175 TYPE_NAME (peb_type) = xstrdup ("peb");
177 /* First bytes contain several flags. */
178 append_composite_type_field (peb_type, "flags", dword_ptr_type);
179 append_composite_type_field (peb_type, "mutant", void_ptr_type);
180 append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
181 append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
182 append_composite_type_field (peb_type, "process_parameters", void_ptr_type);
183 append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
184 append_composite_type_field (peb_type, "process_heap", void_ptr_type);
185 append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
186 peb_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
187 TYPE_LENGTH (void_ptr_type), NULL);
188 TYPE_TARGET_TYPE (peb_ptr_type) = peb_type;
191 /* struct thread information block */
192 tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
193 TYPE_NAME (tib_type) = xstrdup ("tib");
195 /* uint32_t current_seh; %fs:0x0000 */
196 append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
197 /* uint32_t current_top_of_stack; %fs:0x0004 */
198 append_composite_type_field (tib_type, "current_top_of_stack",
200 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
201 append_composite_type_field (tib_type, "current_bottom_of_stack",
203 /* uint32_t sub_system_tib; %fs:0x000c */
204 append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);
206 /* uint32_t fiber_data; %fs:0x0010 */
207 append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
208 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
209 append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
210 /* uint32_t linear_address_tib; %fs:0x0018 */
211 append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
212 /* uint32_t environment_pointer; %fs:0x001c */
213 append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
214 /* uint32_t process_id; %fs:0x0020 */
215 append_composite_type_field (tib_type, "process_id", dword_ptr_type);
216 /* uint32_t current_thread_id; %fs:0x0024 */
217 append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
218 /* uint32_t active_rpc_handle; %fs:0x0028 */
219 append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
220 /* uint32_t thread_local_storage; %fs:0x002c */
221 append_composite_type_field (tib_type, "thread_local_storage",
223 /* uint32_t process_environment_block; %fs:0x0030 */
224 append_composite_type_field (tib_type, "process_environment_block",
226 /* uint32_t last_error_number; %fs:0x0034 */
227 append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);
229 tib_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
230 TYPE_LENGTH (void_ptr_type), NULL);
231 TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;
233 last_tlb_type = tib_ptr_type;
234 last_gdbarch = gdbarch;
239 /* The $_tlb convenience variable is a bit special. We don't know
240 for sure the type of the value until we actually have a chance to
241 fetch the data. The type can change depending on gdbarch, so it is
242 also dependent on which thread you have selected. */
244 /* This function implements the lval_computed support for reading a
248 tlb_value_read (struct value *val)
251 struct type *type = check_typedef (value_type (val));
253 if (!target_get_tib_address (inferior_ptid, &tlb))
254 error (_("Unable to read tlb"));
255 store_typed_address (value_contents_raw (val), type, tlb);
258 /* This function implements the lval_computed support for writing a
262 tlb_value_write (struct value *v, struct value *fromval)
264 error (_("Impossible to change the Thread Local Base"));
267 static const struct lval_funcs tlb_value_funcs =
274 /* Return a new value with the correct type for the tlb object of
275 the current thread using architecture GDBARCH. Return a void value
276 if there's no object available. */
278 static struct value *
279 tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
281 if (target_has_stack && !ptid_equal (inferior_ptid, null_ptid))
283 struct type *type = windows_get_tlb_type (gdbarch);
284 return allocate_computed_value (type, &tlb_value_funcs, NULL);
287 return allocate_value (builtin_type (gdbarch)->builtin_void);
291 /* Display thread information block of a given thread. */
294 display_one_tib (ptid_t ptid)
296 gdb_byte *tib = NULL;
298 CORE_ADDR thread_local_base;
299 ULONGEST i, val, max, max_name, size, tib_size;
300 ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch ());
301 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
303 if (sizeof_ptr == 64)
305 size = sizeof (uint64_t);
306 tib_size = sizeof (thread_information_64);
311 size = sizeof (uint32_t);
312 tib_size = sizeof (thread_information_32);
318 if (maint_display_all_tib)
320 tib_size = FULL_TIB_SIZE;
321 max = tib_size / size;
324 tib = (gdb_byte *) alloca (tib_size);
326 if (target_get_tib_address (ptid, &thread_local_base) == 0)
328 printf_filtered (_("Unable to get thread local base for %s\n"),
329 target_pid_to_str (ptid));
333 if (target_read (¤t_target, TARGET_OBJECT_MEMORY,
334 NULL, tib, thread_local_base, tib_size) != tib_size)
336 printf_filtered (_("Unable to read thread information "
337 "block for %s at address %s\n"),
338 target_pid_to_str (ptid),
339 paddress (target_gdbarch (), thread_local_base));
343 printf_filtered (_("Thread Information Block %s at %s\n"),
344 target_pid_to_str (ptid),
345 paddress (target_gdbarch (), thread_local_base));
347 index = (gdb_byte *) tib;
349 /* All fields have the size of a pointer, this allows to iterate
350 using the same for loop for both layouts. */
351 for (i = 0; i < max; i++)
353 val = extract_unsigned_integer (index, size, byte_order);
355 printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
357 printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
364 /* Display thread information block of the current thread. */
367 display_tib (char * args, int from_tty)
369 if (!ptid_equal (inferior_ptid, null_ptid))
370 display_one_tib (inferior_ptid);
374 windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
375 struct gdbarch *gdbarch, struct obstack *obstack)
379 CORE_ADDR text_offset;
381 obstack_grow_str (obstack, "<library name=\"");
382 p = xml_escape_text (so_name);
383 obstack_grow_str (obstack, p);
385 obstack_grow_str (obstack, "\"><segment address=\"");
386 dll = gdb_bfd_open (so_name, gnutarget, -1);
387 /* The following calls are OK even if dll is NULL.
388 The default value 0x1000 is returned by pe_text_section_offset
390 text_offset = pe_text_section_offset (dll);
392 obstack_grow_str (obstack, paddress (gdbarch, load_addr + text_offset));
393 obstack_grow_str (obstack, "\"/></library>");
396 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
397 method. It searches all objfiles, starting with CURRENT_OBJFILE
400 On Windows, the system behaves a little differently when two
401 objfiles each define a global symbol using the same name, compared
402 to other platforms such as GNU/Linux for instance. On GNU/Linux,
403 all instances of the symbol effectively get merged into a single
404 one, but on Windows, they remain distinct.
406 As a result, it usually makes sense to start global symbol searches
407 with the current objfile before expanding it to all other objfiles.
408 This helps for instance when a user debugs some code in a DLL that
409 refers to a global variable defined inside that DLL. When trying
410 to print the value of that global variable, it would be unhelpful
411 to print the value of another global variable defined with the same
412 name, but in a different DLL. */
415 windows_iterate_over_objfiles_in_search_order
416 (struct gdbarch *gdbarch,
417 iterate_over_objfiles_in_search_order_cb_ftype *cb,
418 void *cb_data, struct objfile *current_objfile)
421 struct objfile *objfile;
425 stop = cb (current_objfile, cb_data);
430 ALL_OBJFILES (objfile)
432 if (objfile != current_objfile)
434 stop = cb (objfile, cb_data);
442 show_maint_show_all_tib (struct ui_file *file, int from_tty,
443 struct cmd_list_element *c, const char *value)
445 fprintf_filtered (file, _("Show all non-zero elements of "
446 "Thread Information Block is %s.\n"), value);
450 info_w32_command (char *args, int from_tty)
452 help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
455 static int w32_prefix_command_valid = 0;
457 init_w32_command_list (void)
459 if (!w32_prefix_command_valid)
461 add_prefix_cmd ("w32", class_info, info_w32_command,
462 _("Print information specific to Win32 debugging."),
463 &info_w32_cmdlist, "info w32 ", 0, &infolist);
464 w32_prefix_command_valid = 1;
468 /* To be called from the various GDB_OSABI_CYGWIN handlers for the
469 various Windows architectures and machine types. */
472 windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
474 /* Canonical paths on this target look like
475 `c:\Program Files\Foo App\mydll.dll', for example. */
476 set_gdbarch_has_dos_based_file_system (gdbarch, 1);
478 set_gdbarch_iterate_over_objfiles_in_search_order
479 (gdbarch, windows_iterate_over_objfiles_in_search_order);
481 set_solib_ops (gdbarch, &solib_target_so_ops);
484 /* Provide a prototype to silence -Wmissing-prototypes. */
485 extern initialize_file_ftype _initialize_windows_tdep;
487 /* Implementation of `tlb' variable. */
489 static const struct internalvar_funcs tlb_funcs =
497 _initialize_windows_tdep (void)
499 init_w32_command_list ();
500 add_cmd ("thread-information-block", class_info, display_tib,
501 _("Display thread information block."),
503 add_alias_cmd ("tib", "thread-information-block", class_info, 1,
506 add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
507 &maint_display_all_tib, _("\
508 Set whether to display all non-zero fields of thread information block."), _("\
509 Show whether to display all non-zero fields of thread information block."), _("\
510 Use \"on\" to enable, \"off\" to disable.\n\
511 If enabled, all non-zero fields of thread information block are displayed,\n\
512 even if their meaning is unknown."),
514 show_maint_show_all_tib,
515 &maintenance_set_cmdlist,
516 &maintenance_show_cmdlist);
518 /* Explicitly create without lookup, since that tries to create a
519 value with a void typed value, and when we get here, gdbarch
520 isn't initialized yet. At this point, we're quite sure there
521 isn't another convenience variable of the same name. */
522 create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);