c - segmentation fault: cache lab -


my cache simulator file generates segmentation fault error when run in command line using command:

./csim.c -s 1 -e 1 -b 1 -t traces/yi2.trace.  

i not familiar segmentation fault errors.

any appreciated.

edit: used gdb find this

program received signal sigsegv, segmentation fault. strcmp () @ ../sysdeps/x86_64/multiarch/../strcmp.s:132 132 ../sysdeps/x86_64/multiarch/../strcmp.s: no such file or directory. (gdb) bt #0  strcmp () @ ../sysdeps/x86_64/multiarch/../strcmp.s:132 #1  0x00007ffff7deb1a5 in _dl_name_match_p (name=0x400459 "libm.so.6",      map=0x7ffff7ffe1c8) @ dl-misc.c:289 #2  0x00007ffff7de402f in do_lookup_x (new_hash=new_hash@entry=193502747,      old_hash=old_hash@entry=0x7fffffffe200,      result=result@entry=0x7fffffffe210, scope=<optimized out>,      i=<optimized out>, i@entry=0, flags=flags@entry=1,                skip=skip@entry=0x0,      undef_map=undef_map@entry=0x7ffff7ffe1c8) @ dl-lookup.c:462 #3  0x00007ffff7de4961 in _dl_lookup_symbol_x (undef_name=0x4004bc "pow",      undef_map=0x7ffff7ffe1c8, ref=ref@entry=0x7fffffffe2c8,      symbol_scope=0x7ffff7ffe520, version=0x7ffff7ff7f58,      type_class=type_class@entry=1, flags=1, skip_map=skip_map@entry=0x0)     @ dl-lookup.c:737 #4  0x00007ffff7de9527 in _dl_fixup (l=<optimized out>,      reloc_arg=<optimized out>) @ ../elf/dl-runtime.c:111 #5  0x00007ffff7df04d5 in _dl_runtime_resolve ()     @ ../sysdeps/x86_64/dl-trampoline.s:45 #6  0x000000000040109d in main (argc=9, argv=0x7fffffffe4c8) @ csim.c:497 

code:

