Thread: Help with Debugging Code

  1. #1
    sparklezilla3
    Join Date
    Mar 2010
    Posts
    41

    Help with Debugging Code

    Hello,

    I have these two items that are not compiling incorrectly...

    Code:
    enum trace_item_type {
    	ti_NOP=0,
    	ti_RTYPE,
    	ti_ITYPE,
    	ti_LOAD,
    	ti_STORE,
    	ti_BRANCH,
    	ti_JTYPE,
    	ti_SPECIAL,
    	ti_JRTYPE
    };
    
    struct trace_item {
    uint8_t type;
    uint8_t sReg_a;
    uint8_t sReg_b;
    uint8_t dReg;
    uint32_t PC;
    uint32_t Addr;
    };
    Everytime I compile it i get the error of this:
    mytrace.h: line 14: Parse Error, expecting `'}''

    'struct trace_item { uint8_t'

    aborting compile


    I checked my syntax and everything seems to be right unless i'm missing something?

    Thanks,
    husslela2

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's "u_int8_t" not "uint8_t". All the u_ints are that way.

    Remember to #include <sys/types.h>.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    sparklezilla3
    Join Date
    Mar 2010
    Posts
    41
    I'm still now getting the error:
    Code:
    mytrace.h: line 15: Parse Error, expecting `'}''
    
    'struct trace_item { u_int8_t'
    aborting compile

  4. #4
    sparklezilla3
    Join Date
    Mar 2010
    Posts
    41
    Here is my header file:

    Code:
    #include <sys/types.h>
    
    enum trace_item_type {
    
                    ti_NOP,
    
                    ti_RTYPE,
    
                    ti_ITYPE,
    
                    ti_LOAD,
    
                    ti_STORE,
    
                    ti_BRANCH,
    
                    ti_JTYPE,
    
                    ti_SPECIAL,
    
                    ti_JRTYPE
    
    };
    
     
    
    struct trace_item {
    
    u_int8_t type;
    
    u_int8_t sReg_a;
    
    u_int8_t sReg_b;
    
    u_int8_t dReg;
    
    u_int32_t PC;
    
    u_int32_t Addr;
    
    };

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That compiles fine for me. What compiler are you using?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    sparklezilla3
    Join Date
    Mar 2010
    Posts
    41
    Miracle C compiler...do you recommend a better one?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmm. I don't really want to tell you you have to do that, there shouldn't be anything that badly wrong with Miracle C, altho I have never used it and so don't know anything about the IDE settings, etc.

    Try compiling this and see if you still get an error.
    Code:
    #include <sys/types.h>
    
    enum trace_item_type {
                    ti_NOP,
                    ti_RTYPE,
                    ti_ITYPE,
                    ti_LOAD,
                    ti_STORE,
                    ti_BRANCH,
                    ti_JTYPE,
                    ti_SPECIAL,
                    ti_JRTYPE
    };
     
    struct trace_item {
    u_int8_t type;
    u_int8_t sReg_a;
    u_int8_t sReg_b;
    u_int8_t dReg;
    u_int32_t PC;
    u_int32_t Addr;
    };
    
    int main() {
    
    	return 0;  
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i had that miracle C thing at one time, i remember the editor was awful, used for about one hour, i cant comment on the compiler, there are a few good free IDEs (integrated development environment) i got code:blocks, its great, download it, it's the most fun you can have with your clothes on

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Miracle C compiler...do you recommend a better one?
    It's a PoS (seriously).
    Do as rogster001 says and get code::blocks

    For starters, it really is free, and not some expensive nagware.
    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.

  10. #10
    sparklezilla3
    Join Date
    Mar 2010
    Posts
    41
    okay i downloaded code blocks.

    Here is the code I have to work with now, I have to count branches with the input of a trace file...

    however I am getting a lot of errors, and would like some direction for debugging.

    here is the code:
    Code:
    #include <stdio.h>
    
    #include <mytrace.h>
    
    #include <sys/types.h>
    
     
    
    #define TRACE_BUFSIZE 1024*1024
    
     
    
    static FILE *trace_fd;
    
    static int trace_buf_ptr;
    
    static int trace_buf_end;
    
    static struct trace_item *trace_buf;
    
     
    
    void trace_init()
    
    {
    
      trace_buf = malloc(sizeof(struct trace_item) * TRACE_BUFSIZE);
    
     
    
      if (!trace_buf) {
    
        fprintf(stdout, "** trace_buf not allocated\n");
    
        exit(-1);
    
      }
    
     
    
      trace_buf_ptr = 0;
    
      trace_buf_end = 0;
    
    }
    
     
    
    void trace_uninit()
    
    {
    
      free(trace_buf);
    
      fclose(trace_fd);
    
    }
    
     
    
    int trace_get_item(struct trace_item **item)
    
    {
    
      int n_items;
    
     
    
      if (trace_buf_ptr == trace_buf_end) {
    
        // get new data
    
        n_items = fread(trace_buf, sizeof(struct trace_item), TRACE_BUFSIZE, trace_fd);
    
        if (!n_items) return 0;
    
     
    
        trace_buf_ptr = 0;
    
        trace_buf_end = n_items;
    
      }
    
     
    
      *item = &trace_buf[trace_buf_ptr];
    
      trace_buf_ptr++;
    
     
    
      return 1;
    
    }
    
     
    
    int main(int argc, char **argv)
    
    {
    
      struct trace_item *tr_entry;
    
      size_t size;
    
      char *trace_file_name;
    
      int trace_view_on = 0;
    
      
    
      unsigned char t_type = 0;
    
      unsigned char t_sReg_a= 0;
    
      unsigned char t_sReg_b= 0;
    
      unsigned char t_dReg= 0;
    
      unsigned int t_PC = 0;
    
      unsigned int t_Addr = 0;
    
     
    
      unsigned int n_items = 0;
    
      unsigned int n_nop = 0;
    
      unsigned int n_rtype = 0;
    
      unsigned int n_itype = 0;
    
      unsigned int n_load = 0;
    
      unsigned int n_store = 0;
    
      unsigned int n_branch = 0;
    
      unsigned int n_jtype = 0;
    
      unsigned int n_jrtype = 0;
    
      unsigned int n_special = 0;
    
      unsigned int n_unknown = 0;
    
      unsigned int n_mem_inst = 0;
    
      unsigned int n_cont_inst = 0;
    
     
    
      if (argc == 1) {
    
        fprintf(stdout, "\nUSAGE: tv <trace_file> <switch - any character>\n");
    
        fprintf(stdout, "\n(switch) to turn on or off individual item view.\n\n");
    
        exit(0);
    
      }
    
        
    
      trace_file_name = argv[1];
    
      trace_view_on = (argc == 3);
    
     
    
      fprintf(stdout, "\n ** opening file %s\n", trace_file_name);
    
     
    
      trace_fd = fopen(trace_file_name, "rb");
    
     
    
      if (!trace_fd) {
    
        fprintf(stdout, "\ntrace file %s not opened.\n\n", trace_file_name);
    
        exit(0);
    
      }
    
     
    
      trace_init();
    
     
    
      while(1) {
    
        size = trace_get_item(&tr_entry);
    
       
    
        if (!size) {
    
          printf("+ total # items : %u\n", n_items);
    
          printf("+ # nop: %u\n", n_nop);
    
          printf("+ # rtype: %u\n", n_rtype);
    
          printf("+ # itype: %u\n", n_itype);
    
          printf("+ # load: %u\n", n_load);
    
          printf("+ # store: %u\n", n_store);
    
          printf("+ # branch: %u\n", n_branch);
    
          printf("+ # jtype: %u\n", n_jtype);
    
          printf("+ # jrtype: %u\n", n_jrtype);
    
          printf("+ # special: %u\n", n_special);
    
          printf("+ # unknown: %u\n", n_unknown);
    
          printf("+ # Avg mem instr: %4.2f\n", (double)n_mem_inst/(double)n_items);
    
          printf("+ # Avg control instr: %4.2f\n", (double)n_cont_inst/(double)n_items);
    
          break;
    
        }
    
        else{
    
          n_items++;
    
          t_type = tr_entry->type;
    
          t_sReg_a = tr_entry->sReg_a;
    
          t_sReg_b = tr_entry->sReg_b;
    
          t_dReg = tr_entry->dReg;
    
          t_PC = tr_entry->PC;
    
          t_Addr = tr_entry->Addr;
    
        }  
    
     
    
        switch(tr_entry->type) {
    
     
    
          case ti_NOP:
    
            if (trace_view_on){
    
              printf("[item %d] entry NOP: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items, tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
     
    
            n_nop++;
    
            break;
    
     
    
          case ti_RTYPE:
    
            if (trace_view_on){
    
              printf("[item %d] entry RTYPE: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items, tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
     
    
            n_rtype++;
    
            break;
    
     
    
         case ti_ITYPE:
    
            if (trace_view_on){
    
              printf("[item %d] entry ITYPE: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items, tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
     
    
            n_itype++;
    
            break;
    
                    
    
         case ti_LOAD:
    
            if (trace_view_on){
    
              printf("[item %d] entry LOAD: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items,  tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
     
    
            n_load++; n_mem_inst++;
    
            break;
    
     
    
         case ti_STORE:
    
            if (trace_view_on){
    
              printf("[item %d] entry STORE: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items, tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
     
    
            n_store++; n_mem_inst++;
    
            break;
    
     
    
         case ti_BRANCH:
    
            if (trace_view_on){
    
              printf("[item %d] entry BRANCH: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items, tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
            n_branch++; n_cont_inst++;
    
            break;
    
     
    
         case ti_JTYPE:
    
            if (trace_view_on){
    
              printf("[item %d] entry JTYPE: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items, tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
     
    
            n_jtype++; n_cont_inst++;
    
            break;
    
     
    
         case ti_SPECIAL:
    
            if (trace_view_on){
    
              printf("[item %d] entry SPECIAL: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items, tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
     
    
            n_special++; n_cont_inst++;
    
            break;
    
     
    
         case ti_JRTYPE:
    
            if (trace_view_on){
    
              printf("[item %d] entry JRTYPE: (PC: %x) (sReg_a: %d) (sReg_b: %d) (dReg: %d) (addr: %x)\n", n_items, tr_entry->PC, tr_entry->sReg_a, tr_entry->sReg_b, tr_entry->dReg, tr_entry->Addr);
    
            }
    
     
    
            n_jrtype++; n_cont_inst++;
    
            break;
    
     
    
          default:
    
                    n_unknown++;
    
                    break;
    
        }
    
      }
    
     
    
      trace_uninit();
    
     
    
      exit(0);
    
    }
    Here is the header:

    Code:
    #include <sys/types.h>
    
    enum trace_item_type
    
    {
    
                    ti_NOP,
    
                    ti_RTYPE,
    
                    ti_ITYPE,
    
                    ti_LOAD,
    
                    ti_STORE,
    
                    ti_BRANCH,
    
                    ti_JTYPE,
    
                    ti_SPECIAL,
    
                    ti_JRTYPE
    
    };
    
     
    
    struct trace_item
    
    {
    
    u_int8_t type;
    
    u_int8_t sReg_a;
    
    u_int8_t sReg_b;
    
    u_int8_t dReg;
    
    u_int32_t PC;
    
    u_int32_t Addr;
    
    };

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by husslela2 View Post
    okay i downloaded code blocks.

    Here is the code I have to work with now, I have to count branches with the input of a trace file...

    however I am getting a lot of errors, and would like some direction for debugging.
    The fun part about error messages is that the error messages tell you what the errors are. So your direction for debugging is to read the error messages, and fix the errors they tell you about.

    (Granted, if you get a lot of errors, some of them may be "spurious" or relate to the same missing character, etc.)

  12. #12
    sparklezilla3
    Join Date
    Mar 2010
    Posts
    41
    I am not very good at C code...and this is all I have to work with...

    For example, I have an error on line #133 saying Error: invalid use of undefined type `struct trace_item`. I don't know how to start debugging that..

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Line 133? Line 133 of the posted code (assuming you don't have all those blank lines in between every single line) is
    Code:
    n_nop++;
    So first I guess is to figure out which line is line 133.

  14. #14
    sparklezilla3
    Join Date
    Mar 2010
    Posts
    41
    line #133 is:

    error: invalid use of undefined type `struct trace_item'
    Code:
    t_sReg_b= tr_entry->sReg_b;
    then on line #22

    which is:

    Code:
    static struct trace_item *trace_buf;
    throws the error of:
    error: foward declaration of `struct trace item'
    Last edited by husslela2; 03-24-2010 at 02:50 PM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you need to include the header file that defines that type, then. Is that what mytrace.h is supposed to be? Your header files go in "quotes", not <angle brackets>. (Your probably also then getting an error for that line, along the lines of "file not found".)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. backward debugging in Visual Studio??
    By George2 in forum Tech Board
    Replies: 12
    Last Post: 11-05-2006, 02:17 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Replies: 8
    Last Post: 04-28-2003, 07:29 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. 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