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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Agola
Member
Member
Posts: 155
Joined: Sun Nov 20, 2016 7:26 am
Location: Somewhere

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

Post 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?
Keyboard not found!

Press F1 to run setup.
Press F2 to continue.
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

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

Post 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.
User avatar
Agola
Member
Member
Posts: 155
Joined: Sun Nov 20, 2016 7:26 am
Location: Somewhere

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

Post 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 :(
Keyboard not found!

Press F1 to run setup.
Press F2 to continue.
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

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

Post 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.
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

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

Post 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.)
User avatar
Agola
Member
Member
Posts: 155
Joined: Sun Nov 20, 2016 7:26 am
Location: Somewhere

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

Post 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()
Keyboard not found!

Press F1 to run setup.
Press F2 to continue.
madanra
Member
Member
Posts: 149
Joined: Mon Sep 07, 2009 12:01 pm

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

Post 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.
User avatar
Agola
Member
Member
Posts: 155
Joined: Sun Nov 20, 2016 7:26 am
Location: Somewhere

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

Post 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.
Keyboard not found!

Press F1 to run setup.
Press F2 to continue.
User avatar
Agola
Member
Member
Posts: 155
Joined: Sun Nov 20, 2016 7:26 am
Location: Somewhere

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

Post 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
Keyboard not found!

Press F1 to run setup.
Press F2 to continue.
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

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

Post 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();
User avatar
Agola
Member
Member
Posts: 155
Joined: Sun Nov 20, 2016 7:26 am
Location: Somewhere

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

Post 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 :(
Keyboard not found!

Press F1 to run setup.
Press F2 to continue.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

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

Post 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.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
User avatar
Agola
Member
Member
Posts: 155
Joined: Sun Nov 20, 2016 7:26 am
Location: Somewhere

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

Post 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.
Keyboard not found!

Press F1 to run setup.
Press F2 to continue.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

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

Post 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.
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

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

Post by Ch4ozz »

Sites like this:
https://onlinedisassembler.com/static/home/

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