1 /* Remote target callback routines. 2 Copyright 1995-2016 Free Software Foundation, Inc. 3 Contributed by Cygnus Solutions. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 /* This file provides a standard way for targets to talk to the host OS 21 level. */ 22 23 #ifdef HAVE_CONFIG_H 24 #include "config.h" 25 #endif 26 #include "ansidecl.h" 27 #include <stdarg.h> 28 #include <stdio.h> 29 #ifdef HAVE_STDLIB_H 30 #include <stdlib.h> 31 #endif 32 #ifdef HAVE_STRING_H 33 #include <string.h> 34 #else 35 #ifdef HAVE_STRINGS_H 36 #include <strings.h> 37 #endif 38 #endif 39 #ifdef HAVE_LIMITS_H 40 /* For PIPE_BUF. */ 41 #include <limits.h> 42 #endif 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <time.h> 46 #include <sys/types.h> 47 #include <sys/stat.h> 48 #include "gdb/callback.h" 49 #include "targ-vals.h" 50 /* For xmalloc. */ 51 #include "libiberty.h" 52 53 #ifdef HAVE_UNISTD_H 54 #include <unistd.h> 55 #endif 56 57 #ifndef PIPE_BUF 58 #define PIPE_BUF 512 59 #endif 60 61 /* ??? sim_cb_printf should be cb_printf, but until the callback support is 62 broken out of the simulator directory, these are here to not require 63 sim-utils.h. */ 64 void sim_cb_printf (host_callback *, const char *, ...); 65 void sim_cb_eprintf (host_callback *, const char *, ...); 66 67 extern CB_TARGET_DEFS_MAP cb_init_syscall_map[]; 68 extern CB_TARGET_DEFS_MAP cb_init_errno_map[]; 69 extern CB_TARGET_DEFS_MAP cb_init_open_map[]; 70 71 /* Set the callback copy of errno from what we see now. */ 72 73 static int 74 wrap (host_callback *p, int val) 75 { 76 p->last_errno = errno; 77 return val; 78 } 79 80 /* Make sure the FD provided is ok. If not, return non-zero 81 and set errno. */ 82 83 static int 84 fdbad (host_callback *p, int fd) 85 { 86 if (fd < 0 || fd > MAX_CALLBACK_FDS || p->fd_buddy[fd] < 0) 87 { 88 p->last_errno = EBADF; 89 return -1; 90 } 91 return 0; 92 } 93 94 static int 95 fdmap (host_callback *p, int fd) 96 { 97 return p->fdmap[fd]; 98 } 99 100 static int 101 os_close (host_callback *p, int fd) 102 { 103 int result; 104 int i, next; 105 106 result = fdbad (p, fd); 107 if (result) 108 return result; 109 /* If this file descripter has one or more buddies (originals / 110 duplicates from a dup), just remove it from the circular list. */ 111 for (i = fd; (next = p->fd_buddy[i]) != fd; ) 112 i = next; 113 if (fd != i) 114 p->fd_buddy[i] = p->fd_buddy[fd]; 115 else 116 { 117 if (p->ispipe[fd]) 118 { 119 int other = p->ispipe[fd]; 120 int reader, writer; 121 122 if (other > 0) 123 { 124 /* Closing the read side. */ 125 reader = fd; 126 writer = other; 127 } 128 else 129 { 130 /* Closing the write side. */ 131 writer = fd; 132 reader = -other; 133 } 134 135 /* If there was data in the buffer, make a last "now empty" 136 call, then deallocate data. */ 137 if (p->pipe_buffer[writer].buffer != NULL) 138 { 139 (*p->pipe_empty) (p, reader, writer); 140 free (p->pipe_buffer[writer].buffer); 141 p->pipe_buffer[writer].buffer = NULL; 142 } 143 144 /* Clear pipe data for this side. */ 145 p->pipe_buffer[fd].size = 0; 146 p->ispipe[fd] = 0; 147 148 /* If this was the first close, mark the other side as the 149 only remaining side. */ 150 if (fd != abs (other)) 151 p->ispipe[abs (other)] = -other; 152 p->fd_buddy[fd] = -1; 153 return 0; 154 } 155 156 result = wrap (p, close (fdmap (p, fd))); 157 } 158 p->fd_buddy[fd] = -1; 159 160 return result; 161 } 162 163 164 /* taken from gdb/util.c:notice_quit() - should be in a library */ 165 166 167 #if defined(__GO32__) || defined (_MSC_VER) 168 static int 169 os_poll_quit (host_callback *p) 170 { 171 #if defined(__GO32__) 172 int kbhit (); 173 int getkey (); 174 if (kbhit ()) 175 { 176 int k = getkey (); 177 if (k == 1) 178 { 179 return 1; 180 } 181 else if (k == 2) 182 { 183 return 1; 184 } 185 else 186 { 187 sim_cb_eprintf (p, "CTRL-A to quit, CTRL-B to quit harder\n"); 188 } 189 } 190 #endif 191 #if defined (_MSC_VER) 192 /* NB - this will not compile! */ 193 int k = win32pollquit (); 194 if (k == 1) 195 return 1; 196 else if (k == 2) 197 return 1; 198 #endif 199 return 0; 200 } 201 #else 202 #define os_poll_quit 0 203 #endif /* defined(__GO32__) || defined(_MSC_VER) */ 204 205 static int 206 os_get_errno (host_callback *p) 207 { 208 return cb_host_to_target_errno (p, p->last_errno); 209 } 210 211 212 static int 213 os_isatty (host_callback *p, int fd) 214 { 215 int result; 216 217 result = fdbad (p, fd); 218 if (result) 219 return result; 220 result = wrap (p, isatty (fdmap (p, fd))); 221 222 return result; 223 } 224 225 static int 226 os_lseek (host_callback *p, int fd, long off, int way) 227 { 228 int result; 229 230 result = fdbad (p, fd); 231 if (result) 232 return result; 233 result = wrap (p, lseek (fdmap (p, fd), off, way)); 234 return result; 235 } 236 237 static int 238 os_open (host_callback *p, const char *name, int flags) 239 { 240 int i; 241 for (i = 0; i < MAX_CALLBACK_FDS; i++) 242 { 243 if (p->fd_buddy[i] < 0) 244 { 245 int f = open (name, cb_target_to_host_open (p, flags), 0644); 246 if (f < 0) 247 { 248 p->last_errno = errno; 249 return f; 250 } 251 p->fd_buddy[i] = i; 252 p->fdmap[i] = f; 253 return i; 254 } 255 } 256 p->last_errno = EMFILE; 257 return -1; 258 } 259 260 static int 261 os_read (host_callback *p, int fd, char *buf, int len) 262 { 263 int result; 264 265 result = fdbad (p, fd); 266 if (result) 267 return result; 268 if (p->ispipe[fd]) 269 { 270 int writer = p->ispipe[fd]; 271 272 /* Can't read from the write-end. */ 273 if (writer < 0) 274 { 275 p->last_errno = EBADF; 276 return -1; 277 } 278 279 /* Nothing to read if nothing is written. */ 280 if (p->pipe_buffer[writer].size == 0) 281 return 0; 282 283 /* Truncate read request size to buffer size minus what's already 284 read. */ 285 if (len > p->pipe_buffer[writer].size - p->pipe_buffer[fd].size) 286 len = p->pipe_buffer[writer].size - p->pipe_buffer[fd].size; 287 288 memcpy (buf, p->pipe_buffer[writer].buffer + p->pipe_buffer[fd].size, 289 len); 290 291 /* Account for what we just read. */ 292 p->pipe_buffer[fd].size += len; 293 294 /* If we've read everything, empty and deallocate the buffer and 295 signal buffer-empty to client. (This isn't expected to be a 296 hot path in the simulator, so we don't hold on to the buffer.) */ 297 if (p->pipe_buffer[fd].size == p->pipe_buffer[writer].size) 298 { 299 free (p->pipe_buffer[writer].buffer); 300 p->pipe_buffer[writer].buffer = NULL; 301 p->pipe_buffer[fd].size = 0; 302 p->pipe_buffer[writer].size = 0; 303 (*p->pipe_empty) (p, fd, writer); 304 } 305 306 return len; 307 } 308 309 result = wrap (p, read (fdmap (p, fd), buf, len)); 310 return result; 311 } 312 313 static int 314 os_read_stdin (host_callback *p, char *buf, int len) 315 { 316 return wrap (p, read (0, buf, len)); 317 } 318 319 static int 320 os_write (host_callback *p, int fd, const char *buf, int len) 321 { 322 int result; 323 int real_fd; 324 325 result = fdbad (p, fd); 326 if (result) 327 return result; 328 329 if (p->ispipe[fd]) 330 { 331 int reader = -p->ispipe[fd]; 332 333 /* Can't write to the read-end. */ 334 if (reader < 0) 335 { 336 p->last_errno = EBADF; 337 return -1; 338 } 339 340 /* Can't write to pipe with closed read end. 341 FIXME: We should send a SIGPIPE. */ 342 if (reader == fd) 343 { 344 p->last_errno = EPIPE; 345 return -1; 346 } 347 348 /* As a sanity-check, we bail out it the buffered contents is much 349 larger than the size of the buffer on the host. We don't want 350 to run out of memory in the simulator due to a target program 351 bug if we can help it. Unfortunately, regarding the value that 352 reaches the simulated program, it's no use returning *less* 353 than the requested amount, because cb_syscall loops calling 354 this function until the whole amount is done. */ 355 if (p->pipe_buffer[fd].size + len > 10 * PIPE_BUF) 356 { 357 p->last_errno = EFBIG; 358 return -1; 359 } 360 361 p->pipe_buffer[fd].buffer 362 = xrealloc (p->pipe_buffer[fd].buffer, p->pipe_buffer[fd].size + len); 363 memcpy (p->pipe_buffer[fd].buffer + p->pipe_buffer[fd].size, 364 buf, len); 365 p->pipe_buffer[fd].size += len; 366 367 (*p->pipe_nonempty) (p, reader, fd); 368 return len; 369 } 370 371 real_fd = fdmap (p, fd); 372 switch (real_fd) 373 { 374 default: 375 result = wrap (p, write (real_fd, buf, len)); 376 break; 377 case 1: 378 result = p->write_stdout (p, buf, len); 379 break; 380 case 2: 381 result = p->write_stderr (p, buf, len); 382 break; 383 } 384 return result; 385 } 386 387 static int 388 os_write_stdout (host_callback *p ATTRIBUTE_UNUSED, const char *buf, int len) 389 { 390 return fwrite (buf, 1, len, stdout); 391 } 392 393 static void 394 os_flush_stdout (host_callback *p ATTRIBUTE_UNUSED) 395 { 396 fflush (stdout); 397 } 398 399 static int 400 os_write_stderr (host_callback *p ATTRIBUTE_UNUSED, const char *buf, int len) 401 { 402 return fwrite (buf, 1, len, stderr); 403 } 404 405 static void 406 os_flush_stderr (host_callback *p ATTRIBUTE_UNUSED) 407 { 408 fflush (stderr); 409 } 410 411 static int 412 os_rename (host_callback *p, const char *f1, const char *f2) 413 { 414 return wrap (p, rename (f1, f2)); 415 } 416 417 418 static int 419 os_system (host_callback *p, const char *s) 420 { 421 return wrap (p, system (s)); 422 } 423 424 static long 425 os_time (host_callback *p, long *t) 426 { 427 long v = (long)time(NULL); 428 429 if (t != NULL) 430 *t = v; 431 return wrap (p, v); 432 } 433 434 435 static int 436 os_unlink (host_callback *p, const char *f1) 437 { 438 return wrap (p, unlink (f1)); 439 } 440 441 static int 442 os_stat (host_callback *p, const char *file, struct stat *buf) 443 { 444 /* ??? There is an issue of when to translate to the target layout. 445 One could do that inside this function, or one could have the 446 caller do it. It's more flexible to let the caller do it, though 447 I'm not sure the flexibility will ever be useful. */ 448 return wrap (p, stat (file, buf)); 449 } 450 451 static int 452 os_fstat (host_callback *p, int fd, struct stat *buf) 453 { 454 if (fdbad (p, fd)) 455 return -1; 456 457 if (p->ispipe[fd]) 458 { 459 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME) 460 time_t t = (*p->time) (p, NULL); 461 #endif 462 463 /* We have to fake the struct stat contents, since the pipe is 464 made up in the simulator. */ 465 memset (buf, 0, sizeof (*buf)); 466 467 #ifdef HAVE_STRUCT_STAT_ST_MODE 468 buf->st_mode = S_IFIFO; 469 #endif 470 471 /* If more accurate tracking than current-time is needed (for 472 example, on GNU/Linux we get accurate numbers), the p->time 473 callback (which may be something other than os_time) should 474 happen for each read and write, and we'd need to keep track of 475 atime, ctime and mtime. */ 476 #ifdef HAVE_STRUCT_STAT_ST_ATIME 477 buf->st_atime = t; 478 #endif 479 #ifdef HAVE_STRUCT_STAT_ST_CTIME 480 buf->st_ctime = t; 481 #endif 482 #ifdef HAVE_STRUCT_STAT_ST_MTIME 483 buf->st_mtime = t; 484 #endif 485 return 0; 486 } 487 488 /* ??? There is an issue of when to translate to the target layout. 489 One could do that inside this function, or one could have the 490 caller do it. It's more flexible to let the caller do it, though 491 I'm not sure the flexibility will ever be useful. */ 492 return wrap (p, fstat (fdmap (p, fd), buf)); 493 } 494 495 static int 496 os_lstat (host_callback *p, const char *file, struct stat *buf) 497 { 498 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */ 499 #ifdef HAVE_LSTAT 500 return wrap (p, lstat (file, buf)); 501 #else 502 return wrap (p, stat (file, buf)); 503 #endif 504 } 505 506 static int 507 os_ftruncate (host_callback *p, int fd, long len) 508 { 509 int result; 510 511 result = fdbad (p, fd); 512 if (p->ispipe[fd]) 513 { 514 p->last_errno = EINVAL; 515 return -1; 516 } 517 if (result) 518 return result; 519 #ifdef HAVE_FTRUNCATE 520 result = wrap (p, ftruncate (fdmap (p, fd), len)); 521 #else 522 p->last_errno = EINVAL; 523 result = -1; 524 #endif 525 return result; 526 } 527 528 static int 529 os_truncate (host_callback *p, const char *file, long len) 530 { 531 #ifdef HAVE_TRUNCATE 532 return wrap (p, truncate (file, len)); 533 #else 534 p->last_errno = EINVAL; 535 return -1; 536 #endif 537 } 538 539 static int 540 os_pipe (host_callback *p, int *filedes) 541 { 542 int i; 543 544 /* We deliberately don't use fd 0. It's probably stdin anyway. */ 545 for (i = 1; i < MAX_CALLBACK_FDS; i++) 546 { 547 int j; 548 549 if (p->fd_buddy[i] < 0) 550 for (j = i + 1; j < MAX_CALLBACK_FDS; j++) 551 if (p->fd_buddy[j] < 0) 552 { 553 /* Found two free fd:s. Set stat to allocated and mark 554 pipeness. */ 555 p->fd_buddy[i] = i; 556 p->fd_buddy[j] = j; 557 p->ispipe[i] = j; 558 p->ispipe[j] = -i; 559 filedes[0] = i; 560 filedes[1] = j; 561 562 /* Poison the FD map to make bugs apparent. */ 563 p->fdmap[i] = -1; 564 p->fdmap[j] = -1; 565 return 0; 566 } 567 } 568 569 p->last_errno = EMFILE; 570 return -1; 571 } 572 573 /* Stub functions for pipe support. They should always be overridden in 574 targets using the pipe support, but that's up to the target. */ 575 576 /* Called when the simulator says that the pipe at (reader, writer) is 577 now empty (so the writer should leave its waiting state). */ 578 579 static void 580 os_pipe_empty (host_callback *p, int reader, int writer) 581 { 582 } 583 584 /* Called when the simulator says the pipe at (reader, writer) is now 585 non-empty (so the writer should wait). */ 586 587 static void 588 os_pipe_nonempty (host_callback *p, int reader, int writer) 589 { 590 } 591 592 static int 593 os_shutdown (host_callback *p) 594 { 595 int i, next, j; 596 for (i = 0; i < MAX_CALLBACK_FDS; i++) 597 { 598 int do_close = 1; 599 600 /* Zero out all pipe state. Don't call callbacks for non-empty 601 pipes; the target program has likely terminated at this point 602 or we're called at initialization time. */ 603 p->ispipe[i] = 0; 604 p->pipe_buffer[i].size = 0; 605 p->pipe_buffer[i].buffer = NULL; 606 607 next = p->fd_buddy[i]; 608 if (next < 0) 609 continue; 610 do 611 { 612 j = next; 613 if (j == MAX_CALLBACK_FDS) 614 do_close = 0; 615 next = p->fd_buddy[j]; 616 p->fd_buddy[j] = -1; 617 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */ 618 if (next < 0) 619 { 620 p->fd_buddy[i] = -1; 621 do_close = 0; 622 break; 623 } 624 } 625 while (j != i); 626 if (do_close) 627 close (p->fdmap[i]); 628 } 629 return 1; 630 } 631 632 static int 633 os_init (host_callback *p) 634 { 635 int i; 636 637 os_shutdown (p); 638 for (i = 0; i < 3; i++) 639 { 640 p->fdmap[i] = i; 641 p->fd_buddy[i] = i - 1; 642 } 643 p->fd_buddy[0] = MAX_CALLBACK_FDS; 644 p->fd_buddy[MAX_CALLBACK_FDS] = 2; 645 646 p->syscall_map = cb_init_syscall_map; 647 p->errno_map = cb_init_errno_map; 648 p->open_map = cb_init_open_map; 649 650 return 1; 651 } 652 653 /* DEPRECATED */ 654 655 /* VARARGS */ 656 static void 657 os_printf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...) 658 { 659 va_list args; 660 va_start (args, format); 661 662 vfprintf (stdout, format, args); 663 va_end (args); 664 } 665 666 /* VARARGS */ 667 static void 668 os_vprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args) 669 { 670 vprintf (format, args); 671 } 672 673 /* VARARGS */ 674 static void 675 os_evprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args) 676 { 677 vfprintf (stderr, format, args); 678 } 679 680 /* VARARGS */ 681 #ifdef __GNUC__ 682 __attribute__ ((__noreturn__)) 683 #endif 684 static void 685 os_error (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...) 686 { 687 va_list args; 688 va_start (args, format); 689 690 vfprintf (stderr, format, args); 691 fprintf (stderr, "\n"); 692 693 va_end (args); 694 exit (1); 695 } 696 697 host_callback default_callback = 698 { 699 os_close, 700 os_get_errno, 701 os_isatty, 702 os_lseek, 703 os_open, 704 os_read, 705 os_read_stdin, 706 os_rename, 707 os_system, 708 os_time, 709 os_unlink, 710 os_write, 711 os_write_stdout, 712 os_flush_stdout, 713 os_write_stderr, 714 os_flush_stderr, 715 716 os_stat, 717 os_fstat, 718 os_lstat, 719 720 os_ftruncate, 721 os_truncate, 722 723 os_pipe, 724 os_pipe_empty, 725 os_pipe_nonempty, 726 727 os_poll_quit, 728 729 os_shutdown, 730 os_init, 731 732 os_printf_filtered, /* deprecated */ 733 734 os_vprintf_filtered, 735 os_evprintf_filtered, 736 os_error, 737 738 0, /* last errno */ 739 740 { 0, }, /* fdmap */ 741 { -1, }, /* fd_buddy */ 742 { 0, }, /* ispipe */ 743 { { 0, 0 }, }, /* pipe_buffer */ 744 745 0, /* syscall_map */ 746 0, /* errno_map */ 747 0, /* open_map */ 748 0, /* signal_map */ 749 0, /* stat_map */ 750 751 /* Defaults expected to be overridden at initialization, where needed. */ 752 BFD_ENDIAN_UNKNOWN, /* target_endian */ 753 4, /* target_sizeof_int */ 754 755 HOST_CALLBACK_MAGIC, 756 }; 757 758 /* Read in a file describing the target's system call values. 759 E.g. maybe someone will want to use something other than newlib. 760 This assumes that the basic system call recognition and value passing/ 761 returning is supported. So maybe some coding/recompilation will be 762 necessary, but not as much. 763 764 If an error occurs, the existing mapping is not changed. */ 765 766 CB_RC 767 cb_read_target_syscall_maps (host_callback *cb, const char *file) 768 { 769 CB_TARGET_DEFS_MAP *syscall_map, *errno_map, *open_map, *signal_map; 770 const char *stat_map; 771 FILE *f; 772 773 if ((f = fopen (file, "r")) == NULL) 774 return CB_RC_ACCESS; 775 776 /* ... read in and parse file ... */ 777 778 fclose (f); 779 return CB_RC_NO_MEM; /* FIXME:wip */ 780 781 /* Free storage allocated for any existing maps. */ 782 if (cb->syscall_map) 783 free (cb->syscall_map); 784 if (cb->errno_map) 785 free (cb->errno_map); 786 if (cb->open_map) 787 free (cb->open_map); 788 if (cb->signal_map) 789 free (cb->signal_map); 790 if (cb->stat_map) 791 free ((PTR) cb->stat_map); 792 793 cb->syscall_map = syscall_map; 794 cb->errno_map = errno_map; 795 cb->open_map = open_map; 796 cb->signal_map = signal_map; 797 cb->stat_map = stat_map; 798 799 return CB_RC_OK; 800 } 801 802 /* General utility functions to search a map for a value. */ 803 804 static const CB_TARGET_DEFS_MAP * 805 cb_target_map_entry (const CB_TARGET_DEFS_MAP map[], int target_val) 806 { 807 const CB_TARGET_DEFS_MAP *m; 808 809 for (m = &map[0]; m->target_val != -1; ++m) 810 if (m->target_val == target_val) 811 return m; 812 813 return NULL; 814 } 815 816 static const CB_TARGET_DEFS_MAP * 817 cb_host_map_entry (const CB_TARGET_DEFS_MAP map[], int host_val) 818 { 819 const CB_TARGET_DEFS_MAP *m; 820 821 for (m = &map[0]; m->host_val != -1; ++m) 822 if (m->host_val == host_val) 823 return m; 824 825 return NULL; 826 } 827 828 /* Translate the target's version of a syscall number to the host's. 829 This isn't actually the host's version, rather a canonical form. 830 ??? Perhaps this should be renamed to ..._canon_syscall. */ 831 832 int 833 cb_target_to_host_syscall (host_callback *cb, int target_val) 834 { 835 const CB_TARGET_DEFS_MAP *m = 836 cb_target_map_entry (cb->syscall_map, target_val); 837 838 return m ? m->host_val : -1; 839 } 840 841 /* FIXME: sort tables if large. 842 Alternatively, an obvious improvement for errno conversion is 843 to machine generate a function with a large switch(). */ 844 845 /* Translate the host's version of errno to the target's. */ 846 847 int 848 cb_host_to_target_errno (host_callback *cb, int host_val) 849 { 850 const CB_TARGET_DEFS_MAP *m = cb_host_map_entry (cb->errno_map, host_val); 851 852 /* ??? Which error to return in this case is up for grabs. 853 Note that some missing values may have standard alternatives. 854 For now return 0 and require caller to deal with it. */ 855 return m ? m->target_val : 0; 856 } 857 858 /* Given a set of target bitmasks for the open system call, 859 return the host equivalent. 860 Mapping open flag values is best done by looping so there's no need 861 to machine generate this function. */ 862 863 int 864 cb_target_to_host_open (host_callback *cb, int target_val) 865 { 866 int host_val = 0; 867 CB_TARGET_DEFS_MAP *m; 868 869 for (m = &cb->open_map[0]; m->host_val != -1; ++m) 870 { 871 switch (m->target_val) 872 { 873 /* O_RDONLY can be (and usually is) 0 which needs to be treated 874 specially. */ 875 case TARGET_O_RDONLY : 876 case TARGET_O_WRONLY : 877 case TARGET_O_RDWR : 878 if ((target_val & (TARGET_O_RDONLY | TARGET_O_WRONLY | TARGET_O_RDWR)) 879 == m->target_val) 880 host_val |= m->host_val; 881 /* Handle the host/target differentiating between binary and 882 text mode. Only one case is of importance */ 883 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY) 884 host_val |= O_BINARY; 885 #endif 886 break; 887 default : 888 if ((m->target_val & target_val) == m->target_val) 889 host_val |= m->host_val; 890 break; 891 } 892 } 893 894 return host_val; 895 } 896 897 /* Utility for e.g. cb_host_to_target_stat to store values in the target's 898 stat struct. 899 900 ??? The "val" must be as big as target word size. */ 901 902 void 903 cb_store_target_endian (host_callback *cb, char *p, int size, long val) 904 { 905 if (cb->target_endian == BFD_ENDIAN_BIG) 906 { 907 p += size; 908 while (size-- > 0) 909 { 910 *--p = val; 911 val >>= 8; 912 } 913 } 914 else 915 { 916 while (size-- > 0) 917 { 918 *p++ = val; 919 val >>= 8; 920 } 921 } 922 } 923 924 /* Translate a host's stat struct into a target's. 925 If HS is NULL, just compute the length of the buffer required, 926 TS is ignored. 927 928 The result is the size of the target's stat struct, 929 or zero if an error occurred during the translation. */ 930 931 int 932 cb_host_to_target_stat (host_callback *cb, const struct stat *hs, PTR ts) 933 { 934 const char *m = cb->stat_map; 935 char *p; 936 937 if (hs == NULL) 938 ts = NULL; 939 p = ts; 940 941 while (m) 942 { 943 char *q = strchr (m, ','); 944 int size; 945 946 /* FIXME: Use sscanf? */ 947 if (q == NULL) 948 { 949 /* FIXME: print error message */ 950 return 0; 951 } 952 size = atoi (q + 1); 953 if (size == 0) 954 { 955 /* FIXME: print error message */ 956 return 0; 957 } 958 959 if (hs != NULL) 960 { 961 if (0) 962 ; 963 /* Defined here to avoid emacs indigestion on a lone "else". */ 964 #undef ST_x 965 #define ST_x(FLD) \ 966 else if (strncmp (m, #FLD, q - m) == 0) \ 967 cb_store_target_endian (cb, p, size, hs->FLD) 968 969 #ifdef HAVE_STRUCT_STAT_ST_DEV 970 ST_x (st_dev); 971 #endif 972 #ifdef HAVE_STRUCT_STAT_ST_INO 973 ST_x (st_ino); 974 #endif 975 #ifdef HAVE_STRUCT_STAT_ST_MODE 976 ST_x (st_mode); 977 #endif 978 #ifdef HAVE_STRUCT_STAT_ST_NLINK 979 ST_x (st_nlink); 980 #endif 981 #ifdef HAVE_STRUCT_STAT_ST_UID 982 ST_x (st_uid); 983 #endif 984 #ifdef HAVE_STRUCT_STAT_ST_GID 985 ST_x (st_gid); 986 #endif 987 #ifdef HAVE_STRUCT_STAT_ST_RDEV 988 ST_x (st_rdev); 989 #endif 990 #ifdef HAVE_STRUCT_STAT_ST_SIZE 991 ST_x (st_size); 992 #endif 993 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE 994 ST_x (st_blksize); 995 #endif 996 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS 997 ST_x (st_blocks); 998 #endif 999 #ifdef HAVE_STRUCT_STAT_ST_ATIME 1000 ST_x (st_atime); 1001 #endif 1002 #ifdef HAVE_STRUCT_STAT_ST_MTIME 1003 ST_x (st_mtime); 1004 #endif 1005 #ifdef HAVE_STRUCT_STAT_ST_CTIME 1006 ST_x (st_ctime); 1007 #endif 1008 #undef ST_x 1009 /* FIXME:wip */ 1010 else 1011 /* Unsupported field, store 0. */ 1012 cb_store_target_endian (cb, p, size, 0); 1013 } 1014 1015 p += size; 1016 m = strchr (q, ':'); 1017 if (m) 1018 ++m; 1019 } 1020 1021 return p - (char *) ts; 1022 } 1023 1024 /* Cover functions to the vfprintf callbacks. 1025 1026 ??? If one thinks of the callbacks as a subsystem onto itself [or part of 1027 a larger "remote target subsystem"] with a well defined interface, then 1028 one would think that the subsystem would provide these. However, until 1029 one is allowed to create such a subsystem (with its own source tree 1030 independent of any particular user), such a critter can't exist. Thus 1031 these functions are here for the time being. */ 1032 1033 void 1034 sim_cb_printf (host_callback *p, const char *fmt, ...) 1035 { 1036 va_list ap; 1037 1038 va_start (ap, fmt); 1039 p->vprintf_filtered (p, fmt, ap); 1040 va_end (ap); 1041 } 1042 1043 void 1044 sim_cb_eprintf (host_callback *p, const char *fmt, ...) 1045 { 1046 va_list ap; 1047 1048 va_start (ap, fmt); 1049 p->evprintf_filtered (p, fmt, ap); 1050 va_end (ap); 1051 } 1052 1053 int 1054 cb_is_stdin (host_callback *cb, int fd) 1055 { 1056 return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 0; 1057 } 1058 1059 int 1060 cb_is_stdout (host_callback *cb, int fd) 1061 { 1062 return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 1; 1063 } 1064 1065 int 1066 cb_is_stderr (host_callback *cb, int fd) 1067 { 1068 return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 2; 1069 } 1070 1071 const char * 1072 cb_host_str_syscall (host_callback *cb, int host_val) 1073 { 1074 const CB_TARGET_DEFS_MAP *m = cb_host_map_entry (cb->syscall_map, host_val); 1075 1076 return m ? m->name : NULL; 1077 } 1078 1079 const char * 1080 cb_host_str_errno (host_callback *cb, int host_val) 1081 { 1082 const CB_TARGET_DEFS_MAP *m = cb_host_map_entry (cb->errno_map, host_val); 1083 1084 return m ? m->name : NULL; 1085 } 1086 1087 const char * 1088 cb_host_str_signal (host_callback *cb, int host_val) 1089 { 1090 const CB_TARGET_DEFS_MAP *m = cb_host_map_entry (cb->signal_map, host_val); 1091 1092 return m ? m->name : NULL; 1093 } 1094 1095 const char * 1096 cb_target_str_syscall (host_callback *cb, int target_val) 1097 { 1098 const CB_TARGET_DEFS_MAP *m = 1099 cb_target_map_entry (cb->syscall_map, target_val); 1100 1101 return m ? m->name : NULL; 1102 } 1103 1104 const char * 1105 cb_target_str_errno (host_callback *cb, int target_val) 1106 { 1107 const CB_TARGET_DEFS_MAP *m = 1108 cb_target_map_entry (cb->errno_map, target_val); 1109 1110 return m ? m->name : NULL; 1111 } 1112 1113 const char * 1114 cb_target_str_signal (host_callback *cb, int target_val) 1115 { 1116 const CB_TARGET_DEFS_MAP *m = 1117 cb_target_map_entry (cb->signal_map, target_val); 1118 1119 return m ? m->name : NULL; 1120 } 1121