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 enumerations were gleaned from the
45 respective headers. */
47 /* Signal numbers for the various MinGW flavors. The ones marked with
48 MinGW-w64 are defined by MinGW-w64, not by mingw.org's MinGW. */
52 WINDOWS_SIGHUP
= 1, /* MinGW-w64 */
54 WINDOWS_SIGQUIT
= 3, /* MinGW-w64 */
56 WINDOWS_SIGTRAP
= 5, /* MinGW-w64 */
57 WINDOWS_SIGIOT
= 6, /* MinGW-w64 */
58 WINDOWS_SIGEMT
= 7, /* MinGW-w64 */
60 WINDOWS_SIGKILL
= 9, /* MinGW-w64 */
61 WINDOWS_SIGBUS
= 10, /* MinGW-w64 */
63 WINDOWS_SIGSYS
= 12, /* MinGW-w64 */
64 WINDOWS_SIGPIPE
= 13, /* MinGW-w64 */
65 WINDOWS_SIGALRM
= 14, /* MinGW-w64 */
67 WINDOWS_SIGBREAK
= 21,
71 /* Signal numbers for Cygwin. */
100 CYGWIN_SIGVTALRM
= 26,
102 CYGWIN_SIGWINCH
= 28,
108 struct cmd_list_element
*info_w32_cmdlist
;
110 typedef struct thread_information_block_32
112 uint32_t current_seh
; /* %fs:0x0000 */
113 uint32_t current_top_of_stack
; /* %fs:0x0004 */
114 uint32_t current_bottom_of_stack
; /* %fs:0x0008 */
115 uint32_t sub_system_tib
; /* %fs:0x000c */
116 uint32_t fiber_data
; /* %fs:0x0010 */
117 uint32_t arbitrary_data_slot
; /* %fs:0x0014 */
118 uint32_t linear_address_tib
; /* %fs:0x0018 */
119 uint32_t environment_pointer
; /* %fs:0x001c */
120 uint32_t process_id
; /* %fs:0x0020 */
121 uint32_t current_thread_id
; /* %fs:0x0024 */
122 uint32_t active_rpc_handle
; /* %fs:0x0028 */
123 uint32_t thread_local_storage
; /* %fs:0x002c */
124 uint32_t process_environment_block
; /* %fs:0x0030 */
125 uint32_t last_error_number
; /* %fs:0x0034 */
127 thread_information_32
;
129 typedef struct thread_information_block_64
131 uint64_t current_seh
; /* %gs:0x0000 */
132 uint64_t current_top_of_stack
; /* %gs:0x0008 */
133 uint64_t current_bottom_of_stack
; /* %gs:0x0010 */
134 uint64_t sub_system_tib
; /* %gs:0x0018 */
135 uint64_t fiber_data
; /* %gs:0x0020 */
136 uint64_t arbitrary_data_slot
; /* %gs:0x0028 */
137 uint64_t linear_address_tib
; /* %gs:0x0030 */
138 uint64_t environment_pointer
; /* %gs:0x0038 */
139 uint64_t process_id
; /* %gs:0x0040 */
140 uint64_t current_thread_id
; /* %gs:0x0048 */
141 uint64_t active_rpc_handle
; /* %gs:0x0050 */
142 uint64_t thread_local_storage
; /* %gs:0x0058 */
143 uint64_t process_environment_block
; /* %gs:0x0060 */
144 uint64_t last_error_number
; /* %gs:0x0068 */
146 thread_information_64
;
149 static const char* TIB_NAME
[] =
151 " current_seh ", /* %fs:0x0000 */
152 " current_top_of_stack ", /* %fs:0x0004 */
153 " current_bottom_of_stack ", /* %fs:0x0008 */
154 " sub_system_tib ", /* %fs:0x000c */
155 " fiber_data ", /* %fs:0x0010 */
156 " arbitrary_data_slot ", /* %fs:0x0014 */
157 " linear_address_tib ", /* %fs:0x0018 */
158 " environment_pointer ", /* %fs:0x001c */
159 " process_id ", /* %fs:0x0020 */
160 " current_thread_id ", /* %fs:0x0024 */
161 " active_rpc_handle ", /* %fs:0x0028 */
162 " thread_local_storage ", /* %fs:0x002c */
163 " process_environment_block ", /* %fs:0x0030 */
164 " last_error_number " /* %fs:0x0034 */
167 static const int MAX_TIB32
=
168 sizeof (thread_information_32
) / sizeof (uint32_t);
169 static const int MAX_TIB64
=
170 sizeof (thread_information_64
) / sizeof (uint64_t);
171 static const int FULL_TIB_SIZE
= 0x1000;
173 static bool maint_display_all_tib
= false;
175 static struct gdbarch_data
*windows_gdbarch_data_handle
;
177 struct windows_gdbarch_data
179 struct type
*siginfo_type
;
180 struct type
*tib_ptr_type
; /* Type of thread information block */
183 /* Allocate windows_gdbarch_data for an arch. */
186 init_windows_gdbarch_data (struct gdbarch
*gdbarch
)
188 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct windows_gdbarch_data
);
191 /* Get windows_gdbarch_data of an arch. */
193 static struct windows_gdbarch_data
*
194 get_windows_gdbarch_data (struct gdbarch
*gdbarch
)
196 return ((struct windows_gdbarch_data
*)
197 gdbarch_data (gdbarch
, windows_gdbarch_data_handle
));
200 /* Define Thread Local Base pointer type. */
203 windows_get_tlb_type (struct gdbarch
*gdbarch
)
205 struct type
*dword_ptr_type
, *dword32_type
, *void_ptr_type
;
206 struct type
*peb_ldr_type
, *peb_ldr_ptr_type
;
207 struct type
*peb_type
, *peb_ptr_type
, *list_type
;
208 struct type
*module_list_ptr_type
;
209 struct type
*tib_type
, *seh_type
, *tib_ptr_type
, *seh_ptr_type
;
210 struct type
*word_type
, *wchar_type
, *wchar_ptr_type
;
211 struct type
*uni_str_type
, *rupp_type
, *rupp_ptr_type
;
213 windows_gdbarch_data
*windows_gdbarch_data
214 = get_windows_gdbarch_data (gdbarch
);
215 if (windows_gdbarch_data
->tib_ptr_type
!= nullptr)
216 return windows_gdbarch_data
->tib_ptr_type
;
218 dword_ptr_type
= arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
220 dword32_type
= arch_integer_type (gdbarch
, 32,
222 word_type
= arch_integer_type (gdbarch
, 16,
224 wchar_type
= arch_integer_type (gdbarch
, 16,
226 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
227 wchar_ptr_type
= arch_pointer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
232 list_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
233 TYPE_NAME (list_type
) = xstrdup ("list");
235 module_list_ptr_type
= void_ptr_type
;
237 append_composite_type_field (list_type
, "forward_list",
238 module_list_ptr_type
);
239 append_composite_type_field (list_type
, "backward_list",
240 module_list_ptr_type
);
242 /* Structured Exception Handler */
244 seh_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
245 TYPE_NAME (seh_type
) = xstrdup ("seh");
247 seh_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
248 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
250 TYPE_TARGET_TYPE (seh_ptr_type
) = seh_type
;
252 append_composite_type_field (seh_type
, "next_seh", seh_ptr_type
);
253 append_composite_type_field (seh_type
, "handler",
254 builtin_type (gdbarch
)->builtin_func_ptr
);
256 /* struct _PEB_LDR_DATA */
257 peb_ldr_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
258 TYPE_NAME (peb_ldr_type
) = xstrdup ("peb_ldr_data");
260 append_composite_type_field (peb_ldr_type
, "length", dword32_type
);
261 append_composite_type_field (peb_ldr_type
, "initialized", dword32_type
);
262 append_composite_type_field (peb_ldr_type
, "ss_handle", void_ptr_type
);
263 append_composite_type_field (peb_ldr_type
, "in_load_order", list_type
);
264 append_composite_type_field (peb_ldr_type
, "in_memory_order", list_type
);
265 append_composite_type_field (peb_ldr_type
, "in_init_order", list_type
);
266 append_composite_type_field (peb_ldr_type
, "entry_in_progress",
268 peb_ldr_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
269 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
271 TYPE_TARGET_TYPE (peb_ldr_ptr_type
) = peb_ldr_type
;
273 /* struct UNICODE_STRING */
274 uni_str_type
= arch_composite_type (gdbarch
, "unicode_string",
277 append_composite_type_field (uni_str_type
, "length", word_type
);
278 append_composite_type_field (uni_str_type
, "maximum_length", word_type
);
279 append_composite_type_field_aligned (uni_str_type
, "buffer",
281 TYPE_LENGTH (wchar_ptr_type
));
283 /* struct _RTL_USER_PROCESS_PARAMETERS */
284 rupp_type
= arch_composite_type (gdbarch
, "rtl_user_process_parameters",
287 append_composite_type_field (rupp_type
, "maximum_length", dword32_type
);
288 append_composite_type_field (rupp_type
, "length", dword32_type
);
289 append_composite_type_field (rupp_type
, "flags", dword32_type
);
290 append_composite_type_field (rupp_type
, "debug_flags", dword32_type
);
291 append_composite_type_field (rupp_type
, "console_handle", void_ptr_type
);
292 append_composite_type_field (rupp_type
, "console_flags", dword32_type
);
293 append_composite_type_field_aligned (rupp_type
, "standard_input",
295 TYPE_LENGTH (void_ptr_type
));
296 append_composite_type_field (rupp_type
, "standard_output", void_ptr_type
);
297 append_composite_type_field (rupp_type
, "standard_error", void_ptr_type
);
298 append_composite_type_field (rupp_type
, "current_directory", uni_str_type
);
299 append_composite_type_field (rupp_type
, "current_directory_handle",
301 append_composite_type_field (rupp_type
, "dll_path", uni_str_type
);
302 append_composite_type_field (rupp_type
, "image_path_name", uni_str_type
);
303 append_composite_type_field (rupp_type
, "command_line", uni_str_type
);
304 append_composite_type_field (rupp_type
, "environment", void_ptr_type
);
305 append_composite_type_field (rupp_type
, "starting_x", dword32_type
);
306 append_composite_type_field (rupp_type
, "starting_y", dword32_type
);
307 append_composite_type_field (rupp_type
, "count_x", dword32_type
);
308 append_composite_type_field (rupp_type
, "count_y", dword32_type
);
309 append_composite_type_field (rupp_type
, "count_chars_x", dword32_type
);
310 append_composite_type_field (rupp_type
, "count_chars_y", dword32_type
);
311 append_composite_type_field (rupp_type
, "fill_attribute", dword32_type
);
312 append_composite_type_field (rupp_type
, "window_flags", dword32_type
);
313 append_composite_type_field (rupp_type
, "show_window_flags", dword32_type
);
314 append_composite_type_field_aligned (rupp_type
, "window_title",
316 TYPE_LENGTH (void_ptr_type
));
317 append_composite_type_field (rupp_type
, "desktop_info", uni_str_type
);
318 append_composite_type_field (rupp_type
, "shell_info", uni_str_type
);
319 append_composite_type_field (rupp_type
, "runtime_data", uni_str_type
);
321 rupp_ptr_type
= arch_pointer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
325 /* struct process environment block */
326 peb_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
327 TYPE_NAME (peb_type
) = xstrdup ("peb");
329 /* First bytes contain several flags. */
330 append_composite_type_field (peb_type
, "flags", dword_ptr_type
);
331 append_composite_type_field (peb_type
, "mutant", void_ptr_type
);
332 append_composite_type_field (peb_type
, "image_base_address", void_ptr_type
);
333 append_composite_type_field (peb_type
, "ldr", peb_ldr_ptr_type
);
334 append_composite_type_field (peb_type
, "process_parameters", rupp_ptr_type
);
335 append_composite_type_field (peb_type
, "sub_system_data", void_ptr_type
);
336 append_composite_type_field (peb_type
, "process_heap", void_ptr_type
);
337 append_composite_type_field (peb_type
, "fast_peb_lock", void_ptr_type
);
338 peb_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
339 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
341 TYPE_TARGET_TYPE (peb_ptr_type
) = peb_type
;
344 /* struct thread information block */
345 tib_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
346 TYPE_NAME (tib_type
) = xstrdup ("tib");
348 /* uint32_t current_seh; %fs:0x0000 */
349 append_composite_type_field (tib_type
, "current_seh", seh_ptr_type
);
350 /* uint32_t current_top_of_stack; %fs:0x0004 */
351 append_composite_type_field (tib_type
, "current_top_of_stack",
353 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
354 append_composite_type_field (tib_type
, "current_bottom_of_stack",
356 /* uint32_t sub_system_tib; %fs:0x000c */
357 append_composite_type_field (tib_type
, "sub_system_tib", void_ptr_type
);
359 /* uint32_t fiber_data; %fs:0x0010 */
360 append_composite_type_field (tib_type
, "fiber_data", void_ptr_type
);
361 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
362 append_composite_type_field (tib_type
, "arbitrary_data_slot", void_ptr_type
);
363 /* uint32_t linear_address_tib; %fs:0x0018 */
364 append_composite_type_field (tib_type
, "linear_address_tib", void_ptr_type
);
365 /* uint32_t environment_pointer; %fs:0x001c */
366 append_composite_type_field (tib_type
, "environment_pointer", void_ptr_type
);
367 /* uint32_t process_id; %fs:0x0020 */
368 append_composite_type_field (tib_type
, "process_id", dword_ptr_type
);
369 /* uint32_t current_thread_id; %fs:0x0024 */
370 append_composite_type_field (tib_type
, "thread_id", dword_ptr_type
);
371 /* uint32_t active_rpc_handle; %fs:0x0028 */
372 append_composite_type_field (tib_type
, "active_rpc_handle", dword_ptr_type
);
373 /* uint32_t thread_local_storage; %fs:0x002c */
374 append_composite_type_field (tib_type
, "thread_local_storage",
376 /* uint32_t process_environment_block; %fs:0x0030 */
377 append_composite_type_field (tib_type
, "process_environment_block",
379 /* uint32_t last_error_number; %fs:0x0034 */
380 append_composite_type_field (tib_type
, "last_error_number", dword_ptr_type
);
382 tib_ptr_type
= arch_type (gdbarch
, TYPE_CODE_PTR
,
383 TYPE_LENGTH (void_ptr_type
) * TARGET_CHAR_BIT
,
385 TYPE_TARGET_TYPE (tib_ptr_type
) = tib_type
;
387 windows_gdbarch_data
->tib_ptr_type
= tib_ptr_type
;
392 /* The $_tlb convenience variable is a bit special. We don't know
393 for sure the type of the value until we actually have a chance to
394 fetch the data. The type can change depending on gdbarch, so it is
395 also dependent on which thread you have selected. */
397 /* This function implements the lval_computed support for reading a
401 tlb_value_read (struct value
*val
)
404 struct type
*type
= check_typedef (value_type (val
));
406 if (!target_get_tib_address (inferior_ptid
, &tlb
))
407 error (_("Unable to read tlb"));
408 store_typed_address (value_contents_raw (val
), type
, tlb
);
411 /* This function implements the lval_computed support for writing a
415 tlb_value_write (struct value
*v
, struct value
*fromval
)
417 error (_("Impossible to change the Thread Local Base"));
420 static const struct lval_funcs tlb_value_funcs
=
427 /* Return a new value with the correct type for the tlb object of
428 the current thread using architecture GDBARCH. Return a void value
429 if there's no object available. */
431 static struct value
*
432 tlb_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
, void *ignore
)
434 if (target_has_stack
&& inferior_ptid
!= null_ptid
)
436 struct type
*type
= windows_get_tlb_type (gdbarch
);
437 return allocate_computed_value (type
, &tlb_value_funcs
, NULL
);
440 return allocate_value (builtin_type (gdbarch
)->builtin_void
);
444 /* Display thread information block of a given thread. */
447 display_one_tib (ptid_t ptid
)
449 gdb_byte
*tib
= NULL
;
451 CORE_ADDR thread_local_base
;
452 ULONGEST i
, val
, max
, max_name
, size
, tib_size
;
453 ULONGEST sizeof_ptr
= gdbarch_ptr_bit (target_gdbarch ());
454 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
456 if (sizeof_ptr
== 64)
458 size
= sizeof (uint64_t);
459 tib_size
= sizeof (thread_information_64
);
464 size
= sizeof (uint32_t);
465 tib_size
= sizeof (thread_information_32
);
471 if (maint_display_all_tib
)
473 tib_size
= FULL_TIB_SIZE
;
474 max
= tib_size
/ size
;
477 tib
= (gdb_byte
*) alloca (tib_size
);
479 if (target_get_tib_address (ptid
, &thread_local_base
) == 0)
481 printf_filtered (_("Unable to get thread local base for %s\n"),
482 target_pid_to_str (ptid
).c_str ());
486 if (target_read (current_top_target (), TARGET_OBJECT_MEMORY
,
487 NULL
, tib
, thread_local_base
, tib_size
) != tib_size
)
489 printf_filtered (_("Unable to read thread information "
490 "block for %s at address %s\n"),
491 target_pid_to_str (ptid
).c_str (),
492 paddress (target_gdbarch (), thread_local_base
));
496 printf_filtered (_("Thread Information Block %s at %s\n"),
497 target_pid_to_str (ptid
).c_str (),
498 paddress (target_gdbarch (), thread_local_base
));
500 index
= (gdb_byte
*) tib
;
502 /* All fields have the size of a pointer, this allows to iterate
503 using the same for loop for both layouts. */
504 for (i
= 0; i
< max
; i
++)
506 val
= extract_unsigned_integer (index
, size
, byte_order
);
508 printf_filtered (_("%s is 0x%s\n"), TIB_NAME
[i
], phex (val
, size
));
510 printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i
* size
, 2),
517 /* Display thread information block of the current thread. */
520 display_tib (const char * args
, int from_tty
)
522 if (inferior_ptid
!= null_ptid
)
523 display_one_tib (inferior_ptid
);
527 windows_xfer_shared_library (const char* so_name
, CORE_ADDR load_addr
,
528 CORE_ADDR
*text_offset_cached
,
529 struct gdbarch
*gdbarch
, struct obstack
*obstack
)
531 CORE_ADDR text_offset
= text_offset_cached
? *text_offset_cached
: 0;
533 obstack_grow_str (obstack
, "<library name=\"");
534 std::string p
= xml_escape_text (so_name
);
535 obstack_grow_str (obstack
, p
.c_str ());
536 obstack_grow_str (obstack
, "\"><segment address=\"");
540 gdb_bfd_ref_ptr
dll (gdb_bfd_open (so_name
, gnutarget
, -1));
541 /* The following calls are OK even if dll is NULL.
542 The default value 0x1000 is returned by pe_text_section_offset
544 text_offset
= pe_text_section_offset (dll
.get ());
545 if (text_offset_cached
)
546 *text_offset_cached
= text_offset
;
549 obstack_grow_str (obstack
, paddress (gdbarch
, load_addr
+ text_offset
));
550 obstack_grow_str (obstack
, "\"/></library>");
553 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
554 method. It searches all objfiles, starting with CURRENT_OBJFILE
557 On Windows, the system behaves a little differently when two
558 objfiles each define a global symbol using the same name, compared
559 to other platforms such as GNU/Linux for instance. On GNU/Linux,
560 all instances of the symbol effectively get merged into a single
561 one, but on Windows, they remain distinct.
563 As a result, it usually makes sense to start global symbol searches
564 with the current objfile before expanding it to all other objfiles.
565 This helps for instance when a user debugs some code in a DLL that
566 refers to a global variable defined inside that DLL. When trying
567 to print the value of that global variable, it would be unhelpful
568 to print the value of another global variable defined with the same
569 name, but in a different DLL. */
572 windows_iterate_over_objfiles_in_search_order
573 (struct gdbarch
*gdbarch
,
574 iterate_over_objfiles_in_search_order_cb_ftype
*cb
,
575 void *cb_data
, struct objfile
*current_objfile
)
581 stop
= cb (current_objfile
, cb_data
);
586 for (objfile
*objfile
: current_program_space
->objfiles ())
588 if (objfile
!= current_objfile
)
590 stop
= cb (objfile
, cb_data
);
598 show_maint_show_all_tib (struct ui_file
*file
, int from_tty
,
599 struct cmd_list_element
*c
, const char *value
)
601 fprintf_filtered (file
, _("Show all non-zero elements of "
602 "Thread Information Block is %s.\n"), value
);
605 /* Implementation of `gdbarch_gdb_signal_to_target' for Windows. */
608 windows_gdb_signal_to_target (struct gdbarch
*gdbarch
, enum gdb_signal signal
)
615 return WINDOWS_SIGHUP
;
617 return WINDOWS_SIGINT
;
618 case GDB_SIGNAL_QUIT
:
619 return WINDOWS_SIGQUIT
;
621 return WINDOWS_SIGILL
;
622 case GDB_SIGNAL_TRAP
:
623 return WINDOWS_SIGTRAP
;
624 case GDB_SIGNAL_ABRT
:
625 return WINDOWS_SIGABRT
;
627 return WINDOWS_SIGEMT
;
629 return WINDOWS_SIGFPE
;
630 case GDB_SIGNAL_KILL
:
631 return WINDOWS_SIGKILL
;
633 return WINDOWS_SIGBUS
;
634 case GDB_SIGNAL_SEGV
:
635 return WINDOWS_SIGSEGV
;
637 return WINDOWS_SIGSYS
;
638 case GDB_SIGNAL_PIPE
:
639 return WINDOWS_SIGPIPE
;
640 case GDB_SIGNAL_ALRM
:
641 return WINDOWS_SIGALRM
;
642 case GDB_SIGNAL_TERM
:
643 return WINDOWS_SIGTERM
;
648 /* Implementation of `gdbarch_gdb_signal_to_target' for Cygwin. */
651 cygwin_gdb_signal_to_target (struct gdbarch
*gdbarch
, enum gdb_signal signal
)
658 return CYGWIN_SIGHUP
;
660 return CYGWIN_SIGINT
;
661 case GDB_SIGNAL_QUIT
:
662 return CYGWIN_SIGQUIT
;
664 return CYGWIN_SIGILL
;
665 case GDB_SIGNAL_TRAP
:
666 return CYGWIN_SIGTRAP
;
667 case GDB_SIGNAL_ABRT
:
668 return CYGWIN_SIGABRT
;
670 return CYGWIN_SIGEMT
;
672 return CYGWIN_SIGFPE
;
673 case GDB_SIGNAL_KILL
:
674 return CYGWIN_SIGKILL
;
676 return CYGWIN_SIGBUS
;
677 case GDB_SIGNAL_SEGV
:
678 return CYGWIN_SIGSEGV
;
680 return CYGWIN_SIGSYS
;
681 case GDB_SIGNAL_PIPE
:
682 return CYGWIN_SIGPIPE
;
683 case GDB_SIGNAL_ALRM
:
684 return CYGWIN_SIGALRM
;
685 case GDB_SIGNAL_TERM
:
686 return CYGWIN_SIGTERM
;
688 return CYGWIN_SIGURG
;
689 case GDB_SIGNAL_STOP
:
690 return CYGWIN_SIGSTOP
;
691 case GDB_SIGNAL_TSTP
:
692 return CYGWIN_SIGTSTP
;
693 case GDB_SIGNAL_CONT
:
694 return CYGWIN_SIGCONT
;
695 case GDB_SIGNAL_CHLD
:
696 return CYGWIN_SIGCHLD
;
697 case GDB_SIGNAL_TTIN
:
698 return CYGWIN_SIGTTIN
;
699 case GDB_SIGNAL_TTOU
:
700 return CYGWIN_SIGTTOU
;
703 case GDB_SIGNAL_XCPU
:
704 return CYGWIN_SIGXCPU
;
705 case GDB_SIGNAL_XFSZ
:
706 return CYGWIN_SIGXFSZ
;
707 case GDB_SIGNAL_VTALRM
:
708 return CYGWIN_SIGVTALRM
;
709 case GDB_SIGNAL_PROF
:
710 return CYGWIN_SIGPROF
;
711 case GDB_SIGNAL_WINCH
:
712 return CYGWIN_SIGWINCH
;
714 return CYGWIN_SIGLOST
;
715 case GDB_SIGNAL_USR1
:
716 return CYGWIN_SIGUSR1
;
717 case GDB_SIGNAL_USR2
:
718 return CYGWIN_SIGUSR2
;
723 struct enum_value_name
729 /* Allocate a TYPE_CODE_ENUM type structure with its named values. */
732 create_enum (struct gdbarch
*gdbarch
, int bit
, const char *name
,
733 const struct enum_value_name
*values
, int count
)
738 type
= arch_type (gdbarch
, TYPE_CODE_ENUM
, bit
, name
);
739 TYPE_NFIELDS (type
) = count
;
740 TYPE_FIELDS (type
) = (struct field
*)
741 TYPE_ZALLOC (type
, sizeof (struct field
) * count
);
742 TYPE_UNSIGNED (type
) = 1;
744 for (i
= 0; i
< count
; i
++)
746 TYPE_FIELD_NAME (type
, i
) = values
[i
].name
;
747 SET_FIELD_ENUMVAL (TYPE_FIELD (type
, i
), values
[i
].value
);
753 static const struct enum_value_name exception_values
[] =
755 { 0x40000015, "FATAL_APP_EXIT" },
756 { 0x40010005, "DBG_CONTROL_C" },
757 { 0x40010008, "DBG_CONTROL_BREAK" },
758 { 0x80000002, "DATATYPE_MISALIGNMENT" },
759 { 0x80000003, "BREAKPOINT" },
760 { 0x80000004, "SINGLE_STEP" },
761 { 0xC0000005, "ACCESS_VIOLATION" },
762 { 0xC0000006, "IN_PAGE_ERROR" },
763 { 0xC000001D, "ILLEGAL_INSTRUCTION" },
764 { 0xC0000025, "NONCONTINUABLE_EXCEPTION" },
765 { 0xC0000026, "INVALID_DISPOSITION" },
766 { 0xC000008C, "ARRAY_BOUNDS_EXCEEDED" },
767 { 0xC000008D, "FLOAT_DENORMAL_OPERAND" },
768 { 0xC000008E, "FLOAT_DIVIDE_BY_ZERO" },
769 { 0xC000008F, "FLOAT_INEXACT_RESULT" },
770 { 0xC0000090, "FLOAT_INVALID_OPERATION" },
771 { 0xC0000091, "FLOAT_OVERFLOW" },
772 { 0xC0000092, "FLOAT_STACK_CHECK" },
773 { 0xC0000093, "FLOAT_UNDERFLOW" },
774 { 0xC0000094, "INTEGER_DIVIDE_BY_ZERO" },
775 { 0xC0000095, "INTEGER_OVERFLOW" },
776 { 0xC0000096, "PRIV_INSTRUCTION" },
777 { 0xC00000FD, "STACK_OVERFLOW" },
778 { 0xC0000409, "FAST_FAIL" },
781 static const struct enum_value_name violation_values
[] =
783 { 0, "READ_ACCESS_VIOLATION" },
784 { 1, "WRITE_ACCESS_VIOLATION" },
785 { 8, "DATA_EXECUTION_PREVENTION_VIOLATION" },
788 /* Implement the "get_siginfo_type" gdbarch method. */
791 windows_get_siginfo_type (struct gdbarch
*gdbarch
)
793 struct windows_gdbarch_data
*windows_gdbarch_data
;
794 struct type
*dword_type
, *pvoid_type
, *ulongptr_type
;
795 struct type
*code_enum
, *violation_enum
;
796 struct type
*violation_type
, *para_type
, *siginfo_ptr_type
, *siginfo_type
;
798 windows_gdbarch_data
= get_windows_gdbarch_data (gdbarch
);
799 if (windows_gdbarch_data
->siginfo_type
!= NULL
)
800 return windows_gdbarch_data
->siginfo_type
;
802 dword_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
804 pvoid_type
= arch_pointer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), "PVOID",
805 builtin_type (gdbarch
)->builtin_void
);
806 ulongptr_type
= arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
809 /* ExceptionCode value names */
810 code_enum
= create_enum (gdbarch
, gdbarch_int_bit (gdbarch
),
811 "ExceptionCode", exception_values
,
812 ARRAY_SIZE (exception_values
));
814 /* ACCESS_VIOLATION type names */
815 violation_enum
= create_enum (gdbarch
, gdbarch_ptr_bit (gdbarch
),
816 "ViolationType", violation_values
,
817 ARRAY_SIZE (violation_values
));
819 /* ACCESS_VIOLATION information */
820 violation_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
821 append_composite_type_field (violation_type
, "Type", violation_enum
);
822 append_composite_type_field (violation_type
, "Address", pvoid_type
);
824 /* Unnamed union of the documented field ExceptionInformation,
825 and the alternative AccessViolationInformation (which displays
826 human-readable values for ExceptionCode ACCESS_VIOLATION). */
827 para_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
828 append_composite_type_field (para_type
, "ExceptionInformation",
829 lookup_array_range_type (ulongptr_type
, 0, 14));
830 append_composite_type_field (para_type
, "AccessViolationInformation",
833 siginfo_type
= arch_composite_type (gdbarch
, "EXCEPTION_RECORD",
835 siginfo_ptr_type
= arch_pointer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
),
838 /* ExceptionCode is documented as type DWORD, but here a helper
839 enum type is used instead to display a human-readable value. */
840 append_composite_type_field (siginfo_type
, "ExceptionCode", code_enum
);
841 append_composite_type_field (siginfo_type
, "ExceptionFlags", dword_type
);
842 append_composite_type_field (siginfo_type
, "ExceptionRecord",
844 append_composite_type_field (siginfo_type
, "ExceptionAddress",
846 append_composite_type_field (siginfo_type
, "NumberParameters", dword_type
);
847 /* The 64-bit variant needs some padding. */
848 append_composite_type_field_aligned (siginfo_type
, "",
849 para_type
, TYPE_LENGTH (ulongptr_type
));
851 windows_gdbarch_data
->siginfo_type
= siginfo_type
;
856 /* Implement the "solib_create_inferior_hook" target_so_ops method. */
859 windows_solib_create_inferior_hook (int from_tty
)
861 CORE_ADDR exec_base
= 0;
863 /* Find base address of main executable in
864 TIB->process_environment_block->image_base_address. */
865 struct gdbarch
*gdbarch
= target_gdbarch ();
866 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
868 int peb_offset
; /* Offset of process_environment_block in TIB. */
869 int base_offset
; /* Offset of image_base_address in PEB. */
870 if (gdbarch_ptr_bit (gdbarch
) == 32)
884 if (target_has_execution
885 && target_get_tib_address (inferior_ptid
, &tlb
)
886 && !target_read_memory (tlb
+ peb_offset
, buf
, ptr_bytes
))
888 CORE_ADDR peb
= extract_unsigned_integer (buf
, ptr_bytes
, byte_order
);
889 if (!target_read_memory (peb
+ base_offset
, buf
, ptr_bytes
))
890 exec_base
= extract_unsigned_integer (buf
, ptr_bytes
, byte_order
);
893 /* Rebase executable if the base address changed because of ASLR. */
894 if (symfile_objfile
!= nullptr && exec_base
!= 0)
896 CORE_ADDR vmaddr
= pe_data (exec_bfd
)->pe_opthdr
.ImageBase
;
897 if (vmaddr
!= exec_base
)
898 objfile_rebase (symfile_objfile
, exec_base
- vmaddr
);
902 static struct target_so_ops windows_so_ops
;
904 /* Common parts for gdbarch initialization for the Windows and Cygwin OS
908 windows_init_abi_common (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
910 set_gdbarch_wchar_bit (gdbarch
, 16);
911 set_gdbarch_wchar_signed (gdbarch
, 0);
913 /* Canonical paths on this target look like
914 `c:\Program Files\Foo App\mydll.dll', for example. */
915 set_gdbarch_has_dos_based_file_system (gdbarch
, 1);
917 set_gdbarch_iterate_over_objfiles_in_search_order
918 (gdbarch
, windows_iterate_over_objfiles_in_search_order
);
920 windows_so_ops
= solib_target_so_ops
;
921 windows_so_ops
.solib_create_inferior_hook
922 = windows_solib_create_inferior_hook
;
923 set_solib_ops (gdbarch
, &windows_so_ops
);
925 set_gdbarch_get_siginfo_type (gdbarch
, windows_get_siginfo_type
);
928 /* See windows-tdep.h. */
930 windows_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
932 windows_init_abi_common (info
, gdbarch
);
933 set_gdbarch_gdb_signal_to_target (gdbarch
, windows_gdb_signal_to_target
);
936 /* See windows-tdep.h. */
939 cygwin_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
941 windows_init_abi_common (info
, gdbarch
);
942 set_gdbarch_gdb_signal_to_target (gdbarch
, cygwin_gdb_signal_to_target
);
945 /* Implementation of `tlb' variable. */
947 static const struct internalvar_funcs tlb_funcs
=
954 /* Layout of an element of a PE's Import Directory Table. Based on:
956 https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-directory-table
959 struct pe_import_directory_entry
961 uint32_t import_lookup_table_rva
;
963 uint32_t forwarder_chain
;
965 uint32_t import_address_table_rva
;
968 gdb_static_assert (sizeof (pe_import_directory_entry
) == 20);
970 /* See windows-tdep.h. */
973 is_linked_with_cygwin_dll (bfd
*abfd
)
975 /* The list of DLLs a PE is linked to is in the .idata section. See:
977 https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#the-idata-section
979 asection
*idata_section
= bfd_get_section_by_name (abfd
, ".idata");
980 if (idata_section
== nullptr)
983 bfd_size_type idata_section_size
= bfd_section_size (idata_section
);
984 internal_extra_pe_aouthdr
*pe_extra
= &pe_data (abfd
)->pe_opthdr
;
985 bfd_vma import_table_va
= pe_extra
->DataDirectory
[PE_IMPORT_TABLE
].VirtualAddress
;
986 bfd_vma idata_section_va
= bfd_section_vma (idata_section
);
988 /* The section's virtual address as reported by BFD has the image base applied,
990 gdb_assert (idata_section_va
>= pe_extra
->ImageBase
);
991 idata_section_va
-= pe_extra
->ImageBase
;
993 bfd_vma idata_section_end_va
= idata_section_va
+ idata_section_size
;
995 /* Make sure that the import table is indeed within the .idata section's range. */
996 if (import_table_va
< idata_section_va
997 || import_table_va
>= idata_section_end_va
)
1000 %s: import table's virtual address (0x%" BFD_VMA_FMT
"x) is outside .idata \
1001 section's range [0x%" BFD_VMA_FMT
"x, 0x%" BFD_VMA_FMT
"x[."),
1002 bfd_get_filename (abfd
), import_table_va
, idata_section_va
,
1003 idata_section_end_va
);
1007 /* The import table starts at this offset into the .idata section. */
1008 bfd_vma import_table_offset_in_sect
= import_table_va
- idata_section_va
;
1010 /* Get the section's data. */
1011 gdb::byte_vector idata_contents
;
1012 if (!gdb_bfd_get_full_section_contents (abfd
, idata_section
, &idata_contents
))
1014 warning (_("%s: failed to get contents of .idata section."),
1015 bfd_get_filename (abfd
));
1019 gdb_assert (idata_contents
.size () == idata_section_size
);
1021 const gdb_byte
*iter
= idata_contents
.data () + import_table_offset_in_sect
;
1022 const gdb_byte
*end
= idata_contents
.data () + idata_section_size
;
1023 const pe_import_directory_entry null_dir_entry
= { 0 };
1025 /* Iterate through all directory entries. */
1028 /* Is there enough space left in the section for another entry? */
1029 if (iter
+ sizeof (pe_import_directory_entry
) > end
)
1031 warning (_("%s: unexpected end of .idata section."),
1032 bfd_get_filename (abfd
));
1036 pe_import_directory_entry
*dir_entry
= (pe_import_directory_entry
*) iter
;
1038 /* Is it the end of list marker? */
1039 if (memcmp (dir_entry
, &null_dir_entry
,
1040 sizeof (pe_import_directory_entry
)) == 0)
1043 bfd_vma name_va
= dir_entry
->name_rva
;
1045 /* If the name's virtual address is smaller than the section's virtual
1046 address, there's a problem. */
1047 if (name_va
< idata_section_va
|| name_va
>= idata_section_end_va
)
1050 %s: name's virtual address (0x%" BFD_VMA_FMT
"x) is outside .idata section's \
1051 range [0x%" BFD_VMA_FMT
"x, 0x%" BFD_VMA_FMT
"x[."),
1052 bfd_get_filename (abfd
), name_va
, idata_section_va
,
1053 idata_section_end_va
);
1057 const gdb_byte
*name
= &idata_contents
[name_va
- idata_section_va
];
1059 /* Make sure we don't overshoot the end of the section with the streq. */
1060 if (name
+ sizeof (CYGWIN_DLL_NAME
) > end
)
1063 /* Finally, check if this is the dll name we are looking for. */
1064 if (streq ((const char *) name
, CYGWIN_DLL_NAME
))
1067 iter
+= sizeof (pe_import_directory_entry
);
1073 void _initialize_windows_tdep ();
1075 _initialize_windows_tdep ()
1077 windows_gdbarch_data_handle
1078 = gdbarch_data_register_post_init (init_windows_gdbarch_data
);
1080 add_basic_prefix_cmd ("w32", class_info
,
1081 _("Print information specific to Win32 debugging."),
1082 &info_w32_cmdlist
, "info w32 ", 0, &infolist
);
1084 add_cmd ("thread-information-block", class_info
, display_tib
,
1085 _("Display thread information block."),
1087 add_alias_cmd ("tib", "thread-information-block", class_info
, 1,
1090 add_setshow_boolean_cmd ("show-all-tib", class_maintenance
,
1091 &maint_display_all_tib
, _("\
1092 Set whether to display all non-zero fields of thread information block."), _("\
1093 Show whether to display all non-zero fields of thread information block."), _("\
1094 Use \"on\" to enable, \"off\" to disable.\n\
1095 If enabled, all non-zero fields of thread information block are displayed,\n\
1096 even if their meaning is unknown."),
1098 show_maint_show_all_tib
,
1099 &maintenance_set_cmdlist
,
1100 &maintenance_show_cmdlist
);
1102 /* Explicitly create without lookup, since that tries to create a
1103 value with a void typed value, and when we get here, gdbarch
1104 isn't initialized yet. At this point, we're quite sure there
1105 isn't another convenience variable of the same name. */
1106 create_internalvar_type_lazy ("_tlb", &tlb_funcs
, NULL
);