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