* ppc-opc.c (powerpc_opcodes) <"lswx">: Use RAX for the second and
[binutils-gdb.git] / gdb / windows-tdep.c
blob116525c9fa290c175a045599222288fd35a19915
1 /* Copyright (C) 2008-2012 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 "defs.h"
19 #include "windows-tdep.h"
20 #include "gdb_obstack.h"
21 #include "xml-support.h"
22 #include "gdbarch.h"
23 #include "target.h"
24 #include "value.h"
25 #include "inferior.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "gdbthread.h"
29 #include "objfiles.h"
31 struct cmd_list_element *info_w32_cmdlist;
33 typedef struct thread_information_block_32
35 uint32_t current_seh; /* %fs:0x0000 */
36 uint32_t current_top_of_stack; /* %fs:0x0004 */
37 uint32_t current_bottom_of_stack; /* %fs:0x0008 */
38 uint32_t sub_system_tib; /* %fs:0x000c */
39 uint32_t fiber_data; /* %fs:0x0010 */
40 uint32_t arbitrary_data_slot; /* %fs:0x0014 */
41 uint32_t linear_address_tib; /* %fs:0x0018 */
42 uint32_t environment_pointer; /* %fs:0x001c */
43 uint32_t process_id; /* %fs:0x0020 */
44 uint32_t current_thread_id; /* %fs:0x0024 */
45 uint32_t active_rpc_handle; /* %fs:0x0028 */
46 uint32_t thread_local_storage; /* %fs:0x002c */
47 uint32_t process_environment_block; /* %fs:0x0030 */
48 uint32_t last_error_number; /* %fs:0x0034 */
50 thread_information_32;
52 typedef struct thread_information_block_64
54 uint64_t current_seh; /* %gs:0x0000 */
55 uint64_t current_top_of_stack; /* %gs:0x0008 */
56 uint64_t current_bottom_of_stack; /* %gs:0x0010 */
57 uint64_t sub_system_tib; /* %gs:0x0018 */
58 uint64_t fiber_data; /* %gs:0x0020 */
59 uint64_t arbitrary_data_slot; /* %gs:0x0028 */
60 uint64_t linear_address_tib; /* %gs:0x0030 */
61 uint64_t environment_pointer; /* %gs:0x0038 */
62 uint64_t process_id; /* %gs:0x0040 */
63 uint64_t current_thread_id; /* %gs:0x0048 */
64 uint64_t active_rpc_handle; /* %gs:0x0050 */
65 uint64_t thread_local_storage; /* %gs:0x0058 */
66 uint64_t process_environment_block; /* %gs:0x0060 */
67 uint64_t last_error_number; /* %gs:0x0068 */
69 thread_information_64;
72 static const char* TIB_NAME[] =
74 " current_seh ", /* %fs:0x0000 */
75 " current_top_of_stack ", /* %fs:0x0004 */
76 " current_bottom_of_stack ", /* %fs:0x0008 */
77 " sub_system_tib ", /* %fs:0x000c */
78 " fiber_data ", /* %fs:0x0010 */
79 " arbitrary_data_slot ", /* %fs:0x0014 */
80 " linear_address_tib ", /* %fs:0x0018 */
81 " environment_pointer ", /* %fs:0x001c */
82 " process_id ", /* %fs:0x0020 */
83 " current_thread_id ", /* %fs:0x0024 */
84 " active_rpc_handle ", /* %fs:0x0028 */
85 " thread_local_storage ", /* %fs:0x002c */
86 " process_environment_block ", /* %fs:0x0030 */
87 " last_error_number " /* %fs:0x0034 */
90 static const int MAX_TIB32 =
91 sizeof (thread_information_32) / sizeof (uint32_t);
92 static const int MAX_TIB64 =
93 sizeof (thread_information_64) / sizeof (uint64_t);
94 static const int FULL_TIB_SIZE = 0x1000;
96 static int maint_display_all_tib = 0;
98 /* Define Thread Local Base pointer type. */
100 static struct type *
101 windows_get_tlb_type (struct gdbarch *gdbarch)
103 static struct gdbarch *last_gdbarch = NULL;
104 static struct type *last_tlb_type = NULL;
105 struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
106 struct type *peb_ldr_type, *peb_ldr_ptr_type;
107 struct type *peb_type, *peb_ptr_type, *list_type, *list_ptr_type;
108 struct type *module_list_ptr_type;
109 struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
111 /* Do not rebuild type if same gdbarch as last time. */
112 if (last_tlb_type && last_gdbarch == gdbarch)
113 return last_tlb_type;
115 dword_ptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
116 1, "DWORD_PTR");
117 dword32_type = arch_integer_type (gdbarch, 32,
118 1, "DWORD32");
119 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
121 /* list entry */
123 list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
124 TYPE_NAME (list_type) = xstrdup ("list");
126 list_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
127 TYPE_LENGTH (void_ptr_type), NULL);
129 module_list_ptr_type = void_ptr_type;
131 append_composite_type_field (list_type, "forward_list",
132 module_list_ptr_type);
133 append_composite_type_field (list_type, "backward_list",
134 module_list_ptr_type);
136 /* Structured Exception Handler */
138 seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
139 TYPE_NAME (seh_type) = xstrdup ("seh");
141 seh_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
142 TYPE_LENGTH (void_ptr_type), NULL);
143 TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;
145 append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
146 append_composite_type_field (seh_type, "handler",
147 builtin_type (gdbarch)->builtin_func_ptr);
149 /* struct _PEB_LDR_DATA */
150 peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
151 TYPE_NAME (peb_ldr_type) = xstrdup ("peb_ldr_data");
153 append_composite_type_field (peb_ldr_type, "length", dword32_type);
154 append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
155 append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
156 append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
157 append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
158 append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
159 append_composite_type_field (peb_ldr_type, "entry_in_progress",
160 void_ptr_type);
161 peb_ldr_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
162 TYPE_LENGTH (void_ptr_type), NULL);
163 TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;
166 /* struct process environment block */
167 peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
168 TYPE_NAME (peb_type) = xstrdup ("peb");
170 /* First bytes contain several flags. */
171 append_composite_type_field (peb_type, "flags", dword_ptr_type);
172 append_composite_type_field (peb_type, "mutant", void_ptr_type);
173 append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
174 append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
175 append_composite_type_field (peb_type, "process_parameters", void_ptr_type);
176 append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
177 append_composite_type_field (peb_type, "process_heap", void_ptr_type);
178 append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
179 peb_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
180 TYPE_LENGTH (void_ptr_type), NULL);
181 TYPE_TARGET_TYPE (peb_ptr_type) = peb_type;
184 /* struct thread information block */
185 tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
186 TYPE_NAME (tib_type) = xstrdup ("tib");
188 /* uint32_t current_seh; %fs:0x0000 */
189 append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
190 /* uint32_t current_top_of_stack; %fs:0x0004 */
191 append_composite_type_field (tib_type, "current_top_of_stack",
192 void_ptr_type);
193 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
194 append_composite_type_field (tib_type, "current_bottom_of_stack",
195 void_ptr_type);
196 /* uint32_t sub_system_tib; %fs:0x000c */
197 append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);
199 /* uint32_t fiber_data; %fs:0x0010 */
200 append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
201 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
202 append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
203 /* uint32_t linear_address_tib; %fs:0x0018 */
204 append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
205 /* uint32_t environment_pointer; %fs:0x001c */
206 append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
207 /* uint32_t process_id; %fs:0x0020 */
208 append_composite_type_field (tib_type, "process_id", dword_ptr_type);
209 /* uint32_t current_thread_id; %fs:0x0024 */
210 append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
211 /* uint32_t active_rpc_handle; %fs:0x0028 */
212 append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
213 /* uint32_t thread_local_storage; %fs:0x002c */
214 append_composite_type_field (tib_type, "thread_local_storage",
215 void_ptr_type);
216 /* uint32_t process_environment_block; %fs:0x0030 */
217 append_composite_type_field (tib_type, "process_environment_block",
218 peb_ptr_type);
219 /* uint32_t last_error_number; %fs:0x0034 */
220 append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);
222 tib_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
223 TYPE_LENGTH (void_ptr_type), NULL);
224 TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;
226 last_tlb_type = tib_ptr_type;
227 last_gdbarch = gdbarch;
229 return tib_ptr_type;
232 /* The $_tlb convenience variable is a bit special. We don't know
233 for sure the type of the value until we actually have a chance to
234 fetch the data. The type can change depending on gdbarch, so it is
235 also dependent on which thread you have selected. */
237 /* This function implements the lval_computed support for reading a
238 $_tlb value. */
240 static void
241 tlb_value_read (struct value *val)
243 CORE_ADDR tlb;
244 struct type *type = check_typedef (value_type (val));
246 if (!target_get_tib_address (inferior_ptid, &tlb))
247 error (_("Unable to read tlb"));
248 store_typed_address (value_contents_raw (val), type, tlb);
251 /* This function implements the lval_computed support for writing a
252 $_tlb value. */
254 static void
255 tlb_value_write (struct value *v, struct value *fromval)
257 error (_("Impossible to change the Thread Local Base"));
260 static const struct lval_funcs tlb_value_funcs =
262 tlb_value_read,
263 tlb_value_write
267 /* Return a new value with the correct type for the tlb object of
268 the current thread using architecture GDBARCH. Return a void value
269 if there's no object available. */
271 static struct value *
272 tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
274 if (target_has_stack && !ptid_equal (inferior_ptid, null_ptid))
276 struct type *type = windows_get_tlb_type (gdbarch);
277 return allocate_computed_value (type, &tlb_value_funcs, NULL);
280 return allocate_value (builtin_type (gdbarch)->builtin_void);
284 /* Display thread information block of a given thread. */
286 static int
287 display_one_tib (ptid_t ptid)
289 gdb_byte *tib = NULL;
290 gdb_byte *index;
291 CORE_ADDR thread_local_base;
292 ULONGEST i, val, max, max_name, size, tib_size;
293 ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch);
294 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
296 if (sizeof_ptr == 64)
298 size = sizeof (uint64_t);
299 tib_size = sizeof (thread_information_64);
300 max = MAX_TIB64;
302 else
304 size = sizeof (uint32_t);
305 tib_size = sizeof (thread_information_32);
306 max = MAX_TIB32;
309 max_name = max;
311 if (maint_display_all_tib)
313 tib_size = FULL_TIB_SIZE;
314 max = tib_size / size;
317 tib = alloca (tib_size);
319 if (target_get_tib_address (ptid, &thread_local_base) == 0)
321 printf_filtered (_("Unable to get thread local base for %s\n"),
322 target_pid_to_str (ptid));
323 return -1;
326 if (target_read (&current_target, TARGET_OBJECT_MEMORY,
327 NULL, tib, thread_local_base, tib_size) != tib_size)
329 printf_filtered (_("Unable to read thread information "
330 "block for %s at address %s\n"),
331 target_pid_to_str (ptid),
332 paddress (target_gdbarch, thread_local_base));
333 return -1;
336 printf_filtered (_("Thread Information Block %s at %s\n"),
337 target_pid_to_str (ptid),
338 paddress (target_gdbarch, thread_local_base));
340 index = (gdb_byte *) tib;
342 /* All fields have the size of a pointer, this allows to iterate
343 using the same for loop for both layouts. */
344 for (i = 0; i < max; i++)
346 val = extract_unsigned_integer (index, size, byte_order);
347 if (i < max_name)
348 printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
349 else if (val != 0)
350 printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
351 phex (val, size));
352 index += size;
354 return 1;
357 /* Display thread information block of a thread specified by ARGS.
358 If ARGS is empty, display thread information block of current_thread
359 if current_thread is non NULL.
360 Otherwise ARGS is parsed and converted to a integer that should
361 be the windows ThreadID (not the internal GDB thread ID). */
363 static void
364 display_tib (char * args, int from_tty)
366 if (args)
368 struct thread_info *tp;
369 int gdb_id = value_as_long (parse_and_eval (args));
371 tp = find_thread_id (gdb_id);
373 if (!tp)
374 error (_("Thread ID %d not known."), gdb_id);
376 if (!target_thread_alive (tp->ptid))
377 error (_("Thread ID %d has terminated."), gdb_id);
379 display_one_tib (tp->ptid);
381 else if (!ptid_equal (inferior_ptid, null_ptid))
382 display_one_tib (inferior_ptid);
385 void
386 windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
387 struct gdbarch *gdbarch, struct obstack *obstack)
389 char *p;
390 obstack_grow_str (obstack, "<library name=\"");
391 p = xml_escape_text (so_name);
392 obstack_grow_str (obstack, p);
393 xfree (p);
394 obstack_grow_str (obstack, "\"><segment address=\"");
395 /* The symbols in a dll are offset by 0x1000, which is the
396 offset from 0 of the first byte in an image - because of the file
397 header and the section alignment. */
398 obstack_grow_str (obstack, paddress (gdbarch, load_addr + 0x1000));
399 obstack_grow_str (obstack, "\"/></library>");
402 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
403 method. It searches all objfiles, starting with CURRENT_OBJFILE
404 first (if not NULL).
406 On Windows, the system behaves a little differently when two
407 objfiles each define a global symbol using the same name, compared
408 to other platforms such as GNU/Linux for instance. On GNU/Linux,
409 all instances of the symbol effectively get merged into a single
410 one, but on Windows, they remain distinct.
412 As a result, it usually makes sense to start global symbol searches
413 with the current objfile before expanding it to all other objfiles.
414 This helps for instance when a user debugs some code in a DLL that
415 refers to a global variable defined inside that DLL. When trying
416 to print the value of that global variable, it would be unhelpful
417 to print the value of another global variable defined with the same
418 name, but in a different DLL. */
420 void
421 windows_iterate_over_objfiles_in_search_order
422 (struct gdbarch *gdbarch,
423 iterate_over_objfiles_in_search_order_cb_ftype *cb,
424 void *cb_data, struct objfile *current_objfile)
426 int stop;
427 struct objfile *objfile;
429 if (current_objfile)
431 stop = cb (current_objfile, cb_data);
432 if (stop)
433 return;
436 ALL_OBJFILES (objfile)
438 if (objfile != current_objfile)
440 stop = cb (objfile, cb_data);
441 if (stop)
442 return;
447 static void
448 show_maint_show_all_tib (struct ui_file *file, int from_tty,
449 struct cmd_list_element *c, const char *value)
451 fprintf_filtered (file, _("Show all non-zero elements of "
452 "Thread Information Block is %s.\n"), value);
455 static void
456 info_w32_command (char *args, int from_tty)
458 help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
461 static int w32_prefix_command_valid = 0;
462 void
463 init_w32_command_list (void)
465 if (!w32_prefix_command_valid)
467 add_prefix_cmd ("w32", class_info, info_w32_command,
468 _("Print information specific to Win32 debugging."),
469 &info_w32_cmdlist, "info w32 ", 0, &infolist);
470 w32_prefix_command_valid = 1;
474 /* Provide a prototype to silence -Wmissing-prototypes. */
475 extern initialize_file_ftype _initialize_windows_tdep;
477 /* Implementation of `tlb' variable. */
479 static const struct internalvar_funcs tlb_funcs =
481 tlb_make_value,
482 NULL,
483 NULL
486 void
487 _initialize_windows_tdep (void)
489 init_w32_command_list ();
490 add_cmd ("thread-information-block", class_info, display_tib,
491 _("Display thread information block."),
492 &info_w32_cmdlist);
493 add_alias_cmd ("tib", "thread-information-block", class_info, 1,
494 &info_w32_cmdlist);
496 add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
497 &maint_display_all_tib, _("\
498 Set whether to display all non-zero fields of thread information block."), _("\
499 Show whether to display all non-zero fields of thread information block."), _("\
500 Use \"on\" to enable, \"off\" to disable.\n\
501 If enabled, all non-zero fields of thread information block are displayed,\n\
502 even if their meaning is unknown."),
503 NULL,
504 show_maint_show_all_tib,
505 &maintenance_set_cmdlist,
506 &maintenance_show_cmdlist);
508 /* Explicitly create without lookup, since that tries to create a
509 value with a void typed value, and when we get here, gdbarch
510 isn't initialized yet. At this point, we're quite sure there
511 isn't another convenience variable of the same name. */
512 create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);