25#include "gdbsupport/gdb_select.h"
29#include "gdbsupport/gdb_optional.h"
32#if defined (HAVE_POLL_H)
34#elif defined (HAVE_SYS_POLL_H)
41#define USING_GUILE_BEFORE_2_2 \
42 (SCM_MAJOR_VERSION == 2 && SCM_MINOR_VERSION == 0)
53 void flush ()
override;
54 void write (
const char *buf,
long length_buf)
override;
75#if USING_GUILE_BEFORE_2_2
81 unsigned read_buf_size, write_buf_size;
92#if USING_GUILE_BEFORE_2_2
117#if USING_GUILE_BEFORE_2_2
126#if USING_GUILE_BEFORE_2_2
134static const unsigned default_read_buf_size = 16;
135static const unsigned default_write_buf_size = 16;
138static const unsigned min_memory_port_buf_size = 1;
139static const unsigned max_memory_port_buf_size = 4096;
142static gdb::unique_xmalloc_ptr<char> out_of_range_buf_size;
158#if USING_GUILE_BEFORE_2_2
161ioscm_open_port (scm_t_bits port_type,
long mode_bits, scm_t_bits stream)
166 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
169 port = scm_new_port_table_entry (port_type);
171 SCM_SET_CELL_TYPE (port, port_type | mode_bits);
172 SCM_SETSTREAM (port, stream);
175 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
186 return scm_c_make_port (port_type, mode_bits, stream);
202 for (i = 0; i <
size; ++i)
211#if USING_GUILE_BEFORE_2_2
217ioscm_input_waiting (SCM port)
227 struct pollfd pollfd = { fdes, POLLIN, 0 };
228 static int use_poll = -1;
234 struct pollfd test_pollfd = { fdes, POLLIN, 0 };
236 if (poll (&test_pollfd, 1, 0) == 1 && (test_pollfd.revents & POLLNVAL))
246 if (poll (&pollfd, 1, 0) < 0)
249 return pollfd.revents & POLLIN ? 1 : 0;
256 struct timeval timeout;
258 int num_fds = fdes + 1;
261 memset (&timeout, 0,
sizeof (timeout));
262 FD_ZERO (&input_fds);
263 FD_SET (fdes, &input_fds);
266 &input_fds, NULL, NULL,
274 return num_found > 0 && FD_ISSET (fdes, &input_fds);
281ioscm_fill_input (SCM port)
285 scm_t_port *pt = SCM_PTAB_ENTRY (port);
289 return (scm_t_wchar) EOF;
294 count =
gdb_stdin->read ((
char *) pt->read_buf, pt->read_buf_size);
298 return (scm_t_wchar) EOF;
300 pt->read_pos = pt->read_buf;
301 pt->read_end = pt->read_buf + count;
302 return *pt->read_buf;
323 catch (
const gdb_exception &except)
333ioscm_flush (SCM port)
363 read_buf = (
char *) SCM_BYTEVECTOR_CONTENTS (dst) + start;
368 return (
size_t)
read;
376 const char *data = (
char *) SCM_BYTEVECTOR_CONTENTS (src) + start;
390 catch (
const gdb_exception &except)
417#if USING_GUILE_BEFORE_2_2
425#define GDB_STDIO_BUFFER_DEFAULT_SIZE 1024
427#if USING_GUILE_BEFORE_2_2
436 scm_t_port *pt = SCM_PTAB_ENTRY (port);
438 int writing = (mode_bits & SCM_WRTNG) != 0;
442 if (!writing &&
size > 0)
445 = (
unsigned char *) scm_gc_malloc_pointerless (
size,
"port buffer");
446 pt->read_pos = pt->read_end = pt->read_buf;
447 pt->read_buf_size =
size;
451 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
452 pt->read_buf_size = 1;
455 if (writing &&
size > 0)
458 = (
unsigned char *) scm_gc_malloc_pointerless (
size,
"port buffer");
459 pt->write_pos = pt->write_buf;
460 pt->write_buf_size =
size;
464 pt->write_buf = pt->write_pos = &pt->shortbuf;
465 pt->write_buf_size = 1;
467 pt->write_end = pt->write_buf + pt->write_buf_size;
475 if (mode_bits & SCM_BUF0)
476 scm_setvbuf (port, scm_from_utf8_symbol (
"none"), scm_from_size_t (0));
478 scm_setvbuf (port, scm_from_utf8_symbol (
"block"),
489 int is_a_tty = isatty (fd);
491 const char *mode_str;
499 mode_str = is_a_tty ?
"r0" :
"r";
503 mode_str = is_a_tty ?
"w0" :
"w";
507 mode_str = is_a_tty ?
"w0" :
"w";
510 gdb_assert_not_reached (
"bad stdio file descriptor");
513 mode_bits = scm_mode_bits ((
char *) mode_str);
528#if USING_GUILE_BEFORE_2_2
530 return scm_from_bool (!SCM_IMP (scm)
533 return scm_from_bool (SCM_PORTP (scm)
578 scm_c_write (
m_port, buffer, length_buffer);
586 const char *func_name)
591 SCM_ARG1, func_name, _(
"output port"));
593 SCM_ARG2, func_name, _(
"thunk"));
597 scoped_restore restore_async = make_scoped_restore (&
current_ui->
async, 0);
605 gdb::optional<ui_out_redirect_pop> redirect_popper;
663 CORE_ADDR new_current;
671 if ((offset < 0 && iomem->current + offset > iomem->
current)
672 || (offset > 0 && iomem->
current + offset < iomem->current))
674 new_current = iomem->
current + offset;
677 new_current = offset;
682 new_current = iomem->
size;
691 if (new_current > iomem->
size)
697#if USING_GUILE_BEFORE_2_2
702gdbscm_memory_port_fill_input (SCM port)
704 scm_t_port *pt = SCM_PTAB_ENTRY (port);
714 to_read = pt->read_buf_size;
723 pt->read_pos = pt->read_buf;
724 pt->read_end = pt->read_buf + to_read;
725 return *pt->read_buf;
732gdbscm_memory_port_end_input (SCM port,
int offset)
734 scm_t_port *pt = SCM_PTAB_ENTRY (port);
736 size_t remaining = pt->read_end - pt->read_pos;
739 if ((offset < 0 && remaining + offset > remaining)
740 || (offset > 0 && remaining + offset < remaining))
743 _(
"overflow in offset calculation"));
749 pt->read_pos = pt->read_end;
759 pt->rw_active = SCM_PORT_NEITHER;
765gdbscm_memory_port_flush (SCM port)
767 scm_t_port *pt = SCM_PTAB_ENTRY (port);
769 size_t to_write = pt->write_pos - pt->write_buf;
780 _(
"writing beyond end of memory range"));
788 pt->write_pos = pt->write_buf;
789 pt->rw_active = SCM_PORT_NEITHER;
797 scm_t_port *pt = SCM_PTAB_ENTRY (port);
802 if (pt->rw_active == SCM_PORT_WRITE)
804 if (offset != 0 || whence !=
SEEK_CUR)
806 gdbscm_memory_port_flush (port);
815 CORE_ADDR current = iomem->
current;
816 size_t delta = pt->write_pos - pt->write_buf;
818 if (current + delta < current
819 || current + delta > iomem->
size)
823 result = current + delta;
828 else if (pt->rw_active == SCM_PORT_READ)
830 if (offset != 0 || whence !=
SEEK_CUR)
832 scm_end_input (port);
840 CORE_ADDR current = iomem->
current;
841 size_t remaining = pt->read_end - pt->read_pos;
843 if (current - remaining > current
844 || current - remaining < iomem->start)
848 result = current - remaining;
852 if (rc != 0 && pt->read_buf == pt->putback_buf)
854 size_t saved_remaining = pt->saved_read_end - pt->saved_read_pos;
856 if (result - saved_remaining > result
857 || result - saved_remaining < iomem->start)
860 result -= saved_remaining;
889 scm_t_port *pt = SCM_PTAB_ENTRY (port);
891 const gdb_byte *
data = (
const gdb_byte *) void_data;
898 _(
"writing beyond end of memory range"));
901 if (pt->write_buf == &pt->shortbuf)
916 size_t space = pt->write_end - pt->write_pos;
921 memcpy (pt->write_pos, data,
size);
922 pt->write_pos +=
size;
926 memcpy (pt->write_pos, data, space);
927 pt->write_pos = pt->write_end;
928 gdbscm_memory_port_flush (port);
930 const gdb_byte *ptr =
data + space;
931 size_t remaining =
size - space;
933 if (remaining >= pt->write_buf_size)
943 memcpy (pt->write_pos, ptr, remaining);
944 pt->write_pos += remaining;
956 scm_t_port *pt = SCM_PTAB_ENTRY (port);
959 gdbscm_memory_port_flush (port);
961 if (pt->read_buf == pt->putback_buf)
962 pt->read_buf = pt->saved_read_buf;
963 if (pt->read_buf != &pt->shortbuf)
964 xfree (pt->read_buf);
965 if (pt->write_buf != &pt->shortbuf)
966 xfree (pt->write_buf);
967 scm_gc_free (iomem,
sizeof (*iomem),
"memory port");
975gdbscm_memory_port_free (SCM port)
989ioscm_reinit_memory_port (SCM port,
size_t read_buf_size,
990 size_t write_buf_size,
const char *func_name)
992 scm_t_port *pt = SCM_PTAB_ENTRY (port);
995 gdb_assert (read_buf_size >= min_memory_port_buf_size
996 && read_buf_size <= max_memory_port_buf_size);
997 gdb_assert (write_buf_size >= min_memory_port_buf_size
998 && write_buf_size <= max_memory_port_buf_size);
1002 if (pt->read_buf == &pt->shortbuf)
1004 gdb_assert (pt->write_buf == &pt->shortbuf);
1005 scm_misc_error (func_name, _(
"port is unbuffered: ~a"),
1011 if (read_buf_size != pt->read_buf_size
1012 && pt->read_end != pt->read_buf)
1014 scm_misc_error (func_name, _(
"read buffer not empty: ~a"),
1018 if (write_buf_size != pt->write_buf_size
1019 && pt->write_pos != pt->write_buf)
1021 scm_misc_error (func_name, _(
"write buffer not empty: ~a"),
1027 if (read_buf_size != pt->read_buf_size)
1029 iomem->read_buf_size = read_buf_size;
1030 pt->read_buf_size = read_buf_size;
1031 xfree (pt->read_buf);
1032 pt->read_buf = (
unsigned char *)
xmalloc (pt->read_buf_size);
1033 pt->read_pos = pt->read_end = pt->read_buf;
1036 if (write_buf_size != pt->write_buf_size)
1038 iomem->write_buf_size = write_buf_size;
1039 pt->write_buf_size = write_buf_size;
1040 xfree (pt->write_buf);
1041 pt->write_buf = (
unsigned char *)
xmalloc (pt->write_buf_size);
1042 pt->write_pos = pt->write_buf;
1043 pt->write_end = pt->write_buf + pt->write_buf_size;
1059 if (iomem != NULL && iomem->
size <
size)
1061 *read_size = *write_size =
size;
1082 read_buf = (gdb_byte *) SCM_BYTEVECTOR_CONTENTS (dst) + start;
1095 const gdb_byte *data =
1096 (
const gdb_byte *) SCM_BYTEVECTOR_CONTENTS (src) + start;
1102 _(
"writing beyond end of memory range"));
1137 scm_gc_free (iomem,
sizeof (*iomem),
"memory port");
1138 SCM_SETSTREAM (port, NULL);
1150 scm_puts (
"#<", port);
1151 scm_print_port_mode (exp, port);
1154 hex_string (iomem->
start), hex_string (iomem->
end));
1155 scm_putc (
'>', port);
1166 gdbscm_memory_port_fill_input,
1172#if USING_GUILE_BEFORE_2_2
1194 if (*mode !=
'r' && *mode !=
'w')
1198 _(
"bad mode string"));
1200 for (p = mode + 1; *p !=
'\0'; ++p)
1211 _(
"bad mode string"));
1217 mode_bits = scm_mode_bits ((
char *) mode);
1230 gdb_assert (start <= end);
1235 iomem->
start = start;
1237 iomem->
size = end - start;
1243#if USING_GUILE_BEFORE_2_2
1251ioscm_init_memory_port_buffers (SCM port)
1255 int buffered = (SCM_CELL_WORD_0 (port) & SCM_BUF0) == 0;
1258 iomem->read_buf_size = default_read_buf_size;
1259 iomem->write_buf_size = default_write_buf_size;
1263 iomem->read_buf_size = 1;
1264 iomem->write_buf_size = 1;
1267 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1269 pt->encoding = NULL;
1271 pt->read_buf_size = iomem->read_buf_size;
1272 pt->write_buf_size = iomem->write_buf_size;
1275 pt->read_buf = (
unsigned char *)
xmalloc (pt->read_buf_size);
1276 pt->write_buf = (
unsigned char *)
xmalloc (pt->write_buf_size);
1280 pt->read_buf = &pt->shortbuf;
1281 pt->write_buf = &pt->shortbuf;
1283 pt->read_pos = pt->read_end = pt->read_buf;
1284 pt->write_pos = pt->write_buf;
1285 pt->write_end = pt->write_buf + pt->write_buf_size;
1317 const SCM keywords[] = {
1321 CORE_ADDR start = 0;
1323 int mode_arg_pos = -1, start_arg_pos = -1, size_arg_pos = -1;
1329 &mode_arg_pos, &mode,
1330 &start_arg_pos, &start,
1331 &size_arg_pos, &
size);
1333 scm_dynwind_begin ((scm_t_dynwind_flags) 0);
1336 mode = xstrdup (
"r");
1337 scm_dynwind_free (mode);
1339 if (size_arg_pos > 0)
1343 if (start +
size < start)
1348 _(
"start+size overflows"));
1353 end = ~(CORE_ADDR) 0;
1361 mode_bits |= SCM_BUF0;
1365 (scm_t_bits) stream);
1367#if USING_GUILE_BEFORE_2_2
1368 ioscm_init_memory_port_buffers (port);
1382#if USING_GUILE_BEFORE_2_2
1417#if USING_GUILE_BEFORE_2_2
1424 return scm_from_uint (iomem->read_buf_size);
1426 return scm_from_uint (0);
1437#if USING_GUILE_BEFORE_2_2
1445 if (!scm_is_unsigned_integer (
size, min_memory_port_buf_size,
1446 max_memory_port_buf_size))
1449 out_of_range_buf_size.get ());
1453 ioscm_reinit_memory_port (port, scm_to_uint (
size), iomem->write_buf_size,
1456 return SCM_UNSPECIFIED;
1458 return scm_setvbuf (port, scm_from_utf8_symbol (
"block"),
size);
1467#if USING_GUILE_BEFORE_2_2
1474 return scm_from_uint (iomem->write_buf_size);
1476 return scm_from_uint (0);
1487#if USING_GUILE_BEFORE_2_2
1495 if (!scm_is_unsigned_integer (
size, min_memory_port_buf_size,
1496 max_memory_port_buf_size))
1499 out_of_range_buf_size.get ());
1503 ioscm_reinit_memory_port (port, iomem->read_buf_size, scm_to_uint (
size),
1506 return SCM_UNSPECIFIED;
1508 return scm_setvbuf (port, scm_from_utf8_symbol (
"block"),
size);
1518Return gdb's input port." },
1522Return gdb's output port." },
1526Return gdb's error port." },
1530Return #t if the object is a gdb:stdio-port." },
1534Return a port that can be used for reading/writing inferior memory.\n\
1536 Arguments: [#:mode string] [#:start address] [#:size integer]\n\
1537 Returns: A port object." },
1541Return #t if the object is a memory port." },
1545Return the memory range of the port as (start end)." },
1547 {
"memory-port-read-buffer-size", 1, 0, 0,
1550Return the size of the read buffer for the memory port." },
1552 {
"set-memory-port-read-buffer-size!", 2, 0, 0,
1555Set the size of the read buffer for the memory port.\n\
1557 Arguments: port integer\n\
1558 Returns: unspecified." },
1560 {
"memory-port-write-buffer-size", 1, 0, 0,
1563Return the size of the write buffer for the memory port." },
1565 {
"set-memory-port-write-buffer-size!", 2, 0, 0,
1568Set the size of the write buffer for the memory port.\n\
1570 Arguments: port integer\n\
1571 Returns: unspecified." },
1579 {
"%with-gdb-input-from-port", 2, 0, 0,
1582Temporarily set GDB's input port to PORT and then invoke THUNK.\n\
1584 Arguments: port thunk\n\
1585 Returns: The result of calling THUNK.\n\
1587This procedure is experimental." },
1590 {
"%with-gdb-output-to-port", 2, 0, 0,
1593Temporarily set GDB's output port to PORT and then invoke THUNK.\n\
1595 Arguments: port thunk\n\
1596 Returns: The result of calling THUNK.\n\
1598This procedure is experimental." },
1600 {
"%with-gdb-error-to-port", 2, 0, 0,
1603Temporarily set GDB's error port to PORT and then invoke THUNK.\n\
1605 Arguments: port thunk\n\
1606 Returns: The result of calling THUNK.\n\
1608This procedure is experimental." },
1644#if USING_GUILE_BEFORE_2_2
1647 out_of_range_buf_size = xstrprintf (
"size not between %u - %u",
1648 min_memory_port_buf_size,
1649 max_memory_port_buf_size);
static struct parser_state * pstate
void write(const char *buf, long length_buf) override
ioscm_file_port(SCM port)
int interruptible_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
ssize_t read(int fd, void *buf, size_t count)
#define gdbscm_is_true(scm)
void gdbscm_parse_function_args(const char *function_name, int beginning_arg_pos, const SCM *keywords, const char *format,...)
void gdbscm_memory_error(const char *subr, const char *msg, SCM args) ATTRIBUTE_NORETURN
gdbscm_gdb_exception unpack(const gdb_exception &exc)
SCM gdbscm_scm_from_longest(LONGEST l)
void gdbscm_out_of_range_error(const char *subr, int arg_pos, SCM bad_value, const char *error) ATTRIBUTE_NORETURN
SCM gdbscm_scm_from_ulongest(ULONGEST l)
void gdbscm_printf(SCM port, const char *format,...) ATTRIBUTE_PRINTF(2
SCM gdbscm_safe_call_0(SCM proc, excp_matcher_func *ok_excps)
int gdbscm_is_exception(SCM scm)
void gdbscm_define_functions(const scheme_function *, int is_public)
#define GDBSCM_HANDLE_GDB_EXCEPTION(exception)
void gdbscm_throw(SCM exception) ATTRIBUTE_NORETURN
static scm_t_subr as_a_scm_t_subr(SCM(*func)(void))
SCM gdbscm_scm_from_c_string(const char *string)
static char memory_port_desc_name[]
static void ioscm_init_gdb_stdio_port(void)
static const char output_port_name[]
static SCM gdbscm_percent_with_gdb_error_to_port(SCM port, SCM thunk)
static SCM ioscm_with_output_to_port_worker(SCM port, SCM thunk, enum oport oport, const char *func_name)
static scm_t_port_type * memory_port_desc
static SCM error_port_scm
static int gdbscm_memory_port_print(SCM exp, SCM port, scm_print_state *pstate)
static SCM gdbscm_output_port(void)
static SCM gdbscm_set_memory_port_write_buffer_size_x(SCM port, SCM size)
static SCM gdbscm_memory_port_write_buffer_size(SCM port)
static SCM gdbscm_memory_port_range(SCM port)
static const char error_port_name[]
static long ioscm_parse_mode_bits(const char *func_name, const char *mode)
static SCM ioscm_make_gdb_stdio_port(int fd)
static SCM gdbscm_stdio_port_p(SCM scm)
static SCM gdbscm_error_port(void)
static SCM orig_output_port_scm
static SCM input_port_scm
static scm_t_off gdbscm_memory_port_seek(SCM port, scm_t_off offset, int whence)
static SCM gdbscm_percent_with_gdb_output_to_port(SCM port, SCM thunk)
static SCM orig_error_port_scm
static size_t ioscm_read_from_port(SCM port, SCM dst, size_t start, size_t count)
static const scheme_function private_port_functions[]
static size_t gdbscm_memory_port_write(SCM port, SCM src, size_t start, size_t count)
static int ioscm_lseek_address(ioscm_memory_port *iomem, LONGEST offset, int whence)
static void ioscm_init_memory_port_type(void)
static size_t gdbscm_memory_port_read(SCM port, SCM dst, size_t start, size_t count)
static SCM gdbscm_input_port(void)
static SCM gdbscm_memory_port_p(SCM obj)
static SCM orig_input_port_scm
static void gdbscm_get_natural_buffer_sizes(SCM port, size_t *read_size, size_t *write_size)
static const scheme_function port_functions[]
static SCM gdbscm_memory_port_read_buffer_size(SCM port)
static SCM gdbscm_set_memory_port_read_buffer_size_x(SCM port, SCM size)
static void ioscm_init_stdio_buffers(SCM port, long mode_bits)
static scm_t_port_type * stdio_port_desc
static size_t ioscm_write(SCM port, SCM src, size_t start, size_t count)
static SCM ioscm_open_port(scm_t_port_type *port_type, long mode_bits, scm_t_bits stream)
static char stdio_port_desc_name[]
static void gdbscm_memory_port_close(SCM port)
static SCM gdbscm_open_memory(SCM rest)
#define USING_GUILE_BEFORE_2_2
static const char input_port_name[]
void gdbscm_initialize_ports(void)
static const unsigned natural_buf_size
static ioscm_memory_port * ioscm_init_memory_port_stream(CORE_ADDR start, CORE_ADDR end)
static int gdbscm_is_memory_port(SCM obj)
static SCM output_port_scm
#define GDB_STDIO_BUFFER_DEFAULT_SIZE
static void fputsn_filtered(const char *s, size_t size, struct ui_file *stream)
int target_write_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
std::unique_ptr< ui_file > ui_file_up
void gdb_flush(struct ui_file *stream)
void gdb_puts(const char *linebuffer, struct ui_file *stream)