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 (int desc, int code, int arg)
68 getcwd (char *buf, unsigned int len)
75 /* Used to check for existence of .gdbinit. Say no. */
84 error ("Fatal error; restarting.");
87 /* Reading "files". The contents of some files are written into kdb's
88 data area before it is run. These files are used to contain the
89 symbol table for kdb to load, and the source files (in case the
90 kdb user wants to print them). The symbols are stored in a file
91 named "kdb-symbols" in a.out format (except that all the text and
92 data have been stripped to save room).
94 The files are stored in the following format:
95 int number of bytes of data for this file, including these four.
96 char[] name of the file, ending with a null.
97 padding to multiple of 4 boundary.
98 char[] file contents. The length can be deduced from what was
99 specified before. There is no terminating null here.
101 If the int at the front is zero, it means there are no more files.
103 Opening a file in kdb returns a nonzero value to indicate success,
104 but the value does not matter. Only one file can be open, and only
105 for reading. All the primitives for input from the file know
106 which file is open and ignore what is specified for the descriptor
107 or for the stdio stream.
109 Input with fgetc can be done either on the file that is open
110 or on stdin (which reads from the terminal through tty_input () */
112 /* Address of data for the files stored in format described above. */
115 /* The file stream currently open: */
117 char *sourcebeg; /* beginning of contents */
118 int sourcesize; /* size of contents */
119 char *sourceptr; /* current read pointer */
120 int sourceleft; /* number of bytes to eof */
122 /* "descriptor" for the file now open.
123 Incremented at each close.
124 If specified descriptor does not match this,
125 it means the program is trying to use a closed descriptor.
126 We report an error for that. */
130 open (char *filename, int modes)
146 for (next = files_start; *(int *) next; next += *(int *) next)
148 if (!STRCMP (next + 4, filename))
150 sourcebeg = next + 4 + strlen (next + 4) + 1;
151 sourcebeg = (char *) (((int) sourcebeg + 3) & (-4));
152 sourceptr = sourcebeg;
153 sourcesize = next + *(int *) next - sourceptr;
154 sourceleft = sourcesize;
165 /* Don't let sourcedesc get big enough to be confused with stdin. */
166 if (sourcedesc == 100)
171 fopen (char *filename, char *modes)
173 return (FILE *) open (filename, *modes == 'w');
179 return (FILE *) desc;
187 fstat (int desc, struct stat *statbuf)
189 if (desc != sourcedesc)
194 statbuf->st_size = sourcesize;
197 myread (int desc, char *destptr, int size, char *filename)
199 int len = min (sourceleft, size);
201 if (desc != sourcedesc)
207 memcpy (destptr, sourceptr, len);
213 fread (int bufp, int numelts, int eltsize, int stream)
215 register int elts = min (numelts, sourceleft / eltsize);
216 register int len = elts * eltsize;
218 if (stream != sourcedesc)
224 memcpy (bufp, sourceptr, len);
233 if (desc == (int) stdin)
236 if (desc != sourcedesc)
242 if (sourceleft-- <= 0)
247 lseek (int desc, int pos)
250 if (desc != sourcedesc)
256 if (pos < 0 || pos > sourcesize)
262 sourceptr = sourcebeg + pos;
263 sourceleft = sourcesize - pos;
266 /* Output in kdb can go only to the terminal, so the stream
267 specified may be ignored. */
269 printf (int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9)
272 sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
273 display_string (buffer);
276 fprintf (int ign, int a1, int a2, int a3, int a4, int a5, int a6, int a7,
280 sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
281 display_string (buffer);
284 fwrite (register char *buf, int numelts, int size, int stream)
286 register int i = numelts * size;
288 fputc (*buf++, stream);
291 fputc (int c, int ign)
296 display_string (buf);
299 /* sprintf refers to this, but loading this from the
300 library would cause fflush to be loaded from it too.
301 In fact there should be no need to call this (I hope). */
305 error ("_flsbuf was actually called.");
312 /* Entries into core and inflow, needed only to make things link ok. */
314 exec_file_command (void)
318 core_file_command (void)
323 get_exec_file (int err)
325 /* Makes one printout look reasonable; value does not matter otherwise. */
329 /* Nonzero if there is a core file. */
331 have_core_file_p (void)
341 terminal_inferior (void)
349 terminal_init_inferior (void)
353 write_inferior_register (void)
357 read_inferior_register (void)
361 read_memory (CORE_ADDR memaddr, char *myaddr, int len)
363 memcpy (myaddr, memaddr, len);
366 /* Always return 0 indicating success. */
368 write_memory (CORE_ADDR memaddr, char *myaddr, int len)
370 memcpy (memaddr, myaddr, len);
374 static REGISTER_TYPE saved_regs[NUM_REGS];
377 read_register (int regno)
379 if (regno < 0 || regno >= NUM_REGS)
380 error ("Register number %d out of range.", regno);
381 return saved_regs[regno];
385 write_register (int regno, REGISTER_TYPE value)
387 if (regno < 0 || regno >= NUM_REGS)
388 error ("Register number %d out of range.", regno);
389 saved_regs[regno] = value;
392 /* System calls needed in relation to running the "inferior". */
396 /* Just appear to "succeed". Say the inferior's pid is 1. */
400 /* These are called by code that normally runs in the inferior
401 that has just been forked. That code never runs, when standalone,
402 and these definitions are so it will link without errors. */
420 /* Malloc calls these. */
422 malloc_warning (char *str)
424 printf ("\n%s.\n\n", str);
433 if (next_free + amount > memory_limit)
436 return next_free - amount;
439 /* Various ways malloc might ask where end of memory is. */
450 return memory_limit - next_free;
453 getrlimit (struct rlimit *addr)
455 addr->rlim_cur = memory_limit - next_free;
458 /* Context switching to and from program being debugged. */
460 /* GDB calls here to run the user program.
461 The frame pointer for this function is saved in
462 gdb_stack by save_frame_pointer; then we restore
463 all of the user program's registers, including PC and PS. */
465 static int fault_code;
466 static REGISTER_TYPE gdb_stack;
470 REGISTER_TYPE restore[NUM_REGS];
473 save_frame_pointer ();
475 memcpy (restore, saved_regs, sizeof restore);
477 /* Control does not drop through here! */
480 save_frame_pointer (CORE_ADDR val)
485 /* Fault handlers call here, running in the user program stack.
486 They must first push a fault code,
487 old PC, old PS, and any other info about the fault.
488 The exact format is machine-dependent and is known only
489 in the definition of PUSH_REGISTERS. */
493 /* Transfer all registers and fault code to the stack
494 in canonical order: registers in order of GDB register number,
495 followed by fault code. */
498 /* Transfer them to saved_regs and fault_code. */
502 /* Control does not reach here */
507 CORE_ADDR new_fp = gdb_stack;
508 /* Switch to GDB's stack */
510 /* Return from the function `resume'. */
513 /* Assuming register contents and fault code have been pushed on the stack as
514 arguments to this function, copy them into the standard place
515 for the program's registers while GDB is running. */
517 save_registers (int firstreg)
519 memcpy (saved_regs, &firstreg, sizeof saved_regs);
520 fault_code = (&firstreg)[NUM_REGS];
523 /* Store into the structure such as `wait' would return
524 the information on why the program faulted,
525 converted into a machine-independent signal number. */
527 static int fault_table[] = FAULT_TABLE;
532 WSETSTOP (*w, fault_table[fault_code / FAULT_CODE_UNITS]);
536 /* Allocate a big space in which files for kdb to read will be stored.
537 Whatever is left is where malloc can allocate storage.
539 Initialize it, so that there will be space in the executable file
540 for it. Then the files can be put into kdb by writing them into
541 kdb's executable file. */
543 /* The default size is as much space as we expect to be available
547 #define HEAP_SIZE 400000
550 char heap[HEAP_SIZE] =
554 #define STACK_SIZE 100000
557 int kdb_stack_beg[STACK_SIZE / sizeof (int)];
560 _initialize_standalone (void)
564 /* Find start of data on files. */
568 /* Find the end of the data on files. */
570 for (next = files_start; *(int *) next; next += *(int *) next)
574 /* That is where free storage starts for sbrk to give out. */
577 memory_limit = heap + sizeof heap;