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"
42 /* readline defines this. */
45 /* Prototypes for local functions */
47 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
51 malloc_botch PARAMS ((void));
53 #endif /* NO_MMALLOC, etc */
56 fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
59 prompt_for_continue PARAMS ((void));
62 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
64 /* If this definition isn't overridden by the header files, assume
65 that isatty and fileno exist on this system. */
67 #define ISATTY(FP) (isatty (fileno (FP)))
70 /* Chain of cleanup actions established with make_cleanup,
71 to be executed if an error happens. */
73 static struct cleanup *cleanup_chain;
75 /* Nonzero means a quit has been requested. */
79 /* Nonzero means quit immediately if Control-C is typed now, rather
80 than waiting until QUIT is executed. Be careful in setting this;
81 code which executes with immediate_quit set has to be very careful
82 about being able to deal with being interrupted at any time. It is
83 almost always better to use QUIT; the only exception I can think of
84 is being able to quit out of a system call (using EINTR loses if
85 the SIGINT happens between the previous QUIT and the system call).
86 To immediately quit in the case in which a SIGINT happens between
87 the previous QUIT and setting immediate_quit (desirable anytime we
88 expect to block), call QUIT after setting immediate_quit. */
92 /* Nonzero means that encoded C++ names should be printed out in their
93 C++ form rather than raw. */
97 /* Nonzero means that encoded C++ names should be printed out in their
98 C++ form even in assembler language displays. If this is set, but
99 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
101 int asm_demangle = 0;
103 /* Nonzero means that strings with character values >0x7F should be printed
104 as octal escapes. Zero means just print the value (e.g. it's an
105 international character, and the terminal or window can cope.) */
107 int sevenbit_strings = 0;
109 /* String to be printed before error messages, if any. */
111 char *error_pre_print;
112 char *warning_pre_print = "\nwarning: ";
114 /* Add a new cleanup to the cleanup_chain,
115 and return the previous chain pointer
116 to be passed later to do_cleanups or discard_cleanups.
117 Args are FUNCTION to clean up with, and ARG to pass to it. */
120 make_cleanup (function, arg)
121 void (*function) PARAMS ((PTR));
124 register struct cleanup *new
125 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
126 register struct cleanup *old_chain = cleanup_chain;
128 new->next = cleanup_chain;
129 new->function = function;
136 /* Discard cleanups and do the actions they describe
137 until we get back to the point OLD_CHAIN in the cleanup_chain. */
140 do_cleanups (old_chain)
141 register struct cleanup *old_chain;
143 register struct cleanup *ptr;
144 while ((ptr = cleanup_chain) != old_chain)
146 cleanup_chain = ptr->next; /* Do this first incase recursion */
147 (*ptr->function) (ptr->arg);
152 /* Discard cleanups, not doing the actions they describe,
153 until we get back to the point OLD_CHAIN in the cleanup_chain. */
156 discard_cleanups (old_chain)
157 register struct cleanup *old_chain;
159 register struct cleanup *ptr;
160 while ((ptr = cleanup_chain) != old_chain)
162 cleanup_chain = ptr->next;
167 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
171 struct cleanup *old_chain = cleanup_chain;
177 /* Restore the cleanup chain from a previously saved chain. */
179 restore_cleanups (chain)
180 struct cleanup *chain;
182 cleanup_chain = chain;
185 /* This function is useful for cleanups.
189 old_chain = make_cleanup (free_current_contents, &foo);
191 to arrange to free the object thus allocated. */
194 free_current_contents (location)
200 /* Provide a known function that does nothing, to use as a base for
201 for a possibly long chain of cleanups. This is useful where we
202 use the cleanup chain for handling normal cleanups as well as dealing
203 with cleanups that need to be done as a result of a call to error().
204 In such cases, we may not be certain where the first cleanup is, unless
205 we have a do-nothing one to always use as the base. */
215 /* Provide a hook for modules wishing to print their own warning messages
216 to set up the terminal state in a compatible way, without them having
217 to import all the target_<...> macros. */
222 target_terminal_ours ();
223 wrap_here(""); /* Force out any buffered output */
224 gdb_flush (gdb_stdout);
227 /* Print a warning message.
228 The first argument STRING is the warning message, used as a fprintf string,
229 and the remaining args are passed as arguments to it.
230 The primary difference between warnings and errors is that a warning
231 does not force the return to command level. */
242 target_terminal_ours ();
243 wrap_here(""); /* Force out any buffered output */
244 gdb_flush (gdb_stdout);
245 if (warning_pre_print)
246 fprintf_unfiltered (gdb_stderr, warning_pre_print);
247 string = va_arg (args, char *);
248 vfprintf_unfiltered (gdb_stderr, string, args);
249 fprintf_unfiltered (gdb_stderr, "\n");
253 /* Print an error message and return to command level.
254 The first argument STRING is the error message, used as a fprintf string,
255 and the remaining args are passed as arguments to it. */
266 target_terminal_ours ();
267 wrap_here(""); /* Force out any buffered output */
268 gdb_flush (gdb_stdout);
270 fprintf_filtered (gdb_stderr, error_pre_print);
271 string = va_arg (args, char *);
272 vfprintf_filtered (gdb_stderr, string, args);
273 fprintf_filtered (gdb_stderr, "\n");
275 return_to_top_level (RETURN_ERROR);
278 /* Print an error message and exit reporting failure.
279 This is for a error that we cannot continue from.
280 The arguments are printed a la printf.
282 This function cannot be declared volatile (NORETURN) in an
283 ANSI environment because exit() is not declared volatile. */
294 string = va_arg (args, char *);
295 fprintf_unfiltered (gdb_stderr, "\ngdb: ");
296 vfprintf_unfiltered (gdb_stderr, string, args);
297 fprintf_unfiltered (gdb_stderr, "\n");
302 /* Print an error message and exit, dumping core.
303 The arguments are printed a la printf (). */
307 fatal_dump_core (va_alist)
314 string = va_arg (args, char *);
315 /* "internal error" is always correct, since GDB should never dump
316 core, no matter what the input. */
317 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: ");
318 vfprintf_unfiltered (gdb_stderr, string, args);
319 fprintf_unfiltered (gdb_stderr, "\n");
322 signal (SIGQUIT, SIG_DFL);
323 kill (getpid (), SIGQUIT);
324 /* We should never get here, but just in case... */
328 /* The strerror() function can return NULL for errno values that are
329 out of range. Provide a "safe" version that always returns a
333 safe_strerror (errnum)
339 if ((msg = strerror (errnum)) == NULL)
341 sprintf (buf, "(undocumented errno %d)", errnum);
347 /* The strsignal() function can return NULL for signal values that are
348 out of range. Provide a "safe" version that always returns a
352 safe_strsignal (signo)
358 if ((msg = strsignal (signo)) == NULL)
360 sprintf (buf, "(undocumented signal %d)", signo);
367 /* Print the system error message for errno, and also mention STRING
368 as the file name for which the error was encountered.
369 Then return to command level. */
372 perror_with_name (string)
378 err = safe_strerror (errno);
379 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
380 strcpy (combined, string);
381 strcat (combined, ": ");
382 strcat (combined, err);
384 /* I understand setting these is a matter of taste. Still, some people
385 may clear errno but not know about bfd_error. Doing this here is not
387 bfd_set_error (bfd_error_no_error);
390 error ("%s.", combined);
393 /* Print the system error message for ERRCODE, and also mention STRING
394 as the file name for which the error was encountered. */
397 print_sys_errmsg (string, errcode)
404 err = safe_strerror (errcode);
405 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
406 strcpy (combined, string);
407 strcat (combined, ": ");
408 strcat (combined, err);
410 /* We want anything which was printed on stdout to come out first, before
412 gdb_flush (gdb_stdout);
413 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
416 /* Control C eventually causes this to be called, at a convenient time. */
421 serial_t gdb_stdout_serial = serial_fdopen (1);
423 target_terminal_ours ();
425 /* We want all output to appear now, before we print "Quit". We
426 have 3 levels of buffering we have to flush (it's possible that
427 some of these should be changed to flush the lower-level ones
430 /* 1. The _filtered buffer. */
431 wrap_here ((char *)0);
433 /* 2. The stdio buffer. */
434 gdb_flush (gdb_stdout);
435 gdb_flush (gdb_stderr);
437 /* 3. The system-level buffer. */
438 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial);
439 SERIAL_UN_FDOPEN (gdb_stdout_serial);
441 /* Don't use *_filtered; we don't want to prompt the user to continue. */
443 fprintf_unfiltered (gdb_stderr, error_pre_print);
446 /* If there is no terminal switching for this target, then we can't
447 possibly get screwed by the lack of job control. */
448 || current_target->to_terminal_ours == NULL)
449 fprintf_unfiltered (gdb_stderr, "Quit\n");
451 fprintf_unfiltered (gdb_stderr,
452 "Quit (expect signal SIGINT when the program is resumed)\n");
453 return_to_top_level (RETURN_QUIT);
459 /* In the absence of signals, poll keyboard for a quit.
460 Called from #define QUIT pollquit() in xm-go32.h. */
478 /* We just ignore it */
479 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
501 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
508 /* Done by signals */
511 /* Control C comes here */
519 /* Restore the signal handler. Harmless with BSD-style signals, needed
520 for System V-style signals. So just always do it, rather than worrying
521 about USG defines and stuff like that. */
522 signal (signo, request_quit);
529 /* Memory management stuff (malloc friends). */
531 #if defined (NO_MMALLOC)
538 return (malloc (size));
542 mrealloc (md, ptr, size)
547 if (ptr == 0) /* Guard against old realloc's */
548 return malloc (size);
550 return realloc (ptr, size);
561 #endif /* NO_MMALLOC */
563 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
571 #else /* have mmalloc and want corruption checking */
576 fatal_dump_core ("Memory corruption");
579 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
580 by MD, to detect memory corruption. Note that MD may be NULL to specify
581 the default heap that grows via sbrk.
583 Note that for freshly created regions, we must call mmcheck prior to any
584 mallocs in the region. Otherwise, any region which was allocated prior to
585 installing the checking hooks, which is later reallocated or freed, will
586 fail the checks! The mmcheck function only allows initial hooks to be
587 installed before the first mmalloc. However, anytime after we have called
588 mmcheck the first time to install the checking hooks, we can call it again
589 to update the function pointer to the memory corruption handler.
591 Returns zero on failure, non-zero on success. */
597 if (!mmcheck (md, malloc_botch))
599 warning ("internal error: failed to install memory consistency checks");
605 #endif /* Have mmalloc and want corruption checking */
607 /* Called when a memory allocation fails, with the number of bytes of
608 memory requested in SIZE. */
616 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
620 fatal ("virtual memory exhausted.");
624 /* Like mmalloc but get error if no storage available, and protect against
625 the caller wanting to allocate zero bytes. Whether to return NULL for
626 a zero byte request, or translate the request into a request for one
627 byte of zero'd storage, is a religious issue. */
640 else if ((val = mmalloc (md, size)) == NULL)
647 /* Like mrealloc but get error if no storage available. */
650 xmrealloc (md, ptr, size)
659 val = mrealloc (md, ptr, size);
663 val = mmalloc (md, size);
672 /* Like malloc but get error if no storage available, and protect against
673 the caller wanting to allocate zero bytes. */
679 return (xmmalloc ((PTR) NULL, size));
682 /* Like mrealloc but get error if no storage available. */
689 return (xmrealloc ((PTR) NULL, ptr, size));
693 /* My replacement for the read system call.
694 Used like `read' but keeps going if `read' returns too soon. */
697 myread (desc, addr, len)
707 val = read (desc, addr, len);
718 /* Make a copy of the string at PTR with SIZE characters
719 (and add a null character at the end in the copy).
720 Uses malloc to get the space. Returns the address of the copy. */
723 savestring (ptr, size)
727 register char *p = (char *) xmalloc (size + 1);
728 memcpy (p, ptr, size);
734 msavestring (md, ptr, size)
739 register char *p = (char *) xmmalloc (md, size + 1);
740 memcpy (p, ptr, size);
745 /* The "const" is so it compiles under DGUX (which prototypes strsave
746 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
747 Doesn't real strsave return NULL if out of memory? */
752 return savestring (ptr, strlen (ptr));
760 return (msavestring (md, ptr, strlen (ptr)));
764 print_spaces (n, file)
772 /* Print a host address. */
775 gdb_print_address (addr, stream)
780 /* We could use the %p conversion specifier to fprintf if we had any
781 way of knowing whether this host supports it. But the following
782 should work on the Alpha and on 32 bit machines. */
784 fprintf_filtered (stream, "0x%lx", (unsigned long)addr);
787 /* Ask user a y-or-n question and return 1 iff answer is yes.
788 Takes three args which are given to printf to print the question.
789 The first, a control string, should end in "? ".
790 It should not say how to answer, because we do that. */
803 /* Automatically answer "yes" if input is not from a terminal. */
804 if (!input_from_terminal_p ())
809 wrap_here (""); /* Flush any buffered output */
810 gdb_flush (gdb_stdout);
812 if (annotation_level > 1)
813 printf_filtered ("\n\032\032pre-query\n");
816 ctlstr = va_arg (args, char *);
817 vfprintf_filtered (gdb_stdout, ctlstr, args);
819 printf_filtered ("(y or n) ");
821 if (annotation_level > 1)
822 printf_filtered ("\n\032\032query\n");
824 gdb_flush (gdb_stdout);
825 answer = fgetc (stdin);
826 clearerr (stdin); /* in case of C-d */
827 if (answer == EOF) /* C-d */
832 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
835 ans2 = fgetc (stdin);
838 while (ans2 != EOF && ans2 != '\n');
851 printf_filtered ("Please answer y or n.\n");
854 if (annotation_level > 1)
855 printf_filtered ("\n\032\032post-query\n");
860 /* Parse a C escape sequence. STRING_PTR points to a variable
861 containing a pointer to the string to parse. That pointer
862 should point to the character after the \. That pointer
863 is updated past the characters we use. The value of the
864 escape sequence is returned.
866 A negative value means the sequence \ newline was seen,
867 which is supposed to be equivalent to nothing at all.
869 If \ is followed by a null character, we return a negative
870 value and leave the string pointer pointing at the null character.
872 If \ is followed by 000, we return 0 and leave the string pointer
873 after the zeros. A value of 0 does not mean end of string. */
876 parse_escape (string_ptr)
879 register int c = *(*string_ptr)++;
883 return 007; /* Bell (alert) char */
886 case 'e': /* Escape character */
904 c = *(*string_ptr)++;
906 c = parse_escape (string_ptr);
909 return (c & 0200) | (c & 037);
920 register int i = c - '0';
921 register int count = 0;
924 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
942 /* Print the character C on STREAM as part of the contents of a literal
943 string whose delimiter is QUOTER. Note that this routine should only
944 be call for printing things which are independent of the language
945 of the program being debugged. */
948 gdb_printchar (c, stream, quoter)
954 c &= 0xFF; /* Avoid sign bit follies */
956 if ( c < 0x20 || /* Low control chars */
957 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
958 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
962 fputs_filtered ("\\n", stream);
965 fputs_filtered ("\\b", stream);
968 fputs_filtered ("\\t", stream);
971 fputs_filtered ("\\f", stream);
974 fputs_filtered ("\\r", stream);
977 fputs_filtered ("\\e", stream);
980 fputs_filtered ("\\a", stream);
983 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
987 if (c == '\\' || c == quoter)
988 fputs_filtered ("\\", stream);
989 fprintf_filtered (stream, "%c", c);
993 /* Number of lines per page or UINT_MAX if paging is disabled. */
994 static unsigned int lines_per_page;
995 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
996 static unsigned int chars_per_line;
997 /* Current count of lines printed on this page, chars on this line. */
998 static unsigned int lines_printed, chars_printed;
1000 /* Buffer and start column of buffered text, for doing smarter word-
1001 wrapping. When someone calls wrap_here(), we start buffering output
1002 that comes through fputs_filtered(). If we see a newline, we just
1003 spit it out and forget about the wrap_here(). If we see another
1004 wrap_here(), we spit it out and remember the newer one. If we see
1005 the end of the line, we spit out a newline, the indent, and then
1006 the buffered output. */
1008 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1009 are waiting to be output (they have already been counted in chars_printed).
1010 When wrap_buffer[0] is null, the buffer is empty. */
1011 static char *wrap_buffer;
1013 /* Pointer in wrap_buffer to the next character to fill. */
1014 static char *wrap_pointer;
1016 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1018 static char *wrap_indent;
1020 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1021 is not in effect. */
1022 static int wrap_column;
1026 set_width_command (args, from_tty, c)
1029 struct cmd_list_element *c;
1033 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1034 wrap_buffer[0] = '\0';
1037 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1038 wrap_pointer = wrap_buffer; /* Start it at the beginning */
1041 /* Wait, so the user can read what's on the screen. Prompt the user
1042 to continue by pressing RETURN. */
1045 prompt_for_continue ()
1048 char cont_prompt[120];
1050 strcpy (cont_prompt,
1051 "---Type <return> to continue, or q <return> to quit---");
1052 if (annotation_level > 1)
1053 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1055 /* We must do this *before* we call gdb_readline, else it will eventually
1056 call us -- thinking that we're trying to print beyond the end of the
1058 reinitialize_more_filter ();
1061 /* On a real operating system, the user can quit with SIGINT.
1064 'q' is provided on all systems so users don't have to change habits
1065 from system to system, and because telling them what to do in
1066 the prompt is more user-friendly than expecting them to think of
1068 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1069 whereas control-C to gdb_readline will cause the user to get dumped
1071 ignore = readline (cont_prompt);
1075 while (*p == ' ' || *p == '\t')
1078 request_quit (SIGINT);
1083 /* Now we have to do this again, so that GDB will know that it doesn't
1084 need to save the ---Type <return>--- line at the top of the screen. */
1085 reinitialize_more_filter ();
1087 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1090 /* Reinitialize filter; ie. tell it to reset to original values. */
1093 reinitialize_more_filter ()
1099 /* Indicate that if the next sequence of characters overflows the line,
1100 a newline should be inserted here rather than when it hits the end.
1101 If INDENT is non-null, it is a string to be printed to indent the
1102 wrapped part on the next line. INDENT must remain accessible until
1103 the next call to wrap_here() or until a newline is printed through
1106 If the line is already overfull, we immediately print a newline and
1107 the indentation, and disable further wrapping.
1109 If we don't know the width of lines, but we know the page height,
1110 we must not wrap words, but should still keep track of newlines
1111 that were explicitly printed.
1113 INDENT should not contain tabs, as that will mess up the char count
1114 on the next line. FIXME.
1116 This routine is guaranteed to force out any output which has been
1117 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1118 used to force out output from the wrap_buffer. */
1126 *wrap_pointer = '\0';
1127 fputs (wrap_buffer, gdb_stdout);
1129 wrap_pointer = wrap_buffer;
1130 wrap_buffer[0] = '\0';
1131 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1135 else if (chars_printed >= chars_per_line)
1137 puts_filtered ("\n");
1139 puts_filtered (indent);
1144 wrap_column = chars_printed;
1148 wrap_indent = indent;
1152 /* Ensure that whatever gets printed next, using the filtered output
1153 commands, starts at the beginning of the line. I.E. if there is
1154 any pending output for the current line, flush it and start a new
1155 line. Otherwise do nothing. */
1160 if (chars_printed > 0)
1162 puts_filtered ("\n");
1168 gdb_fopen (name, mode)
1172 return fopen (name, mode);
1182 /* Like fputs but if FILTER is true, pause after every screenful.
1184 Regardless of FILTER can wrap at points other than the final
1185 character of a line.
1187 Unlike fputs, fputs_maybe_filtered does not return a value.
1188 It is OK for LINEBUFFER to be NULL, in which case just don't print
1191 Note that a longjmp to top level may occur in this routine (only if
1192 FILTER is true) (since prompt_for_continue may do so) so this
1193 routine should not be called when cleanups are not in place. */
1196 fputs_maybe_filtered (linebuffer, stream, filter)
1197 const char *linebuffer;
1201 const char *lineptr;
1203 if (linebuffer == 0)
1206 /* Don't do any filtering if it is disabled. */
1207 if (stream != gdb_stdout
1208 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1210 fputs (linebuffer, stream);
1214 /* Go through and output each character. Show line extension
1215 when this is necessary; prompt user for new page when this is
1218 lineptr = linebuffer;
1221 /* Possible new page. */
1223 (lines_printed >= lines_per_page - 1))
1224 prompt_for_continue ();
1226 while (*lineptr && *lineptr != '\n')
1228 /* Print a single line. */
1229 if (*lineptr == '\t')
1232 *wrap_pointer++ = '\t';
1234 putc ('\t', stream);
1235 /* Shifting right by 3 produces the number of tab stops
1236 we have already passed, and then adding one and
1237 shifting left 3 advances to the next tab stop. */
1238 chars_printed = ((chars_printed >> 3) + 1) << 3;
1244 *wrap_pointer++ = *lineptr;
1246 putc (*lineptr, stream);
1251 if (chars_printed >= chars_per_line)
1253 unsigned int save_chars = chars_printed;
1257 /* If we aren't actually wrapping, don't output newline --
1258 if chars_per_line is right, we probably just overflowed
1259 anyway; if it's wrong, let us keep going. */
1261 putc ('\n', stream);
1263 /* Possible new page. */
1264 if (lines_printed >= lines_per_page - 1)
1265 prompt_for_continue ();
1267 /* Now output indentation and wrapped string */
1270 fputs (wrap_indent, stream);
1271 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1272 fputs (wrap_buffer, stream); /* and eject it */
1273 /* FIXME, this strlen is what prevents wrap_indent from
1274 containing tabs. However, if we recurse to print it
1275 and count its chars, we risk trouble if wrap_indent is
1276 longer than (the user settable) chars_per_line.
1277 Note also that this can set chars_printed > chars_per_line
1278 if we are printing a long string. */
1279 chars_printed = strlen (wrap_indent)
1280 + (save_chars - wrap_column);
1281 wrap_pointer = wrap_buffer; /* Reset buffer */
1282 wrap_buffer[0] = '\0';
1283 wrap_column = 0; /* And disable fancy wrap */
1288 if (*lineptr == '\n')
1291 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1293 putc ('\n', stream);
1300 fputs_filtered (linebuffer, stream)
1301 const char *linebuffer;
1304 fputs_maybe_filtered (linebuffer, stream, 1);
1308 fputs_unfiltered (linebuffer, stream)
1309 const char *linebuffer;
1314 /* This gets the wrap_buffer buffering wrong when called from
1315 gdb_readline (GDB was sometimes failing to print the prompt
1316 before reading input). Even at other times, it seems kind of
1317 misguided, especially now that printf_unfiltered doesn't use
1318 printf_maybe_filtered. */
1320 fputs_maybe_filtered (linebuffer, stream, 0);
1322 fputs (linebuffer, stream);
1333 fputs_unfiltered (buf, gdb_stdout);
1337 fputc_unfiltered (c, stream)
1344 fputs_unfiltered (buf, stream);
1348 /* Print a variable number of ARGS using format FORMAT. If this
1349 information is going to put the amount written (since the last call
1350 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1351 print out a pause message and do a gdb_readline to get the users
1352 permision to continue.
1354 Unlike fprintf, this function does not return a value.
1356 We implement three variants, vfprintf (takes a vararg list and stream),
1357 fprintf (takes a stream to write on), and printf (the usual).
1359 Note that this routine has a restriction that the length of the
1360 final output line must be less than 255 characters *or* it must be
1361 less than twice the size of the format string. This is a very
1362 arbitrary restriction, but it is an internal restriction, so I'll
1363 put it in. This means that the %s format specifier is almost
1364 useless; unless the caller can GUARANTEE that the string is short
1365 enough, fputs_filtered should be used instead.
1367 Note also that a longjmp to top level may occur in this routine
1368 (since prompt_for_continue may do so) so this routine should not be
1369 called when cleanups are not in place. */
1371 #define MIN_LINEBUF 255
1374 vfprintf_maybe_filtered (stream, format, args, filter)
1380 char line_buf[MIN_LINEBUF+10];
1381 char *linebuffer = line_buf;
1384 format_length = strlen (format);
1386 /* Reallocate buffer to a larger size if this is necessary. */
1387 if (format_length * 2 > MIN_LINEBUF)
1389 linebuffer = alloca (10 + format_length * 2);
1392 /* This won't blow up if the restrictions described above are
1394 vsprintf (linebuffer, format, args);
1396 fputs_maybe_filtered (linebuffer, stream, filter);
1401 vfprintf_filtered (stream, format, args)
1406 vfprintf_maybe_filtered (stream, format, args, 1);
1410 vfprintf_unfiltered (stream, format, args)
1415 vfprintf (stream, format, args);
1419 vprintf_filtered (format, args)
1423 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
1427 vprintf_unfiltered (format, args)
1431 vfprintf (gdb_stdout, format, args);
1436 fprintf_filtered (va_alist)
1444 stream = va_arg (args, FILE *);
1445 format = va_arg (args, char *);
1447 /* This won't blow up if the restrictions described above are
1449 vfprintf_filtered (stream, format, args);
1455 fprintf_unfiltered (va_alist)
1463 stream = va_arg (args, FILE *);
1464 format = va_arg (args, char *);
1466 /* This won't blow up if the restrictions described above are
1468 vfprintf_unfiltered (stream, format, args);
1472 /* Like fprintf_filtered, but prints it's result indent.
1473 Called as fprintfi_filtered (spaces, stream, format, ...); */
1477 fprintfi_filtered (va_alist)
1486 spaces = va_arg (args, int);
1487 stream = va_arg (args, FILE *);
1488 format = va_arg (args, char *);
1489 print_spaces_filtered (spaces, stream);
1491 /* This won't blow up if the restrictions described above are
1493 vfprintf_filtered (stream, format, args);
1500 printf_filtered (va_alist)
1507 format = va_arg (args, char *);
1509 vfprintf_filtered (gdb_stdout, format, args);
1516 printf_unfiltered (va_alist)
1523 format = va_arg (args, char *);
1525 vfprintf_unfiltered (gdb_stdout, format, args);
1529 /* Like printf_filtered, but prints it's result indented.
1530 Called as printfi_filtered (spaces, format, ...); */
1534 printfi_filtered (va_alist)
1542 spaces = va_arg (args, int);
1543 format = va_arg (args, char *);
1544 print_spaces_filtered (spaces, gdb_stdout);
1545 vfprintf_filtered (gdb_stdout, format, args);
1549 /* Easy -- but watch out!
1551 This routine is *not* a replacement for puts()! puts() appends a newline.
1552 This one doesn't, and had better not! */
1555 puts_filtered (string)
1558 fputs_filtered (string, gdb_stdout);
1562 puts_unfiltered (string)
1565 fputs_unfiltered (string, gdb_stdout);
1568 /* Return a pointer to N spaces and a null. The pointer is good
1569 until the next call to here. */
1575 static char *spaces;
1576 static int max_spaces;
1582 spaces = (char *) xmalloc (n+1);
1583 for (t = spaces+n; t != spaces;)
1589 return spaces + max_spaces - n;
1592 /* Print N spaces. */
1594 print_spaces_filtered (n, stream)
1598 fputs_filtered (n_spaces (n), stream);
1601 /* C++ demangler stuff. */
1603 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1604 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1605 If the name is not mangled, or the language for the name is unknown, or
1606 demangling is off, the name is printed in its "raw" form. */
1609 fprintf_symbol_filtered (stream, name, lang, arg_mode)
1619 /* If user wants to see raw output, no problem. */
1622 fputs_filtered (name, stream);
1628 case language_cplus:
1629 demangled = cplus_demangle (name, arg_mode);
1631 case language_chill:
1632 demangled = chill_demangle (name);
1638 fputs_filtered (demangled ? demangled : name, stream);
1639 if (demangled != NULL)
1647 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1648 differences in whitespace. Returns 0 if they match, non-zero if they
1649 don't (slightly different than strcmp()'s range of return values).
1651 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1652 This "feature" is useful when searching for matching C++ function names
1653 (such as if the user types 'break FOO', where FOO is a mangled C++
1657 strcmp_iw (string1, string2)
1658 const char *string1;
1659 const char *string2;
1661 while ((*string1 != '\0') && (*string2 != '\0'))
1663 while (isspace (*string1))
1667 while (isspace (*string2))
1671 if (*string1 != *string2)
1675 if (*string1 != '\0')
1681 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1686 _initialize_utils ()
1688 struct cmd_list_element *c;
1690 c = add_set_cmd ("width", class_support, var_uinteger,
1691 (char *)&chars_per_line,
1692 "Set number of characters gdb thinks are in a line.",
1694 add_show_from_set (c, &showlist);
1695 c->function.sfunc = set_width_command;
1698 (add_set_cmd ("height", class_support,
1699 var_uinteger, (char *)&lines_per_page,
1700 "Set number of lines gdb thinks are in a page.", &setlist),
1703 /* These defaults will be used if we are unable to get the correct
1704 values from termcap. */
1705 #if defined(__GO32__)
1706 lines_per_page = ScreenRows();
1707 chars_per_line = ScreenCols();
1709 lines_per_page = 24;
1710 chars_per_line = 80;
1711 /* Initialize the screen height and width from termcap. */
1713 char *termtype = getenv ("TERM");
1715 /* Positive means success, nonpositive means failure. */
1718 /* 2048 is large enough for all known terminals, according to the
1719 GNU termcap manual. */
1720 char term_buffer[2048];
1724 status = tgetent (term_buffer, termtype);
1729 val = tgetnum ("li");
1731 lines_per_page = val;
1733 /* The number of lines per page is not mentioned
1734 in the terminal description. This probably means
1735 that paging is not useful (e.g. emacs shell window),
1736 so disable paging. */
1737 lines_per_page = UINT_MAX;
1739 val = tgetnum ("co");
1741 chars_per_line = val;
1746 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1748 /* If there is a better way to determine the window size, use it. */
1749 SIGWINCH_HANDLER ();
1752 /* If the output is not a terminal, don't paginate it. */
1753 if (!ISATTY (gdb_stdout))
1754 lines_per_page = UINT_MAX;
1756 set_width_command ((char *)NULL, 0, c);
1759 (add_set_cmd ("demangle", class_support, var_boolean,
1761 "Set demangling of encoded C++ names when displaying symbols.",
1766 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1767 (char *)&sevenbit_strings,
1768 "Set printing of 8-bit characters in strings as \\nnn.",
1773 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1774 (char *)&asm_demangle,
1775 "Set demangling of C++ names in disassembly listings.",
1780 /* Machine specific function to handle SIGWINCH signal. */
1782 #ifdef SIGWINCH_HANDLER_BODY
1783 SIGWINCH_HANDLER_BODY