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