ab48cec6aafe443de07e66cf27fceaf0dfc57803
1 /* General utility routines for GDB, the GNU debugger.
2 Copyright (C) 1986, 1989, 1990, 1991 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 #include <sys/ioctl.h>
22 #include <sys/param.h>
35 extern volatile void return_to_top_level ();
36 extern volatile void exit ();
37 extern char *gdb_readline ();
38 extern char *getenv();
39 extern char *malloc();
40 extern char *realloc();
42 /* If this definition isn't overridden by the header files, assume
43 that isatty and fileno exist on this system. */
45 #define ISATTY(FP) (isatty (fileno (FP)))
48 /* Chain of cleanup actions established with make_cleanup,
49 to be executed if an error happens. */
51 static struct cleanup
*cleanup_chain
;
53 /* Nonzero means a quit has been requested. */
57 /* Nonzero means quit immediately if Control-C is typed now,
58 rather than waiting until QUIT is executed. */
62 /* Nonzero means that encoded C++ names should be printed out in their
63 C++ form rather than raw. */
67 /* Nonzero means that encoded C++ names should be printed out in their
68 C++ form even in assembler language displays. If this is set, but
69 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
73 /* Nonzero means that strings with character values >0x7F should be printed
74 as octal escapes. Zero means just print the value (e.g. it's an
75 international character, and the terminal or window can cope.) */
77 int sevenbit_strings
= 0;
79 /* String to be printed before error messages, if any. */
81 char *error_pre_print
;
82 char *warning_pre_print
;
84 /* Add a new cleanup to the cleanup_chain,
85 and return the previous chain pointer
86 to be passed later to do_cleanups or discard_cleanups.
87 Args are FUNCTION to clean up with, and ARG to pass to it. */
90 make_cleanup (function
, arg
)
94 register struct cleanup
*new
95 = (struct cleanup
*) xmalloc (sizeof (struct cleanup
));
96 register struct cleanup
*old_chain
= cleanup_chain
;
98 new->next
= cleanup_chain
;
99 new->function
= function
;
106 /* Discard cleanups and do the actions they describe
107 until we get back to the point OLD_CHAIN in the cleanup_chain. */
110 do_cleanups (old_chain
)
111 register struct cleanup
*old_chain
;
113 register struct cleanup
*ptr
;
114 while ((ptr
= cleanup_chain
) != old_chain
)
116 cleanup_chain
= ptr
->next
; /* Do this first incase recursion */
117 (*ptr
->function
) (ptr
->arg
);
122 /* Discard cleanups, not doing the actions they describe,
123 until we get back to the point OLD_CHAIN in the cleanup_chain. */
126 discard_cleanups (old_chain
)
127 register struct cleanup
*old_chain
;
129 register struct cleanup
*ptr
;
130 while ((ptr
= cleanup_chain
) != old_chain
)
132 cleanup_chain
= ptr
->next
;
137 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
141 struct cleanup
*old_chain
= cleanup_chain
;
147 /* Restore the cleanup chain from a previously saved chain. */
149 restore_cleanups (chain
)
150 struct cleanup
*chain
;
152 cleanup_chain
= chain
;
155 /* This function is useful for cleanups.
159 old_chain = make_cleanup (free_current_contents, &foo);
161 to arrange to free the object thus allocated. */
164 free_current_contents (location
)
170 /* Provide a hook for modules wishing to print their own warning messages
171 to set up the terminal state in a compatible way, without them having
172 to import all the target_<...> macros. */
177 target_terminal_ours ();
178 wrap_here(""); /* Force out any buffered output */
182 /* Print a warning message.
183 The first argument STRING is the warning message, used as a fprintf string,
184 and the remaining args are passed as arguments to it.
185 The primary difference between warnings and errors is that a warning
186 does not force the return to command level. */
197 target_terminal_ours ();
198 wrap_here(""); /* Force out any buffered output */
200 if (warning_pre_print
)
201 fprintf (stderr
, warning_pre_print
);
202 string
= va_arg (args
, char *);
203 vfprintf (stderr
, string
, args
);
204 fprintf (stderr
, "\n");
208 /* Print an error message and return to command level.
209 The first argument STRING is the error message, used as a fprintf string,
210 and the remaining args are passed as arguments to it. */
221 target_terminal_ours ();
222 wrap_here(""); /* Force out any buffered output */
225 fprintf (stderr
, error_pre_print
);
226 string
= va_arg (args
, char *);
227 vfprintf (stderr
, string
, args
);
228 fprintf (stderr
, "\n");
230 return_to_top_level ();
233 /* Print an error message and exit reporting failure.
234 This is for a error that we cannot continue from.
235 The arguments are printed a la printf. */
246 string
= va_arg (args
, char *);
247 fprintf (stderr
, "gdb: ");
248 vfprintf (stderr
, string
, args
);
249 fprintf (stderr
, "\n");
254 /* Print an error message and exit, dumping core.
255 The arguments are printed a la printf (). */
258 fatal_dump_core (va_alist
)
265 string
= va_arg (args
, char *);
266 /* "internal error" is always correct, since GDB should never dump
267 core, no matter what the input. */
268 fprintf (stderr
, "gdb internal error: ");
269 vfprintf (stderr
, string
, args
);
270 fprintf (stderr
, "\n");
273 signal (SIGQUIT
, SIG_DFL
);
274 kill (getpid (), SIGQUIT
);
275 /* We should never get here, but just in case... */
279 /* Memory management stuff (malloc friends). */
281 #if defined (NO_MALLOC_CHECK)
285 #else /* Have mcheck(). */
289 fatal_dump_core ("Memory corruption");
295 mcheck (malloc_botch
);
298 #endif /* Have mcheck(). */
300 /* Like malloc but get error if no storage available. */
312 /* At least one place (dbxread.c:condense_misc_bunches where misc_count == 0)
313 GDB wants to allocate zero bytes. */
317 val
= (char *) malloc (size
);
319 fatal ("virtual memory exhausted.", 0);
323 /* Like realloc but get error if no storage available. */
334 register char *val
= (char *) realloc (ptr
, size
);
336 fatal ("virtual memory exhausted.", 0);
340 /* Print the system error message for errno, and also mention STRING
341 as the file name for which the error was encountered.
342 Then return to command level. */
345 perror_with_name (string
)
349 extern char *sys_errlist
[];
353 if (errno
< sys_nerr
)
354 err
= sys_errlist
[errno
];
356 err
= "unknown error";
358 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
359 strcpy (combined
, string
);
360 strcat (combined
, ": ");
361 strcat (combined
, err
);
363 /* I understand setting these is a matter of taste. Still, some people
364 may clear errno but not know about bfd_error. Doing this here is not
366 bfd_error
= no_error
;
369 error ("%s.", combined
);
372 /* Print the system error message for ERRCODE, and also mention STRING
373 as the file name for which the error was encountered. */
376 print_sys_errmsg (string
, errcode
)
381 extern char *sys_errlist
[];
385 if (errcode
< sys_nerr
)
386 err
= sys_errlist
[errcode
];
388 err
= "unknown error";
390 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
391 strcpy (combined
, string
);
392 strcat (combined
, ": ");
393 strcat (combined
, err
);
395 printf ("%s.\n", combined
);
398 /* Control C eventually causes this to be called, at a convenient time. */
403 target_terminal_ours ();
404 wrap_here ((char *)0); /* Force out any pending output */
406 ioctl (fileno (stdout
), TCFLSH
, 1);
407 #else /* not HAVE_TERMIO */
408 ioctl (fileno (stdout
), TIOCFLUSH
, 0);
409 #endif /* not HAVE_TERMIO */
413 error ("Quit (expect signal %d when inferior is resumed)", SIGINT
);
414 #endif /* TIOCGPGRP */
417 /* Control C comes here */
425 /* Restore the signal handler. */
426 signal (SIGINT
, request_quit
);
433 /* My replacement for the read system call.
434 Used like `read' but keeps going if `read' returns too soon. */
437 myread (desc
, addr
, len
)
447 val
= read (desc
, addr
, len
);
458 /* Make a copy of the string at PTR with SIZE characters
459 (and add a null character at the end in the copy).
460 Uses malloc to get the space. Returns the address of the copy. */
463 savestring (ptr
, size
)
467 register char *p
= (char *) xmalloc (size
+ 1);
468 bcopy (ptr
, p
, size
);
473 /* The "const" is so it compiles under DGUX (which prototypes strsave
474 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
475 Doesn't real strsave return NULL if out of memory? */
480 return savestring (ptr
, strlen (ptr
));
484 print_spaces (n
, file
)
492 /* Ask user a y-or-n question and return 1 iff answer is yes.
493 Takes three args which are given to printf to print the question.
494 The first, a control string, should end in "? ".
495 It should not say how to answer, because we do that. */
507 /* Automatically answer "yes" if input is not from a terminal. */
508 if (!input_from_terminal_p ())
514 ctlstr
= va_arg (args
, char *);
515 vfprintf (stdout
, ctlstr
, args
);
517 printf ("(y or n) ");
519 answer
= fgetc (stdin
);
520 clearerr (stdin
); /* in case of C-d */
521 if (answer
== EOF
) /* C-d */
523 if (answer
!= '\n') /* Eat rest of input line, to EOF or newline */
526 ans2
= fgetc (stdin
);
529 while (ans2
!= EOF
&& ans2
!= '\n');
536 printf ("Please answer y or n.\n");
540 /* Parse a C escape sequence. STRING_PTR points to a variable
541 containing a pointer to the string to parse. That pointer
542 should point to the character after the \. That pointer
543 is updated past the characters we use. The value of the
544 escape sequence is returned.
546 A negative value means the sequence \ newline was seen,
547 which is supposed to be equivalent to nothing at all.
549 If \ is followed by a null character, we return a negative
550 value and leave the string pointer pointing at the null character.
552 If \ is followed by 000, we return 0 and leave the string pointer
553 after the zeros. A value of 0 does not mean end of string. */
556 parse_escape (string_ptr
)
559 register int c
= *(*string_ptr
)++;
563 return 007; /* Bell (alert) char */
566 case 'e': /* Escape character */
584 c
= *(*string_ptr
)++;
586 c
= parse_escape (string_ptr
);
589 return (c
& 0200) | (c
& 037);
600 register int i
= c
- '0';
601 register int count
= 0;
604 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
622 /* Print the character CH on STREAM as part of the contents
623 of a literal string whose delimiter is QUOTER. */
626 printchar (ch
, stream
, quoter
)
633 if (c
< 040 || (sevenbit_strings
&& c
>= 0177)) {
637 fputs_filtered ("\\n", stream
);
640 fputs_filtered ("\\b", stream
);
643 fputs_filtered ("\\t", stream
);
646 fputs_filtered ("\\f", stream
);
649 fputs_filtered ("\\r", stream
);
652 fputs_filtered ("\\e", stream
);
655 fputs_filtered ("\\a", stream
);
658 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
662 if (c
== '\\' || c
== quoter
)
663 fputs_filtered ("\\", stream
);
664 fprintf_filtered (stream
, "%c", c
);
668 /* Number of lines per page or UINT_MAX if paging is disabled. */
669 static unsigned int lines_per_page
;
670 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
671 static unsigned int chars_per_line
;
672 /* Current count of lines printed on this page, chars on this line. */
673 static unsigned int lines_printed
, chars_printed
;
675 /* Buffer and start column of buffered text, for doing smarter word-
676 wrapping. When someone calls wrap_here(), we start buffering output
677 that comes through fputs_filtered(). If we see a newline, we just
678 spit it out and forget about the wrap_here(). If we see another
679 wrap_here(), we spit it out and remember the newer one. If we see
680 the end of the line, we spit out a newline, the indent, and then
683 wrap_column is the column number on the screen where wrap_buffer begins.
684 When wrap_column is zero, wrapping is not in effect.
685 wrap_buffer is malloc'd with chars_per_line+2 bytes.
686 When wrap_buffer[0] is null, the buffer is empty.
687 wrap_pointer points into it at the next character to fill.
688 wrap_indent is the string that should be used as indentation if the
691 static char *wrap_buffer
, *wrap_pointer
, *wrap_indent
;
692 static int wrap_column
;
696 set_width_command (args
, from_tty
, c
)
699 struct cmd_list_element
*c
;
703 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
704 wrap_buffer
[0] = '\0';
707 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
708 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
712 prompt_for_continue ()
717 ignore
= gdb_readline ("---Type <return> to continue---");
720 chars_printed
= lines_printed
= 0;
722 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
725 /* Reinitialize filter; ie. tell it to reset to original values. */
728 reinitialize_more_filter ()
734 /* Indicate that if the next sequence of characters overflows the line,
735 a newline should be inserted here rather than when it hits the end.
736 If INDENT is nonzero, it is a string to be printed to indent the
737 wrapped part on the next line. INDENT must remain accessible until
738 the next call to wrap_here() or until a newline is printed through
741 If the line is already overfull, we immediately print a newline and
742 the indentation, and disable further wrapping.
744 If we don't know the width of lines, but we know the page height,
745 we must not wrap words, but should still keep track of newlines
746 that were explicitly printed.
748 INDENT should not contain tabs, as that
749 will mess up the char count on the next line. FIXME. */
757 *wrap_pointer
= '\0';
758 fputs (wrap_buffer
, stdout
);
760 wrap_pointer
= wrap_buffer
;
761 wrap_buffer
[0] = '\0';
762 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
766 else if (chars_printed
>= chars_per_line
)
768 puts_filtered ("\n");
769 puts_filtered (indent
);
774 wrap_column
= chars_printed
;
775 wrap_indent
= indent
;
779 /* Like fputs but pause after every screenful, and can wrap at points
780 other than the final character of a line.
781 Unlike fputs, fputs_filtered does not return a value.
782 It is OK for LINEBUFFER to be NULL, in which case just don't print
785 Note that a longjmp to top level may occur in this routine
786 (since prompt_for_continue may do so) so this routine should not be
787 called when cleanups are not in place. */
790 fputs_filtered (linebuffer
, stream
)
799 /* Don't do any filtering if it is disabled. */
801 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
803 fputs (linebuffer
, stream
);
807 /* Go through and output each character. Show line extension
808 when this is necessary; prompt user for new page when this is
811 lineptr
= linebuffer
;
814 /* Possible new page. */
815 if (lines_printed
>= lines_per_page
- 1)
816 prompt_for_continue ();
818 while (*lineptr
&& *lineptr
!= '\n')
820 /* Print a single line. */
821 if (*lineptr
== '\t')
824 *wrap_pointer
++ = '\t';
827 /* Shifting right by 3 produces the number of tab stops
828 we have already passed, and then adding one and
829 shifting left 3 advances to the next tab stop. */
830 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
836 *wrap_pointer
++ = *lineptr
;
838 putc (*lineptr
, stream
);
843 if (chars_printed
>= chars_per_line
)
845 unsigned int save_chars
= chars_printed
;
849 /* If we aren't actually wrapping, don't output newline --
850 if chars_per_line is right, we probably just overflowed
851 anyway; if it's wrong, let us keep going. */
855 /* Possible new page. */
856 if (lines_printed
>= lines_per_page
- 1)
857 prompt_for_continue ();
859 /* Now output indentation and wrapped string */
863 fputs (wrap_indent
, stream
);
864 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
865 fputs (wrap_buffer
, stream
); /* and eject it */
866 /* FIXME, this strlen is what prevents wrap_indent from
867 containing tabs. However, if we recurse to print it
868 and count its chars, we risk trouble if wrap_indent is
869 longer than (the user settable) chars_per_line.
870 Note also that this can set chars_printed > chars_per_line
871 if we are printing a long string. */
872 chars_printed
= strlen (wrap_indent
)
873 + (save_chars
- wrap_column
);
874 wrap_pointer
= wrap_buffer
; /* Reset buffer */
875 wrap_buffer
[0] = '\0';
876 wrap_column
= 0; /* And disable fancy wrap */
881 if (*lineptr
== '\n')
884 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
893 /* fputs_demangled is a variant of fputs_filtered that
894 demangles g++ names.*/
897 fputs_demangled (linebuffer
, stream
, arg_mode
)
903 extern char *cplus_demangle (const char *, int);
905 extern char *cplus_demangle ();
907 #define SYMBOL_MAX 1024
909 #define SYMBOL_CHAR(c) (isascii(c) \
910 && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
912 char buf
[SYMBOL_MAX
+1];
913 # define SLOP 5 /* How much room to leave in buf */
916 if (linebuffer
== NULL
)
919 /* If user wants to see raw output, no problem. */
921 fputs_filtered (linebuffer
, stream
);
927 while ( *p
!= (char) 0 ) {
930 /* collect non-interesting characters into buf */
931 while ( *p
!= (char) 0 && !SYMBOL_CHAR(*p
) && i
< (int)sizeof(buf
)-SLOP
) {
936 /* output the non-interesting characters without demangling */
938 fputs_filtered(buf
, stream
);
939 i
= 0; /* reset buf */
942 /* and now the interesting characters */
943 while (i
< SYMBOL_MAX
946 && i
< (int)sizeof(buf
) - SLOP
) {
954 if ( (result
= cplus_demangle(buf
, arg_mode
)) != NULL
) {
955 fputs_filtered(result
, stream
);
959 fputs_filtered(buf
, stream
);
965 /* Print a variable number of ARGS using format FORMAT. If this
966 information is going to put the amount written (since the last call
967 to INITIALIZE_MORE_FILTER or the last page break) over the page size,
968 print out a pause message and do a gdb_readline to get the users
969 permision to continue.
971 Unlike fprintf, this function does not return a value.
973 We implement three variants, vfprintf (takes a vararg list and stream),
974 fprintf (takes a stream to write on), and printf (the usual).
976 Note that this routine has a restriction that the length of the
977 final output line must be less than 255 characters *or* it must be
978 less than twice the size of the format string. This is a very
979 arbitrary restriction, but it is an internal restriction, so I'll
980 put it in. This means that the %s format specifier is almost
981 useless; unless the caller can GUARANTEE that the string is short
982 enough, fputs_filtered should be used instead.
984 Note also that a longjmp to top level may occur in this routine
985 (since prompt_for_continue may do so) so this routine should not be
986 called when cleanups are not in place. */
990 vfprintf_filtered (stream
, format
, args
)
995 static char *linebuffer
= (char *) 0;
996 static int line_size
;
999 format_length
= strlen (format
);
1001 /* Allocated linebuffer for the first time. */
1004 linebuffer
= (char *) xmalloc (255);
1008 /* Reallocate buffer to a larger size if this is necessary. */
1009 if (format_length
* 2 > line_size
)
1011 line_size
= format_length
* 2;
1013 /* You don't have to copy. */
1015 linebuffer
= (char *) xmalloc (line_size
);
1019 /* This won't blow up if the restrictions described above are
1021 (void) vsprintf (linebuffer
, format
, args
);
1023 fputs_filtered (linebuffer
, stream
);
1028 fprintf_filtered (va_alist
)
1036 stream
= va_arg (args
, FILE *);
1037 format
= va_arg (args
, char *);
1039 /* This won't blow up if the restrictions described above are
1041 (void) vfprintf_filtered (stream
, format
, args
);
1047 printf_filtered (va_alist
)
1054 format
= va_arg (args
, char *);
1056 (void) vfprintf_filtered (stdout
, format
, args
);
1063 puts_filtered (string
)
1066 fputs_filtered (string
, stdout
);
1069 /* Return a pointer to N spaces and a null. The pointer is good
1070 until the next call to here. */
1076 static char *spaces
;
1077 static int max_spaces
;
1083 spaces
= malloc (n
+1);
1084 for (t
= spaces
+n
; t
!= spaces
;)
1090 return spaces
+ max_spaces
- n
;
1093 /* Print N spaces. */
1095 print_spaces_filtered (n
, stream
)
1099 fputs_filtered (n_spaces (n
), stream
);
1102 /* C++ demangler stuff. */
1103 char *cplus_demangle ();
1105 /* Print NAME on STREAM, demangling if necessary. */
1107 fprint_symbol (stream
, name
)
1112 if ((!demangle
) || NULL
== (demangled
= cplus_demangle (name
, 1)))
1113 fputs_filtered (name
, stream
);
1116 fputs_filtered (demangled
, stream
);
1122 _initialize_utils ()
1124 struct cmd_list_element
*c
;
1126 c
= add_set_cmd ("width", class_support
, var_uinteger
,
1127 (char *)&chars_per_line
,
1128 "Set number of characters gdb thinks are in a line.",
1130 add_show_from_set (c
, &showlist
);
1131 c
->function
= set_width_command
;
1134 (add_set_cmd ("height", class_support
,
1135 var_uinteger
, (char *)&lines_per_page
,
1136 "Set number of lines gdb thinks are in a page.", &setlist
),
1139 /* These defaults will be used if we are unable to get the correct
1140 values from termcap. */
1141 lines_per_page
= 24;
1142 chars_per_line
= 80;
1143 /* Initialize the screen height and width from termcap. */
1145 char *termtype
= getenv ("TERM");
1147 /* Positive means success, nonpositive means failure. */
1150 /* 2048 is large enough for all known terminals, according to the
1151 GNU termcap manual. */
1152 char term_buffer
[2048];
1156 status
= tgetent (term_buffer
, termtype
);
1161 val
= tgetnum ("li");
1163 lines_per_page
= val
;
1165 /* The number of lines per page is not mentioned
1166 in the terminal description. This probably means
1167 that paging is not useful (e.g. emacs shell window),
1168 so disable paging. */
1169 lines_per_page
= UINT_MAX
;
1171 val
= tgetnum ("co");
1173 chars_per_line
= val
;
1178 /* If the output is not a terminal, don't paginate it. */
1179 if (!ISATTY (stdout
))
1180 lines_per_page
= UINT_MAX
;
1182 set_width_command ((char *)NULL
, 0, c
);
1185 (add_set_cmd ("demangle", class_support
, var_boolean
,
1187 "Set demangling of encoded C++ names when displaying symbols.",
1192 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
1193 (char *)&sevenbit_strings
,
1194 "Set printing of 8-bit characters in strings as \\nnn.",
1199 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
1200 (char *)&asm_demangle
,
1201 "Set demangling of C++ names in disassembly listings.",