1 /* Target-vector operations for controlling win32 child processes, for GDB.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions, A Red Hat Company.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without eve nthe implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
23 /* by Steve Chamberlain, sac@cygnus.com */
25 /* We assume we're being built with and will be used for cygwin. */
28 #include "frame.h" /* required by inferior.h */
34 #include <sys/types.h>
40 #else /* other WIN32 compiler */
48 #include "gdb_string.h"
49 #include "gdbthread.h"
51 #include <sys/param.h>
54 /* The ui's event loop. */
55 extern int (*ui_loop_hook
) PARAMS ((int signo
));
57 /* If we're not using the old Cygwin header file set, define the
58 following which never should have been in the generic Win32 API
59 headers in the first place since they were our own invention... */
60 #ifndef _GNU_H_WINDOWS_H
61 #define FLAG_TRACE_BIT 0x100
62 #define CONTEXT_DEBUGGER (CONTEXT_FULL | CONTEXT_FLOATING_POINT)
65 /* The string sent by cygwin when it processes a signal.
66 FIXME: This should be in a cygwin include file. */
67 #define CYGWIN_SIGNAL_STRING "cygwin: signal"
69 #define CHECK(x) check (x, __FILE__,__LINE__)
70 #define DEBUG_EXEC(x) if (debug_exec) printf x
71 #define DEBUG_EVENTS(x) if (debug_events) printf x
72 #define DEBUG_MEM(x) if (debug_memory) printf x
73 #define DEBUG_EXCEPT(x) if (debug_exceptions) printf x
75 /* Forward declaration */
76 extern struct target_ops child_ops
;
78 static void child_stop
PARAMS ((void));
79 static int win32_child_thread_alive
PARAMS ((int));
80 void child_kill_inferior
PARAMS ((void));
82 static int last_sig
= 0; /* Set if a signal was received from the
85 /* Thread information structure used to track information that is
86 not available in gdb's thread structure. */
87 typedef struct thread_info_struct
89 struct thread_info_struct
*next
;
98 static thread_info thread_head
= {NULL
};
100 /* The process and thread handles for the above context. */
102 static DEBUG_EVENT current_event
; /* The current debug event from
104 static HANDLE current_process_handle
; /* Currently executing process */
105 static thread_info
*current_thread
; /* Info on currently selected thread */
106 static DWORD main_thread_id
; /* Thread ID of the main thread */
107 static int ignore_first_first_chance
= 0; /* True if we should ignore the
108 first first chance exception that we get. */
110 /* Counts of things. */
111 static int exception_count
= 0;
112 static int event_count
= 0;
115 static int new_console
= 0;
116 static int new_group
= 1;
117 static int debug_exec
= 0; /* show execution */
118 static int debug_events
= 0; /* show events from kernel */
119 static int debug_memory
= 0; /* show target memory accesses */
120 static int debug_exceptions
= 0; /* show target exceptions */
122 /* This vector maps GDB's idea of a register's number into an address
123 in the win32 exception context vector.
125 It also contains the bit mask needed to load the register in question.
127 One day we could read a reg, we could inspect the context we
128 already have loaded, if it doesn't have the bit set that we need,
129 we read that set of registers in using GetThreadContext. If the
130 context already contains what we need, we just unpack it. Then to
131 write a register, first we have to ensure that the context contains
132 the other regs of the group, and then we copy the info in and set
135 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
136 static const int mappings
[] =
138 context_offset (Eax
),
139 context_offset (Ecx
),
140 context_offset (Edx
),
141 context_offset (Ebx
),
142 context_offset (Esp
),
143 context_offset (Ebp
),
144 context_offset (Esi
),
145 context_offset (Edi
),
146 context_offset (Eip
),
147 context_offset (EFlags
),
148 context_offset (SegCs
),
149 context_offset (SegSs
),
150 context_offset (SegDs
),
151 context_offset (SegEs
),
152 context_offset (SegFs
),
153 context_offset (SegGs
),
154 context_offset (FloatSave
.RegisterArea
[0 * 10]),
155 context_offset (FloatSave
.RegisterArea
[1 * 10]),
156 context_offset (FloatSave
.RegisterArea
[2 * 10]),
157 context_offset (FloatSave
.RegisterArea
[3 * 10]),
158 context_offset (FloatSave
.RegisterArea
[4 * 10]),
159 context_offset (FloatSave
.RegisterArea
[5 * 10]),
160 context_offset (FloatSave
.RegisterArea
[6 * 10]),
161 context_offset (FloatSave
.RegisterArea
[7 * 10]),
162 context_offset (FloatSave
.ControlWord
),
163 context_offset (FloatSave
.StatusWord
),
164 context_offset (FloatSave
.TagWord
),
165 context_offset (FloatSave
.ErrorSelector
),
166 context_offset (FloatSave
.ErrorOffset
),
167 context_offset (FloatSave
.DataSelector
),
168 context_offset (FloatSave
.DataOffset
),
169 context_offset (FloatSave
.ErrorSelector
)
172 #undef context_offset
174 /* This vector maps the target's idea of an exception (extracted
175 from the DEBUG_EVENT structure) to GDB's idea. */
177 struct xlate_exception
180 enum target_signal us
;
183 static const struct xlate_exception
186 {EXCEPTION_ACCESS_VIOLATION
, TARGET_SIGNAL_SEGV
},
187 {STATUS_STACK_OVERFLOW
, TARGET_SIGNAL_SEGV
},
188 {EXCEPTION_BREAKPOINT
, TARGET_SIGNAL_TRAP
},
189 {DBG_CONTROL_C
, TARGET_SIGNAL_INT
},
190 {EXCEPTION_SINGLE_STEP
, TARGET_SIGNAL_TRAP
},
193 /* Find a thread record given a thread id.
194 If get_context then also retrieve the context for this
197 thread_rec (DWORD id
, int get_context
)
201 for (th
= &thread_head
; (th
= th
->next
) != NULL
;)
204 if (!th
->suspend_count
&& get_context
)
206 if (get_context
> 0 && id
!= current_event
.dwThreadId
)
207 th
->suspend_count
= SuspendThread (th
->h
) + 1;
208 else if (get_context
< 0)
209 th
->suspend_count
= -1;
211 th
->context
.ContextFlags
= CONTEXT_DEBUGGER
;
212 GetThreadContext (th
->h
, &th
->context
);
220 /* Add a thread to the thread list */
222 child_add_thread (DWORD id
, HANDLE h
)
226 if ((th
= thread_rec (id
, FALSE
)))
229 th
= (thread_info
*) xmalloc (sizeof (*th
));
230 memset (th
, 0, sizeof (*th
));
233 th
->next
= thread_head
.next
;
234 thread_head
.next
= th
;
239 /* Clear out any old thread list and reintialize it to a
242 child_init_thread_list ()
244 thread_info
*th
= &thread_head
;
246 DEBUG_EVENTS (("gdb: child_init_thread_list\n"));
248 while (th
->next
!= NULL
)
250 thread_info
*here
= th
->next
;
251 th
->next
= here
->next
;
252 (void) CloseHandle (here
->h
);
257 /* Delete a thread from the list of threads */
259 child_delete_thread (DWORD id
)
264 printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (id
));
267 for (th
= &thread_head
;
268 th
->next
!= NULL
&& th
->next
->id
!= id
;
272 if (th
->next
!= NULL
)
274 thread_info
*here
= th
->next
;
275 th
->next
= here
->next
;
276 CloseHandle (here
->h
);
282 check (BOOL ok
, const char *file
, int line
)
285 printf_filtered ("error return %s:%d was %d\n", file
, line
, GetLastError ());
289 do_child_fetch_inferior_registers (int r
)
291 char *context_offset
= ((char *) ¤t_thread
->context
) + mappings
[r
];
295 l
= *((long *)context_offset
) & 0xffff;
296 supply_register (r
, (char *) &l
);
298 else if (r
== FOP_REGNUM
)
300 l
= (*((long *)context_offset
) >> 16) & ((1 << 11) - 1);
301 supply_register (r
, (char *) &l
);
304 supply_register (r
, context_offset
);
307 for (r
= 0; r
< NUM_REGS
; r
++)
308 do_child_fetch_inferior_registers (r
);
313 child_fetch_inferior_registers (int r
)
315 current_thread
= thread_rec (inferior_pid
, TRUE
);
316 do_child_fetch_inferior_registers (r
);
320 do_child_store_inferior_registers (int r
)
323 read_register_gen (r
, ((char *) ¤t_thread
->context
) + mappings
[r
]);
326 for (r
= 0; r
< NUM_REGS
; r
++)
327 do_child_store_inferior_registers (r
);
331 /* Store a new register value into the current thread context */
333 child_store_inferior_registers (int r
)
335 current_thread
= thread_rec (inferior_pid
, TRUE
);
336 do_child_store_inferior_registers (r
);
340 static int psapi_loaded
= 0;
341 static HMODULE psapi_module_handle
= NULL
;
342 static BOOL
WINAPI (*psapi_EnumProcessModules
)(HANDLE
, HMODULE
*, DWORD
, LPDWORD
)= NULL
;
343 static BOOL
WINAPI (*psapi_GetModuleInformation
) (HANDLE
, HMODULE
, LPMODULEINFO
, DWORD
)= NULL
;
344 static DWORD
WINAPI (*psapi_GetModuleFileNameExA
) (HANDLE
, HMODULE
, LPSTR
, DWORD
)= NULL
;
346 int psapi_get_dll_name (DWORD BaseAddress
, char *dll_name_ret
)
351 HMODULE dh_buf
[ 1 ];
352 HMODULE
* DllHandle
= dh_buf
;
357 psapi_EnumProcessModules
== NULL
||
358 psapi_GetModuleInformation
== NULL
||
359 psapi_GetModuleFileNameExA
== NULL
)
361 if (psapi_loaded
)goto failed
;
363 psapi_module_handle
= LoadLibrary ("psapi.dll");
364 if (!psapi_module_handle
)
366 /* printf_unfiltered ("error loading psapi.dll: %u", GetLastError ());*/
369 psapi_EnumProcessModules
= GetProcAddress (psapi_module_handle
, "EnumProcessModules" );
370 psapi_GetModuleInformation
= GetProcAddress (psapi_module_handle
, "GetModuleInformation");
371 psapi_GetModuleFileNameExA
= (void *) GetProcAddress (psapi_module_handle
,
372 "GetModuleFileNameExA");
373 if (psapi_EnumProcessModules
== NULL
||
374 psapi_GetModuleInformation
== NULL
||
375 psapi_GetModuleFileNameExA
== NULL
)
380 ok
= (*psapi_EnumProcessModules
) (current_process_handle
,
385 if (!ok
|| !cbNeeded
)
388 DllHandle
= (HMODULE
*) alloca (cbNeeded
);
392 ok
= (*psapi_EnumProcessModules
) (current_process_handle
,
399 for (i
= 0; i
< cbNeeded
/ sizeof (HMODULE
); i
++)
401 if (!(*psapi_GetModuleInformation
) (current_process_handle
,
405 error ("Can't get module info");
407 len
= (*psapi_GetModuleFileNameExA
) (current_process_handle
,
412 error ("Error getting dll name: %u\n", GetLastError ());
414 if ((DWORD
) (mi
.lpBaseOfDll
) == BaseAddress
)
419 dll_name_ret
[0] = '\0';
423 struct safe_symbol_file_add_args
427 struct section_addr_info
*addrs
;
434 safe_symbol_file_add_stub (void *argv
)
436 #define p ((struct safe_symbol_file_add_args *)argv)
437 p
->ret
= symbol_file_add (p
->name
, p
->from_tty
, p
->addrs
, p
->mainline
, p
->flags
);
443 safe_symbol_file_add_cleanup (void *gdb_stderrv
)
445 ui_file_delete (gdb_stderr
);
446 gdb_stderr
= (struct ui_file
*)gdb_stderrv
;
449 static struct objfile
*
450 safe_symbol_file_add (char *name
, int from_tty
,
451 struct section_addr_info
*addrs
,
452 int mainline
, int flags
)
455 struct safe_symbol_file_add_args p
;
456 struct cleanup
*cleanup
;
458 cleanup
= make_cleanup (safe_symbol_file_add_cleanup
, gdb_stderr
);
460 gdb_stderr
= ui_file_new ();
462 p
.from_tty
= from_tty
;
464 p
.mainline
= mainline
;
466 catch_errors (safe_symbol_file_add_stub
, &p
, "", RETURN_MASK_ERROR
);
468 do_cleanups (cleanup
);
472 /* Wait for child to do something. Return pid of child, or -1 in case
473 of error; store status through argument pointer OURSTATUS. */
476 handle_load_dll (PTR dummy
)
478 LOAD_DLL_DEBUG_INFO
*event
= ¤t_event
.u
.LoadDll
;
481 char dll_buf
[MAX_PATH
+ 1];
482 char *p
, *dll_name
= NULL
;
483 struct objfile
*objfile
;
484 MEMORY_BASIC_INFORMATION minfo
;
485 struct section_addr_info section_addrs
;
487 memset (§ion_addrs
, 0, sizeof (section_addrs
));
488 dll_buf
[0] = dll_buf
[sizeof (dll_buf
) - 1] = '\0';
490 if (!psapi_get_dll_name ((DWORD
) (event
->lpBaseOfDll
), dll_buf
))
491 dll_buf
[0] = dll_buf
[sizeof(dll_buf
) - 1] = '\0';
495 /* Attempt to read the name of the dll that was detected.
496 This is documented to work only when actively debugging
497 a program. It will not work for attached processes. */
498 if (dll_name
== NULL
|| *dll_name
== '\0')
500 int size
= event
->fUnicode
? sizeof (WCHAR
) : sizeof (char);
504 ReadProcessMemory (current_process_handle
,
505 (LPCVOID
) event
->lpImageName
,
506 (char *) &dll_name_ptr
,
507 sizeof (dll_name_ptr
), &done
);
509 /* See if we could read the address of a string, and that the
510 address isn't null. */
512 if (done
!= sizeof (dll_name_ptr
) || !dll_name_ptr
)
517 ReadProcessMemory (current_process_handle
,
518 (LPCVOID
) (dll_name_ptr
+ len
* size
),
524 while ((b
[0] != 0 || b
[size
- 1] != 0) && done
== size
);
526 dll_name
= alloca (len
);
530 WCHAR
*unicode_dll_name
= (WCHAR
*) alloca (len
* sizeof (WCHAR
));
531 ReadProcessMemory (current_process_handle
,
532 (LPCVOID
) dll_name_ptr
,
534 len
* sizeof (WCHAR
),
537 WideCharToMultiByte (CP_ACP
, 0,
538 unicode_dll_name
, len
,
539 dll_name
, len
, 0, 0);
543 ReadProcessMemory (current_process_handle
,
544 (LPCVOID
) dll_name_ptr
,
554 while ((p
= strchr (dll_name
, '\\')))
557 /* The symbols in a dll are offset by 0x1000, which is the
558 the offset from 0 of the first byte in an image - because
559 of the file header and the section alignment. */
561 section_addrs
.text_addr
= (int) event
->lpBaseOfDll
+ 0x1000;
562 safe_symbol_file_add (dll_name
, 0, §ion_addrs
, 0, OBJF_SHARED
);
563 printf_unfiltered ("%x:%s\n", event
->lpBaseOfDll
, dll_name
);
568 /* Handle DEBUG_STRING output from child process.
569 Cygwin prepends its messages with a "cygwin:". Interpret this as
570 a Cygwin signal. Otherwise just print the string as a warning. */
572 handle_output_debug_string (struct target_waitstatus
*ourstatus
)
577 if (!target_read_string
578 ((CORE_ADDR
) current_event
.u
.DebugString
.lpDebugStringData
, &s
, 1024, 0)
582 if (strncmp (s
, CYGWIN_SIGNAL_STRING
, sizeof (CYGWIN_SIGNAL_STRING
) - 1) != 0)
584 if (strncmp (s
, "cYg", 3) != 0)
590 int sig
= strtol (s
+ sizeof (CYGWIN_SIGNAL_STRING
) - 1, &p
, 0);
591 gotasig
= target_signal_from_host (sig
);
592 ourstatus
->value
.sig
= gotasig
;
594 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
602 handle_exception (struct target_waitstatus
*ourstatus
)
607 int fc
= ignore_first_first_chance
;
609 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
611 ignore_first_first_chance
= 0;
613 /* Record the context of the current thread */
614 th
= thread_rec (current_event
.dwThreadId
, -1);
618 switch (current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
)
620 case EXCEPTION_ACCESS_VIOLATION
:
621 DEBUG_EXCEPT (("gdb: Target exception ACCESS_VIOLATION at 0x%08x\n",
622 current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
));
623 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
626 case STATUS_FLOAT_UNDERFLOW
:
627 case STATUS_FLOAT_DIVIDE_BY_ZERO
:
628 case STATUS_FLOAT_OVERFLOW
:
629 case STATUS_INTEGER_DIVIDE_BY_ZERO
:
630 DEBUG_EXCEPT (("gdb: Target exception STACK_OVERFLOW at 0x%08x\n",
631 current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
));
632 ourstatus
->value
.sig
= TARGET_SIGNAL_FPE
;
634 case STATUS_STACK_OVERFLOW
:
635 DEBUG_EXCEPT (("gdb: Target exception STACK_OVERFLOW at 0x%08x\n",
636 current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
));
637 ourstatus
->value
.sig
= TARGET_SIGNAL_SEGV
;
639 case EXCEPTION_BREAKPOINT
:
640 if (fc
&& current_event
.u
.Exception
.dwFirstChance
&&
641 ((DWORD
) current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
& 0xc0000000))
646 DEBUG_EXCEPT (("gdb: Target exception BREAKPOINT at 0x%08x\n",
647 current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
));
648 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
651 DEBUG_EXCEPT (("gdb: Target exception CONTROL_C at 0x%08x\n",
652 current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
));
653 ourstatus
->value
.sig
= TARGET_SIGNAL_INT
;
654 last_sig
= SIGINT
; /* FIXME - should check pass state */
656 case EXCEPTION_SINGLE_STEP
:
657 DEBUG_EXCEPT (("gdb: Target exception SINGLE_STEP at 0x%08x\n",
658 current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
));
659 ourstatus
->value
.sig
= TARGET_SIGNAL_TRAP
;
661 case EXCEPTION_ILLEGAL_INSTRUCTION
:
662 DEBUG_EXCEPT (("gdb: Target exception SINGLE_ILL at 0x%08x\n",
663 current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
));
664 ourstatus
->value
.sig
= TARGET_SIGNAL_ILL
;
668 printf_unfiltered ("gdb: unknown target exception 0x%08x at 0x%08x\n",
669 current_event
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
670 current_event
.u
.Exception
.ExceptionRecord
.ExceptionAddress
);
671 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
678 /* Resume all artificially suspended threads if we are continuing
681 child_continue (DWORD continue_status
, int id
)
687 DEBUG_EVENTS (("ContinueDebugEvent (cpid=%d, ctid=%d, DBG_CONTINUE);\n",
688 current_event
.dwProcessId
, current_event
.dwThreadId
));
689 res
= ContinueDebugEvent (current_event
.dwProcessId
,
690 current_event
.dwThreadId
,
694 for (th
= &thread_head
; (th
= th
->next
) != NULL
;)
695 if (((id
== -1) || (id
== th
->id
)) && th
->suspend_count
)
697 for (i
= 0; i
< th
->suspend_count
; i
++)
698 (void) ResumeThread (th
->h
);
699 th
->suspend_count
= 0;
705 /* Get the next event from the child. Return 1 if the event requires
706 handling by WFI (or whatever).
709 get_child_debug_event (int pid
, struct target_waitstatus
*ourstatus
,
710 DWORD target_event_code
, int *retval
)
714 DWORD continue_status
, event_code
;
715 thread_info
*th
= NULL
;
716 static thread_info dummy_thread_info
;
718 if (!(debug_event
= WaitForDebugEvent (¤t_event
, 1000)))
725 continue_status
= DBG_CONTINUE
;
728 event_code
= current_event
.dwDebugEventCode
;
729 breakout
= event_code
== target_event_code
;
733 case CREATE_THREAD_DEBUG_EVENT
:
734 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n",
735 (unsigned) current_event
.dwProcessId
,
736 (unsigned) current_event
.dwThreadId
,
737 "CREATE_THREAD_DEBUG_EVENT"));
738 /* Record the existence of this thread */
739 th
= child_add_thread (current_event
.dwThreadId
,
740 current_event
.u
.CreateThread
.hThread
);
742 printf_unfiltered ("[New %s]\n",
743 target_pid_to_str (current_event
.dwThreadId
));
746 case EXIT_THREAD_DEBUG_EVENT
:
747 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
748 (unsigned) current_event
.dwProcessId
,
749 (unsigned) current_event
.dwThreadId
,
750 "EXIT_THREAD_DEBUG_EVENT"));
751 child_delete_thread (current_event
.dwThreadId
);
752 th
= &dummy_thread_info
;
755 case CREATE_PROCESS_DEBUG_EVENT
:
756 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
757 (unsigned) current_event
.dwProcessId
,
758 (unsigned) current_event
.dwThreadId
,
759 "CREATE_PROCESS_DEBUG_EVENT"));
760 current_process_handle
= current_event
.u
.CreateProcessInfo
.hProcess
;
762 main_thread_id
= inferior_pid
= current_event
.dwThreadId
;
763 /* Add the main thread */
764 th
= child_add_thread (inferior_pid
,
765 current_event
.u
.CreateProcessInfo
.hThread
);
768 case EXIT_PROCESS_DEBUG_EVENT
:
769 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
770 (unsigned) current_event
.dwProcessId
,
771 (unsigned) current_event
.dwThreadId
,
772 "EXIT_PROCESS_DEBUG_EVENT"));
773 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
774 ourstatus
->value
.integer
= current_event
.u
.ExitProcess
.dwExitCode
;
775 CloseHandle (current_process_handle
);
776 *retval
= current_event
.dwProcessId
;
780 case LOAD_DLL_DEBUG_EVENT
:
781 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
782 (unsigned) current_event
.dwProcessId
,
783 (unsigned) current_event
.dwThreadId
,
784 "LOAD_DLL_DEBUG_EVENT"));
785 catch_errors (handle_load_dll
, NULL
, (char *) "", RETURN_MASK_ALL
);
786 registers_changed (); /* mark all regs invalid */
789 case UNLOAD_DLL_DEBUG_EVENT
:
790 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
791 (unsigned) current_event
.dwProcessId
,
792 (unsigned) current_event
.dwThreadId
,
793 "UNLOAD_DLL_DEBUG_EVENT"));
794 break; /* FIXME: don't know what to do here */
796 case EXCEPTION_DEBUG_EVENT
:
797 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
798 (unsigned) current_event
.dwProcessId
,
799 (unsigned) current_event
.dwThreadId
,
800 "EXCEPTION_DEBUG_EVENT"));
801 if (handle_exception (ourstatus
))
802 *retval
= current_event
.dwThreadId
;
806 continue_status
= DBG_EXCEPTION_NOT_HANDLED
;
811 case OUTPUT_DEBUG_STRING_EVENT
: /* message from the kernel */
812 DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n",
813 (unsigned) current_event
.dwProcessId
,
814 (unsigned) current_event
.dwThreadId
,
815 "OUTPUT_DEBUG_STRING_EVENT"));
816 handle_output_debug_string ( ourstatus
);
819 printf_unfiltered ("gdb: kernel event for pid=%d tid=%d\n",
820 current_event
.dwProcessId
,
821 current_event
.dwThreadId
);
822 printf_unfiltered (" unknown event code %d\n",
823 current_event
.dwDebugEventCode
);
828 current_thread
= th
?: thread_rec (current_event
.dwThreadId
, TRUE
);
830 CHECK (child_continue (continue_status
, -1));
836 /* Wait for interesting events to occur in the target process. */
838 child_wait (int pid
, struct target_waitstatus
*ourstatus
)
843 /* We loop when we get a non-standard exception rather than return
844 with a SPURIOUS because resume can try and step or modify things,
845 which needs a current_thread->h. But some of these exceptions mark
846 the birth or death of threads, which mean that the current thread
847 isn't necessarily what you think it is. */
850 if (get_child_debug_event (pid
, ourstatus
, EXCEPTION_DEBUG_EVENT
, &retval
))
856 if (ui_loop_hook
!= NULL
)
857 detach
= ui_loop_hook (0);
860 child_kill_inferior ();
864 /* Attach to process PID, then initialize for debugging it. */
867 child_attach (args
, from_tty
)
874 error_no_arg ("process-id to attach");
876 current_event
.dwProcessId
= strtoul (args
, 0, 0);
878 ok
= DebugActiveProcess (current_event
.dwProcessId
);
881 error ("Can't attach to process.");
888 char *exec_file
= (char *) get_exec_file (0);
891 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
,
892 target_pid_to_str (current_event
.dwProcessId
));
894 printf_unfiltered ("Attaching to %s\n",
895 target_pid_to_str (current_event
.dwProcessId
));
897 gdb_flush (gdb_stdout
);
900 push_target (&child_ops
);
904 child_detach (args
, from_tty
)
910 char *exec_file
= get_exec_file (0);
913 printf_unfiltered ("Detaching from program: %s %s\n", exec_file
,
914 target_pid_to_str (inferior_pid
));
915 gdb_flush (gdb_stdout
);
918 unpush_target (&child_ops
);
921 /* Print status information about what we're accessing. */
924 child_files_info (ignore
)
925 struct target_ops
*ignore
;
927 printf_unfiltered ("\tUsing the running image of %s %s.\n",
928 attach_flag
? "attached" : "child", target_pid_to_str (inferior_pid
));
933 child_open (arg
, from_tty
)
937 error ("Use the \"run\" command to start a Unix child process.");
940 /* Start an inferior win32 child process and sets inferior_pid to its pid.
941 EXEC_FILE is the file to run.
942 ALLARGS is a string containing the arguments to the program.
943 ENV is the environment vector to pass. Errors reported with error(). */
946 child_create_inferior (exec_file
, allargs
, env
)
951 char real_path
[MAXPATHLEN
];
957 PROCESS_INFORMATION pi
;
958 struct target_waitstatus dummy
;
966 error ("No executable specified, use `target exec'.\n");
969 memset (&si
, 0, sizeof (si
));
972 cygwin32_conv_to_win32_path (exec_file
, real_path
);
974 flags
= DEBUG_ONLY_THIS_PROCESS
;
977 flags
|= CREATE_NEW_PROCESS_GROUP
;
980 flags
|= CREATE_NEW_CONSOLE
;
982 args
= alloca (strlen (real_path
) + strlen (allargs
) + 2);
984 strcpy (args
, real_path
);
987 strcat (args
, allargs
);
989 /* Prepare the environment vars for CreateProcess. */
991 /* This code use to assume all env vars were file names and would
992 translate them all to win32 style. That obviously doesn't work in the
993 general case. The current rule is that we only translate PATH.
994 We need to handle PATH because we're about to call CreateProcess and
995 it uses PATH to find DLL's. Fortunately PATH has a well-defined value
996 in both posix and win32 environments. cygwin.dll will change it back
997 to posix style if necessary. */
999 static const char *conv_path_names
[] =
1005 /* CreateProcess takes the environment list as a null terminated set of
1006 strings (i.e. two nulls terminate the list). */
1008 /* Get total size for env strings. */
1009 for (envlen
= 0, i
= 0; env
[i
] && *env
[i
]; i
++)
1013 for (j
= 0; conv_path_names
[j
]; j
++)
1015 len
= strlen (conv_path_names
[j
]);
1016 if (strncmp (conv_path_names
[j
], env
[i
], len
) == 0)
1018 if (cygwin32_posix_path_list_p (env
[i
] + len
))
1020 + cygwin32_posix_to_win32_path_list_buf_size (env
[i
] + len
);
1022 envlen
+= strlen (env
[i
]) + 1;
1026 if (conv_path_names
[j
] == NULL
)
1027 envlen
+= strlen (env
[i
]) + 1;
1030 winenv
= alloca (envlen
+ 1);
1032 /* Copy env strings into new buffer. */
1033 for (temp
= winenv
, i
= 0; env
[i
] && *env
[i
]; i
++)
1037 for (j
= 0; conv_path_names
[j
]; j
++)
1039 len
= strlen (conv_path_names
[j
]);
1040 if (strncmp (conv_path_names
[j
], env
[i
], len
) == 0)
1042 if (cygwin32_posix_path_list_p (env
[i
] + len
))
1044 memcpy (temp
, env
[i
], len
);
1045 cygwin32_posix_to_win32_path_list (env
[i
] + len
, temp
+ len
);
1048 strcpy (temp
, env
[i
]);
1052 if (conv_path_names
[j
] == NULL
)
1053 strcpy (temp
, env
[i
]);
1055 temp
+= strlen (temp
) + 1;
1058 /* Final nil string to terminate new env. */
1062 ret
= CreateProcess (0,
1063 args
, /* command line */
1064 NULL
, /* Security */
1066 TRUE
, /* inherit handles */
1067 flags
, /* start flags */
1069 NULL
, /* current directory */
1073 error ("Error creating process %s, (error %d)\n", exec_file
, GetLastError ());
1075 exception_count
= 0;
1078 current_process_handle
= pi
.hProcess
;
1079 current_event
.dwProcessId
= pi
.dwProcessId
;
1080 memset (¤t_event
, 0, sizeof (current_event
));
1081 inferior_pid
= current_event
.dwThreadId
= pi
.dwThreadId
;
1082 push_target (&child_ops
);
1083 child_init_thread_list ();
1084 init_wait_for_inferior ();
1085 clear_proceed_status ();
1086 target_terminal_init ();
1087 target_terminal_inferior ();
1089 ignore_first_first_chance
= 1;
1091 /* Run until process and threads are loaded */
1092 while (!get_child_debug_event (inferior_pid
, &dummy
,
1093 CREATE_PROCESS_DEBUG_EVENT
, &ret
))
1096 /* child_continue (DBG_CONTINUE, -1);*/
1097 proceed ((CORE_ADDR
) - 1, TARGET_SIGNAL_0
, 0);
1101 child_mourn_inferior ()
1103 (void) child_continue (DBG_CONTINUE
, -1);
1104 unpush_target (&child_ops
);
1105 generic_mourn_inferior ();
1108 /* Send a SIGINT to the process group. This acts just like the user typed a
1109 ^C on the controlling terminal. */
1114 DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n"));
1115 CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT
, current_event
.dwProcessId
));
1116 registers_changed (); /* refresh register state */
1120 child_xfer_memory (CORE_ADDR memaddr
, char *our
, int len
,
1121 int write
, struct target_ops
*target
)
1126 DEBUG_MEM (("gdb: write target memory, %d bytes at 0x%08x\n",
1128 WriteProcessMemory (current_process_handle
, (LPVOID
) memaddr
, our
,
1130 FlushInstructionCache (current_process_handle
, (LPCVOID
) memaddr
, len
);
1134 DEBUG_MEM (("gdb: read target memory, %d bytes at 0x%08x\n",
1136 ReadProcessMemory (current_process_handle
, (LPCVOID
) memaddr
, our
, len
,
1143 child_kill_inferior (void)
1145 CHECK (TerminateProcess (current_process_handle
, 0));
1149 if (!child_continue (DBG_CONTINUE
, -1))
1151 if (!WaitForDebugEvent (¤t_event
, INFINITE
))
1153 if (current_event
.dwDebugEventCode
== EXIT_PROCESS_DEBUG_EVENT
)
1157 CHECK (CloseHandle (current_process_handle
));
1159 /* this may fail in an attached process so don't check. */
1160 (void) CloseHandle (current_thread
->h
);
1161 target_mourn_inferior (); /* or just child_mourn_inferior? */
1165 child_resume (int pid
, int step
, enum target_signal sig
)
1168 DWORD continue_status
= last_sig
> 0 && last_sig
< NSIG
?
1169 DBG_EXCEPTION_NOT_HANDLED
: DBG_CONTINUE
;
1173 DEBUG_EXEC (("gdb: child_resume (pid=%d, step=%d, sig=%d);\n",
1176 /* Get context for currently selected thread */
1177 th
= thread_rec (current_event
.dwThreadId
, FALSE
);
1181 /* Single step by setting t bit */
1182 child_fetch_inferior_registers (PS_REGNUM
);
1183 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
1187 if (th
->context
.ContextFlags
)
1189 CHECK (SetThreadContext (th
->h
, &th
->context
));
1190 th
->context
.ContextFlags
= 0;
1193 /* Allow continuing with the same signal that interrupted us.
1194 Otherwise complain. */
1196 child_continue (continue_status
, pid
);
1200 child_prepare_to_store ()
1202 /* Do nothing, since we can store individual regs */
1214 DEBUG_EVENTS (("gdb: child_close, inferior_pid=%d\n", inferior_pid
));
1217 struct target_ops child_ops
;
1220 init_child_ops (void)
1222 child_ops
.to_shortname
= "child";
1223 child_ops
.to_longname
= "Win32 child process";
1224 child_ops
.to_doc
= "Win32 child process (started by the \"run\" command).";
1225 child_ops
.to_open
= child_open
;
1226 child_ops
.to_close
= child_close
;
1227 child_ops
.to_attach
= child_attach
;
1228 child_ops
.to_detach
= child_detach
;
1229 child_ops
.to_resume
= child_resume
;
1230 child_ops
.to_wait
= child_wait
;
1231 child_ops
.to_fetch_registers
= child_fetch_inferior_registers
;
1232 child_ops
.to_store_registers
= child_store_inferior_registers
;
1233 child_ops
.to_prepare_to_store
= child_prepare_to_store
;
1234 child_ops
.to_xfer_memory
= child_xfer_memory
;
1235 child_ops
.to_files_info
= child_files_info
;
1236 child_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
1237 child_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
1238 child_ops
.to_terminal_init
= terminal_init_inferior
;
1239 child_ops
.to_terminal_inferior
= terminal_inferior
;
1240 child_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
1241 child_ops
.to_terminal_ours
= terminal_ours
;
1242 child_ops
.to_terminal_info
= child_terminal_info
;
1243 child_ops
.to_kill
= child_kill_inferior
;
1244 child_ops
.to_load
= 0;
1245 child_ops
.to_lookup_symbol
= 0;
1246 child_ops
.to_create_inferior
= child_create_inferior
;
1247 child_ops
.to_mourn_inferior
= child_mourn_inferior
;
1248 child_ops
.to_can_run
= child_can_run
;
1249 child_ops
.to_notice_signals
= 0;
1250 child_ops
.to_thread_alive
= win32_child_thread_alive
;
1251 child_ops
.to_pid_to_str
= cygwin_pid_to_str
;
1252 child_ops
.to_stop
= child_stop
;
1253 child_ops
.to_stratum
= process_stratum
;
1254 child_ops
.DONT_USE
= 0;
1255 child_ops
.to_has_all_memory
= 1;
1256 child_ops
.to_has_memory
= 1;
1257 child_ops
.to_has_stack
= 1;
1258 child_ops
.to_has_registers
= 1;
1259 child_ops
.to_has_execution
= 1;
1260 child_ops
.to_sections
= 0;
1261 child_ops
.to_sections_end
= 0;
1262 child_ops
.to_magic
= OPS_MAGIC
;
1266 _initialize_inftarg ()
1271 (add_set_cmd ("new-console", class_support
, var_boolean
,
1272 (char *) &new_console
,
1273 "Set creation of new console when creating child process.",
1278 (add_set_cmd ("new-group", class_support
, var_boolean
,
1279 (char *) &new_group
,
1280 "Set creation of new group when creating child process.",
1285 (add_set_cmd ("debugexec", class_support
, var_boolean
,
1286 (char *) &debug_exec
,
1287 "Set whether to display execution in child process.",
1292 (add_set_cmd ("debugevents", class_support
, var_boolean
,
1293 (char *) &debug_events
,
1294 "Set whether to display kernel events in child process.",
1299 (add_set_cmd ("debugmemory", class_support
, var_boolean
,
1300 (char *) &debug_memory
,
1301 "Set whether to display memory accesses in child process.",
1306 (add_set_cmd ("debugexceptions", class_support
, var_boolean
,
1307 (char *) &debug_exceptions
,
1308 "Set whether to display kernel exceptions in child process.",
1312 add_target (&child_ops
);
1315 /* Determine if the thread referenced by "pid" is alive
1316 by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0
1317 it means that the pid has died. Otherwise it is assumed to be alive. */
1319 win32_child_thread_alive (int pid
)
1321 return WaitForSingleObject (thread_rec (pid
, FALSE
)->h
, 0) == WAIT_OBJECT_0
?
1325 /* Convert pid to printable format. */
1327 cygwin_pid_to_str (int pid
)
1329 static char buf
[80];
1330 if (pid
== current_event
.dwProcessId
)
1331 sprintf (buf
, "process %d", pid
);
1333 sprintf (buf
, "thread %d.0x%x", current_event
.dwProcessId
, pid
);