From: Andrea Monaco <andrea.monaco@autistici.org>
To: gdb@sourceware.org
Subject: Program with pseudo-tty behaving differently in single-step
Date: Fri, 18 Nov 2022 20:54:13 +0100 [thread overview]
Message-ID: <87a64om42i.fsf@autistici.org> (raw)
I have a program that behaves very differently when single-stepping and
when running freely in gdb.
The first part of the program opens a pseudo-terminal pair in the usual
way. This pair includes a master (that sort of simulates the user: you
can read from master and write to master like a human would do) and a
slave (that the child process uses as a terminal for stdin and stdout,
receiving what you send to master and sending back to master).
The child process runs bc (a calculator). The parent sends the line
"1+1\n" and reads back the subsequent line.
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
char buffer [1024];
int master, slave;
ssize_t
readl (int fd, char *buf)
{
ssize_t i = 0;
do
{
read (fd, buf+i, 1);
}
while (buf [i++] != '\n');
return i;
}
int
main (int argc, char *argv [])
{
int i;
char *name;
pid_t child;
master = getpt ();
if (master < 0)
{
perror ("getpt() failed");
return 1;
}
if (grantpt (master) < 0 || unlockpt (master) < 0)
{
perror ("grantpt() or unlockpt() failed");
return 1;
}
name = ptsname (master);
if (!name)
return 1;
slave = open (name, O_RDWR);
if (slave < 0)
{
perror ("could not open slave");
return 1;
}
/* until now, we opened a pseudo-tty pair in the standard way */
child = fork ();
if (child < 0)
{
perror ("fork failed");
return 1;
}
if (!child)
{
/* in the child, we close stdin and stdout and substitute them
both with the slave pseudo-terminal for input and output */
close (1);
close (0);
if (dup (slave) == -1 || dup (slave) == -1)
{
perror ("dup failed");
return 1;
}
execlp ("bc", "bc", NULL);
perror ("could not exec bc");
}
/* in the parent, we skip the startup message of bc */
for (i = 0; i < 4; i++)
{
readl (master, buffer);
}
write (master, "1+1\n", strlen ("1+1\n"));
readl (master, buffer); /* we skip the echo line of 1+1 */
read (master, buffer, 3); /* now we read the result line */
buffer [3] = '\0';
printf ("%s", buffer);
return 0;
}
In gdb, when I do a run I get "1+1" as output. When I single-step, I
get 2 from the last printf instead, which is the desired output.
Can anyone replicate this? Is there a known explanation?
Andrea Monaco
reply other threads:[~2022-11-18 19:54 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a64om42i.fsf@autistici.org \
--to=andrea.monaco@autistici.org \
--cc=gdb@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).