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"
37 #include "coff/internal.h"
41 #define CYGWIN_DLL_NAME "cygwin1.dll"
43 /* Windows signal numbers differ between MinGW flavors and between
44 those and Cygwin. The below enumeration was gleaned from the
45 respective headers; the ones marked with MinGW64/Cygwin are defined
46 only by MinGW64 and Cygwin, not by mingw.org's MinGW. FIXME: We
47 should really have distinct MinGW vs Cygwin OSABIs, and two
48 separate enums, selected at runtime. */
52 WINDOWS_SIGHUP
= 1, /* MinGW64/Cygwin */
54 WINDOWS_SIGQUIT
= 3, /* MinGW64/Cygwin */
56 WINDOWS_SIGTRAP
= 5, /* MinGW64/Cygwin */
60 WINDOWS_SIGIOT
= 6, /* MinGW64 */
62 WINDOWS_SIGEMT
= 7, /* MinGW64/Cygwin */
64 WINDOWS_SIGKILL
= 9, /* MinGW64/Cygwin */
65 WINDOWS_SIGBUS
= 10, /* MinGW64/Cygwin */
67 WINDOWS_SIGSYS
= 12, /* MinGW64/Cygwin */
68 WINDOWS_SIGPIPE
= 13,/* MinGW64/Cygwin */
69 WINDOWS_SIGALRM
= 14,/* MinGW64/Cygwin */
82 WINDOWS_SIGVTALRM
= 26,
84 WINDOWS_SIGWINCH
= 28,
89 WINDOWS_SIGBREAK
= 21,
94 struct cmd_list_element
*info_w32_cmdlist
;
96 typedef struct thread_information_block_32
98 uint32_t current_seh
; /* %fs:0x0000 */
99 uint32_t current_top_of_stack
; /* %fs:0x0004 */
100 uint32_t current_bottom_of_stack
; /* %fs:0x0008 */
101 uint32_t sub_system_tib
; /* %fs:0x000c */
102 uint32_t fiber_data
; /* %fs:0x0010 */
103 uint32_t arbitrary_data_slot
; /* %fs:0x0014 */
104 uint32_t linear_address_tib
; /* %fs:0x0018 */
105 uint32_t environment_pointer
; /* %fs:0x001c */
106 uint32_t process_id
; /* %fs:0x0020 */
107 uint32_t current_thread_id
; /* %fs:0x0024 */
108 uint32_t active_rpc_handle
; /* %fs:0x0028 */
109 uint32_t thread_local_storage
; /* %fs:0x002c */
110 uint32_t process_environment_block
; /* %fs:0x0030 */
111 uint32_t last_error_number
; /* %fs:0x0034 */
113 thread_information_32
;
115 typedef struct thread_information_block_64
117 uint64_t current_seh
; /* %gs:0x0000 */
118 uint64_t current_top_of_stack
; /* %gs:0x0008 */
119 uint64_t current_bottom_of_stack
; /* %gs:0x0010 */
120 uint64_t sub_system_tib
; /* %gs:0x0018 */
121 uint64_t fiber_data
; /* %gs:0x0020 */
122 uint64_t arbitrary_data_slot
; /* %gs:0x0028 */
123 uint64_t linear_address_tib
; /* %gs:0x0030 */
124 uint64_t environment_pointer
; /* %gs:0x0038 */
125 uint64_t process_id
; /* %gs:0x0040 */
126 uint64_t current_thread_id
; /* %gs:0x0048 */
127 uint64_t active_rpc_handle
; /* %gs:0x0050 */
128 uint64_t thread_local_storage
; /* %gs:0x0058 */
129 uint64_t process_environment_block
; /* %gs:0x0060 */
130 uint64_t last_error_number
; /* %gs:0x0068 */
132 thread_information_64
;
135 static const char* TIB_NAME
[] =
137 " current_seh ", /* %fs:0x0000 */
138 " current_top_of_stack ", /* %fs:0x0004 */
139 " current_bottom_of_stack ", /* %fs:0x0008 */
140 " sub_system_tib ", /* %fs:0x000c */
141 " fiber_data ", /* %fs:0x0010 */
142 " arbitrary_data_slot ", /* %fs:0x0014 */
143 " linear_address_tib ", /* %fs:0x0018 */
144 " environment_pointer ", /* %fs:0x001c */
145 " process_id ", /* %fs:0x0020 */
146 " current_thread_id ", /* %fs:0x0024 */
147 " active_rpc_handle ", /* %fs:0x0028 */
148 " thread_local_storage ", /* %fs:0x002c */
149 " process_environment_block ", /* %fs:0x0030 */
150 " last_error_number " /* %fs:0x0034 */
153 static const int MAX_TIB32
=
154 sizeof (thread_information_32
) / sizeof (uint32_t);
155 static const int MAX_TIB64
=
156 sizeof (thread_information_64
) / sizeof (uint64_t);
157 static const int FULL_TIB_SIZE
= 0x1000;
159 static bool maint_display_all_tib
= false;
161 static struct gdbarch_data
*windows_gdbarch_data_handle
;
163 struct windows_gdbarch_data
165 struct type
*siginfo_type
;
166 struct type
*tib_ptr_type
; /* Type of thread information block */
169 /* Allocate windows_gdbarch_data for an arch. */
172 init_windows_gdbarch_data (struct gdbarch
*gdbarch
)
174 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct windows_gdbarch_data
);
177 /* Get windows_gdbarch_data of an arch. */
179 static struct windows_gdbarch_data
*
180 get_windows_gdbarch_data (struct gdbarch
*gdbarch
)
182 return ((struct windows_gdbarch_data
*)
183 gdbarch_data (gdbarch
, windows_gdbarch_data_handle
));
186 /* Define Thread Local Base pointer type. */
189 windows_get_tlb_type (struct gdbarch
*gdbarch
)
191 struct type
*dword_ptr_type
, *dword32_type
, *void_ptr_type
;
192 struct type
*peb_ldr_type
, *peb_ldr_ptr_type
;
193 struct type
*peb_type
, *peb_ptr_type
, *list_type
;
194 struct type
*module_list_ptr_type
;
195 struct type
*tib_type
, *seh_type
, *tib_ptr_type
, *seh_ptr_type
;
196 struct type
*word_type
, *wchar_type
, *wchar_ptr_type
;
197 struct type
*uni_str_type
, *rupp_type
, *rupp_ptr_type
;
199 windows_gdbarch_data
*windows_gdbarch_data
200 = get_windows_gdbarch_data (gdbarch
);
201 if (windows_gdbarch_data
->tib_ptr_type
!= nullptr)
202 return windows_gdbarch_data
->tib_ptr_type
;
204 dword_ptr_type
= arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
206 dword32_type
= arch_integer_type (gdbarch
, 32,
208 word_type
= arch_integer_type (gdbarch
, 16,
210 wchar_type
= arch_integer_type (gdbarch
, 16,
212 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
213 wchar_ptr_type
= arch_pointer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
218 list_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
219 TYPE_NAME (list_type
) = xstrdup ("list");
221 module_list_ptr_type
= void_ptr_type
;
223 append_composite_type_field (list_type
, "forward_list",
224 module_list_ptr_type
);
225 append_composite_type_field (list_type
, "backward_list",
226 module_list_ptr_type
);
228 /* Structured Exception Handler */
230 seh_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
231 TYPE_NAME (seh_type
) = xstrdup ("seh");
233 seh_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
234 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
236 TYPE_TARGET_TYPE (seh_ptr_type
) = seh_type
;
238 append_composite_type_field (seh_type
, "next_seh", seh_ptr_type
);
239 append_composite_type_field (seh_type
, "handler",
240 builtin_type (gdbarch
)->builtin_func_ptr
);
242 /* struct _PEB_LDR_DATA */
243 peb_ldr_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
244 TYPE_NAME (peb_ldr_type
) = xstrdup ("peb_ldr_data");
246 append_composite_type_field (peb_ldr_type
, "length", dword32_type
);
247 append_composite_type_field (peb_ldr_type
, "initialized", dword32_type
);
248 append_composite_type_field (peb_ldr_type
, "ss_handle", void_ptr_type
);
249 append_composite_type_field (peb_ldr_type
, "in_load_order", list_type
);
250 append_composite_type_field (peb_ldr_type
, "in_memory_order", list_type
);
251 append_composite_type_field (peb_ldr_type
, "in_init_order", list_type
);
252 append_composite_type_field (peb_ldr_type
, "entry_in_progress",
254 peb_ldr_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
255 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
257 TYPE_TARGET_TYPE (peb_ldr_ptr_type
) = peb_ldr_type
;
259 /* struct UNICODE_STRING */
260 uni_str_type
= arch_composite_type (gdbarch
, "unicode_string",
263 append_composite_type_field (uni_str_type
, "length", word_type
);
264 append_composite_type_field (uni_str_type
, "maximum_length", word_type
);
265 append_composite_type_field_aligned (uni_str_type
, "buffer",
267 TYPE_LENGTH (wchar_ptr_type
));
269 /* struct _RTL_USER_PROCESS_PARAMETERS */
270 rupp_type
= arch_composite_type (gdbarch
, "rtl_user_process_parameters",
273 append_composite_type_field (rupp_type
, "maximum_length", dword32_type
);
274 append_composite_type_field (rupp_type
, "length", dword32_type
);
275 append_composite_type_field (rupp_type
, "flags", dword32_type
);
276 append_composite_type_field (rupp_type
, "debug_flags", dword32_type
);
277 append_composite_type_field (rupp_type
, "console_handle", void_ptr_type
);
278 append_composite_type_field (rupp_type
, "console_flags", dword32_type
);
279 append_composite_type_field_aligned (rupp_type
, "standard_input",
281 TYPE_LENGTH (void_ptr_type
));
282 append_composite_type_field (rupp_type
, "standard_output", void_ptr_type
);
283 append_composite_type_field (rupp_type
, "standard_error", void_ptr_type
);
284 append_composite_type_field (rupp_type
, "current_directory", uni_str_type
);
285 append_composite_type_field (rupp_type
, "current_directory_handle",
287 append_composite_type_field (rupp_type
, "dll_path", uni_str_type
);
288 append_composite_type_field (rupp_type
, "image_path_name", uni_str_type
);
289 append_composite_type_field (rupp_type
, "command_line", uni_str_type
);
290 append_composite_type_field (rupp_type
, "environment", void_ptr_type
);
291 append_composite_type_field (rupp_type
, "starting_x", dword32_type
);
292 append_composite_type_field (rupp_type
, "starting_y", dword32_type
);
293 append_composite_type_field (rupp_type
, "count_x", dword32_type
);
294 append_composite_type_field (rupp_type
, "count_y", dword32_type
);
295 append_composite_type_field (rupp_type
, "count_chars_x", dword32_type
);
296 append_composite_type_field (rupp_type
, "count_chars_y", dword32_type
);
297 append_composite_type_field (rupp_type
, "fill_attribute", dword32_type
);
298 append_composite_type_field (rupp_type
, "window_flags", dword32_type
);
299 append_composite_type_field (rupp_type
, "show_window_flags", dword32_type
);
300 append_composite_type_field_aligned (rupp_type
, "window_title",
302 TYPE_LENGTH (void_ptr_type
));
303 append_composite_type_field (rupp_type
, "desktop_info", uni_str_type
);
304 append_composite_type_field (rupp_type
, "shell_info", uni_str_type
);
305 append_composite_type_field (rupp_type
, "runtime_data", uni_str_type
);
307 rupp_ptr_type
= arch_pointer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
311 /* struct process environment block */
312 peb_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
313 TYPE_NAME (peb_type
) = xstrdup ("peb");
315 /* First bytes contain several flags. */
316 append_composite_type_field (peb_type
, "flags", dword_ptr_type
);
317 append_composite_type_field (peb_type
, "mutant", void_ptr_type
);
318 append_composite_type_field (peb_type
, "image_base_address", void_ptr_type
);
319 append_composite_type_field (peb_type
, "ldr", peb_ldr_ptr_type
);
320 append_composite_type_field (peb_type
, "process_parameters", rupp_ptr_type
);
321 append_composite_type_field (peb_type
, "sub_system_data", void_ptr_type
);
322 append_composite_type_field (peb_type
, "process_heap", void_ptr_type
);
323 append_composite_type_field (peb_type
, "fast_peb_lock", void_ptr_type
);
324 peb_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
325 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
327 TYPE_TARGET_TYPE (peb_ptr_type
) = peb_type
;
330 /* struct thread information block */
331 tib_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
332 TYPE_NAME (tib_type
) = xstrdup ("tib");
334 /* uint32_t current_seh; %fs:0x0000 */
335 append_composite_type_field (tib_type
, "current_seh", seh_ptr_type
);
336 /* uint32_t current_top_of_stack; %fs:0x0004 */
337 append_composite_type_field (tib_type
, "current_top_of_stack",
339 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
340 append_composite_type_field (tib_type
, "current_bottom_of_stack",
342 /* uint32_t sub_system_tib; %fs:0x000c */
343 append_composite_type_field (tib_type
, "sub_system_tib", void_ptr_type
);
345 /* uint32_t fiber_data; %fs:0x0010 */
346 append_composite_type_field (tib_type
, "fiber_data", void_ptr_type
);
347 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
348 append_composite_type_field (tib_type
, "arbitrary_data_slot", void_ptr_type
);
349 /* uint32_t linear_address_tib; %fs:0x0018 */
350 append_composite_type_field (tib_type
, "linear_address_tib", void_ptr_type
);
351 /* uint32_t environment_pointer; %fs:0x001c */
352 append_composite_type_field (tib_type
, "environment_pointer", void_ptr_type
);
353 /* uint32_t process_id; %fs:0x0020 */
354 append_composite_type_field (tib_type
, "process_id", dword_ptr_type
);
355 /* uint32_t current_thread_id; %fs:0x0024 */
356 append_composite_type_field (tib_type
, "thread_id", dword_ptr_type
);
357 /* uint32_t active_rpc_handle; %fs:0x0028 */
358 append_composite_type_field (tib_type
, "active_rpc_handle", dword_ptr_type
);
359 /* uint32_t thread_local_storage; %fs:0x002c */
360 append_composite_type_field (tib_type
, "thread_local_storage",
362 /* uint32_t process_environment_block; %fs:0x0030 */
363 append_composite_type_field (tib_type
, "process_environment_block",
365 /* uint32_t last_error_number; %fs:0x0034 */
366 append_composite_type_field (tib_type
, "last_error_number", dword_ptr_type
);
368 tib_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
369 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
371 TYPE_TARGET_TYPE (tib_ptr_type
) = tib_type
;
373 windows_gdbarch_data
->tib_ptr_type
= tib_ptr_type
;
378 /* The $_tlb convenience variable is a bit special. We don't know
379 for sure the type of the value until we actually have a chance to
380 fetch the data. The type can change depending on gdbarch, so it is
381 also dependent on which thread you have selected. */
383 /* This function implements the lval_computed support for reading a
387 tlb_value_read (struct value
*val
)
390 struct type
*type
= check_typedef (value_type (val
));
392 if (!target_get_tib_address (inferior_ptid
, &tlb
))
393 error (_("Unable to read tlb"));
394 store_typed_address (value_contents_raw (val
), type
, tlb
);
397 /* This function implements the lval_computed support for writing a
401 tlb_value_write (struct value
*v
, struct value
*fromval
)
403 error (_("Impossible to change the Thread Local Base"));
406 static const struct lval_funcs tlb_value_funcs
=
413 /* Return a new value with the correct type for the tlb object of
414 the current thread using architecture GDBARCH. Return a void value
415 if there's no object available. */
417 static struct value
*
418 tlb_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
, void *ignore
)
420 if (target_has_stack
&& inferior_ptid
!= null_ptid
)
422 struct type
*type
= windows_get_tlb_type (gdbarch
);
423 return allocate_computed_value (type
, &tlb_value_funcs
, NULL
);
426 return allocate_value (builtin_type (gdbarch
)->builtin_void
);
430 /* Display thread information block of a given thread. */
433 display_one_tib (ptid_t ptid
)
435 gdb_byte
*tib
= NULL
;
437 CORE_ADDR thread_local_base
;
438 ULONGEST i
, val
, max
, max_name
, size
, tib_size
;
439 ULONGEST sizeof_ptr
= gdbarch_ptr_bit (target_gdbarch ());
440 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
442 if (sizeof_ptr
== 64)
444 size
= sizeof (uint64_t);
445 tib_size
= sizeof (thread_information_64
);
450 size
= sizeof (uint32_t);
451 tib_size
= sizeof (thread_information_32
);
457 if (maint_display_all_tib
)
459 tib_size
= FULL_TIB_SIZE
;
460 max
= tib_size
/ size
;
463 tib
= (gdb_byte
*) alloca (tib_size
);
465 if (target_get_tib_address (ptid
, &thread_local_base
) == 0)
467 printf_filtered (_("Unable to get thread local base for %s\n"),
468 target_pid_to_str (ptid
).c_str ());
472 if (target_read (current_top_target (), TARGET_OBJECT_MEMORY
,
473 NULL
, tib
, thread_local_base
, tib_size
) != tib_size
)
475 printf_filtered (_("Unable to read thread information "
476 "block for %s at address %s\n"),
477 target_pid_to_str (ptid
).c_str (),
478 paddress (target_gdbarch (), thread_local_base
));
482 printf_filtered (_("Thread Information Block %s at %s\n"),
483 target_pid_to_str (ptid
).c_str (),
484 paddress (target_gdbarch (), thread_local_base
));
486 index
= (gdb_byte
*) tib
;
488 /* All fields have the size of a pointer, this allows to iterate
489 using the same for loop for both layouts. */
490 for (i
= 0; i
< max
; i
++)
492 val
= extract_unsigned_integer (index
, size
, byte_order
);
494 printf_filtered (_("%s is 0x%s\n"), TIB_NAME
[i
], phex (val
, size
));
496 printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i
* size
, 2),
503 /* Display thread information block of the current thread. */
506 display_tib (const char * args
, int from_tty
)
508 if (inferior_ptid
!= null_ptid
)
509 display_one_tib (inferior_ptid
);
513 windows_xfer_shared_library (const char* so_name
, CORE_ADDR load_addr
,
514 CORE_ADDR
*text_offset_cached
,
515 struct gdbarch
*gdbarch
, struct obstack
*obstack
)
517 CORE_ADDR text_offset
= text_offset_cached
? *text_offset_cached
: 0;
519 obstack_grow_str (obstack
, "<library name=\"");
520 std::string p
= xml_escape_text (so_name
);
521 obstack_grow_str (obstack
, p
.c_str ());
522 obstack_grow_str (obstack
, "\"><segment address=\"");
526 gdb_bfd_ref_ptr
dll (gdb_bfd_open (so_name
, gnutarget
, -1));
527 /* The following calls are OK even if dll is NULL.
528 The default value 0x1000 is returned by pe_text_section_offset
530 text_offset
= pe_text_section_offset (dll
.get ());
531 if (text_offset_cached
)
532 *text_offset_cached
= text_offset
;
535 obstack_grow_str (obstack
, paddress (gdbarch
, load_addr
+ text_offset
));
536 obstack_grow_str (obstack
, "\"/></library>");
539 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
540 method. It searches all objfiles, starting with CURRENT_OBJFILE
543 On Windows, the system behaves a little differently when two
544 objfiles each define a global symbol using the same name, compared
545 to other platforms such as GNU/Linux for instance. On GNU/Linux,
546 all instances of the symbol effectively get merged into a single
547 one, but on Windows, they remain distinct.
549 As a result, it usually makes sense to start global symbol searches
550 with the current objfile before expanding it to all other objfiles.
551 This helps for instance when a user debugs some code in a DLL that
552 refers to a global variable defined inside that DLL. When trying
553 to print the value of that global variable, it would be unhelpful
554 to print the value of another global variable defined with the same
555 name, but in a different DLL. */
558 windows_iterate_over_objfiles_in_search_order
559 (struct gdbarch
*gdbarch
,
560 iterate_over_objfiles_in_search_order_cb_ftype
*cb
,
561 void *cb_data
, struct objfile
*current_objfile
)
567 stop
= cb (current_objfile
, cb_data
);
572 for (objfile
*objfile
: current_program_space
->objfiles ())
574 if (objfile
!= current_objfile
)
576 stop
= cb (objfile
, cb_data
);
584 show_maint_show_all_tib (struct ui_file
*file
, int from_tty
,
585 struct cmd_list_element
*c
, const char *value
)
587 fprintf_filtered (file
, _("Show all non-zero elements of "
588 "Thread Information Block is %s.\n"), value
);
592 info_w32_command (const char *args
, int from_tty
)
594 help_list (info_w32_cmdlist
, "info w32 ", class_info
, gdb_stdout
);
597 static int w32_prefix_command_valid
= 0;
599 init_w32_command_list (void)
601 if (!w32_prefix_command_valid
)
603 add_prefix_cmd ("w32", class_info
, info_w32_command
,
604 _("Print information specific to Win32 debugging."),
605 &info_w32_cmdlist
, "info w32 ", 0, &infolist
);
606 w32_prefix_command_valid
= 1;
610 /* Implementation of `gdbarch_gdb_signal_to_target'. */
613 windows_gdb_signal_to_target (struct gdbarch
*gdbarch
, enum gdb_signal signal
)
620 return WINDOWS_SIGHUP
;
622 return WINDOWS_SIGINT
;
623 case GDB_SIGNAL_QUIT
:
624 return WINDOWS_SIGQUIT
;
626 return WINDOWS_SIGILL
;
627 case GDB_SIGNAL_TRAP
:
628 return WINDOWS_SIGTRAP
;
629 case GDB_SIGNAL_ABRT
:
630 return WINDOWS_SIGABRT
;
632 return WINDOWS_SIGEMT
;
634 return WINDOWS_SIGFPE
;
635 case GDB_SIGNAL_KILL
:
636 return WINDOWS_SIGKILL
;
638 return WINDOWS_SIGBUS
;
639 case GDB_SIGNAL_SEGV
:
640 return WINDOWS_SIGSEGV
;
642 return WINDOWS_SIGSYS
;
643 case GDB_SIGNAL_PIPE
:
644 return WINDOWS_SIGPIPE
;
645 case GDB_SIGNAL_ALRM
:
646 return WINDOWS_SIGALRM
;
647 case GDB_SIGNAL_TERM
:
648 return WINDOWS_SIGTERM
;
651 return WINDOWS_SIGURG
;
652 case GDB_SIGNAL_STOP
:
653 return WINDOWS_SIGSTOP
;
654 case GDB_SIGNAL_TSTP
:
655 return WINDOWS_SIGTSTP
;
656 case GDB_SIGNAL_CONT
:
657 return WINDOWS_SIGCONT
;
658 case GDB_SIGNAL_CHLD
:
659 return WINDOWS_SIGCHLD
;
660 case GDB_SIGNAL_TTIN
:
661 return WINDOWS_SIGTTIN
;
662 case GDB_SIGNAL_TTOU
:
663 return WINDOWS_SIGTTOU
;
665 return WINDOWS_SIGIO
;
666 case GDB_SIGNAL_XCPU
:
667 return WINDOWS_SIGXCPU
;
668 case GDB_SIGNAL_XFSZ
:
669 return WINDOWS_SIGXFSZ
;
670 case GDB_SIGNAL_VTALRM
:
671 return WINDOWS_SIGVTALRM
;
672 case GDB_SIGNAL_PROF
:
673 return WINDOWS_SIGPROF
;
674 case GDB_SIGNAL_WINCH
:
675 return WINDOWS_SIGWINCH
;
677 return WINDOWS_SIGLOST
;
678 case GDB_SIGNAL_USR1
:
679 return WINDOWS_SIGUSR1
;
680 case GDB_SIGNAL_USR2
:
681 return WINDOWS_SIGUSR2
;
682 #endif /* __CYGWIN__ */
687 struct enum_value_name
693 /* Allocate a TYPE_CODE_ENUM type structure with its named values. */
696 create_enum (struct gdbarch
*gdbarch
, int bit
, const char *name
,
697 const struct enum_value_name
*values
, int count
)
702 type
= arch_type (gdbarch
, TYPE_CODE_ENUM
, bit
, name
);
703 TYPE_NFIELDS (type
) = count
;
704 TYPE_FIELDS (type
) = (struct field
*)
705 TYPE_ZALLOC (type
, sizeof (struct field
) * count
);
706 TYPE_UNSIGNED (type
) = 1;
708 for (i
= 0; i
< count
; i
++)
710 TYPE_FIELD_NAME (type
, i
) = values
[i
].name
;
711 SET_FIELD_ENUMVAL (TYPE_FIELD (type
, i
), values
[i
].value
);
717 static const struct enum_value_name exception_values
[] =
719 { 0x40000015, "FATAL_APP_EXIT" },
720 { 0x40010005, "DBG_CONTROL_C" },
721 { 0x40010008, "DBG_CONTROL_BREAK" },
722 { 0x80000002, "DATATYPE_MISALIGNMENT" },
723 { 0x80000003, "BREAKPOINT" },
724 { 0x80000004, "SINGLE_STEP" },
725 { 0xC0000005, "ACCESS_VIOLATION" },
726 { 0xC0000006, "IN_PAGE_ERROR" },
727 { 0xC000001D, "ILLEGAL_INSTRUCTION" },
728 { 0xC0000025, "NONCONTINUABLE_EXCEPTION" },
729 { 0xC0000026, "INVALID_DISPOSITION" },
730 { 0xC000008C, "ARRAY_BOUNDS_EXCEEDED" },
731 { 0xC000008D, "FLOAT_DENORMAL_OPERAND" },
732 { 0xC000008E, "FLOAT_DIVIDE_BY_ZERO" },
733 { 0xC000008F, "FLOAT_INEXACT_RESULT" },
734 { 0xC0000090, "FLOAT_INVALID_OPERATION" },
735 { 0xC0000091, "FLOAT_OVERFLOW" },
736 { 0xC0000092, "FLOAT_STACK_CHECK" },
737 { 0xC0000093, "FLOAT_UNDERFLOW" },
738 { 0xC0000094, "INTEGER_DIVIDE_BY_ZERO" },
739 { 0xC0000095, "INTEGER_OVERFLOW" },
740 { 0xC0000096, "PRIV_INSTRUCTION" },
741 { 0xC00000FD, "STACK_OVERFLOW" },
742 { 0xC0000409, "FAST_FAIL" },
745 static const struct enum_value_name violation_values
[] =
747 { 0, "READ_ACCESS_VIOLATION" },
748 { 1, "WRITE_ACCESS_VIOLATION" },
749 { 8, "DATA_EXECUTION_PREVENTION_VIOLATION" },
752 /* Implement the "get_siginfo_type" gdbarch method. */
755 windows_get_siginfo_type (struct gdbarch
*gdbarch
)
757 struct windows_gdbarch_data
*windows_gdbarch_data
;
758 struct type
*dword_type
, *pvoid_type
, *ulongptr_type
;
759 struct type
*code_enum
, *violation_enum
;
760 struct type
*violation_type
, *para_type
, *siginfo_ptr_type
, *siginfo_type
;
762 windows_gdbarch_data
= get_windows_gdbarch_data (gdbarch
);
763 if (windows_gdbarch_data
->siginfo_type
!= NULL
)
764 return windows_gdbarch_data
->siginfo_type
;
766 dword_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
768 pvoid_type
= arch_pointer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), "PVOID",
769 builtin_type (gdbarch
)->builtin_void
);
770 ulongptr_type
= arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
773 /* ExceptionCode value names */
774 code_enum
= create_enum (gdbarch
, gdbarch_int_bit (gdbarch
),
775 "ExceptionCode", exception_values
,
776 ARRAY_SIZE (exception_values
));
778 /* ACCESS_VIOLATION type names */
779 violation_enum
= create_enum (gdbarch
, gdbarch_ptr_bit (gdbarch
),
780 "ViolationType", violation_values
,
781 ARRAY_SIZE (violation_values
));
783 /* ACCESS_VIOLATION information */
784 violation_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
785 append_composite_type_field (violation_type
, "Type", violation_enum
);
786 append_composite_type_field (violation_type
, "Address", pvoid_type
);
788 /* Unnamed union of the documented field ExceptionInformation,
789 and the alternative AccessViolationInformation (which displays
790 human-readable values for ExceptionCode ACCESS_VIOLATION). */
791 para_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
792 append_composite_type_field (para_type
, "ExceptionInformation",
793 lookup_array_range_type (ulongptr_type
, 0, 14));
794 append_composite_type_field (para_type
, "AccessViolationInformation",
797 siginfo_type
= arch_composite_type (gdbarch
, "EXCEPTION_RECORD",
799 siginfo_ptr_type
= arch_pointer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
802 /* ExceptionCode is documented as type DWORD, but here a helper
803 enum type is used instead to display a human-readable value. */
804 append_composite_type_field (siginfo_type
, "ExceptionCode", code_enum
);
805 append_composite_type_field (siginfo_type
, "ExceptionFlags", dword_type
);
806 append_composite_type_field (siginfo_type
, "ExceptionRecord",
808 append_composite_type_field (siginfo_type
, "ExceptionAddress",
810 append_composite_type_field (siginfo_type
, "NumberParameters", dword_type
);
811 /* The 64-bit variant needs some padding. */
812 append_composite_type_field_aligned (siginfo_type
, "",
813 para_type
, TYPE_LENGTH (ulongptr_type
));
815 windows_gdbarch_data
->siginfo_type
= siginfo_type
;
820 /* Implement the "solib_create_inferior_hook" target_so_ops method. */
823 windows_solib_create_inferior_hook (int from_tty
)
825 CORE_ADDR exec_base
= 0;
827 /* Find base address of main executable in
828 TIB->process_environment_block->image_base_address. */
829 struct gdbarch
*gdbarch
= target_gdbarch ();
830 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
832 int peb_offset
; /* Offset of process_environment_block in TIB. */
833 int base_offset
; /* Offset of image_base_address in PEB. */
834 if (gdbarch_ptr_bit (gdbarch
) == 32)
848 if (target_has_execution
849 && target_get_tib_address (inferior_ptid
, &tlb
)
850 && !target_read_memory (tlb
+ peb_offset
, buf
, ptr_bytes
))
852 CORE_ADDR peb
= extract_unsigned_integer (buf
, ptr_bytes
, byte_order
);
853 if (!target_read_memory (peb
+ base_offset
, buf
, ptr_bytes
))
854 exec_base
= extract_unsigned_integer (buf
, ptr_bytes
, byte_order
);
857 /* Rebase executable if the base address changed because of ASLR. */
858 if (symfile_objfile
!= nullptr && exec_base
!= 0)
860 CORE_ADDR vmaddr
= pe_data (exec_bfd
)->pe_opthdr
.ImageBase
;
861 if (vmaddr
!= exec_base
)
862 objfile_rebase (symfile_objfile
, exec_base
- vmaddr
);
866 static struct target_so_ops windows_so_ops
;
868 /* To be called from the various GDB_OSABI_CYGWIN handlers for the
869 various Windows architectures and machine types. */
872 windows_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
874 set_gdbarch_wchar_bit (gdbarch
, 16);
875 set_gdbarch_wchar_signed (gdbarch
, 0);
877 /* Canonical paths on this target look like
878 `c:\Program Files\Foo App\mydll.dll', for example. */
879 set_gdbarch_has_dos_based_file_system (gdbarch
, 1);
881 set_gdbarch_iterate_over_objfiles_in_search_order
882 (gdbarch
, windows_iterate_over_objfiles_in_search_order
);
884 set_gdbarch_gdb_signal_to_target (gdbarch
, windows_gdb_signal_to_target
);
886 windows_so_ops
= solib_target_so_ops
;
887 windows_so_ops
.solib_create_inferior_hook
888 = windows_solib_create_inferior_hook
;
889 set_solib_ops (gdbarch
, &windows_so_ops
);
891 set_gdbarch_get_siginfo_type (gdbarch
, windows_get_siginfo_type
);
894 /* Implementation of `tlb' variable. */
896 static const struct internalvar_funcs tlb_funcs
=
903 /* Layout of an element of a PE's Import Directory Table. Based on:
905 https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-directory-table
908 struct pe_import_directory_entry
910 uint32_t import_lookup_table_rva
;
912 uint32_t forwarder_chain
;
914 uint32_t import_address_table_rva
;
917 gdb_static_assert (sizeof (pe_import_directory_entry
) == 20);
919 /* See windows-tdep.h. */
922 is_linked_with_cygwin_dll (bfd
*abfd
)
924 /* The list of DLLs a PE is linked to is in the .idata section. See:
926 https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#the-idata-section
928 asection
*idata_section
= bfd_get_section_by_name (abfd
, ".idata");
929 if (idata_section
== nullptr)
932 /* Find the virtual address of the .idata section. We must subtract this
933 from the RVAs (relative virtual addresses) to obtain an offset in the
936 = pe_data (abfd
)->pe_opthdr
.DataDirectory
[PE_IMPORT_TABLE
].VirtualAddress
;
938 /* Map the section's data. */
939 bfd_size_type idata_size
;
940 const gdb_byte
*const idata_contents
941 = gdb_bfd_map_section (idata_section
, &idata_size
);
942 if (idata_contents
== nullptr)
944 warning (_("Failed to get content of .idata section."));
948 const gdb_byte
*iter
= idata_contents
;
949 const gdb_byte
*end
= idata_contents
+ idata_size
;
950 const pe_import_directory_entry null_dir_entry
= { 0 };
952 /* Iterate through all directory entries. */
955 /* Is there enough space left in the section for another entry? */
956 if (iter
+ sizeof (pe_import_directory_entry
) > end
)
958 warning (_("Failed to parse .idata section: unexpected end of "
963 pe_import_directory_entry
*dir_entry
= (pe_import_directory_entry
*) iter
;
965 /* Is it the end of list marker? */
966 if (memcmp (dir_entry
, &null_dir_entry
,
967 sizeof (pe_import_directory_entry
)) == 0)
970 bfd_vma name_addr
= dir_entry
->name_rva
;
972 /* If the name's virtual address is smaller than the section's virtual
973 address, there's a problem. */
974 if (name_addr
< idata_addr
975 || name_addr
>= (idata_addr
+ idata_size
))
978 Failed to parse .idata section: name's virtual address (0x%" BFD_VMA_FMT
"x) \
979 is outside .idata section's range [0x%" BFD_VMA_FMT
"x, 0x%" BFD_VMA_FMT
"x[."),
980 name_addr
, idata_addr
, idata_addr
+ idata_size
);
984 const gdb_byte
*name
= &idata_contents
[name_addr
- idata_addr
];
986 /* Make sure we don't overshoot the end of the section with the streq. */
987 if (name
+ sizeof (CYGWIN_DLL_NAME
) > end
)
990 /* Finally, check if this is the dll name we are looking for. */
991 if (streq ((const char *) name
, CYGWIN_DLL_NAME
))
994 iter
+= sizeof (pe_import_directory_entry
);
1000 void _initialize_windows_tdep ();
1002 _initialize_windows_tdep ()
1004 windows_gdbarch_data_handle
1005 = gdbarch_data_register_post_init (init_windows_gdbarch_data
);
1007 init_w32_command_list ();
1008 add_cmd ("thread-information-block", class_info
, display_tib
,
1009 _("Display thread information block."),
1011 add_alias_cmd ("tib", "thread-information-block", class_info
, 1,
1014 add_setshow_boolean_cmd ("show-all-tib", class_maintenance
,
1015 &maint_display_all_tib
, _("\
1016 Set whether to display all non-zero fields of thread information block."), _("\
1017 Show whether to display all non-zero fields of thread information block."), _("\
1018 Use \"on\" to enable, \"off\" to disable.\n\
1019 If enabled, all non-zero fields of thread information block are displayed,\n\
1020 even if their meaning is unknown."),
1022 show_maint_show_all_tib
,
1023 &maintenance_set_cmdlist
,
1024 &maintenance_show_cmdlist
);
1026 /* Explicitly create without lookup, since that tries to create a
1027 value with a void typed value, and when we get here, gdbarch
1028 isn't initialized yet. At this point, we're quite sure there
1029 isn't another convenience variable of the same name. */
1030 create_internalvar_type_lazy ("_tlb", &tlb_funcs
, NULL
);