2002-02-27 Daniel Jacobowitz <drow@mvista.com>
[binutils-gdb.git] / gdb / gdbserver / linux-low.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 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 "server.h"
23 #include "linux-low.h"
24
25 #include <sys/wait.h>
26 #include <stdio.h>
27 #include <sys/param.h>
28 #include <sys/dir.h>
29 #include <sys/ptrace.h>
30 #include <sys/user.h>
31 #include <signal.h>
32 #include <sys/ioctl.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 #define PTRACE_ARG3_TYPE long
39 #define PTRACE_XFER_TYPE int
40
41 #ifdef HAVE_LINUX_REGSETS
42 static int use_regsets_p = 1;
43 #endif
44
45 extern int errno;
46 extern int num_regs;
47 extern int regmap[];
48
49 /* Start an inferior process and returns its pid.
50 ALLARGS is a vector of program-name and args. */
51
52 int
53 create_inferior (char *program, char **allargs)
54 {
55 int pid;
56
57 pid = fork ();
58 if (pid < 0)
59 perror_with_name ("fork");
60
61 if (pid == 0)
62 {
63 ptrace (PTRACE_TRACEME, 0, 0, 0);
64
65 execv (program, allargs);
66
67 fprintf (stderr, "Cannot exec %s: %s.\n", program,
68 strerror (errno));
69 fflush (stderr);
70 _exit (0177);
71 }
72
73 return pid;
74 }
75
76 /* Attach to an inferior process. */
77
78 int
79 myattach (int pid)
80 {
81 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
82 {
83 fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
84 errno < sys_nerr ? sys_errlist[errno] : "unknown error",
85 errno);
86 fflush (stderr);
87 _exit (0177);
88 }
89
90 return 0;
91 }
92
93 /* Kill the inferior process. Make us have no inferior. */
94
95 void
96 kill_inferior (void)
97 {
98 if (inferior_pid == 0)
99 return;
100 ptrace (PTRACE_KILL, inferior_pid, 0, 0);
101 wait (0);
102 }
103
104 /* Return nonzero if the given thread is still alive. */
105 int
106 mythread_alive (int pid)
107 {
108 return 1;
109 }
110
111 /* Wait for process, returns status */
112
113 unsigned char
114 mywait (char *status)
115 {
116 int pid;
117 int w;
118
119 enable_async_io ();
120 pid = waitpid (inferior_pid, &w, 0);
121 disable_async_io ();
122 if (pid != inferior_pid)
123 perror_with_name ("wait");
124
125 if (WIFEXITED (w))
126 {
127 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
128 *status = 'W';
129 return ((unsigned char) WEXITSTATUS (w));
130 }
131 else if (!WIFSTOPPED (w))
132 {
133 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
134 *status = 'X';
135 return ((unsigned char) WTERMSIG (w));
136 }
137
138 fetch_inferior_registers (0);
139
140 *status = 'T';
141 return ((unsigned char) WSTOPSIG (w));
142 }
143
144 /* Resume execution of the inferior process.
145 If STEP is nonzero, single-step it.
146 If SIGNAL is nonzero, give it that signal. */
147
148 void
149 myresume (int step, int signal)
150 {
151 errno = 0;
152 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
153 if (errno)
154 perror_with_name ("ptrace");
155 }
156
157 #define REGISTER_RAW_SIZE(regno) register_size((regno))
158
159 int
160 register_addr (int regnum)
161 {
162 int addr;
163
164 if (regnum < 0 || regnum >= num_regs)
165 error ("Invalid register number %d.", regnum);
166
167 addr = regmap[regnum];
168 if (addr == -1)
169 addr = 0;
170
171 return addr;
172 }
173
174
175
176 #ifdef HAVE_LINUX_USRREGS
177
178 /* Fetch one register. */
179 static void
180 fetch_register (int regno)
181 {
182 CORE_ADDR regaddr;
183 register int i;
184
185 if (regno >= num_regs)
186 return;
187 if (cannot_fetch_register (regno))
188 return;
189
190 regaddr = register_addr (regno);
191 if (regaddr == -1)
192 return;
193 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
194 {
195 errno = 0;
196 *(PTRACE_XFER_TYPE *) (register_data (regno) + i) =
197 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
198 regaddr += sizeof (PTRACE_XFER_TYPE);
199 if (errno != 0)
200 {
201 /* Warning, not error, in case we are attached; sometimes the
202 kernel doesn't let us at the registers. */
203 char *err = strerror (errno);
204 char *msg = alloca (strlen (err) + 128);
205 sprintf (msg, "reading register %d: %s", regno, err);
206 error (msg);
207 goto error_exit;
208 }
209 }
210 error_exit:;
211 }
212
213 /* Fetch all registers, or just one, from the child process. */
214 static void
215 usr_fetch_inferior_registers (int regno)
216 {
217 if (regno == -1 || regno == 0)
218 for (regno = 0; regno < num_regs; regno++)
219 fetch_register (regno);
220 else
221 fetch_register (regno);
222 }
223
224 /* Store our register values back into the inferior.
225 If REGNO is -1, do this for all registers.
226 Otherwise, REGNO specifies which register (so we can save time). */
227 static void
228 usr_store_inferior_registers (int regno)
229 {
230 CORE_ADDR regaddr;
231 int i;
232
233 if (regno >= 0)
234 {
235 if (regno >= num_regs)
236 return;
237
238 if (cannot_store_register (regno))
239 return;
240
241 regaddr = register_addr (regno);
242 if (regaddr == -1)
243 return;
244 errno = 0;
245 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
246 {
247 errno = 0;
248 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
249 *(int *) (register_data (regno) + i));
250 if (errno != 0)
251 {
252 /* Warning, not error, in case we are attached; sometimes the
253 kernel doesn't let us at the registers. */
254 char *err = strerror (errno);
255 char *msg = alloca (strlen (err) + 128);
256 sprintf (msg, "writing register %d: %s",
257 regno, err);
258 error (msg);
259 return;
260 }
261 regaddr += sizeof (int);
262 }
263 }
264 else
265 for (regno = 0; regno < num_regs; regno++)
266 store_inferior_registers (regno);
267 }
268 #endif /* HAVE_LINUX_USRREGS */
269
270
271
272 #ifdef HAVE_LINUX_REGSETS
273
274 static int
275 regsets_fetch_inferior_registers (void)
276 {
277 struct regset_info *regset;
278
279 regset = target_regsets;
280
281 while (regset->size >= 0)
282 {
283 void *buf;
284 int res;
285
286 if (regset->size == 0)
287 {
288 regset ++;
289 continue;
290 }
291
292 buf = malloc (regset->size);
293 res = ptrace (regset->get_request, inferior_pid, 0, (int) buf);
294 if (res < 0)
295 {
296 if (errno == EIO)
297 {
298 /* If we get EIO on the first regset, do not try regsets again.
299 If we get EIO on a later regset, disable that regset. */
300 if (regset == target_regsets)
301 {
302 use_regsets_p = 0;
303 return -1;
304 }
305 else
306 {
307 regset->size = 0;
308 continue;
309 }
310 }
311 else
312 {
313 perror ("Warning: ptrace(regsets_fetch_inferior_registers)");
314 }
315 }
316 regset->store_function (buf);
317 regset ++;
318 }
319 }
320
321 static int
322 regsets_store_inferior_registers (void)
323 {
324 struct regset_info *regset;
325
326 regset = target_regsets;
327
328 while (regset->size >= 0)
329 {
330 void *buf;
331 int res;
332
333 if (regset->size == 0)
334 {
335 regset ++;
336 continue;
337 }
338
339 buf = malloc (regset->size);
340 regset->fill_function (buf);
341 res = ptrace (regset->set_request, inferior_pid, 0, (int) buf);
342 if (res < 0)
343 {
344 if (errno == EIO)
345 {
346 /* If we get EIO on the first regset, do not try regsets again.
347 If we get EIO on a later regset, disable that regset. */
348 if (regset == target_regsets)
349 {
350 use_regsets_p = 0;
351 return -1;
352 }
353 else
354 {
355 regset->size = 0;
356 continue;
357 }
358 }
359 else
360 {
361 perror ("Warning: ptrace(regsets_fetch_inferior_registers)");
362 }
363 }
364 regset ++;
365 }
366 }
367
368 #endif /* HAVE_LINUX_REGSETS */
369
370
371 void
372 fetch_inferior_registers (int regno)
373 {
374 #ifdef HAVE_LINUX_REGSETS
375 if (use_regsets_p)
376 {
377 if (regsets_fetch_inferior_registers () == 0)
378 return;
379 }
380 #endif
381 #ifdef HAVE_LINUX_USRREGS
382 usr_fetch_inferior_registers (regno);
383 #endif
384 }
385
386 void
387 store_inferior_registers (int regno)
388 {
389 #ifdef HAVE_LINUX_REGSETS
390 if (use_regsets_p)
391 {
392 if (regsets_store_inferior_registers () == 0)
393 return;
394 }
395 #endif
396 #ifdef HAVE_LINUX_USRREGS
397 usr_store_inferior_registers (regno);
398 #endif
399 }
400
401
402 /* Copy LEN bytes from inferior's memory starting at MEMADDR
403 to debugger memory starting at MYADDR. */
404
405 void
406 read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
407 {
408 register int i;
409 /* Round starting address down to longword boundary. */
410 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
411 /* Round ending address up; get number of longwords that makes. */
412 register int count
413 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
414 / sizeof (PTRACE_XFER_TYPE);
415 /* Allocate buffer of that many longwords. */
416 register PTRACE_XFER_TYPE *buffer
417 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
418
419 /* Read all the longwords */
420 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
421 {
422 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
423 }
424
425 /* Copy appropriate bytes out of the buffer. */
426 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
427 }
428
429 /* Copy LEN bytes of data from debugger memory at MYADDR
430 to inferior's memory at MEMADDR.
431 On failure (cannot write the inferior)
432 returns the value of errno. */
433
434 int
435 write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
436 {
437 register int i;
438 /* Round starting address down to longword boundary. */
439 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
440 /* Round ending address up; get number of longwords that makes. */
441 register int count
442 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
443 /* Allocate buffer of that many longwords. */
444 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
445 extern int errno;
446
447 /* Fill start and end extra bytes of buffer with existing memory data. */
448
449 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
450 (PTRACE_ARG3_TYPE) addr, 0);
451
452 if (count > 1)
453 {
454 buffer[count - 1]
455 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
456 (PTRACE_ARG3_TYPE) (addr + (count - 1)
457 * sizeof (PTRACE_XFER_TYPE)),
458 0);
459 }
460
461 /* Copy data to be written over corresponding part of buffer */
462
463 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
464
465 /* Write the entire buffer. */
466
467 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
468 {
469 errno = 0;
470 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
471 if (errno)
472 return errno;
473 }
474
475 return 0;
476 }
477 \f
478 void
479 initialize_low (void)
480 {
481 init_registers ();
482 }