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