1 /* <proc_service.h> implementation.
3 Copyright (C) 1999-2000, 2002, 2007-2012 Free Software Foundation,
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "gdb_proc_service.h"
31 #include <sys/procfs.h>
33 /* Prototypes for supply_gregset etc. */
37 /* Fix-up some broken systems. */
39 /* The prototypes in <proc_service.h> are slightly different on older
40 systems. Compensate for the discrepancies. */
42 #ifdef PROC_SERVICE_IS_OLD
43 typedef const struct ps_prochandle *gdb_ps_prochandle_t;
44 typedef char *gdb_ps_read_buf_t;
45 typedef char *gdb_ps_write_buf_t;
46 typedef int gdb_ps_size_t;
48 typedef struct ps_prochandle *gdb_ps_prochandle_t;
49 typedef void *gdb_ps_read_buf_t;
50 typedef const void *gdb_ps_write_buf_t;
51 typedef size_t gdb_ps_size_t;
55 /* Building process ids. */
57 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
60 /* Helper functions. */
62 /* Convert a psaddr_t to a CORE_ADDR. */
65 ps_addr_to_core_addr (psaddr_t addr)
67 if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
68 return (intptr_t) addr;
70 return (uintptr_t) addr;
73 /* Convert a CORE_ADDR to a psaddr_t. */
76 core_addr_to_ps_addr (CORE_ADDR addr)
78 if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
79 return (psaddr_t) (intptr_t) addr;
81 return (psaddr_t) (uintptr_t) addr;
84 /* Transfer LEN bytes of memory between BUF and address ADDR in the
85 process specified by PH. If WRITE, transfer them to the process,
86 else transfer them from the process. Returns PS_OK for success,
89 This is a helper function for ps_pdread, ps_pdwrite, ps_ptread and
93 ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr,
94 gdb_byte *buf, size_t len, int write)
96 struct cleanup *old_chain = save_inferior_ptid ();
98 CORE_ADDR core_addr = ps_addr_to_core_addr (addr);
100 inferior_ptid = ph->ptid;
103 ret = target_write_memory (core_addr, buf, len);
105 ret = target_read_memory (core_addr, buf, len);
107 do_cleanups (old_chain);
109 return (ret == 0 ? PS_OK : PS_ERR);
113 /* Stop the target process PH. */
116 ps_pstop (gdb_ps_prochandle_t ph)
118 /* The process is always stopped when under control of GDB. */
122 /* Resume the target process PH. */
125 ps_pcontinue (gdb_ps_prochandle_t ph)
127 /* Pretend we did successfully continue the process. GDB will take
128 care of it later on. */
132 /* Stop the lightweight process LWPID within the target process PH. */
135 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
137 /* All lightweight processes are stopped when under control of GDB. */
141 /* Resume the lightweight process (LWP) LWPID within the target
145 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
147 /* Pretend we did successfully continue LWPID. GDB will take care
152 /* Get the size of the architecture-dependent extra state registers
153 for LWP LWPID within the target process PH and return it in
157 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
159 /* FIXME: Not supported yet. */
163 /* Get the extra state registers of LWP LWPID within the target
164 process PH and store them in XREGSET. */
167 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
169 /* FIXME: Not supported yet. */
173 /* Set the extra state registers of LWP LWPID within the target
174 process PH from XREGSET. */
177 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
179 /* FIXME: Not supported yet. */
183 /* Log (additional) diognostic information. */
186 ps_plog (const char *fmt, ...)
190 va_start (args, fmt);
191 vfprintf_filtered (gdb_stderr, fmt, args);
195 /* Search for the symbol named NAME within the object named OBJ within
196 the target process PH. If the symbol is found the address of the
197 symbol is stored in SYM_ADDR. */
200 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *obj,
201 const char *name, psaddr_t *sym_addr)
203 struct minimal_symbol *ms;
204 struct cleanup *old_chain = save_current_program_space ();
205 struct inferior *inf = find_inferior_pid (ptid_get_pid (ph->ptid));
208 set_current_program_space (inf->pspace);
210 /* FIXME: kettenis/2000-09-03: What should we do with OBJ? */
211 ms = lookup_minimal_symbol (name, NULL, NULL);
216 *sym_addr = core_addr_to_ps_addr (SYMBOL_VALUE_ADDRESS (ms));
220 do_cleanups (old_chain);
224 /* Read SIZE bytes from the target process PH at address ADDR and copy
228 ps_pdread (gdb_ps_prochandle_t ph, psaddr_t addr,
229 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
231 return ps_xfer_memory (ph, addr, buf, size, 0);
234 /* Write SIZE bytes from BUF into the target process PH at address ADDR. */
237 ps_pdwrite (gdb_ps_prochandle_t ph, psaddr_t addr,
238 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
240 return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
243 /* Read SIZE bytes from the target process PH at address ADDR and copy
247 ps_ptread (gdb_ps_prochandle_t ph, psaddr_t addr,
248 gdb_ps_read_buf_t buf, gdb_ps_size_t size)
250 return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 0);
253 /* Write SIZE bytes from BUF into the target process PH at address ADDR. */
256 ps_ptwrite (gdb_ps_prochandle_t ph, psaddr_t addr,
257 gdb_ps_write_buf_t buf, gdb_ps_size_t size)
259 return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
262 /* Get the general registers of LWP LWPID within the target process PH
263 and store them in GREGSET. */
266 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
268 struct cleanup *old_chain = save_inferior_ptid ();
269 struct regcache *regcache;
271 inferior_ptid = BUILD_LWP (lwpid, ptid_get_pid (ph->ptid));
272 regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch);
274 target_fetch_registers (regcache, -1);
275 fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
277 do_cleanups (old_chain);
281 /* Set the general registers of LWP LWPID within the target process PH
285 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, const prgregset_t gregset)
287 struct cleanup *old_chain = save_inferior_ptid ();
288 struct regcache *regcache;
290 inferior_ptid = BUILD_LWP (lwpid, ptid_get_pid (ph->ptid));
291 regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch);
293 supply_gregset (regcache, (const gdb_gregset_t *) gregset);
294 target_store_registers (regcache, -1);
296 do_cleanups (old_chain);
300 /* Get the floating-point registers of LWP LWPID within the target
301 process PH and store them in FPREGSET. */
304 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
305 gdb_prfpregset_t *fpregset)
307 struct cleanup *old_chain = save_inferior_ptid ();
308 struct regcache *regcache;
310 inferior_ptid = BUILD_LWP (lwpid, ptid_get_pid (ph->ptid));
311 regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch);
313 target_fetch_registers (regcache, -1);
314 fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
316 do_cleanups (old_chain);
320 /* Set the floating-point registers of LWP LWPID within the target
321 process PH from FPREGSET. */
324 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
325 const gdb_prfpregset_t *fpregset)
327 struct cleanup *old_chain = save_inferior_ptid ();
328 struct regcache *regcache;
330 inferior_ptid = BUILD_LWP (lwpid, ptid_get_pid (ph->ptid));
331 regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch);
333 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
334 target_store_registers (regcache, -1);
336 do_cleanups (old_chain);
340 /* Return overall process id of the target PH. Special for GNU/Linux
341 -- not used on Solaris. */
344 ps_getpid (gdb_ps_prochandle_t ph)
346 return ptid_get_pid (ph->ptid);
349 /* Provide a prototype to silence -Wmissing-prototypes. */
350 extern initialize_file_ftype _initialize_proc_service;
353 _initialize_proc_service (void)
355 /* This function solely exists to make sure this module is linked
356 into the final binary. */