#include <stdlib.h> #include <stdio.h> #include <getopt.h> #include <strings.h> #include <unistd.h> #include <math.h> #include "cachelab.h"  typedef struct lines{      short int valid;     long long int tag;     char block;     int last, n_accesses; } line;  typedef struct sets{      line *set_line; } set;  typedef struct caches{      set *cache_set; } cache;  typedef struct cache_evaluations{      int hits, misses, evictions; } evaluation;  typedef struct hme{    evaluation *eval; } hme;  typedef struct data_reads{      int s, e, b, n_sets, n_lines; } data;  int verbosity;  void help(){      printf("usage: [-hv] -s <num> -e <num> -b <num> -t <file>\n\n");     printf("  -h         message.\n");     printf("  -v         verbose flag (optional).\n");     printf("  -s <num>   number of set index bits.\n");     printf("  -e <num>   number of lines per set.\n");     printf("  -b <num>   number of block offset bits.\n");     printf("  -t <file>  trace file.\n");      exit(0); }  void create_cache(cache c_cache, data r_data){      set c_set;     line c_line;     int n_sets = r_data.n_sets;     int n_lines = r_data.n_lines;     int set_i, line_j;     c_cache.cache_set = (set *) (malloc(sizeof(set) * n_sets));      for(set_i = 0; set_i < n_sets; set_i += 1){          c_set.set_line = (line *) (malloc(sizeof(line) * n_lines));         c_cache.cache_set[set_i] = c_set;          for(line_j = 0; line_j < n_lines; line_j += 1){              c_line.valid = 0;             c_line.tag = 0;             c_line.n_accesses = 0;             c_line.last = 0;             c_set.set_line[line_j] = c_line;         }     } }  void empty_cache(cache c_cache, data r_data){      set set_ptr;     int n_sets = r_data.n_sets;     int set_i;     for(set_i = 0; set_i < n_sets; set_i += 1){         set_ptr = c_cache.cache_set[set_i];         if(set_ptr.set_line != null)           free(set_ptr.set_line);     } }  int find_lru(set tmp_set, data r_data){      int min_count = tmp_set.set_line[0].n_accesses;     int n_lines = r_data.n_lines;     int line_j;      for(line_j = 0; line_j < n_lines; line_j += 1){          if(tmp_set.set_line[line_j].n_accesses < min_count){            min_count = tmp_set.set_line[line_j].n_accesses;         }     }      for(line_j = 0; line_j < n_lines; line_j += 1){          if(tmp_set.set_line[line_j].n_accesses != min_count){            tmp_set.set_line[line_j].n_accesses += 1;         }     }     return min_count; }  int eviction(set tmp_set, data r_data){      int lru = find_lru(tmp_set, r_data);     int n_lines = r_data.n_lines;     int evict_idx;     int line_n;      for(line_n = 0; line_n < n_lines; line_n += 1){          if((tmp_set.set_line[line_n].last == 0) && tmp_set.set_line[line_n].n_accesses == lru){              tmp_set.set_line[line_n].valid = 0;             tmp_set.set_line[line_n].tag = 0;             tmp_set.set_line[line_n].n_accesses = 0;             tmp_set.set_line[line_n].last = 0;             evict_idx = line_n;             break;         }         tmp_set.set_line[line_n].n_accesses += 1;     }     return evict_idx; }  int find_line(set tmp_set, data r_data, hme c_hme){      short int cont = 1;     int n_lines = r_data.n_lines;     int line_idx;     int line_n;      for(line_n = 0; line_n < n_lines; line_n += 1){          tmp_set.set_line[line_n].n_accesses += 1;          if(!(tmp_set.set_line[line_n].valid)){              cont = 0;             line_idx = line_n;             break;         }     }      if(cont){        c_hme.eval[0].evictions += 1;       line_idx = eviction(tmp_set, r_data);     }     return line_idx; }  int mask(int b){      int masked_b = pow(2.0,b) - 1;   return masked_b; }  void cache_sim(cache c_cache, hme c_hme, data r_data, unsigned long long int address){      int n_lines = r_data.n_lines;     unsigned long long int n_tag = address >> (r_data.s + r_data.b);     int set_index = (address >> r_data.b) & mask(r_data.s);     set tmp_set = c_cache.cache_set[set_index];     int block_offset = address & mask(r_data.b);     char n_block = (char) block_offset;     int hit = 0;     int line_j;      (line_j = 0; line_j < n_lines; line_j += 1){          tmp_set.set_line[line_j].n_accesses += 1;         tmp_set.set_line[line_j].last = 0;          if(tmp_set.set_line[line_j].valid && tmp_set.set_line[line_j].tag == n_tag){              tmp_set.set_line[line_j].last = 1;             hit = 1;             break;         }     }      if(hit)         c_hme.eval[0].hits += 1;      else{          c_hme.eval[0].misses += 1;         int line_n = find_line(tmp_set, r_data, c_hme);         tmp_set.set_line[line_n].valid = 1;         tmp_set.set_line[line_n].tag = n_tag;         tmp_set.set_line[line_n].block = n_block;         tmp_set.set_line[line_n].last = 1;         tmp_set.set_line[line_n].n_accesses += 1;       } }  int main(int argc, char **argv){      char cl_arg, *tracefile;     data r_data = {0};      while((cl_arg = getopt(argc,argv,"hv:s:e:b:t")) != -1){         switch(cl_arg){              case 'h':                 help();             case 'v':                 verbosity = 1;                 break;             case 's':                 r_data.s = atoi(optarg);                 break;             case 'e':                 r_data.e = atoi(optarg);                 break;             case 'b':                 r_data.b = atoi(optarg);                 break;             case 't':                 tracefile = optarg;                 break;             default:                 help();                 exit(1);         }     }      cache c_cache;     evaluation c_eval;     c_eval.hits = 0;     c_eval.misses = 0;     c_eval.evictions = 0;     hme c_hme;     c_hme.eval[0] = c_eval;     r_data.n_sets = pow(2, r_data.s);     r_data.n_lines = r_data.e;     create_cache(c_cache, r_data);     unsigned long long int address;     char instr;     int size;      file *file;     file = fopen(tracefile,"r");      if (file != null){         while (fscanf(file, " %c %llx,%d", &instr, &address, &size) == 3){             switch(instr){                  case 'l':                     cache_sim(c_cache, c_hme, r_data, address);                 break;                 case 's':                     cache_sim(c_cache, c_hme, r_data, address);                 break;                 case 'm':                     cache_sim(c_cache, c_hme, r_data, address);                     cache_sim(c_cache, c_hme, r_data, address);                 break;                 default:                 break;             }         }     }      printsummary(c_hme.eval[0].hits, c_hme.eval[0].misses, c_hme.eval[0].evictions);     empty_cache(c_cache, r_data);     fclose(file);     return 0; } 

