6cfe0d5aea533aa79782ad1b46b82a83e5cd81a6
[external/binutils.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 static CORE_ADDR linux_bp_reinsert;
39
40 static void linux_resume (int step, int signal);
41
42 #define PTRACE_ARG3_TYPE long
43 #define PTRACE_XFER_TYPE long
44
45 #ifdef HAVE_LINUX_REGSETS
46 static int use_regsets_p = 1;
47 #endif
48
49 extern int errno;
50
51 static int inferior_pid;
52
53 struct inferior_linux_data
54 {
55   int pid;
56 };
57
58 /* Start an inferior process and returns its pid.
59    ALLARGS is a vector of program-name and args. */
60
61 static int
62 linux_create_inferior (char *program, char **allargs)
63 {
64   struct inferior_linux_data *tdata;
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,
78                strerror (errno));
79       fflush (stderr);
80       _exit (0177);
81     }
82
83   add_inferior (pid);
84   tdata = (struct inferior_linux_data *) malloc (sizeof (*tdata));
85   tdata->pid = pid;
86   set_inferior_target_data (current_inferior, tdata);
87
88   /* FIXME remove */
89   inferior_pid = pid;
90   return 0;
91 }
92
93 /* Attach to an inferior process.  */
94
95 static int
96 linux_attach (int pid)
97 {
98   struct inferior_linux_data *tdata;
99
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
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);
113   return 0;
114 }
115
116 /* Kill the inferior process.  Make us have no inferior.  */
117
118 static void
119 linux_kill (void)
120 {
121   if (inferior_pid == 0)
122     return;
123   ptrace (PTRACE_KILL, inferior_pid, 0, 0);
124   wait (0);
125   clear_inferiors ();
126 }
127
128 /* Return nonzero if the given thread is still alive.  */
129 static int
130 linux_thread_alive (int pid)
131 {
132   return 1;
133 }
134
135 static int
136 linux_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
194 /* Wait for process, returns status */
195
196 static unsigned char
197 linux_wait (char *status)
198 {
199   int w;
200
201   enable_async_io ();
202   w = linux_wait_for_one_inferior (current_inferior);
203   disable_async_io ();
204
205   if (WIFEXITED (w))
206     {
207       fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
208       *status = 'W';
209       clear_inferiors ();
210       return ((unsigned char) WEXITSTATUS (w));
211     }
212   else if (!WIFSTOPPED (w))
213     {
214       fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
215       clear_inferiors ();
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
230 static void
231 linux_resume (int step, int signal)
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
239
240 #ifdef HAVE_LINUX_USRREGS
241
242 #define REGISTER_RAW_SIZE(regno) register_size((regno))
243
244 int
245 register_addr (int regnum)
246 {
247   int addr;
248
249   if (regnum < 0 || regnum >= the_low_target.num_regs)
250     error ("Invalid register number %d.", regnum);
251
252   addr = the_low_target.regmap[regnum];
253   if (addr == -1)
254     addr = 0;
255
256   return addr;
257 }
258
259 /* Fetch one register.  */
260 static void
261 fetch_register (int regno)
262 {
263   CORE_ADDR regaddr;
264   register int i;
265
266   if (regno >= the_low_target.num_regs)
267     return;
268   if ((*the_low_target.cannot_fetch_register) (regno))
269     return;
270
271   regaddr = register_addr (regno);
272   if (regaddr == -1)
273     return;
274   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
275     {
276       errno = 0;
277       *(PTRACE_XFER_TYPE *) (register_data (regno) + i) =
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     }
291 error_exit:;
292 }
293
294 /* Fetch all registers, or just one, from the child process.  */
295 static void
296 usr_fetch_inferior_registers (int regno)
297 {
298   if (regno == -1 || regno == 0)
299     for (regno = 0; regno < the_low_target.num_regs; regno++)
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).  */
308 static void
309 usr_store_inferior_registers (int regno)
310 {
311   CORE_ADDR regaddr;
312   int i;
313
314   if (regno >= 0)
315     {
316       if (regno >= the_low_target.num_regs)
317         return;
318
319       if ((*the_low_target.cannot_store_register) (regno) == 1)
320         return;
321
322       regaddr = register_addr (regno);
323       if (regaddr == -1)
324         return;
325       errno = 0;
326       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
327         {
328           errno = 0;
329           ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
330                   *(int *) (register_data (regno) + i));
331           if (errno != 0)
332             {
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                 }
342             }
343           regaddr += sizeof (int);
344         }
345     }
346   else
347     for (regno = 0; regno < the_low_target.num_regs; regno++)
348       store_inferior_registers (regno);
349 }
350 #endif /* HAVE_LINUX_USRREGS */
351
352
353
354 #ifdef HAVE_LINUX_REGSETS
355
356 static int
357 regsets_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);
375       res = ptrace (regset->get_request, inferior_pid, 0, buf);
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     }
401   return 0;
402 }
403
404 static int
405 regsets_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);
424       res = ptrace (regset->set_request, inferior_pid, 0, buf);
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             {
444               perror ("Warning: ptrace(regsets_store_inferior_registers)");
445             }
446         }
447       regset ++;
448     }
449   return 0;
450 }
451
452 #endif /* HAVE_LINUX_REGSETS */
453
454
455 void
456 linux_fetch_registers (int regno)
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
470 void
471 linux_store_registers (int regno)
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
485
486 /* Copy LEN bytes from inferior's memory starting at MEMADDR
487    to debugger memory starting at MYADDR.  */
488
489 static void
490 linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
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     {
506       buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
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
518 static int
519 linux_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
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
533   buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
534                       (PTRACE_ARG3_TYPE) addr, 0);
535
536   if (count > 1)
537     {
538       buffer[count - 1]
539         = ptrace (PTRACE_PEEKTEXT, inferior_pid,
540                   (PTRACE_ARG3_TYPE) (addr + (count - 1)
541                                       * sizeof (PTRACE_XFER_TYPE)),
542                   0);
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;
554       ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
555       if (errno)
556         return errno;
557     }
558
559   return 0;
560 }
561
562 static void
563 linux_look_up_symbols (void)
564 {
565   /* Don't need to look up any symbols yet.  */
566 }
567
568 \f
569 static 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,
580   linux_look_up_symbols,
581 };
582
583 void
584 initialize_low (void)
585 {
586   set_target_ops (&linux_target_ops);
587   set_breakpoint_data (the_low_target.breakpoint,
588                        the_low_target.breakpoint_len);
589   init_registers ();
590 }