1 /* Native debugging support for Intel x86 running DJGPP.
2 Copyright 1997, 1999, 2000, 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 go32_mourn_inferior (void);
170 static int go32_can_run (void);
172 static struct target_ops go32_ops
;
173 static void go32_terminal_init (void);
174 static void go32_terminal_inferior (void);
175 static void go32_terminal_ours (void);
177 #define r_ofs(x) (offsetof(TSS,x))
186 {r_ofs (tss_eax
), 4}, /* normal registers, from a_tss */
187 {r_ofs (tss_ecx
), 4},
188 {r_ofs (tss_edx
), 4},
189 {r_ofs (tss_ebx
), 4},
190 {r_ofs (tss_esp
), 4},
191 {r_ofs (tss_ebp
), 4},
192 {r_ofs (tss_esi
), 4},
193 {r_ofs (tss_edi
), 4},
194 {r_ofs (tss_eip
), 4},
195 {r_ofs (tss_eflags
), 4},
202 {0, 10}, /* 8 FP registers, from npx.reg[] */
210 /* The order of the next 7 registers must be consistent
211 with their numbering in config/i386/tm-i386.h, which see. */
212 {0, 2}, /* control word, from npx */
213 {4, 2}, /* status word, from npx */
214 {8, 2}, /* tag word, from npx */
215 {16, 2}, /* last FP exception CS from npx */
216 {12, 4}, /* last FP exception EIP from npx */
217 {24, 2}, /* last FP exception operand selector from npx */
218 {20, 4}, /* last FP exception operand offset from npx */
219 {18, 2} /* last FP opcode from npx */
225 enum target_signal gdb_sig
;
229 {0, TARGET_SIGNAL_FPE
},
230 {1, TARGET_SIGNAL_TRAP
},
231 /* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
232 but I think SIGBUS is better, since the NMI is usually activated
233 as a result of a memory parity check failure. */
234 {2, TARGET_SIGNAL_BUS
},
235 {3, TARGET_SIGNAL_TRAP
},
236 {4, TARGET_SIGNAL_FPE
},
237 {5, TARGET_SIGNAL_SEGV
},
238 {6, TARGET_SIGNAL_ILL
},
239 {7, TARGET_SIGNAL_EMT
}, /* no-coprocessor exception */
240 {8, TARGET_SIGNAL_SEGV
},
241 {9, TARGET_SIGNAL_SEGV
},
242 {10, TARGET_SIGNAL_BUS
},
243 {11, TARGET_SIGNAL_SEGV
},
244 {12, TARGET_SIGNAL_SEGV
},
245 {13, TARGET_SIGNAL_SEGV
},
246 {14, TARGET_SIGNAL_SEGV
},
247 {16, TARGET_SIGNAL_FPE
},
248 {17, TARGET_SIGNAL_BUS
},
249 {31, TARGET_SIGNAL_ILL
},
250 {0x1b, TARGET_SIGNAL_INT
},
251 {0x75, TARGET_SIGNAL_FPE
},
252 {0x78, TARGET_SIGNAL_ALRM
},
253 {0x79, TARGET_SIGNAL_INT
},
254 {0x7a, TARGET_SIGNAL_QUIT
},
255 {-1, TARGET_SIGNAL_LAST
}
259 enum target_signal gdb_sig
;
262 {TARGET_SIGNAL_0
, -1},
263 {TARGET_SIGNAL_ILL
, 6}, /* Invalid Opcode */
264 {TARGET_SIGNAL_EMT
, 7}, /* triggers SIGNOFP */
265 {TARGET_SIGNAL_SEGV
, 13}, /* GPF */
266 {TARGET_SIGNAL_BUS
, 17}, /* Alignment Check */
267 /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
269 {TARGET_SIGNAL_TERM
, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
270 {TARGET_SIGNAL_FPE
, 0x75},
271 {TARGET_SIGNAL_INT
, 0x79},
272 {TARGET_SIGNAL_QUIT
, 0x7a},
273 {TARGET_SIGNAL_ALRM
, 0x78}, /* triggers SIGTIMR */
274 {TARGET_SIGNAL_PROF
, 0x78},
275 {TARGET_SIGNAL_LAST
, -1}
279 go32_open (char *name ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
281 printf_unfiltered ("Done. Use the \"run\" command to run the program.\n");
285 go32_close (int quitting ATTRIBUTE_UNUSED
)
290 go32_attach (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
293 You cannot attach to a running program on this platform.\n\
294 Use the `run' command to run DJGPP programs.");
298 go32_detach (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
302 static int resume_is_step
;
303 static int resume_signal
= -1;
306 go32_resume (int pid ATTRIBUTE_UNUSED
, int step
, enum target_signal siggnal
)
310 resume_is_step
= step
;
312 if (siggnal
!= TARGET_SIGNAL_0
&& siggnal
!= TARGET_SIGNAL_TRAP
)
314 for (i
= 0, resume_signal
= -1;
315 excepn_map
[i
].gdb_sig
!= TARGET_SIGNAL_LAST
; i
++)
316 if (excepn_map
[i
].gdb_sig
== siggnal
)
318 resume_signal
= excepn_map
[i
].djgpp_excepno
;
321 if (resume_signal
== -1)
322 printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
323 target_signal_to_name (siggnal
));
327 static char child_cwd
[FILENAME_MAX
];
330 go32_wait (int pid ATTRIBUTE_UNUSED
, struct target_waitstatus
*status
)
333 unsigned char saved_opcode
;
334 unsigned long INT3_addr
= 0;
335 int stepping_over_INT
= 0;
337 a_tss
.tss_eflags
&= 0xfeff; /* reset the single-step flag (TF) */
340 /* If the next instruction is INT xx or INTO, we need to handle
341 them specially. Intel manuals say that these instructions
342 reset the single-step flag (a.k.a. TF). However, it seems
343 that, at least in the DPMI environment, and at least when
344 stepping over the DPMI interrupt 31h, the problem is having
345 TF set at all when INT 31h is executed: the debuggee either
346 crashes (and takes the system with it) or is killed by a
349 So we need to emulate single-step mode: we put an INT3 opcode
350 right after the INT xx instruction, let the debuggee run
351 until it hits INT3 and stops, then restore the original
352 instruction which we overwrote with the INT3 opcode, and back
353 up the debuggee's EIP to that instruction. */
354 read_child (a_tss
.tss_eip
, &saved_opcode
, 1);
355 if (saved_opcode
== 0xCD || saved_opcode
== 0xCE)
357 unsigned char INT3_opcode
= 0xCC;
360 = saved_opcode
== 0xCD ? a_tss
.tss_eip
+ 2 : a_tss
.tss_eip
+ 1;
361 stepping_over_INT
= 1;
362 read_child (INT3_addr
, &saved_opcode
, 1);
363 write_child (INT3_addr
, &INT3_opcode
, 1);
366 a_tss
.tss_eflags
|= 0x0100; /* normal instruction: set TF */
369 /* The special value FFFFh in tss_trap indicates to run_child that
370 tss_irqn holds a signal to be delivered to the debuggee. */
371 if (resume_signal
<= -1)
374 a_tss
.tss_irqn
= 0xff;
378 a_tss
.tss_trap
= 0xffff; /* run_child looks for this */
379 a_tss
.tss_irqn
= resume_signal
;
382 /* The child might change working directory behind our back. The
383 GDB users won't like the side effects of that when they work with
384 relative file names, and GDB might be confused by its current
385 directory not being in sync with the truth. So we always make a
386 point of changing back to where GDB thinks is its cwd, when we
387 return control to the debugger, but restore child's cwd before we
389 /* Initialize child_cwd, before the first call to run_child and not
390 in the initialization, so the child get also the changed directory
391 set with the gdb-command "cd ..." */
393 /* Initialize child's cwd with the current one. */
394 getcwd (child_cwd
, sizeof (child_cwd
));
398 #if __DJGPP_MINOR__ < 3
402 #if __DJGPP_MINOR__ < 3
406 /* Did we step over an INT xx instruction? */
407 if (stepping_over_INT
&& a_tss
.tss_eip
== INT3_addr
+ 1)
409 /* Restore the original opcode. */
410 a_tss
.tss_eip
--; /* EIP points *after* the INT3 instruction */
411 write_child (a_tss
.tss_eip
, &saved_opcode
, 1);
412 /* Simulate a TRAP exception. */
414 a_tss
.tss_eflags
|= 0x0100;
417 getcwd (child_cwd
, sizeof (child_cwd
)); /* in case it has changed */
418 chdir (current_directory
);
420 if (a_tss
.tss_irqn
== 0x21)
422 status
->kind
= TARGET_WAITKIND_EXITED
;
423 status
->value
.integer
= a_tss
.tss_eax
& 0xff;
427 status
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
428 status
->kind
= TARGET_WAITKIND_STOPPED
;
429 for (i
= 0; sig_map
[i
].go32_sig
!= -1; i
++)
431 if (a_tss
.tss_irqn
== sig_map
[i
].go32_sig
)
433 #if __DJGPP_MINOR__ < 3
434 if ((status
->value
.sig
= sig_map
[i
].gdb_sig
) !=
436 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
438 status
->value
.sig
= sig_map
[i
].gdb_sig
;
448 fetch_register (int regno
)
450 if (regno
< FP0_REGNUM
)
451 supply_register (regno
, (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
);
452 else if (regno
<= LAST_FPU_CTRL_REGNUM
)
453 i387_supply_register (regno
, (char *) &npx
);
455 internal_error (__FILE__
, __LINE__
,
456 "Invalid register no. %d in fetch_register.", regno
);
460 go32_fetch_registers (int regno
)
463 fetch_register (regno
);
466 for (regno
= 0; regno
< FP0_REGNUM
; regno
++)
467 fetch_register (regno
);
468 i387_supply_fsave ((char *) &npx
);
473 store_register (int regno
)
476 void *v
= (void *) ®isters
[REGISTER_BYTE (regno
)];
478 if (regno
< FP0_REGNUM
)
479 memcpy ((char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
,
480 v
, regno_mapping
[regno
].size
);
481 else if (regno
<= LAST_FPU_CTRL_REGNUM
)
482 i387_fill_fsave ((char *)&npx
, regno
);
484 internal_error (__FILE__
, __LINE__
,
485 "Invalid register no. %d in store_register.", regno
);
489 go32_store_registers (int regno
)
494 store_register (regno
);
497 for (r
= 0; r
< FP0_REGNUM
; r
++)
499 i387_fill_fsave ((char *) &npx
, -1);
504 go32_prepare_to_store (void)
509 go32_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
510 struct mem_attrib
*attrib ATTRIBUTE_UNUSED
,
511 struct target_ops
*target ATTRIBUTE_UNUSED
)
515 if (write_child (memaddr
, myaddr
, len
))
526 if (read_child (memaddr
, myaddr
, len
))
537 static cmdline_t child_cmd
; /* parsed child's command line kept here */
540 go32_files_info (struct target_ops
*target ATTRIBUTE_UNUSED
)
542 printf_unfiltered ("You are running a DJGPP V2 program.\n");
551 prog_has_started
= 0;
555 go32_kill_inferior (void)
557 redir_cmdline_delete (&child_cmd
);
560 unpush_target (&go32_ops
);
564 go32_create_inferior (char *exec_file
, char *args
, char **env
)
568 char **env_save
= environ
;
570 /* If no exec file handed to us, get it from the exec-file command -- with
571 a good, common error message if none is specified. */
573 exec_file
= get_exec_file (1);
575 if (prog_has_started
)
578 go32_kill_inferior ();
583 /* Initialize child's cwd as empty to be initialized when starting
587 /* Init command line storage. */
588 if (redir_debug_init (&child_cmd
) == -1)
589 internal_error (__FILE__
, __LINE__
,
590 "Cannot allocate redirection storage: not enough memory.\n");
592 /* Parse the command line and create redirections. */
593 if (strpbrk (args
, "<>"))
595 if (redir_cmdline_parse (args
, &child_cmd
) == 0)
596 args
= child_cmd
.command
;
598 error ("Syntax error in command line.");
601 child_cmd
.command
= xstrdup (args
);
603 cmdline
= (char *) alloca (strlen (args
) + 4);
604 cmdline
[0] = strlen (args
);
605 strcpy (cmdline
+ 1, args
);
606 cmdline
[strlen (args
) + 1] = 13;
610 if (v2loadimage (exec_file
, cmdline
, start_state
))
613 printf_unfiltered ("Load failed for image %s\n", exec_file
);
618 edi_init (start_state
);
619 #if __DJGPP_MINOR__ < 3
623 inferior_pid
= SOME_PID
;
624 push_target (&go32_ops
);
625 clear_proceed_status ();
626 insert_breakpoints ();
627 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
628 prog_has_started
= 1;
632 go32_mourn_inferior (void)
634 /* We need to make sure all the breakpoint enable bits in the DR7
635 register are reset when the inferior exits. Otherwise, if they
636 rerun the inferior, the uncleared bits may cause random SIGTRAPs,
637 failure to set more watchpoints, and other calamities. It would
638 be nice if GDB itself would take care to remove all breakpoints
639 at all times, but it doesn't, probably under an assumption that
640 the OS cleans up when the debuggee exits. */
641 i386_cleanup_dregs ();
642 go32_kill_inferior ();
643 generic_mourn_inferior ();
652 /* Hardware watchpoint support. */
654 #define D_REGS edi.dr
655 #define CONTROL D_REGS[7]
656 #define STATUS D_REGS[6]
658 /* Pass the address ADDR to the inferior in the I'th debug register.
659 Here we just store the address in D_REGS, the watchpoint will be
660 actually set up when go32_wait runs the debuggee. */
662 go32_set_dr (int i
, CORE_ADDR addr
)
667 /* Pass the value VAL to the inferior in the DR7 debug control
668 register. Here we just store the address in D_REGS, the watchpoint
669 will be actually set up when go32_wait runs the debuggee. */
671 go32_set_dr7 (unsigned val
)
676 /* Get the value of the DR6 debug status register from the inferior.
677 Here we just return the value stored in D_REGS, as we've got it
678 from the last go32_wait call. */
685 /* Put the device open on handle FD into either raw or cooked
686 mode, return 1 if it was in raw mode, zero otherwise. */
689 device_mode (int fd
, int raw_p
)
691 int oldmode
, newmode
;
696 __dpmi_int (0x21, ®s
);
697 if (regs
.x
.flags
& 1)
699 newmode
= oldmode
= regs
.x
.dx
;
706 if (oldmode
& 0x80) /* Only for character dev */
710 regs
.x
.dx
= newmode
& 0xff; /* Force upper byte zero, else it fails */
711 __dpmi_int (0x21, ®s
);
712 if (regs
.x
.flags
& 1)
715 return (oldmode
& 0x20) == 0x20;
719 static int inf_mode_valid
= 0;
720 static int inf_terminal_mode
;
722 /* This semaphore is needed because, amazingly enough, GDB calls
723 target.to_terminal_ours more than once after the inferior stops.
724 But we need the information from the first call only, since the
725 second call will always see GDB's own cooked terminal. */
726 static int terminal_is_ours
= 1;
729 go32_terminal_init (void)
731 inf_mode_valid
= 0; /* reinitialize, in case they are restarting child */
732 terminal_is_ours
= 1;
736 go32_terminal_info (char *args ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
738 printf_unfiltered ("Inferior's terminal is in %s mode.\n",
740 ? "default" : inf_terminal_mode
? "raw" : "cooked");
742 #if __DJGPP_MINOR__ > 2
743 if (child_cmd
.redirection
)
747 for (i
= 0; i
< DBG_HANDLES
; i
++)
749 if (child_cmd
.redirection
[i
]->file_name
)
750 printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
751 i
, child_cmd
.redirection
[i
]->file_name
);
752 else if (_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) == -1)
754 ("\tFile handle %d appears to be closed by inferior.\n", i
);
755 /* Mask off the raw/cooked bit when comparing device info words. */
756 else if ((_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) & 0xdf)
757 != (_get_dev_info (i
) & 0xdf))
759 ("\tFile handle %d appears to be redirected by inferior.\n", i
);
766 go32_terminal_inferior (void)
768 /* Redirect standard handles as child wants them. */
770 if (redir_to_child (&child_cmd
) == -1)
772 redir_to_debugger (&child_cmd
);
773 error ("Cannot redirect standard handles for program: %s.",
776 /* set the console device of the inferior to whatever mode
777 (raw or cooked) we found it last time */
778 if (terminal_is_ours
)
781 device_mode (0, inf_terminal_mode
);
782 terminal_is_ours
= 0;
787 go32_terminal_ours (void)
789 /* Switch to cooked mode on the gdb terminal and save the inferior
790 terminal mode to be restored when it is resumed */
791 if (!terminal_is_ours
)
793 inf_terminal_mode
= device_mode (0, 0);
794 if (inf_terminal_mode
!= -1)
797 /* If device_mode returned -1, we don't know what happens with
798 handle 0 anymore, so make the info invalid. */
800 terminal_is_ours
= 1;
802 /* Restore debugger's standard handles. */
804 if (redir_to_debugger (&child_cmd
) == -1)
806 redir_to_child (&child_cmd
);
807 error ("Cannot redirect standard handles for debugger: %s.",
816 go32_ops
.to_shortname
= "djgpp";
817 go32_ops
.to_longname
= "djgpp target process";
819 "Program loaded by djgpp, when gdb is used as an external debugger";
820 go32_ops
.to_open
= go32_open
;
821 go32_ops
.to_close
= go32_close
;
822 go32_ops
.to_attach
= go32_attach
;
823 go32_ops
.to_detach
= go32_detach
;
824 go32_ops
.to_resume
= go32_resume
;
825 go32_ops
.to_wait
= go32_wait
;
826 go32_ops
.to_fetch_registers
= go32_fetch_registers
;
827 go32_ops
.to_store_registers
= go32_store_registers
;
828 go32_ops
.to_prepare_to_store
= go32_prepare_to_store
;
829 go32_ops
.to_xfer_memory
= go32_xfer_memory
;
830 go32_ops
.to_files_info
= go32_files_info
;
831 go32_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
832 go32_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
833 go32_ops
.to_terminal_init
= go32_terminal_init
;
834 go32_ops
.to_terminal_inferior
= go32_terminal_inferior
;
835 go32_ops
.to_terminal_ours_for_output
= go32_terminal_ours
;
836 go32_ops
.to_terminal_ours
= go32_terminal_ours
;
837 go32_ops
.to_terminal_info
= go32_terminal_info
;
838 go32_ops
.to_kill
= go32_kill_inferior
;
839 go32_ops
.to_create_inferior
= go32_create_inferior
;
840 go32_ops
.to_mourn_inferior
= go32_mourn_inferior
;
841 go32_ops
.to_can_run
= go32_can_run
;
842 go32_ops
.to_stop
= go32_stop
;
843 go32_ops
.to_stratum
= process_stratum
;
844 go32_ops
.to_has_all_memory
= 1;
845 go32_ops
.to_has_memory
= 1;
846 go32_ops
.to_has_stack
= 1;
847 go32_ops
.to_has_registers
= 1;
848 go32_ops
.to_has_execution
= 1;
849 go32_ops
.to_magic
= OPS_MAGIC
;
851 /* Initialize child's cwd as empty to be initialized when starting
855 /* Initialize child's command line storage. */
856 if (redir_debug_init (&child_cmd
) == -1)
857 internal_error (__FILE__
, __LINE__
,
858 "Cannot allocate redirection storage: not enough memory.\n");
860 /* We are always processing GCC-compiled programs. */
861 processing_gcc_compilation
= 2;
865 _initialize_go32_nat (void)
868 add_target (&go32_ops
);
881 tcsetpgrp (int fd
, pid_t pgid
)
883 if (isatty (fd
) && pgid
== SOME_PID
)
885 errno
= pgid
== SOME_PID
? ENOTTY
: ENOSYS
;