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"
34 #include <stdio.h> /* required for __DJGPP_MINOR__ */
41 #include <debug/v2load.h>
42 #include <debug/dbgcom.h>
43 #if __DJGPP_MINOR__ > 2
44 #include <debug/redir.h>
47 #if __DJGPP_MINOR__ < 3
48 /* This code will be provided from DJGPP 2.03 on. Until then I code it
56 unsigned short exponent
:15;
57 unsigned short sign
:1;
76 static void save_npx (void); /* Save the FPU of the debugged program */
77 static void load_npx (void); /* Restore the FPU of the debugged program */
79 /* ------------------------------------------------------------------------- */
80 /* Store the contents of the NPX in the global variable `npx'. */
108 /* ------------------------------------------------------------------------- */
109 /* Reload the contents of the NPX from the global variable `npx'. */
114 asm ("frstor %0":"=m" (npx
));
116 /* ------------------------------------------------------------------------- */
117 /* Stubs for the missing redirection functions. */
123 void redir_cmdline_delete (cmdline_t
*ptr
) {ptr
->redirected
= 0;}
124 int redir_cmdline_parse (const char *args
, cmdline_t
*ptr
)
128 int redir_to_child (cmdline_t
*ptr
)
132 int redir_to_debugger (cmdline_t
*ptr
)
136 int redir_debug_init (cmdline_t
*ptr
) { return 0; }
137 #endif /* __DJGPP_MINOR < 3 */
139 extern void _initialize_go32_nat (void);
141 typedef enum { wp_insert
, wp_remove
, wp_count
} wp_op
;
143 /* This holds the current reference counts for each debug register. */
144 static int dr_ref_count
[4];
146 extern char **environ
;
150 static int prog_has_started
= 0;
151 static void go32_open (char *name
, int from_tty
);
152 static void go32_close (int quitting
);
153 static void go32_attach (char *args
, int from_tty
);
154 static void go32_detach (char *args
, int from_tty
);
155 static void go32_resume (int pid
, int step
, enum target_signal siggnal
);
156 static int go32_wait (int pid
, struct target_waitstatus
*status
);
157 static void go32_fetch_registers (int regno
);
158 static void store_register (int regno
);
159 static void go32_store_registers (int regno
);
160 static void go32_prepare_to_store (void);
161 static int go32_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
,
163 struct mem_attrib
*attrib
,
164 struct target_ops
*target
);
165 static void go32_files_info (struct target_ops
*target
);
166 static void go32_stop (void);
167 static void go32_kill_inferior (void);
168 static void go32_create_inferior (char *exec_file
, char *args
, char **env
);
169 static void cleanup_dregs (void);
170 static void go32_mourn_inferior (void);
171 static int go32_can_run (void);
172 static int go32_insert_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
174 static int go32_remove_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
176 static int go32_handle_nonaligned_watchpoint (wp_op what
, CORE_ADDR waddr
,
177 CORE_ADDR addr
, int len
, int rw
);
179 static struct target_ops go32_ops
;
180 static void go32_terminal_init (void);
181 static void go32_terminal_inferior (void);
182 static void go32_terminal_ours (void);
184 #define r_ofs(x) (offsetof(TSS,x))
193 {r_ofs (tss_eax
), 4}, /* normal registers, from a_tss */
194 {r_ofs (tss_ecx
), 4},
195 {r_ofs (tss_edx
), 4},
196 {r_ofs (tss_ebx
), 4},
197 {r_ofs (tss_esp
), 4},
198 {r_ofs (tss_ebp
), 4},
199 {r_ofs (tss_esi
), 4},
200 {r_ofs (tss_edi
), 4},
201 {r_ofs (tss_eip
), 4},
202 {r_ofs (tss_eflags
), 4},
209 {0, 10}, /* 8 FP registers, from npx.reg[] */
217 /* The order of the next 7 registers must be consistent
218 with their numbering in config/i386/tm-i386.h, which see. */
219 {0, 2}, /* control word, from npx */
220 {4, 2}, /* status word, from npx */
221 {8, 2}, /* tag word, from npx */
222 {16, 2}, /* last FP exception CS from npx */
223 {12, 4}, /* last FP exception EIP from npx */
224 {24, 2}, /* last FP exception operand selector from npx */
225 {20, 4}, /* last FP exception operand offset from npx */
226 {18, 2} /* last FP opcode from npx */
232 enum target_signal gdb_sig
;
236 {0, TARGET_SIGNAL_FPE
},
237 {1, TARGET_SIGNAL_TRAP
},
238 /* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
239 but I think SIGBUS is better, since the NMI is usually activated
240 as a result of a memory parity check failure. */
241 {2, TARGET_SIGNAL_BUS
},
242 {3, TARGET_SIGNAL_TRAP
},
243 {4, TARGET_SIGNAL_FPE
},
244 {5, TARGET_SIGNAL_SEGV
},
245 {6, TARGET_SIGNAL_ILL
},
246 {7, TARGET_SIGNAL_EMT
}, /* no-coprocessor exception */
247 {8, TARGET_SIGNAL_SEGV
},
248 {9, TARGET_SIGNAL_SEGV
},
249 {10, TARGET_SIGNAL_BUS
},
250 {11, TARGET_SIGNAL_SEGV
},
251 {12, TARGET_SIGNAL_SEGV
},
252 {13, TARGET_SIGNAL_SEGV
},
253 {14, TARGET_SIGNAL_SEGV
},
254 {16, TARGET_SIGNAL_FPE
},
255 {17, TARGET_SIGNAL_BUS
},
256 {31, TARGET_SIGNAL_ILL
},
257 {0x1b, TARGET_SIGNAL_INT
},
258 {0x75, TARGET_SIGNAL_FPE
},
259 {0x78, TARGET_SIGNAL_ALRM
},
260 {0x79, TARGET_SIGNAL_INT
},
261 {0x7a, TARGET_SIGNAL_QUIT
},
262 {-1, TARGET_SIGNAL_LAST
}
266 enum target_signal gdb_sig
;
269 {TARGET_SIGNAL_0
, -1},
270 {TARGET_SIGNAL_ILL
, 6}, /* Invalid Opcode */
271 {TARGET_SIGNAL_EMT
, 7}, /* triggers SIGNOFP */
272 {TARGET_SIGNAL_SEGV
, 13}, /* GPF */
273 {TARGET_SIGNAL_BUS
, 17}, /* Alignment Check */
274 /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
276 {TARGET_SIGNAL_TERM
, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
277 {TARGET_SIGNAL_FPE
, 0x75},
278 {TARGET_SIGNAL_INT
, 0x79},
279 {TARGET_SIGNAL_QUIT
, 0x7a},
280 {TARGET_SIGNAL_ALRM
, 0x78}, /* triggers SIGTIMR */
281 {TARGET_SIGNAL_PROF
, 0x78},
282 {TARGET_SIGNAL_LAST
, -1}
286 go32_open (char *name ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
288 printf_unfiltered ("Done. Use the \"run\" command to run the program.\n");
292 go32_close (int quitting ATTRIBUTE_UNUSED
)
297 go32_attach (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
300 You cannot attach to a running program on this platform.\n\
301 Use the `run' command to run DJGPP programs.");
305 go32_detach (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
309 static int resume_is_step
;
310 static int resume_signal
= -1;
313 go32_resume (int pid ATTRIBUTE_UNUSED
, int step
, enum target_signal siggnal
)
317 resume_is_step
= step
;
319 if (siggnal
!= TARGET_SIGNAL_0
&& siggnal
!= TARGET_SIGNAL_TRAP
)
321 for (i
= 0, resume_signal
= -1;
322 excepn_map
[i
].gdb_sig
!= TARGET_SIGNAL_LAST
; i
++)
323 if (excepn_map
[i
].gdb_sig
== siggnal
)
325 resume_signal
= excepn_map
[i
].djgpp_excepno
;
328 if (resume_signal
== -1)
329 printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
330 target_signal_to_name (siggnal
));
334 static char child_cwd
[FILENAME_MAX
];
337 go32_wait (int pid ATTRIBUTE_UNUSED
, struct target_waitstatus
*status
)
340 unsigned char saved_opcode
;
341 unsigned long INT3_addr
= 0;
342 int stepping_over_INT
= 0;
344 a_tss
.tss_eflags
&= 0xfeff; /* reset the single-step flag (TF) */
347 /* If the next instruction is INT xx or INTO, we need to handle
348 them specially. Intel manuals say that these instructions
349 reset the single-step flag (a.k.a. TF). However, it seems
350 that, at least in the DPMI environment, and at least when
351 stepping over the DPMI interrupt 31h, the problem is having
352 TF set at all when INT 31h is executed: the debuggee either
353 crashes (and takes the system with it) or is killed by a
356 So we need to emulate single-step mode: we put an INT3 opcode
357 right after the INT xx instruction, let the debuggee run
358 until it hits INT3 and stops, then restore the original
359 instruction which we overwrote with the INT3 opcode, and back
360 up the debuggee's EIP to that instruction. */
361 read_child (a_tss
.tss_eip
, &saved_opcode
, 1);
362 if (saved_opcode
== 0xCD || saved_opcode
== 0xCE)
364 unsigned char INT3_opcode
= 0xCC;
367 = saved_opcode
== 0xCD ? a_tss
.tss_eip
+ 2 : a_tss
.tss_eip
+ 1;
368 stepping_over_INT
= 1;
369 read_child (INT3_addr
, &saved_opcode
, 1);
370 write_child (INT3_addr
, &INT3_opcode
, 1);
373 a_tss
.tss_eflags
|= 0x0100; /* normal instruction: set TF */
376 /* The special value FFFFh in tss_trap indicates to run_child that
377 tss_irqn holds a signal to be delivered to the debuggee. */
378 if (resume_signal
<= -1)
381 a_tss
.tss_irqn
= 0xff;
385 a_tss
.tss_trap
= 0xffff; /* run_child looks for this */
386 a_tss
.tss_irqn
= resume_signal
;
389 /* The child might change working directory behind our back. The
390 GDB users won't like the side effects of that when they work with
391 relative file names, and GDB might be confused by its current
392 directory not being in sync with the truth. So we always make a
393 point of changing back to where GDB thinks is its cwd, when we
394 return control to the debugger, but restore child's cwd before we
396 /* Initialize child_cwd, before the first call to run_child and not
397 in the initialization, so the child get also the changed directory
398 set with the gdb-command "cd ..." */
400 /* Initialize child's cwd with the current one. */
401 getcwd (child_cwd
, sizeof (child_cwd
));
405 #if __DJGPP_MINOR__ < 3
409 #if __DJGPP_MINOR__ < 3
413 /* Did we step over an INT xx instruction? */
414 if (stepping_over_INT
&& a_tss
.tss_eip
== INT3_addr
+ 1)
416 /* Restore the original opcode. */
417 a_tss
.tss_eip
--; /* EIP points *after* the INT3 instruction */
418 write_child (a_tss
.tss_eip
, &saved_opcode
, 1);
419 /* Simulate a TRAP exception. */
421 a_tss
.tss_eflags
|= 0x0100;
424 getcwd (child_cwd
, sizeof (child_cwd
)); /* in case it has changed */
425 chdir (current_directory
);
427 if (a_tss
.tss_irqn
== 0x21)
429 status
->kind
= TARGET_WAITKIND_EXITED
;
430 status
->value
.integer
= a_tss
.tss_eax
& 0xff;
434 status
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
435 status
->kind
= TARGET_WAITKIND_STOPPED
;
436 for (i
= 0; sig_map
[i
].go32_sig
!= -1; i
++)
438 if (a_tss
.tss_irqn
== sig_map
[i
].go32_sig
)
440 #if __DJGPP_MINOR__ < 3
441 if ((status
->value
.sig
= sig_map
[i
].gdb_sig
) !=
443 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
445 status
->value
.sig
= sig_map
[i
].gdb_sig
;
455 fetch_register (int regno
)
457 if (regno
< FP0_REGNUM
)
458 supply_register (regno
, (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
);
459 else if (regno
<= LAST_FPU_CTRL_REGNUM
)
460 i387_supply_register (regno
, (char *) &npx
);
462 internal_error (__FILE__
, __LINE__
,
463 "Invalid register no. %d in fetch_register.", regno
);
467 go32_fetch_registers (int regno
)
470 fetch_register (regno
);
473 for (regno
= 0; regno
< FP0_REGNUM
; regno
++)
474 fetch_register (regno
);
475 i387_supply_fsave ((char *) &npx
);
480 store_register (int regno
)
483 void *v
= (void *) ®isters
[REGISTER_BYTE (regno
)];
485 if (regno
< FP0_REGNUM
)
486 memcpy ((char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
,
487 v
, regno_mapping
[regno
].size
);
488 else if (regno
<= LAST_FPU_CTRL_REGNUM
)
489 i387_fill_fsave ((char *)&npx
, regno
);
491 internal_error (__FILE__
, __LINE__
,
492 "Invalid register no. %d in store_register.", regno
);
496 go32_store_registers (int regno
)
501 store_register (regno
);
504 for (r
= 0; r
< FP0_REGNUM
; r
++)
506 i387_fill_fsave ((char *) &npx
, -1);
511 go32_prepare_to_store (void)
516 go32_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
517 struct mem_attrib
*attrib ATTRIBUTE_UNUSED
,
518 struct target_ops
*target ATTRIBUTE_UNUSED
)
522 if (write_child (memaddr
, myaddr
, len
))
533 if (read_child (memaddr
, myaddr
, len
))
544 static cmdline_t child_cmd
; /* parsed child's command line kept here */
547 go32_files_info (struct target_ops
*target ATTRIBUTE_UNUSED
)
549 printf_unfiltered ("You are running a DJGPP V2 program.\n");
558 prog_has_started
= 0;
562 go32_kill_inferior (void)
564 redir_cmdline_delete (&child_cmd
);
567 unpush_target (&go32_ops
);
571 go32_create_inferior (char *exec_file
, char *args
, char **env
)
575 char **env_save
= environ
;
577 /* If no exec file handed to us, get it from the exec-file command -- with
578 a good, common error message if none is specified. */
580 exec_file
= get_exec_file (1);
582 if (prog_has_started
)
585 go32_kill_inferior ();
590 /* Initialize child's cwd as empty to be initialized when starting
594 /* Init command line storage. */
595 if (redir_debug_init (&child_cmd
) == -1)
596 internal_error (__FILE__
, __LINE__
,
597 "Cannot allocate redirection storage: not enough memory.\n");
599 /* Parse the command line and create redirections. */
600 if (strpbrk (args
, "<>"))
602 if (redir_cmdline_parse (args
, &child_cmd
) == 0)
603 args
= child_cmd
.command
;
605 error ("Syntax error in command line.");
608 child_cmd
.command
= xstrdup (args
);
610 cmdline
= (char *) alloca (strlen (args
) + 4);
611 cmdline
[0] = strlen (args
);
612 strcpy (cmdline
+ 1, args
);
613 cmdline
[strlen (args
) + 1] = 13;
617 if (v2loadimage (exec_file
, cmdline
, start_state
))
620 printf_unfiltered ("Load failed for image %s\n", exec_file
);
625 edi_init (start_state
);
626 #if __DJGPP_MINOR__ < 3
630 inferior_pid
= SOME_PID
;
631 push_target (&go32_ops
);
632 clear_proceed_status ();
633 insert_breakpoints ();
634 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
635 prog_has_started
= 1;
639 go32_mourn_inferior (void)
641 /* We need to make sure all the breakpoint enable bits in the DR7
642 register are reset when the inferior exits. Otherwise, if they
643 rerun the inferior, the uncleared bits may cause random SIGTRAPs,
644 failure to set more watchpoints, and other calamities. It would
645 be nice if GDB itself would take care to remove all breakpoints
646 at all times, but it doesn't, probably under an assumption that
647 the OS cleans up when the debuggee exits. */
649 go32_kill_inferior ();
650 generic_mourn_inferior ();
659 /* Hardware watchpoint support. */
663 #define DR_ENABLE_SIZE 2
664 #define DR_LOCAL_ENABLE_SHIFT 0
665 #define DR_GLOBAL_ENABLE_SHIFT 1
666 #define DR_LOCAL_SLOWDOWN 0x100
667 #define DR_GLOBAL_SLOWDOWN 0x200
668 #define DR_CONTROL_SHIFT 16
669 #define DR_CONTROL_SIZE 4
670 #define DR_RW_READWRITE 0x3
671 #define DR_RW_WRITE 0x1
672 #define DR_CONTROL_MASK 0xf
673 #define DR_ENABLE_MASK 0x3
678 #define D_REGS edi.dr
679 #define CONTROL D_REGS[DR_CONTROL]
680 #define STATUS D_REGS[DR_STATUS]
682 #define IS_REG_FREE(index) \
683 (!(CONTROL & (3 << (DR_ENABLE_SIZE * (index)))))
685 #define LOCAL_ENABLE_REG(index) \
686 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
688 #define GLOBAL_ENABLE_REG(index) \
689 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
691 #define DISABLE_REG(index) \
692 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * (index))))
694 #define SET_LOCAL_EXACT() \
695 (CONTROL |= DR_LOCAL_SLOWDOWN)
697 #define SET_GLOBAL_EXACT() \
698 (CONTROL |= DR_GLOBAL_SLOWDOWN)
700 #define RESET_LOCAL_EXACT() \
701 (CONTROL &= ~(DR_LOCAL_SLOWDOWN))
703 #define RESET_GLOBAL_EXACT() \
704 (CONTROL &= ~(DR_GLOBAL_SLOWDOWN))
706 #define SET_BREAK(index,address) \
708 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index)));\
709 D_REGS[index] = address;\
710 dr_ref_count[index]++;\
713 #define SET_WATCH(index,address,rw,len) \
715 SET_BREAK(index,address);\
716 CONTROL |= ((len)|(rw)) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index));\
719 #define IS_WATCH(index) \
720 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE*(index))))
722 #define WATCH_HIT(index) ((STATUS & (1 << (index))) && IS_WATCH(index))
724 #define DR_DEF(index) \
725 ((CONTROL >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index))) & 0x0f)
728 #if 0 /* use debugging macro */
729 #define SHOW_DR(text,len) \
731 if (!getenv ("GDB_SHOW_DR")) break; \
732 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
733 fprintf(stderr,"%08x %d %08x %d ", \
734 edi.dr[0],dr_ref_count[0],edi.dr[1],dr_ref_count[1]); \
735 fprintf(stderr,"%08x %d %08x %d ", \
736 edi.dr[2],dr_ref_count[2],edi.dr[3],dr_ref_count[3]); \
737 fprintf(stderr,(len)?"(%s:%d)\n":"(%s)\n",#text,len); \
740 #define SHOW_DR(text,len) do {} while (0)
750 for (i
= 0; i
< 4; i
++)
757 /* Insert a watchpoint. */
760 go32_insert_watchpoint (int pid ATTRIBUTE_UNUSED
, CORE_ADDR addr
,
763 int ret
= go32_insert_aligned_watchpoint (addr
, addr
, len
, rw
);
765 SHOW_DR (insert_watch
, len
);
770 go32_insert_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
774 int read_write_bits
, len_bits
;
776 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
777 However, x86 doesn't support read-only data breakpoints. */
778 read_write_bits
= rw
? DR_RW_READWRITE
: DR_RW_WRITE
;
792 /* The debug registers only have 2 bits for the length, so
793 so this value will always fail the loop below. */
797 /* Look for an occupied debug register with the same address and the
798 same RW and LEN definitions. If we find one, we can use it for
799 this watchpoint as well (and save a register). */
800 for (i
= 0; i
< 4; i
++)
802 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
803 && DR_DEF (i
) == (unsigned)(len_bits
| read_write_bits
))
810 /* Look for a free debug register. */
811 for (i
= 0; i
<= 3; i
++)
817 /* No more debug registers! */
824 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
,
830 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
,
834 return go32_handle_nonaligned_watchpoint (wp_insert
, waddr
, addr
, len
, rw
);
836 SET_WATCH (i
, addr
, read_write_bits
, len_bits
);
837 LOCAL_ENABLE_REG (i
);
844 go32_handle_nonaligned_watchpoint (wp_op what
, CORE_ADDR waddr
, CORE_ADDR addr
,
849 int rv
= 0, status
= 0;
851 static int size_try_array
[4][4] =
853 { 1, 1, 1, 1 }, /* trying size one */
854 { 2, 1, 2, 1 }, /* trying size two */
855 { 2, 1, 2, 1 }, /* trying size three */
856 { 4, 1, 2, 1 } /* trying size four */
862 /* Four is the maximum length a 386 debug register can watch. */
863 size
= size_try_array
[len
> 4 ? 3 : len
- 1][align
];
864 if (what
== wp_insert
)
865 status
= go32_insert_aligned_watchpoint (waddr
, addr
, size
, rw
);
866 else if (what
== wp_remove
)
867 status
= go32_remove_aligned_watchpoint (waddr
, addr
, size
, rw
);
868 else if (what
== wp_count
)
872 /* We keep the loop going even after a failure, because some of
873 the other aligned watchpoints might still succeed, e.g. if
874 they watch addresses that are already watched, and thus just
875 increment the reference counts of occupied debug registers.
876 If we break out of the loop too early, we could cause those
877 addresses watched by other watchpoints to be disabled when
878 GDB reacts to our failure to insert this watchpoint and tries
888 /* Remove a watchpoint. */
891 go32_remove_watchpoint (int pid ATTRIBUTE_UNUSED
, CORE_ADDR addr
,
894 int ret
= go32_remove_aligned_watchpoint (addr
, addr
, len
, rw
);
896 SHOW_DR (remove_watch
, len
);
901 go32_remove_aligned_watchpoint (CORE_ADDR waddr
, CORE_ADDR addr
,
905 int read_write_bits
, len_bits
;
907 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
908 However, x86 doesn't support read-only data breakpoints. */
909 read_write_bits
= rw
? DR_RW_READWRITE
: DR_RW_WRITE
;
923 /* The debug registers only have 2 bits for the length, so
924 so this value will always fail the loop below. */
931 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
,
937 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
,
941 return go32_handle_nonaligned_watchpoint (wp_remove
, waddr
, addr
, len
, rw
);
943 for (i
= 0; i
<= 3; i
++)
945 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
946 && DR_DEF (i
) == (unsigned)(len_bits
| read_write_bits
))
949 if (dr_ref_count
[i
] == 0)
953 RESET_LOCAL_EXACT ();
954 RESET_GLOBAL_EXACT ();
959 /* Can we use debug registers to watch a region whose address is ADDR
960 and whose length is LEN bytes? */
963 go32_region_ok_for_watchpoint (CORE_ADDR addr
, int len
)
965 /* Compute how many aligned watchpoints we would need to cover this
967 int nregs
= go32_handle_nonaligned_watchpoint (wp_count
, addr
, addr
, len
, 0);
969 return nregs
<= 4 ? 1 : 0;
972 /* Check if stopped by a data watchpoint. If so, return the address
973 whose access triggered the watchpoint. */
976 go32_stopped_by_watchpoint (int pid ATTRIBUTE_UNUSED
, int data_watchpoint
)
981 status
= edi
.dr
[DR_STATUS
];
982 SHOW_DR (stopped_by
, 0);
983 for (i
= 0; i
<= 3; i
++)
985 if (WATCH_HIT (i
) && data_watchpoint
)
995 /* Remove a breakpoint. */
998 go32_remove_hw_breakpoint (CORE_ADDR addr
, void *shadow ATTRIBUTE_UNUSED
)
1001 for (i
= 0; i
<= 3; i
++)
1003 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
&& DR_DEF (i
) == 0)
1006 if (dr_ref_count
[i
] == 0)
1010 SHOW_DR (remove_hw
, 0);
1015 go32_insert_hw_breakpoint (CORE_ADDR addr
, void *shadow ATTRIBUTE_UNUSED
)
1019 /* Look for an occupied debug register with the same address and the
1020 same RW and LEN definitions. If we find one, we can use it for
1021 this breakpoint as well (and save a register). */
1022 for (i
= 0; i
< 4; i
++)
1024 if (!IS_REG_FREE (i
) && D_REGS
[i
] == addr
&& DR_DEF (i
) == 0)
1027 SHOW_DR (insert_hw
, 0);
1032 /* Look for a free debug register. */
1033 for (i
= 0; i
<= 3; i
++)
1035 if (IS_REG_FREE (i
))
1039 /* No more debug registers? */
1042 SET_BREAK (i
, addr
);
1043 LOCAL_ENABLE_REG (i
);
1045 SHOW_DR (insert_hw
, 0);
1047 return i
< 4 ? 0 : EBUSY
;
1050 /* Put the device open on handle FD into either raw or cooked
1051 mode, return 1 if it was in raw mode, zero otherwise. */
1054 device_mode (int fd
, int raw_p
)
1056 int oldmode
, newmode
;
1061 __dpmi_int (0x21, ®s
);
1062 if (regs
.x
.flags
& 1)
1064 newmode
= oldmode
= regs
.x
.dx
;
1071 if (oldmode
& 0x80) /* Only for character dev */
1075 regs
.x
.dx
= newmode
& 0xff; /* Force upper byte zero, else it fails */
1076 __dpmi_int (0x21, ®s
);
1077 if (regs
.x
.flags
& 1)
1080 return (oldmode
& 0x20) == 0x20;
1084 static int inf_mode_valid
= 0;
1085 static int inf_terminal_mode
;
1087 /* This semaphore is needed because, amazingly enough, GDB calls
1088 target.to_terminal_ours more than once after the inferior stops.
1089 But we need the information from the first call only, since the
1090 second call will always see GDB's own cooked terminal. */
1091 static int terminal_is_ours
= 1;
1094 go32_terminal_init (void)
1096 inf_mode_valid
= 0; /* reinitialize, in case they are restarting child */
1097 terminal_is_ours
= 1;
1101 go32_terminal_info (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
1103 printf_unfiltered ("Inferior's terminal is in %s mode.\n",
1105 ? "default" : inf_terminal_mode
? "raw" : "cooked");
1107 #if __DJGPP_MINOR__ > 2
1108 if (child_cmd
.redirection
)
1112 for (i
= 0; i
< DBG_HANDLES
; i
++)
1114 if (child_cmd
.redirection
[i
]->file_name
)
1115 printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
1116 i
, child_cmd
.redirection
[i
]->file_name
);
1117 else if (_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) == -1)
1119 ("\tFile handle %d appears to be closed by inferior.\n", i
);
1120 /* Mask off the raw/cooked bit when comparing device info words. */
1121 else if ((_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) & 0xdf)
1122 != (_get_dev_info (i
) & 0xdf))
1124 ("\tFile handle %d appears to be redirected by inferior.\n", i
);
1131 go32_terminal_inferior (void)
1133 /* Redirect standard handles as child wants them. */
1135 if (redir_to_child (&child_cmd
) == -1)
1137 redir_to_debugger (&child_cmd
);
1138 error ("Cannot redirect standard handles for program: %s.",
1141 /* set the console device of the inferior to whatever mode
1142 (raw or cooked) we found it last time */
1143 if (terminal_is_ours
)
1146 device_mode (0, inf_terminal_mode
);
1147 terminal_is_ours
= 0;
1152 go32_terminal_ours (void)
1154 /* Switch to cooked mode on the gdb terminal and save the inferior
1155 terminal mode to be restored when it is resumed */
1156 if (!terminal_is_ours
)
1158 inf_terminal_mode
= device_mode (0, 0);
1159 if (inf_terminal_mode
!= -1)
1162 /* If device_mode returned -1, we don't know what happens with
1163 handle 0 anymore, so make the info invalid. */
1165 terminal_is_ours
= 1;
1167 /* Restore debugger's standard handles. */
1169 if (redir_to_debugger (&child_cmd
) == -1)
1171 redir_to_child (&child_cmd
);
1172 error ("Cannot redirect standard handles for debugger: %s.",
1179 init_go32_ops (void)
1181 go32_ops
.to_shortname
= "djgpp";
1182 go32_ops
.to_longname
= "djgpp target process";
1184 "Program loaded by djgpp, when gdb is used as an external debugger";
1185 go32_ops
.to_open
= go32_open
;
1186 go32_ops
.to_close
= go32_close
;
1187 go32_ops
.to_attach
= go32_attach
;
1188 go32_ops
.to_detach
= go32_detach
;
1189 go32_ops
.to_resume
= go32_resume
;
1190 go32_ops
.to_wait
= go32_wait
;
1191 go32_ops
.to_fetch_registers
= go32_fetch_registers
;
1192 go32_ops
.to_store_registers
= go32_store_registers
;
1193 go32_ops
.to_prepare_to_store
= go32_prepare_to_store
;
1194 go32_ops
.to_xfer_memory
= go32_xfer_memory
;
1195 go32_ops
.to_files_info
= go32_files_info
;
1196 go32_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
1197 go32_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
1198 go32_ops
.to_terminal_init
= go32_terminal_init
;
1199 go32_ops
.to_terminal_inferior
= go32_terminal_inferior
;
1200 go32_ops
.to_terminal_ours_for_output
= go32_terminal_ours
;
1201 go32_ops
.to_terminal_ours
= go32_terminal_ours
;
1202 go32_ops
.to_terminal_info
= go32_terminal_info
;
1203 go32_ops
.to_kill
= go32_kill_inferior
;
1204 go32_ops
.to_create_inferior
= go32_create_inferior
;
1205 go32_ops
.to_mourn_inferior
= go32_mourn_inferior
;
1206 go32_ops
.to_can_run
= go32_can_run
;
1207 go32_ops
.to_stop
= go32_stop
;
1208 go32_ops
.to_stratum
= process_stratum
;
1209 go32_ops
.to_has_all_memory
= 1;
1210 go32_ops
.to_has_memory
= 1;
1211 go32_ops
.to_has_stack
= 1;
1212 go32_ops
.to_has_registers
= 1;
1213 go32_ops
.to_has_execution
= 1;
1214 go32_ops
.to_magic
= OPS_MAGIC
;
1216 /* Initialize child's cwd as empty to be initialized when starting
1220 /* Initialize child's command line storage. */
1221 if (redir_debug_init (&child_cmd
) == -1)
1222 internal_error (__FILE__
, __LINE__
,
1223 "Cannot allocate redirection storage: not enough memory.\n");
1225 /* We are always processing GCC-compiled programs. */
1226 processing_gcc_compilation
= 2;
1230 _initialize_go32_nat (void)
1233 add_target (&go32_ops
);
1246 tcsetpgrp (int fd
, pid_t pgid
)
1248 if (isatty (fd
) && pgid
== SOME_PID
)
1250 errno
= pgid
== SOME_PID
? ENOTTY
: ENOSYS
;