gdb/hppa-linux-nat.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Functions specific to running GDB native on HPPA running GNU/Linux.

  2.    Copyright (C) 2004-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 "gdbcore.h"
  16. #include "regcache.h"
  17. #include "inferior.h"
  18. #include "target.h"
  19. #include "linux-nat.h"

  20. #include <sys/procfs.h>
  21. #include <sys/ptrace.h>
  22. #include <linux/version.h>

  23. #include <asm/ptrace.h>
  24. #include "hppa-linux-offsets.h"

  25. #include "hppa-tdep.h"

  26. /* Prototypes for supply_gregset etc.  */
  27. #include "gregset.h"

  28. /* These must match the order of the register names.

  29.    Some sort of lookup table is needed because the offsets associated
  30.    with the registers are all over the board.  */

  31. static const int u_offsets[] =
  32.   {
  33.     /* general registers */
  34.     -1,
  35.     PT_GR1,
  36.     PT_GR2,
  37.     PT_GR3,
  38.     PT_GR4,
  39.     PT_GR5,
  40.     PT_GR6,
  41.     PT_GR7,
  42.     PT_GR8,
  43.     PT_GR9,
  44.     PT_GR10,
  45.     PT_GR11,
  46.     PT_GR12,
  47.     PT_GR13,
  48.     PT_GR14,
  49.     PT_GR15,
  50.     PT_GR16,
  51.     PT_GR17,
  52.     PT_GR18,
  53.     PT_GR19,
  54.     PT_GR20,
  55.     PT_GR21,
  56.     PT_GR22,
  57.     PT_GR23,
  58.     PT_GR24,
  59.     PT_GR25,
  60.     PT_GR26,
  61.     PT_GR27,
  62.     PT_GR28,
  63.     PT_GR29,
  64.     PT_GR30,
  65.     PT_GR31,

  66.     PT_SAR,
  67.     PT_IAOQ0,
  68.     PT_IASQ0,
  69.     PT_IAOQ1,
  70.     PT_IASQ1,
  71.     -1, /* eiem */
  72.     PT_IIR,
  73.     PT_ISR,
  74.     PT_IOR,
  75.     PT_PSW,
  76.     -1, /* goto */

  77.     PT_SR4,
  78.     PT_SR0,
  79.     PT_SR1,
  80.     PT_SR2,
  81.     PT_SR3,
  82.     PT_SR5,
  83.     PT_SR6,
  84.     PT_SR7,

  85.     -1, /* cr0 */
  86.     -1, /* pid0 */
  87.     -1, /* pid1 */
  88.     -1, /* ccr */
  89.     -1, /* pid2 */
  90.     -1, /* pid3 */
  91.     -1, /* cr24 */
  92.     -1, /* cr25 */
  93.     -1, /* cr26 */
  94.     PT_CR27,
  95.     -1, /* cr28 */
  96.     -1, /* cr29 */
  97.     -1, /* cr30 */

  98.     /* Floating point regs.  */
  99.     PT_FR0PT_FR0 + 4,
  100.     PT_FR1PT_FR1 + 4,
  101.     PT_FR2PT_FR2 + 4,
  102.     PT_FR3PT_FR3 + 4,
  103.     PT_FR4PT_FR4 + 4,
  104.     PT_FR5PT_FR5 + 4,
  105.     PT_FR6PT_FR6 + 4,
  106.     PT_FR7PT_FR7 + 4,
  107.     PT_FR8PT_FR8 + 4,
  108.     PT_FR9PT_FR9 + 4,
  109.     PT_FR10, PT_FR10 + 4,
  110.     PT_FR11, PT_FR11 + 4,
  111.     PT_FR12, PT_FR12 + 4,
  112.     PT_FR13, PT_FR13 + 4,
  113.     PT_FR14, PT_FR14 + 4,
  114.     PT_FR15, PT_FR15 + 4,
  115.     PT_FR16, PT_FR16 + 4,
  116.     PT_FR17, PT_FR17 + 4,
  117.     PT_FR18, PT_FR18 + 4,
  118.     PT_FR19, PT_FR19 + 4,
  119.     PT_FR20, PT_FR20 + 4,
  120.     PT_FR21, PT_FR21 + 4,
  121.     PT_FR22, PT_FR22 + 4,
  122.     PT_FR23, PT_FR23 + 4,
  123.     PT_FR24, PT_FR24 + 4,
  124.     PT_FR25, PT_FR25 + 4,
  125.     PT_FR26, PT_FR26 + 4,
  126.     PT_FR27, PT_FR27 + 4,
  127.     PT_FR28, PT_FR28 + 4,
  128.     PT_FR29, PT_FR29 + 4,
  129.     PT_FR30, PT_FR30 + 4,
  130.     PT_FR31, PT_FR31 + 4,
  131.   };

  132. static CORE_ADDR
  133. hppa_linux_register_addr (int regno, CORE_ADDR blockend)
  134. {
  135.   CORE_ADDR addr;

  136.   if ((unsigned) regno >= ARRAY_SIZE (u_offsets))
  137.     error (_("Invalid register number %d."), regno);

  138.   if (u_offsets[regno] == -1)
  139.     addr = 0;
  140.   else
  141.     {
  142.       addr = (CORE_ADDR) u_offsets[regno];
  143.     }

  144.   return addr;
  145. }

  146. /*
  147. * Registers saved in a coredump:
  148. * gr0..gr31
  149. * sr0..sr7
  150. * iaoq0..iaoq1
  151. * iasq0..iasq1
  152. * sar, iir, isr, ior, ipsw
  153. * cr0, cr24..cr31
  154. * cr8,9,12,13
  155. * cr10, cr15
  156. */
  157. #define GR_REGNUM(_n)        (HPPA_R0_REGNUM+_n)
  158. #define TR_REGNUM(_n)        (HPPA_TR0_REGNUM+_n)
  159. static const int greg_map[] =
  160.   {
  161.     GR_REGNUM(0), GR_REGNUM(1), GR_REGNUM(2), GR_REGNUM(3),
  162.     GR_REGNUM(4), GR_REGNUM(5), GR_REGNUM(6), GR_REGNUM(7),
  163.     GR_REGNUM(8), GR_REGNUM(9), GR_REGNUM(10), GR_REGNUM(11),
  164.     GR_REGNUM(12), GR_REGNUM(13), GR_REGNUM(14), GR_REGNUM(15),
  165.     GR_REGNUM(16), GR_REGNUM(17), GR_REGNUM(18), GR_REGNUM(19),
  166.     GR_REGNUM(20), GR_REGNUM(21), GR_REGNUM(22), GR_REGNUM(23),
  167.     GR_REGNUM(24), GR_REGNUM(25), GR_REGNUM(26), GR_REGNUM(27),
  168.     GR_REGNUM(28), GR_REGNUM(29), GR_REGNUM(30), GR_REGNUM(31),

  169.     HPPA_SR4_REGNUM+1, HPPA_SR4_REGNUM+2, HPPA_SR4_REGNUM+3, HPPA_SR4_REGNUM+4,
  170.     HPPA_SR4_REGNUM, HPPA_SR4_REGNUM+5, HPPA_SR4_REGNUM+6, HPPA_SR4_REGNUM+7,

  171.     HPPA_PCOQ_HEAD_REGNUM, HPPA_PCOQ_TAIL_REGNUM,
  172.     HPPA_PCSQ_HEAD_REGNUM, HPPA_PCSQ_TAIL_REGNUM,

  173.     HPPA_SAR_REGNUM, HPPA_IIR_REGNUM, HPPA_ISR_REGNUM, HPPA_IOR_REGNUM,
  174.     HPPA_IPSW_REGNUM, HPPA_RCR_REGNUM,

  175.     TR_REGNUM(0), TR_REGNUM(1), TR_REGNUM(2), TR_REGNUM(3),
  176.     TR_REGNUM(4), TR_REGNUM(5), TR_REGNUM(6), TR_REGNUM(7),

  177.     HPPA_PID0_REGNUM, HPPA_PID1_REGNUM, HPPA_PID2_REGNUM, HPPA_PID3_REGNUM,
  178.     HPPA_CCR_REGNUM, HPPA_EIEM_REGNUM,
  179.   };



  180. /* Fetch one register.  */

  181. static void
  182. fetch_register (struct regcache *regcache, int regno)
  183. {
  184.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  185.   int tid;
  186.   int val;

  187.   if (gdbarch_cannot_fetch_register (gdbarch, regno))
  188.     {
  189.       regcache_raw_supply (regcache, regno, NULL);
  190.       return;
  191.     }

  192.   /* GNU/Linux LWP ID's are process ID's.  */
  193.   tid = ptid_get_lwp (inferior_ptid);
  194.   if (tid == 0)
  195.     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */

  196.   errno = 0;
  197.   val = ptrace (PTRACE_PEEKUSER, tid, hppa_linux_register_addr (regno, 0), 0);
  198.   if (errno != 0)
  199.     error (_("Couldn't read register %s (#%d): %s."),
  200.            gdbarch_register_name (gdbarch, regno),
  201.            regno, safe_strerror (errno));

  202.   regcache_raw_supply (regcache, regno, &val);
  203. }

  204. /* Store one register.  */

  205. static void
  206. store_register (const struct regcache *regcache, int regno)
  207. {
  208.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  209.   int tid;
  210.   int val;

  211.   if (gdbarch_cannot_store_register (gdbarch, regno))
  212.     return;

  213.   /* GNU/Linux LWP ID's are process ID's.  */
  214.   tid = ptid_get_lwp (inferior_ptid);
  215.   if (tid == 0)
  216.     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */

  217.   errno = 0;
  218.   regcache_raw_collect (regcache, regno, &val);
  219.   ptrace (PTRACE_POKEUSER, tid, hppa_linux_register_addr (regno, 0), val);
  220.   if (errno != 0)
  221.     error (_("Couldn't write register %s (#%d): %s."),
  222.            gdbarch_register_name (gdbarch, regno),
  223.            regno, safe_strerror (errno));
  224. }

  225. /* Fetch registers from the child process.  Fetch all registers if
  226.    regno == -1, otherwise fetch all general registers or all floating
  227.    point registers depending upon the value of regno.  */

  228. static void
  229. hppa_linux_fetch_inferior_registers (struct target_ops *ops,
  230.                                      struct regcache *regcache, int regno)
  231. {
  232.   if (-1 == regno)
  233.     {
  234.       for (regno = 0;
  235.            regno < gdbarch_num_regs (get_regcache_arch (regcache));
  236.            regno++)
  237.         fetch_register (regcache, regno);
  238.     }
  239.   else
  240.     {
  241.       fetch_register (regcache, regno);
  242.     }
  243. }

  244. /* Store registers back into the inferior.  Store all registers if
  245.    regno == -1, otherwise store all general registers or all floating
  246.    point registers depending upon the value of regno.  */

  247. static void
  248. hppa_linux_store_inferior_registers (struct target_ops *ops,
  249.                                      struct regcache *regcache, int regno)
  250. {
  251.   if (-1 == regno)
  252.     {
  253.       for (regno = 0;
  254.            regno < gdbarch_num_regs (get_regcache_arch (regcache));
  255.            regno++)
  256.         store_register (regcache, regno);
  257.     }
  258.   else
  259.     {
  260.       store_register (regcache, regno);
  261.     }
  262. }

  263. /* Fill GDB's register array with the general-purpose register values
  264.    in *gregsetp.  */

  265. void
  266. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  267. {
  268.   int i;
  269.   const greg_t *regp = (const elf_greg_t *) gregsetp;

  270.   for (i = 0; i < sizeof (greg_map) / sizeof (greg_map[0]); i++, regp++)
  271.     {
  272.       int regno = greg_map[i];
  273.       regcache_raw_supply (regcache, regno, regp);
  274.     }
  275. }

  276. /* Fill register regno (if it is a general-purpose register) in
  277.    *gregsetp with the appropriate value from GDB's register array.
  278.    If regno is -1, do this for all registers.  */

  279. void
  280. fill_gregset (const struct regcache *regcache,
  281.               gdb_gregset_t *gregsetp, int regno)
  282. {
  283.   int i;

  284.   for (i = 0; i < sizeof (greg_map) / sizeof (greg_map[0]); i++)
  285.     {
  286.       int mregno = greg_map[i];

  287.       if (regno == -1 || regno == mregno)
  288.         {
  289.           regcache_raw_collect(regcache, mregno, &(*gregsetp)[i]);
  290.         }
  291.     }
  292. }

  293. /*  Given a pointer to a floating point register set in /proc format
  294.    (fpregset_t *), unpack the register contents and supply them as gdb's
  295.    idea of the current floating point register values.  */

  296. void
  297. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
  298. {
  299.   int regi;
  300.   const char *from;

  301.   for (regi = 0; regi <= 31; regi++)
  302.     {
  303.       from = (const char *) &((*fpregsetp)[regi]);
  304.       regcache_raw_supply (regcache, 2*regi + HPPA_FP0_REGNUM, from);
  305.       regcache_raw_supply (regcache, 2*regi + HPPA_FP0_REGNUM + 1, from + 4);
  306.     }
  307. }

  308. /*  Given a pointer to a floating point register set in /proc format
  309.    (fpregset_t *), update the register specified by REGNO from gdb's idea
  310.    of the current floating point register set.  If REGNO is -1, update
  311.    them all.  */

  312. void
  313. fill_fpregset (const struct regcache *regcache,
  314.                gdb_fpregset_t *fpregsetp, int regno)
  315. {
  316.   int i;

  317.   for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32 * 2; i++)
  318.    {
  319.       /* Gross.  fpregset_t is double, registers[x] has single
  320.          precision reg.  */
  321.       char *to = (char *) &((*fpregsetp)[(i - HPPA_FP0_REGNUM) / 2]);
  322.       if ((i - HPPA_FP0_REGNUM) & 1)
  323.         to += 4;
  324.       regcache_raw_collect (regcache, i, to);
  325.    }
  326. }

  327. void _initialize_hppa_linux_nat (void);

  328. void
  329. _initialize_hppa_linux_nat (void)
  330. {
  331.   struct target_ops *t;

  332.   /* Fill in the generic GNU/Linux methods.  */
  333.   t = linux_target ();

  334.   /* Add our register access methods.  */
  335.   t->to_fetch_registers = hppa_linux_fetch_inferior_registers;
  336.   t->to_store_registers = hppa_linux_store_inferior_registers;

  337.   /* Register the target.  */
  338.   linux_nat_add_target (t);
  339. }