31 #if defined (HAVE_POLL_H)
33 #elif defined (HAVE_SYS_POLL_H)
136 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
139 port = scm_new_port_table_entry (port_type);
141 SCM_SET_CELL_TYPE (port, port_type | mode_bits);
144 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
166 struct pollfd pollfd = { fdes, POLLIN, 0 };
173 struct pollfd test_pollfd = { fdes, POLLIN, 0 };
175 if (poll (&test_pollfd, 1, 0) == 1 && (test_pollfd.revents & POLLNVAL))
185 if (poll (&pollfd, 1, 0) < 0)
188 return pollfd.revents & POLLIN ? 1 : 0;
195 struct timeval timeout;
200 memset (&timeout, 0,
sizeof (timeout));
201 FD_ZERO (&input_fds);
202 FD_SET (fdes, &input_fds);
204 num_found =
gdb_select (num_fds, &input_fds, NULL, NULL, &timeout);
211 return num_found > 0 && FD_ISSET (fdes, &input_fds);
222 scm_t_port *pt = SCM_PTAB_ENTRY (port);
226 return (scm_t_wchar) EOF;
235 return (scm_t_wchar) EOF;
237 pt->read_pos = pt->read_buf;
238 pt->read_end = pt->read_buf + count;
239 return *pt->read_buf;
250 for (i = 0; i <
size; ++i)
320 scm_t_port *pt = SCM_PTAB_ENTRY (port);
321 #define GDB_STDIO_BUFFER_DEFAULT_SIZE 1024
323 int writing = (mode_bits & SCM_WRTNG) != 0;
327 if (!writing && size > 0)
329 pt->read_buf = scm_gc_malloc_pointerless (size,
"port buffer");
330 pt->read_pos = pt->read_end = pt->read_buf;
331 pt->read_buf_size =
size;
335 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
336 pt->read_buf_size = 1;
339 if (writing && size > 0)
341 pt->write_buf = scm_gc_malloc_pointerless (size,
"port buffer");
342 pt->write_pos = pt->write_buf;
343 pt->write_buf_size =
size;
347 pt->write_buf = pt->write_pos = &pt->shortbuf;
348 pt->write_buf_size = 1;
350 pt->write_end = pt->write_buf + pt->write_buf_size;
358 int is_a_tty = isatty (fd);
367 mode_bits = scm_mode_bits (is_a_tty ?
"r0" :
"r");
371 mode_bits = scm_mode_bits (is_a_tty ?
"w0" :
"w");
375 mode_bits = scm_mode_bits (is_a_tty ?
"w0" :
"w");
396 return scm_from_bool (!SCM_IMP (scm)
435 _(
"ioscm_file_port_delete: bad magic number"));
446 _(
"ioscm_file_port_rewind: bad magic number"));
448 scm_truncate_file (stream->
port, 0);
460 _(
"ioscm_file_port_put: bad magic number"));
474 _(
"ioscm_pot_file_write: bad magic number"));
476 scm_c_write (stream->
port, buffer, length_buffer);
501 const char *func_name)
508 SCM_ARG1, func_name,
_(
"output port"));
510 SCM_ARG2, func_name,
_(
"thunk"));
531 warning (
_(
"Current output protocol does not support redirection"));
595 if ((offset < 0 && iomem->current + offset > iomem->
current)
596 || (offset > 0 && iomem->
current + offset < iomem->current))
606 new_current = iomem->
size;
615 if (new_current > iomem->
size)
626 scm_t_port *pt = SCM_PTAB_ENTRY (port);
636 to_read = pt->read_buf_size;
645 pt->read_pos = pt->read_buf;
646 pt->read_end = pt->read_buf + to_read;
647 return *pt->read_buf;
656 scm_t_port *pt = SCM_PTAB_ENTRY (port);
658 size_t remaining = pt->read_end - pt->read_pos;
661 if ((offset < 0 && remaining + offset > remaining)
662 || (offset > 0 && remaining + offset < remaining))
665 _(
"overflow in offset calculation"));
671 pt->read_pos = pt->read_end;
681 pt->rw_active = SCM_PORT_NEITHER;
689 scm_t_port *pt = SCM_PTAB_ENTRY (port);
691 size_t to_write = pt->write_pos - pt->write_buf;
702 _(
"writing beyond end of memory range"));
710 pt->write_pos = pt->write_buf;
711 pt->rw_active = SCM_PORT_NEITHER;
719 scm_t_port *pt = SCM_PTAB_ENTRY (port);
727 _(
"writing beyond end of memory range"));
730 if (pt->write_buf == &pt->shortbuf)
745 size_t space = pt->write_end - pt->write_pos;
750 memcpy (pt->write_pos, data, size);
751 pt->write_pos +=
size;
755 memcpy (pt->write_pos, data, space);
756 pt->write_pos = pt->write_end;
759 const void *ptr = ((
const char *) data) + space;
760 size_t remaining = size - space;
762 if (remaining >= pt->write_buf_size)
772 memcpy (pt->write_pos, ptr, remaining);
773 pt->write_pos += remaining;
785 scm_t_port *pt = SCM_PTAB_ENTRY (port);
790 if (pt->rw_active == SCM_PORT_WRITE)
792 if (offset != 0 || whence !=
SEEK_CUR)
804 size_t delta = pt->write_pos - pt->write_buf;
806 if (current + delta < current
807 || current + delta > iomem->
size)
811 result = current + delta;
816 else if (pt->rw_active == SCM_PORT_READ)
818 if (offset != 0 || whence !=
SEEK_CUR)
820 scm_end_input (port);
829 size_t remaining = pt->read_end - pt->read_pos;
831 if (current - remaining > current
832 || current - remaining < iomem->start)
836 result = current - remaining;
840 if (rc != 0 && pt->read_buf == pt->putback_buf)
842 size_t saved_remaining = pt->saved_read_end - pt->saved_read_pos;
844 if (result - saved_remaining > result
845 || result - saved_remaining < iomem->start)
848 result -= saved_remaining;
877 scm_t_port *pt = SCM_PTAB_ENTRY (port);
882 if (pt->read_buf == pt->putback_buf)
883 pt->read_buf = pt->saved_read_buf;
884 if (pt->read_buf != &pt->shortbuf)
885 xfree (pt->read_buf);
886 if (pt->write_buf != &pt->shortbuf)
887 xfree (pt->write_buf);
888 scm_gc_free (iomem,
sizeof (*iomem),
"memory port");
909 char *
type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
911 scm_puts (
"#<", port);
912 scm_print_port_mode (exp, port);
916 scm_putc (
'>', port);
946 if (*mode !=
'r' && *mode !=
'w')
950 _(
"bad mode string"));
952 for (p = mode + 1; *p !=
'\0'; ++p)
963 _(
"bad mode string"));
969 mode_bits = scm_mode_bits ((
char *) mode);
984 int buffered = (SCM_CELL_WORD_0 (port) & SCM_BUF0) == 0;
991 iomem->
start = start;
993 iomem->
size = end - start;
1006 pt = SCM_PTAB_ENTRY (port);
1008 pt->encoding = NULL;
1014 pt->read_buf =
xmalloc (pt->read_buf_size);
1015 pt->write_buf =
xmalloc (pt->write_buf_size);
1019 pt->read_buf = &pt->shortbuf;
1020 pt->write_buf = &pt->shortbuf;
1022 pt->read_pos = pt->read_end = pt->read_buf;
1023 pt->write_pos = pt->write_buf;
1024 pt->write_end = pt->write_buf + pt->write_buf_size;
1026 SCM_SETSTREAM (port, iomem);
1037 size_t write_buf_size,
const char *func_name)
1039 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1049 if (pt->read_buf == &pt->shortbuf)
1052 scm_misc_error (func_name,
_(
"port is unbuffered: ~a"),
1058 if (read_buf_size != pt->read_buf_size
1059 && pt->read_end != pt->read_buf)
1061 scm_misc_error (func_name,
_(
"read buffer not empty: ~a"),
1065 if (write_buf_size != pt->write_buf_size
1066 && pt->write_pos != pt->write_buf)
1068 scm_misc_error (func_name,
_(
"write buffer not empty: ~a"),
1074 if (read_buf_size != pt->read_buf_size)
1077 pt->read_buf_size = read_buf_size;
1078 xfree (pt->read_buf);
1079 pt->read_buf =
xmalloc (pt->read_buf_size);
1080 pt->read_pos = pt->read_end = pt->read_buf;
1083 if (write_buf_size != pt->write_buf_size)
1086 pt->write_buf_size = write_buf_size;
1087 xfree (pt->write_buf);
1088 pt->write_buf =
xmalloc (pt->write_buf_size);
1089 pt->write_pos = pt->write_buf;
1090 pt->write_end = pt->write_buf + pt->write_buf_size;
1121 const SCM keywords[] = {
1127 int mode_arg_pos = -1, start_arg_pos = -1, size_arg_pos = -1;
1133 &mode_arg_pos, &mode,
1134 &start_arg_pos, &start,
1135 &size_arg_pos, &size);
1137 scm_dynwind_begin (0);
1140 mode = xstrdup (
"r");
1141 scm_dynwind_free (mode);
1143 if (size_arg_pos > 0)
1147 if (start + size < start)
1152 _(
"start+size overflows"));
1227 SCM_ASSERT_TYPE (scm_is_integer (size), size, SCM_ARG2,
FUNC_NAME,
1241 return SCM_UNSPECIFIED;
1269 SCM_ASSERT_TYPE (scm_is_integer (size), size, SCM_ARG2,
FUNC_NAME,
1283 return SCM_UNSPECIFIED;
1292 Return gdb's input port." },
1296 Return gdb's output port." },
1300 Return gdb's error port." },
1304 Return #t if the object is a gdb:stdio-port." },
1308 Return a port that can be used for reading/writing inferior memory.\n\
1310 Arguments: [#:mode string] [#:start address] [#:size integer]\n\
1311 Returns: A port object." },
1315 Return #t if the object is a memory port." },
1319 Return the memory range of the port as (start end)." },
1321 {
"memory-port-read-buffer-size", 1, 0, 0,
1324 Return the size of the read buffer for the memory port." },
1326 {
"set-memory-port-read-buffer-size!", 2, 0, 0,
1329 Set the size of the read buffer for the memory port.\n\
1331 Arguments: port integer\n\
1332 Returns: unspecified." },
1334 {
"memory-port-write-buffer-size", 1, 0, 0,
1337 Return the size of the write buffer for the memory port." },
1339 {
"set-memory-port-write-buffer-size!", 2, 0, 0,
1342 Set the size of the write buffer for the memory port.\n\
1344 Arguments: port integer\n\
1345 Returns: unspecified." },
1353 {
"%with-gdb-input-from-port", 2, 0, 0,
1354 gdbscm_percent_with_gdb_input_from_port,
1356 Temporarily set GDB's input port to PORT and then invoke THUNK.\n\
1358 Arguments: port thunk\n\
1359 Returns: The result of calling THUNK.\n\
1361 This procedure is experimental." },
1364 {
"%with-gdb-output-to-port", 2, 0, 0,
1367 Temporarily set GDB's output port to PORT and then invoke THUNK.\n\
1369 Arguments: port thunk\n\
1370 Returns: The result of calling THUNK.\n\
1372 This procedure is experimental." },
1374 {
"%with-gdb-error-to-port", 2, 0, 0,
1377 Temporarily set GDB's error port to PORT and then invoke THUNK.\n\
1379 Arguments: port thunk\n\
1380 Returns: The result of calling THUNK.\n\
1382 This procedure is experimental." },
static void gdbscm_memory_port_flush(SCM port)
char * hex_string(LONGEST num)
static SCM orig_output_port_scm
void gdbscm_memory_error(const char *subr, const char *msg, SCM args) ATTRIBUTE_NORETURN
struct ui_file * gdb_stdin
void gdbscm_define_functions(const scheme_function *, int is_public)
int target_write_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
SCM gdbscm_scm_from_ulongest(ULONGEST l)
static void ioscm_file_port_delete(struct ui_file *file)
void warning(const char *fmt,...)
int fputc_filtered(int c, struct ui_file *stream)
static SCM gdbscm_memory_port_write_buffer_size(SCM port)
static const char input_port_name[]
static void gdbscm_memory_port_end_input(SCM port, int offset)
static void ioscm_file_port_put(struct ui_file *file, ui_file_put_method_ftype *write, void *dest)
static SCM gdbscm_error_port(void)
static const unsigned min_memory_port_buf_size
static SCM gdbscm_stdio_port_p(SCM scm)
struct ui_file * gdb_stdout
struct cleanup * make_cleanup_ui_out_redirect_pop(struct ui_out *uiout)
void internal_error(const char *file, int line, const char *fmt,...)
static SCM ioscm_make_gdb_stdio_port(int fd)
SCM gdbscm_scm_from_longest(LONGEST l)
static SCM gdbscm_open_memory(SCM rest)
static int gdbscm_is_memory_port(SCM obj)
static int ioscm_fill_input(SCM port)
static SCM gdbscm_input_port(void)
int gdbscm_is_exception(SCM scm)
static SCM gdbscm_set_memory_port_read_buffer_size_x(SCM port, SCM size)
#define gdbscm_is_true(scm)
struct cleanup * make_cleanup_restore_ui_file(struct ui_file **variable)
void set_ui_file_rewind(struct ui_file *file, ui_file_rewind_ftype *rewind_ptr)
static SCM gdbscm_percent_with_gdb_error_to_port(SCM port, SCM thunk)
static long ioscm_parse_mode_bits(const char *func_name, const char *mode)
static SCM error_port_scm
static const unsigned default_read_buf_size
#define CATCH(EXCEPTION, MASK)
static SCM gdbscm_memory_port_read_buffer_size(SCM port)
struct cleanup * make_cleanup_restore_integer(int *variable)
static SCM ioscm_with_output_to_port_worker(SCM port, SCM thunk, enum oport oport, const char *func_name)
static struct parser_state * pstate
static SCM input_port_scm
struct cleanup * set_batch_flag_and_make_cleanup_restore_page_info(void)
SCM gdbscm_scm_from_c_string(const char *string)
static void fputsn_filtered(const char *s, size_t size, struct ui_file *stream)
struct ui_file * ui_file_new(void)
static SCM gdbscm_percent_with_gdb_output_to_port(SCM port, SCM thunk)
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
#define gdb_assert_not_reached(message)
static unsigned char use_poll
static scm_t_bits stdio_port_desc
static void ioscm_init_stdio_buffers(SCM port, long mode_bits)
static int ioscm_input_waiting(SCM port)
static void ioscm_file_port_write(struct ui_file *file, const char *buffer, long length_buffer)
static SCM ioscm_open_port(scm_t_bits port_type, long mode_bits)
void gdbscm_throw(SCM exception) ATTRIBUTE_NORETURN
static SCM gdbscm_set_memory_port_write_buffer_size_x(SCM port, SCM size)
int gdb_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
static int gdbscm_memory_port_print(SCM exp, SCM port, scm_print_state *pstate)
static size_t gdbscm_memory_port_free(SCM port)
char * xstrprintf(const char *format,...)
static struct ui_file * ioscm_file_port_new(SCM port)
static char stdio_port_desc_name[]
static const unsigned max_memory_port_buf_size
static SCM gdbscm_memory_port_range(SCM port)
void gdbscm_printf(SCM port, const char *format,...) ATTRIBUTE_PRINTF(2
static void ioscm_write(SCM port, const void *data, size_t size)
static SCM gdbscm_output_port(void)
#define GDB_STDIO_BUFFER_DEFAULT_SIZE
static SCM orig_error_port_scm
static SCM orig_input_port_scm
static void ioscm_init_gdb_stdio_port(void)
struct cleanup * make_cleanup_ui_file_delete(struct ui_file *arg)
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
static void ioscm_init_memory_port_type(void)
struct ui_file * gdb_stderr
static char memory_port_desc_name[]
static void gdbscm_memory_port_write(SCM port, const void *data, size_t size)
static int file_port_magic
static int gdbscm_memory_port_close(SCM port)
void set_ui_file_data(struct ui_file *file, void *data, ui_file_delete_ftype *delete_ptr)
void gdbscm_out_of_range_error(const char *subr, int arg_pos, SCM bad_value, const char *error) ATTRIBUTE_NORETURN
SCM gdbscm_safe_call_0(SCM proc, excp_matcher_func *ok_excps)
static scm_t_bits memory_port_desc
long ui_file_read(struct ui_file *file, char *buf, long length_buf)
static void ioscm_flush(SCM port)
static void ioscm_file_port_rewind(struct ui_file *file)
void gdbscm_initialize_ports(void)
void gdbscm_parse_function_args(const char *function_name, int beginning_arg_pos, const SCM *keywords, const char *format,...)
static const unsigned default_write_buf_size
int ui_out_redirect(struct ui_out *uiout, struct ui_file *outstream)
unsigned long long ULONGEST
void * ui_file_data(struct ui_file *file)
#define GDBSCM_HANDLE_GDB_EXCEPTION(exception)
static int ioscm_lseek_address(ioscm_memory_port *iomem, LONGEST offset, int whence)
static const char error_port_name[]
static char * out_of_range_buf_size
static int gdbscm_memory_port_fill_input(SCM port)
void gdb_flush(struct ui_file *file)
struct ui_out * current_uiout
static const char output_port_name[]
static void ioscm_init_memory_port(SCM port, CORE_ADDR start, CORE_ADDR end)
static void ioscm_reinit_memory_port(SCM port, size_t read_buf_size, size_t write_buf_size, const char *func_name)
static SCM output_port_scm
void set_ui_file_write(struct ui_file *file, ui_file_write_ftype *write_ptr)
void set_ui_file_put(struct ui_file *file, ui_file_put_ftype *put_ptr)
void do_cleanups(struct cleanup *old_chain)
static scm_t_off gdbscm_memory_port_seek(SCM port, scm_t_off offset, int whence)
void( ui_file_put_method_ftype)(void *object, const char *buffer, long length_buffer)
static SCM gdbscm_memory_port_p(SCM obj)