Page 1 of 2

Strange behaviour of compiler (Same code is not working now)

Posted: Sat Dec 24, 2016 11:47 am
by Agola
Hello forum.

I had a really really strange something today. I wrote my keyboard-read-key code about 2 weeks ago and today I used it again. It was working, but it is not working now.
The only modification was I did recompiling gcc with --with-newlib because I forgot it before. I returned back to old compiler, but problem is still persist.

It should work, but it is not working now.

Code: Select all

char keyboard_getchar()
{
    while (!(inb(0x64) & 0x1));

    unsigned char scancode = inb(0x60);

    if (scancode & 0x80) keyboard_getchar();
    
    char chr = key_list[scancode];
    terminal_putchar(chr);

    return chr;
}
I know it is bad, but I'm going to make it better later.

Surprisingly, it works if I make it:

Code: Select all

char keyboard_getchar()
{
X:
    while (!(inb(0x64) & 0x1));

    unsigned char scancode = inb(0x60);

    if (scancode & 0x80) goto X;
    
    char chr = key_list[scancode];
    terminal_putchar(chr);

    return chr;
}
That made me suspect about optimization for recursive functions, but with -O0 problem still persist, too :(

That problem is really strange for me. Also I have another now-not-working code, too.

Newlib was handling backspace itself great, but now it is not handling it, too.

Code: Select all

void rw_test()
{
   char *str = malloc(50);
   printf("Enter some string: ");
   gets(str);
   printf("The string you entered: %s\n", str);

   if (!strncmp("abcdef", str, 6))
   {
       printf("You entered abcdef, yay!");
   }
}
That code was working, but it is not working now, like the other code.

In that code if I send an output to newlib like xyz\b\b\babcdef;
It results with str[0] is x, str[1] is y, str[2] is z, str[3] is \b, str[4] is \b, str[5] is \b, str[6] is a, str[7] is b, str[8] is c, ...

printf prints the correct text, abcdef (surprisingly), but strncmp or strcmp says strings are not same. I'm sure it was working before.

That is my read syscall:

Code: Select all

int read(int file, char *ptr, int len) {
  if (file == 1) {
    *ptr = keyboard_getchar();
    return 1;
  }
  else
  {
    return -1; //Until fixing that problem I won't add VFS read code
  }
}
Is it a really unnoticed gcc bug or I didn't notice that until today (but I don't believe that, I'm sure it was working)

Help please [-o<

GCC version 6.2.0, Binutils version 2.27, Newlib version 2.2.0-1

Why it is happening?

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 12:08 pm
by Nable
> if (file == 1) {
stdin is usually associated with descriptor 0, not 1. And don't use "gets", please, just don't. One have "fgets" and others for a safer way.

> Newlib was handling backspace itself great, but now it is not handling it, too.
Backspace should be handled when descriptor is associated with TTY (see isatty() description) and related FILE structure has line buffering enabled.

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 12:17 pm
by Agola
Nable wrote:> if (file == 1) {
stdin is usually associated with descriptor 0, not 1. And don't use "gets", please, just don't. One have "fgets" and others for a safer way.

> Newlib was handling backspace itself great, but now it is not handling it, too.
Backspace should be handled when descriptor is associated with TTY (see isatty() description) and related FILE structure has line buffering enabled.
My bad, it is 0, just a copy mistake.
And yes, it should, and it was handling, something is really messed up in Agola :(

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 12:38 pm
by Nable
You can use isatty and fstat to find information about current state of 0 descriptor. Just print what they tell you and you'll get more clues for debugging.

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 12:50 pm
by Octocontrabass
Why is your keyboard_getchar() function polling the PS/2 controller instead of waiting for an interrupt handler to fill a buffer?

Why is your keyboard_getchar() function printing every key press to the screen?

The gets() function does not handle backspace. If you want it to handle backspace, your terminal must do that work before passing the input to the program. (The printf() function does handle backspace, though not necessarily in the way you expect.)

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 12:53 pm
by Agola
Nable wrote:You can use isatty and fstat to find information about current state of 0 descriptor. Just print what they tell you and you'll get more clues for debugging.
I made isatty() always return 1, nothing changes. fstat returns IFCHR
What about keyboard_getchar()

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 12:58 pm
by madanra
Are you using source control and automated builds (eg Makefile)? If not, I'd advise doing so ASAP, it will make things much easier, particularly in "same code is not working now" situations.

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 12:58 pm
by Agola
Octocontrabass wrote:Why is your keyboard_getchar() function polling the PS/2 controller instead of waiting for an interrupt handler to fill a buffer?

Why is your keyboard_getchar() function printing every key press to the screen?

The gets() function does not handle backspace. If you want it to handle backspace, your terminal must do that work before passing the input to the program. (The printf() function does handle backspace, though not necessarily in the way you expect.)
It was handling backspace before.

keyboard_getchar() is ugly, I know.

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 1:02 pm
by Agola
madanra wrote:Are you using source control and automated builds (eg Makefile)? If not, I'd advise doing so ASAP, it will make things much easier, particularly in "same code is not working now" situations.
I use Makefile, I backup sources every 2-3 weeks, but I don't have a backup for same code :(
Edit: fixed typo

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 1:09 pm
by Octocontrabass
Agola wrote:That made me suspect about optimization for recursive functions
Is keyboard_getchar() supposed to be recursive? If so, you've forgotten a "return" statement.

Code: Select all

if (scancode & 0x80) return keyboard_getchar();

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 1:21 pm
by Agola
Octocontrabass wrote:
Agola wrote:That made me suspect about optimization for recursive functions
Is keyboard_getchar() supposed to be recursive? If so, you've forgotten a "return" statement.

Code: Select all

if (scancode & 0x80) return keyboard_getchar();
Yay, that keyboard_getchar(). Looks I *accidentally* deleted "return", it explains "same code is not working now"

gets and scanf problems are still persist :(

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 1:40 pm
by Schol-R-LEA
Version control is different from general backups; the point of VCS is not just to have a spare copy (though it does serve as that as well) but to give you a history and an audit trail of changes. Also, unlike general backups, you usually want to push an update of your revisions every time you finish making a change to the code. Two or three commits a day during an eight hour day is entirely reasonable, and any time you make a change that you don't roll back out of, you should commit, at least to your local repository.

Also, if you use a 'cloud hosted' service such as Cloudforge or Github, you have an off-site backup to go along with your local backups, and you can use it to show the current state of the code to others simply by giving them a link to the repo.

Trust me, this is well worth it, especially since you can get free hosting for open-source projects from any of a dozen DCVS hosts, and some even offer free (if limited and ad-supported) closed-source repos.

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 1:42 pm
by Agola
Schol-R-LEA wrote:Version control is different from general backups; the point of VCS is not just to have a spare copy (though it does serve as that as well) but to give you a history and an audit trail of changes. Also, unlike general backups, you usually want to push an update of your revisions every time you finish making a change to the code. Two or three commits a day during an eight hour day is entirely reasonable, and any time you make a change that you don't roll back out of, you should commit, at least to your local repository.

Also, if you use a 'cloud hosted' service such as Cloudforge or Github, you have an off-site backup to go along with your local backups, and you can use it to show the current state of the code to others simply by giving them a link to the repo.

Trust me, this is well worth it, especially since you can get free hosting for open-source projects from any of a dozen DCVS hosts, and some even offer free (if limited and ad-supported) closed-source repos.
I use Visual Studio Code as my text editor, also it has built-in git support.
I will learn how to use it. Thanks.

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 4:49 pm
by alexfru
+1 on version control. I'm using git(hub) for my compiler project. I know it would be a mess without it. It's so much easier to see what changed and when and go back if needed. It's also great that I may create a branch of the project for a big feature I'm working on without disturbing the master branch all while both branches being in control. I can switch between the branches to compare/test the code with the new feature and without it. And when I'm done, I can easily merge the feature branch into the master branch. No surprises as to not remembering what you've changed and where. Accept the limits of your brain and use existing tools to extend them.

Re: Strange behaviour of compiler (Same code is not working

Posted: Sat Dec 24, 2016 6:20 pm
by Ch4ozz
Sites like this:
https://onlinedisassembler.com/static/home/

will help you out alot if you dont own your own IDA (leak)