Annotation of gdb/standalone.c, revision 1.1.1.1

1.1       root        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: 
                     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: 
                     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: 
                    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: 
                    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 *
                    344: get_exec_file ()
                    345: {
                    346:   /* Makes one printout look reasonable; value does not matter otherwise.  */
                    347:   return "run";
                    348: }
                    349: 
                    350: have_core_file_p ()
                    351: {
                    352:   return 0;
                    353: }
                    354: 
                    355: kill_command ()
                    356: {
                    357:   inferior_pid = 0;
                    358: }
                    359: 
                    360: terminal_inferior ()
                    361: {}
                    362: 
                    363: terminal_ours ()
                    364: {}
                    365: 
                    366: terminal_init_inferior ()
                    367: {}
                    368: 
                    369: write_inferior_register ()
                    370: {}
                    371: 
                    372: read_inferior_register ()
                    373: {}
                    374: 
                    375: read_memory (memaddr, myaddr, len)
                    376:      CORE_ADDR memaddr;
                    377:      char *myaddr;
                    378:      int len;
                    379: {
                    380:   bcopy (memaddr, myaddr, len);
                    381: }
                    382: 
                    383: /* Always return 0 indicating success.  */
                    384: 
                    385: write_memory (memaddr, myaddr, len)
                    386:      CORE_ADDR memaddr;
                    387:      char *myaddr;
                    388:      int len;
                    389: {
                    390:   bcopy (myaddr, memaddr, len);
                    391:   return 0;
                    392: }
                    393: 
                    394: static REGISTER_TYPE saved_regs[NUM_REGS];
                    395: 
                    396: REGISTER_TYPE
                    397: read_register (regno)
                    398:      int regno;
                    399: {
                    400:   if (regno < 0 || regno >= NUM_REGS)
                    401:     error ("Register number %d out of range.", regno);
                    402:   return saved_regs[regno];
                    403: }
                    404: 
                    405: void
                    406: write_register (regno, value)
                    407:      int regno;
                    408:      REGISTER_TYPE value;
                    409: {
                    410:   if (regno < 0 || regno >= NUM_REGS)
                    411:     error ("Register number %d out of range.", regno);
                    412:   saved_regs[regno] = value;
                    413: }
                    414: 
                    415: /* System calls needed in relation to running the "inferior".  */
                    416: 
                    417: vfork ()
                    418: {
                    419:   /* Just appear to "succeed".  Say the inferior's pid is 1.  */
                    420:   return 1;
                    421: }
                    422: 
                    423: /* These are called by code that normally runs in the inferior
                    424:    that has just been forked.  That code never runs, when standalone,
                    425:    and these definitions are so it will link without errors.  */
                    426: 
                    427: ptrace ()
                    428: {}
                    429: 
                    430: setpgrp ()
                    431: {}
                    432: 
                    433: execle ()
                    434: {}
                    435: 
                    436: _exit ()
                    437: {}
                    438: 
                    439: /* Malloc calls these.  */
                    440: 
                    441: malloc_warning (str)
                    442:      char *str;
                    443: {
                    444:   printf ("\n%s.\n\n", str);
                    445: }
                    446: 
                    447: char *next_free;
                    448: char *memory_limit;
                    449: 
                    450: char *
                    451: sbrk (amount)
                    452:      int amount;
                    453: {
                    454:   if (next_free + amount > memory_limit)
                    455:     return (char *) -1;
                    456:   next_free += amount;
                    457:   return next_free - amount;
                    458: }
                    459: 
                    460: /* Various ways malloc might ask where end of memory is.  */
                    461: 
                    462: char *
                    463: ulimit ()
                    464: {
                    465:   return memory_limit;
                    466: }
                    467: 
                    468: int
                    469: vlimit ()
                    470: {
                    471:   return memory_limit - next_free;
                    472: }
                    473: 
                    474: getrlimit (addr)
                    475:      struct rlimit *addr;
                    476: {
                    477:   addr->rlim_cur = memory_limit - next_free;
                    478: }
                    479: 
                    480: /* Context switching to and from program being debugged.  */
                    481: 
                    482: /* GDB calls here to run the user program.
                    483:    The frame pointer for this function is saved in
                    484:    gdb_stack by save_frame_pointer; then we restore
                    485:    all of the user program's registers, including PC and PS.  */
                    486: 
                    487: static int fault_code;
                    488: static REGISTER_TYPE gdb_stack;
                    489: 
                    490: resume ()
                    491: {
                    492:   REGISTER_TYPE restore[NUM_REGS];
                    493: 
                    494:   PUSH_FRAME_PTR;
                    495:   save_frame_pointer ();
                    496: 
                    497:   bcopy (saved_regs, restore, sizeof restore);
                    498:   POP_REGISTERS;
                    499:   /* Control does not drop through here!  */
                    500: }
                    501: 
                    502: save_frame_pointer (val)
                    503:      CORE_ADDR val;
                    504: {
                    505:   gdb_stack = val;
                    506: }
                    507: 
                    508: /* Fault handlers call here, running in the user program stack.
                    509:    They must first push a fault code,
                    510:    old PC, old PS, and any other info about the fault.
                    511:    The exact format is machine-dependent and is known only
                    512:    in the definition of PUSH_REGISTERS.  */
                    513: 
                    514: fault ()
                    515: {
                    516:   /* Transfer all registers and fault code to the stack
                    517:      in canonical order: registers in order of GDB register number,
                    518:      followed by fault code.  */
                    519:   PUSH_REGISTERS;
                    520: 
                    521:   /* Transfer them to saved_regs and fault_code.  */
                    522:   save_registers ();
                    523: 
                    524:   restore_gdb ();
                    525:   /* Control does not reach here */
                    526: }
                    527: 
                    528: restore_gdb ()
                    529: {
                    530:   CORE_ADDR new_fp = gdb_stack;
                    531:   /* Switch to GDB's stack  */
                    532:   POP_FRAME_PTR;
                    533:   /* Return from the function `resume'.  */
                    534: }
                    535: 
                    536: /* Assuming register contents and fault code have been pushed on the stack as
                    537:    arguments to this function, copy them into the standard place
                    538:    for the program's registers while GDB is running.  */
                    539: 
                    540: save_registers (firstreg)
                    541:      int firstreg;
                    542: {
                    543:   bcopy (&firstreg, saved_regs, sizeof saved_regs);
                    544:   fault_code = (&firstreg)[NUM_REGS];
                    545: }
                    546: 
                    547: /* Store into the structure such as `wait' would return
                    548:    the information on why the program faulted,
                    549:    converted into a machine-independent signal number.  */
                    550: 
                    551: static int fault_table[] = FAULT_TABLE;
                    552: 
                    553: int
                    554: wait (w)
                    555:      WAITTYPE *w;
                    556: {
                    557:   WSETSTOP (*w, fault_table[fault_code / FAULT_CODE_UNITS]);
                    558:   return inferior_pid;
                    559: }
                    560: 
                    561: /* Allocate a big space in which files for kdb to read will be stored.
                    562:    Whatever is left is where malloc can allocate storage.
                    563: 
                    564:    Initialize it, so that there will be space in the executable file
                    565:    for it.  Then the files can be put into kdb by writing them into
                    566:    kdb's executable file.  */
                    567: 
                    568: /* The default size is as much space as we expect to be available
                    569:    for kdb to use!  */
                    570: 
                    571: #ifndef HEAP_SIZE
                    572: #define HEAP_SIZE 400000
                    573: #endif
                    574: 
                    575: char heap[HEAP_SIZE] = {0};
                    576: 
                    577: #ifndef STACK_SIZE
                    578: #define STACK_SIZE 100000
                    579: #endif
                    580: 
                    581: int kdb_stack_beg[STACK_SIZE / sizeof (int)];
                    582: int kdb_stack_end;
                    583: 
                    584: static
                    585: initialize ()
                    586: {
                    587:   register char *next;
                    588: 
                    589:   /* Find start of data on files.  */
                    590: 
                    591:   files_start = heap;
                    592: 
                    593:   /* Find the end of the data on files.  */
                    594: 
                    595:   for (next - files_start; * (int *) next;
                    596:        next += * (int *) next)
                    597:     {}
                    598: 
                    599:   /* That is where free storage starts for sbrk to give out.  */
                    600:   next_free = next;
                    601: 
                    602:   memory_limit = heap + sizeof heap;
                    603: }
                    604: 
                    605: END_FILE

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.