* lib/gdb.exp (standard_output_file): Use "file join".
[deliverable/binutils-gdb.git] / gdb / windows-tdep.c
CommitLineData
c5a57081 1/* Copyright (C) 2008-2012 Free Software Foundation, Inc.
bfb87e33
JB
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"
31b060a2 19#include "windows-tdep.h"
bfb87e33
JB
20#include "gdb_obstack.h"
21#include "xml-support.h"
711e434b
PM
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"
a8e1bb34 29#include "objfiles.h"
711e434b
PM
30
31struct cmd_list_element *info_w32_cmdlist;
32
33typedef struct thread_information_block_32
34 {
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 */
49 }
50thread_information_32;
51
52typedef struct thread_information_block_64
53 {
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 */
68 }
69thread_information_64;
70
71
72static const char* TIB_NAME[] =
73 {
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 */
88 };
89
581e13c1
MS
90static const int MAX_TIB32 =
91 sizeof (thread_information_32) / sizeof (uint32_t);
92static const int MAX_TIB64 =
93 sizeof (thread_information_64) / sizeof (uint64_t);
711e434b
PM
94static const int FULL_TIB_SIZE = 0x1000;
95
96static int maint_display_all_tib = 0;
97
98/* Define Thread Local Base pointer type. */
99
100static struct type *
101windows_get_tlb_type (struct gdbarch *gdbarch)
102{
ea1fae46
PM
103 static struct gdbarch *last_gdbarch = NULL;
104 static struct type *last_tlb_type = NULL;
711e434b
PM
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;
110
ea1fae46
PM
111 /* Do not rebuild type if same gdbarch as last time. */
112 if (last_tlb_type && last_gdbarch == gdbarch)
113 return last_tlb_type;
114
711e434b
PM
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);
120
121 /* list entry */
122
123 list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
124 TYPE_NAME (list_type) = xstrdup ("list");
125
126 list_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
127 TYPE_LENGTH (void_ptr_type), NULL);
128
129 module_list_ptr_type = void_ptr_type;
130
581e13c1
MS
131 append_composite_type_field (list_type, "forward_list",
132 module_list_ptr_type);
711e434b
PM
133 append_composite_type_field (list_type, "backward_list",
134 module_list_ptr_type);
135
136 /* Structured Exception Handler */
137
138 seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
139 TYPE_NAME (seh_type) = xstrdup ("seh");
140
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;
144
145 append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
e8e6c82e
PM
146 append_composite_type_field (seh_type, "handler",
147 builtin_type (gdbarch)->builtin_func_ptr);
711e434b
PM
148
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");
152
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;
164
165
166 /* struct process environment block */
167 peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
168 TYPE_NAME (peb_type) = xstrdup ("peb");
169
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;
182
183
184 /* struct thread information block */
185 tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
186 TYPE_NAME (tib_type) = xstrdup ("tib");
187
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 */
581e13c1
MS
191 append_composite_type_field (tib_type, "current_top_of_stack",
192 void_ptr_type);
711e434b
PM
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);
198
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 */
581e13c1
MS
214 append_composite_type_field (tib_type, "thread_local_storage",
215 void_ptr_type);
711e434b
PM
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);
221
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;
225
ea1fae46
PM
226 last_tlb_type = tib_ptr_type;
227 last_gdbarch = gdbarch;
228
711e434b
PM
229 return tib_ptr_type;
230}
231
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. */
236
237/* This function implements the lval_computed support for reading a
238 $_tlb value. */
239
240static void
241tlb_value_read (struct value *val)
242{
243 CORE_ADDR tlb;
244 struct type *type = check_typedef (value_type (val));
245
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);
249}
250
251/* This function implements the lval_computed support for writing a
252 $_tlb value. */
253
254static void
255tlb_value_write (struct value *v, struct value *fromval)
256{
257 error (_("Impossible to change the Thread Local Base"));
258}
259
c8f2448a 260static const struct lval_funcs tlb_value_funcs =
711e434b
PM
261 {
262 tlb_value_read,
263 tlb_value_write
264 };
265
266
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. */
270
271static struct value *
22d2b532 272tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
711e434b
PM
273{
274 if (target_has_stack && !ptid_equal (inferior_ptid, null_ptid))
275 {
276 struct type *type = windows_get_tlb_type (gdbarch);
277 return allocate_computed_value (type, &tlb_value_funcs, NULL);
278 }
279
280 return allocate_value (builtin_type (gdbarch)->builtin_void);
281}
282
283
284/* Display thread information block of a given thread. */
285
286static int
287display_one_tib (ptid_t ptid)
288{
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);
295
296 if (sizeof_ptr == 64)
297 {
298 size = sizeof (uint64_t);
299 tib_size = sizeof (thread_information_64);
300 max = MAX_TIB64;
301 }
302 else
303 {
304 size = sizeof (uint32_t);
305 tib_size = sizeof (thread_information_32);
306 max = MAX_TIB32;
307 }
308
309 max_name = max;
310
311 if (maint_display_all_tib)
312 {
313 tib_size = FULL_TIB_SIZE;
314 max = tib_size / size;
315 }
316
317 tib = alloca (tib_size);
318
319 if (target_get_tib_address (ptid, &thread_local_base) == 0)
320 {
321 printf_filtered (_("Unable to get thread local base for %s\n"),
322 target_pid_to_str (ptid));
323 return -1;
324 }
325
326 if (target_read (&current_target, TARGET_OBJECT_MEMORY,
327 NULL, tib, thread_local_base, tib_size) != tib_size)
328 {
581e13c1
MS
329 printf_filtered (_("Unable to read thread information "
330 "block for %s at address %s\n"),
711e434b
PM
331 target_pid_to_str (ptid),
332 paddress (target_gdbarch, thread_local_base));
333 return -1;
334 }
335
336 printf_filtered (_("Thread Information Block %s at %s\n"),
337 target_pid_to_str (ptid),
338 paddress (target_gdbarch, thread_local_base));
339
340 index = (gdb_byte *) tib;
341
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++)
345 {
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;
353 }
354 return 1;
355}
356
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). */
362
363static void
364display_tib (char * args, int from_tty)
365{
366 if (args)
367 {
368 struct thread_info *tp;
369 int gdb_id = value_as_long (parse_and_eval (args));
370
371 tp = find_thread_id (gdb_id);
372
373 if (!tp)
374 error (_("Thread ID %d not known."), gdb_id);
375
376 if (!target_thread_alive (tp->ptid))
377 error (_("Thread ID %d has terminated."), gdb_id);
378
379 display_one_tib (tp->ptid);
380 }
381 else if (!ptid_equal (inferior_ptid, null_ptid))
382 display_one_tib (inferior_ptid);
383}
bfb87e33
JB
384
385void
dc05df57 386windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
5af949e3 387 struct gdbarch *gdbarch, struct obstack *obstack)
bfb87e33
JB
388{
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);
5af949e3 394 obstack_grow_str (obstack, "\"><segment address=\"");
7a9dd1b2 395 /* The symbols in a dll are offset by 0x1000, which is the
bfb87e33 396 offset from 0 of the first byte in an image - because of the file
581e13c1 397 header and the section alignment. */
5af949e3 398 obstack_grow_str (obstack, paddress (gdbarch, load_addr + 0x1000));
bfb87e33
JB
399 obstack_grow_str (obstack, "\"/></library>");
400}
711e434b 401
a8e1bb34
JB
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).
405
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.
411
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. */
419
420void
421windows_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)
425{
426 int stop;
427 struct objfile *objfile;
428
429 if (current_objfile)
430 {
431 stop = cb (current_objfile, cb_data);
432 if (stop)
433 return;
434 }
435
436 ALL_OBJFILES (objfile)
437 {
438 if (objfile != current_objfile)
439 {
440 stop = cb (objfile, cb_data);
441 if (stop)
442 return;
443 }
444 }
445}
446
711e434b
PM
447static void
448show_maint_show_all_tib (struct ui_file *file, int from_tty,
449 struct cmd_list_element *c, const char *value)
450{
581e13c1
MS
451 fprintf_filtered (file, _("Show all non-zero elements of "
452 "Thread Information Block is %s.\n"), value);
711e434b
PM
453}
454
455static void
456info_w32_command (char *args, int from_tty)
457{
458 help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
459}
460
461static int w32_prefix_command_valid = 0;
462void
463init_w32_command_list (void)
464{
465 if (!w32_prefix_command_valid)
466 {
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;
471 }
472}
473
70221824
PA
474/* Provide a prototype to silence -Wmissing-prototypes. */
475extern initialize_file_ftype _initialize_windows_tdep;
476
22d2b532
SDJ
477/* Implementation of `tlb' variable. */
478
479static const struct internalvar_funcs tlb_funcs =
480{
481 tlb_make_value,
482 NULL,
483 NULL
484};
485
711e434b
PM
486void
487_initialize_windows_tdep (void)
488{
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);
495
496 add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
497 &maint_display_all_tib, _("\
498Set whether to display all non-zero fields of thread information block."), _("\
499Show whether to display all non-zero fields of thread information block."), _("\
500Use \"on\" to enable, \"off\" to disable.\n\
501If enabled, all non-zero fields of thread information block are displayed,\n\
502even if their meaning is unknown."),
503 NULL,
504 show_maint_show_all_tib,
505 &maintenance_set_cmdlist,
506 &maintenance_show_cmdlist);
507
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. */
22d2b532 512 create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);
711e434b 513}
This page took 0.448051 seconds and 4 git commands to generate.