1 /* Acorn Risc Machine host machine support.
2 Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "arm-opcode.h"
26 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/ptrace.h>
31 #include <machine/reg.h>
33 #define N_TXTADDR(hdr) 0x8000
34 #define N_DATADDR(hdr) (hdr.a_text + 0x8000)
38 #include <sys/user.h> /* After a.out.h */
45 fetch_inferior_registers (regno
)
46 int regno
; /* Original value discarded */
48 register unsigned int regaddr
;
49 char buf
[MAX_REGISTER_RAW_SIZE
];
53 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
54 offset
= ptrace (PT_READ_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) offset
, 0)
59 for (regno
= 0; regno
< 16; regno
++)
61 regaddr
= offset
+ regno
* 4;
62 *(int *) &buf
[0] = ptrace (PT_READ_U
, inferior_pid
,
63 (PTRACE_ARG3_TYPE
) regaddr
, 0);
64 if (regno
== PC_REGNUM
)
65 *(int *) &buf
[0] = GET_PC_PART (*(int *) &buf
[0]);
66 supply_register (regno
, buf
);
68 *(int *) &buf
[0] = ptrace (PT_READ_U
, inferior_pid
,
69 (PTRACE_ARG3_TYPE
) (offset
+ PC
* 4), 0);
70 supply_register (PS_REGNUM
, buf
); /* set virtual register ps same as pc */
72 /* read the floating point registers */
73 offset
= (char *) &u
.u_fp_regs
- (char *) &u
;
74 *(int *) buf
= ptrace (PT_READ_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) offset
, 0);
75 supply_register (FPS_REGNUM
, buf
);
76 for (regno
= 16; regno
< 24; regno
++)
78 regaddr
= offset
+ 4 + 12 * (regno
- 16);
79 for (i
= 0; i
< 12; i
+= sizeof (int))
80 *(int *) &buf
[i
] = ptrace (PT_READ_U
, inferior_pid
,
81 (PTRACE_ARG3_TYPE
) (regaddr
+ i
), 0);
82 supply_register (regno
, buf
);
86 /* Store our register values back into the inferior.
87 If REGNO is -1, do this for all registers.
88 Otherwise, REGNO specifies which register (so we can save time). */
91 store_inferior_registers (regno
)
94 register unsigned int regaddr
;
99 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
100 offset
= ptrace (PT_READ_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) offset
, 0)
107 regaddr
= offset
+ 4 * regno
;
109 value
= read_register (regno
);
110 if (regno
== PC_REGNUM
)
111 value
= SET_PC_PART (read_register (PS_REGNUM
), value
);
112 ptrace (PT_WRITE_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, value
);
115 sprintf (buf
, "writing register number %d", regno
);
116 perror_with_name (buf
);
120 for (regno
= 0; regno
< 15; regno
++)
122 regaddr
= offset
+ regno
* 4;
124 value
= read_register (regno
);
125 if (regno
== PC_REGNUM
)
126 value
= SET_PC_PART (read_register (PS_REGNUM
), value
);
127 ptrace (6, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, value
);
130 sprintf (buf
, "writing all regs, number %d", regno
);
131 perror_with_name (buf
);
136 /* Work with core dump and executable files, for GDB.
137 This code would be in corefile.c if it weren't machine-dependent. */
139 /* Structure to describe the chain of shared libraries used
141 e.g. prog shares Xt which shares X11 which shares c. */
143 struct shared_library
145 struct exec_header header
;
147 CORE_ADDR text_start
; /* CORE_ADDR of 1st byte of text, this file */
148 long data_offset
; /* offset of data section in file */
149 int chan
; /* file descriptor for the file */
150 struct shared_library
*shares
; /* library this one shares */
152 static struct shared_library
*shlib
= 0;
154 /* Hook for `exec_file_command' command to call. */
156 extern void (*exec_file_display_hook
) ();
158 static CORE_ADDR unshared_text_start
;
160 /* extended header from exec file (for shared library info) */
162 static struct exec_header exec_header
;
165 core_file_command (filename
, from_tty
)
171 /* Discard all vestiges of any previous core file
172 and mark data and stack spaces as empty. */
184 stack_start
= STACK_END_ADDR
;
185 stack_end
= STACK_END_ADDR
;
187 /* Now, if a new core file was specified, open it and digest it. */
191 filename
= tilde_expand (filename
);
192 make_cleanup (free
, filename
);
194 if (have_inferior_p ())
195 error ("To look at a core file, you must kill the program with \"kill\".");
196 corechan
= open (filename
, O_RDONLY
, 0);
198 perror_with_name (filename
);
199 /* 4.2-style (and perhaps also sysV-style) core dump file. */
203 unsigned int reg_offset
, fp_reg_offset
;
205 val
= myread (corechan
, &u
, sizeof u
);
207 perror_with_name ("Not a core file: reading upage");
209 error ("Not a core file: could only read %d bytes", val
);
211 /* We are depending on exec_file_command having been called
212 previously to set exec_data_start. Since the executable
213 and the core file share the same text segment, the address
214 of the data segment will be the same in both. */
215 data_start
= exec_data_start
;
217 data_end
= data_start
+ NBPG
* u
.u_dsize
;
218 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
219 data_offset
= NBPG
* UPAGES
;
220 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
222 /* Some machines put an absolute address in here and some put
223 the offset in the upage of the regs. */
224 reg_offset
= (int) u
.u_ar0
;
225 if (reg_offset
> NBPG
* UPAGES
)
226 reg_offset
-= KERNEL_U_ADDR
;
227 fp_reg_offset
= (char *) &u
.u_fp_regs
- (char *) &u
;
229 /* I don't know where to find this info.
230 So, for now, mark it as not available. */
231 N_SET_MAGIC (core_aouthdr
, 0);
233 /* Read the register values out of the core file and store
234 them where `read_register' will find them. */
239 for (regno
= 0; regno
< NUM_REGS
; regno
++)
241 char buf
[MAX_REGISTER_RAW_SIZE
];
244 val
= lseek (corechan
, reg_offset
+ 4 * regno
, 0);
246 val
= lseek (corechan
, fp_reg_offset
+ 4 + 12 * (regno
- 24), 0);
247 else if (regno
== 24)
248 val
= lseek (corechan
, fp_reg_offset
, 0);
249 else if (regno
== 25)
250 val
= lseek (corechan
, reg_offset
+ 4 * PC
, 0);
252 || (val
= myread (corechan
, buf
, sizeof buf
)) < 0)
254 char *buffer
= (char *) alloca (strlen (REGISTER_NAME (regno
))
256 strcpy (buffer
, "Reading register ");
257 strcat (buffer
, REGISTER_NAME (regno
));
259 perror_with_name (buffer
);
262 if (regno
== PC_REGNUM
)
263 *(int *) buf
= GET_PC_PART (*(int *) buf
);
264 supply_register (regno
, buf
);
268 if (filename
[0] == '/')
269 corefile
= savestring (filename
, strlen (filename
));
272 corefile
= concat (current_directory
, "/", filename
, NULL
);
275 flush_cached_frames ();
276 select_frame (get_current_frame (), 0);
280 printf ("No core file now.\n");
284 /* Work with core dump and executable files, for GDB.
285 This code would be in corefile.c if it weren't machine-dependent. */
287 /* Structure to describe the chain of shared libraries used
289 e.g. prog shares Xt which shares X11 which shares c. */
291 struct shared_library
293 struct exec_header header
;
295 CORE_ADDR text_start
; /* CORE_ADDR of 1st byte of text, this file */
296 long data_offset
; /* offset of data section in file */
297 int chan
; /* file descriptor for the file */
298 struct shared_library
*shares
; /* library this one shares */
300 static struct shared_library
*shlib
= 0;
302 /* Hook for `exec_file_command' command to call. */
304 extern void (*exec_file_display_hook
) ();
306 static CORE_ADDR unshared_text_start
;
308 /* extended header from exec file (for shared library info) */
310 static struct exec_header exec_header
;
313 exec_file_command (filename
, from_tty
)
319 /* Eliminate all traces of old exec file.
320 Mark text segment as empty. */
326 data_end
-= exec_data_start
;
328 unshared_text_start
= 0;
337 close_shared_library (shlib
);
341 /* Now open and digest the file the user requested, if any. */
345 filename
= tilde_expand (filename
);
346 make_cleanup (free
, filename
);
348 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
351 perror_with_name (filename
);
356 #ifdef HEADER_SEEK_FD
357 HEADER_SEEK_FD (execchan
);
360 val
= myread (execchan
, &exec_header
, sizeof exec_header
);
361 exec_aouthdr
= exec_header
.a_exec
;
364 perror_with_name (filename
);
368 /* Look for shared library if needed */
369 if (exec_header
.a_exec
.a_magic
& MF_USES_SL
)
370 shlib
= open_shared_library (exec_header
.a_shlibname
, text_start
);
372 text_offset
= N_TXTOFF (exec_aouthdr
);
373 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
377 unshared_text_start
= shared_text_end (shlib
) & ~0x7fff;
378 stack_start
= shlib
->header
.a_exec
.a_sldatabase
;
379 stack_end
= STACK_END_ADDR
;
382 unshared_text_start
= 0x8000;
383 text_end
= unshared_text_start
+ exec_aouthdr
.a_text
;
385 exec_data_start
= unshared_text_start
+ exec_aouthdr
.a_text
;
386 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
388 data_start
= exec_data_start
;
389 data_end
+= exec_data_start
;
391 fstat (execchan
, &st_exec
);
392 exec_mtime
= st_exec
.st_mtime
;
398 printf ("No executable file now.\n");
400 /* Tell display code (if any) about the changed file name. */
401 if (exec_file_display_hook
)
402 (*exec_file_display_hook
) (filename
);
407 /* Read from the program's memory (except for inferior processes).
408 This function is misnamed, since it only reads, never writes; and
409 since it will use the core file and/or executable file as necessary.
411 It should be extended to write as well as read, FIXME, for patching files.
413 Return 0 if address could be read, EIO if addresss out of bounds. */
416 xfer_core_file (memaddr
, myaddr
, len
)
433 /* Determine which file the next bunch of addresses reside in,
434 and where in the file. Set the file's read/write pointer
435 to point at the proper place for the desired address
436 and set xferfile and xferchan for the correct file.
438 If desired address is nonexistent, leave them zero.
440 i is set to the number of bytes that can be handled
441 along with the next address.
443 We put the most likely tests first for efficiency. */
445 /* Note that if there is no core file
446 data_start and data_end are equal. */
447 if (memaddr
>= data_start
&& memaddr
< data_end
)
449 i
= min (len
, data_end
- memaddr
);
450 fileptr
= memaddr
- data_start
+ data_offset
;
451 xferfile
= &corefile
;
454 /* Note that if there is no core file
455 stack_start and stack_end define the shared library data. */
456 else if (memaddr
>= stack_start
&& memaddr
< stack_end
)
460 struct shared_library
*lib
;
461 for (lib
= shlib
; lib
; lib
= lib
->shares
)
462 if (memaddr
>= lib
->header
.a_exec
.a_sldatabase
&&
463 memaddr
< lib
->header
.a_exec
.a_sldatabase
+
464 lib
->header
.a_exec
.a_data
)
468 i
= min (len
, lib
->header
.a_exec
.a_sldatabase
+
469 lib
->header
.a_exec
.a_data
- memaddr
);
470 fileptr
= lib
->data_offset
+ memaddr
-
471 lib
->header
.a_exec
.a_sldatabase
;
473 xferchan
= lib
->chan
;
478 i
= min (len
, stack_end
- memaddr
);
479 fileptr
= memaddr
- stack_start
+ stack_offset
;
480 xferfile
= &corefile
;
484 else if (corechan
< 0
485 && memaddr
>= exec_data_start
&& memaddr
< exec_data_end
)
487 i
= min (len
, exec_data_end
- memaddr
);
488 fileptr
= memaddr
- exec_data_start
+ exec_data_offset
;
489 xferfile
= &execfile
;
492 else if (memaddr
>= text_start
&& memaddr
< text_end
)
494 struct shared_library
*lib
;
495 for (lib
= shlib
; lib
; lib
= lib
->shares
)
496 if (memaddr
>= lib
->text_start
&&
497 memaddr
< lib
->text_start
+ lib
->header
.a_exec
.a_text
)
501 i
= min (len
, lib
->header
.a_exec
.a_text
+
502 lib
->text_start
- memaddr
);
503 fileptr
= memaddr
- lib
->text_start
+ text_offset
;
504 xferfile
= &execfile
;
505 xferchan
= lib
->chan
;
509 i
= min (len
, text_end
- memaddr
);
510 fileptr
= memaddr
- unshared_text_start
+ text_offset
;
511 xferfile
= &execfile
;
515 else if (memaddr
< text_start
)
517 i
= min (len
, text_start
- memaddr
);
519 else if (memaddr
>= text_end
520 && memaddr
< (corechan
>= 0 ? data_start
: exec_data_start
))
522 i
= min (len
, data_start
- memaddr
);
524 else if (corechan
>= 0
525 && memaddr
>= data_end
&& memaddr
< stack_start
)
527 i
= min (len
, stack_start
- memaddr
);
529 else if (corechan
< 0 && memaddr
>= exec_data_end
)
531 i
= min (len
, -memaddr
);
533 else if (memaddr
>= stack_end
&& stack_end
!= 0)
535 i
= min (len
, -memaddr
);
539 /* Address did not classify into one of the known ranges.
540 This shouldn't happen; we catch the endpoints. */
541 fatal ("Internal: Bad case logic in xfer_core_file.");
544 /* Now we know which file to use.
545 Set up its pointer and transfer the data. */
549 if (xferfile
== &execfile
)
550 error ("No program file to examine.");
552 error ("No core dump file or running program to examine.");
553 val
= lseek (xferchan
, fileptr
, 0);
555 perror_with_name (*xferfile
);
556 val
= myread (xferchan
, myaddr
, i
);
558 perror_with_name (*xferfile
);
560 /* If this address is for nonexistent memory,
561 read zeros if reading, or do nothing if writing.
562 Actually, we never right. */
565 memset (myaddr
, '\0', i
);