xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/proc-service.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* <proc_service.h> implementation.
2 
3    Copyright (C) 1999-2020 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 
22 #include "gdbcore.h"
23 #include "inferior.h"
24 #include "gdbthread.h"
25 #include "symtab.h"
26 #include "target.h"
27 #include "regcache.h"
28 #include "objfiles.h"
29 
30 #include "gdb_proc_service.h"
31 
32 #include <sys/procfs.h>
33 
34 /* Prototypes for supply_gregset etc.  */
35 #include "gregset.h"
36 
37 
38 /* Helper functions.  */
39 
40 /* Convert a psaddr_t to a CORE_ADDR.  */
41 
42 static CORE_ADDR
43 ps_addr_to_core_addr (psaddr_t addr)
44 {
45   if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
46     return (intptr_t) addr;
47   else
48     return (uintptr_t) addr;
49 }
50 
51 /* Convert a CORE_ADDR to a psaddr_t.  */
52 
53 static psaddr_t
54 core_addr_to_ps_addr (CORE_ADDR addr)
55 {
56   if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
57     return (psaddr_t) (intptr_t) addr;
58   else
59     return (psaddr_t) (uintptr_t) addr;
60 }
61 
62 /* Transfer LEN bytes of memory between BUF and address ADDR in the
63    process specified by PH.  If WRITE, transfer them to the process,
64    else transfer them from the process.  Returns PS_OK for success,
65    PS_ERR on failure.
66 
67    This is a helper function for ps_pdread and ps_pdwrite.  */
68 
69 static ps_err_e
70 ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr,
71 		gdb_byte *buf, size_t len, int write)
72 {
73   scoped_restore_current_inferior restore_inferior;
74   set_current_inferior (ph->thread->inf);
75 
76   scoped_restore_current_program_space restore_current_progspace;
77   set_current_program_space (ph->thread->inf->pspace);
78 
79   scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
80   inferior_ptid = ph->thread->ptid;
81 
82   CORE_ADDR core_addr = ps_addr_to_core_addr (addr);
83 
84   int ret;
85   if (write)
86     ret = target_write_memory (core_addr, buf, len);
87   else
88     ret = target_read_memory (core_addr, buf, len);
89   return (ret == 0 ? PS_OK : PS_ERR);
90 }
91 
92 
93 /* Search for the symbol named NAME within the object named OBJ within
94    the target process PH.  If the symbol is found the address of the
95    symbol is stored in SYM_ADDR.  */
96 
97 ps_err_e
98 ps_pglobal_lookup (struct ps_prochandle *ph, const char *obj,
99 		   const char *name, psaddr_t *sym_addr)
100 {
101   inferior *inf = ph->thread->inf;
102 
103   scoped_restore_current_program_space restore_pspace;
104 
105   set_current_program_space (inf->pspace);
106 
107   /* FIXME: kettenis/2000-09-03: What should we do with OBJ?  */
108   bound_minimal_symbol ms = lookup_minimal_symbol (name, NULL, NULL);
109   if (ms.minsym == NULL)
110     return PS_NOSYM;
111 
112   *sym_addr = core_addr_to_ps_addr (BMSYMBOL_VALUE_ADDRESS (ms));
113   return PS_OK;
114 }
115 
116 /* Read SIZE bytes from the target process PH at address ADDR and copy
117    them into BUF.  */
118 
119 ps_err_e
120 ps_pdread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t size)
121 {
122   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 0);
123 }
124 
125 /* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
126 
127 ps_err_e
128 ps_pdwrite (struct ps_prochandle *ph, psaddr_t addr,
129 	    const void *buf, size_t size)
130 {
131   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
132 }
133 
134 /* Get a regcache for LWPID using its inferior's "main" architecture,
135    which is the register set libthread_db expects to be using.  In
136    multi-arch debugging scenarios, the thread's architecture may
137    differ from the inferior's "main" architecture.  */
138 
139 static struct regcache *
140 get_ps_regcache (struct ps_prochandle *ph, lwpid_t lwpid)
141 {
142   inferior *inf = ph->thread->inf;
143   return get_thread_arch_regcache (inf->process_target (),
144 				   ptid_t (inf->pid, lwpid),
145 				   inf->gdbarch);
146 }
147 
148 /* Get the general registers of LWP LWPID within the target process PH
149    and store them in GREGSET.  */
150 
151 ps_err_e
152 ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
153 {
154   struct regcache *regcache = get_ps_regcache (ph, lwpid);
155 
156   target_fetch_registers (regcache, -1);
157   fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
158 
159   return PS_OK;
160 }
161 
162 /* Set the general registers of LWP LWPID within the target process PH
163    from GREGSET.  */
164 
165 ps_err_e
166 ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid, const prgregset_t gregset)
167 {
168   struct regcache *regcache = get_ps_regcache (ph, lwpid);
169 
170   supply_gregset (regcache, (const gdb_gregset_t *) gregset);
171   target_store_registers (regcache, -1);
172 
173   return PS_OK;
174 }
175 
176 /* Get the floating-point registers of LWP LWPID within the target
177    process PH and store them in FPREGSET.  */
178 
179 ps_err_e
180 ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
181 	       prfpregset_t *fpregset)
182 {
183   struct regcache *regcache = get_ps_regcache (ph, lwpid);
184 
185   target_fetch_registers (regcache, -1);
186   fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
187 
188   return PS_OK;
189 }
190 
191 /* Set the floating-point registers of LWP LWPID within the target
192    process PH from FPREGSET.  */
193 
194 ps_err_e
195 ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
196 	       const prfpregset_t *fpregset)
197 {
198   struct regcache *regcache = get_ps_regcache (ph, lwpid);
199 
200   supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
201   target_store_registers (regcache, -1);
202 
203   return PS_OK;
204 }
205 
206 /* Return overall process id of the target PH.  Special for GNU/Linux
207    -- not used on Solaris.  */
208 
209 pid_t
210 ps_getpid (struct ps_prochandle *ph)
211 {
212   return ph->thread->ptid.pid ();
213 }
214 
215 void _initialize_proc_service ();
216 void
217 _initialize_proc_service ()
218 {
219   /* This function solely exists to make sure this module is linked
220      into the final binary.  */
221 }
222