1130812Smarcel/* Interface to bare machine for GDB running as kernel debugger.
2130812Smarcel
3130812Smarcel   Copyright 1986, 1989, 1991, 1992, 1993, 1995, 1996, 2000, 2001,
4130812Smarcel   2003 Free Software Foundation, Inc.
5130812Smarcel
6130812Smarcel   This file is part of GDB.
7130812Smarcel
8130812Smarcel   This program is free software; you can redistribute it and/or modify
9130812Smarcel   it under the terms of the GNU General Public License as published by
10130812Smarcel   the Free Software Foundation; either version 2 of the License, or
11130812Smarcel   (at your option) any later version.
12130812Smarcel
13130812Smarcel   This program is distributed in the hope that it will be useful,
14130812Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
15130812Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16130812Smarcel   GNU General Public License for more details.
17130812Smarcel
18130812Smarcel   You should have received a copy of the GNU General Public License
19130812Smarcel   along with this program; if not, write to the Free Software
20130812Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
21130812Smarcel   Boston, MA 02111-1307, USA.  */
22130812Smarcel
23130812Smarcel#include <stdio.h>
24130812Smarcel#include <sys/ioctl.h>
25130812Smarcel#include <errno.h>
26130812Smarcel#include <sys/types.h>
27130812Smarcel#include "gdb_stat.h"
28130812Smarcel
29130812Smarcel#if defined (SIGTSTP) && defined (SIGIO)
30130812Smarcel#include <sys/time.h>
31130812Smarcel#include <sys/resource.h>
32130812Smarcel#endif /* SIGTSTP and SIGIO defined (must be 4.2) */
33130812Smarcel
34130812Smarcel#include "defs.h"
35130812Smarcel#include <signal.h>
36130812Smarcel#include "symtab.h"
37130812Smarcel#include "frame.h"
38130812Smarcel#include "inferior.h"
39130812Smarcel#include "gdb_wait.h"
40130812Smarcel
41130812Smarcel
42130812Smarcel/* Random system calls, mostly no-ops to prevent link problems  */
43130812Smarcel
44130812Smarcelioctl (int desc, int code, int arg)
45130812Smarcel{
46130812Smarcel}
47130812Smarcel
48130812Smarcelint (*signal ()) ()
49130812Smarcel{
50130812Smarcel}
51130812Smarcel
52130812Smarcelkill (void)
53130812Smarcel{
54130812Smarcel}
55130812Smarcel
56130812Smarcelgetpid (void)
57130812Smarcel{
58130812Smarcel  return 0;
59130812Smarcel}
60130812Smarcel
61130812Smarcelsigsetmask (void)
62130812Smarcel{
63130812Smarcel}
64130812Smarcel
65130812Smarcelchdir (void)
66130812Smarcel{
67130812Smarcel}
68130812Smarcel
69130812Smarcelchar *
70130812Smarcelgetcwd (char *buf, unsigned int len)
71130812Smarcel{
72130812Smarcel  buf[0] = '/';
73130812Smarcel  buf[1] = 0;
74130812Smarcel  return buf;
75130812Smarcel}
76130812Smarcel
77130812Smarcel/* Used to check for existence of .gdbinit.  Say no.  */
78130812Smarcel
79130812Smarcelaccess (void)
80130812Smarcel{
81130812Smarcel  return -1;
82130812Smarcel}
83130812Smarcel
84130812Smarcelexit (void)
85130812Smarcel{
86130812Smarcel  error ("Fatal error; restarting.");
87130812Smarcel}
88130812Smarcel
89130812Smarcel/* Reading "files".  The contents of some files are written into kdb's
90130812Smarcel   data area before it is run.  These files are used to contain the
91130812Smarcel   symbol table for kdb to load, and the source files (in case the
92130812Smarcel   kdb user wants to print them).  The symbols are stored in a file
93130812Smarcel   named "kdb-symbols" in a.out format (except that all the text and
94130812Smarcel   data have been stripped to save room).
95130812Smarcel
96130812Smarcel   The files are stored in the following format:
97130812Smarcel   int     number of bytes of data for this file, including these four.
98130812Smarcel   char[]  name of the file, ending with a null.
99130812Smarcel   padding to multiple of 4 boundary.
100130812Smarcel   char[]  file contents.  The length can be deduced from what was
101130812Smarcel   specified before.  There is no terminating null here.
102130812Smarcel
103130812Smarcel   If the int at the front is zero, it means there are no more files.
104130812Smarcel
105130812Smarcel   Opening a file in kdb returns a nonzero value to indicate success,
106130812Smarcel   but the value does not matter.  Only one file can be open, and only
107130812Smarcel   for reading.  All the primitives for input from the file know
108130812Smarcel   which file is open and ignore what is specified for the descriptor
109130812Smarcel   or for the stdio stream.
110130812Smarcel
111130812Smarcel   Input with fgetc can be done either on the file that is open
112130812Smarcel   or on stdin (which reads from the terminal through tty_input ()  */
113130812Smarcel
114130812Smarcel/* Address of data for the files stored in format described above.  */
115130812Smarcelchar *files_start;
116130812Smarcel
117130812Smarcel/* The file stream currently open:  */
118130812Smarcel
119130812Smarcelchar *sourcebeg;		/* beginning of contents */
120130812Smarcelint sourcesize;			/* size of contents */
121130812Smarcelchar *sourceptr;		/* current read pointer */
122130812Smarcelint sourceleft;			/* number of bytes to eof */
123130812Smarcel
124130812Smarcel/* "descriptor" for the file now open.
125130812Smarcel   Incremented at each close.
126130812Smarcel   If specified descriptor does not match this,
127130812Smarcel   it means the program is trying to use a closed descriptor.
128130812Smarcel   We report an error for that.  */
129130812Smarcel
130130812Smarcelint sourcedesc;
131130812Smarcel
132130812Smarcelopen (char *filename, int modes)
133130812Smarcel{
134130812Smarcel  char *next;
135130812Smarcel
136130812Smarcel  if (modes)
137130812Smarcel    {
138130812Smarcel      errno = EROFS;
139130812Smarcel      return -1;
140130812Smarcel    }
141130812Smarcel
142130812Smarcel  if (sourceptr)
143130812Smarcel    {
144130812Smarcel      errno = EMFILE;
145130812Smarcel      return -1;
146130812Smarcel    }
147130812Smarcel
148130812Smarcel  for (next = files_start; *(int *) next; next += *(int *) next)
149130812Smarcel    {
150130812Smarcel      if (!strcmp (next + 4, filename))
151130812Smarcel	{
152130812Smarcel	  sourcebeg = next + 4 + strlen (next + 4) + 1;
153130812Smarcel	  sourcebeg = (char *) (((int) sourcebeg + 3) & (-4));
154130812Smarcel	  sourceptr = sourcebeg;
155130812Smarcel	  sourcesize = next + *(int *) next - sourceptr;
156130812Smarcel	  sourceleft = sourcesize;
157130812Smarcel	  return sourcedesc;
158130812Smarcel	}
159130812Smarcel    }
160130812Smarcel  return 0;
161130812Smarcel}
162130812Smarcel
163130812Smarcelclose (int desc)
164130812Smarcel{
165130812Smarcel  sourceptr = 0;
166130812Smarcel  sourcedesc++;
167130812Smarcel  /* Don't let sourcedesc get big enough to be confused with stdin.  */
168130812Smarcel  if (sourcedesc == 100)
169130812Smarcel    sourcedesc = 5;
170130812Smarcel}
171130812Smarcel
172130812SmarcelFILE *
173130812Smarcelfopen (char *filename, char *modes)
174130812Smarcel{
175130812Smarcel  return (FILE *) open (filename, *modes == 'w');
176130812Smarcel}
177130812Smarcel
178130812SmarcelFILE *
179130812Smarcelfdopen (int desc)
180130812Smarcel{
181130812Smarcel  return (FILE *) desc;
182130812Smarcel}
183130812Smarcel
184130812Smarcelfclose (int desc)
185130812Smarcel{
186130812Smarcel  close (desc);
187130812Smarcel}
188130812Smarcel
189130812Smarcelfstat (int desc, struct stat *statbuf)
190130812Smarcel{
191130812Smarcel  if (desc != sourcedesc)
192130812Smarcel    {
193130812Smarcel      errno = EBADF;
194130812Smarcel      return -1;
195130812Smarcel    }
196130812Smarcel  statbuf->st_size = sourcesize;
197130812Smarcel}
198130812Smarcel
199130812Smarcelmyread (int desc, char *destptr, int size, char *filename)
200130812Smarcel{
201130812Smarcel  int len = min (sourceleft, size);
202130812Smarcel
203130812Smarcel  if (desc != sourcedesc)
204130812Smarcel    {
205130812Smarcel      errno = EBADF;
206130812Smarcel      return -1;
207130812Smarcel    }
208130812Smarcel
209130812Smarcel  memcpy (destptr, sourceptr, len);
210130812Smarcel  sourceleft -= len;
211130812Smarcel  return len;
212130812Smarcel}
213130812Smarcel
214130812Smarcelint
215130812Smarcelfread (int bufp, int numelts, int eltsize, int stream)
216130812Smarcel{
217130812Smarcel  int elts = min (numelts, sourceleft / eltsize);
218130812Smarcel  int len = elts * eltsize;
219130812Smarcel
220130812Smarcel  if (stream != sourcedesc)
221130812Smarcel    {
222130812Smarcel      errno = EBADF;
223130812Smarcel      return -1;
224130812Smarcel    }
225130812Smarcel
226130812Smarcel  memcpy (bufp, sourceptr, len);
227130812Smarcel  sourceleft -= len;
228130812Smarcel  return elts;
229130812Smarcel}
230130812Smarcel
231130812Smarcelint
232130812Smarcelfgetc (int desc)
233130812Smarcel{
234130812Smarcel
235130812Smarcel  if (desc == (int) stdin)
236130812Smarcel    return tty_input ();
237130812Smarcel
238130812Smarcel  if (desc != sourcedesc)
239130812Smarcel    {
240130812Smarcel      errno = EBADF;
241130812Smarcel      return -1;
242130812Smarcel    }
243130812Smarcel
244130812Smarcel  if (sourceleft-- <= 0)
245130812Smarcel    return EOF;
246130812Smarcel  return *sourceptr++;
247130812Smarcel}
248130812Smarcel
249130812Smarcellseek (int desc, int pos)
250130812Smarcel{
251130812Smarcel
252130812Smarcel  if (desc != sourcedesc)
253130812Smarcel    {
254130812Smarcel      errno = EBADF;
255130812Smarcel      return -1;
256130812Smarcel    }
257130812Smarcel
258130812Smarcel  if (pos < 0 || pos > sourcesize)
259130812Smarcel    {
260130812Smarcel      errno = EINVAL;
261130812Smarcel      return -1;
262130812Smarcel    }
263130812Smarcel
264130812Smarcel  sourceptr = sourcebeg + pos;
265130812Smarcel  sourceleft = sourcesize - pos;
266130812Smarcel}
267130812Smarcel
268130812Smarcel/* Output in kdb can go only to the terminal, so the stream
269130812Smarcel   specified may be ignored.  */
270130812Smarcel
271130812Smarcelprintf (int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9)
272130812Smarcel{
273130812Smarcel  char buffer[1024];
274130812Smarcel  sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
275130812Smarcel  display_string (buffer);
276130812Smarcel}
277130812Smarcel
278130812Smarcelfprintf (int ign, int a1, int a2, int a3, int a4, int a5, int a6, int a7,
279130812Smarcel	 int a8, int a9)
280130812Smarcel{
281130812Smarcel  char buffer[1024];
282130812Smarcel  sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9);
283130812Smarcel  display_string (buffer);
284130812Smarcel}
285130812Smarcel
286130812Smarcelfwrite (char *buf, int numelts, int size, int stream)
287130812Smarcel{
288130812Smarcel  int i = numelts * size;
289130812Smarcel  while (i-- > 0)
290130812Smarcel    fputc (*buf++, stream);
291130812Smarcel}
292130812Smarcel
293130812Smarcelfputc (int c, int ign)
294130812Smarcel{
295130812Smarcel  char buf[2];
296130812Smarcel  buf[0] = c;
297130812Smarcel  buf[1] = 0;
298130812Smarcel  display_string (buf);
299130812Smarcel}
300130812Smarcel
301130812Smarcel/* sprintf refers to this, but loading this from the
302130812Smarcel   library would cause fflush to be loaded from it too.
303130812Smarcel   In fact there should be no need to call this (I hope).  */
304130812Smarcel
305130812Smarcel_flsbuf (void)
306130812Smarcel{
307130812Smarcel  error ("_flsbuf was actually called.");
308130812Smarcel}
309130812Smarcel
310130812Smarcelfflush (int ign)
311130812Smarcel{
312130812Smarcel}
313130812Smarcel
314130812Smarcel/* Entries into core and inflow, needed only to make things link ok.  */
315130812Smarcel
316130812Smarcelexec_file_command (void)
317130812Smarcel{
318130812Smarcel}
319130812Smarcel
320130812Smarcelcore_file_command (void)
321130812Smarcel{
322130812Smarcel}
323130812Smarcel
324130812Smarcelchar *
325130812Smarcelget_exec_file (int err)
326130812Smarcel{
327130812Smarcel  /* Makes one printout look reasonable; value does not matter otherwise.  */
328130812Smarcel  return "run";
329130812Smarcel}
330130812Smarcel
331130812Smarcel/* Nonzero if there is a core file.  */
332130812Smarcel
333130812Smarcelhave_core_file_p (void)
334130812Smarcel{
335130812Smarcel  return 0;
336130812Smarcel}
337130812Smarcel
338130812Smarcelkill_command (void)
339130812Smarcel{
340130812Smarcel  inferior_ptid = null_ptid;
341130812Smarcel}
342130812Smarcel
343130812Smarcelterminal_inferior (void)
344130812Smarcel{
345130812Smarcel}
346130812Smarcel
347130812Smarcelterminal_ours (void)
348130812Smarcel{
349130812Smarcel}
350130812Smarcel
351130812Smarcelterminal_init_inferior (void)
352130812Smarcel{
353130812Smarcel}
354130812Smarcel
355130812Smarcelwrite_inferior_register (void)
356130812Smarcel{
357130812Smarcel}
358130812Smarcel
359130812Smarcelread_inferior_register (void)
360130812Smarcel{
361130812Smarcel}
362130812Smarcel
363130812Smarcelread_memory (CORE_ADDR memaddr, char *myaddr, int len)
364130812Smarcel{
365130812Smarcel  memcpy (myaddr, memaddr, len);
366130812Smarcel}
367130812Smarcel
368130812Smarcel/* Always return 0 indicating success.  */
369130812Smarcel
370130812Smarcelwrite_memory (CORE_ADDR memaddr, char *myaddr, int len)
371130812Smarcel{
372130812Smarcel  memcpy (memaddr, myaddr, len);
373130812Smarcel  return 0;
374130812Smarcel}
375130812Smarcel
376130812Smarcelstatic REGISTER_TYPE saved_regs[NUM_REGS];
377130812Smarcel
378130812SmarcelREGISTER_TYPE
379130812Smarcelread_register (int regno)
380130812Smarcel{
381130812Smarcel  if (regno < 0 || regno >= NUM_REGS)
382130812Smarcel    error ("Register number %d out of range.", regno);
383130812Smarcel  return saved_regs[regno];
384130812Smarcel}
385130812Smarcel
386130812Smarcelvoid
387130812Smarcelwrite_register (int regno, REGISTER_TYPE value)
388130812Smarcel{
389130812Smarcel  if (regno < 0 || regno >= NUM_REGS)
390130812Smarcel    error ("Register number %d out of range.", regno);
391130812Smarcel  saved_regs[regno] = value;
392130812Smarcel}
393130812Smarcel
394130812Smarcel/* System calls needed in relation to running the "inferior".  */
395130812Smarcel
396130812Smarcelvfork (void)
397130812Smarcel{
398130812Smarcel  /* Just appear to "succeed".  Say the inferior's pid is 1.  */
399130812Smarcel  return 1;
400130812Smarcel}
401130812Smarcel
402130812Smarcel/* These are called by code that normally runs in the inferior
403130812Smarcel   that has just been forked.  That code never runs, when standalone,
404130812Smarcel   and these definitions are so it will link without errors.  */
405130812Smarcel
406130812Smarcelptrace (void)
407130812Smarcel{
408130812Smarcel}
409130812Smarcel
410130812Smarcelsetpgrp (void)
411130812Smarcel{
412130812Smarcel}
413130812Smarcel
414130812Smarcelexecle (void)
415130812Smarcel{
416130812Smarcel}
417130812Smarcel
418130812Smarcel_exit (void)
419130812Smarcel{
420130812Smarcel}
421130812Smarcel
422130812Smarcel/* Malloc calls these.  */
423130812Smarcel
424130812Smarcelmalloc_warning (char *str)
425130812Smarcel{
426130812Smarcel  printf ("\n%s.\n\n", str);
427130812Smarcel}
428130812Smarcel
429130812Smarcelchar *next_free;
430130812Smarcelchar *memory_limit;
431130812Smarcel
432130812Smarcelchar *
433130812Smarcelsbrk (int amount)
434130812Smarcel{
435130812Smarcel  if (next_free + amount > memory_limit)
436130812Smarcel    return (char *) -1;
437130812Smarcel  next_free += amount;
438130812Smarcel  return next_free - amount;
439130812Smarcel}
440130812Smarcel
441130812Smarcel/* Various ways malloc might ask where end of memory is.  */
442130812Smarcel
443130812Smarcelchar *
444130812Smarcelulimit (void)
445130812Smarcel{
446130812Smarcel  return memory_limit;
447130812Smarcel}
448130812Smarcel
449130812Smarcelint
450130812Smarcelvlimit (void)
451130812Smarcel{
452130812Smarcel  return memory_limit - next_free;
453130812Smarcel}
454130812Smarcel
455130812Smarcelgetrlimit (struct rlimit *addr)
456130812Smarcel{
457130812Smarcel  addr->rlim_cur = memory_limit - next_free;
458130812Smarcel}
459130812Smarcel
460130812Smarcel/* Context switching to and from program being debugged.  */
461130812Smarcel
462130812Smarcel/* GDB calls here to run the user program.
463130812Smarcel   The frame pointer for this function is saved in
464130812Smarcel   gdb_stack by save_frame_pointer; then we restore
465130812Smarcel   all of the user program's registers, including PC and PS.  */
466130812Smarcel
467130812Smarcelstatic int fault_code;
468130812Smarcelstatic REGISTER_TYPE gdb_stack;
469130812Smarcel
470130812Smarcelresume (void)
471130812Smarcel{
472130812Smarcel  REGISTER_TYPE restore[NUM_REGS];
473130812Smarcel
474130812Smarcel  PUSH_FRAME_PTR;
475130812Smarcel  save_frame_pointer ();
476130812Smarcel
477130812Smarcel  memcpy (restore, saved_regs, sizeof restore);
478130812Smarcel  POP_REGISTERS;
479130812Smarcel  /* Control does not drop through here!  */
480130812Smarcel}
481130812Smarcel
482130812Smarcelsave_frame_pointer (CORE_ADDR val)
483130812Smarcel{
484130812Smarcel  gdb_stack = val;
485130812Smarcel}
486130812Smarcel
487130812Smarcel/* Fault handlers call here, running in the user program stack.
488130812Smarcel   They must first push a fault code,
489130812Smarcel   old PC, old PS, and any other info about the fault.
490130812Smarcel   The exact format is machine-dependent and is known only
491130812Smarcel   in the definition of PUSH_REGISTERS.  */
492130812Smarcel
493130812Smarcelfault (void)
494130812Smarcel{
495130812Smarcel  /* Transfer all registers and fault code to the stack
496130812Smarcel     in canonical order: registers in order of GDB register number,
497130812Smarcel     followed by fault code.  */
498130812Smarcel  PUSH_REGISTERS;
499130812Smarcel
500130812Smarcel  /* Transfer them to saved_regs and fault_code.  */
501130812Smarcel  save_registers ();
502130812Smarcel
503130812Smarcel  restore_gdb ();
504130812Smarcel  /* Control does not reach here */
505130812Smarcel}
506130812Smarcel
507130812Smarcelrestore_gdb (void)
508130812Smarcel{
509130812Smarcel  CORE_ADDR new_fp = gdb_stack;
510130812Smarcel  /* Switch to GDB's stack  */
511130812Smarcel  POP_FRAME_PTR;
512130812Smarcel  /* Return from the function `resume'.  */
513130812Smarcel}
514130812Smarcel
515130812Smarcel/* Assuming register contents and fault code have been pushed on the stack as
516130812Smarcel   arguments to this function, copy them into the standard place
517130812Smarcel   for the program's registers while GDB is running.  */
518130812Smarcel
519130812Smarcelsave_registers (int firstreg)
520130812Smarcel{
521130812Smarcel  memcpy (saved_regs, &firstreg, sizeof saved_regs);
522130812Smarcel  fault_code = (&firstreg)[NUM_REGS];
523130812Smarcel}
524130812Smarcel
525130812Smarcel/* Store into the structure such as `wait' would return
526130812Smarcel   the information on why the program faulted,
527130812Smarcel   converted into a machine-independent signal number.  */
528130812Smarcel
529130812Smarcelstatic int fault_table[] = FAULT_TABLE;
530130812Smarcel
531130812Smarcelint
532130812Smarcelwait (WAITTYPE *w)
533130812Smarcel{
534130812Smarcel  WSETSTOP (*w, fault_table[fault_code / FAULT_CODE_UNITS]);
535130812Smarcel  return PIDGET (inferior_ptid);
536130812Smarcel}
537130812Smarcel
538130812Smarcel/* Allocate a big space in which files for kdb to read will be stored.
539130812Smarcel   Whatever is left is where malloc can allocate storage.
540130812Smarcel
541130812Smarcel   Initialize it, so that there will be space in the executable file
542130812Smarcel   for it.  Then the files can be put into kdb by writing them into
543130812Smarcel   kdb's executable file.  */
544130812Smarcel
545130812Smarcel/* The default size is as much space as we expect to be available
546130812Smarcel   for kdb to use!  */
547130812Smarcel
548130812Smarcel#ifndef HEAP_SIZE
549130812Smarcel#define HEAP_SIZE 400000
550130812Smarcel#endif
551130812Smarcel
552130812Smarcelchar heap[HEAP_SIZE] =
553130812Smarcel{0};
554130812Smarcel
555130812Smarcel#ifndef STACK_SIZE
556130812Smarcel#define STACK_SIZE 100000
557130812Smarcel#endif
558130812Smarcel
559130812Smarcelint kdb_stack_beg[STACK_SIZE / sizeof (int)];
560130812Smarcelint kdb_stack_end;
561130812Smarcel
562130812Smarcel_initialize_standalone (void)
563130812Smarcel{
564130812Smarcel  char *next;
565130812Smarcel
566130812Smarcel  /* Find start of data on files.  */
567130812Smarcel
568130812Smarcel  files_start = heap;
569130812Smarcel
570130812Smarcel  /* Find the end of the data on files.  */
571130812Smarcel
572130812Smarcel  for (next = files_start; *(int *) next; next += *(int *) next)
573130812Smarcel    {
574130812Smarcel    }
575130812Smarcel
576130812Smarcel  /* That is where free storage starts for sbrk to give out.  */
577130812Smarcel  next_free = next;
578130812Smarcel
579130812Smarcel  memory_limit = heap + sizeof heap;
580130812Smarcel}
581