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