gdb/proc-service.c - gdb

Data types defined

Functions defined

Source code

  1. /* <proc_service.h> implementation.

  2.    Copyright (C) 1999-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 "inferior.h"
  17. #include "symtab.h"
  18. #include "target.h"
  19. #include "regcache.h"
  20. #include "objfiles.h"

  21. #include "gdb_proc_service.h"

  22. #include <sys/procfs.h>

  23. /* Prototypes for supply_gregset etc.  */
  24. #include "gregset.h"


  25. /* Fix-up some broken systems.  */

  26. /* The prototypes in <proc_service.h> are slightly different on older
  27.    systems.  Compensate for the discrepancies.  */

  28. #ifdef PROC_SERVICE_IS_OLD
  29. typedef const struct ps_prochandle *gdb_ps_prochandle_t;
  30. typedef char *gdb_ps_read_buf_t;
  31. typedef char *gdb_ps_write_buf_t;
  32. typedef int gdb_ps_size_t;
  33. #else
  34. typedef struct ps_prochandle *gdb_ps_prochandle_t;
  35. typedef void *gdb_ps_read_buf_t;
  36. typedef const void *gdb_ps_write_buf_t;
  37. typedef size_t gdb_ps_size_t;
  38. #endif


  39. /* Helper functions.  */

  40. /* Convert a psaddr_t to a CORE_ADDR.  */

  41. static CORE_ADDR
  42. ps_addr_to_core_addr (psaddr_t addr)
  43. {
  44.   if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
  45.     return (intptr_t) addr;
  46.   else
  47.     return (uintptr_t) addr;
  48. }

  49. /* Convert a CORE_ADDR to a psaddr_t.  */

  50. static psaddr_t
  51. core_addr_to_ps_addr (CORE_ADDR addr)
  52. {
  53.   if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
  54.     return (psaddr_t) (intptr_t) addr;
  55.   else
  56.     return (psaddr_t) (uintptr_t) addr;
  57. }

  58. /* Transfer LEN bytes of memory between BUF and address ADDR in the
  59.    process specified by PH.  If WRITE, transfer them to the process,
  60.    else transfer them from the process.  Returns PS_OK for success,
  61.    PS_ERR on failure.

  62.    This is a helper function for ps_pdread and ps_pdwrite.  */

  63. static ps_err_e
  64. ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr,
  65.                 gdb_byte *buf, size_t len, int write)
  66. {
  67.   struct cleanup *old_chain = save_inferior_ptid ();
  68.   int ret;
  69.   CORE_ADDR core_addr = ps_addr_to_core_addr (addr);

  70.   inferior_ptid = ph->ptid;

  71.   if (write)
  72.     ret = target_write_memory (core_addr, buf, len);
  73.   else
  74.     ret = target_read_memory (core_addr, buf, len);

  75.   do_cleanups (old_chain);

  76.   return (ret == 0 ? PS_OK : PS_ERR);
  77. }


  78. /* Search for the symbol named NAME within the object named OBJ within
  79.    the target process PH.  If the symbol is found the address of the
  80.    symbol is stored in SYM_ADDR.  */

  81. ps_err_e
  82. ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *obj,
  83.                    const char *name, psaddr_t *sym_addr)
  84. {
  85.   struct bound_minimal_symbol ms;
  86.   struct cleanup *old_chain = save_current_program_space ();
  87.   struct inferior *inf = find_inferior_ptid (ph->ptid);
  88.   ps_err_e result;

  89.   set_current_program_space (inf->pspace);

  90.   /* FIXME: kettenis/2000-09-03: What should we do with OBJ?  */
  91.   ms = lookup_minimal_symbol (name, NULL, NULL);
  92.   if (ms.minsym == NULL)
  93.     result = PS_NOSYM;
  94.   else
  95.     {
  96.       *sym_addr = core_addr_to_ps_addr (BMSYMBOL_VALUE_ADDRESS (ms));
  97.       result = PS_OK;
  98.     }

  99.   do_cleanups (old_chain);
  100.   return result;
  101. }

  102. /* Read SIZE bytes from the target process PH at address ADDR and copy
  103.    them into BUF.  */

  104. ps_err_e
  105. ps_pdread (gdb_ps_prochandle_t ph, psaddr_t addr,
  106.            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
  107. {
  108.   return ps_xfer_memory (ph, addr, buf, size, 0);
  109. }

  110. /* Write SIZE bytes from BUF into the target process PH at address ADDR.  */

  111. ps_err_e
  112. ps_pdwrite (gdb_ps_prochandle_t ph, psaddr_t addr,
  113.             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
  114. {
  115.   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
  116. }

  117. /* Get the general registers of LWP LWPID within the target process PH
  118.    and store them in GREGSET.  */

  119. ps_err_e
  120. ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
  121. {
  122.   struct cleanup *old_chain = save_inferior_ptid ();
  123.   struct regcache *regcache;

  124.   inferior_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
  125.   regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());

  126.   target_fetch_registers (regcache, -1);
  127.   fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);

  128.   do_cleanups (old_chain);
  129.   return PS_OK;
  130. }

  131. /* Set the general registers of LWP LWPID within the target process PH
  132.    from GREGSET.  */

  133. ps_err_e
  134. ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, const prgregset_t gregset)
  135. {
  136.   struct cleanup *old_chain = save_inferior_ptid ();
  137.   struct regcache *regcache;

  138.   inferior_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
  139.   regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());

  140.   supply_gregset (regcache, (const gdb_gregset_t *) gregset);
  141.   target_store_registers (regcache, -1);

  142.   do_cleanups (old_chain);
  143.   return PS_OK;
  144. }

  145. /* Get the floating-point registers of LWP LWPID within the target
  146.    process PH and store them in FPREGSET.  */

  147. ps_err_e
  148. ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
  149.                gdb_prfpregset_t *fpregset)
  150. {
  151.   struct cleanup *old_chain = save_inferior_ptid ();
  152.   struct regcache *regcache;

  153.   inferior_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
  154.   regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());

  155.   target_fetch_registers (regcache, -1);
  156.   fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);

  157.   do_cleanups (old_chain);
  158.   return PS_OK;
  159. }

  160. /* Set the floating-point registers of LWP LWPID within the target
  161.    process PH from FPREGSET.  */

  162. ps_err_e
  163. ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
  164.                const gdb_prfpregset_t *fpregset)
  165. {
  166.   struct cleanup *old_chain = save_inferior_ptid ();
  167.   struct regcache *regcache;

  168.   inferior_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
  169.   regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());

  170.   supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
  171.   target_store_registers (regcache, -1);

  172.   do_cleanups (old_chain);
  173.   return PS_OK;
  174. }

  175. /* Return overall process id of the target PH.  Special for GNU/Linux
  176.    -- not used on Solaris.  */

  177. pid_t
  178. ps_getpid (gdb_ps_prochandle_t ph)
  179. {
  180.   return ptid_get_pid (ph->ptid);
  181. }

  182. /* Provide a prototype to silence -Wmissing-prototypes.  */
  183. extern initialize_file_ftype _initialize_proc_service;

  184. void
  185. _initialize_proc_service (void)
  186. {
  187.   /* This function solely exists to make sure this module is linked
  188.      into the final binary.  */
  189. }