OSDN Git Service

include/elf/
[pf3gnuchains/sourceware.git] / gdb / proc-service.c
1 /* <proc_service.h> implementation.
2
3    Copyright (C) 1999, 2000, 2002, 2007, 2008 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 = pid_to_ptid (ph->pid);
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 }
192
193 /* Search for the symbol named NAME within the object named OBJ within
194    the target process PH.  If the symbol is found the address of the
195    symbol is stored in SYM_ADDR.  */
196
197 ps_err_e
198 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *obj,
199                    const char *name, psaddr_t *sym_addr)
200 {
201   struct minimal_symbol *ms;
202
203   /* FIXME: kettenis/2000-09-03: What should we do with OBJ?  */
204   ms = lookup_minimal_symbol (name, NULL, NULL);
205   if (ms == NULL)
206     return PS_NOSYM;
207
208   *sym_addr = core_addr_to_ps_addr (SYMBOL_VALUE_ADDRESS (ms));
209   return PS_OK;
210 }
211
212 /* Read SIZE bytes from the target process PH at address ADDR and copy
213    them into BUF.  */
214
215 ps_err_e
216 ps_pdread (gdb_ps_prochandle_t ph, psaddr_t addr,
217            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
218 {
219   return ps_xfer_memory (ph, addr, buf, size, 0);
220 }
221
222 /* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
223
224 ps_err_e
225 ps_pdwrite (gdb_ps_prochandle_t ph, psaddr_t addr,
226             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
227 {
228   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
229 }
230
231 /* Read SIZE bytes from the target process PH at address ADDR and copy
232    them into BUF.  */
233
234 ps_err_e
235 ps_ptread (gdb_ps_prochandle_t ph, psaddr_t addr,
236            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
237 {
238   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 0);
239 }
240
241 /* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
242
243 ps_err_e
244 ps_ptwrite (gdb_ps_prochandle_t ph, psaddr_t addr,
245             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
246 {
247   return ps_xfer_memory (ph, addr, (gdb_byte *) buf, size, 1);
248 }
249
250 /* Get the general registers of LWP LWPID within the target process PH
251    and store them in GREGSET.  */
252
253 ps_err_e
254 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
255 {
256   struct cleanup *old_chain = save_inferior_ptid ();
257   struct regcache *regcache;
258
259   inferior_ptid = BUILD_LWP (lwpid, ph->pid);
260   regcache = get_thread_regcache (inferior_ptid);
261
262   target_fetch_registers (regcache, -1);
263   fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
264
265   do_cleanups (old_chain);
266   return PS_OK;
267 }
268
269 /* Set the general registers of LWP LWPID within the target process PH
270    from GREGSET.  */
271
272 ps_err_e
273 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, const prgregset_t gregset)
274 {
275   struct cleanup *old_chain = save_inferior_ptid ();
276   struct regcache *regcache;
277
278   inferior_ptid = BUILD_LWP (lwpid, ph->pid);
279   regcache = get_thread_regcache (inferior_ptid);
280
281   supply_gregset (regcache, (const gdb_gregset_t *) gregset);
282   target_store_registers (regcache, -1);
283
284   do_cleanups (old_chain);
285   return PS_OK;
286 }
287
288 /* Get the floating-point registers of LWP LWPID within the target
289    process PH and store them in FPREGSET.  */
290
291 ps_err_e
292 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
293                gdb_prfpregset_t *fpregset)
294 {
295   struct cleanup *old_chain = save_inferior_ptid ();
296   struct regcache *regcache;
297
298   inferior_ptid = BUILD_LWP (lwpid, ph->pid);
299   regcache = get_thread_regcache (inferior_ptid);
300
301   target_fetch_registers (regcache, -1);
302   fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
303
304   do_cleanups (old_chain);
305   return PS_OK;
306 }
307
308 /* Set the floating-point registers of LWP LWPID within the target
309    process PH from FPREGSET.  */
310
311 ps_err_e
312 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
313                const gdb_prfpregset_t *fpregset)
314 {
315   struct cleanup *old_chain = save_inferior_ptid ();
316   struct regcache *regcache;
317
318   inferior_ptid = BUILD_LWP (lwpid, ph->pid);
319   regcache = get_thread_regcache (inferior_ptid);
320
321   supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
322   target_store_registers (regcache, -1);
323
324   do_cleanups (old_chain);
325   return PS_OK;
326 }
327
328 /* Return overall process id of the target PH.  Special for GNU/Linux
329    -- not used on Solaris.  */
330
331 pid_t
332 ps_getpid (gdb_ps_prochandle_t ph)
333 {
334   return ph->pid;
335 }
336
337 void
338 _initialize_proc_service (void)
339 {
340   /* This function solely exists to make sure this module is linked
341      into the final binary.  */
342 }