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