A
A
Antony2015-12-01 11:57:52
linux
Antony, 2015-12-01 11:57:52

Why can fgets behave strangely when debugging through gdbserver?

Actually the problem is this - there is a program that requests data from the user via fgets.
When working without a debugger, everything is ok. If you run it through gdbserver, then the first two (exactly two, always, no more, no less) fgets work as expected, and the third is simply "skipped".
What could it possibly be?
upd.
"Working" example:

#include <stdio.h>
#include <stdlib.h>

char tmp[255]={0};

int main(void) {

  printf("Type somthing:");
  if(NULL == fgets(tmp, sizeof(tmp)-1, stdin))
  {
    printf("Unable to read string\n");
  }

  printf("You write '%s'",tmp);

  tmp[0]=0;

  if(NULL == fgets(tmp, sizeof(tmp)-1, stdin))
  {
    printf("Unable to read string\n");
  }
  printf("You write '%s'",tmp);

  tmp[0]=0;

  if(NULL == fgets(tmp, sizeof(tmp)-1, stdin))
  {
    printf("Unable to read string\n");
  }
  printf("You write '%s'",tmp);

  tmp[0]=0;

  if(NULL == fgets(tmp, sizeof(tmp)-1, stdin))
  {
    printf("Unable to read string\n");
  }
  printf("You write '%s'",tmp);

  tmp[0]=0;

  return EXIT_SUCCESS;
}

My "dialogue":

Type somthing:
Test
Test// This echoes back what I type.
You write 'Test
'You write '
'
2nd try
2nd try// This echoes back what I type.
You write '2nd try
'You write '
'

upd.
I localized this glitch to the Eclipse console, or to the Allocate Console (I don’t know for sure).
Vobshchem probably actually GDB here in general is not necessary.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question