// Sample program that shows a bug in gdb.  The bug is described
// from a C language perspective as well as from a machine
// language perspective (the latter will help the implementor
// in isolating the cause of the problem)
//
// The bug in terms of "C"
// Single stepping through this statement:
//      byte = (val1 & 0x00007f00) >> 8;
// causes byte to be assigned the incorrect value of 0x00 when it
// should have been assigned 0x02.
//
// The bug in terms of sparc machine code:
// Single stepping on a Shift Right Logical overwrites %o0 -
// the "srl %o0, 8, %o1" instruction changes the value of %o0
// to 0 during srl execution, resulting in both %o0 and %o1 
// to contain 0 (they should contain 0x0200 and 0x2 respectively).
//
// Conditions for the bug:
//
// 1) this behavior is manifested only when single stepping
// thru the program (step or next)
//
// 2) this only happens if the value being shifted is 0x0200!!!
// Try any other value (single step ok) and it works..
//
// compiled with "gcc -g gbug.c"
//

#include "stdio.h"

int main(int argc, char* argv[])
{
    unsigned long   val1 = 0x00000200;      // we want to extract 02 - fails
    unsigned long   val2 = 0x00001200;      // we want to extract 12 - works!
    unsigned char   byte;

    byte = 0;

    /*************start-line-that-fails-if-single-stepping**********/
    byte = (val1 & 0x00007f00) >> 8;
    /***************end-line-that-fails-if-single-stepping*********/

    printf("byte = %02x\n", byte);

    byte = (val2 & 0x00007f00) >> 8;
    printf("byte = %02x\n", byte);

    return 0;
}

