include/opcode/
[external/binutils.git] / gdb / proc-service.c
1 /* <proc_service.h> implementation.
2
3    Copyright (C) 1999-2013 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
28 #include "gdb_proc_service.h"
29
30 #include <sys/procfs.h>
31
32 /* Prototypes for supply_gregset etc.  */
33 #include "gregset.h"
34 \f
35
36 /* Fix-up some broken systems.  */
37
38 /* The prototypes in <proc_service.h> are slightly different on older
39    systems.  Compensate for the discrepancies.  */
40
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;
46 #else
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;
51 #endif
52 \f
53
54 /* Building process ids.  */
55
56 #define BUILD_LWP(lwp, pid)     ptid_build (pid, lwp, 0)
57 \f
58
59 /* Helper functions.  */
60
61 /* Convert a psaddr_t to a CORE_ADDR.  */
62
63 static CORE_ADDR
64 ps_addr_to_core_addr (psaddr_t addr)
65 {
66   if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
67     return (intptr_t) addr;
68   else
69     return (uintptr_t) addr;
70 }
71
72 /* Convert a CORE_ADDR to a psaddr_t.  */
73
74 static psaddr_t
75 core_addr_to_ps_addr (CORE_ADDR addr)
76 {
77   if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
78     return (psaddr_t) (intptr_t) addr;
79   else
80     return (psaddr_t) (uintptr_t) addr;
81 }
82
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,
86    PS_ERR on failure.
87
88    This is a helper function for ps_pdread, ps_pdwrite, ps_ptread and
89    ps_ptwrite.  */
90
91 static ps_err_e
92 ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr,
93                 gdb_byte *buf, size_t len, int write)
94 {
95   struct cleanup *old_chain = save_inferior_ptid ();
96   int ret;
97   CORE_ADDR core_addr = ps_addr_to_core_addr (addr);
98
99   inferior_ptid = ph->ptid;
100
101   if (write)
102     ret = target_write_memory (core_addr, buf, len);
103   else
104     ret = target_read_memory (core_addr, buf, len);
105
106   do_cleanups (old_chain);
107
108   return (ret == 0 ? PS_OK : PS_ERR);
109 }
110 \f
111
112 /* Stop the target process PH.  */
113
114 ps_err_e
115 ps_pstop (gdb_ps_prochandle_t ph)
116 {
117   /* The process is always stopped when under control of GDB.  */
118   return PS_OK;
119 }
120
121 /* Resume the target process PH.  */
122
123 ps_err_e
124 ps_pcontinue (gdb_ps_prochandle_t ph)
125 {
126   /* Pretend we did successfully continue the process.  GDB will take
127      care of it later on.  */
128   return PS_OK;
129 }
130
131 /* Stop the lightweight process LWPID within the target process PH.  */
132
133 ps_err_e
134 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
135 {
136   /* All lightweight processes are stopped when under control of GDB.  */
137   return PS_OK;
138 }
139
140 /* Resume the lightweight process (LWP) LWPID within the target
141    process PH.  */
142
143 ps_err_e
144 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
145 {
146   /* Pretend we did successfully continue LWPID.  GDB will take care
147      of it later on.  */
148   return PS_OK;
149 }
150
151 /* Get the size of the architecture-dependent extra state registers
152    for LWP LWPID within the target process PH and return it in
153    *XREGSIZE.  */
154
155 ps_err_e
156 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
157 {
158   /* FIXME: Not supported yet.  */
159   return PS_OK;
160 }
161
162 /* Get the extra state registers of LWP LWPID within the target
163    process PH and store them in XREGSET.  */
164
165 ps_err_e
166 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
167 {
168   /* FIXME: Not supported yet.  */
169   return PS_OK;
170 }
171
172 /* Set the extra state registers of LWP LWPID within the target
173    process PH from XREGSET.  */
174
175 ps_err_e
176 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
177 {
178   /* FIXME: Not supported yet.  */
179   return PS_OK;
180 }
181
182 /* Log (additional) diognostic information.  */
183
184 void
185 ps_plog (const char *fmt, ...)
186 {
187   va_list args;
188
189   va_start (args, fmt);
190   vfprintf_filtered (gdb_stderr, fmt, args);
191   va_end (args);
192 }
193
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.  */
197
198 ps_err_e
199 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *obj,
200                    const char *name, psaddr_t *sym_addr)
201 {
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));
205   ps_err_e result;
206
207   set_current_program_space (inf->pspace);
208
209   /* FIXME: kettenis/2000-09-03: What should we do with OBJ?  */
210   ms = lookup_minimal_symbol (name, NULL, NULL);
211   if (ms == NULL)
212     result = PS_NOSYM;
213   else
214     {
215       *sym_addr = core_addr_to_ps_addr (SYMBOL_VALUE_ADDRESS (ms));
216       result = PS_OK;
217     }
218
219   do_cleanups (old_chain);
220   return result;
221 }
222
223 /* Read SIZE bytes from the target process PH at address ADDR and copy
224    them into BUF.  */
225
226 ps_err_e
227 ps_pdread (gdb_ps_prochandle_t ph, psaddr_t addr,
228            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
229 {
230   return ps_xfer_memory (ph, addr, buf, size, 0);
231 }
232
233 /* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
234
235 ps_err_e
236 ps_pdwrite (gdb_ps_prochandle_t ph, psaddr_t addr,
237             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
238 {
239   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
240 }
241
242 /* Read SIZE bytes from the target process PH at address ADDR and copy
243    them into BUF.  */
244
245 ps_err_e
246 ps_ptread (gdb_ps_prochandle_t ph, psaddr_t addr,
247            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
248 {
249   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 0);
250 }
251
252 /* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
253
254 ps_err_e
255 ps_ptwrite (gdb_ps_prochandle_t ph, psaddr_t addr,
256             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
257 {
258   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
259 }
260
261 /* Get the general registers of LWP LWPID within the target process PH
262    and store them in GREGSET.  */
263
264 ps_err_e
265 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
266 {
267   struct cleanup *old_chain = save_inferior_ptid ();
268   struct regcache *regcache;
269
270   inferior_ptid = BUILD_LWP (lwpid, ptid_get_pid (ph->ptid));
271   regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());
272
273   target_fetch_registers (regcache, -1);
274   fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
275
276   do_cleanups (old_chain);
277   return PS_OK;
278 }
279
280 /* Set the general registers of LWP LWPID within the target process PH
281    from GREGSET.  */
282
283 ps_err_e
284 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, const prgregset_t gregset)
285 {
286   struct cleanup *old_chain = save_inferior_ptid ();
287   struct regcache *regcache;
288
289   inferior_ptid = BUILD_LWP (lwpid, ptid_get_pid (ph->ptid));
290   regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());
291
292   supply_gregset (regcache, (const gdb_gregset_t *) gregset);
293   target_store_registers (regcache, -1);
294
295   do_cleanups (old_chain);
296   return PS_OK;
297 }
298
299 /* Get the floating-point registers of LWP LWPID within the target
300    process PH and store them in FPREGSET.  */
301
302 ps_err_e
303 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
304                gdb_prfpregset_t *fpregset)
305 {
306   struct cleanup *old_chain = save_inferior_ptid ();
307   struct regcache *regcache;
308
309   inferior_ptid = BUILD_LWP (lwpid, ptid_get_pid (ph->ptid));
310   regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());
311
312   target_fetch_registers (regcache, -1);
313   fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
314
315   do_cleanups (old_chain);
316   return PS_OK;
317 }
318
319 /* Set the floating-point registers of LWP LWPID within the target
320    process PH from FPREGSET.  */
321
322 ps_err_e
323 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
324                const gdb_prfpregset_t *fpregset)
325 {
326   struct cleanup *old_chain = save_inferior_ptid ();
327   struct regcache *regcache;
328
329   inferior_ptid = BUILD_LWP (lwpid, ptid_get_pid (ph->ptid));
330   regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());
331
332   supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
333   target_store_registers (regcache, -1);
334
335   do_cleanups (old_chain);
336   return PS_OK;
337 }
338
339 /* Return overall process id of the target PH.  Special for GNU/Linux
340    -- not used on Solaris.  */
341
342 pid_t
343 ps_getpid (gdb_ps_prochandle_t ph)
344 {
345   return ptid_get_pid (ph->ptid);
346 }
347
348 /* Provide a prototype to silence -Wmissing-prototypes.  */
349 extern initialize_file_ftype _initialize_proc_service;
350
351 void
352 _initialize_proc_service (void)
353 {
354   /* This function solely exists to make sure this module is linked
355      into the final binary.  */
356 }