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