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, Boston, MA 02111-1307, USA. */
24 #include "frame.h" /* required by inferior.h */
30 #include "floatformat.h"
32 #include <stdio.h> /* required for __DJGPP_MINOR__ */
36 #include <debug/v2load.h>
37 #include <debug/dbgcom.h>
39 #if __DJGPP_MINOR__ < 3
40 /* This code will be provided from DJGPP 2.03 on. Until then I code it
47 unsigned short exponent:15;
48 unsigned short sign:1;
64 static void save_npx (void); /* Save the FPU of the debugged program */
65 static void load_npx (void); /* Restore the FPU of the debugged program */
67 /* ------------------------------------------------------------------------- */
68 /* Store the contents of the NPX in the global variable `npx'. */
88 /* ------------------------------------------------------------------------- */
89 /* Reload the contents of the NPX from the global variable `npx'. */
94 asm ("frstor %0" : "=m" (npx));
96 #endif /* __DJGPP_MINOR < 3 */
98 extern void _initialize_go32_nat (void);
102 unsigned short control;
104 unsigned short status;
109 unsigned short code_seg;
110 unsigned short opcode;
111 unsigned long operand;
112 unsigned short operand_seg;
114 unsigned char regs[8][10];
117 extern char **environ;
121 static int prog_has_started = 0;
123 print_387_status (unsigned short status, struct env387 *ep);
125 go32_open (char *name, int from_tty);
127 go32_close (int quitting);
129 go32_attach (char *args, int from_tty);
131 go32_detach (char *args, int from_tty);
133 go32_resume (int pid, int step, enum target_signal siggnal);
135 go32_wait (int pid, struct target_waitstatus *status);
137 go32_fetch_registers (int regno);
139 store_register (int regno);
141 go32_store_registers (int regno);
143 go32_prepare_to_store (void);
145 go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
146 struct target_ops *target);
148 go32_files_info (struct target_ops *target);
152 go32_kill_inferior (void);
154 go32_create_inferior (char *exec_file, char *args, char **env);
156 go32_mourn_inferior (void);
162 ignore2 (char *a, int b);
163 static int go32_insert_aligned_watchpoint (int pid, CORE_ADDR waddr,
164 CORE_ADDR addr, int len, int rw);
165 static int go32_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr,
166 CORE_ADDR addr, int len, int rw);
168 static struct target_ops go32_ops;
171 print_387_status (unsigned short status, struct env387 *ep)
178 bothstatus = ((status != 0) && (ep->status != 0));
182 printf_unfiltered ("u: ");
183 print_387_status_word (status);
189 printf_unfiltered ("e: ");
190 print_387_status_word (ep->status);
193 print_387_control_word (ep->control & 0xffff);
194 printf_unfiltered ("last exception: ");
195 printf_unfiltered ("opcode %s; ", local_hex_string (ep->opcode));
196 printf_unfiltered ("pc %s:", local_hex_string (ep->code_seg));
197 printf_unfiltered ("%s; ", local_hex_string (ep->eip));
198 printf_unfiltered ("operand %s", local_hex_string (ep->operand_seg));
199 printf_unfiltered (":%s\n", local_hex_string (ep->operand));
201 top = (ep->status >> 11) & 7;
203 printf_unfiltered ("regno tag msb lsb value\n");
204 for (fpreg = 0; fpreg < 8; fpreg++)
208 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
210 switch ((ep->tag >> (fpreg * 2)) & 3)
213 printf_unfiltered ("valid ");
216 printf_unfiltered ("zero ");
219 printf_unfiltered ("trap ");
222 printf_unfiltered ("empty ");
225 for (i = 0; i < 8; i++)
226 printf_unfiltered ("%02x", ep->regs[fpreg][i]);
228 REGISTER_CONVERT_TO_VIRTUAL (FP0_REGNUM + fpreg, builtin_type_long_double,
229 &ep->regs[fpreg], &val);
231 printf_unfiltered (" %LG\n", val);
236 i386_go32_float_info (void)
238 print_387_status (0, (struct env387 *) &npx);
241 #define r_ofs(x) ((int)(&(((TSS *)0)->x)))
259 r_ofs (tss_eflags), 4,
290 0, TARGET_SIGNAL_FPE,
291 1, TARGET_SIGNAL_TRAP,
292 2, TARGET_SIGNAL_UNKNOWN,
293 3, TARGET_SIGNAL_TRAP,
294 4, TARGET_SIGNAL_FPE,
295 5, TARGET_SIGNAL_SEGV,
296 6, TARGET_SIGNAL_ILL,
297 7, TARGET_SIGNAL_FPE,
298 8, TARGET_SIGNAL_SEGV,
299 9, TARGET_SIGNAL_SEGV,
300 10, TARGET_SIGNAL_BUS,
301 11, TARGET_SIGNAL_SEGV,
302 12, TARGET_SIGNAL_SEGV,
303 13, TARGET_SIGNAL_ABRT,
304 14, TARGET_SIGNAL_SEGV,
305 16, TARGET_SIGNAL_FPE,
306 31, TARGET_SIGNAL_ILL,
307 0x75, TARGET_SIGNAL_FPE,
308 0x79, TARGET_SIGNAL_INT,
309 0x1b, TARGET_SIGNAL_INT,
314 go32_open (char *name, int from_tty)
316 printf_unfiltered ("Use the `run' command to run go32 programs\n");
320 go32_close (int quitting)
325 go32_attach (char *args, int from_tty)
327 printf_unfiltered ("Use the `run' command to run go32 programs\n");
331 go32_detach (char *args, int from_tty)
335 static int resume_is_step;
338 go32_resume (int pid, int step, enum target_signal siggnal)
340 resume_is_step = step;
344 go32_wait (int pid, struct target_waitstatus *status)
349 a_tss.tss_eflags |= 0x0100;
351 a_tss.tss_eflags &= 0xfeff;
353 #if __DJGPP_MINOR__ < 3
357 #if __DJGPP_MINOR__ < 3
361 if (a_tss.tss_irqn == 0x21)
363 status->kind = TARGET_WAITKIND_EXITED;
364 status->value.integer = a_tss.tss_eax & 0xff;
368 status->value.sig = TARGET_SIGNAL_UNKNOWN;
369 status->kind = TARGET_WAITKIND_STOPPED;
370 for (i = 0; sig_map[i].go32_sig != -1; i++)
372 if (a_tss.tss_irqn == sig_map[i].go32_sig)
374 if ((status->value.sig = sig_map[i].gdb_sig) !=
376 status->kind = TARGET_WAITKIND_SIGNALLED;
385 go32_fetch_registers (int regno)
388 int end_reg = regno + 1; /* just one reg initially */
390 if (regno < 0) /* do the all registers */
392 regno = 0; /* start at first register */
393 /* # regs in table */
394 end_reg = sizeof (regno_mapping) / sizeof (regno_mapping[0]);
397 for (; regno < end_reg; regno++)
400 supply_register (regno,
401 (char *) &a_tss + regno_mapping[regno].tss_ofs);
403 supply_register (regno,
404 (char *) &npx.reg[regno_mapping[regno].tss_ofs]);
406 supply_register (regno,
407 (char *) &npx.reg + regno_mapping[regno].tss_ofs);
410 printf_unfiltered ("Invalid register in go32_fetch_register(%d)",
418 store_register (int regno)
421 void *v = (void *) ®isters[REGISTER_BYTE (regno)];
424 rp = (char *) &a_tss + regno_mapping[regno].tss_ofs;
426 rp = (char *) &npx.reg[regno_mapping[regno].tss_ofs];
428 rp = (char *) &npx + regno_mapping[regno].tss_ofs;
431 printf_unfiltered ("Invalid register in store_register(%d)", regno);
434 memcpy (rp, v, regno_mapping[regno].size);
438 go32_store_registers (int regno)
443 store_register (regno);
446 for (r = 0; r < sizeof (regno_mapping) / sizeof (regno_mapping[0]); r++)
452 go32_prepare_to_store (void)
457 go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
458 struct target_ops *target)
462 if (write_child (memaddr, myaddr, len))
473 if (read_child (memaddr, myaddr, len))
485 go32_files_info (struct target_ops *target)
487 printf_unfiltered ("You are running a DJGPP V2 program\n");
496 prog_has_started = 0;
500 go32_kill_inferior (void)
502 unpush_target (&go32_ops);
506 go32_create_inferior (char *exec_file, char *args, char **env)
510 char **env_save = environ;
512 if (prog_has_started)
515 go32_kill_inferior ();
518 cmdline = (char *) alloca (strlen (args) + 4);
519 cmdline[0] = strlen (args);
520 strcpy (cmdline + 1, args);
521 cmdline[strlen (args) + 1] = 13;
525 if (v2loadimage (exec_file, cmdline, start_state))
528 printf_unfiltered ("Load failed for image %s\n", exec_file);
533 edi_init (start_state);
535 inferior_pid = SOME_PID;
536 push_target (&go32_ops);
537 clear_proceed_status ();
538 insert_breakpoints ();
539 proceed ((CORE_ADDR) - 1, TARGET_SIGNAL_0, 0);
540 prog_has_started = 1;
544 go32_mourn_inferior (void)
546 go32_kill_inferior ();
547 generic_mourn_inferior ();
562 ignore2 (char *a, int b)
566 /* Hardware watchpoint support. */
570 #define DR_ENABLE_SIZE 2
571 #define DR_LOCAL_ENABLE_SHIFT 0
572 #define DR_GLOBAL_ENABLE_SHIFT 1
573 #define DR_LOCAL_SLOWDOWN 0x100
574 #define DR_GLOBAL_SLOWDOWN 0x200
575 #define DR_CONTROL_SHIFT 16
576 #define DR_CONTROL_SIZE 4
577 #define DR_RW_READ 0x3
578 #define DR_RW_WRITE 0x1
579 #define DR_CONTROL_MASK 0xf
580 #define DR_ENABLE_MASK 0x3
585 #define D_REGS edi.dr
586 #define CONTROL D_REGS[DR_CONTROL]
587 #define STATUS D_REGS[DR_STATUS]
589 #define IS_REG_FREE(index) \
590 (!(CONTROL & (3 << (DR_ENABLE_SIZE * index))))
592 #define LOCAL_ENABLE_REG(index) \
593 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * index)))
595 #define GLOBAL_ENABLE_REG(index) \
596 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * index)))
598 #define DISABLE_REG(index) \
599 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * index)))
601 #define SET_LOCAL_EXACT() \
602 (CONTROL |= DR_LOCAL_SLOWDOWN)
604 #define SET_GLOBAL_EXACT() \
605 (CONTROL |= DR_GLOBAL_SLOWDOWN)
607 #define SET_BREAK(index,address) \
609 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index));\
610 D_REGS[index] = address;\
613 #define SET_WATCH(index,address,rw,len) \
615 SET_BREAK(index,address);\
616 CONTROL |= (len | rw) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index);\
619 #define WATCH_HIT(index) \
621 (STATUS & (1 << index)) && \
622 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index)))\
625 #if 0 /* use debugging macro */
626 #define SHOW_DR(text) \
628 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
629 fprintf(stderr,"%08x %08x ",edi.dr[0],edi.dr[1]); \
630 fprintf(stderr,"%08x %08x ",edi.dr[2],edi.dr[3]); \
631 fprintf(stderr,"(%s)\n",#text); \
634 #define SHOW_DR(text) do {} while (0)
637 /* Insert a watchpoint. */
640 go32_insert_watchpoint (int pid, CORE_ADDR addr, int len, int rw)
642 int ret = go32_insert_aligned_watchpoint (pid, addr, addr, len, rw);
644 SHOW_DR (insert_watch);
649 go32_insert_aligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
653 int read_write_bits, len_bits;
655 /* Look for a free debug register. */
656 for (i = 0; i <= 3; i++)
662 /* No more debug registers! */
666 read_write_bits = ((rw & 1) ? DR_RW_READ : 0) | ((rw & 2) ? DR_RW_WRITE : 0);
673 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
679 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
683 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
685 SET_WATCH (i, addr, read_write_bits, len_bits);
686 LOCAL_ENABLE_REG (i);
691 go32_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
698 static int size_try_array[16] =
700 1, 1, 1, 1, /* trying size one */
701 2, 1, 2, 1, /* trying size two */
702 2, 1, 2, 1, /* trying size three */
703 4, 1, 2, 1 /* trying size four */
709 /* Four is the maximum length for 386. */
710 size = (len > 4) ? 3 : len - 1;
711 size = size_try_array[size * 4 + align];
712 rv = go32_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
715 go32_remove_watchpoint (pid, waddr, size);
724 /* Remove a watchpoint. */
727 go32_remove_watchpoint (int pid, CORE_ADDR addr, int len)
731 for (i = 0; i <= 3; i++)
733 if (D_REGS[i] == addr)
738 SHOW_DR (remove_watch);
743 /* Check if stopped by a watchpoint. */
746 go32_stopped_by_watchpoint (int pid)
751 status = edi.dr[DR_STATUS];
752 SHOW_DR (stopped_by);
753 for (i = 0; i <= 3; i++)
761 /* this is a hack to GDB. If we stopped at a hardware breakpoint,
762 the stop_pc must incremented by DECR_PC_AFTER_BREAK. I tried everything
763 with the DECR_PC_AFTER_HW_BREAK, but nothing works. */
764 /* This is probably fixed by jtc's recent patch -sts 2/19/99 */
766 stop_pc += DECR_PC_AFTER_BREAK;
772 /* Remove a breakpoint. */
775 go32_remove_hw_breakpoint (CORE_ADDR addr, CORE_ADDR shadow)
778 for (i = 0; i <= 3; i++)
780 if (D_REGS[i] == addr)
790 go32_insert_hw_breakpoint (CORE_ADDR addr, CORE_ADDR shadow)
793 int read_write_bits, len_bits;
794 int free_debug_register;
797 /* Look for a free debug register. */
798 for (i = 0; i <= 3; i++)
804 /* No more debug registers! */
809 LOCAL_ENABLE_REG (i);
818 go32_ops.to_shortname = "djgpp";
819 go32_ops.to_longname = "djgpp target process";
821 "Program loaded by djgpp, when gdb is used as an external debugger";
822 go32_ops.to_open = go32_open;
823 go32_ops.to_close = go32_close;
824 go32_ops.to_detach = go32_detach;
825 go32_ops.to_resume = go32_resume;
826 go32_ops.to_wait = go32_wait;
827 go32_ops.to_fetch_registers = go32_fetch_registers;
828 go32_ops.to_store_registers = go32_store_registers;
829 go32_ops.to_prepare_to_store = go32_prepare_to_store;
830 go32_ops.to_xfer_memory = go32_xfer_memory;
831 go32_ops.to_files_info = go32_files_info;
832 go32_ops.to_insert_breakpoint = memory_insert_breakpoint;
833 go32_ops.to_remove_breakpoint = memory_remove_breakpoint;
834 go32_ops.to_terminal_init = ignore;
835 go32_ops.to_terminal_inferior = ignore;
836 go32_ops.to_terminal_ours_for_output = ignore;
837 go32_ops.to_terminal_ours = ignore;
838 go32_ops.to_terminal_info = ignore2;
839 go32_ops.to_kill = go32_kill_inferior;
840 go32_ops.to_create_inferior = go32_create_inferior;
841 go32_ops.to_mourn_inferior = go32_mourn_inferior;
842 go32_ops.to_can_run = go32_can_run;
843 go32_ops.to_stop = go32_stop;
844 go32_ops.to_stratum = process_stratum;
845 go32_ops.to_has_all_memory = 1;
846 go32_ops.to_has_memory = 1;
847 go32_ops.to_has_stack = 1;
848 go32_ops.to_has_registers = 1;
849 go32_ops.to_has_execution = 1;
850 go32_ops.to_magic = OPS_MAGIC;
854 _initialize_go32_nat (void)
857 add_target (&go32_ops);