1 /* <proc_service.h> implementation.
3 Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
28 #include "gdb_proc_service.h"
30 #include <sys/procfs.h>
32 /* Prototypes for supply_gregset etc. */
36 /* Fix-up some broken systems. */
38 /* The prototypes in <proc_service.h> are slightly different on older
39 systems. Compensate for the discrepancies. */
41 #ifdef PROC_SERVICE_IS_OLD
42 typedef const struct ps_prochandle
*gdb_ps_prochandle_t
;
43 typedef char *gdb_ps_read_buf_t
;
44 typedef char *gdb_ps_write_buf_t
;
45 typedef int gdb_ps_size_t
;
47 typedef struct ps_prochandle
*gdb_ps_prochandle_t
;
48 typedef void *gdb_ps_read_buf_t
;
49 typedef const void *gdb_ps_write_buf_t
;
50 typedef size_t gdb_ps_size_t
;
54 /* Building process ids. */
56 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
59 /* Helper functions. */
61 /* Convert a psaddr_t to a CORE_ADDR. */
64 ps_addr_to_core_addr (psaddr_t addr
)
66 if (exec_bfd
&& bfd_get_sign_extend_vma (exec_bfd
))
67 return (intptr_t) addr
;
69 return (uintptr_t) addr
;
72 /* Convert a CORE_ADDR to a psaddr_t. */
75 core_addr_to_ps_addr (CORE_ADDR addr
)
77 if (exec_bfd
&& bfd_get_sign_extend_vma (exec_bfd
))
78 return (psaddr_t
) (intptr_t) addr
;
80 return (psaddr_t
) (uintptr_t) addr
;
83 /* Transfer LEN bytes of memory between BUF and address ADDR in the
84 process specified by PH. If WRITE, transfer them to the process,
85 else transfer them from the process. Returns PS_OK for success,
88 This is a helper function for ps_pdread, ps_pdwrite, ps_ptread and
92 ps_xfer_memory (const struct ps_prochandle
*ph
, psaddr_t addr
,
93 gdb_byte
*buf
, size_t len
, int write
)
95 struct cleanup
*old_chain
= save_inferior_ptid ();
97 CORE_ADDR core_addr
= ps_addr_to_core_addr (addr
);
99 inferior_ptid
= ph
->ptid
;
102 ret
= target_write_memory (core_addr
, buf
, len
);
104 ret
= target_read_memory (core_addr
, buf
, len
);
106 do_cleanups (old_chain
);
108 return (ret
== 0 ? PS_OK
: PS_ERR
);
112 /* Stop the target process PH. */
115 ps_pstop (gdb_ps_prochandle_t ph
)
117 /* The process is always stopped when under control of GDB. */
121 /* Resume the target process PH. */
124 ps_pcontinue (gdb_ps_prochandle_t ph
)
126 /* Pretend we did successfully continue the process. GDB will take
127 care of it later on. */
131 /* Stop the lightweight process LWPID within the target process PH. */
134 ps_lstop (gdb_ps_prochandle_t ph
, lwpid_t lwpid
)
136 /* All lightweight processes are stopped when under control of GDB. */
140 /* Resume the lightweight process (LWP) LWPID within the target
144 ps_lcontinue (gdb_ps_prochandle_t ph
, lwpid_t lwpid
)
146 /* Pretend we did successfully continue LWPID. GDB will take care
151 /* Get the size of the architecture-dependent extra state registers
152 for LWP LWPID within the target process PH and return it in
156 ps_lgetxregsize (gdb_ps_prochandle_t ph
, lwpid_t lwpid
, int *xregsize
)
158 /* FIXME: Not supported yet. */
162 /* Get the extra state registers of LWP LWPID within the target
163 process PH and store them in XREGSET. */
166 ps_lgetxregs (gdb_ps_prochandle_t ph
, lwpid_t lwpid
, caddr_t xregset
)
168 /* FIXME: Not supported yet. */
172 /* Set the extra state registers of LWP LWPID within the target
173 process PH from XREGSET. */
176 ps_lsetxregs (gdb_ps_prochandle_t ph
, lwpid_t lwpid
, caddr_t xregset
)
178 /* FIXME: Not supported yet. */
182 /* Log (additional) diognostic information. */
185 ps_plog (const char *fmt
, ...)
189 va_start (args
, fmt
);
190 vfprintf_filtered (gdb_stderr
, fmt
, args
);
194 /* Search for the symbol named NAME within the object named OBJ within
195 the target process PH. If the symbol is found the address of the
196 symbol is stored in SYM_ADDR. */
199 ps_pglobal_lookup (gdb_ps_prochandle_t ph
, const char *obj
,
200 const char *name
, psaddr_t
*sym_addr
)
202 struct minimal_symbol
*ms
;
203 struct cleanup
*old_chain
= save_current_program_space ();
204 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (ph
->ptid
));
207 set_current_program_space (inf
->pspace
);
209 /* FIXME: kettenis/2000-09-03: What should we do with OBJ? */
210 ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
215 *sym_addr
= core_addr_to_ps_addr (SYMBOL_VALUE_ADDRESS (ms
));
219 do_cleanups (old_chain
);
223 /* Read SIZE bytes from the target process PH at address ADDR and copy
227 ps_pdread (gdb_ps_prochandle_t ph
, psaddr_t addr
,
228 gdb_ps_read_buf_t buf
, gdb_ps_size_t size
)
230 return ps_xfer_memory (ph
, addr
, buf
, size
, 0);
233 /* Write SIZE bytes from BUF into the target process PH at address ADDR. */
236 ps_pdwrite (gdb_ps_prochandle_t ph
, psaddr_t addr
,
237 gdb_ps_write_buf_t buf
, gdb_ps_size_t size
)
239 return ps_xfer_memory (ph
, addr
, (gdb_byte
*) buf
, size
, 1);
242 /* Read SIZE bytes from the target process PH at address ADDR and copy
246 ps_ptread (gdb_ps_prochandle_t ph
, psaddr_t addr
,
247 gdb_ps_read_buf_t buf
, gdb_ps_size_t size
)
249 return ps_xfer_memory (ph
, addr
, (gdb_byte
*) buf
, size
, 0);
252 /* Write SIZE bytes from BUF into the target process PH at address ADDR. */
255 ps_ptwrite (gdb_ps_prochandle_t ph
, psaddr_t addr
,
256 gdb_ps_write_buf_t buf
, gdb_ps_size_t size
)
258 return ps_xfer_memory (ph
, addr
, (gdb_byte
*) buf
, size
, 1);
261 /* Get the general registers of LWP LWPID within the target process PH
262 and store them in GREGSET. */
265 ps_lgetregs (gdb_ps_prochandle_t ph
, lwpid_t lwpid
, prgregset_t gregset
)
267 struct cleanup
*old_chain
= save_inferior_ptid ();
268 struct regcache
*regcache
;
270 inferior_ptid
= BUILD_LWP (lwpid
, ptid_get_pid (ph
->ptid
));
271 regcache
= get_thread_arch_regcache (inferior_ptid
, target_gdbarch ());
273 target_fetch_registers (regcache
, -1);
274 fill_gregset (regcache
, (gdb_gregset_t
*) gregset
, -1);
276 do_cleanups (old_chain
);
280 /* Set the general registers of LWP LWPID within the target process PH
284 ps_lsetregs (gdb_ps_prochandle_t ph
, lwpid_t lwpid
, const prgregset_t gregset
)
286 struct cleanup
*old_chain
= save_inferior_ptid ();
287 struct regcache
*regcache
;
289 inferior_ptid
= BUILD_LWP (lwpid
, ptid_get_pid (ph
->ptid
));
290 regcache
= get_thread_arch_regcache (inferior_ptid
, target_gdbarch ());
292 supply_gregset (regcache
, (const gdb_gregset_t
*) gregset
);
293 target_store_registers (regcache
, -1);
295 do_cleanups (old_chain
);
299 /* Get the floating-point registers of LWP LWPID within the target
300 process PH and store them in FPREGSET. */
303 ps_lgetfpregs (gdb_ps_prochandle_t ph
, lwpid_t lwpid
,
304 gdb_prfpregset_t
*fpregset
)
306 struct cleanup
*old_chain
= save_inferior_ptid ();
307 struct regcache
*regcache
;
309 inferior_ptid
= BUILD_LWP (lwpid
, ptid_get_pid (ph
->ptid
));
310 regcache
= get_thread_arch_regcache (inferior_ptid
, target_gdbarch ());
312 target_fetch_registers (regcache
, -1);
313 fill_fpregset (regcache
, (gdb_fpregset_t
*) fpregset
, -1);
315 do_cleanups (old_chain
);
319 /* Set the floating-point registers of LWP LWPID within the target
320 process PH from FPREGSET. */
323 ps_lsetfpregs (gdb_ps_prochandle_t ph
, lwpid_t lwpid
,
324 const gdb_prfpregset_t
*fpregset
)
326 struct cleanup
*old_chain
= save_inferior_ptid ();
327 struct regcache
*regcache
;
329 inferior_ptid
= BUILD_LWP (lwpid
, ptid_get_pid (ph
->ptid
));
330 regcache
= get_thread_arch_regcache (inferior_ptid
, target_gdbarch ());
332 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregset
);
333 target_store_registers (regcache
, -1);
335 do_cleanups (old_chain
);
339 /* Return overall process id of the target PH. Special for GNU/Linux
340 -- not used on Solaris. */
343 ps_getpid (gdb_ps_prochandle_t ph
)
345 return ptid_get_pid (ph
->ptid
);
348 /* Provide a prototype to silence -Wmissing-prototypes. */
349 extern initialize_file_ftype _initialize_proc_service
;
352 _initialize_proc_service (void)
354 /* This function solely exists to make sure this module is linked
355 into the final binary. */