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