Commit | Line | Data |
---|---|---|
24e60978 | 1 | /* Target-vector operations for controlling win32 child processes, for GDB. |
b5edcb45 | 2 | Copyright 1995, 1996, 1997, 1998 Free Software Foundation, Inc. |
24e60978 | 3 | Contributed by Cygnus Support. |
e88c49c3 | 4 | |
24e60978 SC |
5 | This file is part of GDB. |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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 | |
3a4b77d8 JM |
19 | Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. | |
21 | */ | |
24e60978 SC |
22 | |
23 | /* by Steve Chamberlain, sac@cygnus.com */ | |
24 | ||
3cee93ac | 25 | /* We assume we're being built with and will be used for cygwin. */ |
e88c49c3 | 26 | |
24e60978 SC |
27 | #include "defs.h" |
28 | #include "frame.h" /* required by inferior.h */ | |
29 | #include "inferior.h" | |
30 | #include "target.h" | |
31 | #include "wait.h" | |
32 | #include "gdbcore.h" | |
33 | #include "command.h" | |
34 | #include <signal.h> | |
35 | #include <sys/types.h> | |
36 | #include <fcntl.h> | |
cad9cd60 GN |
37 | #include <stdlib.h> |
38 | ||
39 | #ifdef _MSC_VER | |
28444bf3 | 40 | #include "windefs.h" |
cad9cd60 GN |
41 | #else /* other WIN32 compiler */ |
42 | #include <windows.h> | |
1e37c281 | 43 | #include <imagehlp.h> |
cad9cd60 GN |
44 | #endif |
45 | ||
24e60978 | 46 | #include "buildsym.h" |
1ef980b9 SC |
47 | #include "symfile.h" |
48 | #include "objfiles.h" | |
24e60978 | 49 | #include "gdb_string.h" |
fdfa3315 | 50 | #include "gdbthread.h" |
24e60978 | 51 | #include "gdbcmd.h" |
1750a5ef | 52 | #include <sys/param.h> |
1e37c281 | 53 | #include <unistd.h> |
24e60978 | 54 | |
0714f9bf SS |
55 | /* The ui's event loop. */ |
56 | extern int (*ui_loop_hook) PARAMS ((int signo)); | |
57 | ||
58 | /* If we're not using the old Cygwin header file set, define the | |
59 | following which never should have been in the generic Win32 API | |
60 | headers in the first place since they were our own invention... */ | |
61 | #ifndef _GNU_H_WINDOWS_H | |
62 | #define FLAG_TRACE_BIT 0x100 | |
63 | #define CONTEXT_DEBUGGER (CONTEXT_FULL | CONTEXT_FLOATING_POINT) | |
64 | #endif | |
65 | ||
3cee93ac CF |
66 | /* The string sent by cygwin when it processes a signal. |
67 | FIXME: This should be in a cygwin include file. */ | |
68 | #define CYGWIN_SIGNAL_STRING "cygwin: signal" | |
69 | ||
1ef980b9 SC |
70 | #define CHECK(x) check (x, __FILE__,__LINE__) |
71 | #define DEBUG_EXEC(x) if (debug_exec) printf x | |
72 | #define DEBUG_EVENTS(x) if (debug_events) printf x | |
73 | #define DEBUG_MEM(x) if (debug_memory) printf x | |
74 | #define DEBUG_EXCEPT(x) if (debug_exceptions) printf x | |
24e60978 SC |
75 | |
76 | /* Forward declaration */ | |
77 | extern struct target_ops child_ops; | |
78 | ||
b607efe7 | 79 | static void child_stop PARAMS ((void)); |
bc8bd256 | 80 | static int win32_child_thread_alive PARAMS ((int)); |
0714f9bf | 81 | void child_kill_inferior PARAMS ((void)); |
3cee93ac | 82 | |
3cee93ac CF |
83 | /* Thread information structure used to track information that is |
84 | not available in gdb's thread structure. */ | |
85 | typedef struct thread_info_struct | |
3a4b77d8 JM |
86 | { |
87 | struct thread_info_struct *next; | |
88 | DWORD id; | |
89 | HANDLE h; | |
90 | char *name; | |
91 | int suspend_count; | |
92 | CONTEXT context; | |
1e37c281 JM |
93 | STACKFRAME sf; |
94 | } thread_info; | |
95 | ||
96 | static thread_info thread_head = {NULL}; | |
24e60978 | 97 | |
1e37c281 JM |
98 | /* The saved state for a continue after breaking back to gdb. */ |
99 | static DWORD continue_status; | |
24e60978 SC |
100 | |
101 | /* The process and thread handles for the above context. */ | |
102 | ||
3cee93ac CF |
103 | static DEBUG_EVENT current_event; /* The current debug event from |
104 | WaitForDebugEvent */ | |
105 | static HANDLE current_process_handle; /* Currently executing process */ | |
106 | static thread_info *current_thread; /* Info on currently selected thread */ | |
3a4b77d8 | 107 | static DWORD main_thread_id; /* Thread ID of the main thread */ |
24e60978 SC |
108 | |
109 | /* Counts of things. */ | |
110 | static int exception_count = 0; | |
111 | static int event_count = 0; | |
112 | ||
113 | /* User options. */ | |
114 | static int new_console = 0; | |
1e37c281 | 115 | static int new_group = 1; |
3a4b77d8 JM |
116 | static int debug_exec = 0; /* show execution */ |
117 | static int debug_events = 0; /* show events from kernel */ | |
118 | static int debug_memory = 0; /* show target memory accesses */ | |
1ef980b9 | 119 | static int debug_exceptions = 0; /* show target exceptions */ |
24e60978 SC |
120 | |
121 | /* This vector maps GDB's idea of a register's number into an address | |
3cee93ac | 122 | in the win32 exception context vector. |
24e60978 | 123 | |
3cee93ac | 124 | It also contains the bit mask needed to load the register in question. |
24e60978 SC |
125 | |
126 | One day we could read a reg, we could inspect the context we | |
127 | already have loaded, if it doesn't have the bit set that we need, | |
128 | we read that set of registers in using GetThreadContext. If the | |
129 | context already contains what we need, we just unpack it. Then to | |
130 | write a register, first we have to ensure that the context contains | |
131 | the other regs of the group, and then we copy the info in and set | |
132 | out bit. */ | |
133 | ||
3cee93ac CF |
134 | #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x)) |
135 | static const int mappings[] = | |
24e60978 | 136 | { |
3a4b77d8 JM |
137 | context_offset (Eax), |
138 | context_offset (Ecx), | |
139 | context_offset (Edx), | |
140 | context_offset (Ebx), | |
141 | context_offset (Esp), | |
142 | context_offset (Ebp), | |
143 | context_offset (Esi), | |
144 | context_offset (Edi), | |
145 | context_offset (Eip), | |
146 | context_offset (EFlags), | |
147 | context_offset (SegCs), | |
148 | context_offset (SegSs), | |
149 | context_offset (SegDs), | |
150 | context_offset (SegEs), | |
151 | context_offset (SegFs), | |
152 | context_offset (SegGs), | |
153 | context_offset (FloatSave.RegisterArea[0 * 10]), | |
154 | context_offset (FloatSave.RegisterArea[1 * 10]), | |
155 | context_offset (FloatSave.RegisterArea[2 * 10]), | |
156 | context_offset (FloatSave.RegisterArea[3 * 10]), | |
157 | context_offset (FloatSave.RegisterArea[4 * 10]), | |
158 | context_offset (FloatSave.RegisterArea[5 * 10]), | |
159 | context_offset (FloatSave.RegisterArea[6 * 10]), | |
160 | context_offset (FloatSave.RegisterArea[7 * 10]), | |
1e37c281 JM |
161 | context_offset (FloatSave.ControlWord), |
162 | context_offset (FloatSave.StatusWord), | |
163 | context_offset (FloatSave.TagWord), | |
164 | context_offset (FloatSave.ErrorSelector), | |
165 | context_offset (FloatSave.ErrorOffset), | |
166 | context_offset (FloatSave.DataSelector), | |
167 | context_offset (FloatSave.DataOffset), | |
d3a09475 | 168 | context_offset (FloatSave.ErrorSelector) |
24e60978 SC |
169 | }; |
170 | ||
d3a09475 JM |
171 | #undef context_offset |
172 | ||
24e60978 SC |
173 | /* This vector maps the target's idea of an exception (extracted |
174 | from the DEBUG_EVENT structure) to GDB's idea. */ | |
175 | ||
176 | struct xlate_exception | |
177 | { | |
178 | int them; | |
179 | enum target_signal us; | |
180 | }; | |
181 | ||
24e60978 SC |
182 | static const struct xlate_exception |
183 | xlate[] = | |
184 | { | |
185 | {EXCEPTION_ACCESS_VIOLATION, TARGET_SIGNAL_SEGV}, | |
9cbf6c0e | 186 | {STATUS_STACK_OVERFLOW, TARGET_SIGNAL_SEGV}, |
24e60978 SC |
187 | {EXCEPTION_BREAKPOINT, TARGET_SIGNAL_TRAP}, |
188 | {DBG_CONTROL_C, TARGET_SIGNAL_INT}, | |
189 | {EXCEPTION_SINGLE_STEP, TARGET_SIGNAL_TRAP}, | |
190 | {-1, -1}}; | |
191 | ||
3cee93ac CF |
192 | /* Find a thread record given a thread id. |
193 | If get_context then also retrieve the context for this | |
194 | thread. */ | |
195 | static thread_info * | |
196 | thread_rec (DWORD id, int get_context) | |
24e60978 | 197 | { |
3cee93ac CF |
198 | thread_info *th; |
199 | ||
3a4b77d8 | 200 | for (th = &thread_head; (th = th->next) != NULL;) |
3cee93ac CF |
201 | if (th->id == id) |
202 | { | |
203 | if (!th->suspend_count && get_context) | |
204 | { | |
205 | if (get_context > 0) | |
206 | th->suspend_count = SuspendThread (th->h) + 1; | |
207 | else if (get_context < 0) | |
208 | th->suspend_count = -1; | |
209 | ||
210 | th->context.ContextFlags = CONTEXT_DEBUGGER; | |
211 | GetThreadContext (th->h, &th->context); | |
212 | } | |
213 | return th; | |
214 | } | |
215 | ||
216 | return NULL; | |
217 | } | |
218 | ||
219 | /* Add a thread to the thread list */ | |
220 | static thread_info * | |
3a4b77d8 | 221 | child_add_thread (DWORD id, HANDLE h) |
3cee93ac CF |
222 | { |
223 | thread_info *th; | |
224 | ||
225 | if ((th = thread_rec (id, FALSE))) | |
226 | return th; | |
227 | ||
228 | th = (thread_info *) xmalloc (sizeof (*th)); | |
3a4b77d8 | 229 | memset (th, 0, sizeof (*th)); |
3cee93ac CF |
230 | th->id = id; |
231 | th->h = h; | |
232 | th->next = thread_head.next; | |
233 | thread_head.next = th; | |
234 | add_thread (id); | |
235 | return th; | |
24e60978 SC |
236 | } |
237 | ||
3cee93ac CF |
238 | /* Clear out any old thread list and reintialize it to a |
239 | pristine state. */ | |
24e60978 | 240 | static void |
3cee93ac | 241 | child_init_thread_list () |
24e60978 | 242 | { |
3cee93ac CF |
243 | thread_info *th = &thread_head; |
244 | ||
245 | DEBUG_EVENTS (("gdb: child_init_thread_list\n")); | |
246 | init_thread_list (); | |
247 | while (th->next != NULL) | |
24e60978 | 248 | { |
3cee93ac CF |
249 | thread_info *here = th->next; |
250 | th->next = here->next; | |
251 | (void) CloseHandle (here->h); | |
252 | free (here); | |
24e60978 | 253 | } |
3cee93ac CF |
254 | } |
255 | ||
256 | /* Delete a thread from the list of threads */ | |
257 | static void | |
258 | child_delete_thread (DWORD id) | |
259 | { | |
260 | thread_info *th; | |
261 | ||
262 | if (info_verbose) | |
263 | printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (id)); | |
264 | delete_thread (id); | |
265 | ||
266 | for (th = &thread_head; | |
267 | th->next != NULL && th->next->id != id; | |
268 | th = th->next) | |
269 | continue; | |
270 | ||
271 | if (th->next != NULL) | |
24e60978 | 272 | { |
3cee93ac CF |
273 | thread_info *here = th->next; |
274 | th->next = here->next; | |
275 | CloseHandle (here->h); | |
276 | free (here); | |
24e60978 SC |
277 | } |
278 | } | |
279 | ||
280 | static void | |
3cee93ac CF |
281 | check (BOOL ok, const char *file, int line) |
282 | { | |
283 | if (!ok) | |
284 | printf_filtered ("error return %s:%d was %d\n", file, line, GetLastError ()); | |
285 | } | |
286 | ||
287 | static void | |
288 | do_child_fetch_inferior_registers (int r) | |
24e60978 | 289 | { |
1e37c281 JM |
290 | char *context_offset = ((char *) ¤t_thread->context) + mappings[r]; |
291 | long l; | |
292 | if (r == FCS_REGNUM) | |
293 | { | |
294 | l = *((long *)context_offset) & 0xffff; | |
295 | supply_register (r, (char *) &l); | |
296 | } | |
297 | else if (r == FOP_REGNUM) | |
298 | { | |
299 | l = (*((long *)context_offset) >> 16) & ((1 << 11) - 1); | |
300 | supply_register (r, (char *) &l); | |
301 | } | |
302 | else if (r >= 0) | |
d3a09475 | 303 | supply_register (r, context_offset); |
3cee93ac | 304 | else |
24e60978 SC |
305 | { |
306 | for (r = 0; r < NUM_REGS; r++) | |
3cee93ac | 307 | do_child_fetch_inferior_registers (r); |
24e60978 | 308 | } |
3cee93ac CF |
309 | } |
310 | ||
311 | static void | |
312 | child_fetch_inferior_registers (int r) | |
313 | { | |
314 | current_thread = thread_rec (inferior_pid, TRUE); | |
315 | do_child_fetch_inferior_registers (r); | |
316 | } | |
317 | ||
318 | static void | |
319 | do_child_store_inferior_registers (int r) | |
320 | { | |
321 | if (r >= 0) | |
322 | read_register_gen (r, ((char *) ¤t_thread->context) + mappings[r]); | |
24e60978 SC |
323 | else |
324 | { | |
3cee93ac CF |
325 | for (r = 0; r < NUM_REGS; r++) |
326 | do_child_store_inferior_registers (r); | |
24e60978 SC |
327 | } |
328 | } | |
329 | ||
3cee93ac CF |
330 | /* Store a new register value into the current thread context */ |
331 | static void | |
332 | child_store_inferior_registers (int r) | |
333 | { | |
334 | current_thread = thread_rec (inferior_pid, TRUE); | |
335 | do_child_store_inferior_registers (r); | |
336 | } | |
24e60978 | 337 | |
1e37c281 JM |
338 | #include <psapi.h> |
339 | static int psapi_loaded = 0; | |
340 | static HMODULE psapi_module_handle = NULL; | |
341 | static BOOL WINAPI (*psapi_EnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD)= NULL; | |
342 | static BOOL WINAPI (*psapi_GetModuleInformation) (HANDLE, HMODULE, LPMODULEINFO, DWORD)= NULL; | |
343 | static DWORD WINAPI (*psapi_GetModuleFileNameExA) (HANDLE, HMODULE, LPSTR, DWORD)= NULL; | |
344 | ||
345 | int psapi_get_dll_name (DWORD BaseAddress, char *dll_name_ret) | |
346 | { | |
347 | DWORD len; | |
348 | MODULEINFO mi; | |
349 | int i; | |
350 | HMODULE dh_buf [ 1 ]; | |
351 | HMODULE* DllHandle = dh_buf; | |
352 | DWORD cbNeeded; | |
353 | BOOL ok; | |
354 | ||
355 | if (!psapi_loaded || | |
356 | psapi_EnumProcessModules == NULL || | |
357 | psapi_GetModuleInformation == NULL || | |
358 | psapi_GetModuleFileNameExA == NULL) | |
359 | { | |
360 | if (psapi_loaded)goto failed; | |
361 | psapi_loaded = 1; | |
362 | psapi_module_handle = LoadLibrary ("psapi.dll"); | |
363 | if (!psapi_module_handle) | |
364 | { | |
365 | /* printf_unfiltered ("error loading psapi.dll: %u", GetLastError ());*/ | |
366 | goto failed; | |
367 | } | |
368 | psapi_EnumProcessModules = GetProcAddress (psapi_module_handle, "EnumProcessModules" ); | |
369 | psapi_GetModuleInformation = GetProcAddress (psapi_module_handle, "GetModuleInformation"); | |
370 | psapi_GetModuleFileNameExA = (void *) GetProcAddress (psapi_module_handle, | |
371 | "GetModuleFileNameExA"); | |
372 | if (psapi_EnumProcessModules == NULL || | |
373 | psapi_GetModuleInformation == NULL || | |
374 | psapi_GetModuleFileNameExA == NULL) | |
375 | goto failed; | |
376 | } | |
377 | ||
378 | cbNeeded = 0; | |
379 | ok = (*psapi_EnumProcessModules) (current_process_handle, | |
380 | DllHandle, | |
381 | sizeof (HMODULE), | |
382 | &cbNeeded); | |
383 | ||
384 | if (!ok || !cbNeeded) | |
385 | goto failed; | |
386 | ||
387 | DllHandle = (HMODULE*) alloca (cbNeeded); | |
388 | if (!DllHandle) | |
389 | goto failed; | |
390 | ||
391 | ok = (*psapi_EnumProcessModules) (current_process_handle, | |
392 | DllHandle, | |
393 | cbNeeded, | |
394 | &cbNeeded); | |
395 | if (!ok) | |
396 | goto failed; | |
397 | ||
398 | for (i = 0; i < cbNeeded / sizeof (HMODULE); i++) | |
399 | { | |
400 | if (!(*psapi_GetModuleInformation) (current_process_handle, | |
401 | DllHandle [i], | |
402 | &mi, | |
403 | sizeof (mi))) | |
404 | error ("Can't get module info"); | |
405 | ||
406 | len = (*psapi_GetModuleFileNameExA) (current_process_handle, | |
407 | DllHandle [i], | |
408 | dll_name_ret, | |
409 | MAX_PATH); | |
410 | if (len == 0) | |
411 | error ("Error getting dll name: %u\n", GetLastError ()); | |
412 | ||
413 | if ((DWORD) (mi.lpBaseOfDll) == BaseAddress) | |
414 | return 1; | |
415 | } | |
416 | ||
417 | failed: | |
418 | dll_name_ret[0] = '\0'; | |
419 | return 0; | |
420 | } | |
421 | ||
24e60978 SC |
422 | /* Wait for child to do something. Return pid of child, or -1 in case |
423 | of error; store status through argument pointer OURSTATUS. */ | |
424 | ||
1750a5ef | 425 | static int |
bc8bd256 | 426 | handle_load_dll (PTR dummy) |
24e60978 | 427 | { |
3a4b77d8 | 428 | LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; |
24e60978 SC |
429 | DWORD dll_name_ptr; |
430 | DWORD done; | |
3cee93ac | 431 | char dll_buf[MAX_PATH + 1]; |
041d852c | 432 | char *p, *dll_name = NULL; |
3cee93ac CF |
433 | struct objfile *objfile; |
434 | MEMORY_BASIC_INFORMATION minfo; | |
3e9c4287 | 435 | struct section_addr_info section_addrs; |
3cee93ac | 436 | |
3e9c4287 | 437 | memset (§ion_addrs, 0, sizeof (section_addrs)); |
3a4b77d8 | 438 | dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0'; |
3cee93ac | 439 | |
1e37c281 JM |
440 | if (!psapi_get_dll_name ((DWORD) (event->lpBaseOfDll), dll_buf)) |
441 | dll_buf[0] = dll_buf[sizeof(dll_buf) - 1] = '\0'; | |
3cee93ac | 442 | |
1e37c281 | 443 | dll_name = dll_buf; |
24e60978 | 444 | |
3cee93ac CF |
445 | /* Attempt to read the name of the dll that was detected. |
446 | This is documented to work only when actively debugging | |
447 | a program. It will not work for attached processes. */ | |
448 | if (dll_name == NULL || *dll_name == '\0') | |
24e60978 | 449 | { |
3cee93ac | 450 | int size = event->fUnicode ? sizeof (WCHAR) : sizeof (char); |
24e60978 SC |
451 | int len = 0; |
452 | char b[2]; | |
3cee93ac CF |
453 | |
454 | ReadProcessMemory (current_process_handle, | |
455 | (LPCVOID) event->lpImageName, | |
456 | (char *) &dll_name_ptr, | |
457 | sizeof (dll_name_ptr), &done); | |
458 | ||
459 | /* See if we could read the address of a string, and that the | |
3a4b77d8 | 460 | address isn't null. */ |
3cee93ac CF |
461 | |
462 | if (done != sizeof (dll_name_ptr) || !dll_name_ptr) | |
463 | return 1; | |
464 | ||
24e60978 SC |
465 | do |
466 | { | |
3cee93ac CF |
467 | ReadProcessMemory (current_process_handle, |
468 | (LPCVOID) (dll_name_ptr + len * size), | |
24e60978 SC |
469 | &b, |
470 | size, | |
471 | &done); | |
472 | len++; | |
473 | } | |
474 | while ((b[0] != 0 || b[size - 1] != 0) && done == size); | |
475 | ||
24e60978 SC |
476 | dll_name = alloca (len); |
477 | ||
3cee93ac | 478 | if (event->fUnicode) |
24e60978 SC |
479 | { |
480 | WCHAR *unicode_dll_name = (WCHAR *) alloca (len * sizeof (WCHAR)); | |
3cee93ac CF |
481 | ReadProcessMemory (current_process_handle, |
482 | (LPCVOID) dll_name_ptr, | |
24e60978 SC |
483 | unicode_dll_name, |
484 | len * sizeof (WCHAR), | |
485 | &done); | |
486 | ||
487 | WideCharToMultiByte (CP_ACP, 0, | |
488 | unicode_dll_name, len, | |
489 | dll_name, len, 0, 0); | |
490 | } | |
491 | else | |
492 | { | |
3cee93ac CF |
493 | ReadProcessMemory (current_process_handle, |
494 | (LPCVOID) dll_name_ptr, | |
24e60978 SC |
495 | dll_name, |
496 | len, | |
497 | &done); | |
498 | } | |
24e60978 | 499 | } |
3cee93ac CF |
500 | |
501 | if (!dll_name) | |
502 | return 1; | |
503 | ||
504 | while ((p = strchr (dll_name, '\\'))) | |
505 | *p = '/'; | |
506 | ||
3cee93ac CF |
507 | /* The symbols in a dll are offset by 0x1000, which is the |
508 | the offset from 0 of the first byte in an image - because | |
509 | of the file header and the section alignment. | |
510 | ||
511 | FIXME: Is this the real reason that we need the 0x1000 ? */ | |
512 | ||
513 | printf_unfiltered ("%x:%s", event->lpBaseOfDll, dll_name); | |
3e9c4287 | 514 | section_addrs.text_addr = (int) event->lpBaseOfDll + 0x1000; |
50a6e31f | 515 | symbol_file_add (dll_name, 0, §ion_addrs, 0, OBJF_SHARED); |
3cee93ac CF |
516 | printf_unfiltered ("\n"); |
517 | ||
1750a5ef | 518 | return 1; |
24e60978 SC |
519 | } |
520 | ||
3cee93ac CF |
521 | /* Handle DEBUG_STRING output from child process. |
522 | Cygwin prepends its messages with a "cygwin:". Interpret this as | |
523 | a Cygwin signal. Otherwise just print the string as a warning. */ | |
524 | static int | |
525 | handle_output_debug_string (struct target_waitstatus *ourstatus) | |
526 | { | |
527 | char *s; | |
528 | int gotasig = FALSE; | |
529 | ||
530 | if (!target_read_string | |
3a4b77d8 | 531 | ((CORE_ADDR) current_event.u.DebugString.lpDebugStringData, &s, 1024, 0) |
3cee93ac CF |
532 | || !s || !*s) |
533 | return gotasig; | |
534 | ||
d3a09475 | 535 | if (strncmp (s, CYGWIN_SIGNAL_STRING, sizeof (CYGWIN_SIGNAL_STRING) - 1) != 0) |
3cee93ac | 536 | { |
d3a09475 | 537 | if (strncmp (s, "cYg", 3) != 0) |
1e37c281 | 538 | warning (s); |
3cee93ac | 539 | } |
d3a09475 | 540 | else |
3cee93ac CF |
541 | { |
542 | char *p; | |
1e37c281 JM |
543 | int sig = strtol (s + sizeof (CYGWIN_SIGNAL_STRING) - 1, &p, 0); |
544 | gotasig = target_signal_from_host (sig); | |
0714f9bf SS |
545 | ourstatus->value.sig = gotasig; |
546 | if (gotasig) | |
3cee93ac CF |
547 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
548 | } | |
549 | ||
550 | free (s); | |
551 | return gotasig; | |
552 | } | |
24e60978 | 553 | |
36339ecd | 554 | static int |
3cee93ac | 555 | handle_exception (struct target_waitstatus *ourstatus) |
24e60978 SC |
556 | { |
557 | int i; | |
558 | int done = 0; | |
3cee93ac CF |
559 | thread_info *th; |
560 | ||
24e60978 SC |
561 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
562 | ||
3cee93ac CF |
563 | /* Record the context of the current thread */ |
564 | th = thread_rec (current_event.dwThreadId, -1); | |
24e60978 | 565 | |
3cee93ac | 566 | switch (current_event.u.Exception.ExceptionRecord.ExceptionCode) |
24e60978 | 567 | { |
1ef980b9 SC |
568 | case EXCEPTION_ACCESS_VIOLATION: |
569 | DEBUG_EXCEPT (("gdb: Target exception ACCESS_VIOLATION at 0x%08x\n", | |
3a4b77d8 | 570 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
1ef980b9 | 571 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; |
1e37c281 | 572 | continue_status = DBG_EXCEPTION_NOT_HANDLED; |
1ef980b9 SC |
573 | break; |
574 | case STATUS_STACK_OVERFLOW: | |
575 | DEBUG_EXCEPT (("gdb: Target exception STACK_OVERFLOW at 0x%08x\n", | |
3a4b77d8 | 576 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
1ef980b9 | 577 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; |
1e37c281 | 578 | continue_status = DBG_EXCEPTION_NOT_HANDLED; |
1ef980b9 SC |
579 | break; |
580 | case EXCEPTION_BREAKPOINT: | |
581 | DEBUG_EXCEPT (("gdb: Target exception BREAKPOINT at 0x%08x\n", | |
3a4b77d8 | 582 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
1ef980b9 SC |
583 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; |
584 | break; | |
585 | case DBG_CONTROL_C: | |
586 | DEBUG_EXCEPT (("gdb: Target exception CONTROL_C at 0x%08x\n", | |
3a4b77d8 | 587 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
1ef980b9 | 588 | ourstatus->value.sig = TARGET_SIGNAL_INT; |
1e37c281 | 589 | continue_status = DBG_EXCEPTION_NOT_HANDLED; |
1ef980b9 SC |
590 | break; |
591 | case EXCEPTION_SINGLE_STEP: | |
592 | DEBUG_EXCEPT (("gdb: Target exception SINGLE_STEP at 0x%08x\n", | |
3a4b77d8 | 593 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
1ef980b9 SC |
594 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; |
595 | break; | |
596 | default: | |
36339ecd | 597 | /* This may be a structured exception handling exception. In |
3a4b77d8 | 598 | that case, we want to let the program try to handle it, and |
d3a09475 | 599 | only break if we see the exception a second time. |
3cee93ac | 600 | if (current_event.u.Exception.dwFirstChance) |
d3a09475 | 601 | |
36339ecd | 602 | return 0; |
d3a09475 | 603 | */ |
36339ecd | 604 | |
1ef980b9 | 605 | printf_unfiltered ("gdb: unknown target exception 0x%08x at 0x%08x\n", |
3a4b77d8 JM |
606 | current_event.u.Exception.ExceptionRecord.ExceptionCode, |
607 | current_event.u.Exception.ExceptionRecord.ExceptionAddress); | |
24e60978 | 608 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; |
1e37c281 | 609 | continue_status = DBG_EXCEPTION_NOT_HANDLED; |
1ef980b9 | 610 | break; |
24e60978 | 611 | } |
24e60978 | 612 | exception_count++; |
36339ecd | 613 | return 1; |
24e60978 SC |
614 | } |
615 | ||
3cee93ac CF |
616 | /* Resume all artificially suspended threads if we are continuing |
617 | execution */ | |
618 | static BOOL | |
1e37c281 | 619 | child_continue (int id) |
3cee93ac CF |
620 | { |
621 | int i; | |
622 | thread_info *th; | |
623 | BOOL res; | |
624 | ||
625 | DEBUG_EVENTS (("ContinueDebugEvent (cpid=%d, ctid=%d, DBG_CONTINUE);\n", | |
626 | current_event.dwProcessId, current_event.dwThreadId)); | |
0714f9bf SS |
627 | res = ContinueDebugEvent (current_event.dwProcessId, |
628 | current_event.dwThreadId, | |
629 | continue_status); | |
1e37c281 | 630 | continue_status = 0; |
0714f9bf | 631 | if (res) |
3a4b77d8 | 632 | for (th = &thread_head; (th = th->next) != NULL;) |
3cee93ac CF |
633 | if (((id == -1) || (id == th->id)) && th->suspend_count) |
634 | { | |
635 | for (i = 0; i < th->suspend_count; i++) | |
636 | (void) ResumeThread (th->h); | |
637 | th->suspend_count = 0; | |
638 | } | |
639 | ||
640 | return res; | |
641 | } | |
642 | ||
1e37c281 JM |
643 | static int |
644 | get_child_debug_event (int pid, struct target_waitstatus *ourstatus, | |
645 | DWORD *event_code, int *retval) | |
646 | { | |
647 | BOOL debug_event; | |
648 | int breakout = 1; | |
649 | ||
650 | if (!(debug_event = WaitForDebugEvent (¤t_event, 20))) | |
651 | { | |
652 | breakout = *retval = *event_code = 0; | |
653 | goto out; | |
654 | } | |
655 | ||
656 | event_count++; | |
657 | continue_status = DBG_CONTINUE; | |
658 | *retval = 0; | |
659 | ||
660 | switch (*event_code = current_event.dwDebugEventCode) | |
661 | { | |
662 | case CREATE_THREAD_DEBUG_EVENT: | |
663 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", | |
664 | (unsigned) current_event.dwProcessId, | |
665 | (unsigned) current_event.dwThreadId, | |
666 | "CREATE_THREAD_DEBUG_EVENT")); | |
667 | /* Record the existence of this thread */ | |
668 | child_add_thread (current_event.dwThreadId, | |
669 | current_event.u.CreateThread.hThread); | |
670 | if (info_verbose) | |
671 | printf_unfiltered ("[New %s]\n", | |
672 | target_pid_to_str (current_event.dwThreadId)); | |
673 | break; | |
674 | ||
675 | case EXIT_THREAD_DEBUG_EVENT: | |
676 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", | |
677 | (unsigned) current_event.dwProcessId, | |
678 | (unsigned) current_event.dwThreadId, | |
679 | "EXIT_THREAD_DEBUG_EVENT")); | |
680 | child_delete_thread (current_event.dwThreadId); | |
681 | break; | |
682 | ||
683 | case CREATE_PROCESS_DEBUG_EVENT: | |
684 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", | |
685 | (unsigned) current_event.dwProcessId, | |
686 | (unsigned) current_event.dwThreadId, | |
687 | "CREATE_PROCESS_DEBUG_EVENT")); | |
688 | current_process_handle = current_event.u.CreateProcessInfo.hProcess; | |
689 | ||
690 | main_thread_id = inferior_pid = current_event.dwThreadId; | |
691 | /* Add the main thread */ | |
692 | current_thread = child_add_thread (inferior_pid, | |
693 | current_event.u.CreateProcessInfo.hThread); | |
694 | break; | |
695 | ||
696 | case EXIT_PROCESS_DEBUG_EVENT: | |
697 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", | |
698 | (unsigned) current_event.dwProcessId, | |
699 | (unsigned) current_event.dwThreadId, | |
700 | "EXIT_PROCESS_DEBUG_EVENT")); | |
701 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
702 | ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; | |
703 | CloseHandle (current_process_handle); | |
704 | *retval = current_event.dwProcessId; | |
705 | goto out; | |
706 | ||
707 | case LOAD_DLL_DEBUG_EVENT: | |
708 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", | |
709 | (unsigned) current_event.dwProcessId, | |
710 | (unsigned) current_event.dwThreadId, | |
711 | "LOAD_DLL_DEBUG_EVENT")); | |
712 | catch_errors (handle_load_dll, NULL, "", RETURN_MASK_ALL); | |
713 | registers_changed (); /* mark all regs invalid */ | |
714 | break; | |
715 | ||
716 | case UNLOAD_DLL_DEBUG_EVENT: | |
717 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", | |
718 | (unsigned) current_event.dwProcessId, | |
719 | (unsigned) current_event.dwThreadId, | |
720 | "UNLOAD_DLL_DEBUG_EVENT")); | |
721 | break; /* FIXME: don't know what to do here */ | |
722 | ||
723 | case EXCEPTION_DEBUG_EVENT: | |
724 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", | |
725 | (unsigned) current_event.dwProcessId, | |
726 | (unsigned) current_event.dwThreadId, | |
727 | "EXCEPTION_DEBUG_EVENT")); | |
728 | if (handle_exception (ourstatus)) /* sets continue_status */ | |
729 | { | |
730 | *retval = current_event.dwThreadId; | |
731 | goto out; | |
732 | } | |
733 | break; | |
734 | ||
735 | case OUTPUT_DEBUG_STRING_EVENT: | |
736 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", | |
737 | (unsigned) current_event.dwProcessId, | |
738 | (unsigned) current_event.dwThreadId, | |
739 | "OUTPUT_DEBUG_STRING_EVENT")); | |
740 | if (handle_output_debug_string (ourstatus)) | |
741 | { | |
742 | *retval = main_thread_id; | |
743 | goto out; | |
744 | } | |
745 | break; | |
746 | default: | |
747 | printf_unfiltered ("gdb: kernel event for pid=%d tid=%d\n", | |
748 | current_event.dwProcessId, | |
749 | current_event.dwThreadId); | |
750 | printf_unfiltered (" unknown event code %d\n", | |
751 | current_event.dwDebugEventCode); | |
752 | break; | |
753 | } | |
754 | ||
755 | breakout = 0; | |
756 | CHECK (child_continue (-1)); | |
757 | continue_status = 0; | |
758 | ||
759 | out: | |
760 | return breakout; | |
761 | } | |
762 | ||
763 | ||
764 | /* Wait for interesting events to occur in the target process. */ | |
24e60978 SC |
765 | static int |
766 | child_wait (int pid, struct target_waitstatus *ourstatus) | |
767 | { | |
1e37c281 JM |
768 | DWORD event_code; |
769 | int retval; | |
770 | ||
24e60978 SC |
771 | /* We loop when we get a non-standard exception rather than return |
772 | with a SPURIOUS because resume can try and step or modify things, | |
3cee93ac | 773 | which needs a current_thread->h. But some of these exceptions mark |
24e60978 SC |
774 | the birth or death of threads, which mean that the current thread |
775 | isn't necessarily what you think it is. */ | |
776 | ||
777 | while (1) | |
778 | { | |
1e37c281 JM |
779 | if (continue_status != 0) |
780 | CHECK (child_continue (-1)); | |
781 | if (get_child_debug_event (pid, ourstatus, &event_code, &retval)) | |
782 | return retval; | |
0714f9bf SS |
783 | else |
784 | { | |
785 | int detach = 0; | |
3cee93ac | 786 | |
0714f9bf SS |
787 | if (ui_loop_hook != NULL) |
788 | detach = ui_loop_hook (0); | |
789 | ||
790 | if (detach) | |
791 | child_kill_inferior (); | |
792 | } | |
24e60978 SC |
793 | } |
794 | } | |
795 | ||
24e60978 SC |
796 | /* Attach to process PID, then initialize for debugging it. */ |
797 | ||
798 | static void | |
799 | child_attach (args, from_tty) | |
800 | char *args; | |
801 | int from_tty; | |
802 | { | |
803 | BOOL ok; | |
804 | ||
805 | if (!args) | |
806 | error_no_arg ("process-id to attach"); | |
807 | ||
3cee93ac | 808 | current_event.dwProcessId = strtoul (args, 0, 0); |
24e60978 | 809 | |
3cee93ac | 810 | ok = DebugActiveProcess (current_event.dwProcessId); |
24e60978 SC |
811 | |
812 | if (!ok) | |
813 | error ("Can't attach to process."); | |
814 | ||
24e60978 SC |
815 | exception_count = 0; |
816 | event_count = 0; | |
817 | ||
818 | if (from_tty) | |
819 | { | |
820 | char *exec_file = (char *) get_exec_file (0); | |
821 | ||
822 | if (exec_file) | |
823 | printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, | |
3cee93ac | 824 | target_pid_to_str (current_event.dwProcessId)); |
24e60978 SC |
825 | else |
826 | printf_unfiltered ("Attaching to %s\n", | |
3cee93ac | 827 | target_pid_to_str (current_event.dwProcessId)); |
24e60978 SC |
828 | |
829 | gdb_flush (gdb_stdout); | |
830 | } | |
831 | ||
24e60978 SC |
832 | push_target (&child_ops); |
833 | } | |
834 | ||
24e60978 SC |
835 | static void |
836 | child_detach (args, from_tty) | |
837 | char *args; | |
838 | int from_tty; | |
839 | { | |
840 | if (from_tty) | |
841 | { | |
842 | char *exec_file = get_exec_file (0); | |
843 | if (exec_file == 0) | |
844 | exec_file = ""; | |
845 | printf_unfiltered ("Detaching from program: %s %s\n", exec_file, | |
846 | target_pid_to_str (inferior_pid)); | |
847 | gdb_flush (gdb_stdout); | |
848 | } | |
849 | inferior_pid = 0; | |
850 | unpush_target (&child_ops); | |
851 | } | |
852 | ||
24e60978 SC |
853 | /* Print status information about what we're accessing. */ |
854 | ||
855 | static void | |
856 | child_files_info (ignore) | |
857 | struct target_ops *ignore; | |
858 | { | |
859 | printf_unfiltered ("\tUsing the running image of %s %s.\n", | |
860 | attach_flag ? "attached" : "child", target_pid_to_str (inferior_pid)); | |
861 | } | |
862 | ||
863 | /* ARGSUSED */ | |
864 | static void | |
865 | child_open (arg, from_tty) | |
866 | char *arg; | |
867 | int from_tty; | |
868 | { | |
869 | error ("Use the \"run\" command to start a Unix child process."); | |
870 | } | |
871 | ||
eb708f2e | 872 | /* Start an inferior win32 child process and sets inferior_pid to its pid. |
24e60978 SC |
873 | EXEC_FILE is the file to run. |
874 | ALLARGS is a string containing the arguments to the program. | |
875 | ENV is the environment vector to pass. Errors reported with error(). */ | |
876 | ||
24e60978 SC |
877 | static void |
878 | child_create_inferior (exec_file, allargs, env) | |
879 | char *exec_file; | |
880 | char *allargs; | |
881 | char **env; | |
882 | { | |
1750a5ef SC |
883 | char real_path[MAXPATHLEN]; |
884 | char *winenv; | |
885 | char *temp; | |
3a4b77d8 | 886 | int envlen; |
1750a5ef | 887 | int i; |
24e60978 SC |
888 | STARTUPINFO si; |
889 | PROCESS_INFORMATION pi; | |
890 | struct target_waitstatus dummy; | |
891 | BOOL ret; | |
892 | DWORD flags; | |
eb708f2e | 893 | char *args; |
1e37c281 | 894 | DWORD event_code; |
24e60978 SC |
895 | |
896 | if (!exec_file) | |
897 | { | |
898 | error ("No executable specified, use `target exec'.\n"); | |
899 | } | |
900 | ||
901 | memset (&si, 0, sizeof (si)); | |
902 | si.cb = sizeof (si); | |
903 | ||
1a443730 | 904 | cygwin32_conv_to_win32_path (exec_file, real_path); |
24e60978 | 905 | |
3cee93ac | 906 | flags = DEBUG_ONLY_THIS_PROCESS; |
24e60978 SC |
907 | |
908 | if (new_group) | |
909 | flags |= CREATE_NEW_PROCESS_GROUP; | |
910 | ||
911 | if (new_console) | |
912 | flags |= CREATE_NEW_CONSOLE; | |
913 | ||
3d78f532 SC |
914 | args = alloca (strlen (real_path) + strlen (allargs) + 2); |
915 | ||
916 | strcpy (args, real_path); | |
eb708f2e | 917 | |
eb708f2e SC |
918 | strcat (args, " "); |
919 | strcat (args, allargs); | |
920 | ||
e88c49c3 DE |
921 | /* Prepare the environment vars for CreateProcess. */ |
922 | { | |
923 | /* This code use to assume all env vars were file names and would | |
924 | translate them all to win32 style. That obviously doesn't work in the | |
2dcfc9c7 DE |
925 | general case. The current rule is that we only translate PATH. |
926 | We need to handle PATH because we're about to call CreateProcess and | |
927 | it uses PATH to find DLL's. Fortunately PATH has a well-defined value | |
928 | in both posix and win32 environments. cygwin.dll will change it back | |
929 | to posix style if necessary. */ | |
e88c49c3 DE |
930 | |
931 | static const char *conv_path_names[] = | |
3a4b77d8 JM |
932 | { |
933 | "PATH=", | |
934 | 0 | |
935 | }; | |
e88c49c3 DE |
936 | |
937 | /* CreateProcess takes the environment list as a null terminated set of | |
938 | strings (i.e. two nulls terminate the list). */ | |
939 | ||
940 | /* Get total size for env strings. */ | |
941 | for (envlen = 0, i = 0; env[i] && *env[i]; i++) | |
942 | { | |
2dcfc9c7 | 943 | int j, len; |
e88c49c3 | 944 | |
2dcfc9c7 DE |
945 | for (j = 0; conv_path_names[j]; j++) |
946 | { | |
947 | len = strlen (conv_path_names[j]); | |
948 | if (strncmp (conv_path_names[j], env[i], len) == 0) | |
e88c49c3 | 949 | { |
1a443730 | 950 | if (cygwin32_posix_path_list_p (env[i] + len)) |
2dcfc9c7 | 951 | envlen += len |
1a443730 | 952 | + cygwin32_posix_to_win32_path_list_buf_size (env[i] + len); |
2dcfc9c7 DE |
953 | else |
954 | envlen += strlen (env[i]) + 1; | |
955 | break; | |
e88c49c3 | 956 | } |
e88c49c3 | 957 | } |
2dcfc9c7 | 958 | if (conv_path_names[j] == NULL) |
e88c49c3 DE |
959 | envlen += strlen (env[i]) + 1; |
960 | } | |
961 | ||
962 | winenv = alloca (envlen + 1); | |
963 | ||
964 | /* Copy env strings into new buffer. */ | |
3cee93ac | 965 | for (temp = winenv, i = 0; env[i] && *env[i]; i++) |
e88c49c3 | 966 | { |
2dcfc9c7 | 967 | int j, len; |
e88c49c3 | 968 | |
2dcfc9c7 DE |
969 | for (j = 0; conv_path_names[j]; j++) |
970 | { | |
971 | len = strlen (conv_path_names[j]); | |
972 | if (strncmp (conv_path_names[j], env[i], len) == 0) | |
e88c49c3 | 973 | { |
1a443730 | 974 | if (cygwin32_posix_path_list_p (env[i] + len)) |
e88c49c3 DE |
975 | { |
976 | memcpy (temp, env[i], len); | |
1a443730 | 977 | cygwin32_posix_to_win32_path_list (env[i] + len, temp + len); |
e88c49c3 | 978 | } |
2dcfc9c7 DE |
979 | else |
980 | strcpy (temp, env[i]); | |
981 | break; | |
e88c49c3 | 982 | } |
e88c49c3 | 983 | } |
2dcfc9c7 | 984 | if (conv_path_names[j] == NULL) |
e88c49c3 | 985 | strcpy (temp, env[i]); |
2dcfc9c7 | 986 | |
e88c49c3 DE |
987 | temp += strlen (temp) + 1; |
988 | } | |
989 | ||
990 | /* Final nil string to terminate new env. */ | |
991 | *temp = 0; | |
992 | } | |
1750a5ef | 993 | |
1750a5ef | 994 | ret = CreateProcess (0, |
3a4b77d8 | 995 | args, /* command line */ |
24e60978 SC |
996 | NULL, /* Security */ |
997 | NULL, /* thread */ | |
998 | TRUE, /* inherit handles */ | |
999 | flags, /* start flags */ | |
1750a5ef | 1000 | winenv, |
24e60978 SC |
1001 | NULL, /* current directory */ |
1002 | &si, | |
1003 | &pi); | |
1004 | if (!ret) | |
3a4b77d8 | 1005 | error ("Error creating process %s, (error %d)\n", exec_file, GetLastError ()); |
24e60978 SC |
1006 | |
1007 | exception_count = 0; | |
1008 | event_count = 0; | |
1009 | ||
3cee93ac CF |
1010 | current_process_handle = pi.hProcess; |
1011 | current_event.dwProcessId = pi.dwProcessId; | |
1012 | memset (¤t_event, 0, sizeof (current_event)); | |
1013 | inferior_pid = current_event.dwThreadId = pi.dwThreadId; | |
24e60978 | 1014 | push_target (&child_ops); |
3cee93ac | 1015 | child_init_thread_list (); |
24e60978 SC |
1016 | init_wait_for_inferior (); |
1017 | clear_proceed_status (); | |
1018 | target_terminal_init (); | |
1019 | target_terminal_inferior (); | |
1020 | ||
1e37c281 JM |
1021 | /* Run until process and threads are loaded */ |
1022 | do | |
1023 | get_child_debug_event (inferior_pid, &dummy, &event_code, &ret); | |
1024 | while (event_code != EXCEPTION_DEBUG_EVENT); | |
24e60978 | 1025 | |
d3a09475 JM |
1026 | SymSetOptions (SYMOPT_DEFERRED_LOADS); |
1027 | SymInitialize (current_process_handle, NULL, TRUE); | |
1028 | ||
1e37c281 | 1029 | proceed ((CORE_ADDR) - 1, TARGET_SIGNAL_0, 0); |
24e60978 SC |
1030 | } |
1031 | ||
1032 | static void | |
1033 | child_mourn_inferior () | |
1034 | { | |
1e37c281 JM |
1035 | continue_status = DBG_CONTINUE; |
1036 | (void) child_continue (-1); | |
24e60978 SC |
1037 | unpush_target (&child_ops); |
1038 | generic_mourn_inferior (); | |
1039 | } | |
1040 | ||
24e60978 SC |
1041 | /* Send a SIGINT to the process group. This acts just like the user typed a |
1042 | ^C on the controlling terminal. */ | |
1043 | ||
b607efe7 | 1044 | static void |
24e60978 SC |
1045 | child_stop () |
1046 | { | |
1ef980b9 | 1047 | DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n")); |
1e37c281 | 1048 | CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT, current_event.dwProcessId)); |
3a4b77d8 | 1049 | registers_changed (); /* refresh register state */ |
24e60978 SC |
1050 | } |
1051 | ||
1052 | int | |
eb708f2e SC |
1053 | child_xfer_memory (CORE_ADDR memaddr, char *our, int len, |
1054 | int write, struct target_ops *target) | |
24e60978 SC |
1055 | { |
1056 | DWORD done; | |
1057 | if (write) | |
1058 | { | |
1ef980b9 SC |
1059 | DEBUG_MEM (("gdb: write target memory, %d bytes at 0x%08x\n", |
1060 | len, memaddr)); | |
3cee93ac CF |
1061 | WriteProcessMemory (current_process_handle, (LPVOID) memaddr, our, |
1062 | len, &done); | |
1063 | FlushInstructionCache (current_process_handle, (LPCVOID) memaddr, len); | |
24e60978 SC |
1064 | } |
1065 | else | |
1066 | { | |
1ef980b9 SC |
1067 | DEBUG_MEM (("gdb: read target memory, %d bytes at 0x%08x\n", |
1068 | len, memaddr)); | |
3cee93ac CF |
1069 | ReadProcessMemory (current_process_handle, (LPCVOID) memaddr, our, len, |
1070 | &done); | |
24e60978 SC |
1071 | } |
1072 | return done; | |
1073 | } | |
1074 | ||
1075 | void | |
1076 | child_kill_inferior (void) | |
1077 | { | |
3cee93ac CF |
1078 | CHECK (TerminateProcess (current_process_handle, 0)); |
1079 | ||
b5edcb45 ILT |
1080 | for (;;) |
1081 | { | |
1e37c281 JM |
1082 | continue_status = DBG_CONTINUE; |
1083 | if (!child_continue (-1)) | |
b5edcb45 | 1084 | break; |
3cee93ac | 1085 | if (!WaitForDebugEvent (¤t_event, INFINITE)) |
b5edcb45 | 1086 | break; |
3cee93ac | 1087 | if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) |
b5edcb45 ILT |
1088 | break; |
1089 | } | |
1090 | ||
3cee93ac CF |
1091 | CHECK (CloseHandle (current_process_handle)); |
1092 | ||
1093 | /* this may fail in an attached process so don't check. */ | |
1094 | (void) CloseHandle (current_thread->h); | |
3a4b77d8 | 1095 | target_mourn_inferior (); /* or just child_mourn_inferior? */ |
24e60978 SC |
1096 | } |
1097 | ||
1098 | void | |
3cee93ac | 1099 | child_resume (int pid, int step, enum target_signal sig) |
24e60978 | 1100 | { |
3cee93ac CF |
1101 | int i; |
1102 | thread_info *th; | |
24e60978 | 1103 | |
3cee93ac CF |
1104 | DEBUG_EXEC (("gdb: child_resume (pid=%d, step=%d, sig=%d);\n", |
1105 | pid, step, sig)); | |
1106 | ||
1107 | /* Get context for currently selected thread */ | |
1108 | th = thread_rec (current_event.dwThreadId, FALSE); | |
24e60978 SC |
1109 | if (step) |
1110 | { | |
1ef980b9 | 1111 | #ifdef i386 |
24e60978 SC |
1112 | /* Single step by setting t bit */ |
1113 | child_fetch_inferior_registers (PS_REGNUM); | |
3cee93ac | 1114 | th->context.EFlags |= FLAG_TRACE_BIT; |
454ffae5 | 1115 | #endif |
24e60978 SC |
1116 | } |
1117 | ||
3cee93ac | 1118 | if (th->context.ContextFlags) |
24e60978 | 1119 | { |
3cee93ac CF |
1120 | CHECK (SetThreadContext (th->h, &th->context)); |
1121 | th->context.ContextFlags = 0; | |
24e60978 SC |
1122 | } |
1123 | ||
3cee93ac CF |
1124 | /* Allow continuing with the same signal that interrupted us. |
1125 | Otherwise complain. */ | |
24e60978 | 1126 | |
1e37c281 | 1127 | child_continue (pid); |
24e60978 SC |
1128 | } |
1129 | ||
1130 | static void | |
1131 | child_prepare_to_store () | |
1132 | { | |
1133 | /* Do nothing, since we can store individual regs */ | |
1134 | } | |
1135 | ||
1136 | static int | |
1137 | child_can_run () | |
1138 | { | |
1139 | return 1; | |
1140 | } | |
1141 | ||
1142 | static void | |
1143 | child_close () | |
1144 | { | |
1ef980b9 | 1145 | DEBUG_EVENTS (("gdb: child_close, inferior_pid=%d\n", inferior_pid)); |
24e60978 | 1146 | } |
1ef980b9 | 1147 | |
3a4b77d8 | 1148 | struct target_ops child_ops; |
c719b714 | 1149 | |
3a4b77d8 JM |
1150 | static void |
1151 | init_child_ops (void) | |
24e60978 | 1152 | { |
3a4b77d8 JM |
1153 | child_ops.to_shortname = "child"; |
1154 | child_ops.to_longname = "Win32 child process"; | |
1155 | child_ops.to_doc = "Win32 child process (started by the \"run\" command)."; | |
1156 | child_ops.to_open = child_open; | |
1157 | child_ops.to_close = child_close; | |
1158 | child_ops.to_attach = child_attach; | |
1159 | child_ops.to_detach = child_detach; | |
1160 | child_ops.to_resume = child_resume; | |
1161 | child_ops.to_wait = child_wait; | |
1162 | child_ops.to_fetch_registers = child_fetch_inferior_registers; | |
1163 | child_ops.to_store_registers = child_store_inferior_registers; | |
1164 | child_ops.to_prepare_to_store = child_prepare_to_store; | |
1165 | child_ops.to_xfer_memory = child_xfer_memory; | |
1166 | child_ops.to_files_info = child_files_info; | |
1167 | child_ops.to_insert_breakpoint = memory_insert_breakpoint; | |
1168 | child_ops.to_remove_breakpoint = memory_remove_breakpoint; | |
1169 | child_ops.to_terminal_init = terminal_init_inferior; | |
1170 | child_ops.to_terminal_inferior = terminal_inferior; | |
1171 | child_ops.to_terminal_ours_for_output = terminal_ours_for_output; | |
1172 | child_ops.to_terminal_ours = terminal_ours; | |
1173 | child_ops.to_terminal_info = child_terminal_info; | |
1174 | child_ops.to_kill = child_kill_inferior; | |
1175 | child_ops.to_load = 0; | |
1176 | child_ops.to_lookup_symbol = 0; | |
1177 | child_ops.to_create_inferior = child_create_inferior; | |
1178 | child_ops.to_mourn_inferior = child_mourn_inferior; | |
1179 | child_ops.to_can_run = child_can_run; | |
1180 | child_ops.to_notice_signals = 0; | |
1181 | child_ops.to_thread_alive = win32_child_thread_alive; | |
d3a09475 | 1182 | child_ops.to_pid_to_str = cygwin_pid_to_str; |
3a4b77d8 JM |
1183 | child_ops.to_stop = child_stop; |
1184 | child_ops.to_stratum = process_stratum; | |
1185 | child_ops.DONT_USE = 0; | |
1186 | child_ops.to_has_all_memory = 1; | |
1187 | child_ops.to_has_memory = 1; | |
1188 | child_ops.to_has_stack = 1; | |
1189 | child_ops.to_has_registers = 1; | |
1190 | child_ops.to_has_execution = 1; | |
1191 | child_ops.to_sections = 0; | |
1192 | child_ops.to_sections_end = 0; | |
1193 | child_ops.to_magic = OPS_MAGIC; | |
c719b714 | 1194 | } |
24e60978 SC |
1195 | |
1196 | void | |
1197 | _initialize_inftarg () | |
1198 | { | |
1ef980b9 | 1199 | struct cmd_list_element *c; |
3a4b77d8 | 1200 | init_child_ops (); |
1ef980b9 | 1201 | |
24e60978 SC |
1202 | add_show_from_set |
1203 | (add_set_cmd ("new-console", class_support, var_boolean, | |
1204 | (char *) &new_console, | |
1205 | "Set creation of new console when creating child process.", | |
1206 | &setlist), | |
1207 | &showlist); | |
1208 | ||
1209 | add_show_from_set | |
1210 | (add_set_cmd ("new-group", class_support, var_boolean, | |
1211 | (char *) &new_group, | |
1212 | "Set creation of new group when creating child process.", | |
1213 | &setlist), | |
1214 | &showlist); | |
1215 | ||
1ef980b9 SC |
1216 | add_show_from_set |
1217 | (add_set_cmd ("debugexec", class_support, var_boolean, | |
1218 | (char *) &debug_exec, | |
1219 | "Set whether to display execution in child process.", | |
1220 | &setlist), | |
1221 | &showlist); | |
1222 | ||
1223 | add_show_from_set | |
1224 | (add_set_cmd ("debugevents", class_support, var_boolean, | |
1225 | (char *) &debug_events, | |
1226 | "Set whether to display kernel events in child process.", | |
1227 | &setlist), | |
1228 | &showlist); | |
1229 | ||
1230 | add_show_from_set | |
1231 | (add_set_cmd ("debugmemory", class_support, var_boolean, | |
1232 | (char *) &debug_memory, | |
1233 | "Set whether to display memory accesses in child process.", | |
1234 | &setlist), | |
1235 | &showlist); | |
1236 | ||
1237 | add_show_from_set | |
1238 | (add_set_cmd ("debugexceptions", class_support, var_boolean, | |
1239 | (char *) &debug_exceptions, | |
3a4b77d8 | 1240 | "Set whether to display kernel exceptions in child process.", |
1ef980b9 SC |
1241 | &setlist), |
1242 | &showlist); | |
1243 | ||
24e60978 SC |
1244 | add_target (&child_ops); |
1245 | } | |
3cee93ac CF |
1246 | |
1247 | /* Determine if the thread referenced by "pid" is alive | |
1248 | by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0 | |
1249 | it means that the pid has died. Otherwise it is assumed to be alive. */ | |
1250 | static int | |
bc8bd256 | 1251 | win32_child_thread_alive (int pid) |
3cee93ac | 1252 | { |
3a4b77d8 JM |
1253 | return WaitForSingleObject (thread_rec (pid, FALSE)->h, 0) == WAIT_OBJECT_0 ? |
1254 | FALSE : TRUE; | |
3cee93ac CF |
1255 | } |
1256 | ||
1257 | /* Convert pid to printable format. */ | |
1258 | char * | |
1259 | cygwin_pid_to_str (int pid) | |
1260 | { | |
1261 | static char buf[80]; | |
1262 | if (pid == current_event.dwProcessId) | |
1263 | sprintf (buf, "process %d", pid); | |
1264 | else | |
1265 | sprintf (buf, "thread %d.0x%x", current_event.dwProcessId, pid); | |
1266 | return buf; | |
1267 | } | |
d3a09475 JM |
1268 | |
1269 | static LPVOID __stdcall | |
1270 | sfta(HANDLE h, DWORD d) | |
1e37c281 | 1271 | { |
d3a09475 | 1272 | return NULL; |
1e37c281 JM |
1273 | } |
1274 | ||
d3a09475 JM |
1275 | static DWORD __stdcall |
1276 | sgmb(HANDLE h, DWORD d) | |
1e37c281 | 1277 | { |
1e37c281 | 1278 | #if 0 |
d3a09475 JM |
1279 | return 4; |
1280 | #else | |
1281 | return SymGetModuleBase (h, d) ?: 4; | |
1e37c281 | 1282 | #endif |
d3a09475 JM |
1283 | } |
1284 | ||
1285 | CORE_ADDR | |
1286 | child_frame_chain(struct frame_info *f) | |
1287 | { | |
1288 | STACKFRAME *sf = (STACKFRAME *) f->extra_info; | |
1e37c281 | 1289 | if (!StackWalk (IMAGE_FILE_MACHINE_I386, current_process_handle, |
d3a09475 JM |
1290 | current_thread->h, sf, NULL, NULL, SymFunctionTableAccess, sgmb, NULL) || |
1291 | !sf->AddrReturn.Offset) | |
1292 | return 0; | |
1293 | return sf->AddrFrame.Offset; | |
1294 | } | |
1295 | ||
1296 | CORE_ADDR | |
1297 | child_frame_saved_pc(struct frame_info *f) | |
1298 | { | |
1299 | STACKFRAME *sf = (STACKFRAME *) f->extra_info; | |
1300 | return sf->AddrReturn.Offset; | |
1301 | } | |
1302 | ||
1303 | void | |
1304 | child_init_frame(int leaf, struct frame_info *f) | |
1305 | { | |
1306 | STACKFRAME *sf; | |
1307 | ||
1308 | if (f->next && f->next->extra_info) | |
1309 | f->extra_info = f->next->extra_info; | |
1310 | else if (f->prev && f->prev->extra_info) | |
1311 | f->extra_info = f->prev->extra_info; | |
1312 | else | |
1313 | { | |
1314 | sf = (STACKFRAME *) frame_obstack_alloc (sizeof (*sf)); | |
1315 | f->extra_info = (struct frame_extra_info *) sf; | |
1316 | memset (sf, 0, sizeof(*sf)); | |
1317 | sf->AddrPC.Offset = f->pc; | |
1318 | sf->AddrPC.Mode = AddrModeFlat; | |
1319 | sf->AddrStack.Offset = current_thread->context.Esp; | |
1320 | sf->AddrStack.Mode = AddrModeFlat; | |
1321 | sf->AddrFrame.Offset = f->frame; | |
1322 | sf->AddrFrame.Mode = AddrModeFlat; | |
1323 | } | |
1e37c281 | 1324 | } |