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 /* We want anything which was printed on stdout to come out first, before
407 gdb_flush (gdb_stdout
);
408 fprintf_unfiltered (gdb_stderr
, "%s.\n", combined
);
411 /* Control C eventually causes this to be called, at a convenient time. */
416 serial_t gdb_stdout_serial
= serial_fdopen (1);
418 target_terminal_ours ();
420 /* We want all output to appear now, before we print "Quit". We
421 have 3 levels of buffering we have to flush (it's possible that
422 some of these should be changed to flush the lower-level ones
425 /* 1. The _filtered buffer. */
426 wrap_here ((char *)0);
428 /* 2. The stdio buffer. */
429 gdb_flush (gdb_stdout
);
430 gdb_flush (gdb_stderr
);
432 /* 3. The system-level buffer. */
433 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial
);
434 SERIAL_UN_FDOPEN (gdb_stdout_serial
);
436 /* Don't use *_filtered; we don't want to prompt the user to continue. */
438 fprintf_unfiltered (gdb_stderr
, error_pre_print
);
441 /* If there is no terminal switching for this target, then we can't
442 possibly get screwed by the lack of job control. */
443 || current_target
->to_terminal_ours
== NULL
)
444 fprintf_unfiltered (gdb_stderr
, "Quit\n");
446 fprintf_unfiltered (gdb_stderr
,
447 "Quit (expect signal SIGINT when the program is resumed)\n");
448 return_to_top_level (RETURN_QUIT
);
454 /* In the absence of signals, poll keyboard for a quit.
455 Called from #define QUIT pollquit() in xm-go32.h. */
473 /* We just ignore it */
474 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
496 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
503 /* Done by signals */
506 /* Control C comes here */
514 /* Restore the signal handler. Harmless with BSD-style signals, needed
515 for System V-style signals. So just always do it, rather than worrying
516 about USG defines and stuff like that. */
517 signal (signo
, request_quit
);
524 /* Memory management stuff (malloc friends). */
526 #if defined (NO_MMALLOC)
533 return (malloc (size
));
537 mrealloc (md
, ptr
, size
)
542 if (ptr
== 0) /* Guard against old realloc's */
543 return malloc (size
);
545 return realloc (ptr
, size
);
556 #endif /* NO_MMALLOC */
558 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
566 #else /* have mmalloc and want corruption checking */
571 fatal_dump_core ("Memory corruption");
574 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
575 by MD, to detect memory corruption. Note that MD may be NULL to specify
576 the default heap that grows via sbrk.
578 Note that for freshly created regions, we must call mmcheck prior to any
579 mallocs in the region. Otherwise, any region which was allocated prior to
580 installing the checking hooks, which is later reallocated or freed, will
581 fail the checks! The mmcheck function only allows initial hooks to be
582 installed before the first mmalloc. However, anytime after we have called
583 mmcheck the first time to install the checking hooks, we can call it again
584 to update the function pointer to the memory corruption handler.
586 Returns zero on failure, non-zero on success. */
592 if (!mmcheck (md
, malloc_botch
))
594 warning ("internal error: failed to install memory consistency checks");
600 #endif /* Have mmalloc and want corruption checking */
602 /* Called when a memory allocation fails, with the number of bytes of
603 memory requested in SIZE. */
611 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size
);
615 fatal ("virtual memory exhausted.");
619 /* Like mmalloc but get error if no storage available, and protect against
620 the caller wanting to allocate zero bytes. Whether to return NULL for
621 a zero byte request, or translate the request into a request for one
622 byte of zero'd storage, is a religious issue. */
635 else if ((val
= mmalloc (md
, size
)) == NULL
)
642 /* Like mrealloc but get error if no storage available. */
645 xmrealloc (md
, ptr
, size
)
654 val
= mrealloc (md
, ptr
, size
);
658 val
= mmalloc (md
, size
);
667 /* Like malloc but get error if no storage available, and protect against
668 the caller wanting to allocate zero bytes. */
674 return (xmmalloc ((PTR
) NULL
, size
));
677 /* Like mrealloc but get error if no storage available. */
684 return (xmrealloc ((PTR
) NULL
, ptr
, size
));
688 /* My replacement for the read system call.
689 Used like `read' but keeps going if `read' returns too soon. */
692 myread (desc
, addr
, len
)
702 val
= read (desc
, addr
, len
);
713 /* Make a copy of the string at PTR with SIZE characters
714 (and add a null character at the end in the copy).
715 Uses malloc to get the space. Returns the address of the copy. */
718 savestring (ptr
, size
)
722 register char *p
= (char *) xmalloc (size
+ 1);
723 memcpy (p
, ptr
, size
);
729 msavestring (md
, ptr
, size
)
734 register char *p
= (char *) xmmalloc (md
, size
+ 1);
735 memcpy (p
, ptr
, size
);
740 /* The "const" is so it compiles under DGUX (which prototypes strsave
741 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
742 Doesn't real strsave return NULL if out of memory? */
747 return savestring (ptr
, strlen (ptr
));
755 return (msavestring (md
, ptr
, strlen (ptr
)));
759 print_spaces (n
, file
)
767 /* Ask user a y-or-n question and return 1 iff answer is yes.
768 Takes three args which are given to printf to print the question.
769 The first, a control string, should end in "? ".
770 It should not say how to answer, because we do that. */
782 /* Automatically answer "yes" if input is not from a terminal. */
783 if (!input_from_terminal_p ())
788 wrap_here (""); /* Flush any buffered output */
789 gdb_flush (gdb_stdout
);
791 ctlstr
= va_arg (args
, char *);
792 vfprintf_filtered (gdb_stdout
, ctlstr
, args
);
794 printf_filtered ("(y or n) ");
795 gdb_flush (gdb_stdout
);
796 answer
= fgetc (stdin
);
797 clearerr (stdin
); /* in case of C-d */
798 if (answer
== EOF
) /* C-d */
800 if (answer
!= '\n') /* Eat rest of input line, to EOF or newline */
803 ans2
= fgetc (stdin
);
806 while (ans2
!= EOF
&& ans2
!= '\n');
813 printf_filtered ("Please answer y or n.\n");
818 /* Parse a C escape sequence. STRING_PTR points to a variable
819 containing a pointer to the string to parse. That pointer
820 should point to the character after the \. That pointer
821 is updated past the characters we use. The value of the
822 escape sequence is returned.
824 A negative value means the sequence \ newline was seen,
825 which is supposed to be equivalent to nothing at all.
827 If \ is followed by a null character, we return a negative
828 value and leave the string pointer pointing at the null character.
830 If \ is followed by 000, we return 0 and leave the string pointer
831 after the zeros. A value of 0 does not mean end of string. */
834 parse_escape (string_ptr
)
837 register int c
= *(*string_ptr
)++;
841 return 007; /* Bell (alert) char */
844 case 'e': /* Escape character */
862 c
= *(*string_ptr
)++;
864 c
= parse_escape (string_ptr
);
867 return (c
& 0200) | (c
& 037);
878 register int i
= c
- '0';
879 register int count
= 0;
882 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
900 /* Print the character C on STREAM as part of the contents of a literal
901 string whose delimiter is QUOTER. Note that this routine should only
902 be call for printing things which are independent of the language
903 of the program being debugged. */
906 gdb_printchar (c
, stream
, quoter
)
912 c
&= 0xFF; /* Avoid sign bit follies */
914 if ( c
< 0x20 || /* Low control chars */
915 (c
>= 0x7F && c
< 0xA0) || /* DEL, High controls */
916 (sevenbit_strings
&& c
>= 0x80)) { /* high order bit set */
920 fputs_filtered ("\\n", stream
);
923 fputs_filtered ("\\b", stream
);
926 fputs_filtered ("\\t", stream
);
929 fputs_filtered ("\\f", stream
);
932 fputs_filtered ("\\r", stream
);
935 fputs_filtered ("\\e", stream
);
938 fputs_filtered ("\\a", stream
);
941 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
945 if (c
== '\\' || c
== quoter
)
946 fputs_filtered ("\\", stream
);
947 fprintf_filtered (stream
, "%c", c
);
951 /* Number of lines per page or UINT_MAX if paging is disabled. */
952 static unsigned int lines_per_page
;
953 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
954 static unsigned int chars_per_line
;
955 /* Current count of lines printed on this page, chars on this line. */
956 static unsigned int lines_printed
, chars_printed
;
958 /* Buffer and start column of buffered text, for doing smarter word-
959 wrapping. When someone calls wrap_here(), we start buffering output
960 that comes through fputs_filtered(). If we see a newline, we just
961 spit it out and forget about the wrap_here(). If we see another
962 wrap_here(), we spit it out and remember the newer one. If we see
963 the end of the line, we spit out a newline, the indent, and then
964 the buffered output. */
966 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
967 are waiting to be output (they have already been counted in chars_printed).
968 When wrap_buffer[0] is null, the buffer is empty. */
969 static char *wrap_buffer
;
971 /* Pointer in wrap_buffer to the next character to fill. */
972 static char *wrap_pointer
;
974 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
976 static char *wrap_indent
;
978 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
980 static int wrap_column
;
984 set_width_command (args
, from_tty
, c
)
987 struct cmd_list_element
*c
;
991 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
992 wrap_buffer
[0] = '\0';
995 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
996 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
999 /* Wait, so the user can read what's on the screen. Prompt the user
1000 to continue by pressing RETURN. */
1003 prompt_for_continue ()
1007 /* We must do this *before* we call gdb_readline, else it will eventually
1008 call us -- thinking that we're trying to print beyond the end of the
1010 reinitialize_more_filter ();
1013 /* On a real operating system, the user can quit with SIGINT.
1016 'q' is provided on all systems so users don't have to change habits
1017 from system to system, and because telling them what to do in
1018 the prompt is more user-friendly than expecting them to think of
1021 gdb_readline ("---Type <return> to continue, or q <return> to quit---");
1025 while (*p
== ' ' || *p
== '\t')
1028 request_quit (SIGINT
);
1033 /* Now we have to do this again, so that GDB will know that it doesn't
1034 need to save the ---Type <return>--- line at the top of the screen. */
1035 reinitialize_more_filter ();
1037 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1040 /* Reinitialize filter; ie. tell it to reset to original values. */
1043 reinitialize_more_filter ()
1049 /* Indicate that if the next sequence of characters overflows the line,
1050 a newline should be inserted here rather than when it hits the end.
1051 If INDENT is non-null, it is a string to be printed to indent the
1052 wrapped part on the next line. INDENT must remain accessible until
1053 the next call to wrap_here() or until a newline is printed through
1056 If the line is already overfull, we immediately print a newline and
1057 the indentation, and disable further wrapping.
1059 If we don't know the width of lines, but we know the page height,
1060 we must not wrap words, but should still keep track of newlines
1061 that were explicitly printed.
1063 INDENT should not contain tabs, as that will mess up the char count
1064 on the next line. FIXME.
1066 This routine is guaranteed to force out any output which has been
1067 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1068 used to force out output from the wrap_buffer. */
1076 *wrap_pointer
= '\0';
1077 fputs (wrap_buffer
, gdb_stdout
);
1079 wrap_pointer
= wrap_buffer
;
1080 wrap_buffer
[0] = '\0';
1081 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
1085 else if (chars_printed
>= chars_per_line
)
1087 puts_filtered ("\n");
1089 puts_filtered (indent
);
1094 wrap_column
= chars_printed
;
1098 wrap_indent
= indent
;
1102 /* Ensure that whatever gets printed next, using the filtered output
1103 commands, starts at the beginning of the line. I.E. if there is
1104 any pending output for the current line, flush it and start a new
1105 line. Otherwise do nothing. */
1110 if (chars_printed
> 0)
1112 puts_filtered ("\n");
1118 gdb_fopen (name
, mode
)
1122 return fopen (name
, mode
);
1132 /* Like fputs but if FILTER is true, pause after every screenful.
1134 Regardless of FILTER can wrap at points other than the final
1135 character of a line.
1137 Unlike fputs, fputs_maybe_filtered does not return a value.
1138 It is OK for LINEBUFFER to be NULL, in which case just don't print
1141 Note that a longjmp to top level may occur in this routine (only if
1142 FILTER is true) (since prompt_for_continue may do so) so this
1143 routine should not be called when cleanups are not in place. */
1146 fputs_maybe_filtered (linebuffer
, stream
, filter
)
1147 const char *linebuffer
;
1151 const char *lineptr
;
1153 if (linebuffer
== 0)
1156 /* Don't do any filtering if it is disabled. */
1157 if (stream
!= gdb_stdout
1158 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
1160 fputs (linebuffer
, stream
);
1164 /* Go through and output each character. Show line extension
1165 when this is necessary; prompt user for new page when this is
1168 lineptr
= linebuffer
;
1171 /* Possible new page. */
1173 (lines_printed
>= lines_per_page
- 1))
1174 prompt_for_continue ();
1176 while (*lineptr
&& *lineptr
!= '\n')
1178 /* Print a single line. */
1179 if (*lineptr
== '\t')
1182 *wrap_pointer
++ = '\t';
1184 putc ('\t', stream
);
1185 /* Shifting right by 3 produces the number of tab stops
1186 we have already passed, and then adding one and
1187 shifting left 3 advances to the next tab stop. */
1188 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
1194 *wrap_pointer
++ = *lineptr
;
1196 putc (*lineptr
, stream
);
1201 if (chars_printed
>= chars_per_line
)
1203 unsigned int save_chars
= chars_printed
;
1207 /* If we aren't actually wrapping, don't output newline --
1208 if chars_per_line is right, we probably just overflowed
1209 anyway; if it's wrong, let us keep going. */
1211 putc ('\n', stream
);
1213 /* Possible new page. */
1214 if (lines_printed
>= lines_per_page
- 1)
1215 prompt_for_continue ();
1217 /* Now output indentation and wrapped string */
1220 fputs (wrap_indent
, stream
);
1221 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
1222 fputs (wrap_buffer
, stream
); /* and eject it */
1223 /* FIXME, this strlen is what prevents wrap_indent from
1224 containing tabs. However, if we recurse to print it
1225 and count its chars, we risk trouble if wrap_indent is
1226 longer than (the user settable) chars_per_line.
1227 Note also that this can set chars_printed > chars_per_line
1228 if we are printing a long string. */
1229 chars_printed
= strlen (wrap_indent
)
1230 + (save_chars
- wrap_column
);
1231 wrap_pointer
= wrap_buffer
; /* Reset buffer */
1232 wrap_buffer
[0] = '\0';
1233 wrap_column
= 0; /* And disable fancy wrap */
1238 if (*lineptr
== '\n')
1241 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1243 putc ('\n', stream
);
1250 fputs_filtered (linebuffer
, stream
)
1251 const char *linebuffer
;
1254 fputs_maybe_filtered (linebuffer
, stream
, 1);
1258 fputs_unfiltered (linebuffer
, stream
)
1259 const char *linebuffer
;
1264 /* This gets the wrap_buffer buffering wrong when called from
1265 gdb_readline (GDB was sometimes failing to print the prompt
1266 before reading input). Even at other times, it seems kind of
1267 misguided, especially now that printf_unfiltered doesn't use
1268 printf_maybe_filtered. */
1270 fputs_maybe_filtered (linebuffer
, stream
, 0);
1272 fputs (linebuffer
, stream
);
1283 fputs_unfiltered (buf
, gdb_stdout
);
1287 fputc_unfiltered (c
, stream
)
1294 fputs_unfiltered (buf
, stream
);
1298 /* Print a variable number of ARGS using format FORMAT. If this
1299 information is going to put the amount written (since the last call
1300 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1301 print out a pause message and do a gdb_readline to get the users
1302 permision to continue.
1304 Unlike fprintf, this function does not return a value.
1306 We implement three variants, vfprintf (takes a vararg list and stream),
1307 fprintf (takes a stream to write on), and printf (the usual).
1309 Note that this routine has a restriction that the length of the
1310 final output line must be less than 255 characters *or* it must be
1311 less than twice the size of the format string. This is a very
1312 arbitrary restriction, but it is an internal restriction, so I'll
1313 put it in. This means that the %s format specifier is almost
1314 useless; unless the caller can GUARANTEE that the string is short
1315 enough, fputs_filtered should be used instead.
1317 Note also that a longjmp to top level may occur in this routine
1318 (since prompt_for_continue may do so) so this routine should not be
1319 called when cleanups are not in place. */
1321 #define MIN_LINEBUF 255
1324 vfprintf_maybe_filtered (stream
, format
, args
, filter
)
1330 char line_buf
[MIN_LINEBUF
+10];
1331 char *linebuffer
= line_buf
;
1334 format_length
= strlen (format
);
1336 /* Reallocate buffer to a larger size if this is necessary. */
1337 if (format_length
* 2 > MIN_LINEBUF
)
1339 linebuffer
= alloca (10 + format_length
* 2);
1342 /* This won't blow up if the restrictions described above are
1344 vsprintf (linebuffer
, format
, args
);
1346 fputs_maybe_filtered (linebuffer
, stream
, filter
);
1351 vfprintf_filtered (stream
, format
, args
)
1356 vfprintf_maybe_filtered (stream
, format
, args
, 1);
1360 vfprintf_unfiltered (stream
, format
, args
)
1365 vfprintf (stream
, format
, args
);
1369 vprintf_filtered (format
, args
)
1373 vfprintf_maybe_filtered (gdb_stdout
, format
, args
, 1);
1377 vprintf_unfiltered (format
, args
)
1381 vfprintf (gdb_stdout
, format
, args
);
1386 fprintf_filtered (va_alist
)
1394 stream
= va_arg (args
, FILE *);
1395 format
= va_arg (args
, char *);
1397 /* This won't blow up if the restrictions described above are
1399 vfprintf_filtered (stream
, format
, args
);
1405 fprintf_unfiltered (va_alist
)
1413 stream
= va_arg (args
, FILE *);
1414 format
= va_arg (args
, char *);
1416 /* This won't blow up if the restrictions described above are
1418 vfprintf_unfiltered (stream
, format
, args
);
1422 /* Like fprintf_filtered, but prints it's result indent.
1423 Called as fprintfi_filtered (spaces, stream, format, ...); */
1427 fprintfi_filtered (va_alist
)
1436 spaces
= va_arg (args
, int);
1437 stream
= va_arg (args
, FILE *);
1438 format
= va_arg (args
, char *);
1439 print_spaces_filtered (spaces
, stream
);
1441 /* This won't blow up if the restrictions described above are
1443 vfprintf_filtered (stream
, format
, args
);
1450 printf_filtered (va_alist
)
1457 format
= va_arg (args
, char *);
1459 vfprintf_filtered (gdb_stdout
, format
, args
);
1466 printf_unfiltered (va_alist
)
1473 format
= va_arg (args
, char *);
1475 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1479 /* Like printf_filtered, but prints it's result indented.
1480 Called as printfi_filtered (spaces, format, ...); */
1484 printfi_filtered (va_alist
)
1492 spaces
= va_arg (args
, int);
1493 format
= va_arg (args
, char *);
1494 print_spaces_filtered (spaces
, gdb_stdout
);
1495 vfprintf_filtered (gdb_stdout
, format
, args
);
1499 /* Easy -- but watch out!
1501 This routine is *not* a replacement for puts()! puts() appends a newline.
1502 This one doesn't, and had better not! */
1505 puts_filtered (string
)
1508 fputs_filtered (string
, gdb_stdout
);
1512 puts_unfiltered (string
)
1515 fputs_unfiltered (string
, gdb_stdout
);
1518 /* Return a pointer to N spaces and a null. The pointer is good
1519 until the next call to here. */
1525 static char *spaces
;
1526 static int max_spaces
;
1532 spaces
= (char *) xmalloc (n
+1);
1533 for (t
= spaces
+n
; t
!= spaces
;)
1539 return spaces
+ max_spaces
- n
;
1542 /* Print N spaces. */
1544 print_spaces_filtered (n
, stream
)
1548 fputs_filtered (n_spaces (n
), stream
);
1551 /* C++ demangler stuff. */
1553 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1554 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1555 If the name is not mangled, or the language for the name is unknown, or
1556 demangling is off, the name is printed in its "raw" form. */
1559 fprintf_symbol_filtered (stream
, name
, lang
, arg_mode
)
1569 /* If user wants to see raw output, no problem. */
1572 fputs_filtered (name
, stream
);
1578 case language_cplus
:
1579 demangled
= cplus_demangle (name
, arg_mode
);
1581 case language_chill
:
1582 demangled
= chill_demangle (name
);
1588 fputs_filtered (demangled
? demangled
: name
, stream
);
1589 if (demangled
!= NULL
)
1597 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1598 differences in whitespace. Returns 0 if they match, non-zero if they
1599 don't (slightly different than strcmp()'s range of return values).
1601 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1602 This "feature" is useful when searching for matching C++ function names
1603 (such as if the user types 'break FOO', where FOO is a mangled C++
1607 strcmp_iw (string1
, string2
)
1608 const char *string1
;
1609 const char *string2
;
1611 while ((*string1
!= '\0') && (*string2
!= '\0'))
1613 while (isspace (*string1
))
1617 while (isspace (*string2
))
1621 if (*string1
!= *string2
)
1625 if (*string1
!= '\0')
1631 return (*string1
!= '\0' && *string1
!= '(') || (*string2
!= '\0');
1636 _initialize_utils ()
1638 struct cmd_list_element
*c
;
1640 c
= add_set_cmd ("width", class_support
, var_uinteger
,
1641 (char *)&chars_per_line
,
1642 "Set number of characters gdb thinks are in a line.",
1644 add_show_from_set (c
, &showlist
);
1645 c
->function
.sfunc
= set_width_command
;
1648 (add_set_cmd ("height", class_support
,
1649 var_uinteger
, (char *)&lines_per_page
,
1650 "Set number of lines gdb thinks are in a page.", &setlist
),
1653 /* These defaults will be used if we are unable to get the correct
1654 values from termcap. */
1655 #if defined(__GO32__)
1656 lines_per_page
= ScreenRows();
1657 chars_per_line
= ScreenCols();
1659 lines_per_page
= 24;
1660 chars_per_line
= 80;
1661 /* Initialize the screen height and width from termcap. */
1663 char *termtype
= getenv ("TERM");
1665 /* Positive means success, nonpositive means failure. */
1668 /* 2048 is large enough for all known terminals, according to the
1669 GNU termcap manual. */
1670 char term_buffer
[2048];
1674 status
= tgetent (term_buffer
, termtype
);
1679 val
= tgetnum ("li");
1681 lines_per_page
= val
;
1683 /* The number of lines per page is not mentioned
1684 in the terminal description. This probably means
1685 that paging is not useful (e.g. emacs shell window),
1686 so disable paging. */
1687 lines_per_page
= UINT_MAX
;
1689 val
= tgetnum ("co");
1691 chars_per_line
= val
;
1696 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1698 /* If there is a better way to determine the window size, use it. */
1699 SIGWINCH_HANDLER ();
1702 /* If the output is not a terminal, don't paginate it. */
1703 if (!ISATTY (gdb_stdout
))
1704 lines_per_page
= UINT_MAX
;
1706 set_width_command ((char *)NULL
, 0, c
);
1709 (add_set_cmd ("demangle", class_support
, var_boolean
,
1711 "Set demangling of encoded C++ names when displaying symbols.",
1716 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
1717 (char *)&sevenbit_strings
,
1718 "Set printing of 8-bit characters in strings as \\nnn.",
1723 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
1724 (char *)&asm_demangle
,
1725 "Set demangling of C++ names in disassembly listings.",
1730 /* Machine specific function to handle SIGWINCH signal. */
1732 #ifdef SIGWINCH_HANDLER_BODY
1733 SIGWINCH_HANDLER_BODY