Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Interface to bare machine for GDB running as kernel debugger. |
69517000 AC |
2 | |
3 | Copyright 1986, 1989, 1991, 1992, 1993, 1995, 1996, 2000, 2001, | |
4 | 2003 Free Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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. | |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b JM |
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. */ | |
c906108c SS |
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" | |
042be3a9 | 35 | #include <signal.h> |
c906108c SS |
36 | #include "symtab.h" |
37 | #include "frame.h" | |
38 | #include "inferior.h" | |
03f2053f | 39 | #include "gdb_wait.h" |
c906108c | 40 | \f |
c5aa993b | 41 | |
c906108c SS |
42 | /* Random system calls, mostly no-ops to prevent link problems */ |
43 | ||
fba45db2 | 44 | ioctl (int desc, int code, int arg) |
c5aa993b JM |
45 | { |
46 | } | |
c906108c | 47 | |
c5aa993b JM |
48 | int (*signal ()) () |
49 | { | |
50 | } | |
c906108c | 51 | |
fba45db2 | 52 | kill (void) |
c5aa993b JM |
53 | { |
54 | } | |
c906108c | 55 | |
fba45db2 | 56 | getpid (void) |
c906108c SS |
57 | { |
58 | return 0; | |
59 | } | |
60 | ||
fba45db2 | 61 | sigsetmask (void) |
c5aa993b JM |
62 | { |
63 | } | |
c906108c | 64 | |
fba45db2 | 65 | chdir (void) |
c5aa993b JM |
66 | { |
67 | } | |
c906108c SS |
68 | |
69 | char * | |
fba45db2 | 70 | getcwd (char *buf, unsigned int len) |
c906108c SS |
71 | { |
72 | buf[0] = '/'; | |
73 | buf[1] = 0; | |
74 | return buf; | |
75 | } | |
76 | ||
77 | /* Used to check for existence of .gdbinit. Say no. */ | |
78 | ||
fba45db2 | 79 | access (void) |
c906108c SS |
80 | { |
81 | return -1; | |
82 | } | |
83 | ||
fba45db2 | 84 | exit (void) |
c906108c SS |
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 | |
c5aa993b | 101 | specified before. There is no terminating null here. |
c906108c SS |
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. */ | |
115 | char *files_start; | |
116 | ||
117 | /* The file stream currently open: */ | |
118 | ||
119 | char *sourcebeg; /* beginning of contents */ | |
120 | int sourcesize; /* size of contents */ | |
121 | char *sourceptr; /* current read pointer */ | |
122 | int 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 | ||
130 | int sourcedesc; | |
131 | ||
fba45db2 | 132 | open (char *filename, int modes) |
c906108c | 133 | { |
52f0bd74 | 134 | char *next; |
c906108c SS |
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 | ||
c5aa993b | 148 | for (next = files_start; *(int *) next; next += *(int *) next) |
c906108c | 149 | { |
494b7ec9 | 150 | if (!strcmp (next + 4, filename)) |
c906108c SS |
151 | { |
152 | sourcebeg = next + 4 + strlen (next + 4) + 1; | |
153 | sourcebeg = (char *) (((int) sourcebeg + 3) & (-4)); | |
154 | sourceptr = sourcebeg; | |
c5aa993b | 155 | sourcesize = next + *(int *) next - sourceptr; |
c906108c SS |
156 | sourceleft = sourcesize; |
157 | return sourcedesc; | |
158 | } | |
159 | } | |
160 | return 0; | |
161 | } | |
162 | ||
fba45db2 | 163 | close (int desc) |
c906108c SS |
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 * | |
fba45db2 | 173 | fopen (char *filename, char *modes) |
c906108c SS |
174 | { |
175 | return (FILE *) open (filename, *modes == 'w'); | |
176 | } | |
177 | ||
178 | FILE * | |
fba45db2 | 179 | fdopen (int desc) |
c906108c SS |
180 | { |
181 | return (FILE *) desc; | |
182 | } | |
183 | ||
fba45db2 | 184 | fclose (int desc) |
c906108c SS |
185 | { |
186 | close (desc); | |
187 | } | |
188 | ||
fba45db2 | 189 | fstat (int desc, struct stat *statbuf) |
c906108c SS |
190 | { |
191 | if (desc != sourcedesc) | |
192 | { | |
193 | errno = EBADF; | |
194 | return -1; | |
195 | } | |
196 | statbuf->st_size = sourcesize; | |
197 | } | |
198 | ||
fba45db2 | 199 | myread (int desc, char *destptr, int size, char *filename) |
c906108c SS |
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 | ||
214 | int | |
fba45db2 | 215 | fread (int bufp, int numelts, int eltsize, int stream) |
c906108c | 216 | { |
52f0bd74 AC |
217 | int elts = min (numelts, sourceleft / eltsize); |
218 | int len = elts * eltsize; | |
c906108c SS |
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 | ||
231 | int | |
fba45db2 | 232 | fgetc (int desc) |
c906108c SS |
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 | ||
fba45db2 | 249 | lseek (int desc, int pos) |
c906108c SS |
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 | ||
fba45db2 | 271 | printf (int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9) |
c906108c SS |
272 | { |
273 | char buffer[1024]; | |
274 | sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9); | |
275 | display_string (buffer); | |
276 | } | |
277 | ||
fba45db2 KB |
278 | fprintf (int ign, int a1, int a2, int a3, int a4, int a5, int a6, int a7, |
279 | int a8, int a9) | |
c906108c SS |
280 | { |
281 | char buffer[1024]; | |
282 | sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9); | |
283 | display_string (buffer); | |
284 | } | |
285 | ||
aa1ee363 | 286 | fwrite (char *buf, int numelts, int size, int stream) |
c906108c | 287 | { |
52f0bd74 | 288 | int i = numelts * size; |
c906108c SS |
289 | while (i-- > 0) |
290 | fputc (*buf++, stream); | |
291 | } | |
292 | ||
fba45db2 | 293 | fputc (int c, int ign) |
c906108c SS |
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 | ||
fba45db2 | 305 | _flsbuf (void) |
c906108c SS |
306 | { |
307 | error ("_flsbuf was actually called."); | |
308 | } | |
309 | ||
fba45db2 | 310 | fflush (int ign) |
c906108c SS |
311 | { |
312 | } | |
313 | \f | |
314 | /* Entries into core and inflow, needed only to make things link ok. */ | |
315 | ||
fba45db2 | 316 | exec_file_command (void) |
c5aa993b JM |
317 | { |
318 | } | |
c906108c | 319 | |
fba45db2 | 320 | core_file_command (void) |
c5aa993b JM |
321 | { |
322 | } | |
c906108c SS |
323 | |
324 | char * | |
fba45db2 | 325 | get_exec_file (int err) |
c906108c SS |
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 | ||
fba45db2 | 333 | have_core_file_p (void) |
c906108c SS |
334 | { |
335 | return 0; | |
336 | } | |
337 | ||
fba45db2 | 338 | kill_command (void) |
c906108c | 339 | { |
39f77062 | 340 | inferior_ptid = null_ptid; |
c906108c SS |
341 | } |
342 | ||
fba45db2 | 343 | terminal_inferior (void) |
c5aa993b JM |
344 | { |
345 | } | |
c906108c | 346 | |
fba45db2 | 347 | terminal_ours (void) |
c5aa993b JM |
348 | { |
349 | } | |
c906108c | 350 | |
fba45db2 | 351 | terminal_init_inferior (void) |
c5aa993b JM |
352 | { |
353 | } | |
c906108c | 354 | |
fba45db2 | 355 | write_inferior_register (void) |
c5aa993b JM |
356 | { |
357 | } | |
c906108c | 358 | |
fba45db2 | 359 | read_inferior_register (void) |
c5aa993b JM |
360 | { |
361 | } | |
c906108c | 362 | |
fba45db2 | 363 | read_memory (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
364 | { |
365 | memcpy (myaddr, memaddr, len); | |
366 | } | |
367 | ||
368 | /* Always return 0 indicating success. */ | |
369 | ||
fba45db2 | 370 | write_memory (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
371 | { |
372 | memcpy (memaddr, myaddr, len); | |
373 | return 0; | |
374 | } | |
375 | ||
376 | static REGISTER_TYPE saved_regs[NUM_REGS]; | |
377 | ||
378 | REGISTER_TYPE | |
fba45db2 | 379 | read_register (int regno) |
c906108c SS |
380 | { |
381 | if (regno < 0 || regno >= NUM_REGS) | |
382 | error ("Register number %d out of range.", regno); | |
383 | return saved_regs[regno]; | |
384 | } | |
385 | ||
386 | void | |
fba45db2 | 387 | write_register (int regno, REGISTER_TYPE value) |
c906108c SS |
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 | ||
fba45db2 | 396 | vfork (void) |
c906108c SS |
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 | ||
fba45db2 | 406 | ptrace (void) |
c5aa993b JM |
407 | { |
408 | } | |
c906108c | 409 | |
fba45db2 | 410 | setpgrp (void) |
c5aa993b JM |
411 | { |
412 | } | |
c906108c | 413 | |
fba45db2 | 414 | execle (void) |
c5aa993b JM |
415 | { |
416 | } | |
c906108c | 417 | |
fba45db2 | 418 | _exit (void) |
c5aa993b JM |
419 | { |
420 | } | |
c906108c SS |
421 | \f |
422 | /* Malloc calls these. */ | |
423 | ||
fba45db2 | 424 | malloc_warning (char *str) |
c906108c SS |
425 | { |
426 | printf ("\n%s.\n\n", str); | |
427 | } | |
428 | ||
429 | char *next_free; | |
430 | char *memory_limit; | |
431 | ||
432 | char * | |
fba45db2 | 433 | sbrk (int amount) |
c906108c SS |
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 | ||
443 | char * | |
fba45db2 | 444 | ulimit (void) |
c906108c SS |
445 | { |
446 | return memory_limit; | |
447 | } | |
448 | ||
449 | int | |
fba45db2 | 450 | vlimit (void) |
c906108c SS |
451 | { |
452 | return memory_limit - next_free; | |
453 | } | |
454 | ||
fba45db2 | 455 | getrlimit (struct rlimit *addr) |
c906108c SS |
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 | ||
467 | static int fault_code; | |
468 | static REGISTER_TYPE gdb_stack; | |
469 | ||
fba45db2 | 470 | resume (void) |
c906108c SS |
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 | ||
fba45db2 | 482 | save_frame_pointer (CORE_ADDR val) |
c906108c SS |
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 | ||
fba45db2 | 493 | fault (void) |
c906108c SS |
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 | ||
fba45db2 | 507 | restore_gdb (void) |
c906108c SS |
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 | ||
fba45db2 | 519 | save_registers (int firstreg) |
c906108c SS |
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 | ||
529 | static int fault_table[] = FAULT_TABLE; | |
530 | ||
531 | int | |
fba45db2 | 532 | wait (WAITTYPE *w) |
c906108c SS |
533 | { |
534 | WSETSTOP (*w, fault_table[fault_code / FAULT_CODE_UNITS]); | |
39f77062 | 535 | return PIDGET (inferior_ptid); |
c906108c SS |
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 | ||
c5aa993b JM |
552 | char heap[HEAP_SIZE] = |
553 | {0}; | |
c906108c SS |
554 | |
555 | #ifndef STACK_SIZE | |
556 | #define STACK_SIZE 100000 | |
557 | #endif | |
558 | ||
559 | int kdb_stack_beg[STACK_SIZE / sizeof (int)]; | |
560 | int kdb_stack_end; | |
561 | ||
fba45db2 | 562 | _initialize_standalone (void) |
c906108c | 563 | { |
52f0bd74 | 564 | char *next; |
c906108c SS |
565 | |
566 | /* Find start of data on files. */ | |
567 | ||
568 | files_start = heap; | |
569 | ||
570 | /* Find the end of the data on files. */ | |
571 | ||
c5aa993b JM |
572 | for (next = files_start; *(int *) next; next += *(int *) next) |
573 | { | |
574 | } | |
c906108c SS |
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 | } |