Thread: DEBUG code

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    12

    DEBUG code

    can somebody give me an example of a DEBUG program...I have to create my own DEBUG mode........I know what that thing do....but I dunno what it really looks like

    i just have the idea that i can use
    Code:
     #ifdef DEBUG
    .......
    #endif
    any idea????

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    int function(void){
      #ifdef DEBUG
      printf("function001() called\n");
      #endif
      ...
      ...
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    12

    uhhh....

    btw...what the meaning of that "......" kinda thing???
    ym id >>>>> asmahmazlan

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    it means the rest of the code...
    When no one helps you out. Call google();

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    12

    command line for debug

    then...what the command line suppose to looks like?? i'm currently using Borland 5.5 compiler....
    ym id >>>>> asmahmazlan

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's one way:

    Code:
    #include <stdio.h>
    
    void foo(void)
    {
    #ifdef DEBUG
      fprintf (stderr, "DEBUG: Entering function foo()\n");
    #endif
    
    }
    
    int main(void)
    {
      foo();  
      return(0);
    }
    
    
    
    
    >bcc32 -DDEBUG junk1.c
    >junk1
    DEBUG: Entering function foo()
    
    >bcc32 junk1.c
    >junk1
    (no output from program this time)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    12

    thanks!!!!

    thanks guys..that's really help..
    ahha....now I can produce an error message for myself (that kinda thing that alwiz appear ,makes us very mad sometimes .... ) ...just to share something.......
    Code:
    #include <stdio.h>
    main()
    {  
         #ifdef DEBUG
         fprintf(stderr,"Error %s %d :There's a bug in this line!!\n",__FILE__,__LINE__);
         #else
         printf("Can't find any bug though!!!\n");
         #endif
      return 0;
    }
    and then when i type in the command lines, i've got something like this

    Code:
    C:\Borland\bcc55\Bin>bcc32 -DDEBUG debug.c
    Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    debug.c:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    
    C:\Borland\bcc55\Bin>debug
    Error debug.c 6 :There's a bug in this line!!
    
    C:\Borland\bcc55\Bin>bcc32 debug.c
    Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    debug.c:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    
    C:\Borland\bcc55\Bin>debug
    Can't find any bug though!!!
    hope that anyone else who're also working on this thing find it somewhat useful..
    ym id >>>>> asmahmazlan

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    Thats a debug message, I guess, you compile it in ir not - not very useful, but a more useful "debug mode" would be to:

    Code:
    #include <...
    
    int DEBUG = 0; // or false
    
    int main ( int argc, char **argv )
    {
            char *progname;
            ...
            progname = *argv++;
            if (!strcmp(*argv,"-d") {
                 argv++;
                 DEBUG = 1; // or true
            }
            .... handle rest of arguments ....
            ...
            if (DEBUG) printf("Program %s started.\n",progname);
            .... more program ....
            if (DEBUG) printf("Program %s ended.\n",progname);
    }
    
    void functionOne ( int arg )
    {
        if (DEBUG) printf("Entering functionOne(%d)\n",arg);
        ...
    }
    
    int functionTwo ( int argi, char *argc )
    {
         if (DEBUG) printf("Entering functionTwo(%d,%s)\n",argi,argc);
         ...
         if (DEBUG) printf("  did this!\n");
         ...
         if (DEBUG) printf("  did that with result %d!\n",result);
         ...
    }
    
    ... etc.

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    #include <stdio.h>
    main()
    {  
         #ifdef DEBUG
         fprintf(stderr,"Error %s %d :There's a bug in this line!!\n",__FILE__,__LINE__);
         #else
         printf("Can't find any bug though!!!\n");
         #endif
      return 0;
    }
    This does not mean there is a bug in the program. Basically if you program is not running properly then you want to c ompile and run it in DEBUG mode so that you can see what your program is doing.
    Like hammer posted
    Code:
    void foo(void)
    {
    #ifdef DEBUG
      fprintf (stderr, "DEBUG: Entering function foo()\n");
    #endif
    
    }
    If DEBUG is defined then you will know when this function is called. It does not mean there is a bug in the code.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    12
    hmmm....I'm still quite confuse...what is actually the function of this "debug" command??is it to indicate what the program is doing at each stage or only at a particular stage??
    Last edited by myer_784; 03-14-2005 at 11:13 AM.
    ym id >>>>> asmahmazlan

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>what is actually the function of this "debug" command
    You output what messages you think are necessary to help you solve problems. Sometimes, you might need to know what functions have been invoked and in what order; sometimes you may need to know the value of a particular variable at a given time. These are only basic examples, you can make it as simple or complex as you like.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    ---
    Join Date
    May 2004
    Posts
    1,379
    If your program has crashed before foo() has been called you wouldnt get the msg. That is how you can narrow down where your bug may be.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    12

    compilation

    what if I have both multiple files and this debug message at the same time??...in using the " -DDEBUG " command in the compilation process before running the program,what the command line suppose to be??...
    this is the sample of the make file..
    Code:
    CC=bcc32
    HEADERS=headers.h
    silly.exe: main.obj fun_a.obj fun_b.obj fun_c.obj
    	$(CC) -esilly.exe main.obj fun_a.obj fun_b.obj fun_c.obj
    main.obj: $(HEADERS) main.c
    	$(CC) -I. -c main.c
    fun_a.obj: $(HEADERS) fun_a.c
    	$(CC) -I. -c fun_a.c
    fun_b.obj: $(HEADERS) fun_b.c fun_c.obj
    	$(CC) -I. -c fun_b.c
    fun_c.obj: $(HEADERS) fun_c.c
    	$(CC) -I. -c fun_c.c
    clean:
    	del silly *.obj *.tds
    tidy:
    	del *.tds
    usually we will have to type in the command as bcc32 -DDEBUG main.c......but this time I have all these combined files.....what file name should i use??? (say the name of this makefile is "MakeFile.txt" ...)
    Last edited by myer_784; 03-16-2005 at 01:05 AM.
    ym id >>>>> asmahmazlan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. Debugging leads to buggy code and longer hours?
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 11:14 AM
  4. anyone bored enough to help me debug some code?
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2002, 07:55 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM