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