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