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,
20 Boston, MA 02111-1307, USA. */
25 #include "frame.h" /* required by inferior.h */
31 #include "floatformat.h"
33 #include <stdio.h> /* required for __DJGPP_MINOR__ */
37 #include <debug/v2load.h>
38 #include <debug/dbgcom.h>
40 #if __DJGPP_MINOR__ < 3
41 /* This code will be provided from DJGPP 2.03 on. Until then I code it
49 unsigned short exponent
:15;
50 unsigned short sign
:1;
69 static void save_npx (void); /* Save the FPU of the debugged program */
70 static void load_npx (void); /* Restore the FPU of the debugged program */
72 /* ------------------------------------------------------------------------- */
73 /* Store the contents of the NPX in the global variable `npx'. */
101 /* ------------------------------------------------------------------------- */
102 /* Reload the contents of the NPX from the global variable `npx'. */
107 asm ("frstor %0":"=m" (npx
));
109 #endif /* __DJGPP_MINOR < 3 */
111 extern void _initialize_go32_nat (void);
115 unsigned short control
;
117 unsigned short status
;
122 unsigned short code_seg
;
123 unsigned short opcode
;
124 unsigned long operand
;
125 unsigned short operand_seg
;
127 unsigned char regs
[8][10];
130 extern char **environ
;
134 static int prog_has_started
= 0;
135 static void print_387_status (unsigned short status
, struct env387
*ep
);
136 static void go32_open (char *name
, int from_tty
);
137 static void go32_close (int quitting
);
138 static void go32_attach (char *args
, int from_tty
);
139 static void go32_detach (char *args
, int from_tty
);
140 static void go32_resume (int pid
, int step
, enum target_signal siggnal
);
141 static int go32_wait (int pid
, struct target_waitstatus
*status
);
142 static void go32_fetch_registers (int regno
);
143 static void store_register (int regno
);
144 static void go32_store_registers (int regno
);
145 static void go32_prepare_to_store (void);
146 static int go32_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
,
147 int write
, struct target_ops
*target
);
148 static void go32_files_info (struct target_ops
*target
);
149 static void go32_stop (void);
150 static void go32_kill_inferior (void);
151 static void go32_create_inferior (char *exec_file
, char *args
, char **env
);
152 static void go32_mourn_inferior (void);
153 static int go32_can_run (void);
154 static void ignore (void);
155 static void ignore2 (char *a
, int b
);
156 static int go32_insert_aligned_watchpoint (int pid
, CORE_ADDR waddr
,
157 CORE_ADDR addr
, int len
, int rw
);
158 static int go32_insert_nonaligned_watchpoint (int pid
, CORE_ADDR waddr
,
159 CORE_ADDR addr
, int len
, int rw
);
161 static struct target_ops go32_ops
;
162 static void go32_terminal_init (void);
163 static void go32_terminal_inferior (void);
164 static void go32_terminal_ours (void);
167 print_387_status (unsigned short status
, struct env387
*ep
)
174 bothstatus
= ((status
!= 0) && (ep
->status
!= 0));
178 printf_unfiltered ("u: ");
179 print_387_status_word (status
);
185 printf_unfiltered ("e: ");
186 print_387_status_word (ep
->status
);
189 print_387_control_word (ep
->control
& 0xffff);
190 printf_unfiltered ("last exception: ");
191 printf_unfiltered ("opcode %s; ", local_hex_string (ep
->opcode
));
192 printf_unfiltered ("pc %s:", local_hex_string (ep
->code_seg
));
193 printf_unfiltered ("%s; ", local_hex_string (ep
->eip
));
194 printf_unfiltered ("operand %s", local_hex_string (ep
->operand_seg
));
195 printf_unfiltered (":%s\n", local_hex_string (ep
->operand
));
197 top
= (ep
->status
>> 11) & 7;
199 printf_unfiltered ("regno tag msb lsb value\n");
200 for (fpreg
= 0; fpreg
< 8; fpreg
++)
204 printf_unfiltered ("%s %d: ", fpreg
== top
? "=>" : " ", fpreg
);
206 switch ((ep
->tag
>> (fpreg
* 2)) & 3)
209 printf_unfiltered ("valid ");
212 printf_unfiltered ("zero ");
215 printf_unfiltered ("trap ");
218 printf_unfiltered ("empty ");
221 for (i
= 0; i
< 8; i
++)
222 printf_unfiltered ("%02x", ep
->regs
[fpreg
][i
]);
224 REGISTER_CONVERT_TO_VIRTUAL (FP0_REGNUM
+ fpreg
, builtin_type_long_double
,
225 &ep
->regs
[fpreg
], &val
);
227 printf_unfiltered (" %LG\n", val
);
232 i386_go32_float_info (void)
234 print_387_status (0, (struct env387
*) &npx
);
237 #define r_ofs(x) ((int)(&(((TSS *)0)->x)))
255 r_ofs (tss_eflags
), 4,
286 0, TARGET_SIGNAL_FPE
,
287 1, TARGET_SIGNAL_TRAP
,
288 2, TARGET_SIGNAL_UNKNOWN
,
289 3, TARGET_SIGNAL_TRAP
,
290 4, TARGET_SIGNAL_FPE
,
291 5, TARGET_SIGNAL_SEGV
,
292 6, TARGET_SIGNAL_ILL
,
293 7, TARGET_SIGNAL_FPE
,
294 8, TARGET_SIGNAL_SEGV
,
295 9, TARGET_SIGNAL_SEGV
,
296 10, TARGET_SIGNAL_BUS
,
297 11, TARGET_SIGNAL_SEGV
,
298 12, TARGET_SIGNAL_SEGV
,
299 13, TARGET_SIGNAL_ABRT
,
300 14, TARGET_SIGNAL_SEGV
,
301 16, TARGET_SIGNAL_FPE
,
302 31, TARGET_SIGNAL_ILL
,
303 0x75, TARGET_SIGNAL_FPE
,
304 0x79, TARGET_SIGNAL_INT
,
305 0x1b, TARGET_SIGNAL_INT
,
310 go32_open (char *name
, int from_tty
)
312 printf_unfiltered ("Use the `run' command to run go32 programs\n");
316 go32_close (int quitting
)
321 go32_attach (char *args
, int from_tty
)
323 printf_unfiltered ("Use the `run' command to run go32 programs\n");
327 go32_detach (char *args
, int from_tty
)
331 static int resume_is_step
;
334 go32_resume (int pid
, int step
, enum target_signal siggnal
)
336 resume_is_step
= step
;
340 go32_wait (int pid
, struct target_waitstatus
*status
)
345 a_tss
.tss_eflags
|= 0x0100;
347 a_tss
.tss_eflags
&= 0xfeff;
349 #if __DJGPP_MINOR__ < 3
353 #if __DJGPP_MINOR__ < 3
357 if (a_tss
.tss_irqn
== 0x21)
359 status
->kind
= TARGET_WAITKIND_EXITED
;
360 status
->value
.integer
= a_tss
.tss_eax
& 0xff;
364 status
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
365 status
->kind
= TARGET_WAITKIND_STOPPED
;
366 for (i
= 0; sig_map
[i
].go32_sig
!= -1; i
++)
368 if (a_tss
.tss_irqn
== sig_map
[i
].go32_sig
)
370 if ((status
->value
.sig
= sig_map
[i
].gdb_sig
) !=
372 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
381 go32_fetch_registers (int regno
)
384 int end_reg
= regno
+ 1; /* just one reg initially */
386 if (regno
< 0) /* do the all registers */
388 regno
= 0; /* start at first register */
389 /* # regs in table */
390 end_reg
= sizeof (regno_mapping
) / sizeof (regno_mapping
[0]);
393 for (; regno
< end_reg
; regno
++)
396 supply_register (regno
,
397 (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
);
399 supply_register (regno
,
400 (char *) &npx
.reg
[regno_mapping
[regno
].tss_ofs
]);
402 supply_register (regno
,
403 (char *) &npx
.reg
+ regno_mapping
[regno
].tss_ofs
);
406 printf_unfiltered ("Invalid register in go32_fetch_register(%d)",
414 store_register (int regno
)
417 void *v
= (void *) ®isters
[REGISTER_BYTE (regno
)];
420 rp
= (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
;
422 rp
= (char *) &npx
.reg
[regno_mapping
[regno
].tss_ofs
];
424 rp
= (char *) &npx
+ regno_mapping
[regno
].tss_ofs
;
427 printf_unfiltered ("Invalid register in store_register(%d)", regno
);
430 memcpy (rp
, v
, regno_mapping
[regno
].size
);
434 go32_store_registers (int regno
)
439 store_register (regno
);
442 for (r
= 0; r
< sizeof (regno_mapping
) / sizeof (regno_mapping
[0]); r
++)
448 go32_prepare_to_store (void)
453 go32_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
454 struct target_ops
*target
)
458 if (write_child (memaddr
, myaddr
, len
))
469 if (read_child (memaddr
, myaddr
, len
))
481 go32_files_info (struct target_ops
*target
)
483 printf_unfiltered ("You are running a DJGPP V2 program\n");
492 prog_has_started
= 0;
496 go32_kill_inferior (void)
498 unpush_target (&go32_ops
);
502 go32_create_inferior (char *exec_file
, char *args
, char **env
)
506 char **env_save
= environ
;
508 if (prog_has_started
)
511 go32_kill_inferior ();
514 cmdline
= (char *) alloca (strlen (args
) + 4);
515 cmdline
[0] = strlen (args
);
516 strcpy (cmdline
+ 1, args
);
517 cmdline
[strlen (args
) + 1] = 13;
521 if (v2loadimage (exec_file
, cmdline
, start_state
))
524 printf_unfiltered ("Load failed for image %s\n", exec_file
);
529 edi_init (start_state
);
531 inferior_pid
= SOME_PID
;
532 push_target (&go32_ops
);
533 clear_proceed_status ();
534 insert_breakpoints ();
535 proceed ((CORE_ADDR
) - 1, TARGET_SIGNAL_0
, 0);
536 prog_has_started
= 1;
540 go32_mourn_inferior (void)
542 go32_kill_inferior ();
543 generic_mourn_inferior ();
558 ignore2 (char *a
, int b
)
562 /* Hardware watchpoint support. */
566 #define DR_ENABLE_SIZE 2
567 #define DR_LOCAL_ENABLE_SHIFT 0
568 #define DR_GLOBAL_ENABLE_SHIFT 1
569 #define DR_LOCAL_SLOWDOWN 0x100
570 #define DR_GLOBAL_SLOWDOWN 0x200
571 #define DR_CONTROL_SHIFT 16
572 #define DR_CONTROL_SIZE 4
573 #define DR_RW_READ 0x3
574 #define DR_RW_WRITE 0x1
575 #define DR_CONTROL_MASK 0xf
576 #define DR_ENABLE_MASK 0x3
581 #define D_REGS edi.dr
582 #define CONTROL D_REGS[DR_CONTROL]
583 #define STATUS D_REGS[DR_STATUS]
585 #define IS_REG_FREE(index) \
586 (!(CONTROL & (3 << (DR_ENABLE_SIZE * index))))
588 #define LOCAL_ENABLE_REG(index) \
589 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * index)))
591 #define GLOBAL_ENABLE_REG(index) \
592 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * index)))
594 #define DISABLE_REG(index) \
595 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * index)))
597 #define SET_LOCAL_EXACT() \
598 (CONTROL |= DR_LOCAL_SLOWDOWN)
600 #define SET_GLOBAL_EXACT() \
601 (CONTROL |= DR_GLOBAL_SLOWDOWN)
603 #define SET_BREAK(index,address) \
605 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index));\
606 D_REGS[index] = address;\
609 #define SET_WATCH(index,address,rw,len) \
611 SET_BREAK(index,address);\
612 CONTROL |= (len | rw) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index);\
615 #define WATCH_HIT(index) \
617 (STATUS & (1 << index)) && \
618 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index)))\
621 #if 0 /* use debugging macro */
622 #define SHOW_DR(text) \
624 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
625 fprintf(stderr,"%08x %08x ",edi.dr[0],edi.dr[1]); \
626 fprintf(stderr,"%08x %08x ",edi.dr[2],edi.dr[3]); \
627 fprintf(stderr,"(%s)\n",#text); \
630 #define SHOW_DR(text) do {} while (0)
633 /* Insert a watchpoint. */
636 go32_insert_watchpoint (int pid
, CORE_ADDR addr
, int len
, int rw
)
638 int ret
= go32_insert_aligned_watchpoint (pid
, addr
, addr
, len
, rw
);
640 SHOW_DR (insert_watch
);
645 go32_insert_aligned_watchpoint (int pid
, CORE_ADDR waddr
, CORE_ADDR addr
,
649 int read_write_bits
, len_bits
;
651 /* Look for a free debug register. */
652 for (i
= 0; i
<= 3; i
++)
658 /* No more debug registers! */
662 read_write_bits
= ((rw
& 1) ? DR_RW_READ
: 0) | ((rw
& 2) ? DR_RW_WRITE
: 0);
669 return go32_insert_nonaligned_watchpoint (pid
, waddr
, addr
, len
, rw
);
675 return go32_insert_nonaligned_watchpoint (pid
, waddr
, addr
, len
, rw
);
679 return go32_insert_nonaligned_watchpoint (pid
, waddr
, addr
, len
, rw
);
681 SET_WATCH (i
, addr
, read_write_bits
, len_bits
);
682 LOCAL_ENABLE_REG (i
);
687 go32_insert_nonaligned_watchpoint (int pid
, CORE_ADDR waddr
, CORE_ADDR addr
,
694 static int size_try_array
[16] =
696 1, 1, 1, 1, /* trying size one */
697 2, 1, 2, 1, /* trying size two */
698 2, 1, 2, 1, /* trying size three */
699 4, 1, 2, 1 /* trying size four */
705 /* Four is the maximum length for 386. */
706 size
= (len
> 4) ? 3 : len
- 1;
707 size
= size_try_array
[size
* 4 + align
];
708 rv
= go32_insert_aligned_watchpoint (pid
, waddr
, addr
, size
, rw
);
711 go32_remove_watchpoint (pid
, waddr
, size
);
720 /* Remove a watchpoint. */
723 go32_remove_watchpoint (int pid
, CORE_ADDR addr
, int len
)
727 for (i
= 0; i
<= 3; i
++)
729 if (D_REGS
[i
] == addr
)
734 SHOW_DR (remove_watch
);
739 /* Check if stopped by a watchpoint. */
742 go32_stopped_by_watchpoint (int pid
)
747 status
= edi
.dr
[DR_STATUS
];
748 SHOW_DR (stopped_by
);
749 for (i
= 0; i
<= 3; i
++)
757 /* this is a hack to GDB. If we stopped at a hardware breakpoint,
758 the stop_pc must incremented by DECR_PC_AFTER_BREAK. I tried everything
759 with the DECR_PC_AFTER_HW_BREAK, but nothing works. */
760 /* This is probably fixed by jtc's recent patch -sts 2/19/99 */
762 stop_pc
+= DECR_PC_AFTER_BREAK
;
768 /* Remove a breakpoint. */
771 go32_remove_hw_breakpoint (CORE_ADDR addr
, CORE_ADDR shadow
)
774 for (i
= 0; i
<= 3; i
++)
776 if (D_REGS
[i
] == addr
)
786 go32_insert_hw_breakpoint (CORE_ADDR addr
, CORE_ADDR shadow
)
789 int read_write_bits
, len_bits
;
790 int free_debug_register
;
793 /* Look for a free debug register. */
794 for (i
= 0; i
<= 3; i
++)
800 /* No more debug registers! */
805 LOCAL_ENABLE_REG (i
);
811 static int inf_flags_valid
= 0;
812 static int inf_in_flag
;
813 static int inf_out_flag
;
816 go32_terminal_init (void)
818 /* Save the filemodes for stdin/stout */
819 inf_in_flag
= setmode (0, 0);
820 setmode (0, inf_in_flag
);
821 inf_out_flag
= setmode (1, 0);
822 setmode (1, inf_out_flag
);
827 go32_terminal_inferior (void)
829 /* set the filemodes for stdin/stdout of the inferior */
832 setmode (0, inf_in_flag
);
833 setmode (1, inf_out_flag
);
838 go32_terminal_ours (void)
840 /* Switch to text mode on stdin/stdout always on the gdb terminal and
841 save the inferior modes to be restored later */
842 inf_in_flag
= setmode (0, O_TEXT
);
843 inf_out_flag
= setmode (1, O_TEXT
);
849 go32_ops
.to_shortname
= "djgpp";
850 go32_ops
.to_longname
= "djgpp target process";
852 "Program loaded by djgpp, when gdb is used as an external debugger";
853 go32_ops
.to_open
= go32_open
;
854 go32_ops
.to_close
= go32_close
;
855 go32_ops
.to_detach
= go32_detach
;
856 go32_ops
.to_resume
= go32_resume
;
857 go32_ops
.to_wait
= go32_wait
;
858 go32_ops
.to_fetch_registers
= go32_fetch_registers
;
859 go32_ops
.to_store_registers
= go32_store_registers
;
860 go32_ops
.to_prepare_to_store
= go32_prepare_to_store
;
861 go32_ops
.to_xfer_memory
= go32_xfer_memory
;
862 go32_ops
.to_files_info
= go32_files_info
;
863 go32_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
864 go32_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
865 go32_ops
.to_terminal_init
= go32_terminal_init
;
866 go32_ops
.to_terminal_inferior
= go32_terminal_inferior
;
867 go32_ops
.to_terminal_ours_for_output
= ignore
;
868 go32_ops
.to_terminal_ours
= go32_terminal_ours
;
869 go32_ops
.to_terminal_info
= ignore2
;
870 go32_ops
.to_kill
= go32_kill_inferior
;
871 go32_ops
.to_create_inferior
= go32_create_inferior
;
872 go32_ops
.to_mourn_inferior
= go32_mourn_inferior
;
873 go32_ops
.to_can_run
= go32_can_run
;
874 go32_ops
.to_stop
= go32_stop
;
875 go32_ops
.to_stratum
= process_stratum
;
876 go32_ops
.to_has_all_memory
= 1;
877 go32_ops
.to_has_memory
= 1;
878 go32_ops
.to_has_stack
= 1;
879 go32_ops
.to_has_registers
= 1;
880 go32_ops
.to_has_execution
= 1;
881 go32_ops
.to_magic
= OPS_MAGIC
;
885 _initialize_go32_nat (void)
888 add_target (&go32_ops
);