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) */
34 #include "initialize.h"
43 /* Random system calls, mostly no-ops to prevent link problems */
45 ioctl (desc
, code
, arg
)
74 /* Used to check for existence of .gdbinit. Say no. */
83 error ("Fatal error; restarting.");
86 /* Reading "files". The contents of some files are written into kdb's
87 data area before it is run. These files are used to contain the
88 symbol table for kdb to load, and the source files (in case the
89 kdb user wants to print them). The symbols are stored in a file
90 named "kdb-symbols" in a.out format (except that all the text and
91 data have been stripped to save room).
93 The files are stored in the following format:
94 int number of bytes of data for this file, including these four.
95 char[] name of the file, ending with a null.
96 padding to multiple of 4 boundary.
97 char[] file contents. The length can be deduced from what was
98 specified before. There is no terminating null here.
100 If the int at the front is zero, it means there are no more files.
102 Opening a file in kdb returns a nonzero value to indicate success,
103 but the value does not matter. Only one file can be open, and only
104 for reading. All the primitives for input from the file know
105 which file is open and ignore what is specified for the descriptor
106 or for the stdio stream.
108 Input with fgetc can be done either on the file that is open
109 or on stdin (which reads from the terminal through tty_input () */
111 /* Address of data for the files stored in format described above. */
114 /* The file stream currently open: */
116 char *sourcebeg
; /* beginning of contents */
117 int sourcesize
; /* size of contents */
118 char *sourceptr
; /* current read pointer */
119 int sourceleft
; /* number of bytes to eof */
121 /* "descriptor" for the file now open.
122 Incremented at each close.
123 If specified descriptor does not match this,
124 it means the program is trying to use a closed descriptor.
125 We report an error for that. */
129 open (filename
, modes
)
148 for (next
- files_start
; * (int *) next
;
149 next
+= * (int *) next
)
151 if (!strcmp (next
+ 4, filename
))
153 sourcebeg
= next
+ 4 + strlen (next
+ 4) + 1;
154 sourcebeg
= (char *) (((int) sourcebeg
+ 3) & (-4));
155 sourceptr
= sourcebeg
;
156 sourcesize
= next
+ * (int *) next
- sourceptr
;
157 sourceleft
= sourcesize
;
169 /* Don't let sourcedesc get big enough to be confused with stdin. */
170 if (sourcedesc
== 100)
175 fopen (filename
, modes
)
179 return (FILE *) open (filename
, *modes
== 'w');
186 return (FILE *) desc
;
195 fstat (desc
, statbuf
)
196 struct stat
*statbuf
;
200 if (desc
!= sourcedesc
)
205 statbuf
->st_size
= sourcesize
;
208 myread (desc
, destptr
, size
, filename
)
214 int len
= min (sourceleft
, size
);
217 if (desc
!= sourcedesc
)
223 bcopy (sourceptr
, destptr
, len
);
229 fread (bufp
, numelts
, eltsize
, stream
)
231 register int elts
= min (numelts
, sourceleft
/ eltsize
);
232 register int len
= elts
* eltsize
;
235 if (stream
!= sourcedesc
)
241 bcopy (sourceptr
, bufp
, len
);
252 if (desc
== (int) stdin
)
255 if (desc
!= sourcedesc
)
261 if (sourceleft
-- <= 0)
272 if (desc
!= sourcedesc
)
278 if (pos
< 0 || pos
> sourcesize
)
284 sourceptr
= sourcebeg
+ pos
;
285 sourceleft
= sourcesize
- pos
;
288 /* Output in kdb can go only to the terminal, so the stream
289 specified may be ignored. */
291 printf (a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
)
294 sprintf (buffer
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
);
295 display_string (buffer
);
298 fprintf (ign
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
)
301 sprintf (buffer
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
);
302 display_string (buffer
);
305 fwrite (buf
, numelts
, size
, stream
)
309 register int i
= numelts
* size
;
311 fputc (*buf
++, stream
);
319 display_string (buf
);
322 /* sprintf refers to this, but loading this from the
323 library would cause fflush to be loaded from it too.
324 In fact there should be no need to call this (I hope). */
328 error ("_flsbuf was actually called.");
335 /* Entries into core and inflow, needed only to make things link ok. */
346 /* Makes one printout look reasonable; value does not matter otherwise. */
366 terminal_init_inferior ()
369 write_inferior_register ()
372 read_inferior_register ()
375 read_memory (memaddr
, myaddr
, len
)
380 bcopy (memaddr
, myaddr
, len
);
383 /* Always return 0 indicating success. */
385 write_memory (memaddr
, myaddr
, len
)
390 bcopy (myaddr
, memaddr
, len
);
394 static REGISTER_TYPE saved_regs
[NUM_REGS
];
397 read_register (regno
)
400 if (regno
< 0 || regno
>= NUM_REGS
)
401 error ("Register number %d out of range.", regno
);
402 return saved_regs
[regno
];
406 write_register (regno
, value
)
410 if (regno
< 0 || regno
>= NUM_REGS
)
411 error ("Register number %d out of range.", regno
);
412 saved_regs
[regno
] = value
;
415 /* System calls needed in relation to running the "inferior". */
419 /* Just appear to "succeed". Say the inferior's pid is 1. */
423 /* These are called by code that normally runs in the inferior
424 that has just been forked. That code never runs, when standalone,
425 and these definitions are so it will link without errors. */
439 /* Malloc calls these. */
444 printf ("\n%s.\n\n", str
);
454 if (next_free
+ amount
> memory_limit
)
457 return next_free
- amount
;
460 /* Various ways malloc might ask where end of memory is. */
471 return memory_limit
- next_free
;
477 addr
->rlim_cur
= memory_limit
- next_free
;
480 /* Context switching to and from program being debugged. */
482 /* GDB calls here to run the user program.
483 The frame pointer for this function is saved in
484 gdb_stack by save_frame_pointer; then we restore
485 all of the user program's registers, including PC and PS. */
487 static int fault_code
;
488 static REGISTER_TYPE gdb_stack
;
492 REGISTER_TYPE restore
[NUM_REGS
];
495 save_frame_pointer ();
497 bcopy (saved_regs
, restore
, sizeof restore
);
499 /* Control does not drop through here! */
502 save_frame_pointer (val
)
508 /* Fault handlers call here, running in the user program stack.
509 They must first push a fault code,
510 old PC, old PS, and any other info about the fault.
511 The exact format is machine-dependent and is known only
512 in the definition of PUSH_REGISTERS. */
516 /* Transfer all registers and fault code to the stack
517 in canonical order: registers in order of GDB register number,
518 followed by fault code. */
521 /* Transfer them to saved_regs and fault_code. */
525 /* Control does not reach here */
530 CORE_ADDR new_fp
= gdb_stack
;
531 /* Switch to GDB's stack */
533 /* Return from the function `resume'. */
536 /* Assuming register contents and fault code have been pushed on the stack as
537 arguments to this function, copy them into the standard place
538 for the program's registers while GDB is running. */
540 save_registers (firstreg
)
543 bcopy (&firstreg
, saved_regs
, sizeof saved_regs
);
544 fault_code
= (&firstreg
)[NUM_REGS
];
547 /* Store into the structure such as `wait' would return
548 the information on why the program faulted,
549 converted into a machine-independent signal number. */
551 static int fault_table
[] = FAULT_TABLE
;
557 WSETSTOP (*w
, fault_table
[fault_code
/ FAULT_CODE_UNITS
]);
561 /* Allocate a big space in which files for kdb to read will be stored.
562 Whatever is left is where malloc can allocate storage.
564 Initialize it, so that there will be space in the executable file
565 for it. Then the files can be put into kdb by writing them into
566 kdb's executable file. */
568 /* The default size is as much space as we expect to be available
572 #define HEAP_SIZE 400000
575 char heap
[HEAP_SIZE
] = {0};
578 #define STACK_SIZE 100000
581 int kdb_stack_beg
[STACK_SIZE
/ sizeof (int)];
589 /* Find start of data on files. */
593 /* Find the end of the data on files. */
595 for (next
- files_start
; * (int *) next
;
596 next
+= * (int *) next
)
599 /* That is where free storage starts for sbrk to give out. */
602 memory_limit
= heap
+ sizeof heap
;
This page took 0.040335 seconds and 4 git commands to generate.