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