gdb-3.5
[deliverable/binutils-gdb.git] / gdb / umax-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/ioctl.h>
30#include <fcntl.h>
31
32#include <a.out.h>
33#include <sys/ptrace.h>
34#define PTRACE_ATTACH PT_ATTACH
35#define PTRACE_DETACH PT_FREEPROC
36
37#include <sys/file.h>
38#include <sys/stat.h>
39
40extern int errno;
41extern int attach_flag;
42\f
43/* This function simply calls ptrace with the given arguments.
44 It exists so that all calls to ptrace are isolated in this
45 machine-dependent file. */
46int
47call_ptrace (request, pid, arg3, arg4)
48 int request, pid, arg3, arg4;
49{
50 return ptrace (request, pid, arg3, arg4);
51}
52
53kill_inferior ()
54{
55 if (remote_debugging)
56 return;
57 if (inferior_pid == 0)
58 return;
59 ptrace (8, inferior_pid, 0, 0);
60 wait (0);
61 inferior_died ();
62}
63
64/* This is used when GDB is exiting. It gives less chance of error.*/
65
66kill_inferior_fast ()
67{
68 if (remote_debugging)
69 return;
70 if (inferior_pid == 0)
71 return;
72 ptrace (8, inferior_pid, 0, 0);
73 wait (0);
74}
75
76/* Resume execution of the inferior process.
77 If STEP is nonzero, single-step it.
78 If SIGNAL is nonzero, give it that signal. */
79
80void
81resume (step, signal)
82 int step;
83 int signal;
84{
85 errno = 0;
86 if (remote_debugging)
87 remote_resume (step, signal);
88 else
89 {
90 ptrace (step ? 9 : 7, inferior_pid, 1, signal);
91 if (errno)
92 perror_with_name ("ptrace");
93 }
94}
95\f
96#ifdef ATTACH_DETACH
97
98/* Start debugging the process whose number is PID. */
99
100attach (pid)
101 int pid;
102{
103 errno = 0;
104 ptrace (PTRACE_ATTACH, pid, 0, 0);
105 if (errno)
106 perror_with_name ("ptrace");
107 attach_flag = 1;
108 return pid;
109}
110
111/* Stop debugging the process whose number is PID
112 and continue it with signal number SIGNAL.
113 SIGNAL = 0 means just continue it. */
114
115void
116detach (signal)
117 int signal;
118{
119 errno = 0;
120 ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
121 if (errno)
122 perror_with_name ("ptrace");
123 attach_flag = 0;
124}
125#endif /* ATTACH_DETACH */
126\f
127void
128fetch_inferior_registers ()
129{
130 register int regno;
131 register unsigned int regaddr;
132 char buf[MAX_REGISTER_RAW_SIZE];
133 register int i;
134
135 unsigned int offset = 0;
136
137 for (regno = 0; regno < NUM_REGS; regno++)
138 {
139 regaddr = register_addr (regno, offset);
140 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
141 {
142 *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
143 regaddr += sizeof (int);
144 }
145 supply_register (regno, buf);
146 }
147}
148
149/* Store our register values back into the inferior.
150 If REGNO is -1, do this for all registers.
151 Otherwise, REGNO specifies which register (so we can save time). */
152
153store_inferior_registers (regno)
154 int regno;
155{
156 register unsigned int regaddr;
157 char buf[80];
158
159 unsigned int offset = 0;
160
161 if (regno >= 0)
162 {
163 regaddr = register_addr (regno, offset);
164 errno = 0;
165 ptrace (6, inferior_pid, regaddr, read_register (regno));
166 if (errno != 0)
167 {
168 sprintf (buf, "writing register number %d", regno);
169 perror_with_name (buf);
170 }
171 }
172 else for (regno = 0; regno < NUM_REGS; regno++)
173 {
174 regaddr = register_addr (regno, offset);
175 errno = 0;
176 ptrace (6, inferior_pid, regaddr, read_register (regno));
177 if (errno != 0)
178 {
179 sprintf (buf, "writing register number %d", regno);
180 perror_with_name (buf);
181 }
182 }
183}
184\f
185/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
186 in the NEW_SUN_PTRACE case.
187 It ought to be straightforward. But it appears that writing did
188 not write the data that I specified. I cannot understand where
189 it got the data that it actually did write. */
190
191/* Copy LEN bytes from inferior's memory starting at MEMADDR
192 to debugger memory starting at MYADDR.
193 On failure (cannot read from inferior, usually because address is out
194 of bounds) returns the value of errno. */
195
196int
197read_inferior_memory (memaddr, myaddr, len)
198 CORE_ADDR memaddr;
199 char *myaddr;
200 int len;
201{
202 register int i;
203 /* Round starting address down to longword boundary. */
204 register CORE_ADDR addr = memaddr & - sizeof (int);
205 /* Round ending address up; get number of longwords that makes. */
206 register int count
207 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
208 /* Allocate buffer of that many longwords. */
209 register int *buffer = (int *) alloca (count * sizeof (int));
210 extern int errno;
211
212 /* Read all the longwords */
213 for (i = 0; i < count; i++, addr += sizeof (int))
214 {
215 errno = 0;
216 if (remote_debugging)
217 buffer[i] = remote_fetch_word (addr);
218 else
219 buffer[i] = ptrace (1, inferior_pid, addr, 0);
220 if (errno)
221 return errno;
222 }
223
224 /* Copy appropriate bytes out of the buffer. */
225 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
226 return 0;
227}
228
229/* Copy LEN bytes of data from debugger memory at MYADDR
230 to inferior's memory at MEMADDR.
231 On failure (cannot write the inferior)
232 returns the value of errno. */
233
234int
235write_inferior_memory (memaddr, myaddr, len)
236 CORE_ADDR memaddr;
237 char *myaddr;
238 int len;
239{
240 register int i;
241 /* Round starting address down to longword boundary. */
242 register CORE_ADDR addr = memaddr & - sizeof (int);
243 /* Round ending address up; get number of longwords that makes. */
244 register int count
245 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
246 /* Allocate buffer of that many longwords. */
247 register int *buffer = (int *) alloca (count * sizeof (int));
248 extern int errno;
249
250 /* Fill start and end extra bytes of buffer with existing memory data. */
251
252 if (remote_debugging)
253 buffer[0] = remote_fetch_word (addr);
254 else
255 buffer[0] = ptrace (1, inferior_pid, addr, 0);
256
257 if (count > 1)
258 {
259 if (remote_debugging)
260 buffer[count - 1]
261 = remote_fetch_word (addr + (count - 1) * sizeof (int));
262 else
263 buffer[count - 1]
264 = ptrace (1, inferior_pid,
265 addr + (count - 1) * sizeof (int), 0);
266 }
267
268 /* Copy data to be written over corresponding part of buffer */
269
270 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
271
272 /* Write the entire buffer. */
273
274 for (i = 0; i < count; i++, addr += sizeof (int))
275 {
276 errno = 0;
277 if (remote_debugging)
278 remote_store_word (addr, buffer[i]);
279 else
280 ptrace (4, inferior_pid, addr, buffer[i]);
281 if (errno)
282 return errno;
283 }
284
285 return 0;
286}
287\f
288/* Work with core dump and executable files, for GDB.
289 This code would be in core.c if it weren't machine-dependent. */
290
291/* Recognize COFF format systems because a.out.h defines AOUTHDR. */
292#ifdef AOUTHDR
293#define COFF_FORMAT
294#endif
295
296#ifndef N_TXTADDR
297#define N_TXTADDR(hdr) 0
298#endif /* no N_TXTADDR */
299
300#ifndef N_DATADDR
301#define N_DATADDR(hdr) hdr.a_text
302#endif /* no N_DATADDR */
303
304/* Make COFF and non-COFF names for things a little more compatible
305 to reduce conditionals later. */
306
307#ifdef COFF_FORMAT
308#define a_magic magic
309#endif
310
311#ifndef COFF_FORMAT
4187119d 312#ifndef AOUTHDR
e91b87a3 313#define AOUTHDR struct exec
314#endif
4187119d 315#endif
e91b87a3 316
317extern char *sys_siglist[];
318
319
320/* Hook for `exec_file_command' command to call. */
321
322extern void (*exec_file_display_hook) ();
323
324/* File names of core file and executable file. */
325
326extern char *corefile;
327extern char *execfile;
328
329/* Descriptors on which core file and executable file are open.
330 Note that the execchan is closed when an inferior is created
331 and reopened if the inferior dies or is killed. */
332
333extern int corechan;
334extern int execchan;
335
336/* Last modification time of executable file.
337 Also used in source.c to compare against mtime of a source file. */
338
339extern int exec_mtime;
340
341/* Virtual addresses of bounds of the two areas of memory in the core file. */
342
343extern CORE_ADDR data_start;
344extern CORE_ADDR data_end;
345extern CORE_ADDR stack_start;
346extern CORE_ADDR stack_end;
347
348/* Virtual addresses of bounds of two areas of memory in the exec file.
349 Note that the data area in the exec file is used only when there is no core file. */
350
351extern CORE_ADDR text_start;
352extern CORE_ADDR text_end;
353
354extern CORE_ADDR exec_data_start;
355extern CORE_ADDR exec_data_end;
356
357/* Address in executable file of start of text area data. */
358
359extern int text_offset;
360
361/* Address in executable file of start of data area data. */
362
363extern int exec_data_offset;
364
365/* Address in core file of start of data area data. */
366
367extern int data_offset;
368
369/* Address in core file of start of stack area data. */
370
371extern int stack_offset;
372
373#ifdef COFF_FORMAT
374/* various coff data structures */
375
376extern FILHDR file_hdr;
377extern SCNHDR text_hdr;
378extern SCNHDR data_hdr;
379
380#endif /* not COFF_FORMAT */
381
382/* a.out header saved in core file. */
383
384extern AOUTHDR core_aouthdr;
385
386/* a.out header of exec file. */
387
388extern AOUTHDR exec_aouthdr;
389
390extern void validate_files ();
391\f
392core_file_command (filename, from_tty)
393 char *filename;
394 int from_tty;
395{
396 int val;
397 extern char registers[];
398
399 /* Discard all vestiges of any previous core file
400 and mark data and stack spaces as empty. */
401
402 if (corefile)
403 free (corefile);
404 corefile = 0;
405
406 if (corechan >= 0)
407 close (corechan);
408 corechan = -1;
409
410 data_start = 0;
411 data_end = 0;
412 stack_start = STACK_END_ADDR;
413 stack_end = STACK_END_ADDR;
414
415 /* Now, if a new core file was specified, open it and digest it. */
416
417 if (filename)
418 {
4187119d 419 filename = tilde_expand (filename);
420 make_cleanup (free, filename);
421
e91b87a3 422 if (have_inferior_p ())
423 error ("To look at a core file, you must kill the inferior with \"kill\".");
424 corechan = open (filename, O_RDONLY, 0);
425 if (corechan < 0)
426 perror_with_name (filename);
427 /* 4.2-style (and perhaps also sysV-style) core dump file. */
428 {
429 struct ptrace_user u;
430 int reg_offset;
431
432 val = myread (corechan, &u, sizeof u);
433 if (val < 0)
434 perror_with_name (filename);
435 data_start = exec_data_start;
436
437 data_end = data_start + u.pt_dsize;
438 stack_start = stack_end - u.pt_ssize;
439 data_offset = sizeof u;
440 stack_offset = data_offset + u.pt_dsize;
441 reg_offset = 0;
442
443 bcopy (&u.pt_aouthdr, &core_aouthdr, sizeof (AOUTHDR));
444 printf ("Core file is from \"%s\".\n", u.pt_comm);
445 if (u.pt_signal > 0)
446 printf ("Program terminated with signal %d, %s.\n",
447 u.pt_signal,
448 u.pt_signal < NSIG
449 ? sys_siglist[u.pt_signal]
450 : "(undocumented)");
451
452 /* Read the register values out of the core file and store
453 them where `read_register' will find them. */
454
455 {
456 register int regno;
457
458 for (regno = 0; regno < NUM_REGS; regno++)
459 {
460 char buf[MAX_REGISTER_RAW_SIZE];
461
462 val = lseek (corechan, register_addr (regno, reg_offset), 0);
463 if (val < 0)
464 perror_with_name (filename);
465
466 val = myread (corechan, buf, sizeof buf);
467 if (val < 0)
468 perror_with_name (filename);
469 supply_register (regno, buf);
470 }
471 }
472 }
473 if (filename[0] == '/')
474 corefile = savestring (filename, strlen (filename));
475 else
476 {
477 corefile = concat (current_directory, "/", filename);
478 }
479
480 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
481 read_pc ()));
482 select_frame (get_current_frame (), 0);
483 validate_files ();
484 }
485 else if (from_tty)
486 printf ("No core file now.\n");
487}
488\f
489exec_file_command (filename, from_tty)
490 char *filename;
491 int from_tty;
492{
493 int val;
494
495 /* Eliminate all traces of old exec file.
496 Mark text segment as empty. */
497
498 if (execfile)
499 free (execfile);
500 execfile = 0;
501 data_start = 0;
502 data_end -= exec_data_start;
503 text_start = 0;
504 text_end = 0;
505 exec_data_start = 0;
506 exec_data_end = 0;
507 if (execchan >= 0)
508 close (execchan);
509 execchan = -1;
510
511 /* Now open and digest the file the user requested, if any. */
512
513 if (filename)
514 {
4187119d 515 filename = tilde_expand (filename);
516 make_cleanup (free, filename);
517
e91b87a3 518 execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
519 &execfile);
520 if (execchan < 0)
521 perror_with_name (filename);
522
523#ifdef COFF_FORMAT
524 {
525 int aout_hdrsize;
526 int num_sections;
527
528 if (read_file_hdr (execchan, &file_hdr) < 0)
529 error ("\"%s\": not in executable format.", execfile);
530
531 aout_hdrsize = file_hdr.f_opthdr;
532 num_sections = file_hdr.f_nscns;
533
534 if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
535 error ("\"%s\": can't read optional aouthdr", execfile);
536
7a67dd45 537 if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
538 aout_hdrsize) < 0)
e91b87a3 539 error ("\"%s\": can't read text section header", execfile);
540
7a67dd45 541 if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
542 aout_hdrsize) < 0)
e91b87a3 543 error ("\"%s\": can't read data section header", execfile);
544
545 text_start = exec_aouthdr.text_start;
546 text_end = text_start + exec_aouthdr.tsize;
547 text_offset = text_hdr.s_scnptr;
548 exec_data_start = exec_aouthdr.data_start;
549 exec_data_end = exec_data_start + exec_aouthdr.dsize;
550 exec_data_offset = data_hdr.s_scnptr;
551 data_start = exec_data_start;
552 data_end += exec_data_start;
553 exec_mtime = file_hdr.f_timdat;
554 }
555#else /* not COFF_FORMAT */
556 {
557 struct stat st_exec;
558
559 val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
560
561 if (val < 0)
562 perror_with_name (filename);
563
564 text_start = N_TXTADDR (exec_aouthdr);
565 exec_data_start = N_DATADDR (exec_aouthdr);
566
567 text_offset = N_TXTOFF (exec_aouthdr);
568 exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
569
570 text_end = text_start + exec_aouthdr.a_text;
571 exec_data_end = exec_data_start + exec_aouthdr.a_data;
572 data_start = exec_data_start;
573 data_end += exec_data_start;
574
575 fstat (execchan, &st_exec);
576 exec_mtime = st_exec.st_mtime;
577 }
578#endif /* not COFF_FORMAT */
579
580 validate_files ();
581 }
582 else if (from_tty)
583 printf ("No exec file now.\n");
584
585 /* Tell display code (if any) about the changed file name. */
586 if (exec_file_display_hook)
587 (*exec_file_display_hook) (filename);
588}
This page took 0.045947 seconds and 4 git commands to generate.