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