1 /* Interface to bare machine for GDB running as kernel debugger.
2 Copyright (C) 1986, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program 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 2 of the License, or
9 (at your option) any later version.
11 This program 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <sys/ioctl.h>
24 #include <sys/types.h>
27 #if defined (SIGTSTP) && defined (SIGIO)
29 #include <sys/resource.h>
30 #endif /* SIGTSTP and SIGIO defined (must be 4.2) */
40 /* Random system calls, mostly no-ops to prevent link problems */
42 ioctl (desc
, code
, arg
)
77 /* Used to check for existence of .gdbinit. Say no. */
86 error ("Fatal error; restarting.");
89 /* Reading "files". The contents of some files are written into kdb's
90 data area before it is run. These files are used to contain the
91 symbol table for kdb to load, and the source files (in case the
92 kdb user wants to print them). The symbols are stored in a file
93 named "kdb-symbols" in a.out format (except that all the text and
94 data have been stripped to save room).
96 The files are stored in the following format:
97 int number of bytes of data for this file, including these four.
98 char[] name of the file, ending with a null.
99 padding to multiple of 4 boundary.
100 char[] file contents. The length can be deduced from what was
101 specified before. There is no terminating null here.
103 If the int at the front is zero, it means there are no more files.
105 Opening a file in kdb returns a nonzero value to indicate success,
106 but the value does not matter. Only one file can be open, and only
107 for reading. All the primitives for input from the file know
108 which file is open and ignore what is specified for the descriptor
109 or for the stdio stream.
111 Input with fgetc can be done either on the file that is open
112 or on stdin (which reads from the terminal through tty_input () */
114 /* Address of data for the files stored in format described above. */
117 /* The file stream currently open: */
119 char *sourcebeg
; /* beginning of contents */
120 int sourcesize
; /* size of contents */
121 char *sourceptr
; /* current read pointer */
122 int sourceleft
; /* number of bytes to eof */
124 /* "descriptor" for the file now open.
125 Incremented at each close.
126 If specified descriptor does not match this,
127 it means the program is trying to use a closed descriptor.
128 We report an error for that. */
132 open (filename
, modes
)
150 for (next
= files_start
; *(int *) next
; next
+= *(int *) next
)
152 if (!STRCMP (next
+ 4, filename
))
154 sourcebeg
= next
+ 4 + strlen (next
+ 4) + 1;
155 sourcebeg
= (char *) (((int) sourcebeg
+ 3) & (-4));
156 sourceptr
= sourcebeg
;
157 sourcesize
= next
+ *(int *) next
- sourceptr
;
158 sourceleft
= sourcesize
;
170 /* Don't let sourcedesc get big enough to be confused with stdin. */
171 if (sourcedesc
== 100)
176 fopen (filename
, modes
)
180 return (FILE *) open (filename
, *modes
== 'w');
187 return (FILE *) desc
;
196 fstat (desc
, statbuf
)
197 struct stat
*statbuf
;
199 if (desc
!= sourcedesc
)
204 statbuf
->st_size
= sourcesize
;
207 myread (desc
, destptr
, size
, filename
)
213 int len
= min (sourceleft
, size
);
215 if (desc
!= sourcedesc
)
221 memcpy (destptr
, sourceptr
, len
);
227 fread (bufp
, numelts
, eltsize
, stream
)
229 register int elts
= min (numelts
, sourceleft
/ eltsize
);
230 register int len
= elts
* eltsize
;
232 if (stream
!= sourcedesc
)
238 memcpy (bufp
, sourceptr
, len
);
248 if (desc
== (int) stdin
)
251 if (desc
!= sourcedesc
)
257 if (sourceleft
-- <= 0)
267 if (desc
!= sourcedesc
)
273 if (pos
< 0 || pos
> sourcesize
)
279 sourceptr
= sourcebeg
+ pos
;
280 sourceleft
= sourcesize
- pos
;
283 /* Output in kdb can go only to the terminal, so the stream
284 specified may be ignored. */
286 printf (a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
)
289 sprintf (buffer
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
);
290 display_string (buffer
);
293 fprintf (ign
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
)
296 sprintf (buffer
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
);
297 display_string (buffer
);
300 fwrite (buf
, numelts
, size
, stream
)
304 register int i
= numelts
* size
;
306 fputc (*buf
++, stream
);
314 display_string (buf
);
317 /* sprintf refers to this, but loading this from the
318 library would cause fflush to be loaded from it too.
319 In fact there should be no need to call this (I hope). */
323 error ("_flsbuf was actually called.");
330 /* Entries into core and inflow, needed only to make things link ok. */
344 /* Makes one printout look reasonable; value does not matter otherwise. */
348 /* Nonzero if there is a core file. */
368 terminal_init_inferior ()
372 write_inferior_register ()
376 read_inferior_register ()
380 read_memory (memaddr
, myaddr
, len
)
385 memcpy (myaddr
, memaddr
, len
);
388 /* Always return 0 indicating success. */
390 write_memory (memaddr
, myaddr
, len
)
395 memcpy (memaddr
, myaddr
, len
);
399 static REGISTER_TYPE saved_regs
[NUM_REGS
];
402 read_register (regno
)
405 if (regno
< 0 || regno
>= NUM_REGS
)
406 error ("Register number %d out of range.", regno
);
407 return saved_regs
[regno
];
411 write_register (regno
, value
)
415 if (regno
< 0 || regno
>= NUM_REGS
)
416 error ("Register number %d out of range.", regno
);
417 saved_regs
[regno
] = value
;
420 /* System calls needed in relation to running the "inferior". */
424 /* Just appear to "succeed". Say the inferior's pid is 1. */
428 /* These are called by code that normally runs in the inferior
429 that has just been forked. That code never runs, when standalone,
430 and these definitions are so it will link without errors. */
448 /* Malloc calls these. */
453 printf ("\n%s.\n\n", str
);
463 if (next_free
+ amount
> memory_limit
)
466 return next_free
- amount
;
469 /* Various ways malloc might ask where end of memory is. */
480 return memory_limit
- next_free
;
486 addr
->rlim_cur
= memory_limit
- next_free
;
489 /* Context switching to and from program being debugged. */
491 /* GDB calls here to run the user program.
492 The frame pointer for this function is saved in
493 gdb_stack by save_frame_pointer; then we restore
494 all of the user program's registers, including PC and PS. */
496 static int fault_code
;
497 static REGISTER_TYPE gdb_stack
;
501 REGISTER_TYPE restore
[NUM_REGS
];
504 save_frame_pointer ();
506 memcpy (restore
, saved_regs
, sizeof restore
);
508 /* Control does not drop through here! */
511 save_frame_pointer (val
)
517 /* Fault handlers call here, running in the user program stack.
518 They must first push a fault code,
519 old PC, old PS, and any other info about the fault.
520 The exact format is machine-dependent and is known only
521 in the definition of PUSH_REGISTERS. */
525 /* Transfer all registers and fault code to the stack
526 in canonical order: registers in order of GDB register number,
527 followed by fault code. */
530 /* Transfer them to saved_regs and fault_code. */
534 /* Control does not reach here */
539 CORE_ADDR new_fp
= gdb_stack
;
540 /* Switch to GDB's stack */
542 /* Return from the function `resume'. */
545 /* Assuming register contents and fault code have been pushed on the stack as
546 arguments to this function, copy them into the standard place
547 for the program's registers while GDB is running. */
549 save_registers (firstreg
)
552 memcpy (saved_regs
, &firstreg
, sizeof saved_regs
);
553 fault_code
= (&firstreg
)[NUM_REGS
];
556 /* Store into the structure such as `wait' would return
557 the information on why the program faulted,
558 converted into a machine-independent signal number. */
560 static int fault_table
[] = FAULT_TABLE
;
566 WSETSTOP (*w
, fault_table
[fault_code
/ FAULT_CODE_UNITS
]);
570 /* Allocate a big space in which files for kdb to read will be stored.
571 Whatever is left is where malloc can allocate storage.
573 Initialize it, so that there will be space in the executable file
574 for it. Then the files can be put into kdb by writing them into
575 kdb's executable file. */
577 /* The default size is as much space as we expect to be available
581 #define HEAP_SIZE 400000
584 char heap
[HEAP_SIZE
] =
588 #define STACK_SIZE 100000
591 int kdb_stack_beg
[STACK_SIZE
/ sizeof (int)];
594 _initialize_standalone ()
598 /* Find start of data on files. */
602 /* Find the end of the data on files. */
604 for (next
= files_start
; *(int *) next
; next
+= *(int *) next
)
608 /* That is where free storage starts for sbrk to give out. */
611 memory_limit
= heap
+ sizeof heap
;
This page took 0.04067 seconds and 4 git commands to generate.