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