1 /* Interface to bare machine for GDB running as kernel debugger.
3 Copyright 1986, 1989, 1991, 1992, 1993, 1995, 1996, 2000, 2001,
4 2003 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include <sys/ioctl.h>
26 #include <sys/types.h>
29 #if defined (SIGTSTP) && defined (SIGIO)
31 #include <sys/resource.h>
32 #endif /* SIGTSTP and SIGIO defined (must be 4.2) */
42 /* Random system calls, mostly no-ops to prevent link problems */
44 ioctl (int desc, int code, int arg)
70 getcwd (char *buf, unsigned int len)
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 (char *filename, int modes)
148 for (next = files_start; *(int *) next; next += *(int *) next)
150 if (!strcmp (next + 4, filename))
152 sourcebeg = next + 4 + strlen (next + 4) + 1;
153 sourcebeg = (char *) (((int) sourcebeg + 3) & (-4));
154 sourceptr = sourcebeg;
155 sourcesize = next + *(int *) next - sourceptr;
156 sourceleft = sourcesize;
167 /* Don't let sourcedesc get big enough to be confused with stdin. */
168 if (sourcedesc == 100)
173 fopen (char *filename, char *modes)
175 return (FILE *) open (filename, *modes == 'w');
181 return (FILE *) desc;
189 fstat (int desc, struct stat *statbuf)
191 if (desc != sourcedesc)
196 statbuf->st_size = sourcesize;
199 myread (int desc, char *destptr, int size, char *filename)
201 int len = min (sourceleft, size);
203 if (desc != sourcedesc)
209 memcpy (destptr, sourceptr, len);
215 fread (int bufp, int numelts, int eltsize, int stream)
217 int elts = min (numelts, sourceleft / eltsize);
218 int len = elts * eltsize;
220 if (stream != sourcedesc)
226 memcpy (bufp, sourceptr, len);
235 if (desc == (int) stdin)
238 if (desc != sourcedesc)
244 if (sourceleft-- <= 0)
249 lseek (int desc, int pos)
252 if (desc != sourcedesc)
258 if (pos < 0 || pos > sourcesize)
264 sourceptr = sourcebeg + pos;
265 sourceleft = sourcesize - pos;
268 /* Output in kdb can go only to the terminal, so the stream
269 specified may be ignored. */
271 printf (int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9)
274 sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
275 display_string (buffer);
278 fprintf (int ign, int a1, int a2, int a3, int a4, int a5, int a6, int a7,
282 sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
283 display_string (buffer);
286 fwrite (char *buf, int numelts, int size, int stream)
288 int i = numelts * size;
290 fputc (*buf++, stream);
293 fputc (int c, int ign)
298 display_string (buf);
301 /* sprintf refers to this, but loading this from the
302 library would cause fflush to be loaded from it too.
303 In fact there should be no need to call this (I hope). */
307 error ("_flsbuf was actually called.");
314 /* Entries into core and inflow, needed only to make things link ok. */
316 exec_file_command (void)
320 core_file_command (void)
325 get_exec_file (int err)
327 /* Makes one printout look reasonable; value does not matter otherwise. */
331 /* Nonzero if there is a core file. */
333 have_core_file_p (void)
340 inferior_ptid = null_ptid;
343 terminal_inferior (void)
351 terminal_init_inferior (void)
355 write_inferior_register (void)
359 read_inferior_register (void)
363 read_memory (CORE_ADDR memaddr, char *myaddr, int len)
365 memcpy (myaddr, memaddr, len);
368 /* Always return 0 indicating success. */
370 write_memory (CORE_ADDR memaddr, char *myaddr, int len)
372 memcpy (memaddr, myaddr, len);
376 static REGISTER_TYPE saved_regs[NUM_REGS];
379 read_register (int regno)
381 if (regno < 0 || regno >= NUM_REGS)
382 error ("Register number %d out of range.", regno);
383 return saved_regs[regno];
387 write_register (int regno, REGISTER_TYPE value)
389 if (regno < 0 || regno >= NUM_REGS)
390 error ("Register number %d out of range.", regno);
391 saved_regs[regno] = value;
394 /* System calls needed in relation to running the "inferior". */
398 /* Just appear to "succeed". Say the inferior's pid is 1. */
402 /* These are called by code that normally runs in the inferior
403 that has just been forked. That code never runs, when standalone,
404 and these definitions are so it will link without errors. */
422 /* Malloc calls these. */
424 malloc_warning (char *str)
426 printf ("\n%s.\n\n", str);
435 if (next_free + amount > memory_limit)
438 return next_free - amount;
441 /* Various ways malloc might ask where end of memory is. */
452 return memory_limit - next_free;
455 getrlimit (struct rlimit *addr)
457 addr->rlim_cur = memory_limit - next_free;
460 /* Context switching to and from program being debugged. */
462 /* GDB calls here to run the user program.
463 The frame pointer for this function is saved in
464 gdb_stack by save_frame_pointer; then we restore
465 all of the user program's registers, including PC and PS. */
467 static int fault_code;
468 static REGISTER_TYPE gdb_stack;
472 REGISTER_TYPE restore[NUM_REGS];
475 save_frame_pointer ();
477 memcpy (restore, saved_regs, sizeof restore);
479 /* Control does not drop through here! */
482 save_frame_pointer (CORE_ADDR val)
487 /* Fault handlers call here, running in the user program stack.
488 They must first push a fault code,
489 old PC, old PS, and any other info about the fault.
490 The exact format is machine-dependent and is known only
491 in the definition of PUSH_REGISTERS. */
495 /* Transfer all registers and fault code to the stack
496 in canonical order: registers in order of GDB register number,
497 followed by fault code. */
500 /* Transfer them to saved_regs and fault_code. */
504 /* Control does not reach here */
509 CORE_ADDR new_fp = gdb_stack;
510 /* Switch to GDB's stack */
512 /* Return from the function `resume'. */
515 /* Assuming register contents and fault code have been pushed on the stack as
516 arguments to this function, copy them into the standard place
517 for the program's registers while GDB is running. */
519 save_registers (int firstreg)
521 memcpy (saved_regs, &firstreg, sizeof saved_regs);
522 fault_code = (&firstreg)[NUM_REGS];
525 /* Store into the structure such as `wait' would return
526 the information on why the program faulted,
527 converted into a machine-independent signal number. */
529 static int fault_table[] = FAULT_TABLE;
534 WSETSTOP (*w, fault_table[fault_code / FAULT_CODE_UNITS]);
535 return PIDGET (inferior_ptid);
538 /* Allocate a big space in which files for kdb to read will be stored.
539 Whatever is left is where malloc can allocate storage.
541 Initialize it, so that there will be space in the executable file
542 for it. Then the files can be put into kdb by writing them into
543 kdb's executable file. */
545 /* The default size is as much space as we expect to be available
549 #define HEAP_SIZE 400000
552 char heap[HEAP_SIZE] =
556 #define STACK_SIZE 100000
559 int kdb_stack_beg[STACK_SIZE / sizeof (int)];
562 _initialize_standalone (void)
566 /* Find start of data on files. */
570 /* Find the end of the data on files. */
572 for (next = files_start; *(int *) next; next += *(int *) next)
576 /* That is where free storage starts for sbrk to give out. */
579 memory_limit = heap + sizeof heap;