1 /* Support for connecting Guile's stdio to GDB's.
2 as well as r/w memory via ports.
4 Copyright (C) 2014-2024 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* See README file in this directory for implementation notes, coding
22 conventions, et.al. */
24 #include "gdbsupport/gdb_select.h"
27 #include "guile-internal.h"
31 #if defined (HAVE_POLL_H)
33 #elif defined (HAVE_SYS_POLL_H)
38 /* Whether we're using Guile < 2.2 and its clumsy port API. */
40 #define USING_GUILE_BEFORE_2_2 \
41 (SCM_MAJOR_VERSION == 2 && SCM_MINOR_VERSION == 0)
44 /* A ui-file for sending output to Guile. */
46 class ioscm_file_port
: public ui_file
49 /* Return a ui_file that writes to PORT. */
50 explicit ioscm_file_port (SCM port
);
52 void flush () override
;
53 void write (const char *buf
, long length_buf
) override
;
59 /* Data for a memory port. */
61 struct ioscm_memory_port
63 /* Bounds of memory range this port is allowed to access: [start, end).
64 This means that 0xff..ff is not accessible. I can live with that. */
67 /* (end - start), recorded for convenience. */
70 /* Think of this as the lseek value maintained by the kernel.
71 This value is always in the range [0, size]. */
74 #if USING_GUILE_BEFORE_2_2
75 /* The size of the internal r/w buffers.
76 Scheme ports aren't a straightforward mapping to memory r/w.
77 Generally the user specifies how much to r/w and all access is
78 unbuffered. We don't try to provide equivalent access, but we allow
79 the user to specify these values to help get something similar. */
80 unsigned read_buf_size
, write_buf_size
;
84 /* Copies of the original system input/output/error ports.
85 These are recorded for debugging purposes. */
86 static SCM orig_input_port_scm
;
87 static SCM orig_output_port_scm
;
88 static SCM orig_error_port_scm
;
90 /* This is the stdio port descriptor, scm_ptob_descriptor. */
91 #if USING_GUILE_BEFORE_2_2
92 static scm_t_bits stdio_port_desc
;
94 static scm_t_port_type
*stdio_port_desc
;
97 /* Note: scm_make_port_type takes a char * instead of a const char *. */
98 static /*const*/ char stdio_port_desc_name
[] = "gdb:stdio-port";
100 /* Names of each gdb port. */
101 static const char input_port_name
[] = "gdb:stdin";
102 static const char output_port_name
[] = "gdb:stdout";
103 static const char error_port_name
[] = "gdb:stderr";
105 /* This is the actual port used from Guile.
106 We don't expose these to the user though, to ensure they're not
108 static SCM input_port_scm
;
109 static SCM output_port_scm
;
110 static SCM error_port_scm
;
112 /* Internal enum for specifying output port. */
113 enum oport
{ GDB_STDOUT
, GDB_STDERR
};
115 /* This is the memory port descriptor, scm_ptob_descriptor. */
116 #if USING_GUILE_BEFORE_2_2
117 static scm_t_bits memory_port_desc
;
119 static scm_t_port_type
*memory_port_desc
;
122 /* Note: scm_make_port_type takes a char * instead of a const char *. */
123 static /*const*/ char memory_port_desc_name
[] = "gdb:memory-port";
125 #if USING_GUILE_BEFORE_2_2
127 /* The default amount of memory to fetch for each read/write request.
128 Scheme ports don't provide a way to specify the size of a read,
129 which is important to us to minimize the number of inferior interactions,
130 which over a remote link can be important. To compensate we augment the
131 port API with a new function that let's the user specify how much the next
132 read request should fetch. This is the initial value for each new port. */
133 static const unsigned default_read_buf_size
= 16;
134 static const unsigned default_write_buf_size
= 16;
136 /* Arbitrarily limit memory port buffers to 1 byte to 4K. */
137 static const unsigned min_memory_port_buf_size
= 1;
138 static const unsigned max_memory_port_buf_size
= 4096;
140 /* "out of range" error message for buf sizes. */
141 static gdb::unique_xmalloc_ptr
<char> out_of_range_buf_size
;
145 /* The maximum values to use for get_natural_buffer_sizes. */
146 static const unsigned natural_buf_size
= 16;
150 /* Keywords used by open-memory. */
151 static SCM mode_keyword
;
152 static SCM start_keyword
;
153 static SCM size_keyword
;
155 /* Helper to do the low level work of opening a port. */
157 #if USING_GUILE_BEFORE_2_2
160 ioscm_open_port (scm_t_bits port_type
, long mode_bits
, scm_t_bits stream
)
164 #if 0 /* TODO: Guile doesn't export this. What to do? */
165 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex
);
168 port
= scm_new_port_table_entry (port_type
);
170 SCM_SET_CELL_TYPE (port
, port_type
| mode_bits
);
171 SCM_SETSTREAM (port
, stream
);
173 #if 0 /* TODO: Guile doesn't export this. What to do? */
174 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex
);
183 ioscm_open_port (scm_t_port_type
*port_type
, long mode_bits
, scm_t_bits stream
)
185 return scm_c_make_port (port_type
, mode_bits
, stream
);
191 /* Support for connecting Guile's stdio ports to GDB's stdio ports. */
193 /* Print a string S, length SIZE, but don't escape characters, except
197 fputsn_filtered (const char *s
, size_t size
, struct ui_file
*stream
)
201 for (i
= 0; i
< size
; ++i
)
204 gdb_puts ("\\000", stream
);
206 gdb_putc (s
[i
], stream
);
210 #if USING_GUILE_BEFORE_2_2
212 /* The scm_t_ptob_descriptor.input_waiting "method".
213 Return a lower bound on the number of bytes available for input. */
216 ioscm_input_waiting (SCM port
)
220 if (! scm_is_eq (port
, input_port_scm
))
225 /* This is copied from libguile/fports.c. */
226 struct pollfd pollfd
= { fdes
, POLLIN
, 0 };
227 static int use_poll
= -1;
231 /* This is copied from event-loop.c: poll cannot be used for stdin on
232 m68k-motorola-sysv. */
233 struct pollfd test_pollfd
= { fdes
, POLLIN
, 0 };
235 if (poll (&test_pollfd
, 1, 0) == 1 && (test_pollfd
.revents
& POLLNVAL
))
243 /* Guile doesn't export SIGINT hooks like Python does.
244 For now pass EINTR to scm_syserror, that's what fports.c does. */
245 if (poll (&pollfd
, 1, 0) < 0)
246 scm_syserror (FUNC_NAME
);
248 return pollfd
.revents
& POLLIN
? 1 : 0;
255 struct timeval timeout
;
257 int num_fds
= fdes
+ 1;
260 memset (&timeout
, 0, sizeof (timeout
));
261 FD_ZERO (&input_fds
);
262 FD_SET (fdes
, &input_fds
);
264 num_found
= interruptible_select (num_fds
,
265 &input_fds
, NULL
, NULL
,
269 /* Guile doesn't export SIGINT hooks like Python does.
270 For now pass EINTR to scm_syserror, that's what fports.c does. */
271 scm_syserror (FUNC_NAME
);
273 return num_found
> 0 && FD_ISSET (fdes
, &input_fds
);
277 /* The scm_t_ptob_descriptor.fill_input "method". */
280 ioscm_fill_input (SCM port
)
282 /* Borrowed from libguile/fports.c. */
284 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
286 /* If we're called on stdout,stderr, punt. */
287 if (! scm_is_eq (port
, input_port_scm
))
288 return (scm_t_wchar
) EOF
; /* Set errno and return -1? */
290 gdb_flush (gdb_stdout
);
291 gdb_flush (gdb_stderr
);
293 count
= gdb_stdin
->read ((char *) pt
->read_buf
, pt
->read_buf_size
);
295 scm_syserror (FUNC_NAME
);
297 return (scm_t_wchar
) EOF
;
299 pt
->read_pos
= pt
->read_buf
;
300 pt
->read_end
= pt
->read_buf
+ count
;
301 return *pt
->read_buf
;
304 /* Write to gdb's stdout or stderr. */
307 ioscm_write (SCM port
, const void *data
, size_t size
)
310 /* If we're called on stdin, punt. */
311 if (scm_is_eq (port
, input_port_scm
))
314 gdbscm_gdb_exception exc
{};
317 if (scm_is_eq (port
, error_port_scm
))
318 fputsn_filtered ((const char *) data
, size
, gdb_stderr
);
320 fputsn_filtered ((const char *) data
, size
, gdb_stdout
);
322 catch (const gdb_exception
&except
)
324 exc
= unpack (except
);
326 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
329 /* Flush gdb's stdout or stderr. */
332 ioscm_flush (SCM port
)
334 /* If we're called on stdin, punt. */
335 if (scm_is_eq (port
, input_port_scm
))
338 if (scm_is_eq (port
, error_port_scm
))
339 gdb_flush (gdb_stderr
);
341 gdb_flush (gdb_stdout
);
344 #else /* !USING_GUILE_BEFORE_2_2 */
346 /* Read up to COUNT bytes into bytevector DST at offset START. Return the
347 number of bytes read, zero for the end of file. */
350 ioscm_read_from_port (SCM port
, SCM dst
, size_t start
, size_t count
)
355 /* If we're called on stdout,stderr, punt. */
356 if (! scm_is_eq (port
, input_port_scm
))
359 gdb_flush (gdb_stdout
);
360 gdb_flush (gdb_stderr
);
362 read_buf
= (char *) SCM_BYTEVECTOR_CONTENTS (dst
) + start
;
363 read
= gdb_stdin
->read (read_buf
, count
);
365 scm_syserror (FUNC_NAME
);
367 return (size_t) read
;
370 /* Write to gdb's stdout or stderr. */
373 ioscm_write (SCM port
, SCM src
, size_t start
, size_t count
)
375 const char *data
= (char *) SCM_BYTEVECTOR_CONTENTS (src
) + start
;
377 /* If we're called on stdin, punt. */
378 if (scm_is_eq (port
, input_port_scm
))
381 gdbscm_gdb_exception exc
{};
384 if (scm_is_eq (port
, error_port_scm
))
385 fputsn_filtered ((const char *) data
, count
, gdb_stderr
);
387 fputsn_filtered ((const char *) data
, count
, gdb_stdout
);
389 catch (const gdb_exception
&except
)
391 exc
= unpack (except
);
393 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
398 #endif /* !USING_GUILE_BEFORE_2_2 */
400 /* Initialize the gdb stdio port type.
402 N.B. isatty? will fail on these ports, it is only supported for file
403 ports. IWBN if we could "subclass" file ports. */
406 ioscm_init_gdb_stdio_port (void)
408 stdio_port_desc
= scm_make_port_type (stdio_port_desc_name
,
409 #if USING_GUILE_BEFORE_2_2
412 ioscm_read_from_port
,
416 #if USING_GUILE_BEFORE_2_2
417 scm_set_port_input_waiting (stdio_port_desc
, ioscm_input_waiting
);
418 scm_set_port_flush (stdio_port_desc
, ioscm_flush
);
420 scm_set_port_read_wait_fd (stdio_port_desc
, STDIN_FILENO
);
424 #define GDB_STDIO_BUFFER_DEFAULT_SIZE 1024
426 #if USING_GUILE_BEFORE_2_2
428 /* Subroutine of ioscm_make_gdb_stdio_port to simplify it.
429 Set up the buffers of port PORT.
430 MODE_BITS are the mode bits of PORT. */
433 ioscm_init_stdio_buffers (SCM port
, long mode_bits
)
435 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
436 int size
= mode_bits
& SCM_BUF0
? 0 : GDB_STDIO_BUFFER_DEFAULT_SIZE
;
437 int writing
= (mode_bits
& SCM_WRTNG
) != 0;
439 /* This is heavily copied from scm_fport_buffer_add. */
441 if (!writing
&& size
> 0)
444 = (unsigned char *) scm_gc_malloc_pointerless (size
, "port buffer");
445 pt
->read_pos
= pt
->read_end
= pt
->read_buf
;
446 pt
->read_buf_size
= size
;
450 pt
->read_pos
= pt
->read_buf
= pt
->read_end
= &pt
->shortbuf
;
451 pt
->read_buf_size
= 1;
454 if (writing
&& size
> 0)
457 = (unsigned char *) scm_gc_malloc_pointerless (size
, "port buffer");
458 pt
->write_pos
= pt
->write_buf
;
459 pt
->write_buf_size
= size
;
463 pt
->write_buf
= pt
->write_pos
= &pt
->shortbuf
;
464 pt
->write_buf_size
= 1;
466 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
472 ioscm_init_stdio_buffers (SCM port
, long mode_bits
)
474 if (mode_bits
& SCM_BUF0
)
475 scm_setvbuf (port
, scm_from_utf8_symbol ("none"), scm_from_size_t (0));
477 scm_setvbuf (port
, scm_from_utf8_symbol ("block"),
478 scm_from_size_t (GDB_STDIO_BUFFER_DEFAULT_SIZE
));
483 /* Create a gdb stdio port. */
486 ioscm_make_gdb_stdio_port (int fd
)
488 int is_a_tty
= isatty (fd
);
490 const char *mode_str
;
497 name
= input_port_name
;
498 mode_str
= is_a_tty
? "r0" : "r";
501 name
= output_port_name
;
502 mode_str
= is_a_tty
? "w0" : "w";
505 name
= error_port_name
;
506 mode_str
= is_a_tty
? "w0" : "w";
509 gdb_assert_not_reached ("bad stdio file descriptor");
512 mode_bits
= scm_mode_bits ((char *) mode_str
);
513 port
= ioscm_open_port (stdio_port_desc
, mode_bits
, 0);
515 scm_set_port_filename_x (port
, gdbscm_scm_from_c_string (name
));
517 ioscm_init_stdio_buffers (port
, mode_bits
);
522 /* (stdio-port? object) -> boolean */
525 gdbscm_stdio_port_p (SCM scm
)
527 #if USING_GUILE_BEFORE_2_2
528 /* This is copied from SCM_FPORTP. */
529 return scm_from_bool (!SCM_IMP (scm
)
530 && (SCM_TYP16 (scm
) == stdio_port_desc
));
532 return scm_from_bool (SCM_PORTP (scm
)
533 && (SCM_PORT_TYPE (scm
) == stdio_port_desc
));
537 /* GDB's ports are accessed via functions to keep them read-only. */
539 /* (input-port) -> port */
542 gdbscm_input_port (void)
544 return input_port_scm
;
547 /* (output-port) -> port */
550 gdbscm_output_port (void)
552 return output_port_scm
;
555 /* (error-port) -> port */
558 gdbscm_error_port (void)
560 return error_port_scm
;
563 /* Support for sending GDB I/O to Guile ports. */
565 ioscm_file_port::ioscm_file_port (SCM port
)
570 ioscm_file_port::flush ()
575 ioscm_file_port::write (const char *buffer
, long length_buffer
)
577 scm_c_write (m_port
, buffer
, length_buffer
);
581 /* Helper routine for with-{output,error}-to-port. */
584 ioscm_with_output_to_port_worker (SCM port
, SCM thunk
, enum oport oport
,
585 const char *func_name
)
589 SCM_ASSERT_TYPE (gdbscm_is_true (scm_output_port_p (port
)), port
,
590 SCM_ARG1
, func_name
, _("output port"));
591 SCM_ASSERT_TYPE (gdbscm_is_true (scm_thunk_p (thunk
)), thunk
,
592 SCM_ARG2
, func_name
, _("thunk"));
594 set_batch_flag_and_restore_page_info save_page_info
;
596 scoped_restore restore_async
= make_scoped_restore (¤t_ui
->async
, 0);
598 ui_file_up
port_file (new ioscm_file_port (port
));
600 scoped_restore save_file
= make_scoped_restore (oport
== GDB_STDERR
601 ? &gdb_stderr
: &gdb_stdout
);
604 std::optional
<ui_out_redirect_pop
> redirect_popper
;
605 if (oport
== GDB_STDERR
)
606 gdb_stderr
= port_file
.get ();
609 redirect_popper
.emplace (current_uiout
, port_file
.get ());
611 gdb_stdout
= port_file
.get ();
614 result
= gdbscm_safe_call_0 (thunk
, NULL
);
617 if (gdbscm_is_exception (result
))
618 gdbscm_throw (result
);
623 /* (%with-gdb-output-to-port port thunk) -> object
624 This function is experimental.
625 IWBN to not include "gdb" in the name, but it would collide with a standard
626 procedure, and it's common to import the gdb module without a prefix.
627 There are ways around this, but they're more cumbersome.
629 This has % in the name because it's experimental, and we want the
630 user-visible version to come from module (gdb experimental). */
633 gdbscm_percent_with_gdb_output_to_port (SCM port
, SCM thunk
)
635 return ioscm_with_output_to_port_worker (port
, thunk
, GDB_STDOUT
, FUNC_NAME
);
638 /* (%with-gdb-error-to-port port thunk) -> object
639 This function is experimental.
640 IWBN to not include "gdb" in the name, but it would collide with a standard
641 procedure, and it's common to import the gdb module without a prefix.
642 There are ways around this, but they're more cumbersome.
644 This has % in the name because it's experimental, and we want the
645 user-visible version to come from module (gdb experimental). */
648 gdbscm_percent_with_gdb_error_to_port (SCM port
, SCM thunk
)
650 return ioscm_with_output_to_port_worker (port
, thunk
, GDB_STDERR
, FUNC_NAME
);
653 /* Support for r/w memory via ports. */
655 /* Perform an "lseek" to OFFSET,WHENCE on memory port IOMEM.
656 OFFSET must be in the range [0,size].
657 The result is non-zero for success, zero for failure. */
660 ioscm_lseek_address (ioscm_memory_port
*iomem
, LONGEST offset
, int whence
)
662 CORE_ADDR new_current
;
664 gdb_assert (iomem
->current
<= iomem
->size
);
669 /* Catch over/underflow. */
670 if ((offset
< 0 && iomem
->current
+ offset
> iomem
->current
)
671 || (offset
> 0 && iomem
->current
+ offset
< iomem
->current
))
673 new_current
= iomem
->current
+ offset
;
676 new_current
= offset
;
681 new_current
= iomem
->size
;
684 /* TODO: Not supported yet. */
690 if (new_current
> iomem
->size
)
692 iomem
->current
= new_current
;
696 #if USING_GUILE_BEFORE_2_2
698 /* "fill_input" method for memory ports. */
701 gdbscm_memory_port_fill_input (SCM port
)
703 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
704 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
707 /* "current" is the offset of the first byte we want to read. */
708 gdb_assert (iomem
->current
<= iomem
->size
);
709 if (iomem
->current
== iomem
->size
)
712 /* Don't read outside the allowed memory range. */
713 to_read
= pt
->read_buf_size
;
714 if (to_read
> iomem
->size
- iomem
->current
)
715 to_read
= iomem
->size
- iomem
->current
;
717 if (target_read_memory (iomem
->start
+ iomem
->current
, pt
->read_buf
,
719 gdbscm_memory_error (FUNC_NAME
, _("error reading memory"), SCM_EOL
);
721 iomem
->current
+= to_read
;
722 pt
->read_pos
= pt
->read_buf
;
723 pt
->read_end
= pt
->read_buf
+ to_read
;
724 return *pt
->read_buf
;
727 /* "end_input" method for memory ports.
728 Clear the read buffer and adjust the file position for unread bytes. */
731 gdbscm_memory_port_end_input (SCM port
, int offset
)
733 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
734 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
735 size_t remaining
= pt
->read_end
- pt
->read_pos
;
737 /* Note: Use of "int offset" is specified by Guile ports API. */
738 if ((offset
< 0 && remaining
+ offset
> remaining
)
739 || (offset
> 0 && remaining
+ offset
< remaining
))
741 gdbscm_out_of_range_error (FUNC_NAME
, 0, scm_from_int (offset
),
742 _("overflow in offset calculation"));
748 pt
->read_pos
= pt
->read_end
;
749 /* Throw error if unread-char used at beginning of file
750 then attempting to write. Seems correct. */
751 if (!ioscm_lseek_address (iomem
, -offset
, SEEK_CUR
))
753 gdbscm_out_of_range_error (FUNC_NAME
, 0, scm_from_int (offset
),
758 pt
->rw_active
= SCM_PORT_NEITHER
;
761 /* "flush" method for memory ports. */
764 gdbscm_memory_port_flush (SCM port
)
766 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
767 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
768 size_t to_write
= pt
->write_pos
- pt
->write_buf
;
773 /* There's no way to indicate a short write, so if the request goes past
774 the end of the port's memory range, flag an error. */
775 if (to_write
> iomem
->size
- iomem
->current
)
777 gdbscm_out_of_range_error (FUNC_NAME
, 0,
778 gdbscm_scm_from_ulongest (to_write
),
779 _("writing beyond end of memory range"));
782 if (target_write_memory (iomem
->start
+ iomem
->current
, pt
->write_buf
,
784 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"), SCM_EOL
);
786 iomem
->current
+= to_write
;
787 pt
->write_pos
= pt
->write_buf
;
788 pt
->rw_active
= SCM_PORT_NEITHER
;
791 /* "seek" method for memory ports. */
794 gdbscm_memory_port_seek (SCM port
, scm_t_off offset
, int whence
)
796 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
797 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
801 if (pt
->rw_active
== SCM_PORT_WRITE
)
803 if (offset
!= 0 || whence
!= SEEK_CUR
)
805 gdbscm_memory_port_flush (port
);
806 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
807 result
= iomem
->current
;
811 /* Read current position without disturbing the buffer,
812 but flag an error if what's in the buffer goes outside the
814 CORE_ADDR current
= iomem
->current
;
815 size_t delta
= pt
->write_pos
- pt
->write_buf
;
817 if (current
+ delta
< current
818 || current
+ delta
> iomem
->size
)
822 result
= current
+ delta
;
827 else if (pt
->rw_active
== SCM_PORT_READ
)
829 if (offset
!= 0 || whence
!= SEEK_CUR
)
831 scm_end_input (port
);
832 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
833 result
= iomem
->current
;
837 /* Read current position without disturbing the buffer
838 (particularly the unread-char buffer). */
839 CORE_ADDR current
= iomem
->current
;
840 size_t remaining
= pt
->read_end
- pt
->read_pos
;
842 if (current
- remaining
> current
843 || current
- remaining
< iomem
->start
)
847 result
= current
- remaining
;
851 if (rc
!= 0 && pt
->read_buf
== pt
->putback_buf
)
853 size_t saved_remaining
= pt
->saved_read_end
- pt
->saved_read_pos
;
855 if (result
- saved_remaining
> result
856 || result
- saved_remaining
< iomem
->start
)
859 result
-= saved_remaining
;
863 else /* SCM_PORT_NEITHER */
865 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
866 result
= iomem
->current
;
871 gdbscm_out_of_range_error (FUNC_NAME
, 0,
872 gdbscm_scm_from_longest (offset
),
876 /* TODO: The Guile API doesn't support 32x64. We can't fix that here,
877 and there's no need to throw an error if the new address can't be
878 represented in a scm_t_off. But we could return something less
883 /* "write" method for memory ports. */
886 gdbscm_memory_port_write (SCM port
, const void *void_data
, size_t size
)
888 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
889 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
890 const gdb_byte
*data
= (const gdb_byte
*) void_data
;
892 /* There's no way to indicate a short write, so if the request goes past
893 the end of the port's memory range, flag an error. */
894 if (size
> iomem
->size
- iomem
->current
)
896 gdbscm_out_of_range_error (FUNC_NAME
, 0, gdbscm_scm_from_ulongest (size
),
897 _("writing beyond end of memory range"));
900 if (pt
->write_buf
== &pt
->shortbuf
)
902 /* Unbuffered port. */
903 if (target_write_memory (iomem
->start
+ iomem
->current
, data
, size
) != 0)
904 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"), SCM_EOL
);
905 iomem
->current
+= size
;
909 /* Note: The edge case of what to do when the buffer exactly fills is
910 debatable. Guile flushes when the buffer exactly fills up, so we
911 do too. It's counter-intuitive to my mind, but in case there's a
912 subtlety somewhere that depends on this, we do the same. */
915 size_t space
= pt
->write_end
- pt
->write_pos
;
919 /* Data fits in buffer, and does not fill it. */
920 memcpy (pt
->write_pos
, data
, size
);
921 pt
->write_pos
+= size
;
925 memcpy (pt
->write_pos
, data
, space
);
926 pt
->write_pos
= pt
->write_end
;
927 gdbscm_memory_port_flush (port
);
929 const gdb_byte
*ptr
= data
+ space
;
930 size_t remaining
= size
- space
;
932 if (remaining
>= pt
->write_buf_size
)
934 if (target_write_memory (iomem
->start
+ iomem
->current
, ptr
,
936 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"),
938 iomem
->current
+= remaining
;
942 memcpy (pt
->write_pos
, ptr
, remaining
);
943 pt
->write_pos
+= remaining
;
950 /* "close" method for memory ports. */
953 gdbscm_memory_port_close (SCM port
)
955 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
956 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
958 gdbscm_memory_port_flush (port
);
960 if (pt
->read_buf
== pt
->putback_buf
)
961 pt
->read_buf
= pt
->saved_read_buf
;
962 if (pt
->read_buf
!= &pt
->shortbuf
)
963 xfree (pt
->read_buf
);
964 if (pt
->write_buf
!= &pt
->shortbuf
)
965 xfree (pt
->write_buf
);
966 scm_gc_free (iomem
, sizeof (*iomem
), "memory port");
971 /* "free" method for memory ports. */
974 gdbscm_memory_port_free (SCM port
)
976 gdbscm_memory_port_close (port
);
981 /* Re-initialize a memory port, updating its read/write buffer sizes.
982 An exception is thrown if the port is unbuffered.
983 TODO: Allow switching buffered/unbuffered.
984 An exception is also thrown if data is still buffered, except in the case
985 where the buffer size isn't changing (since that's just a nop). */
988 ioscm_reinit_memory_port (SCM port
, size_t read_buf_size
,
989 size_t write_buf_size
, const char *func_name
)
991 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
992 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
994 gdb_assert (read_buf_size
>= min_memory_port_buf_size
995 && read_buf_size
<= max_memory_port_buf_size
);
996 gdb_assert (write_buf_size
>= min_memory_port_buf_size
997 && write_buf_size
<= max_memory_port_buf_size
);
999 /* First check if the port is unbuffered. */
1001 if (pt
->read_buf
== &pt
->shortbuf
)
1003 gdb_assert (pt
->write_buf
== &pt
->shortbuf
);
1004 scm_misc_error (func_name
, _("port is unbuffered: ~a"),
1008 /* Next check if anything is buffered. */
1010 if (read_buf_size
!= pt
->read_buf_size
1011 && pt
->read_end
!= pt
->read_buf
)
1013 scm_misc_error (func_name
, _("read buffer not empty: ~a"),
1017 if (write_buf_size
!= pt
->write_buf_size
1018 && pt
->write_pos
!= pt
->write_buf
)
1020 scm_misc_error (func_name
, _("write buffer not empty: ~a"),
1024 /* Now we can update the buffer sizes, but only if the size has changed. */
1026 if (read_buf_size
!= pt
->read_buf_size
)
1028 iomem
->read_buf_size
= read_buf_size
;
1029 pt
->read_buf_size
= read_buf_size
;
1030 xfree (pt
->read_buf
);
1031 pt
->read_buf
= (unsigned char *) xmalloc (pt
->read_buf_size
);
1032 pt
->read_pos
= pt
->read_end
= pt
->read_buf
;
1035 if (write_buf_size
!= pt
->write_buf_size
)
1037 iomem
->write_buf_size
= write_buf_size
;
1038 pt
->write_buf_size
= write_buf_size
;
1039 xfree (pt
->write_buf
);
1040 pt
->write_buf
= (unsigned char *) xmalloc (pt
->write_buf_size
);
1041 pt
->write_pos
= pt
->write_buf
;
1042 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
1046 #else /* !USING_GUILE_BEFORE_2_2 */
1048 /* The semantics get weird if the buffer size is larger than the port range,
1049 so provide a better default buffer size. */
1052 gdbscm_get_natural_buffer_sizes (SCM port
, size_t *read_size
,
1055 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1057 size_t size
= natural_buf_size
;
1058 if (iomem
!= NULL
&& iomem
->size
< size
)
1060 *read_size
= *write_size
= size
;
1063 /* Read up to COUNT bytes into bytevector DST at offset START. Return the
1064 number of bytes read, zero for the end of file. */
1067 gdbscm_memory_port_read (SCM port
, SCM dst
, size_t start
, size_t count
)
1070 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1072 /* "current" is the offset of the first byte we want to read. */
1073 gdb_assert (iomem
->current
<= iomem
->size
);
1074 if (iomem
->current
== iomem
->size
)
1077 /* Don't read outside the allowed memory range. */
1078 if (count
> iomem
->size
- iomem
->current
)
1079 count
= iomem
->size
- iomem
->current
;
1081 read_buf
= (gdb_byte
*) SCM_BYTEVECTOR_CONTENTS (dst
) + start
;
1082 if (target_read_memory (iomem
->start
+ iomem
->current
, read_buf
,
1084 gdbscm_memory_error (FUNC_NAME
, _("error reading memory"), SCM_EOL
);
1086 iomem
->current
+= count
;
1091 gdbscm_memory_port_write (SCM port
, SCM src
, size_t start
, size_t count
)
1093 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1094 const gdb_byte
*data
=
1095 (const gdb_byte
*) SCM_BYTEVECTOR_CONTENTS (src
) + start
;
1097 /* If the request goes past the end of the port's memory range, flag an
1099 if (count
> iomem
->size
- iomem
->current
)
1100 gdbscm_out_of_range_error (FUNC_NAME
, 0, scm_from_size_t (count
),
1101 _("writing beyond end of memory range"));
1103 if (target_write_memory (iomem
->start
+ iomem
->current
, data
,
1105 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"),
1108 iomem
->current
+= count
;
1114 gdbscm_memory_port_seek (SCM port
, scm_t_off offset
, int whence
)
1116 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1119 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
1121 gdbscm_out_of_range_error (FUNC_NAME
, 0,
1122 gdbscm_scm_from_longest (offset
),
1125 /* TODO: The Guile API doesn't support 32x64. We can't fix that here,
1126 and there's no need to throw an error if the new address can't be
1127 represented in a scm_t_off. But we could return something less
1129 return iomem
->current
;
1133 gdbscm_memory_port_close (SCM port
)
1135 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1136 scm_gc_free (iomem
, sizeof (*iomem
), "memory port");
1137 SCM_SETSTREAM (port
, NULL
);
1140 #endif /* !USING_GUILE_BEFORE_2_2 */
1142 /* "print" method for memory ports. */
1145 gdbscm_memory_port_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
1147 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (exp
);
1149 scm_puts ("#<", port
);
1150 scm_print_port_mode (exp
, port
);
1151 /* scm_print_port_mode includes a trailing space. */
1152 gdbscm_printf (port
, "%s %s-%s", memory_port_desc_name
,
1153 hex_string (iomem
->start
), hex_string (iomem
->end
));
1154 scm_putc ('>', port
);
1158 /* Create the port type used for memory. */
1161 ioscm_init_memory_port_type (void)
1163 memory_port_desc
= scm_make_port_type (memory_port_desc_name
,
1164 #if USING_GUILE_BEFORE_2_2
1165 gdbscm_memory_port_fill_input
,
1167 gdbscm_memory_port_read
,
1169 gdbscm_memory_port_write
);
1171 #if USING_GUILE_BEFORE_2_2
1172 scm_set_port_end_input (memory_port_desc
, gdbscm_memory_port_end_input
);
1173 scm_set_port_flush (memory_port_desc
, gdbscm_memory_port_flush
);
1174 scm_set_port_free (memory_port_desc
, gdbscm_memory_port_free
);
1176 scm_set_port_get_natural_buffer_sizes (memory_port_desc
,
1177 gdbscm_get_natural_buffer_sizes
);
1179 scm_set_port_seek (memory_port_desc
, gdbscm_memory_port_seek
);
1180 scm_set_port_close (memory_port_desc
, gdbscm_memory_port_close
);
1181 scm_set_port_print (memory_port_desc
, gdbscm_memory_port_print
);
1184 /* Helper for gdbscm_open_memory to parse the mode bits.
1185 An exception is thrown if MODE is invalid. */
1188 ioscm_parse_mode_bits (const char *func_name
, const char *mode
)
1193 if (*mode
!= 'r' && *mode
!= 'w')
1195 gdbscm_out_of_range_error (func_name
, 0,
1196 gdbscm_scm_from_c_string (mode
),
1197 _("bad mode string"));
1199 for (p
= mode
+ 1; *p
!= '\0'; ++p
)
1208 gdbscm_out_of_range_error (func_name
, 0,
1209 gdbscm_scm_from_c_string (mode
),
1210 _("bad mode string"));
1214 /* Kinda awkward to convert the mode from SCM -> string only to have Guile
1215 convert it back to SCM, but that's the API we have to work with. */
1216 mode_bits
= scm_mode_bits ((char *) mode
);
1221 /* Return the memory object to be used as a "stream" associated with a memory
1222 port for the START--END range. */
1224 static ioscm_memory_port
*
1225 ioscm_init_memory_port_stream (CORE_ADDR start
, CORE_ADDR end
)
1227 ioscm_memory_port
*iomem
;
1229 gdb_assert (start
<= end
);
1231 iomem
= (ioscm_memory_port
*) scm_gc_malloc_pointerless (sizeof (*iomem
),
1234 iomem
->start
= start
;
1236 iomem
->size
= end
- start
;
1242 #if USING_GUILE_BEFORE_2_2
1244 /* Helper for gdbscm_open_memory to finish initializing the port.
1245 The port has address range [start,end).
1246 This means that address of 0xff..ff is not accessible.
1247 I can live with that. */
1250 ioscm_init_memory_port_buffers (SCM port
)
1252 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1254 int buffered
= (SCM_CELL_WORD_0 (port
) & SCM_BUF0
) == 0;
1257 iomem
->read_buf_size
= default_read_buf_size
;
1258 iomem
->write_buf_size
= default_write_buf_size
;
1262 iomem
->read_buf_size
= 1;
1263 iomem
->write_buf_size
= 1;
1266 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
1267 /* Match the expectation of `binary-port?'. */
1268 pt
->encoding
= NULL
;
1270 pt
->read_buf_size
= iomem
->read_buf_size
;
1271 pt
->write_buf_size
= iomem
->write_buf_size
;
1274 pt
->read_buf
= (unsigned char *) xmalloc (pt
->read_buf_size
);
1275 pt
->write_buf
= (unsigned char *) xmalloc (pt
->write_buf_size
);
1279 pt
->read_buf
= &pt
->shortbuf
;
1280 pt
->write_buf
= &pt
->shortbuf
;
1282 pt
->read_pos
= pt
->read_end
= pt
->read_buf
;
1283 pt
->write_pos
= pt
->write_buf
;
1284 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
1289 /* (open-memory [#:mode string] [#:start address] [#:size integer]) -> port
1290 Return a port that can be used for reading and writing memory.
1291 MODE is a string, and must be one of "r", "w", or "r+".
1292 "0" may be appended to MODE to mark the port as unbuffered.
1293 For compatibility "b" (binary) may also be appended, but we ignore it:
1294 memory ports are binary only.
1296 The chunk of memory that can be accessed can be bounded.
1297 If both START,SIZE are unspecified, all of memory can be accessed
1298 (except 0xff..ff). If only START is specified, all of memory from that
1299 point on can be accessed (except 0xff..ff). If only SIZE if specified,
1300 all memory in [0,SIZE) can be accessed. If both are specified, all memory
1301 in [START,START+SIZE) can be accessed.
1303 Note: If it becomes useful enough we can later add #:end as an alternative
1304 to #:size. For now it is left out.
1306 The result is a Scheme port, and its semantics are a bit odd for accessing
1307 memory (e.g., unget), but we don't try to hide this. It's a port.
1309 N.B. Seeks on the port must be in the range [0,size].
1310 This is for similarity with bytevector ports, and so that one can seek
1311 to the first byte. */
1314 gdbscm_open_memory (SCM rest
)
1316 const SCM keywords
[] = {
1317 mode_keyword
, start_keyword
, size_keyword
, SCM_BOOL_F
1320 CORE_ADDR start
= 0;
1322 int mode_arg_pos
= -1, start_arg_pos
= -1, size_arg_pos
= -1;
1327 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, keywords
, "#sUU", rest
,
1328 &mode_arg_pos
, &mode
,
1329 &start_arg_pos
, &start
,
1330 &size_arg_pos
, &size
);
1332 scm_dynwind_begin ((scm_t_dynwind_flags
) 0);
1335 mode
= xstrdup ("r");
1336 scm_dynwind_free (mode
);
1338 if (size_arg_pos
> 0)
1340 /* For now be strict about start+size overflowing. If it becomes
1341 a nuisance we can relax things later. */
1342 if (start
+ size
< start
)
1344 gdbscm_out_of_range_error (FUNC_NAME
, 0,
1345 scm_list_2 (gdbscm_scm_from_ulongest (start
),
1346 gdbscm_scm_from_ulongest (size
)),
1347 _("start+size overflows"));
1352 end
= ~(CORE_ADDR
) 0;
1354 mode_bits
= ioscm_parse_mode_bits (FUNC_NAME
, mode
);
1356 /* Edge case: empty range -> unbuffered.
1357 There's no need to disallow empty ranges, but we need an unbuffered port
1358 to get the semantics right. */
1360 mode_bits
|= SCM_BUF0
;
1362 auto stream
= ioscm_init_memory_port_stream (start
, end
);
1363 port
= ioscm_open_port (memory_port_desc
, mode_bits
,
1364 (scm_t_bits
) stream
);
1366 #if USING_GUILE_BEFORE_2_2
1367 ioscm_init_memory_port_buffers (port
);
1372 /* TODO: Set the file name as "memory-start-end"? */
1376 /* Return non-zero if OBJ is a memory port. */
1379 gdbscm_is_memory_port (SCM obj
)
1381 #if USING_GUILE_BEFORE_2_2
1382 return !SCM_IMP (obj
) && (SCM_TYP16 (obj
) == memory_port_desc
);
1384 return SCM_PORTP (obj
) && (SCM_PORT_TYPE (obj
) == memory_port_desc
);
1388 /* (memory-port? obj) -> boolean */
1391 gdbscm_memory_port_p (SCM obj
)
1393 return scm_from_bool (gdbscm_is_memory_port (obj
));
1396 /* (memory-port-range port) -> (start end) */
1399 gdbscm_memory_port_range (SCM port
)
1401 ioscm_memory_port
*iomem
;
1403 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1404 memory_port_desc_name
);
1406 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1407 return scm_list_2 (gdbscm_scm_from_ulongest (iomem
->start
),
1408 gdbscm_scm_from_ulongest (iomem
->end
));
1411 /* (memory-port-read-buffer-size port) -> integer */
1414 gdbscm_memory_port_read_buffer_size (SCM port
)
1416 #if USING_GUILE_BEFORE_2_2
1417 ioscm_memory_port
*iomem
;
1419 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1420 memory_port_desc_name
);
1422 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1423 return scm_from_uint (iomem
->read_buf_size
);
1425 return scm_from_uint (0);
1429 /* (set-memory-port-read-buffer-size! port size) -> unspecified
1430 An exception is thrown if read data is still buffered or if the port
1434 gdbscm_set_memory_port_read_buffer_size_x (SCM port
, SCM size
)
1436 #if USING_GUILE_BEFORE_2_2
1437 ioscm_memory_port
*iomem
;
1439 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1440 memory_port_desc_name
);
1441 SCM_ASSERT_TYPE (scm_is_integer (size
), size
, SCM_ARG2
, FUNC_NAME
,
1444 if (!scm_is_unsigned_integer (size
, min_memory_port_buf_size
,
1445 max_memory_port_buf_size
))
1447 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, size
,
1448 out_of_range_buf_size
.get ());
1451 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1452 ioscm_reinit_memory_port (port
, scm_to_uint (size
), iomem
->write_buf_size
,
1455 return SCM_UNSPECIFIED
;
1457 return scm_setvbuf (port
, scm_from_utf8_symbol ("block"), size
);
1461 /* (memory-port-write-buffer-size port) -> integer */
1464 gdbscm_memory_port_write_buffer_size (SCM port
)
1466 #if USING_GUILE_BEFORE_2_2
1467 ioscm_memory_port
*iomem
;
1469 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1470 memory_port_desc_name
);
1472 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1473 return scm_from_uint (iomem
->write_buf_size
);
1475 return scm_from_uint (0);
1479 /* (set-memory-port-write-buffer-size! port size) -> unspecified
1480 An exception is thrown if write data is still buffered or if the port
1484 gdbscm_set_memory_port_write_buffer_size_x (SCM port
, SCM size
)
1486 #if USING_GUILE_BEFORE_2_2
1487 ioscm_memory_port
*iomem
;
1489 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1490 memory_port_desc_name
);
1491 SCM_ASSERT_TYPE (scm_is_integer (size
), size
, SCM_ARG2
, FUNC_NAME
,
1494 if (!scm_is_unsigned_integer (size
, min_memory_port_buf_size
,
1495 max_memory_port_buf_size
))
1497 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, size
,
1498 out_of_range_buf_size
.get ());
1501 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1502 ioscm_reinit_memory_port (port
, iomem
->read_buf_size
, scm_to_uint (size
),
1505 return SCM_UNSPECIFIED
;
1507 return scm_setvbuf (port
, scm_from_utf8_symbol ("block"), size
);
1511 /* Initialize gdb ports. */
1513 static const scheme_function port_functions
[] =
1515 { "input-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_input_port
),
1517 Return gdb's input port." },
1519 { "output-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_output_port
),
1521 Return gdb's output port." },
1523 { "error-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_error_port
),
1525 Return gdb's error port." },
1527 { "stdio-port?", 1, 0, 0, as_a_scm_t_subr (gdbscm_stdio_port_p
),
1529 Return #t if the object is a gdb:stdio-port." },
1531 { "open-memory", 0, 0, 1, as_a_scm_t_subr (gdbscm_open_memory
),
1533 Return a port that can be used for reading/writing inferior memory.\n\
1535 Arguments: [#:mode string] [#:start address] [#:size integer]\n\
1536 Returns: A port object." },
1538 { "memory-port?", 1, 0, 0, as_a_scm_t_subr (gdbscm_memory_port_p
),
1540 Return #t if the object is a memory port." },
1542 { "memory-port-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_memory_port_range
),
1544 Return the memory range of the port as (start end)." },
1546 { "memory-port-read-buffer-size", 1, 0, 0,
1547 as_a_scm_t_subr (gdbscm_memory_port_read_buffer_size
),
1549 Return the size of the read buffer for the memory port." },
1551 { "set-memory-port-read-buffer-size!", 2, 0, 0,
1552 as_a_scm_t_subr (gdbscm_set_memory_port_read_buffer_size_x
),
1554 Set the size of the read buffer for the memory port.\n\
1556 Arguments: port integer\n\
1557 Returns: unspecified." },
1559 { "memory-port-write-buffer-size", 1, 0, 0,
1560 as_a_scm_t_subr (gdbscm_memory_port_write_buffer_size
),
1562 Return the size of the write buffer for the memory port." },
1564 { "set-memory-port-write-buffer-size!", 2, 0, 0,
1565 as_a_scm_t_subr (gdbscm_set_memory_port_write_buffer_size_x
),
1567 Set the size of the write buffer for the memory port.\n\
1569 Arguments: port integer\n\
1570 Returns: unspecified." },
1575 static const scheme_function private_port_functions
[] =
1578 { "%with-gdb-input-from-port", 2, 0, 0,
1579 as_a_scm_t_subr (gdbscm_percent_with_gdb_input_from_port
),
1581 Temporarily set GDB's input port to PORT and then invoke THUNK.\n\
1583 Arguments: port thunk\n\
1584 Returns: The result of calling THUNK.\n\
1586 This procedure is experimental." },
1589 { "%with-gdb-output-to-port", 2, 0, 0,
1590 as_a_scm_t_subr (gdbscm_percent_with_gdb_output_to_port
),
1592 Temporarily set GDB's output port to PORT and then invoke THUNK.\n\
1594 Arguments: port thunk\n\
1595 Returns: The result of calling THUNK.\n\
1597 This procedure is experimental." },
1599 { "%with-gdb-error-to-port", 2, 0, 0,
1600 as_a_scm_t_subr (gdbscm_percent_with_gdb_error_to_port
),
1602 Temporarily set GDB's error port to PORT and then invoke THUNK.\n\
1604 Arguments: port thunk\n\
1605 Returns: The result of calling THUNK.\n\
1607 This procedure is experimental." },
1613 gdbscm_initialize_ports (void)
1615 /* Save the original stdio ports for debugging purposes. */
1617 orig_input_port_scm
= scm_current_input_port ();
1618 orig_output_port_scm
= scm_current_output_port ();
1619 orig_error_port_scm
= scm_current_error_port ();
1621 /* Set up the stdio ports. */
1623 ioscm_init_gdb_stdio_port ();
1624 input_port_scm
= ioscm_make_gdb_stdio_port (0);
1625 output_port_scm
= ioscm_make_gdb_stdio_port (1);
1626 error_port_scm
= ioscm_make_gdb_stdio_port (2);
1628 /* Set up memory ports. */
1630 ioscm_init_memory_port_type ();
1632 /* Install the accessor functions. */
1634 gdbscm_define_functions (port_functions
, 1);
1635 gdbscm_define_functions (private_port_functions
, 0);
1637 /* Keyword args for open-memory. */
1639 mode_keyword
= scm_from_latin1_keyword ("mode");
1640 start_keyword
= scm_from_latin1_keyword ("start");
1641 size_keyword
= scm_from_latin1_keyword ("size");
1643 #if USING_GUILE_BEFORE_2_2
1644 /* Error message text for "out of range" memory port buffer sizes. */
1646 out_of_range_buf_size
= xstrprintf ("size not between %u - %u",
1647 min_memory_port_buf_size
,
1648 max_memory_port_buf_size
);