Hi, I'm working on a simulator and I have found a spot where I repeatedly get a segfault and sometimes a stack smashing error. I believe with the code I will post here I do not get the stack smashing error, but I was earlier with similar code.
The problem occurs when I call fscanf to read from a file. The format of the file is one char, one space, eight chars, then a newline. "r 0affbe01" for example. The first char is never read correctly as when I print it out, I get a question mark inside a diamond. I am using fscanf like so: "fscanf(fptr, "%c %s\n", &instr->op, instr->hex)".
I'll try to give a little more background now.
the function is like so:
Code:
void readNextInstr(FILE *fptr, instruction *instr)
{
//char temp;
//char temp2[45];
/* With these uncommented, these values can be printed afterwards */
//instr->op = 'r';
//strcpy(instr->hex, "abcdef01");
#ifdef DEBUG_2
printf("*BDON: readNextInstr: Reading next line\n");
#endif
// Read one line
fscanf(fptr, "%c %s\n", &instr->op, instr->hex);
/* Alternatively, I've tried this with no luck */
//fscanf(fptr, "%c %s\n", &temp, temp2);
#ifdef DEBUG_2
printf("*BDON: readNextInstr: Read line. Op = %c\n", instr->op);
fflush(stdout);
#endif
return;
}
That function is in smp.c, in cache.h I define instruction
Code:
typedef struct _instruction
{
int proc;
char op; /* Operation (r/w) */
char hex[9]; /* String representation of address read from file */
address *addr; /* Pointer to address struct for the memory reference location */
} instruction;
Here is where I open the file in a function in smp.c. This is just a snippet.
Code:
// Open the trace file
trace_fptr = fopen(argv[6], "r");
if (trace_fptr == NULL)
{
fprintf(stderr, "ERROR: Could not open trace_file\n");
fflush(stderr);
return -1;
}
And these are the relevant statements in main.c where readNextInstr is called.
Code:
int main(int argc, char **argv)
{
// Instruction being executed
instruction *instr;
...
// Malloc the instruction struct and address struct inside instruction
instr = (instruction *)malloc( sizeof( instruction ));
instr->addr = (address *)malloc( sizeof( *instr->addr));
...
// Read the first instruction
readNextInstr(trace_fptr, instr);
...
}
Here is some output from gdb
Code:
Program received signal SIGSEGV, Segmentation fault.
0x00007f505fdd9f4d in _IO_vfscanf () from /lib/libc.so.6
(gdb) info registers
...
rip 0x7f505fdd9f4d 0x7f505fdd9f4d <_IO_vfscanf+1789>
...
Here is output from command line
Code:
*BDON: readNextInstr: Reading next line
*BDON: readNextInstr: Read line. Op = �
Instruction: 1 Read �� Processor nBDON: executeInstr: hex: ��, tag: 0, index: 0, offset: 0 op:
*BDON: DoRequest: tag: 0, index: 0, offset: 0 op:
Segmentation fault
After the "Instruction: 1" is output from other functions. Basically those are trying to print instr->op and instr->hex, too.
Also, I use most of this code for another simulator as well. The other one imports multilevel.h instead of smp.h so I have a few functions like readNextInstr in both multilevel.c and smp.c that have the same declaration but do different things. When I compile for this other simulator, everything works perfectly. The only part of the code that I posted that is dependent on which simulator is the function readNextInstr and the function that opens the file. Here is readNextInstr from multilevel.c and again, everything works fine with this implementation.
Code:
void readNextInstr(FILE *fptr, instruction *instr)
{
// Read one line
fscanf(fptr, "%c %s\n", &instr->op, instr->hex);
#ifdef DEBUG_2
printf("*BDON: readNextInstr: Read line. Op = %c \n", instr->op);
fflush(stdout);
#endif
return;
}
I have a feeling it is not something obvious and I may have not posted enough information but I am just trying to keep this somewhat simple for now. Please let me know if I should post more code or explain more/better.
Brandon