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