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