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