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"
35 #include <debug/v2load.h>
36 #include <debug/dbgcom.h>
38 extern void _initialize_go32_nat (void);
42 unsigned short control;
44 unsigned short status;
49 unsigned short code_seg;
50 unsigned short opcode;
51 unsigned long operand;
52 unsigned short operand_seg;
54 unsigned char regs[8][10];
57 extern char **environ;
61 /* FIXME add decls of all static functions here */
63 static int prog_has_started = 0;
66 print_387_status (unsigned short status, struct env387 *ep)
73 bothstatus = ((status != 0) && (ep->status != 0));
77 printf_unfiltered ("u: ");
78 print_387_status_word (status);
84 printf_unfiltered ("e: ");
85 print_387_status_word (ep->status);
88 print_387_control_word (ep->control & 0xffff);
89 printf_unfiltered ("last exception: ");
90 printf_unfiltered ("opcode %s; ", local_hex_string (ep->opcode));
91 printf_unfiltered ("pc %s:", local_hex_string (ep->code_seg));
92 printf_unfiltered ("%s; ", local_hex_string (ep->eip));
93 printf_unfiltered ("operand %s", local_hex_string (ep->operand_seg));
94 printf_unfiltered (":%s\n", local_hex_string (ep->operand));
96 top = (ep->status >> 11) & 7;
98 printf_unfiltered ("regno tag msb lsb value\n");
99 for (fpreg = 0; fpreg < 8; fpreg++)
103 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
105 switch ((ep->tag >> (fpreg * 2)) & 3)
108 printf_unfiltered ("valid ");
111 printf_unfiltered ("zero ");
114 printf_unfiltered ("trap ");
117 printf_unfiltered ("empty ");
120 for (i = 0; i < 8; i++)
121 printf_unfiltered ("%02x", ep->regs[fpreg][i]);
123 REGISTER_CONVERT_TO_VIRTUAL (FP0_REGNUM + fpreg, builtin_type_long_double,
124 &ep->regs[fpreg], &val);
126 printf_unfiltered (" %LG\n", val);
131 i386_go32_float_info (void)
133 print_387_status (0, (struct env387 *) &npx);
136 #define r_ofs(x) ((int)(&(((TSS *)0)->x)))
154 r_ofs (tss_eflags), 4,
185 0, TARGET_SIGNAL_FPE,
186 1, TARGET_SIGNAL_TRAP,
187 2, TARGET_SIGNAL_UNKNOWN,
188 3, TARGET_SIGNAL_TRAP,
189 4, TARGET_SIGNAL_FPE,
190 5, TARGET_SIGNAL_SEGV,
191 6, TARGET_SIGNAL_ILL,
192 7, TARGET_SIGNAL_FPE,
193 8, TARGET_SIGNAL_SEGV,
194 9, TARGET_SIGNAL_SEGV,
195 10, TARGET_SIGNAL_BUS,
196 11, TARGET_SIGNAL_SEGV,
197 12, TARGET_SIGNAL_SEGV,
198 13, TARGET_SIGNAL_ABRT,
199 14, TARGET_SIGNAL_SEGV,
200 16, TARGET_SIGNAL_FPE,
201 31, TARGET_SIGNAL_ILL,
202 0x75, TARGET_SIGNAL_FPE,
203 0x79, TARGET_SIGNAL_INT,
204 0x1b, TARGET_SIGNAL_INT,
209 go32_open (char *name, int from_tty)
211 printf_unfiltered ("Use the `run' command to run go32 programs\n");
215 go32_close (int quitting)
220 go32_attach (char *args, int from_tty)
222 printf_unfiltered ("Use the `run' command to run go32 programs\n");
226 go32_detach (char *args, int from_tty)
230 static int resume_is_step;
233 go32_resume (int pid, int step, enum target_signal siggnal)
235 resume_is_step = step;
239 go32_wait (int pid, struct target_waitstatus *status)
244 a_tss.tss_eflags |= 0x0100;
246 a_tss.tss_eflags &= 0xfeff;
250 if (a_tss.tss_irqn == 0x21)
252 status->kind = TARGET_WAITKIND_EXITED;
253 status->value.integer = a_tss.tss_eax & 0xff;
257 status->value.sig = TARGET_SIGNAL_UNKNOWN;
258 status->kind = TARGET_WAITKIND_STOPPED;
259 for (i = 0; sig_map[i].go32_sig != -1; i++)
261 if (a_tss.tss_irqn == sig_map[i].go32_sig)
263 if ((status->value.sig = sig_map[i].gdb_sig) !=
265 status->kind = TARGET_WAITKIND_SIGNALLED;
274 go32_fetch_registers (int regno)
277 int end_reg = regno + 1; /* just one reg initially */
279 if (regno < 0) /* do the all registers */
281 regno = 0; /* start at first register */
282 /* # regs in table */
283 end_reg = sizeof (regno_mapping) / sizeof (regno_mapping[0]);
286 for (; regno < end_reg; regno++)
289 supply_register (regno,
290 (char *) &a_tss + regno_mapping[regno].tss_ofs);
292 supply_register (regno,
293 (char *) &npx.reg[regno_mapping[regno].tss_ofs]);
295 supply_register (regno,
296 (char *) &npx.reg + regno_mapping[regno].tss_ofs);
299 printf_unfiltered ("Invalid register in go32_fetch_register(%d)",
307 store_register (int regno)
310 void *v = (void *) ®isters[REGISTER_BYTE (regno)];
313 rp = (char *) &a_tss + regno_mapping[regno].tss_ofs;
315 rp = (char *) &npx.reg[regno_mapping[regno].tss_ofs];
317 rp = (char *) &npx + regno_mapping[regno].tss_ofs;
320 printf_unfiltered ("Invalid register in store_register(%d)", regno);
323 memcpy (rp, v, regno_mapping[regno].size);
327 go32_store_registers (int regno)
332 store_register (regno);
335 for (r = 0; r < sizeof (regno_mapping) / sizeof (regno_mapping[0]); r++)
341 go32_prepare_to_store (void)
346 go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
347 struct target_ops *target)
351 if (write_child (memaddr, myaddr, len))
362 if (read_child (memaddr, myaddr, len))
374 go32_files_info (struct target_ops *target)
376 printf_unfiltered ("You are running a DJGPP V2 program\n");
385 prog_has_started = 0;
389 go32_kill_inferior (void)
392 unpush_target (&go32_ops);
396 go32_create_inferior (char *exec_file, char *args, char **env)
400 char **env_save = environ;
402 if (prog_has_started)
404 go32_kill_inferior ();
407 cmdline = (char *) alloca (strlen (args) + 4);
408 cmdline[0] = strlen (args);
409 strcpy (cmdline + 1, args);
410 cmdline[strlen (args) + 1] = 13;
414 if (v2loadimage (exec_file, cmdline, start_state))
417 printf_unfiltered ("Load failed for image %s\n", exec_file);
422 edi_init (start_state);
424 inferior_pid = SOME_PID;
425 push_target (&go32_ops);
426 clear_proceed_status ();
427 insert_breakpoints ();
428 proceed ((CORE_ADDR) - 1, TARGET_SIGNAL_0, 0);
432 go32_mourn_inferior (void)
434 go32_kill_inferior ();
435 generic_mourn_inferior ();
450 ignore2 (char *a, int b)
454 /* Hardware watchpoint support. */
458 #define DR_ENABLE_SIZE 2
459 #define DR_LOCAL_ENABLE_SHIFT 0
460 #define DR_GLOBAL_ENABLE_SHIFT 1
461 #define DR_LOCAL_SLOWDOWN 0x100
462 #define DR_GLOBAL_SLOWDOWN 0x200
463 #define DR_CONTROL_SHIFT 16
464 #define DR_CONTROL_SIZE 4
465 #define DR_RW_READ 0x3
466 #define DR_RW_WRITE 0x1
467 #define DR_CONTROL_MASK 0xf
468 #define DR_ENABLE_MASK 0x3
473 #define D_REGS edi.dr
474 #define CONTROL D_REGS[DR_CONTROL]
475 #define STATUS D_REGS[DR_STATUS]
477 #define IS_REG_FREE(index) \
478 (!(CONTROL & (3 << (DR_ENABLE_SIZE * index))))
480 #define LOCAL_ENABLE_REG(index) \
481 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * index)))
483 #define GLOBAL_ENABLE_REG(index) \
484 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * index)))
486 #define DISABLE_REG(index) \
487 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * index)))
489 #define SET_LOCAL_EXACT() \
490 (CONTROL |= DR_LOCAL_SLOWDOWN)
492 #define SET_GLOBAL_EXACT() \
493 (CONTROL |= DR_GLOBAL_SLOWDOWN)
495 #define SET_BREAK(index,address) \
497 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index));\
498 D_REGS[index] = address;\
501 #define SET_WATCH(index,address,rw,len) \
503 SET_BREAK(index,address);\
504 CONTROL |= (len | rw) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index);\
507 #define WATCH_HIT(index) \
509 (STATUS & (1 << index)) && \
510 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * index)))\
513 #if 0 /* use debugging macro */
514 #define SHOW_DR(text) \
516 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
517 fprintf(stderr,"%08x %08x ",edi.dr[0],edi.dr[1]); \
518 fprintf(stderr,"%08x %08x ",edi.dr[2],edi.dr[3]); \
519 fprintf(stderr,"(%s)\n",#text); \
522 #define SHOW_DR(text) do {} while (0)
525 static int go32_insert_aligned_watchpoint (int pid, CORE_ADDR waddr,
526 CORE_ADDR addr, int len, int rw);
528 static int go32_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr,
529 CORE_ADDR addr, int len, int rw);
531 /* Insert a watchpoint. */
534 go32_insert_watchpoint (int pid, CORE_ADDR addr, int len, int rw)
536 int ret = go32_insert_aligned_watchpoint (pid, addr, addr, len, rw);
538 SHOW_DR (insert_watch);
543 go32_insert_aligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
547 int read_write_bits, len_bits;
549 /* Look for a free debug register. */
550 for (i = 0; i <= 3; i++)
556 /* No more debug registers! */
560 read_write_bits = ((rw & 1) ? DR_RW_READ : 0) | ((rw & 2) ? DR_RW_WRITE : 0);
567 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
573 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
577 return go32_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw);
579 SET_WATCH (i, addr, read_write_bits, len_bits);
580 LOCAL_ENABLE_REG (i);
585 go32_insert_nonaligned_watchpoint (int pid, CORE_ADDR waddr, CORE_ADDR addr,
592 static int size_try_array[16] =
594 1, 1, 1, 1, /* trying size one */
595 2, 1, 2, 1, /* trying size two */
596 2, 1, 2, 1, /* trying size three */
597 4, 1, 2, 1 /* trying size four */
603 /* Four is the maximum length for 386. */
604 size = (len > 4) ? 3 : len - 1;
605 size = size_try_array[size * 4 + align];
606 rv = go32_insert_aligned_watchpoint (pid, waddr, addr, size, rw);
609 go32_remove_watchpoint (pid, waddr, size);
618 /* Remove a watchpoint. */
621 go32_remove_watchpoint (int pid, CORE_ADDR addr, int len)
625 for (i = 0; i <= 3; i++)
627 if (D_REGS[i] == addr)
632 SHOW_DR (remove_watch);
637 /* Check if stopped by a watchpoint. */
640 go32_stopped_by_watchpoint (int pid)
645 status = edi.dr[DR_STATUS];
646 SHOW_DR (stopped_by);
647 for (i = 0; i <= 3; i++)
655 /* this is a hack to GDB. If we stopped at a hardware breakpoint,
656 the stop_pc must incremented by DECR_PC_AFTER_BREAK. I tried everything
657 with the DECR_PC_AFTER_HW_BREAK, but nothing works. */
658 /* This is probably fixed by jtc's recent patch -sts 2/19/99 */
660 stop_pc += DECR_PC_AFTER_BREAK;
666 /* Remove a breakpoint. */
669 go32_remove_hw_breakpoint (CORE_ADDR addr, CORE_ADDR shadow)
672 for (i = 0; i <= 3; i++)
674 if (D_REGS[i] == addr)
684 go32_insert_hw_breakpoint (CORE_ADDR addr, CORE_ADDR shadow)
687 int read_write_bits, len_bits;
688 int free_debug_register;
691 /* Look for a free debug register. */
692 for (i = 0; i <= 3; i++)
698 /* No more debug registers! */
703 LOCAL_ENABLE_REG (i);
709 static struct target_ops go32_ops;
714 go32_ops.to_shortname = "djgpp";
715 go32_ops.to_longname = "djgpp target process";
717 "Program loaded by djgpp, when gdb is used as an external debugger";
718 go32_ops.to_open = go32_open;
719 go32_ops.to_close = go32_close;
720 go32_ops.to_detach = go32_detach;
721 go32_ops.to_resume = go32_resume;
722 go32_ops.to_wait = go32_wait;
723 go32_ops.to_fetch_registers = go32_fetch_registers;
724 go32_ops.to_store_registers = go32_store_registers;
725 go32_ops.to_prepare_to_store = go32_prepare_to_store;
726 go32_ops.to_xfer_memory = go32_xfer_memory;
727 go32_ops.to_files_info = go32_files_info;
728 go32_ops.to_insert_breakpoint = memory_insert_breakpoint;
729 go32_ops.to_remove_breakpoint = memory_remove_breakpoint;
730 go32_ops.to_terminal_init = ignore;
731 go32_ops.to_terminal_inferior = ignore;
732 go32_ops.to_terminal_ours_for_output = ignore;
733 go32_ops.to_terminal_ours = ignore;
734 go32_ops.to_terminal_info = ignore2;
735 go32_ops.to_kill = go32_kill_inferior;
736 go32_ops.to_create_inferior = go32_create_inferior;
737 go32_ops.to_mourn_inferior = go32_mourn_inferior;
738 go32_ops.to_can_run = go32_can_run;
739 go32_ops.to_stop = go32_stop;
740 go32_ops.to_stratum = process_stratum;
741 go32_ops.to_has_all_memory = 1;
742 go32_ops.to_has_memory = 1;
743 go32_ops.to_has_stack = 1;
744 go32_ops.to_has_registers = 1;
745 go32_ops.to_has_execution = 1;
746 go32_ops.to_magic = OPS_MAGIC;
750 _initialize_go32_nat (void)
753 add_target (&go32_ops);