* config/djgpp/djconfig.sh: Add copyright notice.
[external/binutils.git] / gdb / go32-nat.c
1 /* Native debugging support for Intel x86 running DJGPP.
2    Copyright 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
3    Written by Robert Hoehne.
4
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 even the 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
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <fcntl.h>
23
24 #include "defs.h"
25 #include "inferior.h"
26 #include "gdb_wait.h"
27 #include "gdbcore.h"
28 #include "command.h"
29 #include "floatformat.h"
30 #include "buildsym.h"
31 #include "i387-nat.h"
32 #include "value.h"
33 #include "regcache.h"
34 #include "gdb_string.h"
35
36 #include <stdio.h>              /* required for __DJGPP_MINOR__ */
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <io.h>
41 #include <dpmi.h>
42 #include <debug/v2load.h>
43 #include <debug/dbgcom.h>
44 #if __DJGPP_MINOR__ > 2
45 #include <debug/redir.h>
46 #endif
47
48 #if __DJGPP_MINOR__ < 3
49 /* This code will be provided from DJGPP 2.03 on. Until then I code it
50    here */
51 typedef struct
52   {
53     unsigned short sig0;
54     unsigned short sig1;
55     unsigned short sig2;
56     unsigned short sig3;
57     unsigned short exponent:15;
58     unsigned short sign:1;
59   }
60 NPXREG;
61
62 typedef struct
63   {
64     unsigned int control;
65     unsigned int status;
66     unsigned int tag;
67     unsigned int eip;
68     unsigned int cs;
69     unsigned int dataptr;
70     unsigned int datasel;
71     NPXREG reg[8];
72   }
73 NPX;
74
75 static NPX npx;
76
77 static void save_npx (void);    /* Save the FPU of the debugged program */
78 static void load_npx (void);    /* Restore the FPU of the debugged program */
79
80 /* ------------------------------------------------------------------------- */
81 /* Store the contents of the NPX in the global variable `npx'.  */
82 /* *INDENT-OFF* */
83
84 static void
85 save_npx (void)
86 {
87   asm ("inb    $0xa0, %%al
88        testb $0x20, %%al
89        jz 1f
90        xorb %% al, %%al
91        outb %% al, $0xf0
92        movb $0x20, %%al
93        outb %% al, $0xa0
94        outb %% al, $0x20
95 1:
96        fnsave % 0
97        fwait "
98 :     "=m" (npx)
99 :                               /* No input */
100 :     "%eax");
101 }
102
103 /* *INDENT-ON* */
104
105
106
107
108
109 /* ------------------------------------------------------------------------- */
110 /* Reload the contents of the NPX from the global variable `npx'.  */
111
112 static void
113 load_npx (void)
114 {
115 asm ("frstor %0":"=m" (npx));
116 }
117 /* ------------------------------------------------------------------------- */
118 /* Stubs for the missing redirection functions.  */
119 typedef struct {
120   char *command;
121   int redirected;
122 } cmdline_t;
123
124 void
125 redir_cmdline_delete (cmdline_t *ptr) {ptr->redirected = 0;}
126
127 int
128 redir_cmdline_parse (const char *args, cmdline_t *ptr)
129 {
130   return -1;
131 }
132 int
133 redir_to_child (cmdline_t *ptr)
134 {
135   return 1;
136 }
137 int
138 redir_to_debugger (cmdline_t *ptr)
139 {
140   return 1;
141 }
142 int
143 redir_debug_init (cmdline_t *ptr) { return 0; }
144 #endif /* __DJGPP_MINOR < 3 */
145
146 typedef enum { wp_insert, wp_remove, wp_count } wp_op;
147
148 /* This holds the current reference counts for each debug register.  */
149 static int dr_ref_count[4];
150
151 #define SOME_PID 42
152
153 static int prog_has_started = 0;
154 static void go32_open (char *name, int from_tty);
155 static void go32_close (int quitting);
156 static void go32_attach (char *args, int from_tty);
157 static void go32_detach (char *args, int from_tty);
158 static void go32_resume (int pid, int step, enum target_signal siggnal);
159 static int go32_wait (int pid, struct target_waitstatus *status);
160 static void go32_fetch_registers (int regno);
161 static void store_register (int regno);
162 static void go32_store_registers (int regno);
163 static void go32_prepare_to_store (void);
164 static int go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
165                              int write,
166                              struct mem_attrib *attrib,
167                              struct target_ops *target);
168 static void go32_files_info (struct target_ops *target);
169 static void go32_stop (void);
170 static void go32_kill_inferior (void);
171 static void go32_create_inferior (char *exec_file, char *args, char **env);
172 static void go32_mourn_inferior (void);
173 static int go32_can_run (void);
174
175 static struct target_ops go32_ops;
176 static void go32_terminal_init (void);
177 static void go32_terminal_inferior (void);
178 static void go32_terminal_ours (void);
179
180 #define r_ofs(x) (offsetof(TSS,x))
181
182 static struct
183 {
184   size_t tss_ofs;
185   size_t size;
186 }
187 regno_mapping[] =
188 {
189   {r_ofs (tss_eax), 4}, /* normal registers, from a_tss */
190   {r_ofs (tss_ecx), 4},
191   {r_ofs (tss_edx), 4},
192   {r_ofs (tss_ebx), 4},
193   {r_ofs (tss_esp), 4},
194   {r_ofs (tss_ebp), 4},
195   {r_ofs (tss_esi), 4},
196   {r_ofs (tss_edi), 4},
197   {r_ofs (tss_eip), 4},
198   {r_ofs (tss_eflags), 4},
199   {r_ofs (tss_cs), 2},
200   {r_ofs (tss_ss), 2},
201   {r_ofs (tss_ds), 2},
202   {r_ofs (tss_es), 2},
203   {r_ofs (tss_fs), 2},
204   {r_ofs (tss_gs), 2},
205   {0, 10},              /* 8 FP registers, from npx.reg[] */
206   {1, 10},
207   {2, 10},
208   {3, 10},
209   {4, 10},
210   {5, 10},
211   {6, 10},
212   {7, 10},
213         /* The order of the next 7 registers must be consistent
214            with their numbering in config/i386/tm-i386.h, which see.  */
215   {0, 2},               /* control word, from npx */
216   {4, 2},               /* status word, from npx */
217   {8, 2},               /* tag word, from npx */
218   {16, 2},              /* last FP exception CS from npx */
219   {12, 4},              /* last FP exception EIP from npx */
220   {24, 2},              /* last FP exception operand selector from npx */
221   {20, 4},              /* last FP exception operand offset from npx */
222   {18, 2}               /* last FP opcode from npx */
223 };
224
225 static struct
226   {
227     int go32_sig;
228     enum target_signal gdb_sig;
229   }
230 sig_map[] =
231 {
232   {0, TARGET_SIGNAL_FPE},
233   {1, TARGET_SIGNAL_TRAP},
234   /* Exception 2 is triggered by the NMI.  DJGPP handles it as SIGILL,
235      but I think SIGBUS is better, since the NMI is usually activated
236      as a result of a memory parity check failure.  */
237   {2, TARGET_SIGNAL_BUS},
238   {3, TARGET_SIGNAL_TRAP},
239   {4, TARGET_SIGNAL_FPE},
240   {5, TARGET_SIGNAL_SEGV},
241   {6, TARGET_SIGNAL_ILL},
242   {7, TARGET_SIGNAL_EMT},       /* no-coprocessor exception */
243   {8, TARGET_SIGNAL_SEGV},
244   {9, TARGET_SIGNAL_SEGV},
245   {10, TARGET_SIGNAL_BUS},
246   {11, TARGET_SIGNAL_SEGV},
247   {12, TARGET_SIGNAL_SEGV},
248   {13, TARGET_SIGNAL_SEGV},
249   {14, TARGET_SIGNAL_SEGV},
250   {16, TARGET_SIGNAL_FPE},
251   {17, TARGET_SIGNAL_BUS},
252   {31, TARGET_SIGNAL_ILL},
253   {0x1b, TARGET_SIGNAL_INT},
254   {0x75, TARGET_SIGNAL_FPE},
255   {0x78, TARGET_SIGNAL_ALRM},
256   {0x79, TARGET_SIGNAL_INT},
257   {0x7a, TARGET_SIGNAL_QUIT},
258   {-1, TARGET_SIGNAL_LAST}
259 };
260
261 static struct {
262   enum target_signal gdb_sig;
263   int djgpp_excepno;
264 } excepn_map[] = {
265   {TARGET_SIGNAL_0, -1},
266   {TARGET_SIGNAL_ILL, 6},       /* Invalid Opcode */
267   {TARGET_SIGNAL_EMT, 7},       /* triggers SIGNOFP */
268   {TARGET_SIGNAL_SEGV, 13},     /* GPF */
269   {TARGET_SIGNAL_BUS, 17},      /* Alignment Check */
270   /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
271      details.  */
272   {TARGET_SIGNAL_TERM, 0x1b},   /* triggers Ctrl-Break type of SIGINT */
273   {TARGET_SIGNAL_FPE, 0x75},
274   {TARGET_SIGNAL_INT, 0x79},
275   {TARGET_SIGNAL_QUIT, 0x7a},
276   {TARGET_SIGNAL_ALRM, 0x78},   /* triggers SIGTIMR */
277   {TARGET_SIGNAL_PROF, 0x78},
278   {TARGET_SIGNAL_LAST, -1}
279 };
280
281 static void
282 go32_open (char *name, int from_tty)
283 {
284   printf_unfiltered ("Done.  Use the \"run\" command to run the program.\n");
285 }
286
287 static void
288 go32_close (int quitting)
289 {
290 }
291
292 static void
293 go32_attach (char *args, int from_tty)
294 {
295   error ("\
296 You cannot attach to a running program on this platform.\n\
297 Use the `run' command to run DJGPP programs.");
298 }
299
300 static void
301 go32_detach (char *args, int from_tty)
302 {
303 }
304
305 static int resume_is_step;
306 static int resume_signal = -1;
307
308 static void
309 go32_resume (int pid, int step, enum target_signal siggnal)
310 {
311   int i;
312
313   resume_is_step = step;
314
315   if (siggnal != TARGET_SIGNAL_0 && siggnal != TARGET_SIGNAL_TRAP)
316   {
317     for (i = 0, resume_signal = -1;
318          excepn_map[i].gdb_sig != TARGET_SIGNAL_LAST; i++)
319       if (excepn_map[i].gdb_sig == siggnal)
320       {
321         resume_signal = excepn_map[i].djgpp_excepno;
322         break;
323       }
324     if (resume_signal == -1)
325       printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
326                          target_signal_to_name (siggnal));
327   }
328 }
329
330 static char child_cwd[FILENAME_MAX];
331
332 static int
333 go32_wait (int pid, struct target_waitstatus *status)
334 {
335   int i;
336   unsigned char saved_opcode;
337   unsigned long INT3_addr = 0;
338   int stepping_over_INT = 0;
339
340   a_tss.tss_eflags &= 0xfeff;   /* reset the single-step flag (TF) */
341   if (resume_is_step)
342     {
343       /* If the next instruction is INT xx or INTO, we need to handle
344          them specially.  Intel manuals say that these instructions
345          reset the single-step flag (a.k.a. TF).  However, it seems
346          that, at least in the DPMI environment, and at least when
347          stepping over the DPMI interrupt 31h, the problem is having
348          TF set at all when INT 31h is executed: the debuggee either
349          crashes (and takes the system with it) or is killed by a
350          SIGTRAP.
351
352          So we need to emulate single-step mode: we put an INT3 opcode
353          right after the INT xx instruction, let the debuggee run
354          until it hits INT3 and stops, then restore the original
355          instruction which we overwrote with the INT3 opcode, and back
356          up the debuggee's EIP to that instruction.  */
357       read_child (a_tss.tss_eip, &saved_opcode, 1);
358       if (saved_opcode == 0xCD || saved_opcode == 0xCE)
359         {
360           unsigned char INT3_opcode = 0xCC;
361
362           INT3_addr
363             = saved_opcode == 0xCD ? a_tss.tss_eip + 2 : a_tss.tss_eip + 1;
364           stepping_over_INT = 1;
365           read_child (INT3_addr, &saved_opcode, 1);
366           write_child (INT3_addr, &INT3_opcode, 1);
367         }
368       else
369         a_tss.tss_eflags |= 0x0100; /* normal instruction: set TF */
370     }
371
372   /* The special value FFFFh in tss_trap indicates to run_child that
373      tss_irqn holds a signal to be delivered to the debuggee.  */
374   if (resume_signal <= -1)
375     {
376       a_tss.tss_trap = 0;
377       a_tss.tss_irqn = 0xff;
378     }
379   else
380     {
381       a_tss.tss_trap = 0xffff;  /* run_child looks for this */
382       a_tss.tss_irqn = resume_signal;
383     }
384
385   /* The child might change working directory behind our back.  The
386      GDB users won't like the side effects of that when they work with
387      relative file names, and GDB might be confused by its current
388      directory not being in sync with the truth.  So we always make a
389      point of changing back to where GDB thinks is its cwd, when we
390      return control to the debugger, but restore child's cwd before we
391      run it.  */
392   /* Initialize child_cwd, before the first call to run_child and not
393      in the initialization, so the child get also the changed directory
394      set with the gdb-command "cd ..." */
395   if (!*child_cwd)
396     /* Initialize child's cwd with the current one.  */
397     getcwd (child_cwd, sizeof (child_cwd));
398
399   chdir (child_cwd);
400
401 #if __DJGPP_MINOR__ < 3
402   load_npx ();
403 #endif
404   run_child ();
405 #if __DJGPP_MINOR__ < 3
406   save_npx ();
407 #endif
408
409   /* Did we step over an INT xx instruction?  */
410   if (stepping_over_INT && a_tss.tss_eip == INT3_addr + 1)
411     {
412       /* Restore the original opcode.  */
413       a_tss.tss_eip--;  /* EIP points *after* the INT3 instruction */
414       write_child (a_tss.tss_eip, &saved_opcode, 1);
415       /* Simulate a TRAP exception.  */
416       a_tss.tss_irqn = 1;
417       a_tss.tss_eflags |= 0x0100;
418     }
419
420   getcwd (child_cwd, sizeof (child_cwd)); /* in case it has changed */
421   chdir (current_directory);
422
423   if (a_tss.tss_irqn == 0x21)
424     {
425       status->kind = TARGET_WAITKIND_EXITED;
426       status->value.integer = a_tss.tss_eax & 0xff;
427     }
428   else
429     {
430       status->value.sig = TARGET_SIGNAL_UNKNOWN;
431       status->kind = TARGET_WAITKIND_STOPPED;
432       for (i = 0; sig_map[i].go32_sig != -1; i++)
433         {
434           if (a_tss.tss_irqn == sig_map[i].go32_sig)
435             {
436 #if __DJGPP_MINOR__ < 3
437               if ((status->value.sig = sig_map[i].gdb_sig) !=
438                   TARGET_SIGNAL_TRAP)
439                 status->kind = TARGET_WAITKIND_SIGNALLED;
440 #else
441               status->value.sig = sig_map[i].gdb_sig;
442 #endif
443               break;
444             }
445         }
446     }
447   return SOME_PID;
448 }
449
450 static void
451 fetch_register (int regno)
452 {
453   if (regno < FP0_REGNUM)
454     supply_register (regno, (char *) &a_tss + regno_mapping[regno].tss_ofs);
455   else if (regno <= LAST_FPU_CTRL_REGNUM)
456     i387_supply_register (regno, (char *) &npx);
457   else
458     internal_error (__FILE__, __LINE__,
459                     "Invalid register no. %d in fetch_register.", regno);
460 }
461
462 static void
463 go32_fetch_registers (int regno)
464 {
465   if (regno >= 0)
466     fetch_register (regno);
467   else
468     {
469       for (regno = 0; regno < FP0_REGNUM; regno++)
470         fetch_register (regno);
471       i387_supply_fsave ((char *) &npx);
472     }
473 }
474
475 static void
476 store_register (int regno)
477 {
478   void *rp;
479   void *v = (void *) register_buffer (regno);
480
481   if (regno < FP0_REGNUM)
482     memcpy ((char *) &a_tss + regno_mapping[regno].tss_ofs,
483             v, regno_mapping[regno].size);
484   else if (regno <= LAST_FPU_CTRL_REGNUM)
485     i387_fill_fsave ((char *)&npx, regno);
486   else
487     internal_error (__FILE__, __LINE__,
488                     "Invalid register no. %d in store_register.", regno);
489 }
490
491 static void
492 go32_store_registers (int regno)
493 {
494   unsigned r;
495
496   if (regno >= 0)
497     store_register (regno);
498   else
499     {
500       for (r = 0; r < FP0_REGNUM; r++)
501         store_register (r);
502       i387_fill_fsave ((char *) &npx, -1);
503     }
504 }
505
506 static void
507 go32_prepare_to_store (void)
508 {
509 }
510
511 static int
512 go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
513                   struct mem_attrib *attrib, struct target_ops *target)
514 {
515   if (write)
516     {
517       if (write_child (memaddr, myaddr, len))
518         {
519           return 0;
520         }
521       else
522         {
523           return len;
524         }
525     }
526   else
527     {
528       if (read_child (memaddr, myaddr, len))
529         {
530           return 0;
531         }
532       else
533         {
534           return len;
535         }
536     }
537 }
538
539 static cmdline_t child_cmd;     /* parsed child's command line kept here */
540
541 static void
542 go32_files_info (struct target_ops *target)
543 {
544   printf_unfiltered ("You are running a DJGPP V2 program.\n");
545 }
546
547 static void
548 go32_stop (void)
549 {
550   normal_stop ();
551   cleanup_client ();
552   inferior_pid = 0;
553   prog_has_started = 0;
554 }
555
556 static void
557 go32_kill_inferior (void)
558 {
559   redir_cmdline_delete (&child_cmd);
560   resume_signal = -1;
561   resume_is_step = 0;
562   unpush_target (&go32_ops);
563 }
564
565 static void
566 go32_create_inferior (char *exec_file, char *args, char **env)
567 {
568   extern char **environ;
569   jmp_buf start_state;
570   char *cmdline;
571   char **env_save = environ;
572
573   /* If no exec file handed to us, get it from the exec-file command -- with
574      a good, common error message if none is specified.  */
575   if (exec_file == 0)
576     exec_file = get_exec_file (1);
577
578   if (prog_has_started)
579     {
580       go32_stop ();
581       go32_kill_inferior ();
582     }
583   resume_signal = -1;
584   resume_is_step = 0;
585
586   /* Initialize child's cwd as empty to be initialized when starting
587      the child.  */
588   *child_cwd = 0;
589
590   /* Init command line storage.  */
591   if (redir_debug_init (&child_cmd) == -1)
592     internal_error (__FILE__, __LINE__,
593                     "Cannot allocate redirection storage: not enough memory.\n");
594
595   /* Parse the command line and create redirections.  */
596   if (strpbrk (args, "<>"))
597     {
598       if (redir_cmdline_parse (args, &child_cmd) == 0)
599         args = child_cmd.command;
600       else
601         error ("Syntax error in command line.");
602     }
603   else
604     child_cmd.command = xstrdup (args);
605
606   cmdline = (char *) alloca (strlen (args) + 4);
607   cmdline[0] = strlen (args);
608   strcpy (cmdline + 1, args);
609   cmdline[strlen (args) + 1] = 13;
610
611   environ = env;
612
613   if (v2loadimage (exec_file, cmdline, start_state))
614     {
615       environ = env_save;
616       printf_unfiltered ("Load failed for image %s\n", exec_file);
617       exit (1);
618     }
619   environ = env_save;
620
621   edi_init (start_state);
622 #if __DJGPP_MINOR__ < 3
623   save_npx ();
624 #endif
625
626   inferior_pid = SOME_PID;
627   push_target (&go32_ops);
628   clear_proceed_status ();
629   insert_breakpoints ();
630   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
631   prog_has_started = 1;
632 }
633
634 static void
635 go32_mourn_inferior (void)
636 {
637   /* We need to make sure all the breakpoint enable bits in the DR7
638      register are reset when the inferior exits.  Otherwise, if they
639      rerun the inferior, the uncleared bits may cause random SIGTRAPs,
640      failure to set more watchpoints, and other calamities.  It would
641      be nice if GDB itself would take care to remove all breakpoints
642      at all times, but it doesn't, probably under an assumption that
643      the OS cleans up when the debuggee exits.  */
644   i386_cleanup_dregs ();
645   go32_kill_inferior ();
646   generic_mourn_inferior ();
647 }
648
649 static int
650 go32_can_run (void)
651 {
652   return 1;
653 }
654
655 /* Hardware watchpoint support.  */
656
657 #define D_REGS edi.dr
658 #define CONTROL D_REGS[7]
659 #define STATUS D_REGS[6]
660
661 /* Pass the address ADDR to the inferior in the I'th debug register.
662    Here we just store the address in D_REGS, the watchpoint will be
663    actually set up when go32_wait runs the debuggee.  */
664 void
665 go32_set_dr (int i, CORE_ADDR addr)
666 {
667   if (i < 0 || i > 3)
668     internal_error (__FILE__, __LINE__, 
669                     "Invalid register %d in go32_set_dr.\n", i);
670   D_REGS[i] = addr;
671 }
672
673 /* Pass the value VAL to the inferior in the DR7 debug control
674    register.  Here we just store the address in D_REGS, the watchpoint
675    will be actually set up when go32_wait runs the debuggee.  */
676 void
677 go32_set_dr7 (unsigned val)
678 {
679   CONTROL = val;
680 }
681
682 /* Get the value of the DR6 debug status register from the inferior.
683    Here we just return the value stored in D_REGS, as we've got it
684    from the last go32_wait call.  */
685 unsigned
686 go32_get_dr6 (void)
687 {
688   return STATUS;
689 }
690
691 /* Put the device open on handle FD into either raw or cooked
692    mode, return 1 if it was in raw mode, zero otherwise.  */
693
694 static int
695 device_mode (int fd, int raw_p)
696 {
697   int oldmode, newmode;
698   __dpmi_regs regs;
699
700   regs.x.ax = 0x4400;
701   regs.x.bx = fd;
702   __dpmi_int (0x21, &regs);
703   if (regs.x.flags & 1)
704     return -1;
705   newmode = oldmode = regs.x.dx;
706
707   if (raw_p)
708     newmode |= 0x20;
709   else
710     newmode &= ~0x20;
711
712   if (oldmode & 0x80)   /* Only for character dev */
713   {
714     regs.x.ax = 0x4401;
715     regs.x.bx = fd;
716     regs.x.dx = newmode & 0xff;   /* Force upper byte zero, else it fails */
717     __dpmi_int (0x21, &regs);
718     if (regs.x.flags & 1)
719       return -1;
720   }
721   return (oldmode & 0x20) == 0x20;
722 }
723
724
725 static int inf_mode_valid = 0;
726 static int inf_terminal_mode;
727
728 /* This semaphore is needed because, amazingly enough, GDB calls
729    target.to_terminal_ours more than once after the inferior stops.
730    But we need the information from the first call only, since the
731    second call will always see GDB's own cooked terminal.  */
732 static int terminal_is_ours = 1;
733
734 static void
735 go32_terminal_init (void)
736 {
737   inf_mode_valid = 0;   /* reinitialize, in case they are restarting child */
738   terminal_is_ours = 1;
739 }
740
741 static void
742 go32_terminal_info (char *args, int from_tty)
743 {
744   printf_unfiltered ("Inferior's terminal is in %s mode.\n",
745                      !inf_mode_valid
746                      ? "default" : inf_terminal_mode ? "raw" : "cooked");
747
748 #if __DJGPP_MINOR__ > 2
749   if (child_cmd.redirection)
750   {
751     int i;
752
753     for (i = 0; i < DBG_HANDLES; i++)
754     {
755       if (child_cmd.redirection[i]->file_name)
756         printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
757                            i, child_cmd.redirection[i]->file_name);
758       else if (_get_dev_info (child_cmd.redirection[i]->inf_handle) == -1)
759         printf_unfiltered
760           ("\tFile handle %d appears to be closed by inferior.\n", i);
761       /* Mask off the raw/cooked bit when comparing device info words.  */
762       else if ((_get_dev_info (child_cmd.redirection[i]->inf_handle) & 0xdf)
763                != (_get_dev_info (i) & 0xdf))
764         printf_unfiltered
765           ("\tFile handle %d appears to be redirected by inferior.\n", i);
766     }
767   }
768 #endif
769 }
770
771 static void
772 go32_terminal_inferior (void)
773 {
774   /* Redirect standard handles as child wants them.  */
775   errno = 0;
776   if (redir_to_child (&child_cmd) == -1)
777   {
778     redir_to_debugger (&child_cmd);
779     error ("Cannot redirect standard handles for program: %s.",
780            strerror (errno));
781   }
782   /* set the console device of the inferior to whatever mode
783      (raw or cooked) we found it last time */
784   if (terminal_is_ours)
785   {
786     if (inf_mode_valid)
787       device_mode (0, inf_terminal_mode);
788     terminal_is_ours = 0;
789   }
790 }
791
792 static void
793 go32_terminal_ours (void)
794 {
795   /* Switch to cooked mode on the gdb terminal and save the inferior
796      terminal mode to be restored when it is resumed */
797   if (!terminal_is_ours)
798   {
799     inf_terminal_mode = device_mode (0, 0);
800     if (inf_terminal_mode != -1)
801       inf_mode_valid = 1;
802     else
803       /* If device_mode returned -1, we don't know what happens with
804          handle 0 anymore, so make the info invalid.  */
805       inf_mode_valid = 0;
806     terminal_is_ours = 1;
807
808     /* Restore debugger's standard handles.  */
809     errno = 0;
810     if (redir_to_debugger (&child_cmd) == -1)
811     {
812       redir_to_child (&child_cmd);
813       error ("Cannot redirect standard handles for debugger: %s.",
814              strerror (errno));
815     }
816   }
817 }
818
819 static void
820 init_go32_ops (void)
821 {
822   go32_ops.to_shortname = "djgpp";
823   go32_ops.to_longname = "djgpp target process";
824   go32_ops.to_doc =
825     "Program loaded by djgpp, when gdb is used as an external debugger";
826   go32_ops.to_open = go32_open;
827   go32_ops.to_close = go32_close;
828   go32_ops.to_attach = go32_attach;
829   go32_ops.to_detach = go32_detach;
830   go32_ops.to_resume = go32_resume;
831   go32_ops.to_wait = go32_wait;
832   go32_ops.to_fetch_registers = go32_fetch_registers;
833   go32_ops.to_store_registers = go32_store_registers;
834   go32_ops.to_prepare_to_store = go32_prepare_to_store;
835   go32_ops.to_xfer_memory = go32_xfer_memory;
836   go32_ops.to_files_info = go32_files_info;
837   go32_ops.to_insert_breakpoint = memory_insert_breakpoint;
838   go32_ops.to_remove_breakpoint = memory_remove_breakpoint;
839   go32_ops.to_terminal_init = go32_terminal_init;
840   go32_ops.to_terminal_inferior = go32_terminal_inferior;
841   go32_ops.to_terminal_ours_for_output = go32_terminal_ours;
842   go32_ops.to_terminal_ours = go32_terminal_ours;
843   go32_ops.to_terminal_info = go32_terminal_info;
844   go32_ops.to_kill = go32_kill_inferior;
845   go32_ops.to_create_inferior = go32_create_inferior;
846   go32_ops.to_mourn_inferior = go32_mourn_inferior;
847   go32_ops.to_can_run = go32_can_run;
848   go32_ops.to_stop = go32_stop;
849   go32_ops.to_stratum = process_stratum;
850   go32_ops.to_has_all_memory = 1;
851   go32_ops.to_has_memory = 1;
852   go32_ops.to_has_stack = 1;
853   go32_ops.to_has_registers = 1;
854   go32_ops.to_has_execution = 1;
855   go32_ops.to_magic = OPS_MAGIC;
856
857   /* Initialize child's cwd as empty to be initialized when starting
858      the child.  */
859   *child_cwd = 0;
860
861   /* Initialize child's command line storage.  */
862   if (redir_debug_init (&child_cmd) == -1)
863     internal_error (__FILE__, __LINE__,
864                     "Cannot allocate redirection storage: not enough memory.\n");
865
866   /* We are always processing GCC-compiled programs.  */
867   processing_gcc_compilation = 2;
868 }
869
870 void
871 _initialize_go32_nat (void)
872 {
873   init_go32_ops ();
874   add_target (&go32_ops);
875 }
876
877 pid_t
878 tcgetpgrp (int fd)
879 {
880   if (isatty (fd))
881     return SOME_PID;
882   errno = ENOTTY;
883   return -1;
884 }
885
886 int
887 tcsetpgrp (int fd, pid_t pgid)
888 {
889   if (isatty (fd) && pgid == SOME_PID)
890     return 0;
891   errno = pgid == SOME_PID ? ENOTTY : ENOSYS;
892   return -1;
893 }