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