Thread: A small Debug routine

  1. #1
    Registered User
    Join Date
    Sep 2004

    A small Debug routine

    following is a small debugging routine embedded in some working sample source code provided by a hardware supplier. I am just going through their sample source code to get a better understanding of how their API works. However, I just can't make sense of how the parameters of DEBUG(x, outp) are being passed to dbg_p (char i, char *fmt, ...). What does "..." in dbg_p (char i, char *fmt, ...) mean?

    Would really appreciate if someone can enlighten me.

    Thanks in advance.

    /* defines for debugging */
    #define DEBUG_OFF           99
    #define DEBUG_BASIC         03 
    #define DEBUG_ADV           02
    #define DEBUG_EXP           01
    static int can_debug = DEBUG_EXP;
    // debug version
    #ifdef _DEBUG
        #define DEBUG(x, outp)        ( dbg_p(x, outp ) )
        _inline void dbg_p (char i, char *fmt, ...)
          char tmp[100];
          if (i>=can_debug){
            sprintf (tmp, "xlCANcontrol: %s", fmt);
            OutputDebugString (tmp);
    // release version.
        #define DEBUG(x, outp)     

    Following is a portion of the main routine that calls the debugging routine:

    XLstatus CCANFunctions::canGetChannelMask()
      XLstatus        xlStatus = XL_ERROR;
      char            tmp[100];
      // default values
      unsigned int  hwType     = 0;
      unsigned int  hwIndex    = 0;
      unsigned int  hwChannel  = 0;
      unsigned int  appChannel = 0;
      unsigned int  busType    = XL_BUS_TYPE_CAN;   
      unsigned int  i; 
      XLdriverConfig  xlDrvConfig;
       //check for hardware:
      xlStatus = xlGetDriverConfig(&xlDrvConfig);
      if (xlStatus) return xlStatus;
      // we check only if there is an application registered or not.
      xlStatus = xlGetApplConfig("xlCANcontrol", CHAN01, &hwType, &hwIndex, &hwChannel, busType); 
      // Set the params into registry (default values...!)
      if (xlStatus) {
        DEBUG(DEBUG_ADV,"set in VHWConf");
        for (i=0; i < xlDrvConfig.channelCount; i++) {
          sprintf (tmp, "hwType: %d, bustype: %d, hwChannel: %d, cap: 0x%x", 
          // we search not the first CAN cabs
          if ( ([i].channelBusCapabilities & XL_BUS_ACTIVE_CAP_CAN) && (appChannel < 2) ) {
            hwType    =[i].hwType;
            hwIndex   =[i].hwIndex;
            hwChannel =[i].hwChannel;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > What does "..." in dbg_p (char i, char *fmt, ...) mean?
    It means that a variable number of arguments are expected, in exactly the same way that printf() works.

    But then the author of the debug code has completely messed up that aspect with
    sprintf (tmp, "xlCANcontrol: %s", fmt);

    What they should have done is used vsprintf(), then all the arguments would be printed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM