1 /* Support for connecting Guile's stdio to GDB's.
2 as well as r/w memory via ports.
4 Copyright (C) 2014-2017 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 "gdb_select.h"
28 #include "guile-internal.h"
31 #if defined (HAVE_POLL_H)
33 #elif defined (HAVE_SYS_POLL_H)
38 /* A ui-file for sending output to Guile. */
40 class ioscm_file_port
: public ui_file
43 /* Return a ui_file that writes to PORT. */
44 explicit ioscm_file_port (SCM port
);
46 void flush () override
;
47 void write (const char *buf
, long length_buf
) override
;
53 /* Data for a memory port. */
57 /* Bounds of memory range this port is allowed to access: [start, end).
58 This means that 0xff..ff is not accessible. I can live with that. */
61 /* (end - start), recorded for convenience. */
64 /* Think of this as the lseek value maintained by the kernel.
65 This value is always in the range [0, size]. */
68 /* The size of the internal r/w buffers.
69 Scheme ports aren't a straightforward mapping to memory r/w.
70 Generally the user specifies how much to r/w and all access is
71 unbuffered. We don't try to provide equivalent access, but we allow
72 the user to specify these values to help get something similar. */
73 unsigned read_buf_size
, write_buf_size
;
76 /* Copies of the original system input/output/error ports.
77 These are recorded for debugging purposes. */
78 static SCM orig_input_port_scm
;
79 static SCM orig_output_port_scm
;
80 static SCM orig_error_port_scm
;
82 /* This is the stdio port descriptor, scm_ptob_descriptor. */
83 static scm_t_bits stdio_port_desc
;
85 /* Note: scm_make_port_type takes a char * instead of a const char *. */
86 static /*const*/ char stdio_port_desc_name
[] = "gdb:stdio-port";
88 /* Names of each gdb port. */
89 static const char input_port_name
[] = "gdb:stdin";
90 static const char output_port_name
[] = "gdb:stdout";
91 static const char error_port_name
[] = "gdb:stderr";
93 /* This is the actual port used from Guile.
94 We don't expose these to the user though, to ensure they're not
96 static SCM input_port_scm
;
97 static SCM output_port_scm
;
98 static SCM error_port_scm
;
100 /* Magic number to identify port ui-files.
101 Actually, the address of this variable is the magic number. */
102 static int file_port_magic
;
104 /* Internal enum for specifying output port. */
105 enum oport
{ GDB_STDOUT
, GDB_STDERR
};
107 /* This is the memory port descriptor, scm_ptob_descriptor. */
108 static scm_t_bits memory_port_desc
;
110 /* Note: scm_make_port_type takes a char * instead of a const char *. */
111 static /*const*/ char memory_port_desc_name
[] = "gdb:memory-port";
113 /* The default amount of memory to fetch for each read/write request.
114 Scheme ports don't provide a way to specify the size of a read,
115 which is important to us to minimize the number of inferior interactions,
116 which over a remote link can be important. To compensate we augment the
117 port API with a new function that let's the user specify how much the next
118 read request should fetch. This is the initial value for each new port. */
119 static const unsigned default_read_buf_size
= 16;
120 static const unsigned default_write_buf_size
= 16;
122 /* Arbitrarily limit memory port buffers to 1 byte to 4K. */
123 static const unsigned min_memory_port_buf_size
= 1;
124 static const unsigned max_memory_port_buf_size
= 4096;
126 /* "out of range" error message for buf sizes. */
127 static char *out_of_range_buf_size
;
129 /* Keywords used by open-memory. */
130 static SCM mode_keyword
;
131 static SCM start_keyword
;
132 static SCM size_keyword
;
134 /* Helper to do the low level work of opening a port.
135 Newer versions of Guile (2.1.x) have scm_c_make_port. */
138 ioscm_open_port (scm_t_bits port_type
, long mode_bits
)
142 #if 0 /* TODO: Guile doesn't export this. What to do? */
143 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex
);
146 port
= scm_new_port_table_entry (port_type
);
148 SCM_SET_CELL_TYPE (port
, port_type
| mode_bits
);
150 #if 0 /* TODO: Guile doesn't export this. What to do? */
151 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex
);
157 /* Support for connecting Guile's stdio ports to GDB's stdio ports. */
159 /* The scm_t_ptob_descriptor.input_waiting "method".
160 Return a lower bound on the number of bytes available for input. */
163 ioscm_input_waiting (SCM port
)
167 if (! scm_is_eq (port
, input_port_scm
))
172 /* This is copied from libguile/fports.c. */
173 struct pollfd pollfd
= { fdes
, POLLIN
, 0 };
174 static int use_poll
= -1;
178 /* This is copied from event-loop.c: poll cannot be used for stdin on
179 m68k-motorola-sysv. */
180 struct pollfd test_pollfd
= { fdes
, POLLIN
, 0 };
182 if (poll (&test_pollfd
, 1, 0) == 1 && (test_pollfd
.revents
& POLLNVAL
))
190 /* Guile doesn't export SIGINT hooks like Python does.
191 For now pass EINTR to scm_syserror, that's what fports.c does. */
192 if (poll (&pollfd
, 1, 0) < 0)
193 scm_syserror (FUNC_NAME
);
195 return pollfd
.revents
& POLLIN
? 1 : 0;
202 struct timeval timeout
;
204 int num_fds
= fdes
+ 1;
207 memset (&timeout
, 0, sizeof (timeout
));
208 FD_ZERO (&input_fds
);
209 FD_SET (fdes
, &input_fds
);
211 num_found
= interruptible_select (num_fds
,
212 &input_fds
, NULL
, NULL
,
216 /* Guile doesn't export SIGINT hooks like Python does.
217 For now pass EINTR to scm_syserror, that's what fports.c does. */
218 scm_syserror (FUNC_NAME
);
220 return num_found
> 0 && FD_ISSET (fdes
, &input_fds
);
224 /* The scm_t_ptob_descriptor.fill_input "method". */
227 ioscm_fill_input (SCM port
)
229 /* Borrowed from libguile/fports.c. */
231 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
233 /* If we're called on stdout,stderr, punt. */
234 if (! scm_is_eq (port
, input_port_scm
))
235 return (scm_t_wchar
) EOF
; /* Set errno and return -1? */
237 gdb_flush (gdb_stdout
);
238 gdb_flush (gdb_stderr
);
240 count
= ui_file_read (gdb_stdin
, (char *) pt
->read_buf
, pt
->read_buf_size
);
242 scm_syserror (FUNC_NAME
);
244 return (scm_t_wchar
) EOF
;
246 pt
->read_pos
= pt
->read_buf
;
247 pt
->read_end
= pt
->read_buf
+ count
;
248 return *pt
->read_buf
;
251 /* Like fputstrn_filtered, but don't escape characters, except nul.
252 Also like fputs_filtered, but a length is specified. */
255 fputsn_filtered (const char *s
, size_t size
, struct ui_file
*stream
)
259 for (i
= 0; i
< size
; ++i
)
262 fputs_filtered ("\\000", stream
);
264 fputc_filtered (s
[i
], stream
);
268 /* Write to gdb's stdout or stderr. */
271 ioscm_write (SCM port
, const void *data
, size_t size
)
274 /* If we're called on stdin, punt. */
275 if (scm_is_eq (port
, input_port_scm
))
280 if (scm_is_eq (port
, error_port_scm
))
281 fputsn_filtered ((const char *) data
, size
, gdb_stderr
);
283 fputsn_filtered ((const char *) data
, size
, gdb_stdout
);
285 CATCH (except
, RETURN_MASK_ALL
)
287 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
292 /* Flush gdb's stdout or stderr. */
295 ioscm_flush (SCM port
)
297 /* If we're called on stdin, punt. */
298 if (scm_is_eq (port
, input_port_scm
))
301 if (scm_is_eq (port
, error_port_scm
))
302 gdb_flush (gdb_stderr
);
304 gdb_flush (gdb_stdout
);
307 /* Initialize the gdb stdio port type.
309 N.B. isatty? will fail on these ports, it is only supported for file
310 ports. IWBN if we could "subclass" file ports. */
313 ioscm_init_gdb_stdio_port (void)
315 stdio_port_desc
= scm_make_port_type (stdio_port_desc_name
,
316 ioscm_fill_input
, ioscm_write
);
318 scm_set_port_input_waiting (stdio_port_desc
, ioscm_input_waiting
);
319 scm_set_port_flush (stdio_port_desc
, ioscm_flush
);
322 /* Subroutine of ioscm_make_gdb_stdio_port to simplify it.
323 Set up the buffers of port PORT.
324 MODE_BITS are the mode bits of PORT. */
327 ioscm_init_stdio_buffers (SCM port
, long mode_bits
)
329 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
330 #define GDB_STDIO_BUFFER_DEFAULT_SIZE 1024
331 int size
= mode_bits
& SCM_BUF0
? 0 : GDB_STDIO_BUFFER_DEFAULT_SIZE
;
332 int writing
= (mode_bits
& SCM_WRTNG
) != 0;
334 /* This is heavily copied from scm_fport_buffer_add. */
336 if (!writing
&& size
> 0)
339 = (unsigned char *) scm_gc_malloc_pointerless (size
, "port buffer");
340 pt
->read_pos
= pt
->read_end
= pt
->read_buf
;
341 pt
->read_buf_size
= size
;
345 pt
->read_pos
= pt
->read_buf
= pt
->read_end
= &pt
->shortbuf
;
346 pt
->read_buf_size
= 1;
349 if (writing
&& size
> 0)
352 = (unsigned char *) scm_gc_malloc_pointerless (size
, "port buffer");
353 pt
->write_pos
= pt
->write_buf
;
354 pt
->write_buf_size
= size
;
358 pt
->write_buf
= pt
->write_pos
= &pt
->shortbuf
;
359 pt
->write_buf_size
= 1;
361 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
364 /* Create a gdb stdio port. */
367 ioscm_make_gdb_stdio_port (int fd
)
369 int is_a_tty
= isatty (fd
);
371 const char *mode_str
;
378 name
= input_port_name
;
379 mode_str
= is_a_tty
? "r0" : "r";
382 name
= output_port_name
;
383 mode_str
= is_a_tty
? "w0" : "w";
386 name
= error_port_name
;
387 mode_str
= is_a_tty
? "w0" : "w";
390 gdb_assert_not_reached ("bad stdio file descriptor");
393 mode_bits
= scm_mode_bits ((char *) mode_str
);
394 port
= ioscm_open_port (stdio_port_desc
, mode_bits
);
396 scm_set_port_filename_x (port
, gdbscm_scm_from_c_string (name
));
398 ioscm_init_stdio_buffers (port
, mode_bits
);
403 /* (stdio-port? object) -> boolean */
406 gdbscm_stdio_port_p (SCM scm
)
408 /* This is copied from SCM_FPORTP. */
409 return scm_from_bool (!SCM_IMP (scm
)
410 && (SCM_TYP16 (scm
) == stdio_port_desc
));
413 /* GDB's ports are accessed via functions to keep them read-only. */
415 /* (input-port) -> port */
418 gdbscm_input_port (void)
420 return input_port_scm
;
423 /* (output-port) -> port */
426 gdbscm_output_port (void)
428 return output_port_scm
;
431 /* (error-port) -> port */
434 gdbscm_error_port (void)
436 return error_port_scm
;
439 /* Support for sending GDB I/O to Guile ports. */
441 ioscm_file_port::ioscm_file_port (SCM port
)
446 ioscm_file_port::flush ()
451 ioscm_file_port::write (const char *buffer
, long length_buffer
)
453 scm_c_write (m_port
, buffer
, length_buffer
);
457 /* Helper routine for with-{output,error}-to-port. */
460 ioscm_with_output_to_port_worker (SCM port
, SCM thunk
, enum oport oport
,
461 const char *func_name
)
463 struct cleanup
*cleanups
;
466 SCM_ASSERT_TYPE (gdbscm_is_true (scm_output_port_p (port
)), port
,
467 SCM_ARG1
, func_name
, _("output port"));
468 SCM_ASSERT_TYPE (gdbscm_is_true (scm_thunk_p (thunk
)), thunk
,
469 SCM_ARG2
, func_name
, _("thunk"));
471 cleanups
= set_batch_flag_and_make_cleanup_restore_page_info ();
473 make_cleanup_restore_integer (¤t_ui
->async
);
474 current_ui
->async
= 0;
476 ui_file_up
port_file (new ioscm_file_port (port
));
478 scoped_restore save_file
= make_scoped_restore (oport
== GDB_STDERR
479 ? &gdb_stderr
: &gdb_stdout
);
481 if (oport
== GDB_STDERR
)
482 gdb_stderr
= port_file
.get ();
485 current_uiout
->redirect (port_file
.get ());
486 make_cleanup_ui_out_redirect_pop (current_uiout
);
488 gdb_stdout
= port_file
.get ();
491 result
= gdbscm_safe_call_0 (thunk
, NULL
);
493 do_cleanups (cleanups
);
495 if (gdbscm_is_exception (result
))
496 gdbscm_throw (result
);
501 /* (%with-gdb-output-to-port port thunk) -> object
502 This function is experimental.
503 IWBN to not include "gdb" in the name, but it would collide with a standard
504 procedure, and it's common to import the gdb module without a prefix.
505 There are ways around this, but they're more cumbersome.
507 This has % in the name because it's experimental, and we want the
508 user-visible version to come from module (gdb experimental). */
511 gdbscm_percent_with_gdb_output_to_port (SCM port
, SCM thunk
)
513 return ioscm_with_output_to_port_worker (port
, thunk
, GDB_STDOUT
, FUNC_NAME
);
516 /* (%with-gdb-error-to-port port thunk) -> object
517 This function is experimental.
518 IWBN to not include "gdb" in the name, but it would collide with a standard
519 procedure, and it's common to import the gdb module without a prefix.
520 There are ways around this, but they're more cumbersome.
522 This has % in the name because it's experimental, and we want the
523 user-visible version to come from module (gdb experimental). */
526 gdbscm_percent_with_gdb_error_to_port (SCM port
, SCM thunk
)
528 return ioscm_with_output_to_port_worker (port
, thunk
, GDB_STDERR
, FUNC_NAME
);
531 /* Support for r/w memory via ports. */
533 /* Perform an "lseek" to OFFSET,WHENCE on memory port IOMEM.
534 OFFSET must be in the range [0,size].
535 The result is non-zero for success, zero for failure. */
538 ioscm_lseek_address (ioscm_memory_port
*iomem
, LONGEST offset
, int whence
)
540 CORE_ADDR new_current
;
542 gdb_assert (iomem
->current
<= iomem
->size
);
547 /* Catch over/underflow. */
548 if ((offset
< 0 && iomem
->current
+ offset
> iomem
->current
)
549 || (offset
> 0 && iomem
->current
+ offset
< iomem
->current
))
551 new_current
= iomem
->current
+ offset
;
554 new_current
= offset
;
559 new_current
= iomem
->size
;
562 /* TODO: Not supported yet. */
568 if (new_current
> iomem
->size
)
570 iomem
->current
= new_current
;
574 /* "fill_input" method for memory ports. */
577 gdbscm_memory_port_fill_input (SCM port
)
579 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
580 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
583 /* "current" is the offset of the first byte we want to read. */
584 gdb_assert (iomem
->current
<= iomem
->size
);
585 if (iomem
->current
== iomem
->size
)
588 /* Don't read outside the allowed memory range. */
589 to_read
= pt
->read_buf_size
;
590 if (to_read
> iomem
->size
- iomem
->current
)
591 to_read
= iomem
->size
- iomem
->current
;
593 if (target_read_memory (iomem
->start
+ iomem
->current
, pt
->read_buf
,
595 gdbscm_memory_error (FUNC_NAME
, _("error reading memory"), SCM_EOL
);
597 iomem
->current
+= to_read
;
598 pt
->read_pos
= pt
->read_buf
;
599 pt
->read_end
= pt
->read_buf
+ to_read
;
600 return *pt
->read_buf
;
603 /* "end_input" method for memory ports.
604 Clear the read buffer and adjust the file position for unread bytes. */
607 gdbscm_memory_port_end_input (SCM port
, int offset
)
609 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
610 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
611 size_t remaining
= pt
->read_end
- pt
->read_pos
;
613 /* Note: Use of "int offset" is specified by Guile ports API. */
614 if ((offset
< 0 && remaining
+ offset
> remaining
)
615 || (offset
> 0 && remaining
+ offset
< remaining
))
617 gdbscm_out_of_range_error (FUNC_NAME
, 0, scm_from_int (offset
),
618 _("overflow in offset calculation"));
624 pt
->read_pos
= pt
->read_end
;
625 /* Throw error if unread-char used at beginning of file
626 then attempting to write. Seems correct. */
627 if (!ioscm_lseek_address (iomem
, -offset
, SEEK_CUR
))
629 gdbscm_out_of_range_error (FUNC_NAME
, 0, scm_from_int (offset
),
634 pt
->rw_active
= SCM_PORT_NEITHER
;
637 /* "flush" method for memory ports. */
640 gdbscm_memory_port_flush (SCM port
)
642 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
643 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
644 size_t to_write
= pt
->write_pos
- pt
->write_buf
;
649 /* There's no way to indicate a short write, so if the request goes past
650 the end of the port's memory range, flag an error. */
651 if (to_write
> iomem
->size
- iomem
->current
)
653 gdbscm_out_of_range_error (FUNC_NAME
, 0,
654 gdbscm_scm_from_ulongest (to_write
),
655 _("writing beyond end of memory range"));
658 if (target_write_memory (iomem
->start
+ iomem
->current
, pt
->write_buf
,
660 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"), SCM_EOL
);
662 iomem
->current
+= to_write
;
663 pt
->write_pos
= pt
->write_buf
;
664 pt
->rw_active
= SCM_PORT_NEITHER
;
667 /* "write" method for memory ports. */
670 gdbscm_memory_port_write (SCM port
, const void *void_data
, size_t size
)
672 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
673 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
674 const gdb_byte
*data
= (const gdb_byte
*) void_data
;
676 /* There's no way to indicate a short write, so if the request goes past
677 the end of the port's memory range, flag an error. */
678 if (size
> iomem
->size
- iomem
->current
)
680 gdbscm_out_of_range_error (FUNC_NAME
, 0, gdbscm_scm_from_ulongest (size
),
681 _("writing beyond end of memory range"));
684 if (pt
->write_buf
== &pt
->shortbuf
)
686 /* Unbuffered port. */
687 if (target_write_memory (iomem
->start
+ iomem
->current
, data
, size
) != 0)
688 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"), SCM_EOL
);
689 iomem
->current
+= size
;
693 /* Note: The edge case of what to do when the buffer exactly fills is
694 debatable. Guile flushes when the buffer exactly fills up, so we
695 do too. It's counter-intuitive to my mind, but in case there's a
696 subtlety somewhere that depends on this, we do the same. */
699 size_t space
= pt
->write_end
- pt
->write_pos
;
703 /* Data fits in buffer, and does not fill it. */
704 memcpy (pt
->write_pos
, data
, size
);
705 pt
->write_pos
+= size
;
709 memcpy (pt
->write_pos
, data
, space
);
710 pt
->write_pos
= pt
->write_end
;
711 gdbscm_memory_port_flush (port
);
713 const gdb_byte
*ptr
= data
+ space
;
714 size_t remaining
= size
- space
;
716 if (remaining
>= pt
->write_buf_size
)
718 if (target_write_memory (iomem
->start
+ iomem
->current
, ptr
,
720 gdbscm_memory_error (FUNC_NAME
, _("error writing memory"),
722 iomem
->current
+= remaining
;
726 memcpy (pt
->write_pos
, ptr
, remaining
);
727 pt
->write_pos
+= remaining
;
734 /* "seek" method for memory ports. */
737 gdbscm_memory_port_seek (SCM port
, scm_t_off offset
, int whence
)
739 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
740 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
744 if (pt
->rw_active
== SCM_PORT_WRITE
)
746 if (offset
!= 0 || whence
!= SEEK_CUR
)
748 gdbscm_memory_port_flush (port
);
749 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
750 result
= iomem
->current
;
754 /* Read current position without disturbing the buffer,
755 but flag an error if what's in the buffer goes outside the
757 CORE_ADDR current
= iomem
->current
;
758 size_t delta
= pt
->write_pos
- pt
->write_buf
;
760 if (current
+ delta
< current
761 || current
+ delta
> iomem
->size
)
765 result
= current
+ delta
;
770 else if (pt
->rw_active
== SCM_PORT_READ
)
772 if (offset
!= 0 || whence
!= SEEK_CUR
)
774 scm_end_input (port
);
775 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
776 result
= iomem
->current
;
780 /* Read current position without disturbing the buffer
781 (particularly the unread-char buffer). */
782 CORE_ADDR current
= iomem
->current
;
783 size_t remaining
= pt
->read_end
- pt
->read_pos
;
785 if (current
- remaining
> current
786 || current
- remaining
< iomem
->start
)
790 result
= current
- remaining
;
794 if (rc
!= 0 && pt
->read_buf
== pt
->putback_buf
)
796 size_t saved_remaining
= pt
->saved_read_end
- pt
->saved_read_pos
;
798 if (result
- saved_remaining
> result
799 || result
- saved_remaining
< iomem
->start
)
802 result
-= saved_remaining
;
806 else /* SCM_PORT_NEITHER */
808 rc
= ioscm_lseek_address (iomem
, offset
, whence
);
809 result
= iomem
->current
;
814 gdbscm_out_of_range_error (FUNC_NAME
, 0,
815 gdbscm_scm_from_longest (offset
),
819 /* TODO: The Guile API doesn't support 32x64. We can't fix that here,
820 and there's no need to throw an error if the new address can't be
821 represented in a scm_t_off. But we could return something less
826 /* "close" method for memory ports. */
829 gdbscm_memory_port_close (SCM port
)
831 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
832 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
834 gdbscm_memory_port_flush (port
);
836 if (pt
->read_buf
== pt
->putback_buf
)
837 pt
->read_buf
= pt
->saved_read_buf
;
838 if (pt
->read_buf
!= &pt
->shortbuf
)
839 xfree (pt
->read_buf
);
840 if (pt
->write_buf
!= &pt
->shortbuf
)
841 xfree (pt
->write_buf
);
842 scm_gc_free (iomem
, sizeof (*iomem
), "memory port");
847 /* "free" method for memory ports. */
850 gdbscm_memory_port_free (SCM port
)
852 gdbscm_memory_port_close (port
);
857 /* "print" method for memory ports. */
860 gdbscm_memory_port_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
862 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (exp
);
863 char *type
= SCM_PTOBNAME (SCM_PTOBNUM (exp
));
865 scm_puts ("#<", port
);
866 scm_print_port_mode (exp
, port
);
867 /* scm_print_port_mode includes a trailing space. */
868 gdbscm_printf (port
, "%s %s-%s", type
,
869 hex_string (iomem
->start
), hex_string (iomem
->end
));
870 scm_putc ('>', port
);
874 /* Create the port type used for memory. */
877 ioscm_init_memory_port_type (void)
879 memory_port_desc
= scm_make_port_type (memory_port_desc_name
,
880 gdbscm_memory_port_fill_input
,
881 gdbscm_memory_port_write
);
883 scm_set_port_end_input (memory_port_desc
, gdbscm_memory_port_end_input
);
884 scm_set_port_flush (memory_port_desc
, gdbscm_memory_port_flush
);
885 scm_set_port_seek (memory_port_desc
, gdbscm_memory_port_seek
);
886 scm_set_port_close (memory_port_desc
, gdbscm_memory_port_close
);
887 scm_set_port_free (memory_port_desc
, gdbscm_memory_port_free
);
888 scm_set_port_print (memory_port_desc
, gdbscm_memory_port_print
);
891 /* Helper for gdbscm_open_memory to parse the mode bits.
892 An exception is thrown if MODE is invalid. */
895 ioscm_parse_mode_bits (const char *func_name
, const char *mode
)
900 if (*mode
!= 'r' && *mode
!= 'w')
902 gdbscm_out_of_range_error (func_name
, 0,
903 gdbscm_scm_from_c_string (mode
),
904 _("bad mode string"));
906 for (p
= mode
+ 1; *p
!= '\0'; ++p
)
915 gdbscm_out_of_range_error (func_name
, 0,
916 gdbscm_scm_from_c_string (mode
),
917 _("bad mode string"));
921 /* Kinda awkward to convert the mode from SCM -> string only to have Guile
922 convert it back to SCM, but that's the API we have to work with. */
923 mode_bits
= scm_mode_bits ((char *) mode
);
928 /* Helper for gdbscm_open_memory to finish initializing the port.
929 The port has address range [start,end).
930 This means that address of 0xff..ff is not accessible.
931 I can live with that. */
934 ioscm_init_memory_port (SCM port
, CORE_ADDR start
, CORE_ADDR end
)
937 ioscm_memory_port
*iomem
;
938 int buffered
= (SCM_CELL_WORD_0 (port
) & SCM_BUF0
) == 0;
940 gdb_assert (start
<= end
);
942 iomem
= (ioscm_memory_port
*) scm_gc_malloc_pointerless (sizeof (*iomem
),
945 iomem
->start
= start
;
947 iomem
->size
= end
- start
;
951 iomem
->read_buf_size
= default_read_buf_size
;
952 iomem
->write_buf_size
= default_write_buf_size
;
956 iomem
->read_buf_size
= 1;
957 iomem
->write_buf_size
= 1;
960 pt
= SCM_PTAB_ENTRY (port
);
961 /* Match the expectation of `binary-port?'. */
964 pt
->read_buf_size
= iomem
->read_buf_size
;
965 pt
->write_buf_size
= iomem
->write_buf_size
;
968 pt
->read_buf
= (unsigned char *) xmalloc (pt
->read_buf_size
);
969 pt
->write_buf
= (unsigned char *) xmalloc (pt
->write_buf_size
);
973 pt
->read_buf
= &pt
->shortbuf
;
974 pt
->write_buf
= &pt
->shortbuf
;
976 pt
->read_pos
= pt
->read_end
= pt
->read_buf
;
977 pt
->write_pos
= pt
->write_buf
;
978 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
980 SCM_SETSTREAM (port
, iomem
);
983 /* Re-initialize a memory port, updating its read/write buffer sizes.
984 An exception is thrown if the port is unbuffered.
985 TODO: Allow switching buffered/unbuffered.
986 An exception is also thrown if data is still buffered, except in the case
987 where the buffer size isn't changing (since that's just a nop). */
990 ioscm_reinit_memory_port (SCM port
, size_t read_buf_size
,
991 size_t write_buf_size
, const char *func_name
)
993 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
994 ioscm_memory_port
*iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
996 gdb_assert (read_buf_size
>= min_memory_port_buf_size
997 && read_buf_size
<= max_memory_port_buf_size
);
998 gdb_assert (write_buf_size
>= min_memory_port_buf_size
999 && write_buf_size
<= max_memory_port_buf_size
);
1001 /* First check if the port is unbuffered. */
1003 if (pt
->read_buf
== &pt
->shortbuf
)
1005 gdb_assert (pt
->write_buf
== &pt
->shortbuf
);
1006 scm_misc_error (func_name
, _("port is unbuffered: ~a"),
1010 /* Next check if anything is buffered. */
1012 if (read_buf_size
!= pt
->read_buf_size
1013 && pt
->read_end
!= pt
->read_buf
)
1015 scm_misc_error (func_name
, _("read buffer not empty: ~a"),
1019 if (write_buf_size
!= pt
->write_buf_size
1020 && pt
->write_pos
!= pt
->write_buf
)
1022 scm_misc_error (func_name
, _("write buffer not empty: ~a"),
1026 /* Now we can update the buffer sizes, but only if the size has changed. */
1028 if (read_buf_size
!= pt
->read_buf_size
)
1030 iomem
->read_buf_size
= read_buf_size
;
1031 pt
->read_buf_size
= read_buf_size
;
1032 xfree (pt
->read_buf
);
1033 pt
->read_buf
= (unsigned char *) xmalloc (pt
->read_buf_size
);
1034 pt
->read_pos
= pt
->read_end
= pt
->read_buf
;
1037 if (write_buf_size
!= pt
->write_buf_size
)
1039 iomem
->write_buf_size
= write_buf_size
;
1040 pt
->write_buf_size
= write_buf_size
;
1041 xfree (pt
->write_buf
);
1042 pt
->write_buf
= (unsigned char *) xmalloc (pt
->write_buf_size
);
1043 pt
->write_pos
= pt
->write_buf
;
1044 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
1048 /* (open-memory [#:mode string] [#:start address] [#:size integer]) -> port
1049 Return a port that can be used for reading and writing memory.
1050 MODE is a string, and must be one of "r", "w", or "r+".
1051 "0" may be appended to MODE to mark the port as unbuffered.
1052 For compatibility "b" (binary) may also be appended, but we ignore it:
1053 memory ports are binary only.
1055 The chunk of memory that can be accessed can be bounded.
1056 If both START,SIZE are unspecified, all of memory can be accessed
1057 (except 0xff..ff). If only START is specified, all of memory from that
1058 point on can be accessed (except 0xff..ff). If only SIZE if specified,
1059 all memory in [0,SIZE) can be accessed. If both are specified, all memory
1060 in [START,START+SIZE) can be accessed.
1062 Note: If it becomes useful enough we can later add #:end as an alternative
1063 to #:size. For now it is left out.
1065 The result is a Scheme port, and its semantics are a bit odd for accessing
1066 memory (e.g., unget), but we don't try to hide this. It's a port.
1068 N.B. Seeks on the port must be in the range [0,size].
1069 This is for similarity with bytevector ports, and so that one can seek
1070 to the first byte. */
1073 gdbscm_open_memory (SCM rest
)
1075 const SCM keywords
[] = {
1076 mode_keyword
, start_keyword
, size_keyword
, SCM_BOOL_F
1079 CORE_ADDR start
= 0;
1081 int mode_arg_pos
= -1, start_arg_pos
= -1, size_arg_pos
= -1;
1086 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, keywords
, "#sUU", rest
,
1087 &mode_arg_pos
, &mode
,
1088 &start_arg_pos
, &start
,
1089 &size_arg_pos
, &size
);
1091 scm_dynwind_begin ((scm_t_dynwind_flags
) 0);
1094 mode
= xstrdup ("r");
1095 scm_dynwind_free (mode
);
1097 if (size_arg_pos
> 0)
1099 /* For now be strict about start+size overflowing. If it becomes
1100 a nuisance we can relax things later. */
1101 if (start
+ size
< start
)
1103 gdbscm_out_of_range_error (FUNC_NAME
, 0,
1104 scm_list_2 (gdbscm_scm_from_ulongest (start
),
1105 gdbscm_scm_from_ulongest (size
)),
1106 _("start+size overflows"));
1111 end
= ~(CORE_ADDR
) 0;
1113 mode_bits
= ioscm_parse_mode_bits (FUNC_NAME
, mode
);
1115 port
= ioscm_open_port (memory_port_desc
, mode_bits
);
1117 ioscm_init_memory_port (port
, start
, end
);
1121 /* TODO: Set the file name as "memory-start-end"? */
1125 /* Return non-zero if OBJ is a memory port. */
1128 gdbscm_is_memory_port (SCM obj
)
1130 return !SCM_IMP (obj
) && (SCM_TYP16 (obj
) == memory_port_desc
);
1133 /* (memory-port? obj) -> boolean */
1136 gdbscm_memory_port_p (SCM obj
)
1138 return scm_from_bool (gdbscm_is_memory_port (obj
));
1141 /* (memory-port-range port) -> (start end) */
1144 gdbscm_memory_port_range (SCM port
)
1146 ioscm_memory_port
*iomem
;
1148 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1149 memory_port_desc_name
);
1151 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1152 return scm_list_2 (gdbscm_scm_from_ulongest (iomem
->start
),
1153 gdbscm_scm_from_ulongest (iomem
->end
));
1156 /* (memory-port-read-buffer-size port) -> integer */
1159 gdbscm_memory_port_read_buffer_size (SCM port
)
1161 ioscm_memory_port
*iomem
;
1163 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1164 memory_port_desc_name
);
1166 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1167 return scm_from_uint (iomem
->read_buf_size
);
1170 /* (set-memory-port-read-buffer-size! port size) -> unspecified
1171 An exception is thrown if read data is still buffered or if the port
1175 gdbscm_set_memory_port_read_buffer_size_x (SCM port
, SCM size
)
1177 ioscm_memory_port
*iomem
;
1179 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1180 memory_port_desc_name
);
1181 SCM_ASSERT_TYPE (scm_is_integer (size
), size
, SCM_ARG2
, FUNC_NAME
,
1184 if (!scm_is_unsigned_integer (size
, min_memory_port_buf_size
,
1185 max_memory_port_buf_size
))
1187 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, size
,
1188 out_of_range_buf_size
);
1191 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1192 ioscm_reinit_memory_port (port
, scm_to_uint (size
), iomem
->write_buf_size
,
1195 return SCM_UNSPECIFIED
;
1198 /* (memory-port-write-buffer-size port) -> integer */
1201 gdbscm_memory_port_write_buffer_size (SCM port
)
1203 ioscm_memory_port
*iomem
;
1205 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1206 memory_port_desc_name
);
1208 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1209 return scm_from_uint (iomem
->write_buf_size
);
1212 /* (set-memory-port-write-buffer-size! port size) -> unspecified
1213 An exception is thrown if write data is still buffered or if the port
1217 gdbscm_set_memory_port_write_buffer_size_x (SCM port
, SCM size
)
1219 ioscm_memory_port
*iomem
;
1221 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port
), port
, SCM_ARG1
, FUNC_NAME
,
1222 memory_port_desc_name
);
1223 SCM_ASSERT_TYPE (scm_is_integer (size
), size
, SCM_ARG2
, FUNC_NAME
,
1226 if (!scm_is_unsigned_integer (size
, min_memory_port_buf_size
,
1227 max_memory_port_buf_size
))
1229 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG2
, size
,
1230 out_of_range_buf_size
);
1233 iomem
= (ioscm_memory_port
*) SCM_STREAM (port
);
1234 ioscm_reinit_memory_port (port
, iomem
->read_buf_size
, scm_to_uint (size
),
1237 return SCM_UNSPECIFIED
;
1240 /* Initialize gdb ports. */
1242 static const scheme_function port_functions
[] =
1244 { "input-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_input_port
),
1246 Return gdb's input port." },
1248 { "output-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_output_port
),
1250 Return gdb's output port." },
1252 { "error-port", 0, 0, 0, as_a_scm_t_subr (gdbscm_error_port
),
1254 Return gdb's error port." },
1256 { "stdio-port?", 1, 0, 0, as_a_scm_t_subr (gdbscm_stdio_port_p
),
1258 Return #t if the object is a gdb:stdio-port." },
1260 { "open-memory", 0, 0, 1, as_a_scm_t_subr (gdbscm_open_memory
),
1262 Return a port that can be used for reading/writing inferior memory.\n\
1264 Arguments: [#:mode string] [#:start address] [#:size integer]\n\
1265 Returns: A port object." },
1267 { "memory-port?", 1, 0, 0, as_a_scm_t_subr (gdbscm_memory_port_p
),
1269 Return #t if the object is a memory port." },
1271 { "memory-port-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_memory_port_range
),
1273 Return the memory range of the port as (start end)." },
1275 { "memory-port-read-buffer-size", 1, 0, 0,
1276 as_a_scm_t_subr (gdbscm_memory_port_read_buffer_size
),
1278 Return the size of the read buffer for the memory port." },
1280 { "set-memory-port-read-buffer-size!", 2, 0, 0,
1281 as_a_scm_t_subr (gdbscm_set_memory_port_read_buffer_size_x
),
1283 Set the size of the read buffer for the memory port.\n\
1285 Arguments: port integer\n\
1286 Returns: unspecified." },
1288 { "memory-port-write-buffer-size", 1, 0, 0,
1289 as_a_scm_t_subr (gdbscm_memory_port_write_buffer_size
),
1291 Return the size of the write buffer for the memory port." },
1293 { "set-memory-port-write-buffer-size!", 2, 0, 0,
1294 as_a_scm_t_subr (gdbscm_set_memory_port_write_buffer_size_x
),
1296 Set the size of the write buffer for the memory port.\n\
1298 Arguments: port integer\n\
1299 Returns: unspecified." },
1304 static const scheme_function private_port_functions
[] =
1307 { "%with-gdb-input-from-port", 2, 0, 0,
1308 as_a_scm_t_subr (gdbscm_percent_with_gdb_input_from_port
),
1310 Temporarily set GDB's input port to PORT and then invoke THUNK.\n\
1312 Arguments: port thunk\n\
1313 Returns: The result of calling THUNK.\n\
1315 This procedure is experimental." },
1318 { "%with-gdb-output-to-port", 2, 0, 0,
1319 as_a_scm_t_subr (gdbscm_percent_with_gdb_output_to_port
),
1321 Temporarily set GDB's output port to PORT and then invoke THUNK.\n\
1323 Arguments: port thunk\n\
1324 Returns: The result of calling THUNK.\n\
1326 This procedure is experimental." },
1328 { "%with-gdb-error-to-port", 2, 0, 0,
1329 as_a_scm_t_subr (gdbscm_percent_with_gdb_error_to_port
),
1331 Temporarily set GDB's error port to PORT and then invoke THUNK.\n\
1333 Arguments: port thunk\n\
1334 Returns: The result of calling THUNK.\n\
1336 This procedure is experimental." },
1342 gdbscm_initialize_ports (void)
1344 /* Save the original stdio ports for debugging purposes. */
1346 orig_input_port_scm
= scm_current_input_port ();
1347 orig_output_port_scm
= scm_current_output_port ();
1348 orig_error_port_scm
= scm_current_error_port ();
1350 /* Set up the stdio ports. */
1352 ioscm_init_gdb_stdio_port ();
1353 input_port_scm
= ioscm_make_gdb_stdio_port (0);
1354 output_port_scm
= ioscm_make_gdb_stdio_port (1);
1355 error_port_scm
= ioscm_make_gdb_stdio_port (2);
1357 /* Set up memory ports. */
1359 ioscm_init_memory_port_type ();
1361 /* Install the accessor functions. */
1363 gdbscm_define_functions (port_functions
, 1);
1364 gdbscm_define_functions (private_port_functions
, 0);
1366 /* Keyword args for open-memory. */
1368 mode_keyword
= scm_from_latin1_keyword ("mode");
1369 start_keyword
= scm_from_latin1_keyword ("start");
1370 size_keyword
= scm_from_latin1_keyword ("size");
1372 /* Error message text for "out of range" memory port buffer sizes. */
1374 out_of_range_buf_size
= xstrprintf ("size not between %u - %u",
1375 min_memory_port_buf_size
,
1376 max_memory_port_buf_size
);