1 /* Native debugging support for Intel x86 running DJGPP.
2 Copyright 1997, 1999, 2001 Free Software Foundation, Inc.
3 Written by Robert Hoehne.
5 This file is part of GDB.
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.
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.
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. */
29 #include "floatformat.h"
32 #include <stdio.h> /* required for __DJGPP_MINOR__ */
39 #include <debug/v2load.h>
40 #include <debug/dbgcom.h>
41 #if __DJGPP_MINOR__ > 2
42 #include <debug/redir.h>
45 #if __DJGPP_MINOR__ < 3
46 /* This code will be provided from DJGPP 2.03 on. Until then I code it
54 unsigned short exponent:15;
55 unsigned short sign:1;
74 static void save_npx (void); /* Save the FPU of the debugged program */
75 static void load_npx (void); /* Restore the FPU of the debugged program */
77 /* ------------------------------------------------------------------------- */
78 /* Store the contents of the NPX in the global variable `npx'. */
106 /* ------------------------------------------------------------------------- */
107 /* Reload the contents of the NPX from the global variable `npx'. */
112 asm ("frstor %0":"=m" (npx));
114 /* ------------------------------------------------------------------------- */
115 /* Stubs for the missing redirection functions. */
121 void redir_cmdline_delete (cmdline_t *ptr) {ptr->redirected = 0;}
122 int redir_cmdline_parse (const char *args, cmdline_t *ptr)
126 int redir_to_child (cmdline_t *ptr)
130 int redir_to_debugger (cmdline_t *ptr)
134 int redir_debug_init (cmdline_t *ptr) { return 0; }
135 #endif /* __DJGPP_MINOR < 3 */
137 extern void _initialize_go32_nat (void);
139 typedef enum { wp_insert, wp_remove, wp_count } wp_op;
141 /* This holds the current reference counts for each debug register. */
142 static int dr_ref_count[4];
144 extern char **environ;
148 static int prog_has_started = 0;
149 static void go32_open (char *name, int from_tty);
150 static void go32_close (int quitting);
151 static void go32_attach (char *args, int from_tty);
152 static void go32_detach (char *args, int from_tty);
153 static void go32_resume (int pid, int step, enum target_signal siggnal);
154 static int go32_wait (int pid, struct target_waitstatus *status);
155 static void go32_fetch_registers (int regno);
156 static void store_register (int regno);
157 static void go32_store_registers (int regno);
158 static void go32_prepare_to_store (void);
159 static int go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
161 struct mem_attrib *attrib,
162 struct target_ops *target);
163 static void go32_files_info (struct target_ops *target);
164 static void go32_stop (void);
165 static void go32_kill_inferior (void);
166 static void go32_create_inferior (char *exec_file, char *args, char **env);
167 static void cleanup_dregs (void);
168 static void go32_mourn_inferior (void);
169 static int go32_can_run (void);
170 static int go32_insert_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
172 static int go32_remove_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
174 static int go32_handle_nonaligned_watchpoint (wp_op what, CORE_ADDR waddr,
175 CORE_ADDR addr, int len, int rw);
177 static struct target_ops go32_ops;
178 static void go32_terminal_init (void);
179 static void go32_terminal_inferior (void);
180 static void go32_terminal_ours (void);
182 #define r_ofs(x) (offsetof(TSS,x))
191 {r_ofs (tss_eax), 4}, /* normal registers, from a_tss */
192 {r_ofs (tss_ecx), 4},
193 {r_ofs (tss_edx), 4},
194 {r_ofs (tss_ebx), 4},
195 {r_ofs (tss_esp), 4},
196 {r_ofs (tss_ebp), 4},
197 {r_ofs (tss_esi), 4},
198 {r_ofs (tss_edi), 4},
199 {r_ofs (tss_eip), 4},
200 {r_ofs (tss_eflags), 4},
207 {0, 10}, /* 8 FP registers, from npx.reg[] */
215 /* The order of the next 7 registers must be consistent
216 with their numbering in config/i386/tm-i386.h, which see. */
217 {0, 2}, /* control word, from npx */
218 {4, 2}, /* status word, from npx */
219 {8, 2}, /* tag word, from npx */
220 {16, 2}, /* last FP exception CS from npx */
221 {12, 4}, /* last FP exception EIP from npx */
222 {24, 2}, /* last FP exception operand selector from npx */
223 {20, 4}, /* last FP exception operand offset from npx */
224 {18, 2} /* last FP opcode from npx */
230 enum target_signal gdb_sig;
234 {0, TARGET_SIGNAL_FPE},
235 {1, TARGET_SIGNAL_TRAP},
236 /* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
237 but I think SIGBUS is better, since the NMI is usually activated
238 as a result of a memory parity check failure. */
239 {2, TARGET_SIGNAL_BUS},
240 {3, TARGET_SIGNAL_TRAP},
241 {4, TARGET_SIGNAL_FPE},
242 {5, TARGET_SIGNAL_SEGV},
243 {6, TARGET_SIGNAL_ILL},
244 {7, TARGET_SIGNAL_EMT}, /* no-coprocessor exception */
245 {8, TARGET_SIGNAL_SEGV},
246 {9, TARGET_SIGNAL_SEGV},
247 {10, TARGET_SIGNAL_BUS},
248 {11, TARGET_SIGNAL_SEGV},
249 {12, TARGET_SIGNAL_SEGV},
250 {13, TARGET_SIGNAL_SEGV},
251 {14, TARGET_SIGNAL_SEGV},
252 {16, TARGET_SIGNAL_FPE},
253 {17, TARGET_SIGNAL_BUS},
254 {31, TARGET_SIGNAL_ILL},
255 {0x1b, TARGET_SIGNAL_INT},
256 {0x75, TARGET_SIGNAL_FPE},
257 {0x78, TARGET_SIGNAL_ALRM},
258 {0x79, TARGET_SIGNAL_INT},
259 {0x7a, TARGET_SIGNAL_QUIT},
260 {-1, TARGET_SIGNAL_LAST}
264 enum target_signal gdb_sig;
267 {TARGET_SIGNAL_0, -1},
268 {TARGET_SIGNAL_ILL, 6}, /* Invalid Opcode */
269 {TARGET_SIGNAL_EMT, 7}, /* triggers SIGNOFP */
270 {TARGET_SIGNAL_SEGV, 13}, /* GPF */
271 {TARGET_SIGNAL_BUS, 17}, /* Alignment Check */
272 /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
274 {TARGET_SIGNAL_TERM, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
275 {TARGET_SIGNAL_FPE, 0x75},
276 {TARGET_SIGNAL_INT, 0x79},
277 {TARGET_SIGNAL_QUIT, 0x7a},
278 {TARGET_SIGNAL_ALRM, 0x78}, /* triggers SIGTIMR */
279 {TARGET_SIGNAL_PROF, 0x78},
280 {TARGET_SIGNAL_LAST, -1}
284 go32_open (char *name ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
286 printf_unfiltered ("Done. Use the \"run\" command to run the program.\n");
290 go32_close (int quitting ATTRIBUTE_UNUSED)
295 go32_attach (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
298 You cannot attach to a running program on this platform.\n\
299 Use the `run' command to run DJGPP programs.");
303 go32_detach (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
307 static int resume_is_step;
308 static int resume_signal = -1;
311 go32_resume (int pid ATTRIBUTE_UNUSED, int step, enum target_signal siggnal)
315 resume_is_step = step;
317 if (siggnal != TARGET_SIGNAL_0 && siggnal != TARGET_SIGNAL_TRAP)
319 for (i = 0, resume_signal = -1;
320 excepn_map[i].gdb_sig != TARGET_SIGNAL_LAST; i++)
321 if (excepn_map[i].gdb_sig == siggnal)
323 resume_signal = excepn_map[i].djgpp_excepno;
326 if (resume_signal == -1)
327 printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
328 target_signal_to_name (siggnal));
332 static char child_cwd[FILENAME_MAX];
335 go32_wait (int pid ATTRIBUTE_UNUSED, struct target_waitstatus *status)
338 unsigned char saved_opcode;
339 unsigned long INT3_addr = 0;
340 int stepping_over_INT = 0;
342 a_tss.tss_eflags &= 0xfeff; /* reset the single-step flag (TF) */
345 /* If the next instruction is INT xx or INTO, we need to handle
346 them specially. Intel manuals say that these instructions
347 reset the single-step flag (a.k.a. TF). However, it seems
348 that, at least in the DPMI environment, and at least when
349 stepping over the DPMI interrupt 31h, the problem is having
350 TF set at all when INT 31h is executed: the debuggee either
351 crashes (and takes the system with it) or is killed by a
354 So we need to emulate single-step mode: we put an INT3 opcode
355 right after the INT xx instruction, let the debuggee run
356 until it hits INT3 and stops, then restore the original
357 instruction which we overwrote with the INT3 opcode, and back
358 up the debuggee's EIP to that instruction. */
359 read_child (a_tss.tss_eip, &saved_opcode, 1);
360 if (saved_opcode == 0xCD || saved_opcode == 0xCE)
362 unsigned char INT3_opcode = 0xCC;
365 = saved_opcode == 0xCD ? a_tss.tss_eip + 2 : a_tss.tss_eip + 1;
366 stepping_over_INT = 1;
367 read_child (INT3_addr, &saved_opcode, 1);
368 write_child (INT3_addr, &INT3_opcode, 1);
371 a_tss.tss_eflags |= 0x0100; /* normal instruction: set TF */
374 /* The special value FFFFh in tss_trap indicates to run_child that
375 tss_irqn holds a signal to be delivered to the debuggee. */
376 if (resume_signal <= -1)
379 a_tss.tss_irqn = 0xff;
383 a_tss.tss_trap = 0xffff; /* run_child looks for this */
384 a_tss.tss_irqn = resume_signal;
387 /* The child might change working directory behind our back. The
388 GDB users won't like the side effects of that when they work with
389 relative file names, and GDB might be confused by its current
390 directory not being in sync with the truth. So we always make a
391 point of changing back to where GDB thinks is its cwd, when we
392 return control to the debugger, but restore child's cwd before we
394 /* Initialize child_cwd, before the first call to run_child and not
395 in the initialization, so the child get also the changed directory
396 set with the gdb-command "cd ..." */
398 /* Initialize child's cwd with the current one. */
399 getcwd (child_cwd, sizeof (child_cwd));
403 #if __DJGPP_MINOR__ < 3
407 #if __DJGPP_MINOR__ < 3
411 /* Did we step over an INT xx instruction? */
412 if (stepping_over_INT && a_tss.tss_eip == INT3_addr + 1)
414 /* Restore the original opcode. */
415 a_tss.tss_eip--; /* EIP points *after* the INT3 instruction */
416 write_child (a_tss.tss_eip, &saved_opcode, 1);
417 /* Simulate a TRAP exception. */
419 a_tss.tss_eflags |= 0x0100;
422 getcwd (child_cwd, sizeof (child_cwd)); /* in case it has changed */
423 chdir (current_directory);
425 if (a_tss.tss_irqn == 0x21)
427 status->kind = TARGET_WAITKIND_EXITED;
428 status->value.integer = a_tss.tss_eax & 0xff;
432 status->value.sig = TARGET_SIGNAL_UNKNOWN;
433 status->kind = TARGET_WAITKIND_STOPPED;
434 for (i = 0; sig_map[i].go32_sig != -1; i++)
436 if (a_tss.tss_irqn == sig_map[i].go32_sig)
438 #if __DJGPP_MINOR__ < 3
439 if ((status->value.sig = sig_map[i].gdb_sig) !=
441 status->kind = TARGET_WAITKIND_SIGNALLED;
443 status->value.sig = sig_map[i].gdb_sig;
453 go32_fetch_registers (int regno)
456 int end_reg = regno + 1; /* just one reg initially */
458 if (regno < 0) /* do the all registers */
460 regno = 0; /* start at first register */
461 /* # regs in table */
462 end_reg = sizeof (regno_mapping) / sizeof (regno_mapping[0]);
465 for (; regno < end_reg; regno++)
468 supply_register (regno,
469 (char *) &a_tss + regno_mapping[regno].tss_ofs);
471 supply_register (regno,
472 (char *) &npx.reg[regno_mapping[regno].tss_ofs]);
477 switch (regno_mapping[regno].size)
480 regval = *(unsigned short *)
481 ((char *) &npx + regno_mapping[regno].tss_ofs);
483 if (regno == FOP_REGNUM && regval)
484 /* Feature: restore the 5 bits of the opcode
485 stripped by FSAVE/FNSAVE. */
489 regval = *(unsigned *)
490 ((char *) &npx + regno_mapping[regno].tss_ofs);
493 internal_error (__FILE__, __LINE__, "\
494 Invalid native size for register no. %d in go32_fetch_register.", regno);
496 supply_register (regno, (char *) ®val);
499 internal_error (__FILE__, __LINE__,
500 "Invalid register no. %d in go32_fetch_register.",
506 store_register (int regno)
509 void *v = (void *) ®isters[REGISTER_BYTE (regno)];
512 rp = (char *) &a_tss + regno_mapping[regno].tss_ofs;
514 rp = (char *) &npx.reg[regno_mapping[regno].tss_ofs];
516 rp = (char *) &npx + regno_mapping[regno].tss_ofs;
518 internal_error (__FILE__, __LINE__,
519 "Invalid register no. %d in store_register.", regno);
520 memcpy (rp, v, regno_mapping[regno].size);
521 if (regno == FOP_REGNUM)
522 *(short *)rp &= 0x07ff; /* strip high 5 bits, in case they added them */
526 go32_store_registers (int regno)
531 store_register (regno);
534 for (r = 0; r < sizeof (regno_mapping) / sizeof (regno_mapping[0]); r++)
540 go32_prepare_to_store (void)
545 go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
546 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
547 struct target_ops *target ATTRIBUTE_UNUSED)
551 if (write_child (memaddr, myaddr, len))
562 if (read_child (memaddr, myaddr, len))
573 static cmdline_t child_cmd; /* parsed child's command line kept here */
576 go32_files_info (struct target_ops *target ATTRIBUTE_UNUSED)
578 printf_unfiltered ("You are running a DJGPP V2 program.\n");
587 prog_has_started = 0;
591 go32_kill_inferior (void)
593 redir_cmdline_delete (&child_cmd);
596 unpush_target (&go32_ops);
600 go32_create_inferior (char *exec_file, char *args, char **env)
604 char **env_save = environ;
606 /* If no exec file handed to us, get it from the exec-file command -- with
607 a good, common error message if none is specified. */
609 exec_file = get_exec_file (1);
611 if (prog_has_started)
614 go32_kill_inferior ();
619 /* Initialize child's cwd as empty to be initialized when starting
623 /* Init command line storage. */
624 if (redir_debug_init (&child_cmd) == -1)
625 internal_error (__FILE__, __LINE__,
626 "Cannot allocate redirection storage: not enough memory.\n");
628 /* Parse the command line and create redirections. */
629 if (strpbrk (args, "<>"))
631 if (redir_cmdline_parse (args, &child_cmd) == 0)
632 args = child_cmd.command;
634 error ("Syntax error in command line.");
637 child_cmd.command = xstrdup (args);
639 cmdline = (char *) alloca (strlen (args) + 4);
640 cmdline[0] = strlen (args);
641 strcpy (cmdline + 1, args);
642 cmdline[strlen (args) + 1] = 13;
646 if (v2loadimage (exec_file, cmdline, start_state))
649 printf_unfiltered ("Load failed for image %s\n", exec_file);
654 edi_init (start_state);
655 #if __DJGPP_MINOR__ < 3
659 inferior_pid = SOME_PID;
660 push_target (&go32_ops);
661 clear_proceed_status ();
662 insert_breakpoints ();
663 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
664 prog_has_started = 1;
668 go32_mourn_inferior (void)
670 /* We need to make sure all the breakpoint enable bits in the DR7
671 register are reset when the inferior exits. Otherwise, if they
672 rerun the inferior, the uncleared bits may cause random SIGTRAPs,
673 failure to set more watchpoints, and other calamities. It would
674 be nice if GDB itself would take care to remove all breakpoints
675 at all times, but it doesn't, probably under an assumption that
676 the OS cleans up when the debuggee exits. */
678 go32_kill_inferior ();
679 generic_mourn_inferior ();
688 /* Hardware watchpoint support. */
692 #define DR_ENABLE_SIZE 2
693 #define DR_LOCAL_ENABLE_SHIFT 0
694 #define DR_GLOBAL_ENABLE_SHIFT 1
695 #define DR_LOCAL_SLOWDOWN 0x100
696 #define DR_GLOBAL_SLOWDOWN 0x200
697 #define DR_CONTROL_SHIFT 16
698 #define DR_CONTROL_SIZE 4
699 #define DR_RW_READWRITE 0x3
700 #define DR_RW_WRITE 0x1
701 #define DR_CONTROL_MASK 0xf
702 #define DR_ENABLE_MASK 0x3
707 #define D_REGS edi.dr
708 #define CONTROL D_REGS[DR_CONTROL]
709 #define STATUS D_REGS[DR_STATUS]
711 #define IS_REG_FREE(index) \
712 (!(CONTROL & (3 << (DR_ENABLE_SIZE * (index)))))
714 #define LOCAL_ENABLE_REG(index) \
715 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
717 #define GLOBAL_ENABLE_REG(index) \
718 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
720 #define DISABLE_REG(index) \
721 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * (index))))
723 #define SET_LOCAL_EXACT() \
724 (CONTROL |= DR_LOCAL_SLOWDOWN)
726 #define SET_GLOBAL_EXACT() \
727 (CONTROL |= DR_GLOBAL_SLOWDOWN)
729 #define RESET_LOCAL_EXACT() \
730 (CONTROL &= ~(DR_LOCAL_SLOWDOWN))
732 #define RESET_GLOBAL_EXACT() \
733 (CONTROL &= ~(DR_GLOBAL_SLOWDOWN))
735 #define SET_BREAK(index,address) \
737 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index)));\
738 D_REGS[index] = address;\
739 dr_ref_count[index]++;\
742 #define SET_WATCH(index,address,rw,len) \
744 SET_BREAK(index,address);\
745 CONTROL |= ((len)|(rw)) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index));\
748 #define IS_WATCH(index) \
749 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE*(index))))
751 #define WATCH_HIT(index) ((STATUS & (1 << (index))) && IS_WATCH(index))
753 #define DR_DEF(index) \
754 ((CONTROL >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index))) & 0x0f)
757 #if 0 /* use debugging macro */
758 #define SHOW_DR(text,len) \
760 if (!getenv ("GDB_SHOW_DR")) break; \
761 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
762 fprintf(stderr,"%08x %d %08x %d ", \
763 edi.dr[0],dr_ref_count[0],edi.dr[1],dr_ref_count[1]); \
764 fprintf(stderr,"%08x %d %08x %d ", \
765 edi.dr[2],dr_ref_count[2],edi.dr[3],dr_ref_count[3]); \
766 fprintf(stderr,(len)?"(%s:%d)\n":"(%s)\n",#text,len); \
769 #define SHOW_DR(text,len) do {} while (0)
779 for (i = 0; i < 4; i++)
786 /* Insert a watchpoint. */
789 go32_insert_watchpoint (int pid ATTRIBUTE_UNUSED, CORE_ADDR addr,
792 int ret = go32_insert_aligned_watchpoint (addr, addr, len, rw);
794 SHOW_DR (insert_watch, len);
799 go32_insert_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
803 int read_write_bits, len_bits;
805 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
806 However, x86 doesn't support read-only data breakpoints. */
807 read_write_bits = rw ? DR_RW_READWRITE : DR_RW_WRITE;
821 /* The debug registers only have 2 bits for the length, so
822 so this value will always fail the loop below. */
826 /* Look for an occupied debug register with the same address and the
827 same RW and LEN definitions. If we find one, we can use it for
828 this watchpoint as well (and save a register). */
829 for (i = 0; i < 4; i++)
831 if (!IS_REG_FREE (i) && D_REGS[i] == addr
832 && DR_DEF (i) == (unsigned)(len_bits | read_write_bits))
839 /* Look for a free debug register. */
840 for (i = 0; i <= 3; i++)
846 /* No more debug registers! */
853 return go32_handle_nonaligned_watchpoint (wp_insert, waddr, addr,
859 return go32_handle_nonaligned_watchpoint (wp_insert, waddr, addr,
863 return go32_handle_nonaligned_watchpoint (wp_insert, waddr, addr, len, rw);
865 SET_WATCH (i, addr, read_write_bits, len_bits);
866 LOCAL_ENABLE_REG (i);
873 go32_handle_nonaligned_watchpoint (wp_op what, CORE_ADDR waddr, CORE_ADDR addr,
878 int rv = 0, status = 0;
880 static int size_try_array[4][4] =
882 { 1, 1, 1, 1 }, /* trying size one */
883 { 2, 1, 2, 1 }, /* trying size two */
884 { 2, 1, 2, 1 }, /* trying size three */
885 { 4, 1, 2, 1 } /* trying size four */
891 /* Four is the maximum length a 386 debug register can watch. */
892 size = size_try_array[len > 4 ? 3 : len - 1][align];
893 if (what == wp_insert)
894 status = go32_insert_aligned_watchpoint (waddr, addr, size, rw);
895 else if (what == wp_remove)
896 status = go32_remove_aligned_watchpoint (waddr, addr, size, rw);
897 else if (what == wp_count)
901 /* We keep the loop going even after a failure, because some of
902 the other aligned watchpoints might still succeed, e.g. if
903 they watch addresses that are already watched, and thus just
904 increment the reference counts of occupied debug registers.
905 If we break out of the loop too early, we could cause those
906 addresses watched by other watchpoints to be disabled when
907 GDB reacts to our failure to insert this watchpoint and tries
917 /* Remove a watchpoint. */
920 go32_remove_watchpoint (int pid ATTRIBUTE_UNUSED, CORE_ADDR addr,
923 int ret = go32_remove_aligned_watchpoint (addr, addr, len, rw);
925 SHOW_DR (remove_watch, len);
930 go32_remove_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
934 int read_write_bits, len_bits;
936 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
937 However, x86 doesn't support read-only data breakpoints. */
938 read_write_bits = rw ? DR_RW_READWRITE : DR_RW_WRITE;
952 /* The debug registers only have 2 bits for the length, so
953 so this value will always fail the loop below. */
960 return go32_handle_nonaligned_watchpoint (wp_remove, waddr, addr,
966 return go32_handle_nonaligned_watchpoint (wp_remove, waddr, addr,
970 return go32_handle_nonaligned_watchpoint (wp_remove, waddr, addr, len, rw);
972 for (i = 0; i <= 3; i++)
974 if (!IS_REG_FREE (i) && D_REGS[i] == addr
975 && DR_DEF (i) == (unsigned)(len_bits | read_write_bits))
978 if (dr_ref_count[i] == 0)
982 RESET_LOCAL_EXACT ();
983 RESET_GLOBAL_EXACT ();
988 /* Can we use debug registers to watch a region whose address is ADDR
989 and whose length is LEN bytes? */
992 go32_region_ok_for_watchpoint (CORE_ADDR addr, int len)
994 /* Compute how many aligned watchpoints we would need to cover this
996 int nregs = go32_handle_nonaligned_watchpoint (wp_count, addr, addr, len, 0);
998 return nregs <= 4 ? 1 : 0;
1001 /* Check if stopped by a data watchpoint. If so, return the address
1002 whose access triggered the watchpoint. */
1005 go32_stopped_by_watchpoint (int pid ATTRIBUTE_UNUSED, int data_watchpoint)
1010 status = edi.dr[DR_STATUS];
1011 SHOW_DR (stopped_by, 0);
1012 for (i = 0; i <= 3; i++)
1014 if (WATCH_HIT (i) && data_watchpoint)
1016 SHOW_DR (WP_HIT, 0);
1024 /* Remove a breakpoint. */
1027 go32_remove_hw_breakpoint (CORE_ADDR addr, void *shadow ATTRIBUTE_UNUSED)
1030 for (i = 0; i <= 3; i++)
1032 if (!IS_REG_FREE (i) && D_REGS[i] == addr && DR_DEF (i) == 0)
1035 if (dr_ref_count[i] == 0)
1039 SHOW_DR (remove_hw, 0);
1044 go32_insert_hw_breakpoint (CORE_ADDR addr, void *shadow ATTRIBUTE_UNUSED)
1048 /* Look for an occupied debug register with the same address and the
1049 same RW and LEN definitions. If we find one, we can use it for
1050 this breakpoint as well (and save a register). */
1051 for (i = 0; i < 4; i++)
1053 if (!IS_REG_FREE (i) && D_REGS[i] == addr && DR_DEF (i) == 0)
1056 SHOW_DR (insert_hw, 0);
1061 /* Look for a free debug register. */
1062 for (i = 0; i <= 3; i++)
1064 if (IS_REG_FREE (i))
1068 /* No more debug registers? */
1071 SET_BREAK (i, addr);
1072 LOCAL_ENABLE_REG (i);
1074 SHOW_DR (insert_hw, 0);
1076 return i < 4 ? 0 : EBUSY;
1079 /* Put the device open on handle FD into either raw or cooked
1080 mode, return 1 if it was in raw mode, zero otherwise. */
1083 device_mode (int fd, int raw_p)
1085 int oldmode, newmode;
1090 __dpmi_int (0x21, ®s);
1091 if (regs.x.flags & 1)
1093 newmode = oldmode = regs.x.dx;
1100 if (oldmode & 0x80) /* Only for character dev */
1104 regs.x.dx = newmode & 0xff; /* Force upper byte zero, else it fails */
1105 __dpmi_int (0x21, ®s);
1106 if (regs.x.flags & 1)
1109 return (oldmode & 0x20) == 0x20;
1113 static int inf_mode_valid = 0;
1114 static int inf_terminal_mode;
1116 /* This semaphore is needed because, amazingly enough, GDB calls
1117 target.to_terminal_ours more than once after the inferior stops.
1118 But we need the information from the first call only, since the
1119 second call will always see GDB's own cooked terminal. */
1120 static int terminal_is_ours = 1;
1123 go32_terminal_init (void)
1125 inf_mode_valid = 0; /* reinitialize, in case they are restarting child */
1126 terminal_is_ours = 1;
1130 go32_terminal_info (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
1132 printf_unfiltered ("Inferior's terminal is in %s mode.\n",
1134 ? "default" : inf_terminal_mode ? "raw" : "cooked");
1136 #if __DJGPP_MINOR__ > 2
1137 if (child_cmd.redirection)
1141 for (i = 0; i < DBG_HANDLES; i++)
1143 if (child_cmd.redirection[i]->file_name)
1144 printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
1145 i, child_cmd.redirection[i]->file_name);
1146 else if (_get_dev_info (child_cmd.redirection[i]->inf_handle) == -1)
1148 ("\tFile handle %d appears to be closed by inferior.\n", i);
1149 /* Mask off the raw/cooked bit when comparing device info words. */
1150 else if ((_get_dev_info (child_cmd.redirection[i]->inf_handle) & 0xdf)
1151 != (_get_dev_info (i) & 0xdf))
1153 ("\tFile handle %d appears to be redirected by inferior.\n", i);
1160 go32_terminal_inferior (void)
1162 /* Redirect standard handles as child wants them. */
1164 if (redir_to_child (&child_cmd) == -1)
1166 redir_to_debugger (&child_cmd);
1167 error ("Cannot redirect standard handles for program: %s.",
1170 /* set the console device of the inferior to whatever mode
1171 (raw or cooked) we found it last time */
1172 if (terminal_is_ours)
1175 device_mode (0, inf_terminal_mode);
1176 terminal_is_ours = 0;
1181 go32_terminal_ours (void)
1183 /* Switch to cooked mode on the gdb terminal and save the inferior
1184 terminal mode to be restored when it is resumed */
1185 if (!terminal_is_ours)
1187 inf_terminal_mode = device_mode (0, 0);
1188 if (inf_terminal_mode != -1)
1191 /* If device_mode returned -1, we don't know what happens with
1192 handle 0 anymore, so make the info invalid. */
1194 terminal_is_ours = 1;
1196 /* Restore debugger's standard handles. */
1198 if (redir_to_debugger (&child_cmd) == -1)
1200 redir_to_child (&child_cmd);
1201 error ("Cannot redirect standard handles for debugger: %s.",
1208 init_go32_ops (void)
1210 go32_ops.to_shortname = "djgpp";
1211 go32_ops.to_longname = "djgpp target process";
1213 "Program loaded by djgpp, when gdb is used as an external debugger";
1214 go32_ops.to_open = go32_open;
1215 go32_ops.to_close = go32_close;
1216 go32_ops.to_attach = go32_attach;
1217 go32_ops.to_detach = go32_detach;
1218 go32_ops.to_resume = go32_resume;
1219 go32_ops.to_wait = go32_wait;
1220 go32_ops.to_fetch_registers = go32_fetch_registers;
1221 go32_ops.to_store_registers = go32_store_registers;
1222 go32_ops.to_prepare_to_store = go32_prepare_to_store;
1223 go32_ops.to_xfer_memory = go32_xfer_memory;
1224 go32_ops.to_files_info = go32_files_info;
1225 go32_ops.to_insert_breakpoint = memory_insert_breakpoint;
1226 go32_ops.to_remove_breakpoint = memory_remove_breakpoint;
1227 go32_ops.to_terminal_init = go32_terminal_init;
1228 go32_ops.to_terminal_inferior = go32_terminal_inferior;
1229 go32_ops.to_terminal_ours_for_output = go32_terminal_ours;
1230 go32_ops.to_terminal_ours = go32_terminal_ours;
1231 go32_ops.to_terminal_info = go32_terminal_info;
1232 go32_ops.to_kill = go32_kill_inferior;
1233 go32_ops.to_create_inferior = go32_create_inferior;
1234 go32_ops.to_mourn_inferior = go32_mourn_inferior;
1235 go32_ops.to_can_run = go32_can_run;
1236 go32_ops.to_stop = go32_stop;
1237 go32_ops.to_stratum = process_stratum;
1238 go32_ops.to_has_all_memory = 1;
1239 go32_ops.to_has_memory = 1;
1240 go32_ops.to_has_stack = 1;
1241 go32_ops.to_has_registers = 1;
1242 go32_ops.to_has_execution = 1;
1243 go32_ops.to_magic = OPS_MAGIC;
1245 /* Initialize child's cwd as empty to be initialized when starting
1249 /* Initialize child's command line storage. */
1250 if (redir_debug_init (&child_cmd) == -1)
1251 internal_error (__FILE__, __LINE__,
1252 "Cannot allocate redirection storage: not enough memory.\n");
1254 /* We are always processing GCC-compiled programs. */
1255 processing_gcc_compilation = 2;
1259 _initialize_go32_nat (void)
1262 add_target (&go32_ops);
1275 tcsetpgrp (int fd, pid_t pgid)
1277 if (isatty (fd) && pgid == SOME_PID)
1279 errno = pgid == SOME_PID ? ENOTTY : ENOSYS;