home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / utils / amd-udi / mondfe / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-23  |  13.0 KB  |  482 lines

  1. static char _[] = "@(#)main.c    5.27 93/10/27 15:11:04, Srini, AMD";
  2. /******************************************************************************
  3.  * Copyright 1991 Advanced Micro Devices, Inc.
  4.  *
  5.  * This software is the property of Advanced Micro Devices, Inc  (AMD)  which
  6.  * specifically  grants the user the right to modify, use and distribute this
  7.  * software provided this notice is not removed or altered.  All other rights
  8.  * are reserved by AMD.
  9.  *
  10.  * AMD MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS
  11.  * SOFTWARE.  IN NO EVENT SHALL AMD BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL
  12.  * DAMAGES IN CONNECTION WITH OR ARISING FROM THE FURNISHING, PERFORMANCE, OR
  13.  * USE OF THIS SOFTWARE.
  14.  *
  15.  * So that all may benefit from your experience, please report  any  problems
  16.  * or  suggestions about this software to the 29K Technical Support Center at
  17.  * 800-29-29-AMD (800-292-9263) in the USA, or 0800-89-1131  in  the  UK,  or
  18.  * 0031-11-1129 in Japan, toll free.  The direct dial number is 512-462-4118.
  19.  *
  20.  * Advanced Micro Devices, Inc.
  21.  * 29K Support Products
  22.  * Mail Stop 573
  23.  * 5900 E. Ben White Blvd.
  24.  * Austin, TX 78741
  25.  * 800-292-9263
  26.  *****************************************************************************
  27.  *      Engineer: Srini Subramanian.
  28.  *****************************************************************************
  29.  * This is the main module of MONDFE.
  30.  *****************************************************************************
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include  <signal.h>
  35. #ifdef    MSDOS
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #else
  39. #include <strings.h>
  40. #endif
  41. #include "coff.h"
  42. #include "main.h"
  43. #include  "monitor.h"
  44. #include "memspcs.h"
  45. #include "miniint.h"
  46. #include "error.h"
  47. #include "versions.h"
  48.  
  49. #ifdef MSDOS
  50. #define    strcasecmp    stricmp
  51. #endif
  52.  
  53. /* Externals */
  54. extern    void    Def_CtrlC_Hdlr PARAMS((int));
  55. extern    void    Mini_parse_args PARAMS((int argc, char  **argv));
  56. extern    INT32    Mini_initialize PARAMS((HOST_CONFIG *host, IO_CONFIG *io,
  57.                     INIT_INFO *init));
  58. extern    INT32    Mini_io_setup PARAMS((void));
  59. extern    INT32    Mini_io_reset PARAMS((void));
  60. extern    INT32    Mini_load_file PARAMS((char *fname, INT32 mspace, 
  61.                        int  argc, char  *args, 
  62.                        INT32 symbols, INT32 sections, int msg));
  63. extern    void    Mini_monitor PARAMS((void));
  64. extern    INT32    Mini_go_forever PARAMS((void));
  65.  
  66. /* Globals */
  67.  
  68. GLOBAL    char  *host_version = HOST_VERSION;
  69. GLOBAL    char  *host_date = HOST_DATE;
  70.  
  71. GLOBAL     TARGET_CONFIG     target_config;
  72. GLOBAL     VERSIONS_ETC     versions_etc;
  73. GLOBAL     TARGET_STATUS     target_status;
  74. GLOBAL     HOST_CONFIG     host_config;
  75. GLOBAL     IO_CONFIG        io_config;
  76.  
  77. /* The filenos of the monitor's stdin, stdout, adn stderr */
  78. int    MON_STDIN;
  79. int    MON_STDOUT;
  80. int    MON_STDERR;
  81.  
  82. int    Session_ids[MAX_SESSIONS];
  83. int    NumberOfConnections=0;
  84. /* The following variables are to be set/initialized in Mini_parse_args()
  85.  */
  86. GLOBAL    BOOLEAN    monitor_enable = FALSE;
  87. GLOBAL    int    QuietMode = 0;
  88. GLOBAL    BOOLEAN    ROM_flag = FALSE;
  89.  
  90. GLOBAL     char      *ROM_file = NULL;
  91. GLOBAL    char    **ROM_argv;
  92. GLOBAL    int    ROM_sym, ROM_sects;
  93. GLOBAL    int    ROM_argc;
  94.  
  95. GLOBAL    char    CoffFileName[1024];
  96. static    char    Ex_argstring[1024];
  97. GLOBAL    int    Ex_sym, Ex_sects, Ex_space;
  98. GLOBAL    int    Ex_argc;
  99. static    int    Ex_loaded=0;
  100.  
  101. GLOBAL    char    *ProgramName=NULL;
  102.  
  103. GLOBAL    char    *connect_string;
  104.  
  105. GLOBAL    INT32    udi_waittime;
  106.  
  107. /* Main routine */
  108.  
  109. main(argc, argv)
  110.    int   argc;
  111.    char *argv[];
  112.    {
  113.  
  114.    char        *temp;
  115.    int        i;
  116.    UINT32    ProcessorState;
  117.    int        GrossState;
  118.    INT32    retval;
  119.  
  120.    ProgramName=argv[0];
  121.    if (strpbrk( ProgramName, "/\\" ))
  122.    {
  123.     temp = ProgramName + strlen( ProgramName );
  124.     while (!strchr( "/\\", *--temp ))
  125.         ;
  126.     ProgramName = temp+1;
  127.    }
  128.  
  129.    if (argc < 2 ) {
  130.      fprintf(stderr, "MiniMON29K Release 3.0\n");
  131.      fprintf(stderr, "MONDFE Debugger Front End (UDI 1.2) Version %s Date %s\n", HOST_VERSION, HOST_DATE);
  132.      fatal_error(EMUSAGE);
  133.    }
  134.  
  135.    /* Initialize stdin, stdout, sdterr to defaults */
  136.    MON_STDIN = fileno(stdin);
  137.    MON_STDOUT = fileno(stdout);
  138.    MON_STDERR = fileno(stderr);
  139.    NumberOfConnections = 0;
  140.    (void) strcpy (CoffFileName,"");
  141.  
  142.    udi_waittime = (INT32) 10;    /* default poll every ? secs */
  143.    /*
  144.    ** Initialize host configuration structure (global), set defaults
  145.    */
  146.    if (Mini_initialize (&host_config, &io_config, &init_info) != SUCCESS)
  147.       fatal_error(EMHINIT);
  148.  
  149.    /* Parse args */
  150.    (void) Mini_parse_args(argc, argv);
  151.  
  152.    if (io_config.echo_mode == (INT32) TRUE) {
  153.       for (i=0; i < argc; i++)
  154.      fprintf(io_config.echo_file, "%s ", argv[i]);
  155.       fprintf(io_config.echo_file, "\n");
  156.       fflush (io_config.echo_file);
  157.    };
  158.  
  159.    if ((monitor_enable == FALSE) & !Ex_loaded)
  160.      fatal_error (EMNOFILE);
  161.  
  162.    /*
  163.    ** Initialize host I/O.
  164.    */
  165.  
  166.    if (Mini_io_setup() != SUCCESS)
  167.       fatal_error(EMIOSETF);
  168.  
  169.    /*
  170.    * Initialize TIP. Load ROM file, if necessary.
  171.    ** Open communication channel
  172.    */
  173.    if (signal (SIGINT, Def_CtrlC_Hdlr) == SIG_ERR) {
  174.      fprintf(stderr, "Couldn't install default Ctrl-C handler.\n"); 
  175.      if (io_config.echo_mode == (INT32) TRUE)
  176.         fprintf(io_config.echo_file, "Couldn't install default Ctrl-C handler.\n"); 
  177.    }
  178.    /* connect_string is made by the Mini_parse_args routine */
  179.    retval = Mini_TIP_init(connect_string, &Session_ids[NumberOfConnections]); 
  180.    if (retval > (INT32) 0) {
  181.        fatal_error(EMTIPINIT);
  182.    } else if (retval == (INT32) SUCCESS) {
  183.        NumberOfConnections=NumberOfConnections+1;
  184.    } else {
  185.        Mini_TIP_exit();
  186.         fatal_error(EMTIPINIT);
  187.    }
  188.    if (Mini_get_target_stats((INT32) -1, &ProcessorState) != SUCCESS) {/* reconnect?*/
  189.          Mini_TIP_exit();
  190.      fatal_error(EMFATAL);
  191.    };
  192.    GrossState = (int) (ProcessorState & 0xFF);
  193.    if (GrossState == NOTEXECUTING)
  194.       if (Mini_TIP_CreateProc() != SUCCESS) {
  195.          Mini_TIP_exit();
  196.          fatal_error(EMNOPROCESS);
  197.       }
  198.  
  199.    /* Get capabilities */
  200.    if (Mini_TIP_Capabilities() != SUCCESS) {
  201.       Mini_TIP_DestroyProc();
  202.       Mini_TIP_exit();
  203.       fatal_error(EMNOTCOMP);
  204.    }
  205.  
  206.    /* Get the target memory configuration, and processor type */
  207.    versions_etc.version = 0;    /* initialize in case not returned */
  208.    if (Mini_config_req(&target_config, &versions_etc) != SUCCESS) {
  209.       warning(EMCONFIG);
  210.    }
  211.  
  212.    if (strcmp(CoffFileName,"") != 0) {
  213.      if (Mini_load_file(&CoffFileName[0], 
  214.             Ex_space,
  215.             Ex_argc,
  216.             Ex_argstring,
  217.             Ex_sym,
  218.             Ex_sects,
  219.             QuietMode) != SUCCESS) {
  220.     Ex_loaded = 0;
  221.     warning(EMLOADF);
  222.      } else {
  223.         Ex_loaded = 1;
  224.      };
  225.    }
  226.  
  227.    if (monitor_enable == FALSE) {
  228.       if (Ex_loaded)
  229.          Mini_go_forever();
  230.       else { /* nothing to do, so quit */
  231.          Mini_TIP_DestroyProc();
  232.          Mini_TIP_exit();
  233.      warning (EMNOFILE);
  234.       }
  235.    } else {
  236.       Mini_monitor();
  237.    };
  238.  
  239.    fflush(stderr);
  240.    fflush(stdout);
  241.  
  242.    /* Perform host-specific clean-up */
  243.    if (Mini_io_reset() != SUCCESS)
  244.       warning(EMIORESETF);
  245.  
  246.    if (!QuietMode) {
  247.       fprintf(stderr, "\nGoodbye.\n");
  248.       if (io_config.echo_mode == (INT32) TRUE) {
  249.           fprintf(io_config.echo_file, "\nGoodbye.\n");
  250.           (void) fclose (io_config.echo_file);
  251.       }
  252.    }
  253.    return(0);
  254.  
  255.    }   /* end Main */
  256.  
  257.  
  258.  
  259. /*
  260. ** Functions
  261. */
  262.  
  263. /*
  264. ** This function prints out a fatal error message 
  265. ** from error_msg[]. 
  266. ** Finally, the program exits with error_number.
  267. */
  268. #ifndef    MINIMON
  269. extern    UINT32    UDIGetDFEIPCId PARAMS((void));
  270. #endif
  271.  
  272. void
  273. fatal_error(error_number)
  274.    INT32 error_number;
  275.    {
  276.    UINT32    IPCId;
  277.  
  278.    if (error_number == (INT32) EMUSAGE) {
  279. #ifndef    MINIMON
  280.      IPCId = (UINT32) UDIGetDFEIPCId();
  281.      fprintf(stderr, "MONDFE UDI IPC Implementation Id %d.%d.%d\n",
  282.                 (int) ((IPCId & 0xf00) >> 8),
  283.                 (int) ((IPCId & 0xf0) >> 4),
  284.                 (int) (IPCId & 0xf));
  285.      if (io_config.echo_mode == (INT32) TRUE)
  286.        fprintf(io_config.echo_file, "MONDFE UDI IPC Implementation Id %d.%d.%d\n",
  287.                 (int) ((IPCId & 0xf00) >> 8),
  288.                 (int) ((IPCId & 0xf0) >> 4),
  289.                 (int) (IPCId & 0xf));
  290. #else
  291.      fprintf(stderr, "Procedurally linked MiniMON29K 3.0 Delta\n");
  292. #endif
  293.        fprintf(stderr, "Usage: %s %s\nGoodbye.\n", 
  294.                   ProgramName, error_msg[(int)error_number]);
  295.        if (io_config.echo_mode == (INT32) TRUE)
  296.           fprintf(io_config.echo_file, "Usage: %s %s\nGoodbye.\n", 
  297.                   ProgramName, error_msg[(int)error_number]);
  298.    } else {
  299.      fprintf(stderr, "DFEERROR: %d : %s\nFatal error. Exiting.\n", 
  300.             (int)error_number, error_msg[(int)error_number]);
  301.      if (io_config.echo_mode == (INT32) TRUE)
  302.         fprintf(io_config.echo_file, "DFEERROR: %d : %s\nFatal error. Exiting.\n", 
  303.             (int)error_number, error_msg[(int)error_number]);
  304.    }
  305.  
  306.    NumberOfConnections=0;
  307.    if (io_config.echo_mode == (INT32) TRUE)
  308.      (void) fclose(io_config.echo_file);
  309.  
  310.    exit((int) error_number);
  311.    }
  312.  
  313.  
  314. /*
  315. ** This function prints out a warning message from
  316. ** the error_msg[] string array.
  317. */
  318.  
  319. void
  320. warning(error_number)
  321.    INT32 error_number;
  322.    {
  323.    fprintf(stderr, "DFEWARNING: %d : %s\n", (int) error_number, error_msg[(int)error_number]);
  324.    if (io_config.echo_mode == (INT32) TRUE)
  325.       fprintf(io_config.echo_file, "DFEWARNING: %d : %s\n", error_number, error_msg[(int)error_number]);
  326.    }
  327.  
  328.  
  329. /* Parse the command line arguments */
  330. void
  331. Mini_parse_args(argc, argv)
  332. int    argc;
  333. char    **argv;
  334. {
  335.    int        i, j;
  336.    int        len;
  337.  
  338.    len = 0;
  339.    for (i = 1; i < argc; i++)  {    /* ISS */
  340.       len = len + (int) strlen(argv[i]);
  341.    };
  342.    if (len == (int) 0) {
  343.      connect_string = NULL;
  344.    } else {
  345.      if ((connect_string = (char *) malloc (len + argc)) == NULL) {
  346.     fatal_error(EMALLOC);
  347.      };
  348.      for (i = 1; i < argc; i++)  {    /* ISS */
  349.       if (strcasecmp(argv[i], "-TIP") == 0) {
  350.       i++;
  351.           if (i >= argc)
  352.             fatal_error(EMUSAGE);
  353.       connect_string = argv[i];
  354.       } else if (strcmp(argv[i], "-log") == 0) {
  355.     i++;
  356.     if (i >= argc)
  357.       fatal_error(EMUSAGE);
  358.     (void) strcpy((char *)(&(io_config.log_filename[0])),argv[i]);
  359.     io_config.log_mode = (INT32) TRUE;
  360.       } else if (strcmp (argv[i], "-w") == 0) { /* Wait time param */
  361.     i++;
  362.     if (i >= argc)
  363.       fatal_error(EMUSAGE);
  364.     if (sscanf(argv[i], "%ld", &udi_waittime) != 1)
  365.       fatal_error(EMUSAGE);
  366.       } else if (strcmp (argv[i], "-ms") == 0) { /* mem stack size */
  367.     i++;
  368.     if (i >= argc)
  369.       fatal_error(EMUSAGE);
  370.     if (sscanf(argv[i], "%lx", &init_info.mem_stack_size) != 1)
  371.       fatal_error(EMUSAGE);
  372.       } else if (strcmp (argv[i], "-rs") == 0) { /* reg stack size */
  373.     i++;
  374.     if (i >= argc)
  375.       fatal_error(EMUSAGE);
  376.     if (sscanf(argv[i], "%lx", &init_info.reg_stack_size) != 1)
  377.       fatal_error(EMUSAGE);
  378.       } else if (strcmp(argv[i], "-d") == 0) {    
  379.          monitor_enable = TRUE;    
  380.       } else if (strcasecmp(argv[i], "-D") == 0) {    
  381.          monitor_enable = TRUE;    
  382.       } else if (strcmp(argv[i], "-le") == 0) {    
  383.          host_config.target_endian = LITTLE;    
  384.       } else if (strcmp(argv[i], "-q") == 0) {    
  385.          QuietMode = 1;    
  386.       } else if (strcmp(argv[i], "-c") == 0) {
  387.          i++;
  388.          if (i >= argc)
  389.             fatal_error(EMUSAGE);
  390.          (void) strcpy((char *)(&(io_config.cmd_filename[0])),argv[i]);
  391.          io_config.cmd_file_io = TRUE;
  392.       } else if (strcmp(argv[i], "-e") == 0) {
  393.          i++;
  394.          if (i >= argc)
  395.             fatal_error(EMUSAGE);
  396.          (void) strcpy((char *)(&(io_config.echo_filename[0])),argv[i]);
  397.      io_config.echo_mode = (INT32) TRUE;
  398.      if ((io_config.echo_file = fopen (io_config.echo_filename, "w")) == NULL) {
  399.         warning (EMECHOPEN);
  400.         io_config.echo_mode = (INT32) FALSE;
  401.      }
  402.       } else { 
  403.      (void) strcpy (&CoffFileName[0], argv[i]);
  404.      Ex_argc = argc - i;
  405.      (void) strcpy(Ex_argstring, argv[i]);
  406.      for (j=1; j < Ex_argc; j++) {
  407.        (void) strcat(Ex_argstring, " ");
  408.        (void) strcat (Ex_argstring, argv[i+j]);
  409.      }
  410.      Ex_sym = 0;
  411.      Ex_sects = (STYP_ABS|STYP_TEXT|STYP_LIT|STYP_DATA|STYP_BSS);
  412.      Ex_space = (INT32) I_MEM;
  413.      Ex_loaded = 1;  /* given */
  414.      break;
  415.        }  
  416.      }; /* end for */
  417.    }; /* end if-else */
  418. }
  419.  
  420. /* Function to initialize host_config and io_config data structures
  421.  * to their default values *
  422.  */
  423.  
  424. INT32
  425. Mini_initialize(host, io, init)
  426. HOST_CONFIG    *host;
  427. IO_CONFIG    *io;
  428. INIT_INFO    *init;
  429. {
  430.    /* Initialize host configuration information */
  431.  
  432. #ifdef    MSDOS
  433.    host->host_endian = LITTLE;
  434. #else
  435.    host->host_endian = BIG;
  436. #endif
  437.  
  438.    host->target_endian = BIG;  /* default */
  439.    host->version = host_version;
  440.    host->date = host_date;
  441.  
  442.    /* Initialize I/O configuration information */
  443.  
  444.    io->hif = TRUE;
  445.    io->io_control = TERM_USER;
  446.    io->cmd_ready = FALSE;
  447.    io->clear_to_send = TRUE;
  448.    io->target_running = FALSE;
  449.    io->cmd_file = NULL;
  450.    io->cmd_filename[0] = '\0';
  451.    io->cmd_file_io = FALSE;
  452.    io->log_mode = FALSE;
  453.    io->log_file = NULL;
  454.    io->log_filename[0] = '\0';
  455.    io->echo_mode = FALSE;
  456.    io->echo_file = NULL;
  457.    io->echo_filename[0] = '\0';
  458.    io->io_toggle_char = (BYTE) 21;  /* CTRL-U */
  459.  
  460.    init->mem_stack_size = (UINT32) -1;
  461.    init->reg_stack_size = (UINT32) -1;
  462.    return(SUCCESS);
  463. }
  464.  
  465. void
  466. Def_CtrlC_Hdlr(num)
  467. int    num;
  468. {
  469.   Mini_io_reset();
  470.    if (!QuietMode) {
  471.       fprintf(stderr, "\nInterrupted.\n");
  472.       if (io_config.echo_mode == (INT32) TRUE) {
  473.           fprintf(io_config.echo_file, "\nInterrupted.\n");
  474.           (void) fclose (io_config.echo_file);
  475.       }
  476.    }
  477.    Mini_TIP_SetCurrSession(0);
  478.    Mini_TIP_exit();
  479.   exit(1);
  480. }
  481.  
  482.