1 /* General utility routines for GDB, the GNU debugger.
2 Copyright (C) 1986, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
31 /* If this definition isn't overridden by the header files, assume
32 that isatty and fileno exist on this system. */
34 #define ISATTY(FP) (isatty (fileno (FP)))
40 /* Chain of cleanup actions established with make_cleanup,
41 to be executed if an error happens. */
43 static struct cleanup
*cleanup_chain
;
45 /* Nonzero means a quit has been requested. */
49 /* Nonzero means quit immediately if Control-C is typed now,
50 rather than waiting until QUIT is executed. */
54 /* Add a new cleanup to the cleanup_chain,
55 and return the previous chain pointer
56 to be passed later to do_cleanups or discard_cleanups.
57 Args are FUNCTION to clean up with, and ARG to pass to it. */
60 make_cleanup (function
, arg
)
64 register struct cleanup
*new
65 = (struct cleanup
*) xmalloc (sizeof (struct cleanup
));
66 register struct cleanup
*old_chain
= cleanup_chain
;
68 new->next
= cleanup_chain
;
69 new->function
= function
;
76 /* Discard cleanups and do the actions they describe
77 until we get back to the point OLD_CHAIN in the cleanup_chain. */
80 do_cleanups (old_chain
)
81 register struct cleanup
*old_chain
;
83 register struct cleanup
*ptr
;
84 while ((ptr
= cleanup_chain
) != old_chain
)
86 (*ptr
->function
) (ptr
->arg
);
87 cleanup_chain
= ptr
->next
;
92 /* Discard cleanups, not doing the actions they describe,
93 until we get back to the point OLD_CHAIN in the cleanup_chain. */
96 discard_cleanups (old_chain
)
97 register struct cleanup
*old_chain
;
99 register struct cleanup
*ptr
;
100 while ((ptr
= cleanup_chain
) != old_chain
)
102 cleanup_chain
= ptr
->next
;
107 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
111 struct cleanup
*old_chain
= cleanup_chain
;
117 /* Restore the cleanup chain from a previously saved chain. */
119 restore_cleanups (chain
)
120 struct cleanup
*chain
;
122 cleanup_chain
= chain
;
125 /* This function is useful for cleanups.
129 old_chain = make_cleanup (free_current_contents, &foo);
131 to arrange to free the object thus allocated. */
134 free_current_contents (location
)
140 /* Generally useful subroutines used throughout the program. */
142 /* Like malloc but get error if no storage available. */
148 register char *val
= (char *) malloc (size
);
150 fatal ("virtual memory exhausted.", 0);
154 /* Like realloc but get error if no storage available. */
161 register char *val
= (char *) realloc (ptr
, size
);
163 fatal ("virtual memory exhausted.", 0);
167 /* Print the system error message for errno, and also mention STRING
168 as the file name for which the error was encountered.
169 Then return to command level. */
172 perror_with_name (string
)
176 extern char *sys_errlist
[];
181 if (errno
< sys_nerr
)
182 err
= sys_errlist
[errno
];
184 err
= "unknown error";
186 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
187 strcpy (combined
, string
);
188 strcat (combined
, ": ");
189 strcat (combined
, err
);
191 error ("%s.", combined
);
194 /* Print the system error message for ERRCODE, and also mention STRING
195 as the file name for which the error was encountered. */
198 print_sys_errmsg (string
, errcode
)
203 extern char *sys_errlist
[];
207 if (errcode
< sys_nerr
)
208 err
= sys_errlist
[errcode
];
210 err
= "unknown error";
212 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
213 strcpy (combined
, string
);
214 strcat (combined
, ": ");
215 strcat (combined
, err
);
217 printf ("%s.\n", combined
);
224 ioctl (fileno (stdout
), TCFLSH
, 1);
225 #else /* not HAVE_TERMIO */
226 ioctl (fileno (stdout
), TIOCFLUSH
, 0);
227 #endif /* not HAVE_TERMIO */
231 error ("Quit (expect signal %d when inferior is resumed)", SIGINT
);
232 #endif /* TIOCGPGRP */
235 /* Control C comes here */
243 /* Restore the signal handler. */
244 signal (SIGINT
, request_quit
);
251 /* Print an error message and return to command level.
252 STRING is the error message, used as a fprintf string,
253 and ARG is passed as an argument to it. */
256 error (string
, arg1
, arg2
, arg3
)
258 int arg1
, arg2
, arg3
;
260 terminal_ours (); /* Should be ok even if no inf. */
262 fprintf (stderr
, string
, arg1
, arg2
, arg3
);
263 fprintf (stderr
, "\n");
264 return_to_top_level ();
267 /* Print an error message and exit reporting failure.
268 This is for a error that we cannot continue from.
269 STRING and ARG are passed to fprintf. */
276 fprintf (stderr
, "gdb: ");
277 fprintf (stderr
, string
, arg
);
278 fprintf (stderr
, "\n");
282 /* Print an error message and exit, dumping core.
283 STRING is a printf-style control string, and ARG is a corresponding
286 fatal_dump_core (string
, arg
)
290 fprintf (stderr
, "gdb: ");
291 fprintf (stderr
, string
, arg
);
292 fprintf (stderr
, "\n");
293 signal (SIGQUIT
, SIG_DFL
);
294 kill (getpid (), SIGQUIT
);
295 /* We should never get here, but just in case... */
299 /* Make a copy of the string at PTR with SIZE characters
300 (and add a null character at the end in the copy).
301 Uses malloc to get the space. Returns the address of the copy. */
304 savestring (ptr
, size
)
308 register char *p
= (char *) xmalloc (size
+ 1);
309 bcopy (ptr
, p
, size
);
318 register int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
319 register char *val
= (char *) xmalloc (len
);
327 print_spaces (n
, file
)
335 /* Ask user a y-or-n question and return 1 iff answer is yes.
336 Takes three args which are given to printf to print the question.
337 The first, a control string, should end in "? ".
338 It should not say how to answer, because we do that. */
341 query (ctlstr
, arg1
, arg2
)
346 /* Automatically answer "yes" if input is not from a terminal. */
347 if (!input_from_terminal_p ())
352 printf (ctlstr
, arg1
, arg2
);
353 printf ("(y or n) ");
355 answer
= fgetc (stdin
);
356 clearerr (stdin
); /* in case of C-d */
358 while (fgetc (stdin
) != '\n') clearerr (stdin
);
365 printf ("Please answer y or n.\n");
369 /* Parse a C escape sequence. STRING_PTR points to a variable
370 containing a pointer to the string to parse. That pointer
371 is updated past the characters we use. The value of the
372 escape sequence is returned.
374 A negative value means the sequence \ newline was seen,
375 which is supposed to be equivalent to nothing at all.
377 If \ is followed by a null character, we return a negative
378 value and leave the string pointer pointing at the null character.
380 If \ is followed by 000, we return 0 and leave the string pointer
381 after the zeros. A value of 0 does not mean end of string. */
384 parse_escape (string_ptr
)
387 register int c
= *(*string_ptr
)++;
412 c
= *(*string_ptr
)++;
414 c
= parse_escape (string_ptr
);
417 return (c
& 0200) | (c
& 037);
428 register int i
= c
- '0';
429 register int count
= 0;
432 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
450 /* Print the character CH on STREAM as part of the contents
451 of a literal string whose delimiter is QUOTER. */
454 printchar (ch
, stream
, quoter
)
460 if (c
< 040 || c
>= 0177)
464 fputs_filtered ("\\n", stream
);
467 fputs_filtered ("\\b", stream
);
470 fputs_filtered ("\\t", stream
);
473 fputs_filtered ("\\f", stream
);
476 fputs_filtered ("\\r", stream
);
479 fputs_filtered ("\\e", stream
);
482 fputs_filtered ("\\a", stream
);
485 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
490 if (c
== '\\' || c
== quoter
)
491 fputs_filtered ("\\", stream
);
492 fprintf_filtered (stream
, "%c", c
);
496 static int lines_per_page
, lines_printed
, chars_per_line
, chars_printed
;
498 /* Set values of page and line size. */
500 set_screensize_command (arg
, from_tty
)
506 int tolinesize
= lines_per_page
;
507 int tocharsize
= chars_per_line
;
510 error_no_arg ("set screensize");
512 while (*p
>= '0' && *p
<= '9')
515 if (*p
&& *p
!= ' ' && *p
!= '\t')
516 error ("Non-integral argument given to \"set screensize\".");
518 tolinesize
= atoi (arg
);
520 while (*p
== ' ' || *p
== '\t')
526 while (*p1
>= '0' && *p1
<= '9')
530 error ("Non-integral second argument given to \"set screensize\".");
532 tocharsize
= atoi (p
);
535 lines_per_page
= tolinesize
;
536 chars_per_line
= tocharsize
;
540 prompt_for_continue ()
543 gdb_readline ("---Type <return> to continue---", 0);
544 chars_printed
= lines_printed
= 0;
548 /* Reinitialize filter; ie. tell it to reset to original values. */
551 reinitialize_more_filter ()
558 screensize_info (arg
, from_tty
)
563 error ("\"info screensize\" does not take any arguments.");
566 printf ("Output more filtering is disabled.\n");
569 printf ("Output more filtering is enabled with\n");
570 printf ("%d lines per page and %d characters per line.\n",
571 lines_per_page
, chars_per_line
);
575 /* Like fputs but pause after every screenful.
576 Unlike fputs, fputs_filtered does not return a value.
577 It is OK for LINEBUFFER to be NULL, in which case just don't print
580 Note that a longjmp to top level may occur in this routine
581 (since prompt_for_continue may do so) so this routine should not be
582 called when cleanups are not in place. */
585 fputs_filtered (linebuffer
, stream
)
594 /* Don't do any filtering if it is disabled. */
595 if (stream
!= stdout
|| !ISATTY(stdout
) || lines_per_page
== 0)
597 fputs (linebuffer
, stream
);
601 /* Go through and output each character. Show line extension
602 when this is necessary; prompt user for new page when this is
605 lineptr
= linebuffer
;
608 /* Possible new page. */
609 if (lines_printed
>= lines_per_page
- 1)
610 prompt_for_continue ();
612 while (*lineptr
&& *lineptr
!= '\n')
614 /* Print a single line. */
615 if (*lineptr
== '\t')
618 /* Shifting right by 3 produces the number of tab stops
619 we have already passed, and then adding one and
620 shifting left 3 advances to the next tab stop. */
621 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
626 putc (*lineptr
, stream
);
631 if (chars_printed
>= chars_per_line
)
635 /* Possible new page. */
636 if (lines_printed
>= lines_per_page
- 1)
637 prompt_for_continue ();
641 if (*lineptr
== '\n')
651 /* Print ARG1, ARG2, and ARG3 on stdout using format FORMAT. If this
652 information is going to put the amount written since the last call
653 to INIIALIZE_MORE_FILTER or the last page break over the page size,
654 print out a pause message and do a gdb_readline to get the users
655 permision to continue.
657 Unlike fprintf, this function does not return a value.
659 Note that this routine has a restriction that the length of the
660 final output line must be less than 255 characters *or* it must be
661 less than twice the size of the format string. This is a very
662 arbitrary restriction, but it is an internal restriction, so I'll
663 put it in. This means that the %s format specifier is almost
664 useless; unless the caller can GUARANTEE that the string is short
665 enough, fputs_filtered should be used instead.
667 Note also that a longjmp to top level may occur in this routine
668 (since prompt_for_continue may do so) so this routine should not be
669 called when cleanups are not in place. */
672 fprintf_filtered (stream
, format
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
)
675 int arg1
, arg2
, arg3
, arg4
, arg5
, arg6
;
677 static char *linebuffer
= (char *) 0;
678 static int line_size
;
679 int format_length
= strlen (format
);
682 /* Allocated linebuffer for the first time. */
685 linebuffer
= (char *) xmalloc (255);
689 /* Reallocate buffer to a larger size if this is necessary. */
690 if (format_length
* 2 > line_size
)
692 line_size
= format_length
* 2;
694 /* You don't have to copy. */
696 linebuffer
= (char *) xmalloc (line_size
);
699 /* This won't blow up if the restrictions described above are
701 (void) sprintf (linebuffer
, format
, arg1
, arg2
, arg3
);
703 fputs_filtered (linebuffer
, stream
);
707 printf_filtered (format
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
)
709 int arg1
, arg2
, arg3
, arg4
, arg5
, arg6
;
711 fprintf_filtered (stdout
, format
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
714 /* Print N spaces. */
716 print_spaces_filtered (n
, stream
)
720 register char *s
= (char *) alloca (n
+ 1);
721 register char *t
= s
;
727 fputs_filtered (s
, stream
);
732 bcopy (from
, to
, count
)
735 memcpy (to
, from
, count
);
738 bcmp (from
, to
, count
)
740 return (memcmp (to
, from
, count
));
753 getcwd (buf
, MAXPATHLEN
);
761 return strchr (s
, c
);
769 return strrchr (s
, c
);
773 char *sys_siglist
[32] = {
811 item
->forw
= after
->forw
;
812 after
->forw
->back
= item
;
821 item
->forw
->back
= item
->back
;
822 item
->back
->forw
= item
->forw
;
827 /* There is too much variation in Sys V signal numbers and names, so
828 we must initialize them at runtime. */
829 static char undoc
[] = "(undocumented)";
831 char *sys_siglist
[NSIG
];
834 extern struct cmd_list_element
*setlist
;
840 add_cmd ("screensize", class_support
, set_screensize_command
,
841 "Change gdb's notion of the size of the output screen.\n\
842 The first argument is the number of lines on a page.\n\
843 The second argument (optional) is the number of characters on a line.",
845 add_info ("screensize", screensize_info
,
846 "Show gdb's current notion of the size of the output screen.");
848 /* These defaults will be used if we are unable to get the correct
849 values from termcap. */
852 /* Initialize the screen height and width from termcap. */
854 int termtype
= getenv ("TERM");
856 /* Positive means success, nonpositive means failure. */
859 /* 2048 is large enough for all known terminals, according to the
860 GNU termcap manual. */
861 char term_buffer
[2048];
865 status
= tgetent (term_buffer
, termtype
);
870 val
= tgetnum ("li");
872 lines_per_page
= val
;
874 /* The number of lines per page is not mentioned
875 in the terminal description. This probably means
876 that paging is not useful (e.g. emacs shell window),
877 so disable paging. */
880 val
= tgetnum ("co");
882 chars_per_line
= val
;
888 /* Initialize signal names. */
889 for (i
= 0; i
< NSIG
; i
++)
890 sys_siglist
[i
] = undoc
;
893 sys_siglist
[SIGHUP
] = "SIGHUP";
896 sys_siglist
[SIGINT
] = "SIGINT";
899 sys_siglist
[SIGQUIT
] = "SIGQUIT";
902 sys_siglist
[SIGILL
] = "SIGILL";
905 sys_siglist
[SIGTRAP
] = "SIGTRAP";
908 sys_siglist
[SIGIOT
] = "SIGIOT";
911 sys_siglist
[SIGEMT
] = "SIGEMT";
914 sys_siglist
[SIGFPE
] = "SIGFPE";
917 sys_siglist
[SIGKILL
] = "SIGKILL";
920 sys_siglist
[SIGBUS
] = "SIGBUS";
923 sys_siglist
[SIGSEGV
] = "SIGSEGV";
926 sys_siglist
[SIGSYS
] = "SIGSYS";
929 sys_siglist
[SIGPIPE
] = "SIGPIPE";
932 sys_siglist
[SIGALRM
] = "SIGALRM";
935 sys_siglist
[SIGTERM
] = "SIGTERM";
938 sys_siglist
[SIGUSR1
] = "SIGUSR1";
941 sys_siglist
[SIGUSR2
] = "SIGUSR2";
944 sys_siglist
[SIGCLD
] = "SIGCLD";
947 sys_siglist
[SIGCHLD
] = "SIGCHLD";
950 sys_siglist
[SIGPWR
] = "SIGPWR";
953 sys_siglist
[SIGTSTP
] = "SIGTSTP";
956 sys_siglist
[SIGTTIN
] = "SIGTTIN";
959 sys_siglist
[SIGTTOU
] = "SIGTTOU";
962 sys_siglist
[SIGSTOP
] = "SIGSTOP";
965 sys_siglist
[SIGXCPU
] = "SIGXCPU";
968 sys_siglist
[SIGXFSZ
] = "SIGXFSZ";
971 sys_siglist
[SIGVTALRM
] = "SIGVTALRM";
974 sys_siglist
[SIGPROF
] = "SIGPROF";
977 sys_siglist
[SIGWINCH
] = "SIGWINCH";
980 sys_siglist
[SIGCONT
] = "SIGCONT";
983 sys_siglist
[SIGURG
] = "SIGURG";
986 sys_siglist
[SIGIO
] = "SIGIO";
989 sys_siglist
[SIGWIND
] = "SIGWIND";
992 sys_siglist
[SIGPHONE
] = "SIGPHONE";
995 sys_siglist
[SIGPOLL
] = "SIGPOLL";