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