Oh GACK! src-clean doesn't quite work that easily since cleandist rebuilds the
[unix-history] / gnu / usr.bin / gdb / config / default-dep.c
CommitLineData
04497f0b
NW
1/*-
2 * This code is derived from software copyrighted by the Free Software
3 * Foundation.
4 *
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6 */
7
8#ifndef lint
9static char sccsid[] = "@(#)default-dep.c 6.3 (Berkeley) 5/8/91";
10#endif /* not lint */
11
12/* Low level interface to ptrace, for GDB when running under Unix.
13 Copyright (C) 1988, 1989 Free Software Foundation, Inc.
14
15This file is part of GDB.
16
17GDB is free software; you can redistribute it and/or modify
18it under the terms of the GNU General Public License as published by
19the Free Software Foundation; either version 1, or (at your option)
20any later version.
21
22GDB is distributed in the hope that it will be useful,
23but WITHOUT ANY WARRANTY; without even the implied warranty of
24MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25GNU General Public License for more details.
26
27You should have received a copy of the GNU General Public License
28along with GDB; see the file COPYING. If not, write to
29the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
30
31#include <stdio.h>
32#include "defs.h"
33#include "param.h"
34#include "frame.h"
35#include "inferior.h"
36
37#ifdef USG
38#include <sys/types.h>
39#endif
40
41#include <sys/param.h>
42#include <sys/dir.h>
43#include <signal.h>
44#include <sys/ioctl.h>
45/* #include <fcntl.h> Can we live without this? */
46
47#ifdef COFF_ENCAPSULATE
48#include "a.out.encap.h"
49#else
50#include <a.out.h>
51#endif
52#ifndef N_SET_MAGIC
53#define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
54#endif
55
56#include <sys/user.h> /* After a.out.h */
57#include <sys/file.h>
58#include <sys/stat.h>
59
60extern int errno;
61\f
62/* This function simply calls ptrace with the given arguments.
63 It exists so that all calls to ptrace are isolated in this
64 machine-dependent file. */
65int
66call_ptrace (request, pid, arg3, arg4)
67 int request, pid, arg3, arg4;
68{
69 return ptrace (request, pid, arg3, arg4);
70}
71
72kill_inferior ()
73{
74 if (remote_debugging)
75 return;
76 if (inferior_pid == 0)
77 return;
78 ptrace (8, inferior_pid, 0, 0);
79 wait (0);
80 inferior_died ();
81}
82
83/* This is used when GDB is exiting. It gives less chance of error.*/
84
85kill_inferior_fast ()
86{
87 if (remote_debugging)
88 return;
89 if (inferior_pid == 0)
90 return;
91 ptrace (8, inferior_pid, 0, 0);
92 wait (0);
93}
94
95/* Resume execution of the inferior process.
96 If STEP is nonzero, single-step it.
97 If SIGNAL is nonzero, give it that signal. */
98
99void
100resume (step, signal)
101 int step;
102 int signal;
103{
104 errno = 0;
105 if (remote_debugging)
106 remote_resume (step, signal);
107 else
108 {
109 ptrace (step ? 9 : 7, inferior_pid, 1, signal);
110 if (errno)
111 perror_with_name ("ptrace");
112 }
113}
114\f
115void
116fetch_inferior_registers ()
117{
118 register int regno;
119 register unsigned int regaddr;
120 char buf[MAX_REGISTER_RAW_SIZE];
121 register int i;
122
123 struct user u;
124 unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
125 offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
126
127 for (regno = 0; regno < NUM_REGS; regno++)
128 {
129 regaddr = register_addr (regno, offset);
130 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
131 {
132 *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
133 regaddr += sizeof (int);
134 }
135 supply_register (regno, buf);
136 }
137}
138
139/* Store our register values back into the inferior.
140 If REGNO is -1, do this for all registers.
141 Otherwise, REGNO specifies which register (so we can save time). */
142
143store_inferior_registers (regno)
144 int regno;
145{
146 register unsigned int regaddr;
147 char buf[80];
148
149 struct user u;
150 unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
151 offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
152
153 if (regno >= 0)
154 {
155 regaddr = register_addr (regno, offset);
156 errno = 0;
157 ptrace (6, inferior_pid, regaddr, read_register (regno));
158 if (errno != 0)
159 {
160 sprintf (buf, "writing register number %d", regno);
161 perror_with_name (buf);
162 }
163 }
164 else for (regno = 0; regno < NUM_REGS; regno++)
165 {
166 regaddr = register_addr (regno, offset);
167 errno = 0;
168 ptrace (6, inferior_pid, regaddr, read_register (regno));
169 if (errno != 0)
170 {
171 sprintf (buf, "writing all regs, number %d", regno);
172 perror_with_name (buf);
173 }
174 }
175}
176\f
177/* Copy LEN bytes from inferior's memory starting at MEMADDR
178 to debugger memory starting at MYADDR.
179 On failure (cannot read from inferior, usually because address is out
180 of bounds) returns the value of errno. */
181
182int
183read_inferior_memory (memaddr, myaddr, len)
184 CORE_ADDR memaddr;
185 char *myaddr;
186 int len;
187{
188 register int i;
189 /* Round starting address down to longword boundary. */
190 register CORE_ADDR addr = memaddr & - sizeof (int);
191 /* Round ending address up; get number of longwords that makes. */
192 register int count
193 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
194 /* Allocate buffer of that many longwords. */
195 register int *buffer = (int *) alloca (count * sizeof (int));
196 extern int errno;
197
198 /* Read all the longwords */
199 for (i = 0; i < count; i++, addr += sizeof (int))
200 {
201 errno = 0;
202#if 0
203 /* This is now done by read_memory, because when this function did it,
204 reading a byte or short int hardware port read whole longs, causing
205 serious side effects
206 such as bus errors and unexpected hardware operation. This would
207 also be a problem with ptrace if the inferior process could read
208 or write hardware registers, but that's not usually the case. */
209 if (remote_debugging)
210 buffer[i] = remote_fetch_word (addr);
211 else
212#endif
213 buffer[i] = ptrace (1, inferior_pid, addr, 0);
214 if (errno)
215 return errno;
216 }
217
218 /* Copy appropriate bytes out of the buffer. */
219 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
220 return 0;
221}
222
223/* Copy LEN bytes of data from debugger memory at MYADDR
224 to inferior's memory at MEMADDR.
225 On failure (cannot write the inferior)
226 returns the value of errno. */
227
228int
229write_inferior_memory (memaddr, myaddr, len)
230 CORE_ADDR memaddr;
231 char *myaddr;
232 int len;
233{
234 register int i;
235 /* Round starting address down to longword boundary. */
236 register CORE_ADDR addr = memaddr & - sizeof (int);
237 /* Round ending address up; get number of longwords that makes. */
238 register int count
239 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
240 /* Allocate buffer of that many longwords. */
241 register int *buffer = (int *) alloca (count * sizeof (int));
242 extern int errno;
243
244 /* Fill start and end extra bytes of buffer with existing memory data. */
245
246 if (remote_debugging)
247 return (remote_write_inferior_memory(memaddr, myaddr, len));
248
249 buffer[0] = ptrace (1, inferior_pid, addr, 0);
250
251 if (count > 1)
252 buffer[count - 1] = ptrace (1, inferior_pid,
253 addr + (count - 1) * sizeof (int), 0);
254
255 /* Copy data to be written over corresponding part of buffer */
256
257 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
258
259 /* Write the entire buffer. */
260
261 for (i = 0; i < count; i++, addr += sizeof (int))
262 {
263 errno = 0;
264 ptrace (4, inferior_pid, addr, buffer[i]);
265 if (errno)
266 return errno;
267 }
268
269 return 0;
270}
271\f
272/* Work with core dump and executable files, for GDB.
273 This code would be in core.c if it weren't machine-dependent. */
274
275#ifndef N_TXTADDR
276#define N_TXTADDR(hdr) 0
277#endif /* no N_TXTADDR */
278
279#ifndef N_DATADDR
280#define N_DATADDR(hdr) hdr.a_text
281#endif /* no N_DATADDR */
282
283/* Make COFF and non-COFF names for things a little more compatible
284 to reduce conditionals later. */
285
286#ifdef COFF_FORMAT
287#define a_magic magic
288#endif
289
290#ifndef COFF_FORMAT
291#ifndef AOUTHDR
292#define AOUTHDR struct exec
293#endif
294#endif
295
296extern char *sys_siglist[];
297
298
299/* Hook for `exec_file_command' command to call. */
300
301extern void (*exec_file_display_hook) ();
302
303/* File names of core file and executable file. */
304
305extern char *corefile;
306extern char *execfile;
307
308/* Descriptors on which core file and executable file are open.
309 Note that the execchan is closed when an inferior is created
310 and reopened if the inferior dies or is killed. */
311
312extern int corechan;
313extern int execchan;
314
315/* Last modification time of executable file.
316 Also used in source.c to compare against mtime of a source file. */
317
318extern int exec_mtime;
319
320/* Virtual addresses of bounds of the two areas of memory in the core file. */
321
322extern CORE_ADDR data_start;
323extern CORE_ADDR data_end;
324extern CORE_ADDR stack_start;
325extern CORE_ADDR stack_end;
326
327/* Virtual addresses of bounds of two areas of memory in the exec file.
328 Note that the data area in the exec file is used only when there is no core file. */
329
330extern CORE_ADDR text_start;
331extern CORE_ADDR text_end;
332
333extern CORE_ADDR exec_data_start;
334extern CORE_ADDR exec_data_end;
335
336/* Address in executable file of start of text area data. */
337
338extern int text_offset;
339
340/* Address in executable file of start of data area data. */
341
342extern int exec_data_offset;
343
344/* Address in core file of start of data area data. */
345
346extern int data_offset;
347
348/* Address in core file of start of stack area data. */
349
350extern int stack_offset;
351
352#ifdef COFF_FORMAT
353/* various coff data structures */
354
355extern FILHDR file_hdr;
356extern SCNHDR text_hdr;
357extern SCNHDR data_hdr;
358
359#endif /* not COFF_FORMAT */
360
361/* a.out header saved in core file. */
362
363extern AOUTHDR core_aouthdr;
364
365/* a.out header of exec file. */
366
367extern AOUTHDR exec_aouthdr;
368
369extern void validate_files ();
370\f
371core_file_command (filename, from_tty)
372 char *filename;
373 int from_tty;
374{
375 int val;
376 extern char registers[];
377
378 /* Discard all vestiges of any previous core file
379 and mark data and stack spaces as empty. */
380
381 if (corefile)
382 free (corefile);
383 corefile = 0;
384
385 if (corechan >= 0)
386 close (corechan);
387 corechan = -1;
388
389 data_start = 0;
390 data_end = 0;
391 stack_start = STACK_END_ADDR;
392 stack_end = STACK_END_ADDR;
393
394 /* Now, if a new core file was specified, open it and digest it. */
395
396 if (filename)
397 {
398 filename = tilde_expand (filename);
399 make_cleanup (free, filename);
400
401 if (have_inferior_p ())
402 error ("To look at a core file, you must kill the inferior with \"kill\".");
403 corechan = open (filename, O_RDONLY, 0);
404 if (corechan < 0)
405 perror_with_name (filename);
406 /* 4.2-style (and perhaps also sysV-style) core dump file. */
407 {
408 struct user u;
409
410 unsigned int reg_offset;
411
412 val = myread (corechan, &u, sizeof u);
413 if (val < 0)
414 perror_with_name ("Not a core file: reading upage");
415 if (val != sizeof u)
416 error ("Not a core file: could only read %d bytes", val);
417
418 /* We are depending on exec_file_command having been called
419 previously to set exec_data_start. Since the executable
420 and the core file share the same text segment, the address
421 of the data segment will be the same in both. */
422 data_start = exec_data_start;
423
424 data_end = data_start + NBPG * u.u_dsize;
425 stack_start = stack_end - NBPG * u.u_ssize;
426 data_offset = NBPG * UPAGES;
427 stack_offset = NBPG * (UPAGES + u.u_dsize);
428
429 /* Some machines put an absolute address in here and some put
430 the offset in the upage of the regs. */
431 reg_offset = (int) u.u_ar0;
432 if (reg_offset > NBPG * UPAGES)
433 reg_offset -= KERNEL_U_ADDR;
434
435 /* I don't know where to find this info.
436 So, for now, mark it as not available. */
437 N_SET_MAGIC (core_aouthdr, 0);
438
439 /* Read the register values out of the core file and store
440 them where `read_register' will find them. */
441
442 {
443 register int regno;
444
445 for (regno = 0; regno < NUM_REGS; regno++)
446 {
447 char buf[MAX_REGISTER_RAW_SIZE];
448
449 val = lseek (corechan, register_addr (regno, reg_offset), 0);
450 if (val < 0
451 || (val = myread (corechan, buf, sizeof buf)) < 0)
452 {
453 char * buffer = (char *) alloca (strlen (reg_names[regno])
454 + 30);
455 strcpy (buffer, "Reading register ");
456 strcat (buffer, reg_names[regno]);
457
458 perror_with_name (buffer);
459 }
460
461 supply_register (regno, buf);
462 }
463 }
464 }
465 if (filename[0] == '/')
466 corefile = savestring (filename, strlen (filename));
467 else
468 {
469 corefile = concat (current_directory, "/", filename);
470 }
471
472 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
473 read_pc ()));
474 select_frame (get_current_frame (), 0);
475 validate_files ();
476 }
477 else if (from_tty)
478 printf ("No core file now.\n");
479}
480\f
481exec_file_command (filename, from_tty)
482 char *filename;
483 int from_tty;
484{
485 int val;
486
487 /* Eliminate all traces of old exec file.
488 Mark text segment as empty. */
489
490 if (execfile)
491 free (execfile);
492 execfile = 0;
493 data_start = 0;
494 data_end -= exec_data_start;
495 text_start = 0;
496 text_end = 0;
497 exec_data_start = 0;
498 exec_data_end = 0;
499 if (execchan >= 0)
500 close (execchan);
501 execchan = -1;
502
503 /* Now open and digest the file the user requested, if any. */
504
505 if (filename)
506 {
507 filename = tilde_expand (filename);
508 make_cleanup (free, filename);
509
510 execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
511 &execfile);
512 if (execchan < 0)
513 perror_with_name (filename);
514
515#ifdef COFF_FORMAT
516 {
517 int aout_hdrsize;
518 int num_sections;
519
520 if (read_file_hdr (execchan, &file_hdr) < 0)
521 error ("\"%s\": not in executable format.", execfile);
522
523 aout_hdrsize = file_hdr.f_opthdr;
524 num_sections = file_hdr.f_nscns;
525
526 if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
527 error ("\"%s\": can't read optional aouthdr", execfile);
528
529 if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
530 aout_hdrsize) < 0)
531 error ("\"%s\": can't read text section header", execfile);
532
533 if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
534 aout_hdrsize) < 0)
535 error ("\"%s\": can't read data section header", execfile);
536
537 text_start = exec_aouthdr.text_start;
538 text_end = text_start + exec_aouthdr.tsize;
539 text_offset = text_hdr.s_scnptr;
540 exec_data_start = exec_aouthdr.data_start;
541 exec_data_end = exec_data_start + exec_aouthdr.dsize;
542 exec_data_offset = data_hdr.s_scnptr;
543 data_start = exec_data_start;
544 data_end += exec_data_start;
545 exec_mtime = file_hdr.f_timdat;
546 }
547#else /* not COFF_FORMAT */
548 {
549 struct stat st_exec;
550
551#ifdef HEADER_SEEK_FD
552 HEADER_SEEK_FD (execchan);
553#endif
554
555 val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
556
557 if (val < 0)
558 perror_with_name (filename);
559
560 text_start = N_TXTADDR (exec_aouthdr);
561 exec_data_start = N_DATADDR (exec_aouthdr);
562
563 text_offset = N_TXTOFF (exec_aouthdr);
564 exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
565
566 text_end = text_start + exec_aouthdr.a_text;
567 exec_data_end = exec_data_start + exec_aouthdr.a_data;
568 data_start = exec_data_start;
569 data_end += exec_data_start;
570
571 if (fstat (execchan, &st_exec) < 0)
572 perror_with_name (filename);
573 exec_mtime = st_exec.st_mtime;
574 }
575#endif /* not COFF_FORMAT */
576
577 validate_files ();
578 }
579 else if (from_tty)
580 printf ("No exec file now.\n");
581
582 /* Tell display code (if any) about the changed file name. */
583 if (exec_file_display_hook)
584 (*exec_file_display_hook) (filename);
585}