32 #if defined (HAVE_POLL_H) 34 #elif defined (HAVE_SYS_POLL_H) 47 void flush ()
override;
48 void write (
const char *buf,
long length_buf)
override;
140 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
143 port = scm_new_port_table_entry (port_type);
145 SCM_SET_CELL_TYPE (port, port_type | mode_bits);
148 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
170 struct pollfd pollfd = { fdes, POLLIN, 0 };
177 struct pollfd test_pollfd = { fdes, POLLIN, 0 };
179 if (poll (&test_pollfd, 1, 0) == 1 && (test_pollfd.revents & POLLNVAL))
189 if (poll (&pollfd, 1, 0) < 0)
192 return pollfd.revents & POLLIN ? 1 : 0;
199 struct timeval timeout;
204 memset (&timeout, 0,
sizeof (timeout));
205 FD_ZERO (&input_fds);
206 FD_SET (fdes, &input_fds);
209 &input_fds, NULL, NULL,
217 return num_found > 0 && FD_ISSET (fdes, &input_fds);
228 scm_t_port *pt = SCM_PTAB_ENTRY (port);
232 return (scm_t_wchar) EOF;
241 return (scm_t_wchar) EOF;
243 pt->read_pos = pt->read_buf;
244 pt->read_end = pt->read_buf + count;
245 return *pt->read_buf;
256 for (i = 0; i <
size; ++i)
326 scm_t_port *pt = SCM_PTAB_ENTRY (port);
327 #define GDB_STDIO_BUFFER_DEFAULT_SIZE 1024 329 int writing = (mode_bits & SCM_WRTNG) != 0;
333 if (!writing &&
size > 0)
336 = (
unsigned char *) scm_gc_malloc_pointerless (
size,
"port buffer");
337 pt->read_pos = pt->read_end = pt->read_buf;
338 pt->read_buf_size =
size;
342 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
343 pt->read_buf_size = 1;
346 if (writing &&
size > 0)
349 = (
unsigned char *) scm_gc_malloc_pointerless (
size,
"port buffer");
350 pt->write_pos = pt->write_buf;
351 pt->write_buf_size =
size;
355 pt->write_buf = pt->write_pos = &pt->shortbuf;
356 pt->write_buf_size = 1;
358 pt->write_end = pt->write_buf + pt->write_buf_size;
366 int is_a_tty = isatty (fd);
368 const char *mode_str;
376 mode_str = is_a_tty ?
"r0" :
"r";
380 mode_str = is_a_tty ?
"w0" :
"w";
384 mode_str = is_a_tty ?
"w0" :
"w";
390 mode_bits = scm_mode_bits ((
char *) mode_str);
406 return scm_from_bool (!SCM_IMP (scm)
458 const char *func_name)
463 SCM_ARG1, func_name,
_(
"output port"));
465 SCM_ARG2, func_name,
_(
"thunk"));
555 new_current = iomem->
size;
564 if (new_current > iomem->
size)
575 scm_t_port *pt = SCM_PTAB_ENTRY (port);
585 to_read = pt->read_buf_size;
594 pt->read_pos = pt->read_buf;
595 pt->read_end = pt->read_buf + to_read;
596 return *pt->read_buf;
605 scm_t_port *pt = SCM_PTAB_ENTRY (port);
607 size_t remaining = pt->read_end - pt->read_pos;
610 if ((offset < 0 && remaining + offset > remaining)
614 _(
"overflow in offset calculation"));
620 pt->read_pos = pt->read_end;
630 pt->rw_active = SCM_PORT_NEITHER;
638 scm_t_port *pt = SCM_PTAB_ENTRY (port);
640 size_t to_write = pt->write_pos - pt->write_buf;
651 _(
"writing beyond end of memory range"));
659 pt->write_pos = pt->write_buf;
660 pt->rw_active = SCM_PORT_NEITHER;
668 scm_t_port *pt = SCM_PTAB_ENTRY (port);
677 _(
"writing beyond end of memory range"));
680 if (pt->write_buf == &pt->shortbuf)
695 size_t space = pt->write_end - pt->write_pos;
700 memcpy (pt->write_pos, data,
size);
701 pt->write_pos +=
size;
705 memcpy (pt->write_pos, data, space);
706 pt->write_pos = pt->write_end;
710 size_t remaining =
size - space;
712 if (remaining >= pt->write_buf_size)
722 memcpy (pt->write_pos, ptr, remaining);
723 pt->write_pos += remaining;
735 scm_t_port *pt = SCM_PTAB_ENTRY (port);
740 if (pt->rw_active == SCM_PORT_WRITE)
754 size_t delta = pt->write_pos - pt->write_buf;
756 if (current + delta < current
757 || current + delta > iomem->
size)
761 result = current + delta;
766 else if (pt->rw_active == SCM_PORT_READ)
770 scm_end_input (port);
779 size_t remaining = pt->read_end - pt->read_pos;
781 if (current - remaining > current
782 || current - remaining < iomem->start)
786 result = current - remaining;
790 if (rc != 0 && pt->read_buf == pt->putback_buf)
792 size_t saved_remaining = pt->saved_read_end - pt->saved_read_pos;
794 if (result - saved_remaining > result
795 || result - saved_remaining < iomem->start)
798 result -= saved_remaining;
827 scm_t_port *pt = SCM_PTAB_ENTRY (port);
832 if (pt->read_buf == pt->putback_buf)
833 pt->read_buf = pt->saved_read_buf;
834 if (pt->read_buf != &pt->shortbuf)
835 xfree (pt->read_buf);
836 if (pt->write_buf != &pt->shortbuf)
837 xfree (pt->write_buf);
838 scm_gc_free (iomem,
sizeof (*iomem),
"memory port");
859 char *
type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
861 scm_puts (
"#<", port);
862 scm_print_port_mode (exp, port);
866 scm_putc (
'>', port);
896 if (*mode !=
'r' && *mode !=
'w')
900 _(
"bad mode string"));
902 for (p = mode + 1; *p !=
'\0'; ++p)
913 _(
"bad mode string"));
919 mode_bits = scm_mode_bits ((
char *) mode);
934 int buffered = (SCM_CELL_WORD_0 (port) & SCM_BUF0) == 0;
941 iomem->
start = start;
943 iomem->
size = end - start;
956 pt = SCM_PTAB_ENTRY (port);
964 pt->read_buf = (
unsigned char *)
xmalloc (pt->read_buf_size);
965 pt->write_buf = (
unsigned char *)
xmalloc (pt->write_buf_size);
969 pt->read_buf = &pt->shortbuf;
970 pt->write_buf = &pt->shortbuf;
972 pt->read_pos = pt->read_end = pt->read_buf;
973 pt->write_pos = pt->write_buf;
974 pt->write_end = pt->write_buf + pt->write_buf_size;
976 SCM_SETSTREAM (port, iomem);
987 size_t write_buf_size,
const char *func_name)
989 scm_t_port *pt = SCM_PTAB_ENTRY (port);
999 if (pt->read_buf == &pt->shortbuf)
1002 scm_misc_error (func_name,
_(
"port is unbuffered: ~a"),
1008 if (read_buf_size != pt->read_buf_size
1009 && pt->read_end != pt->read_buf)
1011 scm_misc_error (func_name,
_(
"read buffer not empty: ~a"),
1015 if (write_buf_size != pt->write_buf_size
1016 && pt->write_pos != pt->write_buf)
1018 scm_misc_error (func_name,
_(
"write buffer not empty: ~a"),
1024 if (read_buf_size != pt->read_buf_size)
1027 pt->read_buf_size = read_buf_size;
1028 xfree (pt->read_buf);
1029 pt->read_buf = (
unsigned char *)
xmalloc (pt->read_buf_size);
1030 pt->read_pos = pt->read_end = pt->read_buf;
1033 if (write_buf_size != pt->write_buf_size)
1036 pt->write_buf_size = write_buf_size;
1037 xfree (pt->write_buf);
1038 pt->write_buf = (
unsigned char *)
xmalloc (pt->write_buf_size);
1039 pt->write_pos = pt->write_buf;
1040 pt->write_end = pt->write_buf + pt->write_buf_size;
1071 const SCM keywords[] = {
1077 int mode_arg_pos = -1, start_arg_pos = -1, size_arg_pos = -1;
1083 &mode_arg_pos, &mode,
1084 &start_arg_pos, &start,
1085 &size_arg_pos, &
size);
1087 scm_dynwind_begin ((scm_t_dynwind_flags) 0);
1090 mode = xstrdup (
"r");
1091 scm_dynwind_free (mode);
1093 if (size_arg_pos > 0)
1097 if (start +
size < start)
1102 _(
"start+size overflows"));
1191 return SCM_UNSPECIFIED;
1233 return SCM_UNSPECIFIED;
1242 Return gdb's input port." },
1246 Return gdb's output port." },
1250 Return gdb's error port." },
1254 Return #t if the object is a gdb:stdio-port." },
1258 Return a port that can be used for reading/writing inferior memory.\n\ 1260 Arguments: [#:mode string] [#:start address] [#:size integer]\n\ 1261 Returns: A port object." },
1265 Return #t if the object is a memory port." },
1269 Return the memory range of the port as (start end)." },
1271 {
"memory-port-read-buffer-size", 1, 0, 0,
1274 Return the size of the read buffer for the memory port." },
1276 {
"set-memory-port-read-buffer-size!", 2, 0, 0,
1279 Set the size of the read buffer for the memory port.\n\ 1281 Arguments: port integer\n\ 1282 Returns: unspecified." },
1284 {
"memory-port-write-buffer-size", 1, 0, 0,
1287 Return the size of the write buffer for the memory port." },
1289 {
"set-memory-port-write-buffer-size!", 2, 0, 0,
1292 Set the size of the write buffer for the memory port.\n\ 1294 Arguments: port integer\n\ 1295 Returns: unspecified." },
1303 {
"%with-gdb-input-from-port", 2, 0, 0,
1306 Temporarily set GDB's input port to PORT and then invoke THUNK.\n\ 1308 Arguments: port thunk\n\ 1309 Returns: The result of calling THUNK.\n\ 1311 This procedure is experimental." },
1314 {
"%with-gdb-output-to-port", 2, 0, 0,
1317 Temporarily set GDB's output port to PORT and then invoke THUNK.\n\ 1319 Arguments: port thunk\n\ 1320 Returns: The result of calling THUNK.\n\ 1322 This procedure is experimental." },
1324 {
"%with-gdb-error-to-port", 2, 0, 0,
1327 Temporarily set GDB's error port to PORT and then invoke THUNK.\n\ 1329 Arguments: port thunk\n\ 1330 Returns: The result of calling THUNK.\n\ 1332 This procedure is experimental." },
static void gdbscm_memory_port_flush(SCM port)
char * hex_string(LONGEST num)
static scm_t_subr as_a_scm_t_subr(SCM(*func)(void))
static SCM orig_output_port_scm
void gdbscm_memory_error(const char *subr, const char *msg, SCM args) ATTRIBUTE_NORETURN
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)
std::unique_ptr< ui_file > ui_file_up
int fputc_filtered(int c, struct ui_file *stream)
static void gdbscm_memory_port_write(SCM port, const void *void_data, size_t size)
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 SCM gdbscm_error_port(void)
void write(const char *buf, long length_buf) override
static const unsigned min_memory_port_buf_size
static SCM gdbscm_stdio_port_p(SCM scm)
static const scheme_function port_functions[]
void * memset(T *s, int c, size_t n)=delete
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)
ioscm_file_port(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)
static const scheme_function private_port_functions[]
static SCM gdbscm_percent_with_gdb_error_to_port(SCM port, SCM thunk)
scoped_restore_tmpl< T > make_scoped_restore(T *var)
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)
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
SCM gdbscm_scm_from_c_string(const char *string)
static void fputsn_filtered(const char *s, size_t size, struct ui_file *stream)
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 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)
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 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
T & emplace(Args &&... args)
static SCM orig_error_port_scm
static SCM orig_input_port_scm
static void ioscm_init_gdb_stdio_port(void)
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
static void ioscm_init_memory_port_type(void)
static char memory_port_desc_name[]
static int gdbscm_memory_port_close(SCM port)
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)
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
unsigned long long ULONGEST
#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)
int interruptible_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
void gdb_flush(struct ui_file *file)
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
static scm_t_off gdbscm_memory_port_seek(SCM port, scm_t_off offset, int whence)
static SCM gdbscm_memory_port_p(SCM obj)