Traceviewer

This is a discussion on Traceviewer within the C Programming forums, part of the General Programming Boards category; I'm working on a traceviewer to count branches in a runtime of the code...i have to import a trace file ...

  1. #1
    sparklezilla3
    Join Date
    Mar 2010
    Posts
    41

    Traceviewer

    I'm working on a traceviewer to count branches in a runtime of the code...i have to import a trace file (.tr).

    However I am getting a lot of errors regarding the trace item types.

    Here is my code i have:

    Code:
    #include <stdio.h>
    #include "mytrace.h"
    //#include <sys/types.h>
    // You can include the trace item structure and type by including a header file
    
    #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 my trace item header file that defines that trace types:
    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;
    };
    here are some of the errors i am receiving...

    |error: `u_int8_t' does not name a type|
    error: `u_int8_t' does not name a type|
    error: `u_int8_t' does not name a type|
    error: `u_int8_t' does not name a type|
    error: `uint32_t' does not name a type|
    error: `uint32_t' does not name a type|

    Any help will be appreciated..

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Instead of "u_int8_t", did you mean "uint8_t" (with no underscore there)? That type and similar ones like "uint32_t" are defined in stdint.h, so if you include that header file you might have better luck.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21