1 /* General utility routines for GDB, the GNU debugger.
2 Copyright 1986, 1989, 1990, 1991, 1992, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #if !defined(__GO32__) && !defined(WIN32)
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
26 #ifdef ANSI_PROTOTYPES
32 #include "gdb_string.h"
43 #include "expression.h"
49 /* readline defines this. */
52 /* Prototypes for local functions */
54 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
58 malloc_botch PARAMS ((void));
60 #endif /* NO_MMALLOC, etc */
63 fatal_dump_core PARAMS((char *, ...));
66 prompt_for_continue PARAMS ((void));
69 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
71 /* If this definition isn't overridden by the header files, assume
72 that isatty and fileno exist on this system. */
74 #define ISATTY(FP) (isatty (fileno (FP)))
77 /* Chain of cleanup actions established with make_cleanup,
78 to be executed if an error happens. */
80 static struct cleanup *cleanup_chain;
82 /* Nonzero if we have job control. */
86 /* Nonzero means a quit has been requested. */
90 /* Nonzero means quit immediately if Control-C is typed now, rather
91 than waiting until QUIT is executed. Be careful in setting this;
92 code which executes with immediate_quit set has to be very careful
93 about being able to deal with being interrupted at any time. It is
94 almost always better to use QUIT; the only exception I can think of
95 is being able to quit out of a system call (using EINTR loses if
96 the SIGINT happens between the previous QUIT and the system call).
97 To immediately quit in the case in which a SIGINT happens between
98 the previous QUIT and setting immediate_quit (desirable anytime we
99 expect to block), call QUIT after setting immediate_quit. */
103 /* Nonzero means that encoded C++ names should be printed out in their
104 C++ form rather than raw. */
108 /* Nonzero means that encoded C++ names should be printed out in their
109 C++ form even in assembler language displays. If this is set, but
110 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
112 int asm_demangle = 0;
114 /* Nonzero means that strings with character values >0x7F should be printed
115 as octal escapes. Zero means just print the value (e.g. it's an
116 international character, and the terminal or window can cope.) */
118 int sevenbit_strings = 0;
120 /* String to be printed before error messages, if any. */
122 char *error_pre_print;
124 /* String to be printed before quit messages, if any. */
126 char *quit_pre_print;
128 /* String to be printed before warning messages, if any. */
130 char *warning_pre_print = "\nwarning: ";
132 /* Add a new cleanup to the cleanup_chain,
133 and return the previous chain pointer
134 to be passed later to do_cleanups or discard_cleanups.
135 Args are FUNCTION to clean up with, and ARG to pass to it. */
138 make_cleanup (function, arg)
139 void (*function) PARAMS ((PTR));
142 register struct cleanup *new
143 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
144 register struct cleanup *old_chain = cleanup_chain;
146 new->next = cleanup_chain;
147 new->function = function;
154 /* Discard cleanups and do the actions they describe
155 until we get back to the point OLD_CHAIN in the cleanup_chain. */
158 do_cleanups (old_chain)
159 register struct cleanup *old_chain;
161 register struct cleanup *ptr;
162 while ((ptr = cleanup_chain) != old_chain)
164 cleanup_chain = ptr->next; /* Do this first incase recursion */
165 (*ptr->function) (ptr->arg);
170 /* Discard cleanups, not doing the actions they describe,
171 until we get back to the point OLD_CHAIN in the cleanup_chain. */
174 discard_cleanups (old_chain)
175 register struct cleanup *old_chain;
177 register struct cleanup *ptr;
178 while ((ptr = cleanup_chain) != old_chain)
180 cleanup_chain = ptr->next;
185 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
189 struct cleanup *old_chain = cleanup_chain;
195 /* Restore the cleanup chain from a previously saved chain. */
197 restore_cleanups (chain)
198 struct cleanup *chain;
200 cleanup_chain = chain;
203 /* This function is useful for cleanups.
207 old_chain = make_cleanup (free_current_contents, &foo);
209 to arrange to free the object thus allocated. */
212 free_current_contents (location)
218 /* Provide a known function that does nothing, to use as a base for
219 for a possibly long chain of cleanups. This is useful where we
220 use the cleanup chain for handling normal cleanups as well as dealing
221 with cleanups that need to be done as a result of a call to error().
222 In such cases, we may not be certain where the first cleanup is, unless
223 we have a do-nothing one to always use as the base. */
233 /* Print a warning message. Way to use this is to call warning_begin,
234 output the warning message (use unfiltered output to gdb_stderr),
235 ending in a newline. There is not currently a warning_end that you
236 call afterwards, but such a thing might be added if it is useful
237 for a GUI to separate warning messages from other output.
239 FIXME: Why do warnings use unfiltered output and errors filtered?
240 Is this anything other than a historical accident? */
245 target_terminal_ours ();
246 wrap_here(""); /* Force out any buffered output */
247 gdb_flush (gdb_stdout);
248 if (warning_pre_print)
249 fprintf_unfiltered (gdb_stderr, warning_pre_print);
252 /* Print a warning message.
253 The first argument STRING is the warning message, used as a fprintf string,
254 and the remaining args are passed as arguments to it.
255 The primary difference between warnings and errors is that a warning
256 does not force the return to command level. */
260 #ifdef ANSI_PROTOTYPES
261 warning (char *string, ...)
268 #ifdef ANSI_PROTOTYPES
269 va_start (args, string);
274 string = va_arg (args, char *);
277 vfprintf_unfiltered (gdb_stderr, string, args);
278 fprintf_unfiltered (gdb_stderr, "\n");
282 /* Start the printing of an error message. Way to use this is to call
283 this, output the error message (use filtered output to gdb_stderr
284 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
285 in a newline, and then call return_to_top_level (RETURN_ERROR).
286 error() provides a convenient way to do this for the special case
287 that the error message can be formatted with a single printf call,
288 but this is more general. */
292 target_terminal_ours ();
293 wrap_here (""); /* Force out any buffered output */
294 gdb_flush (gdb_stdout);
296 annotate_error_begin ();
299 fprintf_filtered (gdb_stderr, error_pre_print);
302 /* Print an error message and return to command level.
303 The first argument STRING is the error message, used as a fprintf string,
304 and the remaining args are passed as arguments to it. */
306 #ifdef ANSI_PROTOTYPES
308 error (char *string, ...)
316 #ifdef ANSI_PROTOTYPES
317 va_start (args, string);
326 #ifdef ANSI_PROTOTYPES
327 vfprintf_filtered (gdb_stderr, string, args);
332 string1 = va_arg (args, char *);
333 vfprintf_filtered (gdb_stderr, string1, args);
336 fprintf_filtered (gdb_stderr, "\n");
338 return_to_top_level (RETURN_ERROR);
343 /* Print an error message and exit reporting failure.
344 This is for a error that we cannot continue from.
345 The arguments are printed a la printf.
347 This function cannot be declared volatile (NORETURN) in an
348 ANSI environment because exit() is not declared volatile. */
352 #ifdef ANSI_PROTOTYPES
353 fatal (char *string, ...)
360 #ifdef ANSI_PROTOTYPES
361 va_start (args, string);
365 string = va_arg (args, char *);
367 fprintf_unfiltered (gdb_stderr, "\ngdb: ");
368 vfprintf_unfiltered (gdb_stderr, string, args);
369 fprintf_unfiltered (gdb_stderr, "\n");
374 /* Print an error message and exit, dumping core.
375 The arguments are printed a la printf (). */
379 #ifdef ANSI_PROTOTYPES
380 fatal_dump_core (char *string, ...)
382 fatal_dump_core (va_alist)
387 #ifdef ANSI_PROTOTYPES
388 va_start (args, string);
393 string = va_arg (args, char *);
395 /* "internal error" is always correct, since GDB should never dump
396 core, no matter what the input. */
397 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: ");
398 vfprintf_unfiltered (gdb_stderr, string, args);
399 fprintf_unfiltered (gdb_stderr, "\n");
402 signal (SIGQUIT, SIG_DFL);
403 kill (getpid (), SIGQUIT);
404 /* We should never get here, but just in case... */
408 /* The strerror() function can return NULL for errno values that are
409 out of range. Provide a "safe" version that always returns a
413 safe_strerror (errnum)
419 if ((msg = strerror (errnum)) == NULL)
421 sprintf (buf, "(undocumented errno %d)", errnum);
427 /* The strsignal() function can return NULL for signal values that are
428 out of range. Provide a "safe" version that always returns a
432 safe_strsignal (signo)
438 if ((msg = strsignal (signo)) == NULL)
440 sprintf (buf, "(undocumented signal %d)", signo);
447 /* Print the system error message for errno, and also mention STRING
448 as the file name for which the error was encountered.
449 Then return to command level. */
452 perror_with_name (string)
458 err = safe_strerror (errno);
459 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
460 strcpy (combined, string);
461 strcat (combined, ": ");
462 strcat (combined, err);
464 /* I understand setting these is a matter of taste. Still, some people
465 may clear errno but not know about bfd_error. Doing this here is not
467 bfd_set_error (bfd_error_no_error);
470 error ("%s.", combined);
473 /* Print the system error message for ERRCODE, and also mention STRING
474 as the file name for which the error was encountered. */
477 print_sys_errmsg (string, errcode)
484 err = safe_strerror (errcode);
485 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
486 strcpy (combined, string);
487 strcat (combined, ": ");
488 strcat (combined, err);
490 /* We want anything which was printed on stdout to come out first, before
492 gdb_flush (gdb_stdout);
493 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
496 /* Control C eventually causes this to be called, at a convenient time. */
501 serial_t gdb_stdout_serial = serial_fdopen (1);
503 target_terminal_ours ();
505 /* We want all output to appear now, before we print "Quit". We
506 have 3 levels of buffering we have to flush (it's possible that
507 some of these should be changed to flush the lower-level ones
510 /* 1. The _filtered buffer. */
511 wrap_here ((char *)0);
513 /* 2. The stdio buffer. */
514 gdb_flush (gdb_stdout);
515 gdb_flush (gdb_stderr);
517 /* 3. The system-level buffer. */
518 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial);
519 SERIAL_UN_FDOPEN (gdb_stdout_serial);
521 annotate_error_begin ();
523 /* Don't use *_filtered; we don't want to prompt the user to continue. */
525 fprintf_unfiltered (gdb_stderr, quit_pre_print);
528 /* If there is no terminal switching for this target, then we can't
529 possibly get screwed by the lack of job control. */
530 || current_target.to_terminal_ours == NULL)
531 fprintf_unfiltered (gdb_stderr, "Quit\n");
533 fprintf_unfiltered (gdb_stderr,
534 "Quit (expect signal SIGINT when the program is resumed)\n");
535 return_to_top_level (RETURN_QUIT);
539 #if defined(__GO32__)||defined(WINGDB)
541 /* In the absence of signals, poll keyboard for a quit.
542 Called from #define QUIT pollquit() in xm-go32.h. */
560 /* We just ignore it */
561 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
568 #if defined(__GO32__)||defined(WINGDB)
583 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
590 /* Done by signals */
593 /* Control C comes here */
600 /* Restore the signal handler. Harmless with BSD-style signals, needed
601 for System V-style signals. So just always do it, rather than worrying
602 about USG defines and stuff like that. */
603 signal (signo, request_quit);
614 /* Memory management stuff (malloc friends). */
616 #if defined (NO_MMALLOC)
618 /* Make a substitute size_t for non-ANSI compilers. */
625 #define size_t unsigned int
635 return malloc (size);
639 mrealloc (md, ptr, size)
644 if (ptr == 0) /* Guard against old realloc's */
645 return malloc (size);
647 return realloc (ptr, size);
658 #endif /* NO_MMALLOC */
660 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
668 #else /* have mmalloc and want corruption checking */
673 fatal_dump_core ("Memory corruption");
676 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
677 by MD, to detect memory corruption. Note that MD may be NULL to specify
678 the default heap that grows via sbrk.
680 Note that for freshly created regions, we must call mmcheck prior to any
681 mallocs in the region. Otherwise, any region which was allocated prior to
682 installing the checking hooks, which is later reallocated or freed, will
683 fail the checks! The mmcheck function only allows initial hooks to be
684 installed before the first mmalloc. However, anytime after we have called
685 mmcheck the first time to install the checking hooks, we can call it again
686 to update the function pointer to the memory corruption handler.
688 Returns zero on failure, non-zero on success. */
694 if (!mmcheck (md, malloc_botch))
696 warning ("internal error: failed to install memory consistency checks");
702 #endif /* Have mmalloc and want corruption checking */
704 /* Called when a memory allocation fails, with the number of bytes of
705 memory requested in SIZE. */
713 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
717 fatal ("virtual memory exhausted.");
721 /* Like mmalloc but get error if no storage available, and protect against
722 the caller wanting to allocate zero bytes. Whether to return NULL for
723 a zero byte request, or translate the request into a request for one
724 byte of zero'd storage, is a religious issue. */
737 else if ((val = mmalloc (md, size)) == NULL)
744 /* Like mrealloc but get error if no storage available. */
747 xmrealloc (md, ptr, size)
756 val = mrealloc (md, ptr, size);
760 val = mmalloc (md, size);
769 /* Like malloc but get error if no storage available, and protect against
770 the caller wanting to allocate zero bytes. */
776 return (xmmalloc ((PTR) NULL, size));
779 /* Like mrealloc but get error if no storage available. */
786 return (xmrealloc ((PTR) NULL, ptr, size));
790 /* My replacement for the read system call.
791 Used like `read' but keeps going if `read' returns too soon. */
794 myread (desc, addr, len)
804 val = read (desc, addr, len);
815 /* Make a copy of the string at PTR with SIZE characters
816 (and add a null character at the end in the copy).
817 Uses malloc to get the space. Returns the address of the copy. */
820 savestring (ptr, size)
824 register char *p = (char *) xmalloc (size + 1);
825 memcpy (p, ptr, size);
831 msavestring (md, ptr, size)
836 register char *p = (char *) xmmalloc (md, size + 1);
837 memcpy (p, ptr, size);
842 /* The "const" is so it compiles under DGUX (which prototypes strsave
843 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
844 Doesn't real strsave return NULL if out of memory? */
849 return savestring (ptr, strlen (ptr));
857 return (msavestring (md, ptr, strlen (ptr)));
861 print_spaces (n, file)
869 /* Print a host address. */
872 gdb_print_address (addr, stream)
877 /* We could use the %p conversion specifier to fprintf if we had any
878 way of knowing whether this host supports it. But the following
879 should work on the Alpha and on 32 bit machines. */
881 fprintf_filtered (stream, "0x%lx", (unsigned long)addr);
884 /* Ask user a y-or-n question and return 1 iff answer is yes.
885 Takes three args which are given to printf to print the question.
886 The first, a control string, should end in "? ".
887 It should not say how to answer, because we do that. */
891 #ifdef ANSI_PROTOTYPES
892 query (char *ctlstr, ...)
903 #ifdef ANSI_PROTOTYPES
904 va_start (args, ctlstr);
908 ctlstr = va_arg (args, char *);
913 return query_hook (ctlstr, args);
916 /* Automatically answer "yes" if input is not from a terminal. */
917 if (!input_from_terminal_p ())
920 /* FIXME Automatically answer "yes" if called from MacGDB. */
927 wrap_here (""); /* Flush any buffered output */
928 gdb_flush (gdb_stdout);
930 if (annotation_level > 1)
931 printf_filtered ("\n\032\032pre-query\n");
933 vfprintf_filtered (gdb_stdout, ctlstr, args);
934 printf_filtered ("(y or n) ");
936 if (annotation_level > 1)
937 printf_filtered ("\n\032\032query\n");
940 /* If not in MacGDB, move to a new line so the entered line doesn't
941 have a prompt on the front of it. */
943 fputs_unfiltered ("\n", gdb_stdout);
946 gdb_flush (gdb_stdout);
947 answer = fgetc (stdin);
948 clearerr (stdin); /* in case of C-d */
949 if (answer == EOF) /* C-d */
954 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
957 ans2 = fgetc (stdin);
960 while (ans2 != EOF && ans2 != '\n');
973 printf_filtered ("Please answer y or n.\n");
976 if (annotation_level > 1)
977 printf_filtered ("\n\032\032post-query\n");
982 /* Parse a C escape sequence. STRING_PTR points to a variable
983 containing a pointer to the string to parse. That pointer
984 should point to the character after the \. That pointer
985 is updated past the characters we use. The value of the
986 escape sequence is returned.
988 A negative value means the sequence \ newline was seen,
989 which is supposed to be equivalent to nothing at all.
991 If \ is followed by a null character, we return a negative
992 value and leave the string pointer pointing at the null character.
994 If \ is followed by 000, we return 0 and leave the string pointer
995 after the zeros. A value of 0 does not mean end of string. */
998 parse_escape (string_ptr)
1001 register int c = *(*string_ptr)++;
1005 return 007; /* Bell (alert) char */
1008 case 'e': /* Escape character */
1026 c = *(*string_ptr)++;
1028 c = parse_escape (string_ptr);
1031 return (c & 0200) | (c & 037);
1042 register int i = c - '0';
1043 register int count = 0;
1046 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
1064 /* Print the character C on STREAM as part of the contents of a literal
1065 string whose delimiter is QUOTER. Note that this routine should only
1066 be call for printing things which are independent of the language
1067 of the program being debugged. */
1070 gdb_printchar (c, stream, quoter)
1076 c &= 0xFF; /* Avoid sign bit follies */
1078 if ( c < 0x20 || /* Low control chars */
1079 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1080 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
1084 fputs_filtered ("\\n", stream);
1087 fputs_filtered ("\\b", stream);
1090 fputs_filtered ("\\t", stream);
1093 fputs_filtered ("\\f", stream);
1096 fputs_filtered ("\\r", stream);
1099 fputs_filtered ("\\e", stream);
1102 fputs_filtered ("\\a", stream);
1105 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
1109 if (c == '\\' || c == quoter)
1110 fputs_filtered ("\\", stream);
1111 fprintf_filtered (stream, "%c", c);
1115 /* Number of lines per page or UINT_MAX if paging is disabled. */
1116 static unsigned int lines_per_page;
1117 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1118 static unsigned int chars_per_line;
1119 /* Current count of lines printed on this page, chars on this line. */
1120 static unsigned int lines_printed, chars_printed;
1122 /* Buffer and start column of buffered text, for doing smarter word-
1123 wrapping. When someone calls wrap_here(), we start buffering output
1124 that comes through fputs_filtered(). If we see a newline, we just
1125 spit it out and forget about the wrap_here(). If we see another
1126 wrap_here(), we spit it out and remember the newer one. If we see
1127 the end of the line, we spit out a newline, the indent, and then
1128 the buffered output. */
1130 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1131 are waiting to be output (they have already been counted in chars_printed).
1132 When wrap_buffer[0] is null, the buffer is empty. */
1133 static char *wrap_buffer;
1135 /* Pointer in wrap_buffer to the next character to fill. */
1136 static char *wrap_pointer;
1138 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1140 static char *wrap_indent;
1142 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1143 is not in effect. */
1144 static int wrap_column;
1148 set_width_command (args, from_tty, c)
1151 struct cmd_list_element *c;
1155 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1156 wrap_buffer[0] = '\0';
1159 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1160 wrap_pointer = wrap_buffer; /* Start it at the beginning */
1163 /* Wait, so the user can read what's on the screen. Prompt the user
1164 to continue by pressing RETURN. */
1167 prompt_for_continue ()
1170 char cont_prompt[120];
1172 if (annotation_level > 1)
1173 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1175 strcpy (cont_prompt,
1176 "---Type <return> to continue, or q <return> to quit---");
1177 if (annotation_level > 1)
1178 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1180 /* We must do this *before* we call gdb_readline, else it will eventually
1181 call us -- thinking that we're trying to print beyond the end of the
1183 reinitialize_more_filter ();
1186 /* On a real operating system, the user can quit with SIGINT.
1189 'q' is provided on all systems so users don't have to change habits
1190 from system to system, and because telling them what to do in
1191 the prompt is more user-friendly than expecting them to think of
1193 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1194 whereas control-C to gdb_readline will cause the user to get dumped
1196 ignore = readline (cont_prompt);
1198 if (annotation_level > 1)
1199 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1204 while (*p == ' ' || *p == '\t')
1207 request_quit (SIGINT);
1212 /* Now we have to do this again, so that GDB will know that it doesn't
1213 need to save the ---Type <return>--- line at the top of the screen. */
1214 reinitialize_more_filter ();
1216 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1219 /* Reinitialize filter; ie. tell it to reset to original values. */
1222 reinitialize_more_filter ()
1228 /* Indicate that if the next sequence of characters overflows the line,
1229 a newline should be inserted here rather than when it hits the end.
1230 If INDENT is non-null, it is a string to be printed to indent the
1231 wrapped part on the next line. INDENT must remain accessible until
1232 the next call to wrap_here() or until a newline is printed through
1235 If the line is already overfull, we immediately print a newline and
1236 the indentation, and disable further wrapping.
1238 If we don't know the width of lines, but we know the page height,
1239 we must not wrap words, but should still keep track of newlines
1240 that were explicitly printed.
1242 INDENT should not contain tabs, as that will mess up the char count
1243 on the next line. FIXME.
1245 This routine is guaranteed to force out any output which has been
1246 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1247 used to force out output from the wrap_buffer. */
1253 /* This should have been allocated, but be paranoid anyway. */
1259 *wrap_pointer = '\0';
1260 fputs_unfiltered (wrap_buffer, gdb_stdout);
1262 wrap_pointer = wrap_buffer;
1263 wrap_buffer[0] = '\0';
1264 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1268 else if (chars_printed >= chars_per_line)
1270 puts_filtered ("\n");
1272 puts_filtered (indent);
1277 wrap_column = chars_printed;
1281 wrap_indent = indent;
1285 /* Ensure that whatever gets printed next, using the filtered output
1286 commands, starts at the beginning of the line. I.E. if there is
1287 any pending output for the current line, flush it and start a new
1288 line. Otherwise do nothing. */
1293 if (chars_printed > 0)
1295 puts_filtered ("\n");
1301 gdb_fopen (name, mode)
1305 return fopen (name, mode);
1314 flush_hook (stream);
1321 /* Like fputs but if FILTER is true, pause after every screenful.
1323 Regardless of FILTER can wrap at points other than the final
1324 character of a line.
1326 Unlike fputs, fputs_maybe_filtered does not return a value.
1327 It is OK for LINEBUFFER to be NULL, in which case just don't print
1330 Note that a longjmp to top level may occur in this routine (only if
1331 FILTER is true) (since prompt_for_continue may do so) so this
1332 routine should not be called when cleanups are not in place. */
1335 fputs_maybe_filtered (linebuffer, stream, filter)
1336 const char *linebuffer;
1340 const char *lineptr;
1342 if (linebuffer == 0)
1345 /* Don't do any filtering if it is disabled. */
1346 if (stream != gdb_stdout
1347 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1349 fputs_unfiltered (linebuffer, stream);
1353 /* Go through and output each character. Show line extension
1354 when this is necessary; prompt user for new page when this is
1357 lineptr = linebuffer;
1360 /* Possible new page. */
1362 (lines_printed >= lines_per_page - 1))
1363 prompt_for_continue ();
1365 while (*lineptr && *lineptr != '\n')
1367 /* Print a single line. */
1368 if (*lineptr == '\t')
1371 *wrap_pointer++ = '\t';
1373 fputc_unfiltered ('\t', stream);
1374 /* Shifting right by 3 produces the number of tab stops
1375 we have already passed, and then adding one and
1376 shifting left 3 advances to the next tab stop. */
1377 chars_printed = ((chars_printed >> 3) + 1) << 3;
1383 *wrap_pointer++ = *lineptr;
1385 fputc_unfiltered (*lineptr, stream);
1390 if (chars_printed >= chars_per_line)
1392 unsigned int save_chars = chars_printed;
1396 /* If we aren't actually wrapping, don't output newline --
1397 if chars_per_line is right, we probably just overflowed
1398 anyway; if it's wrong, let us keep going. */
1400 fputc_unfiltered ('\n', stream);
1402 /* Possible new page. */
1403 if (lines_printed >= lines_per_page - 1)
1404 prompt_for_continue ();
1406 /* Now output indentation and wrapped string */
1409 fputs_unfiltered (wrap_indent, stream);
1410 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1411 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
1412 /* FIXME, this strlen is what prevents wrap_indent from
1413 containing tabs. However, if we recurse to print it
1414 and count its chars, we risk trouble if wrap_indent is
1415 longer than (the user settable) chars_per_line.
1416 Note also that this can set chars_printed > chars_per_line
1417 if we are printing a long string. */
1418 chars_printed = strlen (wrap_indent)
1419 + (save_chars - wrap_column);
1420 wrap_pointer = wrap_buffer; /* Reset buffer */
1421 wrap_buffer[0] = '\0';
1422 wrap_column = 0; /* And disable fancy wrap */
1427 if (*lineptr == '\n')
1430 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1432 fputc_unfiltered ('\n', stream);
1439 fputs_filtered (linebuffer, stream)
1440 const char *linebuffer;
1443 fputs_maybe_filtered (linebuffer, stream, 1);
1447 putchar_unfiltered (c)
1454 fputs_unfiltered (buf, gdb_stdout);
1459 fputc_unfiltered (c, stream)
1467 fputs_unfiltered (buf, stream);
1472 /* Print a variable number of ARGS using format FORMAT. If this
1473 information is going to put the amount written (since the last call
1474 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1475 call prompt_for_continue to get the users permision to continue.
1477 Unlike fprintf, this function does not return a value.
1479 We implement three variants, vfprintf (takes a vararg list and stream),
1480 fprintf (takes a stream to write on), and printf (the usual).
1482 Note also that a longjmp to top level may occur in this routine
1483 (since prompt_for_continue may do so) so this routine should not be
1484 called when cleanups are not in place. */
1487 vfprintf_maybe_filtered (stream, format, args, filter)
1494 struct cleanup *old_cleanups;
1496 vasprintf (&linebuffer, format, args);
1497 if (linebuffer == NULL)
1499 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
1502 old_cleanups = make_cleanup (free, linebuffer);
1503 fputs_maybe_filtered (linebuffer, stream, filter);
1504 do_cleanups (old_cleanups);
1509 vfprintf_filtered (stream, format, args)
1514 vfprintf_maybe_filtered (stream, format, args, 1);
1518 vfprintf_unfiltered (stream, format, args)
1524 struct cleanup *old_cleanups;
1526 vasprintf (&linebuffer, format, args);
1527 if (linebuffer == NULL)
1529 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
1532 old_cleanups = make_cleanup (free, linebuffer);
1533 fputs_unfiltered (linebuffer, stream);
1534 do_cleanups (old_cleanups);
1538 vprintf_filtered (format, args)
1542 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
1546 vprintf_unfiltered (format, args)
1550 vfprintf_unfiltered (gdb_stdout, format, args);
1555 #ifdef ANSI_PROTOTYPES
1556 fprintf_filtered (FILE *stream, char *format, ...)
1558 fprintf_filtered (va_alist)
1563 #ifdef ANSI_PROTOTYPES
1564 va_start (args, format);
1570 stream = va_arg (args, FILE *);
1571 format = va_arg (args, char *);
1573 vfprintf_filtered (stream, format, args);
1579 #ifdef ANSI_PROTOTYPES
1580 fprintf_unfiltered (FILE *stream, char *format, ...)
1582 fprintf_unfiltered (va_alist)
1587 #ifdef ANSI_PROTOTYPES
1588 va_start (args, format);
1594 stream = va_arg (args, FILE *);
1595 format = va_arg (args, char *);
1597 vfprintf_unfiltered (stream, format, args);
1601 /* Like fprintf_filtered, but prints its result indented.
1602 Called as fprintfi_filtered (spaces, stream, format, ...); */
1606 #ifdef ANSI_PROTOTYPES
1607 fprintfi_filtered (int spaces, FILE *stream, char *format, ...)
1609 fprintfi_filtered (va_alist)
1614 #ifdef ANSI_PROTOTYPES
1615 va_start (args, format);
1622 spaces = va_arg (args, int);
1623 stream = va_arg (args, FILE *);
1624 format = va_arg (args, char *);
1626 print_spaces_filtered (spaces, stream);
1628 vfprintf_filtered (stream, format, args);
1635 #ifdef ANSI_PROTOTYPES
1636 printf_filtered (char *format, ...)
1638 printf_filtered (va_alist)
1643 #ifdef ANSI_PROTOTYPES
1644 va_start (args, format);
1649 format = va_arg (args, char *);
1651 vfprintf_filtered (gdb_stdout, format, args);
1658 #ifdef ANSI_PROTOTYPES
1659 printf_unfiltered (char *format, ...)
1661 printf_unfiltered (va_alist)
1666 #ifdef ANSI_PROTOTYPES
1667 va_start (args, format);
1672 format = va_arg (args, char *);
1674 vfprintf_unfiltered (gdb_stdout, format, args);
1678 /* Like printf_filtered, but prints it's result indented.
1679 Called as printfi_filtered (spaces, format, ...); */
1683 #ifdef ANSI_PROTOTYPES
1684 printfi_filtered (int spaces, char *format, ...)
1686 printfi_filtered (va_alist)
1691 #ifdef ANSI_PROTOTYPES
1692 va_start (args, format);
1698 spaces = va_arg (args, int);
1699 format = va_arg (args, char *);
1701 print_spaces_filtered (spaces, gdb_stdout);
1702 vfprintf_filtered (gdb_stdout, format, args);
1706 /* Easy -- but watch out!
1708 This routine is *not* a replacement for puts()! puts() appends a newline.
1709 This one doesn't, and had better not! */
1712 puts_filtered (string)
1715 fputs_filtered (string, gdb_stdout);
1719 puts_unfiltered (string)
1722 fputs_unfiltered (string, gdb_stdout);
1725 /* Return a pointer to N spaces and a null. The pointer is good
1726 until the next call to here. */
1732 static char *spaces;
1733 static int max_spaces;
1739 spaces = (char *) xmalloc (n+1);
1740 for (t = spaces+n; t != spaces;)
1746 return spaces + max_spaces - n;
1749 /* Print N spaces. */
1751 print_spaces_filtered (n, stream)
1755 fputs_filtered (n_spaces (n), stream);
1758 /* C++ demangler stuff. */
1760 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1761 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1762 If the name is not mangled, or the language for the name is unknown, or
1763 demangling is off, the name is printed in its "raw" form. */
1766 fprintf_symbol_filtered (stream, name, lang, arg_mode)
1776 /* If user wants to see raw output, no problem. */
1779 fputs_filtered (name, stream);
1785 case language_cplus:
1786 demangled = cplus_demangle (name, arg_mode);
1788 case language_chill:
1789 demangled = chill_demangle (name);
1795 fputs_filtered (demangled ? demangled : name, stream);
1796 if (demangled != NULL)
1804 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1805 differences in whitespace. Returns 0 if they match, non-zero if they
1806 don't (slightly different than strcmp()'s range of return values).
1808 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1809 This "feature" is useful when searching for matching C++ function names
1810 (such as if the user types 'break FOO', where FOO is a mangled C++
1814 strcmp_iw (string1, string2)
1815 const char *string1;
1816 const char *string2;
1818 while ((*string1 != '\0') && (*string2 != '\0'))
1820 while (isspace (*string1))
1824 while (isspace (*string2))
1828 if (*string1 != *string2)
1832 if (*string1 != '\0')
1838 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1845 struct cmd_list_element *c;
1847 c = add_set_cmd ("width", class_support, var_uinteger,
1848 (char *)&chars_per_line,
1849 "Set number of characters gdb thinks are in a line.",
1851 add_show_from_set (c, &showlist);
1852 c->function.sfunc = set_width_command;
1855 (add_set_cmd ("height", class_support,
1856 var_uinteger, (char *)&lines_per_page,
1857 "Set number of lines gdb thinks are in a page.", &setlist),
1860 /* These defaults will be used if we are unable to get the correct
1861 values from termcap. */
1862 #if defined(__GO32__) || defined(WIN32)
1863 lines_per_page = ScreenRows();
1864 chars_per_line = ScreenCols();
1866 lines_per_page = 24;
1867 chars_per_line = 80;
1870 /* No termcap under MPW, although might be cool to do something
1871 by looking at worksheet or console window sizes. */
1872 /* Initialize the screen height and width from termcap. */
1874 char *termtype = getenv ("TERM");
1876 /* Positive means success, nonpositive means failure. */
1879 /* 2048 is large enough for all known terminals, according to the
1880 GNU termcap manual. */
1881 char term_buffer[2048];
1885 status = tgetent (term_buffer, termtype);
1890 val = tgetnum ("li");
1892 lines_per_page = val;
1894 /* The number of lines per page is not mentioned
1895 in the terminal description. This probably means
1896 that paging is not useful (e.g. emacs shell window),
1897 so disable paging. */
1898 lines_per_page = UINT_MAX;
1900 val = tgetnum ("co");
1902 chars_per_line = val;
1908 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1910 /* If there is a better way to determine the window size, use it. */
1911 SIGWINCH_HANDLER ();
1914 /* If the output is not a terminal, don't paginate it. */
1915 if (!ISATTY (gdb_stdout))
1916 lines_per_page = UINT_MAX;
1918 set_width_command ((char *)NULL, 0, c);
1921 (add_set_cmd ("demangle", class_support, var_boolean,
1923 "Set demangling of encoded C++ names when displaying symbols.",
1928 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1929 (char *)&sevenbit_strings,
1930 "Set printing of 8-bit characters in strings as \\nnn.",
1935 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1936 (char *)&asm_demangle,
1937 "Set demangling of C++ names in disassembly listings.",
1942 /* Machine specific function to handle SIGWINCH signal. */
1944 #ifdef SIGWINCH_HANDLER_BODY
1945 SIGWINCH_HANDLER_BODY