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