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