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)))
51 /* Chain of cleanup actions established with make_cleanup,
52 to be executed if an error happens. */
54 static struct cleanup
*cleanup_chain
;
56 /* Nonzero means a quit has been requested. */
60 /* Nonzero means quit immediately if Control-C is typed now,
61 rather than waiting until QUIT is executed. */
65 /* Nonzero means that encoded C++ names should be printed out in their
66 C++ form rather than raw. */
70 /* Nonzero means that encoded C++ names should be printed out in their
71 C++ form even in assembler language displays. If this is set, but
72 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
76 /* Nonzero means that strings with character values >0x7F should be printed
77 as octal escapes. Zero means just print the value (e.g. it's an
78 international character, and the terminal or window can cope.) */
80 int sevenbit_strings
= 0;
82 /* String to be printed before error messages, if any. */
84 char *error_pre_print
;
85 char *warning_pre_print
;
87 /* Add a new cleanup to the cleanup_chain,
88 and return the previous chain pointer
89 to be passed later to do_cleanups or discard_cleanups.
90 Args are FUNCTION to clean up with, and ARG to pass to it. */
93 make_cleanup (function
, arg
)
97 register struct cleanup
*new
98 = (struct cleanup
*) xmalloc (sizeof (struct cleanup
));
99 register struct cleanup
*old_chain
= cleanup_chain
;
101 new->next
= cleanup_chain
;
102 new->function
= function
;
109 /* Discard cleanups and do the actions they describe
110 until we get back to the point OLD_CHAIN in the cleanup_chain. */
113 do_cleanups (old_chain
)
114 register struct cleanup
*old_chain
;
116 register struct cleanup
*ptr
;
117 while ((ptr
= cleanup_chain
) != old_chain
)
119 cleanup_chain
= ptr
->next
; /* Do this first incase recursion */
120 (*ptr
->function
) (ptr
->arg
);
125 /* Discard cleanups, not doing the actions they describe,
126 until we get back to the point OLD_CHAIN in the cleanup_chain. */
129 discard_cleanups (old_chain
)
130 register struct cleanup
*old_chain
;
132 register struct cleanup
*ptr
;
133 while ((ptr
= cleanup_chain
) != old_chain
)
135 cleanup_chain
= ptr
->next
;
140 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
144 struct cleanup
*old_chain
= cleanup_chain
;
150 /* Restore the cleanup chain from a previously saved chain. */
152 restore_cleanups (chain
)
153 struct cleanup
*chain
;
155 cleanup_chain
= chain
;
158 /* This function is useful for cleanups.
162 old_chain = make_cleanup (free_current_contents, &foo);
164 to arrange to free the object thus allocated. */
167 free_current_contents (location
)
173 /* Provide a hook for modules wishing to print their own warning messages
174 to set up the terminal state in a compatible way, without them having
175 to import all the target_<...> macros. */
180 target_terminal_ours ();
181 wrap_here(""); /* Force out any buffered output */
185 /* Print a warning message.
186 The first argument STRING is the warning message, used as a fprintf string,
187 and the remaining args are passed as arguments to it.
188 The primary difference between warnings and errors is that a warning
189 does not force the return to command level. */
200 target_terminal_ours ();
201 wrap_here(""); /* Force out any buffered output */
203 if (warning_pre_print
)
204 fprintf (stderr
, warning_pre_print
);
205 string
= va_arg (args
, char *);
206 vfprintf (stderr
, string
, args
);
207 fprintf (stderr
, "\n");
211 /* Print an error message and return to command level.
212 The first argument STRING is the error message, used as a fprintf string,
213 and the remaining args are passed as arguments to it. */
224 target_terminal_ours ();
225 wrap_here(""); /* Force out any buffered output */
228 fprintf (stderr
, error_pre_print
);
229 string
= va_arg (args
, char *);
230 vfprintf (stderr
, string
, args
);
231 fprintf (stderr
, "\n");
233 return_to_top_level ();
236 /* Print an error message and exit reporting failure.
237 This is for a error that we cannot continue from.
238 The arguments are printed a la printf. */
249 string
= va_arg (args
, char *);
250 fprintf (stderr
, "gdb: ");
251 vfprintf (stderr
, string
, args
);
252 fprintf (stderr
, "\n");
257 /* Print an error message and exit, dumping core.
258 The arguments are printed a la printf (). */
261 fatal_dump_core (va_alist
)
268 string
= va_arg (args
, char *);
269 /* "internal error" is always correct, since GDB should never dump
270 core, no matter what the input. */
271 fprintf (stderr
, "gdb internal error: ");
272 vfprintf (stderr
, string
, args
);
273 fprintf (stderr
, "\n");
276 signal (SIGQUIT
, SIG_DFL
);
277 kill (getpid (), SIGQUIT
);
278 /* We should never get here, but just in case... */
282 /* Memory management stuff (malloc friends). */
284 #if defined (NO_MALLOC_CHECK)
288 #else /* Have mcheck(). */
292 fatal_dump_core ("Memory corruption");
298 mcheck (malloc_botch
);
301 #endif /* Have mcheck(). */
303 /* Like malloc but get error if no storage available. */
315 /* At least one place (dbxread.c:condense_misc_bunches where misc_count == 0)
316 GDB wants to allocate zero bytes. */
320 val
= (char *) malloc (size
);
322 fatal ("virtual memory exhausted.", 0);
326 /* Like realloc but get error if no storage available. */
337 register char *val
= (char *) realloc (ptr
, size
);
339 fatal ("virtual memory exhausted.", 0);
343 /* Print the system error message for errno, and also mention STRING
344 as the file name for which the error was encountered.
345 Then return to command level. */
348 perror_with_name (string
)
352 extern char *sys_errlist
[];
356 if (errno
< sys_nerr
)
357 err
= sys_errlist
[errno
];
359 err
= "unknown error";
361 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
362 strcpy (combined
, string
);
363 strcat (combined
, ": ");
364 strcat (combined
, err
);
366 /* I understand setting these is a matter of taste. Still, some people
367 may clear errno but not know about bfd_error. Doing this here is not
369 bfd_error
= no_error
;
372 error ("%s.", combined
);
375 /* Print the system error message for ERRCODE, and also mention STRING
376 as the file name for which the error was encountered. */
379 print_sys_errmsg (string
, errcode
)
384 extern char *sys_errlist
[];
388 if (errcode
< sys_nerr
)
389 err
= sys_errlist
[errcode
];
391 err
= "unknown error";
393 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
394 strcpy (combined
, string
);
395 strcat (combined
, ": ");
396 strcat (combined
, err
);
398 printf ("%s.\n", combined
);
401 /* Control C eventually causes this to be called, at a convenient time. */
406 target_terminal_ours ();
407 wrap_here ((char *)0); /* Force out any pending output */
409 ioctl (fileno (stdout
), TCFLSH
, 1);
410 #else /* not HAVE_TERMIO */
411 ioctl (fileno (stdout
), TIOCFLUSH
, 0);
412 #endif /* not HAVE_TERMIO */
416 error ("Quit (expect signal %d when inferior is resumed)", SIGINT
);
417 #endif /* TIOCGPGRP */
420 /* Control C comes here */
428 /* Restore the signal handler. */
429 signal (SIGINT
, request_quit
);
436 /* My replacement for the read system call.
437 Used like `read' but keeps going if `read' returns too soon. */
440 myread (desc
, addr
, len
)
450 val
= read (desc
, addr
, len
);
461 /* Make a copy of the string at PTR with SIZE characters
462 (and add a null character at the end in the copy).
463 Uses malloc to get the space. Returns the address of the copy. */
466 savestring (ptr
, size
)
470 register char *p
= (char *) xmalloc (size
+ 1);
471 bcopy (ptr
, p
, size
);
476 /* The "const" is so it compiles under DGUX (which prototypes strsave
477 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
478 Doesn't real strsave return NULL if out of memory? */
483 return savestring (ptr
, strlen (ptr
));
487 print_spaces (n
, file
)
495 /* Ask user a y-or-n question and return 1 iff answer is yes.
496 Takes three args which are given to printf to print the question.
497 The first, a control string, should end in "? ".
498 It should not say how to answer, because we do that. */
510 /* Automatically answer "yes" if input is not from a terminal. */
511 if (!input_from_terminal_p ())
517 ctlstr
= va_arg (args
, char *);
518 vfprintf (stdout
, ctlstr
, args
);
520 printf ("(y or n) ");
522 answer
= fgetc (stdin
);
523 clearerr (stdin
); /* in case of C-d */
524 if (answer
== EOF
) /* C-d */
526 if (answer
!= '\n') /* Eat rest of input line, to EOF or newline */
529 ans2
= fgetc (stdin
);
532 while (ans2
!= EOF
&& ans2
!= '\n');
539 printf ("Please answer y or n.\n");
543 /* Parse a C escape sequence. STRING_PTR points to a variable
544 containing a pointer to the string to parse. That pointer
545 should point to the character after the \. That pointer
546 is updated past the characters we use. The value of the
547 escape sequence is returned.
549 A negative value means the sequence \ newline was seen,
550 which is supposed to be equivalent to nothing at all.
552 If \ is followed by a null character, we return a negative
553 value and leave the string pointer pointing at the null character.
555 If \ is followed by 000, we return 0 and leave the string pointer
556 after the zeros. A value of 0 does not mean end of string. */
559 parse_escape (string_ptr
)
562 register int c
= *(*string_ptr
)++;
566 return 007; /* Bell (alert) char */
569 case 'e': /* Escape character */
587 c
= *(*string_ptr
)++;
589 c
= parse_escape (string_ptr
);
592 return (c
& 0200) | (c
& 037);
603 register int i
= c
- '0';
604 register int count
= 0;
607 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
625 /* Print the character CH on STREAM as part of the contents
626 of a literal string whose delimiter is QUOTER. */
629 printchar (ch
, stream
, quoter
)
636 if (c
< 040 || (sevenbit_strings
&& c
>= 0177)) {
640 fputs_filtered ("\\n", stream
);
643 fputs_filtered ("\\b", stream
);
646 fputs_filtered ("\\t", stream
);
649 fputs_filtered ("\\f", stream
);
652 fputs_filtered ("\\r", stream
);
655 fputs_filtered ("\\e", stream
);
658 fputs_filtered ("\\a", stream
);
661 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
665 if (c
== '\\' || c
== quoter
)
666 fputs_filtered ("\\", stream
);
667 fprintf_filtered (stream
, "%c", c
);
671 /* Number of lines per page or UINT_MAX if paging is disabled. */
672 static unsigned int lines_per_page
;
673 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
674 static unsigned int chars_per_line
;
675 /* Current count of lines printed on this page, chars on this line. */
676 static unsigned int lines_printed
, chars_printed
;
678 /* Buffer and start column of buffered text, for doing smarter word-
679 wrapping. When someone calls wrap_here(), we start buffering output
680 that comes through fputs_filtered(). If we see a newline, we just
681 spit it out and forget about the wrap_here(). If we see another
682 wrap_here(), we spit it out and remember the newer one. If we see
683 the end of the line, we spit out a newline, the indent, and then
686 wrap_column is the column number on the screen where wrap_buffer begins.
687 When wrap_column is zero, wrapping is not in effect.
688 wrap_buffer is malloc'd with chars_per_line+2 bytes.
689 When wrap_buffer[0] is null, the buffer is empty.
690 wrap_pointer points into it at the next character to fill.
691 wrap_indent is the string that should be used as indentation if the
694 static char *wrap_buffer
, *wrap_pointer
, *wrap_indent
;
695 static int wrap_column
;
699 set_width_command (args
, from_tty
, c
)
702 struct cmd_list_element
*c
;
706 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
707 wrap_buffer
[0] = '\0';
710 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
711 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
715 prompt_for_continue ()
720 ignore
= gdb_readline ("---Type <return> to continue---");
723 chars_printed
= lines_printed
= 0;
725 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
728 /* Reinitialize filter; ie. tell it to reset to original values. */
731 reinitialize_more_filter ()
737 /* Indicate that if the next sequence of characters overflows the line,
738 a newline should be inserted here rather than when it hits the end.
739 If INDENT is nonzero, it is a string to be printed to indent the
740 wrapped part on the next line. INDENT must remain accessible until
741 the next call to wrap_here() or until a newline is printed through
744 If the line is already overfull, we immediately print a newline and
745 the indentation, and disable further wrapping.
747 If we don't know the width of lines, but we know the page height,
748 we must not wrap words, but should still keep track of newlines
749 that were explicitly printed.
751 INDENT should not contain tabs, as that
752 will mess up the char count on the next line. FIXME. */
760 *wrap_pointer
= '\0';
761 fputs (wrap_buffer
, stdout
);
763 wrap_pointer
= wrap_buffer
;
764 wrap_buffer
[0] = '\0';
765 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
769 else if (chars_printed
>= chars_per_line
)
771 puts_filtered ("\n");
772 puts_filtered (indent
);
777 wrap_column
= chars_printed
;
778 wrap_indent
= indent
;
782 /* Like fputs but pause after every screenful, and can wrap at points
783 other than the final character of a line.
784 Unlike fputs, fputs_filtered does not return a value.
785 It is OK for LINEBUFFER to be NULL, in which case just don't print
788 Note that a longjmp to top level may occur in this routine
789 (since prompt_for_continue may do so) so this routine should not be
790 called when cleanups are not in place. */
793 fputs_filtered (linebuffer
, stream
)
802 /* Don't do any filtering if it is disabled. */
804 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
806 fputs (linebuffer
, stream
);
810 /* Go through and output each character. Show line extension
811 when this is necessary; prompt user for new page when this is
814 lineptr
= linebuffer
;
817 /* Possible new page. */
818 if (lines_printed
>= lines_per_page
- 1)
819 prompt_for_continue ();
821 while (*lineptr
&& *lineptr
!= '\n')
823 /* Print a single line. */
824 if (*lineptr
== '\t')
827 *wrap_pointer
++ = '\t';
830 /* Shifting right by 3 produces the number of tab stops
831 we have already passed, and then adding one and
832 shifting left 3 advances to the next tab stop. */
833 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
839 *wrap_pointer
++ = *lineptr
;
841 putc (*lineptr
, stream
);
846 if (chars_printed
>= chars_per_line
)
848 unsigned int save_chars
= chars_printed
;
852 /* If we aren't actually wrapping, don't output newline --
853 if chars_per_line is right, we probably just overflowed
854 anyway; if it's wrong, let us keep going. */
858 /* Possible new page. */
859 if (lines_printed
>= lines_per_page
- 1)
860 prompt_for_continue ();
862 /* Now output indentation and wrapped string */
866 fputs (wrap_indent
, stream
);
867 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
868 fputs (wrap_buffer
, stream
); /* and eject it */
869 /* FIXME, this strlen is what prevents wrap_indent from
870 containing tabs. However, if we recurse to print it
871 and count its chars, we risk trouble if wrap_indent is
872 longer than (the user settable) chars_per_line.
873 Note also that this can set chars_printed > chars_per_line
874 if we are printing a long string. */
875 chars_printed
= strlen (wrap_indent
)
876 + (save_chars
- wrap_column
);
877 wrap_pointer
= wrap_buffer
; /* Reset buffer */
878 wrap_buffer
[0] = '\0';
879 wrap_column
= 0; /* And disable fancy wrap */
884 if (*lineptr
== '\n')
887 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
896 /* fputs_demangled is a variant of fputs_filtered that
897 demangles g++ names.*/
900 fputs_demangled (linebuffer
, stream
, arg_mode
)
906 extern char *cplus_demangle (const char *, int);
908 extern char *cplus_demangle ();
910 #define SYMBOL_MAX 1024
912 #define SYMBOL_CHAR(c) (isascii(c) \
913 && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
915 char buf
[SYMBOL_MAX
+1];
916 # define SLOP 5 /* How much room to leave in buf */
919 if (linebuffer
== NULL
)
922 /* If user wants to see raw output, no problem. */
924 fputs_filtered (linebuffer
, stream
);
930 while ( *p
!= (char) 0 ) {
933 /* collect non-interesting characters into buf */
934 while ( *p
!= (char) 0 && !SYMBOL_CHAR(*p
) && i
< (int)sizeof(buf
)-SLOP
) {
939 /* output the non-interesting characters without demangling */
941 fputs_filtered(buf
, stream
);
942 i
= 0; /* reset buf */
945 /* and now the interesting characters */
946 while (i
< SYMBOL_MAX
949 && i
< (int)sizeof(buf
) - SLOP
) {
957 if ( (result
= cplus_demangle(buf
, arg_mode
)) != NULL
) {
958 fputs_filtered(result
, stream
);
962 fputs_filtered(buf
, stream
);
968 /* Print a variable number of ARGS using format FORMAT. If this
969 information is going to put the amount written (since the last call
970 to INITIALIZE_MORE_FILTER or the last page break) over the page size,
971 print out a pause message and do a gdb_readline to get the users
972 permision to continue.
974 Unlike fprintf, this function does not return a value.
976 We implement three variants, vfprintf (takes a vararg list and stream),
977 fprintf (takes a stream to write on), and printf (the usual).
979 Note that this routine has a restriction that the length of the
980 final output line must be less than 255 characters *or* it must be
981 less than twice the size of the format string. This is a very
982 arbitrary restriction, but it is an internal restriction, so I'll
983 put it in. This means that the %s format specifier is almost
984 useless; unless the caller can GUARANTEE that the string is short
985 enough, fputs_filtered should be used instead.
987 Note also that a longjmp to top level may occur in this routine
988 (since prompt_for_continue may do so) so this routine should not be
989 called when cleanups are not in place. */
993 vfprintf_filtered (stream
, format
, args
)
998 static char *linebuffer
= (char *) 0;
999 static int line_size
;
1002 format_length
= strlen (format
);
1004 /* Allocated linebuffer for the first time. */
1007 linebuffer
= (char *) xmalloc (255);
1011 /* Reallocate buffer to a larger size if this is necessary. */
1012 if (format_length
* 2 > line_size
)
1014 line_size
= format_length
* 2;
1016 /* You don't have to copy. */
1018 linebuffer
= (char *) xmalloc (line_size
);
1022 /* This won't blow up if the restrictions described above are
1024 (void) vsprintf (linebuffer
, format
, args
);
1026 fputs_filtered (linebuffer
, stream
);
1031 fprintf_filtered (va_alist
)
1039 stream
= va_arg (args
, FILE *);
1040 format
= va_arg (args
, char *);
1042 /* This won't blow up if the restrictions described above are
1044 (void) vfprintf_filtered (stream
, format
, args
);
1050 printf_filtered (va_alist
)
1057 format
= va_arg (args
, char *);
1059 (void) vfprintf_filtered (stdout
, format
, args
);
1066 puts_filtered (string
)
1069 fputs_filtered (string
, stdout
);
1072 /* Return a pointer to N spaces and a null. The pointer is good
1073 until the next call to here. */
1079 static char *spaces
;
1080 static int max_spaces
;
1086 spaces
= malloc (n
+1);
1087 for (t
= spaces
+n
; t
!= spaces
;)
1093 return spaces
+ max_spaces
- n
;
1096 /* Print N spaces. */
1098 print_spaces_filtered (n
, stream
)
1102 fputs_filtered (n_spaces (n
), stream
);
1105 /* C++ demangler stuff. */
1106 char *cplus_demangle ();
1108 /* Print NAME on STREAM, demangling if necessary. */
1110 fprint_symbol (stream
, name
)
1115 if ((!demangle
) || NULL
== (demangled
= cplus_demangle (name
, 1)))
1116 fputs_filtered (name
, stream
);
1119 fputs_filtered (demangled
, stream
);
1125 _initialize_utils ()
1127 struct cmd_list_element
*c
;
1129 c
= add_set_cmd ("width", class_support
, var_uinteger
,
1130 (char *)&chars_per_line
,
1131 "Set number of characters gdb thinks are in a line.",
1133 add_show_from_set (c
, &showlist
);
1134 c
->function
= set_width_command
;
1137 (add_set_cmd ("height", class_support
,
1138 var_uinteger
, (char *)&lines_per_page
,
1139 "Set number of lines gdb thinks are in a page.", &setlist
),
1142 /* These defaults will be used if we are unable to get the correct
1143 values from termcap. */
1144 lines_per_page
= 24;
1145 chars_per_line
= 80;
1146 /* Initialize the screen height and width from termcap. */
1148 char *termtype
= getenv ("TERM");
1150 /* Positive means success, nonpositive means failure. */
1153 /* 2048 is large enough for all known terminals, according to the
1154 GNU termcap manual. */
1155 char term_buffer
[2048];
1159 status
= tgetent (term_buffer
, termtype
);
1164 val
= tgetnum ("li");
1166 lines_per_page
= val
;
1168 /* The number of lines per page is not mentioned
1169 in the terminal description. This probably means
1170 that paging is not useful (e.g. emacs shell window),
1171 so disable paging. */
1172 lines_per_page
= UINT_MAX
;
1174 val
= tgetnum ("co");
1176 chars_per_line
= val
;
1181 /* If the output is not a terminal, don't paginate it. */
1182 if (!ISATTY (stdout
))
1183 lines_per_page
= UINT_MAX
;
1185 set_width_command ((char *)NULL
, 0, c
);
1188 (add_set_cmd ("demangle", class_support
, var_boolean
,
1190 "Set demangling of encoded C++ names when displaying symbols.",
1195 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
1196 (char *)&sevenbit_strings
,
1197 "Set printing of 8-bit characters in strings as \\nnn.",
1202 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
1203 (char *)&asm_demangle
,
1204 "Set demangling of C++ names in disassembly listings.",
This page took 0.076509 seconds and 5 git commands to generate.