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