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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #if !defined(__GO32__) && !defined(WIN32)
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
26 #ifdef ANSI_PROTOTYPES
40 #include "expression.h"
46 /* readline defines this. */
49 /* Prototypes for local functions */
51 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
55 malloc_botch
PARAMS ((void));
57 #endif /* NO_MMALLOC, etc */
60 fatal_dump_core
PARAMS((char *, ...));
63 prompt_for_continue
PARAMS ((void));
66 set_width_command
PARAMS ((char *, int, struct cmd_list_element
*));
68 /* If this definition isn't overridden by the header files, assume
69 that isatty and fileno exist on this system. */
71 #define ISATTY(FP) (isatty (fileno (FP)))
74 /* Chain of cleanup actions established with make_cleanup,
75 to be executed if an error happens. */
77 static struct cleanup
*cleanup_chain
;
79 /* Nonzero if we have job control. */
83 /* Nonzero means a quit has been requested. */
87 /* Nonzero means quit immediately if Control-C is typed now, rather
88 than waiting until QUIT is executed. Be careful in setting this;
89 code which executes with immediate_quit set has to be very careful
90 about being able to deal with being interrupted at any time. It is
91 almost always better to use QUIT; the only exception I can think of
92 is being able to quit out of a system call (using EINTR loses if
93 the SIGINT happens between the previous QUIT and the system call).
94 To immediately quit in the case in which a SIGINT happens between
95 the previous QUIT and setting immediate_quit (desirable anytime we
96 expect to block), call QUIT after setting immediate_quit. */
100 /* Nonzero means that encoded C++ names should be printed out in their
101 C++ form rather than raw. */
105 /* Nonzero means that encoded C++ names should be printed out in their
106 C++ form even in assembler language displays. If this is set, but
107 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
109 int asm_demangle
= 0;
111 /* Nonzero means that strings with character values >0x7F should be printed
112 as octal escapes. Zero means just print the value (e.g. it's an
113 international character, and the terminal or window can cope.) */
115 int sevenbit_strings
= 0;
117 /* String to be printed before error messages, if any. */
119 char *error_pre_print
;
121 /* String to be printed before quit messages, if any. */
123 char *quit_pre_print
;
125 /* String to be printed before warning messages, if any. */
127 char *warning_pre_print
= "\nwarning: ";
129 /* Add a new cleanup to the cleanup_chain,
130 and return the previous chain pointer
131 to be passed later to do_cleanups or discard_cleanups.
132 Args are FUNCTION to clean up with, and ARG to pass to it. */
135 make_cleanup (function
, arg
)
136 void (*function
) PARAMS ((PTR
));
139 register struct cleanup
*new
140 = (struct cleanup
*) xmalloc (sizeof (struct cleanup
));
141 register struct cleanup
*old_chain
= cleanup_chain
;
143 new->next
= cleanup_chain
;
144 new->function
= function
;
151 /* Discard cleanups and do the actions they describe
152 until we get back to the point OLD_CHAIN in the cleanup_chain. */
155 do_cleanups (old_chain
)
156 register struct cleanup
*old_chain
;
158 register struct cleanup
*ptr
;
159 while ((ptr
= cleanup_chain
) != old_chain
)
161 cleanup_chain
= ptr
->next
; /* Do this first incase recursion */
162 (*ptr
->function
) (ptr
->arg
);
167 /* Discard cleanups, not doing the actions they describe,
168 until we get back to the point OLD_CHAIN in the cleanup_chain. */
171 discard_cleanups (old_chain
)
172 register struct cleanup
*old_chain
;
174 register struct cleanup
*ptr
;
175 while ((ptr
= cleanup_chain
) != old_chain
)
177 cleanup_chain
= ptr
->next
;
182 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
186 struct cleanup
*old_chain
= cleanup_chain
;
192 /* Restore the cleanup chain from a previously saved chain. */
194 restore_cleanups (chain
)
195 struct cleanup
*chain
;
197 cleanup_chain
= chain
;
200 /* This function is useful for cleanups.
204 old_chain = make_cleanup (free_current_contents, &foo);
206 to arrange to free the object thus allocated. */
209 free_current_contents (location
)
215 /* Provide a known function that does nothing, to use as a base for
216 for a possibly long chain of cleanups. This is useful where we
217 use the cleanup chain for handling normal cleanups as well as dealing
218 with cleanups that need to be done as a result of a call to error().
219 In such cases, we may not be certain where the first cleanup is, unless
220 we have a do-nothing one to always use as the base. */
230 /* Print a warning message. Way to use this is to call warning_begin,
231 output the warning message (use unfiltered output to gdb_stderr),
232 ending in a newline. There is not currently a warning_end that you
233 call afterwards, but such a thing might be added if it is useful
234 for a GUI to separate warning messages from other output.
236 FIXME: Why do warnings use unfiltered output and errors filtered?
237 Is this anything other than a historical accident? */
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
);
249 /* Print a warning message.
250 The first argument STRING is the warning message, used as a fprintf string,
251 and the remaining args are passed as arguments to it.
252 The primary difference between warnings and errors is that a warning
253 does not force the return to command level. */
257 #ifdef ANSI_PROTOTYPES
258 warning (char *string
, ...)
265 #ifdef ANSI_PROTOTYPES
266 va_start (args
, string
);
271 string
= va_arg (args
, char *);
274 vfprintf_unfiltered (gdb_stderr
, string
, args
);
275 fprintf_unfiltered (gdb_stderr
, "\n");
279 /* Start the printing of an error message. Way to use this is to call
280 this, output the error message (use filtered output to gdb_stderr
281 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
282 in a newline, and then call return_to_top_level (RETURN_ERROR).
283 error() provides a convenient way to do this for the special case
284 that the error message can be formatted with a single printf call,
285 but this is more general. */
289 target_terminal_ours ();
290 wrap_here (""); /* Force out any buffered output */
291 gdb_flush (gdb_stdout
);
293 annotate_error_begin ();
296 fprintf_filtered (gdb_stderr
, error_pre_print
);
299 /* Print an error message and return to command level.
300 The first argument STRING is the error message, used as a fprintf string,
301 and the remaining args are passed as arguments to it. */
303 #ifdef ANSI_PROTOTYPES
305 error (char *string
, ...)
311 #ifdef ANSI_PROTOTYPES
313 va_start (args
, string
);
323 #ifdef ANSI_PROTOTYPES
324 vfprintf_filtered (gdb_stderr
, string
, args
);
326 string1
= va_arg (args
, char *);
327 vfprintf_filtered (gdb_stderr
, string1
, args
);
329 fprintf_filtered (gdb_stderr
, "\n");
331 return_to_top_level (RETURN_ERROR
);
336 /* Print an error message and exit reporting failure.
337 This is for a error that we cannot continue from.
338 The arguments are printed a la printf.
340 This function cannot be declared volatile (NORETURN) in an
341 ANSI environment because exit() is not declared volatile. */
345 #ifdef ANSI_PROTOTYPES
346 fatal (char *string
, ...)
353 #ifdef ANSI_PROTOTYPES
354 va_start (args
, string
);
358 string
= va_arg (args
, char *);
360 fprintf_unfiltered (gdb_stderr
, "\ngdb: ");
361 vfprintf_unfiltered (gdb_stderr
, string
, args
);
362 fprintf_unfiltered (gdb_stderr
, "\n");
367 /* Print an error message and exit, dumping core.
368 The arguments are printed a la printf (). */
372 #ifdef ANSI_PROTOTYPES
373 fatal_dump_core (char *string
, ...)
375 fatal_dump_core (va_alist
)
380 #ifdef ANSI_PROTOTYPES
381 va_start (args
, string
);
386 string
= va_arg (args
, char *);
388 /* "internal error" is always correct, since GDB should never dump
389 core, no matter what the input. */
390 fprintf_unfiltered (gdb_stderr
, "\ngdb internal error: ");
391 vfprintf_unfiltered (gdb_stderr
, string
, args
);
392 fprintf_unfiltered (gdb_stderr
, "\n");
395 signal (SIGQUIT
, SIG_DFL
);
396 kill (getpid (), SIGQUIT
);
397 /* We should never get here, but just in case... */
401 /* The strerror() function can return NULL for errno values that are
402 out of range. Provide a "safe" version that always returns a
406 safe_strerror (errnum
)
412 if ((msg
= strerror (errnum
)) == NULL
)
414 sprintf (buf
, "(undocumented errno %d)", errnum
);
420 /* The strsignal() function can return NULL for signal values that are
421 out of range. Provide a "safe" version that always returns a
425 safe_strsignal (signo
)
431 if ((msg
= strsignal (signo
)) == NULL
)
433 sprintf (buf
, "(undocumented signal %d)", signo
);
440 /* Print the system error message for errno, and also mention STRING
441 as the file name for which the error was encountered.
442 Then return to command level. */
445 perror_with_name (string
)
451 err
= safe_strerror (errno
);
452 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
453 strcpy (combined
, string
);
454 strcat (combined
, ": ");
455 strcat (combined
, err
);
457 /* I understand setting these is a matter of taste. Still, some people
458 may clear errno but not know about bfd_error. Doing this here is not
460 bfd_set_error (bfd_error_no_error
);
463 error ("%s.", combined
);
466 /* Print the system error message for ERRCODE, and also mention STRING
467 as the file name for which the error was encountered. */
470 print_sys_errmsg (string
, errcode
)
477 err
= safe_strerror (errcode
);
478 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
479 strcpy (combined
, string
);
480 strcat (combined
, ": ");
481 strcat (combined
, err
);
483 /* We want anything which was printed on stdout to come out first, before
485 gdb_flush (gdb_stdout
);
486 fprintf_unfiltered (gdb_stderr
, "%s.\n", combined
);
489 /* Control C eventually causes this to be called, at a convenient time. */
494 serial_t gdb_stdout_serial
= serial_fdopen (1);
496 target_terminal_ours ();
498 /* We want all output to appear now, before we print "Quit". We
499 have 3 levels of buffering we have to flush (it's possible that
500 some of these should be changed to flush the lower-level ones
503 /* 1. The _filtered buffer. */
504 wrap_here ((char *)0);
506 /* 2. The stdio buffer. */
507 gdb_flush (gdb_stdout
);
508 gdb_flush (gdb_stderr
);
510 /* 3. The system-level buffer. */
511 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial
);
512 SERIAL_UN_FDOPEN (gdb_stdout_serial
);
514 annotate_error_begin ();
516 /* Don't use *_filtered; we don't want to prompt the user to continue. */
518 fprintf_unfiltered (gdb_stderr
, quit_pre_print
);
521 /* If there is no terminal switching for this target, then we can't
522 possibly get screwed by the lack of job control. */
523 || current_target
.to_terminal_ours
== NULL
)
524 fprintf_unfiltered (gdb_stderr
, "Quit\n");
526 fprintf_unfiltered (gdb_stderr
,
527 "Quit (expect signal SIGINT when the program is resumed)\n");
528 return_to_top_level (RETURN_QUIT
);
532 #if defined(__GO32__)||defined(WIN32)
534 /* In the absence of signals, poll keyboard for a quit.
535 Called from #define QUIT pollquit() in xm-go32.h. */
553 /* We just ignore it */
554 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
561 #if defined(__GO32__)||defined(WIN32)
576 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
583 /* Done by signals */
586 /* Control C comes here */
594 /* Restore the signal handler. Harmless with BSD-style signals, needed
595 for System V-style signals. So just always do it, rather than worrying
596 about USG defines and stuff like that. */
597 signal (signo
, request_quit
);
608 /* Memory management stuff (malloc friends). */
610 #if defined (NO_MMALLOC)
612 /* Make a substitute size_t for non-ANSI compilers. */
619 #define size_t unsigned int
629 return malloc (size
);
633 mrealloc (md
, ptr
, size
)
638 if (ptr
== 0) /* Guard against old realloc's */
639 return malloc (size
);
641 return realloc (ptr
, size
);
652 #endif /* NO_MMALLOC */
654 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
662 #else /* have mmalloc and want corruption checking */
667 fatal_dump_core ("Memory corruption");
670 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
671 by MD, to detect memory corruption. Note that MD may be NULL to specify
672 the default heap that grows via sbrk.
674 Note that for freshly created regions, we must call mmcheck prior to any
675 mallocs in the region. Otherwise, any region which was allocated prior to
676 installing the checking hooks, which is later reallocated or freed, will
677 fail the checks! The mmcheck function only allows initial hooks to be
678 installed before the first mmalloc. However, anytime after we have called
679 mmcheck the first time to install the checking hooks, we can call it again
680 to update the function pointer to the memory corruption handler.
682 Returns zero on failure, non-zero on success. */
688 if (!mmcheck (md
, malloc_botch
))
690 warning ("internal error: failed to install memory consistency checks");
696 #endif /* Have mmalloc and want corruption checking */
698 /* Called when a memory allocation fails, with the number of bytes of
699 memory requested in SIZE. */
707 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size
);
711 fatal ("virtual memory exhausted.");
715 /* Like mmalloc but get error if no storage available, and protect against
716 the caller wanting to allocate zero bytes. Whether to return NULL for
717 a zero byte request, or translate the request into a request for one
718 byte of zero'd storage, is a religious issue. */
731 else if ((val
= mmalloc (md
, size
)) == NULL
)
738 /* Like mrealloc but get error if no storage available. */
741 xmrealloc (md
, ptr
, size
)
750 val
= mrealloc (md
, ptr
, size
);
754 val
= mmalloc (md
, size
);
763 /* Like malloc but get error if no storage available, and protect against
764 the caller wanting to allocate zero bytes. */
770 return (xmmalloc ((PTR
) NULL
, size
));
773 /* Like mrealloc but get error if no storage available. */
780 return (xmrealloc ((PTR
) NULL
, ptr
, size
));
784 /* My replacement for the read system call.
785 Used like `read' but keeps going if `read' returns too soon. */
788 myread (desc
, addr
, len
)
798 val
= read (desc
, addr
, len
);
809 /* Make a copy of the string at PTR with SIZE characters
810 (and add a null character at the end in the copy).
811 Uses malloc to get the space. Returns the address of the copy. */
814 savestring (ptr
, size
)
818 register char *p
= (char *) xmalloc (size
+ 1);
819 memcpy (p
, ptr
, size
);
825 msavestring (md
, ptr
, size
)
830 register char *p
= (char *) xmmalloc (md
, size
+ 1);
831 memcpy (p
, ptr
, size
);
836 /* The "const" is so it compiles under DGUX (which prototypes strsave
837 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
838 Doesn't real strsave return NULL if out of memory? */
843 return savestring (ptr
, strlen (ptr
));
851 return (msavestring (md
, ptr
, strlen (ptr
)));
855 print_spaces (n
, file
)
863 /* Print a host address. */
866 gdb_print_address (addr
, stream
)
871 /* We could use the %p conversion specifier to fprintf if we had any
872 way of knowing whether this host supports it. But the following
873 should work on the Alpha and on 32 bit machines. */
875 fprintf_filtered (stream
, "0x%lx", (unsigned long)addr
);
878 /* Ask user a y-or-n question and return 1 iff answer is yes.
879 Takes three args which are given to printf to print the question.
880 The first, a control string, should end in "? ".
881 It should not say how to answer, because we do that. */
885 #ifdef ANSI_PROTOTYPES
886 query (char *ctlstr
, ...)
897 #ifdef ANSI_PROTOTYPES
898 va_start (args
, ctlstr
);
902 ctlstr
= va_arg (args
, char *);
907 return query_hook (ctlstr
, args
);
910 /* Automatically answer "yes" if input is not from a terminal. */
911 if (!input_from_terminal_p ())
914 /* FIXME Automatically answer "yes" if called from MacGDB. */
921 wrap_here (""); /* Flush any buffered output */
922 gdb_flush (gdb_stdout
);
924 if (annotation_level
> 1)
925 printf_filtered ("\n\032\032pre-query\n");
927 vfprintf_filtered (gdb_stdout
, ctlstr
, args
);
928 printf_filtered ("(y or n) ");
930 if (annotation_level
> 1)
931 printf_filtered ("\n\032\032query\n");
934 /* If not in MacGDB, move to a new line so the entered line doesn't
935 have a prompt on the front of it. */
937 fputs_unfiltered ("\n", gdb_stdout
);
940 gdb_flush (gdb_stdout
);
941 answer
= fgetc (stdin
);
942 clearerr (stdin
); /* in case of C-d */
943 if (answer
== EOF
) /* C-d */
948 if (answer
!= '\n') /* Eat rest of input line, to EOF or newline */
951 ans2
= fgetc (stdin
);
954 while (ans2
!= EOF
&& ans2
!= '\n');
967 printf_filtered ("Please answer y or n.\n");
970 if (annotation_level
> 1)
971 printf_filtered ("\n\032\032post-query\n");
976 /* Parse a C escape sequence. STRING_PTR points to a variable
977 containing a pointer to the string to parse. That pointer
978 should point to the character after the \. That pointer
979 is updated past the characters we use. The value of the
980 escape sequence is returned.
982 A negative value means the sequence \ newline was seen,
983 which is supposed to be equivalent to nothing at all.
985 If \ is followed by a null character, we return a negative
986 value and leave the string pointer pointing at the null character.
988 If \ is followed by 000, we return 0 and leave the string pointer
989 after the zeros. A value of 0 does not mean end of string. */
992 parse_escape (string_ptr
)
995 register int c
= *(*string_ptr
)++;
999 return 007; /* Bell (alert) char */
1002 case 'e': /* Escape character */
1020 c
= *(*string_ptr
)++;
1022 c
= parse_escape (string_ptr
);
1025 return (c
& 0200) | (c
& 037);
1036 register int i
= c
- '0';
1037 register int count
= 0;
1040 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
1058 /* Print the character C on STREAM as part of the contents of a literal
1059 string whose delimiter is QUOTER. Note that this routine should only
1060 be call for printing things which are independent of the language
1061 of the program being debugged. */
1064 gdb_printchar (c
, stream
, quoter
)
1070 c
&= 0xFF; /* Avoid sign bit follies */
1072 if ( c
< 0x20 || /* Low control chars */
1073 (c
>= 0x7F && c
< 0xA0) || /* DEL, High controls */
1074 (sevenbit_strings
&& c
>= 0x80)) { /* high order bit set */
1078 fputs_filtered ("\\n", stream
);
1081 fputs_filtered ("\\b", stream
);
1084 fputs_filtered ("\\t", stream
);
1087 fputs_filtered ("\\f", stream
);
1090 fputs_filtered ("\\r", stream
);
1093 fputs_filtered ("\\e", stream
);
1096 fputs_filtered ("\\a", stream
);
1099 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
1103 if (c
== '\\' || c
== quoter
)
1104 fputs_filtered ("\\", stream
);
1105 fprintf_filtered (stream
, "%c", c
);
1109 /* Number of lines per page or UINT_MAX if paging is disabled. */
1110 static unsigned int lines_per_page
;
1111 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1112 static unsigned int chars_per_line
;
1113 /* Current count of lines printed on this page, chars on this line. */
1114 static unsigned int lines_printed
, chars_printed
;
1116 /* Buffer and start column of buffered text, for doing smarter word-
1117 wrapping. When someone calls wrap_here(), we start buffering output
1118 that comes through fputs_filtered(). If we see a newline, we just
1119 spit it out and forget about the wrap_here(). If we see another
1120 wrap_here(), we spit it out and remember the newer one. If we see
1121 the end of the line, we spit out a newline, the indent, and then
1122 the buffered output. */
1124 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1125 are waiting to be output (they have already been counted in chars_printed).
1126 When wrap_buffer[0] is null, the buffer is empty. */
1127 static char *wrap_buffer
;
1129 /* Pointer in wrap_buffer to the next character to fill. */
1130 static char *wrap_pointer
;
1132 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1134 static char *wrap_indent
;
1136 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1137 is not in effect. */
1138 static int wrap_column
;
1142 set_width_command (args
, from_tty
, c
)
1145 struct cmd_list_element
*c
;
1149 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
1150 wrap_buffer
[0] = '\0';
1153 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
1154 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
1157 /* Wait, so the user can read what's on the screen. Prompt the user
1158 to continue by pressing RETURN. */
1161 prompt_for_continue ()
1164 char cont_prompt
[120];
1166 if (annotation_level
> 1)
1167 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1169 strcpy (cont_prompt
,
1170 "---Type <return> to continue, or q <return> to quit---");
1171 if (annotation_level
> 1)
1172 strcat (cont_prompt
, "\n\032\032prompt-for-continue\n");
1174 /* We must do this *before* we call gdb_readline, else it will eventually
1175 call us -- thinking that we're trying to print beyond the end of the
1177 reinitialize_more_filter ();
1180 /* On a real operating system, the user can quit with SIGINT.
1183 'q' is provided on all systems so users don't have to change habits
1184 from system to system, and because telling them what to do in
1185 the prompt is more user-friendly than expecting them to think of
1187 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1188 whereas control-C to gdb_readline will cause the user to get dumped
1190 ignore
= readline (cont_prompt
);
1192 if (annotation_level
> 1)
1193 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1198 while (*p
== ' ' || *p
== '\t')
1201 request_quit (SIGINT
);
1206 /* Now we have to do this again, so that GDB will know that it doesn't
1207 need to save the ---Type <return>--- line at the top of the screen. */
1208 reinitialize_more_filter ();
1210 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1213 /* Reinitialize filter; ie. tell it to reset to original values. */
1216 reinitialize_more_filter ()
1222 /* Indicate that if the next sequence of characters overflows the line,
1223 a newline should be inserted here rather than when it hits the end.
1224 If INDENT is non-null, it is a string to be printed to indent the
1225 wrapped part on the next line. INDENT must remain accessible until
1226 the next call to wrap_here() or until a newline is printed through
1229 If the line is already overfull, we immediately print a newline and
1230 the indentation, and disable further wrapping.
1232 If we don't know the width of lines, but we know the page height,
1233 we must not wrap words, but should still keep track of newlines
1234 that were explicitly printed.
1236 INDENT should not contain tabs, as that will mess up the char count
1237 on the next line. FIXME.
1239 This routine is guaranteed to force out any output which has been
1240 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1241 used to force out output from the wrap_buffer. */
1247 /* This should have been allocated, but be paranoid anyway. */
1253 *wrap_pointer
= '\0';
1254 fputs_unfiltered (wrap_buffer
, gdb_stdout
);
1256 wrap_pointer
= wrap_buffer
;
1257 wrap_buffer
[0] = '\0';
1258 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
1262 else if (chars_printed
>= chars_per_line
)
1264 puts_filtered ("\n");
1266 puts_filtered (indent
);
1271 wrap_column
= chars_printed
;
1275 wrap_indent
= indent
;
1279 /* Ensure that whatever gets printed next, using the filtered output
1280 commands, starts at the beginning of the line. I.E. if there is
1281 any pending output for the current line, flush it and start a new
1282 line. Otherwise do nothing. */
1287 if (chars_printed
> 0)
1289 puts_filtered ("\n");
1295 gdb_fopen (name
, mode
)
1299 return fopen (name
, mode
);
1308 flush_hook (stream
);
1315 /* Like fputs but if FILTER is true, pause after every screenful.
1317 Regardless of FILTER can wrap at points other than the final
1318 character of a line.
1320 Unlike fputs, fputs_maybe_filtered does not return a value.
1321 It is OK for LINEBUFFER to be NULL, in which case just don't print
1324 Note that a longjmp to top level may occur in this routine (only if
1325 FILTER is true) (since prompt_for_continue may do so) so this
1326 routine should not be called when cleanups are not in place. */
1329 fputs_maybe_filtered (linebuffer
, stream
, filter
)
1330 const char *linebuffer
;
1334 const char *lineptr
;
1336 if (linebuffer
== 0)
1339 /* Don't do any filtering if it is disabled. */
1340 if (stream
!= gdb_stdout
1341 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
1343 fputs_unfiltered (linebuffer
, stream
);
1347 /* Go through and output each character. Show line extension
1348 when this is necessary; prompt user for new page when this is
1351 lineptr
= linebuffer
;
1354 /* Possible new page. */
1356 (lines_printed
>= lines_per_page
- 1))
1357 prompt_for_continue ();
1359 while (*lineptr
&& *lineptr
!= '\n')
1361 /* Print a single line. */
1362 if (*lineptr
== '\t')
1365 *wrap_pointer
++ = '\t';
1367 fputc_unfiltered ('\t', stream
);
1368 /* Shifting right by 3 produces the number of tab stops
1369 we have already passed, and then adding one and
1370 shifting left 3 advances to the next tab stop. */
1371 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
1377 *wrap_pointer
++ = *lineptr
;
1379 fputc_unfiltered (*lineptr
, stream
);
1384 if (chars_printed
>= chars_per_line
)
1386 unsigned int save_chars
= chars_printed
;
1390 /* If we aren't actually wrapping, don't output newline --
1391 if chars_per_line is right, we probably just overflowed
1392 anyway; if it's wrong, let us keep going. */
1394 fputc_unfiltered ('\n', stream
);
1396 /* Possible new page. */
1397 if (lines_printed
>= lines_per_page
- 1)
1398 prompt_for_continue ();
1400 /* Now output indentation and wrapped string */
1403 fputs_unfiltered (wrap_indent
, stream
);
1404 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
1405 fputs_unfiltered (wrap_buffer
, stream
); /* and eject it */
1406 /* FIXME, this strlen is what prevents wrap_indent from
1407 containing tabs. However, if we recurse to print it
1408 and count its chars, we risk trouble if wrap_indent is
1409 longer than (the user settable) chars_per_line.
1410 Note also that this can set chars_printed > chars_per_line
1411 if we are printing a long string. */
1412 chars_printed
= strlen (wrap_indent
)
1413 + (save_chars
- wrap_column
);
1414 wrap_pointer
= wrap_buffer
; /* Reset buffer */
1415 wrap_buffer
[0] = '\0';
1416 wrap_column
= 0; /* And disable fancy wrap */
1421 if (*lineptr
== '\n')
1424 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1426 fputc_unfiltered ('\n', stream
);
1433 fputs_filtered (linebuffer
, stream
)
1434 const char *linebuffer
;
1437 fputs_maybe_filtered (linebuffer
, stream
, 1);
1441 putchar_unfiltered (c
)
1448 fputs_unfiltered (buf
, gdb_stdout
);
1453 fputc_unfiltered (c
, stream
)
1461 fputs_unfiltered (buf
, stream
);
1466 /* Print a variable number of ARGS using format FORMAT. If this
1467 information is going to put the amount written (since the last call
1468 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1469 call prompt_for_continue to get the users permision to continue.
1471 Unlike fprintf, this function does not return a value.
1473 We implement three variants, vfprintf (takes a vararg list and stream),
1474 fprintf (takes a stream to write on), and printf (the usual).
1476 Note also that a longjmp to top level may occur in this routine
1477 (since prompt_for_continue may do so) so this routine should not be
1478 called when cleanups are not in place. */
1481 vfprintf_maybe_filtered (stream
, format
, args
, filter
)
1488 struct cleanup
*old_cleanups
;
1490 vasprintf (&linebuffer
, format
, args
);
1491 if (linebuffer
== NULL
)
1493 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1496 old_cleanups
= make_cleanup (free
, linebuffer
);
1497 fputs_maybe_filtered (linebuffer
, stream
, filter
);
1498 do_cleanups (old_cleanups
);
1503 vfprintf_filtered (stream
, format
, args
)
1508 vfprintf_maybe_filtered (stream
, format
, args
, 1);
1512 vfprintf_unfiltered (stream
, format
, args
)
1518 struct cleanup
*old_cleanups
;
1520 vasprintf (&linebuffer
, format
, args
);
1521 if (linebuffer
== NULL
)
1523 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1526 old_cleanups
= make_cleanup (free
, linebuffer
);
1527 fputs_unfiltered (linebuffer
, stream
);
1528 do_cleanups (old_cleanups
);
1532 vprintf_filtered (format
, args
)
1536 vfprintf_maybe_filtered (gdb_stdout
, format
, args
, 1);
1540 vprintf_unfiltered (format
, args
)
1544 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1549 #ifdef ANSI_PROTOTYPES
1550 fprintf_filtered (FILE *stream
, char *format
, ...)
1552 fprintf_filtered (va_alist
)
1557 #ifdef ANSI_PROTOTYPES
1558 va_start (args
, format
);
1564 stream
= va_arg (args
, FILE *);
1565 format
= va_arg (args
, char *);
1567 vfprintf_filtered (stream
, format
, args
);
1573 #ifdef ANSI_PROTOTYPES
1574 fprintf_unfiltered (FILE *stream
, char *format
, ...)
1576 fprintf_unfiltered (va_alist
)
1581 #ifdef ANSI_PROTOTYPES
1582 va_start (args
, format
);
1588 stream
= va_arg (args
, FILE *);
1589 format
= va_arg (args
, char *);
1591 vfprintf_unfiltered (stream
, format
, args
);
1595 /* Like fprintf_filtered, but prints its result indented.
1596 Called as fprintfi_filtered (spaces, stream, format, ...); */
1600 #ifdef ANSI_PROTOTYPES
1601 fprintfi_filtered (int spaces
, FILE *stream
, char *format
, ...)
1603 fprintfi_filtered (va_alist
)
1608 #ifdef ANSI_PROTOTYPES
1609 va_start (args
, format
);
1616 spaces
= va_arg (args
, int);
1617 stream
= va_arg (args
, FILE *);
1618 format
= va_arg (args
, char *);
1620 print_spaces_filtered (spaces
, stream
);
1622 vfprintf_filtered (stream
, format
, args
);
1629 #ifdef ANSI_PROTOTYPES
1630 printf_filtered (char *format
, ...)
1632 printf_filtered (va_alist
)
1637 #ifdef ANSI_PROTOTYPES
1638 va_start (args
, format
);
1643 format
= va_arg (args
, char *);
1645 vfprintf_filtered (gdb_stdout
, format
, args
);
1652 #ifdef ANSI_PROTOTYPES
1653 printf_unfiltered (char *format
, ...)
1655 printf_unfiltered (va_alist
)
1660 #ifdef ANSI_PROTOTYPES
1661 va_start (args
, format
);
1666 format
= va_arg (args
, char *);
1668 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1672 /* Like printf_filtered, but prints it's result indented.
1673 Called as printfi_filtered (spaces, format, ...); */
1677 #ifdef ANSI_PROTOTYPES
1678 printfi_filtered (int spaces
, char *format
, ...)
1680 printfi_filtered (va_alist
)
1685 #ifdef ANSI_PROTOTYPES
1686 va_start (args
, format
);
1692 spaces
= va_arg (args
, int);
1693 format
= va_arg (args
, char *);
1695 print_spaces_filtered (spaces
, gdb_stdout
);
1696 vfprintf_filtered (gdb_stdout
, format
, args
);
1700 /* Easy -- but watch out!
1702 This routine is *not* a replacement for puts()! puts() appends a newline.
1703 This one doesn't, and had better not! */
1706 puts_filtered (string
)
1709 fputs_filtered (string
, gdb_stdout
);
1713 puts_unfiltered (string
)
1716 fputs_unfiltered (string
, gdb_stdout
);
1719 /* Return a pointer to N spaces and a null. The pointer is good
1720 until the next call to here. */
1726 static char *spaces
;
1727 static int max_spaces
;
1733 spaces
= (char *) xmalloc (n
+1);
1734 for (t
= spaces
+n
; t
!= spaces
;)
1740 return spaces
+ max_spaces
- n
;
1743 /* Print N spaces. */
1745 print_spaces_filtered (n
, stream
)
1749 fputs_filtered (n_spaces (n
), stream
);
1752 /* C++ demangler stuff. */
1754 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1755 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1756 If the name is not mangled, or the language for the name is unknown, or
1757 demangling is off, the name is printed in its "raw" form. */
1760 fprintf_symbol_filtered (stream
, name
, lang
, arg_mode
)
1770 /* If user wants to see raw output, no problem. */
1773 fputs_filtered (name
, stream
);
1779 case language_cplus
:
1780 demangled
= cplus_demangle (name
, arg_mode
);
1782 case language_chill
:
1783 demangled
= chill_demangle (name
);
1789 fputs_filtered (demangled
? demangled
: name
, stream
);
1790 if (demangled
!= NULL
)
1798 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1799 differences in whitespace. Returns 0 if they match, non-zero if they
1800 don't (slightly different than strcmp()'s range of return values).
1802 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1803 This "feature" is useful when searching for matching C++ function names
1804 (such as if the user types 'break FOO', where FOO is a mangled C++
1808 strcmp_iw (string1
, string2
)
1809 const char *string1
;
1810 const char *string2
;
1812 while ((*string1
!= '\0') && (*string2
!= '\0'))
1814 while (isspace (*string1
))
1818 while (isspace (*string2
))
1822 if (*string1
!= *string2
)
1826 if (*string1
!= '\0')
1832 return (*string1
!= '\0' && *string1
!= '(') || (*string2
!= '\0');
1839 struct cmd_list_element
*c
;
1841 c
= add_set_cmd ("width", class_support
, var_uinteger
,
1842 (char *)&chars_per_line
,
1843 "Set number of characters gdb thinks are in a line.",
1845 add_show_from_set (c
, &showlist
);
1846 c
->function
.sfunc
= set_width_command
;
1849 (add_set_cmd ("height", class_support
,
1850 var_uinteger
, (char *)&lines_per_page
,
1851 "Set number of lines gdb thinks are in a page.", &setlist
),
1854 /* These defaults will be used if we are unable to get the correct
1855 values from termcap. */
1856 #if defined(__GO32__) || defined(WIN32)
1857 lines_per_page
= ScreenRows();
1858 chars_per_line
= ScreenCols();
1860 lines_per_page
= 24;
1861 chars_per_line
= 80;
1864 /* No termcap under MPW, although might be cool to do something
1865 by looking at worksheet or console window sizes. */
1866 /* Initialize the screen height and width from termcap. */
1868 char *termtype
= getenv ("TERM");
1870 /* Positive means success, nonpositive means failure. */
1873 /* 2048 is large enough for all known terminals, according to the
1874 GNU termcap manual. */
1875 char term_buffer
[2048];
1879 status
= tgetent (term_buffer
, termtype
);
1884 val
= tgetnum ("li");
1886 lines_per_page
= val
;
1888 /* The number of lines per page is not mentioned
1889 in the terminal description. This probably means
1890 that paging is not useful (e.g. emacs shell window),
1891 so disable paging. */
1892 lines_per_page
= UINT_MAX
;
1894 val
= tgetnum ("co");
1896 chars_per_line
= val
;
1902 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1904 /* If there is a better way to determine the window size, use it. */
1905 SIGWINCH_HANDLER ();
1908 /* If the output is not a terminal, don't paginate it. */
1909 if (!ISATTY (gdb_stdout
))
1910 lines_per_page
= UINT_MAX
;
1912 set_width_command ((char *)NULL
, 0, c
);
1915 (add_set_cmd ("demangle", class_support
, var_boolean
,
1917 "Set demangling of encoded C++ names when displaying symbols.",
1922 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
1923 (char *)&sevenbit_strings
,
1924 "Set printing of 8-bit characters in strings as \\nnn.",
1929 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
1930 (char *)&asm_demangle
,
1931 "Set demangling of C++ names in disassembly listings.",
1936 /* Machine specific function to handle SIGWINCH signal. */
1938 #ifdef SIGWINCH_HANDLER_BODY
1939 SIGWINCH_HANDLER_BODY