1 /* Target-vector operations for controlling win32 child processes, for GDB.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions, A Red Hat Company.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without eve nthe implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
25 /* Originally by Steve Chamberlain, sac@cygnus.com */
27 /* We assume we're being built with and will be used for cygwin. */
30 #include "frame.h" /* required by inferior.h */
33 #include "exceptions.h"
36 #include "completer.h"
40 #include <sys/types.h>
45 #include <sys/cygwin.h>
51 #include "gdb_string.h"
52 #include "gdbthread.h"
54 #include <sys/param.h>
60 #include "i386-tdep.h"
61 #include "i387-tdep.h"
63 static struct target_ops win32_ops
;
64 static struct target_so_ops win32_so_ops
;
66 /* The starting and ending address of the cygwin1.dll text segment. */
67 static bfd_vma cygwin_load_start
;
68 static bfd_vma cygwin_load_end
;
70 static int have_saved_context
; /* True if we've saved context from a cygwin signal. */
71 static CONTEXT saved_context
; /* Containes the saved context from a cygwin signal. */
73 /* If we're not using the old Cygwin header file set, define the
74 following which never should have been in the generic Win32 API
75 headers in the first place since they were our own invention... */
76 #ifndef _GNU_H_WINDOWS_H
79 FLAG_TRACE_BIT
= 0x100,
80 CONTEXT_DEBUGGER
= (CONTEXT_FULL
| CONTEXT_FLOATING_POINT
)
83 #include <sys/procfs.h>
86 #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS \
87 | CONTEXT_EXTENDED_REGISTERS
89 static unsigned dr
[8];
90 static int debug_registers_changed
;
91 static int debug_registers_used
;
93 /* The string sent by cygwin when it processes a signal.
94 FIXME: This should be in a cygwin include file. */
95 #ifndef _CYGWIN_SIGNAL_STRING
96 #define _CYGWIN_SIGNAL_STRING "cYgSiGw00f"
99 #define CHECK(x) check (x, __FILE__,__LINE__)
100 #define DEBUG_EXEC(x) if (debug_exec) printf_unfiltered x
101 #define DEBUG_EVENTS(x) if (debug_events) printf_unfiltered x
102 #define DEBUG_MEM(x) if (debug_memory) printf_unfiltered x
103 #define DEBUG_EXCEPT(x) if (debug_exceptions) printf_unfiltered x
105 static void win32_stop (void);
106 static int win32_win32_thread_alive (ptid_t
);
107 static void win32_kill_inferior (void);
109 static enum target_signal last_sig
= TARGET_SIGNAL_0
;
110 /* Set if a signal was received from the debugged process */
112 /* Thread information structure used to track information that is
113 not available in gdb's thread structure. */
114 typedef struct thread_info_struct
116 struct thread_info_struct
*next
;
127 static thread_info thread_head
;
129 /* The process and thread handles for the above context. */
131 static DEBUG_EVENT current_event
; /* The current debug event from
133 static HANDLE current_process_handle
; /* Currently executing process */
134 static thread_info
*current_thread
; /* Info on currently selected thread */
135 static DWORD main_thread_id
; /* Thread ID of the main thread */
137 /* Counts of things. */
138 static int exception_count
= 0;
139 static int event_count
= 0;
140 static int saw_create
;
143 static int new_console
= 0;
144 static int cygwin_exceptions
= 0;
145 static int new_group
= 1;
146 static int debug_exec
= 0; /* show execution */
147 static int debug_events
= 0; /* show events from kernel */
148 static int debug_memory
= 0; /* show target memory accesses */
149 static int debug_exceptions
= 0; /* show target exceptions */
150 static int useshell
= 0; /* use shell for subprocesses */
152 /* This vector maps GDB's idea of a register's number into an address
153 in the win32 exception context vector.
155 It also contains the bit mask needed to load the register in question.
157 One day we could read a reg, we could inspect the context we
158 already have loaded, if it doesn't have the bit set that we need,
159 we read that set of registers in using GetThreadContext. If the
160 context already contains what we need, we just unpack it. Then to
161 write a register, first we have to ensure that the context contains
162 the other regs of the group, and then we copy the info in and set
165 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
166 static const int mappings
[] =
168 context_offset (Eax
),
169 context_offset (Ecx
),
170 context_offset (Edx
),
171 context_offset (Ebx
),
172 context_offset (Esp
),
173 context_offset (Ebp
),
174 context_offset (Esi
),
175 context_offset (Edi
),
176 context_offset (Eip
),
177 context_offset (EFlags
),
178 context_offset (SegCs
),
179 context_offset (SegSs
),
180 context_offset (SegDs
),
181 context_offset (SegEs
),
182 context_offset (SegFs
),
183 context_offset (SegGs
),
184 context_offset (FloatSave
.RegisterArea
[0 * 10]),
185 context_offset (FloatSave
.RegisterArea
[1 * 10]),
186 context_offset (FloatSave
.RegisterArea
[2 * 10]),
187 context_offset (FloatSave
.RegisterArea
[3 * 10]),
188 context_offset (FloatSave
.RegisterArea
[4 * 10]),
189 context_offset (FloatSave
.RegisterArea
[5 * 10]),
190 context_offset (FloatSave
.RegisterArea
[6 * 10]),
191 context_offset (FloatSave
.RegisterArea
[7 * 10]),
192 context_offset (FloatSave
.ControlWord
),
193 context_offset (FloatSave
.StatusWord
),
194 context_offset (FloatSave
.TagWord
),
195 context_offset (FloatSave
.ErrorSelector
),
196 context_offset (FloatSave
.ErrorOffset
),
197 context_offset (FloatSave
.DataSelector
),
198 context_offset (FloatSave
.DataOffset
),
199 context_offset (FloatSave
.ErrorSelector
)
201 context_offset (ExtendedRegisters
[10*16]),
202 context_offset (ExtendedRegisters
[11*16]),
203 context_offset (ExtendedRegisters
[12*16]),
204 context_offset (ExtendedRegisters
[13*16]),
205 context_offset (ExtendedRegisters
[14*16]),
206 context_offset (ExtendedRegisters
[15*16]),
207 context_offset (ExtendedRegisters
[16*16]),
208 context_offset (ExtendedRegisters
[17*16]),
210 context_offset (ExtendedRegisters
[24])
213 #undef context_offset
215 /* This vector maps the target's idea of an exception (extracted
216 from the DEBUG_EVENT structure) to GDB's idea. */
218 struct xlate_exception
221 enum target_signal us
;
224 static const struct xlate_exception
227 {EXCEPTION_ACCESS_VIOLATION
, TARGET_SIGNAL_SEGV
},
228 {STATUS_STACK_OVERFLOW
, TARGET_SIGNAL_SEGV
},
229 {EXCEPTION_BREAKPOINT
, TARGET_SIGNAL_TRAP
},
230 {DBG_CONTROL_C
, TARGET_SIGNAL_INT
},
231 {EXCEPTION_SINGLE_STEP
, TARGET_SIGNAL_TRAP
},
232 {STATUS_FLOAT_DIVIDE_BY_ZERO
, TARGET_SIGNAL_FPE
},
236 check (BOOL ok
, const char *file
, int line
)
239 printf_filtered ("error return %s:%d was %lu\n", file
, line
,
243 /* Find a thread record given a thread id.
244 If get_context then also retrieve the context for this
247 thread_rec (DWORD id
, int get_context
)
251 for (th
= &thread_head
; (th
= th
->next
) != NULL
;)
254 if (!th
->suspend_count
&& get_context
)
256 if (get_context
> 0 && id
!= current_event
.dwThreadId
)
257 th
->suspend_count
= SuspendThread (th
->h
) + 1;
258 else if (get_context
< 0)
259 th
->suspend_count
= -1;
260 th
->reload_context
= 1;
268 /* Add a thread to the thread list */
270 win32_add_thread (DWORD id
, HANDLE h
)
274 if ((th
= thread_rec (id
, FALSE
)))
277 th
= XZALLOC (thread_info
);
280 th
->next
= thread_head
.next
;
281 thread_head
.next
= th
;
282 add_thread (pid_to_ptid (id
));
283 /* Set the debug registers for the new thread in they are used. */
284 if (debug_registers_used
)
286 /* Only change the value of the debug registers. */
287 th
->context
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
288 CHECK (GetThreadContext (th
->h
, &th
->context
));
289 th
->context
.Dr0
= dr
[0];
290 th
->context
.Dr1
= dr
[1];
291 th
->context
.Dr2
= dr
[2];
292 th
->context
.Dr3
= dr
[3];
293 /* th->context.Dr6 = dr[6];
294 FIXME: should we set dr6 also ?? */
295 th
->context
.Dr7
= dr
[7];
296 CHECK (SetThreadContext (th
->h
, &th
->context
));
297 th
->context
.ContextFlags
= 0;
302 /* Clear out any old thread list and reintialize it to a
305 win32_init_thread_list (void)
307 thread_info
*th
= &thread_head
;
309 DEBUG_EVENTS (("gdb: win32_init_thread_list\n"));
311 while (th
->next
!= NULL
)
313 thread_info
*here
= th
->next
;
314 th
->next
= here
->next
;
315 (void) CloseHandle (here
->h
);
318 thread_head
.next
= NULL
;
321 /* Delete a thread from the list of threads */
323 win32_delete_thread (DWORD id
)
328 printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (pid_to_ptid (id
)));
329 delete_thread (pid_to_ptid (id
));
331 for (th
= &thread_head
;
332 th
->next
!= NULL
&& th
->next
->id
!= id
;
336 if (th
->next
!= NULL
)
338 thread_info
*here
= th
->next
;
339 th
->next
= here
->next
;
340 CloseHandle (here
->h
);
346 do_win32_fetch_inferior_registers (struct regcache
*regcache
, int r
)
348 char *context_offset
= ((char *) ¤t_thread
->context
) + mappings
[r
];
352 return; /* Windows sometimes uses a non-existent thread id in its
355 if (current_thread
->reload_context
)
357 #ifdef __COPY_CONTEXT_SIZE
358 if (have_saved_context
)
360 /* Lie about where the program actually is stopped since cygwin has informed us that
361 we should consider the signal to have occurred at another location which is stored
362 in "saved_context. */
363 memcpy (¤t_thread
->context
, &saved_context
, __COPY_CONTEXT_SIZE
);
364 have_saved_context
= 0;
369 thread_info
*th
= current_thread
;
370 th
->context
.ContextFlags
= CONTEXT_DEBUGGER_DR
;
371 GetThreadContext (th
->h
, &th
->context
);
372 /* Copy dr values from that thread. */
373 dr
[0] = th
->context
.Dr0
;
374 dr
[1] = th
->context
.Dr1
;
375 dr
[2] = th
->context
.Dr2
;
376 dr
[3] = th
->context
.Dr3
;
377 dr
[6] = th
->context
.Dr6
;
378 dr
[7] = th
->context
.Dr7
;
380 current_thread
->reload_context
= 0;
383 #define I387_ST0_REGNUM I386_ST0_REGNUM
385 if (r
== I387_FISEG_REGNUM
)
387 l
= *((long *) context_offset
) & 0xffff;
388 regcache_raw_supply (regcache
, r
, (char *) &l
);
390 else if (r
== I387_FOP_REGNUM
)
392 l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
393 regcache_raw_supply (regcache
, r
, (char *) &l
);
396 regcache_raw_supply (regcache
, r
, context_offset
);
399 for (r
= 0; r
< gdbarch_num_regs (current_gdbarch
); r
++)
400 do_win32_fetch_inferior_registers (regcache
, r
);
403 #undef I387_ST0_REGNUM
407 win32_fetch_inferior_registers (struct regcache
*regcache
, int r
)
409 current_thread
= thread_rec (PIDGET (inferior_ptid
), TRUE
);
410 /* Check if current_thread exists. Windows sometimes uses a non-existent
411 thread id in its events */
413 do_win32_fetch_inferior_registers (regcache
, r
);
417 do_win32_store_inferior_registers (const struct regcache
*regcache
, int r
)
420 /* Windows sometimes uses a non-existent thread id in its events */;
422 regcache_raw_collect (regcache
, r
,
423 ((char *) ¤t_thread
->context
) + mappings
[r
]);
426 for (r
= 0; r
< gdbarch_num_regs (current_gdbarch
); r
++)
427 do_win32_store_inferior_registers (regcache
, r
);
431 /* Store a new register value into the current thread context */
433 win32_store_inferior_registers (struct regcache
*regcache
, int r
)
435 current_thread
= thread_rec (PIDGET (inferior_ptid
), TRUE
);
436 /* Check if current_thread exists. Windows sometimes uses a non-existent
437 thread id in its events */
439 do_win32_store_inferior_registers (regcache
, r
);
442 static int psapi_loaded
= 0;
443 static HMODULE psapi_module_handle
= NULL
;
444 static BOOL
WINAPI (*psapi_EnumProcessModules
) (HANDLE
, HMODULE
*, DWORD
, LPDWORD
) = NULL
;
445 static BOOL
WINAPI (*psapi_GetModuleInformation
) (HANDLE
, HMODULE
, LPMODULEINFO
, DWORD
) = NULL
;
446 static DWORD
WINAPI (*psapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
, LPSTR
, DWORD
) = NULL
;
449 psapi_get_dll_name (DWORD BaseAddress
, char *dll_name_ret
)
455 HMODULE
*DllHandle
= dh_buf
;
460 psapi_EnumProcessModules
== NULL
||
461 psapi_GetModuleInformation
== NULL
||
462 psapi_GetModuleFileNameExA
== NULL
)
467 psapi_module_handle
= LoadLibrary ("psapi.dll");
468 if (!psapi_module_handle
)
470 /* printf_unfiltered ("error loading psapi.dll: %u", GetLastError ()); */
473 psapi_EnumProcessModules
= GetProcAddress (psapi_module_handle
, "EnumProcessModules");
474 psapi_GetModuleInformation
= GetProcAddress (psapi_module_handle
, "GetModuleInformation");
475 psapi_GetModuleFileNameExA
= (void *) GetProcAddress (psapi_module_handle
,
476 "GetModuleFileNameExA");
477 if (psapi_EnumProcessModules
== NULL
||
478 psapi_GetModuleInformation
== NULL
||
479 psapi_GetModuleFileNameExA
== NULL
)
484 ok
= (*psapi_EnumProcessModules
) (current_process_handle
,
489 if (!ok
|| !cbNeeded
)
492 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
496 ok
= (*psapi_EnumProcessModules
) (current_process_handle
,
503 for (i
= 0; i
< (int) (cbNeeded
/ sizeof (HMODULE
)); i
++)
505 if (!(*psapi_GetModuleInformation
) (current_process_handle
,
509 error (_("Can't get module info"));
511 len
= (*psapi_GetModuleFileNameExA
) (current_process_handle
,
516 error (_("Error getting dll name: %u."), (unsigned) GetLastError ());
518 if ((DWORD
) (mi
.lpBaseOfDll
) == BaseAddress
)
523 dll_name_ret
[0] = '\0';
527 /* Encapsulate the information required in a call to
528 symbol_file_add_args */
529 struct safe_symbol_file_add_args
533 struct section_addr_info
*addrs
;
536 struct ui_file
*err
, *out
;
540 /* Maintain a linked list of "so" information. */
546 static struct so_list solib_start
, *solib_end
;
548 /* Call symbol_file_add with stderr redirected. We don't care if there
551 safe_symbol_file_add_stub (void *argv
)
553 #define p ((struct safe_symbol_file_add_args *) argv)
554 struct so_list
*so
= &solib_start
;
556 p
->ret
= symbol_file_add (p
->name
, p
->from_tty
, p
->addrs
, p
->mainline
, p
->flags
);
561 /* Restore gdb's stderr after calling symbol_file_add */
563 safe_symbol_file_add_cleanup (void *p
)
565 #define sp ((struct safe_symbol_file_add_args *)p)
566 gdb_flush (gdb_stderr
);
567 gdb_flush (gdb_stdout
);
568 ui_file_delete (gdb_stderr
);
569 ui_file_delete (gdb_stdout
);
570 gdb_stderr
= sp
->err
;
571 gdb_stdout
= sp
->out
;
575 /* symbol_file_add wrapper that prevents errors from being displayed. */
576 static struct objfile
*
577 safe_symbol_file_add (char *name
, int from_tty
,
578 struct section_addr_info
*addrs
,
579 int mainline
, int flags
)
581 struct safe_symbol_file_add_args p
;
582 struct cleanup
*cleanup
;
584 cleanup
= make_cleanup (safe_symbol_file_add_cleanup
, &p
);
588 gdb_flush (gdb_stderr
);
589 gdb_flush (gdb_stdout
);
590 gdb_stderr
= ui_file_new ();
591 gdb_stdout
= ui_file_new ();
593 p
.from_tty
= from_tty
;
595 p
.mainline
= mainline
;
597 catch_errors (safe_symbol_file_add_stub
, &p
, "", RETURN_MASK_ERROR
);
599 do_cleanups (cleanup
);
603 /* Get the loaded address of all sections, given that .text was loaded
604 at text_load. Assumes that all sections are subject to the same
605 relocation offset. Returns NULL if problems occur or if the
606 sections were not relocated. */
608 static struct section_addr_info
*
609 get_relocated_section_addrs (bfd
*abfd
, CORE_ADDR text_load
)
611 struct section_addr_info
*result
= NULL
;
612 int section_count
= bfd_count_sections (abfd
);
613 asection
*text_section
= bfd_get_section_by_name (abfd
, ".text");
618 /* Couldn't get the .text section. Weird. */
620 else if (text_load
== (text_vma
= bfd_get_section_vma (abfd
, text_section
)))
622 /* DLL wasn't relocated. */
626 /* Figure out all sections' loaded addresses. The offset here is
627 such that taking a bfd_get_section_vma() result and adding
628 offset will give the real load address of the section. */
630 CORE_ADDR offset
= text_load
- text_vma
;
632 struct section_table
*table_start
= NULL
;
633 struct section_table
*table_end
= NULL
;
634 struct section_table
*iter
= NULL
;
636 build_section_table (abfd
, &table_start
, &table_end
);
638 for (iter
= table_start
; iter
< table_end
; ++iter
)
640 /* Relocated addresses. */
641 iter
->addr
+= offset
;
642 iter
->endaddr
+= offset
;
645 result
= build_section_addr_info_from_section_table (table_start
,
654 /* Add DLL symbol information. */
656 solib_symbols_add (struct so_list
*so
, CORE_ADDR load_addr
)
658 struct section_addr_info
*addrs
= NULL
;
659 static struct objfile
*result
= NULL
;
660 char *name
= so
->so_name
;
664 /* The symbols in a dll are offset by 0x1000, which is the
665 the offset from 0 of the first byte in an image - because
666 of the file header and the section alignment. */
668 if (!name
|| !name
[0])
671 abfd
= bfd_openr (name
, "pei-i386");
675 /* pei failed - try pe */
676 abfd
= bfd_openr (name
, "pe-i386");
681 if (bfd_check_format (abfd
, bfd_object
))
682 addrs
= get_relocated_section_addrs (abfd
, load_addr
);
687 result
= safe_symbol_file_add (name
, 0, addrs
, 0, OBJF_SHARED
);
688 free_section_addr_info (addrs
);
692 /* Fallback on handling just the .text section. */
693 struct cleanup
*my_cleanups
;
695 addrs
= alloc_section_addr_info (1);
696 my_cleanups
= make_cleanup (xfree
, addrs
);
697 addrs
->other
[0].name
= ".text";
698 addrs
->other
[0].addr
= load_addr
;
700 result
= safe_symbol_file_add (name
, 0, addrs
, 0, OBJF_SHARED
);
701 do_cleanups (my_cleanups
);
704 p
= strchr (so
->so_name
, '\0') - (sizeof ("/cygwin1.dll") - 1);
705 if (p
>= so
->so_name
&& strcasecmp (p
, "/cygwin1.dll") == 0)
707 asection
*text
= bfd_get_section_by_name (abfd
, ".text");
708 cygwin_load_start
= bfd_section_vma (abfd
, text
);
709 cygwin_load_end
= cygwin_load_start
+ bfd_section_size (abfd
, text
);
714 so
->symbols_loaded
= !!result
;
719 register_loaded_dll (const char *name
, DWORD load_addr
, int readsyms
)
722 char buf
[MAX_PATH
+ 1];
723 char cwd
[MAX_PATH
+ 1];
725 WIN32_FIND_DATA w32_fd
;
726 HANDLE h
= FindFirstFile(name
, &w32_fd
);
727 MEMORY_BASIC_INFORMATION m
;
730 if (h
== INVALID_HANDLE_VALUE
)
736 if (GetCurrentDirectory (MAX_PATH
+ 1, cwd
))
738 p
= strrchr (buf
, '\\');
741 SetCurrentDirectory (buf
);
742 GetFullPathName (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
743 SetCurrentDirectory (cwd
);
747 if (strcasecmp (buf
, "ntdll.dll") == 0)
749 GetSystemDirectory (buf
, sizeof (buf
));
750 strcat (buf
, "\\ntdll.dll");
752 so
= XZALLOC (struct so_list
);
753 so
->lm_info
= (struct lm_info
*) xmalloc (sizeof (struct lm_info
));
754 so
->lm_info
->load_addr
= load_addr
;
755 cygwin_conv_to_posix_path (buf
, so
->so_name
);
756 strcpy (so
->so_original_name
, so
->so_name
);
758 solib_end
->next
= so
;
760 len
= strlen (so
->so_name
);
762 solib_symbols_add (so
, (CORE_ADDR
) load_addr
);
768 get_image_name (HANDLE h
, void *address
, int unicode
)
770 static char buf
[(2 * MAX_PATH
) + 1];
771 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
777 /* Attempt to read the name of the dll that was detected.
778 This is documented to work only when actively debugging
779 a program. It will not work for attached processes. */
783 /* See if we could read the address of a string, and that the
784 address isn't null. */
785 if (!ReadProcessMemory (h
, address
, &address_ptr
, sizeof (address_ptr
), &done
)
786 || done
!= sizeof (address_ptr
) || !address_ptr
)
789 /* Find the length of the string */
790 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
791 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
795 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
798 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
799 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
802 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
808 /* Wait for child to do something. Return pid of child, or -1 in case
809 of error; store status through argument pointer OURSTATUS. */
811 handle_load_dll (void *dummy
)
813 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
814 char dll_buf
[MAX_PATH
+ 1];
815 char *dll_name
= NULL
;
817 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
819 if (!psapi_get_dll_name ((DWORD
) (event
->lpBaseOfDll
), dll_buf
))
820 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
824 if (*dll_name
== '\0')
825 dll_name
= get_image_name (current_process_handle
, event
->lpImageName
, event
->fUnicode
);
829 register_loaded_dll (dll_name
, (DWORD
) event
->lpBaseOfDll
+ 0x1000, auto_solib_add
);
835 win32_free_so (struct so_list
*so
)
842 win32_relocate_section_addresses (struct so_list
*so
,
843 struct section_table
*sec
)
850 win32_solib_create_inferior_hook (void)
852 solib_add (NULL
, 0, NULL
, auto_solib_add
);
857 handle_unload_dll (void *dummy
)
859 DWORD lpBaseOfDll
= (DWORD
) current_event
.u
.UnloadDll
.lpBaseOfDll
+ 0x1000;
862 for (so
= &solib_start
; so
->next
!= NULL
; so
= so
->next
)
863 if (so
->next
->lm_info
->load_addr
== lpBaseOfDll
)
865 struct so_list
*sodel
= so
->next
;
866 so
->next
= sodel
->next
;
870 solib_add (NULL
, 0, NULL
, auto_solib_add
);
874 error (_("Error: dll starting at 0x%lx not found."), (DWORD
) lpBaseOfDll
);
879 /* Clear list of loaded DLLs. */
881 win32_clear_solib (void)
883 solib_start
.next
= NULL
;
884 solib_end
= &solib_start
;
888 win32_special_symbol_handling (void)
893 /* Load DLL symbol info. */
895 dll_symbol_command (char *args
, int from_tty
)
901 error (_("dll-symbols requires a file name"));
904 if (n
> 4 && strcasecmp (args
+ n
- 4, ".dll") != 0)
906 char *newargs
= (char *) alloca (n
+ 4 + 1);
907 strcpy (newargs
, args
);
908 strcat (newargs
, ".dll");
912 safe_symbol_file_add (args
, from_tty
, NULL
, 0, OBJF_SHARED
| OBJF_USERLOADED
);
915 /* Handle DEBUG_STRING output from child process.
916 Cygwin prepends its messages with a "cygwin:". Interpret this as
917 a Cygwin signal. Otherwise just print the string as a warning. */
919 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
924 if (!target_read_string
925 ((CORE_ADDR
) current_event
.u
.DebugString
.lpDebugStringData
, &s
, 1024, 0)
928 else if (strncmp (s
, _CYGWIN_SIGNAL_STRING
, sizeof (_CYGWIN_SIGNAL_STRING
) - 1) != 0)
930 if (strncmp (s
, "cYg", 3) != 0)
933 #ifdef __COPY_CONTEXT_SIZE
936 /* Got a cygwin signal marker. A cygwin signal is followed by the signal number
937 itself and then optionally followed by the thread id and address to saved context
938 within the DLL. If these are supplied, then the given thread is assumed to have
939 issued the signal and the context from the thread is assumed to be stored at the
940 given address in the inferior. Tell gdb to treat this like a real signal. */
942 int sig
= strtol (s
+ sizeof (_CYGWIN_SIGNAL_STRING
) - 1, &p
, 0);
943 int gotasig
= target_signal_from_host (sig
);
944 ourstatus
->value
.sig
= gotasig
;
949 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
950 retval
= strtoul (p
, &p
, 0);
952 retval
= main_thread_id
;
953 else if ((x
= (LPCVOID
) strtoul (p
, &p
, 0))
954 && ReadProcessMemory (current_process_handle
, x
,
955 &saved_context
, __COPY_CONTEXT_SIZE
, &n
)
956 && n
== __COPY_CONTEXT_SIZE
)
957 have_saved_context
= 1;
958 current_event
.dwThreadId
= retval
;
969 display_selector (HANDLE thread
, DWORD sel
)
972 if (GetThreadSelectorEntry (thread
, sel
, &info
))
975 printf_filtered ("0x%03lx: ", sel
);
976 if (!info
.HighWord
.Bits
.Pres
)
978 puts_filtered ("Segment not present\n");
981 base
= (info
.HighWord
.Bits
.BaseHi
<< 24) +
982 (info
.HighWord
.Bits
.BaseMid
<< 16)
984 limit
= (info
.HighWord
.Bits
.LimitHi
<< 16) + info
.LimitLow
;
985 if (info
.HighWord
.Bits
.Granularity
)
986 limit
= (limit
<< 12) | 0xfff;
987 printf_filtered ("base=0x%08x limit=0x%08x", base
, limit
);
988 if (info
.HighWord
.Bits
.Default_Big
)
989 puts_filtered(" 32-bit ");
991 puts_filtered(" 16-bit ");
992 switch ((info
.HighWord
.Bits
.Type
& 0xf) >> 1)
995 puts_filtered ("Data (Read-Only, Exp-up");
998 puts_filtered ("Data (Read/Write, Exp-up");
1001 puts_filtered ("Unused segment (");
1004 puts_filtered ("Data (Read/Write, Exp-down");
1007 puts_filtered ("Code (Exec-Only, N.Conf");
1010 puts_filtered ("Code (Exec/Read, N.Conf");
1013 puts_filtered ("Code (Exec-Only, Conf");
1016 puts_filtered ("Code (Exec/Read, Conf");
1019 printf_filtered ("Unknown type 0x%x",info
.HighWord
.Bits
.Type
);
1021 if ((info
.HighWord
.Bits
.Type
& 0x1) == 0)
1022 puts_filtered(", N.Acc");
1023 puts_filtered (")\n");
1024 if ((info
.HighWord
.Bits
.Type
& 0x10) == 0)
1025 puts_filtered("System selector ");
1026 printf_filtered ("Priviledge level = %d. ", info
.HighWord
.Bits
.Dpl
);
1027 if (info
.HighWord
.Bits
.Granularity
)
1028 puts_filtered ("Page granular.\n");
1030 puts_filtered ("Byte granular.\n");
1035 printf_filtered ("Invalid selector 0x%lx.\n",sel
);
1041 display_selectors (char * args
, int from_tty
)
1043 if (!current_thread
)
1045 puts_filtered ("Impossible to display selectors now.\n");
1051 puts_filtered ("Selector $cs\n");
1052 display_selector (current_thread
->h
,
1053 current_thread
->context
.SegCs
);
1054 puts_filtered ("Selector $ds\n");
1055 display_selector (current_thread
->h
,
1056 current_thread
->context
.SegDs
);
1057 puts_filtered ("Selector $es\n");
1058 display_selector (current_thread
->h
,
1059 current_thread
->context
.SegEs
);
1060 puts_filtered ("Selector $ss\n");
1061 display_selector (current_thread
->h
,
1062 current_thread
->context
.SegSs
);
1063 puts_filtered ("Selector $fs\n");
1064 display_selector (current_thread
->h
,
1065 current_thread
->context
.SegFs
);
1066 puts_filtered ("Selector $gs\n");
1067 display_selector (current_thread
->h
,
1068 current_thread
->context
.SegGs
);
1073 sel
= parse_and_eval_long (args
);
1074 printf_filtered ("Selector \"%s\"\n",args
);
1075 display_selector (current_thread
->h
, sel
);
1079 static struct cmd_list_element
*info_w32_cmdlist
= NULL
;
1082 info_w32_command (char *args
, int from_tty
)
1084 help_list (info_w32_cmdlist
, "info w32 ", class_info
, gdb_stdout
);
1088 #define DEBUG_EXCEPTION_SIMPLE(x) if (debug_exceptions) \
1089 printf_unfiltered ("gdb: Target exception %s at 0x%08lx\n", x, \
1090 (DWORD) current_event.u.Exception.ExceptionRecord.ExceptionAddress)
1093 handle_exception (struct target_waitstatus
*ourstatus
)
1096 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
1098 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
1100 /* Record the context of the current thread */
1101 th
= thread_rec (current_event
.dwThreadId
, -1);
1105 case EXCEPTION_ACCESS_VIOLATION
:
1106 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ACCESS_VIOLATION");
1107 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
1109 /* See if the access violation happened within the cygwin DLL itself. Cygwin uses
1110 a kind of exception handling to deal with passed-in invalid addresses. gdb
1111 should not treat these as real SEGVs since they will be silently handled by
1112 cygwin. A real SEGV will (theoretically) be caught by cygwin later in the process
1113 and will be sent as a cygwin-specific-signal. So, ignore SEGVs if they show up
1114 within the text segment of the DLL itself. */
1116 bfd_vma addr
= (bfd_vma
) current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
;
1117 if ((!cygwin_exceptions
&& (addr
>= cygwin_load_start
&& addr
< cygwin_load_end
))
1118 || (find_pc_partial_function (addr
, &fn
, NULL
, NULL
)
1119 && strncmp (fn
, "KERNEL32!IsBad", strlen ("KERNEL32!IsBad")) == 0))
1123 case STATUS_STACK_OVERFLOW
:
1124 DEBUG_EXCEPTION_SIMPLE ("STATUS_STACK_OVERFLOW");
1125 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
1127 case STATUS_FLOAT_DENORMAL_OPERAND
:
1128 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DENORMAL_OPERAND");
1129 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1131 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
1132 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ARRAY_BOUNDS_EXCEEDED");
1133 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1135 case STATUS_FLOAT_INEXACT_RESULT
:
1136 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INEXACT_RESULT");
1137 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1139 case STATUS_FLOAT_INVALID_OPERATION
:
1140 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INVALID_OPERATION");
1141 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1143 case STATUS_FLOAT_OVERFLOW
:
1144 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_OVERFLOW");
1145 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1147 case STATUS_FLOAT_STACK_CHECK
:
1148 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_STACK_CHECK");
1149 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1151 case STATUS_FLOAT_UNDERFLOW
:
1152 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_UNDERFLOW");
1153 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1155 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1156 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DIVIDE_BY_ZERO");
1157 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1159 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1160 DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_DIVIDE_BY_ZERO");
1161 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1163 case STATUS_INTEGER_OVERFLOW
:
1164 DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_OVERFLOW");
1165 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1167 case EXCEPTION_BREAKPOINT
:
1168 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT");
1169 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1172 DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_C");
1173 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1175 case DBG_CONTROL_BREAK
:
1176 DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_BREAK");
1177 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1179 case EXCEPTION_SINGLE_STEP
:
1180 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_SINGLE_STEP");
1181 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1183 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1184 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ILLEGAL_INSTRUCTION");
1185 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1187 case EXCEPTION_PRIV_INSTRUCTION
:
1188 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_PRIV_INSTRUCTION");
1189 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1191 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1192 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION");
1193 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1196 /* Treat unhandled first chance exceptions specially. */
1197 if (current_event
.u
.Exception
.dwFirstChance
)
1199 printf_unfiltered ("gdb: unknown target exception 0x%08lx at 0x%08lx\n",
1200 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1201 (DWORD
) current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1202 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1206 last_sig
= ourstatus
->value
.sig
;
1210 /* Resume all artificially suspended threads if we are continuing
1213 win32_continue (DWORD continue_status
, int id
)
1219 DEBUG_EVENTS (("ContinueDebugEvent (cpid=%ld, ctid=%ld, %s);\n",
1220 current_event
.dwProcessId
, current_event
.dwThreadId
,
1221 continue_status
== DBG_CONTINUE
?
1222 "DBG_CONTINUE" : "DBG_EXCEPTION_NOT_HANDLED"));
1223 res
= ContinueDebugEvent (current_event
.dwProcessId
,
1224 current_event
.dwThreadId
,
1227 for (th
= &thread_head
; (th
= th
->next
) != NULL
;)
1228 if (((id
== -1) || (id
== (int) th
->id
)) && th
->suspend_count
)
1231 for (i
= 0; i
< th
->suspend_count
; i
++)
1232 (void) ResumeThread (th
->h
);
1233 th
->suspend_count
= 0;
1234 if (debug_registers_changed
)
1236 /* Only change the value of the debug registers */
1237 th
->context
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1238 th
->context
.Dr0
= dr
[0];
1239 th
->context
.Dr1
= dr
[1];
1240 th
->context
.Dr2
= dr
[2];
1241 th
->context
.Dr3
= dr
[3];
1242 /* th->context.Dr6 = dr[6];
1243 FIXME: should we set dr6 also ?? */
1244 th
->context
.Dr7
= dr
[7];
1245 CHECK (SetThreadContext (th
->h
, &th
->context
));
1246 th
->context
.ContextFlags
= 0;
1250 debug_registers_changed
= 0;
1254 /* Called in pathological case where Windows fails to send a
1255 CREATE_PROCESS_DEBUG_EVENT after an attach. */
1257 fake_create_process (void)
1259 current_process_handle
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
,
1260 current_event
.dwProcessId
);
1261 main_thread_id
= current_event
.dwThreadId
;
1262 current_thread
= win32_add_thread (main_thread_id
,
1263 current_event
.u
.CreateThread
.hThread
);
1264 return main_thread_id
;
1268 win32_resume (ptid_t ptid
, int step
, enum target_signal sig
)
1271 DWORD continue_status
= DBG_CONTINUE
;
1273 int pid
= PIDGET (ptid
);
1275 if (sig
!= TARGET_SIGNAL_0
)
1277 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
1279 DEBUG_EXCEPT(("Cannot continue with signal %d here.\n",sig
));
1281 else if (sig
== last_sig
)
1282 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1285 /* This code does not seem to work, because
1286 the kernel does probably not consider changes in the ExceptionRecord
1287 structure when passing the exception to the inferior.
1288 Note that this seems possible in the exception handler itself. */
1291 for (i
= 0; xlate
[i
].them
!= -1; i
++)
1292 if (xlate
[i
].us
== sig
)
1294 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
=
1296 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1299 if (continue_status
== DBG_CONTINUE
)
1301 DEBUG_EXCEPT(("Cannot continue with signal %d.\n",sig
));
1305 DEBUG_EXCEPT(("Can only continue with recieved signal %d.\n",
1309 last_sig
= TARGET_SIGNAL_0
;
1311 DEBUG_EXEC (("gdb: win32_resume (pid=%d, step=%d, sig=%d);\n",
1314 /* Get context for currently selected thread */
1315 th
= thread_rec (current_event
.dwThreadId
, FALSE
);
1320 /* Single step by setting t bit */
1321 win32_fetch_inferior_registers (get_current_regcache (),
1322 gdbarch_ps_regnum (current_gdbarch
));
1323 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
1326 if (th
->context
.ContextFlags
)
1328 if (debug_registers_changed
)
1330 th
->context
.Dr0
= dr
[0];
1331 th
->context
.Dr1
= dr
[1];
1332 th
->context
.Dr2
= dr
[2];
1333 th
->context
.Dr3
= dr
[3];
1334 /* th->context.Dr6 = dr[6];
1335 FIXME: should we set dr6 also ?? */
1336 th
->context
.Dr7
= dr
[7];
1338 CHECK (SetThreadContext (th
->h
, &th
->context
));
1339 th
->context
.ContextFlags
= 0;
1343 /* Allow continuing with the same signal that interrupted us.
1344 Otherwise complain. */
1346 win32_continue (continue_status
, pid
);
1349 /* Get the next event from the child. Return 1 if the event requires
1350 handling by WFI (or whatever).
1353 get_win32_debug_event (int pid
, struct target_waitstatus
*ourstatus
)
1356 DWORD continue_status
, event_code
;
1358 static thread_info dummy_thread_info
;
1362 last_sig
= TARGET_SIGNAL_0
;
1364 if (!(debug_event
= WaitForDebugEvent (¤t_event
, 1000)))
1368 continue_status
= DBG_CONTINUE
;
1370 event_code
= current_event
.dwDebugEventCode
;
1371 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1373 have_saved_context
= 0;
1377 case CREATE_THREAD_DEBUG_EVENT
:
1378 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n",
1379 (unsigned) current_event
.dwProcessId
,
1380 (unsigned) current_event
.dwThreadId
,
1381 "CREATE_THREAD_DEBUG_EVENT"));
1382 if (saw_create
!= 1)
1384 if (!saw_create
&& attach_flag
)
1386 /* Kludge around a Windows bug where first event is a create
1387 thread event. Caused when attached process does not have
1389 retval
= ourstatus
->value
.related_pid
= fake_create_process ();
1394 /* Record the existence of this thread */
1395 th
= win32_add_thread (current_event
.dwThreadId
,
1396 current_event
.u
.CreateThread
.hThread
);
1398 printf_unfiltered ("[New %s]\n",
1400 pid_to_ptid (current_event
.dwThreadId
)));
1401 retval
= current_event
.dwThreadId
;
1404 case EXIT_THREAD_DEBUG_EVENT
:
1405 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1406 (unsigned) current_event
.dwProcessId
,
1407 (unsigned) current_event
.dwThreadId
,
1408 "EXIT_THREAD_DEBUG_EVENT"));
1409 if (current_event
.dwThreadId
!= main_thread_id
)
1411 win32_delete_thread (current_event
.dwThreadId
);
1412 th
= &dummy_thread_info
;
1416 case CREATE_PROCESS_DEBUG_EVENT
:
1417 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1418 (unsigned) current_event
.dwProcessId
,
1419 (unsigned) current_event
.dwThreadId
,
1420 "CREATE_PROCESS_DEBUG_EVENT"));
1421 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1422 if (++saw_create
!= 1)
1424 CloseHandle (current_event
.u
.CreateProcessInfo
.hProcess
);
1428 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1430 win32_delete_thread (main_thread_id
);
1431 main_thread_id
= current_event
.dwThreadId
;
1432 /* Add the main thread */
1433 th
= win32_add_thread (main_thread_id
,
1434 current_event
.u
.CreateProcessInfo
.hThread
);
1435 retval
= ourstatus
->value
.related_pid
= current_event
.dwThreadId
;
1438 case EXIT_PROCESS_DEBUG_EVENT
:
1439 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1440 (unsigned) current_event
.dwProcessId
,
1441 (unsigned) current_event
.dwThreadId
,
1442 "EXIT_PROCESS_DEBUG_EVENT"));
1443 if (saw_create
!= 1)
1445 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1446 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1447 CloseHandle (current_process_handle
);
1448 retval
= main_thread_id
;
1451 case LOAD_DLL_DEBUG_EVENT
:
1452 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1453 (unsigned) current_event
.dwProcessId
,
1454 (unsigned) current_event
.dwThreadId
,
1455 "LOAD_DLL_DEBUG_EVENT"));
1456 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1457 if (saw_create
!= 1)
1459 catch_errors (handle_load_dll
, NULL
, (char *) "", RETURN_MASK_ALL
);
1460 registers_changed (); /* mark all regs invalid */
1461 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1462 ourstatus
->value
.integer
= 0;
1463 retval
= main_thread_id
;
1464 re_enable_breakpoints_in_shlibs ();
1467 case UNLOAD_DLL_DEBUG_EVENT
:
1468 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1469 (unsigned) current_event
.dwProcessId
,
1470 (unsigned) current_event
.dwThreadId
,
1471 "UNLOAD_DLL_DEBUG_EVENT"));
1472 if (saw_create
!= 1)
1474 catch_errors (handle_unload_dll
, NULL
, (char *) "", RETURN_MASK_ALL
);
1475 registers_changed (); /* mark all regs invalid */
1476 /* ourstatus->kind = TARGET_WAITKIND_UNLOADED;
1477 does not exist yet. */
1480 case EXCEPTION_DEBUG_EVENT
:
1481 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1482 (unsigned) current_event
.dwProcessId
,
1483 (unsigned) current_event
.dwThreadId
,
1484 "EXCEPTION_DEBUG_EVENT"));
1485 if (saw_create
!= 1)
1487 switch (handle_exception (ourstatus
))
1490 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1493 retval
= current_event
.dwThreadId
;
1497 continue_status
= -1;
1502 case OUTPUT_DEBUG_STRING_EVENT
: /* message from the kernel */
1503 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1504 (unsigned) current_event
.dwProcessId
,
1505 (unsigned) current_event
.dwThreadId
,
1506 "OUTPUT_DEBUG_STRING_EVENT"));
1507 if (saw_create
!= 1)
1509 retval
= handle_output_debug_string (ourstatus
);
1513 if (saw_create
!= 1)
1515 printf_unfiltered ("gdb: kernel event for pid=%ld tid=%ld\n",
1516 (DWORD
) current_event
.dwProcessId
,
1517 (DWORD
) current_event
.dwThreadId
);
1518 printf_unfiltered (" unknown event code %ld\n",
1519 current_event
.dwDebugEventCode
);
1523 if (!retval
|| saw_create
!= 1)
1525 if (continue_status
== -1)
1526 win32_resume (ptid
, 0, 1);
1528 CHECK (win32_continue (continue_status
, -1));
1532 inferior_ptid
= pid_to_ptid (retval
);
1533 current_thread
= th
?: thread_rec (current_event
.dwThreadId
, TRUE
);
1540 /* Wait for interesting events to occur in the target process. */
1542 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1544 int pid
= PIDGET (ptid
);
1546 target_terminal_ours ();
1548 /* We loop when we get a non-standard exception rather than return
1549 with a SPURIOUS because resume can try and step or modify things,
1550 which needs a current_thread->h. But some of these exceptions mark
1551 the birth or death of threads, which mean that the current thread
1552 isn't necessarily what you think it is. */
1556 int retval
= get_win32_debug_event (pid
, ourstatus
);
1558 return pid_to_ptid (retval
);
1563 if (deprecated_ui_loop_hook
!= NULL
)
1564 detach
= deprecated_ui_loop_hook (0);
1567 win32_kill_inferior ();
1573 do_initial_win32_stuff (DWORD pid
)
1575 extern int stop_after_trap
;
1578 last_sig
= TARGET_SIGNAL_0
;
1580 exception_count
= 0;
1581 debug_registers_changed
= 0;
1582 debug_registers_used
= 0;
1583 for (i
= 0; i
< sizeof (dr
) / sizeof (dr
[0]); i
++)
1585 current_event
.dwProcessId
= pid
;
1586 memset (¤t_event
, 0, sizeof (current_event
));
1587 push_target (&win32_ops
);
1588 disable_breakpoints_in_shlibs (1);
1589 win32_clear_solib ();
1590 clear_proceed_status ();
1591 init_wait_for_inferior ();
1593 terminal_init_inferior_with_pgrp (pid
);
1594 target_terminal_inferior ();
1598 stop_after_trap
= 1;
1599 wait_for_inferior ();
1600 if (stop_signal
!= TARGET_SIGNAL_TRAP
)
1601 resume (0, stop_signal
);
1605 stop_after_trap
= 0;
1609 /* Since Windows XP, detaching from a process is supported by Windows.
1610 The following code tries loading the appropriate functions dynamically.
1611 If loading these functions succeeds use them to actually detach from
1612 the inferior process, otherwise behave as usual, pretending that
1613 detach has worked. */
1614 static BOOL
WINAPI (*DebugSetProcessKillOnExit
)(BOOL
);
1615 static BOOL
WINAPI (*DebugActiveProcessStop
)(DWORD
);
1618 has_detach_ability (void)
1620 static HMODULE kernel32
= NULL
;
1623 kernel32
= LoadLibrary ("kernel32.dll");
1626 if (!DebugSetProcessKillOnExit
)
1627 DebugSetProcessKillOnExit
= GetProcAddress (kernel32
,
1628 "DebugSetProcessKillOnExit");
1629 if (!DebugActiveProcessStop
)
1630 DebugActiveProcessStop
= GetProcAddress (kernel32
,
1631 "DebugActiveProcessStop");
1632 if (DebugSetProcessKillOnExit
&& DebugActiveProcessStop
)
1638 /* Try to set or remove a user privilege to the current process. Return -1
1639 if that fails, the previous setting of that privilege otherwise.
1641 This code is copied from the Cygwin source code and rearranged to allow
1642 dynamically loading of the needed symbols from advapi32 which is only
1643 available on NT/2K/XP. */
1645 set_process_privilege (const char *privilege
, BOOL enable
)
1647 static HMODULE advapi32
= NULL
;
1648 static BOOL
WINAPI (*OpenProcessToken
)(HANDLE
, DWORD
, PHANDLE
);
1649 static BOOL
WINAPI (*LookupPrivilegeValue
)(LPCSTR
, LPCSTR
, PLUID
);
1650 static BOOL
WINAPI (*AdjustTokenPrivileges
)(HANDLE
, BOOL
, PTOKEN_PRIVILEGES
,
1651 DWORD
, PTOKEN_PRIVILEGES
, PDWORD
);
1653 HANDLE token_hdl
= NULL
;
1655 TOKEN_PRIVILEGES new_priv
, orig_priv
;
1659 if (GetVersion () >= 0x80000000) /* No security availbale on 9x/Me */
1664 if (!(advapi32
= LoadLibrary ("advapi32.dll")))
1666 if (!OpenProcessToken
)
1667 OpenProcessToken
= GetProcAddress (advapi32
, "OpenProcessToken");
1668 if (!LookupPrivilegeValue
)
1669 LookupPrivilegeValue
= GetProcAddress (advapi32
,
1670 "LookupPrivilegeValueA");
1671 if (!AdjustTokenPrivileges
)
1672 AdjustTokenPrivileges
= GetProcAddress (advapi32
,
1673 "AdjustTokenPrivileges");
1674 if (!OpenProcessToken
|| !LookupPrivilegeValue
|| !AdjustTokenPrivileges
)
1681 if (!OpenProcessToken (GetCurrentProcess (),
1682 TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
,
1686 if (!LookupPrivilegeValue (NULL
, privilege
, &restore_priv
))
1689 new_priv
.PrivilegeCount
= 1;
1690 new_priv
.Privileges
[0].Luid
= restore_priv
;
1691 new_priv
.Privileges
[0].Attributes
= enable
? SE_PRIVILEGE_ENABLED
: 0;
1693 if (!AdjustTokenPrivileges (token_hdl
, FALSE
, &new_priv
,
1694 sizeof orig_priv
, &orig_priv
, &size
))
1697 /* Disabled, otherwise every `attach' in an unprivileged user session
1698 would raise the "Failed to get SE_DEBUG_NAME privilege" warning in
1700 /* AdjustTokenPrivileges returns TRUE even if the privilege could not
1701 be enabled. GetLastError () returns an correct error code, though. */
1702 if (enable
&& GetLastError () == ERROR_NOT_ALL_ASSIGNED
)
1706 ret
= orig_priv
.Privileges
[0].Attributes
== SE_PRIVILEGE_ENABLED
? 1 : 0;
1710 CloseHandle (token_hdl
);
1715 /* Attach to process PID, then initialize for debugging it. */
1717 win32_attach (char *args
, int from_tty
)
1723 error_no_arg (_("process-id to attach"));
1725 if (set_process_privilege (SE_DEBUG_NAME
, TRUE
) < 0)
1727 printf_unfiltered ("Warning: Failed to get SE_DEBUG_NAME privilege\n");
1728 printf_unfiltered ("This can cause attach to fail on Windows NT/2K/XP\n");
1731 pid
= strtoul (args
, 0, 0); /* Windows pid */
1733 win32_init_thread_list ();
1734 ok
= DebugActiveProcess (pid
);
1739 /* Try fall back to Cygwin pid */
1740 pid
= cygwin_internal (CW_CYGWIN_PID_TO_WINPID
, pid
);
1743 ok
= DebugActiveProcess (pid
);
1746 error (_("Can't attach to process."));
1749 if (has_detach_ability ())
1750 DebugSetProcessKillOnExit (FALSE
);
1756 char *exec_file
= (char *) get_exec_file (0);
1759 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
,
1760 target_pid_to_str (pid_to_ptid (pid
)));
1762 printf_unfiltered ("Attaching to %s\n",
1763 target_pid_to_str (pid_to_ptid (pid
)));
1765 gdb_flush (gdb_stdout
);
1768 do_initial_win32_stuff (pid
);
1769 target_terminal_ours ();
1773 win32_detach (char *args
, int from_tty
)
1777 if (has_detach_ability ())
1780 win32_resume (ptid
, 0, TARGET_SIGNAL_0
);
1782 if (!DebugActiveProcessStop (current_event
.dwProcessId
))
1784 error (_("Can't detach process %lu (error %lu)"),
1785 current_event
.dwProcessId
, GetLastError ());
1788 DebugSetProcessKillOnExit (FALSE
);
1790 if (detached
&& from_tty
)
1792 char *exec_file
= get_exec_file (0);
1795 printf_unfiltered ("Detaching from program: %s, Pid %lu\n", exec_file
,
1796 current_event
.dwProcessId
);
1797 gdb_flush (gdb_stdout
);
1799 inferior_ptid
= null_ptid
;
1800 unpush_target (&win32_ops
);
1804 win32_pid_to_exec_file (int pid
)
1806 /* Try to find the process path using the Cygwin internal process list
1807 pid isn't a valid pid, unfortunately. Use current_event.dwProcessId
1809 /* TODO: Also find native Windows processes using CW_GETPINFO_FULL. */
1811 static char path
[MAX_PATH
+ 1];
1812 char *path_ptr
= NULL
;
1814 struct external_pinfo
*pinfo
;
1816 cygwin_internal (CW_LOCK_PINFO
, 1000);
1818 (pinfo
= (struct external_pinfo
*)
1819 cygwin_internal (CW_GETPINFO
, cpid
| CW_NEXTPID
));
1822 if (pinfo
->dwProcessId
== current_event
.dwProcessId
) /* Got it */
1824 cygwin_conv_to_full_posix_path (pinfo
->progname
, path
);
1829 cygwin_internal (CW_UNLOCK_PINFO
);
1833 /* Print status information about what we're accessing. */
1836 win32_files_info (struct target_ops
*ignore
)
1838 printf_unfiltered ("\tUsing the running image of %s %s.\n",
1839 attach_flag
? "attached" : "child", target_pid_to_str (inferior_ptid
));
1843 win32_open (char *arg
, int from_tty
)
1845 error (_("Use the \"run\" command to start a Unix child process."));
1848 /* Start an inferior win32 child process and sets inferior_ptid to its pid.
1849 EXEC_FILE is the file to run.
1850 ALLARGS is a string containing the arguments to the program.
1851 ENV is the environment vector to pass. Errors reported with error(). */
1854 win32_create_inferior (char *exec_file
, char *allargs
, char **in_env
,
1858 PROCESS_INFORMATION pi
;
1862 char real_path
[MAXPATHLEN
];
1864 char shell
[MAX_PATH
+ 1]; /* Path to shell */
1867 int ostdin
, ostdout
, ostderr
;
1868 const char *inferior_io_terminal
= get_inferior_io_terminal ();
1871 error (_("No executable specified, use `target exec'."));
1873 memset (&si
, 0, sizeof (si
));
1874 si
.cb
= sizeof (si
);
1878 flags
= DEBUG_ONLY_THIS_PROCESS
;
1879 cygwin_conv_to_win32_path (exec_file
, real_path
);
1885 sh
= getenv ("SHELL");
1888 cygwin_conv_to_win32_path (sh
, shell
);
1889 newallargs
= alloca (sizeof (" -c 'exec '") + strlen (exec_file
)
1890 + strlen (allargs
) + 2);
1891 sprintf (newallargs
, " -c 'exec %s %s'", exec_file
, allargs
);
1892 allargs
= newallargs
;
1894 flags
= DEBUG_PROCESS
;
1898 flags
|= CREATE_NEW_PROCESS_GROUP
;
1901 flags
|= CREATE_NEW_CONSOLE
;
1905 args
= alloca (strlen (toexec
) + strlen (allargs
) + 2);
1906 strcpy (args
, toexec
);
1908 strcat (args
, allargs
);
1910 /* Prepare the environment vars for CreateProcess. */
1911 cygwin_internal (CW_SYNC_WINENV
);
1913 if (!inferior_io_terminal
)
1914 tty
= ostdin
= ostdout
= ostderr
= -1;
1917 tty
= open (inferior_io_terminal
, O_RDWR
| O_NOCTTY
);
1920 print_sys_errmsg (inferior_io_terminal
, errno
);
1921 ostdin
= ostdout
= ostderr
= -1;
1934 win32_init_thread_list ();
1935 ret
= CreateProcess (0,
1936 args
, /* command line */
1937 NULL
, /* Security */
1939 TRUE
, /* inherit handles */
1940 flags
, /* start flags */
1941 NULL
, /* environment */
1942 NULL
, /* current directory */
1957 error (_("Error creating process %s, (error %d)."),
1958 exec_file
, (unsigned) GetLastError ());
1960 CloseHandle (pi
.hThread
);
1961 CloseHandle (pi
.hProcess
);
1963 if (useshell
&& shell
[0] != '\0')
1968 do_initial_win32_stuff (pi
.dwProcessId
);
1970 /* win32_continue (DBG_CONTINUE, -1); */
1974 win32_mourn_inferior (void)
1976 (void) win32_continue (DBG_CONTINUE
, -1);
1977 i386_cleanup_dregs();
1978 unpush_target (&win32_ops
);
1979 generic_mourn_inferior ();
1982 /* Send a SIGINT to the process group. This acts just like the user typed a
1983 ^C on the controlling terminal. */
1988 DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n"));
1989 CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT
, current_event
.dwProcessId
));
1990 registers_changed (); /* refresh register state */
1994 win32_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*our
, int len
,
1995 int write
, struct mem_attrib
*mem
,
1996 struct target_ops
*target
)
2001 DEBUG_MEM (("gdb: write target memory, %d bytes at 0x%08lx\n",
2002 len
, (DWORD
) memaddr
));
2003 if (!WriteProcessMemory (current_process_handle
, (LPVOID
) memaddr
, our
,
2006 FlushInstructionCache (current_process_handle
, (LPCVOID
) memaddr
, len
);
2010 DEBUG_MEM (("gdb: read target memory, %d bytes at 0x%08lx\n",
2011 len
, (DWORD
) memaddr
));
2012 if (!ReadProcessMemory (current_process_handle
, (LPCVOID
) memaddr
, our
,
2020 win32_kill_inferior (void)
2022 CHECK (TerminateProcess (current_process_handle
, 0));
2026 if (!win32_continue (DBG_CONTINUE
, -1))
2028 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
2030 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
2034 CHECK (CloseHandle (current_process_handle
));
2036 /* this may fail in an attached process so don't check. */
2037 if (current_thread
&& current_thread
->h
)
2038 (void) CloseHandle (current_thread
->h
);
2039 target_mourn_inferior (); /* or just win32_mourn_inferior? */
2043 win32_prepare_to_store (struct regcache
*regcache
)
2045 /* Do nothing, since we can store individual regs */
2049 win32_can_run (void)
2057 DEBUG_EVENTS (("gdb: win32_close, inferior_ptid=%d\n",
2058 PIDGET (inferior_ptid
)));
2061 /* Convert pid to printable format. */
2063 cygwin_pid_to_str (ptid_t ptid
)
2065 static char buf
[80];
2066 int pid
= PIDGET (ptid
);
2068 if ((DWORD
) pid
== current_event
.dwProcessId
)
2069 sprintf (buf
, "process %d", pid
);
2071 sprintf (buf
, "thread %ld.0x%x", current_event
.dwProcessId
, pid
);
2077 struct target_ops
*target
;
2079 } map_code_section_args
;
2082 map_single_dll_code_section (bfd
*abfd
, asection
*sect
, void *obj
)
2086 struct section_table
*new_target_sect_ptr
;
2088 map_code_section_args
*args
= (map_code_section_args
*) obj
;
2089 struct target_ops
*target
= args
->target
;
2090 if (sect
->flags
& SEC_CODE
)
2092 update_coreops
= core_ops
.to_sections
== target
->to_sections
;
2094 if (target
->to_sections
)
2096 old
= target
->to_sections_end
- target
->to_sections
;
2097 target
->to_sections
= (struct section_table
*)
2098 xrealloc ((char *) target
->to_sections
,
2099 (sizeof (struct section_table
)) * (1 + old
));
2104 target
->to_sections
= (struct section_table
*)
2105 xmalloc ((sizeof (struct section_table
)));
2107 target
->to_sections_end
= target
->to_sections
+ (1 + old
);
2109 /* Update the to_sections field in the core_ops structure
2113 core_ops
.to_sections
= target
->to_sections
;
2114 core_ops
.to_sections_end
= target
->to_sections_end
;
2116 new_target_sect_ptr
= target
->to_sections
+ old
;
2117 new_target_sect_ptr
->addr
= args
->addr
+ bfd_section_vma (abfd
, sect
);
2118 new_target_sect_ptr
->endaddr
= args
->addr
+ bfd_section_vma (abfd
, sect
) +
2119 bfd_section_size (abfd
, sect
);;
2120 new_target_sect_ptr
->the_bfd_section
= sect
;
2121 new_target_sect_ptr
->bfd
= abfd
;
2126 dll_code_sections_add (const char *dll_name
, int base_addr
, struct target_ops
*target
)
2129 map_code_section_args map_args
;
2130 asection
*lowest_sect
;
2132 if (dll_name
== NULL
|| target
== NULL
)
2134 name
= xstrdup (dll_name
);
2135 dll_bfd
= bfd_openr (name
, "pei-i386");
2136 if (dll_bfd
== NULL
)
2139 if (bfd_check_format (dll_bfd
, bfd_object
))
2141 lowest_sect
= bfd_get_section_by_name (dll_bfd
, ".text");
2142 if (lowest_sect
== NULL
)
2144 map_args
.target
= target
;
2145 map_args
.addr
= base_addr
- bfd_section_vma (dll_bfd
, lowest_sect
);
2147 bfd_map_over_sections (dll_bfd
, &map_single_dll_code_section
, (void *) (&map_args
));
2154 core_section_load_dll_symbols (bfd
*abfd
, asection
*sect
, void *obj
)
2156 struct target_ops
*target
= (struct target_ops
*) obj
;
2161 struct win32_pstatus
*pstatus
;
2166 struct objfile
*objfile
;
2167 const char *dll_basename
;
2169 if (strncmp (sect
->name
, ".module", 7) != 0)
2172 buf
= (char *) xmalloc (bfd_get_section_size (sect
) + 1);
2175 printf_unfiltered ("memory allocation failed for %s\n", sect
->name
);
2178 if (!bfd_get_section_contents (abfd
, sect
, buf
, 0, bfd_get_section_size (sect
)))
2181 pstatus
= (struct win32_pstatus
*) buf
;
2183 memmove (&base_addr
, &(pstatus
->data
.module_info
.base_address
), sizeof (base_addr
));
2184 dll_name_size
= pstatus
->data
.module_info
.module_name_size
;
2185 if (offsetof (struct win32_pstatus
, data
.module_info
.module_name
) + dll_name_size
> bfd_get_section_size (sect
))
2188 dll_name
= pstatus
->data
.module_info
.module_name
;
2190 if (!(dll_basename
= strrchr (dll_name
, '/')))
2191 dll_basename
= dll_name
;
2195 ALL_OBJFILES (objfile
)
2197 char *objfile_basename
= strrchr (objfile
->name
, '/');
2199 if (objfile_basename
&&
2200 strcasecmp (dll_basename
, objfile_basename
+ 1) == 0)
2204 base_addr
+= 0x1000;
2205 dll_name
= register_loaded_dll (dll_name
, base_addr
, 1);
2207 if (!dll_code_sections_add (dll_name
, (DWORD
) base_addr
, target
))
2208 printf_unfiltered ("%s: Failed to map dll code sections.\n", dll_name
);
2216 static struct so_list
*
2217 win32_current_sos (void)
2219 struct so_list
*sop
;
2220 struct so_list
*start
= NULL
;
2221 struct so_list
*last
= NULL
;
2223 if (!solib_start
.next
&& core_bfd
)
2225 win32_clear_solib ();
2226 bfd_map_over_sections (core_bfd
, &core_section_load_dll_symbols
,
2230 for (sop
= solib_start
.next
; sop
; sop
= sop
->next
)
2232 struct so_list
*new = XZALLOC (struct so_list
);
2233 strcpy (new->so_name
, sop
->so_name
);
2234 strcpy (new->so_original_name
, sop
->so_original_name
);
2248 fetch_elf_core_registers (struct regcache
*regcache
,
2249 char *core_reg_sect
,
2250 unsigned core_reg_size
,
2255 if (core_reg_size
< sizeof (CONTEXT
))
2257 error (_("Core file register section too small (%u bytes)."), core_reg_size
);
2260 for (r
= 0; r
< gdbarch_num_regs (current_gdbarch
); r
++)
2261 regcache_raw_supply (regcache
, r
, core_reg_sect
+ mappings
[r
]);
2265 open_symbol_file_object (void *from_ttyp
)
2271 in_dynsym_resolve_code (CORE_ADDR pc
)
2277 init_win32_ops (void)
2279 win32_ops
.to_shortname
= "child";
2280 win32_ops
.to_longname
= "Win32 child process";
2281 win32_ops
.to_doc
= "Win32 child process (started by the \"run\" command).";
2282 win32_ops
.to_open
= win32_open
;
2283 win32_ops
.to_close
= win32_close
;
2284 win32_ops
.to_attach
= win32_attach
;
2285 win32_ops
.to_detach
= win32_detach
;
2286 win32_ops
.to_resume
= win32_resume
;
2287 win32_ops
.to_wait
= win32_wait
;
2288 win32_ops
.to_fetch_registers
= win32_fetch_inferior_registers
;
2289 win32_ops
.to_store_registers
= win32_store_inferior_registers
;
2290 win32_ops
.to_prepare_to_store
= win32_prepare_to_store
;
2291 win32_ops
.deprecated_xfer_memory
= win32_xfer_memory
;
2292 win32_ops
.to_files_info
= win32_files_info
;
2293 win32_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
2294 win32_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
2295 win32_ops
.to_terminal_init
= terminal_init_inferior
;
2296 win32_ops
.to_terminal_inferior
= terminal_inferior
;
2297 win32_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
2298 win32_ops
.to_terminal_ours
= terminal_ours
;
2299 win32_ops
.to_terminal_save_ours
= terminal_save_ours
;
2300 win32_ops
.to_terminal_info
= child_terminal_info
;
2301 win32_ops
.to_kill
= win32_kill_inferior
;
2302 win32_ops
.to_create_inferior
= win32_create_inferior
;
2303 win32_ops
.to_mourn_inferior
= win32_mourn_inferior
;
2304 win32_ops
.to_can_run
= win32_can_run
;
2305 win32_ops
.to_thread_alive
= win32_win32_thread_alive
;
2306 win32_ops
.to_pid_to_str
= cygwin_pid_to_str
;
2307 win32_ops
.to_stop
= win32_stop
;
2308 win32_ops
.to_stratum
= process_stratum
;
2309 win32_ops
.to_has_all_memory
= 1;
2310 win32_ops
.to_has_memory
= 1;
2311 win32_ops
.to_has_stack
= 1;
2312 win32_ops
.to_has_registers
= 1;
2313 win32_ops
.to_has_execution
= 1;
2314 win32_ops
.to_magic
= OPS_MAGIC
;
2315 win32_ops
.to_pid_to_exec_file
= win32_pid_to_exec_file
;
2317 win32_so_ops
.relocate_section_addresses
= win32_relocate_section_addresses
;
2318 win32_so_ops
.free_so
= win32_free_so
;
2319 win32_so_ops
.clear_solib
= win32_clear_solib
;
2320 win32_so_ops
.solib_create_inferior_hook
= win32_solib_create_inferior_hook
;
2321 win32_so_ops
.special_symbol_handling
= win32_special_symbol_handling
;
2322 win32_so_ops
.current_sos
= win32_current_sos
;
2323 win32_so_ops
.open_symbol_file_object
= open_symbol_file_object
;
2324 win32_so_ops
.in_dynsym_resolve_code
= in_dynsym_resolve_code
;
2326 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
2327 current_target_so_ops
= &win32_so_ops
;
2331 set_win32_aliases (char *argv0
)
2333 add_info_alias ("dll", "sharedlibrary", 1);
2337 _initialize_win32_nat (void)
2339 struct cmd_list_element
*c
;
2343 c
= add_com ("dll-symbols", class_files
, dll_symbol_command
,
2344 _("Load dll library symbols from FILE."));
2345 set_cmd_completer (c
, filename_completer
);
2347 add_com_alias ("sharedlibrary", "dll-symbols", class_alias
, 1);
2349 add_setshow_boolean_cmd ("shell", class_support
, &useshell
, _("\
2350 Set use of shell to start subprocess."), _("\
2351 Show use of shell to start subprocess."), NULL
,
2353 NULL
, /* FIXME: i18n: */
2354 &setlist
, &showlist
);
2356 add_setshow_boolean_cmd ("cygwin-exceptions", class_support
, &cygwin_exceptions
, _("\
2357 Break when an exception is detected in the Cygwin DLL itself."), _("\
2358 Show whether gdb breaks on exceptions in the Cygwin DLL itself."), NULL
,
2360 NULL
, /* FIXME: i18n: */
2361 &setlist
, &showlist
);
2363 add_setshow_boolean_cmd ("new-console", class_support
, &new_console
, _("\
2364 Set creation of new console when creating child process."), _("\
2365 Show creation of new console when creating child process."), NULL
,
2367 NULL
, /* FIXME: i18n: */
2368 &setlist
, &showlist
);
2370 add_setshow_boolean_cmd ("new-group", class_support
, &new_group
, _("\
2371 Set creation of new group when creating child process."), _("\
2372 Show creation of new group when creating child process."), NULL
,
2374 NULL
, /* FIXME: i18n: */
2375 &setlist
, &showlist
);
2377 add_setshow_boolean_cmd ("debugexec", class_support
, &debug_exec
, _("\
2378 Set whether to display execution in child process."), _("\
2379 Show whether to display execution in child process."), NULL
,
2381 NULL
, /* FIXME: i18n: */
2382 &setlist
, &showlist
);
2384 add_setshow_boolean_cmd ("debugevents", class_support
, &debug_events
, _("\
2385 Set whether to display kernel events in child process."), _("\
2386 Show whether to display kernel events in child process."), NULL
,
2388 NULL
, /* FIXME: i18n: */
2389 &setlist
, &showlist
);
2391 add_setshow_boolean_cmd ("debugmemory", class_support
, &debug_memory
, _("\
2392 Set whether to display memory accesses in child process."), _("\
2393 Show whether to display memory accesses in child process."), NULL
,
2395 NULL
, /* FIXME: i18n: */
2396 &setlist
, &showlist
);
2398 add_setshow_boolean_cmd ("debugexceptions", class_support
,
2399 &debug_exceptions
, _("\
2400 Set whether to display kernel exceptions in child process."), _("\
2401 Show whether to display kernel exceptions in child process."), NULL
,
2403 NULL
, /* FIXME: i18n: */
2404 &setlist
, &showlist
);
2406 add_prefix_cmd ("w32", class_info
, info_w32_command
,
2407 _("Print information specific to Win32 debugging."),
2408 &info_w32_cmdlist
, "info w32 ", 0, &infolist
);
2410 add_cmd ("selector", class_info
, display_selectors
,
2411 _("Display selectors infos."),
2413 add_target (&win32_ops
);
2414 deprecated_init_ui_hook
= set_win32_aliases
;
2417 /* Hardware watchpoint support, adapted from go32-nat.c code. */
2419 /* Pass the address ADDR to the inferior in the I'th debug register.
2420 Here we just store the address in dr array, the registers will be
2421 actually set up when win32_continue is called. */
2423 cygwin_set_dr (int i
, CORE_ADDR addr
)
2426 internal_error (__FILE__
, __LINE__
,
2427 _("Invalid register %d in cygwin_set_dr.\n"), i
);
2428 dr
[i
] = (unsigned) addr
;
2429 debug_registers_changed
= 1;
2430 debug_registers_used
= 1;
2433 /* Pass the value VAL to the inferior in the DR7 debug control
2434 register. Here we just store the address in D_REGS, the watchpoint
2435 will be actually set up in win32_wait. */
2437 cygwin_set_dr7 (unsigned val
)
2440 debug_registers_changed
= 1;
2441 debug_registers_used
= 1;
2444 /* Get the value of the DR6 debug status register from the inferior.
2445 Here we just return the value stored in dr[6]
2446 by the last call to thread_rec for current_event.dwThreadId id. */
2448 cygwin_get_dr6 (void)
2453 /* Determine if the thread referenced by "pid" is alive
2454 by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0
2455 it means that the pid has died. Otherwise it is assumed to be alive. */
2457 win32_win32_thread_alive (ptid_t ptid
)
2459 int pid
= PIDGET (ptid
);
2461 return WaitForSingleObject (thread_rec (pid
, FALSE
)->h
, 0) == WAIT_OBJECT_0
?
2465 static struct core_fns win32_elf_core_fns
=
2467 bfd_target_elf_flavour
,
2468 default_check_format
,
2469 default_core_sniffer
,
2470 fetch_elf_core_registers
,
2475 _initialize_core_win32 (void)
2477 deprecated_add_core_fns (&win32_elf_core_fns
);
2481 _initialize_check_for_gdb_ini (void)
2484 if (inhibit_gdbinit
)
2487 homedir
= getenv ("HOME");
2491 char *oldini
= (char *) alloca (strlen (homedir
) +
2492 sizeof ("/gdb.ini"));
2493 strcpy (oldini
, homedir
);
2494 p
= strchr (oldini
, '\0');
2495 if (p
> oldini
&& p
[-1] != '/')
2497 strcpy (p
, "gdb.ini");
2498 if (access (oldini
, 0) == 0)
2500 int len
= strlen (oldini
);
2501 char *newini
= alloca (len
+ 1);
2502 sprintf (newini
, "%.*s.gdbinit",
2503 (int) (len
- (sizeof ("gdb.ini") - 1)), oldini
);
2504 warning (_("obsolete '%s' found. Rename to '%s'."), oldini
, newini
);