> * gdb.texinfo (hbreak, watch): Fix typo, "date" -> "data".
[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 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
6a6a552a 144 for (next = files_start; * (int *) next; next += * (int *) next)
dd3b648e 145 {
2e4964ad 146 if (!STRCMP (next + 4, filename))
dd3b648e
RP
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
a6e0dae9 215 memcpy (destptr, sourceptr, len);
dd3b648e
RP
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
a6e0dae9 232 memcpy (bufp, sourceptr, len);
dd3b648e
RP
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
7919c3ed
JG
340/* Nonzero if there is a core file. */
341
dd3b648e
RP
342have_core_file_p ()
343{
344 return 0;
345}
346
347kill_command ()
348{
349 inferior_pid = 0;
350}
351
352terminal_inferior ()
353{}
354
355terminal_ours ()
356{}
357
358terminal_init_inferior ()
359{}
360
361write_inferior_register ()
362{}
363
364read_inferior_register ()
365{}
366
367read_memory (memaddr, myaddr, len)
368 CORE_ADDR memaddr;
369 char *myaddr;
370 int len;
371{
a6e0dae9 372 memcpy (myaddr, memaddr, len);
dd3b648e
RP
373}
374
375/* Always return 0 indicating success. */
376
377write_memory (memaddr, myaddr, len)
378 CORE_ADDR memaddr;
379 char *myaddr;
380 int len;
381{
a6e0dae9 382 memcpy (memaddr, myaddr, len);
dd3b648e
RP
383 return 0;
384}
385
386static REGISTER_TYPE saved_regs[NUM_REGS];
387
388REGISTER_TYPE
389read_register (regno)
390 int regno;
391{
392 if (regno < 0 || regno >= NUM_REGS)
393 error ("Register number %d out of range.", regno);
394 return saved_regs[regno];
395}
396
397void
398write_register (regno, value)
399 int regno;
400 REGISTER_TYPE value;
401{
402 if (regno < 0 || regno >= NUM_REGS)
403 error ("Register number %d out of range.", regno);
404 saved_regs[regno] = value;
405}
406\f
407/* System calls needed in relation to running the "inferior". */
408
409vfork ()
410{
411 /* Just appear to "succeed". Say the inferior's pid is 1. */
412 return 1;
413}
414
415/* These are called by code that normally runs in the inferior
416 that has just been forked. That code never runs, when standalone,
417 and these definitions are so it will link without errors. */
418
419ptrace ()
420{}
421
422setpgrp ()
423{}
424
425execle ()
426{}
427
428_exit ()
429{}
430\f
431/* Malloc calls these. */
432
433malloc_warning (str)
434 char *str;
435{
436 printf ("\n%s.\n\n", str);
437}
438
439char *next_free;
440char *memory_limit;
441
442char *
443sbrk (amount)
444 int amount;
445{
446 if (next_free + amount > memory_limit)
447 return (char *) -1;
448 next_free += amount;
449 return next_free - amount;
450}
451
452/* Various ways malloc might ask where end of memory is. */
453
454char *
455ulimit ()
456{
457 return memory_limit;
458}
459
460int
461vlimit ()
462{
463 return memory_limit - next_free;
464}
465
466getrlimit (addr)
467 struct rlimit *addr;
468{
469 addr->rlim_cur = memory_limit - next_free;
470}
471\f
472/* Context switching to and from program being debugged. */
473
474/* GDB calls here to run the user program.
475 The frame pointer for this function is saved in
476 gdb_stack by save_frame_pointer; then we restore
477 all of the user program's registers, including PC and PS. */
478
479static int fault_code;
480static REGISTER_TYPE gdb_stack;
481
482resume ()
483{
484 REGISTER_TYPE restore[NUM_REGS];
485
486 PUSH_FRAME_PTR;
487 save_frame_pointer ();
488
a6e0dae9 489 memcpy (restore, saved_regs, sizeof restore);
dd3b648e
RP
490 POP_REGISTERS;
491 /* Control does not drop through here! */
492}
493
494save_frame_pointer (val)
495 CORE_ADDR val;
496{
497 gdb_stack = val;
498}
499
500/* Fault handlers call here, running in the user program stack.
501 They must first push a fault code,
502 old PC, old PS, and any other info about the fault.
503 The exact format is machine-dependent and is known only
504 in the definition of PUSH_REGISTERS. */
505
506fault ()
507{
508 /* Transfer all registers and fault code to the stack
509 in canonical order: registers in order of GDB register number,
510 followed by fault code. */
511 PUSH_REGISTERS;
512
513 /* Transfer them to saved_regs and fault_code. */
514 save_registers ();
515
516 restore_gdb ();
517 /* Control does not reach here */
518}
519
520restore_gdb ()
521{
522 CORE_ADDR new_fp = gdb_stack;
523 /* Switch to GDB's stack */
524 POP_FRAME_PTR;
525 /* Return from the function `resume'. */
526}
527
528/* Assuming register contents and fault code have been pushed on the stack as
529 arguments to this function, copy them into the standard place
530 for the program's registers while GDB is running. */
531
532save_registers (firstreg)
533 int firstreg;
534{
a6e0dae9 535 memcpy (saved_regs, &firstreg, sizeof saved_regs);
dd3b648e
RP
536 fault_code = (&firstreg)[NUM_REGS];
537}
538
539/* Store into the structure such as `wait' would return
540 the information on why the program faulted,
541 converted into a machine-independent signal number. */
542
543static int fault_table[] = FAULT_TABLE;
544
545int
546wait (w)
547 WAITTYPE *w;
548{
549 WSETSTOP (*w, fault_table[fault_code / FAULT_CODE_UNITS]);
550 return inferior_pid;
551}
552\f
553/* Allocate a big space in which files for kdb to read will be stored.
554 Whatever is left is where malloc can allocate storage.
555
556 Initialize it, so that there will be space in the executable file
557 for it. Then the files can be put into kdb by writing them into
558 kdb's executable file. */
559
560/* The default size is as much space as we expect to be available
561 for kdb to use! */
562
563#ifndef HEAP_SIZE
564#define HEAP_SIZE 400000
565#endif
566
567char heap[HEAP_SIZE] = {0};
568
569#ifndef STACK_SIZE
570#define STACK_SIZE 100000
571#endif
572
573int kdb_stack_beg[STACK_SIZE / sizeof (int)];
574int kdb_stack_end;
575
576_initialize_standalone ()
577{
578 register char *next;
579
580 /* Find start of data on files. */
581
582 files_start = heap;
583
584 /* Find the end of the data on files. */
585
6a6a552a 586 for (next = files_start; * (int *) next; next += * (int *) next) {}
dd3b648e
RP
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.372364 seconds and 4 git commands to generate.