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