gdb/mips-linux-nat.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Native-dependent code for GNU/Linux on MIPS processors.

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "command.h"
  16. #include "gdbcmd.h"
  17. #include "inferior.h"
  18. #include "mips-tdep.h"
  19. #include "target.h"
  20. #include "regcache.h"
  21. #include "linux-nat.h"
  22. #include "mips-linux-tdep.h"
  23. #include "target-descriptions.h"

  24. #include "gdb_proc_service.h"
  25. #include "gregset.h"

  26. #include <sgidefs.h>
  27. #include <sys/ptrace.h>
  28. #include <asm/ptrace.h>

  29. #include "nat/mips-linux-watch.h"

  30. #include "features/mips-linux.c"
  31. #include "features/mips-dsp-linux.c"
  32. #include "features/mips64-linux.c"
  33. #include "features/mips64-dsp-linux.c"

  34. #ifndef PTRACE_GET_THREAD_AREA
  35. #define PTRACE_GET_THREAD_AREA 25
  36. #endif

  37. /* Assume that we have PTRACE_GETREGS et al. support.  If we do not,
  38.    we'll clear this and use PTRACE_PEEKUSER instead.  */
  39. static int have_ptrace_regsets = 1;

  40. /* Saved function pointers to fetch and store a single register using
  41.    PTRACE_PEEKUSER and PTRACE_POKEUSER.  */

  42. static void (*super_fetch_registers) (struct target_ops *,
  43.                                       struct regcache *, int);
  44. static void (*super_store_registers) (struct target_ops *,
  45.                                       struct regcache *, int);

  46. static void (*super_close) (struct target_ops *);

  47. /* Map gdb internal register number to ptrace ``address''.
  48.    These ``addresses'' are normally defined in <asm/ptrace.h>.

  49.    ptrace does not provide a way to read (or set) MIPS_PS_REGNUM,
  50.    and there's no point in reading or setting MIPS_ZERO_REGNUM.
  51.    We also can not set BADVADDR, CAUSE, or FCRIR via ptrace().  */

  52. static CORE_ADDR
  53. mips_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
  54. {
  55.   CORE_ADDR regaddr;

  56.   if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
  57.     error (_("Bogon register number %d."), regno);

  58.   if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
  59.     regaddr = regno;
  60.   else if ((regno >= mips_regnum (gdbarch)->fp0)
  61.            && (regno < mips_regnum (gdbarch)->fp0 + 32))
  62.     regaddr = FPR_BASE + (regno - mips_regnum (gdbarch)->fp0);
  63.   else if (regno == mips_regnum (gdbarch)->pc)
  64.     regaddr = PC;
  65.   else if (regno == mips_regnum (gdbarch)->cause)
  66.     regaddr = store? (CORE_ADDR) -1 : CAUSE;
  67.   else if (regno == mips_regnum (gdbarch)->badvaddr)
  68.     regaddr = store? (CORE_ADDR) -1 : BADVADDR;
  69.   else if (regno == mips_regnum (gdbarch)->lo)
  70.     regaddr = MMLO;
  71.   else if (regno == mips_regnum (gdbarch)->hi)
  72.     regaddr = MMHI;
  73.   else if (regno == mips_regnum (gdbarch)->fp_control_status)
  74.     regaddr = FPC_CSR;
  75.   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
  76.     regaddr = store? (CORE_ADDR) -1 : FPC_EIR;
  77.   else if (mips_regnum (gdbarch)->dspacc != -1
  78.            && regno >= mips_regnum (gdbarch)->dspacc
  79.            && regno < mips_regnum (gdbarch)->dspacc + 6)
  80.     regaddr = DSP_BASE + (regno - mips_regnum (gdbarch)->dspacc);
  81.   else if (regno == mips_regnum (gdbarch)->dspctl)
  82.     regaddr = DSP_CONTROL;
  83.   else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
  84.     regaddr = 0;
  85.   else
  86.     regaddr = (CORE_ADDR) -1;

  87.   return regaddr;
  88. }

  89. static CORE_ADDR
  90. mips64_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
  91. {
  92.   CORE_ADDR regaddr;

  93.   if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
  94.     error (_("Bogon register number %d."), regno);

  95.   if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
  96.     regaddr = regno;
  97.   else if ((regno >= mips_regnum (gdbarch)->fp0)
  98.            && (regno < mips_regnum (gdbarch)->fp0 + 32))
  99.     regaddr = MIPS64_FPR_BASE + (regno - gdbarch_fp0_regnum (gdbarch));
  100.   else if (regno == mips_regnum (gdbarch)->pc)
  101.     regaddr = MIPS64_PC;
  102.   else if (regno == mips_regnum (gdbarch)->cause)
  103.     regaddr = store? (CORE_ADDR) -1 : MIPS64_CAUSE;
  104.   else if (regno == mips_regnum (gdbarch)->badvaddr)
  105.     regaddr = store? (CORE_ADDR) -1 : MIPS64_BADVADDR;
  106.   else if (regno == mips_regnum (gdbarch)->lo)
  107.     regaddr = MIPS64_MMLO;
  108.   else if (regno == mips_regnum (gdbarch)->hi)
  109.     regaddr = MIPS64_MMHI;
  110.   else if (regno == mips_regnum (gdbarch)->fp_control_status)
  111.     regaddr = MIPS64_FPC_CSR;
  112.   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
  113.     regaddr = store? (CORE_ADDR) -1 : MIPS64_FPC_EIR;
  114.   else if (mips_regnum (gdbarch)->dspacc != -1
  115.            && regno >= mips_regnum (gdbarch)->dspacc
  116.            && regno < mips_regnum (gdbarch)->dspacc + 6)
  117.     regaddr = DSP_BASE + (regno - mips_regnum (gdbarch)->dspacc);
  118.   else if (regno == mips_regnum (gdbarch)->dspctl)
  119.     regaddr = DSP_CONTROL;
  120.   else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
  121.     regaddr = 0;
  122.   else
  123.     regaddr = (CORE_ADDR) -1;

  124.   return regaddr;
  125. }

  126. /* Fetch the thread-local storage pointer for libthread_db.  */

  127. ps_err_e
  128. ps_get_thread_area (const struct ps_prochandle *ph,
  129.                     lwpid_t lwpid, int idx, void **base)
  130. {
  131.   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  132.     return PS_ERR;

  133.   /* IDX is the bias from the thread pointer to the beginning of the
  134.      thread descriptor.  It has to be subtracted due to implementation
  135.      quirks in libthread_db.  */
  136.   *base = (void *) ((char *)*base - idx);

  137.   return PS_OK;
  138. }

  139. /* Wrapper functions.  These are only used by libthread_db.  */

  140. void
  141. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  142. {
  143.   if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
  144.     mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
  145.   else
  146.     mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
  147. }

  148. void
  149. fill_gregset (const struct regcache *regcache,
  150.               gdb_gregset_t *gregsetp, int regno)
  151. {
  152.   if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
  153.     mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
  154.   else
  155.     mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
  156. }

  157. void
  158. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
  159. {
  160.   if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
  161.     mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *) fpregsetp);
  162.   else
  163.     mips64_supply_fpregset (regcache,
  164.                             (const mips64_elf_fpregset_t *) fpregsetp);
  165. }

  166. void
  167. fill_fpregset (const struct regcache *regcache,
  168.                gdb_fpregset_t *fpregsetp, int regno)
  169. {
  170.   if (mips_isa_regsize (get_regcache_arch (regcache)) == 4)
  171.     mips_fill_fpregset (regcache, (mips_elf_fpregset_t *) fpregsetp, regno);
  172.   else
  173.     mips64_fill_fpregset (regcache,
  174.                           (mips64_elf_fpregset_t *) fpregsetp, regno);
  175. }


  176. /* Fetch REGNO (or all registers if REGNO == -1) from the target
  177.    using PTRACE_GETREGS et al.  */

  178. static void
  179. mips64_linux_regsets_fetch_registers (struct target_ops *ops,
  180.                                       struct regcache *regcache, int regno)
  181. {
  182.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  183.   int is_fp, is_dsp;
  184.   int have_dsp;
  185.   int regi;
  186.   int tid;

  187.   if (regno >= mips_regnum (gdbarch)->fp0
  188.       && regno <= mips_regnum (gdbarch)->fp0 + 32)
  189.     is_fp = 1;
  190.   else if (regno == mips_regnum (gdbarch)->fp_control_status)
  191.     is_fp = 1;
  192.   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
  193.     is_fp = 1;
  194.   else
  195.     is_fp = 0;

  196.   /* DSP registers are optional and not a part of any set.  */
  197.   have_dsp = mips_regnum (gdbarch)->dspctl != -1;
  198.   if (!have_dsp)
  199.     is_dsp = 0;
  200.   else if (regno >= mips_regnum (gdbarch)->dspacc
  201.       && regno < mips_regnum (gdbarch)->dspacc + 6)
  202.     is_dsp = 1;
  203.   else if (regno == mips_regnum (gdbarch)->dspctl)
  204.     is_dsp = 1;
  205.   else
  206.     is_dsp = 0;

  207.   tid = ptid_get_lwp (inferior_ptid);
  208.   if (tid == 0)
  209.     tid = ptid_get_pid (inferior_ptid);

  210.   if (regno == -1 || (!is_fp && !is_dsp))
  211.     {
  212.       mips64_elf_gregset_t regs;

  213.       if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
  214.         {
  215.           if (errno == EIO)
  216.             {
  217.               have_ptrace_regsets = 0;
  218.               return;
  219.             }
  220.           perror_with_name (_("Couldn't get registers"));
  221.         }

  222.       mips64_supply_gregset (regcache,
  223.                              (const mips64_elf_gregset_t *) &regs);
  224.     }

  225.   if (regno == -1 || is_fp)
  226.     {
  227.       mips64_elf_fpregset_t fp_regs;

  228.       if (ptrace (PTRACE_GETFPREGS, tid, 0L,
  229.                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
  230.         {
  231.           if (errno == EIO)
  232.             {
  233.               have_ptrace_regsets = 0;
  234.               return;
  235.             }
  236.           perror_with_name (_("Couldn't get FP registers"));
  237.         }

  238.       mips64_supply_fpregset (regcache,
  239.                               (const mips64_elf_fpregset_t *) &fp_regs);
  240.     }

  241.   if (is_dsp)
  242.     super_fetch_registers (ops, regcache, regno);
  243.   else if (regno == -1 && have_dsp)
  244.     {
  245.       for (regi = mips_regnum (gdbarch)->dspacc;
  246.            regi < mips_regnum (gdbarch)->dspacc + 6;
  247.            regi++)
  248.         super_fetch_registers (ops, regcache, regi);
  249.       super_fetch_registers (ops, regcache, mips_regnum (gdbarch)->dspctl);
  250.     }
  251. }

  252. /* Store REGNO (or all registers if REGNO == -1) to the target
  253.    using PTRACE_SETREGS et al.  */

  254. static void
  255. mips64_linux_regsets_store_registers (struct target_ops *ops,
  256.                                       struct regcache *regcache, int regno)
  257. {
  258.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  259.   int is_fp, is_dsp;
  260.   int have_dsp;
  261.   int regi;
  262.   int tid;

  263.   if (regno >= mips_regnum (gdbarch)->fp0
  264.       && regno <= mips_regnum (gdbarch)->fp0 + 32)
  265.     is_fp = 1;
  266.   else if (regno == mips_regnum (gdbarch)->fp_control_status)
  267.     is_fp = 1;
  268.   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
  269.     is_fp = 1;
  270.   else
  271.     is_fp = 0;

  272.   /* DSP registers are optional and not a part of any set.  */
  273.   have_dsp = mips_regnum (gdbarch)->dspctl != -1;
  274.   if (!have_dsp)
  275.     is_dsp = 0;
  276.   else if (regno >= mips_regnum (gdbarch)->dspacc
  277.       && regno < mips_regnum (gdbarch)->dspacc + 6)
  278.     is_dsp = 1;
  279.   else if (regno == mips_regnum (gdbarch)->dspctl)
  280.     is_dsp = 1;
  281.   else
  282.     is_dsp = 0;

  283.   tid = ptid_get_lwp (inferior_ptid);
  284.   if (tid == 0)
  285.     tid = ptid_get_pid (inferior_ptid);

  286.   if (regno == -1 || (!is_fp && !is_dsp))
  287.     {
  288.       mips64_elf_gregset_t regs;

  289.       if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
  290.         perror_with_name (_("Couldn't get registers"));

  291.       mips64_fill_gregset (regcache, &regs, regno);

  292.       if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
  293.         perror_with_name (_("Couldn't set registers"));
  294.     }

  295.   if (regno == -1 || is_fp)
  296.     {
  297.       mips64_elf_fpregset_t fp_regs;

  298.       if (ptrace (PTRACE_GETFPREGS, tid, 0L,
  299.                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
  300.         perror_with_name (_("Couldn't get FP registers"));

  301.       mips64_fill_fpregset (regcache, &fp_regs, regno);

  302.       if (ptrace (PTRACE_SETFPREGS, tid, 0L,
  303.                   (PTRACE_TYPE_ARG3) &fp_regs) == -1)
  304.         perror_with_name (_("Couldn't set FP registers"));
  305.     }

  306.   if (is_dsp)
  307.     super_store_registers (ops, regcache, regno);
  308.   else if (regno == -1 && have_dsp)
  309.     {
  310.       for (regi = mips_regnum (gdbarch)->dspacc;
  311.            regi < mips_regnum (gdbarch)->dspacc + 6;
  312.            regi++)
  313.         super_store_registers (ops, regcache, regi);
  314.       super_store_registers (ops, regcache, mips_regnum (gdbarch)->dspctl);
  315.     }
  316. }

  317. /* Fetch REGNO (or all registers if REGNO == -1) from the target
  318.    using any working method.  */

  319. static void
  320. mips64_linux_fetch_registers (struct target_ops *ops,
  321.                               struct regcache *regcache, int regnum)
  322. {
  323.   /* Unless we already know that PTRACE_GETREGS does not work, try it.  */
  324.   if (have_ptrace_regsets)
  325.     mips64_linux_regsets_fetch_registers (ops, regcache, regnum);

  326.   /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
  327.      back to PTRACE_PEEKUSER.  */
  328.   if (!have_ptrace_regsets)
  329.     super_fetch_registers (ops, regcache, regnum);
  330. }

  331. /* Store REGNO (or all registers if REGNO == -1) to the target
  332.    using any working method.  */

  333. static void
  334. mips64_linux_store_registers (struct target_ops *ops,
  335.                               struct regcache *regcache, int regnum)
  336. {
  337.   /* Unless we already know that PTRACE_GETREGS does not work, try it.  */
  338.   if (have_ptrace_regsets)
  339.     mips64_linux_regsets_store_registers (ops, regcache, regnum);

  340.   /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
  341.      back to PTRACE_PEEKUSER.  */
  342.   if (!have_ptrace_regsets)
  343.     super_store_registers (ops, regcache, regnum);
  344. }

  345. /* Return the address in the core dump or inferior of register
  346.    REGNO.  */

  347. static CORE_ADDR
  348. mips_linux_register_u_offset (struct gdbarch *gdbarch, int regno, int store_p)
  349. {
  350.   if (mips_abi_regsize (gdbarch) == 8)
  351.     return mips64_linux_register_addr (gdbarch, regno, store_p);
  352.   else
  353.     return mips_linux_register_addr (gdbarch, regno, store_p);
  354. }

  355. static const struct target_desc *
  356. mips_linux_read_description (struct target_ops *ops)
  357. {
  358.   static int have_dsp = -1;

  359.   if (have_dsp < 0)
  360.     {
  361.       int tid;

  362.       tid = ptid_get_lwp (inferior_ptid);
  363.       if (tid == 0)
  364.         tid = ptid_get_pid (inferior_ptid);

  365.       errno = 0;
  366.       ptrace (PTRACE_PEEKUSER, tid, DSP_CONTROL, 0);
  367.       switch (errno)
  368.         {
  369.         case 0:
  370.           have_dsp = 1;
  371.           break;
  372.         case EIO:
  373.           have_dsp = 0;
  374.           break;
  375.         default:
  376.           perror_with_name (_("Couldn't check DSP support"));
  377.           break;
  378.         }
  379.     }

  380.   /* Report that target registers are a size we know for sure
  381.      that we can get from ptrace.  */
  382.   if (_MIPS_SIM == _ABIO32)
  383.     return have_dsp ? tdesc_mips_dsp_linux : tdesc_mips_linux;
  384.   else
  385.     return have_dsp ? tdesc_mips64_dsp_linux : tdesc_mips64_linux;
  386. }

  387. /* -1 if the kernel and/or CPU do not support watch registers.
  388.     1 if watch_readback is valid and we can read style, num_valid
  389.       and the masks.
  390.     0 if we need to read the watch_readback.  */

  391. static int watch_readback_valid;

  392. /* Cached watch register read values.  */

  393. static struct pt_watch_regs watch_readback;

  394. static struct mips_watchpoint *current_watches;

  395. /*  The current set of watch register values for writing the
  396.     registers.  */

  397. static struct pt_watch_regs watch_mirror;

  398. static void
  399. mips_show_dr (const char *func, CORE_ADDR addr,
  400.               int len, enum target_hw_bp_type type)
  401. {
  402.   int i;

  403.   puts_unfiltered (func);
  404.   if (addr || len)
  405.     printf_unfiltered (" (addr=%s, len=%d, type=%s)",
  406.                        paddress (target_gdbarch (), addr), len,
  407.                        type == hw_write ? "data-write"
  408.                        : (type == hw_read ? "data-read"
  409.                           : (type == hw_access ? "data-read/write"
  410.                              : (type == hw_execute ? "instruction-execute"
  411.                                 : "??unknown??"))));
  412.   puts_unfiltered (":\n");

  413.   for (i = 0; i < MAX_DEBUG_REGISTER; i++)
  414.     printf_unfiltered ("\tDR%d: lo=%s, hi=%s\n", i,
  415.                        paddress (target_gdbarch (),
  416.                                  mips_linux_watch_get_watchlo (&watch_mirror,
  417.                                                                i)),
  418.                        paddress (target_gdbarch (),
  419.                                  mips_linux_watch_get_watchhi (&watch_mirror,
  420.                                                                i)));
  421. }

  422. /* Target to_can_use_hw_breakpoint implementation.  Return 1 if we can
  423.    handle the specified watch type.  */

  424. static int
  425. mips_linux_can_use_hw_breakpoint (struct target_ops *self,
  426.                                   int type, int cnt, int ot)
  427. {
  428.   int i;
  429.   uint32_t wanted_mask, irw_mask;

  430.   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
  431.                                         &watch_readback,
  432.                                         &watch_readback_valid, 0))
  433.     return 0;

  434.    switch (type)
  435.     {
  436.     case bp_hardware_watchpoint:
  437.       wanted_mask = W_MASK;
  438.       break;
  439.     case bp_read_watchpoint:
  440.       wanted_mask = R_MASK;
  441.       break;
  442.     case bp_access_watchpoint:
  443.       wanted_mask = R_MASK | W_MASK;
  444.       break;
  445.     default:
  446.       return 0;
  447.     }

  448.   for (i = 0;
  449.        i < mips_linux_watch_get_num_valid (&watch_readback) && cnt;
  450.        i++)
  451.     {
  452.       irw_mask = mips_linux_watch_get_irw_mask (&watch_readback, i);
  453.       if ((irw_mask & wanted_mask) == wanted_mask)
  454.         cnt--;
  455.     }
  456.   return (cnt == 0) ? 1 : 0;
  457. }

  458. /* Target to_stopped_by_watchpoint implementation.  Return 1 if
  459.    stopped by watchpoint.  The watchhi R and W bits indicate the watch
  460.    register triggered.  */

  461. static int
  462. mips_linux_stopped_by_watchpoint (struct target_ops *ops)
  463. {
  464.   int n;
  465.   int num_valid;

  466.   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
  467.                                         &watch_readback,
  468.                                         &watch_readback_valid, 1))
  469.     return 0;

  470.   num_valid = mips_linux_watch_get_num_valid (&watch_readback);

  471.   for (n = 0; n < MAX_DEBUG_REGISTER && n < num_valid; n++)
  472.     if (mips_linux_watch_get_watchhi (&watch_readback, n) & (R_MASK | W_MASK))
  473.       return 1;

  474.   return 0;
  475. }

  476. /* Target to_stopped_data_address implementation.  Set the address
  477.    where the watch triggered (if known).  Return 1 if the address was
  478.    known.  */

  479. static int
  480. mips_linux_stopped_data_address (struct target_ops *t, CORE_ADDR *paddr)
  481. {
  482.   /* On mips we don't know the low order 3 bits of the data address,
  483.      so we must return false.  */
  484.   return 0;
  485. }

  486. /* Target to_region_ok_for_hw_watchpoint implementation.  Return 1 if
  487.    the specified region can be covered by the watch registers.  */

  488. static int
  489. mips_linux_region_ok_for_hw_watchpoint (struct target_ops *self,
  490.                                         CORE_ADDR addr, int len)
  491. {
  492.   struct pt_watch_regs dummy_regs;
  493.   int i;

  494.   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
  495.                                         &watch_readback,
  496.                                         &watch_readback_valid, 0))
  497.     return 0;

  498.   dummy_regs = watch_readback;
  499.   /* Clear them out.  */
  500.   for (i = 0; i < mips_linux_watch_get_num_valid (&dummy_regs); i++)
  501.     mips_linux_watch_set_watchlo (&dummy_regs, i, 0);
  502.   return mips_linux_watch_try_one_watch (&dummy_regs, addr, len, 0);
  503. }

  504. /* Write the mirrored watch register values for each thread.  */

  505. static int
  506. write_watchpoint_regs (void)
  507. {
  508.   struct lwp_info *lp;
  509.   int tid;

  510.   ALL_LWPS (lp)
  511.     {
  512.       tid = ptid_get_lwp (lp->ptid);
  513.       if (ptrace (PTRACE_SET_WATCH_REGS, tid, &watch_mirror) == -1)
  514.         perror_with_name (_("Couldn't write debug register"));
  515.     }
  516.   return 0;
  517. }

  518. /* linux_nat new_thread implementation.  Write the mirrored watch
  519. register values for the new thread.  */

  520. static void
  521. mips_linux_new_thread (struct lwp_info *lp)
  522. {
  523.   int tid;

  524.   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
  525.                                         &watch_readback,
  526.                                         &watch_readback_valid, 0))
  527.     return;

  528.   tid = ptid_get_lwp (lp->ptid);
  529.   if (ptrace (PTRACE_SET_WATCH_REGS, tid, &watch_mirror) == -1)
  530.     perror_with_name (_("Couldn't write debug register"));
  531. }

  532. /* Target to_insert_watchpoint implementation.  Try to insert a new
  533.    watch.  Return zero on success.  */

  534. static int
  535. mips_linux_insert_watchpoint (struct target_ops *self,
  536.                               CORE_ADDR addr, int len, int type,
  537.                               struct expression *cond)
  538. {
  539.   struct pt_watch_regs regs;
  540.   struct mips_watchpoint *new_watch;
  541.   struct mips_watchpoint **pw;

  542.   int i;
  543.   int retval;

  544.   if (!mips_linux_read_watch_registers (ptid_get_lwp (inferior_ptid),
  545.                                         &watch_readback,
  546.                                         &watch_readback_valid, 0))
  547.     return -1;

  548.   if (len <= 0)
  549.     return -1;

  550.   regs = watch_readback;
  551.   /* Add the current watches.  */
  552.   mips_linux_watch_populate_regs (current_watches, &regs);

  553.   /* Now try to add the new watch.  */
  554.   if (!mips_linux_watch_try_one_watch (&regs, addr, len,
  555.                                        mips_linux_watch_type_to_irw (type)))
  556.     return -1;

  557.   /* It fit.  Stick it on the end of the list.  */
  558.   new_watch = (struct mips_watchpoint *)
  559.     xmalloc (sizeof (struct mips_watchpoint));
  560.   new_watch->addr = addr;
  561.   new_watch->len = len;
  562.   new_watch->type = type;
  563.   new_watch->next = NULL;

  564.   pw = &current_watches;
  565.   while (*pw != NULL)
  566.     pw = &(*pw)->next;
  567.   *pw = new_watch;

  568.   watch_mirror = regs;
  569.   retval = write_watchpoint_regs ();

  570.   if (show_debug_regs)
  571.     mips_show_dr ("insert_watchpoint", addr, len, type);

  572.   return retval;
  573. }

  574. /* Target to_remove_watchpoint implementation.  Try to remove a watch.
  575.    Return zero on success.  */

  576. static int
  577. mips_linux_remove_watchpoint (struct target_ops *self,
  578.                               CORE_ADDR addr, int len, int type,
  579.                               struct expression *cond)
  580. {
  581.   int retval;
  582.   int deleted_one;

  583.   struct mips_watchpoint **pw;
  584.   struct mips_watchpoint *w;

  585.   /* Search for a known watch that matches.  Then unlink and free
  586.      it.  */
  587.   deleted_one = 0;
  588.   pw = &current_watches;
  589.   while ((w = *pw))
  590.     {
  591.       if (w->addr == addr && w->len == len && w->type == type)
  592.         {
  593.           *pw = w->next;
  594.           xfree (w);
  595.           deleted_one = 1;
  596.           break;
  597.         }
  598.       pw = &(w->next);
  599.     }

  600.   if (!deleted_one)
  601.     return -1/* We don't know about it, fail doing nothing.  */

  602.   /* At this point watch_readback is known to be valid because we
  603.      could not have added the watch without reading it.  */
  604.   gdb_assert (watch_readback_valid == 1);

  605.   watch_mirror = watch_readback;
  606.   mips_linux_watch_populate_regs (current_watches, &watch_mirror);

  607.   retval = write_watchpoint_regs ();

  608.   if (show_debug_regs)
  609.     mips_show_dr ("remove_watchpoint", addr, len, type);

  610.   return retval;
  611. }

  612. /* Target to_close implementation.  Free any watches and call the
  613.    super implementation.  */

  614. static void
  615. mips_linux_close (struct target_ops *self)
  616. {
  617.   struct mips_watchpoint *w;
  618.   struct mips_watchpoint *nw;

  619.   /* Clean out the current_watches list.  */
  620.   w = current_watches;
  621.   while (w)
  622.     {
  623.       nw = w->next;
  624.       xfree (w);
  625.       w = nw;
  626.     }
  627.   current_watches = NULL;

  628.   if (super_close)
  629.     super_close (self);
  630. }

  631. void _initialize_mips_linux_nat (void);

  632. void
  633. _initialize_mips_linux_nat (void)
  634. {
  635.   struct target_ops *t;

  636.   add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
  637.                            &show_debug_regs, _("\
  638. Set whether to show variables that mirror the mips debug registers."), _("\
  639. Show whether to show variables that mirror the mips debug registers."), _("\
  640. Use \"on\" to enable, \"off\" to disable.\n\
  641. If enabled, the debug registers values are shown when GDB inserts\n\
  642. or removes a hardware breakpoint or watchpoint, and when the inferior\n\
  643. triggers a breakpoint or watchpoint."),
  644.                            NULL,
  645.                            NULL,
  646.                            &maintenance_set_cmdlist,
  647.                            &maintenance_show_cmdlist);

  648.   t = linux_trad_target (mips_linux_register_u_offset);

  649.   super_close = t->to_close;
  650.   t->to_close = mips_linux_close;

  651.   super_fetch_registers = t->to_fetch_registers;
  652.   super_store_registers = t->to_store_registers;

  653.   t->to_fetch_registers = mips64_linux_fetch_registers;
  654.   t->to_store_registers = mips64_linux_store_registers;

  655.   t->to_can_use_hw_breakpoint = mips_linux_can_use_hw_breakpoint;
  656.   t->to_remove_watchpoint = mips_linux_remove_watchpoint;
  657.   t->to_insert_watchpoint = mips_linux_insert_watchpoint;
  658.   t->to_stopped_by_watchpoint = mips_linux_stopped_by_watchpoint;
  659.   t->to_stopped_data_address = mips_linux_stopped_data_address;
  660.   t->to_region_ok_for_hw_watchpoint = mips_linux_region_ok_for_hw_watchpoint;

  661.   t->to_read_description = mips_linux_read_description;

  662.   linux_nat_add_target (t);
  663.   linux_nat_set_new_thread (t, mips_linux_new_thread);

  664.   /* Initialize the standard target descriptions.  */
  665.   initialize_tdesc_mips_linux ();
  666.   initialize_tdesc_mips_dsp_linux ();
  667.   initialize_tdesc_mips64_linux ();
  668.   initialize_tdesc_mips64_dsp_linux ();
  669. }