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>
33 #include "terminal.h" /* For job_control */
37 #include "expression.h"
40 /* Prototypes for local functions */
42 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
46 malloc_botch PARAMS ((void));
48 #endif /* NO_MMALLOC, etc */
51 fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
54 prompt_for_continue PARAMS ((void));
57 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
59 /* If this definition isn't overridden by the header files, assume
60 that isatty and fileno exist on this system. */
62 #define ISATTY(FP) (isatty (fileno (FP)))
65 /* Chain of cleanup actions established with make_cleanup,
66 to be executed if an error happens. */
68 static struct cleanup *cleanup_chain;
70 /* Nonzero means a quit has been requested. */
74 /* Nonzero means quit immediately if Control-C is typed now, rather
75 than waiting until QUIT is executed. Be careful in setting this;
76 code which executes with immediate_quit set has to be very careful
77 about being able to deal with being interrupted at any time. It is
78 almost always better to use QUIT; the only exception I can think of
79 is being able to quit out of a system call (using EINTR loses if
80 the SIGINT happens between the previous QUIT and the system call).
81 To immediately quit in the case in which a SIGINT happens between
82 the previous QUIT and setting immediate_quit (desirable anytime we
83 expect to block), call QUIT after setting immediate_quit. */
87 /* Nonzero means that encoded C++ names should be printed out in their
88 C++ form rather than raw. */
92 /* Nonzero means that encoded C++ names should be printed out in their
93 C++ form even in assembler language displays. If this is set, but
94 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
98 /* Nonzero means that strings with character values >0x7F should be printed
99 as octal escapes. Zero means just print the value (e.g. it's an
100 international character, and the terminal or window can cope.) */
102 int sevenbit_strings = 0;
104 /* String to be printed before error messages, if any. */
106 char *error_pre_print;
107 char *warning_pre_print = "\nwarning: ";
109 /* Add a new cleanup to the cleanup_chain,
110 and return the previous chain pointer
111 to be passed later to do_cleanups or discard_cleanups.
112 Args are FUNCTION to clean up with, and ARG to pass to it. */
115 make_cleanup (function, arg)
116 void (*function) PARAMS ((PTR));
119 register struct cleanup *new
120 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
121 register struct cleanup *old_chain = cleanup_chain;
123 new->next = cleanup_chain;
124 new->function = function;
131 /* Discard cleanups and do the actions they describe
132 until we get back to the point OLD_CHAIN in the cleanup_chain. */
135 do_cleanups (old_chain)
136 register struct cleanup *old_chain;
138 register struct cleanup *ptr;
139 while ((ptr = cleanup_chain) != old_chain)
141 cleanup_chain = ptr->next; /* Do this first incase recursion */
142 (*ptr->function) (ptr->arg);
147 /* Discard cleanups, not doing the actions they describe,
148 until we get back to the point OLD_CHAIN in the cleanup_chain. */
151 discard_cleanups (old_chain)
152 register struct cleanup *old_chain;
154 register struct cleanup *ptr;
155 while ((ptr = cleanup_chain) != old_chain)
157 cleanup_chain = ptr->next;
162 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
166 struct cleanup *old_chain = cleanup_chain;
172 /* Restore the cleanup chain from a previously saved chain. */
174 restore_cleanups (chain)
175 struct cleanup *chain;
177 cleanup_chain = chain;
180 /* This function is useful for cleanups.
184 old_chain = make_cleanup (free_current_contents, &foo);
186 to arrange to free the object thus allocated. */
189 free_current_contents (location)
195 /* Provide a known function that does nothing, to use as a base for
196 for a possibly long chain of cleanups. This is useful where we
197 use the cleanup chain for handling normal cleanups as well as dealing
198 with cleanups that need to be done as a result of a call to error().
199 In such cases, we may not be certain where the first cleanup is, unless
200 we have a do-nothing one to always use as the base. */
210 /* Provide a hook for modules wishing to print their own warning messages
211 to set up the terminal state in a compatible way, without them having
212 to import all the target_<...> macros. */
217 target_terminal_ours ();
218 wrap_here(""); /* Force out any buffered output */
219 gdb_flush (gdb_stdout);
222 /* Print a warning message.
223 The first argument STRING is the warning message, used as a fprintf string,
224 and the remaining args are passed as arguments to it.
225 The primary difference between warnings and errors is that a warning
226 does not force the return to command level. */
237 target_terminal_ours ();
238 wrap_here(""); /* Force out any buffered output */
239 gdb_flush (gdb_stdout);
240 if (warning_pre_print)
241 fprintf_unfiltered (gdb_stderr, warning_pre_print);
242 string = va_arg (args, char *);
243 vfprintf_unfiltered (gdb_stderr, string, args);
244 fprintf_unfiltered (gdb_stderr, "\n");
248 /* Print an error message and return to command level.
249 The first argument STRING is the error message, used as a fprintf string,
250 and the remaining args are passed as arguments to it. */
261 target_terminal_ours ();
262 wrap_here(""); /* Force out any buffered output */
263 gdb_flush (gdb_stdout);
265 fprintf_filtered (gdb_stderr, error_pre_print);
266 string = va_arg (args, char *);
267 vfprintf_filtered (gdb_stderr, string, args);
268 fprintf_filtered (gdb_stderr, "\n");
270 return_to_top_level (RETURN_ERROR);
273 /* Print an error message and exit reporting failure.
274 This is for a error that we cannot continue from.
275 The arguments are printed a la printf.
277 This function cannot be declared volatile (NORETURN) in an
278 ANSI environment because exit() is not declared volatile. */
289 string = va_arg (args, char *);
290 fprintf_unfiltered (gdb_stderr, "\ngdb: ");
291 vfprintf_unfiltered (gdb_stderr, string, args);
292 fprintf_unfiltered (gdb_stderr, "\n");
297 /* Print an error message and exit, dumping core.
298 The arguments are printed a la printf (). */
302 fatal_dump_core (va_alist)
309 string = va_arg (args, char *);
310 /* "internal error" is always correct, since GDB should never dump
311 core, no matter what the input. */
312 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: ");
313 vfprintf_unfiltered (gdb_stderr, string, args);
314 fprintf_unfiltered (gdb_stderr, "\n");
317 signal (SIGQUIT, SIG_DFL);
318 kill (getpid (), SIGQUIT);
319 /* We should never get here, but just in case... */
323 /* The strerror() function can return NULL for errno values that are
324 out of range. Provide a "safe" version that always returns a
328 safe_strerror (errnum)
334 if ((msg = strerror (errnum)) == NULL)
336 sprintf (buf, "(undocumented errno %d)", errnum);
342 /* The strsignal() function can return NULL for signal values that are
343 out of range. Provide a "safe" version that always returns a
347 safe_strsignal (signo)
353 if ((msg = strsignal (signo)) == NULL)
355 sprintf (buf, "(undocumented signal %d)", signo);
362 /* Print the system error message for errno, and also mention STRING
363 as the file name for which the error was encountered.
364 Then return to command level. */
367 perror_with_name (string)
373 err = safe_strerror (errno);
374 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
375 strcpy (combined, string);
376 strcat (combined, ": ");
377 strcat (combined, err);
379 /* I understand setting these is a matter of taste. Still, some people
380 may clear errno but not know about bfd_error. Doing this here is not
382 bfd_error = no_error;
385 error ("%s.", combined);
388 /* Print the system error message for ERRCODE, and also mention STRING
389 as the file name for which the error was encountered. */
392 print_sys_errmsg (string, errcode)
399 err = safe_strerror (errcode);
400 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
401 strcpy (combined, string);
402 strcat (combined, ": ");
403 strcat (combined, err);
405 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
408 /* Control C eventually causes this to be called, at a convenient time. */
413 serial_t gdb_stdout_serial = serial_fdopen (1);
415 target_terminal_ours ();
416 wrap_here ((char *)0); /* Force out any pending output */
418 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial);
420 SERIAL_UN_FDOPEN (gdb_stdout_serial);
422 /* Don't use *_filtered; we don't want to prompt the user to continue. */
424 fprintf_unfiltered (gdb_stderr, error_pre_print);
427 /* If there is no terminal switching for this target, then we can't
428 possibly get screwed by the lack of job control. */
429 || current_target->to_terminal_ours == NULL)
430 fprintf_unfiltered (gdb_stderr, "Quit\n");
432 fprintf_unfiltered (gdb_stderr,
433 "Quit (expect signal SIGINT when the program is resumed)\n");
434 return_to_top_level (RETURN_QUIT);
440 /* In the absence of signals, poll keyboard for a quit.
441 Called from #define QUIT pollquit() in xm-go32.h. */
459 /* Control C comes here */
468 /* Restore the signal handler. */
469 signal (signo, request_quit);
477 /* Memory management stuff (malloc friends). */
479 #if defined (NO_MMALLOC)
486 return (malloc (size));
490 mrealloc (md, ptr, size)
495 if (ptr == 0) /* Guard against old realloc's */
496 return malloc (size);
498 return realloc (ptr, size);
509 #endif /* NO_MMALLOC */
511 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
519 #else /* have mmalloc and want corruption checking */
524 fatal_dump_core ("Memory corruption");
527 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
528 by MD, to detect memory corruption. Note that MD may be NULL to specify
529 the default heap that grows via sbrk.
531 Note that for freshly created regions, we must call mmcheck prior to any
532 mallocs in the region. Otherwise, any region which was allocated prior to
533 installing the checking hooks, which is later reallocated or freed, will
534 fail the checks! The mmcheck function only allows initial hooks to be
535 installed before the first mmalloc. However, anytime after we have called
536 mmcheck the first time to install the checking hooks, we can call it again
537 to update the function pointer to the memory corruption handler.
539 Returns zero on failure, non-zero on success. */
545 if (!mmcheck (md, malloc_botch))
547 warning ("internal error: failed to install memory consistency checks");
553 #endif /* Have mmalloc and want corruption checking */
555 /* Called when a memory allocation fails, with the number of bytes of
556 memory requested in SIZE. */
564 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
568 fatal ("virtual memory exhausted.");
572 /* Like mmalloc but get error if no storage available, and protect against
573 the caller wanting to allocate zero bytes. Whether to return NULL for
574 a zero byte request, or translate the request into a request for one
575 byte of zero'd storage, is a religious issue. */
588 else if ((val = mmalloc (md, size)) == NULL)
595 /* Like mrealloc but get error if no storage available. */
598 xmrealloc (md, ptr, size)
607 val = mrealloc (md, ptr, size);
611 val = mmalloc (md, size);
620 /* Like malloc but get error if no storage available, and protect against
621 the caller wanting to allocate zero bytes. */
627 return (xmmalloc ((PTR) NULL, size));
630 /* Like mrealloc but get error if no storage available. */
637 return (xmrealloc ((PTR) NULL, ptr, size));
641 /* My replacement for the read system call.
642 Used like `read' but keeps going if `read' returns too soon. */
645 myread (desc, addr, len)
655 val = read (desc, addr, len);
666 /* Make a copy of the string at PTR with SIZE characters
667 (and add a null character at the end in the copy).
668 Uses malloc to get the space. Returns the address of the copy. */
671 savestring (ptr, size)
675 register char *p = (char *) xmalloc (size + 1);
676 memcpy (p, ptr, size);
682 msavestring (md, ptr, size)
687 register char *p = (char *) xmmalloc (md, size + 1);
688 memcpy (p, ptr, size);
693 /* The "const" is so it compiles under DGUX (which prototypes strsave
694 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
695 Doesn't real strsave return NULL if out of memory? */
700 return savestring (ptr, strlen (ptr));
708 return (msavestring (md, ptr, strlen (ptr)));
712 print_spaces (n, file)
720 /* Ask user a y-or-n question and return 1 iff answer is yes.
721 Takes three args which are given to printf to print the question.
722 The first, a control string, should end in "? ".
723 It should not say how to answer, because we do that. */
735 /* Automatically answer "yes" if input is not from a terminal. */
736 if (!input_from_terminal_p ())
741 wrap_here (""); /* Flush any buffered output */
742 gdb_flush (gdb_stdout);
744 ctlstr = va_arg (args, char *);
745 vfprintf_filtered (gdb_stdout, ctlstr, args);
747 printf_filtered ("(y or n) ");
748 gdb_flush (gdb_stdout);
749 answer = fgetc (stdin);
750 clearerr (stdin); /* in case of C-d */
751 if (answer == EOF) /* C-d */
753 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
756 ans2 = fgetc (stdin);
759 while (ans2 != EOF && ans2 != '\n');
766 printf_filtered ("Please answer y or n.\n");
771 /* Parse a C escape sequence. STRING_PTR points to a variable
772 containing a pointer to the string to parse. That pointer
773 should point to the character after the \. That pointer
774 is updated past the characters we use. The value of the
775 escape sequence is returned.
777 A negative value means the sequence \ newline was seen,
778 which is supposed to be equivalent to nothing at all.
780 If \ is followed by a null character, we return a negative
781 value and leave the string pointer pointing at the null character.
783 If \ is followed by 000, we return 0 and leave the string pointer
784 after the zeros. A value of 0 does not mean end of string. */
787 parse_escape (string_ptr)
790 register int c = *(*string_ptr)++;
794 return 007; /* Bell (alert) char */
797 case 'e': /* Escape character */
815 c = *(*string_ptr)++;
817 c = parse_escape (string_ptr);
820 return (c & 0200) | (c & 037);
831 register int i = c - '0';
832 register int count = 0;
835 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
853 /* Print the character C on STREAM as part of the contents of a literal
854 string whose delimiter is QUOTER. Note that this routine should only
855 be call for printing things which are independent of the language
856 of the program being debugged. */
859 gdb_printchar (c, stream, quoter)
865 c &= 0xFF; /* Avoid sign bit follies */
867 if ( c < 0x20 || /* Low control chars */
868 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
869 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
873 fputs_filtered ("\\n", stream);
876 fputs_filtered ("\\b", stream);
879 fputs_filtered ("\\t", stream);
882 fputs_filtered ("\\f", stream);
885 fputs_filtered ("\\r", stream);
888 fputs_filtered ("\\e", stream);
891 fputs_filtered ("\\a", stream);
894 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
898 if (c == '\\' || c == quoter)
899 fputs_filtered ("\\", stream);
900 fprintf_filtered (stream, "%c", c);
904 /* Number of lines per page or UINT_MAX if paging is disabled. */
905 static unsigned int lines_per_page;
906 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
907 static unsigned int chars_per_line;
908 /* Current count of lines printed on this page, chars on this line. */
909 static unsigned int lines_printed, chars_printed;
911 /* Buffer and start column of buffered text, for doing smarter word-
912 wrapping. When someone calls wrap_here(), we start buffering output
913 that comes through fputs_filtered(). If we see a newline, we just
914 spit it out and forget about the wrap_here(). If we see another
915 wrap_here(), we spit it out and remember the newer one. If we see
916 the end of the line, we spit out a newline, the indent, and then
917 the buffered output. */
919 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
920 are waiting to be output (they have already been counted in chars_printed).
921 When wrap_buffer[0] is null, the buffer is empty. */
922 static char *wrap_buffer;
924 /* Pointer in wrap_buffer to the next character to fill. */
925 static char *wrap_pointer;
927 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
929 static char *wrap_indent;
931 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
933 static int wrap_column;
937 set_width_command (args, from_tty, c)
940 struct cmd_list_element *c;
944 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
945 wrap_buffer[0] = '\0';
948 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
949 wrap_pointer = wrap_buffer; /* Start it at the beginning */
952 /* Wait, so the user can read what's on the screen. Prompt the user
953 to continue by pressing RETURN. */
956 prompt_for_continue ()
960 /* We must do this *before* we call gdb_readline, else it will eventually
961 call us -- thinking that we're trying to print beyond the end of the
963 reinitialize_more_filter ();
966 /* On a real operating system, the user can quit with SIGINT.
969 'q' is provided on all systems so users don't have to change habits
970 from system to system, and because telling them what to do in
971 the prompt is more user-friendly than expecting them to think of
974 gdb_readline ("---Type <return> to continue, or q <return> to quit---");
978 while (*p == ' ' || *p == '\t')
981 request_quit (SIGINT);
986 /* Now we have to do this again, so that GDB will know that it doesn't
987 need to save the ---Type <return>--- line at the top of the screen. */
988 reinitialize_more_filter ();
990 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
993 /* Reinitialize filter; ie. tell it to reset to original values. */
996 reinitialize_more_filter ()
1002 /* Indicate that if the next sequence of characters overflows the line,
1003 a newline should be inserted here rather than when it hits the end.
1004 If INDENT is non-null, it is a string to be printed to indent the
1005 wrapped part on the next line. INDENT must remain accessible until
1006 the next call to wrap_here() or until a newline is printed through
1009 If the line is already overfull, we immediately print a newline and
1010 the indentation, and disable further wrapping.
1012 If we don't know the width of lines, but we know the page height,
1013 we must not wrap words, but should still keep track of newlines
1014 that were explicitly printed.
1016 INDENT should not contain tabs, as that will mess up the char count
1017 on the next line. FIXME.
1019 This routine is guaranteed to force out any output which has been
1020 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1021 used to force out output from the wrap_buffer. */
1029 *wrap_pointer = '\0';
1030 fputs (wrap_buffer, gdb_stdout);
1032 wrap_pointer = wrap_buffer;
1033 wrap_buffer[0] = '\0';
1034 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1038 else if (chars_printed >= chars_per_line)
1040 puts_filtered ("\n");
1042 puts_filtered (indent);
1047 wrap_column = chars_printed;
1051 wrap_indent = indent;
1055 /* Ensure that whatever gets printed next, using the filtered output
1056 commands, starts at the beginning of the line. I.E. if there is
1057 any pending output for the current line, flush it and start a new
1058 line. Otherwise do nothing. */
1063 if (chars_printed > 0)
1065 puts_filtered ("\n");
1071 gdb_fopen (name, mode)
1075 return fopen (name, mode);
1078 /* Like fputs but pause after every screenful, and can wrap at points
1079 other than the final character of a line.
1080 Unlike fputs, fputs_filtered does not return a value.
1081 It is OK for LINEBUFFER to be NULL, in which case just don't print
1084 Note that a longjmp to top level may occur in this routine
1085 (since prompt_for_continue may do so) so this routine should not be
1086 called when cleanups are not in place. */
1096 fputs_maybe_filtered (linebuffer, stream, filter)
1097 const char *linebuffer;
1101 const char *lineptr;
1103 if (linebuffer == 0)
1106 /* Don't do any filtering if it is disabled. */
1107 if (stream != gdb_stdout
1108 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1110 fputs (linebuffer, stream);
1114 /* Go through and output each character. Show line extension
1115 when this is necessary; prompt user for new page when this is
1118 lineptr = linebuffer;
1121 /* Possible new page. */
1123 (lines_printed >= lines_per_page - 1))
1124 prompt_for_continue ();
1126 while (*lineptr && *lineptr != '\n')
1128 /* Print a single line. */
1129 if (*lineptr == '\t')
1132 *wrap_pointer++ = '\t';
1134 putc ('\t', stream);
1135 /* Shifting right by 3 produces the number of tab stops
1136 we have already passed, and then adding one and
1137 shifting left 3 advances to the next tab stop. */
1138 chars_printed = ((chars_printed >> 3) + 1) << 3;
1144 *wrap_pointer++ = *lineptr;
1146 putc (*lineptr, stream);
1151 if (chars_printed >= chars_per_line)
1153 unsigned int save_chars = chars_printed;
1157 /* If we aren't actually wrapping, don't output newline --
1158 if chars_per_line is right, we probably just overflowed
1159 anyway; if it's wrong, let us keep going. */
1161 putc ('\n', stream);
1163 /* Possible new page. */
1164 if (lines_printed >= lines_per_page - 1)
1165 prompt_for_continue ();
1167 /* Now output indentation and wrapped string */
1170 fputs (wrap_indent, stream);
1171 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1172 fputs (wrap_buffer, stream); /* and eject it */
1173 /* FIXME, this strlen is what prevents wrap_indent from
1174 containing tabs. However, if we recurse to print it
1175 and count its chars, we risk trouble if wrap_indent is
1176 longer than (the user settable) chars_per_line.
1177 Note also that this can set chars_printed > chars_per_line
1178 if we are printing a long string. */
1179 chars_printed = strlen (wrap_indent)
1180 + (save_chars - wrap_column);
1181 wrap_pointer = wrap_buffer; /* Reset buffer */
1182 wrap_buffer[0] = '\0';
1183 wrap_column = 0; /* And disable fancy wrap */
1188 if (*lineptr == '\n')
1191 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1193 putc ('\n', stream);
1200 fputs_filtered (linebuffer, stream)
1201 const char *linebuffer;
1204 fputs_maybe_filtered (linebuffer, stream, 1);
1208 fputs_unfiltered (linebuffer, stream)
1209 const char *linebuffer;
1212 fputs_maybe_filtered (linebuffer, stream, 0);
1222 fputs_unfiltered (buf, gdb_stdout);
1226 fputc_unfiltered (c, stream)
1233 fputs_unfiltered (buf, stream);
1237 /* Print a variable number of ARGS using format FORMAT. If this
1238 information is going to put the amount written (since the last call
1239 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1240 print out a pause message and do a gdb_readline to get the users
1241 permision to continue.
1243 Unlike fprintf, this function does not return a value.
1245 We implement three variants, vfprintf (takes a vararg list and stream),
1246 fprintf (takes a stream to write on), and printf (the usual).
1248 Note that this routine has a restriction that the length of the
1249 final output line must be less than 255 characters *or* it must be
1250 less than twice the size of the format string. This is a very
1251 arbitrary restriction, but it is an internal restriction, so I'll
1252 put it in. This means that the %s format specifier is almost
1253 useless; unless the caller can GUARANTEE that the string is short
1254 enough, fputs_filtered should be used instead.
1256 Note also that a longjmp to top level may occur in this routine
1257 (since prompt_for_continue may do so) so this routine should not be
1258 called when cleanups are not in place. */
1260 #define MIN_LINEBUF 255
1263 vfprintf_maybe_filtered (stream, format, args, filter)
1269 char line_buf[MIN_LINEBUF+10];
1270 char *linebuffer = line_buf;
1273 format_length = strlen (format);
1275 /* Reallocate buffer to a larger size if this is necessary. */
1276 if (format_length * 2 > MIN_LINEBUF)
1278 linebuffer = alloca (10 + format_length * 2);
1281 /* This won't blow up if the restrictions described above are
1283 vsprintf (linebuffer, format, args);
1285 fputs_maybe_filtered (linebuffer, stream, filter);
1290 vfprintf_filtered (stream, format, args)
1295 vfprintf_maybe_filtered (stream, format, args, 1);
1299 vfprintf_unfiltered (stream, format, args)
1304 vfprintf (stream, format, args);
1308 vprintf_filtered (format, args)
1312 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
1316 vprintf_unfiltered (format, args)
1320 vfprintf (gdb_stdout, format, args);
1325 fprintf_filtered (va_alist)
1333 stream = va_arg (args, FILE *);
1334 format = va_arg (args, char *);
1336 /* This won't blow up if the restrictions described above are
1338 vfprintf_filtered (stream, format, args);
1344 fprintf_unfiltered (va_alist)
1352 stream = va_arg (args, FILE *);
1353 format = va_arg (args, char *);
1355 /* This won't blow up if the restrictions described above are
1357 vfprintf_unfiltered (stream, format, args);
1361 /* Like fprintf_filtered, but prints it's result indent.
1362 Called as fprintfi_filtered (spaces, stream, format, ...); */
1366 fprintfi_filtered (va_alist)
1375 spaces = va_arg (args, int);
1376 stream = va_arg (args, FILE *);
1377 format = va_arg (args, char *);
1378 print_spaces_filtered (spaces, stream);
1380 /* This won't blow up if the restrictions described above are
1382 vfprintf_filtered (stream, format, args);
1389 printf_filtered (va_alist)
1396 format = va_arg (args, char *);
1398 vfprintf_filtered (gdb_stdout, format, args);
1405 printf_unfiltered (va_alist)
1412 format = va_arg (args, char *);
1414 vfprintf_unfiltered (gdb_stdout, format, args);
1418 /* Like printf_filtered, but prints it's result indented.
1419 Called as printfi_filtered (spaces, format, ...); */
1423 printfi_filtered (va_alist)
1431 spaces = va_arg (args, int);
1432 format = va_arg (args, char *);
1433 print_spaces_filtered (spaces, gdb_stdout);
1434 vfprintf_filtered (gdb_stdout, format, args);
1438 /* Easy -- but watch out!
1440 This routine is *not* a replacement for puts()! puts() appends a newline.
1441 This one doesn't, and had better not! */
1444 puts_filtered (string)
1447 fputs_filtered (string, gdb_stdout);
1451 puts_unfiltered (string)
1454 fputs_unfiltered (string, gdb_stdout);
1457 /* Return a pointer to N spaces and a null. The pointer is good
1458 until the next call to here. */
1464 static char *spaces;
1465 static int max_spaces;
1471 spaces = (char *) xmalloc (n+1);
1472 for (t = spaces+n; t != spaces;)
1478 return spaces + max_spaces - n;
1481 /* Print N spaces. */
1483 print_spaces_filtered (n, stream)
1487 fputs_filtered (n_spaces (n), stream);
1490 /* C++ demangler stuff. */
1492 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1493 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1494 If the name is not mangled, or the language for the name is unknown, or
1495 demangling is off, the name is printed in its "raw" form. */
1498 fprintf_symbol_filtered (stream, name, lang, arg_mode)
1508 /* If user wants to see raw output, no problem. */
1511 fputs_filtered (name, stream);
1517 case language_cplus:
1518 demangled = cplus_demangle (name, arg_mode);
1520 case language_chill:
1521 demangled = chill_demangle (name);
1527 fputs_filtered (demangled ? demangled : name, stream);
1528 if (demangled != NULL)
1536 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1537 differences in whitespace. Returns 0 if they match, non-zero if they
1538 don't (slightly different than strcmp()'s range of return values).
1540 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1541 This "feature" is useful when searching for matching C++ function names
1542 (such as if the user types 'break FOO', where FOO is a mangled C++
1546 strcmp_iw (string1, string2)
1547 const char *string1;
1548 const char *string2;
1550 while ((*string1 != '\0') && (*string2 != '\0'))
1552 while (isspace (*string1))
1556 while (isspace (*string2))
1560 if (*string1 != *string2)
1564 if (*string1 != '\0')
1570 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1575 _initialize_utils ()
1577 struct cmd_list_element *c;
1579 c = add_set_cmd ("width", class_support, var_uinteger,
1580 (char *)&chars_per_line,
1581 "Set number of characters gdb thinks are in a line.",
1583 add_show_from_set (c, &showlist);
1584 c->function.sfunc = set_width_command;
1587 (add_set_cmd ("height", class_support,
1588 var_uinteger, (char *)&lines_per_page,
1589 "Set number of lines gdb thinks are in a page.", &setlist),
1592 /* These defaults will be used if we are unable to get the correct
1593 values from termcap. */
1594 #if defined(__GO32__)
1595 lines_per_page = ScreenRows();
1596 chars_per_line = ScreenCols();
1598 lines_per_page = 24;
1599 chars_per_line = 80;
1600 /* Initialize the screen height and width from termcap. */
1602 char *termtype = getenv ("TERM");
1604 /* Positive means success, nonpositive means failure. */
1607 /* 2048 is large enough for all known terminals, according to the
1608 GNU termcap manual. */
1609 char term_buffer[2048];
1613 status = tgetent (term_buffer, termtype);
1618 val = tgetnum ("li");
1620 lines_per_page = val;
1622 /* The number of lines per page is not mentioned
1623 in the terminal description. This probably means
1624 that paging is not useful (e.g. emacs shell window),
1625 so disable paging. */
1626 lines_per_page = UINT_MAX;
1628 val = tgetnum ("co");
1630 chars_per_line = val;
1635 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1637 /* If there is a better way to determine the window size, use it. */
1638 SIGWINCH_HANDLER ();
1641 /* If the output is not a terminal, don't paginate it. */
1642 if (!ISATTY (gdb_stdout))
1643 lines_per_page = UINT_MAX;
1645 set_width_command ((char *)NULL, 0, c);
1648 (add_set_cmd ("demangle", class_support, var_boolean,
1650 "Set demangling of encoded C++ names when displaying symbols.",
1655 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1656 (char *)&sevenbit_strings,
1657 "Set printing of 8-bit characters in strings as \\nnn.",
1662 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1663 (char *)&asm_demangle,
1664 "Set demangling of C++ names in disassembly listings.",
1669 /* Machine specific function to handle SIGWINCH signal. */
1671 #ifdef SIGWINCH_HANDLER_BODY
1672 SIGWINCH_HANDLER_BODY