www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000186

When Created: 10/26/1997 22:18:59
Against DJGPP version: 2.01
By whom: ajohnson@baea.com.au
Abstract: Bug when using kbhit() in GDB under DOS
When the following program is run in GDB V4.16 in DOS (not in Windows
DOS box), the machine locks up. Neither Ctrl-Alt-Del or Ctrl-Break work.

Works fine in a Win 3.1 Dos box. Also works if the kbhit() is taken
out.


#include "bios.h"
#include <conio.h>
#include "keys.h"

/* Runs under DOS.
 * Fails if run under GDB in DOS
 * Runs if run in Windows (with or without GDB)
 *
 * Will run under GDB in DOS if the_bios_keybrd line is uncommented
 */

int main()
{
  while (1) {

//    _bios_keybrd(_KEYBRD_READY);

    if (kbhit()) {
      if (getkey() == K_Escape)
	return 0;
      else
	cprintf(".");
    }
  }
}

Note added: 04/15/1999 11:00:09
By whom: eliz@is.elta.co.il
This problem happens in ALL DJGPP debuggers, including those compiled
with v2.02.  However, once the loop where the program spends its time
is made less trivial (e.g., by adding a call to another function), the
problem disappear.

So it seems this is somehow related to problems with exceptions in
tight register-based loops that don't touch any memory.  In other
words, this is a limitation of DJGPP.

Workaround added: 04/15/1999 11:00:46
By whom: eliz@is.elta.co.il
Insert a call to `printf' or some other function into the loop,
and the problem goes away.

Closed on 04/15/1999 11:00:34: Basic limitation of DJGPP.
By whom: eliz@is.elta.co.il

Workaround added: 08/13/2005 18:55:56
By whom: lijon@kymatica.com
looking at kbhit.c, the problem seems to be this statement:

if (_farpeekw(_dos_ds, 0x41a) == _farpeekw(_dos_ds, 0x41c))
    return 0;

If this is commented out, the problem dissapears. So a simple workaround is to create an alternative kbhit2 like this:

int kbhit2(void) {
  __dpmi_regs r;
  r.h.ah = 0x11;
  __dpmi_int(0x16, &r);
  if (r.x.flags & 0x40) /* Z */
    return 0;
  return 1;
}

link this with your app, and pass -Dkbhit=kbhit2 in CFLAGS when you want to be
able to run gdb on your app under DOS.

Jonatan Liljedahl



  webmaster     delorie software   privacy  
  Copyright © 2010   by DJ Delorie     Updated Jul 2010