you calling (void ...) functions value of struct. struct seen caller (main()) not altered; local copy. example (using int struct member instead of pointer):

#include <stdio.h>  typedef struct {         int val;         } omg;  void wtf(omg this);  /* function change value of ??? */ void wtf(omg this) { this.val = 42; }  /* let's call ... */ int main(void) {  omg jesus = {0};  wtf( jesus); printf("%d\n", jesus.val);  return 0; } 

result:

plasser@pisbak:$ cc -wall omg.c plasser@pisbak:$ ./a.out 0 plasser@pisbak:$  

update: same (minimised) original program:

#include <stdlib.h> #include <stdio.h> #include <getopt.h> #include <string.h> #include <unistd.h> #include <math.h> #pragma include "cachelab.h"  typedef struct lines{     short int valid;     long long int tag;     char block;     int last, n_accesses; } line;  typedef struct sets{     line *set_line; } set;  typedef struct caches{     set *cache_set; } cache;  typedef struct cache_evaluations{     int hits, misses, evictions; } evaluation;  typedef struct data_reads{     int s, e, b, n_sets, n_lines; } data;  void create_cache(cache c_cache, data r_data){      set c_set;     line c_line;     int n_sets = r_data.n_sets;     int n_lines = r_data.n_lines;     int set_i, line_j;      c_cache.cache_set = malloc(sizeof(set) * n_sets);     if (!c_cache.cache_set) fprintf( stderr, "omg1!\n");      for(set_i = 0; set_i < n_sets; set_i++){          c_set.set_line = malloc(sizeof(line) * n_lines);         if (!c_set.set_line) fprintf( stderr, "omg2!\n");          c_cache.cache_set[set_i] = c_set;          for(line_j = 0; line_j < n_lines; line_j++){             c_line.valid = 0;             c_line.tag = 0;             c_line.n_accesses = 0;             c_line.last = 0;             c_set.set_line[line_j] = c_line;         }     } }  int main(int argc, char **argv){      data r_data = {0};     cache c_cache = {null};      r_data.n_sets = 8;     r_data.n_lines = 4;     create_cache(c_cache, r_data);     fprintf( stderr, " c_cache.cache_set = %p\n", (void*) c_cache.cache_set );     if ( !c_cache.cache_set) fprintf( stderr, "omg3!\n" );      return 0; } 

result:

plasser@pisbak:$ cc -wall -pg omg2.c -lm omg2.c:8:0: warning: ignoring #pragma include  [-wunknown-pragmas]  #pragma include "cachelab.h"  ^ plasser@pisbak:$ ./a.out  c_cache.cache_set = (nil) omg3! plasser@pisbak:$  

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -