[gnu.gdb.bug] gdb for the Sequent Symmetry

jw@sics.se (Johan Widen) (02/05/89)

Enclosed are two configuration files and a patch (to config.gdb) to make
gdb work on the Sequent Symmetry. There is also a patch for
/usr/lib/libg.a.

This is for gdb-3.1.

The current version of gdb does not know about shared memory. Also, it is not
a parallel debugger (i.e. you will still need pdbx).

The configuration files are derived from those provided for i386.

I provide the current version with the hope that you will find it useful
and that you do not want to wait for a perfect version.


A problem with /usr/lib/libg.a on the Symmetry under DYNIX V3.0.12.
===================================================================

Gdb currently relies on that libg.a has an N_SO (debugger) symbol in its
symbol table. libg.a on the Symmetry has no such symbol. You can produce
a libg.a with such a symbol added by running the enclosed shell script
'symmetry.libg.fix'.

#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  README.symmetry
#	  config.gdb.patch
#	  m-symmetry.h
#	  symmetry-dep.c
#	  symmetry.libg.fix
#
sed 's/^X//' << 'SHAR_EOF' > README.symmetry &&
XEnclosed are two configuration files and a patch (to config.gdb) to make
Xgdb work on the Sequent Symmetry. There is also a patch for
X/usr/lib/libg.a.
X
XThis is for gdb-3.1.
X
XThe current version of gdb does not know about shared memory. Also, it is not
Xa parallel debugger (i.e. you will still need pdbx).
X
XThe configuration files are derived from those provided for i386.
X
XI provide the current version with the hope that you will find it useful
Xand that you do not want to wait for a perfect version.
X
X
XA problem with /usr/lib/libg.a on the Symmetry under DYNIX V3.0.12.
X===================================================================
X
XGdb currently relies on that libg.a has an N_SO (debugger) symbol in its
Xsymbol table. libg.a on the Symmetry has no such symbol. You can produce
Xa libg.a with such a symbol added by running the enclosed shell script
X'symmetry.libg.fix'.
X
X
X4-FEB-89, Johan Widen
XSICS, PO Box 1263, S-164 28 KISTA, SWEDEN
XTel: +46 8 752 15 32	Ttx: 812 61 54 SICS S	Fax: +46 8 751 72 30
XInternet: jw@sics.se or {mcvax,munnari,ukc,unido}!enea!sics.se!jw
SHAR_EOF
chmod 0664 README.symmetry || echo "restore of README.symmetry fails"
sed 's/^X//' << 'SHAR_EOF' > config.gdb.patch &&
X*** dist-gdb/config.gdb	Sat Feb  4 00:00:06 1989
X--- gdb/config.gdb	Wed Feb  1 19:41:40 1989
X***************
X*** 74,79 ****
X--- 74,85 ----
X  	depfile=i386-dep.c
X  	opcodefile=skip
X  	;;
X+ symmetry)
X+ 	pinsnfile=i386-pinsn.c
X+ 	opcodefile=skip
X+ 	depfile=symmetry-dep.c
X+ 	;;
X+ 
X  merlin)
X  	pinsnfile=ns32k-pinsn.c
X  	opcodefile=ns32k-opcode.h
SHAR_EOF
chmod 0664 config.gdb.patch || echo "restore of config.gdb.patch fails"
sed 's/^X//' << 'SHAR_EOF' > m-symmetry.h &&
X/*
X * Changes for 80386 by Pace Willisson (pace@prep.ai.mit.edu)
X * July 1988
X */
X
X/*
X   Copyright (C) 1986, 1987 Free Software Foundation, Inc.
X
XGDB is distributed in the hope that it will be useful, but WITHOUT ANY
XWARRANTY.  No author or distributor accepts responsibility to anyone
Xfor the consequences of using it or for whether it serves any
Xparticular purpose or works at all, unless he says so in writing.
XRefer to the GDB General Public License for full details.
X
XEveryone is granted permission to copy, modify and redistribute GDB,
Xbut only under the conditions described in the GDB General Public
XLicense.  A copy of this license is supposed to have been given to you
Xalong with GDB so you can know your rights and responsibilities.  It
Xshould be in a file named COPYING.  Among other things, the copyright
Xnotice and this notice must be preserved on all copies.
X
XIn other words, go ahead and share GDB, but don't try to stop
Xanyone else from sharing it farther.  Help stamp out software hoarding!
X*/
X
X#ifndef i386
X#define i386
X#endif
X
X/* define USG if you are using sys5 /usr/include's */
X/* #define USG */
X
X/* USG systems need these */
X/* #define vfork() fork() */
X/* #define MAXPATHLEN 500 */
X
X/* define this if you don't have the extension to coff that allows
X * file names to appear in the string table
X * (aux.x_file.x_foff)
X */
X/* #define COFF_NO_LONG_FILE_NAMES */
X
X/* turn this on when rest of gdb is ready */
X/* #define IEEE_FLOAT */
X
X/* #define NBPG NBPC */
X/* #define UPAGES USIZE */
X
X/* #define HAVE_TERMIO */
X
X/* Get rid of any system-imposed stack limit if possible.  */
X
X#define SET_STACK_LIMIT_HUGE
X
X/* Define this if the C compiler puts an underscore at the front
X   of external names before giving them to the linker.  */
X
X#define NAMES_HAVE_UNDERSCORE
X
X/* Specify debugger information format.  */
X
X#define READ_DBX_FORMAT
X/* #define COFF_FORMAT */
X
X#ifndef N_SEGSIZ
X#define	N_SEGSIZ(x) NBPG
X#endif
X
X#define SIZE_OF_TEXT_SEGMENT (N_DATAOFF(hdr) - N_TXTOFF(hdr))
X
X#ifndef N_TXTADDR
X#define	N_TXTADDR(hdr) \
X  (N_ADDRADJ(hdr))
X#endif /* no N_TXTADDR */
X
X#ifndef N_DATADDR
X#define N_DATADDR(hdr) \
X	(((hdr).a_magic==OMAGIC)? (N_TXTADDR(hdr)+(hdr).a_text) \
X	: (N_SEGSIZ(hdr)+(((hdr).a_text-1) & ~(N_SEGSIZ(hdr)-1))))
X#endif /* no N_DATADDR */
X
X/* number of traps that happen between exec'ing the shell 
X * to run an inferior, and when we finally get to 
X * the inferior code.  This is 2 on most implementations.
X */
X/* #define START_INFERIOR_TRAPS_EXPECTED 4 */
X
X/* Offset from address of function to start of its code.
X   Zero on most machines.  */
X
X#define FUNCTION_START_OFFSET 0
X
X/* Advance PC across any function entry prologue instructions
X   to reach some "real" code.  */
X
X#define SKIP_PROLOGUE(frompc)   {(frompc) = i386_skip_prologue((frompc));}
X
X/* Immediately after a function call, return the saved pc.
X   Can't always go through the frames for this because on some machines
X   the new frame is not set up until the new function executes
X   some instructions.  */
X
X#define SAVED_PC_AFTER_CALL(frame) \
X  (read_memory_integer (read_register (SP_REGNUM), 4))
X
X/* This is the amount to subtract from u.u_ar0
X   to get the offset in the core file of the register values.  */
X
X#define KERNEL_U_ADDR 0x7ffff000
X
X/* Address of end of stack space.  */
X
X#define STACK_END_ADDR 0x40000000
X
X/* Stack grows downward.  */
X
X#define INNER_THAN <
X
X/* Sequence of bytes for breakpoint instruction.  */
X
X#define BREAKPOINT {0xcc}
X
X/* Amount PC must be decremented by after a breakpoint.
X   This is often the number of bytes in BREAKPOINT
X   but not always.  */
X
X#define DECR_PC_AFTER_BREAK 0
X
X/* Nonzero if instruction at PC is a return instruction.  */
X
X#define ABOUT_TO_RETURN(pc) (read_memory_integer (pc, 1) == 0xc3)
X
X/* Return 1 if P points to an invalid floating point value.
X   LEN is the length in bytes -- not relevant on the 386.  */
X
X#define INVALID_FLOAT(p, len) (0)
X
X/* code to execute to print interesting information about the
X * floating point processor (if any)
X * No need to define if there is nothing to do.
X */
X#define FLOAT_INFO { i386_float_info (); }
X
X
X/* Largest integer type */
X#define LONGEST long
X
X/* Name of the builtin type for the LONGEST type above. */
X#define BUILTIN_TYPE_LONGEST builtin_type_long
X
X/* Say how long (ordinary) registers are.  */
X
X#define REGISTER_TYPE long
X
X/* Number of machine registers */
X
X#define NUM_REGS 10
X
X/* Initializer for an array of names of registers.
X   There should be NUM_REGS strings in this initializer.  */
X
X/* the order of the first 8 registers must match the compiler's 
X * numbering scheme (which is the same as the 386 scheme)
X * also, this table must match regmap in i386-pinsn.c.
X */
X#define REGISTER_NAMES { "eax", "ecx", "edx", "ebx", \
X			 "esp", "ebp", "esi", "edi", \
X			 "eip", "ps", "cs", "ss", \
X			 "ds", "es", "fs", "gs", \
X			 }
X
X/* Register numbers of various important registers.
X   Note that some of these values are "real" register numbers,
X   and correspond to the general registers of the machine,
X   and some are "phony" register numbers which are too large
X   to be actual register numbers as far as the user is concerned
X   but do serve to get the desired values when passed to read_register.  */
X
X#define FP_REGNUM 5		/* Contains address of executing stack frame */
X#define SP_REGNUM 4		/* Contains address of top of stack */
X
X#define PC_REGNUM 8
X#define PS_REGNUM 9
X
X#define REGISTER_U_ADDR(addr, blockend, regno) \
X	(addr) = i386_register_u_addr ((blockend),(regno));
X
X/* Total amount of space needed to store our copies of the machine's
X   register state, the array `registers'.  */
X#define REGISTER_BYTES (NUM_REGS * 4)
X
X/* Index within `registers' of the first byte of the space for
X   register N.  */
X
X#define REGISTER_BYTE(N) ((N)*4)
X
X/* Number of bytes of storage in the actual machine representation
X   for register N.  */
X
X#define REGISTER_RAW_SIZE(N) (4)
X
X/* Number of bytes of storage in the program's representation
X   for register N. */
X
X#define REGISTER_VIRTUAL_SIZE(N) (4)
X
X/* Largest value REGISTER_RAW_SIZE can have.  */
X
X#define MAX_REGISTER_RAW_SIZE 4
X
X/* Largest value REGISTER_VIRTUAL_SIZE can have.  */
X
X#define MAX_REGISTER_VIRTUAL_SIZE 4
X
X/* Nonzero if register N requires conversion
X   from raw format to virtual format.  */
X
X#define REGISTER_CONVERTIBLE(N) (0)
X
X/* Convert data from raw format for register REGNUM
X   to virtual format for register REGNUM.  */
X
X#define REGISTER_CONVERT_TO_VIRTUAL(REGNUM,FROM,TO) {bcopy ((FROM), (TO), 4);}
X
X/* Convert data from virtual format for register REGNUM
X   to raw format for register REGNUM.  */
X
X#define REGISTER_CONVERT_TO_RAW(REGNUM,FROM,TO) {bcopy ((FROM), (TO), 4);}
X
X/* Return the GDB type object for the "standard" data type
X   of data in register N.  */
X
X#define REGISTER_VIRTUAL_TYPE(N) (builtin_type_int)
X
X/* Store the address of the place in which to copy the structure the
X   subroutine will return.  This is called from call_function. */
X
X#define STORE_STRUCT_RETURN(ADDR, SP) \
X  { (SP) -= sizeof (ADDR);		\
X    write_memory ((SP), &(ADDR), sizeof (ADDR)); }
X
X/* Extract from an array REGBUF containing the (raw) register state
X   a function return value of type TYPE, and copy that, in virtual format,
X   into VALBUF.  */
X
X#define EXTRACT_RETURN_VALUE(TYPE,REGBUF,VALBUF) \
X  bcopy (REGBUF, VALBUF, TYPE_LENGTH (TYPE))
X
X/* Write into appropriate registers a function return value
X   of type TYPE, given in virtual format.  */
X
X#define STORE_RETURN_VALUE(TYPE,VALBUF) \
X  write_register_bytes (0, VALBUF, TYPE_LENGTH (TYPE))
X
X/* Extract from an array REGBUF containing the (raw) register state
X   the address in which a function should return its structure value,
X   as a CORE_ADDR (or an expression that can be used as one).  */
X
X#define EXTRACT_STRUCT_VALUE_ADDRESS(REGBUF) (*(int *)(REGBUF))
X
X
X/* Describe the pointer in each stack frame to the previous stack frame
X   (its caller).  */
X
X/* FRAME_CHAIN takes a frame's nominal address
X   and produces the frame's chain-pointer.
X
X   FRAME_CHAIN_COMBINE takes the chain pointer and the frame's nominal address
X   and produces the nominal address of the caller frame.
X
X   However, if FRAME_CHAIN_VALID returns zero,
X   it means the given frame is the outermost one and has no caller.
X   In that case, FRAME_CHAIN_COMBINE is not used.  */
X
X#define FRAME_CHAIN(thisframe) (read_memory_integer ((thisframe)->frame, 4))
X
X#define FRAME_CHAIN_VALID(chain, thisframe) \
X  (chain != 0 && (FRAME_SAVED_PC (thisframe) >= first_object_file_end))
X
X#define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
X
X/* Define other aspects of the stack frame.  */
X
X#define FRAME_SAVED_PC(FRAME) (read_memory_integer ((FRAME)->frame + 4, 4))
X
X#define FRAME_ARGS_ADDRESS(fi) ((fi)->frame)
X
X#define FRAME_LOCALS_ADDRESS(fi) ((fi)->frame)
X
X/* Return number of args passed to a frame.
X   Can return -1, meaning no way to tell.  */
X
X/* on the 386, the instruction following the call could be:
X *  popl %ecx        -  one arg
X *  addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
X *  anything else    -  zero args
X */
X#define FRAME_NUM_ARGS(numargs, fi)  { \
X  int retpc;						\
X  unsigned char op;					\
X  struct frame_info *pfi;				\
X  pfi = get_prev_frame_info ((fi));			\
X  retpc = pfi->pc;					\
X  numargs = 0;						\
X  op = read_memory_integer (retpc, 1);			\
X  if (op == 0x59)					\
X	  /* pop %ecx */				\
X	  (numargs) = 1;				\
X  else if (op == 0x83) {				\
X	  op = read_memory_integer (retpc+1, 1);	\
X	  if (op == 0xc4)				\
X		  /* addl $<signed imm 8 bits>, %esp */	\
X		  (numargs) = (read_memory_integer (retpc+2,1)&0xff)/4;\
X  } else if (op == 0x81) { /* add with 32 bit immediate */\
X	  op = read_memory_integer (retpc+1, 1);	\
X	  if (op == 0xc4)				\
X		  /* addl $<imm 32>, %esp */		\
X		  (numargs) = read_memory_integer (retpc+2, 4) / 4;\
X  }							\
X}
X
X/* Return number of bytes at start of arglist that are not really args.  */
X
X#define FRAME_ARGS_SKIP 8
X
X/* Put here the code to store, into a struct frame_saved_regs,
X   the addresses of the saved registers of frame described by FRAME_INFO.
X   This includes special registers such as pc and fp saved in special
X   ways in the stack frame.  sp is even more special:
X   the address we return for it IS the sp for the next frame.  */
X
X#define FRAME_FIND_SAVED_REGS(frame_info, frame_saved_regs) \
X{ i386_frame_find_saved_regs ((frame_info), &(frame_saved_regs)); }
X
X/* Compensate for lack of `vprintf' function.  */ 
X#define vprintf(format, ap) _doprnt (format, ap, stdout) 
X
X/* Things needed for making the inferior call functions.  */
X
X/* Push an empty stack frame, to record the current PC, etc.  */
X
X#define PUSH_DUMMY_FRAME { i386_push_dummy_frame (); }
X
X/* Discard from the stack the innermost frame, restoring all registers.  */
X
X#define POP_FRAME  { i386_pop_frame (); }
X
X/* this is 
X *   call 11223344 (32 bit relative)
X *   int3
X */
X
X#define CALL_DUMMY { 0x223344e8, 0xcc11 }
X
X#define CALL_DUMMY_LENGTH 8
X
X#define CALL_DUMMY_START_OFFSET 0  /* Start execution at beginning of dummy */
X
X/* Insert the specified number of args and function address
X   into a call sequence of the above form stored at DUMMYNAME.  */
X
X#define FIX_CALL_DUMMY(dummyname, pc, fun, nargs, type)   \
X{ \
X	int from, to, delta, loc; \
X	loc = (int)(read_register (SP_REGNUM) - CALL_DUMMY_LENGTH); \
X	from = loc + 5; \
X	to = (int)(fun); \
X	delta = to - from; \
X	*(int *)((char *)(dummyname) + 1) = delta; \
X}
X
X
X#if 0
X/* Interface definitions for kernel debugger KDB.  */
X
X/* Map machine fault codes into signal numbers.
X   First subtract 0, divide by 4, then index in a table.
X   Faults for which the entry in this table is 0
X   are not handled by KDB; the program's own trap handler
X   gets to handle then.  */
X
X#define FAULT_CODE_ORIGIN 0
X#define FAULT_CODE_UNITS 4
X#define FAULT_TABLE    \
X{ 0, 0, 0, 0, 0, 0, 0, 0, \
X  0, 0, 0, 0, 0, 0, 0, 0, \
X  0, 0, 0, 0, 0, 0, 0, 0}
X
X/* Start running with a stack stretching from BEG to END.
X   BEG and END should be symbols meaningful to the assembler.
X   This is used only for kdb.  */
X
X#define INIT_STACK(beg, end)  {}
X
X/* Push the frame pointer register on the stack.  */
X#define PUSH_FRAME_PTR        {}
X
X/* Copy the top-of-stack to the frame pointer register.  */
X#define POP_FRAME_PTR  {}
X
X/* After KDB is entered by a fault, push all registers
X   that GDB thinks about (all NUM_REGS of them),
X   so that they appear in order of ascending GDB register number.
X   The fault code will be on the stack beyond the last register.  */
X
X#define PUSH_REGISTERS        {}
X
X/* Assuming the registers (including processor status) have been
X   pushed on the stack in order of ascending GDB register number,
X   restore them and return to the address in the saved PC register.  */
X
X#define POP_REGISTERS      {}
X#endif
SHAR_EOF
chmod 0664 m-symmetry.h || echo "restore of m-symmetry.h fails"
sed 's/^X//' << 'SHAR_EOF' > symmetry-dep.c &&
X/* Low level interface to ptrace, for GDB when running on the Intel 386.
X   Copyright (C) 1988 Free Software Foundation, Inc.
X
XGDB is distributed in the hope that it will be useful, but WITHOUT ANY
XWARRANTY.  No author or distributor accepts responsibility to anyone
Xfor the consequences of using it or for whether it serves any
Xparticular purpose or works at all, unless he says so in writing.
XRefer to the GDB General Public License for full details.
X
XEveryone is granted permission to copy, modify and redistribute GDB,
Xbut only under the conditions described in the GDB General Public
XLicense.  A copy of this license is supposed to have been given to you
Xalong with GDB so you can know your rights and responsibilities.  It
Xshould be in a file named COPYING.  Among other things, the copyright
Xnotice and this notice must be preserved on all copies.
X
XIn other words, go ahead and share GDB, but don't try to stop
Xanyone else from sharing it farther.  Help stamp out software hoarding!
X*/
X
X#include "defs.h"
X#include "param.h"
X#include "frame.h"
X#include "inferior.h"
X
X#ifdef USG
X#include <sys/types.h>
X#endif
X
X#include <stdio.h>
X#include <sys/param.h>
X#include <sys/dir.h>
X#include <signal.h>
X#include <sys/user.h>
X#include <sys/ioctl.h>
X#include <fcntl.h>
X
X#include <a.out.h>
X
X#ifndef N_SET_MAGIC
X#ifdef COFF_FORMAT
X#define N_SET_MAGIC(exec, val) ((exec).magic = (val))
X#else
X#define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
X#endif
X#endif
X
X#include <sys/file.h>
X#include <sys/stat.h>
X
X#include <machine/reg.h>
X
Xextern int errno;
X
X/* This function simply calls ptrace with the given arguments.  
X   It exists so that all calls to ptrace are isolated in this 
X   machine-dependent file. */
Xint
Xcall_ptrace (request, pid, arg3, arg4)
X     int request, pid, arg3, arg4;
X{
X  return ptrace (request, pid, arg3, arg4);
X}
X
Xkill_inferior ()
X{
X  if (remote_debugging)
X    return;
X  if (inferior_pid == 0)
X    return;
X  ptrace (8, inferior_pid, 0, 0);
X  wait (0);
X  inferior_died ();
X}
X
X/* This is used when GDB is exiting.  It gives less chance of error.*/
X
Xkill_inferior_fast ()
X{
X  if (remote_debugging)
X    return;
X  if (inferior_pid == 0)
X    return;
X  ptrace (8, inferior_pid, 0, 0);
X  wait (0);
X}
X
X/* Resume execution of the inferior process.
X   If STEP is nonzero, single-step it.
X   If SIGNAL is nonzero, give it that signal.  */
X
Xvoid
Xresume (step, signal)
X     int step;
X     int signal;
X{
X  errno = 0;
X  if (remote_debugging)
X    remote_resume (step, signal);
X  else
X    {
X      ptrace (step ? 9 : 7, inferior_pid, 1, signal);
X      if (errno)
X	perror_with_name ("ptrace");
X    }
X}
X
Xvoid
Xfetch_inferior_registers ()
X{
X  struct pt_regset pt_regset;
X
X  ptrace (XPT_RREGS, inferior_pid, &pt_regset, 0);
X  supply_register (0, &pt_regset.pr_eax);
X  supply_register (1, &pt_regset.pr_ecx);
X  supply_register (2, &pt_regset.pr_edx);
X  supply_register (3, &pt_regset.pr_ebx);
X  supply_register (4, &pt_regset.pr_esp);
X  supply_register (5, &pt_regset.pr_ebp);
X  supply_register (6, &pt_regset.pr_esi);
X  supply_register (7, &pt_regset.pr_edi);
X  supply_register (8, &pt_regset.pr_eip);
X  supply_register (9, &pt_regset.pr_flags);
X}
X
X/* Store our register values back into the inferior.
X   If REGNO is -1, do this for all registers.
X   Otherwise, REGNO specifies which register (so we can save time).  */
X
Xstore_inferior_registers (regno)
X     int regno;
X{
X  struct pt_regset pt_regset;
X  int reg;
X  char buf[80];
X
X  ptrace (XPT_RREGS, inferior_pid, &pt_regset, 0);
X
X  if (regno >= 0)
X    {
X      reg = read_register (regno);
X      switch(regno)
X        {
X	case 0:
X	  pt_regset.pr_eax = regno;
X	  break;
X	case 1:
X	  pt_regset.pr_ecx = regno;
X	  break;
X	case 2:
X	  pt_regset.pr_edx = regno;
X	  break;
X	case 3:
X	  pt_regset.pr_ebx = regno;
X	  break;
X	case 4:
X	  pt_regset.pr_esp = regno;
X	  break;
X	case 5:
X	  pt_regset.pr_ebp = regno;
X	  break;
X	case 6:
X	  pt_regset.pr_esi = regno;
X	  break;
X	case 7:
X	  pt_regset.pr_edi = regno;
X	  break;
X	case 8:
X	  pt_regset.pr_eip = regno;
X	  break;
X	case 9:
X	  pt_regset.pr_flags = regno;
X	  break;
X	}
X    }
X  else
X    {
X	  pt_regset.pr_eax = read_register(0);
X	  pt_regset.pr_ecx = read_register(1);
X	  pt_regset.pr_edx = read_register(2);
X	  pt_regset.pr_ebx = read_register(3);
X	  pt_regset.pr_esp = read_register(4);
X	  pt_regset.pr_ebp = read_register(5);
X	  pt_regset.pr_esi = read_register(6);
X	  pt_regset.pr_edi = read_register(7);
X	  pt_regset.pr_eip = read_register(8);
X	  pt_regset.pr_flags = read_register(9);
X    }
X  errno = 0;
X  ptrace (XPT_WREGS, inferior_pid, &pt_regset, 0);
X  if (errno != 0)
X    {
X      sprintf (buf, "writing register number %d", regno);
X      perror_with_name (buf);
X    }
X#if 0
X  register unsigned int regaddr;
X  char buf[80];
X
X  struct user u;
X  unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
X  offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
X
X  if (regno >= 0)
X    {
X      regaddr = register_addr (regno, offset);
X      errno = 0;
X      ptrace (6, inferior_pid, regaddr, read_register (regno));
X      if (errno != 0)
X	{
X	  sprintf (buf, "writing register number %d", regno);
X	  perror_with_name (buf);
X	}
X    }
X  else for (regno = 0; regno < NUM_REGS; regno++)
X    {
X      regaddr = register_addr (regno, offset);
X      errno = 0;
X      ptrace (6, inferior_pid, regaddr, read_register (regno));
X      if (errno != 0)
X	{
X	  sprintf (buf, "writing register number %d", regno);
X	  perror_with_name (buf);
X	}
X    }
X#endif
X}
X
X/* Copy LEN bytes from inferior's memory starting at MEMADDR
X   to debugger memory starting at MYADDR. 
X   On failure (cannot read from inferior, usually because address is out
X   of bounds) returns the value of errno. */
X
Xint
Xread_inferior_memory (memaddr, myaddr, len)
X     CORE_ADDR memaddr;
X     char *myaddr;
X     int len;
X{
X  register int i;
X  /* Round starting address down to longword boundary.  */
X  register CORE_ADDR addr = memaddr & - sizeof (int);
X  /* Round ending address up; get number of longwords that makes.  */
X  register int count
X    = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
X  /* Allocate buffer of that many longwords.  */
X  register int *buffer = (int *) alloca (count * sizeof (int));
X  extern int errno;
X
X  /* Read all the longwords */
X  for (i = 0; i < count; i++, addr += sizeof (int))
X    {
X      errno = 0;
X      if (remote_debugging)
X	buffer[i] = remote_fetch_word (addr);
X      else
X	buffer[i] = ptrace (1, inferior_pid, addr, 0);
X      if (errno)
X	return errno;
X    }
X
X  /* Copy appropriate bytes out of the buffer.  */
X  bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
X  return 0;
X}
X
X/* Copy LEN bytes of data from debugger memory at MYADDR
X   to inferior's memory at MEMADDR.
X   On failure (cannot write the inferior)
X   returns the value of errno.  */
X
Xint
Xwrite_inferior_memory (memaddr, myaddr, len)
X     CORE_ADDR memaddr;
X     char *myaddr;
X     int len;
X{
X  register int i;
X  /* Round starting address down to longword boundary.  */
X  register CORE_ADDR addr = memaddr & - sizeof (int);
X  /* Round ending address up; get number of longwords that makes.  */
X  register int count
X    = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
X  /* Allocate buffer of that many longwords.  */
X  register int *buffer = (int *) alloca (count * sizeof (int));
X  extern int errno;
X
X  /* Fill start and end extra bytes of buffer with existing memory data.  */
X
X  if (remote_debugging)
X    buffer[0] = remote_fetch_word (addr);
X  else
X    buffer[0] = ptrace (1, inferior_pid, addr, 0);
X
X  if (count > 1)
X    {
X      if (remote_debugging)
X	buffer[count - 1]
X	  = remote_fetch_word (addr + (count - 1) * sizeof (int));
X      else
X	buffer[count - 1]
X	  = ptrace (1, inferior_pid,
X		    addr + (count - 1) * sizeof (int), 0);
X    }
X
X  /* Copy data to be written over corresponding part of buffer */
X
X  bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
X
X  /* Write the entire buffer.  */
X
X  for (i = 0; i < count; i++, addr += sizeof (int))
X    {
X      errno = 0;
X      if (remote_debugging)
X	remote_store_word (addr, buffer[i]);
X      else
X	ptrace (4, inferior_pid, addr, buffer[i]);
X      if (errno)
X	return errno;
X    }
X
X  return 0;
X}
X
X/* Work with core dump and executable files, for GDB. 
X   This code would be in core.c if it weren't machine-dependent. */
X
X/* Make COFF and non-COFF names for things a little more compatible
X   to reduce conditionals later.  */
X
X#ifndef COFF_FORMAT
X#define AOUTHDR struct exec
X#endif
X
Xextern char *sys_siglist[];
X
X
X/* Hook for `exec_file_command' command to call.  */
X
Xextern void (*exec_file_display_hook) ();
X   
X/* File names of core file and executable file.  */
X
Xextern char *corefile;
Xextern char *execfile;
X
X/* Descriptors on which core file and executable file are open.
X   Note that the execchan is closed when an inferior is created
X   and reopened if the inferior dies or is killed.  */
X
Xextern int corechan;
Xextern int execchan;
X
X/* Last modification time of executable file.
X   Also used in source.c to compare against mtime of a source file.  */
X
Xextern int exec_mtime;
X
X/* Virtual addresses of bounds of the two areas of memory in the core file.  */
X
Xextern CORE_ADDR data_start;
Xextern CORE_ADDR data_end;
Xextern CORE_ADDR stack_start;
Xextern CORE_ADDR stack_end;
X
X/* Virtual addresses of bounds of two areas of memory in the exec file.
X   Note that the data area in the exec file is used only when there is no core file.  */
X
Xextern CORE_ADDR text_start;
Xextern CORE_ADDR text_end;
X
Xextern CORE_ADDR exec_data_start;
Xextern CORE_ADDR exec_data_end;
X
X/* Address in executable file of start of text area data.  */
X
Xextern int text_offset;
X
X/* Address in executable file of start of data area data.  */
X
Xextern int exec_data_offset;
X
X/* Address in core file of start of data area data.  */
X
Xextern int data_offset;
X
X/* Address in core file of start of stack area data.  */
X
Xextern int stack_offset;
X
X#ifdef COFF_FORMAT
X/* various coff data structures */
X
Xextern FILHDR file_hdr;
Xextern SCNHDR text_hdr;
Xextern SCNHDR data_hdr;
X
X#endif /* not COFF_FORMAT */
X
X/* a.out header saved in core file.  */
X  
Xextern AOUTHDR core_aouthdr;
X
X/* a.out header of exec file.  */
X
Xextern AOUTHDR exec_aouthdr;
X
Xextern void validate_files ();
X
Xcore_file_command (filename, from_tty)
X     char *filename;
X     int from_tty;
X{
X  int val;
X  extern char registers[];
X
X  /* Discard all vestiges of any previous core file
X     and mark data and stack spaces as empty.  */
X
X  if (corefile)
X    free (corefile);
X  corefile = 0;
X
X  if (corechan >= 0)
X    close (corechan);
X  corechan = -1;
X
X  data_start = 0;
X  data_end = 0;
X  stack_start = STACK_END_ADDR;
X  stack_end = STACK_END_ADDR;
X
X  /* Now, if a new core file was specified, open it and digest it.  */
X
X  if (filename)
X    {
X      if (have_inferior_p ())
X	error ("To look at a core file, you must kill the inferior with \"kill\".");
X      corechan = open (filename, O_RDONLY, 0);
X      if (corechan < 0)
X	perror_with_name (filename);
X      /* 4.2-style (and perhaps also sysV-style) core dump file.  */
X      {
X	struct user u;
X
X	int reg_offset;
X
X	val = myread (corechan, &u, sizeof u);
X	if (val < 0)
X	  perror_with_name (filename);
X	data_start = exec_data_start;
X
X	data_end = data_start + NBPG * u.u_dsize;
X	stack_start = stack_end - NBPG * u.u_ssize;
X	data_offset = NBPG * UPAGES;
X	stack_offset = NBPG * (UPAGES + u.u_dsize);
X	reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
X
X	/* I don't know where to find this info.
X	   So, for now, mark it as not available.  */
X/*	N_SET_MAGIC (core_aouthdr, 0);  */
X	bzero ((char *) &core_aouthdr, sizeof core_aouthdr);
X
X	/* Read the register values out of the core file and store
X	   them where `read_register' will find them.  */
X
X	{
X	  register int regno;
X
X	  for (regno = 0; regno < NUM_REGS; regno++)
X	    {
X	      char buf[MAX_REGISTER_RAW_SIZE];
X
X	      val = lseek (corechan, register_addr (regno, reg_offset), 0);
X	      if (val < 0)
X		perror_with_name (filename);
X
X 	      val = myread (corechan, buf, sizeof buf);
X	      if (val < 0)
X		perror_with_name (filename);
X	      supply_register (regno, buf);
X	    }
X	}
X      }
X      if (filename[0] == '/')
X	corefile = savestring (filename, strlen (filename));
X      else
X	{
X	  corefile = concat (current_directory, "/", filename);
X	}
X
X      set_current_frame ( create_new_frame (read_register (FP_REGNUM),
X					    read_pc ()));
X      select_frame (get_current_frame (), 0);
X      validate_files ();
X    }
X  else if (from_tty)
X    printf ("No core file now.\n");
X}
X
Xexec_file_command (filename, from_tty)
X     char *filename;
X     int from_tty;
X{
X  int val;
X
X  /* Eliminate all traces of old exec file.
X     Mark text segment as empty.  */
X
X  if (execfile)
X    free (execfile);
X  execfile = 0;
X  data_start = 0;
X  data_end -= exec_data_start;
X  text_start = 0;
X  text_end = 0;
X  exec_data_start = 0;
X  exec_data_end = 0;
X  if (execchan >= 0)
X    close (execchan);
X  execchan = -1;
X
X  /* Now open and digest the file the user requested, if any.  */
X
X  if (filename)
X    {
X      execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
X			&execfile);
X      if (execchan < 0)
X	perror_with_name (filename);
X
X#ifdef COFF_FORMAT
X      {
X	int aout_hdrsize;
X	int num_sections;
X
X	if (read_file_hdr (execchan, &file_hdr) < 0)
X	  error ("\"%s\": not in executable format.", execfile);
X
X	aout_hdrsize = file_hdr.f_opthdr;
X	num_sections = file_hdr.f_nscns;
X
X	if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
X	  error ("\"%s\": can't read optional aouthdr", execfile);
X
X	if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections) < 0)
X	  error ("\"%s\": can't read text section header", execfile);
X
X	if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections) < 0)
X	  error ("\"%s\": can't read data section header", execfile);
X
X	text_start = exec_aouthdr.text_start;
X	text_end = text_start + exec_aouthdr.tsize;
X	text_offset = text_hdr.s_scnptr;
X	exec_data_start = exec_aouthdr.data_start;
X	exec_data_end = exec_data_start + exec_aouthdr.dsize;
X	exec_data_offset = data_hdr.s_scnptr;
X	data_start = exec_data_start;
X	data_end += exec_data_start;
X	exec_mtime = file_hdr.f_timdat;
X      }
X#else /* not COFF_FORMAT */
X      {
X	struct stat st_exec;
X
X#ifdef HEADER_SEEK_FD
X	HEADER_SEEK_FD (execchan);
X#endif
X
X	val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
X
X	if (val < 0)
X	  perror_with_name (filename);
X
X        text_start = N_TXTADDR (exec_aouthdr);
X        exec_data_start = N_DATADDR (exec_aouthdr);
X
X	text_offset = N_TXTOFF (exec_aouthdr);
X	exec_data_offset = N_DATAOFF (exec_aouthdr);
X
X	text_end = exec_data_start;
X        exec_data_end = exec_data_start + exec_aouthdr.a_data;
X	data_start = exec_data_start;
X	data_end += exec_data_start;
X
X	fstat (execchan, &st_exec);
X	exec_mtime = st_exec.st_mtime;
X      }
X#endif /* not COFF_FORMAT */
X
X      validate_files ();
X    }
X  else if (from_tty)
X    printf ("No exec file now.\n");
X
X  /* Tell display code (if any) about the changed file name.  */
X  if (exec_file_display_hook)
X    (*exec_file_display_hook) (filename);
X}
X
X/* helper functions for m-i386.h */
X
X/* stdio style buffering to minimize calls to ptrace */
Xstatic CORE_ADDR codestream_next_addr;
Xstatic CORE_ADDR codestream_addr;
Xstatic unsigned char codestream_buf[sizeof (int)];
Xstatic int codestream_off;
Xstatic int codestream_cnt;
X
X#define codestream_tell() (codestream_addr + codestream_off)
X#define codestream_peek() (codestream_cnt == 0 ? \
X			   codestream_fill(1): codestream_buf[codestream_off])
X#define codestream_get() (codestream_cnt-- == 0 ? \
X			 codestream_fill(0) : codestream_buf[codestream_off++])
X
Xstatic unsigned char 
Xcodestream_fill (peek_flag)
X{
X  codestream_addr = codestream_next_addr;
X  codestream_next_addr += sizeof (int);
X  codestream_off = 0;
X  codestream_cnt = sizeof (int);
X  read_memory (codestream_addr,
X	       (unsigned char *)codestream_buf,
X	       sizeof (int));
X  
X  if (peek_flag)
X    return (codestream_peek());
X  else
X    return (codestream_get());
X}
X
Xstatic void
Xcodestream_seek (place)
X{
X  codestream_next_addr = place & -sizeof (int);
X  codestream_cnt = 0;
X  codestream_fill (1);
X  while (codestream_tell() != place)
X    codestream_get ();
X}
X
Xstatic void
Xcodestream_read (buf, count)
X     unsigned char *buf;
X{
X  unsigned char *p;
X  int i;
X  p = buf;
X  for (i = 0; i < count; i++)
X    *p++ = codestream_get ();
X}
X
X/* next instruction is a jump, move to target */
Xstatic
Xi386_follow_jump ()
X{
X  int long_delta;
X  short short_delta;
X  char byte_delta;
X  int data16;
X  int pos;
X  
X  pos = codestream_tell ();
X  
X  data16 = 0;
X  if (codestream_peek () == 0x66)
X    {
X      codestream_get ();
X      data16 = 1;
X    }
X  
X  switch (codestream_get ())
X    {
X    case 0xe9:
X      /* relative jump: if data16 == 0, disp32, else disp16 */
X      if (data16)
X	{
X	  codestream_read ((unsigned char *)&short_delta, 2);
X	  pos += short_delta + 3; /* include size of jmp inst */
X	}
X      else
X	{
X	  codestream_read ((unsigned char *)&long_delta, 4);
X	  pos += long_delta + 5;
X	}
X      break;
X    case 0xeb:
X      /* relative jump, disp8 (ignore data16) */
X      codestream_read ((unsigned char *)&byte_delta, 1);
X      pos += byte_delta + 2;
X      break;
X    }
X  codestream_seek (pos + data16);
X}
X
X/*
X * find and return amount of local space allocated, and advance codestream to
X * first register push (if any)
X *
X * if entry sequence doesn't make sense, return -1, and leave 
X * codestream pointer random
X */
Xstatic long
Xi386_get_frame_setup (pc)
X{
X  unsigned char op;
X  
X  codestream_seek (pc);
X  
X  i386_follow_jump ();
X  
X  op = codestream_get ();
X  
X  if (op == 0x58)		/* popl %eax */
X    {
X      /*
X       * this function must start with
X       * 
X       *    popl %eax		  0x58
X       *    xchgl %eax, (%esp)  0x87 0x04 0x24
X       * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
X       *
X       * (the system 5 compiler puts out the second xchg
X       * inst, and the assembler doesn't try to optimize it,
X       * so the 'sib' form gets generated)
X       * 
X       * this sequence is used to get the address of the return
X       * buffer for a function that returns a structure
X       */
X      int pos;
X      unsigned char buf[4];
X      static unsigned char proto1[3] = { 0x87,0x04,0x24 };
X      static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
X      pos = codestream_tell ();
X      codestream_read (buf, 4);
X      if (bcmp (buf, proto1, 3) == 0)
X	pos += 3;
X      else if (bcmp (buf, proto2, 4) == 0)
X	pos += 4;
X      
X      codestream_seek (pos);
X      op = codestream_get (); /* update next opcode */
X    }
X  
X  if (op == 0x55)		/* pushl %esp */
X    {			
X      /* check for movl %esp, %ebp - can be written two ways */
X      switch (codestream_get ())
X	{
X	case 0x8b:
X	  if (codestream_get () != 0xec)
X	    return (-1);
X	  break;
X	case 0x89:
X	  if (codestream_get () != 0xe5)
X	    return (-1);
X	  break;
X	default:
X	  return (-1);
X	}
X      /* check for stack adjustment 
X       *
X       *  subl $XXX, %esp
X       *
X       * note: you can't subtract a 16 bit immediate
X       * from a 32 bit reg, so we don't have to worry
X       * about a data16 prefix 
X       */
X      op = codestream_peek ();
X      if (op == 0x83)
X	{
X	  /* subl with 8 bit immed */
X	  codestream_get ();
X	  if (codestream_get () != 0xec)
X	    return (-1);
X	  /* subl with signed byte immediate 
X	   * (though it wouldn't make sense to be negative)
X	   */
X	  return (codestream_get());
X	}
X      else if (op == 0x81)
X	{
X	  /* subl with 32 bit immed */
X	  int locals;
X	  codestream_get();
X	  if (codestream_get () != 0xec)
X	    return (-1);
X	  /* subl with 32 bit immediate */
X	  codestream_read ((unsigned char *)&locals, 4);
X	  return (locals);
X	}
X      else
X	{
X	  return (0);
X	}
X    }
X  else if (op == 0xc8)
X    {
X      /* enter instruction: arg is 16 bit unsigned immed */
X      unsigned short slocals;
X      codestream_read ((unsigned char *)&slocals, 2);
X      codestream_get (); /* flush final byte of enter instruction */
X      return (slocals);
X    }
X  return (-1);
X}
X
X/*
X * parse the first few instructions of the function to see
X * what registers were stored.
X *
X * We handle these cases:
X *
X * The startup sequence can be at the start of the function,
X * or the function can start with a branch to startup code at the end.
X *
X * %ebp can be set up with either the 'enter' instruction, or 
X * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
X * but was once used in the sys5 compiler)
X *
X * Local space is allocated just below the saved %ebp by either the
X * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
X * a 16 bit unsigned argument for space to allocate, and the
X * 'addl' instruction could have either a signed byte, or
X * 32 bit immediate.
X *
X * Next, the registers used by this function are pushed.  In
X * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
X * (and sometimes a harmless bug causes it to also save but not restore %eax);
X * however, the code below is willing to see the pushes in any order,
X * and will handle up to 8 of them.
X *
X * If the setup sequence is at the end of the function, then the
X * next instruction will be a branch back to the start.
X */
X
Xi386_frame_find_saved_regs (fip, fsrp)
X     struct frame_info *fip;
X     struct frame_saved_regs *fsrp;
X{
X  unsigned long locals;
X  unsigned char *p;
X  unsigned char op;
X  CORE_ADDR dummy_bottom;
X  CORE_ADDR adr;
X  int i;
X  
X  bzero (fsrp, sizeof *fsrp);
X  
X  /* if frame is the end of a dummy, compute where the
X   * beginning would be
X   */
X  dummy_bottom = fip->frame - 4 - NUM_REGS*4 - CALL_DUMMY_LENGTH;
X  
X  /* check if the PC is in the stack, in a dummy frame */
X  if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
X    {
X      /* all regs were saved by push_call_dummy () */
X      adr = fip->frame - 4;
X      for (i = 0; i < NUM_REGS; i++) 
X	{
X	  fsrp->regs[i] = adr;
X	  adr -= 4;
X	}
X      return;
X    }
X  
X  locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
X  
X  if (locals >= 0) 
X    {
X      adr = fip->frame - 4 - locals;
X      for (i = 0; i < 8; i++) 
X	{
X	  op = codestream_get ();
X	  if (op < 0x50 || op > 0x57)
X	    break;
X	  fsrp->regs[op - 0x50] = adr;
X	  adr -= 4;
X	}
X    }
X  
X  fsrp->regs[PC_REGNUM] = fip->frame + 4;
X  fsrp->regs[FP_REGNUM] = fip->frame;
X}
X
X/* return pc of first real instruction */
Xi386_skip_prologue (pc)
X{
X  unsigned char op;
X  int i;
X  
X  if (i386_get_frame_setup (pc) < 0)
X    return (pc);
X  
X  /* found valid frame setup - codestream now points to 
X   * start of push instructions for saving registers
X   */
X  
X  /* skip over register saves */
X  for (i = 0; i < 8; i++)
X    {
X      op = codestream_peek ();
X      /* break if not pushl inst */
X      if (op < 0x50 || op > 0x57) 
X	break;
X      codestream_get ();
X    }
X  
X  i386_follow_jump ();
X  
X  return (codestream_tell ());
X}
X
Xi386_push_dummy_frame ()
X{
X  CORE_ADDR sp = read_register (SP_REGNUM);
X  int regnum;
X  
X  sp = push_word (sp, read_register (PC_REGNUM));
X  sp = push_word (sp, read_register (FP_REGNUM));
X  write_register (FP_REGNUM, sp);
X  for (regnum = 0; regnum < NUM_REGS; regnum++)
X    sp = push_word (sp, read_register (regnum));
X  write_register (SP_REGNUM, sp);
X}
X
Xi386_pop_frame ()
X{
X  FRAME frame = get_current_frame ();
X  CORE_ADDR fp;
X  int regnum;
X  struct frame_saved_regs fsr;
X  struct frame_info *fi;
X  
X  fi = get_frame_info (frame);
X  fp = fi->frame;
X  get_frame_saved_regs (fi, &fsr);
X  for (regnum = 0; regnum < NUM_REGS; regnum++) 
X    {
X      CORE_ADDR adr;
X      adr = fsr.regs[regnum];
X      if (adr)
X	write_register (regnum, read_memory_integer (adr, 4));
X    }
X  write_register (FP_REGNUM, read_memory_integer (fp, 4));
X  write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
X  write_register (SP_REGNUM, fp + 8);
X  flush_cached_frames ();
X  set_current_frame ( create_new_frame (read_register (FP_REGNUM),
X					read_pc ()));
X}
X
X#ifndef GS
X#define GS (SS+1)
X#endif
X#ifndef FS
X#define FS (GS+1)
X#endif
X#ifndef ES
X#define ES (FS+1)
X#endif
X#ifndef DS
X#define DS (ES+1)
X#endif
X
X/* this table must line up with REGISTER_NAMES in m-i386.h */
X/* symbols like 'EAX' come from <sys/reg.h> */
Xstatic int regmap[] = 
X{
X  EAX, ECX, EDX, EBX,
X  ESP, EBP, ESI, EDI,
X  EIP, FLAGS, CS, SS,
X  DS, ES, FS, GS,
X};
X
X/* blockend is the value of u.u_ar0, and points to the
X * place where GS is stored
X */
Xi386_register_u_addr (blockend, regnum)
X{
X#if 0
X  /* this will be needed if fp registers are reinstated */
X  /* for now, you can look at them with 'info float'
X   * sys5 wont let you change them with ptrace anyway
X   */
X  if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM) 
X    {
X      int ubase, fpstate;
X      struct user u;
X      ubase = blockend + 4 * (SS + 1) - KSTKSZ;
X      fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u);
X      return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
X    } 
X  else
X#endif
X    return (blockend + 4 * regmap[regnum]);
X  
X}
X
Xi387_to_double (from, to)
X     char *from;
X     char *to;
X{
X  long *lp;
X  /* push extended mode on 387 stack, then pop in double mode
X   *
X   * first, set exception masks so no error is generated -
X   * number will be rounded to inf or 0, if necessary 
X   */
X  asm ("pushl %eax"); 		/* grab a stack slot */
X  asm ("fstcw (%esp)");		/* get 387 control word */
X  asm ("movl (%esp),%eax");	/* save old value */
X  asm ("orl $0x3f,%eax");		/* mask all exceptions */
X  asm ("pushl %eax");
X  asm ("fldcw (%esp)");		/* load new value into 387 */
X  
X  asm ("movl 8(%ebp),%eax");
X  asm ("fldt (%eax)");		/* push extended number on 387 stack */
X  asm ("fwait");
X  asm ("movl 12(%ebp),%eax");
X  asm ("fstpl (%eax)");		/* pop double */
X  asm ("fwait");
X  
X  asm ("popl %eax");		/* flush modified control word */
X  asm ("fnclex");			/* clear exceptions */
X  asm ("fldcw (%esp)");		/* restore original control word */
X  asm ("popl %eax");		/* flush saved copy */
X}
X
Xdouble_to_i387 (from, to)
X     char *from;
X     char *to;
X{
X  /* push double mode on 387 stack, then pop in extended mode
X   * no errors are possible because every 64-bit pattern
X   * can be converted to an extended
X   */
X  asm ("movl 8(%ebp),%eax");
X  asm ("fldl (%eax)");
X  asm ("fwait");
X  asm ("movl 12(%ebp),%eax");
X  asm ("fstpt (%eax)");
X  asm ("fwait");
X}
X
Xstruct env387 
X{
X  unsigned short control;
X  unsigned short r0;
X  unsigned short status;
X  unsigned short r1;
X  unsigned short tag;
X  unsigned short r2;
X  unsigned long eip;
X  unsigned short code_seg;
X  unsigned short opcode;
X  unsigned long operand;
X  unsigned short operand_seg;
X  unsigned short r3;
X  unsigned char regs[8][10];
X};
X
Xstatic
Xprint_387_control_word (control)
Xunsigned short control;
X{
X  printf ("control 0x%04x: ", control);
X  printf ("compute to ");
X  switch ((control >> 8) & 3) 
X    {
X    case 0: printf ("24 bits; "); break;
X    case 1: printf ("(bad); "); break;
X    case 2: printf ("53 bits; "); break;
X    case 3: printf ("64 bits; "); break;
X    }
X  printf ("round ");
X  switch ((control >> 10) & 3) 
X    {
X    case 0: printf ("NEAREST; "); break;
X    case 1: printf ("DOWN; "); break;
X    case 2: printf ("UP; "); break;
X    case 3: printf ("CHOP; "); break;
X    }
X  if (control & 0x3f) 
X    {
X      printf ("mask:");
X      if (control & 0x0001) printf (" INVALID");
X      if (control & 0x0002) printf (" DENORM");
X      if (control & 0x0004) printf (" DIVZ");
X      if (control & 0x0008) printf (" OVERF");
X      if (control & 0x0010) printf (" UNDERF");
X      if (control & 0x0020) printf (" LOS");
X      printf (";");
X    }
X  printf ("\n");
X  if (control & 0xe080) printf ("warning: reserved bits on 0x%x\n",
X				control & 0xe080);
X}
X
Xstatic
Xprint_387_status_word (status)
X     unsigned short status;
X{
X  printf ("status 0x%04x: ", status);
X  if (status & 0xff) 
X    {
X      printf ("exceptions:");
X      if (status & 0x0001) printf (" INVALID");
X      if (status & 0x0002) printf (" DENORM");
X      if (status & 0x0004) printf (" DIVZ");
X      if (status & 0x0008) printf (" OVERF");
X      if (status & 0x0010) printf (" UNDERF");
X      if (status & 0x0020) printf (" LOS");
X      if (status & 0x0040) printf (" FPSTACK");
X      printf ("; ");
X    }
X  printf ("flags: %d%d%d%d; ",
X	  (status & 0x4000) != 0,
X	  (status & 0x0400) != 0,
X	  (status & 0x0200) != 0,
X	  (status & 0x0100) != 0);
X  
X  printf ("top %d\n", (status >> 11) & 7);
X}
X
Xstatic
Xprint_387_status (status, ep)
X     unsigned short status;
X     struct env387 *ep;
X{
X  int i;
X  int bothstatus;
X  int top;
X  int fpreg;
X  unsigned char *p;
X  
X  bothstatus = ((status != 0) && (ep->status != 0));
X  if (status != 0) 
X    {
X      if (bothstatus)
X	printf ("u: ");
X      print_387_status_word (status);
X    }
X  
X  if (ep->status != 0) 
X    {
X      if (bothstatus)
X	printf ("e: ");
X      print_387_status_word (ep->status);
X    }
X  
X  print_387_control_word (ep->control);
X  printf ("last exception: ");
X  printf ("opcode 0x%x; ", ep->opcode);
X  printf ("pc 0x%x:0x%x; ", ep->code_seg, ep->eip);
X  printf ("operand 0x%x:0x%x\n", ep->operand_seg, ep->operand);
X  
X  top = (ep->status >> 11) & 7;
X  
X  printf ("regno  tag  msb              lsb  value\n");
X  for (fpreg = 7; fpreg >= 0; fpreg--) 
X    {
X      double val;
X      
X      printf ("%s %d: ", fpreg == top ? "=>" : "  ", fpreg);
X      
X      switch ((ep->tag >> (fpreg * 2)) & 3) 
X	{
X	case 0: printf ("valid "); break;
X	case 1: printf ("zero  "); break;
X	case 2: printf ("trap  "); break;
X	case 3: printf ("empty "); break;
X	}
X      for (i = 9; i >= 0; i--)
X	printf ("%02x", ep->regs[fpreg][i]);
X      
X      i387_to_double (ep->regs[fpreg], (char *)&val);
X      printf ("  %g\n", val);
X    }
X  if (ep->r0)
X    printf ("warning: reserved0 is 0x%x\n", ep->r0);
X  if (ep->r1)
X    printf ("warning: reserved1 is 0x%x\n", ep->r1);
X  if (ep->r2)
X    printf ("warning: reserved2 is 0x%x\n", ep->r2);
X  if (ep->r3)
X    printf ("warning: reserved3 is 0x%x\n", ep->r3);
X}
X	
Xi386_float_info ()
X{
X  struct user u; /* just for address computations */
X  int i;
X  /* fpstate defined in <sys/user.h> */
X  struct fpusave *fpusavep;
X  char buf[sizeof (struct fpusave) + 2 * sizeof (int)];
X  unsigned int uaddr;
X  char fpvalid;
X  unsigned int rounded_addr;
X  unsigned int rounded_size;
X  extern int corechan;
X  int skip;
X  
X#if 0
X  uaddr = (char *)&u.u_fpvalid - (char *)&u;
X  if (have_inferior_p()) 
X    {
X      unsigned int data;
X      unsigned int mask;
X      
X      rounded_addr = uaddr & -sizeof (int);
X      data = ptrace (3, inferior_pid, rounded_addr, 0);
X      mask = 0xff << ((uaddr - rounded_addr) * 8);
X      
X      fpvalid = ((data & mask) != 0);
X    } 
X  else 
X    {
X      if (lseek (corechan, uaddr, 0) < 0)
X	perror ("seek on core file");
X      if (myread (corechan, &fpvalid, 1) < 0) 
X	perror ("read on core file");
X      
X    }
X  
X  if (fpvalid == 0) 
X    {
X      printf ("no floating point status saved\n");
X      return;
X    }
X#endif
X  
X  uaddr = (char *)&u.u_fpusave - (char *)&u;
X  if (have_inferior_p ()) 
X    {
X      int *ip;
X      
X      rounded_addr = uaddr & -sizeof (int);
X      rounded_size = (((uaddr + sizeof (struct fpusave)) - uaddr) +
X		      sizeof (int) - 1) / sizeof (int);
X      skip = uaddr - rounded_addr;
X      
X      ip = (int *)buf;
X      for (i = 0; i < rounded_size; i++) 
X	{
X	  *ip++ = ptrace (3, inferior_pid, rounded_addr, 0);
X	  rounded_addr += sizeof (int);
X	}
X    } 
X  else 
X    {
X      if (lseek (corechan, uaddr, 0) < 0)
X	perror_with_name ("seek on core file");
X      if (myread (corechan, buf, sizeof (struct fpusave)) < 0) 
X	perror_with_name ("read from core file");
X      skip = 0;
X    }
X  
X  fpusavep = (struct fpusave *)(buf + skip);
X  print_387_status (fpusavep->fpu_status, (struct env387 *)fpusavep);
X}
X
SHAR_EOF
chmod 0664 symmetry-dep.c || echo "restore of symmetry-dep.c fails"
sed 's/^X//' << 'SHAR_EOF' > symmetry.libg.fix &&
X#! /bin/sh
X# The current version of gdb depends on libg.a having a N_SO symbol.
X# On the Symmetry under DYNIX V3.0.12 /usr/lib/libg.a does not have such a
X# symbol. This script will produce a version of libg.a that has a N_SO symbol.
X# Do not run this script in /usr/lib. Also, remember to save the original
X# version of libg.a.
Xset -x
Xif [ `pwd` = /usr/lib -o `pwd` = /usr/.lib ]
Xthen
X	echo "Do not execute in /usr/lib! Aborting..."
X	exit
Xfi
Xcat >libg.s <<FOO
X	.stabs	"libg.s",0144,0,0,LL0
XLL0:
XFOO
Xcp /usr/lib/libg.a libg1.o
Xcc -c libg.s
Xld -r -X -o libg.a libg.o libg1.o
Xchmod 444 libg.a
Xecho Now do something like
Xecho 'mv /usr/lib/libg.a /usr/lib/libg.a.seq; mv libg.a /usr/lib'
SHAR_EOF
chmod 0755 symmetry.libg.fix || echo "restore of symmetry.libg.fix fails"
exit 0
--
Johan Widen
SICS, PO Box 1263, S-164 28 KISTA, SWEDEN
Tel: +46 8 752 15 32	Ttx: 812 61 54 SICS S	Fax: +46 8 751 72 30
Internet: jw@sics.se or {mcvax,munnari,ukc,unido}!enea!sics.se!jw