home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_oth / debugutl.lha / tDB.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  11.1 KB  |  319 lines

  1. /*                         FILE:    tDB.c
  2.  *
  3.  *    Project:            DeBug Utilities
  4.  *    Version:            v1.1
  5.  *
  6.  *
  7.  * This file contains:
  8.  *
  9.  *                        1.    main()
  10.  *
  11.  * Created:            5/24/89
  12.  * Author:        Mark Porter (fog)
  13.  *
  14.  *
  15.  * $Revision: 1.1 $
  16.  * $Date: 92/05/05 19:59:42 $
  17.  * $Author: fog $
  18.  *
  19.  *
  20.  *    Copyright © 1992 if...only Amiga
  21.  *
  22.  *    Permission is granted to distribute this program's source, executable,
  23.  *    and documentation for non-commercial use only, provided the copyright
  24.  *    and header information are left intact.
  25.  *
  26.  */
  27.  
  28.  
  29. /*----------------------------------------------------------------------*/
  30. /*----------------------------------------------------------------------*
  31.  *
  32.  *
  33.  * $Log:    tDB.c,v $
  34.  * Revision 1.1  92/05/05  19:59:42  fog
  35.  * Initial revision
  36.  * 
  37.  *
  38.  *
  39.  *----------------------------------------------------------------------*/
  40. /*----------------------------------------------------------------------*/
  41.  
  42.  
  43. #include <stdio.h>
  44. #include <libraries/dosextens.h>
  45.  
  46. #include "trace.h"
  47.  
  48.  
  49. /* DB_List is an array of pointers used to communicate with debug to        */
  50. /* add or delete functions from the DB_Function List.                            */
  51.  
  52. char    *DB_List[ DB_NUM ];
  53.  
  54.  
  55. /* Version string so the AmigaDOS VERSION command will work.                */
  56.  
  57. char    *Version = "$VER: tDB version 1.1 07-May-92";
  58.  
  59.  
  60.  
  61. /*------------ main() ---------------
  62.  *
  63.  *
  64.  * FUNCTION:    Allows user to change debugging parameters utilized with
  65.  *                    the trace.lib/debug utility.
  66.  *
  67.  * ARGUMENTS:    1.    argc:    Argument count for main().
  68.  *                    2.    argv:    Pointers to command line arguments.
  69.  *
  70.  * RETURNS:        Nothing.
  71.  *
  72.  * COMMENTS:    
  73.  *
  74.  */
  75.  
  76. main( argc,argv )
  77.     int   argc;
  78.     char *argv[];
  79. {
  80.     struct MsgPort    *send_port,                /* Pointer to debug MsgPort.        */
  81.                         *reply_port;            /* Pointer to tDB MsgPort.            */
  82.     char              **str;                        /* Pointer used to pass DB_List.    */
  83.     Debug_Msg          msg,*chk;                /* Messages sent and received.    */
  84.     int                 i;                        /* The old loop counter.            */
  85.  
  86.     /* Output program information.  Escape sequence set/reset bold.        */
  87.  
  88.     printf( "\n   if Software   tDB  v1.1  Thursday 07-May-92 22:42:41\n\n" );
  89.  
  90.     /* If there are no other arguments passed on the command line, then    */
  91.     /* tDB assumes the user wants to check status of debug internal par-    */
  92.     /* ameters.  This is done by setting DB_CHECK code.                        */
  93.  
  94.     if ( argc < 2 )
  95.         msg.DB_code = DB_CHECK;
  96.     else                                            /* Otherwise parse command line.    */
  97.     {
  98.         /* Check for addition or deletion of functions from DB_Function.    */
  99.         /* Set DB_code according to which option is chosen.                    */
  100.  
  101.         if ( !strcmp( argv[ 1 ],"-a" ) || !strcmp( argv[ 1 ],"-r" ))
  102.         {
  103.             if ( !strcmp( argv[ 1 ],"-a" ))        msg.DB_code    = DB_ADDFUNC;
  104.             if ( !strcmp( argv[ 1 ],"-r" ))        msg.DB_code    = DB_REMFUNC;
  105.  
  106.             /* If there are no function names beyond the given flag, set    */
  107.             /* DB_function pointer to NULL to terminate debug search.        */
  108.  
  109.             if ( argc < 3 )
  110.                 msg.DB_function = NULL;
  111.             else                                            /* Otherwise loop through    */
  112.             {                                                /* command line arguments    */
  113.                 for ( i = 2; i < argc; i++ )        /* and attach them to the    */
  114.                     DB_List[ i - 2 ] = argv[ i ];    /* DB_List pointers.            */
  115.  
  116.                 DB_List[ i ] = NULL;                /* Last item terminates list.    */
  117.  
  118.                 /* Initialize DB_function to point to the list.  debug will    */
  119.                 /* interpret the list in the same way as user programs deal    */
  120.                 /* with command line options passed into main().                */
  121.  
  122.                 msg.DB_function = ( char * )&DB_List[ 0 ];
  123.             }
  124.         }
  125.  
  126.         /* Check for debug level change.  Set DB_level to 0 if no other    */
  127.         /* arguments follow, otherwise convert next command line argument    */
  128.         /* to int and set DB_level.                                                    */
  129.  
  130.         else if ( !strcmp( argv[ 1 ],"-l" ))
  131.         {
  132.             msg.DB_code         = DB_LEVEL;
  133.             if ( argc < 3 )    msg.DB_level = 0;
  134.             else                    msg.DB_level = atoi( argv[ 2 ] );
  135.         }
  136.  
  137.         /* Check for option to print debug messages to file.  The file        */
  138.         /* name becomes 'DB.out' if there are no more command line arg's.    */
  139.         /* Otherwise set output file name to last command line argument.    */
  140.  
  141.         else if ( !strcmp( argv[ 1 ],"-w" ))
  142.         {
  143.             msg.DB_code = DB_WRITE;
  144.             if ( argc < 3 )    msg.DB_file = "DB.out";
  145.             else                    msg.DB_file = argv[ 2 ];
  146.         }
  147.  
  148.         /* Check for remaining options and set DB_code appropriately.        */
  149.  
  150.         else if ( !strcmp( argv[ 1 ],"-c"  ))            msg.DB_code = DB_CLEAR;
  151.         else if ( !strcmp( argv[ 1 ],"-t"  ))            msg.DB_code = DB_TOGGLE;
  152.         else if ( !strcmp( argv[ 1 ],"-we" ))            msg.DB_code = DB_ENDWRITE;
  153.         else if ( !strcmp( argv[ 1 ],"-s"  ))            msg.DB_code = DB_SER;
  154.         else if ( !strcmp( argv[ 1 ],"-se" ))            msg.DB_code = DB_ENDSER;
  155.         else if ( !strcmp( argv[ 1 ],"-p"  ))            msg.DB_code = DB_PAR;
  156.         else if ( !strcmp( argv[ 1 ],"-pe" ))            msg.DB_code = DB_ENDPAR;
  157.         else if ( !strcmp( argv[ 1 ],"-e"  ))            msg.DB_code = DB_QUIT;
  158.  
  159.         else if ( !strcmp( argv[ 1 ],"?" ))        /* '?' gives information.    */
  160.         {
  161.             printf( "tDB options  :\n" );
  162.             printf( "   -e        :  End debug session.\n" );
  163.             printf( "   -a <func> :  Add function <func> to debug function list.\n" );
  164.             printf( "   -r <func> :  Remove function <func> to debug function list.\n" );
  165.             printf( "   -c        :  Clear debug function list.\n" );
  166.             printf( "   -t        :  Toggle between function list enable/disable.\n" );
  167.             printf( "   -l <num>  :  Output debugging information for level <num>.\n" );
  168.             printf( "   -w <file> :  Send debugging output to file <file>.\n" );
  169.             printf( "   -we       :  Close debugging file now open.\n" );
  170.             printf( "   -s        :  Send debugging output to serial port.\n" );
  171.             printf( "   -se       :  Stop sending debugging output to serial port.\n" );
  172.             printf( "   -p        :  Send debugging output to parallel port.\n" );
  173.             printf( "   -pe       :  Stop sending debugging output to parallel port.\n" );
  174.             exit( 0L );
  175.         }
  176.  
  177.         else            /* Last, we have an unknown option.  Exit with error.    */
  178.         {
  179.             fprintf( stderr,"    *** tDB Error ***  use \"tDB ?\" for options\n\n" );
  180.             exit( 1L );
  181.         }
  182.     }
  183.  
  184.     /* First create a MsgPort to allow debug to communicate back to tDB.    */
  185.  
  186.     if ( reply_port = CreatePort( "Trace.DBReplyPort",0L ))
  187.     {
  188.         /* If the MsgPort creation was successful, initialize msg fields    */
  189.         /* to appropriate values.  We have already set the necessary info    */
  190.         /* for debug internals above, now we need to give Exec values it    */
  191.         /* knows how to deal with.                                                        */
  192.  
  193.         msg.DB_msg.mn_Node.ln_Type = ( UBYTE )NT_MESSAGE;
  194.        msg.DB_msg.mn_Node.ln_Pri  = ( BYTE )0;
  195.        msg.DB_msg.mn_Node.ln_Name = NULL;
  196.         msg.DB_msg.mn_ReplyPort    = reply_port;
  197.        msg.DB_msg.mn_Length       = ( UWORD )sizeof( Debug_Msg );
  198.  
  199.         /* Find the MsgPort used by debug.  This is known from having        */
  200.         /* written the code for debug.                                                */
  201.  
  202.         if ( send_port = FindPort( "Trace.DBPort" ))
  203.         {
  204.             PutMsg( send_port,&msg );        /* Send our message.                    */
  205.             WaitPort( reply_port );            /* Wait for debug to reply.        */
  206.             GetMsg( reply_port );            /* Pull message off reply_port.    */
  207.  
  208.             /* Now we're going to output more information to the user.        */
  209.  
  210.             switch( msg.DB_code )
  211.             {
  212.                 /* For a DB_CHECK message tDB must wait for another message    */
  213.                 /* from debug before printing info.  This is due to the        */
  214.                 /* mechanism Exec employs for passing messages.  Since it    */
  215.                 /* is only passing addresses, and not actually copying data    */
  216.                 /* what is really happening is that one task is allowing        */
  217.                 /* another to access its private data.                              */
  218.  
  219.                 /* In our case part of the data we are interested in is the    */
  220.                 /* DB_Function List.  In order to be able to read the    list,    */
  221.                 /* tDB must be granted access to debug's memory.  This can    */
  222.                 /* be handled by having debug send tDB a message with the    */
  223.                 /* required information attached.                                    */
  224.  
  225.                 case DB_CHECK:
  226.                 {
  227.                     if (( chk = ( Debug_Msg * )GetMsg( reply_port )) == NULL )
  228.                     {
  229.                         /* If the message has not yet arrived, wait for it        */
  230.                         /* and pull it off the MsgPort.                                */
  231.  
  232.                         WaitPort( reply_port );
  233.                         chk = ( Debug_Msg * )GetMsg( reply_port );
  234.                     }
  235.  
  236.                     /* Set a local pointer to beginning of DB_Function List    */
  237.                     /* and output information.                                            */
  238.  
  239.                     str = ( char ** )chk->DB_function;
  240.  
  241.                     printf( "\t DB_Level         = %d\n",chk->DB_level );
  242.                     printf( "\t DB_Function List = %s\n",str[ 0 ] );
  243.  
  244.                     /* Loop through DB_Function list by way of str[].  NULL    */
  245.                     /* value indicates we have reached the end.                    */
  246.  
  247.                     for ( i = 1; str[ i ]; i++ )
  248.                         printf( "\t                    %s\n",str[ i ] );
  249.  
  250.                     /* Now give information about whether the DB_Function        */
  251.                     /* List functions are set to print debug messages within    */
  252.                     /* them, or ignore them.  This info is held within the    */
  253.                     /* DB_code parameter.                                                */
  254.  
  255.                     printf( "\n\t Output from Function List functions is " );
  256.  
  257.                     if ( chk->DB_code == db_include )    printf( "enabled\n"  );
  258.                     else                                            printf( "disabled\n" );
  259.  
  260.                     ReplyMsg( chk );    /* Reply to debug to release message.    */
  261.                 }
  262.                 break;
  263.  
  264.                 /* For all other types of message passing, we simply output    */
  265.                 /* a remark indicating that a message of the given type has    */
  266.                 /* been sent to debug, and has been replied.                        */
  267.  
  268.                 case DB_ADDFUNC:    printf( "\t Functions added to DB_Function List\n" );                break;
  269.                 case DB_REMFUNC:    printf( "\t Functions removed from DB_Function List\n" );        break;
  270.                 case DB_CLEAR:        printf( "\t DB_Function List cleared\n" );                            break;
  271.                 case DB_TOGGLE:    printf( "\t DB_Function List output enabling toggled\n" );        break;
  272.                 case DB_LEVEL:        printf( "\t DB_Level changed to %d.\n",msg.DB_level );            break;
  273.                 case DB_WRITE:        printf( "\t Debug output to file <%s>.\n",msg.DB_file );            break;
  274.                 case DB_ENDWRITE:    printf( "\t Debug output file closed.\n" );                            break;
  275.                 case DB_SER:        printf( "\t Debug output to serial port.\n" );                        break;
  276.                 case DB_ENDSER:    printf( "\t Debug output to serial port stopped.\n" );            break;
  277.                 case DB_PAR:        printf( "\t Debug output to parallel port.\n" );                    break;
  278.                 case DB_ENDPAR:    printf( "\t Debug output to parallel port stopped.\n" );            break;
  279.                 case DB_QUIT:        printf( "\t Trace.DBPort Deleted.\n" );                                break;
  280.  
  281.                 /* The next three cases are error returns from debug.  They    */
  282.                 /* result from DB_Function List overflow, not finding a        */
  283.                 /* function we wish to remove, or a message code unknown to    */
  284.                 /* debug.                                                                    */
  285.  
  286.                 case DB_OVERFLOW:    fprintf( stderr,"    *** debug Error *** DB_Function List overflow.\n" );
  287.                                         break;
  288.  
  289.                 /* When we have an unmatched function removal error, debug    */
  290.                 /* will have left the unknown string pointer intact within    */
  291.                 /* DB_List[].  All other pointers will be NULL.                    */
  292.  
  293.                 case DB_UNMATCH:    fprintf( stderr,"    *** Warning *** Function(s) to remove not found:\n" );
  294.  
  295.                                         for ( i = 2; i < argc; i++ )
  296.                                         {
  297.                                             if ( DB_List[ i - 2 ] != NULL )
  298.                                                 fprintf( stderr,"\t                    %s\n",DB_List[ i - 2 ] );
  299.                                         }
  300.                                         break;
  301.  
  302.                 case DB_UNKNOWN:    fprintf( stderr,"    *** debug Error *** Unknown debug option.\n" );
  303.                                         break;
  304.  
  305.                 default:                  fprintf( stderr,"    *** tDB Error *** Unknown tDB Option.\n" );
  306.                                         break;
  307.             }
  308.         }
  309.         else
  310.             fprintf( stderr,"    *** tDB Error *** Trace.DBPort could not be found\n" );
  311.  
  312.         DeletePort( reply_port );
  313.     }
  314.     else
  315.         fprintf( stderr,"    *** tDB Error *** Trace.DBReplyPort not created\n" );
  316.  
  317.     printf( "\n" );
  318. }
  319.