GDB (xrefs)
/tmp/gdb-7.10/gdb/proc-service.c
Go to the documentation of this file.
1 /* <proc_service.h> implementation.
2 
3  Copyright (C) 1999-2015 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 "symtab.h"
25 #include "target.h"
26 #include "regcache.h"
27 #include "objfiles.h"
28 
29 #include "gdb_proc_service.h"
30 
31 #include <sys/procfs.h>
32 
33 /* Prototypes for supply_gregset etc. */
34 #include "gregset.h"
35 
36 
37 /* Fix-up some broken systems. */
38 
39 /* The prototypes in <proc_service.h> are slightly different on older
40  systems. Compensate for the discrepancies. */
41 
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;
47 #else
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;
52 #endif
53 
54 
55 /* Helper functions. */
56 
57 /* Convert a psaddr_t to a CORE_ADDR. */
58 
59 static CORE_ADDR
61 {
62  if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
63  return (intptr_t) addr;
64  else
65  return (uintptr_t) addr;
66 }
67 
68 /* Convert a CORE_ADDR to a psaddr_t. */
69 
70 static psaddr_t
72 {
73  if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
74  return (psaddr_t) (intptr_t) addr;
75  else
76  return (psaddr_t) (uintptr_t) addr;
77 }
78 
79 /* Transfer LEN bytes of memory between BUF and address ADDR in the
80  process specified by PH. If WRITE, transfer them to the process,
81  else transfer them from the process. Returns PS_OK for success,
82  PS_ERR on failure.
83 
84  This is a helper function for ps_pdread and ps_pdwrite. */
85 
86 static ps_err_e
87 ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr,
88  gdb_byte *buf, size_t len, int write)
89 {
90  struct cleanup *old_chain = save_inferior_ptid ();
91  int ret;
92  CORE_ADDR core_addr = ps_addr_to_core_addr (addr);
93 
94  inferior_ptid = ph->ptid;
95 
96  if (write)
97  ret = target_write_memory (core_addr, buf, len);
98  else
99  ret = target_read_memory (core_addr, buf, len);
100 
101  do_cleanups (old_chain);
102 
103  return (ret == 0 ? PS_OK : PS_ERR);
104 }
105 
106 
107 /* Search for the symbol named NAME within the object named OBJ within
108  the target process PH. If the symbol is found the address of the
109  symbol is stored in SYM_ADDR. */
110 
111 ps_err_e
112 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *obj,
113  const char *name, psaddr_t *sym_addr)
114 {
115  struct bound_minimal_symbol ms;
116  struct cleanup *old_chain = save_current_program_space ();
117  struct inferior *inf = find_inferior_ptid (ph->ptid);
118  ps_err_e result;
119 
121 
122  /* FIXME: kettenis/2000-09-03: What should we do with OBJ? */
123  ms = lookup_minimal_symbol (name, NULL, NULL);
124  if (ms.minsym == NULL)
125  result = PS_NOSYM;
126  else
127  {
128  *sym_addr = core_addr_to_ps_addr (BMSYMBOL_VALUE_ADDRESS (ms));
129  result = PS_OK;
130  }
131 
132  do_cleanups (old_chain);
133  return result;
134 }
135 
136 /* Read SIZE bytes from the target process PH at address ADDR and copy
137  them into BUF. */
138 
139 ps_err_e
140 ps_pdread (gdb_ps_prochandle_t ph, psaddr_t addr,
142 {
143  return ps_xfer_memory (ph, addr, buf, size, 0);
144 }
145 
146 /* Write SIZE bytes from BUF into the target process PH at address ADDR. */
147 
148 ps_err_e
149 ps_pdwrite (gdb_ps_prochandle_t ph, psaddr_t addr,
151 {
152  return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
153 }
154 
155 /* Get the general registers of LWP LWPID within the target process PH
156  and store them in GREGSET. */
157 
158 ps_err_e
159 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
160 {
161  struct cleanup *old_chain = save_inferior_ptid ();
162  struct regcache *regcache;
163 
164  inferior_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
166 
167  target_fetch_registers (regcache, -1);
168  fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
169 
170  do_cleanups (old_chain);
171  return PS_OK;
172 }
173 
174 /* Set the general registers of LWP LWPID within the target process PH
175  from GREGSET. */
176 
177 ps_err_e
178 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, const prgregset_t gregset)
179 {
180  struct cleanup *old_chain = save_inferior_ptid ();
181  struct regcache *regcache;
182 
183  inferior_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
185 
186  supply_gregset (regcache, (const gdb_gregset_t *) gregset);
187  target_store_registers (regcache, -1);
188 
189  do_cleanups (old_chain);
190  return PS_OK;
191 }
192 
193 /* Get the floating-point registers of LWP LWPID within the target
194  process PH and store them in FPREGSET. */
195 
196 ps_err_e
197 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
198  gdb_prfpregset_t *fpregset)
199 {
200  struct cleanup *old_chain = save_inferior_ptid ();
201  struct regcache *regcache;
202 
203  inferior_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
205 
206  target_fetch_registers (regcache, -1);
207  fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
208 
209  do_cleanups (old_chain);
210  return PS_OK;
211 }
212 
213 /* Set the floating-point registers of LWP LWPID within the target
214  process PH from FPREGSET. */
215 
216 ps_err_e
217 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
218  const gdb_prfpregset_t *fpregset)
219 {
220  struct cleanup *old_chain = save_inferior_ptid ();
221  struct regcache *regcache;
222 
223  inferior_ptid = ptid_build (ptid_get_pid (ph->ptid), lwpid, 0);
225 
226  supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
227  target_store_registers (regcache, -1);
228 
229  do_cleanups (old_chain);
230  return PS_OK;
231 }
232 
233 /* Return overall process id of the target PH. Special for GNU/Linux
234  -- not used on Solaris. */
235 
236 pid_t
237 ps_getpid (gdb_ps_prochandle_t ph)
238 {
239  return ptid_get_pid (ph->ptid);
240 }
241 
242 /* Provide a prototype to silence -Wmissing-prototypes. */
244 
245 void
247 {
248  /* This function solely exists to make sure this module is linked
249  into the final binary. */
250 }
unsigned int lwpid_t
struct gdbarch * target_gdbarch(void)
Definition: gdbarch.c:5143
struct ps_prochandle * gdb_ps_prochandle_t
Definition: proc-service.c:48
bfd_vma CORE_ADDR
Definition: common-types.h:41
int target_write_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
Definition: target.c:1474
ps_err_e ps_pglobal_lookup(gdb_ps_prochandle_t ph, const char *obj, const char *name, psaddr_t *sym_addr)
Definition: proc-service.c:112
void fill_gregset(const struct regcache *regcache, gdb_gregset_t *gregsetp, int regno)
initialize_file_ftype _initialize_proc_service
EXTERN_C_POP typedef prfpregset_t gdb_prfpregset_t
#define BMSYMBOL_VALUE_ADDRESS(symbol)
Definition: symtab.h:393
ps_err_e ps_lsetregs(gdb_ps_prochandle_t ph, lwpid_t lwpid, const prgregset_t gregset)
Definition: proc-service.c:178
ps_err_e ps_lgetregs(gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
Definition: proc-service.c:159
ps_err_e
const void * gdb_ps_write_buf_t
Definition: proc-service.c:50
struct inferior * find_inferior_ptid(ptid_t ptid)
Definition: inferior.c:373
void target_fetch_registers(struct regcache *regcache, int regno)
Definition: target.c:3419
void fill_fpregset(const struct regcache *regcache, gdb_fpregset_t *fpregsetp, int regno)
ptid_t ptid_build(int pid, long lwp, long tid)
Definition: ptid.c:31
void set_current_program_space(struct program_space *pspace)
Definition: progspace.c:199
ps_err_e ps_pdwrite(gdb_ps_prochandle_t ph, psaddr_t addr, gdb_ps_write_buf_t buf, gdb_ps_size_t size)
Definition: proc-service.c:149
const char *const name
Definition: aarch64-tdep.c:68
struct cleanup * save_inferior_ptid(void)
Definition: infrun.c:7538
void initialize_file_ftype(void)
Definition: defs.h:281
struct program_space * pspace
Definition: inferior.h:317
#define exec_bfd
Definition: exec.h:32
GDB_GREGSET_T gdb_gregset_t
Definition: gregset.h:34
pid_t ps_getpid(gdb_ps_prochandle_t ph)
Definition: proc-service.c:237
GDB_FPREGSET_T gdb_fpregset_t
Definition: gregset.h:35
void supply_gregset(struct regcache *regcache, const gdb_gregset_t *gregsetp)
Definition: gnu-nat.c:163
void * psaddr_t
size_t gdb_ps_size_t
Definition: proc-service.c:51
int ptid_get_pid(ptid_t ptid)
Definition: ptid.c:52
ps_err_e ps_lsetfpregs(gdb_ps_prochandle_t ph, lwpid_t lwpid, const gdb_prfpregset_t *fpregset)
Definition: proc-service.c:217
ps_err_e ps_pdread(gdb_ps_prochandle_t ph, psaddr_t addr, gdb_ps_read_buf_t buf, gdb_ps_size_t size)
Definition: proc-service.c:140
static psaddr_t core_addr_to_ps_addr(CORE_ADDR addr)
Definition: proc-service.c:71
gdb_gregset_t prgregset_t
bfd_byte gdb_byte
Definition: common-types.h:38
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition: target.c:1393
ptid_t inferior_ptid
Definition: infcmd.c:124
struct minimal_symbol * minsym
Definition: minsyms.h:32
static ps_err_e ps_xfer_memory(const struct ps_prochandle *ph, psaddr_t addr, gdb_byte *buf, size_t len, int write)
Definition: proc-service.c:87
ps_err_e ps_lgetfpregs(gdb_ps_prochandle_t ph, lwpid_t lwpid, gdb_prfpregset_t *fpregset)
Definition: proc-service.c:197
static CORE_ADDR ps_addr_to_core_addr(psaddr_t addr)
Definition: proc-service.c:60
void target_store_registers(struct regcache *regcache, int regno)
Definition: target.c:3427
struct cleanup * save_current_program_space(void)
Definition: progspace.c:228
void * gdb_ps_read_buf_t
Definition: proc-service.c:49
struct bound_minimal_symbol lookup_minimal_symbol(const char *name, const char *sfile, struct objfile *objf)
Definition: minsyms.c:163
size_t size
Definition: go32-nat.c:242
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:175
struct regcache * get_thread_arch_regcache(ptid_t ptid, struct gdbarch *gdbarch)
Definition: regcache.c:508
void supply_fpregset(struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
const ULONGEST const LONGEST len
Definition: target.h:309