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
;
117 /* String to be printed before quit messages, if any. */
119 char *quit_pre_print
;
121 /* String to be printed before warning messages, if any. */
123 char *warning_pre_print
= "\nwarning: ";
125 /* Add a new cleanup to the cleanup_chain,
126 and return the previous chain pointer
127 to be passed later to do_cleanups or discard_cleanups.
128 Args are FUNCTION to clean up with, and ARG to pass to it. */
131 make_cleanup (function
, arg
)
132 void (*function
) PARAMS ((PTR
));
135 register struct cleanup
*new
136 = (struct cleanup
*) xmalloc (sizeof (struct cleanup
));
137 register struct cleanup
*old_chain
= cleanup_chain
;
139 new->next
= cleanup_chain
;
140 new->function
= function
;
147 /* Discard cleanups and do the actions they describe
148 until we get back to the point OLD_CHAIN in the cleanup_chain. */
151 do_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
; /* Do this first incase recursion */
158 (*ptr
->function
) (ptr
->arg
);
163 /* Discard cleanups, not doing the actions they describe,
164 until we get back to the point OLD_CHAIN in the cleanup_chain. */
167 discard_cleanups (old_chain
)
168 register struct cleanup
*old_chain
;
170 register struct cleanup
*ptr
;
171 while ((ptr
= cleanup_chain
) != old_chain
)
173 cleanup_chain
= ptr
->next
;
178 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
182 struct cleanup
*old_chain
= cleanup_chain
;
188 /* Restore the cleanup chain from a previously saved chain. */
190 restore_cleanups (chain
)
191 struct cleanup
*chain
;
193 cleanup_chain
= chain
;
196 /* This function is useful for cleanups.
200 old_chain = make_cleanup (free_current_contents, &foo);
202 to arrange to free the object thus allocated. */
205 free_current_contents (location
)
211 /* Provide a known function that does nothing, to use as a base for
212 for a possibly long chain of cleanups. This is useful where we
213 use the cleanup chain for handling normal cleanups as well as dealing
214 with cleanups that need to be done as a result of a call to error().
215 In such cases, we may not be certain where the first cleanup is, unless
216 we have a do-nothing one to always use as the base. */
226 /* Provide a hook for modules wishing to print their own warning messages
227 to set up the terminal state in a compatible way, without them having
228 to import all the target_<...> macros. */
233 target_terminal_ours ();
234 wrap_here(""); /* Force out any buffered output */
235 gdb_flush (gdb_stdout
);
238 /* Print a warning message.
239 The first argument STRING is the warning message, used as a fprintf string,
240 and the remaining args are passed as arguments to it.
241 The primary difference between warnings and errors is that a warning
242 does not force the return to command level. */
253 target_terminal_ours ();
254 wrap_here(""); /* Force out any buffered output */
255 gdb_flush (gdb_stdout
);
256 if (warning_pre_print
)
257 fprintf_unfiltered (gdb_stderr
, warning_pre_print
);
258 string
= va_arg (args
, char *);
259 vfprintf_unfiltered (gdb_stderr
, string
, args
);
260 fprintf_unfiltered (gdb_stderr
, "\n");
264 /* Start the printing of an error message. Way to use this is to call
265 this, output the error message (use filtered output), and then call
266 return_to_top_level (RETURN_ERROR). error() provides a convenient way to
267 do this for the special case that the error message can be formatted with
268 a single printf call, but this is more general. */
272 target_terminal_ours ();
273 wrap_here (""); /* Force out any buffered output */
274 gdb_flush (gdb_stdout
);
276 annotate_error_begin ();
279 fprintf_filtered (gdb_stderr
, error_pre_print
);
282 /* Print an error message and return to command level.
283 The first argument STRING is the error message, used as a fprintf string,
284 and the remaining args are passed as arguments to it. */
297 error_hook (args
); /* Never returns */
300 string
= va_arg (args
, char *);
301 vfprintf_filtered (gdb_stderr
, string
, args
);
302 fprintf_filtered (gdb_stderr
, "\n");
304 return_to_top_level (RETURN_ERROR
);
307 /* Print an error message and exit reporting failure.
308 This is for a error that we cannot continue from.
309 The arguments are printed a la printf.
311 This function cannot be declared volatile (NORETURN) in an
312 ANSI environment because exit() is not declared volatile. */
323 string
= va_arg (args
, char *);
324 fprintf_unfiltered (gdb_stderr
, "\ngdb: ");
325 vfprintf_unfiltered (gdb_stderr
, string
, args
);
326 fprintf_unfiltered (gdb_stderr
, "\n");
331 /* Print an error message and exit, dumping core.
332 The arguments are printed a la printf (). */
336 fatal_dump_core (va_alist
)
343 string
= va_arg (args
, char *);
344 /* "internal error" is always correct, since GDB should never dump
345 core, no matter what the input. */
346 fprintf_unfiltered (gdb_stderr
, "\ngdb internal error: ");
347 vfprintf_unfiltered (gdb_stderr
, string
, args
);
348 fprintf_unfiltered (gdb_stderr
, "\n");
351 signal (SIGQUIT
, SIG_DFL
);
352 kill (getpid (), SIGQUIT
);
353 /* We should never get here, but just in case... */
357 /* The strerror() function can return NULL for errno values that are
358 out of range. Provide a "safe" version that always returns a
362 safe_strerror (errnum
)
368 if ((msg
= strerror (errnum
)) == NULL
)
370 sprintf (buf
, "(undocumented errno %d)", errnum
);
376 /* The strsignal() function can return NULL for signal values that are
377 out of range. Provide a "safe" version that always returns a
381 safe_strsignal (signo
)
387 if ((msg
= strsignal (signo
)) == NULL
)
389 sprintf (buf
, "(undocumented signal %d)", signo
);
396 /* Print the system error message for errno, and also mention STRING
397 as the file name for which the error was encountered.
398 Then return to command level. */
401 perror_with_name (string
)
407 err
= safe_strerror (errno
);
408 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
409 strcpy (combined
, string
);
410 strcat (combined
, ": ");
411 strcat (combined
, err
);
413 /* I understand setting these is a matter of taste. Still, some people
414 may clear errno but not know about bfd_error. Doing this here is not
416 bfd_set_error (bfd_error_no_error
);
419 error ("%s.", combined
);
422 /* Print the system error message for ERRCODE, and also mention STRING
423 as the file name for which the error was encountered. */
426 print_sys_errmsg (string
, errcode
)
433 err
= safe_strerror (errcode
);
434 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
435 strcpy (combined
, string
);
436 strcat (combined
, ": ");
437 strcat (combined
, err
);
439 /* We want anything which was printed on stdout to come out first, before
441 gdb_flush (gdb_stdout
);
442 fprintf_unfiltered (gdb_stderr
, "%s.\n", combined
);
445 /* Control C eventually causes this to be called, at a convenient time. */
450 serial_t gdb_stdout_serial
= serial_fdopen (1);
452 target_terminal_ours ();
454 /* We want all output to appear now, before we print "Quit". We
455 have 3 levels of buffering we have to flush (it's possible that
456 some of these should be changed to flush the lower-level ones
459 /* 1. The _filtered buffer. */
460 wrap_here ((char *)0);
462 /* 2. The stdio buffer. */
463 gdb_flush (gdb_stdout
);
464 gdb_flush (gdb_stderr
);
466 /* 3. The system-level buffer. */
467 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial
);
468 SERIAL_UN_FDOPEN (gdb_stdout_serial
);
470 annotate_error_begin ();
472 /* Don't use *_filtered; we don't want to prompt the user to continue. */
474 fprintf_unfiltered (gdb_stderr
, quit_pre_print
);
477 /* If there is no terminal switching for this target, then we can't
478 possibly get screwed by the lack of job control. */
479 || current_target
.to_terminal_ours
== NULL
)
480 fprintf_unfiltered (gdb_stderr
, "Quit\n");
482 fprintf_unfiltered (gdb_stderr
,
483 "Quit (expect signal SIGINT when the program is resumed)\n");
484 return_to_top_level (RETURN_QUIT
);
488 #if defined(__GO32__)||defined(WIN32)
490 /* In the absence of signals, poll keyboard for a quit.
491 Called from #define QUIT pollquit() in xm-go32.h. */
509 /* We just ignore it */
510 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
517 #if defined(__GO32__)||defined(WIN32)
532 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
539 /* Done by signals */
542 /* Control C comes here */
550 /* Restore the signal handler. Harmless with BSD-style signals, needed
551 for System V-style signals. So just always do it, rather than worrying
552 about USG defines and stuff like that. */
553 signal (signo
, request_quit
);
564 /* Memory management stuff (malloc friends). */
566 #if defined (NO_MMALLOC)
568 /* Make a substitute size_t for non-ANSI compilers. */
575 #define size_t unsigned int
585 return malloc (size
);
589 mrealloc (md
, ptr
, size
)
594 if (ptr
== 0) /* Guard against old realloc's */
595 return malloc (size
);
597 return realloc (ptr
, size
);
608 #endif /* NO_MMALLOC */
610 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
618 #else /* have mmalloc and want corruption checking */
623 fatal_dump_core ("Memory corruption");
626 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
627 by MD, to detect memory corruption. Note that MD may be NULL to specify
628 the default heap that grows via sbrk.
630 Note that for freshly created regions, we must call mmcheck prior to any
631 mallocs in the region. Otherwise, any region which was allocated prior to
632 installing the checking hooks, which is later reallocated or freed, will
633 fail the checks! The mmcheck function only allows initial hooks to be
634 installed before the first mmalloc. However, anytime after we have called
635 mmcheck the first time to install the checking hooks, we can call it again
636 to update the function pointer to the memory corruption handler.
638 Returns zero on failure, non-zero on success. */
644 if (!mmcheck (md
, malloc_botch
))
646 warning ("internal error: failed to install memory consistency checks");
652 #endif /* Have mmalloc and want corruption checking */
654 /* Called when a memory allocation fails, with the number of bytes of
655 memory requested in SIZE. */
663 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size
);
667 fatal ("virtual memory exhausted.");
671 /* Like mmalloc but get error if no storage available, and protect against
672 the caller wanting to allocate zero bytes. Whether to return NULL for
673 a zero byte request, or translate the request into a request for one
674 byte of zero'd storage, is a religious issue. */
687 else if ((val
= mmalloc (md
, size
)) == NULL
)
694 /* Like mrealloc but get error if no storage available. */
697 xmrealloc (md
, ptr
, size
)
706 val
= mrealloc (md
, ptr
, size
);
710 val
= mmalloc (md
, size
);
719 /* Like malloc but get error if no storage available, and protect against
720 the caller wanting to allocate zero bytes. */
726 return (xmmalloc ((PTR
) NULL
, size
));
729 /* Like mrealloc but get error if no storage available. */
736 return (xmrealloc ((PTR
) NULL
, ptr
, size
));
740 /* My replacement for the read system call.
741 Used like `read' but keeps going if `read' returns too soon. */
744 myread (desc
, addr
, len
)
754 val
= read (desc
, addr
, len
);
765 /* Make a copy of the string at PTR with SIZE characters
766 (and add a null character at the end in the copy).
767 Uses malloc to get the space. Returns the address of the copy. */
770 savestring (ptr
, size
)
774 register char *p
= (char *) xmalloc (size
+ 1);
775 memcpy (p
, ptr
, size
);
781 msavestring (md
, ptr
, size
)
786 register char *p
= (char *) xmmalloc (md
, size
+ 1);
787 memcpy (p
, ptr
, size
);
792 /* The "const" is so it compiles under DGUX (which prototypes strsave
793 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
794 Doesn't real strsave return NULL if out of memory? */
799 return savestring (ptr
, strlen (ptr
));
807 return (msavestring (md
, ptr
, strlen (ptr
)));
811 print_spaces (n
, file
)
819 /* Print a host address. */
822 gdb_print_address (addr
, stream
)
827 /* We could use the %p conversion specifier to fprintf if we had any
828 way of knowing whether this host supports it. But the following
829 should work on the Alpha and on 32 bit machines. */
831 fprintf_filtered (stream
, "0x%lx", (unsigned long)addr
);
834 /* Ask user a y-or-n question and return 1 iff answer is yes.
835 Takes three args which are given to printf to print the question.
836 The first, a control string, should end in "? ".
837 It should not say how to answer, because we do that. */
853 return query_hook (args
);
856 /* Automatically answer "yes" if input is not from a terminal. */
857 if (!input_from_terminal_p ())
860 /* FIXME Automatically answer "yes" if called from MacGDB. */
867 wrap_here (""); /* Flush any buffered output */
868 gdb_flush (gdb_stdout
);
870 if (annotation_level
> 1)
871 printf_filtered ("\n\032\032pre-query\n");
874 ctlstr
= va_arg (args
, char *);
875 vfprintf_filtered (gdb_stdout
, ctlstr
, args
);
877 printf_filtered ("(y or n) ");
879 if (annotation_level
> 1)
880 printf_filtered ("\n\032\032query\n");
883 /* If not in MacGDB, move to a new line so the entered line doesn't
884 have a prompt on the front of it. */
886 fputs_unfiltered ("\n", gdb_stdout
);
889 gdb_flush (gdb_stdout
);
890 answer
= fgetc (stdin
);
891 clearerr (stdin
); /* in case of C-d */
892 if (answer
== EOF
) /* C-d */
897 if (answer
!= '\n') /* Eat rest of input line, to EOF or newline */
900 ans2
= fgetc (stdin
);
903 while (ans2
!= EOF
&& ans2
!= '\n');
916 printf_filtered ("Please answer y or n.\n");
919 if (annotation_level
> 1)
920 printf_filtered ("\n\032\032post-query\n");
925 /* Parse a C escape sequence. STRING_PTR points to a variable
926 containing a pointer to the string to parse. That pointer
927 should point to the character after the \. That pointer
928 is updated past the characters we use. The value of the
929 escape sequence is returned.
931 A negative value means the sequence \ newline was seen,
932 which is supposed to be equivalent to nothing at all.
934 If \ is followed by a null character, we return a negative
935 value and leave the string pointer pointing at the null character.
937 If \ is followed by 000, we return 0 and leave the string pointer
938 after the zeros. A value of 0 does not mean end of string. */
941 parse_escape (string_ptr
)
944 register int c
= *(*string_ptr
)++;
948 return 007; /* Bell (alert) char */
951 case 'e': /* Escape character */
969 c
= *(*string_ptr
)++;
971 c
= parse_escape (string_ptr
);
974 return (c
& 0200) | (c
& 037);
985 register int i
= c
- '0';
986 register int count
= 0;
989 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
1007 /* Print the character C on STREAM as part of the contents of a literal
1008 string whose delimiter is QUOTER. Note that this routine should only
1009 be call for printing things which are independent of the language
1010 of the program being debugged. */
1013 gdb_printchar (c
, stream
, quoter
)
1019 c
&= 0xFF; /* Avoid sign bit follies */
1021 if ( c
< 0x20 || /* Low control chars */
1022 (c
>= 0x7F && c
< 0xA0) || /* DEL, High controls */
1023 (sevenbit_strings
&& c
>= 0x80)) { /* high order bit set */
1027 fputs_filtered ("\\n", stream
);
1030 fputs_filtered ("\\b", stream
);
1033 fputs_filtered ("\\t", stream
);
1036 fputs_filtered ("\\f", stream
);
1039 fputs_filtered ("\\r", stream
);
1042 fputs_filtered ("\\e", stream
);
1045 fputs_filtered ("\\a", stream
);
1048 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
1052 if (c
== '\\' || c
== quoter
)
1053 fputs_filtered ("\\", stream
);
1054 fprintf_filtered (stream
, "%c", c
);
1058 /* Number of lines per page or UINT_MAX if paging is disabled. */
1059 static unsigned int lines_per_page
;
1060 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1061 static unsigned int chars_per_line
;
1062 /* Current count of lines printed on this page, chars on this line. */
1063 static unsigned int lines_printed
, chars_printed
;
1065 /* Buffer and start column of buffered text, for doing smarter word-
1066 wrapping. When someone calls wrap_here(), we start buffering output
1067 that comes through fputs_filtered(). If we see a newline, we just
1068 spit it out and forget about the wrap_here(). If we see another
1069 wrap_here(), we spit it out and remember the newer one. If we see
1070 the end of the line, we spit out a newline, the indent, and then
1071 the buffered output. */
1073 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1074 are waiting to be output (they have already been counted in chars_printed).
1075 When wrap_buffer[0] is null, the buffer is empty. */
1076 static char *wrap_buffer
;
1078 /* Pointer in wrap_buffer to the next character to fill. */
1079 static char *wrap_pointer
;
1081 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1083 static char *wrap_indent
;
1085 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1086 is not in effect. */
1087 static int wrap_column
;
1091 set_width_command (args
, from_tty
, c
)
1094 struct cmd_list_element
*c
;
1098 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
1099 wrap_buffer
[0] = '\0';
1102 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
1103 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
1106 /* Wait, so the user can read what's on the screen. Prompt the user
1107 to continue by pressing RETURN. */
1110 prompt_for_continue ()
1113 char cont_prompt
[120];
1115 if (annotation_level
> 1)
1116 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1118 strcpy (cont_prompt
,
1119 "---Type <return> to continue, or q <return> to quit---");
1120 if (annotation_level
> 1)
1121 strcat (cont_prompt
, "\n\032\032prompt-for-continue\n");
1123 /* We must do this *before* we call gdb_readline, else it will eventually
1124 call us -- thinking that we're trying to print beyond the end of the
1126 reinitialize_more_filter ();
1129 /* On a real operating system, the user can quit with SIGINT.
1132 'q' is provided on all systems so users don't have to change habits
1133 from system to system, and because telling them what to do in
1134 the prompt is more user-friendly than expecting them to think of
1136 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1137 whereas control-C to gdb_readline will cause the user to get dumped
1139 ignore
= readline (cont_prompt
);
1141 if (annotation_level
> 1)
1142 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1147 while (*p
== ' ' || *p
== '\t')
1150 request_quit (SIGINT
);
1155 /* Now we have to do this again, so that GDB will know that it doesn't
1156 need to save the ---Type <return>--- line at the top of the screen. */
1157 reinitialize_more_filter ();
1159 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1162 /* Reinitialize filter; ie. tell it to reset to original values. */
1165 reinitialize_more_filter ()
1171 /* Indicate that if the next sequence of characters overflows the line,
1172 a newline should be inserted here rather than when it hits the end.
1173 If INDENT is non-null, it is a string to be printed to indent the
1174 wrapped part on the next line. INDENT must remain accessible until
1175 the next call to wrap_here() or until a newline is printed through
1178 If the line is already overfull, we immediately print a newline and
1179 the indentation, and disable further wrapping.
1181 If we don't know the width of lines, but we know the page height,
1182 we must not wrap words, but should still keep track of newlines
1183 that were explicitly printed.
1185 INDENT should not contain tabs, as that will mess up the char count
1186 on the next line. FIXME.
1188 This routine is guaranteed to force out any output which has been
1189 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1190 used to force out output from the wrap_buffer. */
1196 /* This should have been allocated, but be paranoid anyway. */
1202 *wrap_pointer
= '\0';
1203 fputs_unfiltered (wrap_buffer
, gdb_stdout
);
1205 wrap_pointer
= wrap_buffer
;
1206 wrap_buffer
[0] = '\0';
1207 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
1211 else if (chars_printed
>= chars_per_line
)
1213 puts_filtered ("\n");
1215 puts_filtered (indent
);
1220 wrap_column
= chars_printed
;
1224 wrap_indent
= indent
;
1228 /* Ensure that whatever gets printed next, using the filtered output
1229 commands, starts at the beginning of the line. I.E. if there is
1230 any pending output for the current line, flush it and start a new
1231 line. Otherwise do nothing. */
1236 if (chars_printed
> 0)
1238 puts_filtered ("\n");
1244 gdb_fopen (name
, mode
)
1248 return fopen (name
, mode
);
1257 flush_hook (stream
);
1264 /* Like fputs but if FILTER is true, pause after every screenful.
1266 Regardless of FILTER can wrap at points other than the final
1267 character of a line.
1269 Unlike fputs, fputs_maybe_filtered does not return a value.
1270 It is OK for LINEBUFFER to be NULL, in which case just don't print
1273 Note that a longjmp to top level may occur in this routine (only if
1274 FILTER is true) (since prompt_for_continue may do so) so this
1275 routine should not be called when cleanups are not in place. */
1278 fputs_maybe_filtered (linebuffer
, stream
, filter
)
1279 const char *linebuffer
;
1283 const char *lineptr
;
1285 if (linebuffer
== 0)
1288 /* Don't do any filtering if it is disabled. */
1289 if (stream
!= gdb_stdout
1290 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
1292 fputs_unfiltered (linebuffer
, stream
);
1296 /* Go through and output each character. Show line extension
1297 when this is necessary; prompt user for new page when this is
1300 lineptr
= linebuffer
;
1303 /* Possible new page. */
1305 (lines_printed
>= lines_per_page
- 1))
1306 prompt_for_continue ();
1308 while (*lineptr
&& *lineptr
!= '\n')
1310 /* Print a single line. */
1311 if (*lineptr
== '\t')
1314 *wrap_pointer
++ = '\t';
1316 fputc_unfiltered ('\t', stream
);
1317 /* Shifting right by 3 produces the number of tab stops
1318 we have already passed, and then adding one and
1319 shifting left 3 advances to the next tab stop. */
1320 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
1326 *wrap_pointer
++ = *lineptr
;
1328 fputc_unfiltered (*lineptr
, stream
);
1333 if (chars_printed
>= chars_per_line
)
1335 unsigned int save_chars
= chars_printed
;
1339 /* If we aren't actually wrapping, don't output newline --
1340 if chars_per_line is right, we probably just overflowed
1341 anyway; if it's wrong, let us keep going. */
1343 fputc_unfiltered ('\n', stream
);
1345 /* Possible new page. */
1346 if (lines_printed
>= lines_per_page
- 1)
1347 prompt_for_continue ();
1349 /* Now output indentation and wrapped string */
1352 fputs_unfiltered (wrap_indent
, stream
);
1353 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
1354 fputs_unfiltered (wrap_buffer
, stream
); /* and eject it */
1355 /* FIXME, this strlen is what prevents wrap_indent from
1356 containing tabs. However, if we recurse to print it
1357 and count its chars, we risk trouble if wrap_indent is
1358 longer than (the user settable) chars_per_line.
1359 Note also that this can set chars_printed > chars_per_line
1360 if we are printing a long string. */
1361 chars_printed
= strlen (wrap_indent
)
1362 + (save_chars
- wrap_column
);
1363 wrap_pointer
= wrap_buffer
; /* Reset buffer */
1364 wrap_buffer
[0] = '\0';
1365 wrap_column
= 0; /* And disable fancy wrap */
1370 if (*lineptr
== '\n')
1373 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1375 fputc_unfiltered ('\n', stream
);
1382 fputs_filtered (linebuffer
, stream
)
1383 const char *linebuffer
;
1386 fputs_maybe_filtered (linebuffer
, stream
, 1);
1390 putchar_unfiltered (c
)
1397 fputs_unfiltered (buf
, gdb_stdout
);
1402 fputc_unfiltered (c
, stream
)
1410 fputs_unfiltered (buf
, stream
);
1415 /* Print a variable number of ARGS using format FORMAT. If this
1416 information is going to put the amount written (since the last call
1417 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1418 call prompt_for_continue to get the users permision to continue.
1420 Unlike fprintf, this function does not return a value.
1422 We implement three variants, vfprintf (takes a vararg list and stream),
1423 fprintf (takes a stream to write on), and printf (the usual).
1425 Note also that a longjmp to top level may occur in this routine
1426 (since prompt_for_continue may do so) so this routine should not be
1427 called when cleanups are not in place. */
1430 vfprintf_maybe_filtered (stream
, format
, args
, filter
)
1437 struct cleanup
*old_cleanups
;
1439 vasprintf (&linebuffer
, format
, args
);
1440 if (linebuffer
== NULL
)
1442 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1445 old_cleanups
= make_cleanup (free
, linebuffer
);
1446 fputs_maybe_filtered (linebuffer
, stream
, filter
);
1447 do_cleanups (old_cleanups
);
1452 vfprintf_filtered (stream
, format
, args
)
1457 vfprintf_maybe_filtered (stream
, format
, args
, 1);
1461 vfprintf_unfiltered (stream
, format
, args
)
1467 struct cleanup
*old_cleanups
;
1469 vasprintf (&linebuffer
, format
, args
);
1470 if (linebuffer
== NULL
)
1472 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1475 old_cleanups
= make_cleanup (free
, linebuffer
);
1476 fputs_unfiltered (linebuffer
, stream
);
1477 do_cleanups (old_cleanups
);
1481 vprintf_filtered (format
, args
)
1485 vfprintf_maybe_filtered (gdb_stdout
, format
, args
, 1);
1489 vprintf_unfiltered (format
, args
)
1493 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1498 fprintf_filtered (va_alist
)
1506 stream
= va_arg (args
, FILE *);
1507 format
= va_arg (args
, char *);
1509 vfprintf_filtered (stream
, format
, args
);
1515 fprintf_unfiltered (va_alist
)
1523 stream
= va_arg (args
, FILE *);
1524 format
= va_arg (args
, char *);
1526 vfprintf_unfiltered (stream
, format
, args
);
1530 /* Like fprintf_filtered, but prints its result indented.
1531 Called as fprintfi_filtered (spaces, stream, format, ...); */
1535 fprintfi_filtered (va_alist
)
1544 spaces
= va_arg (args
, int);
1545 stream
= va_arg (args
, FILE *);
1546 format
= va_arg (args
, char *);
1547 print_spaces_filtered (spaces
, stream
);
1549 vfprintf_filtered (stream
, format
, args
);
1556 printf_filtered (va_alist
)
1563 format
= va_arg (args
, char *);
1565 vfprintf_filtered (gdb_stdout
, format
, args
);
1572 printf_unfiltered (va_alist
)
1579 format
= va_arg (args
, char *);
1581 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1585 /* Like printf_filtered, but prints it's result indented.
1586 Called as printfi_filtered (spaces, format, ...); */
1590 printfi_filtered (va_alist
)
1598 spaces
= va_arg (args
, int);
1599 format
= va_arg (args
, char *);
1600 print_spaces_filtered (spaces
, gdb_stdout
);
1601 vfprintf_filtered (gdb_stdout
, format
, args
);
1605 /* Easy -- but watch out!
1607 This routine is *not* a replacement for puts()! puts() appends a newline.
1608 This one doesn't, and had better not! */
1611 puts_filtered (string
)
1614 fputs_filtered (string
, gdb_stdout
);
1618 puts_unfiltered (string
)
1621 fputs_unfiltered (string
, gdb_stdout
);
1624 /* Return a pointer to N spaces and a null. The pointer is good
1625 until the next call to here. */
1631 static char *spaces
;
1632 static int max_spaces
;
1638 spaces
= (char *) xmalloc (n
+1);
1639 for (t
= spaces
+n
; t
!= spaces
;)
1645 return spaces
+ max_spaces
- n
;
1648 /* Print N spaces. */
1650 print_spaces_filtered (n
, stream
)
1654 fputs_filtered (n_spaces (n
), stream
);
1657 /* C++ demangler stuff. */
1659 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1660 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1661 If the name is not mangled, or the language for the name is unknown, or
1662 demangling is off, the name is printed in its "raw" form. */
1665 fprintf_symbol_filtered (stream
, name
, lang
, arg_mode
)
1675 /* If user wants to see raw output, no problem. */
1678 fputs_filtered (name
, stream
);
1684 case language_cplus
:
1685 demangled
= cplus_demangle (name
, arg_mode
);
1687 case language_chill
:
1688 demangled
= chill_demangle (name
);
1694 fputs_filtered (demangled
? demangled
: name
, stream
);
1695 if (demangled
!= NULL
)
1703 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1704 differences in whitespace. Returns 0 if they match, non-zero if they
1705 don't (slightly different than strcmp()'s range of return values).
1707 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1708 This "feature" is useful when searching for matching C++ function names
1709 (such as if the user types 'break FOO', where FOO is a mangled C++
1713 strcmp_iw (string1
, string2
)
1714 const char *string1
;
1715 const char *string2
;
1717 while ((*string1
!= '\0') && (*string2
!= '\0'))
1719 while (isspace (*string1
))
1723 while (isspace (*string2
))
1727 if (*string1
!= *string2
)
1731 if (*string1
!= '\0')
1737 return (*string1
!= '\0' && *string1
!= '(') || (*string2
!= '\0');
1744 struct cmd_list_element
*c
;
1746 c
= add_set_cmd ("width", class_support
, var_uinteger
,
1747 (char *)&chars_per_line
,
1748 "Set number of characters gdb thinks are in a line.",
1750 add_show_from_set (c
, &showlist
);
1751 c
->function
.sfunc
= set_width_command
;
1754 (add_set_cmd ("height", class_support
,
1755 var_uinteger
, (char *)&lines_per_page
,
1756 "Set number of lines gdb thinks are in a page.", &setlist
),
1759 /* These defaults will be used if we are unable to get the correct
1760 values from termcap. */
1761 #if defined(__GO32__) || defined(WIN32)
1762 lines_per_page
= ScreenRows();
1763 chars_per_line
= ScreenCols();
1765 lines_per_page
= 24;
1766 chars_per_line
= 80;
1769 /* No termcap under MPW, although might be cool to do something
1770 by looking at worksheet or console window sizes. */
1771 /* Initialize the screen height and width from termcap. */
1773 char *termtype
= getenv ("TERM");
1775 /* Positive means success, nonpositive means failure. */
1778 /* 2048 is large enough for all known terminals, according to the
1779 GNU termcap manual. */
1780 char term_buffer
[2048];
1784 status
= tgetent (term_buffer
, termtype
);
1789 val
= tgetnum ("li");
1791 lines_per_page
= val
;
1793 /* The number of lines per page is not mentioned
1794 in the terminal description. This probably means
1795 that paging is not useful (e.g. emacs shell window),
1796 so disable paging. */
1797 lines_per_page
= UINT_MAX
;
1799 val
= tgetnum ("co");
1801 chars_per_line
= val
;
1807 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1809 /* If there is a better way to determine the window size, use it. */
1810 SIGWINCH_HANDLER ();
1813 /* If the output is not a terminal, don't paginate it. */
1814 if (!ISATTY (gdb_stdout
))
1815 lines_per_page
= UINT_MAX
;
1817 set_width_command ((char *)NULL
, 0, c
);
1820 (add_set_cmd ("demangle", class_support
, var_boolean
,
1822 "Set demangling of encoded C++ names when displaying symbols.",
1827 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
1828 (char *)&sevenbit_strings
,
1829 "Set printing of 8-bit characters in strings as \\nnn.",
1834 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
1835 (char *)&asm_demangle
,
1836 "Set demangling of C++ names in disassembly listings.",
1841 /* Machine specific function to handle SIGWINCH signal. */
1843 #ifdef SIGWINCH_HANDLER_BODY
1844 SIGWINCH_HANDLER_BODY