old-cross-binutils/gdb/go32-nat.c

905 lines
23 KiB
C
Raw Normal View History

1999-04-27 01:26:45 +00:00
/* Native debugging support for Intel x86 running DJGPP.
Copyright 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
1999-04-27 01:26:45 +00:00
Written by Robert Hoehne.
1999-07-07 20:19:36 +00:00
This file is part of GDB.
1999-04-27 01:26:45 +00:00
1999-07-07 20:19:36 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
1999-04-27 01:26:45 +00:00
1999-07-07 20:19:36 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
1999-04-27 01:26:45 +00:00
1999-07-07 20:19:36 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
1999-04-27 01:26:45 +00:00
#include <fcntl.h>
#include "defs.h"
#include "inferior.h"
#include "gdb_wait.h"
1999-04-27 01:26:45 +00:00
#include "gdbcore.h"
#include "command.h"
#include "floatformat.h"
#include "buildsym.h"
#include "i387-nat.h"
#include "value.h"
#include "regcache.h"
#include "gdb_string.h"
1999-04-27 01:26:45 +00:00
1999-07-07 20:19:36 +00:00
#include <stdio.h> /* required for __DJGPP_MINOR__ */
1999-04-27 01:26:45 +00:00
#include <stdlib.h>
1999-08-23 22:40:00 +00:00
#include <errno.h>
1999-09-22 03:28:34 +00:00
#include <unistd.h>
1999-08-23 22:40:00 +00:00
#include <io.h>
#include <dpmi.h>
1999-04-27 01:26:45 +00:00
#include <debug/v2load.h>
#include <debug/dbgcom.h>
1999-08-23 22:40:00 +00:00
#if __DJGPP_MINOR__ > 2
#include <debug/redir.h>
#endif
1999-04-27 01:26:45 +00:00
1999-05-05 14:45:51 +00:00
#if __DJGPP_MINOR__ < 3
/* This code will be provided from DJGPP 2.03 on. Until then I code it
here */
1999-07-07 20:19:36 +00:00
typedef struct
{
unsigned short sig0;
unsigned short sig1;
unsigned short sig2;
unsigned short sig3;
unsigned short exponent:15;
unsigned short sign:1;
}
NPXREG;
typedef struct
{
unsigned int control;
unsigned int status;
unsigned int tag;
unsigned int eip;
unsigned int cs;
unsigned int dataptr;
unsigned int datasel;
NPXREG reg[8];
}
NPX;
1999-05-05 14:45:51 +00:00
static NPX npx;
1999-07-07 20:19:36 +00:00
static void save_npx (void); /* Save the FPU of the debugged program */
static void load_npx (void); /* Restore the FPU of the debugged program */
1999-05-05 14:45:51 +00:00
/* ------------------------------------------------------------------------- */
/* Store the contents of the NPX in the global variable `npx'. */
1999-07-07 20:19:36 +00:00
/* *INDENT-OFF* */
1999-05-05 14:45:51 +00:00
static void
save_npx (void)
{
asm ("inb $0xa0, %%al
1999-07-07 20:19:36 +00:00
testb $0x20, %%al
jz 1f
xorb %% al, %%al
outb %% al, $0xf0
movb $0x20, %%al
outb %% al, $0xa0
outb %% al, $0x20
1999-05-05 14:45:51 +00:00
1:
1999-07-07 20:19:36 +00:00
fnsave % 0
fwait "
: "=m" (npx)
: /* No input */
: "%eax");
1999-05-05 14:45:51 +00:00
}
1999-07-07 20:19:36 +00:00
/* *INDENT-ON* */
1999-05-05 14:45:51 +00:00
/* ------------------------------------------------------------------------- */
/* Reload the contents of the NPX from the global variable `npx'. */
static void
load_npx (void)
{
asm ("frstor %0":"=m" (npx));
1999-05-05 14:45:51 +00:00
}
1999-08-23 22:40:00 +00:00
/* ------------------------------------------------------------------------- */
/* Stubs for the missing redirection functions. */
typedef struct {
char *command;
int redirected;
} cmdline_t;
void
redir_cmdline_delete (cmdline_t *ptr)
{
ptr->redirected = 0;
}
int
redir_cmdline_parse (const char *args, cmdline_t *ptr)
1999-08-23 22:40:00 +00:00
{
return -1;
}
int
redir_to_child (cmdline_t *ptr)
1999-08-23 22:40:00 +00:00
{
return 1;
}
int
redir_to_debugger (cmdline_t *ptr)
1999-08-23 22:40:00 +00:00
{
return 1;
}
int
redir_debug_init (cmdline_t *ptr)
{
return 0;
}
1999-05-05 14:45:51 +00:00
#endif /* __DJGPP_MINOR < 3 */
1999-08-23 22:40:00 +00:00
typedef enum { wp_insert, wp_remove, wp_count } wp_op;
/* This holds the current reference counts for each debug register. */
static int dr_ref_count[4];
1999-04-27 01:26:45 +00:00
#define SOME_PID 42
static int prog_has_started = 0;
1999-07-07 20:19:36 +00:00
static void go32_open (char *name, int from_tty);
static void go32_close (int quitting);
static void go32_attach (char *args, int from_tty);
static void go32_detach (char *args, int from_tty);
2001-05-04 04:15:33 +00:00
static void go32_resume (ptid_t ptid, int step,
enum target_signal siggnal);
static ptid_t go32_wait (ptid_t ptid,
struct target_waitstatus *status);
1999-07-07 20:19:36 +00:00
static void go32_fetch_registers (int regno);
static void store_register (int regno);
static void go32_store_registers (int regno);
static void go32_prepare_to_store (void);
static int go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
int write,
struct mem_attrib *attrib,
struct target_ops *target);
1999-07-07 20:19:36 +00:00
static void go32_files_info (struct target_ops *target);
static void go32_stop (void);
static void go32_kill_inferior (void);
static void go32_create_inferior (char *exec_file, char *args, char **env);
static void go32_mourn_inferior (void);
static int go32_can_run (void);
1999-05-05 14:45:51 +00:00
static struct target_ops go32_ops;
1999-07-07 20:19:36 +00:00
static void go32_terminal_init (void);
static void go32_terminal_inferior (void);
static void go32_terminal_ours (void);
1999-04-27 01:26:45 +00:00
1999-08-23 22:40:00 +00:00
#define r_ofs(x) (offsetof(TSS,x))
1999-04-27 01:26:45 +00:00
static struct
{
1999-08-23 22:40:00 +00:00
size_t tss_ofs;
size_t size;
1999-04-27 01:26:45 +00:00
}
regno_mapping[] =
{
{r_ofs (tss_eax), 4}, /* normal registers, from a_tss */
{r_ofs (tss_ecx), 4},
{r_ofs (tss_edx), 4},
{r_ofs (tss_ebx), 4},
{r_ofs (tss_esp), 4},
{r_ofs (tss_ebp), 4},
{r_ofs (tss_esi), 4},
{r_ofs (tss_edi), 4},
{r_ofs (tss_eip), 4},
{r_ofs (tss_eflags), 4},
{r_ofs (tss_cs), 2},
{r_ofs (tss_ss), 2},
{r_ofs (tss_ds), 2},
{r_ofs (tss_es), 2},
{r_ofs (tss_fs), 2},
{r_ofs (tss_gs), 2},
{0, 10}, /* 8 FP registers, from npx.reg[] */
{1, 10},
{2, 10},
{3, 10},
{4, 10},
{5, 10},
{6, 10},
{7, 10},
1999-08-23 22:40:00 +00:00
/* The order of the next 7 registers must be consistent
with their numbering in config/i386/tm-i386.h, which see. */
{0, 2}, /* control word, from npx */
{4, 2}, /* status word, from npx */
{8, 2}, /* tag word, from npx */
{16, 2}, /* last FP exception CS from npx */
{12, 4}, /* last FP exception EIP from npx */
{24, 2}, /* last FP exception operand selector from npx */
{20, 4}, /* last FP exception operand offset from npx */
{18, 2} /* last FP opcode from npx */
1999-04-27 01:26:45 +00:00
};
static struct
{
int go32_sig;
enum target_signal gdb_sig;
1999-04-27 01:26:45 +00:00
}
sig_map[] =
{
{0, TARGET_SIGNAL_FPE},
{1, TARGET_SIGNAL_TRAP},
1999-08-23 22:40:00 +00:00
/* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
but I think SIGBUS is better, since the NMI is usually activated
as a result of a memory parity check failure. */
{2, TARGET_SIGNAL_BUS},
{3, TARGET_SIGNAL_TRAP},
{4, TARGET_SIGNAL_FPE},
{5, TARGET_SIGNAL_SEGV},
{6, TARGET_SIGNAL_ILL},
{7, TARGET_SIGNAL_EMT}, /* no-coprocessor exception */
{8, TARGET_SIGNAL_SEGV},
{9, TARGET_SIGNAL_SEGV},
{10, TARGET_SIGNAL_BUS},
{11, TARGET_SIGNAL_SEGV},
{12, TARGET_SIGNAL_SEGV},
{13, TARGET_SIGNAL_SEGV},
{14, TARGET_SIGNAL_SEGV},
{16, TARGET_SIGNAL_FPE},
{17, TARGET_SIGNAL_BUS},
{31, TARGET_SIGNAL_ILL},
{0x1b, TARGET_SIGNAL_INT},
{0x75, TARGET_SIGNAL_FPE},
{0x78, TARGET_SIGNAL_ALRM},
{0x79, TARGET_SIGNAL_INT},
{0x7a, TARGET_SIGNAL_QUIT},
{-1, TARGET_SIGNAL_LAST}
1999-04-27 01:26:45 +00:00
};
1999-08-23 22:40:00 +00:00
static struct {
enum target_signal gdb_sig;
int djgpp_excepno;
} excepn_map[] = {
{TARGET_SIGNAL_0, -1},
{TARGET_SIGNAL_ILL, 6}, /* Invalid Opcode */
{TARGET_SIGNAL_EMT, 7}, /* triggers SIGNOFP */
{TARGET_SIGNAL_SEGV, 13}, /* GPF */
{TARGET_SIGNAL_BUS, 17}, /* Alignment Check */
1999-08-23 22:40:00 +00:00
/* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
details. */
{TARGET_SIGNAL_TERM, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
{TARGET_SIGNAL_FPE, 0x75},
{TARGET_SIGNAL_INT, 0x79},
{TARGET_SIGNAL_QUIT, 0x7a},
{TARGET_SIGNAL_ALRM, 0x78}, /* triggers SIGTIMR */
{TARGET_SIGNAL_PROF, 0x78},
{TARGET_SIGNAL_LAST, -1}
1999-08-23 22:40:00 +00:00
};
1999-04-27 01:26:45 +00:00
static void
go32_open (char *name, int from_tty)
1999-04-27 01:26:45 +00:00
{
1999-08-23 22:40:00 +00:00
printf_unfiltered ("Done. Use the \"run\" command to run the program.\n");
1999-04-27 01:26:45 +00:00
}
static void
go32_close (int quitting)
1999-04-27 01:26:45 +00:00
{
}
static void
go32_attach (char *args, int from_tty)
1999-04-27 01:26:45 +00:00
{
1999-08-23 22:40:00 +00:00
error ("\
You cannot attach to a running program on this platform.\n\
Use the `run' command to run DJGPP programs.");
1999-04-27 01:26:45 +00:00
}
static void
go32_detach (char *args, int from_tty)
1999-04-27 01:26:45 +00:00
{
}
static int resume_is_step;
1999-08-23 22:40:00 +00:00
static int resume_signal = -1;
1999-04-27 01:26:45 +00:00
static void
2001-05-04 04:15:33 +00:00
go32_resume (ptid_t ptid, int step, enum target_signal siggnal)
1999-07-07 20:19:36 +00:00
{
1999-08-23 22:40:00 +00:00
int i;
1999-07-07 20:19:36 +00:00
resume_is_step = step;
1999-08-23 22:40:00 +00:00
if (siggnal != TARGET_SIGNAL_0 && siggnal != TARGET_SIGNAL_TRAP)
{
for (i = 0, resume_signal = -1;
excepn_map[i].gdb_sig != TARGET_SIGNAL_LAST; i++)
1999-08-23 22:40:00 +00:00
if (excepn_map[i].gdb_sig == siggnal)
{
resume_signal = excepn_map[i].djgpp_excepno;
break;
}
if (resume_signal == -1)
printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
target_signal_to_name (siggnal));
}
1999-07-07 20:19:36 +00:00
}
1999-04-27 01:26:45 +00:00
1999-08-23 22:40:00 +00:00
static char child_cwd[FILENAME_MAX];
1999-04-27 01:26:45 +00:00
static int
2001-05-04 04:15:33 +00:00
go32_wait (ptid_t ptid, struct target_waitstatus *status)
1999-04-27 01:26:45 +00:00
{
int i;
1999-08-23 22:40:00 +00:00
unsigned char saved_opcode;
unsigned long INT3_addr = 0;
1999-08-23 22:40:00 +00:00
int stepping_over_INT = 0;
1999-04-27 01:26:45 +00:00
1999-08-23 22:40:00 +00:00
a_tss.tss_eflags &= 0xfeff; /* reset the single-step flag (TF) */
1999-04-27 01:26:45 +00:00
if (resume_is_step)
1999-08-23 22:40:00 +00:00
{
/* If the next instruction is INT xx or INTO, we need to handle
them specially. Intel manuals say that these instructions
reset the single-step flag (a.k.a. TF). However, it seems
that, at least in the DPMI environment, and at least when
stepping over the DPMI interrupt 31h, the problem is having
TF set at all when INT 31h is executed: the debuggee either
crashes (and takes the system with it) or is killed by a
SIGTRAP.
So we need to emulate single-step mode: we put an INT3 opcode
right after the INT xx instruction, let the debuggee run
until it hits INT3 and stops, then restore the original
instruction which we overwrote with the INT3 opcode, and back
up the debuggee's EIP to that instruction. */
read_child (a_tss.tss_eip, &saved_opcode, 1);
if (saved_opcode == 0xCD || saved_opcode == 0xCE)
{
unsigned char INT3_opcode = 0xCC;
INT3_addr
= saved_opcode == 0xCD ? a_tss.tss_eip + 2 : a_tss.tss_eip + 1;
stepping_over_INT = 1;
read_child (INT3_addr, &saved_opcode, 1);
write_child (INT3_addr, &INT3_opcode, 1);
}
else
a_tss.tss_eflags |= 0x0100; /* normal instruction: set TF */
}
/* The special value FFFFh in tss_trap indicates to run_child that
tss_irqn holds a signal to be delivered to the debuggee. */
if (resume_signal <= -1)
{
a_tss.tss_trap = 0;
a_tss.tss_irqn = 0xff;
}
1999-04-27 01:26:45 +00:00
else
1999-08-23 22:40:00 +00:00
{
a_tss.tss_trap = 0xffff; /* run_child looks for this */
a_tss.tss_irqn = resume_signal;
}
/* The child might change working directory behind our back. The
GDB users won't like the side effects of that when they work with
relative file names, and GDB might be confused by its current
directory not being in sync with the truth. So we always make a
point of changing back to where GDB thinks is its cwd, when we
return control to the debugger, but restore child's cwd before we
run it. */
/* Initialize child_cwd, before the first call to run_child and not
in the initialization, so the child get also the changed directory
set with the gdb-command "cd ..." */
if (!*child_cwd)
/* Initialize child's cwd with the current one. */
getcwd (child_cwd, sizeof (child_cwd));
1999-08-23 22:40:00 +00:00
chdir (child_cwd);
1999-04-27 01:26:45 +00:00
1999-05-05 14:45:51 +00:00
#if __DJGPP_MINOR__ < 3
1999-08-23 22:40:00 +00:00
load_npx ();
1999-05-05 14:45:51 +00:00
#endif
1999-04-27 01:26:45 +00:00
run_child ();
1999-05-05 14:45:51 +00:00
#if __DJGPP_MINOR__ < 3
1999-08-23 22:40:00 +00:00
save_npx ();
1999-05-05 14:45:51 +00:00
#endif
1999-04-27 01:26:45 +00:00
1999-08-23 22:40:00 +00:00
/* Did we step over an INT xx instruction? */
if (stepping_over_INT && a_tss.tss_eip == INT3_addr + 1)
{
/* Restore the original opcode. */
a_tss.tss_eip--; /* EIP points *after* the INT3 instruction */
write_child (a_tss.tss_eip, &saved_opcode, 1);
/* Simulate a TRAP exception. */
a_tss.tss_irqn = 1;
a_tss.tss_eflags |= 0x0100;
}
getcwd (child_cwd, sizeof (child_cwd)); /* in case it has changed */
chdir (current_directory);
1999-04-27 01:26:45 +00:00
if (a_tss.tss_irqn == 0x21)
{
status->kind = TARGET_WAITKIND_EXITED;
status->value.integer = a_tss.tss_eax & 0xff;
}
else
{
status->value.sig = TARGET_SIGNAL_UNKNOWN;
status->kind = TARGET_WAITKIND_STOPPED;
for (i = 0; sig_map[i].go32_sig != -1; i++)
{
if (a_tss.tss_irqn == sig_map[i].go32_sig)
{
1999-08-23 22:40:00 +00:00
#if __DJGPP_MINOR__ < 3
1999-04-27 01:26:45 +00:00
if ((status->value.sig = sig_map[i].gdb_sig) !=
TARGET_SIGNAL_TRAP)
status->kind = TARGET_WAITKIND_SIGNALLED;
1999-08-23 22:40:00 +00:00
#else
status->value.sig = sig_map[i].gdb_sig;
#endif
1999-04-27 01:26:45 +00:00
break;
}
}
}
return SOME_PID;
}
static void
fetch_register (int regno)
1999-04-27 01:26:45 +00:00
{
if (regno < FP0_REGNUM)
supply_register (regno, (char *) &a_tss + regno_mapping[regno].tss_ofs);
else if (regno <= LAST_FPU_CTRL_REGNUM)
i387_supply_register (regno, (char *) &npx);
else
internal_error (__FILE__, __LINE__,
"Invalid register no. %d in fetch_register.", regno);
}
1999-04-27 01:26:45 +00:00
static void
go32_fetch_registers (int regno)
{
if (regno >= 0)
fetch_register (regno);
else
1999-04-27 01:26:45 +00:00
{
for (regno = 0; regno < FP0_REGNUM; regno++)
fetch_register (regno);
i387_supply_fsave ((char *) &npx);
1999-04-27 01:26:45 +00:00
}
}
static void
store_register (int regno)
{
void *rp;
void *v = (void *) register_buffer (regno);
1999-04-27 01:26:45 +00:00
if (regno < FP0_REGNUM)
memcpy ((char *) &a_tss + regno_mapping[regno].tss_ofs,
v, regno_mapping[regno].size);
else if (regno <= LAST_FPU_CTRL_REGNUM)
i387_fill_fsave ((char *)&npx, regno);
1999-04-27 01:26:45 +00:00
else
internal_error (__FILE__, __LINE__,
"Invalid register no. %d in store_register.", regno);
1999-04-27 01:26:45 +00:00
}
static void
go32_store_registers (int regno)
{
unsigned r;
1999-04-27 01:26:45 +00:00
if (regno >= 0)
store_register (regno);
else
{
for (r = 0; r < FP0_REGNUM; r++)
1999-04-27 01:26:45 +00:00
store_register (r);
i387_fill_fsave ((char *) &npx, -1);
1999-04-27 01:26:45 +00:00
}
}
static void
go32_prepare_to_store (void)
{
}
static int
go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
struct mem_attrib *attrib, struct target_ops *target)
1999-04-27 01:26:45 +00:00
{
if (write)
{
if (write_child (memaddr, myaddr, len))
{
return 0;
}
else
{
return len;
}
}
else
{
if (read_child (memaddr, myaddr, len))
{
return 0;
}
else
{
return len;
}
}
}
1999-08-23 22:40:00 +00:00
static cmdline_t child_cmd; /* parsed child's command line kept here */
1999-04-27 01:26:45 +00:00
static void
go32_files_info (struct target_ops *target)
1999-04-27 01:26:45 +00:00
{
1999-08-23 22:40:00 +00:00
printf_unfiltered ("You are running a DJGPP V2 program.\n");
1999-04-27 01:26:45 +00:00
}
static void
go32_stop (void)
{
normal_stop ();
cleanup_client ();
2001-05-04 04:15:33 +00:00
inferior_ptid = null_ptid;
1999-04-27 01:26:45 +00:00
prog_has_started = 0;
}
static void
go32_kill_inferior (void)
{
1999-08-23 22:40:00 +00:00
redir_cmdline_delete (&child_cmd);
resume_signal = -1;
resume_is_step = 0;
1999-04-27 01:26:45 +00:00
unpush_target (&go32_ops);
}
static void
go32_create_inferior (char *exec_file, char *args, char **env)
{
extern char **environ;
1999-04-27 01:26:45 +00:00
jmp_buf start_state;
char *cmdline;
char **env_save = environ;
/* If no exec file handed to us, get it from the exec-file command -- with
a good, common error message if none is specified. */
if (exec_file == 0)
exec_file = get_exec_file (1);
1999-04-27 01:26:45 +00:00
if (prog_has_started)
{
1999-05-05 14:45:51 +00:00
go32_stop ();
1999-04-27 01:26:45 +00:00
go32_kill_inferior ();
}
1999-08-23 22:40:00 +00:00
resume_signal = -1;
resume_is_step = 0;
/* Initialize child's cwd as empty to be initialized when starting
the child. */
*child_cwd = 0;
1999-08-23 22:40:00 +00:00
/* Init command line storage. */
if (redir_debug_init (&child_cmd) == -1)
internal_error (__FILE__, __LINE__,
"Cannot allocate redirection storage: not enough memory.\n");
1999-08-23 22:40:00 +00:00
/* Parse the command line and create redirections. */
if (strpbrk (args, "<>"))
{
if (redir_cmdline_parse (args, &child_cmd) == 0)
args = child_cmd.command;
else
error ("Syntax error in command line.");
}
else
1999-12-07 03:56:43 +00:00
child_cmd.command = xstrdup (args);
1999-04-27 01:26:45 +00:00
cmdline = (char *) alloca (strlen (args) + 4);
cmdline[0] = strlen (args);
strcpy (cmdline + 1, args);
cmdline[strlen (args) + 1] = 13;
environ = env;
if (v2loadimage (exec_file, cmdline, start_state))
{
environ = env_save;
printf_unfiltered ("Load failed for image %s\n", exec_file);
exit (1);
}
environ = env_save;
edi_init (start_state);
1999-08-23 22:40:00 +00:00
#if __DJGPP_MINOR__ < 3
save_npx ();
#endif
1999-04-27 01:26:45 +00:00
2001-05-04 04:15:33 +00:00
inferior_ptid = pid_to_ptid (SOME_PID);
1999-04-27 01:26:45 +00:00
push_target (&go32_ops);
clear_proceed_status ();
insert_breakpoints ();
1999-10-05 23:13:56 +00:00
proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
1999-05-05 14:45:51 +00:00
prog_has_started = 1;
1999-04-27 01:26:45 +00:00
}
static void
go32_mourn_inferior (void)
{
1999-08-23 22:40:00 +00:00
/* We need to make sure all the breakpoint enable bits in the DR7
register are reset when the inferior exits. Otherwise, if they
rerun the inferior, the uncleared bits may cause random SIGTRAPs,
failure to set more watchpoints, and other calamities. It would
be nice if GDB itself would take care to remove all breakpoints
at all times, but it doesn't, probably under an assumption that
the OS cleans up when the debuggee exits. */
i386_cleanup_dregs ();
1999-04-27 01:26:45 +00:00
go32_kill_inferior ();
generic_mourn_inferior ();
}
static int
go32_can_run (void)
{
return 1;
}
/* Hardware watchpoint support. */
#define D_REGS edi.dr
#define CONTROL D_REGS[7]
#define STATUS D_REGS[6]
1999-08-23 22:40:00 +00:00
/* Pass the address ADDR to the inferior in the I'th debug register.
Here we just store the address in D_REGS, the watchpoint will be
actually set up when go32_wait runs the debuggee. */
void
go32_set_dr (int i, CORE_ADDR addr)
1999-04-27 01:26:45 +00:00
{
if (i < 0 || i > 3)
internal_error (__FILE__, __LINE__,
"Invalid register %d in go32_set_dr.\n", i);
D_REGS[i] = addr;
1999-04-27 01:26:45 +00:00
}
/* Pass the value VAL to the inferior in the DR7 debug control
register. Here we just store the address in D_REGS, the watchpoint
will be actually set up when go32_wait runs the debuggee. */
void
go32_set_dr7 (unsigned val)
1999-08-23 22:40:00 +00:00
{
CONTROL = val;
1999-08-23 22:40:00 +00:00
}
/* Get the value of the DR6 debug status register from the inferior.
Here we just return the value stored in D_REGS, as we've got it
from the last go32_wait call. */
unsigned
go32_get_dr6 (void)
1999-04-27 01:26:45 +00:00
{
return STATUS;
1999-04-27 01:26:45 +00:00
}
1999-08-23 22:40:00 +00:00
/* Put the device open on handle FD into either raw or cooked
mode, return 1 if it was in raw mode, zero otherwise. */
static int
device_mode (int fd, int raw_p)
{
int oldmode, newmode;
__dpmi_regs regs;
regs.x.ax = 0x4400;
regs.x.bx = fd;
__dpmi_int (0x21, &regs);
if (regs.x.flags & 1)
return -1;
newmode = oldmode = regs.x.dx;
if (raw_p)
newmode |= 0x20;
else
newmode &= ~0x20;
if (oldmode & 0x80) /* Only for character dev */
{
regs.x.ax = 0x4401;
regs.x.bx = fd;
regs.x.dx = newmode & 0xff; /* Force upper byte zero, else it fails */
__dpmi_int (0x21, &regs);
if (regs.x.flags & 1)
return -1;
}
return (oldmode & 0x20) == 0x20;
}
static int inf_mode_valid = 0;
static int inf_terminal_mode;
/* This semaphore is needed because, amazingly enough, GDB calls
target.to_terminal_ours more than once after the inferior stops.
But we need the information from the first call only, since the
second call will always see GDB's own cooked terminal. */
static int terminal_is_ours = 1;
1999-06-07 19:19:32 +00:00
static void
go32_terminal_init (void)
{
1999-08-23 22:40:00 +00:00
inf_mode_valid = 0; /* reinitialize, in case they are restarting child */
terminal_is_ours = 1;
1999-06-07 19:19:32 +00:00
}
static void
go32_terminal_info (char *args, int from_tty)
1999-06-07 19:19:32 +00:00
{
1999-08-23 22:40:00 +00:00
printf_unfiltered ("Inferior's terminal is in %s mode.\n",
!inf_mode_valid
? "default" : inf_terminal_mode ? "raw" : "cooked");
#if __DJGPP_MINOR__ > 2
if (child_cmd.redirection)
{
int i;
for (i = 0; i < DBG_HANDLES; i++)
1999-07-07 20:19:36 +00:00
{
1999-08-23 22:40:00 +00:00
if (child_cmd.redirection[i]->file_name)
printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
i, child_cmd.redirection[i]->file_name);
else if (_get_dev_info (child_cmd.redirection[i]->inf_handle) == -1)
printf_unfiltered
("\tFile handle %d appears to be closed by inferior.\n", i);
/* Mask off the raw/cooked bit when comparing device info words. */
else if ((_get_dev_info (child_cmd.redirection[i]->inf_handle) & 0xdf)
!= (_get_dev_info (i) & 0xdf))
printf_unfiltered
("\tFile handle %d appears to be redirected by inferior.\n", i);
1999-07-07 20:19:36 +00:00
}
1999-08-23 22:40:00 +00:00
}
#endif
}
static void
go32_terminal_inferior (void)
{
/* Redirect standard handles as child wants them. */
errno = 0;
if (redir_to_child (&child_cmd) == -1)
{
redir_to_debugger (&child_cmd);
error ("Cannot redirect standard handles for program: %s.",
strerror (errno));
}
/* set the console device of the inferior to whatever mode
(raw or cooked) we found it last time */
if (terminal_is_ours)
{
if (inf_mode_valid)
device_mode (0, inf_terminal_mode);
terminal_is_ours = 0;
}
1999-06-07 19:19:32 +00:00
}
static void
go32_terminal_ours (void)
{
1999-08-23 22:40:00 +00:00
/* Switch to cooked mode on the gdb terminal and save the inferior
terminal mode to be restored when it is resumed */
if (!terminal_is_ours)
{
inf_terminal_mode = device_mode (0, 0);
if (inf_terminal_mode != -1)
inf_mode_valid = 1;
else
/* If device_mode returned -1, we don't know what happens with
handle 0 anymore, so make the info invalid. */
inf_mode_valid = 0;
terminal_is_ours = 1;
/* Restore debugger's standard handles. */
errno = 0;
if (redir_to_debugger (&child_cmd) == -1)
{
redir_to_child (&child_cmd);
error ("Cannot redirect standard handles for debugger: %s.",
strerror (errno));
}
}
1999-06-07 19:19:32 +00:00
}
1999-04-27 01:26:45 +00:00
static void
init_go32_ops (void)
{
go32_ops.to_shortname = "djgpp";
go32_ops.to_longname = "djgpp target process";
go32_ops.to_doc =
"Program loaded by djgpp, when gdb is used as an external debugger";
go32_ops.to_open = go32_open;
go32_ops.to_close = go32_close;
1999-08-23 22:40:00 +00:00
go32_ops.to_attach = go32_attach;
1999-04-27 01:26:45 +00:00
go32_ops.to_detach = go32_detach;
go32_ops.to_resume = go32_resume;
go32_ops.to_wait = go32_wait;
go32_ops.to_fetch_registers = go32_fetch_registers;
go32_ops.to_store_registers = go32_store_registers;
go32_ops.to_prepare_to_store = go32_prepare_to_store;
go32_ops.to_xfer_memory = go32_xfer_memory;
go32_ops.to_files_info = go32_files_info;
go32_ops.to_insert_breakpoint = memory_insert_breakpoint;
go32_ops.to_remove_breakpoint = memory_remove_breakpoint;
1999-06-07 19:19:32 +00:00
go32_ops.to_terminal_init = go32_terminal_init;
go32_ops.to_terminal_inferior = go32_terminal_inferior;
1999-08-23 22:40:00 +00:00
go32_ops.to_terminal_ours_for_output = go32_terminal_ours;
1999-06-07 19:19:32 +00:00
go32_ops.to_terminal_ours = go32_terminal_ours;
1999-08-23 22:40:00 +00:00
go32_ops.to_terminal_info = go32_terminal_info;
1999-04-27 01:26:45 +00:00
go32_ops.to_kill = go32_kill_inferior;
go32_ops.to_create_inferior = go32_create_inferior;
go32_ops.to_mourn_inferior = go32_mourn_inferior;
go32_ops.to_can_run = go32_can_run;
go32_ops.to_stop = go32_stop;
go32_ops.to_stratum = process_stratum;
go32_ops.to_has_all_memory = 1;
go32_ops.to_has_memory = 1;
go32_ops.to_has_stack = 1;
go32_ops.to_has_registers = 1;
go32_ops.to_has_execution = 1;
go32_ops.to_magic = OPS_MAGIC;
1999-08-23 22:40:00 +00:00
/* Initialize child's cwd as empty to be initialized when starting
the child. */
*child_cwd = 0;
1999-08-23 22:40:00 +00:00
/* Initialize child's command line storage. */
if (redir_debug_init (&child_cmd) == -1)
internal_error (__FILE__, __LINE__,
"Cannot allocate redirection storage: not enough memory.\n");
/* We are always processing GCC-compiled programs. */
processing_gcc_compilation = 2;
1999-04-27 01:26:45 +00:00
}
void
_initialize_go32_nat (void)
{
init_go32_ops ();
add_target (&go32_ops);
}
1999-08-23 22:40:00 +00:00
pid_t
tcgetpgrp (int fd)
{
if (isatty (fd))
return SOME_PID;
errno = ENOTTY;
return -1;
}
int
tcsetpgrp (int fd, pid_t pgid)
{
if (isatty (fd) && pgid == SOME_PID)
return 0;
errno = pgid == SOME_PID ? ENOTTY : ENOSYS;
return -1;
}