proc-service.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* <proc_service.h> implementation.
  2. Copyright (C) 1999-2022 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 "gdbthread.h"
  18. #include "symtab.h"
  19. #include "target.h"
  20. #include "regcache.h"
  21. #include "objfiles.h"
  22. #include "gdb_proc_service.h"
  23. #include <sys/procfs.h>
  24. /* Prototypes for supply_gregset etc. */
  25. #include "gregset.h"
  26. /* Helper functions. */
  27. /* Convert a psaddr_t to a CORE_ADDR. */
  28. static CORE_ADDR
  29. ps_addr_to_core_addr (psaddr_t addr)
  30. {
  31. if (current_program_space->exec_bfd ()
  32. && bfd_get_sign_extend_vma (current_program_space->exec_bfd ()))
  33. return (intptr_t) addr;
  34. else
  35. return (uintptr_t) addr;
  36. }
  37. /* Convert a CORE_ADDR to a psaddr_t. */
  38. static psaddr_t
  39. core_addr_to_ps_addr (CORE_ADDR addr)
  40. {
  41. if (current_program_space->exec_bfd ()
  42. && bfd_get_sign_extend_vma (current_program_space->exec_bfd ()))
  43. return (psaddr_t) (intptr_t) addr;
  44. else
  45. return (psaddr_t) (uintptr_t) addr;
  46. }
  47. /* Transfer LEN bytes of memory between BUF and address ADDR in the
  48. process specified by PH. If WRITE, transfer them to the process,
  49. else transfer them from the process. Returns PS_OK for success,
  50. PS_ERR on failure.
  51. This is a helper function for ps_pdread and ps_pdwrite. */
  52. static ps_err_e
  53. ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr,
  54. gdb_byte *buf, size_t len, int write)
  55. {
  56. scoped_restore_current_inferior restore_inferior;
  57. set_current_inferior (ph->thread->inf);
  58. scoped_restore_current_program_space restore_current_progspace;
  59. set_current_program_space (ph->thread->inf->pspace);
  60. scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
  61. inferior_ptid = ph->thread->ptid;
  62. CORE_ADDR core_addr = ps_addr_to_core_addr (addr);
  63. int ret;
  64. if (write)
  65. ret = target_write_memory (core_addr, buf, len);
  66. else
  67. ret = target_read_memory (core_addr, buf, len);
  68. return (ret == 0 ? PS_OK : PS_ERR);
  69. }
  70. /* Search for the symbol named NAME within the object named OBJ within
  71. the target process PH. If the symbol is found the address of the
  72. symbol is stored in SYM_ADDR. */
  73. ps_err_e
  74. ps_pglobal_lookup (struct ps_prochandle *ph, const char *obj,
  75. const char *name, psaddr_t *sym_addr)
  76. {
  77. inferior *inf = ph->thread->inf;
  78. scoped_restore_current_program_space restore_pspace;
  79. set_current_program_space (inf->pspace);
  80. /* FIXME: kettenis/2000-09-03: What should we do with OBJ? */
  81. bound_minimal_symbol ms = lookup_minimal_symbol (name, NULL, NULL);
  82. if (ms.minsym == NULL)
  83. return PS_NOSYM;
  84. *sym_addr = core_addr_to_ps_addr (BMSYMBOL_VALUE_ADDRESS (ms));
  85. return PS_OK;
  86. }
  87. /* Read SIZE bytes from the target process PH at address ADDR and copy
  88. them into BUF. */
  89. ps_err_e
  90. ps_pdread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t size)
  91. {
  92. return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 0);
  93. }
  94. /* Write SIZE bytes from BUF into the target process PH at address ADDR. */
  95. ps_err_e
  96. ps_pdwrite (struct ps_prochandle *ph, psaddr_t addr,
  97. const void *buf, size_t size)
  98. {
  99. return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
  100. }
  101. /* Get a regcache for LWPID using its inferior's "main" architecture,
  102. which is the register set libthread_db expects to be using. In
  103. multi-arch debugging scenarios, the thread's architecture may
  104. differ from the inferior's "main" architecture. */
  105. static struct regcache *
  106. get_ps_regcache (struct ps_prochandle *ph, lwpid_t lwpid)
  107. {
  108. inferior *inf = ph->thread->inf;
  109. return get_thread_arch_regcache (inf->process_target (),
  110. ptid_t (inf->pid, lwpid),
  111. inf->gdbarch);
  112. }
  113. /* Get the general registers of LWP LWPID within the target process PH
  114. and store them in GREGSET. */
  115. ps_err_e
  116. ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
  117. {
  118. struct regcache *regcache = get_ps_regcache (ph, lwpid);
  119. target_fetch_registers (regcache, -1);
  120. fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
  121. return PS_OK;
  122. }
  123. /* Set the general registers of LWP LWPID within the target process PH
  124. from GREGSET. */
  125. ps_err_e
  126. ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid, const prgregset_t gregset)
  127. {
  128. struct regcache *regcache = get_ps_regcache (ph, lwpid);
  129. supply_gregset (regcache, (const gdb_gregset_t *) gregset);
  130. target_store_registers (regcache, -1);
  131. return PS_OK;
  132. }
  133. /* Get the floating-point registers of LWP LWPID within the target
  134. process PH and store them in FPREGSET. */
  135. ps_err_e
  136. ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
  137. prfpregset_t *fpregset)
  138. {
  139. struct regcache *regcache = get_ps_regcache (ph, lwpid);
  140. target_fetch_registers (regcache, -1);
  141. fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
  142. return PS_OK;
  143. }
  144. /* Set the floating-point registers of LWP LWPID within the target
  145. process PH from FPREGSET. */
  146. ps_err_e
  147. ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
  148. const prfpregset_t *fpregset)
  149. {
  150. struct regcache *regcache = get_ps_regcache (ph, lwpid);
  151. supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
  152. target_store_registers (regcache, -1);
  153. return PS_OK;
  154. }
  155. /* Return overall process id of the target PH. Special for GNU/Linux
  156. -- not used on Solaris. */
  157. pid_t
  158. ps_getpid (struct ps_prochandle *ph)
  159. {
  160. return ph->thread->ptid.pid ();
  161. }
  162. void _initialize_proc_service ();
  163. void
  164. _initialize_proc_service ()
  165. {
  166. /* This function solely exists to make sure this module is linked
  167. into the final binary. */
  168. }