Configure zlib with --enable-host-shared for shared bfd
[deliverable/binutils-gdb.git] / gdb / guile / scm-ports.c
1 /* Support for connecting Guile's stdio to GDB's.
2 as well as r/w memory via ports.
3
4 Copyright (C) 2014-2015 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 /* See README file in this directory for implementation notes, coding
22 conventions, et.al. */
23
24 #include "defs.h"
25 #include "gdb_select.h"
26 #include "interps.h"
27 #include "target.h"
28 #include "guile-internal.h"
29
30 #ifdef HAVE_POLL
31 #if defined (HAVE_POLL_H)
32 #include <poll.h>
33 #elif defined (HAVE_SYS_POLL_H)
34 #include <sys/poll.h>
35 #endif
36 #endif
37
38 /* A ui-file for sending output to Guile. */
39
40 typedef struct
41 {
42 int *magic;
43 SCM port;
44 } ioscm_file_port;
45
46 /* Data for a memory port. */
47
48 typedef struct
49 {
50 /* Bounds of memory range this port is allowed to access, inclusive.
51 To simplify overflow handling, an END of 0xff..ff is not allowed.
52 This also means a start address of 0xff..ff is also not allowed.
53 I can live with that. */
54 CORE_ADDR start, end;
55
56 /* (end - start + 1), recorded for convenience. */
57 ULONGEST size;
58
59 /* Think of this as the lseek value maintained by the kernel.
60 This value is always in the range [0, size]. */
61 ULONGEST current;
62
63 /* The size of the internal r/w buffers.
64 Scheme ports aren't a straightforward mapping to memory r/w.
65 Generally the user specifies how much to r/w and all access is
66 unbuffered. We don't try to provide equivalent access, but we allow
67 the user to specify these values to help get something similar. */
68 unsigned read_buf_size, write_buf_size;
69 } ioscm_memory_port;
70
71 /* Copies of the original system input/output/error ports.
72 These are recorded for debugging purposes. */
73 static SCM orig_input_port_scm;
74 static SCM orig_output_port_scm;
75 static SCM orig_error_port_scm;
76
77 /* This is the stdio port descriptor, scm_ptob_descriptor. */
78 static scm_t_bits stdio_port_desc;
79
80 /* Note: scm_make_port_type takes a char * instead of a const char *. */
81 static /*const*/ char stdio_port_desc_name[] = "gdb:stdio-port";
82
83 /* Names of each gdb port. */
84 static const char input_port_name[] = "gdb:stdin";
85 static const char output_port_name[] = "gdb:stdout";
86 static const char error_port_name[] = "gdb:stderr";
87
88 /* This is the actual port used from Guile.
89 We don't expose these to the user though, to ensure they're not
90 overwritten. */
91 static SCM input_port_scm;
92 static SCM output_port_scm;
93 static SCM error_port_scm;
94
95 /* Magic number to identify port ui-files.
96 Actually, the address of this variable is the magic number. */
97 static int file_port_magic;
98
99 /* Internal enum for specifying output port. */
100 enum oport { GDB_STDOUT, GDB_STDERR };
101
102 /* This is the memory port descriptor, scm_ptob_descriptor. */
103 static scm_t_bits memory_port_desc;
104
105 /* Note: scm_make_port_type takes a char * instead of a const char *. */
106 static /*const*/ char memory_port_desc_name[] = "gdb:memory-port";
107
108 /* The default amount of memory to fetch for each read/write request.
109 Scheme ports don't provide a way to specify the size of a read,
110 which is important to us to minimize the number of inferior interactions,
111 which over a remote link can be important. To compensate we augment the
112 port API with a new function that let's the user specify how much the next
113 read request should fetch. This is the initial value for each new port. */
114 static const unsigned default_read_buf_size = 16;
115 static const unsigned default_write_buf_size = 16;
116
117 /* Arbitrarily limit memory port buffers to 1 byte to 4K. */
118 static const unsigned min_memory_port_buf_size = 1;
119 static const unsigned max_memory_port_buf_size = 4096;
120
121 /* "out of range" error message for buf sizes. */
122 static char *out_of_range_buf_size;
123
124 /* Keywords used by open-memory. */
125 static SCM mode_keyword;
126 static SCM start_keyword;
127 static SCM size_keyword;
128 \f
129 /* Helper to do the low level work of opening a port.
130 Newer versions of Guile (2.1.x) have scm_c_make_port. */
131
132 static SCM
133 ioscm_open_port (scm_t_bits port_type, long mode_bits)
134 {
135 SCM port;
136
137 #if 0 /* TODO: Guile doesn't export this. What to do? */
138 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
139 #endif
140
141 port = scm_new_port_table_entry (port_type);
142
143 SCM_SET_CELL_TYPE (port, port_type | mode_bits);
144
145 #if 0 /* TODO: Guile doesn't export this. What to do? */
146 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
147 #endif
148
149 return port;
150 }
151 \f
152 /* Support for connecting Guile's stdio ports to GDB's stdio ports. */
153
154 /* The scm_t_ptob_descriptor.input_waiting "method".
155 Return a lower bound on the number of bytes available for input. */
156
157 static int
158 ioscm_input_waiting (SCM port)
159 {
160 int fdes = 0;
161
162 if (! scm_is_eq (port, input_port_scm))
163 return 0;
164
165 #ifdef HAVE_POLL
166 {
167 /* This is copied from libguile/fports.c. */
168 struct pollfd pollfd = { fdes, POLLIN, 0 };
169 static int use_poll = -1;
170
171 if (use_poll < 0)
172 {
173 /* This is copied from event-loop.c: poll cannot be used for stdin on
174 m68k-motorola-sysv. */
175 struct pollfd test_pollfd = { fdes, POLLIN, 0 };
176
177 if (poll (&test_pollfd, 1, 0) == 1 && (test_pollfd.revents & POLLNVAL))
178 use_poll = 0;
179 else
180 use_poll = 1;
181 }
182
183 if (use_poll)
184 {
185 /* Guile doesn't export SIGINT hooks like Python does.
186 For now pass EINTR to scm_syserror, that's what fports.c does. */
187 if (poll (&pollfd, 1, 0) < 0)
188 scm_syserror (FUNC_NAME);
189
190 return pollfd.revents & POLLIN ? 1 : 0;
191 }
192 }
193 /* Fall through. */
194 #endif
195
196 {
197 struct timeval timeout;
198 fd_set input_fds;
199 int num_fds = fdes + 1;
200 int num_found;
201
202 memset (&timeout, 0, sizeof (timeout));
203 FD_ZERO (&input_fds);
204 FD_SET (fdes, &input_fds);
205
206 num_found = gdb_select (num_fds, &input_fds, NULL, NULL, &timeout);
207 if (num_found < 0)
208 {
209 /* Guile doesn't export SIGINT hooks like Python does.
210 For now pass EINTR to scm_syserror, that's what fports.c does. */
211 scm_syserror (FUNC_NAME);
212 }
213 return num_found > 0 && FD_ISSET (fdes, &input_fds);
214 }
215 }
216
217 /* The scm_t_ptob_descriptor.fill_input "method". */
218
219 static int
220 ioscm_fill_input (SCM port)
221 {
222 /* Borrowed from libguile/fports.c. */
223 long count;
224 scm_t_port *pt = SCM_PTAB_ENTRY (port);
225
226 /* If we're called on stdout,stderr, punt. */
227 if (! scm_is_eq (port, input_port_scm))
228 return (scm_t_wchar) EOF; /* Set errno and return -1? */
229
230 gdb_flush (gdb_stdout);
231 gdb_flush (gdb_stderr);
232
233 count = ui_file_read (gdb_stdin, (char *) pt->read_buf, pt->read_buf_size);
234 if (count == -1)
235 scm_syserror (FUNC_NAME);
236 if (count == 0)
237 return (scm_t_wchar) EOF;
238
239 pt->read_pos = pt->read_buf;
240 pt->read_end = pt->read_buf + count;
241 return *pt->read_buf;
242 }
243
244 /* Like fputstrn_filtered, but don't escape characters, except nul.
245 Also like fputs_filtered, but a length is specified. */
246
247 static void
248 fputsn_filtered (const char *s, size_t size, struct ui_file *stream)
249 {
250 size_t i;
251
252 for (i = 0; i < size; ++i)
253 {
254 if (s[i] == '\0')
255 fputs_filtered ("\\000", stream);
256 else
257 fputc_filtered (s[i], stream);
258 }
259 }
260
261 /* Write to gdb's stdout or stderr. */
262
263 static void
264 ioscm_write (SCM port, const void *data, size_t size)
265 {
266
267 /* If we're called on stdin, punt. */
268 if (scm_is_eq (port, input_port_scm))
269 return;
270
271 TRY
272 {
273 if (scm_is_eq (port, error_port_scm))
274 fputsn_filtered (data, size, gdb_stderr);
275 else
276 fputsn_filtered (data, size, gdb_stdout);
277 }
278 CATCH (except, RETURN_MASK_ALL)
279 {
280 GDBSCM_HANDLE_GDB_EXCEPTION (except);
281 }
282 END_CATCH
283 }
284
285 /* Flush gdb's stdout or stderr. */
286
287 static void
288 ioscm_flush (SCM port)
289 {
290 /* If we're called on stdin, punt. */
291 if (scm_is_eq (port, input_port_scm))
292 return;
293
294 if (scm_is_eq (port, error_port_scm))
295 gdb_flush (gdb_stderr);
296 else
297 gdb_flush (gdb_stdout);
298 }
299
300 /* Initialize the gdb stdio port type.
301
302 N.B. isatty? will fail on these ports, it is only supported for file
303 ports. IWBN if we could "subclass" file ports. */
304
305 static void
306 ioscm_init_gdb_stdio_port (void)
307 {
308 stdio_port_desc = scm_make_port_type (stdio_port_desc_name,
309 ioscm_fill_input, ioscm_write);
310
311 scm_set_port_input_waiting (stdio_port_desc, ioscm_input_waiting);
312 scm_set_port_flush (stdio_port_desc, ioscm_flush);
313 }
314
315 /* Subroutine of ioscm_make_gdb_stdio_port to simplify it.
316 Set up the buffers of port PORT.
317 MODE_BITS are the mode bits of PORT. */
318
319 static void
320 ioscm_init_stdio_buffers (SCM port, long mode_bits)
321 {
322 scm_t_port *pt = SCM_PTAB_ENTRY (port);
323 #define GDB_STDIO_BUFFER_DEFAULT_SIZE 1024
324 int size = mode_bits & SCM_BUF0 ? 0 : GDB_STDIO_BUFFER_DEFAULT_SIZE;
325 int writing = (mode_bits & SCM_WRTNG) != 0;
326
327 /* This is heavily copied from scm_fport_buffer_add. */
328
329 if (!writing && size > 0)
330 {
331 pt->read_buf = scm_gc_malloc_pointerless (size, "port buffer");
332 pt->read_pos = pt->read_end = pt->read_buf;
333 pt->read_buf_size = size;
334 }
335 else
336 {
337 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
338 pt->read_buf_size = 1;
339 }
340
341 if (writing && size > 0)
342 {
343 pt->write_buf = scm_gc_malloc_pointerless (size, "port buffer");
344 pt->write_pos = pt->write_buf;
345 pt->write_buf_size = size;
346 }
347 else
348 {
349 pt->write_buf = pt->write_pos = &pt->shortbuf;
350 pt->write_buf_size = 1;
351 }
352 pt->write_end = pt->write_buf + pt->write_buf_size;
353 }
354
355 /* Create a gdb stdio port. */
356
357 static SCM
358 ioscm_make_gdb_stdio_port (int fd)
359 {
360 int is_a_tty = isatty (fd);
361 const char *name;
362 long mode_bits;
363 SCM port;
364
365 switch (fd)
366 {
367 case 0:
368 name = input_port_name;
369 mode_bits = scm_mode_bits (is_a_tty ? "r0" : "r");
370 break;
371 case 1:
372 name = output_port_name;
373 mode_bits = scm_mode_bits (is_a_tty ? "w0" : "w");
374 break;
375 case 2:
376 name = error_port_name;
377 mode_bits = scm_mode_bits (is_a_tty ? "w0" : "w");
378 break;
379 default:
380 gdb_assert_not_reached ("bad stdio file descriptor");
381 }
382
383 port = ioscm_open_port (stdio_port_desc, mode_bits);
384
385 scm_set_port_filename_x (port, gdbscm_scm_from_c_string (name));
386
387 ioscm_init_stdio_buffers (port, mode_bits);
388
389 return port;
390 }
391
392 /* (stdio-port? object) -> boolean */
393
394 static SCM
395 gdbscm_stdio_port_p (SCM scm)
396 {
397 /* This is copied from SCM_FPORTP. */
398 return scm_from_bool (!SCM_IMP (scm)
399 && (SCM_TYP16 (scm) == stdio_port_desc));
400 }
401 \f
402 /* GDB's ports are accessed via functions to keep them read-only. */
403
404 /* (input-port) -> port */
405
406 static SCM
407 gdbscm_input_port (void)
408 {
409 return input_port_scm;
410 }
411
412 /* (output-port) -> port */
413
414 static SCM
415 gdbscm_output_port (void)
416 {
417 return output_port_scm;
418 }
419
420 /* (error-port) -> port */
421
422 static SCM
423 gdbscm_error_port (void)
424 {
425 return error_port_scm;
426 }
427 \f
428 /* Support for sending GDB I/O to Guile ports. */
429
430 static void
431 ioscm_file_port_delete (struct ui_file *file)
432 {
433 ioscm_file_port *stream = ui_file_data (file);
434
435 if (stream->magic != &file_port_magic)
436 internal_error (__FILE__, __LINE__,
437 _("ioscm_file_port_delete: bad magic number"));
438 xfree (stream);
439 }
440
441 static void
442 ioscm_file_port_rewind (struct ui_file *file)
443 {
444 ioscm_file_port *stream = ui_file_data (file);
445
446 if (stream->magic != &file_port_magic)
447 internal_error (__FILE__, __LINE__,
448 _("ioscm_file_port_rewind: bad magic number"));
449
450 scm_truncate_file (stream->port, 0);
451 }
452
453 static void
454 ioscm_file_port_put (struct ui_file *file,
455 ui_file_put_method_ftype *write,
456 void *dest)
457 {
458 ioscm_file_port *stream = ui_file_data (file);
459
460 if (stream->magic != &file_port_magic)
461 internal_error (__FILE__, __LINE__,
462 _("ioscm_file_port_put: bad magic number"));
463
464 /* This function doesn't meld with ports very well. */
465 }
466
467 static void
468 ioscm_file_port_write (struct ui_file *file,
469 const char *buffer,
470 long length_buffer)
471 {
472 ioscm_file_port *stream = ui_file_data (file);
473
474 if (stream->magic != &file_port_magic)
475 internal_error (__FILE__, __LINE__,
476 _("ioscm_pot_file_write: bad magic number"));
477
478 scm_c_write (stream->port, buffer, length_buffer);
479 }
480
481 /* Return a ui_file that writes to PORT. */
482
483 static struct ui_file *
484 ioscm_file_port_new (SCM port)
485 {
486 ioscm_file_port *stream = XCNEW (ioscm_file_port);
487 struct ui_file *file = ui_file_new ();
488
489 set_ui_file_data (file, stream, ioscm_file_port_delete);
490 set_ui_file_rewind (file, ioscm_file_port_rewind);
491 set_ui_file_put (file, ioscm_file_port_put);
492 set_ui_file_write (file, ioscm_file_port_write);
493 stream->magic = &file_port_magic;
494 stream->port = port;
495
496 return file;
497 }
498 \f
499 /* Helper routine for with-{output,error}-to-port. */
500
501 static SCM
502 ioscm_with_output_to_port_worker (SCM port, SCM thunk, enum oport oport,
503 const char *func_name)
504 {
505 struct ui_file *port_file;
506 struct cleanup *cleanups;
507 SCM result;
508
509 SCM_ASSERT_TYPE (gdbscm_is_true (scm_output_port_p (port)), port,
510 SCM_ARG1, func_name, _("output port"));
511 SCM_ASSERT_TYPE (gdbscm_is_true (scm_thunk_p (thunk)), thunk,
512 SCM_ARG2, func_name, _("thunk"));
513
514 cleanups = set_batch_flag_and_make_cleanup_restore_page_info ();
515
516 make_cleanup_restore_integer (&interpreter_async);
517 interpreter_async = 0;
518
519 port_file = ioscm_file_port_new (port);
520
521 make_cleanup_ui_file_delete (port_file);
522
523 if (oport == GDB_STDERR)
524 {
525 make_cleanup_restore_ui_file (&gdb_stderr);
526 gdb_stderr = port_file;
527 }
528 else
529 {
530 make_cleanup_restore_ui_file (&gdb_stdout);
531
532 if (ui_out_redirect (current_uiout, port_file) < 0)
533 warning (_("Current output protocol does not support redirection"));
534 else
535 make_cleanup_ui_out_redirect_pop (current_uiout);
536
537 gdb_stdout = port_file;
538 }
539
540 result = gdbscm_safe_call_0 (thunk, NULL);
541
542 do_cleanups (cleanups);
543
544 if (gdbscm_is_exception (result))
545 gdbscm_throw (result);
546
547 return result;
548 }
549
550 /* (%with-gdb-output-to-port port thunk) -> object
551 This function is experimental.
552 IWBN to not include "gdb" in the name, but it would collide with a standard
553 procedure, and it's common to import the gdb module without a prefix.
554 There are ways around this, but they're more cumbersome.
555
556 This has % in the name because it's experimental, and we want the
557 user-visible version to come from module (gdb experimental). */
558
559 static SCM
560 gdbscm_percent_with_gdb_output_to_port (SCM port, SCM thunk)
561 {
562 return ioscm_with_output_to_port_worker (port, thunk, GDB_STDOUT, FUNC_NAME);
563 }
564
565 /* (%with-gdb-error-to-port port thunk) -> object
566 This function is experimental.
567 IWBN to not include "gdb" in the name, but it would collide with a standard
568 procedure, and it's common to import the gdb module without a prefix.
569 There are ways around this, but they're more cumbersome.
570
571 This has % in the name because it's experimental, and we want the
572 user-visible version to come from module (gdb experimental). */
573
574 static SCM
575 gdbscm_percent_with_gdb_error_to_port (SCM port, SCM thunk)
576 {
577 return ioscm_with_output_to_port_worker (port, thunk, GDB_STDERR, FUNC_NAME);
578 }
579 \f
580 /* Support for r/w memory via ports. */
581
582 /* Perform an "lseek" to OFFSET,WHENCE on memory port IOMEM.
583 OFFSET must be in the range [0,size].
584 The result is non-zero for success, zero for failure. */
585
586 static int
587 ioscm_lseek_address (ioscm_memory_port *iomem, LONGEST offset, int whence)
588 {
589 CORE_ADDR new_current;
590
591 gdb_assert (iomem->current <= iomem->size);
592
593 switch (whence)
594 {
595 case SEEK_CUR:
596 /* Catch over/underflow. */
597 if ((offset < 0 && iomem->current + offset > iomem->current)
598 || (offset >= 0 && iomem->current + offset < iomem->current))
599 return 0;
600 new_current = iomem->current + offset;
601 break;
602 case SEEK_SET:
603 new_current = offset;
604 break;
605 case SEEK_END:
606 if (offset == 0)
607 {
608 new_current = iomem->size;
609 break;
610 }
611 /* TODO: Not supported yet. */
612 return 0;
613 default:
614 return 0;
615 }
616
617 if (new_current > iomem->size)
618 return 0;
619 iomem->current = new_current;
620 return 1;
621 }
622
623 /* "fill_input" method for memory ports. */
624
625 static int
626 gdbscm_memory_port_fill_input (SCM port)
627 {
628 scm_t_port *pt = SCM_PTAB_ENTRY (port);
629 ioscm_memory_port *iomem = (ioscm_memory_port *) SCM_STREAM (port);
630 size_t to_read;
631
632 /* "current" is the offset of the first byte we want to read. */
633 if (iomem->current >= iomem->size)
634 return EOF;
635
636 /* Don't read outside the allowed memory range. */
637 to_read = pt->read_buf_size;
638 if (to_read > iomem->size - iomem->current)
639 to_read = iomem->size - iomem->current;
640
641 if (target_read_memory (iomem->start + iomem->current, pt->read_buf,
642 to_read) != 0)
643 gdbscm_memory_error (FUNC_NAME, _("error reading memory"), SCM_EOL);
644
645 pt->read_pos = pt->read_buf;
646 pt->read_end = pt->read_buf + to_read;
647 iomem->current += to_read;
648 return *pt->read_buf;
649 }
650
651 /* "end_input" method for memory ports.
652 Clear the read buffer and adjust the file position for unread bytes. */
653
654 static void
655 gdbscm_memory_port_end_input (SCM port, int offset)
656 {
657 scm_t_port *pt = SCM_PTAB_ENTRY (port);
658 ioscm_memory_port *iomem = (ioscm_memory_port *) SCM_STREAM (port);
659 size_t remaining = pt->read_end - pt->read_pos;
660
661 /* Note: Use of "int offset" is specified by Guile ports API. */
662 if ((offset < 0 && remaining + offset > remaining)
663 || (offset > 0 && remaining + offset < remaining))
664 {
665 gdbscm_out_of_range_error (FUNC_NAME, 0, scm_from_int (offset),
666 _("overflow in offset calculation"));
667 }
668 offset += remaining;
669
670 if (offset > 0)
671 {
672 pt->read_pos = pt->read_end;
673 /* Throw error if unread-char used at beginning of file
674 then attempting to write. Seems correct. */
675 if (!ioscm_lseek_address (iomem, -offset, SEEK_CUR))
676 {
677 gdbscm_out_of_range_error (FUNC_NAME, 0, scm_from_int (offset),
678 _("bad offset"));
679 }
680 }
681
682 pt->rw_active = SCM_PORT_NEITHER;
683 }
684
685 /* "flush" method for memory ports. */
686
687 static void
688 gdbscm_memory_port_flush (SCM port)
689 {
690 scm_t_port *pt = SCM_PTAB_ENTRY (port);
691 ioscm_memory_port *iomem = (ioscm_memory_port *) SCM_STREAM (port);
692 size_t to_write = pt->write_pos - pt->write_buf;
693
694 if (to_write == 0)
695 return;
696
697 /* There's no way to indicate a short write, so if the request goes past
698 the end of the port's memory range, flag an error. */
699 if (to_write > iomem->size - iomem->current)
700 {
701 gdbscm_out_of_range_error (FUNC_NAME, 0,
702 gdbscm_scm_from_ulongest (to_write),
703 _("writing beyond end of memory range"));
704 }
705
706 if (target_write_memory (iomem->start + iomem->current, pt->write_buf,
707 to_write) != 0)
708 gdbscm_memory_error (FUNC_NAME, _("error writing memory"), SCM_EOL);
709
710 iomem->current += to_write;
711 pt->write_pos = pt->write_buf;
712 pt->rw_active = SCM_PORT_NEITHER;
713 }
714
715 /* "write" method for memory ports. */
716
717 static void
718 gdbscm_memory_port_write (SCM port, const void *data, size_t size)
719 {
720 scm_t_port *pt = SCM_PTAB_ENTRY (port);
721 ioscm_memory_port *iomem = (ioscm_memory_port *) SCM_STREAM (port);
722 const char *input = (char *) data;
723
724 /* We could get fancy here, and try to buffer the request since we're
725 buffering anyway. But there's currently no need. */
726
727 /* First flush what's currently buffered. */
728 gdbscm_memory_port_flush (port);
729
730 /* There's no way to indicate a short write, so if the request goes past
731 the end of the port's memory range, flag an error. */
732 if (size > iomem->size - iomem->current)
733 {
734 gdbscm_out_of_range_error (FUNC_NAME, 0, gdbscm_scm_from_ulongest (size),
735 _("writing beyond end of memory range"));
736 }
737
738 if (target_write_memory (iomem->start + iomem->current, data, size) != 0)
739 gdbscm_memory_error (FUNC_NAME, _("error writing memory"), SCM_EOL);
740
741 iomem->current += size;
742 }
743
744 /* "seek" method for memory ports. */
745
746 static scm_t_off
747 gdbscm_memory_port_seek (SCM port, scm_t_off offset, int whence)
748 {
749 scm_t_port *pt = SCM_PTAB_ENTRY (port);
750 ioscm_memory_port *iomem = (ioscm_memory_port *) SCM_STREAM (port);
751 CORE_ADDR result;
752 int rc;
753
754 if (pt->rw_active == SCM_PORT_WRITE)
755 {
756 if (offset != 0 || whence != SEEK_CUR)
757 {
758 gdbscm_memory_port_flush (port);
759 rc = ioscm_lseek_address (iomem, offset, whence);
760 result = iomem->current;
761 }
762 else
763 {
764 /* Read current position without disturbing the buffer,
765 but flag an error if what's in the buffer goes outside the
766 allowed range. */
767 CORE_ADDR current = iomem->current;
768 size_t delta = pt->write_pos - pt->write_buf;
769
770 if (current + delta < current
771 || current + delta > iomem->size + 1)
772 rc = 0;
773 else
774 {
775 result = current + delta;
776 rc = 1;
777 }
778 }
779 }
780 else if (pt->rw_active == SCM_PORT_READ)
781 {
782 if (offset != 0 || whence != SEEK_CUR)
783 {
784 scm_end_input (port);
785 rc = ioscm_lseek_address (iomem, offset, whence);
786 result = iomem->current;
787 }
788 else
789 {
790 /* Read current position without disturbing the buffer
791 (particularly the unread-char buffer). */
792 CORE_ADDR current = iomem->current;
793 size_t remaining = pt->read_end - pt->read_pos;
794
795 if (current - remaining > current
796 || current - remaining < iomem->start)
797 rc = 0;
798 else
799 {
800 result = current - remaining;
801 rc = 1;
802 }
803
804 if (rc != 0 && pt->read_buf == pt->putback_buf)
805 {
806 size_t saved_remaining = pt->saved_read_end - pt->saved_read_pos;
807
808 if (result - saved_remaining > result
809 || result - saved_remaining < iomem->start)
810 rc = 0;
811 else
812 result -= saved_remaining;
813 }
814 }
815 }
816 else /* SCM_PORT_NEITHER */
817 {
818 rc = ioscm_lseek_address (iomem, offset, whence);
819 result = iomem->current;
820 }
821
822 if (rc == 0)
823 {
824 gdbscm_out_of_range_error (FUNC_NAME, 0,
825 gdbscm_scm_from_longest (offset),
826 _("bad seek"));
827 }
828
829 /* TODO: The Guile API doesn't support 32x64. We can't fix that here,
830 and there's no need to throw an error if the new address can't be
831 represented in a scm_t_off. But we could return something less
832 clumsy. */
833 return result;
834 }
835
836 /* "close" method for memory ports. */
837
838 static int
839 gdbscm_memory_port_close (SCM port)
840 {
841 scm_t_port *pt = SCM_PTAB_ENTRY (port);
842 ioscm_memory_port *iomem = (ioscm_memory_port *) SCM_STREAM (port);
843
844 gdbscm_memory_port_flush (port);
845
846 if (pt->read_buf == pt->putback_buf)
847 pt->read_buf = pt->saved_read_buf;
848 xfree (pt->read_buf);
849 xfree (pt->write_buf);
850 scm_gc_free (iomem, sizeof (*iomem), "memory port");
851
852 return 0;
853 }
854
855 /* "free" method for memory ports. */
856
857 static size_t
858 gdbscm_memory_port_free (SCM port)
859 {
860 gdbscm_memory_port_close (port);
861
862 return 0;
863 }
864
865 /* "print" method for memory ports. */
866
867 static int
868 gdbscm_memory_port_print (SCM exp, SCM port, scm_print_state *pstate)
869 {
870 ioscm_memory_port *iomem = (ioscm_memory_port *) SCM_STREAM (exp);
871 char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
872
873 scm_puts ("#<", port);
874 scm_print_port_mode (exp, port);
875 /* scm_print_port_mode includes a trailing space. */
876 gdbscm_printf (port, "%s %s-%s", type,
877 hex_string (iomem->start), hex_string (iomem->end));
878 scm_putc ('>', port);
879 return 1;
880 }
881
882 /* Create the port type used for memory. */
883
884 static void
885 ioscm_init_memory_port_type (void)
886 {
887 memory_port_desc = scm_make_port_type (memory_port_desc_name,
888 gdbscm_memory_port_fill_input,
889 gdbscm_memory_port_write);
890
891 scm_set_port_end_input (memory_port_desc, gdbscm_memory_port_end_input);
892 scm_set_port_flush (memory_port_desc, gdbscm_memory_port_flush);
893 scm_set_port_seek (memory_port_desc, gdbscm_memory_port_seek);
894 scm_set_port_close (memory_port_desc, gdbscm_memory_port_close);
895 scm_set_port_free (memory_port_desc, gdbscm_memory_port_free);
896 scm_set_port_print (memory_port_desc, gdbscm_memory_port_print);
897 }
898
899 /* Helper for gdbscm_open_memory to parse the mode bits.
900 An exception is thrown if MODE is invalid. */
901
902 static long
903 ioscm_parse_mode_bits (const char *func_name, const char *mode)
904 {
905 const char *p;
906 long mode_bits;
907
908 if (*mode != 'r' && *mode != 'w')
909 {
910 gdbscm_out_of_range_error (func_name, 0,
911 gdbscm_scm_from_c_string (mode),
912 _("bad mode string"));
913 }
914 for (p = mode + 1; *p != '\0'; ++p)
915 {
916 switch (*p)
917 {
918 case 'b':
919 case '+':
920 break;
921 default:
922 gdbscm_out_of_range_error (func_name, 0,
923 gdbscm_scm_from_c_string (mode),
924 _("bad mode string"));
925 }
926 }
927
928 /* Kinda awkward to convert the mode from SCM -> string only to have Guile
929 convert it back to SCM, but that's the API we have to work with. */
930 mode_bits = scm_mode_bits ((char *) mode);
931
932 return mode_bits;
933 }
934
935 /* Helper for gdbscm_open_memory to finish initializing the port.
936 The port has address range [start,end].
937 To simplify overflow handling, an END of 0xff..ff is not allowed.
938 This also means a start address of 0xff..f is also not allowed.
939 I can live with that. */
940
941 static void
942 ioscm_init_memory_port (SCM port, CORE_ADDR start, CORE_ADDR end)
943 {
944 scm_t_port *pt;
945 ioscm_memory_port *iomem;
946
947 gdb_assert (start <= end);
948 gdb_assert (end < ~(CORE_ADDR) 0);
949
950 iomem = (ioscm_memory_port *) scm_gc_malloc_pointerless (sizeof (*iomem),
951 "memory port");
952
953 iomem->start = start;
954 iomem->end = end;
955 iomem->size = end - start + 1;
956 iomem->current = 0;
957 iomem->read_buf_size = default_read_buf_size;
958 iomem->write_buf_size = default_write_buf_size;
959
960 pt = SCM_PTAB_ENTRY (port);
961 /* Match the expectation of `binary-port?'. */
962 pt->encoding = NULL;
963 pt->rw_random = 1;
964 pt->read_buf_size = iomem->read_buf_size;
965 pt->read_buf = xmalloc (pt->read_buf_size);
966 pt->read_pos = pt->read_end = pt->read_buf;
967 pt->write_buf_size = iomem->write_buf_size;
968 pt->write_buf = xmalloc (pt->write_buf_size);
969 pt->write_pos = pt->write_buf;
970 pt->write_end = pt->write_buf + pt->write_buf_size;
971
972 SCM_SETSTREAM (port, iomem);
973 }
974
975 /* Re-initialize a memory port, updating its read/write buffer sizes.
976 An exception is thrown if data is still buffered, except in the case
977 where the buffer size isn't changing (since that's just a nop). */
978
979 static void
980 ioscm_reinit_memory_port (SCM port, size_t read_buf_size,
981 size_t write_buf_size, const char *func_name)
982 {
983 scm_t_port *pt = SCM_PTAB_ENTRY (port);
984 ioscm_memory_port *iomem = (ioscm_memory_port *) SCM_STREAM (port);
985
986 gdb_assert (read_buf_size >= min_memory_port_buf_size
987 && read_buf_size <= max_memory_port_buf_size);
988 gdb_assert (write_buf_size >= min_memory_port_buf_size
989 && write_buf_size <= max_memory_port_buf_size);
990
991 /* First check if anything is buffered. */
992
993 if (read_buf_size != pt->read_buf_size
994 && pt->read_end != pt->read_buf)
995 {
996 scm_misc_error (func_name, _("read buffer not empty: ~a"),
997 scm_list_1 (port));
998 }
999
1000 if (write_buf_size != pt->write_buf_size
1001 && pt->write_pos != pt->write_buf)
1002 {
1003 scm_misc_error (func_name, _("write buffer not empty: ~a"),
1004 scm_list_1 (port));
1005 }
1006
1007 /* Now we can update the buffer sizes, but only if the size has changed. */
1008
1009 if (read_buf_size != pt->read_buf_size)
1010 {
1011 iomem->read_buf_size = read_buf_size;
1012 pt->read_buf_size = read_buf_size;
1013 xfree (pt->read_buf);
1014 pt->read_buf = xmalloc (pt->read_buf_size);
1015 pt->read_pos = pt->read_end = pt->read_buf;
1016 }
1017
1018 if (write_buf_size != pt->write_buf_size)
1019 {
1020 iomem->write_buf_size = write_buf_size;
1021 pt->write_buf_size = write_buf_size;
1022 xfree (pt->write_buf);
1023 pt->write_buf = xmalloc (pt->write_buf_size);
1024 pt->write_pos = pt->write_buf;
1025 pt->write_end = pt->write_buf + pt->write_buf_size;
1026 }
1027 }
1028
1029 /* (open-memory [#:mode string] [#:start address] [#:size integer]) -> port
1030 Return a port that can be used for reading and writing memory.
1031 MODE is a string, and must be one of "r", "w", or "r+".
1032 For compatibility "b" (binary) may also be present, but we ignore it:
1033 memory ports are binary only.
1034
1035 TODO: Support "0" (unbuffered)? Only support "0" (always unbuffered)?
1036
1037 The chunk of memory that can be accessed can be bounded.
1038 If both START,SIZE are unspecified, all of memory can be accessed.
1039 If only START is specified, all of memory from that point on can be
1040 accessed. If only SIZE if specified, all memory in [0,SIZE) can be
1041 accessed. If both are specified, all memory in [START,START+SIZE) can be
1042 accessed.
1043
1044 Note: If it becomes useful enough we can later add #:end as an alternative
1045 to #:size. For now it is left out.
1046
1047 The result is a Scheme port, and its semantics are a bit odd for accessing
1048 memory (e.g., unget), but we don't try to hide this. It's a port.
1049
1050 N.B. Seeks on the port must be in the range [0,size).
1051 This is for similarity with bytevector ports, and so that one can seek
1052 to the first byte. */
1053
1054 static SCM
1055 gdbscm_open_memory (SCM rest)
1056 {
1057 const SCM keywords[] = {
1058 mode_keyword, start_keyword, size_keyword, SCM_BOOL_F
1059 };
1060 char *mode = NULL;
1061 CORE_ADDR start = 0;
1062 CORE_ADDR end;
1063 int mode_arg_pos = -1, start_arg_pos = -1, size_arg_pos = -1;
1064 ULONGEST size;
1065 SCM port;
1066 long mode_bits;
1067
1068 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "#sUU", rest,
1069 &mode_arg_pos, &mode,
1070 &start_arg_pos, &start,
1071 &size_arg_pos, &size);
1072
1073 scm_dynwind_begin (0);
1074
1075 if (mode == NULL)
1076 mode = xstrdup ("r");
1077 scm_dynwind_free (mode);
1078
1079 if (start == ~(CORE_ADDR) 0)
1080 {
1081 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, scm_from_int (-1),
1082 _("start address of 0xff..ff not allowed"));
1083 }
1084
1085 if (size_arg_pos > 0)
1086 {
1087 if (size == 0)
1088 {
1089 gdbscm_out_of_range_error (FUNC_NAME, 0, scm_from_int (0),
1090 "zero size");
1091 }
1092 /* For now be strict about start+size overflowing. If it becomes
1093 a nuisance we can relax things later. */
1094 if (start + size < start)
1095 {
1096 gdbscm_out_of_range_error (FUNC_NAME, 0,
1097 scm_list_2 (gdbscm_scm_from_ulongest (start),
1098 gdbscm_scm_from_ulongest (size)),
1099 _("start+size overflows"));
1100 }
1101 end = start + size - 1;
1102 if (end == ~(CORE_ADDR) 0)
1103 {
1104 gdbscm_out_of_range_error (FUNC_NAME, 0,
1105 scm_list_2 (gdbscm_scm_from_ulongest (start),
1106 gdbscm_scm_from_ulongest (size)),
1107 _("end address of 0xff..ff not allowed"));
1108 }
1109 }
1110 else
1111 end = (~(CORE_ADDR) 0) - 1;
1112
1113 mode_bits = ioscm_parse_mode_bits (FUNC_NAME, mode);
1114
1115 port = ioscm_open_port (memory_port_desc, mode_bits);
1116
1117 ioscm_init_memory_port (port, start, end);
1118
1119 scm_dynwind_end ();
1120
1121 /* TODO: Set the file name as "memory-start-end"? */
1122 return port;
1123 }
1124
1125 /* Return non-zero if OBJ is a memory port. */
1126
1127 static int
1128 gdbscm_is_memory_port (SCM obj)
1129 {
1130 return !SCM_IMP (obj) && (SCM_TYP16 (obj) == memory_port_desc);
1131 }
1132
1133 /* (memory-port? obj) -> boolean */
1134
1135 static SCM
1136 gdbscm_memory_port_p (SCM obj)
1137 {
1138 return scm_from_bool (gdbscm_is_memory_port (obj));
1139 }
1140
1141 /* (memory-port-range port) -> (start end) */
1142
1143 static SCM
1144 gdbscm_memory_port_range (SCM port)
1145 {
1146 ioscm_memory_port *iomem;
1147
1148 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port), port, SCM_ARG1, FUNC_NAME,
1149 memory_port_desc_name);
1150
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));
1154 }
1155
1156 /* (memory-port-read-buffer-size port) -> integer */
1157
1158 static SCM
1159 gdbscm_memory_port_read_buffer_size (SCM port)
1160 {
1161 ioscm_memory_port *iomem;
1162
1163 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port), port, SCM_ARG1, FUNC_NAME,
1164 memory_port_desc_name);
1165
1166 iomem = (ioscm_memory_port *) SCM_STREAM (port);
1167 return scm_from_uint (iomem->read_buf_size);
1168 }
1169
1170 /* (set-memory-port-read-buffer-size! port size) -> unspecified
1171 An exception is thrown if read data is still buffered. */
1172
1173 static SCM
1174 gdbscm_set_memory_port_read_buffer_size_x (SCM port, SCM size)
1175 {
1176 ioscm_memory_port *iomem;
1177
1178 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port), port, SCM_ARG1, FUNC_NAME,
1179 memory_port_desc_name);
1180 SCM_ASSERT_TYPE (scm_is_integer (size), size, SCM_ARG2, FUNC_NAME,
1181 _("integer"));
1182
1183 if (!scm_is_unsigned_integer (size, min_memory_port_buf_size,
1184 max_memory_port_buf_size))
1185 {
1186 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2, size,
1187 out_of_range_buf_size);
1188 }
1189
1190 iomem = (ioscm_memory_port *) SCM_STREAM (port);
1191 ioscm_reinit_memory_port (port, scm_to_uint (size), iomem->write_buf_size,
1192 FUNC_NAME);
1193
1194 return SCM_UNSPECIFIED;
1195 }
1196
1197 /* (memory-port-write-buffer-size port) -> integer */
1198
1199 static SCM
1200 gdbscm_memory_port_write_buffer_size (SCM port)
1201 {
1202 ioscm_memory_port *iomem;
1203
1204 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port), port, SCM_ARG1, FUNC_NAME,
1205 memory_port_desc_name);
1206
1207 iomem = (ioscm_memory_port *) SCM_STREAM (port);
1208 return scm_from_uint (iomem->write_buf_size);
1209 }
1210
1211 /* (set-memory-port-write-buffer-size! port size) -> unspecified
1212 An exception is thrown if write data is still buffered. */
1213
1214 static SCM
1215 gdbscm_set_memory_port_write_buffer_size_x (SCM port, SCM size)
1216 {
1217 ioscm_memory_port *iomem;
1218
1219 SCM_ASSERT_TYPE (gdbscm_is_memory_port (port), port, SCM_ARG1, FUNC_NAME,
1220 memory_port_desc_name);
1221 SCM_ASSERT_TYPE (scm_is_integer (size), size, SCM_ARG2, FUNC_NAME,
1222 _("integer"));
1223
1224 if (!scm_is_unsigned_integer (size, min_memory_port_buf_size,
1225 max_memory_port_buf_size))
1226 {
1227 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2, size,
1228 out_of_range_buf_size);
1229 }
1230
1231 iomem = (ioscm_memory_port *) SCM_STREAM (port);
1232 ioscm_reinit_memory_port (port, iomem->read_buf_size, scm_to_uint (size),
1233 FUNC_NAME);
1234
1235 return SCM_UNSPECIFIED;
1236 }
1237 \f
1238 /* Initialize gdb ports. */
1239
1240 static const scheme_function port_functions[] =
1241 {
1242 { "input-port", 0, 0, 0, gdbscm_input_port,
1243 "\
1244 Return gdb's input port." },
1245
1246 { "output-port", 0, 0, 0, gdbscm_output_port,
1247 "\
1248 Return gdb's output port." },
1249
1250 { "error-port", 0, 0, 0, gdbscm_error_port,
1251 "\
1252 Return gdb's error port." },
1253
1254 { "stdio-port?", 1, 0, 0, gdbscm_stdio_port_p,
1255 "\
1256 Return #t if the object is a gdb:stdio-port." },
1257
1258 { "open-memory", 0, 0, 1, gdbscm_open_memory,
1259 "\
1260 Return a port that can be used for reading/writing inferior memory.\n\
1261 \n\
1262 Arguments: [#:mode string] [#:start address] [#:size integer]\n\
1263 Returns: A port object." },
1264
1265 { "memory-port?", 1, 0, 0, gdbscm_memory_port_p,
1266 "\
1267 Return #t if the object is a memory port." },
1268
1269 { "memory-port-range", 1, 0, 0, gdbscm_memory_port_range,
1270 "\
1271 Return the memory range of the port as (start end)." },
1272
1273 { "memory-port-read-buffer-size", 1, 0, 0,
1274 gdbscm_memory_port_read_buffer_size,
1275 "\
1276 Return the size of the read buffer for the memory port." },
1277
1278 { "set-memory-port-read-buffer-size!", 2, 0, 0,
1279 gdbscm_set_memory_port_read_buffer_size_x,
1280 "\
1281 Set the size of the read buffer for the memory port.\n\
1282 \n\
1283 Arguments: port integer\n\
1284 Returns: unspecified." },
1285
1286 { "memory-port-write-buffer-size", 1, 0, 0,
1287 gdbscm_memory_port_write_buffer_size,
1288 "\
1289 Return the size of the write buffer for the memory port." },
1290
1291 { "set-memory-port-write-buffer-size!", 2, 0, 0,
1292 gdbscm_set_memory_port_write_buffer_size_x,
1293 "\
1294 Set the size of the write buffer for the memory port.\n\
1295 \n\
1296 Arguments: port integer\n\
1297 Returns: unspecified." },
1298
1299 END_FUNCTIONS
1300 };
1301
1302 static const scheme_function private_port_functions[] =
1303 {
1304 #if 0 /* TODO */
1305 { "%with-gdb-input-from-port", 2, 0, 0,
1306 gdbscm_percent_with_gdb_input_from_port,
1307 "\
1308 Temporarily set GDB's input port to PORT and then invoke THUNK.\n\
1309 \n\
1310 Arguments: port thunk\n\
1311 Returns: The result of calling THUNK.\n\
1312 \n\
1313 This procedure is experimental." },
1314 #endif
1315
1316 { "%with-gdb-output-to-port", 2, 0, 0,
1317 gdbscm_percent_with_gdb_output_to_port,
1318 "\
1319 Temporarily set GDB's output port to PORT and then invoke THUNK.\n\
1320 \n\
1321 Arguments: port thunk\n\
1322 Returns: The result of calling THUNK.\n\
1323 \n\
1324 This procedure is experimental." },
1325
1326 { "%with-gdb-error-to-port", 2, 0, 0,
1327 gdbscm_percent_with_gdb_error_to_port,
1328 "\
1329 Temporarily set GDB's error port to PORT and then invoke THUNK.\n\
1330 \n\
1331 Arguments: port thunk\n\
1332 Returns: The result of calling THUNK.\n\
1333 \n\
1334 This procedure is experimental." },
1335
1336 END_FUNCTIONS
1337 };
1338
1339 void
1340 gdbscm_initialize_ports (void)
1341 {
1342 /* Save the original stdio ports for debugging purposes. */
1343
1344 orig_input_port_scm = scm_current_input_port ();
1345 orig_output_port_scm = scm_current_output_port ();
1346 orig_error_port_scm = scm_current_error_port ();
1347
1348 /* Set up the stdio ports. */
1349
1350 ioscm_init_gdb_stdio_port ();
1351 input_port_scm = ioscm_make_gdb_stdio_port (0);
1352 output_port_scm = ioscm_make_gdb_stdio_port (1);
1353 error_port_scm = ioscm_make_gdb_stdio_port (2);
1354
1355 /* Set up memory ports. */
1356
1357 ioscm_init_memory_port_type ();
1358
1359 /* Install the accessor functions. */
1360
1361 gdbscm_define_functions (port_functions, 1);
1362 gdbscm_define_functions (private_port_functions, 0);
1363
1364 /* Keyword args for open-memory. */
1365
1366 mode_keyword = scm_from_latin1_keyword ("mode");
1367 start_keyword = scm_from_latin1_keyword ("start");
1368 size_keyword = scm_from_latin1_keyword ("size");
1369
1370 /* Error message text for "out of range" memory port buffer sizes. */
1371
1372 out_of_range_buf_size = xstrprintf ("size not between %u - %u",
1373 min_memory_port_buf_size,
1374 max_memory_port_buf_size);
1375 }
This page took 0.065876 seconds and 4 git commands to generate.