Fix gdb.trace/entry-values.exp for thumb mode
[deliverable/binutils-gdb.git] / gdb / windows-tdep.c
1 /* Copyright (C) 2008-2014 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
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.
9
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.
14
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/>. */
17
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"
30 #include "symfile.h"
31 #include "coff-pe-read.h"
32 #include "gdb_bfd.h"
33 #include "complaints.h"
34 #include "solib.h"
35 #include "solib-target.h"
36
37 struct cmd_list_element *info_w32_cmdlist;
38
39 typedef struct thread_information_block_32
40 {
41 uint32_t current_seh; /* %fs:0x0000 */
42 uint32_t current_top_of_stack; /* %fs:0x0004 */
43 uint32_t current_bottom_of_stack; /* %fs:0x0008 */
44 uint32_t sub_system_tib; /* %fs:0x000c */
45 uint32_t fiber_data; /* %fs:0x0010 */
46 uint32_t arbitrary_data_slot; /* %fs:0x0014 */
47 uint32_t linear_address_tib; /* %fs:0x0018 */
48 uint32_t environment_pointer; /* %fs:0x001c */
49 uint32_t process_id; /* %fs:0x0020 */
50 uint32_t current_thread_id; /* %fs:0x0024 */
51 uint32_t active_rpc_handle; /* %fs:0x0028 */
52 uint32_t thread_local_storage; /* %fs:0x002c */
53 uint32_t process_environment_block; /* %fs:0x0030 */
54 uint32_t last_error_number; /* %fs:0x0034 */
55 }
56 thread_information_32;
57
58 typedef struct thread_information_block_64
59 {
60 uint64_t current_seh; /* %gs:0x0000 */
61 uint64_t current_top_of_stack; /* %gs:0x0008 */
62 uint64_t current_bottom_of_stack; /* %gs:0x0010 */
63 uint64_t sub_system_tib; /* %gs:0x0018 */
64 uint64_t fiber_data; /* %gs:0x0020 */
65 uint64_t arbitrary_data_slot; /* %gs:0x0028 */
66 uint64_t linear_address_tib; /* %gs:0x0030 */
67 uint64_t environment_pointer; /* %gs:0x0038 */
68 uint64_t process_id; /* %gs:0x0040 */
69 uint64_t current_thread_id; /* %gs:0x0048 */
70 uint64_t active_rpc_handle; /* %gs:0x0050 */
71 uint64_t thread_local_storage; /* %gs:0x0058 */
72 uint64_t process_environment_block; /* %gs:0x0060 */
73 uint64_t last_error_number; /* %gs:0x0068 */
74 }
75 thread_information_64;
76
77
78 static const char* TIB_NAME[] =
79 {
80 " current_seh ", /* %fs:0x0000 */
81 " current_top_of_stack ", /* %fs:0x0004 */
82 " current_bottom_of_stack ", /* %fs:0x0008 */
83 " sub_system_tib ", /* %fs:0x000c */
84 " fiber_data ", /* %fs:0x0010 */
85 " arbitrary_data_slot ", /* %fs:0x0014 */
86 " linear_address_tib ", /* %fs:0x0018 */
87 " environment_pointer ", /* %fs:0x001c */
88 " process_id ", /* %fs:0x0020 */
89 " current_thread_id ", /* %fs:0x0024 */
90 " active_rpc_handle ", /* %fs:0x0028 */
91 " thread_local_storage ", /* %fs:0x002c */
92 " process_environment_block ", /* %fs:0x0030 */
93 " last_error_number " /* %fs:0x0034 */
94 };
95
96 static const int MAX_TIB32 =
97 sizeof (thread_information_32) / sizeof (uint32_t);
98 static const int MAX_TIB64 =
99 sizeof (thread_information_64) / sizeof (uint64_t);
100 static const int FULL_TIB_SIZE = 0x1000;
101
102 static int maint_display_all_tib = 0;
103
104 /* Define Thread Local Base pointer type. */
105
106 static struct type *
107 windows_get_tlb_type (struct gdbarch *gdbarch)
108 {
109 static struct gdbarch *last_gdbarch = NULL;
110 static struct type *last_tlb_type = NULL;
111 struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
112 struct type *peb_ldr_type, *peb_ldr_ptr_type;
113 struct type *peb_type, *peb_ptr_type, *list_type, *list_ptr_type;
114 struct type *module_list_ptr_type;
115 struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
116
117 /* Do not rebuild type if same gdbarch as last time. */
118 if (last_tlb_type && last_gdbarch == gdbarch)
119 return last_tlb_type;
120
121 dword_ptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
122 1, "DWORD_PTR");
123 dword32_type = arch_integer_type (gdbarch, 32,
124 1, "DWORD32");
125 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
126
127 /* list entry */
128
129 list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
130 TYPE_NAME (list_type) = xstrdup ("list");
131
132 list_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
133 TYPE_LENGTH (void_ptr_type), NULL);
134
135 module_list_ptr_type = void_ptr_type;
136
137 append_composite_type_field (list_type, "forward_list",
138 module_list_ptr_type);
139 append_composite_type_field (list_type, "backward_list",
140 module_list_ptr_type);
141
142 /* Structured Exception Handler */
143
144 seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
145 TYPE_NAME (seh_type) = xstrdup ("seh");
146
147 seh_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
148 TYPE_LENGTH (void_ptr_type), NULL);
149 TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;
150
151 append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
152 append_composite_type_field (seh_type, "handler",
153 builtin_type (gdbarch)->builtin_func_ptr);
154
155 /* struct _PEB_LDR_DATA */
156 peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
157 TYPE_NAME (peb_ldr_type) = xstrdup ("peb_ldr_data");
158
159 append_composite_type_field (peb_ldr_type, "length", dword32_type);
160 append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
161 append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
162 append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
163 append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
164 append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
165 append_composite_type_field (peb_ldr_type, "entry_in_progress",
166 void_ptr_type);
167 peb_ldr_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
168 TYPE_LENGTH (void_ptr_type), NULL);
169 TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;
170
171
172 /* struct process environment block */
173 peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
174 TYPE_NAME (peb_type) = xstrdup ("peb");
175
176 /* First bytes contain several flags. */
177 append_composite_type_field (peb_type, "flags", dword_ptr_type);
178 append_composite_type_field (peb_type, "mutant", void_ptr_type);
179 append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
180 append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
181 append_composite_type_field (peb_type, "process_parameters", void_ptr_type);
182 append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
183 append_composite_type_field (peb_type, "process_heap", void_ptr_type);
184 append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
185 peb_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
186 TYPE_LENGTH (void_ptr_type), NULL);
187 TYPE_TARGET_TYPE (peb_ptr_type) = peb_type;
188
189
190 /* struct thread information block */
191 tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
192 TYPE_NAME (tib_type) = xstrdup ("tib");
193
194 /* uint32_t current_seh; %fs:0x0000 */
195 append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
196 /* uint32_t current_top_of_stack; %fs:0x0004 */
197 append_composite_type_field (tib_type, "current_top_of_stack",
198 void_ptr_type);
199 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
200 append_composite_type_field (tib_type, "current_bottom_of_stack",
201 void_ptr_type);
202 /* uint32_t sub_system_tib; %fs:0x000c */
203 append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);
204
205 /* uint32_t fiber_data; %fs:0x0010 */
206 append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
207 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
208 append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
209 /* uint32_t linear_address_tib; %fs:0x0018 */
210 append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
211 /* uint32_t environment_pointer; %fs:0x001c */
212 append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
213 /* uint32_t process_id; %fs:0x0020 */
214 append_composite_type_field (tib_type, "process_id", dword_ptr_type);
215 /* uint32_t current_thread_id; %fs:0x0024 */
216 append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
217 /* uint32_t active_rpc_handle; %fs:0x0028 */
218 append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
219 /* uint32_t thread_local_storage; %fs:0x002c */
220 append_composite_type_field (tib_type, "thread_local_storage",
221 void_ptr_type);
222 /* uint32_t process_environment_block; %fs:0x0030 */
223 append_composite_type_field (tib_type, "process_environment_block",
224 peb_ptr_type);
225 /* uint32_t last_error_number; %fs:0x0034 */
226 append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);
227
228 tib_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
229 TYPE_LENGTH (void_ptr_type), NULL);
230 TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;
231
232 last_tlb_type = tib_ptr_type;
233 last_gdbarch = gdbarch;
234
235 return tib_ptr_type;
236 }
237
238 /* The $_tlb convenience variable is a bit special. We don't know
239 for sure the type of the value until we actually have a chance to
240 fetch the data. The type can change depending on gdbarch, so it is
241 also dependent on which thread you have selected. */
242
243 /* This function implements the lval_computed support for reading a
244 $_tlb value. */
245
246 static void
247 tlb_value_read (struct value *val)
248 {
249 CORE_ADDR tlb;
250 struct type *type = check_typedef (value_type (val));
251
252 if (!target_get_tib_address (inferior_ptid, &tlb))
253 error (_("Unable to read tlb"));
254 store_typed_address (value_contents_raw (val), type, tlb);
255 }
256
257 /* This function implements the lval_computed support for writing a
258 $_tlb value. */
259
260 static void
261 tlb_value_write (struct value *v, struct value *fromval)
262 {
263 error (_("Impossible to change the Thread Local Base"));
264 }
265
266 static const struct lval_funcs tlb_value_funcs =
267 {
268 tlb_value_read,
269 tlb_value_write
270 };
271
272
273 /* Return a new value with the correct type for the tlb object of
274 the current thread using architecture GDBARCH. Return a void value
275 if there's no object available. */
276
277 static struct value *
278 tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
279 {
280 if (target_has_stack && !ptid_equal (inferior_ptid, null_ptid))
281 {
282 struct type *type = windows_get_tlb_type (gdbarch);
283 return allocate_computed_value (type, &tlb_value_funcs, NULL);
284 }
285
286 return allocate_value (builtin_type (gdbarch)->builtin_void);
287 }
288
289
290 /* Display thread information block of a given thread. */
291
292 static int
293 display_one_tib (ptid_t ptid)
294 {
295 gdb_byte *tib = NULL;
296 gdb_byte *index;
297 CORE_ADDR thread_local_base;
298 ULONGEST i, val, max, max_name, size, tib_size;
299 ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch ());
300 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
301
302 if (sizeof_ptr == 64)
303 {
304 size = sizeof (uint64_t);
305 tib_size = sizeof (thread_information_64);
306 max = MAX_TIB64;
307 }
308 else
309 {
310 size = sizeof (uint32_t);
311 tib_size = sizeof (thread_information_32);
312 max = MAX_TIB32;
313 }
314
315 max_name = max;
316
317 if (maint_display_all_tib)
318 {
319 tib_size = FULL_TIB_SIZE;
320 max = tib_size / size;
321 }
322
323 tib = alloca (tib_size);
324
325 if (target_get_tib_address (ptid, &thread_local_base) == 0)
326 {
327 printf_filtered (_("Unable to get thread local base for %s\n"),
328 target_pid_to_str (ptid));
329 return -1;
330 }
331
332 if (target_read (&current_target, TARGET_OBJECT_MEMORY,
333 NULL, tib, thread_local_base, tib_size) != tib_size)
334 {
335 printf_filtered (_("Unable to read thread information "
336 "block for %s at address %s\n"),
337 target_pid_to_str (ptid),
338 paddress (target_gdbarch (), thread_local_base));
339 return -1;
340 }
341
342 printf_filtered (_("Thread Information Block %s at %s\n"),
343 target_pid_to_str (ptid),
344 paddress (target_gdbarch (), thread_local_base));
345
346 index = (gdb_byte *) tib;
347
348 /* All fields have the size of a pointer, this allows to iterate
349 using the same for loop for both layouts. */
350 for (i = 0; i < max; i++)
351 {
352 val = extract_unsigned_integer (index, size, byte_order);
353 if (i < max_name)
354 printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
355 else if (val != 0)
356 printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
357 phex (val, size));
358 index += size;
359 }
360 return 1;
361 }
362
363 /* Display thread information block of a thread specified by ARGS.
364 If ARGS is empty, display thread information block of current_thread
365 if current_thread is non NULL.
366 Otherwise ARGS is parsed and converted to a integer that should
367 be the windows ThreadID (not the internal GDB thread ID). */
368
369 static void
370 display_tib (char * args, int from_tty)
371 {
372 if (args)
373 {
374 struct thread_info *tp;
375 int gdb_id = value_as_long (parse_and_eval (args));
376
377 tp = find_thread_id (gdb_id);
378
379 if (!tp)
380 error (_("Thread ID %d not known."), gdb_id);
381
382 if (!target_thread_alive (tp->ptid))
383 error (_("Thread ID %d has terminated."), gdb_id);
384
385 display_one_tib (tp->ptid);
386 }
387 else if (!ptid_equal (inferior_ptid, null_ptid))
388 display_one_tib (inferior_ptid);
389 }
390
391 void
392 windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
393 struct gdbarch *gdbarch, struct obstack *obstack)
394 {
395 char *p;
396 struct bfd * dll;
397 CORE_ADDR text_offset;
398
399 obstack_grow_str (obstack, "<library name=\"");
400 p = xml_escape_text (so_name);
401 obstack_grow_str (obstack, p);
402 xfree (p);
403 obstack_grow_str (obstack, "\"><segment address=\"");
404 dll = gdb_bfd_open_maybe_remote (so_name);
405 /* The following calls are OK even if dll is NULL.
406 The default value 0x1000 is returned by pe_text_section_offset
407 in that case. */
408 text_offset = pe_text_section_offset (dll);
409 gdb_bfd_unref (dll);
410 obstack_grow_str (obstack, paddress (gdbarch, load_addr + text_offset));
411 obstack_grow_str (obstack, "\"/></library>");
412 }
413
414 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
415 method. It searches all objfiles, starting with CURRENT_OBJFILE
416 first (if not NULL).
417
418 On Windows, the system behaves a little differently when two
419 objfiles each define a global symbol using the same name, compared
420 to other platforms such as GNU/Linux for instance. On GNU/Linux,
421 all instances of the symbol effectively get merged into a single
422 one, but on Windows, they remain distinct.
423
424 As a result, it usually makes sense to start global symbol searches
425 with the current objfile before expanding it to all other objfiles.
426 This helps for instance when a user debugs some code in a DLL that
427 refers to a global variable defined inside that DLL. When trying
428 to print the value of that global variable, it would be unhelpful
429 to print the value of another global variable defined with the same
430 name, but in a different DLL. */
431
432 static void
433 windows_iterate_over_objfiles_in_search_order
434 (struct gdbarch *gdbarch,
435 iterate_over_objfiles_in_search_order_cb_ftype *cb,
436 void *cb_data, struct objfile *current_objfile)
437 {
438 int stop;
439 struct objfile *objfile;
440
441 if (current_objfile)
442 {
443 stop = cb (current_objfile, cb_data);
444 if (stop)
445 return;
446 }
447
448 ALL_OBJFILES (objfile)
449 {
450 if (objfile != current_objfile)
451 {
452 stop = cb (objfile, cb_data);
453 if (stop)
454 return;
455 }
456 }
457 }
458
459 static void
460 show_maint_show_all_tib (struct ui_file *file, int from_tty,
461 struct cmd_list_element *c, const char *value)
462 {
463 fprintf_filtered (file, _("Show all non-zero elements of "
464 "Thread Information Block is %s.\n"), value);
465 }
466
467 static void
468 info_w32_command (char *args, int from_tty)
469 {
470 help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
471 }
472
473 static int w32_prefix_command_valid = 0;
474 void
475 init_w32_command_list (void)
476 {
477 if (!w32_prefix_command_valid)
478 {
479 add_prefix_cmd ("w32", class_info, info_w32_command,
480 _("Print information specific to Win32 debugging."),
481 &info_w32_cmdlist, "info w32 ", 0, &infolist);
482 w32_prefix_command_valid = 1;
483 }
484 }
485
486 /* To be called from the various GDB_OSABI_CYGWIN handlers for the
487 various Windows architectures and machine types. */
488
489 void
490 windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
491 {
492 /* Canonical paths on this target look like
493 `c:\Program Files\Foo App\mydll.dll', for example. */
494 set_gdbarch_has_dos_based_file_system (gdbarch, 1);
495
496 set_gdbarch_iterate_over_objfiles_in_search_order
497 (gdbarch, windows_iterate_over_objfiles_in_search_order);
498
499 set_solib_ops (gdbarch, &solib_target_so_ops);
500 }
501
502 /* Provide a prototype to silence -Wmissing-prototypes. */
503 extern initialize_file_ftype _initialize_windows_tdep;
504
505 /* Implementation of `tlb' variable. */
506
507 static const struct internalvar_funcs tlb_funcs =
508 {
509 tlb_make_value,
510 NULL,
511 NULL
512 };
513
514 void
515 _initialize_windows_tdep (void)
516 {
517 init_w32_command_list ();
518 add_cmd ("thread-information-block", class_info, display_tib,
519 _("Display thread information block."),
520 &info_w32_cmdlist);
521 add_alias_cmd ("tib", "thread-information-block", class_info, 1,
522 &info_w32_cmdlist);
523
524 add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
525 &maint_display_all_tib, _("\
526 Set whether to display all non-zero fields of thread information block."), _("\
527 Show whether to display all non-zero fields of thread information block."), _("\
528 Use \"on\" to enable, \"off\" to disable.\n\
529 If enabled, all non-zero fields of thread information block are displayed,\n\
530 even if their meaning is unknown."),
531 NULL,
532 show_maint_show_all_tib,
533 &maintenance_set_cmdlist,
534 &maintenance_show_cmdlist);
535
536 /* Explicitly create without lookup, since that tries to create a
537 value with a void typed value, and when we get here, gdbarch
538 isn't initialized yet. At this point, we're quite sure there
539 isn't another convenience variable of the same name. */
540 create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);
541 }
This page took 0.041831 seconds and 4 git commands to generate.