* Check in Fred Fish's changes in these modules. Fred
[platform/upstream/binutils.git] / gdb / arm-xdep.c
1 /* Acorn Risc Machine host machine support.
2    Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "arm-opcode.h"
24
25 #include <stdio.h>
26 #include <sys/param.h>
27 #include <sys/dir.h>
28 #include <signal.h>
29 #include <sys/ioctl.h>
30 #include <sys/ptrace.h>
31 #include <machine/reg.h>
32
33 #define N_TXTADDR(hdr) 0x8000
34 #define N_DATADDR(hdr) (hdr.a_text + 0x8000)
35
36 #include "gdbcore.h"
37
38 #include <sys/user.h>           /* After a.out.h  */
39 #include <sys/file.h>
40 #include <sys/stat.h>
41
42 #include <errno.h>
43
44 void
45 fetch_inferior_registers (regno)
46      int regno;         /* Original value discarded */
47 {
48   register unsigned int regaddr;
49   char buf[MAX_REGISTER_RAW_SIZE];
50   register int i;
51
52   struct user u;
53   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
54   offset = ptrace (PT_READ_U, inferior_pid, offset, 0) - KERNEL_U_ADDR;
55
56   registers_fetched ();
57   
58   for (regno = 0; regno < 16; regno++)
59     {
60       regaddr = offset + regno * 4;
61       *(int *)&buf[0] = ptrace (PT_READ_U, inferior_pid, regaddr, 0);
62       if (regno == PC_REGNUM)
63           *(int *)&buf[0] = GET_PC_PART(*(int *)&buf[0]);
64       supply_register (regno, buf);
65     }
66   *(int *)&buf[0] = ptrace (PT_READ_U, inferior_pid, offset + PC*4);
67   supply_register (PS_REGNUM, buf); /* set virtual register ps same as pc */
68
69   /* read the floating point registers */
70   offset = (char *) &u.u_fp_regs - (char *)&u;
71   *(int *)buf = ptrace (PT_READ_U, inferior_pid, offset, 0);
72   supply_register (FPS_REGNUM, buf);
73   for (regno = 16; regno < 24; regno++) {
74       regaddr = offset + 4 + 12 * (regno - 16);
75       for (i = 0; i < 12; i += sizeof(int))
76           *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, regaddr + i, 0);
77       supply_register (regno, buf);
78   }
79 }
80
81 /* Store our register values back into the inferior.
82    If REGNO is -1, do this for all registers.
83    Otherwise, REGNO specifies which register (so we can save time).  */
84
85 void
86 store_inferior_registers (regno)
87      int regno;
88 {
89   register unsigned int regaddr;
90   char buf[80];
91
92   struct user u;
93   unsigned long value;
94   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
95   offset = ptrace (PT_READ_U, inferior_pid, offset, 0) - KERNEL_U_ADDR;
96
97   if (regno >= 0) {
98       if (regno >= 16) return;
99       regaddr = offset + 4 * regno;
100       errno = 0;
101       value = read_register(regno);
102       if (regno == PC_REGNUM)
103           value = SET_PC_PART(read_register (PS_REGNUM), value);
104       ptrace (PT_WRITE_U, inferior_pid, regaddr, value);
105       if (errno != 0)
106         {
107           sprintf (buf, "writing register number %d", regno);
108           perror_with_name (buf);
109         }
110     }
111   else for (regno = 0; regno < 15; regno++)
112     {
113       regaddr = offset + regno * 4;
114       errno = 0;
115       value = read_register(regno);
116       if (regno == PC_REGNUM)
117           value = SET_PC_PART(read_register (PS_REGNUM), value);
118       ptrace (6, inferior_pid, regaddr, value);
119       if (errno != 0)
120         {
121           sprintf (buf, "writing all regs, number %d", regno);
122           perror_with_name (buf);
123         }
124     }
125 }
126 \f
127 /* Work with core dump and executable files, for GDB. 
128    This code would be in core.c if it weren't machine-dependent. */
129
130 /* Structure to describe the chain of shared libraries used
131    by the execfile.
132    e.g. prog shares Xt which shares X11 which shares c. */
133
134 struct shared_library {
135     struct exec_header header;
136     char name[SHLIBLEN];
137     CORE_ADDR text_start;       /* CORE_ADDR of 1st byte of text, this file */
138     long data_offset;           /* offset of data section in file */
139     int chan;                   /* file descriptor for the file */
140     struct shared_library *shares; /* library this one shares */
141 };
142 static struct shared_library *shlib = 0;
143
144 /* Hook for `exec_file_command' command to call.  */
145
146 extern void (*exec_file_display_hook) ();
147    
148 static CORE_ADDR unshared_text_start;
149
150 /* extended header from exec file (for shared library info) */
151
152 static struct exec_header exec_header;
153 \f
154 void
155 core_file_command (filename, from_tty)
156      char *filename;
157      int from_tty;
158 {
159   int val;
160   extern char registers[];
161
162   /* Discard all vestiges of any previous core file
163      and mark data and stack spaces as empty.  */
164
165   if (corefile)
166     free (corefile);
167   corefile = 0;
168
169   if (corechan >= 0)
170     close (corechan);
171   corechan = -1;
172
173   data_start = 0;
174   data_end = 0;
175   stack_start = STACK_END_ADDR;
176   stack_end = STACK_END_ADDR;
177
178   /* Now, if a new core file was specified, open it and digest it.  */
179
180   if (filename)
181     {
182       filename = tilde_expand (filename);
183       make_cleanup (free, filename);
184       
185       if (have_inferior_p ())
186         error ("To look at a core file, you must kill the inferior with \"kill\".");
187       corechan = open (filename, O_RDONLY, 0);
188       if (corechan < 0)
189         perror_with_name (filename);
190       /* 4.2-style (and perhaps also sysV-style) core dump file.  */
191       {
192         struct user u;
193
194         unsigned int reg_offset, fp_reg_offset;
195
196         val = myread (corechan, &u, sizeof u);
197         if (val < 0)
198           perror_with_name ("Not a core file: reading upage");
199         if (val != sizeof u)
200           error ("Not a core file: could only read %d bytes", val);
201
202         /* We are depending on exec_file_command having been called
203            previously to set exec_data_start.  Since the executable
204            and the core file share the same text segment, the address
205            of the data segment will be the same in both.  */
206         data_start = exec_data_start;
207
208         data_end = data_start + NBPG * u.u_dsize;
209         stack_start = stack_end - NBPG * u.u_ssize;
210         data_offset = NBPG * UPAGES;
211         stack_offset = NBPG * (UPAGES + u.u_dsize);
212
213         /* Some machines put an absolute address in here and some put
214            the offset in the upage of the regs.  */
215         reg_offset = (int) u.u_ar0;
216         if (reg_offset > NBPG * UPAGES)
217           reg_offset -= KERNEL_U_ADDR;
218         fp_reg_offset = (char *) &u.u_fp_regs - (char *)&u;
219
220         /* I don't know where to find this info.
221            So, for now, mark it as not available.  */
222         N_SET_MAGIC (core_aouthdr, 0);
223
224         /* Read the register values out of the core file and store
225            them where `read_register' will find them.  */
226
227         {
228           register int regno;
229
230           for (regno = 0; regno < NUM_REGS; regno++)
231             {
232               char buf[MAX_REGISTER_RAW_SIZE];
233
234               if (regno < 16)
235                   val = lseek (corechan, reg_offset + 4 * regno, 0);
236               else if (regno < 24)
237                   val = lseek (corechan, fp_reg_offset + 4 + 12*(regno - 24), 0);
238               else if (regno == 24)
239                   val = lseek (corechan, fp_reg_offset, 0);
240               else if (regno == 25)
241                   val = lseek (corechan, reg_offset + 4 * PC, 0);
242               if (val < 0
243                   || (val = myread (corechan, buf, sizeof buf)) < 0)
244                 {
245                   char * buffer = (char *) alloca (strlen (reg_names[regno])
246                                                    + 30);
247                   strcpy (buffer, "Reading register ");
248                   strcat (buffer, reg_names[regno]);
249                                                    
250                   perror_with_name (buffer);
251                 }
252
253               if (regno == PC_REGNUM)
254                   *(int *)buf = GET_PC_PART(*(int *)buf);
255               supply_register (regno, buf);
256             }
257         }
258       }
259       if (filename[0] == '/')
260         corefile = savestring (filename, strlen (filename));
261       else
262         {
263           corefile = concat (current_directory, "/", filename, NULL);
264         }
265
266       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
267                                             read_pc ()));
268       select_frame (get_current_frame (), 0);
269       validate_files ();
270     }
271   else if (from_tty)
272     printf ("No core file now.\n");
273 }