1 /* General utility routines for GDB, the GNU debugger. 2 Copyright 1986, 89, 90, 91, 92, 95, 1996 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19 20 #include "defs.h" 21 #ifdef ANSI_PROTOTYPES 22 #include <stdarg.h> 23 #else 24 #include <varargs.h> 25 #endif 26 #include <ctype.h> 27 #include "gdb_string.h" 28 #ifdef HAVE_UNISTD_H 29 #include <unistd.h> 30 #endif 31 32 #include "signals.h" 33 #include "gdbcmd.h" 34 #include "serial.h" 35 #include "bfd.h" 36 #include "target.h" 37 #include "demangle.h" 38 #include "expression.h" 39 #include "language.h" 40 #include "annotate.h" 41 42 #include "readline.h" 43 44 /* readline defines this. */ 45 #undef savestring 46 47 /* Prototypes for local functions */ 48 49 static void vfprintf_maybe_filtered PARAMS ((FILE *, const char *, va_list, int)); 50 51 static void fputs_maybe_filtered PARAMS ((const char *, FILE *, int)); 52 53 #if !defined (NO_MMALLOC) && !defined (NO_MMCHECK) 54 static void malloc_botch PARAMS ((void)); 55 #endif 56 57 static void 58 fatal_dump_core PARAMS((char *, ...)); 59 60 static void 61 prompt_for_continue PARAMS ((void)); 62 63 static void 64 set_width_command PARAMS ((char *, int, struct cmd_list_element *)); 65 66 /* If this definition isn't overridden by the header files, assume 67 that isatty and fileno exist on this system. */ 68 #ifndef ISATTY 69 #define ISATTY(FP) (isatty (fileno (FP))) 70 #endif 71 72 /* Chain of cleanup actions established with make_cleanup, 73 to be executed if an error happens. */ 74 75 static struct cleanup *cleanup_chain; 76 77 /* Nonzero if we have job control. */ 78 79 int job_control; 80 81 /* Nonzero means a quit has been requested. */ 82 83 int quit_flag; 84 85 /* Nonzero means quit immediately if Control-C is typed now, rather 86 than waiting until QUIT is executed. Be careful in setting this; 87 code which executes with immediate_quit set has to be very careful 88 about being able to deal with being interrupted at any time. It is 89 almost always better to use QUIT; the only exception I can think of 90 is being able to quit out of a system call (using EINTR loses if 91 the SIGINT happens between the previous QUIT and the system call). 92 To immediately quit in the case in which a SIGINT happens between 93 the previous QUIT and setting immediate_quit (desirable anytime we 94 expect to block), call QUIT after setting immediate_quit. */ 95 96 int immediate_quit; 97 98 /* Nonzero means that encoded C++ names should be printed out in their 99 C++ form rather than raw. */ 100 101 int demangle = 1; 102 103 /* Nonzero means that encoded C++ names should be printed out in their 104 C++ form even in assembler language displays. If this is set, but 105 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ 106 107 int asm_demangle = 0; 108 109 /* Nonzero means that strings with character values >0x7F should be printed 110 as octal escapes. Zero means just print the value (e.g. it's an 111 international character, and the terminal or window can cope.) */ 112 113 int sevenbit_strings = 0; 114 115 /* String to be printed before error messages, if any. */ 116 117 char *error_pre_print; 118 119 /* String to be printed before quit messages, if any. */ 120 121 char *quit_pre_print; 122 123 /* String to be printed before warning messages, if any. */ 124 125 char *warning_pre_print = "\nwarning: "; 126 127 /* Add a new cleanup to the cleanup_chain, 128 and return the previous chain pointer 129 to be passed later to do_cleanups or discard_cleanups. 130 Args are FUNCTION to clean up with, and ARG to pass to it. */ 131 132 struct cleanup * 133 make_cleanup (function, arg) 134 void (*function) PARAMS ((PTR)); 135 PTR arg; 136 { 137 register struct cleanup *new 138 = (struct cleanup *) xmalloc (sizeof (struct cleanup)); 139 register struct cleanup *old_chain = cleanup_chain; 140 141 new->next = cleanup_chain; 142 new->function = function; 143 new->arg = arg; 144 cleanup_chain = new; 145 146 return old_chain; 147 } 148 149 /* Discard cleanups and do the actions they describe 150 until we get back to the point OLD_CHAIN in the cleanup_chain. */ 151 152 void 153 do_cleanups (old_chain) 154 register struct cleanup *old_chain; 155 { 156 register struct cleanup *ptr; 157 while ((ptr = cleanup_chain) != old_chain) 158 { 159 cleanup_chain = ptr->next; /* Do this first incase recursion */ 160 (*ptr->function) (ptr->arg); 161 free (ptr); 162 } 163 } 164 165 /* Discard cleanups, not doing the actions they describe, 166 until we get back to the point OLD_CHAIN in the cleanup_chain. */ 167 168 void 169 discard_cleanups (old_chain) 170 register struct cleanup *old_chain; 171 { 172 register struct cleanup *ptr; 173 while ((ptr = cleanup_chain) != old_chain) 174 { 175 cleanup_chain = ptr->next; 176 free ((PTR)ptr); 177 } 178 } 179 180 /* Set the cleanup_chain to 0, and return the old cleanup chain. */ 181 struct cleanup * 182 save_cleanups () 183 { 184 struct cleanup *old_chain = cleanup_chain; 185 186 cleanup_chain = 0; 187 return old_chain; 188 } 189 190 /* Restore the cleanup chain from a previously saved chain. */ 191 void 192 restore_cleanups (chain) 193 struct cleanup *chain; 194 { 195 cleanup_chain = chain; 196 } 197 198 /* This function is useful for cleanups. 199 Do 200 201 foo = xmalloc (...); 202 old_chain = make_cleanup (free_current_contents, &foo); 203 204 to arrange to free the object thus allocated. */ 205 206 void 207 free_current_contents (location) 208 char **location; 209 { 210 free (*location); 211 } 212 213 /* Provide a known function that does nothing, to use as a base for 214 for a possibly long chain of cleanups. This is useful where we 215 use the cleanup chain for handling normal cleanups as well as dealing 216 with cleanups that need to be done as a result of a call to error(). 217 In such cases, we may not be certain where the first cleanup is, unless 218 we have a do-nothing one to always use as the base. */ 219 220 /* ARGSUSED */ 221 void 222 null_cleanup (arg) 223 PTR arg; 224 { 225 } 226 227 228 /* Print a warning message. Way to use this is to call warning_begin, 229 output the warning message (use unfiltered output to gdb_stderr), 230 ending in a newline. There is not currently a warning_end that you 231 call afterwards, but such a thing might be added if it is useful 232 for a GUI to separate warning messages from other output. 233 234 FIXME: Why do warnings use unfiltered output and errors filtered? 235 Is this anything other than a historical accident? */ 236 237 void 238 warning_begin () 239 { 240 target_terminal_ours (); 241 wrap_here(""); /* Force out any buffered output */ 242 gdb_flush (gdb_stdout); 243 if (warning_pre_print) 244 fprintf_unfiltered (gdb_stderr, warning_pre_print); 245 } 246 247 /* Print a warning message. 248 The first argument STRING is the warning message, used as a fprintf string, 249 and the remaining args are passed as arguments to it. 250 The primary difference between warnings and errors is that a warning 251 does not force the return to command level. */ 252 253 /* VARARGS */ 254 void 255 #ifdef ANSI_PROTOTYPES 256 warning (char *string, ...) 257 #else 258 warning (va_alist) 259 va_dcl 260 #endif 261 { 262 va_list args; 263 #ifdef ANSI_PROTOTYPES 264 va_start (args, string); 265 #else 266 char *string; 267 268 va_start (args); 269 string = va_arg (args, char *); 270 #endif 271 warning_begin (); 272 vfprintf_unfiltered (gdb_stderr, string, args); 273 fprintf_unfiltered (gdb_stderr, "\n"); 274 va_end (args); 275 } 276 277 /* Start the printing of an error message. Way to use this is to call 278 this, output the error message (use filtered output to gdb_stderr 279 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending 280 in a newline, and then call return_to_top_level (RETURN_ERROR). 281 error() provides a convenient way to do this for the special case 282 that the error message can be formatted with a single printf call, 283 but this is more general. */ 284 void 285 error_begin () 286 { 287 target_terminal_ours (); 288 wrap_here (""); /* Force out any buffered output */ 289 gdb_flush (gdb_stdout); 290 291 annotate_error_begin (); 292 293 if (error_pre_print) 294 fprintf_filtered (gdb_stderr, error_pre_print); 295 } 296 297 /* Print an error message and return to command level. 298 The first argument STRING is the error message, used as a fprintf string, 299 and the remaining args are passed as arguments to it. */ 300 301 #ifdef ANSI_PROTOTYPES 302 NORETURN void 303 error (char *string, ...) 304 #else 305 void 306 error (va_alist) 307 va_dcl 308 #endif 309 { 310 va_list args; 311 #ifdef ANSI_PROTOTYPES 312 va_start (args, string); 313 #else 314 va_start (args); 315 #endif 316 if (error_hook) 317 (*error_hook) (); 318 else 319 { 320 error_begin (); 321 #ifdef ANSI_PROTOTYPES 322 vfprintf_filtered (gdb_stderr, string, args); 323 #else 324 { 325 char *string1; 326 327 string1 = va_arg (args, char *); 328 vfprintf_filtered (gdb_stderr, string1, args); 329 } 330 #endif 331 fprintf_filtered (gdb_stderr, "\n"); 332 va_end (args); 333 return_to_top_level (RETURN_ERROR); 334 } 335 } 336 337 338 /* Print an error message and exit reporting failure. 339 This is for a error that we cannot continue from. 340 The arguments are printed a la printf. 341 342 This function cannot be declared volatile (NORETURN) in an 343 ANSI environment because exit() is not declared volatile. */ 344 345 /* VARARGS */ 346 NORETURN void 347 #ifdef ANSI_PROTOTYPES 348 fatal (char *string, ...) 349 #else 350 fatal (va_alist) 351 va_dcl 352 #endif 353 { 354 va_list args; 355 #ifdef ANSI_PROTOTYPES 356 va_start (args, string); 357 #else 358 char *string; 359 va_start (args); 360 string = va_arg (args, char *); 361 #endif 362 fprintf_unfiltered (gdb_stderr, "\ngdb: "); 363 vfprintf_unfiltered (gdb_stderr, string, args); 364 fprintf_unfiltered (gdb_stderr, "\n"); 365 va_end (args); 366 exit (1); 367 } 368 369 /* Print an error message and exit, dumping core. 370 The arguments are printed a la printf (). */ 371 372 /* VARARGS */ 373 static void 374 #ifdef ANSI_PROTOTYPES 375 fatal_dump_core (char *string, ...) 376 #else 377 fatal_dump_core (va_alist) 378 va_dcl 379 #endif 380 { 381 va_list args; 382 #ifdef ANSI_PROTOTYPES 383 va_start (args, string); 384 #else 385 char *string; 386 387 va_start (args); 388 string = va_arg (args, char *); 389 #endif 390 /* "internal error" is always correct, since GDB should never dump 391 core, no matter what the input. */ 392 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: "); 393 vfprintf_unfiltered (gdb_stderr, string, args); 394 fprintf_unfiltered (gdb_stderr, "\n"); 395 va_end (args); 396 397 #ifndef _WIN32 398 signal (SIGQUIT, SIG_DFL); 399 kill (getpid (), SIGQUIT); 400 #endif 401 /* We should never get here, but just in case... */ 402 exit (1); 403 } 404 405 /* The strerror() function can return NULL for errno values that are 406 out of range. Provide a "safe" version that always returns a 407 printable string. */ 408 409 char * 410 safe_strerror (errnum) 411 int errnum; 412 { 413 char *msg; 414 static char buf[32]; 415 416 if ((msg = strerror (errnum)) == NULL) 417 { 418 sprintf (buf, "(undocumented errno %d)", errnum); 419 msg = buf; 420 } 421 return (msg); 422 } 423 424 /* The strsignal() function can return NULL for signal values that are 425 out of range. Provide a "safe" version that always returns a 426 printable string. */ 427 428 char * 429 safe_strsignal (signo) 430 int signo; 431 { 432 char *msg; 433 static char buf[32]; 434 435 if ((msg = strsignal (signo)) == NULL) 436 { 437 sprintf (buf, "(undocumented signal %d)", signo); 438 msg = buf; 439 } 440 return (msg); 441 } 442 443 444 /* Print the system error message for errno, and also mention STRING 445 as the file name for which the error was encountered. 446 Then return to command level. */ 447 448 void 449 perror_with_name (string) 450 char *string; 451 { 452 char *err; 453 char *combined; 454 455 err = safe_strerror (errno); 456 combined = (char *) alloca (strlen (err) + strlen (string) + 3); 457 strcpy (combined, string); 458 strcat (combined, ": "); 459 strcat (combined, err); 460 461 /* I understand setting these is a matter of taste. Still, some people 462 may clear errno but not know about bfd_error. Doing this here is not 463 unreasonable. */ 464 bfd_set_error (bfd_error_no_error); 465 errno = 0; 466 467 error ("%s.", combined); 468 } 469 470 /* Print the system error message for ERRCODE, and also mention STRING 471 as the file name for which the error was encountered. */ 472 473 void 474 print_sys_errmsg (string, errcode) 475 char *string; 476 int errcode; 477 { 478 char *err; 479 char *combined; 480 481 err = safe_strerror (errcode); 482 combined = (char *) alloca (strlen (err) + strlen (string) + 3); 483 strcpy (combined, string); 484 strcat (combined, ": "); 485 strcat (combined, err); 486 487 /* We want anything which was printed on stdout to come out first, before 488 this message. */ 489 gdb_flush (gdb_stdout); 490 fprintf_unfiltered (gdb_stderr, "%s.\n", combined); 491 } 492 493 /* Control C eventually causes this to be called, at a convenient time. */ 494 495 void 496 quit () 497 { 498 serial_t gdb_stdout_serial = serial_fdopen (1); 499 500 target_terminal_ours (); 501 502 /* We want all output to appear now, before we print "Quit". We 503 have 3 levels of buffering we have to flush (it's possible that 504 some of these should be changed to flush the lower-level ones 505 too): */ 506 507 /* 1. The _filtered buffer. */ 508 wrap_here ((char *)0); 509 510 /* 2. The stdio buffer. */ 511 gdb_flush (gdb_stdout); 512 gdb_flush (gdb_stderr); 513 514 /* 3. The system-level buffer. */ 515 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial); 516 SERIAL_UN_FDOPEN (gdb_stdout_serial); 517 518 annotate_error_begin (); 519 520 /* Don't use *_filtered; we don't want to prompt the user to continue. */ 521 if (quit_pre_print) 522 fprintf_unfiltered (gdb_stderr, quit_pre_print); 523 524 if (job_control 525 /* If there is no terminal switching for this target, then we can't 526 possibly get screwed by the lack of job control. */ 527 || current_target.to_terminal_ours == NULL) 528 fprintf_unfiltered (gdb_stderr, "Quit\n"); 529 else 530 fprintf_unfiltered (gdb_stderr, 531 "Quit (expect signal SIGINT when the program is resumed)\n"); 532 return_to_top_level (RETURN_QUIT); 533 } 534 535 536 #if defined(__GO32__) || defined(_WIN32) 537 538 /* In the absence of signals, poll keyboard for a quit. 539 Called from #define QUIT pollquit() in xm-go32.h. */ 540 541 void 542 pollquit() 543 { 544 if (kbhit ()) 545 { 546 #ifndef _WIN32 547 int k = getkey (); 548 if (k == 1) { 549 quit_flag = 1; 550 quit(); 551 } 552 else if (k == 2) { 553 immediate_quit = 1; 554 quit (); 555 } 556 else 557 { 558 /* We just ignore it */ 559 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n"); 560 } 561 #else 562 abort (); 563 #endif 564 } 565 } 566 567 568 #endif 569 #if defined(__GO32__) || defined(_WIN32) 570 void notice_quit() 571 { 572 if (kbhit ()) 573 { 574 #ifndef _WIN32 575 int k = getkey (); 576 if (k == 1) { 577 quit_flag = 1; 578 } 579 else if (k == 2) 580 { 581 immediate_quit = 1; 582 } 583 else 584 { 585 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n"); 586 } 587 #else 588 abort (); 589 #endif 590 } 591 } 592 #else 593 void notice_quit() 594 { 595 /* Done by signals */ 596 } 597 #endif 598 /* Control C comes here */ 599 600 void 601 request_quit (signo) 602 int signo; 603 { 604 quit_flag = 1; 605 /* Restore the signal handler. Harmless with BSD-style signals, needed 606 for System V-style signals. So just always do it, rather than worrying 607 about USG defines and stuff like that. */ 608 signal (signo, request_quit); 609 610 611 #ifdef REQUEST_QUIT 612 REQUEST_QUIT; 613 #else 614 if (immediate_quit) 615 quit (); 616 #endif 617 } 618 619 620 /* Memory management stuff (malloc friends). */ 621 622 /* Make a substitute size_t for non-ANSI compilers. */ 623 624 #ifndef HAVE_STDDEF_H 625 #ifndef size_t 626 #define size_t unsigned int 627 #endif 628 #endif 629 630 #if defined (NO_MMALLOC) 631 632 PTR 633 mmalloc (md, size) 634 PTR md; 635 size_t size; 636 { 637 return malloc (size); 638 } 639 640 PTR 641 mrealloc (md, ptr, size) 642 PTR md; 643 PTR ptr; 644 size_t size; 645 { 646 if (ptr == 0) /* Guard against old realloc's */ 647 return malloc (size); 648 else 649 return realloc (ptr, size); 650 } 651 652 void 653 mfree (md, ptr) 654 PTR md; 655 PTR ptr; 656 { 657 free (ptr); 658 } 659 660 #endif /* NO_MMALLOC */ 661 662 #if defined (NO_MMALLOC) || defined (NO_MMCHECK) 663 664 void 665 init_malloc (md) 666 PTR md; 667 { 668 } 669 670 #else /* Have mmalloc and want corruption checking */ 671 672 static void 673 malloc_botch () 674 { 675 fatal_dump_core ("Memory corruption"); 676 } 677 678 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified 679 by MD, to detect memory corruption. Note that MD may be NULL to specify 680 the default heap that grows via sbrk. 681 682 Note that for freshly created regions, we must call mmcheckf prior to any 683 mallocs in the region. Otherwise, any region which was allocated prior to 684 installing the checking hooks, which is later reallocated or freed, will 685 fail the checks! The mmcheck function only allows initial hooks to be 686 installed before the first mmalloc. However, anytime after we have called 687 mmcheck the first time to install the checking hooks, we can call it again 688 to update the function pointer to the memory corruption handler. 689 690 Returns zero on failure, non-zero on success. */ 691 692 #ifndef MMCHECK_FORCE 693 #define MMCHECK_FORCE 0 694 #endif 695 696 void 697 init_malloc (md) 698 PTR md; 699 { 700 if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE)) 701 { 702 /* Don't use warning(), which relies on current_target being set 703 to something other than dummy_target, until after 704 initialize_all_files(). */ 705 706 fprintf_unfiltered 707 (gdb_stderr, "warning: failed to install memory consistency checks; "); 708 fprintf_unfiltered 709 (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n"); 710 } 711 712 mmtrace (); 713 } 714 715 #endif /* Have mmalloc and want corruption checking */ 716 717 /* Called when a memory allocation fails, with the number of bytes of 718 memory requested in SIZE. */ 719 720 NORETURN void 721 nomem (size) 722 long size; 723 { 724 if (size > 0) 725 { 726 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size); 727 } 728 else 729 { 730 fatal ("virtual memory exhausted."); 731 } 732 } 733 734 /* Like mmalloc but get error if no storage available, and protect against 735 the caller wanting to allocate zero bytes. Whether to return NULL for 736 a zero byte request, or translate the request into a request for one 737 byte of zero'd storage, is a religious issue. */ 738 739 PTR 740 xmmalloc (md, size) 741 PTR md; 742 long size; 743 { 744 register PTR val; 745 746 if (size == 0) 747 { 748 val = NULL; 749 } 750 else if ((val = mmalloc (md, size)) == NULL) 751 { 752 nomem (size); 753 } 754 return (val); 755 } 756 757 /* Like mrealloc but get error if no storage available. */ 758 759 PTR 760 xmrealloc (md, ptr, size) 761 PTR md; 762 PTR ptr; 763 long size; 764 { 765 register PTR val; 766 767 if (ptr != NULL) 768 { 769 val = mrealloc (md, ptr, size); 770 } 771 else 772 { 773 val = mmalloc (md, size); 774 } 775 if (val == NULL) 776 { 777 nomem (size); 778 } 779 return (val); 780 } 781 782 /* Like malloc but get error if no storage available, and protect against 783 the caller wanting to allocate zero bytes. */ 784 785 #ifndef NO_XMALLOC 786 PTR 787 xmalloc (size) 788 size_t size; 789 { 790 return (xmmalloc ((PTR) NULL, size)); 791 } 792 793 PTR 794 xcalloc (nelem, elsize) 795 size_t nelem, elsize; 796 { 797 PTR newmem; 798 799 if (nelem == 0 || elsize == 0) 800 nelem = elsize = 1; 801 802 newmem = xmmalloc((PTR) NULL, nelem * elsize); 803 memset(newmem, 0, nelem * elsize); 804 805 return (newmem); 806 } 807 808 /* Like mrealloc but get error if no storage available. */ 809 810 PTR 811 xrealloc (ptr, size) 812 PTR ptr; 813 size_t size; 814 { 815 return (xmrealloc ((PTR) NULL, ptr, size)); 816 } 817 #endif 818 819 820 /* My replacement for the read system call. 821 Used like `read' but keeps going if `read' returns too soon. */ 822 823 int 824 myread (desc, addr, len) 825 int desc; 826 char *addr; 827 int len; 828 { 829 register int val; 830 int orglen = len; 831 832 while (len > 0) 833 { 834 val = read (desc, addr, len); 835 if (val < 0) 836 return val; 837 if (val == 0) 838 return orglen - len; 839 len -= val; 840 addr += val; 841 } 842 return orglen; 843 } 844 845 /* Make a copy of the string at PTR with SIZE characters 846 (and add a null character at the end in the copy). 847 Uses malloc to get the space. Returns the address of the copy. */ 848 849 char * 850 savestring (ptr, size) 851 const char *ptr; 852 int size; 853 { 854 register char *p = (char *) xmalloc (size + 1); 855 memcpy (p, ptr, size); 856 p[size] = 0; 857 return p; 858 } 859 860 char * 861 msavestring (md, ptr, size) 862 PTR md; 863 const char *ptr; 864 int size; 865 { 866 register char *p = (char *) xmmalloc (md, size + 1); 867 memcpy (p, ptr, size); 868 p[size] = 0; 869 return p; 870 } 871 872 /* The "const" is so it compiles under DGUX (which prototypes strsave 873 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it? 874 Doesn't real strsave return NULL if out of memory? */ 875 char * 876 strsave (ptr) 877 const char *ptr; 878 { 879 return savestring (ptr, strlen (ptr)); 880 } 881 882 char * 883 mstrsave (md, ptr) 884 PTR md; 885 const char *ptr; 886 { 887 return (msavestring (md, ptr, strlen (ptr))); 888 } 889 890 void 891 print_spaces (n, file) 892 register int n; 893 register FILE *file; 894 { 895 while (n-- > 0) 896 fputc (' ', file); 897 } 898 899 /* Print a host address. */ 900 901 void 902 gdb_print_address (addr, stream) 903 PTR addr; 904 GDB_FILE *stream; 905 { 906 907 /* We could use the %p conversion specifier to fprintf if we had any 908 way of knowing whether this host supports it. But the following 909 should work on the Alpha and on 32 bit machines. */ 910 911 fprintf_filtered (stream, "0x%lx", (unsigned long)addr); 912 } 913 914 /* Ask user a y-or-n question and return 1 iff answer is yes. 915 Takes three args which are given to printf to print the question. 916 The first, a control string, should end in "? ". 917 It should not say how to answer, because we do that. */ 918 919 /* VARARGS */ 920 int 921 #ifdef ANSI_PROTOTYPES 922 query (char *ctlstr, ...) 923 #else 924 query (va_alist) 925 va_dcl 926 #endif 927 { 928 va_list args; 929 register int answer; 930 register int ans2; 931 int retval; 932 933 #ifdef ANSI_PROTOTYPES 934 va_start (args, ctlstr); 935 #else 936 char *ctlstr; 937 va_start (args); 938 ctlstr = va_arg (args, char *); 939 #endif 940 941 if (query_hook) 942 { 943 return query_hook (ctlstr, args); 944 } 945 946 /* Automatically answer "yes" if input is not from a terminal. */ 947 if (!input_from_terminal_p ()) 948 return 1; 949 #ifdef MPW 950 /* FIXME Automatically answer "yes" if called from MacGDB. */ 951 if (mac_app) 952 return 1; 953 #endif /* MPW */ 954 955 while (1) 956 { 957 wrap_here (""); /* Flush any buffered output */ 958 gdb_flush (gdb_stdout); 959 960 if (annotation_level > 1) 961 printf_filtered ("\n\032\032pre-query\n"); 962 963 vfprintf_filtered (gdb_stdout, ctlstr, args); 964 printf_filtered ("(y or n) "); 965 966 if (annotation_level > 1) 967 printf_filtered ("\n\032\032query\n"); 968 969 #ifdef MPW 970 /* If not in MacGDB, move to a new line so the entered line doesn't 971 have a prompt on the front of it. */ 972 if (!mac_app) 973 fputs_unfiltered ("\n", gdb_stdout); 974 #endif /* MPW */ 975 976 gdb_flush (gdb_stdout); 977 answer = fgetc (stdin); 978 clearerr (stdin); /* in case of C-d */ 979 if (answer == EOF) /* C-d */ 980 { 981 retval = 1; 982 break; 983 } 984 if (answer != '\n') /* Eat rest of input line, to EOF or newline */ 985 do 986 { 987 ans2 = fgetc (stdin); 988 clearerr (stdin); 989 } 990 while (ans2 != EOF && ans2 != '\n'); 991 if (answer >= 'a') 992 answer -= 040; 993 if (answer == 'Y') 994 { 995 retval = 1; 996 break; 997 } 998 if (answer == 'N') 999 { 1000 retval = 0; 1001 break; 1002 } 1003 printf_filtered ("Please answer y or n.\n"); 1004 } 1005 1006 if (annotation_level > 1) 1007 printf_filtered ("\n\032\032post-query\n"); 1008 return retval; 1009 } 1010 1011 1012 /* Parse a C escape sequence. STRING_PTR points to a variable 1013 containing a pointer to the string to parse. That pointer 1014 should point to the character after the \. That pointer 1015 is updated past the characters we use. The value of the 1016 escape sequence is returned. 1017 1018 A negative value means the sequence \ newline was seen, 1019 which is supposed to be equivalent to nothing at all. 1020 1021 If \ is followed by a null character, we return a negative 1022 value and leave the string pointer pointing at the null character. 1023 1024 If \ is followed by 000, we return 0 and leave the string pointer 1025 after the zeros. A value of 0 does not mean end of string. */ 1026 1027 int 1028 parse_escape (string_ptr) 1029 char **string_ptr; 1030 { 1031 register int c = *(*string_ptr)++; 1032 switch (c) 1033 { 1034 case 'a': 1035 return 007; /* Bell (alert) char */ 1036 case 'b': 1037 return '\b'; 1038 case 'e': /* Escape character */ 1039 return 033; 1040 case 'f': 1041 return '\f'; 1042 case 'n': 1043 return '\n'; 1044 case 'r': 1045 return '\r'; 1046 case 't': 1047 return '\t'; 1048 case 'v': 1049 return '\v'; 1050 case '\n': 1051 return -2; 1052 case 0: 1053 (*string_ptr)--; 1054 return 0; 1055 case '^': 1056 c = *(*string_ptr)++; 1057 if (c == '\\') 1058 c = parse_escape (string_ptr); 1059 if (c == '?') 1060 return 0177; 1061 return (c & 0200) | (c & 037); 1062 1063 case '0': 1064 case '1': 1065 case '2': 1066 case '3': 1067 case '4': 1068 case '5': 1069 case '6': 1070 case '7': 1071 { 1072 register int i = c - '0'; 1073 register int count = 0; 1074 while (++count < 3) 1075 { 1076 if ((c = *(*string_ptr)++) >= '0' && c <= '7') 1077 { 1078 i *= 8; 1079 i += c - '0'; 1080 } 1081 else 1082 { 1083 (*string_ptr)--; 1084 break; 1085 } 1086 } 1087 return i; 1088 } 1089 default: 1090 return c; 1091 } 1092 } 1093 1094 /* Print the character C on STREAM as part of the contents of a literal 1095 string whose delimiter is QUOTER. Note that this routine should only 1096 be call for printing things which are independent of the language 1097 of the program being debugged. */ 1098 1099 void 1100 gdb_printchar (c, stream, quoter) 1101 register int c; 1102 FILE *stream; 1103 int quoter; 1104 { 1105 1106 c &= 0xFF; /* Avoid sign bit follies */ 1107 1108 if ( c < 0x20 || /* Low control chars */ 1109 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ 1110 (sevenbit_strings && c >= 0x80)) { /* high order bit set */ 1111 switch (c) 1112 { 1113 case '\n': 1114 fputs_filtered ("\\n", stream); 1115 break; 1116 case '\b': 1117 fputs_filtered ("\\b", stream); 1118 break; 1119 case '\t': 1120 fputs_filtered ("\\t", stream); 1121 break; 1122 case '\f': 1123 fputs_filtered ("\\f", stream); 1124 break; 1125 case '\r': 1126 fputs_filtered ("\\r", stream); 1127 break; 1128 case '\033': 1129 fputs_filtered ("\\e", stream); 1130 break; 1131 case '\007': 1132 fputs_filtered ("\\a", stream); 1133 break; 1134 default: 1135 fprintf_filtered (stream, "\\%.3o", (unsigned int) c); 1136 break; 1137 } 1138 } else { 1139 if (c == '\\' || c == quoter) 1140 fputs_filtered ("\\", stream); 1141 fprintf_filtered (stream, "%c", c); 1142 } 1143 } 1144 1145 /* Number of lines per page or UINT_MAX if paging is disabled. */ 1146 static unsigned int lines_per_page; 1147 /* Number of chars per line or UNIT_MAX is line folding is disabled. */ 1148 static unsigned int chars_per_line; 1149 /* Current count of lines printed on this page, chars on this line. */ 1150 static unsigned int lines_printed, chars_printed; 1151 1152 /* Buffer and start column of buffered text, for doing smarter word- 1153 wrapping. When someone calls wrap_here(), we start buffering output 1154 that comes through fputs_filtered(). If we see a newline, we just 1155 spit it out and forget about the wrap_here(). If we see another 1156 wrap_here(), we spit it out and remember the newer one. If we see 1157 the end of the line, we spit out a newline, the indent, and then 1158 the buffered output. */ 1159 1160 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which 1161 are waiting to be output (they have already been counted in chars_printed). 1162 When wrap_buffer[0] is null, the buffer is empty. */ 1163 static char *wrap_buffer; 1164 1165 /* Pointer in wrap_buffer to the next character to fill. */ 1166 static char *wrap_pointer; 1167 1168 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column 1169 is non-zero. */ 1170 static char *wrap_indent; 1171 1172 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping 1173 is not in effect. */ 1174 static int wrap_column; 1175 1176 /* ARGSUSED */ 1177 static void 1178 set_width_command (args, from_tty, c) 1179 char *args; 1180 int from_tty; 1181 struct cmd_list_element *c; 1182 { 1183 if (!wrap_buffer) 1184 { 1185 wrap_buffer = (char *) xmalloc (chars_per_line + 2); 1186 wrap_buffer[0] = '\0'; 1187 } 1188 else 1189 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); 1190 wrap_pointer = wrap_buffer; /* Start it at the beginning */ 1191 } 1192 1193 /* Wait, so the user can read what's on the screen. Prompt the user 1194 to continue by pressing RETURN. */ 1195 1196 static void 1197 prompt_for_continue () 1198 { 1199 char *ignore; 1200 char cont_prompt[120]; 1201 1202 if (annotation_level > 1) 1203 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n"); 1204 1205 strcpy (cont_prompt, 1206 "---Type <return> to continue, or q <return> to quit---"); 1207 if (annotation_level > 1) 1208 strcat (cont_prompt, "\n\032\032prompt-for-continue\n"); 1209 1210 /* We must do this *before* we call gdb_readline, else it will eventually 1211 call us -- thinking that we're trying to print beyond the end of the 1212 screen. */ 1213 reinitialize_more_filter (); 1214 1215 immediate_quit++; 1216 /* On a real operating system, the user can quit with SIGINT. 1217 But not on GO32. 1218 1219 'q' is provided on all systems so users don't have to change habits 1220 from system to system, and because telling them what to do in 1221 the prompt is more user-friendly than expecting them to think of 1222 SIGINT. */ 1223 /* Call readline, not gdb_readline, because GO32 readline handles control-C 1224 whereas control-C to gdb_readline will cause the user to get dumped 1225 out to DOS. */ 1226 ignore = readline (cont_prompt); 1227 1228 if (annotation_level > 1) 1229 printf_unfiltered ("\n\032\032post-prompt-for-continue\n"); 1230 1231 if (ignore) 1232 { 1233 char *p = ignore; 1234 while (*p == ' ' || *p == '\t') 1235 ++p; 1236 if (p[0] == 'q') 1237 request_quit (SIGINT); 1238 free (ignore); 1239 } 1240 immediate_quit--; 1241 1242 /* Now we have to do this again, so that GDB will know that it doesn't 1243 need to save the ---Type <return>--- line at the top of the screen. */ 1244 reinitialize_more_filter (); 1245 1246 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ 1247 } 1248 1249 /* Reinitialize filter; ie. tell it to reset to original values. */ 1250 1251 void 1252 reinitialize_more_filter () 1253 { 1254 lines_printed = 0; 1255 chars_printed = 0; 1256 } 1257 1258 /* Indicate that if the next sequence of characters overflows the line, 1259 a newline should be inserted here rather than when it hits the end. 1260 If INDENT is non-null, it is a string to be printed to indent the 1261 wrapped part on the next line. INDENT must remain accessible until 1262 the next call to wrap_here() or until a newline is printed through 1263 fputs_filtered(). 1264 1265 If the line is already overfull, we immediately print a newline and 1266 the indentation, and disable further wrapping. 1267 1268 If we don't know the width of lines, but we know the page height, 1269 we must not wrap words, but should still keep track of newlines 1270 that were explicitly printed. 1271 1272 INDENT should not contain tabs, as that will mess up the char count 1273 on the next line. FIXME. 1274 1275 This routine is guaranteed to force out any output which has been 1276 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be 1277 used to force out output from the wrap_buffer. */ 1278 1279 void 1280 wrap_here(indent) 1281 char *indent; 1282 { 1283 /* This should have been allocated, but be paranoid anyway. */ 1284 if (!wrap_buffer) 1285 abort (); 1286 1287 if (wrap_buffer[0]) 1288 { 1289 *wrap_pointer = '\0'; 1290 fputs_unfiltered (wrap_buffer, gdb_stdout); 1291 } 1292 wrap_pointer = wrap_buffer; 1293 wrap_buffer[0] = '\0'; 1294 if (chars_per_line == UINT_MAX) /* No line overflow checking */ 1295 { 1296 wrap_column = 0; 1297 } 1298 else if (chars_printed >= chars_per_line) 1299 { 1300 puts_filtered ("\n"); 1301 if (indent != NULL) 1302 puts_filtered (indent); 1303 wrap_column = 0; 1304 } 1305 else 1306 { 1307 wrap_column = chars_printed; 1308 if (indent == NULL) 1309 wrap_indent = ""; 1310 else 1311 wrap_indent = indent; 1312 } 1313 } 1314 1315 /* Ensure that whatever gets printed next, using the filtered output 1316 commands, starts at the beginning of the line. I.E. if there is 1317 any pending output for the current line, flush it and start a new 1318 line. Otherwise do nothing. */ 1319 1320 void 1321 begin_line () 1322 { 1323 if (chars_printed > 0) 1324 { 1325 puts_filtered ("\n"); 1326 } 1327 } 1328 1329 1330 GDB_FILE * 1331 gdb_fopen (name, mode) 1332 char * name; 1333 char * mode; 1334 { 1335 return fopen (name, mode); 1336 } 1337 1338 void 1339 gdb_flush (stream) 1340 FILE *stream; 1341 { 1342 if (flush_hook) 1343 { 1344 flush_hook (stream); 1345 return; 1346 } 1347 1348 fflush (stream); 1349 } 1350 1351 /* Like fputs but if FILTER is true, pause after every screenful. 1352 1353 Regardless of FILTER can wrap at points other than the final 1354 character of a line. 1355 1356 Unlike fputs, fputs_maybe_filtered does not return a value. 1357 It is OK for LINEBUFFER to be NULL, in which case just don't print 1358 anything. 1359 1360 Note that a longjmp to top level may occur in this routine (only if 1361 FILTER is true) (since prompt_for_continue may do so) so this 1362 routine should not be called when cleanups are not in place. */ 1363 1364 static void 1365 fputs_maybe_filtered (linebuffer, stream, filter) 1366 const char *linebuffer; 1367 FILE *stream; 1368 int filter; 1369 { 1370 const char *lineptr; 1371 1372 if (linebuffer == 0) 1373 return; 1374 1375 /* Don't do any filtering if it is disabled. */ 1376 if (stream != gdb_stdout 1377 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) 1378 { 1379 fputs_unfiltered (linebuffer, stream); 1380 return; 1381 } 1382 1383 /* Go through and output each character. Show line extension 1384 when this is necessary; prompt user for new page when this is 1385 necessary. */ 1386 1387 lineptr = linebuffer; 1388 while (*lineptr) 1389 { 1390 /* Possible new page. */ 1391 if (filter && 1392 (lines_printed >= lines_per_page - 1)) 1393 prompt_for_continue (); 1394 1395 while (*lineptr && *lineptr != '\n') 1396 { 1397 /* Print a single line. */ 1398 if (*lineptr == '\t') 1399 { 1400 if (wrap_column) 1401 *wrap_pointer++ = '\t'; 1402 else 1403 fputc_unfiltered ('\t', stream); 1404 /* Shifting right by 3 produces the number of tab stops 1405 we have already passed, and then adding one and 1406 shifting left 3 advances to the next tab stop. */ 1407 chars_printed = ((chars_printed >> 3) + 1) << 3; 1408 lineptr++; 1409 } 1410 else 1411 { 1412 if (wrap_column) 1413 *wrap_pointer++ = *lineptr; 1414 else 1415 fputc_unfiltered (*lineptr, stream); 1416 chars_printed++; 1417 lineptr++; 1418 } 1419 1420 if (chars_printed >= chars_per_line) 1421 { 1422 unsigned int save_chars = chars_printed; 1423 1424 chars_printed = 0; 1425 lines_printed++; 1426 /* If we aren't actually wrapping, don't output newline -- 1427 if chars_per_line is right, we probably just overflowed 1428 anyway; if it's wrong, let us keep going. */ 1429 if (wrap_column) 1430 fputc_unfiltered ('\n', stream); 1431 1432 /* Possible new page. */ 1433 if (lines_printed >= lines_per_page - 1) 1434 prompt_for_continue (); 1435 1436 /* Now output indentation and wrapped string */ 1437 if (wrap_column) 1438 { 1439 fputs_unfiltered (wrap_indent, stream); 1440 *wrap_pointer = '\0'; /* Null-terminate saved stuff */ 1441 fputs_unfiltered (wrap_buffer, stream); /* and eject it */ 1442 /* FIXME, this strlen is what prevents wrap_indent from 1443 containing tabs. However, if we recurse to print it 1444 and count its chars, we risk trouble if wrap_indent is 1445 longer than (the user settable) chars_per_line. 1446 Note also that this can set chars_printed > chars_per_line 1447 if we are printing a long string. */ 1448 chars_printed = strlen (wrap_indent) 1449 + (save_chars - wrap_column); 1450 wrap_pointer = wrap_buffer; /* Reset buffer */ 1451 wrap_buffer[0] = '\0'; 1452 wrap_column = 0; /* And disable fancy wrap */ 1453 } 1454 } 1455 } 1456 1457 if (*lineptr == '\n') 1458 { 1459 chars_printed = 0; 1460 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */ 1461 lines_printed++; 1462 fputc_unfiltered ('\n', stream); 1463 lineptr++; 1464 } 1465 } 1466 } 1467 1468 void 1469 fputs_filtered (linebuffer, stream) 1470 const char *linebuffer; 1471 FILE *stream; 1472 { 1473 fputs_maybe_filtered (linebuffer, stream, 1); 1474 } 1475 1476 int 1477 putchar_unfiltered (c) 1478 int c; 1479 { 1480 char buf[2]; 1481 1482 buf[0] = c; 1483 buf[1] = 0; 1484 fputs_unfiltered (buf, gdb_stdout); 1485 return c; 1486 } 1487 1488 int 1489 fputc_unfiltered (c, stream) 1490 int c; 1491 FILE * stream; 1492 { 1493 char buf[2]; 1494 1495 buf[0] = c; 1496 buf[1] = 0; 1497 fputs_unfiltered (buf, stream); 1498 return c; 1499 } 1500 1501 1502 /* Print a variable number of ARGS using format FORMAT. If this 1503 information is going to put the amount written (since the last call 1504 to REINITIALIZE_MORE_FILTER or the last page break) over the page size, 1505 call prompt_for_continue to get the users permision to continue. 1506 1507 Unlike fprintf, this function does not return a value. 1508 1509 We implement three variants, vfprintf (takes a vararg list and stream), 1510 fprintf (takes a stream to write on), and printf (the usual). 1511 1512 Note also that a longjmp to top level may occur in this routine 1513 (since prompt_for_continue may do so) so this routine should not be 1514 called when cleanups are not in place. */ 1515 1516 static void 1517 vfprintf_maybe_filtered (stream, format, args, filter) 1518 FILE *stream; 1519 const char *format; 1520 va_list args; 1521 int filter; 1522 { 1523 char *linebuffer; 1524 struct cleanup *old_cleanups; 1525 1526 vasprintf (&linebuffer, format, args); 1527 if (linebuffer == NULL) 1528 { 1529 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr); 1530 exit (1); 1531 } 1532 old_cleanups = make_cleanup (free, linebuffer); 1533 fputs_maybe_filtered (linebuffer, stream, filter); 1534 do_cleanups (old_cleanups); 1535 } 1536 1537 1538 void 1539 vfprintf_filtered (stream, format, args) 1540 FILE *stream; 1541 const char *format; 1542 va_list args; 1543 { 1544 vfprintf_maybe_filtered (stream, format, args, 1); 1545 } 1546 1547 void 1548 vfprintf_unfiltered (stream, format, args) 1549 FILE *stream; 1550 const char *format; 1551 va_list args; 1552 { 1553 char *linebuffer; 1554 struct cleanup *old_cleanups; 1555 1556 vasprintf (&linebuffer, format, args); 1557 if (linebuffer == NULL) 1558 { 1559 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr); 1560 exit (1); 1561 } 1562 old_cleanups = make_cleanup (free, linebuffer); 1563 fputs_unfiltered (linebuffer, stream); 1564 do_cleanups (old_cleanups); 1565 } 1566 1567 void 1568 vprintf_filtered (format, args) 1569 const char *format; 1570 va_list args; 1571 { 1572 vfprintf_maybe_filtered (gdb_stdout, format, args, 1); 1573 } 1574 1575 void 1576 vprintf_unfiltered (format, args) 1577 const char *format; 1578 va_list args; 1579 { 1580 vfprintf_unfiltered (gdb_stdout, format, args); 1581 } 1582 1583 /* VARARGS */ 1584 void 1585 #ifdef ANSI_PROTOTYPES 1586 fprintf_filtered (FILE *stream, const char *format, ...) 1587 #else 1588 fprintf_filtered (va_alist) 1589 va_dcl 1590 #endif 1591 { 1592 va_list args; 1593 #ifdef ANSI_PROTOTYPES 1594 va_start (args, format); 1595 #else 1596 FILE *stream; 1597 char *format; 1598 1599 va_start (args); 1600 stream = va_arg (args, FILE *); 1601 format = va_arg (args, char *); 1602 #endif 1603 vfprintf_filtered (stream, format, args); 1604 va_end (args); 1605 } 1606 1607 /* VARARGS */ 1608 void 1609 #ifdef ANSI_PROTOTYPES 1610 fprintf_unfiltered (FILE *stream, const char *format, ...) 1611 #else 1612 fprintf_unfiltered (va_alist) 1613 va_dcl 1614 #endif 1615 { 1616 va_list args; 1617 #ifdef ANSI_PROTOTYPES 1618 va_start (args, format); 1619 #else 1620 FILE *stream; 1621 char *format; 1622 1623 va_start (args); 1624 stream = va_arg (args, FILE *); 1625 format = va_arg (args, char *); 1626 #endif 1627 vfprintf_unfiltered (stream, format, args); 1628 va_end (args); 1629 } 1630 1631 /* Like fprintf_filtered, but prints its result indented. 1632 Called as fprintfi_filtered (spaces, stream, format, ...); */ 1633 1634 /* VARARGS */ 1635 void 1636 #ifdef ANSI_PROTOTYPES 1637 fprintfi_filtered (int spaces, FILE *stream, const char *format, ...) 1638 #else 1639 fprintfi_filtered (va_alist) 1640 va_dcl 1641 #endif 1642 { 1643 va_list args; 1644 #ifdef ANSI_PROTOTYPES 1645 va_start (args, format); 1646 #else 1647 int spaces; 1648 FILE *stream; 1649 char *format; 1650 1651 va_start (args); 1652 spaces = va_arg (args, int); 1653 stream = va_arg (args, FILE *); 1654 format = va_arg (args, char *); 1655 #endif 1656 print_spaces_filtered (spaces, stream); 1657 1658 vfprintf_filtered (stream, format, args); 1659 va_end (args); 1660 } 1661 1662 1663 /* VARARGS */ 1664 void 1665 #ifdef ANSI_PROTOTYPES 1666 printf_filtered (const char *format, ...) 1667 #else 1668 printf_filtered (va_alist) 1669 va_dcl 1670 #endif 1671 { 1672 va_list args; 1673 #ifdef ANSI_PROTOTYPES 1674 va_start (args, format); 1675 #else 1676 char *format; 1677 1678 va_start (args); 1679 format = va_arg (args, char *); 1680 #endif 1681 vfprintf_filtered (gdb_stdout, format, args); 1682 va_end (args); 1683 } 1684 1685 1686 /* VARARGS */ 1687 void 1688 #ifdef ANSI_PROTOTYPES 1689 printf_unfiltered (const char *format, ...) 1690 #else 1691 printf_unfiltered (va_alist) 1692 va_dcl 1693 #endif 1694 { 1695 va_list args; 1696 #ifdef ANSI_PROTOTYPES 1697 va_start (args, format); 1698 #else 1699 char *format; 1700 1701 va_start (args); 1702 format = va_arg (args, char *); 1703 #endif 1704 vfprintf_unfiltered (gdb_stdout, format, args); 1705 va_end (args); 1706 } 1707 1708 /* Like printf_filtered, but prints it's result indented. 1709 Called as printfi_filtered (spaces, format, ...); */ 1710 1711 /* VARARGS */ 1712 void 1713 #ifdef ANSI_PROTOTYPES 1714 printfi_filtered (int spaces, const char *format, ...) 1715 #else 1716 printfi_filtered (va_alist) 1717 va_dcl 1718 #endif 1719 { 1720 va_list args; 1721 #ifdef ANSI_PROTOTYPES 1722 va_start (args, format); 1723 #else 1724 int spaces; 1725 char *format; 1726 1727 va_start (args); 1728 spaces = va_arg (args, int); 1729 format = va_arg (args, char *); 1730 #endif 1731 print_spaces_filtered (spaces, gdb_stdout); 1732 vfprintf_filtered (gdb_stdout, format, args); 1733 va_end (args); 1734 } 1735 1736 /* Easy -- but watch out! 1737 1738 This routine is *not* a replacement for puts()! puts() appends a newline. 1739 This one doesn't, and had better not! */ 1740 1741 void 1742 puts_filtered (string) 1743 const char *string; 1744 { 1745 fputs_filtered (string, gdb_stdout); 1746 } 1747 1748 void 1749 puts_unfiltered (string) 1750 const char *string; 1751 { 1752 fputs_unfiltered (string, gdb_stdout); 1753 } 1754 1755 /* Return a pointer to N spaces and a null. The pointer is good 1756 until the next call to here. */ 1757 char * 1758 n_spaces (n) 1759 int n; 1760 { 1761 register char *t; 1762 static char *spaces; 1763 static int max_spaces; 1764 1765 if (n > max_spaces) 1766 { 1767 if (spaces) 1768 free (spaces); 1769 spaces = (char *) xmalloc (n+1); 1770 for (t = spaces+n; t != spaces;) 1771 *--t = ' '; 1772 spaces[n] = '\0'; 1773 max_spaces = n; 1774 } 1775 1776 return spaces + max_spaces - n; 1777 } 1778 1779 /* Print N spaces. */ 1780 void 1781 print_spaces_filtered (n, stream) 1782 int n; 1783 FILE *stream; 1784 { 1785 fputs_filtered (n_spaces (n), stream); 1786 } 1787 1788 /* C++ demangler stuff. */ 1789 1790 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language 1791 LANG, using demangling args ARG_MODE, and print it filtered to STREAM. 1792 If the name is not mangled, or the language for the name is unknown, or 1793 demangling is off, the name is printed in its "raw" form. */ 1794 1795 void 1796 fprintf_symbol_filtered (stream, name, lang, arg_mode) 1797 FILE *stream; 1798 char *name; 1799 enum language lang; 1800 int arg_mode; 1801 { 1802 char *demangled; 1803 1804 if (name != NULL) 1805 { 1806 /* If user wants to see raw output, no problem. */ 1807 if (!demangle) 1808 { 1809 fputs_filtered (name, stream); 1810 } 1811 else 1812 { 1813 switch (lang) 1814 { 1815 case language_cplus: 1816 demangled = cplus_demangle (name, arg_mode); 1817 break; 1818 case language_chill: 1819 demangled = chill_demangle (name); 1820 break; 1821 default: 1822 demangled = NULL; 1823 break; 1824 } 1825 fputs_filtered (demangled ? demangled : name, stream); 1826 if (demangled != NULL) 1827 { 1828 free (demangled); 1829 } 1830 } 1831 } 1832 } 1833 1834 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any 1835 differences in whitespace. Returns 0 if they match, non-zero if they 1836 don't (slightly different than strcmp()'s range of return values). 1837 1838 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". 1839 This "feature" is useful when searching for matching C++ function names 1840 (such as if the user types 'break FOO', where FOO is a mangled C++ 1841 function). */ 1842 1843 int 1844 strcmp_iw (string1, string2) 1845 const char *string1; 1846 const char *string2; 1847 { 1848 while ((*string1 != '\0') && (*string2 != '\0')) 1849 { 1850 while (isspace (*string1)) 1851 { 1852 string1++; 1853 } 1854 while (isspace (*string2)) 1855 { 1856 string2++; 1857 } 1858 if (*string1 != *string2) 1859 { 1860 break; 1861 } 1862 if (*string1 != '\0') 1863 { 1864 string1++; 1865 string2++; 1866 } 1867 } 1868 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); 1869 } 1870 1871 1872 void 1873 initialize_utils () 1874 { 1875 struct cmd_list_element *c; 1876 1877 c = add_set_cmd ("width", class_support, var_uinteger, 1878 (char *)&chars_per_line, 1879 "Set number of characters gdb thinks are in a line.", 1880 &setlist); 1881 add_show_from_set (c, &showlist); 1882 c->function.sfunc = set_width_command; 1883 1884 add_show_from_set 1885 (add_set_cmd ("height", class_support, 1886 var_uinteger, (char *)&lines_per_page, 1887 "Set number of lines gdb thinks are in a page.", &setlist), 1888 &showlist); 1889 1890 /* These defaults will be used if we are unable to get the correct 1891 values from termcap. */ 1892 #if defined(__GO32__) 1893 lines_per_page = ScreenRows(); 1894 chars_per_line = ScreenCols(); 1895 #else 1896 lines_per_page = 24; 1897 chars_per_line = 80; 1898 1899 #if !defined MPW && !defined _WIN32 1900 /* No termcap under MPW, although might be cool to do something 1901 by looking at worksheet or console window sizes. */ 1902 /* Initialize the screen height and width from termcap. */ 1903 { 1904 char *termtype = getenv ("TERM"); 1905 1906 /* Positive means success, nonpositive means failure. */ 1907 int status; 1908 1909 /* 2048 is large enough for all known terminals, according to the 1910 GNU termcap manual. */ 1911 char term_buffer[2048]; 1912 1913 if (termtype) 1914 { 1915 status = tgetent (term_buffer, termtype); 1916 if (status > 0) 1917 { 1918 int val; 1919 1920 val = tgetnum ("li"); 1921 if (val >= 0) 1922 lines_per_page = val; 1923 else 1924 /* The number of lines per page is not mentioned 1925 in the terminal description. This probably means 1926 that paging is not useful (e.g. emacs shell window), 1927 so disable paging. */ 1928 lines_per_page = UINT_MAX; 1929 1930 val = tgetnum ("co"); 1931 if (val >= 0) 1932 chars_per_line = val; 1933 } 1934 } 1935 } 1936 #endif /* MPW */ 1937 1938 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) 1939 1940 /* If there is a better way to determine the window size, use it. */ 1941 SIGWINCH_HANDLER (); 1942 #endif 1943 #endif 1944 /* If the output is not a terminal, don't paginate it. */ 1945 if (!ISATTY (gdb_stdout)) 1946 lines_per_page = UINT_MAX; 1947 1948 set_width_command ((char *)NULL, 0, c); 1949 1950 add_show_from_set 1951 (add_set_cmd ("demangle", class_support, var_boolean, 1952 (char *)&demangle, 1953 "Set demangling of encoded C++ names when displaying symbols.", 1954 &setprintlist), 1955 &showprintlist); 1956 1957 add_show_from_set 1958 (add_set_cmd ("sevenbit-strings", class_support, var_boolean, 1959 (char *)&sevenbit_strings, 1960 "Set printing of 8-bit characters in strings as \\nnn.", 1961 &setprintlist), 1962 &showprintlist); 1963 1964 add_show_from_set 1965 (add_set_cmd ("asm-demangle", class_support, var_boolean, 1966 (char *)&asm_demangle, 1967 "Set demangling of C++ names in disassembly listings.", 1968 &setprintlist), 1969 &showprintlist); 1970 } 1971 1972 /* Machine specific function to handle SIGWINCH signal. */ 1973 1974 #ifdef SIGWINCH_HANDLER_BODY 1975 SIGWINCH_HANDLER_BODY 1976 #endif 1977 1978 /* Support for converting target fp numbers into host DOUBLEST format. */ 1979 1980 /* XXX - This code should really be in libiberty/floatformat.c, however 1981 configuration issues with libiberty made this very difficult to do in the 1982 available time. */ 1983 1984 #include "floatformat.h" 1985 #include <math.h> /* ldexp */ 1986 1987 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not 1988 going to bother with trying to muck around with whether it is defined in 1989 a system header, what we do if not, etc. */ 1990 #define FLOATFORMAT_CHAR_BIT 8 1991 1992 static unsigned long get_field PARAMS ((unsigned char *, 1993 enum floatformat_byteorders, 1994 unsigned int, 1995 unsigned int, 1996 unsigned int)); 1997 1998 /* Extract a field which starts at START and is LEN bytes long. DATA and 1999 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ 2000 static unsigned long 2001 get_field (data, order, total_len, start, len) 2002 unsigned char *data; 2003 enum floatformat_byteorders order; 2004 unsigned int total_len; 2005 unsigned int start; 2006 unsigned int len; 2007 { 2008 unsigned long result; 2009 unsigned int cur_byte; 2010 int cur_bitshift; 2011 2012 /* Start at the least significant part of the field. */ 2013 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; 2014 if (order == floatformat_little) 2015 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1; 2016 cur_bitshift = 2017 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; 2018 result = *(data + cur_byte) >> (-cur_bitshift); 2019 cur_bitshift += FLOATFORMAT_CHAR_BIT; 2020 if (order == floatformat_little) 2021 ++cur_byte; 2022 else 2023 --cur_byte; 2024 2025 /* Move towards the most significant part of the field. */ 2026 while (cur_bitshift < len) 2027 { 2028 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) 2029 /* This is the last byte; zero out the bits which are not part of 2030 this field. */ 2031 result |= 2032 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1)) 2033 << cur_bitshift; 2034 else 2035 result |= *(data + cur_byte) << cur_bitshift; 2036 cur_bitshift += FLOATFORMAT_CHAR_BIT; 2037 if (order == floatformat_little) 2038 ++cur_byte; 2039 else 2040 --cur_byte; 2041 } 2042 return result; 2043 } 2044 2045 /* Convert from FMT to a DOUBLEST. 2046 FROM is the address of the extended float. 2047 Store the DOUBLEST in *TO. */ 2048 2049 void 2050 floatformat_to_doublest (fmt, from, to) 2051 const struct floatformat *fmt; 2052 char *from; 2053 DOUBLEST *to; 2054 { 2055 unsigned char *ufrom = (unsigned char *)from; 2056 DOUBLEST dto; 2057 long exponent; 2058 unsigned long mant; 2059 unsigned int mant_bits, mant_off; 2060 int mant_bits_left; 2061 int special_exponent; /* It's a NaN, denorm or zero */ 2062 2063 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize, 2064 fmt->exp_start, fmt->exp_len); 2065 /* Note that if exponent indicates a NaN, we can't really do anything useful 2066 (not knowing if the host has NaN's, or how to build one). So it will 2067 end up as an infinity or something close; that is OK. */ 2068 2069 mant_bits_left = fmt->man_len; 2070 mant_off = fmt->man_start; 2071 dto = 0.0; 2072 2073 special_exponent = exponent == 0 || exponent == fmt->exp_nan; 2074 2075 /* Don't bias zero's, denorms or NaNs. */ 2076 if (!special_exponent) 2077 exponent -= fmt->exp_bias; 2078 2079 /* Build the result algebraically. Might go infinite, underflow, etc; 2080 who cares. */ 2081 2082 /* If this format uses a hidden bit, explicitly add it in now. Otherwise, 2083 increment the exponent by one to account for the integer bit. */ 2084 2085 if (!special_exponent) 2086 if (fmt->intbit == floatformat_intbit_no) 2087 dto = ldexp (1.0, exponent); 2088 else 2089 exponent++; 2090 2091 while (mant_bits_left > 0) 2092 { 2093 mant_bits = min (mant_bits_left, 32); 2094 2095 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize, 2096 mant_off, mant_bits); 2097 2098 dto += ldexp ((double)mant, exponent - mant_bits); 2099 exponent -= mant_bits; 2100 mant_off += mant_bits; 2101 mant_bits_left -= mant_bits; 2102 } 2103 2104 /* Negate it if negative. */ 2105 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1)) 2106 dto = -dto; 2107 *to = dto; 2108 } 2109 2110 static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders, 2111 unsigned int, 2112 unsigned int, 2113 unsigned int, 2114 unsigned long)); 2115 2116 /* Set a field which starts at START and is LEN bytes long. DATA and 2117 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ 2118 static void 2119 put_field (data, order, total_len, start, len, stuff_to_put) 2120 unsigned char *data; 2121 enum floatformat_byteorders order; 2122 unsigned int total_len; 2123 unsigned int start; 2124 unsigned int len; 2125 unsigned long stuff_to_put; 2126 { 2127 unsigned int cur_byte; 2128 int cur_bitshift; 2129 2130 /* Start at the least significant part of the field. */ 2131 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; 2132 if (order == floatformat_little) 2133 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1; 2134 cur_bitshift = 2135 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; 2136 *(data + cur_byte) &= 2137 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift)); 2138 *(data + cur_byte) |= 2139 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); 2140 cur_bitshift += FLOATFORMAT_CHAR_BIT; 2141 if (order == floatformat_little) 2142 ++cur_byte; 2143 else 2144 --cur_byte; 2145 2146 /* Move towards the most significant part of the field. */ 2147 while (cur_bitshift < len) 2148 { 2149 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) 2150 { 2151 /* This is the last byte. */ 2152 *(data + cur_byte) &= 2153 ~((1 << (len - cur_bitshift)) - 1); 2154 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); 2155 } 2156 else 2157 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) 2158 & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); 2159 cur_bitshift += FLOATFORMAT_CHAR_BIT; 2160 if (order == floatformat_little) 2161 ++cur_byte; 2162 else 2163 --cur_byte; 2164 } 2165 } 2166 2167 #ifdef HAVE_LONG_DOUBLE 2168 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR. 2169 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to 2170 frexp, but operates on the long double data type. */ 2171 2172 static long double ldfrexp PARAMS ((long double value, int *eptr)); 2173 2174 static long double 2175 ldfrexp (value, eptr) 2176 long double value; 2177 int *eptr; 2178 { 2179 long double tmp; 2180 int exp; 2181 2182 /* Unfortunately, there are no portable functions for extracting the exponent 2183 of a long double, so we have to do it iteratively by multiplying or dividing 2184 by two until the fraction is between 0.5 and 1.0. */ 2185 2186 if (value < 0.0l) 2187 value = -value; 2188 2189 tmp = 1.0l; 2190 exp = 0; 2191 2192 if (value >= tmp) /* Value >= 1.0 */ 2193 while (value >= tmp) 2194 { 2195 tmp *= 2.0l; 2196 exp++; 2197 } 2198 else if (value != 0.0l) /* Value < 1.0 and > 0.0 */ 2199 { 2200 while (value < tmp) 2201 { 2202 tmp /= 2.0l; 2203 exp--; 2204 } 2205 tmp *= 2.0l; 2206 exp++; 2207 } 2208 2209 *eptr = exp; 2210 return value/tmp; 2211 } 2212 #endif /* HAVE_LONG_DOUBLE */ 2213 2214 2215 /* The converse: convert the DOUBLEST *FROM to an extended float 2216 and store where TO points. Neither FROM nor TO have any alignment 2217 restrictions. */ 2218 2219 void 2220 floatformat_from_doublest (fmt, from, to) 2221 CONST struct floatformat *fmt; 2222 DOUBLEST *from; 2223 char *to; 2224 { 2225 DOUBLEST dfrom; 2226 int exponent; 2227 DOUBLEST mant; 2228 unsigned int mant_bits, mant_off; 2229 int mant_bits_left; 2230 unsigned char *uto = (unsigned char *)to; 2231 2232 memcpy (&dfrom, from, sizeof (dfrom)); 2233 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT); 2234 if (dfrom == 0) 2235 return; /* Result is zero */ 2236 if (dfrom != dfrom) 2237 { 2238 /* From is NaN */ 2239 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, 2240 fmt->exp_len, fmt->exp_nan); 2241 /* Be sure it's not infinity, but NaN value is irrel */ 2242 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start, 2243 32, 1); 2244 return; 2245 } 2246 2247 /* If negative, set the sign bit. */ 2248 if (dfrom < 0) 2249 { 2250 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1); 2251 dfrom = -dfrom; 2252 } 2253 2254 /* How to tell an infinity from an ordinary number? FIXME-someday */ 2255 2256 #ifdef HAVE_LONG_DOUBLE 2257 mant = ldfrexp (dfrom, &exponent); 2258 #else 2259 mant = frexp (dfrom, &exponent); 2260 #endif 2261 2262 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len, 2263 exponent + fmt->exp_bias - 1); 2264 2265 mant_bits_left = fmt->man_len; 2266 mant_off = fmt->man_start; 2267 while (mant_bits_left > 0) 2268 { 2269 unsigned long mant_long; 2270 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; 2271 2272 mant *= 4294967296.0; 2273 mant_long = (unsigned long)mant; 2274 mant -= mant_long; 2275 2276 /* If the integer bit is implicit, then we need to discard it. 2277 If we are discarding a zero, we should be (but are not) creating 2278 a denormalized number which means adjusting the exponent 2279 (I think). */ 2280 if (mant_bits_left == fmt->man_len 2281 && fmt->intbit == floatformat_intbit_no) 2282 { 2283 mant_long <<= 1; 2284 mant_bits -= 1; 2285 } 2286 2287 if (mant_bits < 32) 2288 { 2289 /* The bits we want are in the most significant MANT_BITS bits of 2290 mant_long. Move them to the least significant. */ 2291 mant_long >>= 32 - mant_bits; 2292 } 2293 2294 put_field (uto, fmt->byteorder, fmt->totalsize, 2295 mant_off, mant_bits, mant_long); 2296 mant_off += mant_bits; 2297 mant_bits_left -= mant_bits; 2298 } 2299 } 2300 2301 /* temporary storage using circular buffer */ 2302 #define NUMCELLS 16 2303 #define CELLSIZE 32 2304 static char* 2305 get_cell() 2306 { 2307 static char buf[NUMCELLS][CELLSIZE]; 2308 static int cell=0; 2309 if (++cell>=NUMCELLS) cell=0; 2310 return buf[cell]; 2311 } 2312 2313 /* print routines to handle variable size regs, etc */ 2314 char* 2315 paddr(addr) 2316 t_addr addr; 2317 { 2318 char *paddr_str=get_cell(); 2319 switch (sizeof(t_addr)) 2320 { 2321 case 8: 2322 sprintf(paddr_str,"%08x%08x", 2323 (unsigned long)(addr>>32),(unsigned long)(addr&0xffffffff)); 2324 break; 2325 case 4: 2326 sprintf(paddr_str,"%08x",(unsigned long)addr); 2327 break; 2328 case 2: 2329 sprintf(paddr_str,"%04x",(unsigned short)(addr&0xffff)); 2330 break; 2331 default: 2332 sprintf(paddr_str,"%x",addr); 2333 } 2334 return paddr_str; 2335 } 2336 2337 char* 2338 preg(reg) 2339 t_reg reg; 2340 { 2341 char *preg_str=get_cell(); 2342 switch (sizeof(t_reg)) 2343 { 2344 case 8: 2345 sprintf(preg_str,"%08x%08x", 2346 (unsigned long)(reg>>32),(unsigned long)(reg&0xffffffff)); 2347 break; 2348 case 4: 2349 sprintf(preg_str,"%08x",(unsigned long)reg); 2350 break; 2351 case 2: 2352 sprintf(preg_str,"%04x",(unsigned short)(reg&0xffff)); 2353 break; 2354 default: 2355 sprintf(preg_str,"%x",reg); 2356 } 2357 return preg_str; 2358 } 2359 2360