1 /* General utility routines for GDB, the GNU debugger.
2 Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #if !defined(__GO32__)
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
37 /* Prototypes for local functions */
39 #if !defined (NO_MALLOC_CHECK)
42 malloc_botch PARAMS ((void));
44 #endif /* NO_MALLOC_CHECK */
47 fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
50 prompt_for_continue PARAMS ((void));
53 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
55 /* If this definition isn't overridden by the header files, assume
56 that isatty and fileno exist on this system. */
58 #define ISATTY(FP) (isatty (fileno (FP)))
61 /* Chain of cleanup actions established with make_cleanup,
62 to be executed if an error happens. */
64 static struct cleanup *cleanup_chain;
66 /* Nonzero means a quit has been requested. */
70 /* Nonzero means quit immediately if Control-C is typed now,
71 rather than waiting until QUIT is executed. */
75 /* Nonzero means that encoded C++ names should be printed out in their
76 C++ form rather than raw. */
80 /* Nonzero means that encoded C++ names should be printed out in their
81 C++ form even in assembler language displays. If this is set, but
82 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
86 /* Nonzero means that strings with character values >0x7F should be printed
87 as octal escapes. Zero means just print the value (e.g. it's an
88 international character, and the terminal or window can cope.) */
90 int sevenbit_strings = 0;
92 /* String to be printed before error messages, if any. */
94 char *error_pre_print;
95 char *warning_pre_print = "\nwarning: ";
97 /* Add a new cleanup to the cleanup_chain,
98 and return the previous chain pointer
99 to be passed later to do_cleanups or discard_cleanups.
100 Args are FUNCTION to clean up with, and ARG to pass to it. */
103 make_cleanup (function, arg)
104 void (*function) PARAMS ((PTR));
107 register struct cleanup *new
108 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
109 register struct cleanup *old_chain = cleanup_chain;
111 new->next = cleanup_chain;
112 new->function = function;
119 /* Discard cleanups and do the actions they describe
120 until we get back to the point OLD_CHAIN in the cleanup_chain. */
123 do_cleanups (old_chain)
124 register struct cleanup *old_chain;
126 register struct cleanup *ptr;
127 while ((ptr = cleanup_chain) != old_chain)
129 cleanup_chain = ptr->next; /* Do this first incase recursion */
130 (*ptr->function) (ptr->arg);
135 /* Discard cleanups, not doing the actions they describe,
136 until we get back to the point OLD_CHAIN in the cleanup_chain. */
139 discard_cleanups (old_chain)
140 register struct cleanup *old_chain;
142 register struct cleanup *ptr;
143 while ((ptr = cleanup_chain) != old_chain)
145 cleanup_chain = ptr->next;
150 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
154 struct cleanup *old_chain = cleanup_chain;
160 /* Restore the cleanup chain from a previously saved chain. */
162 restore_cleanups (chain)
163 struct cleanup *chain;
165 cleanup_chain = chain;
168 /* This function is useful for cleanups.
172 old_chain = make_cleanup (free_current_contents, &foo);
174 to arrange to free the object thus allocated. */
177 free_current_contents (location)
183 /* Provide a known function that does nothing, to use as a base for
184 for a possibly long chain of cleanups. This is useful where we
185 use the cleanup chain for handling normal cleanups as well as dealing
186 with cleanups that need to be done as a result of a call to error().
187 In such cases, we may not be certain where the first cleanup is, unless
188 we have a do-nothing one to always use as the base. */
198 /* Provide a hook for modules wishing to print their own warning messages
199 to set up the terminal state in a compatible way, without them having
200 to import all the target_<...> macros. */
205 target_terminal_ours ();
206 wrap_here(""); /* Force out any buffered output */
210 /* Print a warning message.
211 The first argument STRING is the warning message, used as a fprintf string,
212 and the remaining args are passed as arguments to it.
213 The primary difference between warnings and errors is that a warning
214 does not force the return to command level. */
225 target_terminal_ours ();
226 wrap_here(""); /* Force out any buffered output */
228 if (warning_pre_print)
229 fprintf (stderr, warning_pre_print);
230 string = va_arg (args, char *);
231 vfprintf (stderr, string, args);
232 fprintf (stderr, "\n");
236 /* Print an error message and return to command level.
237 The first argument STRING is the error message, used as a fprintf string,
238 and the remaining args are passed as arguments to it. */
249 target_terminal_ours ();
250 wrap_here(""); /* Force out any buffered output */
253 fprintf_filtered (stderr, error_pre_print);
254 string = va_arg (args, char *);
255 vfprintf_filtered (stderr, string, args);
256 fprintf_filtered (stderr, "\n");
258 return_to_top_level ();
261 /* Print an error message and exit reporting failure.
262 This is for a error that we cannot continue from.
263 The arguments are printed a la printf.
265 This function cannot be declared volatile (NORETURN) in an
266 ANSI environment because exit() is not declared volatile. */
277 string = va_arg (args, char *);
278 fprintf (stderr, "\ngdb: ");
279 vfprintf (stderr, string, args);
280 fprintf (stderr, "\n");
285 /* Print an error message and exit, dumping core.
286 The arguments are printed a la printf (). */
290 fatal_dump_core (va_alist)
297 string = va_arg (args, char *);
298 /* "internal error" is always correct, since GDB should never dump
299 core, no matter what the input. */
300 fprintf (stderr, "\ngdb internal error: ");
301 vfprintf (stderr, string, args);
302 fprintf (stderr, "\n");
305 signal (SIGQUIT, SIG_DFL);
306 kill (getpid (), SIGQUIT);
307 /* We should never get here, but just in case... */
311 /* The strerror() function can return NULL for errno values that are
312 out of range. Provide a "safe" version that always returns a
316 safe_strerror (errnum)
322 if ((msg = strerror (errnum)) == NULL)
324 sprintf (buf, "(undocumented errno %d)", errnum);
330 /* The strsignal() function can return NULL for signal values that are
331 out of range. Provide a "safe" version that always returns a
335 safe_strsignal (signo)
341 if ((msg = strsignal (signo)) == NULL)
343 sprintf (buf, "(undocumented signal %d)", signo);
350 /* Print the system error message for errno, and also mention STRING
351 as the file name for which the error was encountered.
352 Then return to command level. */
355 perror_with_name (string)
361 err = safe_strerror (errno);
362 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
363 strcpy (combined, string);
364 strcat (combined, ": ");
365 strcat (combined, err);
367 /* I understand setting these is a matter of taste. Still, some people
368 may clear errno but not know about bfd_error. Doing this here is not
370 bfd_error = no_error;
373 error ("%s.", combined);
376 /* Print the system error message for ERRCODE, and also mention STRING
377 as the file name for which the error was encountered. */
380 print_sys_errmsg (string, errcode)
387 err = safe_strerror (errcode);
388 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
389 strcpy (combined, string);
390 strcat (combined, ": ");
391 strcat (combined, err);
393 fprintf (stderr, "%s.\n", combined);
396 /* Control C eventually causes this to be called, at a convenient time. */
401 target_terminal_ours ();
402 wrap_here ((char *)0); /* Force out any pending output */
403 #if !defined(__GO32__)
405 ioctl (fileno (stdout), TCFLSH, 1);
406 #else /* not HAVE_TERMIO */
407 ioctl (fileno (stdout), TIOCFLUSH, 0);
408 #endif /* not HAVE_TERMIO */
412 error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
413 #endif /* TIOCGPGRP */
417 /* Control C comes here */
426 /* Restore the signal handler. */
427 signal (signo, request_quit);
435 /* Memory management stuff (malloc friends). */
437 #if defined (NO_MMALLOC)
444 return (malloc (size));
448 mrealloc (md, ptr, size)
453 if (ptr == 0) /* Guard against old realloc's */
454 return malloc (size);
456 return realloc (ptr, size);
467 #endif /* NO_MMALLOC */
469 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
477 #else /* have mmalloc and want corruption checking */
482 fatal_dump_core ("Memory corruption");
485 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
486 by MD, to detect memory corruption. Note that MD may be NULL to specify
487 the default heap that grows via sbrk.
489 Note that for freshly created regions, we must call mmcheck prior to any
490 mallocs in the region. Otherwise, any region which was allocated prior to
491 installing the checking hooks, which is later reallocated or freed, will
492 fail the checks! The mmcheck function only allows initial hooks to be
493 installed before the first mmalloc. However, anytime after we have called
494 mmcheck the first time to install the checking hooks, we can call it again
495 to update the function pointer to the memory corruption handler.
497 Returns zero on failure, non-zero on success. */
503 if (!mmcheck (md, malloc_botch))
505 warning ("internal error: failed to install memory consistency checks");
511 #endif /* Have mmalloc and want corruption checking */
513 /* Called when a memory allocation fails, with the number of bytes of
514 memory requested in SIZE. */
522 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
526 fatal ("virtual memory exhausted.");
530 /* Like mmalloc but get error if no storage available, and protect against
531 the caller wanting to allocate zero bytes. Whether to return NULL for
532 a zero byte request, or translate the request into a request for one
533 byte of zero'd storage, is a religious issue. */
546 else if ((val = mmalloc (md, size)) == NULL)
553 /* Like mrealloc but get error if no storage available. */
556 xmrealloc (md, ptr, size)
565 val = mrealloc (md, ptr, size);
569 val = mmalloc (md, size);
578 /* Like malloc but get error if no storage available, and protect against
579 the caller wanting to allocate zero bytes. */
585 return (xmmalloc ((void *) NULL, size));
588 /* Like mrealloc but get error if no storage available. */
595 return (xmrealloc ((void *) NULL, ptr, size));
599 /* My replacement for the read system call.
600 Used like `read' but keeps going if `read' returns too soon. */
603 myread (desc, addr, len)
613 val = read (desc, addr, len);
624 /* Make a copy of the string at PTR with SIZE characters
625 (and add a null character at the end in the copy).
626 Uses malloc to get the space. Returns the address of the copy. */
629 savestring (ptr, size)
633 register char *p = (char *) xmalloc (size + 1);
634 memcpy (p, ptr, size);
640 msavestring (md, ptr, size)
645 register char *p = (char *) xmmalloc (md, size + 1);
646 memcpy (p, ptr, size);
651 /* The "const" is so it compiles under DGUX (which prototypes strsave
652 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
653 Doesn't real strsave return NULL if out of memory? */
658 return savestring (ptr, strlen (ptr));
666 return (msavestring (md, ptr, strlen (ptr)));
670 print_spaces (n, file)
678 /* Ask user a y-or-n question and return 1 iff answer is yes.
679 Takes three args which are given to printf to print the question.
680 The first, a control string, should end in "? ".
681 It should not say how to answer, because we do that. */
693 /* Automatically answer "yes" if input is not from a terminal. */
694 if (!input_from_terminal_p ())
699 wrap_here (""); /* Flush any buffered output */
702 ctlstr = va_arg (args, char *);
703 vfprintf_filtered (stdout, ctlstr, args);
705 printf_filtered ("(y or n) ");
707 answer = fgetc (stdin);
708 clearerr (stdin); /* in case of C-d */
709 if (answer == EOF) /* C-d */
711 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
714 ans2 = fgetc (stdin);
717 while (ans2 != EOF && ans2 != '\n');
724 printf_filtered ("Please answer y or n.\n");
729 /* Parse a C escape sequence. STRING_PTR points to a variable
730 containing a pointer to the string to parse. That pointer
731 should point to the character after the \. That pointer
732 is updated past the characters we use. The value of the
733 escape sequence is returned.
735 A negative value means the sequence \ newline was seen,
736 which is supposed to be equivalent to nothing at all.
738 If \ is followed by a null character, we return a negative
739 value and leave the string pointer pointing at the null character.
741 If \ is followed by 000, we return 0 and leave the string pointer
742 after the zeros. A value of 0 does not mean end of string. */
745 parse_escape (string_ptr)
748 register int c = *(*string_ptr)++;
752 return 007; /* Bell (alert) char */
755 case 'e': /* Escape character */
773 c = *(*string_ptr)++;
775 c = parse_escape (string_ptr);
778 return (c & 0200) | (c & 037);
789 register int i = c - '0';
790 register int count = 0;
793 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
811 /* Print the character C on STREAM as part of the contents of a literal
812 string whose delimiter is QUOTER. Note that this routine should only
813 be call for printing things which are independent of the language
814 of the program being debugged. */
817 gdb_printchar (c, stream, quoter)
823 c &= 0xFF; /* Avoid sign bit follies */
825 if ( c < 0x20 || /* Low control chars */
826 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
827 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
831 fputs_filtered ("\\n", stream);
834 fputs_filtered ("\\b", stream);
837 fputs_filtered ("\\t", stream);
840 fputs_filtered ("\\f", stream);
843 fputs_filtered ("\\r", stream);
846 fputs_filtered ("\\e", stream);
849 fputs_filtered ("\\a", stream);
852 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
856 if (c == '\\' || c == quoter)
857 fputs_filtered ("\\", stream);
858 fprintf_filtered (stream, "%c", c);
862 /* Number of lines per page or UINT_MAX if paging is disabled. */
863 static unsigned int lines_per_page;
864 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
865 static unsigned int chars_per_line;
866 /* Current count of lines printed on this page, chars on this line. */
867 static unsigned int lines_printed, chars_printed;
869 /* Buffer and start column of buffered text, for doing smarter word-
870 wrapping. When someone calls wrap_here(), we start buffering output
871 that comes through fputs_filtered(). If we see a newline, we just
872 spit it out and forget about the wrap_here(). If we see another
873 wrap_here(), we spit it out and remember the newer one. If we see
874 the end of the line, we spit out a newline, the indent, and then
877 wrap_column is the column number on the screen where wrap_buffer begins.
878 When wrap_column is zero, wrapping is not in effect.
879 wrap_buffer is malloc'd with chars_per_line+2 bytes.
880 When wrap_buffer[0] is null, the buffer is empty.
881 wrap_pointer points into it at the next character to fill.
882 wrap_indent is the string that should be used as indentation if the
885 static char *wrap_buffer, *wrap_pointer, *wrap_indent;
886 static int wrap_column;
890 set_width_command (args, from_tty, c)
893 struct cmd_list_element *c;
897 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
898 wrap_buffer[0] = '\0';
901 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
902 wrap_pointer = wrap_buffer; /* Start it at the beginning */
905 /* Wait, so the user can read what's on the screen. Prompt the user
906 to continue by pressing RETURN. */
909 prompt_for_continue ()
913 /* We must do this *before* we call gdb_readline, else it will eventually
914 call us -- thinking that we're trying to print beyond the end of the
916 reinitialize_more_filter ();
919 ignore = gdb_readline ("---Type <return> to continue---");
924 /* Now we have to do this again, so that GDB will know that it doesn't
925 need to save the ---Type <return>--- line at the top of the screen. */
926 reinitialize_more_filter ();
928 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
931 /* Reinitialize filter; ie. tell it to reset to original values. */
934 reinitialize_more_filter ()
940 /* Indicate that if the next sequence of characters overflows the line,
941 a newline should be inserted here rather than when it hits the end.
942 If INDENT is nonzero, it is a string to be printed to indent the
943 wrapped part on the next line. INDENT must remain accessible until
944 the next call to wrap_here() or until a newline is printed through
947 If the line is already overfull, we immediately print a newline and
948 the indentation, and disable further wrapping.
950 If we don't know the width of lines, but we know the page height,
951 we must not wrap words, but should still keep track of newlines
952 that were explicitly printed.
954 INDENT should not contain tabs, as that
955 will mess up the char count on the next line. FIXME. */
963 *wrap_pointer = '\0';
964 fputs (wrap_buffer, stdout);
966 wrap_pointer = wrap_buffer;
967 wrap_buffer[0] = '\0';
968 if (chars_per_line == UINT_MAX) /* No line overflow checking */
972 else if (chars_printed >= chars_per_line)
974 puts_filtered ("\n");
975 puts_filtered (indent);
980 wrap_column = chars_printed;
981 wrap_indent = indent;
985 /* Ensure that whatever gets printed next, using the filtered output
986 commands, starts at the beginning of the line. I.E. if there is
987 any pending output for the current line, flush it and start a new
988 line. Otherwise do nothing. */
993 if (chars_printed > 0)
995 puts_filtered ("\n");
999 /* Like fputs but pause after every screenful, and can wrap at points
1000 other than the final character of a line.
1001 Unlike fputs, fputs_filtered does not return a value.
1002 It is OK for LINEBUFFER to be NULL, in which case just don't print
1005 Note that a longjmp to top level may occur in this routine
1006 (since prompt_for_continue may do so) so this routine should not be
1007 called when cleanups are not in place. */
1010 fputs_filtered (linebuffer, stream)
1011 const char *linebuffer;
1014 const char *lineptr;
1016 if (linebuffer == 0)
1019 /* Don't do any filtering if it is disabled. */
1020 if (stream != stdout
1021 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1023 fputs (linebuffer, stream);
1027 /* Go through and output each character. Show line extension
1028 when this is necessary; prompt user for new page when this is
1031 lineptr = linebuffer;
1034 /* Possible new page. */
1035 if (lines_printed >= lines_per_page - 1)
1036 prompt_for_continue ();
1038 while (*lineptr && *lineptr != '\n')
1040 /* Print a single line. */
1041 if (*lineptr == '\t')
1044 *wrap_pointer++ = '\t';
1046 putc ('\t', stream);
1047 /* Shifting right by 3 produces the number of tab stops
1048 we have already passed, and then adding one and
1049 shifting left 3 advances to the next tab stop. */
1050 chars_printed = ((chars_printed >> 3) + 1) << 3;
1056 *wrap_pointer++ = *lineptr;
1058 putc (*lineptr, stream);
1063 if (chars_printed >= chars_per_line)
1065 unsigned int save_chars = chars_printed;
1069 /* If we aren't actually wrapping, don't output newline --
1070 if chars_per_line is right, we probably just overflowed
1071 anyway; if it's wrong, let us keep going. */
1073 putc ('\n', stream);
1075 /* Possible new page. */
1076 if (lines_printed >= lines_per_page - 1)
1077 prompt_for_continue ();
1079 /* Now output indentation and wrapped string */
1083 fputs (wrap_indent, stream);
1084 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1085 fputs (wrap_buffer, stream); /* and eject it */
1086 /* FIXME, this strlen is what prevents wrap_indent from
1087 containing tabs. However, if we recurse to print it
1088 and count its chars, we risk trouble if wrap_indent is
1089 longer than (the user settable) chars_per_line.
1090 Note also that this can set chars_printed > chars_per_line
1091 if we are printing a long string. */
1092 chars_printed = strlen (wrap_indent)
1093 + (save_chars - wrap_column);
1094 wrap_pointer = wrap_buffer; /* Reset buffer */
1095 wrap_buffer[0] = '\0';
1096 wrap_column = 0; /* And disable fancy wrap */
1101 if (*lineptr == '\n')
1104 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1106 putc ('\n', stream);
1113 /* fputs_demangled is a variant of fputs_filtered that
1114 demangles g++ names.*/
1117 fputs_demangled (linebuffer, stream, arg_mode)
1122 #define SYMBOL_MAX 1024
1124 #define SYMBOL_CHAR(c) (isascii(c) \
1125 && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
1127 char buf[SYMBOL_MAX+1];
1128 # define DMSLOP 5 /* How much room to leave in buf */
1131 if (linebuffer == NULL)
1134 /* If user wants to see raw output, no problem. */
1136 fputs_filtered (linebuffer, stream);
1142 while ( *p != (char) 0 ) {
1145 /* collect non-interesting characters into buf */
1146 while (*p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-DMSLOP ) {
1151 /* output the non-interesting characters without demangling */
1153 fputs_filtered(buf, stream);
1154 i = 0; /* reset buf */
1157 /* and now the interesting characters */
1158 while (i < SYMBOL_MAX
1161 && i < (int)sizeof(buf) - DMSLOP) {
1169 if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
1170 fputs_filtered(result, stream);
1174 fputs_filtered(buf, stream);
1180 /* Print a variable number of ARGS using format FORMAT. If this
1181 information is going to put the amount written (since the last call
1182 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1183 print out a pause message and do a gdb_readline to get the users
1184 permision to continue.
1186 Unlike fprintf, this function does not return a value.
1188 We implement three variants, vfprintf (takes a vararg list and stream),
1189 fprintf (takes a stream to write on), and printf (the usual).
1191 Note that this routine has a restriction that the length of the
1192 final output line must be less than 255 characters *or* it must be
1193 less than twice the size of the format string. This is a very
1194 arbitrary restriction, but it is an internal restriction, so I'll
1195 put it in. This means that the %s format specifier is almost
1196 useless; unless the caller can GUARANTEE that the string is short
1197 enough, fputs_filtered should be used instead.
1199 Note also that a longjmp to top level may occur in this routine
1200 (since prompt_for_continue may do so) so this routine should not be
1201 called when cleanups are not in place. */
1203 #define MIN_LINEBUF 255
1206 vfprintf_filtered (stream, format, args)
1211 char line_buf[MIN_LINEBUF+10];
1212 char *linebuffer = line_buf;
1215 format_length = strlen (format);
1217 /* Reallocate buffer to a larger size if this is necessary. */
1218 if (format_length * 2 > MIN_LINEBUF)
1220 linebuffer = alloca (10 + format_length * 2);
1223 /* This won't blow up if the restrictions described above are
1225 vsprintf (linebuffer, format, args);
1227 fputs_filtered (linebuffer, stream);
1231 vprintf_filtered (format, args)
1235 vfprintf_filtered (stdout, format, args);
1240 fprintf_filtered (va_alist)
1248 stream = va_arg (args, FILE *);
1249 format = va_arg (args, char *);
1251 /* This won't blow up if the restrictions described above are
1253 vfprintf_filtered (stream, format, args);
1257 /* Like fprintf_filtered, but prints it's result indent.
1258 Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */
1262 fprintfi_filtered (va_alist)
1271 spaces = va_arg (args, int);
1272 stream = va_arg (args, FILE *);
1273 format = va_arg (args, char *);
1274 print_spaces_filtered (spaces, stream);
1276 /* This won't blow up if the restrictions described above are
1278 vfprintf_filtered (stream, format, args);
1284 printf_filtered (va_alist)
1291 format = va_arg (args, char *);
1293 vfprintf_filtered (stdout, format, args);
1297 /* Like printf_filtered, but prints it's result indented.
1298 Called as printfi_filtered (spaces, format, arg1, arg2, ...); */
1302 printfi_filtered (va_alist)
1310 spaces = va_arg (args, int);
1311 format = va_arg (args, char *);
1312 print_spaces_filtered (spaces, stdout);
1313 vfprintf_filtered (stdout, format, args);
1317 /* Easy -- but watch out!
1319 This routine is *not* a replacement for puts()! puts() appends a newline.
1320 This one doesn't, and had better not! */
1323 puts_filtered (string)
1326 fputs_filtered (string, stdout);
1329 /* Return a pointer to N spaces and a null. The pointer is good
1330 until the next call to here. */
1336 static char *spaces;
1337 static int max_spaces;
1343 spaces = (char *) xmalloc (n+1);
1344 for (t = spaces+n; t != spaces;)
1350 return spaces + max_spaces - n;
1353 /* Print N spaces. */
1355 print_spaces_filtered (n, stream)
1359 fputs_filtered (n_spaces (n), stream);
1362 /* C++ demangler stuff. */
1364 /* Make a copy of a symbol, applying C++ demangling if demangling is enabled
1365 and a demangled version exists. Note that the value returned from
1366 cplus_demangle is already allocated in malloc'd memory. */
1369 strdup_demangled (name)
1372 char *demangled = NULL;
1376 demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI);
1378 return ((demangled != NULL) ? demangled : strdup (name));
1382 /* Print NAME on STREAM, demangling if necessary. */
1384 fprint_symbol (stream, name)
1390 || NULL == (demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI)))
1391 fputs_filtered (name, stream);
1394 fputs_filtered (demangled, stream);
1399 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1400 differences in whitespace. Returns 0 if they match, non-zero if they
1401 don't (slightly different than strcmp()'s range of return values).
1403 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1404 This "feature" is useful when searching for matching C++ function names
1405 (such as if the user types 'break FOO', where FOO is a mangled C++
1409 strcmp_iw (string1, string2)
1410 const char *string1;
1411 const char *string2;
1413 while ((*string1 != '\0') && (*string2 != '\0'))
1415 while (isspace (*string1))
1419 while (isspace (*string2))
1423 if (*string1 != *string2)
1427 if (*string1 != '\0')
1433 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1438 _initialize_utils ()
1440 struct cmd_list_element *c;
1442 c = add_set_cmd ("width", class_support, var_uinteger,
1443 (char *)&chars_per_line,
1444 "Set number of characters gdb thinks are in a line.",
1446 add_show_from_set (c, &showlist);
1447 c->function.sfunc = set_width_command;
1450 (add_set_cmd ("height", class_support,
1451 var_uinteger, (char *)&lines_per_page,
1452 "Set number of lines gdb thinks are in a page.", &setlist),
1455 /* These defaults will be used if we are unable to get the correct
1456 values from termcap. */
1457 #if defined(__GO32__)
1458 lines_per_page = ScreenRows();
1459 chars_per_line = ScreenCols();
1461 lines_per_page = 24;
1462 chars_per_line = 80;
1463 /* Initialize the screen height and width from termcap. */
1465 char *termtype = getenv ("TERM");
1467 /* Positive means success, nonpositive means failure. */
1470 /* 2048 is large enough for all known terminals, according to the
1471 GNU termcap manual. */
1472 char term_buffer[2048];
1476 status = tgetent (term_buffer, termtype);
1481 val = tgetnum ("li");
1483 lines_per_page = val;
1485 /* The number of lines per page is not mentioned
1486 in the terminal description. This probably means
1487 that paging is not useful (e.g. emacs shell window),
1488 so disable paging. */
1489 lines_per_page = UINT_MAX;
1491 val = tgetnum ("co");
1493 chars_per_line = val;
1498 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1500 /* If there is a better way to determine the window size, use it. */
1501 SIGWINCH_HANDLER ();
1504 /* If the output is not a terminal, don't paginate it. */
1505 if (!ISATTY (stdout))
1506 lines_per_page = UINT_MAX;
1508 set_width_command ((char *)NULL, 0, c);
1511 (add_set_cmd ("demangle", class_support, var_boolean,
1513 "Set demangling of encoded C++ names when displaying symbols.",
1518 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1519 (char *)&sevenbit_strings,
1520 "Set printing of 8-bit characters in strings as \\nnn.",
1525 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1526 (char *)&asm_demangle,
1527 "Set demangling of C++ names in disassembly listings.",
1532 /* Machine specific function to handle SIGWINCH signal. */
1534 #ifdef SIGWINCH_HANDLER_BODY
1535 SIGWINCH_HANDLER_BODY