I am debugging some Linux C code in a signal handler for floating point exceptions. The goal is to check the floating point registers, print some information, and then abort. I get a segmentation fault when attempting to printf the result of (char)('0' + phyreg)
.
struct ucontext * uc = (struct ucontext *) data;
fpregset_t fp = uc -> uc_mcontext.fpregs;
int top = (fp -> sw >> 11) & 0x07;
int i,j,k;
for (i = 0; i < 8; i++) {
static const char * tags [] = {
"valid", "zero", "invalid/infin", "empty"
};
int phyreg = (top + i) & 0x07;
struct _libc_fpreg* r = &(fp -> _st [phyreg]);
const char* regExp = (((r->exponent & 0x8000) != 0) ? "-" : "+");
printf (" FP %s: Mantissa= %s",
(char) ('0' + phyreg), // reg stack (SIGSEGV here)
regExp); // register exponent sign
j = (r->significand[3] >> 15) & 0x01;
printf ("%s.",(char) ('0' + j)); // mantissa (Also SIGSEGV here when
// previous SIGSEGV is commented out)
...
}
It's not the calculation of (char)('0' + phyreg)
that's the problem, because when I move it to a separate line and store the result in a temp variable, I don't get the segfault until the printf tries to display the temp variable. So, where's the bug that causes the segfault?