1 /* Support for connecting Guile's stdio to GDB's.
2 as well as r/w memory via ports.
4 Copyright (C) 2014-2023 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. */
25 #include "gdbsupport/gdb_select.h"
28 #include "guile-internal.h"
29 #include "gdbsupport/gdb_optional.h"
32 #if defined (HAVE_POLL_H)
34 #elif defined (HAVE_SYS_POLL_H)
39 /* Whether we're using Guile < 2.2 and its clumsy port API. */
41 #define USING_GUILE_BEFORE_2_2 \
42 (SCM_MAJOR_VERSION == 2 && SCM_MINOR_VERSION == 0)
45 /* A ui-file for sending output to Guile. */
47 class ioscm_file_port
: public ui_file
50 /* Return a ui_file that writes to PORT. */
51 explicit ioscm_file_port (SCM port
);
53 void flush () override
;
54 void write (const char *buf
, long length_buf
) override
;
60 /* Data for a memory port. */
62 struct ioscm_memory_port
64 /* Bounds of memory range this port is allowed to access: [start, end).
65 This means that 0xff..ff is not accessible. I can live with that. */
68 /* (end - start), recorded for convenience. */
71 /* Think of this as the lseek value maintained by the kernel.
72 This value is always in the range [0, size]. */
75 #if USING_GUILE_BEFORE_2_2
76 /* The size of the internal r/w buffers.
77 Scheme ports aren't a straightforward mapping to memory r/w.
78 Generally the user specifies how much to r/w and all access is
79 unbuffered. We don't try to provide equivalent access, but we allow
80 the user to specify these values to help get something similar. */
81 unsigned read_buf_size
, write_buf_size
;
85 /* Copies of the original system input/output/error ports.
86 These are recorded for debugging purposes. */
87 static SCM orig_input_port_scm
;
88 static SCM orig_output_port_scm
;
89 static SCM orig_error_port_scm
;
91 /* This is the stdio port descriptor, scm_ptob_descriptor. */
92 #if USING_GUILE_BEFORE_2_2
93 static scm_t_bits stdio_port_desc
;
95 static scm_t_port_type
*stdio_port_desc
;
98 /* Note: scm_make_port_type takes a char * instead of a const char *. */
99 static /*const*/ char stdio_port_desc_name
[] = "gdb:stdio-port";
101 /* Names of each gdb port. */
102 static const char input_port_name
[] = "gdb:stdin";
103 static const char output_port_name
[] = "gdb:stdout";
104 static const char error_port_name
[] = "gdb:stderr";
106 /* This is the actual port used from Guile.
107 We don't expose these to the user though, to ensure they're not
109 static SCM input_port_scm
;
110 static SCM output_port_scm
;
111 static SCM error_port_scm
;
113 /* Internal enum for specifying output port. */
114 enum oport
{ GDB_STDOUT
, GDB_STDERR
};
116 /* This is the memory port descriptor, scm_ptob_descriptor. */
117 #if USING_GUILE_BEFORE_2_2
118 static scm_t_bits memory_port_desc
;
120 static scm_t_port_type
*memory_port_desc
;
123 /* Note: scm_make_port_type takes a char * instead of a const char *. */
124 static /*const*/ char memory_port_desc_name
[] = "gdb:memory-port";
126 #if USING_GUILE_BEFORE_2_2
128 /* The default amount of memory to fetch for each read/write request.
129 Scheme ports don't provide a way to specify the size of a read,
130 which is important to us to minimize the number of inferior interactions,
131 which over a remote link can be important. To compensate we augment the
132 port API with a new function that let's the user specify how much the next
133 read request should fetch. This is the initial value for each new port. */
134 static const unsigned default_read_buf_size
= 16;
135 static const unsigned default_write_buf_size
= 16;
137 /* Arbitrarily limit memory port buffers to 1 byte to 4K. */
138 static const unsigned min_memory_port_buf_size
= 1;
139 static const unsigned max_memory_port_buf_size
= 4096;
141 /* "out of range" error message for buf sizes. */
142 static gdb::unique_xmalloc_ptr
<char> out_of_range_buf_size
;
146 /* The maximum values to use for get_natural_buffer_sizes. */
147 static const unsigned natural_buf_size
= 16;
151 /* Keywords used by open-memory. */
152 static SCM mode_keyword
;
153 static SCM start_keyword
;
154 static SCM size_keyword
;
156 /* Helper to do the low level work of opening a port. */
158 #if USING_GUILE_BEFORE_2_2
161 ioscm_open_port (scm_t_bits port_type
, long mode_bits
, scm_t_bits stream
)
165 #if 0 /* TODO: Guile doesn't export this. What to do? */
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
);
174 #if 0 /* TODO: Guile doesn't export this. What to do? */
175 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex
);
184 ioscm_open_port (scm_t_port_type
*port_type
, long mode_bits
, scm_t_bits stream
)
186 return scm_c_make_port (port_type
, mode_bits
, stream
);
192 /* Support for connecting Guile's stdio ports to GDB's stdio ports. */
194 /* Print a string S, length SIZE, but don't escape characters, except
198 fputsn_filtered (const char *s
, size_t size
, struct ui_file
*stream
)
202 for (i
= 0; i
< size
; ++i
)
205 gdb_puts ("\\000", stream
);
207 gdb_putc (s
[i
], stream
);
211 #if USING_GUILE_BEFORE_2_2
213 /* The scm_t_ptob_descriptor.input_waiting "method".
214 Return a lower bound on the number of bytes available for input. */
217 ioscm_input_waiting (SCM port
)
221 if (! scm_is_eq (port
, input_port_scm
))
226 /* This is copied from libguile/fports.c. */
227 struct pollfd pollfd
= { fdes
, POLLIN
, 0 };
228 static int use_poll
= -1;
232 /* This is copied from event-loop.c: poll cannot be used for stdin on
233 m68k-motorola-sysv. */
234 struct pollfd test_pollfd
= { fdes
, POLLIN
, 0 };
236 if (poll (&test_pollfd
, 1, 0) == 1 && (test_pollfd
.revents
& POLLNVAL
))
244 /* Guile doesn't export SIGINT hooks like Python does.
245 For now pass EINTR to scm_syserror, that's what fports.c does. */
246 if (poll (&pollfd
, 1, 0) < 0)
247 scm_syserror (FUNC_NAME
);
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
);
265 num_found
= interruptible_select (num_fds
,
266 &input_fds
, NULL
, NULL
,
270 /* Guile doesn't export SIGINT hooks like Python does.
271 For now pass EINTR to scm_syserror, that's what fports.c does. */
272 scm_syserror (FUNC_NAME
);
274 return num_found
> 0 && FD_ISSET (fdes
, &input_fds
);
278 /* The scm_t_ptob_descriptor.fill_input "method". */
281 ioscm_fill_input (SCM port
)
283 /* Borrowed from libguile/fports.c. */
285 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
287 /* If we're called on stdout,stderr, punt. */
288 if (! scm_is_eq (port
, input_port_scm
))
289 return (scm_t_wchar
) EOF
; /* Set errno and return -1? */
291 gdb_flush (gdb_stdout
);
292 gdb_flush (gdb_stderr
);
294 count
= gdb_stdin
->read ((char *) pt
->read_buf
, pt
->read_buf_size
);
296 scm_syserror (FUNC_NAME
);
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
;
305 /* Write to gdb's stdout or stderr. */
308 ioscm_write (SCM port
, const void *data
, size_t size
)
311 /* If we're called on stdin, punt. */
312 if (scm_is_eq (port
, input_port_scm
))
315 gdbscm_gdb_exception exc
{};
318 if (scm_is_eq (port
, error_port_scm
))
319 fputsn_filtered ((const char *) data
, size
, gdb_stderr
);
321 fputsn_filtered ((const char *) data
, size
, gdb_stdout
);
323 catch (const gdb_exception
&except
)
325 exc
= unpack (except
);
327 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
330 /* Flush gdb's stdout or stderr. */
333 ioscm_flush (SCM port
)
335 /* If we're called on stdin, punt. */
336 if (scm_is_eq (port
, input_port_scm
))
339 if (scm_is_eq (port
, error_port_scm
))
340 gdb_flush (gdb_stderr
);
342 gdb_flush (gdb_stdout
);
345 #else /* !USING_GUILE_BEFORE_2_2 */
347 /* Read up to COUNT bytes into bytevector DST at offset START. Return the
348 number of bytes read, zero for the end of file. */
351 ioscm_read_from_port (SCM port
, SCM dst
, size_t start
, size_t count
)
356 /* If we're called on stdout,stderr, punt. */
357 if (! scm_is_eq (port
, input_port_scm
))
360 gdb_flush (gdb_stdout
);
361 gdb_flush (gdb_stderr
);
363 read_buf
= (char *) SCM_BYTEVECTOR_CONTENTS (dst
) + start
;
364 read
= gdb_stdin
->read (read_buf
, count
);
366 scm_syserror (FUNC_NAME
);
368 return (size_t) read
;
371 /* Write to gdb's stdout or stderr. */
374 ioscm_write (SCM port
, SCM src
, size_t start
, size_t count
)
376 const char *data
= (char *) SCM_BYTEVECTOR_CONTENTS (src
) + start
;
378 /* If we're called on stdin, punt. */
379 if (scm_is_eq (port
, input_port_scm
))
382 gdbscm_gdb_exception exc
{};
385 if (scm_is_eq (port
, error_port_scm
))
386 fputsn_filtered ((const char *) data
, count
, gdb_stderr
);
388 fputsn_filtered ((const char *) data
, count
, gdb_stdout
);
390 catch (const gdb_exception
&except
)
392 exc
= unpack (except
);
394 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
399 #endif /* !USING_GUILE_BEFORE_2_2 */
401 /* Initialize the gdb stdio port type.
403 N.B. isatty? will fail on these ports, it is only supported for file
404 ports. IWBN if we could "subclass" file ports. */
407 ioscm_init_gdb_stdio_port (void)
409 stdio_port_desc
= scm_make_port_type (stdio_port_desc_name
,
410 #if USING_GUILE_BEFORE_2_2
413 ioscm_read_from_port
,
417 #if USING_GUILE_BEFORE_2_2
418 scm_set_port_input_waiting (stdio_port_desc
, ioscm_input_waiting
);
419 scm_set_port_flush (stdio_port_desc
, ioscm_flush
);
421 scm_set_port_read_wait_fd (stdio_port_desc
, STDIN_FILENO
);
425 #define GDB_STDIO_BUFFER_DEFAULT_SIZE 1024
427 #if USING_GUILE_BEFORE_2_2
429 /* Subroutine of ioscm_make_gdb_stdio_port to simplify it.
430 Set up the buffers of port PORT.
431 MODE_BITS are the mode bits of PORT. */
434 ioscm_init_stdio_buffers (SCM port
, long mode_bits
)
436 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
437 int size
= mode_bits
& SCM_BUF0
? 0 : GDB_STDIO_BUFFER_DEFAULT_SIZE
;
438 int writing
= (mode_bits
& SCM_WRTNG
) != 0;
440 /* This is heavily copied from scm_fport_buffer_add. */
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
;
473 ioscm_init_stdio_buffers (SCM port
, long mode_bits
)
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"),
479 scm_from_size_t (GDB_STDIO_BUFFER_DEFAULT_SIZE
));
484 /* Create a gdb stdio port. */
487 ioscm_make_gdb_stdio_port (int fd
)
489 int is_a_tty
= isatty (fd
);
491 const char *mode_str
;
498 name
= input_port_name
;
499 mode_str
= is_a_tty
? "r0" : "r";
502 name
= output_port_name
;
503 mode_str
= is_a_tty
? "w0" : "w";
506 name
= error_port_name
;
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
);
514 port
= ioscm_open_port (stdio_port_desc
, mode_bits
, 0);
516 scm_set_port_filename_x (port
, gdbscm_scm_from_c_string (name
));
518 ioscm_init_stdio_buffers (port
, mode_bits
);
523 /* (stdio-port? object) -> boolean */
526 gdbscm_stdio_port_p (SCM scm
)
528 #if USING_GUILE_BEFORE_2_2
529 /* This is copied from SCM_FPORTP. */
530 return scm_from_bool (!SCM_IMP (scm
)
531 && (SCM_TYP16 (scm
) == stdio_port_desc
));
533 return scm_from_bool (SCM_PORTP (scm
)
534 && (SCM_PORT_TYPE (scm
) == stdio_port_desc
));
538 /* GDB's ports are accessed via functions to keep them read-only. */
540 /* (input-port) -> port */
543 gdbscm_input_port (void)
545 return input_port_scm
;
548 /* (output-port) -> port */
551 gdbscm_output_port (void)
553 return output_port_scm
;
556 /* (error-port) -> port */
559 gdbscm_error_port (void)
561 return error_port_scm
;
564 /* Support for sending GDB I/O to Guile ports. */
566 ioscm_file_port::ioscm_file_port (SCM port
)
571 ioscm_file_port::flush ()
576 ioscm_file_port::write (const char *buffer
, long length_buffer
)
578 scm_c_write (m_port
, buffer
, length_buffer
);
582 /* Helper routine for with-{output,error}-to-port. */
585 ioscm_with_output_to_port_worker (SCM port
, SCM thunk
, enum oport oport
,
586 const char *func_name
)
590 SCM_ASSERT_TYPE (gdbscm_is_true (scm_output_port_p (port
)), port
,
591 SCM_ARG1
, func_name
, _("output port"));
592 SCM_ASSERT_TYPE (gdbscm_is_true (scm_thunk_p (thunk
)), thunk
,
593 SCM_ARG2
, func_name
, _("thunk"));
595 set_batch_flag_and_restore_page_info save_page_info
;
597 scoped_restore restore_async
= make_scoped_restore (¤t_ui
->async
, 0);
599 ui_file_up
port_file (new ioscm_file_port (port
));
601 scoped_restore save_file
= make_scoped_restore (oport
== GDB_STDERR
602 ? &gdb_stderr
: &gdb_stdout
);
605 gdb::optional
<ui_out_redirect_pop
> redirect_popper
;
606 if (oport
== GDB_STDERR
)
607 gdb_stderr
= port_file
.get ();
610 redirect_popper
.emplace (current_uiout
, port_file
.get ());
612 gdb_stdout
= port_file
.get ();
615 result
= gdbscm_safe_call_0 (thunk
, NULL
);
618 if (gdbscm_is_exception (result
))
619 gdbscm_throw (result
);
624 /* (%with-gdb-output-to-port port thunk) -> object
625 This function is experimental.
626 IWBN to not include "gdb" in the name, but it would collide with a standard
627 procedure, and it's common to import the gdb module without a prefix.
628 There are ways around this, but they're more cumbersome.
630 This has % in the name because it's experimental, and we want the
631 user-visible version to come from module (gdb experimental). */
634 gdbscm_percent_with_gdb_output_to_port (SCM port
, SCM thunk
)
636 return ioscm_with_output_to_port_worker (port
, thunk
, GDB_STDOUT
, FUNC_NAME
);
639 /* (%with-gdb-error-to-port port thunk) -> object
640 This function is experimental.
641 IWBN to not include "gdb" in the name, but it would collide with a standard
642 procedure, and it's common to import the gdb module without a prefix.
643 There are ways around this, but they're more cumbersome.
645 This has % in the name because it's experimental, and we want the
646 user-visible version to come from module (gdb experimental). */
649 gdbscm_percent_with_gdb_error_to_port (SCM port
, SCM thunk
)
651 return ioscm_with_output_to_port_worker (port
, thunk
, GDB_STDERR
, FUNC_NAME
);
654 /* Support for r/w memory via ports. */
656 /* Perform an "lseek" to OFFSET,WHENCE on memory port IOMEM.
657 OFFSET must be in the range [0,size].
658 The result is non-zero for success, zero for failure. */
661 ioscm_lseek_address (ioscm_memory_port
*iomem
, LONGEST offset
, int whence
)
663 CORE_ADDR new_current
;
665 gdb_assert (iomem
->current
<= iomem
->size
);
670 /* Catch over/underflow. */
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
;
685 /* TODO: Not supported yet. */
691 if (new_current
> iomem
->size
)
693 iomem
->current
= new_current
;
697 #if USING_GUILE_BEFORE_2_2
699 /* "fill_input" method for memory ports. */
702 gdbscm_memory_port_fill_input (SCM port
)
704 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
705 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
708 /* "current" is the offset of the first byte we want to read. */
709 gdb_assert (iomem
->current
<= iomem
->size
);
710 if (iomem
->current
== iomem
->size
)
713 /* Don't read outside the allowed memory range. */
714 to_read
= pt
->read_buf_size
;
715 if (to_read
> iomem
->size
- iomem
->current
)
716 to_read
= iomem
->size
- iomem
->current
;
718 if (target_read_memory (iomem
->start
+ iomem
->current
, pt
->read_buf
,
720 gdbscm_memory_error (FUNC_NAME
, _("error reading memory"), SCM_EOL
);
722 iomem
->current
+= to_read
;
723 pt
->read_pos
= pt
->read_buf
;
724 pt
->read_end
= pt
->read_buf
+ to_read
;
725 return *pt
->read_buf
;
728 /* "end_input" method for memory ports.
729 Clear the read buffer and adjust the file position for unread bytes. */
732 gdbscm_memory_port_end_input (SCM port
, int offset
)
734 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
735 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
736 size_t remaining
= pt
->read_end
- pt
->read_pos
;
738 /* Note: Use of "int offset" is specified by Guile ports API. */
739 if ((offset
< 0 && remaining
+ offset
> remaining
)
740 || (offset
> 0 && remaining
+ offset
< remaining
))
742 gdbscm_out_of_range_error (FUNC_NAME
, 0, scm_from_int (offset
),
743 _("overflow in offset calculation"));
749 pt
->read_pos
= pt
->read_end
;
750 /* Throw error if unread-char used at beginning of file
751 then attempting to write. Seems correct. */
752 if (!ioscm_lseek_address (iomem
, -offset
, SEEK_CUR
))
754 gdbscm_out_of_range_error (FUNC_NAME
, 0, scm_from_int (offset
),
759 pt
->rw_active
= SCM_PORT_NEITHER
;
762 /* "flush" method for memory ports. */
765 gdbscm_memory_port_flush (SCM port
)
767 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
768 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
769 size_t to_write
= pt
->write_pos
- pt
->write_buf
;
774 /* There's no way to indicate a short write, so if the request goes past
775 the end of the port's memory range, flag an error. */
776 if (to_write
> iomem
->size
- iomem
->current
)
778 gdbscm_out_of_range_error (FUNC_NAME
, 0,
779 gdbscm_scm_from_ulongest (to_write
),
780 _("writing beyond end of memory range"));
783 if (target_write_memory (iomem
->start
+ iomem
->current
, pt
->write_buf
,
785 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"), SCM_EOL
);
787 iomem
->current
+= to_write
;
788 pt
->write_pos
= pt
->write_buf
;
789 pt
->rw_active
= SCM_PORT_NEITHER
;
792 /* "seek" method for memory ports. */
795 gdbscm_memory_port_seek (SCM port
, scm_t_off offset
, int whence
)
797 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
798 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
802 if (pt
->rw_active
== SCM_PORT_WRITE
)
804 if (offset
!= 0 || whence
!= SEEK_CUR
)
806 gdbscm_memory_port_flush (port
);
807 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
808 result
= iomem
->current
;
812 /* Read current position without disturbing the buffer,
813 but flag an error if what's in the buffer goes outside the
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
);
833 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
834 result
= iomem
->current
;
838 /* Read current position without disturbing the buffer
839 (particularly the unread-char buffer). */
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
;
864 else /* SCM_PORT_NEITHER */
866 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
867 result
= iomem
->current
;
872 gdbscm_out_of_range_error (FUNC_NAME
, 0,
873 gdbscm_scm_from_longest (offset
),
877 /* TODO: The Guile API doesn't support 32x64. We can't fix that here,
878 and there's no need to throw an error if the new address can't be
879 represented in a scm_t_off. But we could return something less
884 /* "write" method for memory ports. */
887 gdbscm_memory_port_write (SCM port
, const void *void_data
, size_t size
)
889 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
890 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
891 const gdb_byte
*data
= (const gdb_byte
*) void_data
;
893 /* There's no way to indicate a short write, so if the request goes past
894 the end of the port's memory range, flag an error. */
895 if (size
> iomem
->size
- iomem
->current
)
897 gdbscm_out_of_range_error (FUNC_NAME
, 0, gdbscm_scm_from_ulongest (size
),
898 _("writing beyond end of memory range"));
901 if (pt
->write_buf
== &pt
->shortbuf
)
903 /* Unbuffered port. */
904 if (target_write_memory (iomem
->start
+ iomem
->current
, data
, size
) != 0)
905 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"), SCM_EOL
);
906 iomem
->current
+= size
;
910 /* Note: The edge case of what to do when the buffer exactly fills is
911 debatable. Guile flushes when the buffer exactly fills up, so we
912 do too. It's counter-intuitive to my mind, but in case there's a
913 subtlety somewhere that depends on this, we do the same. */
916 size_t space
= pt
->write_end
- pt
->write_pos
;
920 /* Data fits in buffer, and does not fill it. */
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
)
935 if (target_write_memory (iomem
->start
+ iomem
->current
, ptr
,
937 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"),
939 iomem
->current
+= remaining
;
943 memcpy (pt
->write_pos
, ptr
, remaining
);
944 pt
->write_pos
+= remaining
;
951 /* "close" method for memory ports. */
954 gdbscm_memory_port_close (SCM port
)
956 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
957 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (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");
972 /* "free" method for memory ports. */
975 gdbscm_memory_port_free (SCM port
)
977 gdbscm_memory_port_close (port
);
982 /* Re-initialize a memory port, updating its read/write buffer sizes.
983 An exception is thrown if the port is unbuffered.
984 TODO: Allow switching buffered/unbuffered.
985 An exception is also thrown if data is still buffered, except in the case
986 where the buffer size isn't changing (since that's just a nop). */
989 ioscm_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
);
993 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (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
);
1000 /* First check if the port is unbuffered. */
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"),
1009 /* Next check if anything is buffered. */
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"),
1025 /* Now we can update the buffer sizes, but only if the size has changed. */
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
;
1047 #else /* !USING_GUILE_BEFORE_2_2 */
1049 /* The semantics get weird if the buffer size is larger than the port range,
1050 so provide a better default buffer size. */
1053 gdbscm_get_natural_buffer_sizes (SCM port
, size_t *read_size
,
1056 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1058 size_t size
= natural_buf_size
;
1059 if (iomem
!= NULL
&& iomem
->size
< size
)
1061 *read_size
= *write_size
= size
;
1064 /* Read up to COUNT bytes into bytevector DST at offset START. Return the
1065 number of bytes read, zero for the end of file. */
1068 gdbscm_memory_port_read (SCM port
, SCM dst
, size_t start
, size_t count
)
1071 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1073 /* "current" is the offset of the first byte we want to read. */
1074 gdb_assert (iomem
->current
<= iomem
->size
);
1075 if (iomem
->current
== iomem
->size
)
1078 /* Don't read outside the allowed memory range. */
1079 if (count
> iomem
->size
- iomem
->current
)
1080 count
= iomem
->size
- iomem
->current
;
1082 read_buf
= (gdb_byte
*) SCM_BYTEVECTOR_CONTENTS (dst
) + start
;
1083 if (target_read_memory (iomem
->start
+ iomem
->current
, read_buf
,
1085 gdbscm_memory_error (FUNC_NAME
, _("error reading memory"), SCM_EOL
);
1087 iomem
->current
+= count
;
1092 gdbscm_memory_port_write (SCM port
, SCM src
, size_t start
, size_t count
)
1094 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1095 const gdb_byte
*data
=
1096 (const gdb_byte
*) SCM_BYTEVECTOR_CONTENTS (src
) + start
;
1098 /* If the request goes past the end of the port's memory range, flag an
1100 if (count
> iomem
->size
- iomem
->current
)
1101 gdbscm_out_of_range_error (FUNC_NAME
, 0, scm_from_size_t (count
),
1102 _("writing beyond end of memory range"));
1104 if (target_write_memory (iomem
->start
+ iomem
->current
, data
,
1106 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"),
1109 iomem
->current
+= count
;
1115 gdbscm_memory_port_seek (SCM port
, scm_t_off offset
, int whence
)
1117 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1120 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
1122 gdbscm_out_of_range_error (FUNC_NAME
, 0,
1123 gdbscm_scm_from_longest (offset
),
1126 /* TODO: The Guile API doesn't support 32x64. We can't fix that here,
1127 and there's no need to throw an error if the new address can't be
1128 represented in a scm_t_off. But we could return something less
1130 return iomem
->current
;
1134 gdbscm_memory_port_close (SCM port
)
1136 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1137 scm_gc_free (iomem
, sizeof (*iomem
), "memory port");
1138 SCM_SETSTREAM (port
, NULL
);
1141 #endif /* !USING_GUILE_BEFORE_2_2 */
1143 /* "print" method for memory ports. */
1146 gdbscm_memory_port_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
1148 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (exp
);
1150 scm_puts ("#<", port
);
1151 scm_print_port_mode (exp
, port
);
1152 /* scm_print_port_mode includes a trailing space. */
1153 gdbscm_printf (port
, "%s %s-%s", memory_port_desc_name
,
1154 hex_string (iomem
->start
), hex_string (iomem
->end
));
1155 scm_putc ('>', port
);
1159 /* Create the port type used for memory. */
1162 ioscm_init_memory_port_type (void)
1164 memory_port_desc
= scm_make_port_type (memory_port_desc_name
,
1165 #if USING_GUILE_BEFORE_2_2
1166 gdbscm_memory_port_fill_input
,
1168 gdbscm_memory_port_read
,
1170 gdbscm_memory_port_write
);
1172 #if USING_GUILE_BEFORE_2_2
1173 scm_set_port_end_input (memory_port_desc
, gdbscm_memory_port_end_input
);
1174 scm_set_port_flush (memory_port_desc
, gdbscm_memory_port_flush
);
1175 scm_set_port_free (memory_port_desc
, gdbscm_memory_port_free
);
1177 scm_set_port_get_natural_buffer_sizes (memory_port_desc
,
1178 gdbscm_get_natural_buffer_sizes
);
1180 scm_set_port_seek (memory_port_desc
, gdbscm_memory_port_seek
);
1181 scm_set_port_close (memory_port_desc
, gdbscm_memory_port_close
);
1182 scm_set_port_print (memory_port_desc
, gdbscm_memory_port_print
);
1185 /* Helper for gdbscm_open_memory to parse the mode bits.
1186 An exception is thrown if MODE is invalid. */
1189 ioscm_parse_mode_bits (const char *func_name
, const char *mode
)
1194 if (*mode
!= 'r' && *mode
!= 'w')
1196 gdbscm_out_of_range_error (func_name
, 0,
1197 gdbscm_scm_from_c_string (mode
),
1198 _("bad mode string"));
1200 for (p
= mode
+ 1; *p
!= '\0'; ++p
)
1209 gdbscm_out_of_range_error (func_name
, 0,
1210 gdbscm_scm_from_c_string (mode
),
1211 _("bad mode string"));
1215 /* Kinda awkward to convert the mode from SCM -> string only to have Guile
1216 convert it back to SCM, but that's the API we have to work with. */
1217 mode_bits
= scm_mode_bits ((char *) mode
);
1222 /* Return the memory object to be used as a "stream" associated with a memory
1223 port for the START--END range. */
1225 static ioscm_memory_port
*
1226 ioscm_init_memory_port_stream (CORE_ADDR start
, CORE_ADDR end
)
1228 ioscm_memory_port
*iomem
;
1230 gdb_assert (start
<= end
);
1232 iomem
= (ioscm_memory_port
*) scm_gc_malloc_pointerless (sizeof (*iomem
),
1235 iomem
->start
= start
;
1237 iomem
->size
= end
- start
;
1243 #if USING_GUILE_BEFORE_2_2
1245 /* Helper for gdbscm_open_memory to finish initializing the port.
1246 The port has address range [start,end).
1247 This means that address of 0xff..ff is not accessible.
1248 I can live with that. */
1251 ioscm_init_memory_port_buffers (SCM port
)
1253 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (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
);
1268 /* Match the expectation of `binary-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
;
1290 /* (open-memory [#:mode string] [#:start address] [#:size integer]) -> port
1291 Return a port that can be used for reading and writing memory.
1292 MODE is a string, and must be one of "r", "w", or "r+".
1293 "0" may be appended to MODE to mark the port as unbuffered.
1294 For compatibility "b" (binary) may also be appended, but we ignore it:
1295 memory ports are binary only.
1297 The chunk of memory that can be accessed can be bounded.
1298 If both START,SIZE are unspecified, all of memory can be accessed
1299 (except 0xff..ff). If only START is specified, all of memory from that
1300 point on can be accessed (except 0xff..ff). If only SIZE if specified,
1301 all memory in [0,SIZE) can be accessed. If both are specified, all memory
1302 in [START,START+SIZE) can be accessed.
1304 Note: If it becomes useful enough we can later add #:end as an alternative
1305 to #:size. For now it is left out.
1307 The result is a Scheme port, and its semantics are a bit odd for accessing
1308 memory (e.g., unget), but we don't try to hide this. It's a port.
1310 N.B. Seeks on the port must be in the range [0,size].
1311 This is for similarity with bytevector ports, and so that one can seek
1312 to the first byte. */
1315 gdbscm_open_memory (SCM rest
)
1317 const SCM keywords
[] = {
1318 mode_keyword
, start_keyword
, size_keyword
, SCM_BOOL_F
1321 CORE_ADDR start
= 0;
1323 int mode_arg_pos
= -1, start_arg_pos
= -1, size_arg_pos
= -1;
1328 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, keywords
, "#sUU", rest
,
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)
1341 /* For now be strict about start+size overflowing. If it becomes
1342 a nuisance we can relax things later. */
1343 if (start
+ size
< start
)
1345 gdbscm_out_of_range_error (FUNC_NAME
, 0,
1346 scm_list_2 (gdbscm_scm_from_ulongest (start
),
1347 gdbscm_scm_from_ulongest (size
)),
1348 _("start+size overflows"));
1353 end
= ~(CORE_ADDR
) 0;
1355 mode_bits
= ioscm_parse_mode_bits (FUNC_NAME
, mode
);
1357 /* Edge case: empty range -> unbuffered.
1358 There's no need to disallow empty ranges, but we need an unbuffered port
1359 to get the semantics right. */
1361 mode_bits
|= SCM_BUF0
;
1363 auto stream
= ioscm_init_memory_port_stream (start
, end
);
1364 port
= ioscm_open_port (memory_port_desc
, mode_bits
,
1365 (scm_t_bits
) stream
);
1367 #if USING_GUILE_BEFORE_2_2
1368 ioscm_init_memory_port_buffers (port
);
1373 /* TODO: Set the file name as "memory-start-end"? */
1377 /* Return non-zero if OBJ is a memory port. */
1380 gdbscm_is_memory_port (SCM obj
)
1382 #if USING_GUILE_BEFORE_2_2
1383 return !SCM_IMP (obj
) && (SCM_TYP16 (obj
) == memory_port_desc
);
1385 return SCM_PORTP (obj
) && (SCM_PORT_TYPE (obj
) == memory_port_desc
);
1389 /* (memory-port? obj) -> boolean */
1392 gdbscm_memory_port_p (SCM obj
)
1394 return scm_from_bool (gdbscm_is_memory_port (obj
));
1397 /* (memory-port-range port) -> (start end) */
1400 gdbscm_memory_port_range (SCM port
)
1402 ioscm_memory_port
*iomem
;
1404 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1405 memory_port_desc_name
);
1407 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1408 return scm_list_2 (gdbscm_scm_from_ulongest (iomem
->start
),
1409 gdbscm_scm_from_ulongest (iomem
->end
));
1412 /* (memory-port-read-buffer-size port) -> integer */
1415 gdbscm_memory_port_read_buffer_size (SCM port
)
1417 #if USING_GUILE_BEFORE_2_2
1418 ioscm_memory_port
*iomem
;
1420 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1421 memory_port_desc_name
);
1423 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1424 return scm_from_uint (iomem
->read_buf_size
);
1426 return scm_from_uint (0);
1430 /* (set-memory-port-read-buffer-size! port size) -> unspecified
1431 An exception is thrown if read data is still buffered or if the port
1435 gdbscm_set_memory_port_read_buffer_size_x (SCM port
, SCM size
)
1437 #if USING_GUILE_BEFORE_2_2
1438 ioscm_memory_port
*iomem
;
1440 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1441 memory_port_desc_name
);
1442 SCM_ASSERT_TYPE (scm_is_integer (size
), size
, SCM_ARG2
, FUNC_NAME
,
1445 if (!scm_is_unsigned_integer (size
, min_memory_port_buf_size
,
1446 max_memory_port_buf_size
))
1448 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, size
,
1449 out_of_range_buf_size
.get ());
1452 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
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
);
1462 /* (memory-port-write-buffer-size port) -> integer */
1465 gdbscm_memory_port_write_buffer_size (SCM port
)
1467 #if USING_GUILE_BEFORE_2_2
1468 ioscm_memory_port
*iomem
;
1470 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1471 memory_port_desc_name
);
1473 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1474 return scm_from_uint (iomem
->write_buf_size
);
1476 return scm_from_uint (0);
1480 /* (set-memory-port-write-buffer-size! port size) -> unspecified
1481 An exception is thrown if write data is still buffered or if the port
1485 gdbscm_set_memory_port_write_buffer_size_x (SCM port
, SCM size
)
1487 #if USING_GUILE_BEFORE_2_2
1488 ioscm_memory_port
*iomem
;
1490 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1491 memory_port_desc_name
);
1492 SCM_ASSERT_TYPE (scm_is_integer (size
), size
, SCM_ARG2
, FUNC_NAME
,
1495 if (!scm_is_unsigned_integer (size
, min_memory_port_buf_size
,
1496 max_memory_port_buf_size
))
1498 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, size
,
1499 out_of_range_buf_size
.get ());
1502 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
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
);
1512 /* Initialize gdb ports. */
1514 static const scheme_function port_functions
[] =
1516 { "input-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_input_port
),
1518 Return gdb's input port." },
1520 { "output-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_output_port
),
1522 Return gdb's output port." },
1524 { "error-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_error_port
),
1526 Return gdb's error port." },
1528 { "stdio-port?", 1, 0, 0, as_a_scm_t_subr (gdbscm_stdio_port_p
),
1530 Return #t if the object is a gdb:stdio-port." },
1532 { "open-memory", 0, 0, 1, as_a_scm_t_subr (gdbscm_open_memory
),
1534 Return 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." },
1539 { "memory-port?", 1, 0, 0, as_a_scm_t_subr (gdbscm_memory_port_p
),
1541 Return #t if the object is a memory port." },
1543 { "memory-port-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_memory_port_range
),
1545 Return the memory range of the port as (start end)." },
1547 { "memory-port-read-buffer-size", 1, 0, 0,
1548 as_a_scm_t_subr (gdbscm_memory_port_read_buffer_size
),
1550 Return the size of the read buffer for the memory port." },
1552 { "set-memory-port-read-buffer-size!", 2, 0, 0,
1553 as_a_scm_t_subr (gdbscm_set_memory_port_read_buffer_size_x
),
1555 Set 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,
1561 as_a_scm_t_subr (gdbscm_memory_port_write_buffer_size
),
1563 Return the size of the write buffer for the memory port." },
1565 { "set-memory-port-write-buffer-size!", 2, 0, 0,
1566 as_a_scm_t_subr (gdbscm_set_memory_port_write_buffer_size_x
),
1568 Set the size of the write buffer for the memory port.\n\
1570 Arguments: port integer\n\
1571 Returns: unspecified." },
1576 static const scheme_function private_port_functions
[] =
1579 { "%with-gdb-input-from-port", 2, 0, 0,
1580 as_a_scm_t_subr (gdbscm_percent_with_gdb_input_from_port
),
1582 Temporarily 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\
1587 This procedure is experimental." },
1590 { "%with-gdb-output-to-port", 2, 0, 0,
1591 as_a_scm_t_subr (gdbscm_percent_with_gdb_output_to_port
),
1593 Temporarily 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\
1598 This procedure is experimental." },
1600 { "%with-gdb-error-to-port", 2, 0, 0,
1601 as_a_scm_t_subr (gdbscm_percent_with_gdb_error_to_port
),
1603 Temporarily 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\
1608 This procedure is experimental." },
1614 gdbscm_initialize_ports (void)
1616 /* Save the original stdio ports for debugging purposes. */
1618 orig_input_port_scm
= scm_current_input_port ();
1619 orig_output_port_scm
= scm_current_output_port ();
1620 orig_error_port_scm
= scm_current_error_port ();
1622 /* Set up the stdio ports. */
1624 ioscm_init_gdb_stdio_port ();
1625 input_port_scm
= ioscm_make_gdb_stdio_port (0);
1626 output_port_scm
= ioscm_make_gdb_stdio_port (1);
1627 error_port_scm
= ioscm_make_gdb_stdio_port (2);
1629 /* Set up memory ports. */
1631 ioscm_init_memory_port_type ();
1633 /* Install the accessor functions. */
1635 gdbscm_define_functions (port_functions
, 1);
1636 gdbscm_define_functions (private_port_functions
, 0);
1638 /* Keyword args for open-memory. */
1640 mode_keyword
= scm_from_latin1_keyword ("mode");
1641 start_keyword
= scm_from_latin1_keyword ("start");
1642 size_keyword
= scm_from_latin1_keyword ("size");
1644 #if USING_GUILE_BEFORE_2_2
1645 /* Error message text for "out of range" memory port buffer sizes. */
1647 out_of_range_buf_size
= xstrprintf ("size not between %u - %u",
1648 min_memory_port_buf_size
,
1649 max_memory_port_buf_size
);