1 /* Native debugging support for Intel x86 running DJGPP.
2 Copyright 1997, 1999 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
);
494 Invalid native size for register no. %d in go32_fetch_register.", regno
);
496 supply_register (regno
, (char *) ®val
);
499 internal_error ("Invalid register no. %d in go32_fetch_register.",
505 store_register (int regno
)
508 void *v
= (void *) ®isters
[REGISTER_BYTE (regno
)];
511 rp
= (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
;
513 rp
= (char *) &npx
.reg
[regno_mapping
[regno
].tss_ofs
];
515 rp
= (char *) &npx
+ regno_mapping
[regno
].tss_ofs
;
517 internal_error ("Invalid register no. %d in store_register.", regno
);
518 memcpy (rp
, v
, regno_mapping
[regno
].size
);
519 if (regno
== FOP_REGNUM
)
520 *(short *)rp
&= 0x07ff; /* strip high 5 bits, in case they added them */
524 go32_store_registers (int regno
)
529 store_register (regno
);
532 for (r
= 0; r
< sizeof (regno_mapping
) / sizeof (regno_mapping
[0]); r
++)
538 go32_prepare_to_store (void)
543 go32_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
544 struct mem_attrib
*attrib ATTRIBUTE_UNUSED
,
545 struct target_ops
*target ATTRIBUTE_UNUSED
)
549 if (write_child (memaddr
, myaddr
, len
))
560 if (read_child (memaddr
, myaddr
, len
))
571 static cmdline_t child_cmd
; /* parsed child's command line kept here */
574 go32_files_info (struct target_ops
*target ATTRIBUTE_UNUSED
)
576 printf_unfiltered ("You are running a DJGPP V2 program.\n");
585 prog_has_started
= 0;
589 go32_kill_inferior (void)
591 redir_cmdline_delete (&child_cmd
);
594 unpush_target (&go32_ops
);
598 go32_create_inferior (char *exec_file
, char *args
, char **env
)
602 char **env_save
= environ
;
604 /* If no exec file handed to us, get it from the exec-file command -- with
605 a good, common error message if none is specified. */
607 exec_file
= get_exec_file (1);
609 if (prog_has_started
)
612 go32_kill_inferior ();
617 /* Initialize child's cwd as empty to be initialized when starting
621 /* Init command line storage. */
622 if (redir_debug_init (&child_cmd
) == -1)
623 internal_error ("Cannot allocate redirection storage: not enough memory.\n");
625 /* Parse the command line and create redirections. */
626 if (strpbrk (args
, "<>"))
628 if (redir_cmdline_parse (args
, &child_cmd
) == 0)
629 args
= child_cmd
.command
;
631 error ("Syntax error in command line.");
634 child_cmd
.command
= xstrdup (args
);
636 cmdline
= (char *) alloca (strlen (args
) + 4);
637 cmdline
[0] = strlen (args
);
638 strcpy (cmdline
+ 1, args
);
639 cmdline
[strlen (args
) + 1] = 13;
643 if (v2loadimage (exec_file
, cmdline
, start_state
))
646 printf_unfiltered ("Load failed for image %s\n", exec_file
);
651 edi_init (start_state
);
652 #if __DJGPP_MINOR__ < 3
656 inferior_pid
= SOME_PID
;
657 push_target (&go32_ops
);
658 clear_proceed_status ();
659 insert_breakpoints ();
660 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
661 prog_has_started
= 1;
665 go32_mourn_inferior (void)
667 /* We need to make sure all the breakpoint enable bits in the DR7
668 register are reset when the inferior exits. Otherwise, if they
669 rerun the inferior, the uncleared bits may cause random SIGTRAPs,
670 failure to set more watchpoints, and other calamities. It would
671 be nice if GDB itself would take care to remove all breakpoints
672 at all times, but it doesn't, probably under an assumption that
673 the OS cleans up when the debuggee exits. */
675 go32_kill_inferior ();
676 generic_mourn_inferior ();
685 /* Hardware watchpoint support. */
689 #define DR_ENABLE_SIZE 2
690 #define DR_LOCAL_ENABLE_SHIFT 0
691 #define DR_GLOBAL_ENABLE_SHIFT 1
692 #define DR_LOCAL_SLOWDOWN 0x100
693 #define DR_GLOBAL_SLOWDOWN 0x200
694 #define DR_CONTROL_SHIFT 16
695 #define DR_CONTROL_SIZE 4
696 #define DR_RW_READWRITE 0x3
697 #define DR_RW_WRITE 0x1
698 #define DR_CONTROL_MASK 0xf
699 #define DR_ENABLE_MASK 0x3
704 #define D_REGS edi.dr
705 #define CONTROL D_REGS[DR_CONTROL]
706 #define STATUS D_REGS[DR_STATUS]
708 #define IS_REG_FREE(index) \
709 (!(CONTROL & (3 << (DR_ENABLE_SIZE * (index)))))
711 #define LOCAL_ENABLE_REG(index) \
712 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
714 #define GLOBAL_ENABLE_REG(index) \
715 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
717 #define DISABLE_REG(index) \
718 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * (index))))
720 #define SET_LOCAL_EXACT() \
721 (CONTROL |= DR_LOCAL_SLOWDOWN)
723 #define SET_GLOBAL_EXACT() \
724 (CONTROL |= DR_GLOBAL_SLOWDOWN)
726 #define RESET_LOCAL_EXACT() \
727 (CONTROL &= ~(DR_LOCAL_SLOWDOWN))
729 #define RESET_GLOBAL_EXACT() \
730 (CONTROL &= ~(DR_GLOBAL_SLOWDOWN))
732 #define SET_BREAK(index,address) \
734 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index)));\
735 D_REGS[index] = address;\
736 dr_ref_count[index]++;\
739 #define SET_WATCH(index,address,rw,len) \
741 SET_BREAK(index,address);\
742 CONTROL |= ((len)|(rw)) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index));\
745 #define IS_WATCH(index) \
746 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE*(index))))
748 #define WATCH_HIT(index) ((STATUS & (1 << (index))) && IS_WATCH(index))
750 #define DR_DEF(index) \
751 ((CONTROL >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index))) & 0x0f)
754 #if 0 /* use debugging macro */
755 #define SHOW_DR(text,len) \
757 if (!getenv ("GDB_SHOW_DR")) break; \
758 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
759 fprintf(stderr,"%08x %d %08x %d ", \
760 edi.dr[0],dr_ref_count[0],edi.dr[1],dr_ref_count[1]); \
761 fprintf(stderr,"%08x %d %08x %d ", \
762 edi.dr[2],dr_ref_count[2],edi.dr[3],dr_ref_count[3]); \
763 fprintf(stderr,(len)?"(%s:%d)\n":"(%s)\n",#text,len); \
766 #define SHOW_DR(text,len) do {} while (0)
776 for (i
= 0; i
< 4; i
++)
783 /* Insert a watchpoint. */
786 go32_insert_watchpoint (int pid ATTRIBUTE_UNUSED
, CORE_ADDR addr
,
789 int ret
= go32_insert_aligned_watchpoint (addr
, addr
, len
, rw
);
791 SHOW_DR (insert_watch
, len
);
796 go32_insert_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
800 int read_write_bits
, len_bits
;
802 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
803 However, x86 doesn't support read-only data breakpoints. */
804 read_write_bits
= rw
? DR_RW_READWRITE
: DR_RW_WRITE
;
818 /* The debug registers only have 2 bits for the length, so
819 so this value will always fail the loop below. */
823 /* Look for an occupied debug register with the same address and the
824 same RW and LEN definitions. If we find one, we can use it for
825 this watchpoint as well (and save a register). */
826 for (i
= 0; i
< 4; i
++)
828 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
829 && DR_DEF (i
) == (unsigned)(len_bits
| read_write_bits
))
836 /* Look for a free debug register. */
837 for (i
= 0; i
<= 3; i
++)
843 /* No more debug registers! */
850 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
,
856 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
,
860 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
, len
, rw
);
862 SET_WATCH (i
, addr
, read_write_bits
, len_bits
);
863 LOCAL_ENABLE_REG (i
);
870 go32_handle_nonaligned_watchpoint (wp_op what
, CORE_ADDR waddr
, CORE_ADDR addr
,
875 int rv
= 0, status
= 0;
877 static int size_try_array
[4][4] =
879 { 1, 1, 1, 1 }, /* trying size one */
880 { 2, 1, 2, 1 }, /* trying size two */
881 { 2, 1, 2, 1 }, /* trying size three */
882 { 4, 1, 2, 1 } /* trying size four */
888 /* Four is the maximum length a 386 debug register can watch. */
889 size
= size_try_array
[len
> 4 ? 3 : len
- 1][align
];
890 if (what
== wp_insert
)
891 status
= go32_insert_aligned_watchpoint (waddr
, addr
, size
, rw
);
892 else if (what
== wp_remove
)
893 status
= go32_remove_aligned_watchpoint (waddr
, addr
, size
, rw
);
894 else if (what
== wp_count
)
898 /* We keep the loop going even after a failure, because some of
899 the other aligned watchpoints might still succeed, e.g. if
900 they watch addresses that are already watched, and thus just
901 increment the reference counts of occupied debug registers.
902 If we break out of the loop too early, we could cause those
903 addresses watched by other watchpoints to be disabled when
904 GDB reacts to our failure to insert this watchpoint and tries
914 /* Remove a watchpoint. */
917 go32_remove_watchpoint (int pid ATTRIBUTE_UNUSED
, CORE_ADDR addr
,
920 int ret
= go32_remove_aligned_watchpoint (addr
, addr
, len
, rw
);
922 SHOW_DR (remove_watch
, len
);
927 go32_remove_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
931 int read_write_bits
, len_bits
;
933 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
934 However, x86 doesn't support read-only data breakpoints. */
935 read_write_bits
= rw
? DR_RW_READWRITE
: DR_RW_WRITE
;
949 /* The debug registers only have 2 bits for the length, so
950 so this value will always fail the loop below. */
957 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
,
963 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
,
967 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
, len
, rw
);
969 for (i
= 0; i
<= 3; i
++)
971 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
972 && DR_DEF (i
) == (unsigned)(len_bits
| read_write_bits
))
975 if (dr_ref_count
[i
] == 0)
979 RESET_LOCAL_EXACT ();
980 RESET_GLOBAL_EXACT ();
985 /* Can we use debug registers to watch a region whose address is ADDR
986 and whose length is LEN bytes? */
989 go32_region_ok_for_watchpoint (CORE_ADDR addr
, int len
)
991 /* Compute how many aligned watchpoints we would need to cover this
993 int nregs
= go32_handle_nonaligned_watchpoint (wp_count
, addr
, addr
, len
, 0);
995 return nregs
<= 4 ? 1 : 0;
998 /* Check if stopped by a data watchpoint. If so, return the address
999 whose access triggered the watchpoint. */
1002 go32_stopped_by_watchpoint (int pid ATTRIBUTE_UNUSED
, int data_watchpoint
)
1007 status
= edi
.dr
[DR_STATUS
];
1008 SHOW_DR (stopped_by
, 0);
1009 for (i
= 0; i
<= 3; i
++)
1011 if (WATCH_HIT (i
) && data_watchpoint
)
1013 SHOW_DR (WP_HIT
, 0);
1021 /* Remove a breakpoint. */
1024 go32_remove_hw_breakpoint (CORE_ADDR addr
, void *shadow ATTRIBUTE_UNUSED
)
1027 for (i
= 0; i
<= 3; i
++)
1029 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
&& DR_DEF (i
) == 0)
1032 if (dr_ref_count
[i
] == 0)
1036 SHOW_DR (remove_hw
, 0);
1041 go32_insert_hw_breakpoint (CORE_ADDR addr
, void *shadow ATTRIBUTE_UNUSED
)
1045 /* Look for an occupied debug register with the same address and the
1046 same RW and LEN definitions. If we find one, we can use it for
1047 this breakpoint as well (and save a register). */
1048 for (i
= 0; i
< 4; i
++)
1050 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
&& DR_DEF (i
) == 0)
1053 SHOW_DR (insert_hw
, 0);
1058 /* Look for a free debug register. */
1059 for (i
= 0; i
<= 3; i
++)
1061 if (IS_REG_FREE (i
))
1065 /* No more debug registers? */
1068 SET_BREAK (i
, addr
);
1069 LOCAL_ENABLE_REG (i
);
1071 SHOW_DR (insert_hw
, 0);
1073 return i
< 4 ? 0 : EBUSY
;
1076 /* Put the device open on handle FD into either raw or cooked
1077 mode, return 1 if it was in raw mode, zero otherwise. */
1080 device_mode (int fd
, int raw_p
)
1082 int oldmode
, newmode
;
1087 __dpmi_int (0x21, ®s
);
1088 if (regs
.x
.flags
& 1)
1090 newmode
= oldmode
= regs
.x
.dx
;
1097 if (oldmode
& 0x80) /* Only for character dev */
1101 regs
.x
.dx
= newmode
& 0xff; /* Force upper byte zero, else it fails */
1102 __dpmi_int (0x21, ®s
);
1103 if (regs
.x
.flags
& 1)
1106 return (oldmode
& 0x20) == 0x20;
1110 static int inf_mode_valid
= 0;
1111 static int inf_terminal_mode
;
1113 /* This semaphore is needed because, amazingly enough, GDB calls
1114 target.to_terminal_ours more than once after the inferior stops.
1115 But we need the information from the first call only, since the
1116 second call will always see GDB's own cooked terminal. */
1117 static int terminal_is_ours
= 1;
1120 go32_terminal_init (void)
1122 inf_mode_valid
= 0; /* reinitialize, in case they are restarting child */
1123 terminal_is_ours
= 1;
1127 go32_terminal_info (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
1129 printf_unfiltered ("Inferior's terminal is in %s mode.\n",
1131 ? "default" : inf_terminal_mode
? "raw" : "cooked");
1133 #if __DJGPP_MINOR__ > 2
1134 if (child_cmd
.redirection
)
1138 for (i
= 0; i
< DBG_HANDLES
; i
++)
1140 if (child_cmd
.redirection
[i
]->file_name
)
1141 printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
1142 i
, child_cmd
.redirection
[i
]->file_name
);
1143 else if (_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) == -1)
1145 ("\tFile handle %d appears to be closed by inferior.\n", i
);
1146 /* Mask off the raw/cooked bit when comparing device info words. */
1147 else if ((_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) & 0xdf)
1148 != (_get_dev_info (i
) & 0xdf))
1150 ("\tFile handle %d appears to be redirected by inferior.\n", i
);
1157 go32_terminal_inferior (void)
1159 /* Redirect standard handles as child wants them. */
1161 if (redir_to_child (&child_cmd
) == -1)
1163 redir_to_debugger (&child_cmd
);
1164 error ("Cannot redirect standard handles for program: %s.",
1167 /* set the console device of the inferior to whatever mode
1168 (raw or cooked) we found it last time */
1169 if (terminal_is_ours
)
1172 device_mode (0, inf_terminal_mode
);
1173 terminal_is_ours
= 0;
1178 go32_terminal_ours (void)
1180 /* Switch to cooked mode on the gdb terminal and save the inferior
1181 terminal mode to be restored when it is resumed */
1182 if (!terminal_is_ours
)
1184 inf_terminal_mode
= device_mode (0, 0);
1185 if (inf_terminal_mode
!= -1)
1188 /* If device_mode returned -1, we don't know what happens with
1189 handle 0 anymore, so make the info invalid. */
1191 terminal_is_ours
= 1;
1193 /* Restore debugger's standard handles. */
1195 if (redir_to_debugger (&child_cmd
) == -1)
1197 redir_to_child (&child_cmd
);
1198 error ("Cannot redirect standard handles for debugger: %s.",
1205 init_go32_ops (void)
1207 go32_ops
.to_shortname
= "djgpp";
1208 go32_ops
.to_longname
= "djgpp target process";
1210 "Program loaded by djgpp, when gdb is used as an external debugger";
1211 go32_ops
.to_open
= go32_open
;
1212 go32_ops
.to_close
= go32_close
;
1213 go32_ops
.to_attach
= go32_attach
;
1214 go32_ops
.to_detach
= go32_detach
;
1215 go32_ops
.to_resume
= go32_resume
;
1216 go32_ops
.to_wait
= go32_wait
;
1217 go32_ops
.to_fetch_registers
= go32_fetch_registers
;
1218 go32_ops
.to_store_registers
= go32_store_registers
;
1219 go32_ops
.to_prepare_to_store
= go32_prepare_to_store
;
1220 go32_ops
.to_xfer_memory
= go32_xfer_memory
;
1221 go32_ops
.to_files_info
= go32_files_info
;
1222 go32_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
1223 go32_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
1224 go32_ops
.to_terminal_init
= go32_terminal_init
;
1225 go32_ops
.to_terminal_inferior
= go32_terminal_inferior
;
1226 go32_ops
.to_terminal_ours_for_output
= go32_terminal_ours
;
1227 go32_ops
.to_terminal_ours
= go32_terminal_ours
;
1228 go32_ops
.to_terminal_info
= go32_terminal_info
;
1229 go32_ops
.to_kill
= go32_kill_inferior
;
1230 go32_ops
.to_create_inferior
= go32_create_inferior
;
1231 go32_ops
.to_mourn_inferior
= go32_mourn_inferior
;
1232 go32_ops
.to_can_run
= go32_can_run
;
1233 go32_ops
.to_stop
= go32_stop
;
1234 go32_ops
.to_stratum
= process_stratum
;
1235 go32_ops
.to_has_all_memory
= 1;
1236 go32_ops
.to_has_memory
= 1;
1237 go32_ops
.to_has_stack
= 1;
1238 go32_ops
.to_has_registers
= 1;
1239 go32_ops
.to_has_execution
= 1;
1240 go32_ops
.to_magic
= OPS_MAGIC
;
1242 /* Initialize child's cwd as empty to be initialized when starting
1246 /* Initialize child's command line storage. */
1247 if (redir_debug_init (&child_cmd
) == -1)
1248 internal_error ("Cannot allocate redirection storage: not enough memory.\n");
1250 /* We are always processing GCC-compiled programs. */
1251 processing_gcc_compilation
= 2;
1255 _initialize_go32_nat (void)
1258 add_target (&go32_ops
);
1271 tcsetpgrp (int fd
, pid_t pgid
)
1273 if (isatty (fd
) && pgid
== SOME_PID
)
1275 errno
= pgid
== SOME_PID
? ENOTTY
: ENOSYS
;