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 / trace.c < prev    next >
C/C++ Source or Header  |  1992-05-20  |  5KB  |  187 lines

  1. /*                         FILE:    trace.c
  2.  *
  3.  *    Project:            DeBug Utilities
  4.  *    Version:            v1.1
  5.  *
  6.  *
  7.  * This file contains:
  8.  *
  9.  *                        1.    Trace()
  10.  *                        2.    write_buf()
  11.  *
  12.  * Created:            5/24/89
  13.  * Author:        Mark Porter (fog)
  14.  *
  15.  *
  16.  * $Revision: 1.1 $
  17.  * $Date: 92/05/05 20:00:33 $
  18.  * $Author: fog $
  19.  *
  20.  *
  21.  *    Copyright © 1992 if...only Amiga
  22.  *
  23.  *    Permission is granted to distribute this program's source, executable,
  24.  *    and documentation for non-commercial use only, provided the copyright
  25.  *    and header information are left intact.
  26.  *
  27.  */
  28.  
  29.  
  30. /*----------------------------------------------------------------------*/
  31. /*----------------------------------------------------------------------*
  32.  *
  33.  *
  34.  * $Log:    trace.c,v $
  35.  * Revision 1.1  92/05/05  20:00:33  fog
  36.  * Initial revision
  37.  * 
  38.  *
  39.  *
  40.  *----------------------------------------------------------------------*/
  41. /*----------------------------------------------------------------------*/
  42.  
  43.  
  44. #include <exec/types.h>
  45. #include <ctype.h>
  46.  
  47. #include "trace.h"
  48. #include "debug.h"
  49.  
  50.  
  51. /* Version string so the AmigaDOS VERSION command will work.                */
  52.  
  53. char    *Version = "$VER: trace.lib version 1.1 07-May-92";
  54.  
  55. static int  DB_Count = 0;                    /* Characters within DB_Buffer.    */
  56. static char DB_Buffer[ TDB_BUFSIZE ];    /* Formatted debug message.        */
  57.  
  58.  
  59. static int        write_buf();
  60. static int        format();
  61. static char      *_fmtcvt();
  62. static void        ftoa();
  63.  
  64.  
  65.  
  66.  
  67. /*------------ Trace() ---------------
  68.  *
  69.  *
  70.  * FUNCTION:    Sends a message to the debugging task which will then be sent
  71.  *                    to the display.
  72.  *
  73.  * ARGUMENTS:    1.    level:    Debug level at which we are sending message.
  74.  *                    2.    func:        Function within which Trace() was called.
  75.  *                    3.    fmt:        printf() style format string.
  76.  *                    4.    args:        Pointer to printf() style arguments.
  77.  *
  78.  * RETURNS:        Nothing.
  79.  *
  80.  * COMMENTS:    I have decided to create a MsgPort with each invocation of
  81.  *                    this function mainly to avoid having to write separate
  82.  *                    initialization and shutdown functions which would create
  83.  *                    and delete the MsgPort.  This allows the user to not have
  84.  *                    to worry about proper Trace() initialization.  Although
  85.  *                    the number of instructions executed by each call to Trace()
  86.  *                    increases, the major time penalty associated with this
  87.  *                    debugging technique will always be due to writing data to
  88.  *                    a window, file, serial, or parallel port, and not to the
  89.  *                    setup and shutdown code.  The final justification is that
  90.  *                    I have used this library with great success, and even if
  91.  *                    my program execution slows down somewhat, I'm so busy
  92.  *                    trying to decipher the output that a few microseconds
  93.  *                    just doesn't matter.
  94.  *
  95.  *                    One other critical factor for the trace.lib library is that
  96.  *                    the call to format() below will only compile correctly on
  97.  *                    Manx systems.  SAS has no equivalent function and so will
  98.  *                    error out when blink is run.
  99.  *
  100.  *                    Manx uses this function to parse calls to printf(), sprintf(),
  101.  *                    and fprintf().  I originally compiled the SAS version of
  102.  *                    the library with the source code from format() included in
  103.  *                    this file.  But since the code is copyrighted, commercial
  104.  *                    material, it had to be removed before distributing trace.lib.
  105.  *
  106.  *                    The work-arounds for this problem if you wish to modify the
  107.  *                    source code is to write your own format(), or to purchase
  108.  *                    the commercial version of the Manx compiler, which comes
  109.  *                    with the c.lib source code.
  110.  *
  111.  */
  112.  
  113. void Trace( level,func,fmt,args )
  114.     int         level;
  115.     char        *func,*fmt;
  116.     unsigned     args;
  117. {
  118.     struct MsgPort        *send_port,
  119.                             *reply_port;
  120.     Debug_Msg            msg;
  121.  
  122.     /* Create a reply port for proper message returns.     Even though we    */
  123.     /* do not do anything with the returned message, we still need to        */
  124.     /* wait for the reply.  Otherwise if Trace() exits before debug        */
  125.     /* finishes writing output, the auto variable msg will quite likely    */
  126.     /* get trashed on the stack...boom, boom, out go the lights.            */
  127.  
  128.     if ( reply_port = CreatePort( "Trace.SendPort",0L ))
  129.     {
  130.         /* Initialize the Debug_Msg with all the nice info.                    */
  131.  
  132.         msg.DB_msg.mn_Node.ln_Type = ( UBYTE )NT_MESSAGE;
  133.         msg.DB_msg.mn_Node.ln_Pri  = ( BYTE )0;
  134.        msg.DB_msg.mn_Node.ln_Name = NULL;
  135.         msg.DB_msg.mn_ReplyPort    = reply_port;
  136.        msg.DB_msg.mn_Length       = ( UWORD )sizeof( Debug_Msg );
  137.         msg.DB_code                        = DB_CONTINUE;
  138.         msg.DB_count                    = ( long )format( write_buf,fmt,&args );
  139.         msg.DB_string                    = DB_Buffer;
  140.         msg.DB_function                = func;
  141.         msg.DB_level                    = level;
  142.  
  143.       Forbid();
  144.         send_port = FindPort( "Trace.DBPort" );
  145.        Permit();
  146.  
  147.         /* The only time we will generate output is if Trace() finds the    */
  148.         /* debug MsgPort.  If not, we simply delete our port and return.    */
  149.  
  150.         if ( send_port )
  151.         {
  152.             PutMsg( send_port,&msg );
  153.             WaitPort( reply_port );
  154.             GetMsg( reply_port );
  155.         }
  156.  
  157.         DeletePort( reply_port );
  158.         DB_Count = 0;
  159.     }
  160. }
  161.  
  162.  
  163.  
  164. /*------------ write_buf() ------------
  165.  *
  166.  *
  167.  * FUNCTION:    Writes characters to DB_Buffer as they are passed in
  168.  *                    by format()
  169.  *
  170.  * ARGUMENTS:    1.    c:    Character to write to buffer.
  171.  *
  172.  * RETURNS:        Cumulative number of characters written to buffer.
  173.  *
  174.  * COMMENTS:    This function is passed to format() and is used by
  175.  *                    that function to write data to a buffer.
  176.  *
  177.  */
  178.  
  179. static int write_buf( c )
  180.     char c;
  181. {
  182.     DB_Buffer[ DB_Count++     ] = c;
  183.     DB_Buffer[ DB_Count + 1 ] = '\0';
  184.  
  185.     return( DB_Count );
  186. }
  187.