1 /* Target-vector operations for controlling win32 child processes, for GDB.
3 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free
4 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. */
27 /* We assume we're being built with and will be used for cygwin. */
30 #include "tm.h" /* required for SSE registers */
31 #include "frame.h" /* required by inferior.h */
36 #include "completer.h"
39 #include "i386-tdep.h"
41 #include <sys/types.h>
46 #include <sys/cygwin.h>
51 #include "gdb_string.h"
52 #include "gdbthread.h"
54 #include <sys/param.h>
57 /* The ui's event loop. */
58 extern int (*ui_loop_hook) (int signo);
60 /* If we're not using the old Cygwin header file set, define the
61 following which never should have been in the generic Win32 API
62 headers in the first place since they were our own invention... */
63 #ifndef _GNU_H_WINDOWS_H
66 FLAG_TRACE_BIT = 0x100,
67 CONTEXT_DEBUGGER = (CONTEXT_FULL | CONTEXT_FLOATING_POINT)
70 #include <sys/procfs.h>
74 #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS \
75 | CONTEXT_EXTENDED_REGISTERS
77 #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS
80 static unsigned dr[8];
81 static int debug_registers_changed = 0;
82 static int debug_registers_used = 0;
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 /* Forward declaration */
95 extern struct target_ops child_ops;
97 static void child_stop (void);
98 static int win32_child_thread_alive (ptid_t);
99 void child_kill_inferior (void);
101 static enum target_signal last_sig = TARGET_SIGNAL_0;
102 /* Set if a signal was received from the debugged process */
104 /* Thread information structure used to track information that is
105 not available in gdb's thread structure. */
106 typedef struct thread_info_struct
108 struct thread_info_struct *next;
118 static thread_info thread_head;
120 /* The process and thread handles for the above context. */
122 static DEBUG_EVENT current_event; /* The current debug event from
124 static HANDLE current_process_handle; /* Currently executing process */
125 static thread_info *current_thread; /* Info on currently selected thread */
126 static DWORD main_thread_id; /* Thread ID of the main thread */
128 /* Counts of things. */
129 static int exception_count = 0;
130 static int event_count = 0;
133 static int new_console = 0;
134 static int new_group = 1;
135 static int debug_exec = 0; /* show execution */
136 static int debug_events = 0; /* show events from kernel */
137 static int debug_memory = 0; /* show target memory accesses */
138 static int debug_exceptions = 0; /* show target exceptions */
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)
190 context_offset (ExtendedRegisters[10*16]),
191 context_offset (ExtendedRegisters[11*16]),
192 context_offset (ExtendedRegisters[12*16]),
193 context_offset (ExtendedRegisters[13*16]),
194 context_offset (ExtendedRegisters[14*16]),
195 context_offset (ExtendedRegisters[15*16]),
196 context_offset (ExtendedRegisters[16*16]),
197 context_offset (ExtendedRegisters[17*16]),
199 context_offset (ExtendedRegisters[24])
203 #undef context_offset
205 /* This vector maps the target's idea of an exception (extracted
206 from the DEBUG_EVENT structure) to GDB's idea. */
208 struct xlate_exception
211 enum target_signal us;
214 static const struct xlate_exception
217 {EXCEPTION_ACCESS_VIOLATION, TARGET_SIGNAL_SEGV},
218 {STATUS_STACK_OVERFLOW, TARGET_SIGNAL_SEGV},
219 {EXCEPTION_BREAKPOINT, TARGET_SIGNAL_TRAP},
220 {DBG_CONTROL_C, TARGET_SIGNAL_INT},
221 {EXCEPTION_SINGLE_STEP, TARGET_SIGNAL_TRAP},
222 {STATUS_FLOAT_DIVIDE_BY_ZERO, TARGET_SIGNAL_FPE},
226 check (BOOL ok, const char *file, int line)
229 printf_filtered ("error return %s:%d was %lu\n", file, line,
234 /* Find a thread record given a thread id.
235 If get_context then also retrieve the context for this
238 thread_rec (DWORD id, int get_context)
242 for (th = &thread_head; (th = th->next) != NULL;)
245 if (!th->suspend_count && get_context)
247 if (get_context > 0 && id != current_event.dwThreadId)
248 th->suspend_count = SuspendThread (th->h) + 1;
249 else if (get_context < 0)
250 th->suspend_count = -1;
252 th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
253 GetThreadContext (th->h, &th->context);
254 if (id == current_event.dwThreadId)
256 /* Copy dr values from that thread. */
257 dr[0] = th->context.Dr0;
258 dr[1] = th->context.Dr1;
259 dr[2] = th->context.Dr2;
260 dr[3] = th->context.Dr3;
261 dr[6] = th->context.Dr6;
262 dr[7] = th->context.Dr7;
271 /* Add a thread to the thread list */
273 child_add_thread (DWORD id, HANDLE h)
277 if ((th = thread_rec (id, FALSE)))
280 th = (thread_info *) xmalloc (sizeof (*th));
281 memset (th, 0, sizeof (*th));
284 th->next = thread_head.next;
285 thread_head.next = th;
286 add_thread (pid_to_ptid (id));
287 /* Set the debug registers for the new thread in they are used. */
288 if (debug_registers_used)
290 /* Only change the value of the debug registers. */
291 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
292 CHECK (GetThreadContext (th->h, &th->context));
293 th->context.Dr0 = dr[0];
294 th->context.Dr1 = dr[1];
295 th->context.Dr2 = dr[2];
296 th->context.Dr3 = dr[3];
297 /* th->context.Dr6 = dr[6];
298 FIXME: should we set dr6 also ?? */
299 th->context.Dr7 = dr[7];
300 CHECK (SetThreadContext (th->h, &th->context));
301 th->context.ContextFlags = 0;
306 /* Clear out any old thread list and reintialize it to a
309 child_init_thread_list (void)
311 thread_info *th = &thread_head;
313 DEBUG_EVENTS (("gdb: child_init_thread_list\n"));
315 while (th->next != NULL)
317 thread_info *here = th->next;
318 th->next = here->next;
319 (void) CloseHandle (here->h);
324 /* Delete a thread from the list of threads */
326 child_delete_thread (DWORD id)
331 printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (pid_to_ptid (id)));
332 delete_thread (pid_to_ptid (id));
334 for (th = &thread_head;
335 th->next != NULL && th->next->id != id;
339 if (th->next != NULL)
341 thread_info *here = th->next;
342 th->next = here->next;
343 CloseHandle (here->h);
349 do_child_fetch_inferior_registers (int r)
351 char *context_offset = ((char *) ¤t_thread->context) + mappings[r];
355 l = *((long *) context_offset) & 0xffff;
356 supply_register (r, (char *) &l);
358 else if (r == FOP_REGNUM)
360 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
361 supply_register (r, (char *) &l);
364 supply_register (r, context_offset);
367 for (r = 0; r < NUM_REGS; r++)
368 do_child_fetch_inferior_registers (r);
373 child_fetch_inferior_registers (int r)
375 current_thread = thread_rec (PIDGET (inferior_ptid), TRUE);
376 do_child_fetch_inferior_registers (r);
380 do_child_store_inferior_registers (int r)
383 read_register_gen (r, ((char *) ¤t_thread->context) + mappings[r]);
386 for (r = 0; r < NUM_REGS; r++)
387 do_child_store_inferior_registers (r);
391 /* Store a new register value into the current thread context */
393 child_store_inferior_registers (int r)
395 current_thread = thread_rec (PIDGET (inferior_ptid), TRUE);
396 do_child_store_inferior_registers (r);
399 static int psapi_loaded = 0;
400 static HMODULE psapi_module_handle = NULL;
401 static BOOL WINAPI (*psapi_EnumProcessModules) (HANDLE, HMODULE *, DWORD, LPDWORD) = NULL;
402 static BOOL WINAPI (*psapi_GetModuleInformation) (HANDLE, HMODULE, LPMODULEINFO, DWORD) = NULL;
403 static DWORD WINAPI (*psapi_GetModuleFileNameExA) (HANDLE, HMODULE, LPSTR, DWORD) = NULL;
406 psapi_get_dll_name (DWORD BaseAddress, char *dll_name_ret)
412 HMODULE *DllHandle = dh_buf;
417 psapi_EnumProcessModules == NULL ||
418 psapi_GetModuleInformation == NULL ||
419 psapi_GetModuleFileNameExA == NULL)
424 psapi_module_handle = LoadLibrary ("psapi.dll");
425 if (!psapi_module_handle)
427 /* printf_unfiltered ("error loading psapi.dll: %u", GetLastError ()); */
430 psapi_EnumProcessModules = GetProcAddress (psapi_module_handle, "EnumProcessModules");
431 psapi_GetModuleInformation = GetProcAddress (psapi_module_handle, "GetModuleInformation");
432 psapi_GetModuleFileNameExA = (void *) GetProcAddress (psapi_module_handle,
433 "GetModuleFileNameExA");
434 if (psapi_EnumProcessModules == NULL ||
435 psapi_GetModuleInformation == NULL ||
436 psapi_GetModuleFileNameExA == NULL)
441 ok = (*psapi_EnumProcessModules) (current_process_handle,
446 if (!ok || !cbNeeded)
449 DllHandle = (HMODULE *) alloca (cbNeeded);
453 ok = (*psapi_EnumProcessModules) (current_process_handle,
460 for (i = 0; i < (int) (cbNeeded / sizeof (HMODULE)); i++)
462 if (!(*psapi_GetModuleInformation) (current_process_handle,
466 error ("Can't get module info");
468 len = (*psapi_GetModuleFileNameExA) (current_process_handle,
473 error ("Error getting dll name: %u\n", GetLastError ());
475 if ((DWORD) (mi.lpBaseOfDll) == BaseAddress)
480 dll_name_ret[0] = '\0';
484 /* Encapsulate the information required in a call to
485 symbol_file_add_args */
486 struct safe_symbol_file_add_args
490 struct section_addr_info *addrs;
493 struct ui_file *err, *out;
497 /* Maintain a linked list of "so" information. */
500 struct so_stuff *next;
503 struct objfile *objfile;
505 } solib_start, *solib_end;
507 /* Call symbol_file_add with stderr redirected. We don't care if there
510 safe_symbol_file_add_stub (void *argv)
512 #define p ((struct safe_symbol_file_add_args *)argv)
513 struct so_stuff *so = &solib_start;
515 while ((so = so->next))
516 if (so->loaded && strcasecmp (so->name, p->name) == 0)
518 p->ret = symbol_file_add (p->name, p->from_tty, p->addrs, p->mainline, p->flags);
523 /* Restore gdb's stderr after calling symbol_file_add */
525 safe_symbol_file_add_cleanup (void *p)
527 #define sp ((struct safe_symbol_file_add_args *)p)
528 gdb_flush (gdb_stderr);
529 gdb_flush (gdb_stdout);
530 ui_file_delete (gdb_stderr);
531 ui_file_delete (gdb_stdout);
532 gdb_stderr = sp->err;
533 gdb_stdout = sp->out;
537 /* symbol_file_add wrapper that prevents errors from being displayed. */
538 static struct objfile *
539 safe_symbol_file_add (char *name, int from_tty,
540 struct section_addr_info *addrs,
541 int mainline, int flags)
543 struct safe_symbol_file_add_args p;
544 struct cleanup *cleanup;
546 cleanup = make_cleanup (safe_symbol_file_add_cleanup, &p);
550 gdb_flush (gdb_stderr);
551 gdb_flush (gdb_stdout);
552 gdb_stderr = ui_file_new ();
553 gdb_stdout = ui_file_new ();
555 p.from_tty = from_tty;
557 p.mainline = mainline;
559 catch_errors (safe_symbol_file_add_stub, &p, "", RETURN_MASK_ERROR);
561 do_cleanups (cleanup);
565 /* Remember the maximum DLL length for printing in info dll command. */
566 int max_dll_name_len;
569 register_loaded_dll (const char *name, DWORD load_addr)
572 char ppath[MAX_PATH + 1];
573 char buf[MAX_PATH + 1];
574 char cwd[MAX_PATH + 1];
576 WIN32_FIND_DATA w32_fd;
577 HANDLE h = FindFirstFile(name, &w32_fd);
582 if (GetCurrentDirectory (MAX_PATH + 1, cwd))
584 p = strrchr (buf, '\\');
587 SetCurrentDirectory (buf);
588 GetFullPathName (w32_fd.cFileName, MAX_PATH, buf, &p);
589 SetCurrentDirectory (cwd);
592 cygwin_conv_to_posix_path (buf, ppath);
593 so = (struct so_stuff *) xmalloc (sizeof (struct so_stuff) + strlen (ppath) + 8 + 1);
595 so->load_addr = load_addr;
598 strcpy (so->name, ppath);
600 solib_end->next = so;
602 len = strlen (ppath);
603 if (len > max_dll_name_len)
604 max_dll_name_len = len;
607 /* Wait for child to do something. Return pid of child, or -1 in case
608 of error; store status through argument pointer OURSTATUS. */
610 handle_load_dll (void *dummy)
612 LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll;
615 char dll_buf[MAX_PATH + 1];
616 char *dll_name = NULL;
619 dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0';
621 if (!psapi_get_dll_name ((DWORD) (event->lpBaseOfDll), dll_buf))
622 dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0';
626 /* Attempt to read the name of the dll that was detected.
627 This is documented to work only when actively debugging
628 a program. It will not work for attached processes. */
629 if (dll_name == NULL || *dll_name == '\0')
631 DWORD size = event->fUnicode ? sizeof (WCHAR) : sizeof (char);
635 ReadProcessMemory (current_process_handle,
636 (LPCVOID) event->lpImageName,
637 (char *) &dll_name_ptr,
638 sizeof (dll_name_ptr), &done);
640 /* See if we could read the address of a string, and that the
641 address isn't null. */
643 if (done != sizeof (dll_name_ptr) || !dll_name_ptr)
648 ReadProcessMemory (current_process_handle,
649 (LPCVOID) (dll_name_ptr + len * size),
655 while ((b[0] != 0 || b[size - 1] != 0) && done == size);
657 dll_name = alloca (len);
661 WCHAR *unicode_dll_name = (WCHAR *) alloca (len * sizeof (WCHAR));
662 ReadProcessMemory (current_process_handle,
663 (LPCVOID) dll_name_ptr,
665 len * sizeof (WCHAR),
668 WideCharToMultiByte (CP_ACP, 0,
669 unicode_dll_name, len,
670 dll_name, len, 0, 0);
674 ReadProcessMemory (current_process_handle,
675 (LPCVOID) dll_name_ptr,
685 register_loaded_dll (dll_name, (DWORD) event->lpBaseOfDll + 0x1000);
691 handle_unload_dll (void *dummy)
693 DWORD lpBaseOfDll = (DWORD) current_event.u.UnloadDll.lpBaseOfDll + 0x1000;
696 for (so = &solib_start; so->next != NULL; so = so->next)
697 if (so->next->load_addr == lpBaseOfDll)
699 struct so_stuff *sodel = so->next;
700 so->next = sodel->next;
704 free_objfile (sodel->objfile);
708 error ("Error: dll starting at 0x%lx not found.\n", (DWORD) lpBaseOfDll);
713 /* Return name of last loaded DLL. */
715 child_solib_loaded_library_pathname (int pid)
717 return !solib_end || !solib_end->name[0] ? NULL : solib_end->name;
720 /* Clear list of loaded DLLs. */
722 child_clear_solibs (void)
724 struct so_stuff *so, *so1 = solib_start.next;
726 while ((so = so1) != NULL)
732 solib_start.next = NULL;
733 solib_start.objfile = NULL;
734 solib_end = &solib_start;
735 max_dll_name_len = sizeof ("DLL Name") - 1;
738 /* Add DLL symbol information. */
739 static struct objfile *
740 solib_symbols_add (char *name, int from_tty, CORE_ADDR load_addr)
742 struct section_addr_info section_addrs;
744 /* The symbols in a dll are offset by 0x1000, which is the
745 the offset from 0 of the first byte in an image - because
746 of the file header and the section alignment. */
748 if (!name || !name[0])
751 memset (§ion_addrs, 0, sizeof (section_addrs));
752 section_addrs.other[0].name = ".text";
753 section_addrs.other[0].addr = load_addr;
754 return safe_symbol_file_add (name, from_tty, NULL, 0, OBJF_SHARED);
757 /* Load DLL symbol info. */
759 dll_symbol_command (char *args, int from_tty)
765 error ("dll-symbols requires a file name");
768 if (n > 4 && strcasecmp (args + n - 4, ".dll") != 0)
770 char *newargs = (char *) alloca (n + 4 + 1);
771 strcpy (newargs, args);
772 strcat (newargs, ".dll");
776 safe_symbol_file_add (args, from_tty, NULL, 0, OBJF_SHARED | OBJF_USERLOADED);
779 /* List currently loaded DLLs. */
781 info_dll_command (char *ignore, int from_tty)
783 struct so_stuff *so = &solib_start;
788 printf_filtered ("%*s Load Address\n", -max_dll_name_len, "DLL Name");
789 while ((so = so->next) != NULL)
790 printf_filtered ("%*s %08lx\n", -max_dll_name_len, so->name, so->load_addr);
795 /* Handle DEBUG_STRING output from child process.
796 Cygwin prepends its messages with a "cygwin:". Interpret this as
797 a Cygwin signal. Otherwise just print the string as a warning. */
799 handle_output_debug_string (struct target_waitstatus *ourstatus)
804 if (!target_read_string
805 ((CORE_ADDR) current_event.u.DebugString.lpDebugStringData, &s, 1024, 0)
809 if (strncmp (s, CYGWIN_SIGNAL_STRING, sizeof (CYGWIN_SIGNAL_STRING) - 1) != 0)
811 if (strncmp (s, "cYg", 3) != 0)
817 int sig = strtol (s + sizeof (CYGWIN_SIGNAL_STRING) - 1, &p, 0);
818 gotasig = target_signal_from_host (sig);
819 ourstatus->value.sig = gotasig;
821 ourstatus->kind = TARGET_WAITKIND_STOPPED;
828 #define DEBUG_EXCEPTION_SIMPLE(x) if (debug_exceptions) \
829 printf_unfiltered ("gdb: Target exception %s at 0x%08lx\n", x, \
830 (DWORD) current_event.u.Exception.ExceptionRecord.ExceptionAddress)
833 handle_exception (struct target_waitstatus *ourstatus)
836 DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode;
838 ourstatus->kind = TARGET_WAITKIND_STOPPED;
840 /* Record the context of the current thread */
841 th = thread_rec (current_event.dwThreadId, -1);
845 case EXCEPTION_ACCESS_VIOLATION:
846 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ACCESS_VIOLATION");
847 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
849 case STATUS_STACK_OVERFLOW:
850 DEBUG_EXCEPTION_SIMPLE ("STATUS_STACK_OVERFLOW");
851 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
853 case STATUS_FLOAT_DENORMAL_OPERAND:
854 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DENORMAL_OPERAND");
855 ourstatus->value.sig = TARGET_SIGNAL_FPE;
857 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
858 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ARRAY_BOUNDS_EXCEEDED");
859 ourstatus->value.sig = TARGET_SIGNAL_FPE;
861 case STATUS_FLOAT_INEXACT_RESULT:
862 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INEXACT_RESULT");
863 ourstatus->value.sig = TARGET_SIGNAL_FPE;
865 case STATUS_FLOAT_INVALID_OPERATION:
866 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INVALID_OPERATION");
867 ourstatus->value.sig = TARGET_SIGNAL_FPE;
869 case STATUS_FLOAT_OVERFLOW:
870 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_OVERFLOW");
871 ourstatus->value.sig = TARGET_SIGNAL_FPE;
873 case STATUS_FLOAT_STACK_CHECK:
874 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_STACK_CHECK");
875 ourstatus->value.sig = TARGET_SIGNAL_FPE;
877 case STATUS_FLOAT_UNDERFLOW:
878 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_UNDERFLOW");
879 ourstatus->value.sig = TARGET_SIGNAL_FPE;
881 case STATUS_FLOAT_DIVIDE_BY_ZERO:
882 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DIVIDE_BY_ZERO");
883 ourstatus->value.sig = TARGET_SIGNAL_FPE;
885 case STATUS_INTEGER_DIVIDE_BY_ZERO:
886 DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_DIVIDE_BY_ZERO");
887 ourstatus->value.sig = TARGET_SIGNAL_FPE;
889 case STATUS_INTEGER_OVERFLOW:
890 DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_OVERFLOW");
891 ourstatus->value.sig = TARGET_SIGNAL_FPE;
893 case EXCEPTION_BREAKPOINT:
894 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT");
895 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
898 DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_C");
899 ourstatus->value.sig = TARGET_SIGNAL_INT;
901 case DBG_CONTROL_BREAK:
902 DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_BREAK");
903 ourstatus->value.sig = TARGET_SIGNAL_INT;
905 case EXCEPTION_SINGLE_STEP:
906 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_SINGLE_STEP");
907 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
909 case EXCEPTION_ILLEGAL_INSTRUCTION:
910 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ILLEGAL_INSTRUCTION");
911 ourstatus->value.sig = TARGET_SIGNAL_ILL;
913 case EXCEPTION_PRIV_INSTRUCTION:
914 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_PRIV_INSTRUCTION");
915 ourstatus->value.sig = TARGET_SIGNAL_ILL;
917 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
918 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION");
919 ourstatus->value.sig = TARGET_SIGNAL_ILL;
922 if (current_event.u.Exception.dwFirstChance)
924 printf_unfiltered ("gdb: unknown target exception 0x%08lx at 0x%08lx\n",
925 current_event.u.Exception.ExceptionRecord.ExceptionCode,
926 (DWORD) current_event.u.Exception.ExceptionRecord.ExceptionAddress);
927 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
931 last_sig = ourstatus->value.sig;
935 /* Resume all artificially suspended threads if we are continuing
938 child_continue (DWORD continue_status, int id)
944 DEBUG_EVENTS (("ContinueDebugEvent (cpid=%ld, ctid=%ld, %s);\n",
945 current_event.dwProcessId, current_event.dwThreadId,
946 continue_status == DBG_CONTINUE ?
947 "DBG_CONTINUE" : "DBG_EXCEPTION_NOT_HANDLED"));
948 res = ContinueDebugEvent (current_event.dwProcessId,
949 current_event.dwThreadId,
953 for (th = &thread_head; (th = th->next) != NULL;)
954 if (((id == -1) || (id == (int) th->id)) && th->suspend_count)
957 for (i = 0; i < th->suspend_count; i++)
958 (void) ResumeThread (th->h);
959 th->suspend_count = 0;
960 if (debug_registers_changed)
962 /* Only change the value of the debug reisters */
963 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
964 th->context.Dr0 = dr[0];
965 th->context.Dr1 = dr[1];
966 th->context.Dr2 = dr[2];
967 th->context.Dr3 = dr[3];
968 /* th->context.Dr6 = dr[6];
969 FIXME: should we set dr6 also ?? */
970 th->context.Dr7 = dr[7];
971 CHECK (SetThreadContext (th->h, &th->context));
972 th->context.ContextFlags = 0;
976 debug_registers_changed = 0;
980 /* Get the next event from the child. Return 1 if the event requires
981 handling by WFI (or whatever).
984 get_child_debug_event (int pid, struct target_waitstatus *ourstatus)
987 DWORD continue_status, event_code;
988 thread_info *th = NULL;
989 static thread_info dummy_thread_info;
992 last_sig = TARGET_SIGNAL_0;
994 if (!(debug_event = WaitForDebugEvent (¤t_event, 1000)))
998 continue_status = DBG_CONTINUE;
1000 event_code = current_event.dwDebugEventCode;
1001 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1005 case CREATE_THREAD_DEBUG_EVENT:
1006 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n",
1007 (unsigned) current_event.dwProcessId,
1008 (unsigned) current_event.dwThreadId,
1009 "CREATE_THREAD_DEBUG_EVENT"));
1010 /* Record the existence of this thread */
1011 th = child_add_thread (current_event.dwThreadId,
1012 current_event.u.CreateThread.hThread);
1014 printf_unfiltered ("[New %s]\n",
1016 pid_to_ptid (current_event.dwThreadId)));
1017 retval = current_event.dwThreadId;
1020 case EXIT_THREAD_DEBUG_EVENT:
1021 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1022 (unsigned) current_event.dwProcessId,
1023 (unsigned) current_event.dwThreadId,
1024 "EXIT_THREAD_DEBUG_EVENT"));
1025 child_delete_thread (current_event.dwThreadId);
1026 th = &dummy_thread_info;
1029 case CREATE_PROCESS_DEBUG_EVENT:
1030 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1031 (unsigned) current_event.dwProcessId,
1032 (unsigned) current_event.dwThreadId,
1033 "CREATE_PROCESS_DEBUG_EVENT"));
1034 CloseHandle (current_event.u.CreateProcessInfo.hFile);
1035 current_process_handle = current_event.u.CreateProcessInfo.hProcess;
1037 main_thread_id = current_event.dwThreadId;
1038 /* Add the main thread */
1040 th = child_add_thread (current_event.dwProcessId,
1041 current_event.u.CreateProcessInfo.hProcess);
1043 th = child_add_thread (main_thread_id,
1044 current_event.u.CreateProcessInfo.hThread);
1045 retval = ourstatus->value.related_pid = current_event.dwThreadId;
1048 case EXIT_PROCESS_DEBUG_EVENT:
1049 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1050 (unsigned) current_event.dwProcessId,
1051 (unsigned) current_event.dwThreadId,
1052 "EXIT_PROCESS_DEBUG_EVENT"));
1053 ourstatus->kind = TARGET_WAITKIND_EXITED;
1054 ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode;
1055 CloseHandle (current_process_handle);
1056 retval = main_thread_id;
1059 case LOAD_DLL_DEBUG_EVENT:
1060 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1061 (unsigned) current_event.dwProcessId,
1062 (unsigned) current_event.dwThreadId,
1063 "LOAD_DLL_DEBUG_EVENT"));
1064 CloseHandle (current_event.u.LoadDll.hFile);
1065 catch_errors (handle_load_dll, NULL, (char *) "", RETURN_MASK_ALL);
1066 registers_changed (); /* mark all regs invalid */
1067 ourstatus->kind = TARGET_WAITKIND_LOADED;
1068 ourstatus->value.integer = 0;
1069 retval = main_thread_id;
1072 case UNLOAD_DLL_DEBUG_EVENT:
1073 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1074 (unsigned) current_event.dwProcessId,
1075 (unsigned) current_event.dwThreadId,
1076 "UNLOAD_DLL_DEBUG_EVENT"));
1077 catch_errors (handle_unload_dll, NULL, (char *) "", RETURN_MASK_ALL);
1078 registers_changed (); /* mark all regs invalid */
1079 /* ourstatus->kind = TARGET_WAITKIND_UNLOADED;
1080 does not exist yet. */
1083 case EXCEPTION_DEBUG_EVENT:
1084 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1085 (unsigned) current_event.dwProcessId,
1086 (unsigned) current_event.dwThreadId,
1087 "EXCEPTION_DEBUG_EVENT"));
1088 if (handle_exception (ourstatus))
1089 retval = current_event.dwThreadId;
1092 case OUTPUT_DEBUG_STRING_EVENT: /* message from the kernel */
1093 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
1094 (unsigned) current_event.dwProcessId,
1095 (unsigned) current_event.dwThreadId,
1096 "OUTPUT_DEBUG_STRING_EVENT"));
1097 if (handle_output_debug_string (ourstatus))
1098 retval = main_thread_id;
1102 printf_unfiltered ("gdb: kernel event for pid=%ld tid=%ld\n",
1103 (DWORD) current_event.dwProcessId,
1104 (DWORD) current_event.dwThreadId);
1105 printf_unfiltered (" unknown event code %ld\n",
1106 current_event.dwDebugEventCode);
1111 CHECK (child_continue (continue_status, -1));
1114 current_thread = th ? : thread_rec (current_event.dwThreadId, TRUE);
1115 inferior_ptid = pid_to_ptid (retval);
1122 /* Wait for interesting events to occur in the target process. */
1124 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1126 int pid = PIDGET (ptid);
1128 /* We loop when we get a non-standard exception rather than return
1129 with a SPURIOUS because resume can try and step or modify things,
1130 which needs a current_thread->h. But some of these exceptions mark
1131 the birth or death of threads, which mean that the current thread
1132 isn't necessarily what you think it is. */
1136 int retval = get_child_debug_event (pid, ourstatus);
1138 return pid_to_ptid (retval);
1143 if (ui_loop_hook != NULL)
1144 detach = ui_loop_hook (0);
1147 child_kill_inferior ();
1153 do_initial_child_stuff (DWORD pid)
1155 extern int stop_after_trap;
1158 last_sig = TARGET_SIGNAL_0;
1160 exception_count = 0;
1161 debug_registers_changed = 0;
1162 debug_registers_used = 0;
1163 for (i = 0; i < sizeof (dr) / sizeof (dr[0]); i++)
1165 current_event.dwProcessId = pid;
1166 memset (¤t_event, 0, sizeof (current_event));
1167 push_target (&child_ops);
1168 child_init_thread_list ();
1169 child_clear_solibs ();
1170 clear_proceed_status ();
1171 init_wait_for_inferior ();
1173 target_terminal_init ();
1174 target_terminal_inferior ();
1178 stop_after_trap = 1;
1179 wait_for_inferior ();
1180 if (stop_signal != TARGET_SIGNAL_TRAP)
1181 resume (0, stop_signal);
1185 stop_after_trap = 0;
1189 /* Since Windows XP, detaching from a process is supported by Windows.
1190 The following code tries loading the appropriate functions dynamically.
1191 If loading these functions succeeds use them to actually detach from
1192 the inferior process, otherwise behave as usual, pretending that
1193 detach has worked. */
1194 static BOOL WINAPI (*DebugSetProcessKillOnExit)(BOOL);
1195 static BOOL WINAPI (*DebugActiveProcessStop)(DWORD);
1198 has_detach_ability ()
1200 static HMODULE kernel32 = NULL;
1203 kernel32 = LoadLibrary ("kernel32.dll");
1206 if (!DebugSetProcessKillOnExit)
1207 DebugSetProcessKillOnExit = GetProcAddress (kernel32,
1208 "DebugSetProcessKillOnExit");
1209 if (!DebugActiveProcessStop)
1210 DebugActiveProcessStop = GetProcAddress (kernel32,
1211 "DebugActiveProcessStop");
1212 if (DebugSetProcessKillOnExit && DebugActiveProcessStop)
1218 /* Attach to process PID, then initialize for debugging it. */
1220 child_attach (char *args, int from_tty)
1226 error_no_arg ("process-id to attach");
1228 pid = strtoul (args, 0, 0);
1229 ok = DebugActiveProcess (pid);
1232 error ("Can't attach to process.");
1234 if (has_detach_ability ())
1237 DebugSetProcessKillOnExit (FALSE);
1242 char *exec_file = (char *) get_exec_file (0);
1245 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
1246 target_pid_to_str (pid_to_ptid (pid)));
1248 printf_unfiltered ("Attaching to %s\n",
1249 target_pid_to_str (pid_to_ptid (pid)));
1251 gdb_flush (gdb_stdout);
1254 do_initial_child_stuff (pid);
1255 target_terminal_ours ();
1259 child_detach (char *args, int from_tty)
1263 if (has_detach_ability ())
1265 delete_command (NULL, 0);
1266 child_continue (DBG_CONTINUE, -1);
1267 if (!DebugActiveProcessStop (current_event.dwProcessId))
1269 error ("Can't detach process %lu (error %lu)",
1270 current_event.dwProcessId, GetLastError ());
1273 DebugSetProcessKillOnExit (FALSE);
1275 if (detached && from_tty)
1277 char *exec_file = get_exec_file (0);
1280 printf_unfiltered ("Detaching from program: %s, Pid %lu\n", exec_file,
1281 current_event.dwProcessId);
1282 gdb_flush (gdb_stdout);
1284 inferior_ptid = null_ptid;
1285 unpush_target (&child_ops);
1288 /* Print status information about what we're accessing. */
1291 child_files_info (struct target_ops *ignore)
1293 printf_unfiltered ("\tUsing the running image of %s %s.\n",
1294 attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
1299 child_open (char *arg, int from_tty)
1301 error ("Use the \"run\" command to start a Unix child process.");
1304 /* Start an inferior win32 child process and sets inferior_ptid to its pid.
1305 EXEC_FILE is the file to run.
1306 ALLARGS is a string containing the arguments to the program.
1307 ENV is the environment vector to pass. Errors reported with error(). */
1310 child_create_inferior (char *exec_file, char *allargs, char **env)
1312 char real_path[MAXPATHLEN];
1318 PROCESS_INFORMATION pi;
1324 error ("No executable specified, use `target exec'.\n");
1326 memset (&si, 0, sizeof (si));
1327 si.cb = sizeof (si);
1329 cygwin_conv_to_win32_path (exec_file, real_path);
1331 flags = DEBUG_ONLY_THIS_PROCESS;
1334 flags |= CREATE_NEW_PROCESS_GROUP;
1337 flags |= CREATE_NEW_CONSOLE;
1339 args = alloca (strlen (real_path) + strlen (allargs) + 2);
1341 strcpy (args, real_path);
1344 strcat (args, allargs);
1346 /* Prepare the environment vars for CreateProcess. */
1348 /* This code use to assume all env vars were file names and would
1349 translate them all to win32 style. That obviously doesn't work in the
1350 general case. The current rule is that we only translate PATH.
1351 We need to handle PATH because we're about to call CreateProcess and
1352 it uses PATH to find DLL's. Fortunately PATH has a well-defined value
1353 in both posix and win32 environments. cygwin.dll will change it back
1354 to posix style if necessary. */
1356 static const char *conv_path_names[] =
1362 /* CreateProcess takes the environment list as a null terminated set of
1363 strings (i.e. two nulls terminate the list). */
1365 /* Get total size for env strings. */
1366 for (envlen = 0, i = 0; env[i] && *env[i]; i++)
1370 for (j = 0; conv_path_names[j]; j++)
1372 len = strlen (conv_path_names[j]);
1373 if (strncmp (conv_path_names[j], env[i], len) == 0)
1375 if (cygwin_posix_path_list_p (env[i] + len))
1377 + cygwin_posix_to_win32_path_list_buf_size (env[i] + len);
1379 envlen += strlen (env[i]) + 1;
1383 if (conv_path_names[j] == NULL)
1384 envlen += strlen (env[i]) + 1;
1387 winenv = alloca (envlen + 1);
1389 /* Copy env strings into new buffer. */
1390 for (temp = winenv, i = 0; env[i] && *env[i]; i++)
1394 for (j = 0; conv_path_names[j]; j++)
1396 len = strlen (conv_path_names[j]);
1397 if (strncmp (conv_path_names[j], env[i], len) == 0)
1399 if (cygwin_posix_path_list_p (env[i] + len))
1401 memcpy (temp, env[i], len);
1402 cygwin_posix_to_win32_path_list (env[i] + len, temp + len);
1405 strcpy (temp, env[i]);
1409 if (conv_path_names[j] == NULL)
1410 strcpy (temp, env[i]);
1412 temp += strlen (temp) + 1;
1415 /* Final nil string to terminate new env. */
1419 ret = CreateProcess (0,
1420 args, /* command line */
1421 NULL, /* Security */
1423 TRUE, /* inherit handles */
1424 flags, /* start flags */
1426 NULL, /* current directory */
1430 error ("Error creating process %s, (error %d)\n", exec_file, GetLastError ());
1432 CloseHandle (pi.hThread);
1433 CloseHandle (pi.hProcess);
1434 do_initial_child_stuff (pi.dwProcessId);
1436 /* child_continue (DBG_CONTINUE, -1); */
1437 proceed ((CORE_ADDR) - 1, TARGET_SIGNAL_0, 0);
1441 child_mourn_inferior (void)
1443 (void) child_continue (DBG_CONTINUE, -1);
1444 i386_cleanup_dregs();
1445 unpush_target (&child_ops);
1446 generic_mourn_inferior ();
1449 /* Send a SIGINT to the process group. This acts just like the user typed a
1450 ^C on the controlling terminal. */
1455 DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n"));
1456 CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT, current_event.dwProcessId));
1457 registers_changed (); /* refresh register state */
1461 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
1462 int write, struct mem_attrib *mem,
1463 struct target_ops *target)
1468 DEBUG_MEM (("gdb: write target memory, %d bytes at 0x%08lx\n",
1469 len, (DWORD) memaddr));
1470 WriteProcessMemory (current_process_handle, (LPVOID) memaddr, our,
1472 FlushInstructionCache (current_process_handle, (LPCVOID) memaddr, len);
1476 DEBUG_MEM (("gdb: read target memory, %d bytes at 0x%08lx\n",
1477 len, (DWORD) memaddr));
1478 ReadProcessMemory (current_process_handle, (LPCVOID) memaddr, our, len,
1485 child_kill_inferior (void)
1487 CHECK (TerminateProcess (current_process_handle, 0));
1491 if (!child_continue (DBG_CONTINUE, -1))
1493 if (!WaitForDebugEvent (¤t_event, INFINITE))
1495 if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
1499 CHECK (CloseHandle (current_process_handle));
1501 /* this may fail in an attached process so don't check. */
1502 (void) CloseHandle (current_thread->h);
1503 target_mourn_inferior (); /* or just child_mourn_inferior? */
1507 child_resume (ptid_t ptid, int step, enum target_signal sig)
1510 DWORD continue_status = DBG_CONTINUE;
1512 int pid = PIDGET (ptid);
1514 if (sig != TARGET_SIGNAL_0)
1516 if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
1518 DEBUG_EXCEPT(("Cannot continue with signal %d here.\n",sig));
1520 else if (sig == last_sig)
1521 continue_status = DBG_EXCEPTION_NOT_HANDLED;
1524 /* This code does not seem to work, because
1525 the kernel does probably not consider changes in the ExceptionRecord
1526 structure when passing the exception to the inferior.
1527 Note that this seems possible in the exception handler itself. */
1530 for (i = 0; xlate[i].them != -1; i++)
1531 if (xlate[i].us == sig)
1533 current_event.u.Exception.ExceptionRecord.ExceptionCode =
1535 continue_status = DBG_EXCEPTION_NOT_HANDLED;
1538 if (continue_status == DBG_CONTINUE)
1540 DEBUG_EXCEPT(("Cannot continue with signal %d.\n",sig));
1544 DEBUG_EXCEPT(("Can only continue with recieved signal %d.\n",
1548 last_sig = TARGET_SIGNAL_0;
1550 DEBUG_EXEC (("gdb: child_resume (pid=%d, step=%d, sig=%d);\n",
1553 /* Get context for currently selected thread */
1554 th = thread_rec (current_event.dwThreadId, FALSE);
1559 /* Single step by setting t bit */
1560 child_fetch_inferior_registers (PS_REGNUM);
1561 th->context.EFlags |= FLAG_TRACE_BIT;
1564 if (th->context.ContextFlags)
1566 if (debug_registers_changed)
1568 th->context.Dr0 = dr[0];
1569 th->context.Dr1 = dr[1];
1570 th->context.Dr2 = dr[2];
1571 th->context.Dr3 = dr[3];
1572 /* th->context.Dr6 = dr[6];
1573 FIXME: should we set dr6 also ?? */
1574 th->context.Dr7 = dr[7];
1576 CHECK (SetThreadContext (th->h, &th->context));
1577 th->context.ContextFlags = 0;
1581 /* Allow continuing with the same signal that interrupted us.
1582 Otherwise complain. */
1584 child_continue (continue_status, pid);
1588 child_prepare_to_store (void)
1590 /* Do nothing, since we can store individual regs */
1594 child_can_run (void)
1602 DEBUG_EVENTS (("gdb: child_close, inferior_ptid=%d\n",
1603 PIDGET (inferior_ptid)));
1606 struct target_ops child_ops;
1609 init_child_ops (void)
1611 child_ops.to_shortname = "child";
1612 child_ops.to_longname = "Win32 child process";
1613 child_ops.to_doc = "Win32 child process (started by the \"run\" command).";
1614 child_ops.to_open = child_open;
1615 child_ops.to_close = child_close;
1616 child_ops.to_attach = child_attach;
1617 child_ops.to_detach = child_detach;
1618 child_ops.to_resume = child_resume;
1619 child_ops.to_wait = child_wait;
1620 child_ops.to_fetch_registers = child_fetch_inferior_registers;
1621 child_ops.to_store_registers = child_store_inferior_registers;
1622 child_ops.to_prepare_to_store = child_prepare_to_store;
1623 child_ops.to_xfer_memory = child_xfer_memory;
1624 child_ops.to_files_info = child_files_info;
1625 child_ops.to_insert_breakpoint = memory_insert_breakpoint;
1626 child_ops.to_remove_breakpoint = memory_remove_breakpoint;
1627 child_ops.to_terminal_init = terminal_init_inferior;
1628 child_ops.to_terminal_inferior = terminal_inferior;
1629 child_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1630 child_ops.to_terminal_ours = terminal_ours;
1631 child_ops.to_terminal_info = child_terminal_info;
1632 child_ops.to_kill = child_kill_inferior;
1633 child_ops.to_load = 0;
1634 child_ops.to_lookup_symbol = 0;
1635 child_ops.to_create_inferior = child_create_inferior;
1636 child_ops.to_mourn_inferior = child_mourn_inferior;
1637 child_ops.to_can_run = child_can_run;
1638 child_ops.to_notice_signals = 0;
1639 child_ops.to_thread_alive = win32_child_thread_alive;
1640 child_ops.to_pid_to_str = cygwin_pid_to_str;
1641 child_ops.to_stop = child_stop;
1642 child_ops.to_stratum = process_stratum;
1643 child_ops.DONT_USE = 0;
1644 child_ops.to_has_all_memory = 1;
1645 child_ops.to_has_memory = 1;
1646 child_ops.to_has_stack = 1;
1647 child_ops.to_has_registers = 1;
1648 child_ops.to_has_execution = 1;
1649 child_ops.to_sections = 0;
1650 child_ops.to_sections_end = 0;
1651 child_ops.to_magic = OPS_MAGIC;
1655 _initialize_inftarg (void)
1657 struct cmd_list_element *c;
1661 c = add_com ("dll-symbols", class_files, dll_symbol_command,
1662 "Load dll library symbols from FILE.");
1663 c->completer = filename_completer;
1665 add_com_alias ("sharedlibrary", "dll-symbols", class_alias, 1);
1667 add_show_from_set (add_set_cmd ("new-console", class_support, var_boolean,
1668 (char *) &new_console,
1669 "Set creation of new console when creating child process.",
1673 add_show_from_set (add_set_cmd ("new-group", class_support, var_boolean,
1674 (char *) &new_group,
1675 "Set creation of new group when creating child process.",
1679 add_show_from_set (add_set_cmd ("debugexec", class_support, var_boolean,
1680 (char *) &debug_exec,
1681 "Set whether to display execution in child process.",
1685 add_show_from_set (add_set_cmd ("debugevents", class_support, var_boolean,
1686 (char *) &debug_events,
1687 "Set whether to display kernel events in child process.",
1691 add_show_from_set (add_set_cmd ("debugmemory", class_support, var_boolean,
1692 (char *) &debug_memory,
1693 "Set whether to display memory accesses in child process.",
1697 add_show_from_set (add_set_cmd ("debugexceptions", class_support, var_boolean,
1698 (char *) &debug_exceptions,
1699 "Set whether to display kernel exceptions in child process.",
1703 add_info ("dll", info_dll_command, "Status of loaded DLLs.");
1704 add_info_alias ("sharedlibrary", "dll", 1);
1706 add_target (&child_ops);
1709 /* Hardware watchpoint support, adapted from go32-nat.c code. */
1711 /* Pass the address ADDR to the inferior in the I'th debug register.
1712 Here we just store the address in dr array, the registers will be
1713 actually set up when child_continue is called. */
1715 cygwin_set_dr (int i, CORE_ADDR addr)
1718 internal_error (__FILE__, __LINE__,
1719 "Invalid register %d in cygwin_set_dr.\n", i);
1720 dr[i] = (unsigned) addr;
1721 debug_registers_changed = 1;
1722 debug_registers_used = 1;
1725 /* Pass the value VAL to the inferior in the DR7 debug control
1726 register. Here we just store the address in D_REGS, the watchpoint
1727 will be actually set up in child_wait. */
1729 cygwin_set_dr7 (unsigned val)
1732 debug_registers_changed = 1;
1733 debug_registers_used = 1;
1736 /* Get the value of the DR6 debug status register from the inferior.
1737 Here we just return the value stored in dr[6]
1738 by the last call to thread_rec for current_event.dwThreadId id. */
1740 cygwin_get_dr6 (void)
1746 /* Determine if the thread referenced by "pid" is alive
1747 by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0
1748 it means that the pid has died. Otherwise it is assumed to be alive. */
1750 win32_child_thread_alive (ptid_t ptid)
1752 int pid = PIDGET (ptid);
1754 return WaitForSingleObject (thread_rec (pid, FALSE)->h, 0) == WAIT_OBJECT_0 ?
1758 /* Convert pid to printable format. */
1760 cygwin_pid_to_str (ptid_t ptid)
1762 static char buf[80];
1763 int pid = PIDGET (ptid);
1765 if ((DWORD) pid == current_event.dwProcessId)
1766 sprintf (buf, "process %d", pid);
1768 sprintf (buf, "thread %ld.0x%x", current_event.dwProcessId, pid);
1773 core_dll_symbols_add (char *dll_name, DWORD base_addr)
1775 struct objfile *objfile;
1776 char *objfile_basename;
1777 const char *dll_basename;
1779 if (!(dll_basename = strrchr (dll_name, '/')))
1780 dll_basename = dll_name;
1784 ALL_OBJFILES (objfile)
1786 objfile_basename = strrchr (objfile->name, '/');
1788 if (objfile_basename &&
1789 strcmp (dll_basename, objfile_basename + 1) == 0)
1791 printf_unfiltered ("%08lx:%s (symbols previously loaded)\n",
1792 base_addr, dll_name);
1797 register_loaded_dll (dll_name, base_addr + 0x1000);
1798 solib_symbols_add (dll_name, 0, (CORE_ADDR) base_addr + 0x1000);
1806 struct target_ops *target;
1809 map_code_section_args;
1812 map_single_dll_code_section (bfd * abfd, asection * sect, void *obj)
1816 struct section_table *new_target_sect_ptr;
1818 map_code_section_args *args = (map_code_section_args *) obj;
1819 struct target_ops *target = args->target;
1820 if (sect->flags & SEC_CODE)
1822 update_coreops = core_ops.to_sections == target->to_sections;
1824 if (target->to_sections)
1826 old = target->to_sections_end - target->to_sections;
1827 target->to_sections = (struct section_table *)
1828 xrealloc ((char *) target->to_sections,
1829 (sizeof (struct section_table)) * (1 + old));
1834 target->to_sections = (struct section_table *)
1835 xmalloc ((sizeof (struct section_table)));
1837 target->to_sections_end = target->to_sections + (1 + old);
1839 /* Update the to_sections field in the core_ops structure
1843 core_ops.to_sections = target->to_sections;
1844 core_ops.to_sections_end = target->to_sections_end;
1846 new_target_sect_ptr = target->to_sections + old;
1847 new_target_sect_ptr->addr = args->addr + bfd_section_vma (abfd, sect);
1848 new_target_sect_ptr->endaddr = args->addr + bfd_section_vma (abfd, sect) +
1849 bfd_section_size (abfd, sect);;
1850 new_target_sect_ptr->the_bfd_section = sect;
1851 new_target_sect_ptr->bfd = abfd;
1856 dll_code_sections_add (const char *dll_name, int base_addr, struct target_ops *target)
1859 map_code_section_args map_args;
1860 asection *lowest_sect;
1862 if (dll_name == NULL || target == NULL)
1864 name = xstrdup (dll_name);
1865 dll_bfd = bfd_openr (name, "pei-i386");
1866 if (dll_bfd == NULL)
1869 if (bfd_check_format (dll_bfd, bfd_object))
1871 lowest_sect = bfd_get_section_by_name (dll_bfd, ".text");
1872 if (lowest_sect == NULL)
1874 map_args.target = target;
1875 map_args.addr = base_addr - bfd_section_vma (dll_bfd, lowest_sect);
1877 bfd_map_over_sections (dll_bfd, &map_single_dll_code_section, (void *) (&map_args));
1884 core_section_load_dll_symbols (bfd * abfd, asection * sect, void *obj)
1886 struct target_ops *target = (struct target_ops *) obj;
1891 char *dll_name = NULL;
1893 struct win32_pstatus *pstatus;
1896 if (strncmp (sect->name, ".module", 7))
1899 buf = (char *) xmalloc (sect->_raw_size + 1);
1902 printf_unfiltered ("memory allocation failed for %s\n", sect->name);
1905 if (!bfd_get_section_contents (abfd, sect, buf, 0, sect->_raw_size))
1908 pstatus = (struct win32_pstatus *) buf;
1910 memmove (&base_addr, &(pstatus->data.module_info.base_address), sizeof (base_addr));
1911 dll_name_size = pstatus->data.module_info.module_name_size;
1912 if (offsetof (struct win32_pstatus, data.module_info.module_name) + dll_name_size > sect->_raw_size)
1915 dll_name = (char *) xmalloc (dll_name_size + 1);
1918 printf_unfiltered ("memory allocation failed for %s\n", sect->name);
1921 strncpy (dll_name, pstatus->data.module_info.module_name, dll_name_size);
1923 while ((p = strchr (dll_name, '\\')))
1926 if (!core_dll_symbols_add (dll_name, (DWORD) base_addr))
1927 printf_unfiltered ("%s: Failed to load dll symbols.\n", dll_name);
1929 if (!dll_code_sections_add (dll_name, (DWORD) base_addr + 0x1000, target))
1930 printf_unfiltered ("%s: Failed to map dll code sections.\n", dll_name);
1941 child_solib_add (char *filename, int from_tty, struct target_ops *target,
1948 child_clear_solibs ();
1949 bfd_map_over_sections (core_bfd, &core_section_load_dll_symbols, target);
1953 if (solib_end && solib_end->name)
1954 solib_end->objfile = solib_symbols_add (solib_end->name, from_tty,
1955 solib_end->load_addr);
1960 fetch_elf_core_registers (char *core_reg_sect,
1961 unsigned core_reg_size,
1966 if (core_reg_size < sizeof (CONTEXT))
1968 error ("Core file register section too small (%u bytes).", core_reg_size);
1971 for (r = 0; r < NUM_REGS; r++)
1972 supply_register (r, core_reg_sect + mappings[r]);
1975 static struct core_fns win32_elf_core_fns =
1977 bfd_target_elf_flavour,
1978 default_check_format,
1979 default_core_sniffer,
1980 fetch_elf_core_registers,
1985 _initialize_core_win32 (void)
1987 add_core_fns (&win32_elf_core_fns);
1991 _initialize_check_for_gdb_ini (void)
1994 if (inhibit_gdbinit)
1997 homedir = getenv ("HOME");
2001 char *oldini = (char *) alloca (strlen (homedir) +
2002 sizeof ("/gdb.ini"));
2003 strcpy (oldini, homedir);
2004 p = strchr (oldini, '\0');
2005 if (p > oldini && p[-1] != '/')
2007 strcpy (p, "gdb.ini");
2008 if (access (oldini, 0) == 0)
2010 int len = strlen (oldini);
2011 char *newini = alloca (len + 1);
2012 sprintf (newini, "%.*s.gdbinit",
2013 (int) (len - (sizeof ("gdb.ini") - 1)), oldini);
2014 warning ("obsolete '%s' found. Rename to '%s'.", oldini, newini);