0

I am trying to execute the following piece of code. It passes compilation but fails at runtime with error Segmentation fault (core dumped). I am trying to simulate a cache but am stuck with some pointer inconsistency(probably). `

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE

int hit=0,miss=0,evic=0,verbose=0;

typedef struct{
    long int tag;
    int valid;
    int age;
}cLine;

typedef struct{
    cLine *Lines;
}cSet;

typedef struct{
    cSet *Sets;
}cache;

cache my_cache(int s, int E, int b){
    cache cach; 
    int i=0,j=0;
    cach.Sets=malloc(sizeof(cSet)*(1<<s));
    for (i = 0; i < (1<<s); i++) {
      cach.Sets[i].Lines = malloc(sizeof(cLine) * (1<<E));
      for (j=0;j<(1<<E);j++){
        cach.Sets[i].Lines[j].tag=0;
        cach.Sets[i].Lines[j].valid=0;
        cach.Sets[i].Lines[j].age=0;
      }
    }

    return cach;        
}

void printSummary(int hits, int misses, int evictions)
{
    printf("hits:%d ,misses:%d ,evictions:%d",hits,misses,evictions);
}

void increment_age(cSet *set, int E){
    printf("\n\ninside increment_age");
    int j=0;
    for (j=0;j<(1<<E);j++){
        set->Lines[j].age++;
    }

}

cLine* find_adr(cache *MyCache,long int mem_tag,int set,int E){
    printf("\n\ninside find_adr");
    increment_age(&MyCache->Sets[set], E);
    int j=0;
    for (j=0;j<(1<<E);j++){
        if(MyCache->Sets[set].Lines[j].tag==mem_tag && MyCache->Sets[set].Lines[j].valid==1){
            hit++;
            if(verbose==1){
                printf("hit ");
            }
            return &MyCache->Sets[set].Lines[j];
        }
    }
    if(verbose==1){
                printf("miss ");
            }
    miss++;
    return NULL;
}

cLine* find_empty(cache *MyCache,int set, int E){
    printf("\n\ninside find_empty");
    int j=0;
    for (j=0;j<(1<<E);j++){
        if(MyCache->Sets[set].Lines[j].valid==0){
            return &MyCache->Sets[set].Lines[j];
        }
    }
    return NULL;
}

cLine* find_evicable(cache *MyCache,int set,int E){
    cLine *ptrLine=&MyCache->Sets[set].Lines[0];
    evic++;
    if(verbose==1){
                printf("eviction ");
            }
    int j=0;
    for (j=0;j<(1<<E);j++){
        if(MyCache->Sets[set].Lines[j].age>ptrLine->age){
            ptrLine=&(MyCache->Sets[set].Lines[j]);
        }
    }
    return ptrLine;
}

void operate(cache *MyCache,long int mem_tag,int set,int E){
    cLine *opLine;
    opLine=find_adr(MyCache,mem_tag,set,E);
    if (opLine==NULL){
        opLine=find_empty(MyCache,set, E);
        if(opLine==NULL){
            opLine=find_evicable(MyCache,set,E);
        }
    }
    opLine->age=0;
    opLine->tag=mem_tag;

}
int main(int argc, char *argv[])
{
    int c;
    int hflg=0, vflg=0, sflg=0, Eflg=0, bflg=0;
    FILE *trc=NULL;
    char *tflg=NULL;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    long int tag_size;//=(32-sflg-bflg);
    cache newcache=my_cache(sflg,Eflg,bflg);

    while ((c=getopt(argc, argv, "hvs:E:b:t:"))!=-1)
    {
        switch(c)
        {
            case 'h':
                hflg=1;
                printf("argument h was specified\n");
                break;
            case 'v':
                vflg=1;verbose=1;
                printf("argument v was specified\n");
                break;
            case 's':
                if(optarg == NULL )
                {
                    printf("mandatory argument %c is not specified\n",optopt);
                    break;
                }
                else
                {
                    sflg=atoi(optarg);
                    printf("the value of s is %d\n",sflg);
                    break;
                }
            case 'E':
                if(optarg == NULL )
                {
                    printf("mandatory argument %c is not specified\n",optopt);
                    break;
                }
                else
                {
                    Eflg=atoi(optarg);
                    printf("the value of E is %d\n",Eflg);
                    break;
                }
            case 'b':
                if(optarg == NULL )
                {
                    printf("mandatory argument %c is not specified\n",optopt);
                    break;
                }
                else
                {
                    bflg=atoi(optarg);
                    printf("the value of b is %d\n",bflg);
                    break;
                }
            case 't':
                if(optarg == NULL )
                {
                    printf("mandatory argument %c is not specified\n",optopt);
                    break;
                }
                else
                {
                    tflg=optarg;
                    printf("the value of t is %s\n",tflg);
                    char * str3 = (char *) malloc(1 + strlen("./")+ strlen(optarg) );
                    strcpy(str3, "./");
                    strcat(str3, optarg);
                    trc=fopen(str3,"r");
                    //printf("%s", str3);
                    //free(str3);
                    break;
                }           
        }
    }
    tag_size=(32-sflg-bflg);

if (trc == NULL){
    printf("invalid input file");
    }
else {
    char cmd;
    long int adr;
    int size;
    long int intag,inset;
    while (fscanf(trc, " %c %lx,%d", &cmd, &adr, &size) == 3) {

        switch(cmd) {
            case 'I':
                //printf("%c, %lx\n",cmd,adr);
                //long int intag=adr>>(sflg+bflg);
                //int inset=(adr<<tag_size)>>(tag_size+bflg);
                //printf("%lx, %lx\n",intag,inset);
                break;
            case 'L':
                printf("\n%c, %lx ",cmd,adr);
                intag=adr>>(sflg+bflg);
                inset=(adr<<tag_size)>>(tag_size+bflg);
                operate(&newcache,intag,inset,Eflg);
                //printf("tag:%lx, Set:%lx\n",intag,inset);
                //par = run_sim(sim_cache, par, address);
                break;
            case 'S':
                printf("\n%c, %lx ",cmd,adr);
                intag=adr>>(sflg+bflg);
                inset=(adr<<tag_size)>>(tag_size+bflg);
                operate(&newcache,intag,inset,Eflg);
                //printf("tag:%lx, Set:%lx\n",intag,inset);
                //par = run_sim(sim_cache, par, address);
                break;
            case 'M':
                printf("\n%c, %lx ",cmd,adr);
                intag=adr>>(sflg+bflg);
                inset=(adr<<tag_size)>>(tag_size+bflg);
                operate(&newcache,intag,inset,Eflg);
                operate(&newcache,intag,inset,Eflg);
                //printf("tag:%lx, Set:%lx\n",intag,inset);
                //par = run_sim(sim_cache, par, address);
                    //par = run_sim(sim_cache, par, address);   
                break;
            default:
                break;
        }
    }
}



       fclose(trc);
       printSummary(0, 0, 1);
    //printf("\n%d",1<<4);
    return 0;
}

` Can someone please help...

  • Debugger. But maybe this `cach.Sets=malloc(sizeof(cSet)*(1< – ameyCU Nov 14 '15 at 19:15
  • GDB - to debug read here http://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb/ – ameyCU Nov 14 '15 at 19:18
  • I am trying to execute this code using the following commands; `gcc -o alpha csim.c` `./alpha -s 4 -E 1 -b 4 -t traces/yi.trace` Following are the contents of yi.trace ` L 10,1` ` M 20,1` ` L 22,1` ` S 18,1` ` L 110,1` ` L 210,1` ` M 12,1` Following is the output; `$ ./alpha -s 4 -E 1 -b 4 -t traces/yi.trace` `the value of s is 4` `the value of E is 1` `the value of b is 4` `the value of t is traces/yi.trace` `L, 10` `inside find_adr` `Segmentation fault (core dumped)` – Jazib Jamil Nov 14 '15 at 19:22
  • Are you seriously expecting us to debug this big chunk of code? There are tools for that, so that humans are freed from such a chore. – cadaniluk Nov 14 '15 at 19:28
  • I am getting the error when i call `increment_age(&(MyCache->Sets[set]), E);` I am trying to debug it myself but a little help would be nice. – Jazib Jamil Nov 14 '15 at 19:38
  • So, use your debugger to look at the variables Mycache (a pointer) and set (an index). Do those have values you should expect? – Henrik Carlqvist Nov 14 '15 at 19:57
  • Resolved... Thanks everyone for pitching in. – Jazib Jamil Nov 15 '15 at 06:13

0 Answers0