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