2002-02-08 Pierre Muller <muller@ics.u-strasbg.fr>
[deliverable/binutils-gdb.git] / gdb / windows-nat.c
1 /* Target-vector operations for controlling win32 child processes, for GDB.
2
3 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free
4 Software Foundation, Inc.
5
6 Contributed by Cygnus Solutions, A Red Hat Company.
7
8 This file is part of GDB.
9
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.
14
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.
19
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. */
24
25 /* by Steve Chamberlain, sac@cygnus.com */
26
27 /* We assume we're being built with and will be used for cygwin. */
28
29 #include "defs.h"
30 #include "tm.h" /* required for SSE registers */
31 #include "frame.h" /* required by inferior.h */
32 #include "inferior.h"
33 #include "target.h"
34 #include "gdbcore.h"
35 #include "command.h"
36 #include "completer.h"
37 #include "regcache.h"
38 #include "top.h"
39 #include "i386-tdep.h"
40 #include <signal.h>
41 #include <sys/types.h>
42 #include <fcntl.h>
43 #include <stdlib.h>
44 #include <windows.h>
45 #include <imagehlp.h>
46 #include <sys/cygwin.h>
47
48 #include "buildsym.h"
49 #include "symfile.h"
50 #include "objfiles.h"
51 #include "gdb_string.h"
52 #include "gdbthread.h"
53 #include "gdbcmd.h"
54 #include <sys/param.h>
55 #include <unistd.h>
56
57 /* The ui's event loop. */
58 extern int (*ui_loop_hook) (int signo);
59
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
64 enum
65 {
66 FLAG_TRACE_BIT = 0x100,
67 CONTEXT_DEBUGGER = (CONTEXT_FULL | CONTEXT_FLOATING_POINT)
68 };
69 #endif
70 #include <sys/procfs.h>
71 #include <psapi.h>
72
73 #ifdef HAVE_SSE_REGS
74 #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS \
75 | CONTEXT_EXTENDED_REGISTERS
76 #else
77 #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS
78 #endif
79
80 static unsigned dr[8];
81 static int debug_registers_changed = 0;
82 static int debug_registers_used = 0;
83
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"
87
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
93
94 /* Forward declaration */
95 extern struct target_ops child_ops;
96
97 static void child_stop (void);
98 static int win32_child_thread_alive (ptid_t);
99 void child_kill_inferior (void);
100
101 static enum target_signal last_sig = TARGET_SIGNAL_0;
102 /* Set if a signal was received from the debugged process */
103
104 /* Thread information structure used to track information that is
105 not available in gdb's thread structure. */
106 typedef struct thread_info_struct
107 {
108 struct thread_info_struct *next;
109 DWORD id;
110 HANDLE h;
111 char *name;
112 int suspend_count;
113 CONTEXT context;
114 STACKFRAME sf;
115 }
116 thread_info;
117
118 static thread_info thread_head;
119
120 /* The process and thread handles for the above context. */
121
122 static DEBUG_EVENT current_event; /* The current debug event from
123 WaitForDebugEvent */
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 */
127
128 /* Counts of things. */
129 static int exception_count = 0;
130 static int event_count = 0;
131
132 /* User options. */
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 */
139
140 /* This vector maps GDB's idea of a register's number into an address
141 in the win32 exception context vector.
142
143 It also contains the bit mask needed to load the register in question.
144
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
151 out bit. */
152
153 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
154 static const int mappings[] =
155 {
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)
188 #ifdef HAVE_SSE_REGS
189 /* XMM0-7 */ ,
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]),
198 /* MXCSR */
199 context_offset (ExtendedRegisters[24])
200 #endif
201 };
202
203 #undef context_offset
204
205 /* This vector maps the target's idea of an exception (extracted
206 from the DEBUG_EVENT structure) to GDB's idea. */
207
208 struct xlate_exception
209 {
210 int them;
211 enum target_signal us;
212 };
213
214 static const struct xlate_exception
215 xlate[] =
216 {
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},
223 {-1, -1}};
224
225 static void
226 check (BOOL ok, const char *file, int line)
227 {
228 if (!ok)
229 printf_filtered ("error return %s:%d was %lu\n", file, line,
230 GetLastError ());
231 }
232
233
234 /* Find a thread record given a thread id.
235 If get_context then also retrieve the context for this
236 thread. */
237 static thread_info *
238 thread_rec (DWORD id, int get_context)
239 {
240 thread_info *th;
241
242 for (th = &thread_head; (th = th->next) != NULL;)
243 if (th->id == id)
244 {
245 if (!th->suspend_count && get_context)
246 {
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;
251
252 th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
253 GetThreadContext (th->h, &th->context);
254 if (id == current_event.dwThreadId)
255 {
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;
263 }
264 }
265 return th;
266 }
267
268 return NULL;
269 }
270
271 /* Add a thread to the thread list */
272 static thread_info *
273 child_add_thread (DWORD id, HANDLE h)
274 {
275 thread_info *th;
276
277 if ((th = thread_rec (id, FALSE)))
278 return th;
279
280 th = (thread_info *) xmalloc (sizeof (*th));
281 memset (th, 0, sizeof (*th));
282 th->id = id;
283 th->h = h;
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)
289 {
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;
302 }
303 return th;
304 }
305
306 /* Clear out any old thread list and reintialize it to a
307 pristine state. */
308 static void
309 child_init_thread_list (void)
310 {
311 thread_info *th = &thread_head;
312
313 DEBUG_EVENTS (("gdb: child_init_thread_list\n"));
314 init_thread_list ();
315 while (th->next != NULL)
316 {
317 thread_info *here = th->next;
318 th->next = here->next;
319 (void) CloseHandle (here->h);
320 xfree (here);
321 }
322 }
323
324 /* Delete a thread from the list of threads */
325 static void
326 child_delete_thread (DWORD id)
327 {
328 thread_info *th;
329
330 if (info_verbose)
331 printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (pid_to_ptid (id)));
332 delete_thread (pid_to_ptid (id));
333
334 for (th = &thread_head;
335 th->next != NULL && th->next->id != id;
336 th = th->next)
337 continue;
338
339 if (th->next != NULL)
340 {
341 thread_info *here = th->next;
342 th->next = here->next;
343 CloseHandle (here->h);
344 xfree (here);
345 }
346 }
347
348 static void
349 do_child_fetch_inferior_registers (int r)
350 {
351 char *context_offset = ((char *) &current_thread->context) + mappings[r];
352 long l;
353 if (r == FCS_REGNUM)
354 {
355 l = *((long *) context_offset) & 0xffff;
356 supply_register (r, (char *) &l);
357 }
358 else if (r == FOP_REGNUM)
359 {
360 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
361 supply_register (r, (char *) &l);
362 }
363 else if (r >= 0)
364 supply_register (r, context_offset);
365 else
366 {
367 for (r = 0; r < NUM_REGS; r++)
368 do_child_fetch_inferior_registers (r);
369 }
370 }
371
372 static void
373 child_fetch_inferior_registers (int r)
374 {
375 current_thread = thread_rec (PIDGET (inferior_ptid), TRUE);
376 do_child_fetch_inferior_registers (r);
377 }
378
379 static void
380 do_child_store_inferior_registers (int r)
381 {
382 if (r >= 0)
383 read_register_gen (r, ((char *) &current_thread->context) + mappings[r]);
384 else
385 {
386 for (r = 0; r < NUM_REGS; r++)
387 do_child_store_inferior_registers (r);
388 }
389 }
390
391 /* Store a new register value into the current thread context */
392 static void
393 child_store_inferior_registers (int r)
394 {
395 current_thread = thread_rec (PIDGET (inferior_ptid), TRUE);
396 do_child_store_inferior_registers (r);
397 }
398
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;
404
405 int
406 psapi_get_dll_name (DWORD BaseAddress, char *dll_name_ret)
407 {
408 DWORD len;
409 MODULEINFO mi;
410 int i;
411 HMODULE dh_buf[1];
412 HMODULE *DllHandle = dh_buf;
413 DWORD cbNeeded;
414 BOOL ok;
415
416 if (!psapi_loaded ||
417 psapi_EnumProcessModules == NULL ||
418 psapi_GetModuleInformation == NULL ||
419 psapi_GetModuleFileNameExA == NULL)
420 {
421 if (psapi_loaded)
422 goto failed;
423 psapi_loaded = 1;
424 psapi_module_handle = LoadLibrary ("psapi.dll");
425 if (!psapi_module_handle)
426 {
427 /* printf_unfiltered ("error loading psapi.dll: %u", GetLastError ()); */
428 goto failed;
429 }
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)
437 goto failed;
438 }
439
440 cbNeeded = 0;
441 ok = (*psapi_EnumProcessModules) (current_process_handle,
442 DllHandle,
443 sizeof (HMODULE),
444 &cbNeeded);
445
446 if (!ok || !cbNeeded)
447 goto failed;
448
449 DllHandle = (HMODULE *) alloca (cbNeeded);
450 if (!DllHandle)
451 goto failed;
452
453 ok = (*psapi_EnumProcessModules) (current_process_handle,
454 DllHandle,
455 cbNeeded,
456 &cbNeeded);
457 if (!ok)
458 goto failed;
459
460 for (i = 0; i < (int) (cbNeeded / sizeof (HMODULE)); i++)
461 {
462 if (!(*psapi_GetModuleInformation) (current_process_handle,
463 DllHandle[i],
464 &mi,
465 sizeof (mi)))
466 error ("Can't get module info");
467
468 len = (*psapi_GetModuleFileNameExA) (current_process_handle,
469 DllHandle[i],
470 dll_name_ret,
471 MAX_PATH);
472 if (len == 0)
473 error ("Error getting dll name: %u\n", GetLastError ());
474
475 if ((DWORD) (mi.lpBaseOfDll) == BaseAddress)
476 return 1;
477 }
478
479 failed:
480 dll_name_ret[0] = '\0';
481 return 0;
482 }
483
484 /* Encapsulate the information required in a call to
485 symbol_file_add_args */
486 struct safe_symbol_file_add_args
487 {
488 char *name;
489 int from_tty;
490 struct section_addr_info *addrs;
491 int mainline;
492 int flags;
493 struct ui_file *err, *out;
494 struct objfile *ret;
495 };
496
497 /* Maintain a linked list of "so" information. */
498 struct so_stuff
499 {
500 struct so_stuff *next;
501 DWORD load_addr;
502 int loaded;
503 struct objfile *objfile;
504 char name[1];
505 } solib_start, *solib_end;
506
507 /* Call symbol_file_add with stderr redirected. We don't care if there
508 are errors. */
509 static int
510 safe_symbol_file_add_stub (void *argv)
511 {
512 #define p ((struct safe_symbol_file_add_args *)argv)
513 struct so_stuff *so = &solib_start;
514
515 while ((so = so->next))
516 if (so->loaded && strcasecmp (so->name, p->name) == 0)
517 return 0;
518 p->ret = symbol_file_add (p->name, p->from_tty, p->addrs, p->mainline, p->flags);
519 return !!p->ret;
520 #undef p
521 }
522
523 /* Restore gdb's stderr after calling symbol_file_add */
524 static void
525 safe_symbol_file_add_cleanup (void *p)
526 {
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;
534 #undef sp
535 }
536
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)
542 {
543 struct safe_symbol_file_add_args p;
544 struct cleanup *cleanup;
545
546 cleanup = make_cleanup (safe_symbol_file_add_cleanup, &p);
547
548 p.err = gdb_stderr;
549 p.out = gdb_stdout;
550 gdb_flush (gdb_stderr);
551 gdb_flush (gdb_stdout);
552 gdb_stderr = ui_file_new ();
553 gdb_stdout = ui_file_new ();
554 p.name = name;
555 p.from_tty = from_tty;
556 p.addrs = addrs;
557 p.mainline = mainline;
558 p.flags = flags;
559 catch_errors (safe_symbol_file_add_stub, &p, "", RETURN_MASK_ERROR);
560
561 do_cleanups (cleanup);
562 return p.ret;
563 }
564
565 /* Remember the maximum DLL length for printing in info dll command. */
566 int max_dll_name_len;
567
568 static void
569 register_loaded_dll (const char *name, DWORD load_addr)
570 {
571 struct so_stuff *so;
572 char ppath[MAX_PATH + 1];
573 char buf[MAX_PATH + 1];
574 char cwd[MAX_PATH + 1];
575 char *p;
576 WIN32_FIND_DATA w32_fd;
577 HANDLE h = FindFirstFile(name, &w32_fd);
578 size_t len;
579
580 FindClose (h);
581 strcpy (buf, name);
582 if (GetCurrentDirectory (MAX_PATH + 1, cwd))
583 {
584 p = strrchr (buf, '\\');
585 if (p)
586 p[1] = '\0';
587 SetCurrentDirectory (buf);
588 GetFullPathName (w32_fd.cFileName, MAX_PATH, buf, &p);
589 SetCurrentDirectory (cwd);
590 }
591
592 cygwin_conv_to_posix_path (buf, ppath);
593 so = (struct so_stuff *) xmalloc (sizeof (struct so_stuff) + strlen (ppath) + 8 + 1);
594 so->loaded = 0;
595 so->load_addr = load_addr;
596 so->next = NULL;
597 so->objfile = NULL;
598 strcpy (so->name, ppath);
599
600 solib_end->next = so;
601 solib_end = so;
602 len = strlen (ppath);
603 if (len > max_dll_name_len)
604 max_dll_name_len = len;
605 }
606
607 /* Wait for child to do something. Return pid of child, or -1 in case
608 of error; store status through argument pointer OURSTATUS. */
609 static int
610 handle_load_dll (void *dummy)
611 {
612 LOAD_DLL_DEBUG_INFO *event = &current_event.u.LoadDll;
613 DWORD dll_name_ptr;
614 DWORD done;
615 char dll_buf[MAX_PATH + 1];
616 char *dll_name = NULL;
617 char *p;
618
619 dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0';
620
621 if (!psapi_get_dll_name ((DWORD) (event->lpBaseOfDll), dll_buf))
622 dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0';
623
624 dll_name = dll_buf;
625
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')
630 {
631 DWORD size = event->fUnicode ? sizeof (WCHAR) : sizeof (char);
632 int len = 0;
633 char b[2];
634
635 ReadProcessMemory (current_process_handle,
636 (LPCVOID) event->lpImageName,
637 (char *) &dll_name_ptr,
638 sizeof (dll_name_ptr), &done);
639
640 /* See if we could read the address of a string, and that the
641 address isn't null. */
642
643 if (done != sizeof (dll_name_ptr) || !dll_name_ptr)
644 return 1;
645
646 do
647 {
648 ReadProcessMemory (current_process_handle,
649 (LPCVOID) (dll_name_ptr + len * size),
650 &b,
651 size,
652 &done);
653 len++;
654 }
655 while ((b[0] != 0 || b[size - 1] != 0) && done == size);
656
657 dll_name = alloca (len);
658
659 if (event->fUnicode)
660 {
661 WCHAR *unicode_dll_name = (WCHAR *) alloca (len * sizeof (WCHAR));
662 ReadProcessMemory (current_process_handle,
663 (LPCVOID) dll_name_ptr,
664 unicode_dll_name,
665 len * sizeof (WCHAR),
666 &done);
667
668 WideCharToMultiByte (CP_ACP, 0,
669 unicode_dll_name, len,
670 dll_name, len, 0, 0);
671 }
672 else
673 {
674 ReadProcessMemory (current_process_handle,
675 (LPCVOID) dll_name_ptr,
676 dll_name,
677 len,
678 &done);
679 }
680 }
681
682 if (!dll_name)
683 return 1;
684
685 register_loaded_dll (dll_name, (DWORD) event->lpBaseOfDll + 0x1000);
686
687 return 1;
688 }
689
690 static int
691 handle_unload_dll (void *dummy)
692 {
693 DWORD lpBaseOfDll = (DWORD) current_event.u.UnloadDll.lpBaseOfDll + 0x1000;
694 struct so_stuff *so;
695
696 for (so = &solib_start; so->next != NULL; so = so->next)
697 if (so->next->load_addr == lpBaseOfDll)
698 {
699 struct so_stuff *sodel = so->next;
700 so->next = sodel->next;
701 if (!so->next)
702 solib_end = so;
703 if (sodel->objfile)
704 free_objfile (sodel->objfile);
705 xfree(sodel);
706 return 1;
707 }
708 error ("Error: dll starting at 0x%lx not found.\n", (DWORD) lpBaseOfDll);
709
710 return 0;
711 }
712
713 /* Return name of last loaded DLL. */
714 char *
715 child_solib_loaded_library_pathname (int pid)
716 {
717 return !solib_end || !solib_end->name[0] ? NULL : solib_end->name;
718 }
719
720 /* Clear list of loaded DLLs. */
721 void
722 child_clear_solibs (void)
723 {
724 struct so_stuff *so, *so1 = solib_start.next;
725
726 while ((so = so1) != NULL)
727 {
728 so1 = so->next;
729 xfree (so);
730 }
731
732 solib_start.next = NULL;
733 solib_start.objfile = NULL;
734 solib_end = &solib_start;
735 max_dll_name_len = sizeof ("DLL Name") - 1;
736 }
737
738 /* Add DLL symbol information. */
739 static struct objfile *
740 solib_symbols_add (char *name, int from_tty, CORE_ADDR load_addr)
741 {
742 struct section_addr_info section_addrs;
743
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. */
747
748 if (!name || !name[0])
749 return NULL;
750
751 memset (&section_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);
755 }
756
757 /* Load DLL symbol info. */
758 void
759 dll_symbol_command (char *args, int from_tty)
760 {
761 int n;
762 dont_repeat ();
763
764 if (args == NULL)
765 error ("dll-symbols requires a file name");
766
767 n = strlen (args);
768 if (n > 4 && strcasecmp (args + n - 4, ".dll") != 0)
769 {
770 char *newargs = (char *) alloca (n + 4 + 1);
771 strcpy (newargs, args);
772 strcat (newargs, ".dll");
773 args = newargs;
774 }
775
776 safe_symbol_file_add (args, from_tty, NULL, 0, OBJF_SHARED | OBJF_USERLOADED);
777 }
778
779 /* List currently loaded DLLs. */
780 void
781 info_dll_command (char *ignore, int from_tty)
782 {
783 struct so_stuff *so = &solib_start;
784
785 if (!so->next)
786 return;
787
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);
791
792 return;
793 }
794
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. */
798 static int
799 handle_output_debug_string (struct target_waitstatus *ourstatus)
800 {
801 char *s;
802 int gotasig = FALSE;
803
804 if (!target_read_string
805 ((CORE_ADDR) current_event.u.DebugString.lpDebugStringData, &s, 1024, 0)
806 || !s || !*s)
807 return gotasig;
808
809 if (strncmp (s, CYGWIN_SIGNAL_STRING, sizeof (CYGWIN_SIGNAL_STRING) - 1) != 0)
810 {
811 if (strncmp (s, "cYg", 3) != 0)
812 warning ("%s", s);
813 }
814 else
815 {
816 char *p;
817 int sig = strtol (s + sizeof (CYGWIN_SIGNAL_STRING) - 1, &p, 0);
818 gotasig = target_signal_from_host (sig);
819 ourstatus->value.sig = gotasig;
820 if (gotasig)
821 ourstatus->kind = TARGET_WAITKIND_STOPPED;
822 }
823
824 xfree (s);
825 return gotasig;
826 }
827
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)
831
832 static int
833 handle_exception (struct target_waitstatus *ourstatus)
834 {
835 thread_info *th;
836 DWORD code = current_event.u.Exception.ExceptionRecord.ExceptionCode;
837
838 ourstatus->kind = TARGET_WAITKIND_STOPPED;
839
840 /* Record the context of the current thread */
841 th = thread_rec (current_event.dwThreadId, -1);
842
843 switch (code)
844 {
845 case EXCEPTION_ACCESS_VIOLATION:
846 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ACCESS_VIOLATION");
847 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
848 break;
849 case STATUS_STACK_OVERFLOW:
850 DEBUG_EXCEPTION_SIMPLE ("STATUS_STACK_OVERFLOW");
851 ourstatus->value.sig = TARGET_SIGNAL_SEGV;
852 break;
853 case STATUS_FLOAT_DENORMAL_OPERAND:
854 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DENORMAL_OPERAND");
855 ourstatus->value.sig = TARGET_SIGNAL_FPE;
856 break;
857 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
858 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ARRAY_BOUNDS_EXCEEDED");
859 ourstatus->value.sig = TARGET_SIGNAL_FPE;
860 break;
861 case STATUS_FLOAT_INEXACT_RESULT:
862 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INEXACT_RESULT");
863 ourstatus->value.sig = TARGET_SIGNAL_FPE;
864 break;
865 case STATUS_FLOAT_INVALID_OPERATION:
866 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_INVALID_OPERATION");
867 ourstatus->value.sig = TARGET_SIGNAL_FPE;
868 break;
869 case STATUS_FLOAT_OVERFLOW:
870 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_OVERFLOW");
871 ourstatus->value.sig = TARGET_SIGNAL_FPE;
872 break;
873 case STATUS_FLOAT_STACK_CHECK:
874 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_STACK_CHECK");
875 ourstatus->value.sig = TARGET_SIGNAL_FPE;
876 break;
877 case STATUS_FLOAT_UNDERFLOW:
878 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_UNDERFLOW");
879 ourstatus->value.sig = TARGET_SIGNAL_FPE;
880 break;
881 case STATUS_FLOAT_DIVIDE_BY_ZERO:
882 DEBUG_EXCEPTION_SIMPLE ("STATUS_FLOAT_DIVIDE_BY_ZERO");
883 ourstatus->value.sig = TARGET_SIGNAL_FPE;
884 break;
885 case STATUS_INTEGER_DIVIDE_BY_ZERO:
886 DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_DIVIDE_BY_ZERO");
887 ourstatus->value.sig = TARGET_SIGNAL_FPE;
888 break;
889 case STATUS_INTEGER_OVERFLOW:
890 DEBUG_EXCEPTION_SIMPLE ("STATUS_INTEGER_OVERFLOW");
891 ourstatus->value.sig = TARGET_SIGNAL_FPE;
892 break;
893 case EXCEPTION_BREAKPOINT:
894 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_BREAKPOINT");
895 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
896 break;
897 case DBG_CONTROL_C:
898 DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_C");
899 ourstatus->value.sig = TARGET_SIGNAL_INT;
900 break;
901 case DBG_CONTROL_BREAK:
902 DEBUG_EXCEPTION_SIMPLE ("DBG_CONTROL_BREAK");
903 ourstatus->value.sig = TARGET_SIGNAL_INT;
904 break;
905 case EXCEPTION_SINGLE_STEP:
906 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_SINGLE_STEP");
907 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
908 break;
909 case EXCEPTION_ILLEGAL_INSTRUCTION:
910 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_ILLEGAL_INSTRUCTION");
911 ourstatus->value.sig = TARGET_SIGNAL_ILL;
912 break;
913 case EXCEPTION_PRIV_INSTRUCTION:
914 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_PRIV_INSTRUCTION");
915 ourstatus->value.sig = TARGET_SIGNAL_ILL;
916 break;
917 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
918 DEBUG_EXCEPTION_SIMPLE ("EXCEPTION_NONCONTINUABLE_EXCEPTION");
919 ourstatus->value.sig = TARGET_SIGNAL_ILL;
920 break;
921 default:
922 if (current_event.u.Exception.dwFirstChance)
923 return 0;
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;
928 break;
929 }
930 exception_count++;
931 last_sig = ourstatus->value.sig;
932 return 1;
933 }
934
935 /* Resume all artificially suspended threads if we are continuing
936 execution */
937 static BOOL
938 child_continue (DWORD continue_status, int id)
939 {
940 int i;
941 thread_info *th;
942 BOOL res;
943
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,
950 continue_status);
951 continue_status = 0;
952 if (res)
953 for (th = &thread_head; (th = th->next) != NULL;)
954 if (((id == -1) || (id == (int) th->id)) && th->suspend_count)
955 {
956
957 for (i = 0; i < th->suspend_count; i++)
958 (void) ResumeThread (th->h);
959 th->suspend_count = 0;
960 if (debug_registers_changed)
961 {
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;
973 }
974 }
975
976 debug_registers_changed = 0;
977 return res;
978 }
979
980 /* Get the next event from the child. Return 1 if the event requires
981 handling by WFI (or whatever).
982 */
983 static int
984 get_child_debug_event (int pid, struct target_waitstatus *ourstatus)
985 {
986 BOOL debug_event;
987 DWORD continue_status, event_code;
988 thread_info *th = NULL;
989 static thread_info dummy_thread_info;
990 int retval = 0;
991
992 last_sig = TARGET_SIGNAL_0;
993
994 if (!(debug_event = WaitForDebugEvent (&current_event, 1000)))
995 goto out;
996
997 event_count++;
998 continue_status = DBG_CONTINUE;
999
1000 event_code = current_event.dwDebugEventCode;
1001 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1002
1003 switch (event_code)
1004 {
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);
1013 if (info_verbose)
1014 printf_unfiltered ("[New %s]\n",
1015 target_pid_to_str (
1016 pid_to_ptid (current_event.dwThreadId)));
1017 retval = current_event.dwThreadId;
1018 break;
1019
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;
1027 break;
1028
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;
1036
1037 main_thread_id = current_event.dwThreadId;
1038 /* Add the main thread */
1039 #if 0
1040 th = child_add_thread (current_event.dwProcessId,
1041 current_event.u.CreateProcessInfo.hProcess);
1042 #endif
1043 th = child_add_thread (main_thread_id,
1044 current_event.u.CreateProcessInfo.hThread);
1045 retval = ourstatus->value.related_pid = current_event.dwThreadId;
1046 break;
1047
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;
1057 break;
1058
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;
1070 break;
1071
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. */
1081 break;
1082
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;
1090 break;
1091
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;
1099 break;
1100
1101 default:
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);
1107 break;
1108 }
1109
1110 if (!retval)
1111 CHECK (child_continue (continue_status, -1));
1112 else
1113 {
1114 current_thread = th ? : thread_rec (current_event.dwThreadId, TRUE);
1115 inferior_ptid = pid_to_ptid (retval);
1116 }
1117
1118 out:
1119 return retval;
1120 }
1121
1122 /* Wait for interesting events to occur in the target process. */
1123 static ptid_t
1124 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1125 {
1126 int pid = PIDGET (ptid);
1127
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. */
1133
1134 while (1)
1135 {
1136 int retval = get_child_debug_event (pid, ourstatus);
1137 if (retval)
1138 return pid_to_ptid (retval);
1139 else
1140 {
1141 int detach = 0;
1142
1143 if (ui_loop_hook != NULL)
1144 detach = ui_loop_hook (0);
1145
1146 if (detach)
1147 child_kill_inferior ();
1148 }
1149 }
1150 }
1151
1152 static void
1153 do_initial_child_stuff (DWORD pid)
1154 {
1155 extern int stop_after_trap;
1156 int i;
1157
1158 last_sig = TARGET_SIGNAL_0;
1159 event_count = 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++)
1164 dr[i] = 0;
1165 current_event.dwProcessId = pid;
1166 memset (&current_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 ();
1172
1173 target_terminal_init ();
1174 target_terminal_inferior ();
1175
1176 while (1)
1177 {
1178 stop_after_trap = 1;
1179 wait_for_inferior ();
1180 if (stop_signal != TARGET_SIGNAL_TRAP)
1181 resume (0, stop_signal);
1182 else
1183 break;
1184 }
1185 stop_after_trap = 0;
1186 return;
1187 }
1188
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);
1196
1197 static int
1198 has_detach_ability ()
1199 {
1200 static HMODULE kernel32 = NULL;
1201
1202 if (!kernel32)
1203 kernel32 = LoadLibrary ("kernel32.dll");
1204 if (kernel32)
1205 {
1206 if (!DebugSetProcessKillOnExit)
1207 DebugSetProcessKillOnExit = GetProcAddress (kernel32,
1208 "DebugSetProcessKillOnExit");
1209 if (!DebugActiveProcessStop)
1210 DebugActiveProcessStop = GetProcAddress (kernel32,
1211 "DebugActiveProcessStop");
1212 if (DebugSetProcessKillOnExit && DebugActiveProcessStop)
1213 return 1;
1214 }
1215 return 0;
1216 }
1217
1218 /* Attach to process PID, then initialize for debugging it. */
1219 static void
1220 child_attach (char *args, int from_tty)
1221 {
1222 BOOL ok;
1223 DWORD pid;
1224
1225 if (!args)
1226 error_no_arg ("process-id to attach");
1227
1228 pid = strtoul (args, 0, 0);
1229 ok = DebugActiveProcess (pid);
1230
1231 if (!ok)
1232 error ("Can't attach to process.");
1233
1234 if (has_detach_ability ())
1235 {
1236 attach_flag = 1;
1237 DebugSetProcessKillOnExit (FALSE);
1238 }
1239
1240 if (from_tty)
1241 {
1242 char *exec_file = (char *) get_exec_file (0);
1243
1244 if (exec_file)
1245 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
1246 target_pid_to_str (pid_to_ptid (pid)));
1247 else
1248 printf_unfiltered ("Attaching to %s\n",
1249 target_pid_to_str (pid_to_ptid (pid)));
1250
1251 gdb_flush (gdb_stdout);
1252 }
1253
1254 do_initial_child_stuff (pid);
1255 target_terminal_ours ();
1256 }
1257
1258 static void
1259 child_detach (char *args, int from_tty)
1260 {
1261 int detached = 1;
1262
1263 if (has_detach_ability ())
1264 {
1265 delete_command (NULL, 0);
1266 child_continue (DBG_CONTINUE, -1);
1267 if (!DebugActiveProcessStop (current_event.dwProcessId))
1268 {
1269 error ("Can't detach process %lu (error %lu)",
1270 current_event.dwProcessId, GetLastError ());
1271 detached = 0;
1272 }
1273 DebugSetProcessKillOnExit (FALSE);
1274 }
1275 if (detached && from_tty)
1276 {
1277 char *exec_file = get_exec_file (0);
1278 if (exec_file == 0)
1279 exec_file = "";
1280 printf_unfiltered ("Detaching from program: %s, Pid %lu\n", exec_file,
1281 current_event.dwProcessId);
1282 gdb_flush (gdb_stdout);
1283 }
1284 inferior_ptid = null_ptid;
1285 unpush_target (&child_ops);
1286 }
1287
1288 /* Print status information about what we're accessing. */
1289
1290 static void
1291 child_files_info (struct target_ops *ignore)
1292 {
1293 printf_unfiltered ("\tUsing the running image of %s %s.\n",
1294 attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
1295 }
1296
1297 /* ARGSUSED */
1298 static void
1299 child_open (char *arg, int from_tty)
1300 {
1301 error ("Use the \"run\" command to start a Unix child process.");
1302 }
1303
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(). */
1308
1309 static void
1310 child_create_inferior (char *exec_file, char *allargs, char **env)
1311 {
1312 char real_path[MAXPATHLEN];
1313 char *winenv;
1314 char *temp;
1315 int envlen;
1316 int i;
1317 STARTUPINFO si;
1318 PROCESS_INFORMATION pi;
1319 BOOL ret;
1320 DWORD flags;
1321 char *args;
1322
1323 if (!exec_file)
1324 error ("No executable specified, use `target exec'.\n");
1325
1326 memset (&si, 0, sizeof (si));
1327 si.cb = sizeof (si);
1328
1329 cygwin_conv_to_win32_path (exec_file, real_path);
1330
1331 flags = DEBUG_ONLY_THIS_PROCESS;
1332
1333 if (new_group)
1334 flags |= CREATE_NEW_PROCESS_GROUP;
1335
1336 if (new_console)
1337 flags |= CREATE_NEW_CONSOLE;
1338
1339 args = alloca (strlen (real_path) + strlen (allargs) + 2);
1340
1341 strcpy (args, real_path);
1342
1343 strcat (args, " ");
1344 strcat (args, allargs);
1345
1346 /* Prepare the environment vars for CreateProcess. */
1347 {
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. */
1355
1356 static const char *conv_path_names[] =
1357 {
1358 "PATH=",
1359 0
1360 };
1361
1362 /* CreateProcess takes the environment list as a null terminated set of
1363 strings (i.e. two nulls terminate the list). */
1364
1365 /* Get total size for env strings. */
1366 for (envlen = 0, i = 0; env[i] && *env[i]; i++)
1367 {
1368 int j, len;
1369
1370 for (j = 0; conv_path_names[j]; j++)
1371 {
1372 len = strlen (conv_path_names[j]);
1373 if (strncmp (conv_path_names[j], env[i], len) == 0)
1374 {
1375 if (cygwin_posix_path_list_p (env[i] + len))
1376 envlen += len
1377 + cygwin_posix_to_win32_path_list_buf_size (env[i] + len);
1378 else
1379 envlen += strlen (env[i]) + 1;
1380 break;
1381 }
1382 }
1383 if (conv_path_names[j] == NULL)
1384 envlen += strlen (env[i]) + 1;
1385 }
1386
1387 winenv = alloca (envlen + 1);
1388
1389 /* Copy env strings into new buffer. */
1390 for (temp = winenv, i = 0; env[i] && *env[i]; i++)
1391 {
1392 int j, len;
1393
1394 for (j = 0; conv_path_names[j]; j++)
1395 {
1396 len = strlen (conv_path_names[j]);
1397 if (strncmp (conv_path_names[j], env[i], len) == 0)
1398 {
1399 if (cygwin_posix_path_list_p (env[i] + len))
1400 {
1401 memcpy (temp, env[i], len);
1402 cygwin_posix_to_win32_path_list (env[i] + len, temp + len);
1403 }
1404 else
1405 strcpy (temp, env[i]);
1406 break;
1407 }
1408 }
1409 if (conv_path_names[j] == NULL)
1410 strcpy (temp, env[i]);
1411
1412 temp += strlen (temp) + 1;
1413 }
1414
1415 /* Final nil string to terminate new env. */
1416 *temp = 0;
1417 }
1418
1419 ret = CreateProcess (0,
1420 args, /* command line */
1421 NULL, /* Security */
1422 NULL, /* thread */
1423 TRUE, /* inherit handles */
1424 flags, /* start flags */
1425 winenv,
1426 NULL, /* current directory */
1427 &si,
1428 &pi);
1429 if (!ret)
1430 error ("Error creating process %s, (error %d)\n", exec_file, GetLastError ());
1431
1432 CloseHandle (pi.hThread);
1433 CloseHandle (pi.hProcess);
1434 do_initial_child_stuff (pi.dwProcessId);
1435
1436 /* child_continue (DBG_CONTINUE, -1); */
1437 proceed ((CORE_ADDR) - 1, TARGET_SIGNAL_0, 0);
1438 }
1439
1440 static void
1441 child_mourn_inferior (void)
1442 {
1443 (void) child_continue (DBG_CONTINUE, -1);
1444 i386_cleanup_dregs();
1445 unpush_target (&child_ops);
1446 generic_mourn_inferior ();
1447 }
1448
1449 /* Send a SIGINT to the process group. This acts just like the user typed a
1450 ^C on the controlling terminal. */
1451
1452 static void
1453 child_stop (void)
1454 {
1455 DEBUG_EVENTS (("gdb: GenerateConsoleCtrlEvent (CTRLC_EVENT, 0)\n"));
1456 CHECK (GenerateConsoleCtrlEvent (CTRL_C_EVENT, current_event.dwProcessId));
1457 registers_changed (); /* refresh register state */
1458 }
1459
1460 int
1461 child_xfer_memory (CORE_ADDR memaddr, char *our, int len,
1462 int write, struct mem_attrib *mem,
1463 struct target_ops *target)
1464 {
1465 DWORD done;
1466 if (write)
1467 {
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,
1471 len, &done);
1472 FlushInstructionCache (current_process_handle, (LPCVOID) memaddr, len);
1473 }
1474 else
1475 {
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,
1479 &done);
1480 }
1481 return done;
1482 }
1483
1484 void
1485 child_kill_inferior (void)
1486 {
1487 CHECK (TerminateProcess (current_process_handle, 0));
1488
1489 for (;;)
1490 {
1491 if (!child_continue (DBG_CONTINUE, -1))
1492 break;
1493 if (!WaitForDebugEvent (&current_event, INFINITE))
1494 break;
1495 if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
1496 break;
1497 }
1498
1499 CHECK (CloseHandle (current_process_handle));
1500
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? */
1504 }
1505
1506 void
1507 child_resume (ptid_t ptid, int step, enum target_signal sig)
1508 {
1509 thread_info *th;
1510 DWORD continue_status = DBG_CONTINUE;
1511
1512 int pid = PIDGET (ptid);
1513
1514 if (sig != TARGET_SIGNAL_0)
1515 {
1516 if (current_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
1517 {
1518 DEBUG_EXCEPT(("Cannot continue with signal %d here.\n",sig));
1519 }
1520 else if (sig == last_sig)
1521 continue_status = DBG_EXCEPTION_NOT_HANDLED;
1522 else
1523 #if 0
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. */
1528 {
1529 int i;
1530 for (i = 0; xlate[i].them != -1; i++)
1531 if (xlate[i].us == sig)
1532 {
1533 current_event.u.Exception.ExceptionRecord.ExceptionCode =
1534 xlate[i].them;
1535 continue_status = DBG_EXCEPTION_NOT_HANDLED;
1536 break;
1537 }
1538 if (continue_status == DBG_CONTINUE)
1539 {
1540 DEBUG_EXCEPT(("Cannot continue with signal %d.\n",sig));
1541 }
1542 }
1543 #endif
1544 DEBUG_EXCEPT(("Can only continue with recieved signal %d.\n",
1545 last_sig));
1546 }
1547
1548 last_sig = TARGET_SIGNAL_0;
1549
1550 DEBUG_EXEC (("gdb: child_resume (pid=%d, step=%d, sig=%d);\n",
1551 pid, step, sig));
1552
1553 /* Get context for currently selected thread */
1554 th = thread_rec (current_event.dwThreadId, FALSE);
1555 if (th)
1556 {
1557 if (step)
1558 {
1559 /* Single step by setting t bit */
1560 child_fetch_inferior_registers (PS_REGNUM);
1561 th->context.EFlags |= FLAG_TRACE_BIT;
1562 }
1563
1564 if (th->context.ContextFlags)
1565 {
1566 if (debug_registers_changed)
1567 {
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];
1575 }
1576 CHECK (SetThreadContext (th->h, &th->context));
1577 th->context.ContextFlags = 0;
1578 }
1579 }
1580
1581 /* Allow continuing with the same signal that interrupted us.
1582 Otherwise complain. */
1583
1584 child_continue (continue_status, pid);
1585 }
1586
1587 static void
1588 child_prepare_to_store (void)
1589 {
1590 /* Do nothing, since we can store individual regs */
1591 }
1592
1593 static int
1594 child_can_run (void)
1595 {
1596 return 1;
1597 }
1598
1599 static void
1600 child_close (int x)
1601 {
1602 DEBUG_EVENTS (("gdb: child_close, inferior_ptid=%d\n",
1603 PIDGET (inferior_ptid)));
1604 }
1605
1606 struct target_ops child_ops;
1607
1608 static void
1609 init_child_ops (void)
1610 {
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;
1652 }
1653
1654 void
1655 _initialize_inftarg (void)
1656 {
1657 struct cmd_list_element *c;
1658
1659 init_child_ops ();
1660
1661 c = add_com ("dll-symbols", class_files, dll_symbol_command,
1662 "Load dll library symbols from FILE.");
1663 c->completer = filename_completer;
1664
1665 add_com_alias ("sharedlibrary", "dll-symbols", class_alias, 1);
1666
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.",
1670 &setlist),
1671 &showlist);
1672
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.",
1676 &setlist),
1677 &showlist);
1678
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.",
1682 &setlist),
1683 &showlist);
1684
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.",
1688 &setlist),
1689 &showlist);
1690
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.",
1694 &setlist),
1695 &showlist);
1696
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.",
1700 &setlist),
1701 &showlist);
1702
1703 add_info ("dll", info_dll_command, "Status of loaded DLLs.");
1704 add_info_alias ("sharedlibrary", "dll", 1);
1705
1706 add_target (&child_ops);
1707 }
1708
1709 /* Hardware watchpoint support, adapted from go32-nat.c code. */
1710
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. */
1714 void
1715 cygwin_set_dr (int i, CORE_ADDR addr)
1716 {
1717 if (i < 0 || i > 3)
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;
1723 }
1724
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. */
1728 void
1729 cygwin_set_dr7 (unsigned val)
1730 {
1731 dr[7] = val;
1732 debug_registers_changed = 1;
1733 debug_registers_used = 1;
1734 }
1735
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. */
1739 unsigned
1740 cygwin_get_dr6 (void)
1741 {
1742 return dr[6];
1743 }
1744
1745
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. */
1749 static int
1750 win32_child_thread_alive (ptid_t ptid)
1751 {
1752 int pid = PIDGET (ptid);
1753
1754 return WaitForSingleObject (thread_rec (pid, FALSE)->h, 0) == WAIT_OBJECT_0 ?
1755 FALSE : TRUE;
1756 }
1757
1758 /* Convert pid to printable format. */
1759 char *
1760 cygwin_pid_to_str (ptid_t ptid)
1761 {
1762 static char buf[80];
1763 int pid = PIDGET (ptid);
1764
1765 if ((DWORD) pid == current_event.dwProcessId)
1766 xaprintf (buf, "process %d", pid);
1767 else
1768 xasprintf (buf, "thread %ld.0x%x", current_event.dwProcessId, pid);
1769 return buf;
1770 }
1771
1772 static int
1773 core_dll_symbols_add (char *dll_name, DWORD base_addr)
1774 {
1775 struct objfile *objfile;
1776 char *objfile_basename;
1777 const char *dll_basename;
1778
1779 if (!(dll_basename = strrchr (dll_name, '/')))
1780 dll_basename = dll_name;
1781 else
1782 dll_basename++;
1783
1784 ALL_OBJFILES (objfile)
1785 {
1786 objfile_basename = strrchr (objfile->name, '/');
1787
1788 if (objfile_basename &&
1789 strcmp (dll_basename, objfile_basename + 1) == 0)
1790 {
1791 printf_unfiltered ("%08lx:%s (symbols previously loaded)\n",
1792 base_addr, dll_name);
1793 goto out;
1794 }
1795 }
1796
1797 register_loaded_dll (dll_name, base_addr + 0x1000);
1798 solib_symbols_add (dll_name, 0, (CORE_ADDR) base_addr + 0x1000);
1799
1800 out:
1801 return 1;
1802 }
1803
1804 typedef struct
1805 {
1806 struct target_ops *target;
1807 bfd_vma addr;
1808 }
1809 map_code_section_args;
1810
1811 static void
1812 map_single_dll_code_section (bfd * abfd, asection * sect, void *obj)
1813 {
1814 int old;
1815 int update_coreops;
1816 struct section_table *new_target_sect_ptr;
1817
1818 map_code_section_args *args = (map_code_section_args *) obj;
1819 struct target_ops *target = args->target;
1820 if (sect->flags & SEC_CODE)
1821 {
1822 update_coreops = core_ops.to_sections == target->to_sections;
1823
1824 if (target->to_sections)
1825 {
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));
1830 }
1831 else
1832 {
1833 old = 0;
1834 target->to_sections = (struct section_table *)
1835 xmalloc ((sizeof (struct section_table)));
1836 }
1837 target->to_sections_end = target->to_sections + (1 + old);
1838
1839 /* Update the to_sections field in the core_ops structure
1840 if needed. */
1841 if (update_coreops)
1842 {
1843 core_ops.to_sections = target->to_sections;
1844 core_ops.to_sections_end = target->to_sections_end;
1845 }
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;
1852 }
1853 }
1854
1855 static int
1856 dll_code_sections_add (const char *dll_name, int base_addr, struct target_ops *target)
1857 {
1858 bfd *dll_bfd;
1859 map_code_section_args map_args;
1860 asection *lowest_sect;
1861 char *name;
1862 if (dll_name == NULL || target == NULL)
1863 return 0;
1864 name = xstrdup (dll_name);
1865 dll_bfd = bfd_openr (name, "pei-i386");
1866 if (dll_bfd == NULL)
1867 return 0;
1868
1869 if (bfd_check_format (dll_bfd, bfd_object))
1870 {
1871 lowest_sect = bfd_get_section_by_name (dll_bfd, ".text");
1872 if (lowest_sect == NULL)
1873 return 0;
1874 map_args.target = target;
1875 map_args.addr = base_addr - bfd_section_vma (dll_bfd, lowest_sect);
1876
1877 bfd_map_over_sections (dll_bfd, &map_single_dll_code_section, (void *) (&map_args));
1878 }
1879
1880 return 1;
1881 }
1882
1883 static void
1884 core_section_load_dll_symbols (bfd * abfd, asection * sect, void *obj)
1885 {
1886 struct target_ops *target = (struct target_ops *) obj;
1887
1888 DWORD base_addr;
1889
1890 int dll_name_size;
1891 char *dll_name = NULL;
1892 char *buf = NULL;
1893 struct win32_pstatus *pstatus;
1894 char *p;
1895
1896 if (strncmp (sect->name, ".module", 7))
1897 return;
1898
1899 buf = (char *) xmalloc (sect->_raw_size + 1);
1900 if (!buf)
1901 {
1902 printf_unfiltered ("memory allocation failed for %s\n", sect->name);
1903 goto out;
1904 }
1905 if (!bfd_get_section_contents (abfd, sect, buf, 0, sect->_raw_size))
1906 goto out;
1907
1908 pstatus = (struct win32_pstatus *) buf;
1909
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)
1913 goto out;
1914
1915 dll_name = (char *) xmalloc (dll_name_size + 1);
1916 if (!dll_name)
1917 {
1918 printf_unfiltered ("memory allocation failed for %s\n", sect->name);
1919 goto out;
1920 }
1921 strncpy (dll_name, pstatus->data.module_info.module_name, dll_name_size);
1922
1923 while ((p = strchr (dll_name, '\\')))
1924 *p = '/';
1925
1926 if (!core_dll_symbols_add (dll_name, (DWORD) base_addr))
1927 printf_unfiltered ("%s: Failed to load dll symbols.\n", dll_name);
1928
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);
1931
1932 out:
1933 if (buf)
1934 xfree (buf);
1935 if (dll_name)
1936 xfree (dll_name);
1937 return;
1938 }
1939
1940 void
1941 child_solib_add (char *filename, int from_tty, struct target_ops *target,
1942 int readsyms)
1943 {
1944 if (!readsyms)
1945 return;
1946 if (core_bfd)
1947 {
1948 child_clear_solibs ();
1949 bfd_map_over_sections (core_bfd, &core_section_load_dll_symbols, target);
1950 }
1951 else
1952 {
1953 if (solib_end && solib_end->name)
1954 solib_end->objfile = solib_symbols_add (solib_end->name, from_tty,
1955 solib_end->load_addr);
1956 }
1957 }
1958
1959 static void
1960 fetch_elf_core_registers (char *core_reg_sect,
1961 unsigned core_reg_size,
1962 int which,
1963 CORE_ADDR reg_addr)
1964 {
1965 int r;
1966 if (core_reg_size < sizeof (CONTEXT))
1967 {
1968 error ("Core file register section too small (%u bytes).", core_reg_size);
1969 return;
1970 }
1971 for (r = 0; r < NUM_REGS; r++)
1972 supply_register (r, core_reg_sect + mappings[r]);
1973 }
1974
1975 static struct core_fns win32_elf_core_fns =
1976 {
1977 bfd_target_elf_flavour,
1978 default_check_format,
1979 default_core_sniffer,
1980 fetch_elf_core_registers,
1981 NULL
1982 };
1983
1984 void
1985 _initialize_core_win32 (void)
1986 {
1987 add_core_fns (&win32_elf_core_fns);
1988 }
1989
1990 void
1991 _initialize_check_for_gdb_ini (void)
1992 {
1993 char *homedir;
1994 if (inhibit_gdbinit)
1995 return;
1996
1997 homedir = getenv ("HOME");
1998 if (homedir)
1999 {
2000 char *p;
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] != '/')
2006 *p++ = '/';
2007 strcpy (p, "gdb.ini");
2008 if (access (oldini, 0) == 0)
2009 {
2010 int len = strlen (oldini);
2011 char *newini = alloca (len + 1);
2012 xasprintf (newini, "%.*s.gdbinit",
2013 (int) (len - (sizeof ("gdb.ini") - 1)), oldini);
2014 warning ("obsolete '%s' found. Rename to '%s'.", oldini, newini);
2015 }
2016 }
2017 }
This page took 0.100456 seconds and 5 git commands to generate.