* config/i386/nm-linux.h (PREPARE_TO_PROCEED, ATTCH_LWP,
[external/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 #ifndef MERGEPID
55 #define MERGEPID(PID, TID)      (((PID) & 0xffff) | ((TID) << 16))
56 #endif
57
58 #define BUILD_LWP(tid, pid)     MERGEPID (pid, tid)
59 \f
60
61 /* Helper functions.  */
62
63 static void
64 restore_inferior_pid (void *arg)
65 {
66   int *saved_pid_ptr = arg;
67   inferior_pid = *saved_pid_ptr;
68   free (arg);
69 }
70
71 static struct cleanup *
72 save_inferior_pid (void)
73 {
74   int *saved_pid_ptr;
75
76   saved_pid_ptr = xmalloc (sizeof (int));
77   *saved_pid_ptr = inferior_pid;
78   return make_cleanup (restore_inferior_pid, saved_pid_ptr);
79 }
80
81 /* Transfer LEN bytes of memory between BUF and address ADDR in the
82    process specified by PH.  If WRITE, transfer them to the process,
83    else transfer them from the process.  Returns PS_OK for success,
84    PS_ERR on failure.
85
86    This is a helper function for ps_pdread, ps_pdwrite, ps_ptread and
87    ps_ptwrite.  */
88
89 static ps_err_e
90 ps_xfer_memory (const struct ps_prochandle *ph, paddr_t addr,
91                 char *buf, size_t len, int write)
92 {
93   struct cleanup *old_chain = save_inferior_pid ();
94   int ret;
95
96   inferior_pid = ph->pid;
97
98   if (write)
99     ret = target_write_memory (addr, buf, len);
100   else
101     ret = target_read_memory (addr, buf, len);
102
103   do_cleanups (old_chain);
104
105   return (ret == 0 ? PS_OK : PS_ERR);
106 }
107 \f
108
109 /* Stop the target process PH.  */
110
111 ps_err_e
112 ps_pstop (gdb_ps_prochandle_t ph)
113 {
114   /* The process is always stopped when under control of GDB.  */
115   return PS_OK;
116 }
117
118 /* Resume the target process PH.  */
119
120 ps_err_e
121 ps_pcontinue (gdb_ps_prochandle_t ph)
122 {
123   /* Pretend we did successfully continue the process.  GDB will take
124      care of it later on.  */
125   return PS_OK;
126 }
127
128 /* Stop the lightweight process LWPID within the target process PH.  */
129
130 ps_err_e
131 ps_lstop (gdb_ps_prochandle_t ph, lwpid_t lwpid)
132 {
133   /* All lightweight processes are stopped when under control of GDB.  */
134   return PS_OK;
135 }
136
137 /* Resume the lightweight process (LWP) LWPID within the target
138    process PH.  */
139
140 ps_err_e
141 ps_lcontinue (gdb_ps_prochandle_t ph, lwpid_t lwpid)
142 {
143   /* Pretend we did successfully continue LWPID.  GDB will take care
144      of it later on.  */
145   return PS_OK;
146 }
147
148 /* Get the size of the architecture-dependent extra state registers
149    for LWP LWPID within the target process PH and return it in
150    *XREGSIZE.  */
151
152 ps_err_e
153 ps_lgetxregsize (gdb_ps_prochandle_t ph, lwpid_t lwpid, int *xregsize)
154 {
155   /* FIXME: Not supported yet.  */
156   return PS_OK;
157 }
158
159 /* Get the extra state registers of LWP LWPID within the target
160    process PH and store them in XREGSET.  */
161
162 ps_err_e
163 ps_lgetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
164 {
165   /* FIXME: Not supported yet.  */
166   return PS_OK;
167 }
168
169 /* Set the extra state registers of LWP LWPID within the target
170    process PH from XREGSET.  */
171
172 ps_err_e
173 ps_lsetxregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, caddr_t xregset)
174 {
175   /* FIXME: Not supported yet.  */
176   return PS_OK;
177 }
178
179 /* Log (additional) diognostic information.  */
180
181 void
182 ps_plog (const char *fmt, ...)
183 {
184   va_list args;
185
186   va_start (args, fmt);
187   vfprintf_filtered (gdb_stderr, fmt, args);
188 }
189
190 /* Search for the symbol named NAME within the object named OBJ within
191    the target process PH.  If the symbol is found the address of the
192    symbol is stored in SYM_ADDR.  */
193
194 ps_err_e
195 ps_pglobal_lookup (gdb_ps_prochandle_t ph, const char *obj,
196                    const char *name, paddr_t *sym_addr)
197 {
198   struct minimal_symbol *ms;
199
200   /* FIXME: kettenis/2000-09-03: What should we do with OBJ?  */
201   ms = lookup_minimal_symbol (name, NULL, NULL);
202   if (ms == NULL)
203     return PS_NOSYM;
204
205   *sym_addr = SYMBOL_VALUE_ADDRESS (ms);
206   return PS_OK;
207 }
208
209 /* Read SIZE bytes from the target process PH at address ADDR and copy
210    them into BUF.  */
211
212 ps_err_e
213 ps_pdread (gdb_ps_prochandle_t ph, paddr_t addr,
214            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
215 {
216   return ps_xfer_memory (ph, addr, buf, size, 0);
217 }
218
219 /* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
220
221 ps_err_e
222 ps_pdwrite (gdb_ps_prochandle_t ph, paddr_t addr,
223             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
224 {
225   return ps_xfer_memory (ph, addr, (char *) buf, size, 1);
226 }
227
228 /* Read SIZE bytes from the target process PH at address ADDR and copy
229    them into BUF.  */
230
231 ps_err_e
232 ps_ptread (gdb_ps_prochandle_t ph, paddr_t addr,
233            gdb_ps_read_buf_t buf, gdb_ps_size_t size)
234 {
235   return ps_xfer_memory (ph, addr, buf, size, 0);
236 }
237
238 /* Write SIZE bytes from BUF into the target process PH at address ADDR.  */
239
240 ps_err_e
241 ps_ptwrite (gdb_ps_prochandle_t ph, paddr_t addr,
242             gdb_ps_write_buf_t buf, gdb_ps_size_t size)
243 {
244   return ps_xfer_memory (ph, addr, (char *) buf, size, 1);
245 }
246
247 /* Get the general registers of LWP LWPID within the target process PH
248    and store them in GREGSET.  */
249
250 ps_err_e
251 ps_lgetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, prgregset_t gregset)
252 {
253   struct cleanup *old_chain = save_inferior_pid ();
254
255   inferior_pid = BUILD_LWP (lwpid, ph->pid);
256
257   target_fetch_registers (-1);
258   fill_gregset ((gdb_gregset_t *) gregset, -1);
259
260   do_cleanups (old_chain);
261   return PS_OK;
262 }
263
264 /* Set the general registers of LWP LWPID within the target process PH
265    from GREGSET.  */
266
267 ps_err_e
268 ps_lsetregs (gdb_ps_prochandle_t ph, lwpid_t lwpid, const prgregset_t gregset)
269 {
270   struct cleanup *old_chain = save_inferior_pid ();
271
272   inferior_pid = BUILD_LWP (lwpid, ph->pid);
273
274   /* FIXME: We should really make supply_gregset const-correct.  */
275   supply_gregset ((gdb_gregset_t *) gregset);
276   target_store_registers (-1);
277
278   do_cleanups (old_chain);
279   return PS_OK;
280 }
281
282 /* Get the floating-point registers of LWP LWPID within the target
283    process PH and store them in FPREGSET.  */
284
285 ps_err_e
286 ps_lgetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
287                gdb_prfpregset_t *fpregset)
288 {
289   struct cleanup *old_chain = save_inferior_pid ();
290
291   inferior_pid = BUILD_LWP (lwpid, ph->pid);
292
293   target_fetch_registers (-1);
294   fill_fpregset ((gdb_fpregset_t *) fpregset, -1);
295
296   do_cleanups (old_chain);
297   return PS_OK;
298 }
299
300 /* Set the floating-point registers of LWP LWPID within the target
301    process PH from FPREGSET.  */
302
303 ps_err_e
304 ps_lsetfpregs (gdb_ps_prochandle_t ph, lwpid_t lwpid,
305                const gdb_prfpregset_t *fpregset)
306 {
307   struct cleanup *old_chain = save_inferior_pid ();
308
309   inferior_pid = BUILD_LWP (lwpid, ph->pid);
310
311   /* FIXME: We should really make supply_fpregset const-correct.  */
312   supply_fpregset ((gdb_fpregset_t *) fpregset);
313   target_store_registers (-1);
314
315   do_cleanups (old_chain);
316   return PS_OK;
317 }
318
319 /* Return overall process id of the target PH.
320    Special for Linux -- not used on Solaris.  */
321
322 pid_t
323 ps_getpid (gdb_ps_prochandle_t ph)
324 {
325   return ph->pid;
326 }
327
328 void
329 _initialize_proc_service (void)
330 {
331   /* This function solely exists to make sure this module is linked
332      into the final binary.  */
333 }