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>
36 #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 if we have job control. */
79 /* Nonzero means a quit has been requested. */
83 /* Nonzero means quit immediately if Control-C is typed now, rather
84 than waiting until QUIT is executed. Be careful in setting this;
85 code which executes with immediate_quit set has to be very careful
86 about being able to deal with being interrupted at any time. It is
87 almost always better to use QUIT; the only exception I can think of
88 is being able to quit out of a system call (using EINTR loses if
89 the SIGINT happens between the previous QUIT and the system call).
90 To immediately quit in the case in which a SIGINT happens between
91 the previous QUIT and setting immediate_quit (desirable anytime we
92 expect to block), call QUIT after setting immediate_quit. */
96 /* Nonzero means that encoded C++ names should be printed out in their
97 C++ form rather than raw. */
101 /* Nonzero means that encoded C++ names should be printed out in their
102 C++ form even in assembler language displays. If this is set, but
103 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
105 int asm_demangle
= 0;
107 /* Nonzero means that strings with character values >0x7F should be printed
108 as octal escapes. Zero means just print the value (e.g. it's an
109 international character, and the terminal or window can cope.) */
111 int sevenbit_strings
= 0;
113 /* String to be printed before error messages, if any. */
115 char *error_pre_print
;
116 char *warning_pre_print
= "\nwarning: ";
118 /* Add a new cleanup to the cleanup_chain,
119 and return the previous chain pointer
120 to be passed later to do_cleanups or discard_cleanups.
121 Args are FUNCTION to clean up with, and ARG to pass to it. */
124 make_cleanup (function
, arg
)
125 void (*function
) PARAMS ((PTR
));
128 register struct cleanup
*new
129 = (struct cleanup
*) xmalloc (sizeof (struct cleanup
));
130 register struct cleanup
*old_chain
= cleanup_chain
;
132 new->next
= cleanup_chain
;
133 new->function
= function
;
140 /* Discard cleanups and do the actions they describe
141 until we get back to the point OLD_CHAIN in the cleanup_chain. */
144 do_cleanups (old_chain
)
145 register struct cleanup
*old_chain
;
147 register struct cleanup
*ptr
;
148 while ((ptr
= cleanup_chain
) != old_chain
)
150 cleanup_chain
= ptr
->next
; /* Do this first incase recursion */
151 (*ptr
->function
) (ptr
->arg
);
156 /* Discard cleanups, not doing the actions they describe,
157 until we get back to the point OLD_CHAIN in the cleanup_chain. */
160 discard_cleanups (old_chain
)
161 register struct cleanup
*old_chain
;
163 register struct cleanup
*ptr
;
164 while ((ptr
= cleanup_chain
) != old_chain
)
166 cleanup_chain
= ptr
->next
;
171 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
175 struct cleanup
*old_chain
= cleanup_chain
;
181 /* Restore the cleanup chain from a previously saved chain. */
183 restore_cleanups (chain
)
184 struct cleanup
*chain
;
186 cleanup_chain
= chain
;
189 /* This function is useful for cleanups.
193 old_chain = make_cleanup (free_current_contents, &foo);
195 to arrange to free the object thus allocated. */
198 free_current_contents (location
)
204 /* Provide a known function that does nothing, to use as a base for
205 for a possibly long chain of cleanups. This is useful where we
206 use the cleanup chain for handling normal cleanups as well as dealing
207 with cleanups that need to be done as a result of a call to error().
208 In such cases, we may not be certain where the first cleanup is, unless
209 we have a do-nothing one to always use as the base. */
219 /* Provide a hook for modules wishing to print their own warning messages
220 to set up the terminal state in a compatible way, without them having
221 to import all the target_<...> macros. */
226 target_terminal_ours ();
227 wrap_here(""); /* Force out any buffered output */
228 gdb_flush (gdb_stdout
);
231 /* Print a warning message.
232 The first argument STRING is the warning message, used as a fprintf string,
233 and the remaining args are passed as arguments to it.
234 The primary difference between warnings and errors is that a warning
235 does not force the return to command level. */
246 target_terminal_ours ();
247 wrap_here(""); /* Force out any buffered output */
248 gdb_flush (gdb_stdout
);
249 if (warning_pre_print
)
250 fprintf_unfiltered (gdb_stderr
, warning_pre_print
);
251 string
= va_arg (args
, char *);
252 vfprintf_unfiltered (gdb_stderr
, string
, args
);
253 fprintf_unfiltered (gdb_stderr
, "\n");
257 /* Start the printing of an error message. Way to use this is to call
258 this, output the error message (use filtered output), and then call
259 return_to_top_level (RETURN_ERROR). error() provides a convenient way to
260 do this for the special case that the error message can be formatted with
261 a single printf call, but this is more general. */
265 target_terminal_ours ();
266 wrap_here (""); /* Force out any buffered output */
267 gdb_flush (gdb_stdout
);
269 annotate_error_begin ();
272 fprintf_filtered (gdb_stderr
, error_pre_print
);
275 /* Print an error message and return to command level.
276 The first argument STRING is the error message, used as a fprintf string,
277 and the remaining args are passed as arguments to it. */
290 error_hook (args
); /* Never returns */
293 string
= va_arg (args
, char *);
294 vfprintf_filtered (gdb_stderr
, string
, args
);
295 fprintf_filtered (gdb_stderr
, "\n");
297 return_to_top_level (RETURN_ERROR
);
300 /* Print an error message and exit reporting failure.
301 This is for a error that we cannot continue from.
302 The arguments are printed a la printf.
304 This function cannot be declared volatile (NORETURN) in an
305 ANSI environment because exit() is not declared volatile. */
316 string
= va_arg (args
, char *);
317 fprintf_unfiltered (gdb_stderr
, "\ngdb: ");
318 vfprintf_unfiltered (gdb_stderr
, string
, args
);
319 fprintf_unfiltered (gdb_stderr
, "\n");
324 /* Print an error message and exit, dumping core.
325 The arguments are printed a la printf (). */
329 fatal_dump_core (va_alist
)
336 string
= va_arg (args
, char *);
337 /* "internal error" is always correct, since GDB should never dump
338 core, no matter what the input. */
339 fprintf_unfiltered (gdb_stderr
, "\ngdb internal error: ");
340 vfprintf_unfiltered (gdb_stderr
, string
, args
);
341 fprintf_unfiltered (gdb_stderr
, "\n");
344 signal (SIGQUIT
, SIG_DFL
);
345 kill (getpid (), SIGQUIT
);
346 /* We should never get here, but just in case... */
350 /* The strerror() function can return NULL for errno values that are
351 out of range. Provide a "safe" version that always returns a
355 safe_strerror (errnum
)
361 if ((msg
= strerror (errnum
)) == NULL
)
363 sprintf (buf
, "(undocumented errno %d)", errnum
);
369 /* The strsignal() function can return NULL for signal values that are
370 out of range. Provide a "safe" version that always returns a
374 safe_strsignal (signo
)
380 if ((msg
= strsignal (signo
)) == NULL
)
382 sprintf (buf
, "(undocumented signal %d)", signo
);
389 /* Print the system error message for errno, and also mention STRING
390 as the file name for which the error was encountered.
391 Then return to command level. */
394 perror_with_name (string
)
400 err
= safe_strerror (errno
);
401 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
402 strcpy (combined
, string
);
403 strcat (combined
, ": ");
404 strcat (combined
, err
);
406 /* I understand setting these is a matter of taste. Still, some people
407 may clear errno but not know about bfd_error. Doing this here is not
409 bfd_set_error (bfd_error_no_error
);
412 error ("%s.", combined
);
415 /* Print the system error message for ERRCODE, and also mention STRING
416 as the file name for which the error was encountered. */
419 print_sys_errmsg (string
, errcode
)
426 err
= safe_strerror (errcode
);
427 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
428 strcpy (combined
, string
);
429 strcat (combined
, ": ");
430 strcat (combined
, err
);
432 /* We want anything which was printed on stdout to come out first, before
434 gdb_flush (gdb_stdout
);
435 fprintf_unfiltered (gdb_stderr
, "%s.\n", combined
);
438 /* Control C eventually causes this to be called, at a convenient time. */
443 serial_t gdb_stdout_serial
= serial_fdopen (1);
445 target_terminal_ours ();
447 /* We want all output to appear now, before we print "Quit". We
448 have 3 levels of buffering we have to flush (it's possible that
449 some of these should be changed to flush the lower-level ones
452 /* 1. The _filtered buffer. */
453 wrap_here ((char *)0);
455 /* 2. The stdio buffer. */
456 gdb_flush (gdb_stdout
);
457 gdb_flush (gdb_stderr
);
459 /* 3. The system-level buffer. */
460 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial
);
461 SERIAL_UN_FDOPEN (gdb_stdout_serial
);
463 annotate_error_begin ();
465 /* Don't use *_filtered; we don't want to prompt the user to continue. */
467 fprintf_unfiltered (gdb_stderr
, error_pre_print
);
470 /* If there is no terminal switching for this target, then we can't
471 possibly get screwed by the lack of job control. */
472 || current_target
.to_terminal_ours
== NULL
)
473 fprintf_unfiltered (gdb_stderr
, "Quit\n");
475 fprintf_unfiltered (gdb_stderr
,
476 "Quit (expect signal SIGINT when the program is resumed)\n");
477 return_to_top_level (RETURN_QUIT
);
481 #if defined(__GO32__)||defined(WIN32)
483 /* In the absence of signals, poll keyboard for a quit.
484 Called from #define QUIT pollquit() in xm-go32.h. */
502 /* We just ignore it */
503 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
510 #if defined(__GO32__)||defined(WIN32)
525 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
532 /* Done by signals */
535 /* Control C comes here */
543 /* Restore the signal handler. Harmless with BSD-style signals, needed
544 for System V-style signals. So just always do it, rather than worrying
545 about USG defines and stuff like that. */
546 signal (signo
, request_quit
);
557 /* Memory management stuff (malloc friends). */
559 #if defined (NO_MMALLOC)
561 /* Make a substitute size_t for non-ANSI compilers. */
568 #define size_t unsigned int
578 return malloc (size
);
582 mrealloc (md
, ptr
, size
)
587 if (ptr
== 0) /* Guard against old realloc's */
588 return malloc (size
);
590 return realloc (ptr
, size
);
601 #endif /* NO_MMALLOC */
603 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
611 #else /* have mmalloc and want corruption checking */
616 fatal_dump_core ("Memory corruption");
619 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
620 by MD, to detect memory corruption. Note that MD may be NULL to specify
621 the default heap that grows via sbrk.
623 Note that for freshly created regions, we must call mmcheck prior to any
624 mallocs in the region. Otherwise, any region which was allocated prior to
625 installing the checking hooks, which is later reallocated or freed, will
626 fail the checks! The mmcheck function only allows initial hooks to be
627 installed before the first mmalloc. However, anytime after we have called
628 mmcheck the first time to install the checking hooks, we can call it again
629 to update the function pointer to the memory corruption handler.
631 Returns zero on failure, non-zero on success. */
637 if (!mmcheck (md
, malloc_botch
))
639 warning ("internal error: failed to install memory consistency checks");
645 #endif /* Have mmalloc and want corruption checking */
647 /* Called when a memory allocation fails, with the number of bytes of
648 memory requested in SIZE. */
656 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size
);
660 fatal ("virtual memory exhausted.");
664 /* Like mmalloc but get error if no storage available, and protect against
665 the caller wanting to allocate zero bytes. Whether to return NULL for
666 a zero byte request, or translate the request into a request for one
667 byte of zero'd storage, is a religious issue. */
680 else if ((val
= mmalloc (md
, size
)) == NULL
)
687 /* Like mrealloc but get error if no storage available. */
690 xmrealloc (md
, ptr
, size
)
699 val
= mrealloc (md
, ptr
, size
);
703 val
= mmalloc (md
, size
);
712 /* Like malloc but get error if no storage available, and protect against
713 the caller wanting to allocate zero bytes. */
719 return (xmmalloc ((PTR
) NULL
, size
));
722 /* Like mrealloc but get error if no storage available. */
729 return (xmrealloc ((PTR
) NULL
, ptr
, size
));
733 /* My replacement for the read system call.
734 Used like `read' but keeps going if `read' returns too soon. */
737 myread (desc
, addr
, len
)
747 val
= read (desc
, addr
, len
);
758 /* Make a copy of the string at PTR with SIZE characters
759 (and add a null character at the end in the copy).
760 Uses malloc to get the space. Returns the address of the copy. */
763 savestring (ptr
, size
)
767 register char *p
= (char *) xmalloc (size
+ 1);
768 memcpy (p
, ptr
, size
);
774 msavestring (md
, ptr
, size
)
779 register char *p
= (char *) xmmalloc (md
, size
+ 1);
780 memcpy (p
, ptr
, size
);
785 /* The "const" is so it compiles under DGUX (which prototypes strsave
786 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
787 Doesn't real strsave return NULL if out of memory? */
792 return savestring (ptr
, strlen (ptr
));
800 return (msavestring (md
, ptr
, strlen (ptr
)));
804 print_spaces (n
, file
)
812 /* Print a host address. */
815 gdb_print_address (addr
, stream
)
820 /* We could use the %p conversion specifier to fprintf if we had any
821 way of knowing whether this host supports it. But the following
822 should work on the Alpha and on 32 bit machines. */
824 fprintf_filtered (stream
, "0x%lx", (unsigned long)addr
);
827 /* Ask user a y-or-n question and return 1 iff answer is yes.
828 Takes three args which are given to printf to print the question.
829 The first, a control string, should end in "? ".
830 It should not say how to answer, because we do that. */
846 return query_hook (args
);
849 /* Automatically answer "yes" if input is not from a terminal. */
850 if (!input_from_terminal_p ())
852 /* start-sanitize-mpw */
854 /* Automatically answer "yes" if called from MacGDB. */
858 /* end-sanitize-mpw */
862 wrap_here (""); /* Flush any buffered output */
863 gdb_flush (gdb_stdout
);
865 if (annotation_level
> 1)
866 printf_filtered ("\n\032\032pre-query\n");
869 ctlstr
= va_arg (args
, char *);
870 vfprintf_filtered (gdb_stdout
, ctlstr
, args
);
872 printf_filtered ("(y or n) ");
874 if (annotation_level
> 1)
875 printf_filtered ("\n\032\032query\n");
877 /* start-sanitize-mpw */
879 /* If not in MacGDB, move to a new line so the entered line doesn't
880 have a prompt on the front of it. */
882 fputs_unfiltered ("\n", gdb_stdout
);
884 /* end-sanitize-mpw */
885 gdb_flush (gdb_stdout
);
886 answer
= fgetc (stdin
);
887 clearerr (stdin
); /* in case of C-d */
888 if (answer
== EOF
) /* C-d */
893 if (answer
!= '\n') /* Eat rest of input line, to EOF or newline */
896 ans2
= fgetc (stdin
);
899 while (ans2
!= EOF
&& ans2
!= '\n');
912 printf_filtered ("Please answer y or n.\n");
915 if (annotation_level
> 1)
916 printf_filtered ("\n\032\032post-query\n");
921 /* Parse a C escape sequence. STRING_PTR points to a variable
922 containing a pointer to the string to parse. That pointer
923 should point to the character after the \. That pointer
924 is updated past the characters we use. The value of the
925 escape sequence is returned.
927 A negative value means the sequence \ newline was seen,
928 which is supposed to be equivalent to nothing at all.
930 If \ is followed by a null character, we return a negative
931 value and leave the string pointer pointing at the null character.
933 If \ is followed by 000, we return 0 and leave the string pointer
934 after the zeros. A value of 0 does not mean end of string. */
937 parse_escape (string_ptr
)
940 register int c
= *(*string_ptr
)++;
944 return 007; /* Bell (alert) char */
947 case 'e': /* Escape character */
965 c
= *(*string_ptr
)++;
967 c
= parse_escape (string_ptr
);
970 return (c
& 0200) | (c
& 037);
981 register int i
= c
- '0';
982 register int count
= 0;
985 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
1003 /* Print the character C on STREAM as part of the contents of a literal
1004 string whose delimiter is QUOTER. Note that this routine should only
1005 be call for printing things which are independent of the language
1006 of the program being debugged. */
1009 gdb_printchar (c
, stream
, quoter
)
1015 c
&= 0xFF; /* Avoid sign bit follies */
1017 if ( c
< 0x20 || /* Low control chars */
1018 (c
>= 0x7F && c
< 0xA0) || /* DEL, High controls */
1019 (sevenbit_strings
&& c
>= 0x80)) { /* high order bit set */
1023 fputs_filtered ("\\n", stream
);
1026 fputs_filtered ("\\b", stream
);
1029 fputs_filtered ("\\t", stream
);
1032 fputs_filtered ("\\f", stream
);
1035 fputs_filtered ("\\r", stream
);
1038 fputs_filtered ("\\e", stream
);
1041 fputs_filtered ("\\a", stream
);
1044 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
1048 if (c
== '\\' || c
== quoter
)
1049 fputs_filtered ("\\", stream
);
1050 fprintf_filtered (stream
, "%c", c
);
1054 /* Number of lines per page or UINT_MAX if paging is disabled. */
1055 static unsigned int lines_per_page
;
1056 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1057 static unsigned int chars_per_line
;
1058 /* Current count of lines printed on this page, chars on this line. */
1059 static unsigned int lines_printed
, chars_printed
;
1061 /* Buffer and start column of buffered text, for doing smarter word-
1062 wrapping. When someone calls wrap_here(), we start buffering output
1063 that comes through fputs_filtered(). If we see a newline, we just
1064 spit it out and forget about the wrap_here(). If we see another
1065 wrap_here(), we spit it out and remember the newer one. If we see
1066 the end of the line, we spit out a newline, the indent, and then
1067 the buffered output. */
1069 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1070 are waiting to be output (they have already been counted in chars_printed).
1071 When wrap_buffer[0] is null, the buffer is empty. */
1072 static char *wrap_buffer
;
1074 /* Pointer in wrap_buffer to the next character to fill. */
1075 static char *wrap_pointer
;
1077 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1079 static char *wrap_indent
;
1081 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1082 is not in effect. */
1083 static int wrap_column
;
1087 set_width_command (args
, from_tty
, c
)
1090 struct cmd_list_element
*c
;
1094 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
1095 wrap_buffer
[0] = '\0';
1098 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
1099 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
1102 /* Wait, so the user can read what's on the screen. Prompt the user
1103 to continue by pressing RETURN. */
1106 prompt_for_continue ()
1109 char cont_prompt
[120];
1111 if (annotation_level
> 1)
1112 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1114 strcpy (cont_prompt
,
1115 "---Type <return> to continue, or q <return> to quit---");
1116 if (annotation_level
> 1)
1117 strcat (cont_prompt
, "\n\032\032prompt-for-continue\n");
1119 /* We must do this *before* we call gdb_readline, else it will eventually
1120 call us -- thinking that we're trying to print beyond the end of the
1122 reinitialize_more_filter ();
1125 /* On a real operating system, the user can quit with SIGINT.
1128 'q' is provided on all systems so users don't have to change habits
1129 from system to system, and because telling them what to do in
1130 the prompt is more user-friendly than expecting them to think of
1132 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1133 whereas control-C to gdb_readline will cause the user to get dumped
1135 ignore
= readline (cont_prompt
);
1137 if (annotation_level
> 1)
1138 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1143 while (*p
== ' ' || *p
== '\t')
1146 request_quit (SIGINT
);
1151 /* Now we have to do this again, so that GDB will know that it doesn't
1152 need to save the ---Type <return>--- line at the top of the screen. */
1153 reinitialize_more_filter ();
1155 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1158 /* Reinitialize filter; ie. tell it to reset to original values. */
1161 reinitialize_more_filter ()
1167 /* Indicate that if the next sequence of characters overflows the line,
1168 a newline should be inserted here rather than when it hits the end.
1169 If INDENT is non-null, it is a string to be printed to indent the
1170 wrapped part on the next line. INDENT must remain accessible until
1171 the next call to wrap_here() or until a newline is printed through
1174 If the line is already overfull, we immediately print a newline and
1175 the indentation, and disable further wrapping.
1177 If we don't know the width of lines, but we know the page height,
1178 we must not wrap words, but should still keep track of newlines
1179 that were explicitly printed.
1181 INDENT should not contain tabs, as that will mess up the char count
1182 on the next line. FIXME.
1184 This routine is guaranteed to force out any output which has been
1185 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1186 used to force out output from the wrap_buffer. */
1192 /* This should have been allocated, but be paranoid anyway. */
1198 *wrap_pointer
= '\0';
1199 fputs_unfiltered (wrap_buffer
, gdb_stdout
);
1201 wrap_pointer
= wrap_buffer
;
1202 wrap_buffer
[0] = '\0';
1203 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
1207 else if (chars_printed
>= chars_per_line
)
1209 puts_filtered ("\n");
1211 puts_filtered (indent
);
1216 wrap_column
= chars_printed
;
1220 wrap_indent
= indent
;
1224 /* Ensure that whatever gets printed next, using the filtered output
1225 commands, starts at the beginning of the line. I.E. if there is
1226 any pending output for the current line, flush it and start a new
1227 line. Otherwise do nothing. */
1232 if (chars_printed
> 0)
1234 puts_filtered ("\n");
1240 gdb_fopen (name
, mode
)
1244 return fopen (name
, mode
);
1253 flush_hook (stream
);
1260 /* Like fputs but if FILTER is true, pause after every screenful.
1262 Regardless of FILTER can wrap at points other than the final
1263 character of a line.
1265 Unlike fputs, fputs_maybe_filtered does not return a value.
1266 It is OK for LINEBUFFER to be NULL, in which case just don't print
1269 Note that a longjmp to top level may occur in this routine (only if
1270 FILTER is true) (since prompt_for_continue may do so) so this
1271 routine should not be called when cleanups are not in place. */
1274 fputs_maybe_filtered (linebuffer
, stream
, filter
)
1275 const char *linebuffer
;
1279 const char *lineptr
;
1281 if (linebuffer
== 0)
1284 /* Don't do any filtering if it is disabled. */
1285 if (stream
!= gdb_stdout
1286 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
1288 fputs_unfiltered (linebuffer
, stream
);
1292 /* Go through and output each character. Show line extension
1293 when this is necessary; prompt user for new page when this is
1296 lineptr
= linebuffer
;
1299 /* Possible new page. */
1301 (lines_printed
>= lines_per_page
- 1))
1302 prompt_for_continue ();
1304 while (*lineptr
&& *lineptr
!= '\n')
1306 /* Print a single line. */
1307 if (*lineptr
== '\t')
1310 *wrap_pointer
++ = '\t';
1312 fputc_unfiltered ('\t', stream
);
1313 /* Shifting right by 3 produces the number of tab stops
1314 we have already passed, and then adding one and
1315 shifting left 3 advances to the next tab stop. */
1316 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
1322 *wrap_pointer
++ = *lineptr
;
1324 fputc_unfiltered (*lineptr
, stream
);
1329 if (chars_printed
>= chars_per_line
)
1331 unsigned int save_chars
= chars_printed
;
1335 /* If we aren't actually wrapping, don't output newline --
1336 if chars_per_line is right, we probably just overflowed
1337 anyway; if it's wrong, let us keep going. */
1339 fputc_unfiltered ('\n', stream
);
1341 /* Possible new page. */
1342 if (lines_printed
>= lines_per_page
- 1)
1343 prompt_for_continue ();
1345 /* Now output indentation and wrapped string */
1348 fputs_unfiltered (wrap_indent
, stream
);
1349 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
1350 fputs_unfiltered (wrap_buffer
, stream
); /* and eject it */
1351 /* FIXME, this strlen is what prevents wrap_indent from
1352 containing tabs. However, if we recurse to print it
1353 and count its chars, we risk trouble if wrap_indent is
1354 longer than (the user settable) chars_per_line.
1355 Note also that this can set chars_printed > chars_per_line
1356 if we are printing a long string. */
1357 chars_printed
= strlen (wrap_indent
)
1358 + (save_chars
- wrap_column
);
1359 wrap_pointer
= wrap_buffer
; /* Reset buffer */
1360 wrap_buffer
[0] = '\0';
1361 wrap_column
= 0; /* And disable fancy wrap */
1366 if (*lineptr
== '\n')
1369 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1371 fputc_unfiltered ('\n', stream
);
1378 fputs_filtered (linebuffer
, stream
)
1379 const char *linebuffer
;
1382 fputs_maybe_filtered (linebuffer
, stream
, 1);
1392 fputs_unfiltered (buf
, gdb_stdout
);
1396 fputc_unfiltered (c
, stream
)
1403 fputs_unfiltered (buf
, stream
);
1407 /* Print a variable number of ARGS using format FORMAT. If this
1408 information is going to put the amount written (since the last call
1409 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1410 call prompt_for_continue to get the users permision to continue.
1412 Unlike fprintf, this function does not return a value.
1414 We implement three variants, vfprintf (takes a vararg list and stream),
1415 fprintf (takes a stream to write on), and printf (the usual).
1417 Note also that a longjmp to top level may occur in this routine
1418 (since prompt_for_continue may do so) so this routine should not be
1419 called when cleanups are not in place. */
1422 vfprintf_maybe_filtered (stream
, format
, args
, filter
)
1429 struct cleanup
*old_cleanups
;
1431 vasprintf (&linebuffer
, format
, args
);
1432 if (linebuffer
== NULL
)
1434 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1437 old_cleanups
= make_cleanup (free
, linebuffer
);
1438 fputs_maybe_filtered (linebuffer
, stream
, filter
);
1439 do_cleanups (old_cleanups
);
1444 vfprintf_filtered (stream
, format
, args
)
1449 vfprintf_maybe_filtered (stream
, format
, args
, 1);
1453 vfprintf_unfiltered (stream
, format
, args
)
1459 struct cleanup
*old_cleanups
;
1461 vasprintf (&linebuffer
, format
, args
);
1462 if (linebuffer
== NULL
)
1464 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1467 old_cleanups
= make_cleanup (free
, linebuffer
);
1468 fputs_unfiltered (linebuffer
, stream
);
1469 do_cleanups (old_cleanups
);
1473 vprintf_filtered (format
, args
)
1477 vfprintf_maybe_filtered (gdb_stdout
, format
, args
, 1);
1481 vprintf_unfiltered (format
, args
)
1485 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1490 fprintf_filtered (va_alist
)
1498 stream
= va_arg (args
, FILE *);
1499 format
= va_arg (args
, char *);
1501 vfprintf_filtered (stream
, format
, args
);
1507 fprintf_unfiltered (va_alist
)
1515 stream
= va_arg (args
, FILE *);
1516 format
= va_arg (args
, char *);
1518 vfprintf_unfiltered (stream
, format
, args
);
1522 /* Like fprintf_filtered, but prints its result indented.
1523 Called as fprintfi_filtered (spaces, stream, format, ...); */
1527 fprintfi_filtered (va_alist
)
1536 spaces
= va_arg (args
, int);
1537 stream
= va_arg (args
, FILE *);
1538 format
= va_arg (args
, char *);
1539 print_spaces_filtered (spaces
, stream
);
1541 vfprintf_filtered (stream
, format
, args
);
1548 printf_filtered (va_alist
)
1555 format
= va_arg (args
, char *);
1557 vfprintf_filtered (gdb_stdout
, format
, args
);
1564 printf_unfiltered (va_alist
)
1571 format
= va_arg (args
, char *);
1573 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1577 /* Like printf_filtered, but prints it's result indented.
1578 Called as printfi_filtered (spaces, format, ...); */
1582 printfi_filtered (va_alist
)
1590 spaces
= va_arg (args
, int);
1591 format
= va_arg (args
, char *);
1592 print_spaces_filtered (spaces
, gdb_stdout
);
1593 vfprintf_filtered (gdb_stdout
, format
, args
);
1597 /* Easy -- but watch out!
1599 This routine is *not* a replacement for puts()! puts() appends a newline.
1600 This one doesn't, and had better not! */
1603 puts_filtered (string
)
1606 fputs_filtered (string
, gdb_stdout
);
1610 puts_unfiltered (string
)
1613 fputs_unfiltered (string
, gdb_stdout
);
1616 /* Return a pointer to N spaces and a null. The pointer is good
1617 until the next call to here. */
1623 static char *spaces
;
1624 static int max_spaces
;
1630 spaces
= (char *) xmalloc (n
+1);
1631 for (t
= spaces
+n
; t
!= spaces
;)
1637 return spaces
+ max_spaces
- n
;
1640 /* Print N spaces. */
1642 print_spaces_filtered (n
, stream
)
1646 fputs_filtered (n_spaces (n
), stream
);
1649 /* C++ demangler stuff. */
1651 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1652 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1653 If the name is not mangled, or the language for the name is unknown, or
1654 demangling is off, the name is printed in its "raw" form. */
1657 fprintf_symbol_filtered (stream
, name
, lang
, arg_mode
)
1667 /* If user wants to see raw output, no problem. */
1670 fputs_filtered (name
, stream
);
1676 case language_cplus
:
1677 demangled
= cplus_demangle (name
, arg_mode
);
1679 case language_chill
:
1680 demangled
= chill_demangle (name
);
1686 fputs_filtered (demangled
? demangled
: name
, stream
);
1687 if (demangled
!= NULL
)
1695 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1696 differences in whitespace. Returns 0 if they match, non-zero if they
1697 don't (slightly different than strcmp()'s range of return values).
1699 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1700 This "feature" is useful when searching for matching C++ function names
1701 (such as if the user types 'break FOO', where FOO is a mangled C++
1705 strcmp_iw (string1
, string2
)
1706 const char *string1
;
1707 const char *string2
;
1709 while ((*string1
!= '\0') && (*string2
!= '\0'))
1711 while (isspace (*string1
))
1715 while (isspace (*string2
))
1719 if (*string1
!= *string2
)
1723 if (*string1
!= '\0')
1729 return (*string1
!= '\0' && *string1
!= '(') || (*string2
!= '\0');
1736 struct cmd_list_element
*c
;
1738 c
= add_set_cmd ("width", class_support
, var_uinteger
,
1739 (char *)&chars_per_line
,
1740 "Set number of characters gdb thinks are in a line.",
1742 add_show_from_set (c
, &showlist
);
1743 c
->function
.sfunc
= set_width_command
;
1746 (add_set_cmd ("height", class_support
,
1747 var_uinteger
, (char *)&lines_per_page
,
1748 "Set number of lines gdb thinks are in a page.", &setlist
),
1751 /* These defaults will be used if we are unable to get the correct
1752 values from termcap. */
1753 #if defined(__GO32__) || defined(WIN32)
1754 lines_per_page
= ScreenRows();
1755 chars_per_line
= ScreenCols();
1757 lines_per_page
= 24;
1758 chars_per_line
= 80;
1759 /* start-sanitize-mpw */
1761 /* No termcap under MPW, although might be cool to do something
1762 by looking at worksheet or console window sizes. */
1763 /* end-sanitize-mpw */
1764 /* Initialize the screen height and width from termcap. */
1766 char *termtype
= getenv ("TERM");
1768 /* Positive means success, nonpositive means failure. */
1771 /* 2048 is large enough for all known terminals, according to the
1772 GNU termcap manual. */
1773 char term_buffer
[2048];
1777 status
= tgetent (term_buffer
, termtype
);
1782 val
= tgetnum ("li");
1784 lines_per_page
= val
;
1786 /* The number of lines per page is not mentioned
1787 in the terminal description. This probably means
1788 that paging is not useful (e.g. emacs shell window),
1789 so disable paging. */
1790 lines_per_page
= UINT_MAX
;
1792 val
= tgetnum ("co");
1794 chars_per_line
= val
;
1798 /* start-sanitize-mpw */
1800 /* end-sanitize-mpw */
1802 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1804 /* If there is a better way to determine the window size, use it. */
1805 SIGWINCH_HANDLER ();
1808 /* If the output is not a terminal, don't paginate it. */
1809 if (!ISATTY (gdb_stdout
))
1810 lines_per_page
= UINT_MAX
;
1812 set_width_command ((char *)NULL
, 0, c
);
1815 (add_set_cmd ("demangle", class_support
, var_boolean
,
1817 "Set demangling of encoded C++ names when displaying symbols.",
1822 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
1823 (char *)&sevenbit_strings
,
1824 "Set printing of 8-bit characters in strings as \\nnn.",
1829 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
1830 (char *)&asm_demangle
,
1831 "Set demangling of C++ names in disassembly listings.",
1836 /* Machine specific function to handle SIGWINCH signal. */
1838 #ifdef SIGWINCH_HANDLER_BODY
1839 SIGWINCH_HANDLER_BODY