1 /* Target-vector operations for controlling win32 child processes, for GDB.
3 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 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., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, 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>
50 #include "gdb_string.h"
51 #include "gdbthread.h"
53 #include <sys/param.h>
58 #include "i386-tdep.h"
59 #include "i387-tdep.h"
61 static struct target_ops win32_ops
;
62 static struct target_so_ops win32_so_ops
;
64 /* If we're not using the old Cygwin header file set, define the
65 following which never should have been in the generic Win32 API
66 headers in the first place since they were our own invention... */
67 #ifndef _GNU_H_WINDOWS_H
70 FLAG_TRACE_BIT
= 0x100,
71 CONTEXT_DEBUGGER
= (CONTEXT_FULL
| CONTEXT_FLOATING_POINT
)
74 #include <sys/procfs.h>
77 #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS \
78 | CONTEXT_EXTENDED_REGISTERS
80 static unsigned dr
[8];
81 static int debug_registers_changed
;
82 static int debug_registers_used
;
84 /* The string sent by cygwin when it processes a signal.
85 FIXME: This should be in a cygwin include file. */
86 #define CYGWIN_SIGNAL_STRING "cygwin: signal"
88 #define CHECK(x) check (x, __FILE__,__LINE__)
89 #define DEBUG_EXEC(x) if (debug_exec) printf_unfiltered x
90 #define DEBUG_EVENTS(x) if (debug_events) printf_unfiltered x
91 #define DEBUG_MEM(x) if (debug_memory) printf_unfiltered x
92 #define DEBUG_EXCEPT(x) if (debug_exceptions) printf_unfiltered x
94 static void win32_stop (void);
95 static int win32_win32_thread_alive (ptid_t
);
96 static void win32_kill_inferior (void);
98 static enum target_signal last_sig
= TARGET_SIGNAL_0
;
99 /* Set if a signal was received from the debugged process */
101 /* Thread information structure used to track information that is
102 not available in gdb's thread structure. */
103 typedef struct thread_info_struct
105 struct thread_info_struct
*next
;
116 static thread_info thread_head
;
118 /* The process and thread handles for the above context. */
120 static DEBUG_EVENT current_event
; /* The current debug event from
122 static HANDLE current_process_handle
; /* Currently executing process */
123 static thread_info
*current_thread
; /* Info on currently selected thread */
124 static DWORD main_thread_id
; /* Thread ID of the main thread */
126 /* Counts of things. */
127 static int exception_count
= 0;
128 static int event_count
= 0;
129 static int saw_create
;
132 static int new_console
= 0;
133 static int new_group
= 1;
134 static int debug_exec
= 0; /* show execution */
135 static int debug_events
= 0; /* show events from kernel */
136 static int debug_memory
= 0; /* show target memory accesses */
137 static int debug_exceptions
= 0; /* show target exceptions */
138 static int useshell
= 0; /* use shell for subprocesses */
140 /* This vector maps GDB's idea of a register's number into an address
141 in the win32 exception context vector.
143 It also contains the bit mask needed to load the register in question.
145 One day we could read a reg, we could inspect the context we
146 already have loaded, if it doesn't have the bit set that we need,
147 we read that set of registers in using GetThreadContext. If the
148 context already contains what we need, we just unpack it. Then to
149 write a register, first we have to ensure that the context contains
150 the other regs of the group, and then we copy the info in and set
153 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
154 static const int mappings
[] =
156 context_offset (Eax
),
157 context_offset (Ecx
),
158 context_offset (Edx
),
159 context_offset (Ebx
),
160 context_offset (Esp
),
161 context_offset (Ebp
),
162 context_offset (Esi
),
163 context_offset (Edi
),
164 context_offset (Eip
),
165 context_offset (EFlags
),
166 context_offset (SegCs
),
167 context_offset (SegSs
),
168 context_offset (SegDs
),
169 context_offset (SegEs
),
170 context_offset (SegFs
),
171 context_offset (SegGs
),
172 context_offset (FloatSave
.RegisterArea
[0 * 10]),
173 context_offset (FloatSave
.RegisterArea
[1 * 10]),
174 context_offset (FloatSave
.RegisterArea
[2 * 10]),
175 context_offset (FloatSave
.RegisterArea
[3 * 10]),
176 context_offset (FloatSave
.RegisterArea
[4 * 10]),
177 context_offset (FloatSave
.RegisterArea
[5 * 10]),
178 context_offset (FloatSave
.RegisterArea
[6 * 10]),
179 context_offset (FloatSave
.RegisterArea
[7 * 10]),
180 context_offset (FloatSave
.ControlWord
),
181 context_offset (FloatSave
.StatusWord
),
182 context_offset (FloatSave
.TagWord
),
183 context_offset (FloatSave
.ErrorSelector
),
184 context_offset (FloatSave
.ErrorOffset
),
185 context_offset (FloatSave
.DataSelector
),
186 context_offset (FloatSave
.DataOffset
),
187 context_offset (FloatSave
.ErrorSelector
)
189 context_offset (ExtendedRegisters
[10*16]),
190 context_offset (ExtendedRegisters
[11*16]),
191 context_offset (ExtendedRegisters
[12*16]),
192 context_offset (ExtendedRegisters
[13*16]),
193 context_offset (ExtendedRegisters
[14*16]),
194 context_offset (ExtendedRegisters
[15*16]),
195 context_offset (ExtendedRegisters
[16*16]),
196 context_offset (ExtendedRegisters
[17*16]),
198 context_offset (ExtendedRegisters
[24])
201 #undef context_offset
203 /* This vector maps the target's idea of an exception (extracted
204 from the DEBUG_EVENT structure) to GDB's idea. */
206 struct xlate_exception
209 enum target_signal us
;
212 static const struct xlate_exception
215 {EXCEPTION_ACCESS_VIOLATION
, TARGET_SIGNAL_SEGV
},
216 {STATUS_STACK_OVERFLOW
, TARGET_SIGNAL_SEGV
},
217 {EXCEPTION_BREAKPOINT
, TARGET_SIGNAL_TRAP
},
218 {DBG_CONTROL_C
, TARGET_SIGNAL_INT
},
219 {EXCEPTION_SINGLE_STEP
, TARGET_SIGNAL_TRAP
},
220 {STATUS_FLOAT_DIVIDE_BY_ZERO
, TARGET_SIGNAL_FPE
},
224 check (BOOL ok
, const char *file
, int line
)
227 printf_filtered ("error return %s:%d was %lu\n", file
, line
,
231 /* Find a thread record given a thread id.
232 If get_context then also retrieve the context for this
235 thread_rec (DWORD id
, int get_context
)
239 for (th
= &thread_head
; (th
= th
->next
) != NULL
;)
242 if (!th
->suspend_count
&& get_context
)
244 if (get_context
> 0 && id
!= current_event
.dwThreadId
)
245 th
->suspend_count
= SuspendThread (th
->h
) + 1;
246 else if (get_context
< 0)
247 th
->suspend_count
= -1;
248 th
->reload_context
= 1;
256 /* Add a thread to the thread list */
258 win32_add_thread (DWORD id
, HANDLE h
)
262 if ((th
= thread_rec (id
, FALSE
)))
265 th
= (thread_info
*) xmalloc (sizeof (*th
));
266 memset (th
, 0, sizeof (*th
));
269 th
->next
= thread_head
.next
;
270 thread_head
.next
= th
;
271 add_thread (pid_to_ptid (id
));
272 /* Set the debug registers for the new thread in they are used. */
273 if (debug_registers_used
)
275 /* Only change the value of the debug registers. */
276 th
->context
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
277 CHECK (GetThreadContext (th
->h
, &th
->context
));
278 th
->context
.Dr0
= dr
[0];
279 th
->context
.Dr1
= dr
[1];
280 th
->context
.Dr2
= dr
[2];
281 th
->context
.Dr3
= dr
[3];
282 /* th->context.Dr6 = dr[6];
283 FIXME: should we set dr6 also ?? */
284 th
->context
.Dr7
= dr
[7];
285 CHECK (SetThreadContext (th
->h
, &th
->context
));
286 th
->context
.ContextFlags
= 0;
291 /* Clear out any old thread list and reintialize it to a
294 win32_init_thread_list (void)
296 thread_info
*th
= &thread_head
;
298 DEBUG_EVENTS (("gdb: win32_init_thread_list\n"));
300 while (th
->next
!= NULL
)
302 thread_info
*here
= th
->next
;
303 th
->next
= here
->next
;
304 (void) CloseHandle (here
->h
);
307 thread_head
.next
= NULL
;
310 /* Delete a thread from the list of threads */
312 win32_delete_thread (DWORD id
)
317 printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (pid_to_ptid (id
)));
318 delete_thread (pid_to_ptid (id
));
320 for (th
= &thread_head
;
321 th
->next
!= NULL
&& th
->next
->id
!= id
;
325 if (th
->next
!= NULL
)
327 thread_info
*here
= th
->next
;
328 th
->next
= here
->next
;
329 CloseHandle (here
->h
);
335 do_win32_fetch_inferior_registers (int r
)
337 char *context_offset
= ((char *) ¤t_thread
->context
) + mappings
[r
];
341 return; /* Windows sometimes uses a non-existent thread id in its
344 if (current_thread
->reload_context
)
346 thread_info
*th
= current_thread
;
347 th
->context
.ContextFlags
= CONTEXT_DEBUGGER_DR
;
348 GetThreadContext (th
->h
, &th
->context
);
349 /* Copy dr values from that thread. */
350 dr
[0] = th
->context
.Dr0
;
351 dr
[1] = th
->context
.Dr1
;
352 dr
[2] = th
->context
.Dr2
;
353 dr
[3] = th
->context
.Dr3
;
354 dr
[6] = th
->context
.Dr6
;
355 dr
[7] = th
->context
.Dr7
;
356 current_thread
->reload_context
= 0;
359 #define I387_ST0_REGNUM I386_ST0_REGNUM
361 if (r
== I387_FISEG_REGNUM
)
363 l
= *((long *) context_offset
) & 0xffff;
364 regcache_raw_supply (current_regcache
, r
, (char *) &l
);
366 else if (r
== I387_FOP_REGNUM
)
368 l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
369 regcache_raw_supply (current_regcache
, r
, (char *) &l
);
372 regcache_raw_supply (current_regcache
, r
, context_offset
);
375 for (r
= 0; r
< NUM_REGS
; r
++)
376 do_win32_fetch_inferior_registers (r
);
379 #undef I387_ST0_REGNUM
383 win32_fetch_inferior_registers (int r
)
385 current_thread
= thread_rec (PIDGET (inferior_ptid
), TRUE
);
386 /* Check if current_thread exists. Windows sometimes uses a non-existent
387 thread id in its events */
389 do_win32_fetch_inferior_registers (r
);
393 do_win32_store_inferior_registers (int r
)
396 /* Windows sometimes uses a non-existent thread id in its events */;
398 regcache_raw_collect (current_regcache
, r
,
399 ((char *) ¤t_thread
->context
) + mappings
[r
]);
402 for (r
= 0; r
< NUM_REGS
; r
++)
403 do_win32_store_inferior_registers (r
);
407 /* Store a new register value into the current thread context */
409 win32_store_inferior_registers (int r
)
411 current_thread
= thread_rec (PIDGET (inferior_ptid
), TRUE
);
412 /* Check if current_thread exists. Windows sometimes uses a non-existent
413 thread id in its events */
415 do_win32_store_inferior_registers (r
);
418 static int psapi_loaded
= 0;
419 static HMODULE psapi_module_handle
= NULL
;
420 static BOOL
WINAPI (*psapi_EnumProcessModules
) (HANDLE
, HMODULE
*, DWORD
, LPDWORD
) = NULL
;
421 static BOOL
WINAPI (*psapi_GetModuleInformation
) (HANDLE
, HMODULE
, LPMODULEINFO
, DWORD
) = NULL
;
422 static DWORD
WINAPI (*psapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
, LPSTR
, DWORD
) = NULL
;
425 psapi_get_dll_name (DWORD BaseAddress
, char *dll_name_ret
)
431 HMODULE
*DllHandle
= dh_buf
;
436 psapi_EnumProcessModules
== NULL
||
437 psapi_GetModuleInformation
== NULL
||
438 psapi_GetModuleFileNameExA
== NULL
)
443 psapi_module_handle
= LoadLibrary ("psapi.dll");
444 if (!psapi_module_handle
)
446 /* printf_unfiltered ("error loading psapi.dll: %u", GetLastError ()); */
449 psapi_EnumProcessModules
= GetProcAddress (psapi_module_handle
, "EnumProcessModules");
450 psapi_GetModuleInformation
= GetProcAddress (psapi_module_handle
, "GetModuleInformation");
451 psapi_GetModuleFileNameExA
= (void *) GetProcAddress (psapi_module_handle
,
452 "GetModuleFileNameExA");
453 if (psapi_EnumProcessModules
== NULL
||
454 psapi_GetModuleInformation
== NULL
||
455 psapi_GetModuleFileNameExA
== NULL
)
460 ok
= (*psapi_EnumProcessModules
) (current_process_handle
,
465 if (!ok
|| !cbNeeded
)
468 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
472 ok
= (*psapi_EnumProcessModules
) (current_process_handle
,
479 for (i
= 0; i
< (int) (cbNeeded
/ sizeof (HMODULE
)); i
++)
481 if (!(*psapi_GetModuleInformation
) (current_process_handle
,
485 error (_("Can't get module info"));
487 len
= (*psapi_GetModuleFileNameExA
) (current_process_handle
,
492 error (_("Error getting dll name: %u."), (unsigned) GetLastError ());
494 if ((DWORD
) (mi
.lpBaseOfDll
) == BaseAddress
)
499 dll_name_ret
[0] = '\0';
503 /* Encapsulate the information required in a call to
504 symbol_file_add_args */
505 struct safe_symbol_file_add_args
509 struct section_addr_info
*addrs
;
512 struct ui_file
*err
, *out
;
517 /* Maintain a linked list of "so" information. */
524 static struct so_list solib_start
, *solib_end
;
526 /* Call symbol_file_add with stderr redirected. We don't care if there
529 safe_symbol_file_add_stub (void *argv
)
531 #define p ((struct safe_symbol_file_add_args *) argv)
532 struct so_list
*so
= &solib_start
;
534 p
->ret
= symbol_file_add (p
->name
, p
->from_tty
, p
->addrs
, p
->mainline
, p
->flags
);
539 /* Restore gdb's stderr after calling symbol_file_add */
541 safe_symbol_file_add_cleanup (void *p
)
543 #define sp ((struct safe_symbol_file_add_args *)p)
544 gdb_flush (gdb_stderr
);
545 gdb_flush (gdb_stdout
);
546 ui_file_delete (gdb_stderr
);
547 ui_file_delete (gdb_stdout
);
548 gdb_stderr
= sp
->err
;
549 gdb_stdout
= sp
->out
;
553 /* symbol_file_add wrapper that prevents errors from being displayed. */
554 static struct objfile
*
555 safe_symbol_file_add (char *name
, int from_tty
,
556 struct section_addr_info
*addrs
,
557 int mainline
, int flags
)
559 struct safe_symbol_file_add_args p
;
560 struct cleanup
*cleanup
;
562 cleanup
= make_cleanup (safe_symbol_file_add_cleanup
, &p
);
566 gdb_flush (gdb_stderr
);
567 gdb_flush (gdb_stdout
);
568 gdb_stderr
= ui_file_new ();
569 gdb_stdout
= ui_file_new ();
571 p
.from_tty
= from_tty
;
573 p
.mainline
= mainline
;
575 catch_errors (safe_symbol_file_add_stub
, &p
, "", RETURN_MASK_ERROR
);
577 do_cleanups (cleanup
);
581 /* Remember the maximum DLL length for printing in info dll command. */
582 static int max_dll_name_len
;
585 register_loaded_dll (const char *name
, DWORD load_addr
)
588 char ppath
[MAX_PATH
+ 1];
589 char buf
[MAX_PATH
+ 1];
590 char cwd
[MAX_PATH
+ 1];
592 WIN32_FIND_DATA w32_fd
;
593 HANDLE h
= FindFirstFile(name
, &w32_fd
);
594 MEMORY_BASIC_INFORMATION m
;
597 if (h
== INVALID_HANDLE_VALUE
)
603 if (GetCurrentDirectory (MAX_PATH
+ 1, cwd
))
605 p
= strrchr (buf
, '\\');
608 SetCurrentDirectory (buf
);
609 GetFullPathName (w32_fd
.cFileName
, MAX_PATH
, buf
, &p
);
610 SetCurrentDirectory (cwd
);
614 if (strcasecmp (buf
, "ntdll.dll") == 0)
616 GetSystemDirectory (buf
, sizeof (buf
));
617 strcat (buf
, "\\ntdll.dll");
619 cygwin_conv_to_posix_path (buf
, ppath
);
620 so
= (struct so_list
*) xmalloc (sizeof (struct so_list
) + strlen (ppath
) + 8 + 1);
621 memset (so
, 0, sizeof (*so
));
622 so
->lm_info
= (struct lm_info
*) xmalloc (sizeof (struct lm_info
));
623 so
->lm_info
->load_addr
= load_addr
;
624 if (VirtualQueryEx (current_process_handle
, (void *) load_addr
, &m
,
626 so
->lm_info
->end_addr
= (DWORD
) m
.AllocationBase
+ m
.RegionSize
;
628 so
->lm_info
->end_addr
= load_addr
+ 0x2000; /* completely arbitrary */
631 strcpy (so
->so_name
, ppath
);
633 solib_end
->next
= so
;
635 len
= strlen (ppath
);
636 if (len
> max_dll_name_len
)
637 max_dll_name_len
= len
;
641 get_image_name (HANDLE h
, void *address
, int unicode
)
643 static char buf
[(2 * MAX_PATH
) + 1];
644 DWORD size
= unicode
? sizeof (WCHAR
) : sizeof (char);
650 /* Attempt to read the name of the dll that was detected.
651 This is documented to work only when actively debugging
652 a program. It will not work for attached processes. */
656 /* See if we could read the address of a string, and that the
657 address isn't null. */
658 if (!ReadProcessMemory (h
, address
, &address_ptr
, sizeof (address_ptr
), &done
)
659 || done
!= sizeof (address_ptr
) || !address_ptr
)
662 /* Find the length of the string */
663 while (ReadProcessMemory (h
, address_ptr
+ len
++ * size
, &b
, size
, &done
)
664 && (b
[0] != 0 || b
[size
- 1] != 0) && done
== size
)
668 ReadProcessMemory (h
, address_ptr
, buf
, len
, &done
);
671 WCHAR
*unicode_address
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
672 ReadProcessMemory (h
, address_ptr
, unicode_address
, len
* sizeof (WCHAR
),
675 WideCharToMultiByte (CP_ACP
, 0, unicode_address
, len
, buf
, len
, 0, 0);
681 /* Wait for child to do something. Return pid of child, or -1 in case
682 of error; store status through argument pointer OURSTATUS. */
684 handle_load_dll (void *dummy
)
686 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
687 char dll_buf
[MAX_PATH
+ 1];
688 char *dll_name
= NULL
;
691 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
693 if (!psapi_get_dll_name ((DWORD
) (event
->lpBaseOfDll
), dll_buf
))
694 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
698 if (*dll_name
== '\0')
699 dll_name
= get_image_name (current_process_handle
, event
->lpImageName
, event
->fUnicode
);
703 register_loaded_dll (dll_name
, (DWORD
) event
->lpBaseOfDll
+ 0x1000);
709 win32_free_so (struct so_list
*so
)
712 free_objfile (so
->objfile
);
717 static struct so_list
*
718 win32_current_sos (void)
720 return solib_start
.next
;
724 win32_relocate_section_addresses (struct so_list
*so
,
725 struct section_table
*sec
)
732 handle_unload_dll (void *dummy
)
734 DWORD lpBaseOfDll
= (DWORD
) current_event
.u
.UnloadDll
.lpBaseOfDll
+ 0x1000;
737 for (so
= &solib_start
; so
->next
!= NULL
; so
= so
->next
)
738 if (so
->next
->lm_info
->load_addr
== lpBaseOfDll
)
740 struct so_list
*sodel
= so
->next
;
741 so
->next
= sodel
->next
;
747 error (_("Error: dll starting at 0x%lx not found."), (DWORD
) lpBaseOfDll
);
752 /* Clear list of loaded DLLs. */
754 win32_clear_solib (void)
756 struct so_list
*so
, *so1
= solib_start
.next
;
758 solib_start
.next
= NULL
;
759 solib_end
= &solib_start
;
760 max_dll_name_len
= sizeof ("DLL Name") - 1;
763 /* Load DLL symbol info. */
765 dll_symbol_command (char *args
, int from_tty
)
771 error (_("dll-symbols requires a file name"));
774 if (n
> 4 && strcasecmp (args
+ n
- 4, ".dll") != 0)
776 char *newargs
= (char *) alloca (n
+ 4 + 1);
777 strcpy (newargs
, args
);
778 strcat (newargs
, ".dll");
782 safe_symbol_file_add (args
, from_tty
, NULL
, 0, OBJF_SHARED
| OBJF_USERLOADED
);
785 /* List currently loaded DLLs. */
787 info_dll_command (char *ignore
, int from_tty
)
789 struct so_list
*so
= &solib_start
;
794 printf_filtered ("%*s Load Address\n", -max_dll_name_len
, "DLL Name");
795 while ((so
= so
->next
) != NULL
)
796 printf_filtered ("%*s %08lx\n", -max_dll_name_len
, so
->so_name
, so
->lm_info
->load_addr
);
801 /* Handle DEBUG_STRING output from child process.
802 Cygwin prepends its messages with a "cygwin:". Interpret this as
803 a Cygwin signal. Otherwise just print the string as a warning. */
805 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
810 if (!target_read_string
811 ((CORE_ADDR
) current_event
.u
.DebugString
.lpDebugStringData
, &s
, 1024, 0)
815 if (strncmp (s
, CYGWIN_SIGNAL_STRING
, sizeof (CYGWIN_SIGNAL_STRING
) - 1) != 0)
817 if (strncmp (s
, "cYg", 3) != 0)
823 int sig
= strtol (s
+ sizeof (CYGWIN_SIGNAL_STRING
) - 1, &p
, 0);
824 gotasig
= target_signal_from_host (sig
);
825 ourstatus
->value
.sig
= gotasig
;
827 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
835 display_selector (HANDLE thread
, DWORD sel
)
838 if (GetThreadSelectorEntry (thread
, sel
, &info
))
841 printf_filtered ("0x%03lx: ", sel
);
842 if (!info
.HighWord
.Bits
.Pres
)
844 puts_filtered ("Segment not present\n");
847 base
= (info
.HighWord
.Bits
.BaseHi
<< 24) +
848 (info
.HighWord
.Bits
.BaseMid
<< 16)
850 limit
= (info
.HighWord
.Bits
.LimitHi
<< 16) + info
.LimitLow
;
851 if (info
.HighWord
.Bits
.Granularity
)
852 limit
= (limit
<< 12) | 0xfff;
853 printf_filtered ("base=0x%08x limit=0x%08x", base
, limit
);
854 if (info
.HighWord
.Bits
.Default_Big
)
855 puts_filtered(" 32-bit ");
857 puts_filtered(" 16-bit ");
858 switch ((info
.HighWord
.Bits
.Type
& 0xf) >> 1)
861 puts_filtered ("Data (Read-Only, Exp-up");
864 puts_filtered ("Data (Read/Write, Exp-up");
867 puts_filtered ("Unused segment (");
870 puts_filtered ("Data (Read/Write, Exp-down");
873 puts_filtered ("Code (Exec-Only, N.Conf");
876 puts_filtered ("Code (Exec/Read, N.Conf");
879 puts_filtered ("Code (Exec-Only, Conf");
882 puts_filtered ("Code (Exec/Read, Conf");
885 printf_filtered ("Unknown type 0x%x",info
.HighWord
.Bits
.Type
);
887 if ((info
.HighWord
.Bits
.Type
& 0x1) == 0)
888 puts_filtered(", N.Acc");
889 puts_filtered (")\n");
890 if ((info
.HighWord
.Bits
.Type
& 0x10) == 0)
891 puts_filtered("System selector ");
892 printf_filtered ("Priviledge level = %d. ", info
.HighWord
.Bits
.Dpl
);
893 if (info
.HighWord
.Bits
.Granularity
)
894 puts_filtered ("Page granular.\n");
896 puts_filtered ("Byte granular.\n");
901 printf_filtered ("Invalid selector 0x%lx.\n",sel
);
907 display_selectors (char * args
, int from_tty
)
911 puts_filtered ("Impossible to display selectors now.\n");
917 puts_filtered ("Selector $cs\n");
918 display_selector (current_thread
->h
,
919 current_thread
->context
.SegCs
);
920 puts_filtered ("Selector $ds\n");
921 display_selector (current_thread
->h
,
922 current_thread
->context
.SegDs
);
923 puts_filtered ("Selector $es\n");
924 display_selector (current_thread
->h
,
925 current_thread
->context
.SegEs
);
926 puts_filtered ("Selector $ss\n");
927 display_selector (current_thread
->h
,
928 current_thread
->context
.SegSs
);
929 puts_filtered ("Selector $fs\n");
930 display_selector (current_thread
->h
,
931 current_thread
->context
.SegFs
);
932 puts_filtered ("Selector $gs\n");
933 display_selector (current_thread
->h
,
934 current_thread
->context
.SegGs
);
939 sel
= parse_and_eval_long (args
);
940 printf_filtered ("Selector \"%s\"\n",args
);
941 display_selector (current_thread
->h
, sel
);
945 static struct cmd_list_element
*info_w32_cmdlist
= NULL
;
948 info_w32_command (char *args
, int from_tty
)
950 help_list (info_w32_cmdlist
, "info w32 ", class_info
, gdb_stdout
);
954 #define DEBUG_EXCEPTION_SIMPLE(x) if (debug_exceptions) \
955 printf_unfiltered ("gdb: Target exception %s at 0x%08lx\n", x, \
956 (DWORD) current_event.u.Exception.ExceptionRecord.ExceptionAddress)
959 handle_exception (struct target_waitstatus
*ourstatus
)
962 DWORD code
= current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
;
964 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
966 /* Record the context of the current thread */
967 th
= thread_rec (current_event
.dwThreadId
, -1);
971 case EXCEPTION_ACCESS_VIOLATION
:
972 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ACCESS_VIOLATION");
973 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
976 if (find_pc_partial_function ((CORE_ADDR
) current_event
.u
.Exception
977 .ExceptionRecord
.ExceptionAddress
,
979 && strncmp (fn
, "KERNEL32!IsBad", strlen ("KERNEL32!IsBad")) == 0)
983 case STATUS_STACK_OVERFLOW
:
984 DEBUG_EXCEPTION_SIMPLE ("STATUS_STACK_OVERFLOW");
985 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
987 case STATUS_FLOAT_DENORMAL_OPERAND
:
988 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DENORMAL_OPERAND");
989 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
991 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
992 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ARRAY_BOUNDS_EXCEEDED");
993 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
995 case STATUS_FLOAT_INEXACT_RESULT
:
996 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INEXACT_RESULT");
997 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
999 case STATUS_FLOAT_INVALID_OPERATION
:
1000 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INVALID_OPERATION");
1001 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1003 case STATUS_FLOAT_OVERFLOW
:
1004 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_OVERFLOW");
1005 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1007 case STATUS_FLOAT_STACK_CHECK
:
1008 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_STACK_CHECK");
1009 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1011 case STATUS_FLOAT_UNDERFLOW
:
1012 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_UNDERFLOW");
1013 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1015 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
1016 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DIVIDE_BY_ZERO");
1017 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1019 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
1020 DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_DIVIDE_BY_ZERO");
1021 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1023 case STATUS_INTEGER_OVERFLOW
:
1024 DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_OVERFLOW");
1025 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
1027 case EXCEPTION_BREAKPOINT
:
1028 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT");
1029 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1032 DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_C");
1033 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1035 case DBG_CONTROL_BREAK
:
1036 DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_BREAK");
1037 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
1039 case EXCEPTION_SINGLE_STEP
:
1040 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_SINGLE_STEP");
1041 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
1043 case EXCEPTION_ILLEGAL_INSTRUCTION
:
1044 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ILLEGAL_INSTRUCTION");
1045 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1047 case EXCEPTION_PRIV_INSTRUCTION
:
1048 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_PRIV_INSTRUCTION");
1049 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1051 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
1052 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION");
1053 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
1056 if (current_event
.u
.Exception
.dwFirstChance
)
1058 printf_unfiltered ("gdb: unknown target exception 0x%08lx at 0x%08lx\n",
1059 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
1060 (DWORD
) current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
);
1061 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
1065 last_sig
= ourstatus
->value
.sig
;
1069 /* Resume all artificially suspended threads if we are continuing
1072 win32_continue (DWORD continue_status
, int id
)
1078 DEBUG_EVENTS (("ContinueDebugEvent (cpid=%ld, ctid=%ld, %s);\n",
1079 current_event
.dwProcessId
, current_event
.dwThreadId
,
1080 continue_status
== DBG_CONTINUE
?
1081 "DBG_CONTINUE" : "DBG_EXCEPTION_NOT_HANDLED"));
1082 res
= ContinueDebugEvent (current_event
.dwProcessId
,
1083 current_event
.dwThreadId
,
1085 continue_status
= 0;
1087 for (th
= &thread_head
; (th
= th
->next
) != NULL
;)
1088 if (((id
== -1) || (id
== (int) th
->id
)) && th
->suspend_count
)
1091 for (i
= 0; i
< th
->suspend_count
; i
++)
1092 (void) ResumeThread (th
->h
);
1093 th
->suspend_count
= 0;
1094 if (debug_registers_changed
)
1096 /* Only change the value of the debug registers */
1097 th
->context
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1098 th
->context
.Dr0
= dr
[0];
1099 th
->context
.Dr1
= dr
[1];
1100 th
->context
.Dr2
= dr
[2];
1101 th
->context
.Dr3
= dr
[3];
1102 /* th->context.Dr6 = dr[6];
1103 FIXME: should we set dr6 also ?? */
1104 th
->context
.Dr7
= dr
[7];
1105 CHECK (SetThreadContext (th
->h
, &th
->context
));
1106 th
->context
.ContextFlags
= 0;
1110 debug_registers_changed
= 0;
1114 /* Called in pathological case where Windows fails to send a
1115 CREATE_PROCESS_DEBUG_EVENT after an attach. */
1117 fake_create_process (void)
1119 current_process_handle
= OpenProcess (PROCESS_ALL_ACCESS
, FALSE
,
1120 current_event
.dwProcessId
);
1121 main_thread_id
= current_event
.dwThreadId
;
1122 current_thread
= win32_add_thread (main_thread_id
,
1123 current_event
.u
.CreateThread
.hThread
);
1124 return main_thread_id
;
1127 /* Get the next event from the child. Return 1 if the event requires
1128 handling by WFI (or whatever).
1131 get_win32_debug_event (int pid
, struct target_waitstatus
*ourstatus
)
1134 DWORD continue_status
, event_code
;
1136 static thread_info dummy_thread_info
;
1139 last_sig
= TARGET_SIGNAL_0
;
1141 if (!(debug_event
= WaitForDebugEvent (¤t_event
, 1000)))
1145 continue_status
= DBG_CONTINUE
;
1147 event_code
= current_event
.dwDebugEventCode
;
1148 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1153 case CREATE_THREAD_DEBUG_EVENT
:
1154 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n",
1155 (unsigned) current_event
.dwProcessId
,
1156 (unsigned) current_event
.dwThreadId
,
1157 "CREATE_THREAD_DEBUG_EVENT"));
1158 if (saw_create
!= 1)
1160 if (!saw_create
&& attach_flag
)
1162 /* Kludge around a Windows bug where first event is a create
1163 thread event. Caused when attached process does not have
1165 retval
= ourstatus
->value
.related_pid
= fake_create_process ();
1170 /* Record the existence of this thread */
1171 th
= win32_add_thread (current_event
.dwThreadId
,
1172 current_event
.u
.CreateThread
.hThread
);
1174 printf_unfiltered ("[New %s]\n",
1176 pid_to_ptid (current_event
.dwThreadId
)));
1177 retval
= current_event
.dwThreadId
;
1180 case EXIT_THREAD_DEBUG_EVENT
:
1181 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1182 (unsigned) current_event
.dwProcessId
,
1183 (unsigned) current_event
.dwThreadId
,
1184 "EXIT_THREAD_DEBUG_EVENT"));
1185 if (current_event
.dwThreadId
!= main_thread_id
)
1187 win32_delete_thread (current_event
.dwThreadId
);
1188 th
= &dummy_thread_info
;
1192 case CREATE_PROCESS_DEBUG_EVENT
:
1193 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1194 (unsigned) current_event
.dwProcessId
,
1195 (unsigned) current_event
.dwThreadId
,
1196 "CREATE_PROCESS_DEBUG_EVENT"));
1197 CloseHandle (current_event
.u
.CreateProcessInfo
.hFile
);
1198 if (++saw_create
!= 1)
1200 CloseHandle (current_event
.u
.CreateProcessInfo
.hProcess
);
1204 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
1206 win32_delete_thread (main_thread_id
);
1207 main_thread_id
= current_event
.dwThreadId
;
1208 /* Add the main thread */
1209 th
= win32_add_thread (main_thread_id
,
1210 current_event
.u
.CreateProcessInfo
.hThread
);
1211 retval
= ourstatus
->value
.related_pid
= current_event
.dwThreadId
;
1214 case EXIT_PROCESS_DEBUG_EVENT
:
1215 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1216 (unsigned) current_event
.dwProcessId
,
1217 (unsigned) current_event
.dwThreadId
,
1218 "EXIT_PROCESS_DEBUG_EVENT"));
1219 if (saw_create
!= 1)
1221 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
1222 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
1223 CloseHandle (current_process_handle
);
1224 retval
= main_thread_id
;
1227 case LOAD_DLL_DEBUG_EVENT
:
1228 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1229 (unsigned) current_event
.dwProcessId
,
1230 (unsigned) current_event
.dwThreadId
,
1231 "LOAD_DLL_DEBUG_EVENT"));
1232 CloseHandle (current_event
.u
.LoadDll
.hFile
);
1233 if (saw_create
!= 1)
1235 catch_errors (handle_load_dll
, NULL
, (char *) "", RETURN_MASK_ALL
);
1236 registers_changed (); /* mark all regs invalid */
1237 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
1238 ourstatus
->value
.integer
= 0;
1239 retval
= main_thread_id
;
1240 re_enable_breakpoints_in_shlibs ();
1243 case UNLOAD_DLL_DEBUG_EVENT
:
1244 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1245 (unsigned) current_event
.dwProcessId
,
1246 (unsigned) current_event
.dwThreadId
,
1247 "UNLOAD_DLL_DEBUG_EVENT"));
1248 if (saw_create
!= 1)
1250 catch_errors (handle_unload_dll
, NULL
, (char *) "", RETURN_MASK_ALL
);
1251 registers_changed (); /* mark all regs invalid */
1252 /* ourstatus->kind = TARGET_WAITKIND_UNLOADED;
1253 does not exist yet. */
1256 case EXCEPTION_DEBUG_EVENT
:
1257 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1258 (unsigned) current_event
.dwProcessId
,
1259 (unsigned) current_event
.dwThreadId
,
1260 "EXCEPTION_DEBUG_EVENT"));
1261 if (saw_create
!= 1)
1263 if (handle_exception (ourstatus
))
1264 retval
= current_event
.dwThreadId
;
1266 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1269 case OUTPUT_DEBUG_STRING_EVENT
: /* message from the kernel */
1270 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1271 (unsigned) current_event
.dwProcessId
,
1272 (unsigned) current_event
.dwThreadId
,
1273 "OUTPUT_DEBUG_STRING_EVENT"));
1274 if (saw_create
!= 1)
1276 if (handle_output_debug_string (ourstatus
))
1277 retval
= main_thread_id
;
1281 if (saw_create
!= 1)
1283 printf_unfiltered ("gdb: kernel event for pid=%ld tid=%ld\n",
1284 (DWORD
) current_event
.dwProcessId
,
1285 (DWORD
) current_event
.dwThreadId
);
1286 printf_unfiltered (" unknown event code %ld\n",
1287 current_event
.dwDebugEventCode
);
1291 if (!retval
|| saw_create
!= 1)
1292 CHECK (win32_continue (continue_status
, -1));
1295 inferior_ptid
= pid_to_ptid (retval
);
1296 current_thread
= th
?: thread_rec (current_event
.dwThreadId
, TRUE
);
1303 /* Wait for interesting events to occur in the target process. */
1305 win32_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1307 int pid
= PIDGET (ptid
);
1309 /* We loop when we get a non-standard exception rather than return
1310 with a SPURIOUS because resume can try and step or modify things,
1311 which needs a current_thread->h. But some of these exceptions mark
1312 the birth or death of threads, which mean that the current thread
1313 isn't necessarily what you think it is. */
1317 int retval
= get_win32_debug_event (pid
, ourstatus
);
1319 return pid_to_ptid (retval
);
1324 if (deprecated_ui_loop_hook
!= NULL
)
1325 detach
= deprecated_ui_loop_hook (0);
1328 win32_kill_inferior ();
1334 do_initial_win32_stuff (DWORD pid
)
1336 extern int stop_after_trap
;
1339 last_sig
= TARGET_SIGNAL_0
;
1341 exception_count
= 0;
1342 debug_registers_changed
= 0;
1343 debug_registers_used
= 0;
1344 for (i
= 0; i
< sizeof (dr
) / sizeof (dr
[0]); i
++)
1346 current_event
.dwProcessId
= pid
;
1347 memset (¤t_event
, 0, sizeof (current_event
));
1348 push_target (&win32_ops
);
1349 disable_breakpoints_in_shlibs (1);
1350 win32_clear_solib ();
1351 clear_proceed_status ();
1352 init_wait_for_inferior ();
1354 target_terminal_init ();
1355 target_terminal_inferior ();
1359 stop_after_trap
= 1;
1360 wait_for_inferior ();
1361 if (stop_signal
!= TARGET_SIGNAL_TRAP
)
1362 resume (0, stop_signal
);
1366 stop_after_trap
= 0;
1370 /* Since Windows XP, detaching from a process is supported by Windows.
1371 The following code tries loading the appropriate functions dynamically.
1372 If loading these functions succeeds use them to actually detach from
1373 the inferior process, otherwise behave as usual, pretending that
1374 detach has worked. */
1375 static BOOL
WINAPI (*DebugSetProcessKillOnExit
)(BOOL
);
1376 static BOOL
WINAPI (*DebugActiveProcessStop
)(DWORD
);
1379 has_detach_ability (void)
1381 static HMODULE kernel32
= NULL
;
1384 kernel32
= LoadLibrary ("kernel32.dll");
1387 if (!DebugSetProcessKillOnExit
)
1388 DebugSetProcessKillOnExit
= GetProcAddress (kernel32
,
1389 "DebugSetProcessKillOnExit");
1390 if (!DebugActiveProcessStop
)
1391 DebugActiveProcessStop
= GetProcAddress (kernel32
,
1392 "DebugActiveProcessStop");
1393 if (DebugSetProcessKillOnExit
&& DebugActiveProcessStop
)
1399 /* Try to set or remove a user privilege to the current process. Return -1
1400 if that fails, the previous setting of that privilege otherwise.
1402 This code is copied from the Cygwin source code and rearranged to allow
1403 dynamically loading of the needed symbols from advapi32 which is only
1404 available on NT/2K/XP. */
1406 set_process_privilege (const char *privilege
, BOOL enable
)
1408 static HMODULE advapi32
= NULL
;
1409 static BOOL
WINAPI (*OpenProcessToken
)(HANDLE
, DWORD
, PHANDLE
);
1410 static BOOL
WINAPI (*LookupPrivilegeValue
)(LPCSTR
, LPCSTR
, PLUID
);
1411 static BOOL
WINAPI (*AdjustTokenPrivileges
)(HANDLE
, BOOL
, PTOKEN_PRIVILEGES
,
1412 DWORD
, PTOKEN_PRIVILEGES
, PDWORD
);
1414 HANDLE token_hdl
= NULL
;
1416 TOKEN_PRIVILEGES new_priv
, orig_priv
;
1420 if (GetVersion () >= 0x80000000) /* No security availbale on 9x/Me */
1425 if (!(advapi32
= LoadLibrary ("advapi32.dll")))
1427 if (!OpenProcessToken
)
1428 OpenProcessToken
= GetProcAddress (advapi32
, "OpenProcessToken");
1429 if (!LookupPrivilegeValue
)
1430 LookupPrivilegeValue
= GetProcAddress (advapi32
,
1431 "LookupPrivilegeValueA");
1432 if (!AdjustTokenPrivileges
)
1433 AdjustTokenPrivileges
= GetProcAddress (advapi32
,
1434 "AdjustTokenPrivileges");
1435 if (!OpenProcessToken
|| !LookupPrivilegeValue
|| !AdjustTokenPrivileges
)
1442 if (!OpenProcessToken (GetCurrentProcess (),
1443 TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
,
1447 if (!LookupPrivilegeValue (NULL
, privilege
, &restore_priv
))
1450 new_priv
.PrivilegeCount
= 1;
1451 new_priv
.Privileges
[0].Luid
= restore_priv
;
1452 new_priv
.Privileges
[0].Attributes
= enable
? SE_PRIVILEGE_ENABLED
: 0;
1454 if (!AdjustTokenPrivileges (token_hdl
, FALSE
, &new_priv
,
1455 sizeof orig_priv
, &orig_priv
, &size
))
1458 /* Disabled, otherwise every `attach' in an unprivileged user session
1459 would raise the "Failed to get SE_DEBUG_NAME privilege" warning in
1461 /* AdjustTokenPrivileges returns TRUE even if the privilege could not
1462 be enabled. GetLastError () returns an correct error code, though. */
1463 if (enable
&& GetLastError () == ERROR_NOT_ALL_ASSIGNED
)
1467 ret
= orig_priv
.Privileges
[0].Attributes
== SE_PRIVILEGE_ENABLED
? 1 : 0;
1471 CloseHandle (token_hdl
);
1476 /* Attach to process PID, then initialize for debugging it. */
1478 win32_attach (char *args
, int from_tty
)
1484 error_no_arg (_("process-id to attach"));
1486 if (set_process_privilege (SE_DEBUG_NAME
, TRUE
) < 0)
1488 printf_unfiltered ("Warning: Failed to get SE_DEBUG_NAME privilege\n");
1489 printf_unfiltered ("This can cause attach to fail on Windows NT/2K/XP\n");
1492 pid
= strtoul (args
, 0, 0); /* Windows pid */
1494 win32_init_thread_list ();
1495 ok
= DebugActiveProcess (pid
);
1500 /* Try fall back to Cygwin pid */
1501 pid
= cygwin_internal (CW_CYGWIN_PID_TO_WINPID
, pid
);
1504 ok
= DebugActiveProcess (pid
);
1507 error (_("Can't attach to process."));
1510 if (has_detach_ability ())
1511 DebugSetProcessKillOnExit (FALSE
);
1517 char *exec_file
= (char *) get_exec_file (0);
1520 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
,
1521 target_pid_to_str (pid_to_ptid (pid
)));
1523 printf_unfiltered ("Attaching to %s\n",
1524 target_pid_to_str (pid_to_ptid (pid
)));
1526 gdb_flush (gdb_stdout
);
1529 do_initial_win32_stuff (pid
);
1530 target_terminal_ours ();
1534 win32_detach (char *args
, int from_tty
)
1538 if (has_detach_ability ())
1540 delete_command (NULL
, 0);
1541 win32_continue (DBG_CONTINUE
, -1);
1542 if (!DebugActiveProcessStop (current_event
.dwProcessId
))
1544 error (_("Can't detach process %lu (error %lu)"),
1545 current_event
.dwProcessId
, GetLastError ());
1548 DebugSetProcessKillOnExit (FALSE
);
1550 if (detached
&& from_tty
)
1552 char *exec_file
= get_exec_file (0);
1555 printf_unfiltered ("Detaching from program: %s, Pid %lu\n", exec_file
,
1556 current_event
.dwProcessId
);
1557 gdb_flush (gdb_stdout
);
1559 inferior_ptid
= null_ptid
;
1560 unpush_target (&win32_ops
);
1564 win32_pid_to_exec_file (int pid
)
1566 /* Try to find the process path using the Cygwin internal process list
1567 pid isn't a valid pid, unfortunately. Use current_event.dwProcessId
1569 /* TODO: Also find native Windows processes using CW_GETPINFO_FULL. */
1571 static char path
[MAX_PATH
+ 1];
1572 char *path_ptr
= NULL
;
1574 struct external_pinfo
*pinfo
;
1576 cygwin_internal (CW_LOCK_PINFO
, 1000);
1578 (pinfo
= (struct external_pinfo
*)
1579 cygwin_internal (CW_GETPINFO
, cpid
| CW_NEXTPID
));
1582 if (pinfo
->dwProcessId
== current_event
.dwProcessId
) /* Got it */
1584 cygwin_conv_to_full_posix_path (pinfo
->progname
, path
);
1589 cygwin_internal (CW_UNLOCK_PINFO
);
1593 /* Print status information about what we're accessing. */
1596 win32_files_info (struct target_ops
*ignore
)
1598 printf_unfiltered ("\tUsing the running image of %s %s.\n",
1599 attach_flag
? "attached" : "child", target_pid_to_str (inferior_ptid
));
1603 win32_open (char *arg
, int from_tty
)
1605 error (_("Use the \"run\" command to start a Unix child process."));
1608 /* Start an inferior win32 child process and sets inferior_ptid to its pid.
1609 EXEC_FILE is the file to run.
1610 ALLARGS is a string containing the arguments to the program.
1611 ENV is the environment vector to pass. Errors reported with error(). */
1614 win32_create_inferior (char *exec_file
, char *allargs
, char **env
,
1622 PROCESS_INFORMATION pi
;
1626 char real_path
[MAXPATHLEN
];
1628 char shell
[MAX_PATH
+ 1]; /* Path to shell */
1631 int ostdin
, ostdout
, ostderr
;
1632 const char *inferior_io_terminal
= get_inferior_io_terminal ();
1635 error (_("No executable specified, use `target exec'."));
1637 memset (&si
, 0, sizeof (si
));
1638 si
.cb
= sizeof (si
);
1642 flags
= DEBUG_ONLY_THIS_PROCESS
;
1643 cygwin_conv_to_win32_path (exec_file
, real_path
);
1649 sh
= getenv ("SHELL");
1652 cygwin_conv_to_win32_path (sh
, shell
);
1653 newallargs
= alloca (sizeof (" -c 'exec '") + strlen (exec_file
)
1654 + strlen (allargs
) + 2);
1655 sprintf (newallargs
, " -c 'exec %s %s'", exec_file
, allargs
);
1656 allargs
= newallargs
;
1658 flags
= DEBUG_PROCESS
;
1662 flags
|= CREATE_NEW_PROCESS_GROUP
;
1665 flags
|= CREATE_NEW_CONSOLE
;
1669 args
= alloca (strlen (toexec
) + strlen (allargs
) + 2);
1670 strcpy (args
, toexec
);
1672 strcat (args
, allargs
);
1674 /* Prepare the environment vars for CreateProcess. */
1676 /* This code used to assume all env vars were file names and would
1677 translate them all to win32 style. That obviously doesn't work in the
1678 general case. The current rule is that we only translate PATH.
1679 We need to handle PATH because we're about to call CreateProcess and
1680 it uses PATH to find DLL's. Fortunately PATH has a well-defined value
1681 in both posix and win32 environments. cygwin.dll will change it back
1682 to posix style if necessary. */
1684 static const char *conv_path_names
[] =
1690 /* CreateProcess takes the environment list as a null terminated set of
1691 strings (i.e. two nulls terminate the list). */
1693 /* Get total size for env strings. */
1694 for (envlen
= 0, i
= 0; env
[i
] && *env
[i
]; i
++)
1698 for (j
= 0; conv_path_names
[j
]; j
++)
1700 len
= strlen (conv_path_names
[j
]);
1701 if (strncmp (conv_path_names
[j
], env
[i
], len
) == 0)
1703 if (cygwin_posix_path_list_p (env
[i
] + len
))
1705 + cygwin_posix_to_win32_path_list_buf_size (env
[i
] + len
);
1707 envlen
+= strlen (env
[i
]) + 1;
1711 if (conv_path_names
[j
] == NULL
)
1712 envlen
+= strlen (env
[i
]) + 1;
1715 winenv
= alloca (envlen
+ 1);
1717 /* Copy env strings into new buffer. */
1718 for (temp
= winenv
, i
= 0; env
[i
] && *env
[i
]; i
++)
1722 for (j
= 0; conv_path_names
[j
]; j
++)
1724 len
= strlen (conv_path_names
[j
]);
1725 if (strncmp (conv_path_names
[j
], env
[i
], len
) == 0)
1727 if (cygwin_posix_path_list_p (env
[i
] + len
))
1729 memcpy (temp
, env
[i
], len
);
1730 cygwin_posix_to_win32_path_list (env
[i
] + len
, temp
+ len
);
1733 strcpy (temp
, env
[i
]);
1737 if (conv_path_names
[j
] == NULL
)
1738 strcpy (temp
, env
[i
]);
1740 temp
+= strlen (temp
) + 1;
1743 /* Final nil string to terminate new env. */
1747 if (!inferior_io_terminal
)
1748 tty
= ostdin
= ostdout
= ostderr
= -1;
1751 tty
= open (inferior_io_terminal
, O_RDWR
| O_NOCTTY
);
1754 print_sys_errmsg (inferior_io_terminal
, errno
);
1755 ostdin
= ostdout
= ostderr
= -1;
1768 win32_init_thread_list ();
1769 ret
= CreateProcess (0,
1770 args
, /* command line */
1771 NULL
, /* Security */
1773 TRUE
, /* inherit handles */
1774 flags
, /* start flags */
1776 NULL
, /* current directory */
1791 error (_("Error creating process %s, (error %d)."),
1792 exec_file
, (unsigned) GetLastError ());
1794 CloseHandle (pi
.hThread
);
1795 CloseHandle (pi
.hProcess
);
1797 if (useshell
&& shell
[0] != '\0')
1802 do_initial_win32_stuff (pi
.dwProcessId
);
1804 /* win32_continue (DBG_CONTINUE, -1); */
1805 proceed ((CORE_ADDR
) - 1, TARGET_SIGNAL_0
, 0);
1809 win32_mourn_inferior (void)
1811 (void) win32_continue (DBG_CONTINUE
, -1);
1812 i386_cleanup_dregs();
1813 unpush_target (&win32_ops
);
1814 generic_mourn_inferior ();
1817 /* Send a SIGINT to the process group. This acts just like the user typed a
1818 ^C on the controlling terminal. */
1823 DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n"));
1824 CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT
, current_event
.dwProcessId
));
1825 registers_changed (); /* refresh register state */
1829 win32_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*our
, int len
,
1830 int write
, struct mem_attrib
*mem
,
1831 struct target_ops
*target
)
1836 DEBUG_MEM (("gdb: write target memory, %d bytes at 0x%08lx\n",
1837 len
, (DWORD
) memaddr
));
1838 if (!WriteProcessMemory (current_process_handle
, (LPVOID
) memaddr
, our
,
1841 FlushInstructionCache (current_process_handle
, (LPCVOID
) memaddr
, len
);
1845 DEBUG_MEM (("gdb: read target memory, %d bytes at 0x%08lx\n",
1846 len
, (DWORD
) memaddr
));
1847 if (!ReadProcessMemory (current_process_handle
, (LPCVOID
) memaddr
, our
,
1855 win32_kill_inferior (void)
1857 CHECK (TerminateProcess (current_process_handle
, 0));
1861 if (!win32_continue (DBG_CONTINUE
, -1))
1863 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
1865 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
1869 CHECK (CloseHandle (current_process_handle
));
1871 /* this may fail in an attached process so don't check. */
1872 if (current_thread
&& current_thread
->h
)
1873 (void) CloseHandle (current_thread
->h
);
1874 target_mourn_inferior (); /* or just win32_mourn_inferior? */
1878 win32_resume (ptid_t ptid
, int step
, enum target_signal sig
)
1881 DWORD continue_status
= DBG_CONTINUE
;
1883 int pid
= PIDGET (ptid
);
1885 if (sig
!= TARGET_SIGNAL_0
)
1887 if (current_event
.dwDebugEventCode
!= EXCEPTION_DEBUG_EVENT
)
1889 DEBUG_EXCEPT(("Cannot continue with signal %d here.\n",sig
));
1891 else if (sig
== last_sig
)
1892 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1895 /* This code does not seem to work, because
1896 the kernel does probably not consider changes in the ExceptionRecord
1897 structure when passing the exception to the inferior.
1898 Note that this seems possible in the exception handler itself. */
1901 for (i
= 0; xlate
[i
].them
!= -1; i
++)
1902 if (xlate
[i
].us
== sig
)
1904 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
=
1906 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
1909 if (continue_status
== DBG_CONTINUE
)
1911 DEBUG_EXCEPT(("Cannot continue with signal %d.\n",sig
));
1915 DEBUG_EXCEPT(("Can only continue with recieved signal %d.\n",
1919 last_sig
= TARGET_SIGNAL_0
;
1921 DEBUG_EXEC (("gdb: win32_resume (pid=%d, step=%d, sig=%d);\n",
1924 /* Get context for currently selected thread */
1925 th
= thread_rec (current_event
.dwThreadId
, FALSE
);
1930 /* Single step by setting t bit */
1931 win32_fetch_inferior_registers (PS_REGNUM
);
1932 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
1935 if (th
->context
.ContextFlags
)
1937 if (debug_registers_changed
)
1939 th
->context
.Dr0
= dr
[0];
1940 th
->context
.Dr1
= dr
[1];
1941 th
->context
.Dr2
= dr
[2];
1942 th
->context
.Dr3
= dr
[3];
1943 /* th->context.Dr6 = dr[6];
1944 FIXME: should we set dr6 also ?? */
1945 th
->context
.Dr7
= dr
[7];
1947 CHECK (SetThreadContext (th
->h
, &th
->context
));
1948 th
->context
.ContextFlags
= 0;
1952 /* Allow continuing with the same signal that interrupted us.
1953 Otherwise complain. */
1955 win32_continue (continue_status
, pid
);
1959 win32_prepare_to_store (void)
1961 /* Do nothing, since we can store individual regs */
1965 win32_can_run (void)
1973 DEBUG_EVENTS (("gdb: win32_close, inferior_ptid=%d\n",
1974 PIDGET (inferior_ptid
)));
1977 /* Convert pid to printable format. */
1979 cygwin_pid_to_str (ptid_t ptid
)
1981 static char buf
[80];
1982 int pid
= PIDGET (ptid
);
1984 if ((DWORD
) pid
== current_event
.dwProcessId
)
1985 sprintf (buf
, "process %d", pid
);
1987 sprintf (buf
, "thread %ld.0x%x", current_event
.dwProcessId
, pid
);
1992 init_win32_ops (void)
1994 win32_ops
.to_shortname
= "child";
1995 win32_ops
.to_longname
= "Win32 child process";
1996 win32_ops
.to_doc
= "Win32 child process (started by the \"run\" command).";
1997 win32_ops
.to_open
= win32_open
;
1998 win32_ops
.to_close
= win32_close
;
1999 win32_ops
.to_attach
= win32_attach
;
2000 win32_ops
.to_detach
= win32_detach
;
2001 win32_ops
.to_resume
= win32_resume
;
2002 win32_ops
.to_wait
= win32_wait
;
2003 win32_ops
.to_fetch_registers
= win32_fetch_inferior_registers
;
2004 win32_ops
.to_store_registers
= win32_store_inferior_registers
;
2005 win32_ops
.to_prepare_to_store
= win32_prepare_to_store
;
2006 win32_ops
.deprecated_xfer_memory
= win32_xfer_memory
;
2007 win32_ops
.to_files_info
= win32_files_info
;
2008 win32_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
2009 win32_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
2010 win32_ops
.to_terminal_init
= terminal_init_inferior
;
2011 win32_ops
.to_terminal_inferior
= terminal_inferior
;
2012 win32_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
2013 win32_ops
.to_terminal_ours
= terminal_ours
;
2014 win32_ops
.to_terminal_save_ours
= terminal_save_ours
;
2015 win32_ops
.to_terminal_info
= child_terminal_info
;
2016 win32_ops
.to_kill
= win32_kill_inferior
;
2017 win32_ops
.to_create_inferior
= win32_create_inferior
;
2018 win32_ops
.to_mourn_inferior
= win32_mourn_inferior
;
2019 win32_ops
.to_can_run
= win32_can_run
;
2020 win32_ops
.to_thread_alive
= win32_win32_thread_alive
;
2021 win32_ops
.to_pid_to_str
= cygwin_pid_to_str
;
2022 win32_ops
.to_stop
= win32_stop
;
2023 win32_ops
.to_stratum
= process_stratum
;
2024 win32_ops
.to_has_all_memory
= 1;
2025 win32_ops
.to_has_memory
= 1;
2026 win32_ops
.to_has_stack
= 1;
2027 win32_ops
.to_has_registers
= 1;
2028 win32_ops
.to_has_execution
= 1;
2029 win32_ops
.to_magic
= OPS_MAGIC
;
2030 win32_ops
.to_pid_to_exec_file
= win32_pid_to_exec_file
;
2032 win32_so_ops
.relocate_section_addresses
= win32_relocate_section_addresses
;
2033 win32_so_ops
.free_so
= win32_free_so
;
2034 win32_so_ops
.clear_solib
= win32_clear_solib
;
2035 win32_so_ops
.solib_create_inferior_hook
= NULL
;
2036 win32_so_ops
.special_symbol_handling
= NULL
;
2037 win32_so_ops
.current_sos
= win32_current_sos
;
2038 win32_so_ops
.open_symbol_file_object
= NULL
;
2039 win32_so_ops
.in_dynsym_resolve_code
= NULL
;
2041 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
2042 current_target_so_ops
= &win32_so_ops
;
2046 _initialize_win32_nat (void)
2048 struct cmd_list_element
*c
;
2052 c
= add_com ("dll-symbols", class_files
, dll_symbol_command
,
2053 _("Load dll library symbols from FILE."));
2054 set_cmd_completer (c
, filename_completer
);
2056 add_com_alias ("sharedlibrary", "dll-symbols", class_alias
, 1);
2058 add_setshow_boolean_cmd ("shell", class_support
, &useshell
, _("\
2059 Set use of shell to start subprocess."), _("\
2060 Show use of shell to start subprocess."), NULL
,
2062 NULL
, /* FIXME: i18n: */
2063 &setlist
, &showlist
);
2065 add_setshow_boolean_cmd ("new-console", class_support
, &new_console
, _("\
2066 Set creation of new console when creating child process."), _("\
2067 Show creation of new console when creating child process."), NULL
,
2069 NULL
, /* FIXME: i18n: */
2070 &setlist
, &showlist
);
2072 add_setshow_boolean_cmd ("new-group", class_support
, &new_group
, _("\
2073 Set creation of new group when creating child process."), _("\
2074 Show creation of new group when creating child process."), NULL
,
2076 NULL
, /* FIXME: i18n: */
2077 &setlist
, &showlist
);
2079 add_setshow_boolean_cmd ("debugexec", class_support
, &debug_exec
, _("\
2080 Set whether to display execution in child process."), _("\
2081 Show whether to display execution in child process."), NULL
,
2083 NULL
, /* FIXME: i18n: */
2084 &setlist
, &showlist
);
2086 add_setshow_boolean_cmd ("debugevents", class_support
, &debug_events
, _("\
2087 Set whether to display kernel events in child process."), _("\
2088 Show whether to display kernel events in child process."), NULL
,
2090 NULL
, /* FIXME: i18n: */
2091 &setlist
, &showlist
);
2093 add_setshow_boolean_cmd ("debugmemory", class_support
, &debug_memory
, _("\
2094 Set whether to display memory accesses in child process."), _("\
2095 Show whether to display memory accesses in child process."), NULL
,
2097 NULL
, /* FIXME: i18n: */
2098 &setlist
, &showlist
);
2100 add_setshow_boolean_cmd ("debugexceptions", class_support
,
2101 &debug_exceptions
, _("\
2102 Set whether to display kernel exceptions in child process."), _("\
2103 Show whether to display kernel exceptions in child process."), NULL
,
2105 NULL
, /* FIXME: i18n: */
2106 &setlist
, &showlist
);
2108 add_info ("dll", info_dll_command
, _("Status of loaded DLLs."));
2109 add_info_alias ("sharedlibrary", "dll", 1);
2111 add_prefix_cmd ("w32", class_info
, info_w32_command
,
2112 _("Print information specific to Win32 debugging."),
2113 &info_w32_cmdlist
, "info w32 ", 0, &infolist
);
2115 add_cmd ("selector", class_info
, display_selectors
,
2116 _("Display selectors infos."),
2118 add_target (&win32_ops
);
2121 /* Hardware watchpoint support, adapted from go32-nat.c code. */
2123 /* Pass the address ADDR to the inferior in the I'th debug register.
2124 Here we just store the address in dr array, the registers will be
2125 actually set up when win32_continue is called. */
2127 cygwin_set_dr (int i
, CORE_ADDR addr
)
2130 internal_error (__FILE__
, __LINE__
,
2131 _("Invalid register %d in cygwin_set_dr.\n"), i
);
2132 dr
[i
] = (unsigned) addr
;
2133 debug_registers_changed
= 1;
2134 debug_registers_used
= 1;
2137 /* Pass the value VAL to the inferior in the DR7 debug control
2138 register. Here we just store the address in D_REGS, the watchpoint
2139 will be actually set up in win32_wait. */
2141 cygwin_set_dr7 (unsigned val
)
2144 debug_registers_changed
= 1;
2145 debug_registers_used
= 1;
2148 /* Get the value of the DR6 debug status register from the inferior.
2149 Here we just return the value stored in dr[6]
2150 by the last call to thread_rec for current_event.dwThreadId id. */
2152 cygwin_get_dr6 (void)
2157 /* Determine if the thread referenced by "pid" is alive
2158 by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0
2159 it means that the pid has died. Otherwise it is assumed to be alive. */
2161 win32_win32_thread_alive (ptid_t ptid
)
2163 int pid
= PIDGET (ptid
);
2165 return WaitForSingleObject (thread_rec (pid
, FALSE
)->h
, 0) == WAIT_OBJECT_0
?
2170 fetch_elf_core_registers (char *core_reg_sect
,
2171 unsigned core_reg_size
,
2176 if (core_reg_size
< sizeof (CONTEXT
))
2178 error (_("Core file register section too small (%u bytes)."), core_reg_size
);
2181 for (r
= 0; r
< NUM_REGS
; r
++)
2182 regcache_raw_supply (current_regcache
, r
, core_reg_sect
+ mappings
[r
]);
2185 static struct core_fns win32_elf_core_fns
=
2187 bfd_target_elf_flavour
,
2188 default_check_format
,
2189 default_core_sniffer
,
2190 fetch_elf_core_registers
,
2195 _initialize_core_win32 (void)
2197 deprecated_add_core_fns (&win32_elf_core_fns
);
2201 _initialize_check_for_gdb_ini (void)
2204 if (inhibit_gdbinit
)
2207 homedir
= getenv ("HOME");
2211 char *oldini
= (char *) alloca (strlen (homedir
) +
2212 sizeof ("/gdb.ini"));
2213 strcpy (oldini
, homedir
);
2214 p
= strchr (oldini
, '\0');
2215 if (p
> oldini
&& p
[-1] != '/')
2217 strcpy (p
, "gdb.ini");
2218 if (access (oldini
, 0) == 0)
2220 int len
= strlen (oldini
);
2221 char *newini
= alloca (len
+ 1);
2222 sprintf (newini
, "%.*s.gdbinit",
2223 (int) (len
- (sizeof ("gdb.ini") - 1)), oldini
);
2224 warning (_("obsolete '%s' found. Rename to '%s'."), oldini
, newini
);