2010-04-19 Stan Shebs <stan@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / windows-tdep.c
CommitLineData
4c38e0a4 1/* Copyright (C) 2008, 2009, 2010 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"
29
30struct cmd_list_element *info_w32_cmdlist;
31
32typedef struct thread_information_block_32
33 {
34 uint32_t current_seh; /* %fs:0x0000 */
35 uint32_t current_top_of_stack; /* %fs:0x0004 */
36 uint32_t current_bottom_of_stack; /* %fs:0x0008 */
37 uint32_t sub_system_tib; /* %fs:0x000c */
38 uint32_t fiber_data; /* %fs:0x0010 */
39 uint32_t arbitrary_data_slot; /* %fs:0x0014 */
40 uint32_t linear_address_tib; /* %fs:0x0018 */
41 uint32_t environment_pointer; /* %fs:0x001c */
42 uint32_t process_id; /* %fs:0x0020 */
43 uint32_t current_thread_id; /* %fs:0x0024 */
44 uint32_t active_rpc_handle; /* %fs:0x0028 */
45 uint32_t thread_local_storage; /* %fs:0x002c */
46 uint32_t process_environment_block; /* %fs:0x0030 */
47 uint32_t last_error_number; /* %fs:0x0034 */
48 }
49thread_information_32;
50
51typedef struct thread_information_block_64
52 {
53 uint64_t current_seh; /* %gs:0x0000 */
54 uint64_t current_top_of_stack; /* %gs:0x0008 */
55 uint64_t current_bottom_of_stack; /* %gs:0x0010 */
56 uint64_t sub_system_tib; /* %gs:0x0018 */
57 uint64_t fiber_data; /* %gs:0x0020 */
58 uint64_t arbitrary_data_slot; /* %gs:0x0028 */
59 uint64_t linear_address_tib; /* %gs:0x0030 */
60 uint64_t environment_pointer; /* %gs:0x0038 */
61 uint64_t process_id; /* %gs:0x0040 */
62 uint64_t current_thread_id; /* %gs:0x0048 */
63 uint64_t active_rpc_handle; /* %gs:0x0050 */
64 uint64_t thread_local_storage; /* %gs:0x0058 */
65 uint64_t process_environment_block; /* %gs:0x0060 */
66 uint64_t last_error_number; /* %gs:0x0068 */
67 }
68thread_information_64;
69
70
71static const char* TIB_NAME[] =
72 {
73 " current_seh ", /* %fs:0x0000 */
74 " current_top_of_stack ", /* %fs:0x0004 */
75 " current_bottom_of_stack ", /* %fs:0x0008 */
76 " sub_system_tib ", /* %fs:0x000c */
77 " fiber_data ", /* %fs:0x0010 */
78 " arbitrary_data_slot ", /* %fs:0x0014 */
79 " linear_address_tib ", /* %fs:0x0018 */
80 " environment_pointer ", /* %fs:0x001c */
81 " process_id ", /* %fs:0x0020 */
82 " current_thread_id ", /* %fs:0x0024 */
83 " active_rpc_handle ", /* %fs:0x0028 */
84 " thread_local_storage ", /* %fs:0x002c */
85 " process_environment_block ", /* %fs:0x0030 */
86 " last_error_number " /* %fs:0x0034 */
87 };
88
89static const int MAX_TIB32 = sizeof (thread_information_32) / sizeof (uint32_t);
90static const int MAX_TIB64 = sizeof (thread_information_64) / sizeof (uint64_t);
91static const int FULL_TIB_SIZE = 0x1000;
92
93static int maint_display_all_tib = 0;
94
95/* Define Thread Local Base pointer type. */
96
97static struct type *
98windows_get_tlb_type (struct gdbarch *gdbarch)
99{
100 struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
101 struct type *peb_ldr_type, *peb_ldr_ptr_type;
102 struct type *peb_type, *peb_ptr_type, *list_type, *list_ptr_type;
103 struct type *module_list_ptr_type;
104 struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
105
106 dword_ptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
107 1, "DWORD_PTR");
108 dword32_type = arch_integer_type (gdbarch, 32,
109 1, "DWORD32");
110 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
111
112 /* list entry */
113
114 list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
115 TYPE_NAME (list_type) = xstrdup ("list");
116
117 list_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
118 TYPE_LENGTH (void_ptr_type), NULL);
119
120 module_list_ptr_type = void_ptr_type;
121
122 append_composite_type_field (list_type, "forward_list", module_list_ptr_type);
123 append_composite_type_field (list_type, "backward_list",
124 module_list_ptr_type);
125
126 /* Structured Exception Handler */
127
128 seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
129 TYPE_NAME (seh_type) = xstrdup ("seh");
130
131 seh_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
132 TYPE_LENGTH (void_ptr_type), NULL);
133 TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;
134
135 append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
136 append_composite_type_field (seh_type, "handler", void_ptr_type);
137
138 /* struct _PEB_LDR_DATA */
139 peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
140 TYPE_NAME (peb_ldr_type) = xstrdup ("peb_ldr_data");
141
142 append_composite_type_field (peb_ldr_type, "length", dword32_type);
143 append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
144 append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
145 append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
146 append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
147 append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
148 append_composite_type_field (peb_ldr_type, "entry_in_progress",
149 void_ptr_type);
150 peb_ldr_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
151 TYPE_LENGTH (void_ptr_type), NULL);
152 TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;
153
154
155 /* struct process environment block */
156 peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
157 TYPE_NAME (peb_type) = xstrdup ("peb");
158
159 /* First bytes contain several flags. */
160 append_composite_type_field (peb_type, "flags", dword_ptr_type);
161 append_composite_type_field (peb_type, "mutant", void_ptr_type);
162 append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
163 append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
164 append_composite_type_field (peb_type, "process_parameters", void_ptr_type);
165 append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
166 append_composite_type_field (peb_type, "process_heap", void_ptr_type);
167 append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
168 peb_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
169 TYPE_LENGTH (void_ptr_type), NULL);
170 TYPE_TARGET_TYPE (peb_ptr_type) = peb_type;
171
172
173 /* struct thread information block */
174 tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
175 TYPE_NAME (tib_type) = xstrdup ("tib");
176
177 /* uint32_t current_seh; %fs:0x0000 */
178 append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
179 /* uint32_t current_top_of_stack; %fs:0x0004 */
180 append_composite_type_field (tib_type, "current_top_of_stack", void_ptr_type);
181 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
182 append_composite_type_field (tib_type, "current_bottom_of_stack",
183 void_ptr_type);
184 /* uint32_t sub_system_tib; %fs:0x000c */
185 append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);
186
187 /* uint32_t fiber_data; %fs:0x0010 */
188 append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
189 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
190 append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
191 /* uint32_t linear_address_tib; %fs:0x0018 */
192 append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
193 /* uint32_t environment_pointer; %fs:0x001c */
194 append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
195 /* uint32_t process_id; %fs:0x0020 */
196 append_composite_type_field (tib_type, "process_id", dword_ptr_type);
197 /* uint32_t current_thread_id; %fs:0x0024 */
198 append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
199 /* uint32_t active_rpc_handle; %fs:0x0028 */
200 append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
201 /* uint32_t thread_local_storage; %fs:0x002c */
202 append_composite_type_field (tib_type, "thread_local_storage", void_ptr_type);
203 /* uint32_t process_environment_block; %fs:0x0030 */
204 append_composite_type_field (tib_type, "process_environment_block",
205 peb_ptr_type);
206 /* uint32_t last_error_number; %fs:0x0034 */
207 append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);
208
209 tib_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
210 TYPE_LENGTH (void_ptr_type), NULL);
211 TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;
212
213 return tib_ptr_type;
214}
215
216/* The $_tlb convenience variable is a bit special. We don't know
217 for sure the type of the value until we actually have a chance to
218 fetch the data. The type can change depending on gdbarch, so it is
219 also dependent on which thread you have selected. */
220
221/* This function implements the lval_computed support for reading a
222 $_tlb value. */
223
224static void
225tlb_value_read (struct value *val)
226{
227 CORE_ADDR tlb;
228 struct type *type = check_typedef (value_type (val));
229
230 if (!target_get_tib_address (inferior_ptid, &tlb))
231 error (_("Unable to read tlb"));
232 store_typed_address (value_contents_raw (val), type, tlb);
233}
234
235/* This function implements the lval_computed support for writing a
236 $_tlb value. */
237
238static void
239tlb_value_write (struct value *v, struct value *fromval)
240{
241 error (_("Impossible to change the Thread Local Base"));
242}
243
244static struct lval_funcs tlb_value_funcs =
245 {
246 tlb_value_read,
247 tlb_value_write
248 };
249
250
251/* Return a new value with the correct type for the tlb object of
252 the current thread using architecture GDBARCH. Return a void value
253 if there's no object available. */
254
255static struct value *
256tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var)
257{
258 if (target_has_stack && !ptid_equal (inferior_ptid, null_ptid))
259 {
260 struct type *type = windows_get_tlb_type (gdbarch);
261 return allocate_computed_value (type, &tlb_value_funcs, NULL);
262 }
263
264 return allocate_value (builtin_type (gdbarch)->builtin_void);
265}
266
267
268/* Display thread information block of a given thread. */
269
270static int
271display_one_tib (ptid_t ptid)
272{
273 gdb_byte *tib = NULL;
274 gdb_byte *index;
275 CORE_ADDR thread_local_base;
276 ULONGEST i, val, max, max_name, size, tib_size;
277 ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch);
278 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
279
280 if (sizeof_ptr == 64)
281 {
282 size = sizeof (uint64_t);
283 tib_size = sizeof (thread_information_64);
284 max = MAX_TIB64;
285 }
286 else
287 {
288 size = sizeof (uint32_t);
289 tib_size = sizeof (thread_information_32);
290 max = MAX_TIB32;
291 }
292
293 max_name = max;
294
295 if (maint_display_all_tib)
296 {
297 tib_size = FULL_TIB_SIZE;
298 max = tib_size / size;
299 }
300
301 tib = alloca (tib_size);
302
303 if (target_get_tib_address (ptid, &thread_local_base) == 0)
304 {
305 printf_filtered (_("Unable to get thread local base for %s\n"),
306 target_pid_to_str (ptid));
307 return -1;
308 }
309
310 if (target_read (&current_target, TARGET_OBJECT_MEMORY,
311 NULL, tib, thread_local_base, tib_size) != tib_size)
312 {
313 printf_filtered (_("Unable to read thread information block for %s at \
314address %s\n"),
315 target_pid_to_str (ptid),
316 paddress (target_gdbarch, thread_local_base));
317 return -1;
318 }
319
320 printf_filtered (_("Thread Information Block %s at %s\n"),
321 target_pid_to_str (ptid),
322 paddress (target_gdbarch, thread_local_base));
323
324 index = (gdb_byte *) tib;
325
326 /* All fields have the size of a pointer, this allows to iterate
327 using the same for loop for both layouts. */
328 for (i = 0; i < max; i++)
329 {
330 val = extract_unsigned_integer (index, size, byte_order);
331 if (i < max_name)
332 printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
333 else if (val != 0)
334 printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
335 phex (val, size));
336 index += size;
337 }
338 return 1;
339}
340
341/* Display thread information block of a thread specified by ARGS.
342 If ARGS is empty, display thread information block of current_thread
343 if current_thread is non NULL.
344 Otherwise ARGS is parsed and converted to a integer that should
345 be the windows ThreadID (not the internal GDB thread ID). */
346
347static void
348display_tib (char * args, int from_tty)
349{
350 if (args)
351 {
352 struct thread_info *tp;
353 int gdb_id = value_as_long (parse_and_eval (args));
354
355 tp = find_thread_id (gdb_id);
356
357 if (!tp)
358 error (_("Thread ID %d not known."), gdb_id);
359
360 if (!target_thread_alive (tp->ptid))
361 error (_("Thread ID %d has terminated."), gdb_id);
362
363 display_one_tib (tp->ptid);
364 }
365 else if (!ptid_equal (inferior_ptid, null_ptid))
366 display_one_tib (inferior_ptid);
367}
bfb87e33
JB
368
369void
dc05df57 370windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
5af949e3 371 struct gdbarch *gdbarch, struct obstack *obstack)
bfb87e33
JB
372{
373 char *p;
374 obstack_grow_str (obstack, "<library name=\"");
375 p = xml_escape_text (so_name);
376 obstack_grow_str (obstack, p);
377 xfree (p);
5af949e3 378 obstack_grow_str (obstack, "\"><segment address=\"");
bfb87e33
JB
379 /* The symbols in a dll are offset by 0x1000, which is the the
380 offset from 0 of the first byte in an image - because of the file
381 header and the section alignment. */
5af949e3 382 obstack_grow_str (obstack, paddress (gdbarch, load_addr + 0x1000));
bfb87e33
JB
383 obstack_grow_str (obstack, "\"/></library>");
384}
711e434b
PM
385
386static void
387show_maint_show_all_tib (struct ui_file *file, int from_tty,
388 struct cmd_list_element *c, const char *value)
389{
390 fprintf_filtered (file, _("Show all non-zero elements of Thread Information \
391Block is %s.\n"), value);
392}
393
394static void
395info_w32_command (char *args, int from_tty)
396{
397 help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
398}
399
400static int w32_prefix_command_valid = 0;
401void
402init_w32_command_list (void)
403{
404 if (!w32_prefix_command_valid)
405 {
406 add_prefix_cmd ("w32", class_info, info_w32_command,
407 _("Print information specific to Win32 debugging."),
408 &info_w32_cmdlist, "info w32 ", 0, &infolist);
409 w32_prefix_command_valid = 1;
410 }
411}
412
413void
414_initialize_windows_tdep (void)
415{
416 init_w32_command_list ();
417 add_cmd ("thread-information-block", class_info, display_tib,
418 _("Display thread information block."),
419 &info_w32_cmdlist);
420 add_alias_cmd ("tib", "thread-information-block", class_info, 1,
421 &info_w32_cmdlist);
422
423 add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
424 &maint_display_all_tib, _("\
425Set whether to display all non-zero fields of thread information block."), _("\
426Show whether to display all non-zero fields of thread information block."), _("\
427Use \"on\" to enable, \"off\" to disable.\n\
428If enabled, all non-zero fields of thread information block are displayed,\n\
429even if their meaning is unknown."),
430 NULL,
431 show_maint_show_all_tib,
432 &maintenance_set_cmdlist,
433 &maintenance_show_cmdlist);
434
435 /* Explicitly create without lookup, since that tries to create a
436 value with a void typed value, and when we get here, gdbarch
437 isn't initialized yet. At this point, we're quite sure there
438 isn't another convenience variable of the same name. */
439 create_internalvar_type_lazy ("_tlb", tlb_make_value);
440}
This page took 0.195522 seconds and 4 git commands to generate.