1 /* Copyright (C) 2008-2020 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 /* Windows signal numbers differ between MinGW flavors and between
39 those and Cygwin. The below enumeration was gleaned from the
40 respective headers; the ones marked with MinGW64/Cygwin are defined
41 only by MinGW64 and Cygwin, not by mingw.org's MinGW. FIXME: We
42 should really have distinct MinGW vs Cygwin OSABIs, and two
43 separate enums, selected at runtime. */
47 WINDOWS_SIGHUP
= 1, /* MinGW64/Cygwin */
49 WINDOWS_SIGQUIT
= 3, /* MinGW64/Cygwin */
51 WINDOWS_SIGTRAP
= 5, /* MinGW64/Cygwin */
55 WINDOWS_SIGIOT
= 6, /* MinGW64 */
57 WINDOWS_SIGEMT
= 7, /* MinGW64/Cygwin */
59 WINDOWS_SIGKILL
= 9, /* MinGW64/Cygwin */
60 WINDOWS_SIGBUS
= 10, /* MinGW64/Cygwin */
62 WINDOWS_SIGSYS
= 12, /* MinGW64/Cygwin */
63 WINDOWS_SIGPIPE
= 13,/* MinGW64/Cygwin */
64 WINDOWS_SIGALRM
= 14,/* MinGW64/Cygwin */
77 WINDOWS_SIGVTALRM
= 26,
79 WINDOWS_SIGWINCH
= 28,
84 WINDOWS_SIGBREAK
= 21,
89 struct cmd_list_element
*info_w32_cmdlist
;
91 typedef struct thread_information_block_32
93 uint32_t current_seh
; /* %fs:0x0000 */
94 uint32_t current_top_of_stack
; /* %fs:0x0004 */
95 uint32_t current_bottom_of_stack
; /* %fs:0x0008 */
96 uint32_t sub_system_tib
; /* %fs:0x000c */
97 uint32_t fiber_data
; /* %fs:0x0010 */
98 uint32_t arbitrary_data_slot
; /* %fs:0x0014 */
99 uint32_t linear_address_tib
; /* %fs:0x0018 */
100 uint32_t environment_pointer
; /* %fs:0x001c */
101 uint32_t process_id
; /* %fs:0x0020 */
102 uint32_t current_thread_id
; /* %fs:0x0024 */
103 uint32_t active_rpc_handle
; /* %fs:0x0028 */
104 uint32_t thread_local_storage
; /* %fs:0x002c */
105 uint32_t process_environment_block
; /* %fs:0x0030 */
106 uint32_t last_error_number
; /* %fs:0x0034 */
108 thread_information_32
;
110 typedef struct thread_information_block_64
112 uint64_t current_seh
; /* %gs:0x0000 */
113 uint64_t current_top_of_stack
; /* %gs:0x0008 */
114 uint64_t current_bottom_of_stack
; /* %gs:0x0010 */
115 uint64_t sub_system_tib
; /* %gs:0x0018 */
116 uint64_t fiber_data
; /* %gs:0x0020 */
117 uint64_t arbitrary_data_slot
; /* %gs:0x0028 */
118 uint64_t linear_address_tib
; /* %gs:0x0030 */
119 uint64_t environment_pointer
; /* %gs:0x0038 */
120 uint64_t process_id
; /* %gs:0x0040 */
121 uint64_t current_thread_id
; /* %gs:0x0048 */
122 uint64_t active_rpc_handle
; /* %gs:0x0050 */
123 uint64_t thread_local_storage
; /* %gs:0x0058 */
124 uint64_t process_environment_block
; /* %gs:0x0060 */
125 uint64_t last_error_number
; /* %gs:0x0068 */
127 thread_information_64
;
130 static const char* TIB_NAME
[] =
132 " current_seh ", /* %fs:0x0000 */
133 " current_top_of_stack ", /* %fs:0x0004 */
134 " current_bottom_of_stack ", /* %fs:0x0008 */
135 " sub_system_tib ", /* %fs:0x000c */
136 " fiber_data ", /* %fs:0x0010 */
137 " arbitrary_data_slot ", /* %fs:0x0014 */
138 " linear_address_tib ", /* %fs:0x0018 */
139 " environment_pointer ", /* %fs:0x001c */
140 " process_id ", /* %fs:0x0020 */
141 " current_thread_id ", /* %fs:0x0024 */
142 " active_rpc_handle ", /* %fs:0x0028 */
143 " thread_local_storage ", /* %fs:0x002c */
144 " process_environment_block ", /* %fs:0x0030 */
145 " last_error_number " /* %fs:0x0034 */
148 static const int MAX_TIB32
=
149 sizeof (thread_information_32
) / sizeof (uint32_t);
150 static const int MAX_TIB64
=
151 sizeof (thread_information_64
) / sizeof (uint64_t);
152 static const int FULL_TIB_SIZE
= 0x1000;
154 static bool maint_display_all_tib
= false;
156 /* Define Thread Local Base pointer type. */
159 windows_get_tlb_type (struct gdbarch
*gdbarch
)
161 static struct gdbarch
*last_gdbarch
= NULL
;
162 static struct type
*last_tlb_type
= NULL
;
163 struct type
*dword_ptr_type
, *dword32_type
, *void_ptr_type
;
164 struct type
*peb_ldr_type
, *peb_ldr_ptr_type
;
165 struct type
*peb_type
, *peb_ptr_type
, *list_type
;
166 struct type
*module_list_ptr_type
;
167 struct type
*tib_type
, *seh_type
, *tib_ptr_type
, *seh_ptr_type
;
169 /* Do not rebuild type if same gdbarch as last time. */
170 if (last_tlb_type
&& last_gdbarch
== gdbarch
)
171 return last_tlb_type
;
173 dword_ptr_type
= arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
175 dword32_type
= arch_integer_type (gdbarch
, 32,
177 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
181 list_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
182 TYPE_NAME (list_type
) = xstrdup ("list");
184 module_list_ptr_type
= void_ptr_type
;
186 append_composite_type_field (list_type
, "forward_list",
187 module_list_ptr_type
);
188 append_composite_type_field (list_type
, "backward_list",
189 module_list_ptr_type
);
191 /* Structured Exception Handler */
193 seh_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
194 TYPE_NAME (seh_type
) = xstrdup ("seh");
196 seh_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
197 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
199 TYPE_TARGET_TYPE (seh_ptr_type
) = seh_type
;
201 append_composite_type_field (seh_type
, "next_seh", seh_ptr_type
);
202 append_composite_type_field (seh_type
, "handler",
203 builtin_type (gdbarch
)->builtin_func_ptr
);
205 /* struct _PEB_LDR_DATA */
206 peb_ldr_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
207 TYPE_NAME (peb_ldr_type
) = xstrdup ("peb_ldr_data");
209 append_composite_type_field (peb_ldr_type
, "length", dword32_type
);
210 append_composite_type_field (peb_ldr_type
, "initialized", dword32_type
);
211 append_composite_type_field (peb_ldr_type
, "ss_handle", void_ptr_type
);
212 append_composite_type_field (peb_ldr_type
, "in_load_order", list_type
);
213 append_composite_type_field (peb_ldr_type
, "in_memory_order", list_type
);
214 append_composite_type_field (peb_ldr_type
, "in_init_order", list_type
);
215 append_composite_type_field (peb_ldr_type
, "entry_in_progress",
217 peb_ldr_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
218 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
220 TYPE_TARGET_TYPE (peb_ldr_ptr_type
) = peb_ldr_type
;
223 /* struct process environment block */
224 peb_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
225 TYPE_NAME (peb_type
) = xstrdup ("peb");
227 /* First bytes contain several flags. */
228 append_composite_type_field (peb_type
, "flags", dword_ptr_type
);
229 append_composite_type_field (peb_type
, "mutant", void_ptr_type
);
230 append_composite_type_field (peb_type
, "image_base_address", void_ptr_type
);
231 append_composite_type_field (peb_type
, "ldr", peb_ldr_ptr_type
);
232 append_composite_type_field (peb_type
, "process_parameters", void_ptr_type
);
233 append_composite_type_field (peb_type
, "sub_system_data", void_ptr_type
);
234 append_composite_type_field (peb_type
, "process_heap", void_ptr_type
);
235 append_composite_type_field (peb_type
, "fast_peb_lock", void_ptr_type
);
236 peb_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
237 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
239 TYPE_TARGET_TYPE (peb_ptr_type
) = peb_type
;
242 /* struct thread information block */
243 tib_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
244 TYPE_NAME (tib_type
) = xstrdup ("tib");
246 /* uint32_t current_seh; %fs:0x0000 */
247 append_composite_type_field (tib_type
, "current_seh", seh_ptr_type
);
248 /* uint32_t current_top_of_stack; %fs:0x0004 */
249 append_composite_type_field (tib_type
, "current_top_of_stack",
251 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
252 append_composite_type_field (tib_type
, "current_bottom_of_stack",
254 /* uint32_t sub_system_tib; %fs:0x000c */
255 append_composite_type_field (tib_type
, "sub_system_tib", void_ptr_type
);
257 /* uint32_t fiber_data; %fs:0x0010 */
258 append_composite_type_field (tib_type
, "fiber_data", void_ptr_type
);
259 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
260 append_composite_type_field (tib_type
, "arbitrary_data_slot", void_ptr_type
);
261 /* uint32_t linear_address_tib; %fs:0x0018 */
262 append_composite_type_field (tib_type
, "linear_address_tib", void_ptr_type
);
263 /* uint32_t environment_pointer; %fs:0x001c */
264 append_composite_type_field (tib_type
, "environment_pointer", void_ptr_type
);
265 /* uint32_t process_id; %fs:0x0020 */
266 append_composite_type_field (tib_type
, "process_id", dword_ptr_type
);
267 /* uint32_t current_thread_id; %fs:0x0024 */
268 append_composite_type_field (tib_type
, "thread_id", dword_ptr_type
);
269 /* uint32_t active_rpc_handle; %fs:0x0028 */
270 append_composite_type_field (tib_type
, "active_rpc_handle", dword_ptr_type
);
271 /* uint32_t thread_local_storage; %fs:0x002c */
272 append_composite_type_field (tib_type
, "thread_local_storage",
274 /* uint32_t process_environment_block; %fs:0x0030 */
275 append_composite_type_field (tib_type
, "process_environment_block",
277 /* uint32_t last_error_number; %fs:0x0034 */
278 append_composite_type_field (tib_type
, "last_error_number", dword_ptr_type
);
280 tib_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
281 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
283 TYPE_TARGET_TYPE (tib_ptr_type
) = tib_type
;
285 last_tlb_type
= tib_ptr_type
;
286 last_gdbarch
= gdbarch
;
291 /* The $_tlb convenience variable is a bit special. We don't know
292 for sure the type of the value until we actually have a chance to
293 fetch the data. The type can change depending on gdbarch, so it is
294 also dependent on which thread you have selected. */
296 /* This function implements the lval_computed support for reading a
300 tlb_value_read (struct value
*val
)
303 struct type
*type
= check_typedef (value_type (val
));
305 if (!target_get_tib_address (inferior_ptid
, &tlb
))
306 error (_("Unable to read tlb"));
307 store_typed_address (value_contents_raw (val
), type
, tlb
);
310 /* This function implements the lval_computed support for writing a
314 tlb_value_write (struct value
*v
, struct value
*fromval
)
316 error (_("Impossible to change the Thread Local Base"));
319 static const struct lval_funcs tlb_value_funcs
=
326 /* Return a new value with the correct type for the tlb object of
327 the current thread using architecture GDBARCH. Return a void value
328 if there's no object available. */
330 static struct value
*
331 tlb_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
, void *ignore
)
333 if (target_has_stack
&& inferior_ptid
!= null_ptid
)
335 struct type
*type
= windows_get_tlb_type (gdbarch
);
336 return allocate_computed_value (type
, &tlb_value_funcs
, NULL
);
339 return allocate_value (builtin_type (gdbarch
)->builtin_void
);
343 /* Display thread information block of a given thread. */
346 display_one_tib (ptid_t ptid
)
348 gdb_byte
*tib
= NULL
;
350 CORE_ADDR thread_local_base
;
351 ULONGEST i
, val
, max
, max_name
, size
, tib_size
;
352 ULONGEST sizeof_ptr
= gdbarch_ptr_bit (target_gdbarch ());
353 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
355 if (sizeof_ptr
== 64)
357 size
= sizeof (uint64_t);
358 tib_size
= sizeof (thread_information_64
);
363 size
= sizeof (uint32_t);
364 tib_size
= sizeof (thread_information_32
);
370 if (maint_display_all_tib
)
372 tib_size
= FULL_TIB_SIZE
;
373 max
= tib_size
/ size
;
376 tib
= (gdb_byte
*) alloca (tib_size
);
378 if (target_get_tib_address (ptid
, &thread_local_base
) == 0)
380 printf_filtered (_("Unable to get thread local base for %s\n"),
381 target_pid_to_str (ptid
).c_str ());
385 if (target_read (current_top_target (), TARGET_OBJECT_MEMORY
,
386 NULL
, tib
, thread_local_base
, tib_size
) != tib_size
)
388 printf_filtered (_("Unable to read thread information "
389 "block for %s at address %s\n"),
390 target_pid_to_str (ptid
).c_str (),
391 paddress (target_gdbarch (), thread_local_base
));
395 printf_filtered (_("Thread Information Block %s at %s\n"),
396 target_pid_to_str (ptid
).c_str (),
397 paddress (target_gdbarch (), thread_local_base
));
399 index
= (gdb_byte
*) tib
;
401 /* All fields have the size of a pointer, this allows to iterate
402 using the same for loop for both layouts. */
403 for (i
= 0; i
< max
; i
++)
405 val
= extract_unsigned_integer (index
, size
, byte_order
);
407 printf_filtered (_("%s is 0x%s\n"), TIB_NAME
[i
], phex (val
, size
));
409 printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i
* size
, 2),
416 /* Display thread information block of the current thread. */
419 display_tib (const char * args
, int from_tty
)
421 if (inferior_ptid
!= null_ptid
)
422 display_one_tib (inferior_ptid
);
426 windows_xfer_shared_library (const char* so_name
, CORE_ADDR load_addr
,
427 struct gdbarch
*gdbarch
, struct obstack
*obstack
)
429 CORE_ADDR text_offset
;
431 obstack_grow_str (obstack
, "<library name=\"");
432 std::string p
= xml_escape_text (so_name
);
433 obstack_grow_str (obstack
, p
.c_str ());
434 obstack_grow_str (obstack
, "\"><segment address=\"");
435 gdb_bfd_ref_ptr
dll (gdb_bfd_open (so_name
, gnutarget
, -1));
436 /* The following calls are OK even if dll is NULL.
437 The default value 0x1000 is returned by pe_text_section_offset
439 text_offset
= pe_text_section_offset (dll
.get ());
440 obstack_grow_str (obstack
, paddress (gdbarch
, load_addr
+ text_offset
));
441 obstack_grow_str (obstack
, "\"/></library>");
444 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
445 method. It searches all objfiles, starting with CURRENT_OBJFILE
448 On Windows, the system behaves a little differently when two
449 objfiles each define a global symbol using the same name, compared
450 to other platforms such as GNU/Linux for instance. On GNU/Linux,
451 all instances of the symbol effectively get merged into a single
452 one, but on Windows, they remain distinct.
454 As a result, it usually makes sense to start global symbol searches
455 with the current objfile before expanding it to all other objfiles.
456 This helps for instance when a user debugs some code in a DLL that
457 refers to a global variable defined inside that DLL. When trying
458 to print the value of that global variable, it would be unhelpful
459 to print the value of another global variable defined with the same
460 name, but in a different DLL. */
463 windows_iterate_over_objfiles_in_search_order
464 (struct gdbarch
*gdbarch
,
465 iterate_over_objfiles_in_search_order_cb_ftype
*cb
,
466 void *cb_data
, struct objfile
*current_objfile
)
472 stop
= cb (current_objfile
, cb_data
);
477 for (objfile
*objfile
: current_program_space
->objfiles ())
479 if (objfile
!= current_objfile
)
481 stop
= cb (objfile
, cb_data
);
489 show_maint_show_all_tib (struct ui_file
*file
, int from_tty
,
490 struct cmd_list_element
*c
, const char *value
)
492 fprintf_filtered (file
, _("Show all non-zero elements of "
493 "Thread Information Block is %s.\n"), value
);
497 info_w32_command (const char *args
, int from_tty
)
499 help_list (info_w32_cmdlist
, "info w32 ", class_info
, gdb_stdout
);
502 static int w32_prefix_command_valid
= 0;
504 init_w32_command_list (void)
506 if (!w32_prefix_command_valid
)
508 add_prefix_cmd ("w32", class_info
, info_w32_command
,
509 _("Print information specific to Win32 debugging."),
510 &info_w32_cmdlist
, "info w32 ", 0, &infolist
);
511 w32_prefix_command_valid
= 1;
515 /* Implementation of `gdbarch_gdb_signal_to_target'. */
518 windows_gdb_signal_to_target (struct gdbarch
*gdbarch
, enum gdb_signal signal
)
525 return WINDOWS_SIGHUP
;
527 return WINDOWS_SIGINT
;
528 case GDB_SIGNAL_QUIT
:
529 return WINDOWS_SIGQUIT
;
531 return WINDOWS_SIGILL
;
532 case GDB_SIGNAL_TRAP
:
533 return WINDOWS_SIGTRAP
;
534 case GDB_SIGNAL_ABRT
:
535 return WINDOWS_SIGABRT
;
537 return WINDOWS_SIGEMT
;
539 return WINDOWS_SIGFPE
;
540 case GDB_SIGNAL_KILL
:
541 return WINDOWS_SIGKILL
;
543 return WINDOWS_SIGBUS
;
544 case GDB_SIGNAL_SEGV
:
545 return WINDOWS_SIGSEGV
;
547 return WINDOWS_SIGSYS
;
548 case GDB_SIGNAL_PIPE
:
549 return WINDOWS_SIGPIPE
;
550 case GDB_SIGNAL_ALRM
:
551 return WINDOWS_SIGALRM
;
552 case GDB_SIGNAL_TERM
:
553 return WINDOWS_SIGTERM
;
556 return WINDOWS_SIGURG
;
557 case GDB_SIGNAL_STOP
:
558 return WINDOWS_SIGSTOP
;
559 case GDB_SIGNAL_TSTP
:
560 return WINDOWS_SIGTSTP
;
561 case GDB_SIGNAL_CONT
:
562 return WINDOWS_SIGCONT
;
563 case GDB_SIGNAL_CHLD
:
564 return WINDOWS_SIGCHLD
;
565 case GDB_SIGNAL_TTIN
:
566 return WINDOWS_SIGTTIN
;
567 case GDB_SIGNAL_TTOU
:
568 return WINDOWS_SIGTTOU
;
570 return WINDOWS_SIGIO
;
571 case GDB_SIGNAL_XCPU
:
572 return WINDOWS_SIGXCPU
;
573 case GDB_SIGNAL_XFSZ
:
574 return WINDOWS_SIGXFSZ
;
575 case GDB_SIGNAL_VTALRM
:
576 return WINDOWS_SIGVTALRM
;
577 case GDB_SIGNAL_PROF
:
578 return WINDOWS_SIGPROF
;
579 case GDB_SIGNAL_WINCH
:
580 return WINDOWS_SIGWINCH
;
582 return WINDOWS_SIGLOST
;
583 case GDB_SIGNAL_USR1
:
584 return WINDOWS_SIGUSR1
;
585 case GDB_SIGNAL_USR2
:
586 return WINDOWS_SIGUSR2
;
587 #endif /* __CYGWIN__ */
592 /* To be called from the various GDB_OSABI_CYGWIN handlers for the
593 various Windows architectures and machine types. */
596 windows_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
598 set_gdbarch_wchar_bit (gdbarch
, 16);
599 set_gdbarch_wchar_signed (gdbarch
, 0);
601 /* Canonical paths on this target look like
602 `c:\Program Files\Foo App\mydll.dll', for example. */
603 set_gdbarch_has_dos_based_file_system (gdbarch
, 1);
605 set_gdbarch_iterate_over_objfiles_in_search_order
606 (gdbarch
, windows_iterate_over_objfiles_in_search_order
);
608 set_gdbarch_gdb_signal_to_target (gdbarch
, windows_gdb_signal_to_target
);
610 set_solib_ops (gdbarch
, &solib_target_so_ops
);
613 /* Implementation of `tlb' variable. */
615 static const struct internalvar_funcs tlb_funcs
=
623 _initialize_windows_tdep (void)
625 init_w32_command_list ();
626 add_cmd ("thread-information-block", class_info
, display_tib
,
627 _("Display thread information block."),
629 add_alias_cmd ("tib", "thread-information-block", class_info
, 1,
632 add_setshow_boolean_cmd ("show-all-tib", class_maintenance
,
633 &maint_display_all_tib
, _("\
634 Set whether to display all non-zero fields of thread information block."), _("\
635 Show whether to display all non-zero fields of thread information block."), _("\
636 Use \"on\" to enable, \"off\" to disable.\n\
637 If enabled, all non-zero fields of thread information block are displayed,\n\
638 even if their meaning is unknown."),
640 show_maint_show_all_tib
,
641 &maintenance_set_cmdlist
,
642 &maintenance_show_cmdlist
);
644 /* Explicitly create without lookup, since that tries to create a
645 value with a void typed value, and when we get here, gdbarch
646 isn't initialized yet. At this point, we're quite sure there
647 isn't another convenience variable of the same name. */
648 create_internalvar_type_lazy ("_tlb", &tlb_funcs
, NULL
);