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