home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / gdb / config / sparc-dep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-08  |  37.0 KB  |  1,457 lines

  1. /*-
  2.  * This code is derived from software copyrighted by the Free Software
  3.  * Foundation.
  4.  *
  5.  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
  6.  * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
  7.  */
  8.  
  9. #ifndef lint
  10. static char sccsid[] = "@(#)sparc-dep.c    6.4 (Berkeley) 5/8/91";
  11. #endif /* not lint */
  12.  
  13. /* Machine-dependent code which would otherwise be in inflow.c and core.c,
  14.    for GDB, the GNU debugger.
  15.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  16.    This code is for the sparc cpu.
  17.  
  18. This file is part of GDB.
  19.  
  20. GDB is free software; you can redistribute it and/or modify
  21. it under the terms of the GNU General Public License as published by
  22. the Free Software Foundation; either version 1, or (at your option)
  23. any later version.
  24.  
  25. GDB is distributed in the hope that it will be useful,
  26. but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. GNU General Public License for more details.
  29.  
  30. You should have received a copy of the GNU General Public License
  31. along with GDB; see the file COPYING.  If not, write to
  32. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  33.  
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include "defs.h"
  37. #include "param.h"
  38. #include "frame.h"
  39. #include "inferior.h"
  40. #include "obstack.h"
  41. #include "value.h"
  42.  
  43. #include <sys/param.h>
  44. #include <sys/dir.h>
  45. #include <sys/user.h>
  46. #include <signal.h>
  47. #include <sys/ioctl.h>
  48. #include <fcntl.h>
  49.  
  50. #include <sys/ptrace.h>
  51. #include <machine/reg.h>
  52.  
  53. #include <a.out.h>
  54. #include <sys/file.h>
  55. #include <sys/stat.h>
  56. #include <sys/core.h>
  57.  
  58. #ifdef KERNELDEBUG
  59. #include <kvm.h>
  60. #include <fcntl.h>
  61.  
  62. extern int kernel_debugging;
  63. kvm_t *kd;
  64.  
  65. static struct proc *cur_proc;
  66. CORE_ADDR intstack_top;
  67. CORE_ADDR intstack_bottom;
  68.  
  69. CORE_ADDR kernstack_top;
  70. CORE_ADDR kernstack_bottom;
  71.  
  72. #endif
  73.  
  74. extern int errno;
  75. extern int attach_flag;
  76.  
  77. /* This function simply calls ptrace with the given arguments.  
  78.    It exists so that all calls to ptrace are isolated in this 
  79.    machine-dependent file. */
  80. int
  81. call_ptrace (request, pid, arg3, arg4)
  82.      int request, pid, arg3, arg4;
  83. {
  84.   return ptrace (request, pid, arg3, arg4);
  85. }
  86.  
  87. void
  88. kill_inferior ()
  89. {
  90.     if (remote_debugging) {
  91. #ifdef KERNELDEBUG
  92.         if (kernel_debugging) {
  93.             /*
  94.              * It's a very, very bad idea to go away leaving
  95.              * breakpoints in a remote kernel or to leave it
  96.              * stopped at a breakpoint. 
  97.              */
  98.             clear_breakpoints();
  99.         }
  100. #endif
  101.         remote_close(0);
  102.         inferior_died();
  103.     } else if (inferior_pid != 0) {
  104.         ptrace(8, inferior_pid, 0, 0);
  105.         wait(0);
  106.         inferior_died();
  107.     }
  108. }
  109.  
  110. /* This is used when GDB is exiting.  It gives less chance of error.*/
  111.  
  112. void
  113. kill_inferior_fast ()
  114. {
  115.     if (remote_debugging) {
  116. #ifdef KERNELDEBUG
  117.         if (kernel_debugging)
  118.             clear_breakpoints();
  119. #endif
  120.         remote_close(0);
  121.     } else if (inferior_pid != 0) {
  122.         ptrace(8, inferior_pid, 0, 0);
  123.         wait(0);
  124.     }
  125. }
  126.  
  127. /*
  128.  * Simulate single-step ptrace call for sun4.  Code written by Gary
  129.  * Beihl (beihl@mcc.com); modified by Steven McCanne (mccanne@ee.lbl.gov).
  130.  */
  131.  
  132. union sparcinsn {
  133.     u_long code;
  134.     struct {
  135.         u_int op:2;
  136.         u_int a:1;
  137.         u_int cond:4;
  138.         u_int op2:3;
  139.         u_int disp22:22;
  140.     } b;
  141. };
  142.  
  143. /*
  144.  * Return the address, other than npc, that could be executed next.
  145.  * If only the possibility is npc, return 0.
  146.  * (There is only one such "other" possible address.)
  147.  */
  148. CORE_ADDR
  149. annulled_dest(insn, pc, npc)
  150.     union sparcinsn insn;
  151.     CORE_ADDR pc, npc;
  152. {
  153.     long int offset;
  154.  
  155.     if (insn.b.op == 0 && insn.b.a &&
  156.         (insn.b.op2 == 2 || insn.b.op2 == 6 || insn.b.op2 == 7)) {
  157.         offset = 4 * ((int) (insn.b.disp22 << 10) >> 10);
  158.         if (insn.b.cond == 8)
  159.             return pc + offset;
  160.         else
  161.             return npc + 4;
  162.     }
  163.     return 0;
  164. }
  165.  
  166. /* 
  167.  * Duplicated from breakpoint.c because (at least for now) this is a
  168.  * machine dependent routine.
  169.  */
  170. static char break_insn[] = BREAKPOINT;
  171.  
  172. /* From infrun.c */
  173. extern int stop_after_trap, stop_after_attach;
  174.  
  175. u_long target0_addr;
  176. u_long target0_shadow;
  177. u_long target1_addr;
  178. u_long target1_shadow;
  179.  
  180. /*
  181.  * Non-zero if we just simulated a single-step ptrace call.  This is
  182.  * needed because we cannot remove the breakpoints in the inferior
  183.  * process until after the `wait' in `wait_for_inferior'. 
  184.  * Used for sun4.
  185.  */
  186. int one_stepped;
  187.  
  188. void
  189. single_step(signal)
  190.     int signal;
  191. {
  192.     CORE_ADDR pc, target0, target1;
  193.     union sparcinsn insn;
  194.     
  195.     pc = read_register(PC_REGNUM);
  196.     (void)read_memory(pc, &insn.code, 4);
  197.  
  198.     if (!one_stepped) {
  199.         /*
  200.          * This is a hack to special case call instructions.
  201.          * If we are stepping over subroutines, find each call
  202.          * and trap on return, rather than single step until
  203.          * wait_for_inferior() discovers that we hit a new routine.
  204.          * The reason is that stepping over functions in a remote 
  205.          * kernel can have bad results when the function being 
  206.          * stepped over is used by the kernel in between traps.
  207.          * (i.e., a trap instruction gets poked into the function
  208.          * being stepped over).
  209.          */
  210.         if (step_over_calls > 0 &&
  211.             ((insn.code & 0xc0000000) == 0x40000000 || 
  212.              (insn.code & 0xfff80000) == 0x9fc00000)) {
  213.             target0 = PC_ADJUST(pc);
  214.             target1 = 0;
  215.         } else {
  216.             target0 = read_register(NPC_REGNUM);
  217.             target1 = annulled_dest(insn, pc, target0);
  218.         }
  219.         target0_addr = target0;
  220.         read_memory(target0, &target0_shadow, 4);
  221.         write_memory(target0, break_insn, 4);
  222.  
  223.         target1_addr = target1;
  224.         if (target1) {
  225.             read_memory(target1, &target1_shadow, 4);
  226.             write_memory(target1, break_insn, 4);
  227.         }
  228.         /*
  229.          * Resume the inferior.
  230.          */
  231.         if (remote_debugging)
  232.             remote_resume(0, 0);
  233.         else
  234.             ptrace(7, inferior_pid, 1, signal);
  235.         one_stepped = 1;
  236.     } else {
  237.         /* Remove breakpoints */
  238.         write_memory(target0_addr, &target0_shadow, 4);
  239.         if (target1_addr)
  240.             write_memory(target1_addr, &target1_shadow, 4);
  241.         one_stepped = 0;
  242.     }
  243. }
  244.  
  245. /* Resume execution of the inferior process.
  246.    If STEP is nonzero, single-step it.
  247.    If SIGNAL is nonzero, give it that signal.  */
  248.  
  249. void
  250. resume (step, signal)
  251.      int step;
  252.      int signal;
  253. {
  254.     errno = 0;
  255.     if (remote_debugging) {
  256.         /* invalidate the kernel stack limits */
  257.         cur_proc = 0;
  258.         remote_resume(step, signal);
  259.     } else    {
  260.         /* Sparc doesn't have single step on ptrace */
  261.         if (step)
  262.             single_step(signal);
  263.         else
  264.             ptrace(7, inferior_pid, 1, signal);
  265.         if (errno)
  266.             perror_with_name ("ptrace");
  267.     }
  268. }
  269.  
  270. #ifdef ATTACH_DETACH
  271.  
  272. /* Start debugging the process whose number is PID.  */
  273.  
  274. int
  275. attach (pid)
  276.      int pid;
  277. {
  278.   errno = 0;
  279.   ptrace (PTRACE_ATTACH, pid, 0, 0);
  280.   if (errno)
  281.     perror_with_name ("ptrace");
  282.   attach_flag = 1;
  283.   return pid;
  284. }
  285.  
  286. /* Stop debugging the process whose number is PID
  287.    and continue it with signal number SIGNAL.
  288.    SIGNAL = 0 means just continue it.  */
  289.  
  290. void
  291. detach (signal)
  292.      int signal;
  293. {
  294.   errno = 0;
  295.   ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
  296.   if (errno)
  297.     perror_with_name ("ptrace");
  298.   attach_flag = 0;
  299. }
  300. #endif /* ATTACH_DETACH */
  301.  
  302. void
  303. fetch_inferior_registers ()
  304. {
  305.     struct regs inferior_registers;
  306.     struct fp_status inferior_fp_registers;
  307.     extern char registers[];
  308.     int cwp;
  309.     struct rwindow local_and_ins;
  310.     
  311.     if (remote_debugging) {
  312.         remote_fetch_registers(registers);
  313.         return;
  314.     }
  315.     ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
  316.     ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
  317.     
  318.     registers[REGISTER_BYTE (0)] = 0;
  319.     bcopy (&inferior_registers.r_g1, ®isters[REGISTER_BYTE (1)], 15 * 4);
  320.     bcopy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
  321.            sizeof inferior_fp_registers.fpu_fr);
  322.     *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps; 
  323.     *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
  324.     *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc;
  325.     *(int *)®isters[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
  326.     /*      *(int *)®isters[REGISTER_BYTE (RP_REGNUM)] =
  327.         inferior_registers.r_o7 + 8;
  328.         bcopy (&inferior_fp_registers.Fpu_fsr,
  329.         ®isters[REGISTER_BYTE (FPS_REGNUM)],
  330.         sizeof (FPU_FSR_TYPE)); */
  331.     
  332.     read_inferior_memory (inferior_registers.r_sp,
  333.                   ®isters[REGISTER_BYTE (16)],
  334.                   16*4);
  335. }
  336.  
  337. /* Store our register values back into the inferior.
  338.    If REGNO is -1, do this for all registers.
  339.    Otherwise, REGNO specifies which register (so we can save time).  */
  340.  
  341. void
  342. store_inferior_registers (regno)
  343.      int regno;
  344. {
  345.   struct regs inferior_registers;
  346.   struct fp_status inferior_fp_registers;
  347.   extern char registers[];
  348.  
  349.   if (remote_debugging)
  350.     remote_store_registers (registers);
  351.   else
  352.     {
  353.       int in_regs = 1, in_fpregs = 1, in_fparegs, in_cpregs = 1;
  354.   
  355.       if (regno >= 0)
  356.     if (FP0_REGNUM <= regno && regno <= FP0_REGNUM + 32)
  357.       in_regs = 0;
  358.     else
  359.       in_fpregs = 0;
  360.  
  361.       if (in_regs)
  362.     {
  363.       bcopy (®isters[REGISTER_BYTE (1)],
  364.          &inferior_registers.r_g1, 15 * 4);
  365.  
  366.       inferior_registers.r_ps =
  367.         *(int *)®isters[REGISTER_BYTE (PS_REGNUM)];
  368.       inferior_registers.r_pc =
  369.         *(int *)®isters[REGISTER_BYTE (PC_REGNUM)];
  370.       inferior_registers.r_npc =
  371.         *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)];
  372.       inferior_registers.r_y =
  373.         *(int *)®isters[REGISTER_BYTE (Y_REGNUM)];
  374.  
  375.       write_inferior_memory (*(int *)®isters[REGISTER_BYTE (SP_REGNUM)],
  376.                  ®isters[REGISTER_BYTE (16)],
  377.                  16*4);
  378.     }
  379.       if (in_fpregs)
  380.     {
  381.       bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)],
  382.          &inferior_fp_registers,
  383.          sizeof inferior_fp_registers.fpu_fr);
  384.  
  385.   /*      bcopy (®isters[REGISTER_BYTE (FPS_REGNUM)],
  386.              &inferior_fp_registers.Fpu_fsr,
  387.          sizeof (FPU_FSR_TYPE));
  388.   ****/
  389.     }
  390.  
  391.       if (in_regs)
  392.     ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
  393.       if (in_fpregs)
  394.     ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
  395.     }
  396. }
  397.  
  398. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  399.    in the NEW_SUN_PTRACE case.
  400.    It ought to be straightforward.  But it appears that writing did
  401.    not write the data that I specified.  I cannot understand where
  402.    it got the data that it actually did write.  */
  403.  
  404. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  405.    to debugger memory starting at MYADDR. 
  406.    On failure (cannot read from inferior, usually because address is out
  407.    of bounds) returns the value of errno. */
  408.  
  409. int
  410. read_inferior_memory (memaddr, myaddr, len)
  411.      CORE_ADDR memaddr;
  412.      char *myaddr;
  413.      int len;
  414. {
  415.   register int i;
  416.   /* Round starting address down to longword boundary.  */
  417.   register CORE_ADDR addr = memaddr & - sizeof (int);
  418.   /* Round ending address up; get number of longwords that makes.  */
  419.   register int count
  420.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  421.   /* Allocate buffer of that many longwords.  */
  422.   register int *buffer = (int *) alloca (count * sizeof (int));
  423.   extern int errno;
  424.  
  425.   if (remote_debugging)
  426.       return (remote_read_inferior_memory(memaddr, myaddr, len));
  427.   /* Read all the longwords */
  428.   errno = 0;
  429.   for (i = 0; i < count && errno == 0; i++, addr += sizeof (int))
  430.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  431.   /* Copy appropriate bytes out of the buffer.  */
  432.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  433.   return errno;
  434. }
  435.  
  436. /* Copy LEN bytes of data from debugger memory at MYADDR
  437.    to inferior's memory at MEMADDR.
  438.    On failure (cannot write the inferior)
  439.    returns the value of errno.  */
  440.  
  441. int
  442. write_inferior_memory (memaddr, myaddr, len)
  443.      CORE_ADDR memaddr;
  444.      char *myaddr;
  445.      int len;
  446. {
  447.   register int i;
  448.   /* Round starting address down to longword boundary.  */
  449.   register CORE_ADDR addr = memaddr & - sizeof (int);
  450.   /* Round ending address up; get number of longwords that makes.  */
  451.   register int count
  452.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  453.   /* Allocate buffer of that many longwords.  */
  454.   register int *buffer = (int *) alloca (count * sizeof (int));
  455.   extern int errno;
  456.  
  457.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  458.  
  459.   if (remote_debugging)
  460.       return (remote_write_inferior_memory(memaddr, myaddr, len));
  461.  
  462.   buffer[0] = ptrace (1, inferior_pid, addr, 0);
  463.  
  464.   if (count > 1)
  465.     buffer[count - 1]
  466.       = ptrace (1, inferior_pid,
  467.             addr + (count - 1) * sizeof (int), 0);
  468.  
  469.   /* Copy data to be written over corresponding part of buffer */
  470.  
  471.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  472.  
  473.   /* Write the entire buffer.  */
  474.  
  475.   errno = 0;
  476.   for (i = 0; i < count && errno == 0; i++, addr += sizeof (int))
  477.     ptrace (4, inferior_pid, addr, buffer[i]);
  478.  
  479.   return errno;
  480. }
  481.  
  482.  
  483. /* Machine-dependent code which would otherwise be in core.c */
  484. /* Work with core dump and executable files, for GDB. */
  485.  
  486. #ifndef N_TXTADDR
  487. #define N_TXTADDR(hdr) 0
  488. #endif /* no N_TXTADDR */
  489.  
  490. #ifndef N_DATADDR
  491. #define N_DATADDR(hdr) hdr.a_text
  492. #endif /* no N_DATADDR */
  493.  
  494. /* Non-zero if this is an object (.o) file, rather than an executable.
  495.    Distinguishing between the two is rarely necessary (and seems like
  496.    a hack, but there is no other way to get the text and data
  497.    addresses--N_TXTADDR should probably take care of
  498.    this, but it doesn't).  */
  499. /* This definition will not work
  500.    if someone decides to make ld preserve relocation info.  */
  501. #define IS_OBJECT_FILE(hdr) (hdr.a_trsize != 0)
  502.  
  503. /* Make COFF and non-COFF names for things a little more compatible
  504.    to reduce conditionals later.  */
  505.  
  506. #ifdef COFF_FORMAT
  507. #define a_magic magic
  508. #endif
  509.  
  510. #ifndef COFF_FORMAT
  511. #ifndef AOUTHDR
  512. #define AOUTHDR struct exec
  513. #endif
  514. #endif
  515.  
  516. extern char *sys_siglist[];
  517.  
  518. /* Hook for `exec_file_command' command to call.  */
  519.  
  520. extern void (*exec_file_display_hook) ();
  521.    
  522. /* File names of core file and executable file.  */
  523.  
  524. extern char *corefile;
  525. extern char *execfile;
  526.  
  527. /* Descriptors on which core file and executable file are open.
  528.    Note that the execchan is closed when an inferior is created
  529.    and reopened if the inferior dies or is killed.  */
  530.  
  531. extern int corechan;
  532. extern int execchan;
  533.  
  534. /* Last modification time of executable file.
  535.    Also used in source.c to compare against mtime of a source file.  */
  536.  
  537. extern int exec_mtime;
  538.  
  539. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  540.  
  541. extern CORE_ADDR data_start;
  542. extern CORE_ADDR data_end;
  543. extern CORE_ADDR stack_start;
  544. extern CORE_ADDR stack_end;
  545.  
  546. /* Virtual addresses of bounds of two areas of memory in the exec file.
  547.    Note that the data area in the exec file is used only when there is no core file.  */
  548.  
  549. extern CORE_ADDR text_start;
  550. extern CORE_ADDR text_end;
  551.  
  552. extern CORE_ADDR exec_data_start;
  553. extern CORE_ADDR exec_data_end;
  554.  
  555. /* Address in executable file of start of text area data.  */
  556.  
  557. extern int text_offset;
  558.  
  559. /* Address in executable file of start of data area data.  */
  560.  
  561. extern int exec_data_offset;
  562.  
  563. /* Address in core file of start of data area data.  */
  564.  
  565. extern int data_offset;
  566.  
  567. /* Address in core file of start of stack area data.  */
  568.  
  569. extern int stack_offset;
  570.   
  571. #ifdef COFF_FORMAT
  572. /* various coff data structures */
  573.  
  574. extern FILHDR file_hdr;
  575. extern SCNHDR text_hdr;
  576. extern SCNHDR data_hdr;
  577.  
  578. #endif /* not COFF_FORMAT */
  579.  
  580. /* a.out header saved in core file.  */
  581.   
  582. extern AOUTHDR core_aouthdr;
  583.  
  584. /* a.out header of exec file.  */
  585.  
  586. extern AOUTHDR exec_aouthdr;
  587.  
  588. extern void validate_files ();
  589.  
  590. extern int (*core_file_hook)();
  591.  
  592. #ifdef KERNELDEBUG
  593.  
  594. /*
  595.  * Process control block.
  596.  */
  597. static struct pcb pcb;
  598. /*
  599.  * Floating point unit.
  600.  */
  601. static struct fpu fpu;
  602.  
  603. /* XXX For misc_function_vector. */
  604. #include "symtab.h" 
  605.  
  606. /*
  607.  * Read the "thing" at kernel address 'addr' into the space pointed to
  608.  * by point.  The length of the "thing" is determined by the type of p.
  609.  * Result is non-zero if transfer fails.
  610.  */
  611. #define kvread(addr, p) \
  612.     (read_memory((CORE_ADDR)(addr), (char *)(p), sizeof(*(p))))
  613.  
  614. static CORE_ADDR
  615. ksym_lookup(name)
  616.     char *name;
  617. {
  618.     struct symbol *sym;
  619.     int i;
  620.  
  621.     if ((i = lookup_misc_func(name)) < 0)
  622.         error("Kernel symbol `%s' not found.", name);
  623.  
  624.     return (misc_function_vector[i].address);
  625. }
  626.  
  627. /*
  628.  * Return the current proc.  masterprocp points to
  629.  * current proc which points to current u area.
  630.  */
  631. struct proc *
  632. curProc()
  633. {
  634.     struct proc *p;
  635.     CORE_ADDR addr = ksym_lookup("masterprocp");
  636.  
  637.     if (kvread(addr, &p))
  638.         error("cannot read proc pointer at %x\n", addr);
  639.     return p;
  640. }
  641.  
  642. /*
  643.  * (re-)set the variables that make inside_kernstack() work.
  644.  */
  645. set_kernel_boundaries(p)
  646.     struct proc *p;
  647. {
  648.     CORE_ADDR kstack;
  649.  
  650.     if (intstack_top == 0) {
  651.         intstack_top = ksym_lookup("eintstack");
  652.         intstack_bottom = ksym_lookup("intstack");
  653.     }
  654.     if (kvread(&p->p_segu, &kstack)) 
  655.         error("cannot read kernel stack pointer at %x\n", &p->p_segu);
  656.     kernstack_bottom = kstack;
  657.     kernstack_top = kstack + KERNSTACK;
  658. }
  659.  
  660. inside_kernstack(addr)
  661.     CORE_ADDR addr;
  662. {
  663.     if (cur_proc == 0) {
  664.         cur_proc = curProc();
  665.         set_kernel_boundaries(cur_proc);
  666.     }
  667.     return (addr > intstack_bottom && addr < intstack_top) ||
  668.         (addr > kernstack_bottom && addr < kernstack_top);
  669. }
  670.  
  671. clear_regs()
  672. {
  673.     u_long reg = 0;
  674.     float freg = 0.0;
  675.     int i;
  676.  
  677.     for (i = 0; i < FP0_REGNUM; ++i) 
  678.         supply_register(i, ®);
  679.     for (; i < FP0_REGNUM + 32; ++i) /* XXX */
  680.         supply_register(i, &freg);
  681.     for (; i < NUM_REGS; ++i) 
  682.         supply_register(i, ®);
  683. }
  684.  
  685. static void
  686. read_pcb()
  687. {
  688.     struct user *uaddr;
  689.     int i;
  690.     u_long cps, reg, sp;
  691.     float freg;
  692.     struct rwindow win;
  693.  
  694.     /* find the pcb for the current process */
  695.     if (kvread(&cur_proc->p_uarea, &uaddr))
  696.         error("cannot u area ptr for proc at 0x%x", cur_proc);
  697.     if (kvread(&uaddr->u_pcb, &pcb))
  698.         error("cannot read pcb at 0x%x", &uaddr->u_pcb);
  699.     /*
  700.      * Zero out register set then fill in the ones we know about.
  701.      */
  702.     clear_regs();
  703.     sp = pcb.pcb_sp;
  704.     printf("sp=%x pc=%x sr=%x\n", sp, pcb.pcb_pc, pcb.pcb_psr);
  705.     supply_register(SP_REGNUM, (char *)&pcb.pcb_sp);
  706.     supply_register(PC_REGNUM, (char *)&pcb.pcb_pc);
  707.     /* PC came from o7. */
  708.     supply_register(15, (char *)&pcb.pcb_pc);
  709.     supply_register(PS_REGNUM, (char *)&pcb.pcb_psr);
  710.     /* XXX There should be a WIM_REGNUM. */
  711.     supply_register(66, (char *)&pcb.pcb_uwm);
  712.     /*
  713.      * Read last register window saved on stack.
  714.      */
  715.     if (kvread(sp, &win)) {
  716.         printf("cannot read register window at sp=%x\n", pcb.pcb_sp);
  717.         bzero((char *)&win, sizeof win);
  718.     }
  719.     for (i = 0; i < sizeof(win.rw_local); ++i)
  720.         supply_register(i + 16, &win.rw_local[i]);
  721.     for (i = 0; i < sizeof(win.rw_in); ++i)
  722.         supply_register(i + 24, &win.rw_in[i]);
  723.     /*
  724.      * read the globals & outs saved on the stack (for a trap frame).
  725.      */
  726.     sp += 92 + 12; /* XXX - MINFRAME + R_Y */
  727.     for (i = 1; i < 14; ++i) {
  728.         u_long val;
  729.  
  730.         if (kvread(sp + i*4, &val) == 0)
  731.             supply_register(i, (char *)&val);
  732.     }
  733.     if (kvread(pcb.pcb_cpctxp, &cps) == 0)
  734.         supply_register(CPS_REGNUM, (char *)&cps);
  735. }
  736.  
  737. /*
  738.  * Set the process context to that of the proc structure at
  739.  * system address paddr.
  740.  *
  741.  * This is REALLY STUPID.  The only way to tell libkvm that we want to 
  742.  * change user address maps is with kvm_getu.
  743.  */
  744. set_procaddr(paddr)
  745.     CORE_ADDR paddr;
  746. {
  747.     struct proc p;
  748.     struct user *uaddr;
  749.  
  750.     if (paddr < KERNELBASE)
  751.         return (1);
  752.  
  753.     if (cur_proc == NULL)
  754.         if (kvread(ksym_lookup("proc"), &cur_proc))
  755.             error("cannot find proc table");
  756.     if (kvread(paddr, &p))
  757.         error("cannot read proc struct at 0x%x", paddr);
  758.     if (kd)
  759.         if (kvm_getu(kd, (u_long *)&p) == 0) {
  760.             (void)kvread(cur_proc, &p);
  761.             (void)kvm_getu(kd, (u_long *)&p);
  762.             error("cannot read uarea for proc at 0x%x", paddr);
  763.             return (1);
  764.         }
  765.     cur_proc = (struct proc *)paddr;
  766.     read_pcb();
  767.     set_kernel_boundaries(cur_proc);
  768.     return (0);
  769. }
  770.  
  771. static void
  772. setup_kernel_corefile(namefile, corefile)
  773.     char *namefile, *corefile;
  774. {
  775.     struct stat stb;
  776.     int kmem = 0;
  777.     CORE_ADDR addr, paddr;
  778.     char buf[256], *cp;
  779.  
  780.     if (strcmp(corefile, "/dev/mem") == 0)
  781.         /* XXX - Sun libkvm botch: this is only way to get
  782.          * correct mappings for swap set up. */
  783.         kd = kvm_open(namefile, (char *)0, (char *)0, O_RDONLY,
  784.                   (char *)0);
  785.     else
  786.         kd = kvm_open(namefile, corefile, (char *)0, O_RDONLY,
  787.                   (char *)0);
  788.     if (kd == 0) {
  789.         printf("Cannot open '%s' as core file of '%s'\n",
  790.                corefile, namefile);
  791.         return;
  792.     }
  793.     /*
  794.      * Need to find current u area to get kernel stack and pcb
  795.      * where "panic" saved registers.  
  796.      * (libkvm also needs to know current u area to get user
  797.      * address space mapping).
  798.      */
  799.     (void)set_procaddr(curProc());
  800.  
  801.     /* print out the panic string if there is one */
  802.     if (kvread(ksym_lookup("panicstr"), &addr) || addr == 0 ||
  803.         read_memory(addr, buf, sizeof(buf)))
  804.         return;
  805.  
  806.     for (cp = buf; cp < &buf[sizeof(buf)] && *cp; cp++)
  807.         if (!isascii(*cp) || (!isprint(*cp) && !isspace(*cp)))
  808.             *cp = '?';
  809.     *cp = '\0';
  810.     if (buf[0] != '\0')
  811.         printf("panic: %s\n", buf);
  812. }
  813.  
  814. set_paddr_command(arg)
  815.     char *arg;
  816. {
  817.     u_int paddr, uaddr;
  818.     
  819.     if (!arg)
  820.         error_no_arg("proc address for new current process");
  821.     if (!kernel_debugging)
  822.         error("not debugging kernel");
  823.  
  824.     paddr = (u_int)parse_and_eval_address(arg);
  825.     if (set_procaddr(paddr))
  826.         error("invalid proc address");
  827.  
  828.     flush_cached_frames();
  829.     set_current_frame(create_new_frame(read_register(FP_REGNUM),
  830.                        read_pc()));
  831.     select_frame(get_current_frame(), 0);
  832. }
  833.  
  834. /*
  835.  * read len bytes from kernel virtual address 'addr' into local 
  836.  * buffer 'buf'.  Return 0 if read ok, 1 otherwise.  On read
  837.  * errors, portion of buffer not read is zeroed.
  838.  */
  839. kernel_core_file_hook(addr, buf, len)
  840.     CORE_ADDR addr;
  841.     char *buf;
  842.     int len;
  843. {
  844.     int i, cc;
  845.  
  846.     if (kd == 0)
  847.         error("no kernel core file");
  848.  
  849.     cc = kvm_read(kd, (u_long)addr, buf, len);
  850.     if (cc == len)
  851.         return (0);
  852.     if (cc < 0)
  853.         cc = 0;
  854.     bzero(buf, len - cc);
  855.     return (1);
  856. }
  857.  
  858. static int
  859. is_a_vmunix(name)
  860.     char *name;
  861. {
  862.     register char *cp;
  863.  
  864.     if (name && (cp = strstr(name, "vmunix")) &&
  865.         (cp == name || cp[-1] == '/'))
  866.         return (1);
  867.     return (0);
  868. }
  869. #endif
  870.  
  871. void
  872. core_file_command (filename, from_tty)
  873.      char *filename;
  874.      int from_tty;
  875. {
  876.     int val, sp;
  877.     extern char registers[];
  878. #ifdef KERNELDEBUG
  879.     struct stat stb;
  880.  
  881.     if (kd != 0) {
  882.         kvm_close(kd);
  883.         kd = 0;
  884.     }
  885. #endif
  886.     /* Discard all vestiges of any previous core file
  887.        and mark data and stack spaces as empty.  */
  888.     
  889.     if (corefile)
  890.         free (corefile);
  891.     corefile = 0;
  892.     
  893.     if (corechan >= 0)
  894.         close (corechan);
  895.     corechan = -1;
  896.     
  897.     data_start = 0;
  898.     data_end = 0;
  899.     stack_start = STACK_END_ADDR;
  900.     stack_end = STACK_END_ADDR;
  901.  
  902.     if (filename == 0) {
  903.         if (from_tty)
  904.             printf("No core file now.\n");
  905.         return;
  906.     }
  907.     filename = tilde_expand (filename);
  908.     make_cleanup (free, filename);
  909.       
  910.     if (have_inferior_p ())
  911.         error ("To look at a core file, you must kill the inferior with \"kill\".");
  912. #ifdef KERNELDEBUG
  913.     if (!kernel_debugging && is_a_vmunix(execfile)) {
  914.         kernel_debugging = 1;
  915.         set_prompt_command("(kgdb)");
  916.     }
  917.     if (kernel_debugging) {
  918.         core_file_hook = kernel_core_file_hook;    
  919.         setup_kernel_corefile(execfile, filename);
  920.         goto finish;
  921.     }
  922. #endif
  923.     corechan = open (filename, O_RDONLY, 0);
  924.     if (corechan < 0)
  925.         perror_with_name (filename);
  926. #ifdef KERNELDEBUG
  927.     fstat(corechan, &stb);
  928.     if ((stb.st_mode & S_IFMT) == S_IFCHR &&
  929.         stb.st_rdev == makedev(3, 1)) {
  930.         /* looking at /dev/kmem */
  931.         data_offset = data_start = KERNELBASE;
  932.         data_end = ~0; /* XXX */
  933.         stack_end = stack_start = data_end;
  934.         set_kernel_boundaries(curProc());
  935.         goto finish;
  936.     } 
  937. #endif
  938.     {
  939.         struct core corestr;
  940.         
  941.         val = myread (corechan, &corestr, sizeof corestr);
  942.         if (val < 0)
  943.             perror_with_name (filename);
  944.         if (corestr.c_magic != CORE_MAGIC)
  945.             error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
  946.                    filename, corestr.c_magic, (int) CORE_MAGIC);
  947.         else if (sizeof (struct core) != corestr.c_len)
  948.             error ("\"%s\" has an invalid struct core length (%d, expected %d)",
  949.                    filename, corestr.c_len, (int) sizeof (struct core));
  950.         
  951.         data_start = exec_data_start;
  952.         data_end = data_start + corestr.c_dsize;
  953.         stack_start = stack_end - corestr.c_ssize;
  954.         data_offset = sizeof corestr;
  955.         stack_offset = sizeof corestr + corestr.c_dsize;
  956.         
  957.         /* G0 *always* holds 0.  */
  958.         *(int *)®isters[REGISTER_BYTE (0)] = 0;
  959.         /* The globals and output registers.  */
  960.         
  961.         bcopy (&corestr.c_regs.r_g1, ((int *) registers) + 1, 15 * 4);
  962.         *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = corestr.c_regs.r_ps;
  963.         *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = corestr.c_regs.r_pc;
  964.         *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)] = corestr.c_regs.r_npc;
  965.         *(int *)®isters[REGISTER_BYTE (Y_REGNUM)] = corestr.c_regs.r_y;
  966.         
  967.         /* My best guess at where to get the locals and input
  968.            registers is exactly where they usually are, right above
  969.            the stack pointer.  If the core dump was caused by a bus
  970.            writing off the stack pointer (as is possible) then this
  971.            won't work, but it's worth the try. */
  972.         
  973.         sp = *(int *)®isters[REGISTER_BYTE (SP_REGNUM)];
  974.         lseek (corechan, sp - stack_start + stack_offset, L_SET);
  975.         if (16 * 4 != myread (corechan,
  976.                       ®isters[REGISTER_BYTE (16)],
  977.                       16 * 4))
  978.             /* fprintf so user can still use gdb */
  979.             fprintf (stderr, "Couldn't read input and local registers from core file\n");
  980.         
  981.         bcopy (corestr.c_fpu.fpu_regs,
  982.                ®isters[REGISTER_BYTE (FP0_REGNUM)],
  983.                sizeof corestr.c_fpu.fpu_regs);
  984. #ifdef FPU
  985.         bcopy (&corestr.c_fpu.fpu_fsr,
  986.                ®isters[REGISTER_BYTE (FPS_REGNUM)],
  987.                sizeof (FPU_FSR_TYPE));
  988. #endif
  989.         
  990.         bcopy (&corestr.c_aouthdr, &core_aouthdr, sizeof (struct exec));
  991.         
  992.         printf ("Core file is from \"%s\".\n", corestr.c_cmdname);
  993.         if (corestr.c_signo > 0)
  994.             printf ("Program terminated with signal %d, %s.\n",
  995.                 corestr.c_signo,
  996.                 corestr.c_signo < NSIG
  997.                 ? sys_siglist[corestr.c_signo]
  998.                 : "(undocumented)");
  999.     }
  1000.  finish:
  1001.     if (filename[0] == '/')
  1002.         corefile = savestring (filename, strlen (filename));
  1003.     else
  1004.         corefile = concat (current_directory, "/", filename);
  1005.     
  1006.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  1007.                           read_pc ()));
  1008.     select_frame (get_current_frame (), 0);
  1009.     validate_files ();
  1010. }
  1011.  
  1012. void
  1013. exec_file_command (filename, from_tty)
  1014.      char *filename;
  1015.      int from_tty;
  1016. {
  1017.   int val;
  1018.  
  1019.   /* Eliminate all traces of old exec file.
  1020.      Mark text segment as empty.  */
  1021.  
  1022.   if (execfile)
  1023.     free (execfile);
  1024.   execfile = 0;
  1025.   data_start = 0;
  1026.   data_end -= exec_data_start;
  1027.   text_start = 0;
  1028.   text_end = 0;
  1029.   exec_data_start = 0;
  1030.   exec_data_end = 0;
  1031.   if (execchan >= 0)
  1032.     close (execchan);
  1033.   execchan = -1;
  1034.  
  1035.   /* Now open and digest the file the user requested, if any.  */
  1036.  
  1037.   if (filename)
  1038.     {
  1039.       filename = tilde_expand (filename);
  1040.       make_cleanup (free, filename);
  1041.       
  1042.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  1043.             &execfile);
  1044.       if (execchan < 0)
  1045.     perror_with_name (filename);
  1046.  
  1047. #ifdef COFF_FORMAT
  1048.       {
  1049.     int aout_hdrsize;
  1050.     int num_sections;
  1051.  
  1052.     if (read_file_hdr (execchan, &file_hdr) < 0)
  1053.       error ("\"%s\": not in executable format.", execfile);
  1054.  
  1055.     aout_hdrsize = file_hdr.f_opthdr;
  1056.     num_sections = file_hdr.f_nscns;
  1057.  
  1058.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  1059.       error ("\"%s\": can't read optional aouthdr", execfile);
  1060.  
  1061.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  1062.                   aout_hdrsize) < 0)
  1063.       error ("\"%s\": can't read text section header", execfile);
  1064.  
  1065.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  1066.                   aout_hdrsize) < 0)
  1067.       error ("\"%s\": can't read data section header", execfile);
  1068.  
  1069.     text_start = exec_aouthdr.text_start;
  1070.     text_end = text_start + exec_aouthdr.tsize;
  1071.     text_offset = text_hdr.s_scnptr;
  1072.     exec_data_start = exec_aouthdr.data_start;
  1073.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  1074.     exec_data_offset = data_hdr.s_scnptr;
  1075.     data_start = exec_data_start;
  1076.     data_end += exec_data_start;
  1077.     exec_mtime = file_hdr.f_timdat;
  1078.       }
  1079. #else /* not COFF_FORMAT */
  1080.       {
  1081.     struct stat st_exec;
  1082.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  1083.  
  1084.     if (val < 0)
  1085.       perror_with_name (filename);
  1086.  
  1087.     if (kernel_debugging)
  1088.         text_start = exec_aouthdr.a_entry;
  1089.     else
  1090.         text_start = IS_OBJECT_FILE (exec_aouthdr) ? 
  1091.             0 : N_TXTADDR (exec_aouthdr);
  1092.  
  1093.     exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
  1094.         ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
  1095.     text_offset = N_TXTOFF (exec_aouthdr);
  1096.     exec_data_offset = N_DATOFF (exec_aouthdr);
  1097.  
  1098.     text_end = text_start + exec_aouthdr.a_text;
  1099.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  1100.     data_start = exec_data_start;
  1101.     data_end += exec_data_start;
  1102.  
  1103.     fstat (execchan, &st_exec);
  1104.     exec_mtime = st_exec.st_mtime;
  1105.       }
  1106. #endif /* not COFF_FORMAT */
  1107.  
  1108.       validate_files ();
  1109.     }
  1110.   else if (from_tty)
  1111.     printf ("No exec file now.\n");
  1112.  
  1113.   /* Tell display code (if any) about the changed file name.  */
  1114.   if (exec_file_display_hook)
  1115.     (*exec_file_display_hook) (filename);
  1116. }
  1117.  
  1118. #ifndef offsetof
  1119. #define offsetof(t, f)  ((int)(&((t *)0)->f))
  1120. #endif
  1121.  
  1122. /*
  1123.  * Return the address of the saved pc in frame.
  1124.  */
  1125. CORE_ADDR
  1126. addr_of_pc(frame)
  1127.     struct frame_info *frame;
  1128. {
  1129.     CORE_ADDR addr;
  1130.  
  1131. #ifdef KERNELDEBUG
  1132.     /*
  1133.      * If we are kernel debugging, we must special case trap frames.
  1134.      * We can tell if we are a trap frame by looking at the return 
  1135.      * address of the frame below us.  If it is in locore, then
  1136.      * we are such a frame and we can find our saved pc in %l1.
  1137.      */
  1138.     if (kernel_debugging && frame->next) {
  1139.         static CORE_ADDR locore_h, locore_t;
  1140.     
  1141.         if (locore_h == 0) {
  1142.             locore_h = ksym_lookup("sys_trap");
  1143.             locore_t = ksym_lookup("kadb_tcode");
  1144.         }
  1145.         addr = GET_RWINDOW_REG(frame->next->bottom, rw_in[7]);
  1146.         if (addr > locore_h && addr < locore_t)
  1147.             return frame->bottom + 
  1148.                 offsetof(struct rwindow, rw_local[1]);
  1149.     }
  1150. #endif
  1151.     return (CORE_ADDR)&((struct rwindow *)frame->bottom)->rw_in[7];
  1152. }
  1153.  
  1154. /*
  1155.  * Find the pc saved in frame FRAME.  
  1156.  */
  1157. CORE_ADDR
  1158. frame_saved_pc(frame)
  1159.     FRAME frame;
  1160. {
  1161.     return PC_ADJUST(read_memory_integer(addr_of_pc(frame), 4));
  1162. }
  1163.  
  1164. /*
  1165.  * Since an individual frame in the frame cache is defined by two
  1166.  * arguments (a frame pointer and a stack pointer), we need two
  1167.  * arguments to get info for an arbitrary stack frame.  This routine
  1168.  * takes two arguments and makes the cached frames look as if these
  1169.  * two arguments defined a frame on the cache.  This allows the rest
  1170.  * of info frame to extract the important arguments without
  1171.  * difficulty. 
  1172.  */
  1173. FRAME
  1174. setup_arbitrary_frame (frame, stack)
  1175.      FRAME_ADDR frame, stack;
  1176. {
  1177.   struct frame_info *fci;
  1178.   FRAME fid = create_new_frame (frame, 0);
  1179.  
  1180.   if (!fid)
  1181.     fatal ("internal: create_new_frame returned invalid frame id");
  1182.   
  1183.   fid->bottom = stack;
  1184.  
  1185.   return fid;
  1186. }
  1187.  
  1188. /* This code was written by Gary Beihl (beihl@mcc.com).
  1189.    It was modified by Michael Tiemann (tiemann@corto.inria.fr). */
  1190.  
  1191. struct command_line *get_breakpoint_commands ();
  1192.  
  1193. /*
  1194.  * This routine takes a program counter value.  It restores the
  1195.  * register window system to the frame above the current one, and sets
  1196.  * the pc and npc to the correct values.
  1197.  */
  1198.  
  1199. /*    The following insns translate to:
  1200.  
  1201.      restore
  1202.      t g0,0x1,o0
  1203.      sethi %hi(0x0), g0    */
  1204.  
  1205. static int restore_insn_opcodes[] = { 0x81e80000, 0x91d02001, 0x01000000 };
  1206.  
  1207. void
  1208. do_restore_insn (pc)
  1209.      CORE_ADDR pc;
  1210. {
  1211.   CORE_ADDR sp = read_register (SP_REGNUM);
  1212.   CORE_ADDR npc = pc + 4;
  1213.   CORE_ADDR fake_pc = sp - sizeof (restore_insn_opcodes);
  1214.   struct inferior_status inf_status;
  1215.  
  1216.   save_inferior_status (&inf_status, 0); /* Don't restore stack info */
  1217.  
  1218.   if (!pc)
  1219.     abort();
  1220.  
  1221.   write_memory (fake_pc, restore_insn_opcodes, sizeof (restore_insn_opcodes));
  1222.  
  1223.   clear_proceed_status ();
  1224.   stop_after_trap = 1;
  1225.   proceed (fake_pc, 0, 0);
  1226.  
  1227.   write_register (PC_REGNUM, pc);
  1228.   write_register (NPC_REGNUM, npc);
  1229.   restore_inferior_status (&inf_status);
  1230. }
  1231.  
  1232. /*
  1233.  * This routine should be more specific in it's actions; making sure
  1234.  * that it uses the same register in the initial prologue section.
  1235.  */
  1236. CORE_ADDR 
  1237. skip_prologue (pc)
  1238.      CORE_ADDR pc;
  1239. {
  1240.   union
  1241.     {
  1242.       unsigned long int code;
  1243.       struct
  1244.     {
  1245.       unsigned int op:2;
  1246.       unsigned int rd:5;
  1247.       unsigned int op2:3;
  1248.       unsigned int imm22:22;
  1249.     } sethi;
  1250.       struct
  1251.     {
  1252.       unsigned int op:2;
  1253.       unsigned int rd:5;
  1254.       unsigned int op3:6;
  1255.       unsigned int rs1:5;
  1256.       unsigned int i:1;
  1257.       unsigned int simm13:13;
  1258.     } add;
  1259.       int i;
  1260.     } x;
  1261.   int dest = -1;
  1262.  
  1263.   x.i = read_memory_integer (pc, 4);
  1264.  
  1265.   /* Recognize the `sethi' insn and record its destination.  */
  1266.   if (x.sethi.op == 0 && x.sethi.op2 == 4)
  1267.     {
  1268.       dest = x.sethi.rd;
  1269.       pc += 4;
  1270.       x.i = read_memory_integer (pc, 4);
  1271.     }
  1272.  
  1273.   /* Recognize an add immediate value to register to either %g1 or
  1274.      the destination register recorded above.  Actually, this might
  1275.      well recognize several different arithmetic operations.  */
  1276.   if (x.add.op == 2 && x.add.i && (x.add.rd == 1 || x.add.rd == dest))
  1277.     {
  1278.       pc += 4;
  1279.       x.i = read_memory_integer (pc, 4);
  1280.     }
  1281.  
  1282.   /* This recognizes any SAVE insn.  But why do the XOR and then
  1283.      the compare?  That's identical to comparing against 60 (as long
  1284.      as there isn't any sign extension).  */
  1285.   if (x.add.op == 2 && (x.add.op3 ^ 32) == 28)
  1286.     {
  1287.       pc += 4;
  1288.       x.i = read_memory_integer (pc, 4);
  1289.     }
  1290.  
  1291.   /* Now we need to recognize stores into the frame from the input
  1292.      registers.  This recognizes all non alternate stores of input
  1293.      register, into a location offset from the frame pointer.  */
  1294.   while (x.add.op == 3
  1295.      && (x.add.op3 & 0x3c) == 4 /* Store, non-alternate.  */
  1296.      && (x.add.rd & 0x18) == 0x18 /* Input register.  */
  1297.      && x.add.i        /* Immediate mode.  */
  1298.      && x.add.rs1 == 30    /* Off of frame pointer.  */
  1299.      /* Into reserved stack space.  */
  1300.      && x.add.simm13 >= 0x44
  1301.      && x.add.simm13 < 0x5b)
  1302.     {
  1303.       pc += 4;
  1304.       x.i = read_memory_integer (pc, 4);
  1305.     }
  1306.   return pc;
  1307. }
  1308.  
  1309. int dummy_code[] = {
  1310.     0xd003a044,        /* ld    [%sp + 68], %o0 */
  1311.     0xd203a048,        /* ld    [%sp + 72], %o1 */
  1312.     0xd403a04c,        /* ld    [%sp + 76], %o2 */
  1313.     0xd603a050,        /* ld    [%sp + 80], %o3 */
  1314.     0xd803a054,        /* ld    [%sp + 84], %o4 */
  1315. #define DUMMY_CALL_INDEX 5
  1316.     0x40000000,        /* call .        */
  1317.     0xda03a058,        /* ld    [%sp + 88], %o5 */
  1318.     0x01000000,        /* nop - extra insn for Sun cc */
  1319.     0x91d02001,        /* ta    1        */
  1320. };
  1321.  
  1322. /*
  1323.  * Leave room on the stack for the kernel save area and the pointer
  1324.  * for structure return values.
  1325.  */
  1326. #define KSA_AND_STRUCT_ADJUST 68
  1327.  
  1328. /*
  1329.  * Build `dummy' call instructions on inferior's stack to cause
  1330.  * it to call a subroutine.
  1331.  *
  1332.  * N.B. - code in wait_for_inferior requires that sp < pc < fp when
  1333.  * we take the trap 2 above so it will recognize that we stopped
  1334.  * at a `dummy' call.  So, after the call sp is *not* decremented
  1335.  * to clean the arguments, code & other stuff we lay on the stack.
  1336.  * Since the regs are restored to saved values at the breakpoint,
  1337.  * sp will get reset correctly.  Also, this restore means we don't
  1338.  * have to construct frame linkage info to save pc & fp.  The lack
  1339.  * of frame linkage means we can't do a backtrace, etc., if the
  1340.  * called function gets a fault or hits a breakpoint but code in
  1341.  * run_stack_dummy makes this impossible anyway.
  1342.  */
  1343. CORE_ADDR
  1344. setup_dummy(sp, funaddr, nargs, args, struct_return_bytes, pushfn)
  1345.     CORE_ADDR sp;
  1346.     CORE_ADDR funaddr;
  1347.     int nargs;
  1348.     value *args;
  1349.     int struct_return_bytes;
  1350.     CORE_ADDR (*pushfn)();
  1351. {
  1352.     int len, padding, i;
  1353.     CORE_ADDR top = sp, struct_addr, pc;
  1354.  
  1355.     pc = sp - sizeof(dummy_code);
  1356.     len = arg_stacklen(nargs, args) + KSA_AND_STRUCT_ADJUST +
  1357.         sizeof(dummy_code) + struct_return_bytes;
  1358.     padding = STACK_ALIGN(len) - len;
  1359.     sp = pc - padding - struct_return_bytes;
  1360.     struct_addr = sp;
  1361.     for (i = 0; i < nargs; ++i) {
  1362.         /* pushfn doesn't actually change SP_REGNUM */
  1363.         sp = (*pushfn)(sp, args[i]);
  1364.     }
  1365.     sp -= KSA_AND_STRUCT_ADJUST;
  1366.     if (struct_return_bytes)
  1367.         write_memory(sp + KSA_AND_STRUCT_ADJUST - 4,
  1368.                  (char *)&struct_addr, 4);
  1369.  
  1370.     write_register(SP_REGNUM, sp);
  1371.     dummy_code[DUMMY_CALL_INDEX] = 0x40000000 | 
  1372.         ((unsigned)(funaddr - (pc + 4 * DUMMY_CALL_INDEX)) >> 2);
  1373.  
  1374.     write_memory(pc, (char *)dummy_code, sizeof(dummy_code));
  1375.  
  1376.     return pc;
  1377. }
  1378.  
  1379. int default_function_nargs = 4;
  1380.  
  1381. set_default_funargs_command(arg)
  1382.     char *arg;
  1383. {
  1384.     if (arg == 0 || arg[0] == 0)
  1385.         printf("%d\n", default_function_nargs);
  1386.     else
  1387.         default_function_nargs = atoi(arg);
  1388. }
  1389.  
  1390. frame_find_saved_regs(fi, srp)
  1391.     struct frame_info *fi;
  1392.     struct frame_saved_regs *srp;
  1393. {
  1394.     register int i;
  1395.     register CORE_ADDR pc;
  1396.  
  1397.     FRAME_ADDR frame = read_register (FP_REGNUM);
  1398.     FRAME fid = FRAME_INFO_ID (fi);
  1399.     if (!fid) fatal ("Bad frame info struct in FRAME_FIND_SAVED_REGS");
  1400.     bzero ((char *)srp, sizeof *srp);
  1401.     if (fi->pc >= (fi->bottom ? fi->bottom :
  1402.              read_register (SP_REGNUM)) &&
  1403.         fi->pc <= FRAME_FP(fi)) {
  1404.  
  1405.         for (i = 1; i < 8; i++)
  1406.             srp->regs[i] = frame + i * 4 - 0xa0;
  1407.         for (i = 24; i < 32; i++)
  1408.             srp->regs[i] = frame + (i - 24) * 4 - 0xc0;
  1409.         for (i = FP0_REGNUM; i < FP0_REGNUM + 32; i++)
  1410.             srp->regs[i] = frame + (i - FP0_REGNUM) * 4 - 0x80;
  1411.         for (i = 64; i < NUM_REGS; i++)
  1412.             srp->regs[i] = frame + (i - 64) * 4 - 0xe0;
  1413.         frame = fi->bottom ? fi->bottom : read_register (SP_REGNUM);
  1414.     } else {
  1415.         frame = fi->bottom ? fi->bottom : read_register (SP_REGNUM);
  1416.         for (i = 16; i < 32; i++)
  1417.             srp->regs[i] = frame + (i-16) * 4;
  1418.     }
  1419.     if (fi->next) {
  1420.         /* Pull off either the next frame pointer or
  1421.            the stack pointer */
  1422.         FRAME_ADDR next_next_frame =(fi->next->bottom ?
  1423.                          fi->next->bottom :
  1424.                          read_register (SP_REGNUM));
  1425.         for (i = 8; i < 16; i++)
  1426.             srp->regs[i] = next_next_frame + i * 4;
  1427.     }
  1428.     /* Otherwise, whatever we would get from ptrace(GETREGS) */
  1429.     /* is accurate */
  1430.     for (i = 30; i < 32; i++)
  1431.         srp->regs[i] = frame + (i-16) * 4;
  1432.     srp->regs[SP_REGNUM] = FRAME_FP (fi);
  1433.     srp->regs[PC_REGNUM] =
  1434. #ifdef KERNELDEBUG
  1435.         kernel_debugging ? addr_of_pc(fi) :
  1436. #endif
  1437.         frame + 15*4;
  1438. }
  1439.  
  1440. extern struct cmd_list_element *setlist;
  1441.  
  1442. #ifdef KERNELDEBUG
  1443. void
  1444.     _initialize_sparc_dep()
  1445. {
  1446.     add_com ("process-address", class_obscure, set_paddr_command,
  1447. "The process with proc structure at ADDR becomes the\n\
  1448. \"current\" process context for kernel debugging.");
  1449.     add_com_alias ("paddr", "process-address", class_obscure, 0);
  1450.     add_cmd("default-funargs", class_support,
  1451.         set_default_funargs_command,
  1452. "Set the number of arguments to be printed for functions with\n\
  1453. no debugging info.\n",
  1454.         &setlist);
  1455. }
  1456. #endif
  1457.