1f334d93marcel/* Interface to bare machine for GDB running as kernel debugger.
2f334d93marcel
3f334d93marcel   Copyright 1986, 1989, 1991, 1992, 1993, 1995, 1996, 2000, 2001,
4f334d93marcel   2003 Free Software Foundation, Inc.
5f334d93marcel
6f334d93marcel   This file is part of GDB.
7f334d93marcel
8f334d93marcel   This program is free software; you can redistribute it and/or modify
9f334d93marcel   it under the terms of the GNU General Public License as published by
10f334d93marcel   the Free Software Foundation; either version 2 of the License, or
11f334d93marcel   (at your option) any later version.
12f334d93marcel
13f334d93marcel   This program is distributed in the hope that it will be useful,
14f334d93marcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
15f334d93marcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16f334d93marcel   GNU General Public License for more details.
17f334d93marcel
18f334d93marcel   You should have received a copy of the GNU General Public License
19f334d93marcel   along with this program; if not, write to the Free Software
20f334d93marcel   Foundation, Inc., 59 Temple Place - Suite 330,
21f334d93marcel   Boston, MA 02111-1307, USA.  */
22f334d93marcel
23f334d93marcel#include <stdio.h>
24f334d93marcel#include <sys/ioctl.h>
25f334d93marcel#include <errno.h>
26f334d93marcel#include <sys/types.h>
27f334d93marcel#include "gdb_stat.h"
28f334d93marcel
29f334d93marcel#if defined (SIGTSTP) && defined (SIGIO)
30f334d93marcel#include <sys/time.h>
31f334d93marcel#include <sys/resource.h>
32f334d93marcel#endif /* SIGTSTP and SIGIO defined (must be 4.2) */
33f334d93marcel
34f334d93marcel#include "defs.h"
35f334d93marcel#include <signal.h>
36f334d93marcel#include "symtab.h"
37f334d93marcel#include "frame.h"
38f334d93marcel#include "inferior.h"
39f334d93marcel#include "gdb_wait.h"
40f334d93marcel
41f334d93marcel
42f334d93marcel/* Random system calls, mostly no-ops to prevent link problems  */
43f334d93marcel
44f334d93marcelioctl (int desc, int code, int arg)
45f334d93marcel{
46f334d93marcel}
47f334d93marcel
48f334d93marcelint (*signal ()) ()
49f334d93marcel{
50f334d93marcel}
51f334d93marcel
52f334d93marcelkill (void)
53f334d93marcel{
54f334d93marcel}
55f334d93marcel
56f334d93marcelgetpid (void)
57f334d93marcel{
58f334d93marcel  return 0;
59f334d93marcel}
60f334d93marcel
61f334d93marcelsigsetmask (void)
62f334d93marcel{
63f334d93marcel}
64f334d93marcel
65f334d93marcelchdir (void)
66f334d93marcel{
67f334d93marcel}
68f334d93marcel
69f334d93marcelchar *
70f334d93marcelgetcwd (char *buf, unsigned int len)
71f334d93marcel{
72f334d93marcel  buf[0] = '/';
73f334d93marcel  buf[1] = 0;
74f334d93marcel  return buf;
75f334d93marcel}
76f334d93marcel
77f334d93marcel/* Used to check for existence of .gdbinit.  Say no.  */
78f334d93marcel
79f334d93marcelaccess (void)
80f334d93marcel{
81f334d93marcel  return -1;
82f334d93marcel}
83f334d93marcel
84f334d93marcelexit (void)
85f334d93marcel{
86f334d93marcel  error ("Fatal error; restarting.");
87f334d93marcel}
88f334d93marcel
89f334d93marcel/* Reading "files".  The contents of some files are written into kdb's
90f334d93marcel   data area before it is run.  These files are used to contain the
91f334d93marcel   symbol table for kdb to load, and the source files (in case the
92f334d93marcel   kdb user wants to print them).  The symbols are stored in a file
93f334d93marcel   named "kdb-symbols" in a.out format (except that all the text and
94f334d93marcel   data have been stripped to save room).
95f334d93marcel
96f334d93marcel   The files are stored in the following format:
97f334d93marcel   int     number of bytes of data for this file, including these four.
98f334d93marcel   char[]  name of the file, ending with a null.
99f334d93marcel   padding to multiple of 4 boundary.
100f334d93marcel   char[]  file contents.  The length can be deduced from what was
101f334d93marcel   specified before.  There is no terminating null here.
102f334d93marcel
103f334d93marcel   If the int at the front is zero, it means there are no more files.
104f334d93marcel
105f334d93marcel   Opening a file in kdb returns a nonzero value to indicate success,
106f334d93marcel   but the value does not matter.  Only one file can be open, and only
107f334d93marcel   for reading.  All the primitives for input from the file know
108f334d93marcel   which file is open and ignore what is specified for the descriptor
109f334d93marcel   or for the stdio stream.
110f334d93marcel
111f334d93marcel   Input with fgetc can be done either on the file that is open
112f334d93marcel   or on stdin (which reads from the terminal through tty_input ()  */
113f334d93marcel
114f334d93marcel/* Address of data for the files stored in format described above.  */
115f334d93marcelchar *files_start;
116f334d93marcel
117f334d93marcel/* The file stream currently open:  */
118f334d93marcel
119f334d93marcelchar *sourcebeg;		/* beginning of contents */
120f334d93marcelint sourcesize;			/* size of contents */
121f334d93marcelchar *sourceptr;		/* current read pointer */
122f334d93marcelint sourceleft;			/* number of bytes to eof */
123f334d93marcel
124f334d93marcel/* "descriptor" for the file now open.
125f334d93marcel   Incremented at each close.
126f334d93marcel   If specified descriptor does not match this,
127f334d93marcel   it means the program is trying to use a closed descriptor.
128f334d93marcel   We report an error for that.  */
129f334d93marcel
130f334d93marcelint sourcedesc;
131f334d93marcel
132f334d93marcelopen (char *filename, int modes)
133f334d93marcel{
134f334d93marcel  char *next;
135f334d93marcel
136f334d93marcel  if (modes)
137f334d93marcel    {
138f334d93marcel      errno = EROFS;
139f334d93marcel      return -1;
140f334d93marcel    }
141f334d93marcel
142f334d93marcel  if (sourceptr)
143f334d93marcel    {
144f334d93marcel      errno = EMFILE;
145f334d93marcel      return -1;
146f334d93marcel    }
147f334d93marcel
148f334d93marcel  for (next = files_start; *(int *) next; next += *(int *) next)
149f334d93marcel    {
150f334d93marcel      if (!strcmp (next + 4, filename))
151f334d93marcel	{
152f334d93marcel	  sourcebeg = next + 4 + strlen (next + 4) + 1;
153f334d93marcel	  sourcebeg = (char *) (((int) sourcebeg + 3) & (-4));
154f334d93marcel	  sourceptr = sourcebeg;
155f334d93marcel	  sourcesize = next + *(int *) next - sourceptr;
156f334d93marcel	  sourceleft = sourcesize;
157f334d93marcel	  return sourcedesc;
158f334d93marcel	}
159f334d93marcel    }
160f334d93marcel  return 0;
161f334d93marcel}
162f334d93marcel
163f334d93marcelclose (int desc)
164f334d93marcel{
165f334d93marcel  sourceptr = 0;
166f334d93marcel  sourcedesc++;
167f334d93marcel  /* Don't let sourcedesc get big enough to be confused with stdin.  */
168f334d93marcel  if (sourcedesc == 100)
169f334d93marcel    sourcedesc = 5;
170f334d93marcel}
171f334d93marcel
172f334d93marcelFILE *
173f334d93marcelfopen (char *filename, char *modes)
174f334d93marcel{
175f334d93marcel  return (FILE *) open (filename, *modes == 'w');
176f334d93marcel}
177f334d93marcel
178f334d93marcelFILE *
179f334d93marcelfdopen (int desc)
180f334d93marcel{
181f334d93marcel  return (FILE *) desc;
182f334d93marcel}
183f334d93marcel
184f334d93marcelfclose (int desc)
185f334d93marcel{
186f334d93marcel  close (desc);
187f334d93marcel}
188f334d93marcel
189f334d93marcelfstat (int desc, struct stat *statbuf)
190f334d93marcel{
191f334d93marcel  if (desc != sourcedesc)
192f334d93marcel    {
193f334d93marcel      errno = EBADF;
194f334d93marcel      return -1;
195f334d93marcel    }
196f334d93marcel  statbuf->st_size = sourcesize;
197f334d93marcel}
198f334d93marcel
199f334d93marcelmyread (int desc, char *destptr, int size, char *filename)
200f334d93marcel{
201f334d93marcel  int len = min (sourceleft, size);
202f334d93marcel
203f334d93marcel  if (desc != sourcedesc)
204f334d93marcel    {
205f334d93marcel      errno = EBADF;
206f334d93marcel      return -1;
207f334d93marcel    }
208f334d93marcel
209f334d93marcel  memcpy (destptr, sourceptr, len);
210f334d93marcel  sourceleft -= len;
211f334d93marcel  return len;
212f334d93marcel}
213f334d93marcel
214f334d93marcelint
215f334d93marcelfread (int bufp, int numelts, int eltsize, int stream)
216f334d93marcel{
217f334d93marcel  int elts = min (numelts, sourceleft / eltsize);
218f334d93marcel  int len = elts * eltsize;
219f334d93marcel
220f334d93marcel  if (stream != sourcedesc)
221f334d93marcel    {
222f334d93marcel      errno = EBADF;
223f334d93marcel      return -1;
224f334d93marcel    }
225f334d93marcel
226f334d93marcel  memcpy (bufp, sourceptr, len);
227f334d93marcel  sourceleft -= len;
228f334d93marcel  return elts;
229f334d93marcel}
230f334d93marcel
231f334d93marcelint
232f334d93marcelfgetc (int desc)
233f334d93marcel{
234f334d93marcel
235f334d93marcel  if (desc == (int) stdin)
236f334d93marcel    return tty_input ();
237f334d93marcel
238f334d93marcel  if (desc != sourcedesc)
239f334d93marcel    {
240f334d93marcel      errno = EBADF;
241f334d93marcel      return -1;
242f334d93marcel    }
243f334d93marcel
244f334d93marcel  if (sourceleft-- <= 0)
245f334d93marcel    return EOF;
246f334d93marcel  return *sourceptr++;
247f334d93marcel}
248f334d93marcel
249f334d93marcellseek (int desc, int pos)
250f334d93marcel{
251f334d93marcel
252f334d93marcel  if (desc != sourcedesc)
253f334d93marcel    {
254f334d93marcel      errno = EBADF;
255f334d93marcel      return -1;
256f334d93marcel    }
257f334d93marcel
258f334d93marcel  if (pos < 0 || pos > sourcesize)
259f334d93marcel    {
260f334d93marcel      errno = EINVAL;
261f334d93marcel      return -1;
262f334d93marcel    }
263f334d93marcel
264f334d93marcel  sourceptr = sourcebeg + pos;
265f334d93marcel  sourceleft = sourcesize - pos;
266f334d93marcel}
267f334d93marcel
268f334d93marcel/* Output in kdb can go only to the terminal, so the stream
269f334d93marcel   specified may be ignored.  */
270f334d93marcel
271f334d93marcelprintf (int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9)
272f334d93marcel{
273f334d93marcel  char buffer[1024];
274f334d93marcel  sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
275f334d93marcel  display_string (buffer);
276f334d93marcel}
277f334d93marcel
278f334d93marcelfprintf (int ign, int a1, int a2, int a3, int a4, int a5, int a6, int a7,
279f334d93marcel	 int a8, int a9)
280f334d93marcel{
281f334d93marcel  char buffer[1024];
282f334d93marcel  sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
283f334d93marcel  display_string (buffer);
284f334d93marcel}
285f334d93marcel
286f334d93marcelfwrite (char *buf, int numelts, int size, int stream)
287f334d93marcel{
288f334d93marcel  int i = numelts * size;
289f334d93marcel  while (i-- > 0)
290f334d93marcel    fputc (*buf++, stream);
291f334d93marcel}
292f334d93marcel
293f334d93marcelfputc (int c, int ign)
294f334d93marcel{
295f334d93marcel  char buf[2];
296f334d93marcel  buf[0] = c;
297f334d93marcel  buf[1] = 0;
298f334d93marcel  display_string (buf);
299f334d93marcel}
300f334d93marcel
301f334d93marcel/* sprintf refers to this, but loading this from the
302f334d93marcel   library would cause fflush to be loaded from it too.
303f334d93marcel   In fact there should be no need to call this (I hope).  */
304f334d93marcel
305f334d93marcel_flsbuf (void)
306f334d93marcel{
307f334d93marcel  error ("_flsbuf was actually called.");
308f334d93marcel}
309f334d93marcel
310f334d93marcelfflush (int ign)
311f334d93marcel{
312f334d93marcel}
313f334d93marcel
314f334d93marcel/* Entries into core and inflow, needed only to make things link ok.  */
315f334d93marcel
316f334d93marcelexec_file_command (void)
317f334d93marcel{
318f334d93marcel}
319f334d93marcel
320f334d93marcelcore_file_command (void)
321f334d93marcel{
322f334d93marcel}
323f334d93marcel
324f334d93marcelchar *
325f334d93marcelget_exec_file (int err)
326f334d93marcel{
327f334d93marcel  /* Makes one printout look reasonable; value does not matter otherwise.  */
328f334d93marcel  return "run";
329f334d93marcel}
330f334d93marcel
331f334d93marcel/* Nonzero if there is a core file.  */
332f334d93marcel
333f334d93marcelhave_core_file_p (void)
334f334d93marcel{
335f334d93marcel  return 0;
336f334d93marcel}
337f334d93marcel
338f334d93marcelkill_command (void)
339f334d93marcel{
340f334d93marcel  inferior_ptid = null_ptid;
341f334d93marcel}
342f334d93marcel
343f334d93marcelterminal_inferior (void)
344f334d93marcel{
345f334d93marcel}
346f334d93marcel
347f334d93marcelterminal_ours (void)
348f334d93marcel{
349f334d93marcel}
350f334d93marcel
351f334d93marcelterminal_init_inferior (void)
352f334d93marcel{
353f334d93marcel}
354f334d93marcel
355f334d93marcelwrite_inferior_register (void)
356f334d93marcel{
357f334d93marcel}
358f334d93marcel
359f334d93marcelread_inferior_register (void)
360f334d93marcel{
361f334d93marcel}
362f334d93marcel
363f334d93marcelread_memory (CORE_ADDR memaddr, char *myaddr, int len)
364f334d93marcel{
365f334d93marcel  memcpy (myaddr, memaddr, len);
366f334d93marcel}
367f334d93marcel
368f334d93marcel/* Always return 0 indicating success.  */
369f334d93marcel
370f334d93marcelwrite_memory (CORE_ADDR memaddr, char *myaddr, int len)
371f334d93marcel{
372f334d93marcel  memcpy (memaddr, myaddr, len);
373f334d93marcel  return 0;
374f334d93marcel}
375f334d93marcel
376f334d93marcelstatic REGISTER_TYPE saved_regs[NUM_REGS];
377f334d93marcel
378f334d93marcelREGISTER_TYPE
379f334d93marcelread_register (int regno)
380f334d93marcel{
381f334d93marcel  if (regno < 0 || regno >= NUM_REGS)
382f334d93marcel    error ("Register number %d out of range.", regno);
383f334d93marcel  return saved_regs[regno];
384f334d93marcel}
385f334d93marcel
386f334d93marcelvoid
387f334d93marcelwrite_register (int regno, REGISTER_TYPE value)
388f334d93marcel{
389f334d93marcel  if (regno < 0 || regno >= NUM_REGS)
390f334d93marcel    error ("Register number %d out of range.", regno);
391f334d93marcel  saved_regs[regno] = value;
392f334d93marcel}
393f334d93marcel
394f334d93marcel/* System calls needed in relation to running the "inferior".  */
395f334d93marcel
396f334d93marcelvfork (void)
397f334d93marcel{
398f334d93marcel  /* Just appear to "succeed".  Say the inferior's pid is 1.  */
399f334d93marcel  return 1;
400f334d93marcel}
401f334d93marcel
402f334d93marcel/* These are called by code that normally runs in the inferior
403f334d93marcel   that has just been forked.  That code never runs, when standalone,
404f334d93marcel   and these definitions are so it will link without errors.  */
405f334d93marcel
406f334d93marcelptrace (void)
407f334d93marcel{
408f334d93marcel}
409f334d93marcel
410f334d93marcelsetpgrp (void)
411f334d93marcel{
412f334d93marcel}
413f334d93marcel
414f334d93marcelexecle (void)
415f334d93marcel{
416f334d93marcel}
417f334d93marcel
418f334d93marcel_exit (void)
419f334d93marcel{
420f334d93marcel}
421f334d93marcel
422f334d93marcel/* Malloc calls these.  */
423f334d93marcel
424f334d93marcelmalloc_warning (char *str)
425f334d93marcel{
426f334d93marcel  printf ("\n%s.\n\n", str);
427f334d93marcel}
428f334d93marcel
429f334d93marcelchar *next_free;
430f334d93marcelchar *memory_limit;
431f334d93marcel
432f334d93marcelchar *
433f334d93marcelsbrk (int amount)
434f334d93marcel{
435f334d93marcel  if (next_free + amount > memory_limit)
436f334d93marcel    return (char *) -1;
437f334d93marcel  next_free += amount;
438f334d93marcel  return next_free - amount;
439f334d93marcel}
440f334d93marcel
441f334d93marcel/* Various ways malloc might ask where end of memory is.  */
442f334d93marcel
443f334d93marcelchar *
444f334d93marcelulimit (void)
445f334d93marcel{
446f334d93marcel  return memory_limit;
447f334d93marcel}
448f334d93marcel
449f334d93marcelint
450f334d93marcelvlimit (void)
451f334d93marcel{
452f334d93marcel  return memory_limit - next_free;
453f334d93marcel}
454f334d93marcel
455f334d93marcelgetrlimit (struct rlimit *addr)
456f334d93marcel{
457f334d93marcel  addr->rlim_cur = memory_limit - next_free;
458f334d93marcel}
459f334d93marcel
460f334d93marcel/* Context switching to and from program being debugged.  */
461f334d93marcel
462f334d93marcel/* GDB calls here to run the user program.
463f334d93marcel   The frame pointer for this function is saved in
464f334d93marcel   gdb_stack by save_frame_pointer; then we restore
465f334d93marcel   all of the user program's registers, including PC and PS.  */
466f334d93marcel
467f334d93marcelstatic int fault_code;
468f334d93marcelstatic REGISTER_TYPE gdb_stack;
469f334d93marcel
470f334d93marcelresume (void)
471f334d93marcel{
472f334d93marcel  REGISTER_TYPE restore[NUM_REGS];
473f334d93marcel
474f334d93marcel  PUSH_FRAME_PTR;
475f334d93marcel  save_frame_pointer ();
476f334d93marcel
477f334d93marcel  memcpy (restore, saved_regs, sizeof restore);
478f334d93marcel  POP_REGISTERS;
479f334d93marcel  /* Control does not drop through here!  */
480f334d93marcel}
481f334d93marcel
482f334d93marcelsave_frame_pointer (CORE_ADDR val)
483f334d93marcel{
484f334d93marcel  gdb_stack = val;
485f334d93marcel}
486f334d93marcel
487f334d93marcel/* Fault handlers call here, running in the user program stack.
488f334d93marcel   They must first push a fault code,
489f334d93marcel   old PC, old PS, and any other info about the fault.
490f334d93marcel   The exact format is machine-dependent and is known only
491f334d93marcel   in the definition of PUSH_REGISTERS.  */
492f334d93marcel
493f334d93marcelfault (void)
494f334d93marcel{
495f334d93marcel  /* Transfer all registers and fault code to the stack
496f334d93marcel     in canonical order: registers in order of GDB register number,
497f334d93marcel     followed by fault code.  */
498f334d93marcel  PUSH_REGISTERS;
499f334d93marcel
500f334d93marcel  /* Transfer them to saved_regs and fault_code.  */
501f334d93marcel  save_registers ();
502f334d93marcel
503f334d93marcel  restore_gdb ();
504f334d93marcel  /* Control does not reach here */
505f334d93marcel}
506f334d93marcel
507f334d93marcelrestore_gdb (void)
508f334d93marcel{
509f334d93marcel  CORE_ADDR new_fp = gdb_stack;
510f334d93marcel  /* Switch to GDB's stack  */
511f334d93marcel  POP_FRAME_PTR;
512f334d93marcel  /* Return from the function `resume'.  */
513f334d93marcel}
514f334d93marcel
515f334d93marcel/* Assuming register contents and fault code have been pushed on the stack as
516f334d93marcel   arguments to this function, copy them into the standard place
517f334d93marcel   for the program's registers while GDB is running.  */
518f334d93marcel
519f334d93marcelsave_registers (int firstreg)
520f334d93marcel{
521f334d93marcel  memcpy (saved_regs, &firstreg, sizeof saved_regs);
522f334d93marcel  fault_code = (&firstreg)[NUM_REGS];
523f334d93marcel}
524f334d93marcel
525f334d93marcel/* Store into the structure such as `wait' would return
526f334d93marcel   the information on why the program faulted,
527f334d93marcel   converted into a machine-independent signal number.  */
528f334d93marcel
529f334d93marcelstatic int fault_table[] = FAULT_TABLE;
530f334d93marcel
531f334d93marcelint
532f334d93marcelwait (WAITTYPE *w)
533f334d93marcel{
534f334d93marcel  WSETSTOP (*w, fault_table[fault_code / FAULT_CODE_UNITS]);
535f334d93marcel  return PIDGET (inferior_ptid);
536f334d93marcel}
537f334d93marcel
538f334d93marcel/* Allocate a big space in which files for kdb to read will be stored.
539f334d93marcel   Whatever is left is where malloc can allocate storage.
540f334d93marcel
541f334d93marcel   Initialize it, so that there will be space in the executable file
542f334d93marcel   for it.  Then the files can be put into kdb by writing them into
543f334d93marcel   kdb's executable file.  */
544f334d93marcel
545f334d93marcel/* The default size is as much space as we expect to be available
546f334d93marcel   for kdb to use!  */
547f334d93marcel
548f334d93marcel#ifndef HEAP_SIZE
549f334d93marcel#define HEAP_SIZE 400000
550f334d93marcel#endif
551f334d93marcel
552f334d93marcelchar heap[HEAP_SIZE] =
553f334d93marcel{0};
554f334d93marcel
555f334d93marcel#ifndef STACK_SIZE
556f334d93marcel#define STACK_SIZE 100000
557f334d93marcel#endif
558f334d93marcel
559f334d93marcelint kdb_stack_beg[STACK_SIZE / sizeof (int)];
560f334d93marcelint kdb_stack_end;
561f334d93marcel
562f334d93marcel_initialize_standalone (void)
563f334d93marcel{
564f334d93marcel  char *next;
565f334d93marcel
566f334d93marcel  /* Find start of data on files.  */
567f334d93marcel
568f334d93marcel  files_start = heap;
569f334d93marcel
570f334d93marcel  /* Find the end of the data on files.  */
571f334d93marcel
572f334d93marcel  for (next = files_start; *(int *) next; next += *(int *) next)
573f334d93marcel    {
574f334d93marcel    }
575f334d93marcel
576f334d93marcel  /* That is where free storage starts for sbrk to give out.  */
577f334d93marcel  next_free = next;
578f334d93marcel
579f334d93marcel  memory_limit = heap + sizeof heap;
580f334d93marcel}
581