1 /* This is part of GDB, the GNU debugger.
3 Copyright 2011-2023 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /* Default READMORE method. */
29 #define READMORE_METHOD_DEFAULT 2
31 /* Default READMORE sleep time in miliseconds. */
32 #define READMORE_SLEEP_DEFAULT 10
34 /* Helper function. Intialize *METHOD according to environment variable
35 READMORE_METHOD, and *SLEEP according to environment variable
39 init_readmore (int *method
, unsigned int *sleep
, FILE **log
)
41 char *env
= getenv ("READMORE_METHOD");
43 *method
= READMORE_METHOD_DEFAULT
;
44 else if (strcmp (env
, "1") == 0)
46 else if (strcmp (env
, "2") == 0)
50 *method
= READMORE_METHOD_DEFAULT
;
52 env
= getenv ("READMORE_SLEEP");
54 *sleep
= READMORE_SLEEP_DEFAULT
;
58 env
= getenv ("READMORE_LOG");
62 *log
= fopen (env
, "w");
65 /* Wrap 'read', and modify it's behaviour using READ1 or READMORE style. */
68 read (int fd
, void *buf
, size_t count
)
70 static ssize_t (*read2
) (int fd
, void *buf
, size_t count
) = NULL
;
78 static int readmore_method
;
79 static unsigned int readmore_sleep
;
82 /* Use setenv (v, "", 1) rather than unsetenv (v) to work around
83 https://core.tcl-lang.org/tcl/tktview?name=67fd4f973a "incorrect
84 results of 'info exists' when unset env var in one interp and check
85 for existence from another interp". */
86 setenv ("LD_PRELOAD", "", 1);
87 read2
= dlsym (RTLD_NEXT
, "read");
89 init_readmore (&readmore_method
, &readmore_sleep
, &log
);
92 /* Only modify 'read' behaviour when reading from the terminal. */
98 /* READ1. Force read to return only one byte at a time. */
99 return read2 (fd
, buf
, 1);
102 if (readmore_method
== 1)
104 /* READMORE, method 1. Wait a little before doing a read. */
105 usleep (readmore_sleep
* 1000);
106 return read2 (fd
, buf
, count
);
109 if (readmore_method
== 2)
111 /* READMORE, method 2. After doing a read, either return or wait
112 a little and do another read, and so on. */
115 int max_iterations
= -1;
118 for (iteration
= 1; ; iteration
++)
120 res
= read2 (fd
, (char *)buf
+ total
, count
- total
);
123 "READ (%d): fd: %d, COUNT: %zd, RES: %zd, ERRNO: %s\n",
124 iteration
, fd
, count
- total
, res
,
125 res
== -1 ? strerror (errno
) : "none");
130 /* Error on first read, report. */
136 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EIO
))
138 /* Ignore error, but don't try anymore reading. */
143 /* Other error, report back. */
150 /* Buf full, no need to do any more reading. */
153 /* Handle end-of-file. */
157 if (iteration
== max_iterations
)
160 usleep (readmore_sleep
* 1000);
164 fprintf (log
, "READ returning: RES: %zd, ERRNO: %s\n",
165 total
, total
== -1 ? strerror (errno
) : "none");
170 /* Fallback, regular read. */
171 return read2 (fd
, buf
, count
);