1/* <proc_service.h> implementation.
2
3   Copyright 1999, 2000, 2002 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 2 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, write to the Free Software
19   Foundation, Inc., 59 Temple Place - Suite 330,
20   Boston, MA 02111-1307, USA.  */
21
22#include "defs.h"
23
24#include "gdb_proc_service.h"
25#include <sys/procfs.h>
26
27#include "inferior.h"
28#include "symtab.h"
29#include "target.h"
30
31/* Prototypes for supply_gregset etc.  */
32#include "gregset.h"
33
34
35/* Fix-up some broken systems.  */
36
37/* The prototypes in <proc_service.h> are slightly different on older
38   systems.  Compensate for the discrepancies.  */
39
40#ifdef PROC_SERVICE_IS_OLD
41typedef const struct ps_prochandle *gdb_ps_prochandle_t;
42typedef char *gdb_ps_read_buf_t;
43typedef char *gdb_ps_write_buf_t;
44typedef int gdb_ps_size_t;
45#else
46typedef struct ps_prochandle *gdb_ps_prochandle_t;
47typedef void *gdb_ps_read_buf_t;
48typedef const void *gdb_ps_write_buf_t;
49typedef size_t gdb_ps_size_t;
50#endif
51
52
53/* Building process ids.  */
54
55#define BUILD_LWP(lwp, pid)	ptid_build (pid, lwp, 0)
56
57
58/* Helper functions.  */
59
60/* Transfer LEN bytes of memory between BUF and address ADDR in the
61   process specified by PH.  If WRITE, transfer them to the process,
62   else transfer them from the process.  Returns PS_OK for success,
63   PS_ERR on failure.
64
65   This is a helper function for ps_pdread, ps_pdwrite, ps_ptread and
66   ps_ptwrite.  */
67
68static ps_err_e
69ps_xfer_memory (const struct ps_prochandle *ph, paddr_t addr,
70		char *buf, size_t len, int write)
71{
72  struct cleanup *old_chain = save_inferior_ptid ();
73  int ret;
74
75  inferior_ptid = pid_to_ptid (ph->pid);
76
77  if (write)
78    ret = target_write_memory (addr, buf, len);
79  else
80    ret = target_read_memory (addr, buf, len);
81
82  do_cleanups (old_chain);
83
84  return (ret == 0 ? PS_OK : PS_ERR);
85}
86
87
88/* Stop the target process PH.  */
89
90ps_err_e
91ps_pstop (gdb_ps_prochandle_t ph)
92{
93  /* The process is always stopped when under control of GDB.  */
94  return PS_OK;
95}
96
97/* Resume the target process PH.  */
98
99ps_err_e
100ps_pcontinue (gdb_ps_prochandle_t ph)
101{
102  /* Pretend we did successfully continue the process.  GDB will take
103     care of it later on.  */
104  return PS_OK;
105}
106
107/* Stop the lightweight process LWPID within the target process PH.  */
108
109ps_err_e
110ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
111{
112  /* All lightweight processes are stopped when under control of GDB.  */
113  return PS_OK;
114}
115
116/* Resume the lightweight process (LWP) LWPID within the target
117   process PH.  */
118
119ps_err_e
120ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
121{
122  /* Pretend we did successfully continue LWPID.  GDB will take care
123     of it later on.  */
124  return PS_OK;
125}
126
127/* Get the size of the architecture-dependent extra state registers
128   for LWP LWPID within the target process PH and return it in
129   *XREGSIZE.  */
130
131ps_err_e
132ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
133{
134  /* FIXME: Not supported yet.  */
135  return PS_OK;
136}
137
138/* Get the extra state registers of LWP LWPID within the target
139   process PH and store them in XREGSET.  */
140
141ps_err_e
142ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
143{
144  /* FIXME: Not supported yet.  */
145  return PS_OK;
146}
147
148/* Set the extra state registers of LWP LWPID within the target
149   process PH from XREGSET.  */
150
151ps_err_e
152ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
153{
154  /* FIXME: Not supported yet.  */
155  return PS_OK;
156}
157
158/* Log (additional) diognostic information.  */
159
160void
161ps_plog (const char *fmt, ...)
162{
163  va_list args;
164
165  va_start (args, fmt);
166  vfprintf_filtered (gdb_stderr, fmt, args);
167}
168
169/* Search for the symbol named NAME within the object named OBJ within
170   the target process PH.  If the symbol is found the address of the
171   symbol is stored in SYM_ADDR.  */
172
173ps_err_e
174ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *obj,
175		   const char *name, paddr_t *sym_addr)
176{
177  struct minimal_symbol *ms;
178
179  /* FIXME: kettenis/2000-09-03: What should we do with OBJ?  */
180  ms = lookup_minimal_symbol (name, NULL, NULL);
181  if (ms == NULL)
182    return PS_NOSYM;
183
184  *sym_addr = SYMBOL_VALUE_ADDRESS (ms);
185  return PS_OK;
186}
187
188/* Read SIZE bytes from the target process PH at address ADDR and copy
189   them into BUF.  */
190
191ps_err_e
192ps_pdread (gdb_ps_prochandle_t ph, paddr_t addr,
193	   gdb_ps_read_buf_t buf, gdb_ps_size_t size)
194{
195  return ps_xfer_memory (ph, addr, buf, size, 0);
196}
197
198/* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
199
200ps_err_e
201ps_pdwrite (gdb_ps_prochandle_t ph, paddr_t addr,
202	    gdb_ps_write_buf_t buf, gdb_ps_size_t size)
203{
204  return ps_xfer_memory (ph, addr, (char *) buf, size, 1);
205}
206
207/* Read SIZE bytes from the target process PH at address ADDR and copy
208   them into BUF.  */
209
210ps_err_e
211ps_ptread (gdb_ps_prochandle_t ph, paddr_t addr,
212	   gdb_ps_read_buf_t buf, gdb_ps_size_t size)
213{
214  return ps_xfer_memory (ph, addr, buf, size, 0);
215}
216
217/* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
218
219ps_err_e
220ps_ptwrite (gdb_ps_prochandle_t ph, paddr_t addr,
221	    gdb_ps_write_buf_t buf, gdb_ps_size_t size)
222{
223  return ps_xfer_memory (ph, addr, (char *) buf, size, 1);
224}
225
226/* Get the general registers of LWP LWPID within the target process PH
227   and store them in GREGSET.  */
228
229ps_err_e
230ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
231{
232  struct cleanup *old_chain = save_inferior_ptid ();
233
234  inferior_ptid = BUILD_LWP (lwpid, ph->pid);
235
236  target_fetch_registers (-1);
237  fill_gregset ((gdb_gregset_t *) gregset, -1);
238
239  do_cleanups (old_chain);
240  return PS_OK;
241}
242
243/* Set the general registers of LWP LWPID within the target process PH
244   from GREGSET.  */
245
246ps_err_e
247ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, const prgregset_t gregset)
248{
249  struct cleanup *old_chain = save_inferior_ptid ();
250
251  inferior_ptid = BUILD_LWP (lwpid, ph->pid);
252
253  /* FIXME: We should really make supply_gregset const-correct.  */
254  supply_gregset ((gdb_gregset_t *) gregset);
255  target_store_registers (-1);
256
257  do_cleanups (old_chain);
258  return PS_OK;
259}
260
261/* Get the floating-point registers of LWP LWPID within the target
262   process PH and store them in FPREGSET.  */
263
264ps_err_e
265ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
266	       gdb_prfpregset_t *fpregset)
267{
268  struct cleanup *old_chain = save_inferior_ptid ();
269
270  inferior_ptid = BUILD_LWP (lwpid, ph->pid);
271
272  target_fetch_registers (-1);
273  fill_fpregset ((gdb_fpregset_t *) fpregset, -1);
274
275  do_cleanups (old_chain);
276  return PS_OK;
277}
278
279/* Set the floating-point registers of LWP LWPID within the target
280   process PH from FPREGSET.  */
281
282ps_err_e
283ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
284	       const gdb_prfpregset_t *fpregset)
285{
286  struct cleanup *old_chain = save_inferior_ptid ();
287
288  inferior_ptid = BUILD_LWP (lwpid, ph->pid);
289
290  /* FIXME: We should really make supply_fpregset const-correct.  */
291  supply_fpregset ((gdb_fpregset_t *) fpregset);
292  target_store_registers (-1);
293
294  do_cleanups (old_chain);
295  return PS_OK;
296}
297
298/* Return overall process id of the target PH.  Special for GNU/Linux
299   -- not used on Solaris.  */
300
301pid_t
302ps_getpid (gdb_ps_prochandle_t ph)
303{
304  return ph->pid;
305}
306
307void
308_initialize_proc_service (void)
309{
310  /* This function solely exists to make sure this module is linked
311     into the final binary.  */
312}
313