| 1 | /* Interface to bare machine for GDB running as kernel debugger. |
| 2 | Copyright (C) 1986, 1989, 1991 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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. */ |
| 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" |
| 37 | #include "gdb_wait.h" |
| 38 | \f |
| 39 | |
| 40 | /* Random system calls, mostly no-ops to prevent link problems */ |
| 41 | |
| 42 | ioctl (int desc, int code, int arg) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | int (*signal ()) () |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | kill (void) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | getpid (void) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | sigsetmask (void) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | chdir (void) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | char * |
| 68 | getcwd (char *buf, unsigned int len) |
| 69 | { |
| 70 | buf[0] = '/'; |
| 71 | buf[1] = 0; |
| 72 | return buf; |
| 73 | } |
| 74 | |
| 75 | /* Used to check for existence of .gdbinit. Say no. */ |
| 76 | |
| 77 | access (void) |
| 78 | { |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | exit (void) |
| 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 |
| 99 | specified before. There is no terminating null here. |
| 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 | |
| 130 | open (char *filename, int modes) |
| 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 | |
| 146 | for (next = files_start; *(int *) next; next += *(int *) next) |
| 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; |
| 153 | sourcesize = next + *(int *) next - sourceptr; |
| 154 | sourceleft = sourcesize; |
| 155 | return sourcedesc; |
| 156 | } |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | close (int desc) |
| 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 * |
| 171 | fopen (char *filename, char *modes) |
| 172 | { |
| 173 | return (FILE *) open (filename, *modes == 'w'); |
| 174 | } |
| 175 | |
| 176 | FILE * |
| 177 | fdopen (int desc) |
| 178 | { |
| 179 | return (FILE *) desc; |
| 180 | } |
| 181 | |
| 182 | fclose (int desc) |
| 183 | { |
| 184 | close (desc); |
| 185 | } |
| 186 | |
| 187 | fstat (int desc, struct stat *statbuf) |
| 188 | { |
| 189 | if (desc != sourcedesc) |
| 190 | { |
| 191 | errno = EBADF; |
| 192 | return -1; |
| 193 | } |
| 194 | statbuf->st_size = sourcesize; |
| 195 | } |
| 196 | |
| 197 | myread (int desc, char *destptr, int size, char *filename) |
| 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 |
| 213 | fread (int bufp, int numelts, int eltsize, int stream) |
| 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 |
| 230 | fgetc (int desc) |
| 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 | |
| 247 | lseek (int desc, int pos) |
| 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 | |
| 269 | printf (int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9) |
| 270 | { |
| 271 | char buffer[1024]; |
| 272 | sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9); |
| 273 | display_string (buffer); |
| 274 | } |
| 275 | |
| 276 | fprintf (int ign, int a1, int a2, int a3, int a4, int a5, int a6, int a7, |
| 277 | int a8, int a9) |
| 278 | { |
| 279 | char buffer[1024]; |
| 280 | sprintf (buffer, a1, a2, a3, a4, a5, a6, a7, a8, a9); |
| 281 | display_string (buffer); |
| 282 | } |
| 283 | |
| 284 | fwrite (register char *buf, int numelts, int size, int stream) |
| 285 | { |
| 286 | register int i = numelts * size; |
| 287 | while (i-- > 0) |
| 288 | fputc (*buf++, stream); |
| 289 | } |
| 290 | |
| 291 | fputc (int c, int ign) |
| 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 | |
| 303 | _flsbuf (void) |
| 304 | { |
| 305 | error ("_flsbuf was actually called."); |
| 306 | } |
| 307 | |
| 308 | fflush (int ign) |
| 309 | { |
| 310 | } |
| 311 | \f |
| 312 | /* Entries into core and inflow, needed only to make things link ok. */ |
| 313 | |
| 314 | exec_file_command (void) |
| 315 | { |
| 316 | } |
| 317 | |
| 318 | core_file_command (void) |
| 319 | { |
| 320 | } |
| 321 | |
| 322 | char * |
| 323 | get_exec_file (int err) |
| 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 | |
| 331 | have_core_file_p (void) |
| 332 | { |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | kill_command (void) |
| 337 | { |
| 338 | inferior_pid = 0; |
| 339 | } |
| 340 | |
| 341 | terminal_inferior (void) |
| 342 | { |
| 343 | } |
| 344 | |
| 345 | terminal_ours (void) |
| 346 | { |
| 347 | } |
| 348 | |
| 349 | terminal_init_inferior (void) |
| 350 | { |
| 351 | } |
| 352 | |
| 353 | write_inferior_register (void) |
| 354 | { |
| 355 | } |
| 356 | |
| 357 | read_inferior_register (void) |
| 358 | { |
| 359 | } |
| 360 | |
| 361 | read_memory (CORE_ADDR memaddr, char *myaddr, int len) |
| 362 | { |
| 363 | memcpy (myaddr, memaddr, len); |
| 364 | } |
| 365 | |
| 366 | /* Always return 0 indicating success. */ |
| 367 | |
| 368 | write_memory (CORE_ADDR memaddr, char *myaddr, int len) |
| 369 | { |
| 370 | memcpy (memaddr, myaddr, len); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static REGISTER_TYPE saved_regs[NUM_REGS]; |
| 375 | |
| 376 | REGISTER_TYPE |
| 377 | read_register (int regno) |
| 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 |
| 385 | write_register (int regno, REGISTER_TYPE value) |
| 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 | |
| 394 | vfork (void) |
| 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 | |
| 404 | ptrace (void) |
| 405 | { |
| 406 | } |
| 407 | |
| 408 | setpgrp (void) |
| 409 | { |
| 410 | } |
| 411 | |
| 412 | execle (void) |
| 413 | { |
| 414 | } |
| 415 | |
| 416 | _exit (void) |
| 417 | { |
| 418 | } |
| 419 | \f |
| 420 | /* Malloc calls these. */ |
| 421 | |
| 422 | malloc_warning (char *str) |
| 423 | { |
| 424 | printf ("\n%s.\n\n", str); |
| 425 | } |
| 426 | |
| 427 | char *next_free; |
| 428 | char *memory_limit; |
| 429 | |
| 430 | char * |
| 431 | sbrk (int amount) |
| 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 * |
| 442 | ulimit (void) |
| 443 | { |
| 444 | return memory_limit; |
| 445 | } |
| 446 | |
| 447 | int |
| 448 | vlimit (void) |
| 449 | { |
| 450 | return memory_limit - next_free; |
| 451 | } |
| 452 | |
| 453 | getrlimit (struct rlimit *addr) |
| 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 | |
| 468 | resume (void) |
| 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 | |
| 480 | save_frame_pointer (CORE_ADDR val) |
| 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 | |
| 491 | fault (void) |
| 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 | |
| 505 | restore_gdb (void) |
| 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 | |
| 517 | save_registers (int firstreg) |
| 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 |
| 530 | wait (WAITTYPE *w) |
| 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 | |
| 550 | char heap[HEAP_SIZE] = |
| 551 | {0}; |
| 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 | |
| 560 | _initialize_standalone (void) |
| 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 | |
| 570 | for (next = files_start; *(int *) next; next += *(int *) next) |
| 571 | { |
| 572 | } |
| 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 | } |