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