1 /* Interface to bare machine for GDB running as kernel debugger.
2 Copyright (C) 1986 Free Software Foundation, Inc.
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.
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.
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!
22 #include <sys/ioctl.h>
25 #include <sys/types.h>
28 #if defined (SIGTSTP) && defined (SIGIO)
30 #include <sys/resource.h>
31 #endif /* SIGTSTP and SIGIO defined (must be 4.2) */
41 /* Random system calls, mostly no-ops to prevent link problems */
43 ioctl (desc
, code
, arg
)
72 /* Used to check for existence of .gdbinit. Say no. */
81 error ("Fatal error; restarting.");
84 /* Reading "files". The contents of some files are written into kdb's
85 data area before it is run. These files are used to contain the
86 symbol table for kdb to load, and the source files (in case the
87 kdb user wants to print them). The symbols are stored in a file
88 named "kdb-symbols" in a.out format (except that all the text and
89 data have been stripped to save room).
91 The files are stored in the following format:
92 int number of bytes of data for this file, including these four.
93 char[] name of the file, ending with a null.
94 padding to multiple of 4 boundary.
95 char[] file contents. The length can be deduced from what was
96 specified before. There is no terminating null here.
98 If the int at the front is zero, it means there are no more files.
100 Opening a file in kdb returns a nonzero value to indicate success,
101 but the value does not matter. Only one file can be open, and only
102 for reading. All the primitives for input from the file know
103 which file is open and ignore what is specified for the descriptor
104 or for the stdio stream.
106 Input with fgetc can be done either on the file that is open
107 or on stdin (which reads from the terminal through tty_input () */
109 /* Address of data for the files stored in format described above. */
112 /* The file stream currently open: */
114 char *sourcebeg
; /* beginning of contents */
115 int sourcesize
; /* size of contents */
116 char *sourceptr
; /* current read pointer */
117 int sourceleft
; /* number of bytes to eof */
119 /* "descriptor" for the file now open.
120 Incremented at each close.
121 If specified descriptor does not match this,
122 it means the program is trying to use a closed descriptor.
123 We report an error for that. */
127 open (filename
, modes
)
146 for (next
- files_start
; * (int *) next
;
147 next
+= * (int *) next
)
149 if (!strcmp (next
+ 4, filename
))
151 sourcebeg
= next
+ 4 + strlen (next
+ 4) + 1;
152 sourcebeg
= (char *) (((int) sourcebeg
+ 3) & (-4));
153 sourceptr
= sourcebeg
;
154 sourcesize
= next
+ * (int *) next
- sourceptr
;
155 sourceleft
= sourcesize
;
167 /* Don't let sourcedesc get big enough to be confused with stdin. */
168 if (sourcedesc
== 100)
173 fopen (filename
, modes
)
177 return (FILE *) open (filename
, *modes
== 'w');
184 return (FILE *) desc
;
193 fstat (desc
, statbuf
)
194 struct stat
*statbuf
;
198 if (desc
!= sourcedesc
)
203 statbuf
->st_size
= sourcesize
;
206 myread (desc
, destptr
, size
, filename
)
212 int len
= min (sourceleft
, size
);
215 if (desc
!= sourcedesc
)
221 bcopy (sourceptr
, destptr
, len
);
227 fread (bufp
, numelts
, eltsize
, stream
)
229 register int elts
= min (numelts
, sourceleft
/ eltsize
);
230 register int len
= elts
* eltsize
;
233 if (stream
!= sourcedesc
)
239 bcopy (sourceptr
, bufp
, len
);
250 if (desc
== (int) stdin
)
253 if (desc
!= sourcedesc
)
259 if (sourceleft
-- <= 0)
270 if (desc
!= sourcedesc
)
276 if (pos
< 0 || pos
> sourcesize
)
282 sourceptr
= sourcebeg
+ pos
;
283 sourceleft
= sourcesize
- pos
;
286 /* Output in kdb can go only to the terminal, so the stream
287 specified may be ignored. */
289 printf (a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
)
292 sprintf (buffer
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
);
293 display_string (buffer
);
296 fprintf (ign
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
)
299 sprintf (buffer
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
);
300 display_string (buffer
);
303 fwrite (buf
, numelts
, size
, stream
)
307 register int i
= numelts
* size
;
309 fputc (*buf
++, stream
);
317 display_string (buf
);
320 /* sprintf refers to this, but loading this from the
321 library would cause fflush to be loaded from it too.
322 In fact there should be no need to call this (I hope). */
326 error ("_flsbuf was actually called.");
333 /* Entries into core and inflow, needed only to make things link ok. */
345 /* Makes one printout look reasonable; value does not matter otherwise. */
365 terminal_init_inferior ()
368 write_inferior_register ()
371 read_inferior_register ()
374 read_memory (memaddr
, myaddr
, len
)
379 bcopy (memaddr
, myaddr
, len
);
382 /* Always return 0 indicating success. */
384 write_memory (memaddr
, myaddr
, len
)
389 bcopy (myaddr
, memaddr
, len
);
393 static REGISTER_TYPE saved_regs
[NUM_REGS
];
396 read_register (regno
)
399 if (regno
< 0 || regno
>= NUM_REGS
)
400 error ("Register number %d out of range.", regno
);
401 return saved_regs
[regno
];
405 write_register (regno
, value
)
409 if (regno
< 0 || regno
>= NUM_REGS
)
410 error ("Register number %d out of range.", regno
);
411 saved_regs
[regno
] = value
;
414 /* System calls needed in relation to running the "inferior". */
418 /* Just appear to "succeed". Say the inferior's pid is 1. */
422 /* These are called by code that normally runs in the inferior
423 that has just been forked. That code never runs, when standalone,
424 and these definitions are so it will link without errors. */
438 /* Malloc calls these. */
443 printf ("\n%s.\n\n", str
);
453 if (next_free
+ amount
> memory_limit
)
456 return next_free
- amount
;
459 /* Various ways malloc might ask where end of memory is. */
470 return memory_limit
- next_free
;
476 addr
->rlim_cur
= memory_limit
- next_free
;
479 /* Context switching to and from program being debugged. */
481 /* GDB calls here to run the user program.
482 The frame pointer for this function is saved in
483 gdb_stack by save_frame_pointer; then we restore
484 all of the user program's registers, including PC and PS. */
486 static int fault_code
;
487 static REGISTER_TYPE gdb_stack
;
491 REGISTER_TYPE restore
[NUM_REGS
];
494 save_frame_pointer ();
496 bcopy (saved_regs
, restore
, sizeof restore
);
498 /* Control does not drop through here! */
501 save_frame_pointer (val
)
507 /* Fault handlers call here, running in the user program stack.
508 They must first push a fault code,
509 old PC, old PS, and any other info about the fault.
510 The exact format is machine-dependent and is known only
511 in the definition of PUSH_REGISTERS. */
515 /* Transfer all registers and fault code to the stack
516 in canonical order: registers in order of GDB register number,
517 followed by fault code. */
520 /* Transfer them to saved_regs and fault_code. */
524 /* Control does not reach here */
529 CORE_ADDR new_fp
= gdb_stack
;
530 /* Switch to GDB's stack */
532 /* Return from the function `resume'. */
535 /* Assuming register contents and fault code have been pushed on the stack as
536 arguments to this function, copy them into the standard place
537 for the program's registers while GDB is running. */
539 save_registers (firstreg
)
542 bcopy (&firstreg
, saved_regs
, sizeof saved_regs
);
543 fault_code
= (&firstreg
)[NUM_REGS
];
546 /* Store into the structure such as `wait' would return
547 the information on why the program faulted,
548 converted into a machine-independent signal number. */
550 static int fault_table
[] = FAULT_TABLE
;
556 WSETSTOP (*w
, fault_table
[fault_code
/ FAULT_CODE_UNITS
]);
560 /* Allocate a big space in which files for kdb to read will be stored.
561 Whatever is left is where malloc can allocate storage.
563 Initialize it, so that there will be space in the executable file
564 for it. Then the files can be put into kdb by writing them into
565 kdb's executable file. */
567 /* The default size is as much space as we expect to be available
571 #define HEAP_SIZE 400000
574 char heap
[HEAP_SIZE
] = {0};
577 #define STACK_SIZE 100000
580 int kdb_stack_beg
[STACK_SIZE
/ sizeof (int)];
583 _initialize_standalone ()
587 /* Find start of data on files. */
591 /* Find the end of the data on files. */
593 for (next
- files_start
; * (int *) next
;
594 next
+= * (int *) next
)
597 /* That is where free storage starts for sbrk to give out. */
600 memory_limit
= heap
+ sizeof heap
;