Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / windows-tdep.c
blobf9585adf8fa365d4ee841a7c3503f79b083c1159
1 /* Copyright (C) 2008-2024 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/>. */
18 #include "windows-tdep.h"
19 #include "gdbsupport/gdb_obstack.h"
20 #include "xml-support.h"
21 #include "gdbarch.h"
22 #include "target.h"
23 #include "value.h"
24 #include "inferior.h"
25 #include "command.h"
26 #include "gdbcmd.h"
27 #include "gdbthread.h"
28 #include "objfiles.h"
29 #include "symfile.h"
30 #include "coff-pe-read.h"
31 #include "gdb_bfd.h"
32 #include "solib.h"
33 #include "solib-target.h"
34 #include "frame-unwind.h"
35 #include "gdbcore.h"
36 #include "coff/internal.h"
37 #include "libcoff.h"
38 #include "solist.h"
40 #define CYGWIN_DLL_NAME "cygwin1.dll"
42 /* Windows signal numbers differ between MinGW flavors and between
43 those and Cygwin. The below enumerations were gleaned from the
44 respective headers. */
46 /* Signal numbers for the various MinGW flavors. The ones marked with
47 MinGW-w64 are defined by MinGW-w64, not by mingw.org's MinGW. */
49 enum
51 WINDOWS_SIGHUP = 1, /* MinGW-w64 */
52 WINDOWS_SIGINT = 2,
53 WINDOWS_SIGQUIT = 3, /* MinGW-w64 */
54 WINDOWS_SIGILL = 4,
55 WINDOWS_SIGTRAP = 5, /* MinGW-w64 */
56 WINDOWS_SIGIOT = 6, /* MinGW-w64 */
57 WINDOWS_SIGEMT = 7, /* MinGW-w64 */
58 WINDOWS_SIGFPE = 8,
59 WINDOWS_SIGKILL = 9, /* MinGW-w64 */
60 WINDOWS_SIGBUS = 10, /* MinGW-w64 */
61 WINDOWS_SIGSEGV = 11,
62 WINDOWS_SIGSYS = 12, /* MinGW-w64 */
63 WINDOWS_SIGPIPE = 13, /* MinGW-w64 */
64 WINDOWS_SIGALRM = 14, /* MinGW-w64 */
65 WINDOWS_SIGTERM = 15,
66 WINDOWS_SIGBREAK = 21,
67 WINDOWS_SIGABRT = 22,
70 /* Signal numbers for Cygwin. */
72 enum
74 CYGWIN_SIGHUP = 1,
75 CYGWIN_SIGINT = 2,
76 CYGWIN_SIGQUIT = 3,
77 CYGWIN_SIGILL = 4,
78 CYGWIN_SIGTRAP = 5,
79 CYGWIN_SIGABRT = 6,
80 CYGWIN_SIGEMT = 7,
81 CYGWIN_SIGFPE = 8,
82 CYGWIN_SIGKILL = 9,
83 CYGWIN_SIGBUS = 10,
84 CYGWIN_SIGSEGV = 11,
85 CYGWIN_SIGSYS = 12,
86 CYGWIN_SIGPIPE = 13,
87 CYGWIN_SIGALRM = 14,
88 CYGWIN_SIGTERM = 15,
89 CYGWIN_SIGURG = 16,
90 CYGWIN_SIGSTOP = 17,
91 CYGWIN_SIGTSTP = 18,
92 CYGWIN_SIGCONT = 19,
93 CYGWIN_SIGCHLD = 20,
94 CYGWIN_SIGTTIN = 21,
95 CYGWIN_SIGTTOU = 22,
96 CYGWIN_SIGIO = 23,
97 CYGWIN_SIGXCPU = 24,
98 CYGWIN_SIGXFSZ = 25,
99 CYGWIN_SIGVTALRM = 26,
100 CYGWIN_SIGPROF = 27,
101 CYGWIN_SIGWINCH = 28,
102 CYGWIN_SIGLOST = 29,
103 CYGWIN_SIGUSR1 = 30,
104 CYGWIN_SIGUSR2 = 31,
107 /* These constants are defined by Cygwin's core_dump.h */
108 static constexpr unsigned int NOTE_INFO_MODULE = 3;
109 static constexpr unsigned int NOTE_INFO_MODULE64 = 4;
111 struct cmd_list_element *info_w32_cmdlist;
113 typedef struct thread_information_block_32
115 uint32_t current_seh; /* %fs:0x0000 */
116 uint32_t current_top_of_stack; /* %fs:0x0004 */
117 uint32_t current_bottom_of_stack; /* %fs:0x0008 */
118 uint32_t sub_system_tib; /* %fs:0x000c */
119 uint32_t fiber_data; /* %fs:0x0010 */
120 uint32_t arbitrary_data_slot; /* %fs:0x0014 */
121 uint32_t linear_address_tib; /* %fs:0x0018 */
122 uint32_t environment_pointer; /* %fs:0x001c */
123 uint32_t process_id; /* %fs:0x0020 */
124 uint32_t current_thread_id; /* %fs:0x0024 */
125 uint32_t active_rpc_handle; /* %fs:0x0028 */
126 uint32_t thread_local_storage; /* %fs:0x002c */
127 uint32_t process_environment_block; /* %fs:0x0030 */
128 uint32_t last_error_number; /* %fs:0x0034 */
130 thread_information_32;
132 typedef struct thread_information_block_64
134 uint64_t current_seh; /* %gs:0x0000 */
135 uint64_t current_top_of_stack; /* %gs:0x0008 */
136 uint64_t current_bottom_of_stack; /* %gs:0x0010 */
137 uint64_t sub_system_tib; /* %gs:0x0018 */
138 uint64_t fiber_data; /* %gs:0x0020 */
139 uint64_t arbitrary_data_slot; /* %gs:0x0028 */
140 uint64_t linear_address_tib; /* %gs:0x0030 */
141 uint64_t environment_pointer; /* %gs:0x0038 */
142 uint64_t process_id; /* %gs:0x0040 */
143 uint64_t current_thread_id; /* %gs:0x0048 */
144 uint64_t active_rpc_handle; /* %gs:0x0050 */
145 uint64_t thread_local_storage; /* %gs:0x0058 */
146 uint64_t process_environment_block; /* %gs:0x0060 */
147 uint64_t last_error_number; /* %gs:0x0068 */
149 thread_information_64;
152 static const char* TIB_NAME[] =
154 " current_seh ", /* %fs:0x0000 */
155 " current_top_of_stack ", /* %fs:0x0004 */
156 " current_bottom_of_stack ", /* %fs:0x0008 */
157 " sub_system_tib ", /* %fs:0x000c */
158 " fiber_data ", /* %fs:0x0010 */
159 " arbitrary_data_slot ", /* %fs:0x0014 */
160 " linear_address_tib ", /* %fs:0x0018 */
161 " environment_pointer ", /* %fs:0x001c */
162 " process_id ", /* %fs:0x0020 */
163 " current_thread_id ", /* %fs:0x0024 */
164 " active_rpc_handle ", /* %fs:0x0028 */
165 " thread_local_storage ", /* %fs:0x002c */
166 " process_environment_block ", /* %fs:0x0030 */
167 " last_error_number " /* %fs:0x0034 */
170 static const int MAX_TIB32 =
171 sizeof (thread_information_32) / sizeof (uint32_t);
172 static const int MAX_TIB64 =
173 sizeof (thread_information_64) / sizeof (uint64_t);
174 static const int FULL_TIB_SIZE = 0x1000;
176 static bool maint_display_all_tib = false;
178 struct windows_gdbarch_data
180 struct type *siginfo_type = nullptr;
181 /* Type of thread information block. */
182 struct type *tib_ptr_type = nullptr;
185 static const registry<gdbarch>::key<windows_gdbarch_data>
186 windows_gdbarch_data_handle;
188 /* Get windows_gdbarch_data of an arch. */
190 static struct windows_gdbarch_data *
191 get_windows_gdbarch_data (struct gdbarch *gdbarch)
193 windows_gdbarch_data *result = windows_gdbarch_data_handle.get (gdbarch);
194 if (result == nullptr)
195 result = windows_gdbarch_data_handle.emplace (gdbarch);
196 return result;
199 /* Define Thread Local Base pointer type. */
201 static struct type *
202 windows_get_tlb_type (struct gdbarch *gdbarch)
204 struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
205 struct type *peb_ldr_type, *peb_ldr_ptr_type;
206 struct type *peb_type, *peb_ptr_type, *list_type;
207 struct type *module_list_ptr_type;
208 struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
209 struct type *word_type, *wchar_type, *wchar_ptr_type;
210 struct type *uni_str_type, *rupp_type, *rupp_ptr_type;
212 windows_gdbarch_data *windows_gdbarch_data
213 = get_windows_gdbarch_data (gdbarch);
214 if (windows_gdbarch_data->tib_ptr_type != nullptr)
215 return windows_gdbarch_data->tib_ptr_type;
217 type_allocator alloc (gdbarch);
219 dword_ptr_type = init_integer_type (alloc, gdbarch_ptr_bit (gdbarch),
220 1, "DWORD_PTR");
221 dword32_type = init_integer_type (alloc, 32,
222 1, "DWORD32");
223 word_type = init_integer_type (alloc, 16,
224 1, "WORD");
225 wchar_type = init_integer_type (alloc, 16,
226 1, "wchar_t");
227 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
228 wchar_ptr_type = init_pointer_type (alloc, gdbarch_ptr_bit (gdbarch),
229 nullptr, wchar_type);
231 /* list entry */
233 list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
234 list_type->set_name (xstrdup ("list"));
236 module_list_ptr_type = void_ptr_type;
238 append_composite_type_field (list_type, "forward_list",
239 module_list_ptr_type);
240 append_composite_type_field (list_type, "backward_list",
241 module_list_ptr_type);
243 /* Structured Exception Handler */
245 seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
246 seh_type->set_name (xstrdup ("seh"));
248 seh_ptr_type = alloc.new_type (TYPE_CODE_PTR,
249 void_ptr_type->length () * TARGET_CHAR_BIT,
250 NULL);
251 seh_ptr_type->set_target_type (seh_type);
253 append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
254 append_composite_type_field (seh_type, "handler",
255 builtin_type (gdbarch)->builtin_func_ptr);
257 /* struct _PEB_LDR_DATA */
258 peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
259 peb_ldr_type->set_name (xstrdup ("peb_ldr_data"));
261 append_composite_type_field (peb_ldr_type, "length", dword32_type);
262 append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
263 append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
264 append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
265 append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
266 append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
267 append_composite_type_field (peb_ldr_type, "entry_in_progress",
268 void_ptr_type);
269 peb_ldr_ptr_type = alloc.new_type (TYPE_CODE_PTR,
270 void_ptr_type->length () * TARGET_CHAR_BIT,
271 NULL);
272 peb_ldr_ptr_type->set_target_type (peb_ldr_type);
274 /* struct UNICODE_STRING */
275 uni_str_type = arch_composite_type (gdbarch, "unicode_string",
276 TYPE_CODE_STRUCT);
278 append_composite_type_field (uni_str_type, "length", word_type);
279 append_composite_type_field (uni_str_type, "maximum_length", word_type);
280 append_composite_type_field_aligned (uni_str_type, "buffer",
281 wchar_ptr_type,
282 wchar_ptr_type->length ());
284 /* struct _RTL_USER_PROCESS_PARAMETERS */
285 rupp_type = arch_composite_type (gdbarch, "rtl_user_process_parameters",
286 TYPE_CODE_STRUCT);
288 append_composite_type_field (rupp_type, "maximum_length", dword32_type);
289 append_composite_type_field (rupp_type, "length", dword32_type);
290 append_composite_type_field (rupp_type, "flags", dword32_type);
291 append_composite_type_field (rupp_type, "debug_flags", dword32_type);
292 append_composite_type_field (rupp_type, "console_handle", void_ptr_type);
293 append_composite_type_field (rupp_type, "console_flags", dword32_type);
294 append_composite_type_field_aligned (rupp_type, "standard_input",
295 void_ptr_type,
296 void_ptr_type->length ());
297 append_composite_type_field (rupp_type, "standard_output", void_ptr_type);
298 append_composite_type_field (rupp_type, "standard_error", void_ptr_type);
299 append_composite_type_field (rupp_type, "current_directory", uni_str_type);
300 append_composite_type_field (rupp_type, "current_directory_handle",
301 void_ptr_type);
302 append_composite_type_field (rupp_type, "dll_path", uni_str_type);
303 append_composite_type_field (rupp_type, "image_path_name", uni_str_type);
304 append_composite_type_field (rupp_type, "command_line", uni_str_type);
305 append_composite_type_field (rupp_type, "environment", void_ptr_type);
306 append_composite_type_field (rupp_type, "starting_x", dword32_type);
307 append_composite_type_field (rupp_type, "starting_y", dword32_type);
308 append_composite_type_field (rupp_type, "count_x", dword32_type);
309 append_composite_type_field (rupp_type, "count_y", dword32_type);
310 append_composite_type_field (rupp_type, "count_chars_x", dword32_type);
311 append_composite_type_field (rupp_type, "count_chars_y", dword32_type);
312 append_composite_type_field (rupp_type, "fill_attribute", dword32_type);
313 append_composite_type_field (rupp_type, "window_flags", dword32_type);
314 append_composite_type_field (rupp_type, "show_window_flags", dword32_type);
315 append_composite_type_field_aligned (rupp_type, "window_title",
316 uni_str_type,
317 void_ptr_type->length ());
318 append_composite_type_field (rupp_type, "desktop_info", uni_str_type);
319 append_composite_type_field (rupp_type, "shell_info", uni_str_type);
320 append_composite_type_field (rupp_type, "runtime_data", uni_str_type);
322 rupp_ptr_type = init_pointer_type (alloc, gdbarch_ptr_bit (gdbarch),
323 nullptr, rupp_type);
326 /* struct process environment block */
327 peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
328 peb_type->set_name (xstrdup ("peb"));
330 /* First bytes contain several flags. */
331 append_composite_type_field (peb_type, "flags", dword_ptr_type);
332 append_composite_type_field (peb_type, "mutant", void_ptr_type);
333 append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
334 append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
335 append_composite_type_field (peb_type, "process_parameters", rupp_ptr_type);
336 append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
337 append_composite_type_field (peb_type, "process_heap", void_ptr_type);
338 append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
339 peb_ptr_type = alloc.new_type (TYPE_CODE_PTR,
340 void_ptr_type->length () * TARGET_CHAR_BIT,
341 NULL);
342 peb_ptr_type->set_target_type (peb_type);
345 /* struct thread information block */
346 tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
347 tib_type->set_name (xstrdup ("tib"));
349 /* uint32_t current_seh; %fs:0x0000 */
350 append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
351 /* uint32_t current_top_of_stack; %fs:0x0004 */
352 append_composite_type_field (tib_type, "current_top_of_stack",
353 void_ptr_type);
354 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
355 append_composite_type_field (tib_type, "current_bottom_of_stack",
356 void_ptr_type);
357 /* uint32_t sub_system_tib; %fs:0x000c */
358 append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);
360 /* uint32_t fiber_data; %fs:0x0010 */
361 append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
362 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
363 append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
364 /* uint32_t linear_address_tib; %fs:0x0018 */
365 append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
366 /* uint32_t environment_pointer; %fs:0x001c */
367 append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
368 /* uint32_t process_id; %fs:0x0020 */
369 append_composite_type_field (tib_type, "process_id", dword_ptr_type);
370 /* uint32_t current_thread_id; %fs:0x0024 */
371 append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
372 /* uint32_t active_rpc_handle; %fs:0x0028 */
373 append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
374 /* uint32_t thread_local_storage; %fs:0x002c */
375 append_composite_type_field (tib_type, "thread_local_storage",
376 void_ptr_type);
377 /* uint32_t process_environment_block; %fs:0x0030 */
378 append_composite_type_field (tib_type, "process_environment_block",
379 peb_ptr_type);
380 /* uint32_t last_error_number; %fs:0x0034 */
381 append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);
383 tib_ptr_type = alloc.new_type (TYPE_CODE_PTR,
384 void_ptr_type->length () * TARGET_CHAR_BIT,
385 NULL);
386 tib_ptr_type->set_target_type (tib_type);
388 windows_gdbarch_data->tib_ptr_type = tib_ptr_type;
390 return tib_ptr_type;
393 /* The $_tlb convenience variable is a bit special. We don't know
394 for sure the type of the value until we actually have a chance to
395 fetch the data. The type can change depending on gdbarch, so it is
396 also dependent on which thread you have selected. */
398 /* This function implements the lval_computed support for reading a
399 $_tlb value. */
401 static void
402 tlb_value_read (struct value *val)
404 CORE_ADDR tlb;
405 struct type *type = check_typedef (val->type ());
407 if (!target_get_tib_address (inferior_ptid, &tlb))
408 error (_("Unable to read tlb"));
409 store_typed_address (val->contents_raw ().data (), type, tlb);
412 /* This function implements the lval_computed support for writing a
413 $_tlb value. */
415 static void
416 tlb_value_write (struct value *v, struct value *fromval)
418 error (_("Impossible to change the Thread Local Base"));
421 static const struct lval_funcs tlb_value_funcs =
423 tlb_value_read,
424 tlb_value_write
428 /* Return a new value with the correct type for the tlb object of
429 the current thread using architecture GDBARCH. Return a void value
430 if there's no object available. */
432 static struct value *
433 tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
435 if (target_has_stack () && inferior_ptid != null_ptid)
437 struct type *type = windows_get_tlb_type (gdbarch);
438 return value::allocate_computed (type, &tlb_value_funcs, NULL);
441 return value::allocate (builtin_type (gdbarch)->builtin_void);
445 /* Display thread information block of a given thread. */
447 static int
448 display_one_tib (ptid_t ptid)
450 gdb_byte *tib = NULL;
451 gdb_byte *index;
452 CORE_ADDR thread_local_base;
453 ULONGEST i, val, max, max_name, size, tib_size;
454 ULONGEST sizeof_ptr = gdbarch_ptr_bit (current_inferior ()->arch ());
455 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
457 if (sizeof_ptr == 64)
459 size = sizeof (uint64_t);
460 tib_size = sizeof (thread_information_64);
461 max = MAX_TIB64;
463 else
465 size = sizeof (uint32_t);
466 tib_size = sizeof (thread_information_32);
467 max = MAX_TIB32;
470 max_name = max;
472 if (maint_display_all_tib)
474 tib_size = FULL_TIB_SIZE;
475 max = tib_size / size;
478 tib = (gdb_byte *) alloca (tib_size);
480 if (target_get_tib_address (ptid, &thread_local_base) == 0)
482 gdb_printf (_("Unable to get thread local base for %s\n"),
483 target_pid_to_str (ptid).c_str ());
484 return -1;
487 if (target_read (current_inferior ()->top_target (), TARGET_OBJECT_MEMORY,
488 NULL, tib, thread_local_base, tib_size) != tib_size)
490 gdb_printf (_("Unable to read thread information "
491 "block for %s at address %s\n"),
492 target_pid_to_str (ptid).c_str (),
493 paddress (current_inferior ()->arch (), thread_local_base));
494 return -1;
497 gdb_printf (_("Thread Information Block %s at %s\n"),
498 target_pid_to_str (ptid).c_str (),
499 paddress (current_inferior ()->arch (), thread_local_base));
501 index = (gdb_byte *) tib;
503 /* All fields have the size of a pointer, this allows to iterate
504 using the same for loop for both layouts. */
505 for (i = 0; i < max; i++)
507 val = extract_unsigned_integer (index, size, byte_order);
508 if (i < max_name)
509 gdb_printf (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
510 else if (val != 0)
511 gdb_printf (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
512 phex (val, size));
513 index += size;
515 return 1;
518 /* Display thread information block of the current thread. */
520 static void
521 display_tib (const char * args, int from_tty)
523 if (inferior_ptid != null_ptid)
524 display_one_tib (inferior_ptid);
527 void
528 windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
529 CORE_ADDR *text_offset_cached,
530 struct gdbarch *gdbarch, std::string &xml)
532 CORE_ADDR text_offset = text_offset_cached ? *text_offset_cached : 0;
534 xml += "<library name=\"";
535 xml_escape_text_append (xml, so_name);
536 xml += "\"><segment address=\"";
538 if (!text_offset)
540 gdb_bfd_ref_ptr dll (gdb_bfd_open (so_name, gnutarget));
541 /* The following calls are OK even if dll is NULL.
542 The default value 0x1000 is returned by pe_text_section_offset
543 in that case. */
544 text_offset = pe_text_section_offset (dll.get ());
545 if (text_offset_cached)
546 *text_offset_cached = text_offset;
549 xml += paddress (gdbarch, load_addr + text_offset);
550 xml += "\"/></library>";
553 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
554 method. It searches all objfiles, starting with CURRENT_OBJFILE
555 first (if not NULL).
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. */
571 static void
572 windows_iterate_over_objfiles_in_search_order
573 (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
574 objfile *current_objfile)
576 if (current_objfile)
578 if (cb (current_objfile))
579 return;
582 for (objfile *objfile : current_program_space->objfiles ())
583 if (objfile != current_objfile)
585 if (cb (objfile))
586 return;
590 static void
591 show_maint_show_all_tib (struct ui_file *file, int from_tty,
592 struct cmd_list_element *c, const char *value)
594 gdb_printf (file, _("Show all non-zero elements of "
595 "Thread Information Block is %s.\n"), value);
599 static int w32_prefix_command_valid = 0;
600 void
601 init_w32_command_list (void)
603 if (!w32_prefix_command_valid)
605 add_basic_prefix_cmd
606 ("w32", class_info,
607 _("Print information specific to Win32 debugging."),
608 &info_w32_cmdlist, 0, &infolist);
609 w32_prefix_command_valid = 1;
613 /* Implementation of `gdbarch_gdb_signal_to_target' for Windows. */
615 static int
616 windows_gdb_signal_to_target (struct gdbarch *gdbarch, enum gdb_signal signal)
618 switch (signal)
620 case GDB_SIGNAL_0:
621 return 0;
622 case GDB_SIGNAL_HUP:
623 return WINDOWS_SIGHUP;
624 case GDB_SIGNAL_INT:
625 return WINDOWS_SIGINT;
626 case GDB_SIGNAL_QUIT:
627 return WINDOWS_SIGQUIT;
628 case GDB_SIGNAL_ILL:
629 return WINDOWS_SIGILL;
630 case GDB_SIGNAL_TRAP:
631 return WINDOWS_SIGTRAP;
632 case GDB_SIGNAL_ABRT:
633 return WINDOWS_SIGABRT;
634 case GDB_SIGNAL_EMT:
635 return WINDOWS_SIGEMT;
636 case GDB_SIGNAL_FPE:
637 return WINDOWS_SIGFPE;
638 case GDB_SIGNAL_KILL:
639 return WINDOWS_SIGKILL;
640 case GDB_SIGNAL_BUS:
641 return WINDOWS_SIGBUS;
642 case GDB_SIGNAL_SEGV:
643 return WINDOWS_SIGSEGV;
644 case GDB_SIGNAL_SYS:
645 return WINDOWS_SIGSYS;
646 case GDB_SIGNAL_PIPE:
647 return WINDOWS_SIGPIPE;
648 case GDB_SIGNAL_ALRM:
649 return WINDOWS_SIGALRM;
650 case GDB_SIGNAL_TERM:
651 return WINDOWS_SIGTERM;
653 return -1;
656 /* Implementation of `gdbarch_gdb_signal_to_target' for Cygwin. */
658 static int
659 cygwin_gdb_signal_to_target (struct gdbarch *gdbarch, enum gdb_signal signal)
661 switch (signal)
663 case GDB_SIGNAL_0:
664 return 0;
665 case GDB_SIGNAL_HUP:
666 return CYGWIN_SIGHUP;
667 case GDB_SIGNAL_INT:
668 return CYGWIN_SIGINT;
669 case GDB_SIGNAL_QUIT:
670 return CYGWIN_SIGQUIT;
671 case GDB_SIGNAL_ILL:
672 return CYGWIN_SIGILL;
673 case GDB_SIGNAL_TRAP:
674 return CYGWIN_SIGTRAP;
675 case GDB_SIGNAL_ABRT:
676 return CYGWIN_SIGABRT;
677 case GDB_SIGNAL_EMT:
678 return CYGWIN_SIGEMT;
679 case GDB_SIGNAL_FPE:
680 return CYGWIN_SIGFPE;
681 case GDB_SIGNAL_KILL:
682 return CYGWIN_SIGKILL;
683 case GDB_SIGNAL_BUS:
684 return CYGWIN_SIGBUS;
685 case GDB_SIGNAL_SEGV:
686 return CYGWIN_SIGSEGV;
687 case GDB_SIGNAL_SYS:
688 return CYGWIN_SIGSYS;
689 case GDB_SIGNAL_PIPE:
690 return CYGWIN_SIGPIPE;
691 case GDB_SIGNAL_ALRM:
692 return CYGWIN_SIGALRM;
693 case GDB_SIGNAL_TERM:
694 return CYGWIN_SIGTERM;
695 case GDB_SIGNAL_URG:
696 return CYGWIN_SIGURG;
697 case GDB_SIGNAL_STOP:
698 return CYGWIN_SIGSTOP;
699 case GDB_SIGNAL_TSTP:
700 return CYGWIN_SIGTSTP;
701 case GDB_SIGNAL_CONT:
702 return CYGWIN_SIGCONT;
703 case GDB_SIGNAL_CHLD:
704 return CYGWIN_SIGCHLD;
705 case GDB_SIGNAL_TTIN:
706 return CYGWIN_SIGTTIN;
707 case GDB_SIGNAL_TTOU:
708 return CYGWIN_SIGTTOU;
709 case GDB_SIGNAL_IO:
710 return CYGWIN_SIGIO;
711 case GDB_SIGNAL_XCPU:
712 return CYGWIN_SIGXCPU;
713 case GDB_SIGNAL_XFSZ:
714 return CYGWIN_SIGXFSZ;
715 case GDB_SIGNAL_VTALRM:
716 return CYGWIN_SIGVTALRM;
717 case GDB_SIGNAL_PROF:
718 return CYGWIN_SIGPROF;
719 case GDB_SIGNAL_WINCH:
720 return CYGWIN_SIGWINCH;
721 case GDB_SIGNAL_PWR:
722 return CYGWIN_SIGLOST;
723 case GDB_SIGNAL_USR1:
724 return CYGWIN_SIGUSR1;
725 case GDB_SIGNAL_USR2:
726 return CYGWIN_SIGUSR2;
728 return -1;
731 struct enum_value_name
733 uint32_t value;
734 const char *name;
737 /* Allocate a TYPE_CODE_ENUM type structure with its named values. */
739 static struct type *
740 create_enum (struct gdbarch *gdbarch, int bit, const char *name,
741 const struct enum_value_name *values, int count)
743 struct type *type;
744 int i;
746 type = type_allocator (gdbarch).new_type (TYPE_CODE_ENUM, bit, name);
747 type->alloc_fields (count);
748 type->set_is_unsigned (true);
750 for (i = 0; i < count; i++)
752 type->field (i).set_name (values[i].name);
753 type->field (i).set_loc_enumval (values[i].value);
756 return type;
759 static const struct enum_value_name exception_values[] =
761 { 0x40000015, "FATAL_APP_EXIT" },
762 { 0x4000001E, "WX86_SINGLE_STEP" },
763 { 0x4000001F, "WX86_BREAKPOINT" },
764 { 0x40010005, "DBG_CONTROL_C" },
765 { 0x40010008, "DBG_CONTROL_BREAK" },
766 { 0x80000002, "DATATYPE_MISALIGNMENT" },
767 { 0x80000003, "BREAKPOINT" },
768 { 0x80000004, "SINGLE_STEP" },
769 { 0xC0000005, "ACCESS_VIOLATION" },
770 { 0xC0000006, "IN_PAGE_ERROR" },
771 { 0xC000001D, "ILLEGAL_INSTRUCTION" },
772 { 0xC0000025, "NONCONTINUABLE_EXCEPTION" },
773 { 0xC0000026, "INVALID_DISPOSITION" },
774 { 0xC000008C, "ARRAY_BOUNDS_EXCEEDED" },
775 { 0xC000008D, "FLOAT_DENORMAL_OPERAND" },
776 { 0xC000008E, "FLOAT_DIVIDE_BY_ZERO" },
777 { 0xC000008F, "FLOAT_INEXACT_RESULT" },
778 { 0xC0000090, "FLOAT_INVALID_OPERATION" },
779 { 0xC0000091, "FLOAT_OVERFLOW" },
780 { 0xC0000092, "FLOAT_STACK_CHECK" },
781 { 0xC0000093, "FLOAT_UNDERFLOW" },
782 { 0xC0000094, "INTEGER_DIVIDE_BY_ZERO" },
783 { 0xC0000095, "INTEGER_OVERFLOW" },
784 { 0xC0000096, "PRIV_INSTRUCTION" },
785 { 0xC00000FD, "STACK_OVERFLOW" },
786 { 0xC0000409, "FAST_FAIL" },
789 static const struct enum_value_name violation_values[] =
791 { 0, "READ_ACCESS_VIOLATION" },
792 { 1, "WRITE_ACCESS_VIOLATION" },
793 { 8, "DATA_EXECUTION_PREVENTION_VIOLATION" },
796 /* Implement the "get_siginfo_type" gdbarch method. */
798 static struct type *
799 windows_get_siginfo_type (struct gdbarch *gdbarch)
801 struct windows_gdbarch_data *windows_gdbarch_data;
802 struct type *dword_type, *pvoid_type, *ulongptr_type;
803 struct type *code_enum, *violation_enum;
804 struct type *violation_type, *para_type, *siginfo_ptr_type, *siginfo_type;
806 windows_gdbarch_data = get_windows_gdbarch_data (gdbarch);
807 if (windows_gdbarch_data->siginfo_type != NULL)
808 return windows_gdbarch_data->siginfo_type;
810 type_allocator alloc (gdbarch);
811 dword_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
812 1, "DWORD");
813 pvoid_type = init_pointer_type (alloc, gdbarch_ptr_bit (gdbarch), "PVOID",
814 builtin_type (gdbarch)->builtin_void);
815 ulongptr_type = init_integer_type (alloc, gdbarch_ptr_bit (gdbarch),
816 1, "ULONG_PTR");
818 /* ExceptionCode value names */
819 code_enum = create_enum (gdbarch, gdbarch_int_bit (gdbarch),
820 "ExceptionCode", exception_values,
821 ARRAY_SIZE (exception_values));
823 /* ACCESS_VIOLATION type names */
824 violation_enum = create_enum (gdbarch, gdbarch_ptr_bit (gdbarch),
825 "ViolationType", violation_values,
826 ARRAY_SIZE (violation_values));
828 /* ACCESS_VIOLATION information */
829 violation_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
830 append_composite_type_field (violation_type, "Type", violation_enum);
831 append_composite_type_field (violation_type, "Address", pvoid_type);
833 /* Unnamed union of the documented field ExceptionInformation,
834 and the alternative AccessViolationInformation (which displays
835 human-readable values for ExceptionCode ACCESS_VIOLATION). */
836 para_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
837 append_composite_type_field (para_type, "ExceptionInformation",
838 lookup_array_range_type (ulongptr_type, 0, 14));
839 append_composite_type_field (para_type, "AccessViolationInformation",
840 violation_type);
842 siginfo_type = arch_composite_type (gdbarch, "EXCEPTION_RECORD",
843 TYPE_CODE_STRUCT);
844 siginfo_ptr_type = init_pointer_type (alloc, gdbarch_ptr_bit (gdbarch),
845 nullptr, siginfo_type);
847 /* ExceptionCode is documented as type DWORD, but here a helper
848 enum type is used instead to display a human-readable value. */
849 append_composite_type_field (siginfo_type, "ExceptionCode", code_enum);
850 append_composite_type_field (siginfo_type, "ExceptionFlags", dword_type);
851 append_composite_type_field (siginfo_type, "ExceptionRecord",
852 siginfo_ptr_type);
853 append_composite_type_field (siginfo_type, "ExceptionAddress",
854 pvoid_type);
855 append_composite_type_field (siginfo_type, "NumberParameters", dword_type);
856 /* The 64-bit variant needs some padding. */
857 append_composite_type_field_aligned (siginfo_type, "",
858 para_type, ulongptr_type->length ());
860 windows_gdbarch_data->siginfo_type = siginfo_type;
862 return siginfo_type;
865 /* Implement the "solib_create_inferior_hook" solib_ops method. */
867 static void
868 windows_solib_create_inferior_hook (int from_tty)
870 CORE_ADDR exec_base = 0;
872 /* Find base address of main executable in
873 TIB->process_environment_block->image_base_address. */
874 gdbarch *gdbarch = current_inferior ()->arch ();
875 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
876 int ptr_bytes;
877 int peb_offset; /* Offset of process_environment_block in TIB. */
878 int base_offset; /* Offset of image_base_address in PEB. */
879 if (gdbarch_ptr_bit (gdbarch) == 32)
881 ptr_bytes = 4;
882 peb_offset = 48;
883 base_offset = 8;
885 else
887 ptr_bytes = 8;
888 peb_offset = 96;
889 base_offset = 16;
891 CORE_ADDR tlb;
892 gdb_byte buf[8];
893 if (target_has_execution ()
894 && target_get_tib_address (inferior_ptid, &tlb)
895 && !target_read_memory (tlb + peb_offset, buf, ptr_bytes))
897 CORE_ADDR peb = extract_unsigned_integer (buf, ptr_bytes, byte_order);
898 if (!target_read_memory (peb + base_offset, buf, ptr_bytes))
899 exec_base = extract_unsigned_integer (buf, ptr_bytes, byte_order);
902 /* Rebase executable if the base address changed because of ASLR. */
903 if (current_program_space->symfile_object_file != nullptr && exec_base != 0)
905 CORE_ADDR vmaddr
906 = pe_data (current_program_space->exec_bfd ())->pe_opthdr.ImageBase;
907 if (vmaddr != exec_base)
908 objfile_rebase (current_program_space->symfile_object_file,
909 exec_base - vmaddr);
913 static solib_ops windows_so_ops;
915 /* Common parts for gdbarch initialization for the Windows and Cygwin OS
916 ABIs. */
918 static void
919 windows_init_abi_common (struct gdbarch_info info, struct gdbarch *gdbarch)
921 set_gdbarch_wchar_bit (gdbarch, 16);
922 set_gdbarch_wchar_signed (gdbarch, 0);
924 /* Canonical paths on this target look like
925 `c:\Program Files\Foo App\mydll.dll', for example. */
926 set_gdbarch_has_dos_based_file_system (gdbarch, 1);
928 set_gdbarch_iterate_over_objfiles_in_search_order
929 (gdbarch, windows_iterate_over_objfiles_in_search_order);
931 windows_so_ops = solib_target_so_ops;
932 windows_so_ops.solib_create_inferior_hook
933 = windows_solib_create_inferior_hook;
934 set_gdbarch_so_ops (gdbarch, &windows_so_ops);
936 set_gdbarch_get_siginfo_type (gdbarch, windows_get_siginfo_type);
939 /* See windows-tdep.h. */
940 void
941 windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
943 windows_init_abi_common (info, gdbarch);
944 set_gdbarch_gdb_signal_to_target (gdbarch, windows_gdb_signal_to_target);
947 /* See windows-tdep.h. */
949 void
950 cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
952 windows_init_abi_common (info, gdbarch);
953 set_gdbarch_gdb_signal_to_target (gdbarch, cygwin_gdb_signal_to_target);
956 /* Implementation of `tlb' variable. */
958 static const struct internalvar_funcs tlb_funcs =
960 tlb_make_value,
961 NULL,
964 /* Layout of an element of a PE's Import Directory Table. Based on:
966 https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-directory-table
969 struct pe_import_directory_entry
971 uint32_t import_lookup_table_rva;
972 uint32_t timestamp;
973 uint32_t forwarder_chain;
974 uint32_t name_rva;
975 uint32_t import_address_table_rva;
978 static_assert (sizeof (pe_import_directory_entry) == 20);
980 /* See windows-tdep.h. */
982 bool
983 is_linked_with_cygwin_dll (bfd *abfd)
985 /* The list of DLLs a PE is linked to is in the .idata section. See:
987 https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#the-idata-section
989 asection *idata_section = bfd_get_section_by_name (abfd, ".idata");
990 if (idata_section == nullptr)
991 return false;
993 bfd_size_type idata_section_size = bfd_section_size (idata_section);
994 internal_extra_pe_aouthdr *pe_extra = &pe_data (abfd)->pe_opthdr;
995 bfd_vma import_table_va = pe_extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
996 bfd_vma idata_section_va = bfd_section_vma (idata_section);
998 /* The section's virtual address as reported by BFD has the image base applied,
999 remove it. */
1000 gdb_assert (idata_section_va >= pe_extra->ImageBase);
1001 idata_section_va -= pe_extra->ImageBase;
1003 bfd_vma idata_section_end_va = idata_section_va + idata_section_size;
1005 /* Make sure that the import table is indeed within the .idata section's range. */
1006 if (import_table_va < idata_section_va
1007 || import_table_va >= idata_section_end_va)
1009 warning (_("\
1010 %s: import table's virtual address (%s) is outside .idata \
1011 section's range [%s, %s]."),
1012 bfd_get_filename (abfd), hex_string (import_table_va),
1013 hex_string (idata_section_va),
1014 hex_string (idata_section_end_va));
1015 return false;
1018 /* The import table starts at this offset into the .idata section. */
1019 bfd_vma import_table_offset_in_sect = import_table_va - idata_section_va;
1021 /* Get the section's data. */
1022 gdb::byte_vector idata_contents;
1023 if (!gdb_bfd_get_full_section_contents (abfd, idata_section, &idata_contents))
1025 warning (_("%s: failed to get contents of .idata section."),
1026 bfd_get_filename (abfd));
1027 return false;
1030 gdb_assert (idata_contents.size () == idata_section_size);
1032 const gdb_byte *iter = idata_contents.data () + import_table_offset_in_sect;
1033 const gdb_byte *end = idata_contents.data () + idata_section_size;
1034 const pe_import_directory_entry null_dir_entry = { 0 };
1036 /* Iterate through all directory entries. */
1037 while (true)
1039 /* Is there enough space left in the section for another entry? */
1040 if (iter + sizeof (pe_import_directory_entry) > end)
1042 warning (_("%s: unexpected end of .idata section."),
1043 bfd_get_filename (abfd));
1044 break;
1047 pe_import_directory_entry *dir_entry = (pe_import_directory_entry *) iter;
1049 /* Is it the end of list marker? */
1050 if (memcmp (dir_entry, &null_dir_entry,
1051 sizeof (pe_import_directory_entry)) == 0)
1052 break;
1054 bfd_vma name_va = dir_entry->name_rva;
1056 /* If the name's virtual address is smaller than the section's virtual
1057 address, there's a problem. */
1058 if (name_va < idata_section_va || name_va >= idata_section_end_va)
1060 warning (_("\
1061 %s: name's virtual address (%s) is outside .idata section's \
1062 range [%s, %s]."),
1063 bfd_get_filename (abfd), hex_string (name_va),
1064 hex_string (idata_section_va),
1065 hex_string (idata_section_end_va));
1066 break;
1069 const gdb_byte *name = &idata_contents[name_va - idata_section_va];
1071 /* Make sure we don't overshoot the end of the section with the
1072 streq. */
1073 if (name + sizeof (CYGWIN_DLL_NAME) <= end)
1075 /* Finally, check if this is the dll name we are looking for. */
1076 if (streq ((const char *) name, CYGWIN_DLL_NAME))
1077 return true;
1080 iter += sizeof (pe_import_directory_entry);
1083 return false;
1086 struct cpms_data
1088 struct gdbarch *gdbarch;
1089 std::string xml;
1090 int module_count;
1093 static void
1094 core_process_module_section (bfd *abfd, asection *sect, void *obj)
1096 struct cpms_data *data = (struct cpms_data *) obj;
1097 enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);
1099 unsigned int data_type;
1100 char *module_name;
1101 size_t module_name_size;
1102 size_t module_name_offset;
1103 CORE_ADDR base_addr;
1105 if (!startswith (sect->name, ".module"))
1106 return;
1108 gdb::byte_vector buf (bfd_section_size (sect) + 1);
1109 if (!bfd_get_section_contents (abfd, sect,
1110 buf.data (), 0, bfd_section_size (sect)))
1111 return;
1112 /* We're going to treat part of the buffer as a string, so make sure
1113 it is NUL-terminated. */
1114 buf.back () = 0;
1116 /* A DWORD (data_type) followed by struct windows_core_module_info. */
1117 if (bfd_section_size (sect) < 4)
1118 return;
1119 data_type = extract_unsigned_integer (buf.data (), 4, byte_order);
1121 if (data_type == NOTE_INFO_MODULE)
1123 module_name_offset = 12;
1124 if (bfd_section_size (sect) < module_name_offset)
1125 return;
1126 base_addr = extract_unsigned_integer (&buf[4], 4, byte_order);
1127 module_name_size = extract_unsigned_integer (&buf[8], 4, byte_order);
1129 else if (data_type == NOTE_INFO_MODULE64)
1131 module_name_offset = 16;
1132 if (bfd_section_size (sect) < module_name_offset)
1133 return;
1134 base_addr = extract_unsigned_integer (&buf[4], 8, byte_order);
1135 module_name_size = extract_unsigned_integer (&buf[12], 4, byte_order);
1137 else
1138 return;
1140 if (module_name_offset + module_name_size > bfd_section_size (sect))
1141 return;
1142 module_name = (char *) buf.data () + module_name_offset;
1144 /* The first module is the .exe itself. */
1145 if (data->module_count != 0)
1146 windows_xfer_shared_library (module_name, base_addr,
1147 NULL, data->gdbarch, data->xml);
1148 data->module_count++;
1151 ULONGEST
1152 windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
1153 gdb_byte *readbuf,
1154 ULONGEST offset, ULONGEST len)
1156 cpms_data data { gdbarch, "<library-list>\n", 0 };
1157 bfd_map_over_sections (current_program_space->core_bfd (),
1158 core_process_module_section,
1159 &data);
1160 data.xml += "</library-list>\n";
1162 ULONGEST len_avail = data.xml.length ();
1163 if (offset >= len_avail)
1164 return 0;
1166 if (len > len_avail - offset)
1167 len = len_avail - offset;
1169 memcpy (readbuf, data.xml.data () + offset, len);
1171 return len;
1174 /* This is how we want PTIDs from core files to be printed. */
1176 std::string
1177 windows_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
1179 if (ptid.lwp () != 0)
1180 return string_printf ("Thread 0x%lx", ptid.lwp ());
1182 return normal_pid_to_str (ptid);
1185 void _initialize_windows_tdep ();
1186 void
1187 _initialize_windows_tdep ()
1189 init_w32_command_list ();
1190 cmd_list_element *info_w32_thread_information_block_cmd
1191 = add_cmd ("thread-information-block", class_info, display_tib,
1192 _("Display thread information block."),
1193 &info_w32_cmdlist);
1194 add_alias_cmd ("tib", info_w32_thread_information_block_cmd, class_info, 1,
1195 &info_w32_cmdlist);
1197 add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
1198 &maint_display_all_tib, _("\
1199 Set whether to display all non-zero fields of thread information block."), _("\
1200 Show whether to display all non-zero fields of thread information block."), _("\
1201 Use \"on\" to enable, \"off\" to disable.\n\
1202 If enabled, all non-zero fields of thread information block are displayed,\n\
1203 even if their meaning is unknown."),
1204 NULL,
1205 show_maint_show_all_tib,
1206 &maintenance_set_cmdlist,
1207 &maintenance_show_cmdlist);
1209 /* Explicitly create without lookup, since that tries to create a
1210 value with a void typed value, and when we get here, gdbarch
1211 isn't initialized yet. At this point, we're quite sure there
1212 isn't another convenience variable of the same name. */
1213 create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);
1216 /* Frame cache data for the cygwin sigwrapper unwinder. */
1218 struct cygwin_sigwrapper_frame_cache
1220 CORE_ADDR prev_pc;
1221 int tlsoffset;
1224 /* Return true if the instructions at PC match the instructions bytes
1225 in PATTERN. Returns false otherwise. */
1227 static bool
1228 insns_match_pattern (CORE_ADDR pc,
1229 const gdb::array_view<const gdb_byte> pattern)
1231 for (size_t i = 0; i < pattern.size (); i++)
1233 gdb_byte buf;
1234 if (target_read_code (pc + i, &buf, 1) != 0)
1235 return false;
1236 if (buf != pattern[i])
1237 return false;
1239 return true;
1242 /* Helper for cygwin_sigwrapper_frame_cache. Search for one of the
1243 patterns in PATTERNS_LIST within [START, END). If found, record
1244 the tls offset found after the matched pattern in the instruction
1245 stream, in *TLSOFFSET. */
1247 static void
1248 cygwin_sigwrapper_frame_analyze
1249 (struct gdbarch *gdbarch,
1250 CORE_ADDR start, CORE_ADDR end,
1251 gdb::array_view<const gdb::array_view<const gdb_byte>> patterns_list,
1252 int *tlsoffset)
1254 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1256 *tlsoffset = 0;
1258 for (CORE_ADDR addr = start; addr < end; addr++)
1260 for (auto patterns : patterns_list)
1262 if (insns_match_pattern (addr, patterns))
1264 /* The instruction sequence is followed by 4 bytes for
1265 tls::stackptr. */
1266 gdb_byte tls_stackptr[4];
1267 if (target_read_code (addr + patterns.size (), tls_stackptr, 4) == 0)
1269 *tlsoffset = extract_signed_integer (tls_stackptr, 4, byte_order);
1271 frame_debug_printf ("matched pattern at %s, sigstackptroffset=%x",
1272 paddress (gdbarch, addr),
1273 *tlsoffset);
1274 break;
1280 /* XXX: Perhaps we should also note the address of the xaddq
1281 instruction which pops the RA from the sigstack. If PC is after
1282 that, we should look in the appropriate register to get the RA,
1283 not on the sigstack. */
1286 /* Fill THIS_CACHE using the cygwin sigwrapper unwinding data for
1287 THIS_FRAME. */
1289 static cygwin_sigwrapper_frame_cache *
1290 cygwin_sigwrapper_frame_cache (frame_info_ptr this_frame, void **this_cache)
1292 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1293 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1294 auto *cache = (struct cygwin_sigwrapper_frame_cache *) *this_cache;
1295 const int len = gdbarch_addr_bit (gdbarch) / 8;
1297 /* Get address of top of stack from thread information block. */
1298 CORE_ADDR thread_local_base;
1299 target_get_tib_address (inferior_ptid, &thread_local_base);
1301 CORE_ADDR stacktop
1302 = read_memory_unsigned_integer (thread_local_base + len, len, byte_order);
1304 frame_debug_printf ("TEB.stacktop=%s", paddress (gdbarch, stacktop));
1306 /* Find cygtls, relative to stacktop, and read signalstackptr from
1307 cygtls. */
1308 CORE_ADDR signalstackptr
1309 = read_memory_unsigned_integer (stacktop + cache->tlsoffset,
1310 len, byte_order);
1312 frame_debug_printf ("sigsp=%s", paddress (gdbarch, signalstackptr));
1314 /* Read return address from signal stack. */
1315 cache->prev_pc
1316 = read_memory_unsigned_integer (signalstackptr - len, len, byte_order);
1318 frame_debug_printf ("ra=%s", paddress (gdbarch, cache->prev_pc));
1320 return cache;
1323 static struct value *
1324 cygwin_sigwrapper_frame_prev_register (const frame_info_ptr &this_frame,
1325 void **this_cache,
1326 int regnum)
1328 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1329 struct cygwin_sigwrapper_frame_cache *cache
1330 = cygwin_sigwrapper_frame_cache (this_frame, this_cache);
1332 frame_debug_printf ("%s for pc=%s",
1333 gdbarch_register_name (gdbarch, regnum),
1334 paddress (gdbarch, cache->prev_pc));
1336 if (regnum == gdbarch_pc_regnum (gdbarch))
1337 return frame_unwind_got_address (this_frame, regnum, cache->prev_pc);
1339 return frame_unwind_got_register (this_frame, regnum, regnum);
1342 static void
1343 cygwin_sigwrapper_frame_this_id (const frame_info_ptr &this_frame,
1344 void **this_cache,
1345 struct frame_id *this_id)
1347 *this_id = frame_id_build_unavailable_stack (get_frame_func (this_frame));
1350 static int
1351 cygwin_sigwrapper_frame_sniffer (const struct frame_unwind *self_,
1352 const frame_info_ptr &this_frame,
1353 void **this_cache)
1355 const auto *self = (const struct cygwin_sigwrapper_frame_unwind *) self_;
1356 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1358 CORE_ADDR pc = get_frame_pc (this_frame);
1359 const char *name;
1360 CORE_ADDR start, end;
1361 find_pc_partial_function (pc, &name, &start, &end);
1363 if (name == nullptr)
1364 return 0;
1366 if (strcmp (name, "_sigbe") != 0
1367 && strcmp (name, "__sigbe") != 0
1368 && strcmp (name, "sigdelayed") != 0
1369 && strcmp (name, "_sigdelayed") != 0)
1370 return 0;
1372 frame_debug_printf ("name=%s, start=%s, end=%s",
1373 name,
1374 paddress (gdbarch, start),
1375 paddress (gdbarch, end));
1377 int tlsoffset;
1378 cygwin_sigwrapper_frame_analyze (gdbarch, start, end, self->patterns_list,
1379 &tlsoffset);
1380 if (tlsoffset == 0)
1381 return 0;
1383 frame_debug_printf ("sigstackptroffset=%x", tlsoffset);
1385 auto *cache = FRAME_OBSTACK_ZALLOC (struct cygwin_sigwrapper_frame_cache);
1386 cache->tlsoffset = tlsoffset;
1388 *this_cache = cache;
1389 cygwin_sigwrapper_frame_cache (this_frame, this_cache);
1391 return 1;
1394 /* Cygwin sigwapper unwinder. */
1396 cygwin_sigwrapper_frame_unwind::cygwin_sigwrapper_frame_unwind
1397 (gdb::array_view<const gdb::array_view<const gdb_byte>> patterns_list)
1398 : frame_unwind (),
1399 patterns_list (patterns_list)
1401 name = "cygwin sigwrapper";
1402 type = NORMAL_FRAME;
1403 stop_reason = default_frame_unwind_stop_reason;
1404 this_id = cygwin_sigwrapper_frame_this_id;
1405 prev_register = cygwin_sigwrapper_frame_prev_register;
1406 sniffer = cygwin_sigwrapper_frame_sniffer;