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"
33 #include <stdio.h> /* required for __DJGPP_MINOR__ */
40 #include <debug/v2load.h>
41 #include <debug/dbgcom.h>
42 #if __DJGPP_MINOR__ > 2
43 #include <debug/redir.h>
46 #if __DJGPP_MINOR__ < 3
47 /* This code will be provided from DJGPP 2.03 on. Until then I code it
55 unsigned short exponent
:15;
56 unsigned short sign
:1;
75 static void save_npx (void); /* Save the FPU of the debugged program */
76 static void load_npx (void); /* Restore the FPU of the debugged program */
78 /* ------------------------------------------------------------------------- */
79 /* Store the contents of the NPX in the global variable `npx'. */
107 /* ------------------------------------------------------------------------- */
108 /* Reload the contents of the NPX from the global variable `npx'. */
113 asm ("frstor %0":"=m" (npx
));
115 /* ------------------------------------------------------------------------- */
116 /* Stubs for the missing redirection functions. */
122 void redir_cmdline_delete (cmdline_t
*ptr
) {ptr
->redirected
= 0;}
123 int redir_cmdline_parse (const char *args
, cmdline_t
*ptr
)
127 int redir_to_child (cmdline_t
*ptr
)
131 int redir_to_debugger (cmdline_t
*ptr
)
135 int redir_debug_init (cmdline_t
*ptr
) { return 0; }
136 #endif /* __DJGPP_MINOR < 3 */
138 extern void _initialize_go32_nat (void);
140 typedef enum { wp_insert
, wp_remove
, wp_count
} wp_op
;
142 /* This holds the current reference counts for each debug register. */
143 static int dr_ref_count
[4];
145 extern char **environ
;
149 static int prog_has_started
= 0;
150 static void go32_open (char *name
, int from_tty
);
151 static void go32_close (int quitting
);
152 static void go32_attach (char *args
, int from_tty
);
153 static void go32_detach (char *args
, int from_tty
);
154 static void go32_resume (int pid
, int step
, enum target_signal siggnal
);
155 static int go32_wait (int pid
, struct target_waitstatus
*status
);
156 static void go32_fetch_registers (int regno
);
157 static void store_register (int regno
);
158 static void go32_store_registers (int regno
);
159 static void go32_prepare_to_store (void);
160 static int go32_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
,
162 struct mem_attrib
*attrib
,
163 struct target_ops
*target
);
164 static void go32_files_info (struct target_ops
*target
);
165 static void go32_stop (void);
166 static void go32_kill_inferior (void);
167 static void go32_create_inferior (char *exec_file
, char *args
, char **env
);
168 static void cleanup_dregs (void);
169 static void go32_mourn_inferior (void);
170 static int go32_can_run (void);
171 static int go32_insert_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
173 static int go32_remove_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
175 static int go32_handle_nonaligned_watchpoint (wp_op what
, CORE_ADDR waddr
,
176 CORE_ADDR addr
, int len
, int rw
);
178 static struct target_ops go32_ops
;
179 static void go32_terminal_init (void);
180 static void go32_terminal_inferior (void);
181 static void go32_terminal_ours (void);
183 #define r_ofs(x) (offsetof(TSS,x))
192 {r_ofs (tss_eax
), 4}, /* normal registers, from a_tss */
193 {r_ofs (tss_ecx
), 4},
194 {r_ofs (tss_edx
), 4},
195 {r_ofs (tss_ebx
), 4},
196 {r_ofs (tss_esp
), 4},
197 {r_ofs (tss_ebp
), 4},
198 {r_ofs (tss_esi
), 4},
199 {r_ofs (tss_edi
), 4},
200 {r_ofs (tss_eip
), 4},
201 {r_ofs (tss_eflags
), 4},
208 {0, 10}, /* 8 FP registers, from npx.reg[] */
216 /* The order of the next 7 registers must be consistent
217 with their numbering in config/i386/tm-i386.h, which see. */
218 {0, 2}, /* control word, from npx */
219 {4, 2}, /* status word, from npx */
220 {8, 2}, /* tag word, from npx */
221 {16, 2}, /* last FP exception CS from npx */
222 {12, 4}, /* last FP exception EIP from npx */
223 {24, 2}, /* last FP exception operand selector from npx */
224 {20, 4}, /* last FP exception operand offset from npx */
225 {18, 2} /* last FP opcode from npx */
231 enum target_signal gdb_sig
;
235 {0, TARGET_SIGNAL_FPE
},
236 {1, TARGET_SIGNAL_TRAP
},
237 /* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
238 but I think SIGBUS is better, since the NMI is usually activated
239 as a result of a memory parity check failure. */
240 {2, TARGET_SIGNAL_BUS
},
241 {3, TARGET_SIGNAL_TRAP
},
242 {4, TARGET_SIGNAL_FPE
},
243 {5, TARGET_SIGNAL_SEGV
},
244 {6, TARGET_SIGNAL_ILL
},
245 {7, TARGET_SIGNAL_EMT
}, /* no-coprocessor exception */
246 {8, TARGET_SIGNAL_SEGV
},
247 {9, TARGET_SIGNAL_SEGV
},
248 {10, TARGET_SIGNAL_BUS
},
249 {11, TARGET_SIGNAL_SEGV
},
250 {12, TARGET_SIGNAL_SEGV
},
251 {13, TARGET_SIGNAL_SEGV
},
252 {14, TARGET_SIGNAL_SEGV
},
253 {16, TARGET_SIGNAL_FPE
},
254 {17, TARGET_SIGNAL_BUS
},
255 {31, TARGET_SIGNAL_ILL
},
256 {0x1b, TARGET_SIGNAL_INT
},
257 {0x75, TARGET_SIGNAL_FPE
},
258 {0x78, TARGET_SIGNAL_ALRM
},
259 {0x79, TARGET_SIGNAL_INT
},
260 {0x7a, TARGET_SIGNAL_QUIT
},
261 {-1, TARGET_SIGNAL_LAST
}
265 enum target_signal gdb_sig
;
268 {TARGET_SIGNAL_0
, -1},
269 {TARGET_SIGNAL_ILL
, 6}, /* Invalid Opcode */
270 {TARGET_SIGNAL_EMT
, 7}, /* triggers SIGNOFP */
271 {TARGET_SIGNAL_SEGV
, 13}, /* GPF */
272 {TARGET_SIGNAL_BUS
, 17}, /* Alignment Check */
273 /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
275 {TARGET_SIGNAL_TERM
, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
276 {TARGET_SIGNAL_FPE
, 0x75},
277 {TARGET_SIGNAL_INT
, 0x79},
278 {TARGET_SIGNAL_QUIT
, 0x7a},
279 {TARGET_SIGNAL_ALRM
, 0x78}, /* triggers SIGTIMR */
280 {TARGET_SIGNAL_PROF
, 0x78},
281 {TARGET_SIGNAL_LAST
, -1}
285 go32_open (char *name ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
287 printf_unfiltered ("Done. Use the \"run\" command to run the program.\n");
291 go32_close (int quitting ATTRIBUTE_UNUSED
)
296 go32_attach (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
299 You cannot attach to a running program on this platform.\n\
300 Use the `run' command to run DJGPP programs.");
304 go32_detach (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
308 static int resume_is_step
;
309 static int resume_signal
= -1;
312 go32_resume (int pid ATTRIBUTE_UNUSED
, int step
, enum target_signal siggnal
)
316 resume_is_step
= step
;
318 if (siggnal
!= TARGET_SIGNAL_0
&& siggnal
!= TARGET_SIGNAL_TRAP
)
320 for (i
= 0, resume_signal
= -1;
321 excepn_map
[i
].gdb_sig
!= TARGET_SIGNAL_LAST
; i
++)
322 if (excepn_map
[i
].gdb_sig
== siggnal
)
324 resume_signal
= excepn_map
[i
].djgpp_excepno
;
327 if (resume_signal
== -1)
328 printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
329 target_signal_to_name (siggnal
));
333 static char child_cwd
[FILENAME_MAX
];
336 go32_wait (int pid ATTRIBUTE_UNUSED
, struct target_waitstatus
*status
)
339 unsigned char saved_opcode
;
340 unsigned long INT3_addr
= 0;
341 int stepping_over_INT
= 0;
343 a_tss
.tss_eflags
&= 0xfeff; /* reset the single-step flag (TF) */
346 /* If the next instruction is INT xx or INTO, we need to handle
347 them specially. Intel manuals say that these instructions
348 reset the single-step flag (a.k.a. TF). However, it seems
349 that, at least in the DPMI environment, and at least when
350 stepping over the DPMI interrupt 31h, the problem is having
351 TF set at all when INT 31h is executed: the debuggee either
352 crashes (and takes the system with it) or is killed by a
355 So we need to emulate single-step mode: we put an INT3 opcode
356 right after the INT xx instruction, let the debuggee run
357 until it hits INT3 and stops, then restore the original
358 instruction which we overwrote with the INT3 opcode, and back
359 up the debuggee's EIP to that instruction. */
360 read_child (a_tss
.tss_eip
, &saved_opcode
, 1);
361 if (saved_opcode
== 0xCD || saved_opcode
== 0xCE)
363 unsigned char INT3_opcode
= 0xCC;
366 = saved_opcode
== 0xCD ? a_tss
.tss_eip
+ 2 : a_tss
.tss_eip
+ 1;
367 stepping_over_INT
= 1;
368 read_child (INT3_addr
, &saved_opcode
, 1);
369 write_child (INT3_addr
, &INT3_opcode
, 1);
372 a_tss
.tss_eflags
|= 0x0100; /* normal instruction: set TF */
375 /* The special value FFFFh in tss_trap indicates to run_child that
376 tss_irqn holds a signal to be delivered to the debuggee. */
377 if (resume_signal
<= -1)
380 a_tss
.tss_irqn
= 0xff;
384 a_tss
.tss_trap
= 0xffff; /* run_child looks for this */
385 a_tss
.tss_irqn
= resume_signal
;
388 /* The child might change working directory behind our back. The
389 GDB users won't like the side effects of that when they work with
390 relative file names, and GDB might be confused by its current
391 directory not being in sync with the truth. So we always make a
392 point of changing back to where GDB thinks is its cwd, when we
393 return control to the debugger, but restore child's cwd before we
395 /* Initialize child_cwd, before the first call to run_child and not
396 in the initialization, so the child get also the changed directory
397 set with the gdb-command "cd ..." */
399 /* Initialize child's cwd with the current one. */
400 getcwd (child_cwd
, sizeof (child_cwd
));
404 #if __DJGPP_MINOR__ < 3
408 #if __DJGPP_MINOR__ < 3
412 /* Did we step over an INT xx instruction? */
413 if (stepping_over_INT
&& a_tss
.tss_eip
== INT3_addr
+ 1)
415 /* Restore the original opcode. */
416 a_tss
.tss_eip
--; /* EIP points *after* the INT3 instruction */
417 write_child (a_tss
.tss_eip
, &saved_opcode
, 1);
418 /* Simulate a TRAP exception. */
420 a_tss
.tss_eflags
|= 0x0100;
423 getcwd (child_cwd
, sizeof (child_cwd
)); /* in case it has changed */
424 chdir (current_directory
);
426 if (a_tss
.tss_irqn
== 0x21)
428 status
->kind
= TARGET_WAITKIND_EXITED
;
429 status
->value
.integer
= a_tss
.tss_eax
& 0xff;
433 status
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
434 status
->kind
= TARGET_WAITKIND_STOPPED
;
435 for (i
= 0; sig_map
[i
].go32_sig
!= -1; i
++)
437 if (a_tss
.tss_irqn
== sig_map
[i
].go32_sig
)
439 #if __DJGPP_MINOR__ < 3
440 if ((status
->value
.sig
= sig_map
[i
].gdb_sig
) !=
442 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
444 status
->value
.sig
= sig_map
[i
].gdb_sig
;
454 fetch_register (int regno
)
456 if (regno
< FP0_REGNUM
)
457 supply_register (regno
, (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
);
458 else if (regno
<= LAST_FPU_CTRL_REGNUM
)
459 i387_supply_register (regno
, (char *) &npx
);
461 internal_error (__FILE__
, __LINE__
,
462 "Invalid register no. %d in fetch_register.", regno
);
466 go32_fetch_registers (int regno
)
469 fetch_register (regno
);
472 for (regno
= 0; regno
< FP0_REGNUM
; regno
++)
473 fetch_register (regno
);
474 i387_supply_fsave ((char *) &npx
);
479 store_register (int regno
)
482 void *v
= (void *) ®isters
[REGISTER_BYTE (regno
)];
484 if (regno
< FP0_REGNUM
)
485 memcpy ((char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
,
486 v
, regno_mapping
[regno
].size
);
487 else if (regno
<= LAST_FPU_CTRL_REGNUM
)
488 i387_fill_fsave ((char *)&npx
, regno
);
490 internal_error (__FILE__
, __LINE__
,
491 "Invalid register no. %d in store_register.", regno
);
495 go32_store_registers (int regno
)
500 store_register (regno
);
503 for (r
= 0; r
< FP0_REGNUM
; r
++)
505 i387_fill_fsave ((char *) &npx
, -1);
510 go32_prepare_to_store (void)
515 go32_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
516 struct mem_attrib
*attrib ATTRIBUTE_UNUSED
,
517 struct target_ops
*target ATTRIBUTE_UNUSED
)
521 if (write_child (memaddr
, myaddr
, len
))
532 if (read_child (memaddr
, myaddr
, len
))
543 static cmdline_t child_cmd
; /* parsed child's command line kept here */
546 go32_files_info (struct target_ops
*target ATTRIBUTE_UNUSED
)
548 printf_unfiltered ("You are running a DJGPP V2 program.\n");
557 prog_has_started
= 0;
561 go32_kill_inferior (void)
563 redir_cmdline_delete (&child_cmd
);
566 unpush_target (&go32_ops
);
570 go32_create_inferior (char *exec_file
, char *args
, char **env
)
574 char **env_save
= environ
;
576 /* If no exec file handed to us, get it from the exec-file command -- with
577 a good, common error message if none is specified. */
579 exec_file
= get_exec_file (1);
581 if (prog_has_started
)
584 go32_kill_inferior ();
589 /* Initialize child's cwd as empty to be initialized when starting
593 /* Init command line storage. */
594 if (redir_debug_init (&child_cmd
) == -1)
595 internal_error (__FILE__
, __LINE__
,
596 "Cannot allocate redirection storage: not enough memory.\n");
598 /* Parse the command line and create redirections. */
599 if (strpbrk (args
, "<>"))
601 if (redir_cmdline_parse (args
, &child_cmd
) == 0)
602 args
= child_cmd
.command
;
604 error ("Syntax error in command line.");
607 child_cmd
.command
= xstrdup (args
);
609 cmdline
= (char *) alloca (strlen (args
) + 4);
610 cmdline
[0] = strlen (args
);
611 strcpy (cmdline
+ 1, args
);
612 cmdline
[strlen (args
) + 1] = 13;
616 if (v2loadimage (exec_file
, cmdline
, start_state
))
619 printf_unfiltered ("Load failed for image %s\n", exec_file
);
624 edi_init (start_state
);
625 #if __DJGPP_MINOR__ < 3
629 inferior_pid
= SOME_PID
;
630 push_target (&go32_ops
);
631 clear_proceed_status ();
632 insert_breakpoints ();
633 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
634 prog_has_started
= 1;
638 go32_mourn_inferior (void)
640 /* We need to make sure all the breakpoint enable bits in the DR7
641 register are reset when the inferior exits. Otherwise, if they
642 rerun the inferior, the uncleared bits may cause random SIGTRAPs,
643 failure to set more watchpoints, and other calamities. It would
644 be nice if GDB itself would take care to remove all breakpoints
645 at all times, but it doesn't, probably under an assumption that
646 the OS cleans up when the debuggee exits. */
648 go32_kill_inferior ();
649 generic_mourn_inferior ();
658 /* Hardware watchpoint support. */
662 #define DR_ENABLE_SIZE 2
663 #define DR_LOCAL_ENABLE_SHIFT 0
664 #define DR_GLOBAL_ENABLE_SHIFT 1
665 #define DR_LOCAL_SLOWDOWN 0x100
666 #define DR_GLOBAL_SLOWDOWN 0x200
667 #define DR_CONTROL_SHIFT 16
668 #define DR_CONTROL_SIZE 4
669 #define DR_RW_READWRITE 0x3
670 #define DR_RW_WRITE 0x1
671 #define DR_CONTROL_MASK 0xf
672 #define DR_ENABLE_MASK 0x3
677 #define D_REGS edi.dr
678 #define CONTROL D_REGS[DR_CONTROL]
679 #define STATUS D_REGS[DR_STATUS]
681 #define IS_REG_FREE(index) \
682 (!(CONTROL & (3 << (DR_ENABLE_SIZE * (index)))))
684 #define LOCAL_ENABLE_REG(index) \
685 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
687 #define GLOBAL_ENABLE_REG(index) \
688 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
690 #define DISABLE_REG(index) \
691 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * (index))))
693 #define SET_LOCAL_EXACT() \
694 (CONTROL |= DR_LOCAL_SLOWDOWN)
696 #define SET_GLOBAL_EXACT() \
697 (CONTROL |= DR_GLOBAL_SLOWDOWN)
699 #define RESET_LOCAL_EXACT() \
700 (CONTROL &= ~(DR_LOCAL_SLOWDOWN))
702 #define RESET_GLOBAL_EXACT() \
703 (CONTROL &= ~(DR_GLOBAL_SLOWDOWN))
705 #define SET_BREAK(index,address) \
707 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index)));\
708 D_REGS[index] = address;\
709 dr_ref_count[index]++;\
712 #define SET_WATCH(index,address,rw,len) \
714 SET_BREAK(index,address);\
715 CONTROL |= ((len)|(rw)) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index));\
718 #define IS_WATCH(index) \
719 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE*(index))))
721 #define WATCH_HIT(index) ((STATUS & (1 << (index))) && IS_WATCH(index))
723 #define DR_DEF(index) \
724 ((CONTROL >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index))) & 0x0f)
727 #if 0 /* use debugging macro */
728 #define SHOW_DR(text,len) \
730 if (!getenv ("GDB_SHOW_DR")) break; \
731 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
732 fprintf(stderr,"%08x %d %08x %d ", \
733 edi.dr[0],dr_ref_count[0],edi.dr[1],dr_ref_count[1]); \
734 fprintf(stderr,"%08x %d %08x %d ", \
735 edi.dr[2],dr_ref_count[2],edi.dr[3],dr_ref_count[3]); \
736 fprintf(stderr,(len)?"(%s:%d)\n":"(%s)\n",#text,len); \
739 #define SHOW_DR(text,len) do {} while (0)
749 for (i
= 0; i
< 4; i
++)
756 /* Insert a watchpoint. */
759 go32_insert_watchpoint (int pid ATTRIBUTE_UNUSED
, CORE_ADDR addr
,
762 int ret
= go32_insert_aligned_watchpoint (addr
, addr
, len
, rw
);
764 SHOW_DR (insert_watch
, len
);
769 go32_insert_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
773 int read_write_bits
, len_bits
;
775 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
776 However, x86 doesn't support read-only data breakpoints. */
777 read_write_bits
= rw
? DR_RW_READWRITE
: DR_RW_WRITE
;
791 /* The debug registers only have 2 bits for the length, so
792 so this value will always fail the loop below. */
796 /* Look for an occupied debug register with the same address and the
797 same RW and LEN definitions. If we find one, we can use it for
798 this watchpoint as well (and save a register). */
799 for (i
= 0; i
< 4; i
++)
801 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
802 && DR_DEF (i
) == (unsigned)(len_bits
| read_write_bits
))
809 /* Look for a free debug register. */
810 for (i
= 0; i
<= 3; i
++)
816 /* No more debug registers! */
823 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
,
829 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
,
833 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
, len
, rw
);
835 SET_WATCH (i
, addr
, read_write_bits
, len_bits
);
836 LOCAL_ENABLE_REG (i
);
843 go32_handle_nonaligned_watchpoint (wp_op what
, CORE_ADDR waddr
, CORE_ADDR addr
,
848 int rv
= 0, status
= 0;
850 static int size_try_array
[4][4] =
852 { 1, 1, 1, 1 }, /* trying size one */
853 { 2, 1, 2, 1 }, /* trying size two */
854 { 2, 1, 2, 1 }, /* trying size three */
855 { 4, 1, 2, 1 } /* trying size four */
861 /* Four is the maximum length a 386 debug register can watch. */
862 size
= size_try_array
[len
> 4 ? 3 : len
- 1][align
];
863 if (what
== wp_insert
)
864 status
= go32_insert_aligned_watchpoint (waddr
, addr
, size
, rw
);
865 else if (what
== wp_remove
)
866 status
= go32_remove_aligned_watchpoint (waddr
, addr
, size
, rw
);
867 else if (what
== wp_count
)
871 /* We keep the loop going even after a failure, because some of
872 the other aligned watchpoints might still succeed, e.g. if
873 they watch addresses that are already watched, and thus just
874 increment the reference counts of occupied debug registers.
875 If we break out of the loop too early, we could cause those
876 addresses watched by other watchpoints to be disabled when
877 GDB reacts to our failure to insert this watchpoint and tries
887 /* Remove a watchpoint. */
890 go32_remove_watchpoint (int pid ATTRIBUTE_UNUSED
, CORE_ADDR addr
,
893 int ret
= go32_remove_aligned_watchpoint (addr
, addr
, len
, rw
);
895 SHOW_DR (remove_watch
, len
);
900 go32_remove_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
904 int read_write_bits
, len_bits
;
906 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
907 However, x86 doesn't support read-only data breakpoints. */
908 read_write_bits
= rw
? DR_RW_READWRITE
: DR_RW_WRITE
;
922 /* The debug registers only have 2 bits for the length, so
923 so this value will always fail the loop below. */
930 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
,
936 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
,
940 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
, len
, rw
);
942 for (i
= 0; i
<= 3; i
++)
944 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
945 && DR_DEF (i
) == (unsigned)(len_bits
| read_write_bits
))
948 if (dr_ref_count
[i
] == 0)
952 RESET_LOCAL_EXACT ();
953 RESET_GLOBAL_EXACT ();
958 /* Can we use debug registers to watch a region whose address is ADDR
959 and whose length is LEN bytes? */
962 go32_region_ok_for_watchpoint (CORE_ADDR addr
, int len
)
964 /* Compute how many aligned watchpoints we would need to cover this
966 int nregs
= go32_handle_nonaligned_watchpoint (wp_count
, addr
, addr
, len
, 0);
968 return nregs
<= 4 ? 1 : 0;
971 /* Check if stopped by a data watchpoint. If so, return the address
972 whose access triggered the watchpoint. */
975 go32_stopped_by_watchpoint (int pid ATTRIBUTE_UNUSED
, int data_watchpoint
)
980 status
= edi
.dr
[DR_STATUS
];
981 SHOW_DR (stopped_by
, 0);
982 for (i
= 0; i
<= 3; i
++)
984 if (WATCH_HIT (i
) && data_watchpoint
)
994 /* Remove a breakpoint. */
997 go32_remove_hw_breakpoint (CORE_ADDR addr
, void *shadow ATTRIBUTE_UNUSED
)
1000 for (i
= 0; i
<= 3; i
++)
1002 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
&& DR_DEF (i
) == 0)
1005 if (dr_ref_count
[i
] == 0)
1009 SHOW_DR (remove_hw
, 0);
1014 go32_insert_hw_breakpoint (CORE_ADDR addr
, void *shadow ATTRIBUTE_UNUSED
)
1018 /* Look for an occupied debug register with the same address and the
1019 same RW and LEN definitions. If we find one, we can use it for
1020 this breakpoint as well (and save a register). */
1021 for (i
= 0; i
< 4; i
++)
1023 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
&& DR_DEF (i
) == 0)
1026 SHOW_DR (insert_hw
, 0);
1031 /* Look for a free debug register. */
1032 for (i
= 0; i
<= 3; i
++)
1034 if (IS_REG_FREE (i
))
1038 /* No more debug registers? */
1041 SET_BREAK (i
, addr
);
1042 LOCAL_ENABLE_REG (i
);
1044 SHOW_DR (insert_hw
, 0);
1046 return i
< 4 ? 0 : EBUSY
;
1049 /* Put the device open on handle FD into either raw or cooked
1050 mode, return 1 if it was in raw mode, zero otherwise. */
1053 device_mode (int fd
, int raw_p
)
1055 int oldmode
, newmode
;
1060 __dpmi_int (0x21, ®s
);
1061 if (regs
.x
.flags
& 1)
1063 newmode
= oldmode
= regs
.x
.dx
;
1070 if (oldmode
& 0x80) /* Only for character dev */
1074 regs
.x
.dx
= newmode
& 0xff; /* Force upper byte zero, else it fails */
1075 __dpmi_int (0x21, ®s
);
1076 if (regs
.x
.flags
& 1)
1079 return (oldmode
& 0x20) == 0x20;
1083 static int inf_mode_valid
= 0;
1084 static int inf_terminal_mode
;
1086 /* This semaphore is needed because, amazingly enough, GDB calls
1087 target.to_terminal_ours more than once after the inferior stops.
1088 But we need the information from the first call only, since the
1089 second call will always see GDB's own cooked terminal. */
1090 static int terminal_is_ours
= 1;
1093 go32_terminal_init (void)
1095 inf_mode_valid
= 0; /* reinitialize, in case they are restarting child */
1096 terminal_is_ours
= 1;
1100 go32_terminal_info (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
1102 printf_unfiltered ("Inferior's terminal is in %s mode.\n",
1104 ? "default" : inf_terminal_mode
? "raw" : "cooked");
1106 #if __DJGPP_MINOR__ > 2
1107 if (child_cmd
.redirection
)
1111 for (i
= 0; i
< DBG_HANDLES
; i
++)
1113 if (child_cmd
.redirection
[i
]->file_name
)
1114 printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
1115 i
, child_cmd
.redirection
[i
]->file_name
);
1116 else if (_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) == -1)
1118 ("\tFile handle %d appears to be closed by inferior.\n", i
);
1119 /* Mask off the raw/cooked bit when comparing device info words. */
1120 else if ((_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) & 0xdf)
1121 != (_get_dev_info (i
) & 0xdf))
1123 ("\tFile handle %d appears to be redirected by inferior.\n", i
);
1130 go32_terminal_inferior (void)
1132 /* Redirect standard handles as child wants them. */
1134 if (redir_to_child (&child_cmd
) == -1)
1136 redir_to_debugger (&child_cmd
);
1137 error ("Cannot redirect standard handles for program: %s.",
1140 /* set the console device of the inferior to whatever mode
1141 (raw or cooked) we found it last time */
1142 if (terminal_is_ours
)
1145 device_mode (0, inf_terminal_mode
);
1146 terminal_is_ours
= 0;
1151 go32_terminal_ours (void)
1153 /* Switch to cooked mode on the gdb terminal and save the inferior
1154 terminal mode to be restored when it is resumed */
1155 if (!terminal_is_ours
)
1157 inf_terminal_mode
= device_mode (0, 0);
1158 if (inf_terminal_mode
!= -1)
1161 /* If device_mode returned -1, we don't know what happens with
1162 handle 0 anymore, so make the info invalid. */
1164 terminal_is_ours
= 1;
1166 /* Restore debugger's standard handles. */
1168 if (redir_to_debugger (&child_cmd
) == -1)
1170 redir_to_child (&child_cmd
);
1171 error ("Cannot redirect standard handles for debugger: %s.",
1178 init_go32_ops (void)
1180 go32_ops
.to_shortname
= "djgpp";
1181 go32_ops
.to_longname
= "djgpp target process";
1183 "Program loaded by djgpp, when gdb is used as an external debugger";
1184 go32_ops
.to_open
= go32_open
;
1185 go32_ops
.to_close
= go32_close
;
1186 go32_ops
.to_attach
= go32_attach
;
1187 go32_ops
.to_detach
= go32_detach
;
1188 go32_ops
.to_resume
= go32_resume
;
1189 go32_ops
.to_wait
= go32_wait
;
1190 go32_ops
.to_fetch_registers
= go32_fetch_registers
;
1191 go32_ops
.to_store_registers
= go32_store_registers
;
1192 go32_ops
.to_prepare_to_store
= go32_prepare_to_store
;
1193 go32_ops
.to_xfer_memory
= go32_xfer_memory
;
1194 go32_ops
.to_files_info
= go32_files_info
;
1195 go32_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
1196 go32_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
1197 go32_ops
.to_terminal_init
= go32_terminal_init
;
1198 go32_ops
.to_terminal_inferior
= go32_terminal_inferior
;
1199 go32_ops
.to_terminal_ours_for_output
= go32_terminal_ours
;
1200 go32_ops
.to_terminal_ours
= go32_terminal_ours
;
1201 go32_ops
.to_terminal_info
= go32_terminal_info
;
1202 go32_ops
.to_kill
= go32_kill_inferior
;
1203 go32_ops
.to_create_inferior
= go32_create_inferior
;
1204 go32_ops
.to_mourn_inferior
= go32_mourn_inferior
;
1205 go32_ops
.to_can_run
= go32_can_run
;
1206 go32_ops
.to_stop
= go32_stop
;
1207 go32_ops
.to_stratum
= process_stratum
;
1208 go32_ops
.to_has_all_memory
= 1;
1209 go32_ops
.to_has_memory
= 1;
1210 go32_ops
.to_has_stack
= 1;
1211 go32_ops
.to_has_registers
= 1;
1212 go32_ops
.to_has_execution
= 1;
1213 go32_ops
.to_magic
= OPS_MAGIC
;
1215 /* Initialize child's cwd as empty to be initialized when starting
1219 /* Initialize child's command line storage. */
1220 if (redir_debug_init (&child_cmd
) == -1)
1221 internal_error (__FILE__
, __LINE__
,
1222 "Cannot allocate redirection storage: not enough memory.\n");
1224 /* We are always processing GCC-compiled programs. */
1225 processing_gcc_compilation
= 2;
1229 _initialize_go32_nat (void)
1232 add_target (&go32_ops
);
1245 tcsetpgrp (int fd
, pid_t pgid
)
1247 if (isatty (fd
) && pgid
== SOME_PID
)
1249 errno
= pgid
== SOME_PID
? ENOTTY
: ENOSYS
;