]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/linux-low.c
2002-04-11 Daniel Jacobowitz <drow@mvista.com>
[thirdparty/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) == 1)
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 if ((*the_low_target.cannot_store_register) (regno) == 0)
258 {
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 }
267 regaddr += sizeof (int);
268 }
269 }
270 else
271 for (regno = 0; regno < the_low_target.num_regs; regno++)
272 store_inferior_registers (regno);
273 }
274 #endif /* HAVE_LINUX_USRREGS */
275
276
277
278 #ifdef HAVE_LINUX_REGSETS
279
280 static int
281 regsets_fetch_inferior_registers (void)
282 {
283 struct regset_info *regset;
284
285 regset = target_regsets;
286
287 while (regset->size >= 0)
288 {
289 void *buf;
290 int res;
291
292 if (regset->size == 0)
293 {
294 regset ++;
295 continue;
296 }
297
298 buf = malloc (regset->size);
299 res = ptrace (regset->get_request, inferior_pid, 0, (int) buf);
300 if (res < 0)
301 {
302 if (errno == EIO)
303 {
304 /* If we get EIO on the first regset, do not try regsets again.
305 If we get EIO on a later regset, disable that regset. */
306 if (regset == target_regsets)
307 {
308 use_regsets_p = 0;
309 return -1;
310 }
311 else
312 {
313 regset->size = 0;
314 continue;
315 }
316 }
317 else
318 {
319 perror ("Warning: ptrace(regsets_fetch_inferior_registers)");
320 }
321 }
322 regset->store_function (buf);
323 regset ++;
324 }
325 return 0;
326 }
327
328 static int
329 regsets_store_inferior_registers (void)
330 {
331 struct regset_info *regset;
332
333 regset = target_regsets;
334
335 while (regset->size >= 0)
336 {
337 void *buf;
338 int res;
339
340 if (regset->size == 0)
341 {
342 regset ++;
343 continue;
344 }
345
346 buf = malloc (regset->size);
347 regset->fill_function (buf);
348 res = ptrace (regset->set_request, inferior_pid, 0, (int) buf);
349 if (res < 0)
350 {
351 if (errno == EIO)
352 {
353 /* If we get EIO on the first regset, do not try regsets again.
354 If we get EIO on a later regset, disable that regset. */
355 if (regset == target_regsets)
356 {
357 use_regsets_p = 0;
358 return -1;
359 }
360 else
361 {
362 regset->size = 0;
363 continue;
364 }
365 }
366 else
367 {
368 perror ("Warning: ptrace(regsets_store_inferior_registers)");
369 }
370 }
371 regset ++;
372 }
373 return 0;
374 }
375
376 #endif /* HAVE_LINUX_REGSETS */
377
378
379 void
380 linux_fetch_registers (int regno)
381 {
382 #ifdef HAVE_LINUX_REGSETS
383 if (use_regsets_p)
384 {
385 if (regsets_fetch_inferior_registers () == 0)
386 return;
387 }
388 #endif
389 #ifdef HAVE_LINUX_USRREGS
390 usr_fetch_inferior_registers (regno);
391 #endif
392 }
393
394 void
395 linux_store_registers (int regno)
396 {
397 #ifdef HAVE_LINUX_REGSETS
398 if (use_regsets_p)
399 {
400 if (regsets_store_inferior_registers () == 0)
401 return;
402 }
403 #endif
404 #ifdef HAVE_LINUX_USRREGS
405 usr_store_inferior_registers (regno);
406 #endif
407 }
408
409
410 /* Copy LEN bytes from inferior's memory starting at MEMADDR
411 to debugger memory starting at MYADDR. */
412
413 static void
414 linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
415 {
416 register int i;
417 /* Round starting address down to longword boundary. */
418 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
419 /* Round ending address up; get number of longwords that makes. */
420 register int count
421 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
422 / sizeof (PTRACE_XFER_TYPE);
423 /* Allocate buffer of that many longwords. */
424 register PTRACE_XFER_TYPE *buffer
425 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
426
427 /* Read all the longwords */
428 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
429 {
430 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
431 }
432
433 /* Copy appropriate bytes out of the buffer. */
434 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
435 }
436
437 /* Copy LEN bytes of data from debugger memory at MYADDR
438 to inferior's memory at MEMADDR.
439 On failure (cannot write the inferior)
440 returns the value of errno. */
441
442 static int
443 linux_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
444 {
445 register int i;
446 /* Round starting address down to longword boundary. */
447 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
448 /* Round ending address up; get number of longwords that makes. */
449 register int count
450 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
451 /* Allocate buffer of that many longwords. */
452 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
453 extern int errno;
454
455 /* Fill start and end extra bytes of buffer with existing memory data. */
456
457 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
458 (PTRACE_ARG3_TYPE) addr, 0);
459
460 if (count > 1)
461 {
462 buffer[count - 1]
463 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
464 (PTRACE_ARG3_TYPE) (addr + (count - 1)
465 * sizeof (PTRACE_XFER_TYPE)),
466 0);
467 }
468
469 /* Copy data to be written over corresponding part of buffer */
470
471 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
472
473 /* Write the entire buffer. */
474
475 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
476 {
477 errno = 0;
478 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
479 if (errno)
480 return errno;
481 }
482
483 return 0;
484 }
485
486 static void
487 linux_look_up_symbols (void)
488 {
489 /* Don't need to look up any symbols yet. */
490 }
491
492 \f
493 static struct target_ops linux_target_ops = {
494 linux_create_inferior,
495 linux_attach,
496 linux_kill,
497 linux_thread_alive,
498 linux_resume,
499 linux_wait,
500 linux_fetch_registers,
501 linux_store_registers,
502 linux_read_memory,
503 linux_write_memory,
504 linux_look_up_symbols,
505 };
506
507 void
508 initialize_low (void)
509 {
510 set_target_ops (&linux_target_ops);
511 init_registers ();
512 }