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;
170 go32_terminal_init (void);
172 go32_terminal_inferior (void);
174 go32_terminal_ours (void);
177 print_387_status (unsigned short status, struct env387 *ep)
184 bothstatus = ((status != 0) && (ep->status != 0));
188 printf_unfiltered ("u: ");
189 print_387_status_word (status);
195 printf_unfiltered ("e: ");
196 print_387_status_word (ep->status);
199 print_387_control_word (ep->control & 0xffff);
200 printf_unfiltered ("last exception: ");
201 printf_unfiltered ("opcode %s; ", local_hex_string (ep->opcode));
202 printf_unfiltered ("pc %s:", local_hex_string (ep->code_seg));
203 printf_unfiltered ("%s; ", local_hex_string (ep->eip));
204 printf_unfiltered ("operand %s", local_hex_string (ep->operand_seg));
205 printf_unfiltered (":%s\n", local_hex_string (ep->operand));
207 top = (ep->status >> 11) & 7;
209 printf_unfiltered ("regno tag msb lsb value\n");
210 for (fpreg = 0; fpreg < 8; fpreg++)
214 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
216 switch ((ep->tag >> (fpreg * 2)) & 3)
219 printf_unfiltered ("valid ");
222 printf_unfiltered ("zero ");
225 printf_unfiltered ("trap ");
228 printf_unfiltered ("empty ");
231 for (i = 0; i < 8; i++)
232 printf_unfiltered ("%02x", ep->regs[fpreg][i]);
234 REGISTER_CONVERT_TO_VIRTUAL (FP0_REGNUM + fpreg, builtin_type_long_double,
235 &ep->regs[fpreg], &val);
237 printf_unfiltered (" %LG\n", val);
242 i386_go32_float_info (void)
244 print_387_status (0, (struct env387 *) &npx);
247 #define r_ofs(x) ((int)(&(((TSS *)0)->x)))
265 r_ofs (tss_eflags), 4,
296 0, TARGET_SIGNAL_FPE,
297 1, TARGET_SIGNAL_TRAP,
298 2, TARGET_SIGNAL_UNKNOWN,
299 3, TARGET_SIGNAL_TRAP,
300 4, TARGET_SIGNAL_FPE,
301 5, TARGET_SIGNAL_SEGV,
302 6, TARGET_SIGNAL_ILL,
303 7, TARGET_SIGNAL_FPE,
304 8, TARGET_SIGNAL_SEGV,
305 9, TARGET_SIGNAL_SEGV,
306 10, TARGET_SIGNAL_BUS,
307 11, TARGET_SIGNAL_SEGV,
308 12, TARGET_SIGNAL_SEGV,
309 13, TARGET_SIGNAL_ABRT,
310 14, TARGET_SIGNAL_SEGV,
311 16, TARGET_SIGNAL_FPE,
312 31, TARGET_SIGNAL_ILL,
313 0x75, TARGET_SIGNAL_FPE,
314 0x79, TARGET_SIGNAL_INT,
315 0x1b, TARGET_SIGNAL_INT,
320 go32_open (char *name, int from_tty)
322 printf_unfiltered ("Use the `run' command to run go32 programs\n");
326 go32_close (int quitting)
331 go32_attach (char *args, int from_tty)
333 printf_unfiltered ("Use the `run' command to run go32 programs\n");
337 go32_detach (char *args, int from_tty)
341 static int resume_is_step;
344 go32_resume (int pid, int step, enum target_signal siggnal)
346 resume_is_step = step;
350 go32_wait (int pid, struct target_waitstatus *status)
355 a_tss.tss_eflags |= 0x0100;
357 a_tss.tss_eflags &= 0xfeff;
359 #if __DJGPP_MINOR__ < 3
363 #if __DJGPP_MINOR__ < 3
367 if (a_tss.tss_irqn == 0x21)
369 status->kind = TARGET_WAITKIND_EXITED;
370 status->value.integer = a_tss.tss_eax & 0xff;
374 status->value.sig = TARGET_SIGNAL_UNKNOWN;
375 status->kind = TARGET_WAITKIND_STOPPED;
376 for (i = 0; sig_map[i].go32_sig != -1; i++)
378 if (a_tss.tss_irqn == sig_map[i].go32_sig)
380 if ((status->value.sig = sig_map[i].gdb_sig) !=
382 status->kind = TARGET_WAITKIND_SIGNALLED;
391 go32_fetch_registers (int regno)
394 int end_reg = regno + 1; /* just one reg initially */
396 if (regno < 0) /* do the all registers */
398 regno = 0; /* start at first register */
399 /* # regs in table */
400 end_reg = sizeof (regno_mapping) / sizeof (regno_mapping[0]);
403 for (; regno < end_reg; regno++)
406 supply_register (regno,
407 (char *) &a_tss + regno_mapping[regno].tss_ofs);
409 supply_register (regno,
410 (char *) &npx.reg[regno_mapping[regno].tss_ofs]);
412 supply_register (regno,
413 (char *) &npx.reg + regno_mapping[regno].tss_ofs);
416 printf_unfiltered ("Invalid register in go32_fetch_register(%d)",
424 store_register (int regno)
427 void *v = (void *) ®isters[REGISTER_BYTE (regno)];
430 rp = (char *) &a_tss + regno_mapping[regno].tss_ofs;
432 rp = (char *) &npx.reg[regno_mapping[regno].tss_ofs];
434 rp = (char *) &npx + regno_mapping[regno].tss_ofs;
437 printf_unfiltered ("Invalid register in store_register(%d)", regno);
440 memcpy (rp, v, regno_mapping[regno].size);
444 go32_store_registers (int regno)
449 store_register (regno);
452 for (r = 0; r < sizeof (regno_mapping) / sizeof (regno_mapping[0]); r++)
458 go32_prepare_to_store (void)
463 go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
464 struct target_ops *target)
468 if (write_child (memaddr, myaddr, len))
479 if (read_child (memaddr, myaddr, len))
491 go32_files_info (struct target_ops *target)
493 printf_unfiltered ("You are running a DJGPP V2 program\n");
502 prog_has_started = 0;
506 go32_kill_inferior (void)
508 unpush_target (&go32_ops);
512 go32_create_inferior (char *exec_file, char *args, char **env)
516 char **env_save = environ;
518 if (prog_has_started)
521 go32_kill_inferior ();
524 cmdline = (char *) alloca (strlen (args) + 4);
525 cmdline[0] = strlen (args);
526 strcpy (cmdline + 1, args);
527 cmdline[strlen (args) + 1] = 13;
531 if (v2loadimage (exec_file, cmdline, start_state))
534 printf_unfiltered ("Load failed for image %s\n", exec_file);
539 edi_init (start_state);
541 inferior_pid = SOME_PID;
542 push_target (&go32_ops);
543 clear_proceed_status ();
544 insert_breakpoints ();
545 proceed ((CORE_ADDR) - 1, TARGET_SIGNAL_0, 0);
546 prog_has_started = 1;
550 go32_mourn_inferior (void)
552 go32_kill_inferior ();
553 generic_mourn_inferior ();
568 ignore2 (char *a, int b)
572 /* Hardware watchpoint support. */
576 #define DR_ENABLE_SIZE 2
577 #define DR_LOCAL_ENABLE_SHIFT 0
578 #define DR_GLOBAL_ENABLE_SHIFT 1
579 #define DR_LOCAL_SLOWDOWN 0x100
580 #define DR_GLOBAL_SLOWDOWN 0x200
581 #define DR_CONTROL_SHIFT 16
582 #define DR_CONTROL_SIZE 4
583 #define DR_RW_READ 0x3
584 #define DR_RW_WRITE 0x1
585 #define DR_CONTROL_MASK 0xf
586 #define DR_ENABLE_MASK 0x3
591 #define D_REGS edi.dr
592 #define CONTROL D_REGS[DR_CONTROL]
593 #define STATUS D_REGS[DR_STATUS]
595 #define IS_REG_FREE(index) \
596 (!(CONTROL & (3 << (DR_ENABLE_SIZE * index))))
598 #define LOCAL_ENABLE_REG(index) \
599 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * index)))
601 #define GLOBAL_ENABLE_REG(index) \
602 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * index)))
604 #define DISABLE_REG(index) \
605 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * index)))
607 #define SET_LOCAL_EXACT() \
608 (CONTROL |= DR_LOCAL_SLOWDOWN)
610 #define SET_GLOBAL_EXACT() \
611 (CONTROL |= DR_GLOBAL_SLOWDOWN)
613 #define SET_BREAK(index,address) \
615 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index));\
616 D_REGS[index] = address;\
619 #define SET_WATCH(index,address,rw,len) \
621 SET_BREAK(index,address);\
622 CONTROL |= (len | rw) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index);\
625 #define WATCH_HIT(index) \
627 (STATUS & (1 << index)) && \
628 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index)))\
631 #if 0 /* use debugging macro */
632 #define SHOW_DR(text) \
634 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
635 fprintf(stderr,"%08x %08x ",edi.dr[0],edi.dr[1]); \
636 fprintf(stderr,"%08x %08x ",edi.dr[2],edi.dr[3]); \
637 fprintf(stderr,"(%s)\n",#text); \
640 #define SHOW_DR(text) do {} while (0)
643 /* Insert a watchpoint. */
646 go32_insert_watchpoint (int pid, CORE_ADDR addr, int len, int rw)
648 int ret = go32_insert_aligned_watchpoint (pid, addr, addr, len, rw);
650 SHOW_DR (insert_watch);
655 go32_insert_aligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
659 int read_write_bits, len_bits;
661 /* Look for a free debug register. */
662 for (i = 0; i <= 3; i++)
668 /* No more debug registers! */
672 read_write_bits = ((rw & 1) ? DR_RW_READ : 0) | ((rw & 2) ? DR_RW_WRITE : 0);
679 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
685 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
689 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
691 SET_WATCH (i, addr, read_write_bits, len_bits);
692 LOCAL_ENABLE_REG (i);
697 go32_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
704 static int size_try_array[16] =
706 1, 1, 1, 1, /* trying size one */
707 2, 1, 2, 1, /* trying size two */
708 2, 1, 2, 1, /* trying size three */
709 4, 1, 2, 1 /* trying size four */
715 /* Four is the maximum length for 386. */
716 size = (len > 4) ? 3 : len - 1;
717 size = size_try_array[size * 4 + align];
718 rv = go32_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
721 go32_remove_watchpoint (pid, waddr, size);
730 /* Remove a watchpoint. */
733 go32_remove_watchpoint (int pid, CORE_ADDR addr, int len)
737 for (i = 0; i <= 3; i++)
739 if (D_REGS[i] == addr)
744 SHOW_DR (remove_watch);
749 /* Check if stopped by a watchpoint. */
752 go32_stopped_by_watchpoint (int pid)
757 status = edi.dr[DR_STATUS];
758 SHOW_DR (stopped_by);
759 for (i = 0; i <= 3; i++)
767 /* this is a hack to GDB. If we stopped at a hardware breakpoint,
768 the stop_pc must incremented by DECR_PC_AFTER_BREAK. I tried everything
769 with the DECR_PC_AFTER_HW_BREAK, but nothing works. */
770 /* This is probably fixed by jtc's recent patch -sts 2/19/99 */
772 stop_pc += DECR_PC_AFTER_BREAK;
778 /* Remove a breakpoint. */
781 go32_remove_hw_breakpoint (CORE_ADDR addr, CORE_ADDR shadow)
784 for (i = 0; i <= 3; i++)
786 if (D_REGS[i] == addr)
796 go32_insert_hw_breakpoint (CORE_ADDR addr, CORE_ADDR shadow)
799 int read_write_bits, len_bits;
800 int free_debug_register;
803 /* Look for a free debug register. */
804 for (i = 0; i <= 3; i++)
810 /* No more debug registers! */
815 LOCAL_ENABLE_REG (i);
821 static int inf_flags_valid = 0;
822 static int inf_in_flag;
823 static int inf_out_flag;
826 go32_terminal_init (void)
828 /* Save the filemodes for stdin/stout */
829 inf_in_flag = setmode(0, 0);
830 setmode(0, inf_in_flag);
831 inf_out_flag = setmode(1, 0);
832 setmode(1, inf_out_flag);
837 go32_terminal_inferior (void)
839 /* set the filemodes for stdin/stdout of the inferior */
842 setmode(0, inf_in_flag);
843 setmode(1, inf_out_flag);
848 go32_terminal_ours (void)
850 /* Switch to text mode on stdin/stdout always on the gdb terminal and
851 save the inferior modes to be restored later */
852 inf_in_flag = setmode(0, O_TEXT);
853 inf_out_flag = setmode(1, O_TEXT);
859 go32_ops.to_shortname = "djgpp";
860 go32_ops.to_longname = "djgpp target process";
862 "Program loaded by djgpp, when gdb is used as an external debugger";
863 go32_ops.to_open = go32_open;
864 go32_ops.to_close = go32_close;
865 go32_ops.to_detach = go32_detach;
866 go32_ops.to_resume = go32_resume;
867 go32_ops.to_wait = go32_wait;
868 go32_ops.to_fetch_registers = go32_fetch_registers;
869 go32_ops.to_store_registers = go32_store_registers;
870 go32_ops.to_prepare_to_store = go32_prepare_to_store;
871 go32_ops.to_xfer_memory = go32_xfer_memory;
872 go32_ops.to_files_info = go32_files_info;
873 go32_ops.to_insert_breakpoint = memory_insert_breakpoint;
874 go32_ops.to_remove_breakpoint = memory_remove_breakpoint;
875 go32_ops.to_terminal_init = go32_terminal_init;
876 go32_ops.to_terminal_inferior = go32_terminal_inferior;
877 go32_ops.to_terminal_ours_for_output = ignore;
878 go32_ops.to_terminal_ours = go32_terminal_ours;
879 go32_ops.to_terminal_info = ignore2;
880 go32_ops.to_kill = go32_kill_inferior;
881 go32_ops.to_create_inferior = go32_create_inferior;
882 go32_ops.to_mourn_inferior = go32_mourn_inferior;
883 go32_ops.to_can_run = go32_can_run;
884 go32_ops.to_stop = go32_stop;
885 go32_ops.to_stratum = process_stratum;
886 go32_ops.to_has_all_memory = 1;
887 go32_ops.to_has_memory = 1;
888 go32_ops.to_has_stack = 1;
889 go32_ops.to_has_registers = 1;
890 go32_ops.to_has_execution = 1;
891 go32_ops.to_magic = OPS_MAGIC;
895 _initialize_go32_nat (void)
898 add_target (&go32_ops);