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