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 /* "internal error" is always correct, since GDB should never dump
291 core, no matter what the input. */
292 fprintf (stderr
, "gdb internal error: ");
293 fprintf (stderr
, string
, arg
);
294 fprintf (stderr
, "\n");
295 signal (SIGQUIT
, SIG_DFL
);
296 kill (getpid (), SIGQUIT
);
297 /* We should never get here, but just in case... */
301 /* Make a copy of the string at PTR with SIZE characters
302 (and add a null character at the end in the copy).
303 Uses malloc to get the space. Returns the address of the copy. */
306 savestring (ptr
, size
)
310 register char *p
= (char *) xmalloc (size
+ 1);
311 bcopy (ptr
, p
, size
);
320 register int len
= strlen (s1
) + strlen (s2
) + strlen (s3
) + 1;
321 register char *val
= (char *) xmalloc (len
);
329 print_spaces (n
, file
)
337 /* Ask user a y-or-n question and return 1 iff answer is yes.
338 Takes three args which are given to printf to print the question.
339 The first, a control string, should end in "? ".
340 It should not say how to answer, because we do that. */
343 query (ctlstr
, arg1
, arg2
)
348 /* Automatically answer "yes" if input is not from a terminal. */
349 if (!input_from_terminal_p ())
354 printf (ctlstr
, arg1
, arg2
);
355 printf ("(y or n) ");
357 answer
= fgetc (stdin
);
358 clearerr (stdin
); /* in case of C-d */
360 while (fgetc (stdin
) != '\n') clearerr (stdin
);
367 printf ("Please answer y or n.\n");
371 /* Parse a C escape sequence. STRING_PTR points to a variable
372 containing a pointer to the string to parse. That pointer
373 is updated past the characters we use. The value of the
374 escape sequence is returned.
376 A negative value means the sequence \ newline was seen,
377 which is supposed to be equivalent to nothing at all.
379 If \ is followed by a null character, we return a negative
380 value and leave the string pointer pointing at the null character.
382 If \ is followed by 000, we return 0 and leave the string pointer
383 after the zeros. A value of 0 does not mean end of string. */
386 parse_escape (string_ptr
)
389 register int c
= *(*string_ptr
)++;
414 c
= *(*string_ptr
)++;
416 c
= parse_escape (string_ptr
);
419 return (c
& 0200) | (c
& 037);
430 register int i
= c
- '0';
431 register int count
= 0;
434 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
452 /* Print the character CH on STREAM as part of the contents
453 of a literal string whose delimiter is QUOTER. */
456 printchar (ch
, stream
, quoter
)
462 if (c
< 040 || c
>= 0177)
466 fputs_filtered ("\\n", stream
);
469 fputs_filtered ("\\b", stream
);
472 fputs_filtered ("\\t", stream
);
475 fputs_filtered ("\\f", stream
);
478 fputs_filtered ("\\r", stream
);
481 fputs_filtered ("\\e", stream
);
484 fputs_filtered ("\\a", stream
);
487 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
492 if (c
== '\\' || c
== quoter
)
493 fputs_filtered ("\\", stream
);
494 fprintf_filtered (stream
, "%c", c
);
498 static int lines_per_page
, lines_printed
, chars_per_line
, chars_printed
;
500 /* Set values of page and line size. */
502 set_screensize_command (arg
, from_tty
)
508 int tolinesize
= lines_per_page
;
509 int tocharsize
= chars_per_line
;
512 error_no_arg ("set screensize");
514 while (*p
>= '0' && *p
<= '9')
517 if (*p
&& *p
!= ' ' && *p
!= '\t')
518 error ("Non-integral argument given to \"set screensize\".");
520 tolinesize
= atoi (arg
);
522 while (*p
== ' ' || *p
== '\t')
528 while (*p1
>= '0' && *p1
<= '9')
532 error ("Non-integral second argument given to \"set screensize\".");
534 tocharsize
= atoi (p
);
537 lines_per_page
= tolinesize
;
538 chars_per_line
= tocharsize
;
542 prompt_for_continue ()
545 gdb_readline ("---Type <return> to continue---", 0);
546 chars_printed
= lines_printed
= 0;
550 /* Reinitialize filter; ie. tell it to reset to original values. */
553 reinitialize_more_filter ()
560 screensize_info (arg
, from_tty
)
565 error ("\"info screensize\" does not take any arguments.");
568 printf ("Output more filtering is disabled.\n");
571 printf ("Output more filtering is enabled with\n");
572 printf ("%d lines per page and %d characters per line.\n",
573 lines_per_page
, chars_per_line
);
577 /* Like fputs but pause after every screenful.
578 Unlike fputs, fputs_filtered does not return a value.
579 It is OK for LINEBUFFER to be NULL, in which case just don't print
582 Note that a longjmp to top level may occur in this routine
583 (since prompt_for_continue may do so) so this routine should not be
584 called when cleanups are not in place. */
587 fputs_filtered (linebuffer
, stream
)
596 /* Don't do any filtering if it is disabled. */
597 if (stream
!= stdout
|| !ISATTY(stdout
) || lines_per_page
== 0)
599 fputs (linebuffer
, stream
);
603 /* Go through and output each character. Show line extension
604 when this is necessary; prompt user for new page when this is
607 lineptr
= linebuffer
;
610 /* Possible new page. */
611 if (lines_printed
>= lines_per_page
- 1)
612 prompt_for_continue ();
614 while (*lineptr
&& *lineptr
!= '\n')
616 /* Print a single line. */
617 if (*lineptr
== '\t')
620 /* Shifting right by 3 produces the number of tab stops
621 we have already passed, and then adding one and
622 shifting left 3 advances to the next tab stop. */
623 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
628 putc (*lineptr
, stream
);
633 if (chars_printed
>= chars_per_line
)
637 /* Possible new page. */
638 if (lines_printed
>= lines_per_page
- 1)
639 prompt_for_continue ();
643 if (*lineptr
== '\n')
653 /* Print ARG1, ARG2, and ARG3 on stdout using format FORMAT. If this
654 information is going to put the amount written since the last call
655 to INIIALIZE_MORE_FILTER or the last page break over the page size,
656 print out a pause message and do a gdb_readline to get the users
657 permision to continue.
659 Unlike fprintf, this function does not return a value.
661 Note that this routine has a restriction that the length of the
662 final output line must be less than 255 characters *or* it must be
663 less than twice the size of the format string. This is a very
664 arbitrary restriction, but it is an internal restriction, so I'll
665 put it in. This means that the %s format specifier is almost
666 useless; unless the caller can GUARANTEE that the string is short
667 enough, fputs_filtered should be used instead.
669 Note also that a longjmp to top level may occur in this routine
670 (since prompt_for_continue may do so) so this routine should not be
671 called when cleanups are not in place. */
674 fprintf_filtered (stream
, format
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
)
677 int arg1
, arg2
, arg3
, arg4
, arg5
, arg6
;
679 static char *linebuffer
= (char *) 0;
680 static int line_size
;
681 int format_length
= strlen (format
);
684 /* Allocated linebuffer for the first time. */
687 linebuffer
= (char *) xmalloc (255);
691 /* Reallocate buffer to a larger size if this is necessary. */
692 if (format_length
* 2 > line_size
)
694 line_size
= format_length
* 2;
696 /* You don't have to copy. */
698 linebuffer
= (char *) xmalloc (line_size
);
701 /* This won't blow up if the restrictions described above are
703 (void) sprintf (linebuffer
, format
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
705 fputs_filtered (linebuffer
, stream
);
709 printf_filtered (format
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
)
711 int arg1
, arg2
, arg3
, arg4
, arg5
, arg6
;
713 fprintf_filtered (stdout
, format
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
716 /* Print N spaces. */
718 print_spaces_filtered (n
, stream
)
722 register char *s
= (char *) alloca (n
+ 1);
723 register char *t
= s
;
729 fputs_filtered (s
, stream
);
734 bcopy (from
, to
, count
)
737 memcpy (to
, from
, count
);
740 bcmp (from
, to
, count
)
742 return (memcmp (to
, from
, count
));
755 getcwd (buf
, MAXPATHLEN
);
763 return strchr (s
, c
);
771 return strrchr (s
, c
);
775 char *sys_siglist
[32] = {
813 item
->forw
= after
->forw
;
814 after
->forw
->back
= item
;
823 item
->forw
->back
= item
->back
;
824 item
->back
->forw
= item
->forw
;
829 /* There is too much variation in Sys V signal numbers and names, so
830 we must initialize them at runtime. */
831 static char undoc
[] = "(undocumented)";
833 char *sys_siglist
[NSIG
];
836 extern struct cmd_list_element
*setlist
;
842 add_cmd ("screensize", class_support
, set_screensize_command
,
843 "Change gdb's notion of the size of the output screen.\n\
844 The first argument is the number of lines on a page.\n\
845 The second argument (optional) is the number of characters on a line.",
847 add_info ("screensize", screensize_info
,
848 "Show gdb's current notion of the size of the output screen.");
850 /* These defaults will be used if we are unable to get the correct
851 values from termcap. */
854 /* Initialize the screen height and width from termcap. */
856 int termtype
= getenv ("TERM");
858 /* Positive means success, nonpositive means failure. */
861 /* 2048 is large enough for all known terminals, according to the
862 GNU termcap manual. */
863 char term_buffer
[2048];
867 status
= tgetent (term_buffer
, termtype
);
872 val
= tgetnum ("li");
874 lines_per_page
= val
;
876 /* The number of lines per page is not mentioned
877 in the terminal description. This probably means
878 that paging is not useful (e.g. emacs shell window),
879 so disable paging. */
882 val
= tgetnum ("co");
884 chars_per_line
= val
;
890 /* Initialize signal names. */
891 for (i
= 0; i
< NSIG
; i
++)
892 sys_siglist
[i
] = undoc
;
895 sys_siglist
[SIGHUP
] = "SIGHUP";
898 sys_siglist
[SIGINT
] = "SIGINT";
901 sys_siglist
[SIGQUIT
] = "SIGQUIT";
904 sys_siglist
[SIGILL
] = "SIGILL";
907 sys_siglist
[SIGTRAP
] = "SIGTRAP";
910 sys_siglist
[SIGIOT
] = "SIGIOT";
913 sys_siglist
[SIGEMT
] = "SIGEMT";
916 sys_siglist
[SIGFPE
] = "SIGFPE";
919 sys_siglist
[SIGKILL
] = "SIGKILL";
922 sys_siglist
[SIGBUS
] = "SIGBUS";
925 sys_siglist
[SIGSEGV
] = "SIGSEGV";
928 sys_siglist
[SIGSYS
] = "SIGSYS";
931 sys_siglist
[SIGPIPE
] = "SIGPIPE";
934 sys_siglist
[SIGALRM
] = "SIGALRM";
937 sys_siglist
[SIGTERM
] = "SIGTERM";
940 sys_siglist
[SIGUSR1
] = "SIGUSR1";
943 sys_siglist
[SIGUSR2
] = "SIGUSR2";
946 sys_siglist
[SIGCLD
] = "SIGCLD";
949 sys_siglist
[SIGCHLD
] = "SIGCHLD";
952 sys_siglist
[SIGPWR
] = "SIGPWR";
955 sys_siglist
[SIGTSTP
] = "SIGTSTP";
958 sys_siglist
[SIGTTIN
] = "SIGTTIN";
961 sys_siglist
[SIGTTOU
] = "SIGTTOU";
964 sys_siglist
[SIGSTOP
] = "SIGSTOP";
967 sys_siglist
[SIGXCPU
] = "SIGXCPU";
970 sys_siglist
[SIGXFSZ
] = "SIGXFSZ";
973 sys_siglist
[SIGVTALRM
] = "SIGVTALRM";
976 sys_siglist
[SIGPROF
] = "SIGPROF";
979 sys_siglist
[SIGWINCH
] = "SIGWINCH";
982 sys_siglist
[SIGCONT
] = "SIGCONT";
985 sys_siglist
[SIGURG
] = "SIGURG";
988 sys_siglist
[SIGIO
] = "SIGIO";
991 sys_siglist
[SIGWIND
] = "SIGWIND";
994 sys_siglist
[SIGPHONE
] = "SIGPHONE";
997 sys_siglist
[SIGPOLL
] = "SIGPOLL";
This page took 0.051624 seconds and 4 git commands to generate.