1 /* General utility routines for GDB, the GNU debugger.
2 Copyright (C) 1986 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
23 #include <sys/ioctl.h>
24 #include <sys/param.h>
34 /* Chain of cleanup actions established with make_cleanup,
35 to be executed if an error happens. */
37 static struct cleanup *cleanup_chain;
39 /* Nonzero means a quit has been requested. */
43 /* Nonzero means quit immediately if Control-C is typed now,
44 rather than waiting until QUIT is executed. */
48 /* Add a new cleanup to the cleanup_chain,
49 and return the previous chain pointer
50 to be passed later to do_cleanups or discard_cleanups.
51 Args are FUNCTION to clean up with, and ARG to pass to it. */
54 make_cleanup (function, arg)
58 register struct cleanup *new
59 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
60 register struct cleanup *old_chain = cleanup_chain;
62 new->next = cleanup_chain;
63 new->function = function;
70 /* Discard cleanups and do the actions they describe
71 until we get back to the point OLD_CHAIN in the cleanup_chain. */
74 do_cleanups (old_chain)
75 register struct cleanup *old_chain;
77 register struct cleanup *ptr;
78 while ((ptr = cleanup_chain) != old_chain)
80 (*ptr->function) (ptr->arg);
81 cleanup_chain = ptr->next;
86 /* Discard cleanups, not doing the actions they describe,
87 until we get back to the point OLD_CHAIN in the cleanup_chain. */
90 discard_cleanups (old_chain)
91 register struct cleanup *old_chain;
93 register struct cleanup *ptr;
94 while ((ptr = cleanup_chain) != old_chain)
96 cleanup_chain = ptr->next;
101 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
105 struct cleanup *old_chain = cleanup_chain;
111 /* Restore the cleanup chain from a previously saved chain. */
113 restore_cleanups (chain)
114 struct cleanup *chain;
116 cleanup_chain = chain;
119 /* This function is useful for cleanups.
123 old_chain = make_cleanup (free_current_contents, &foo);
125 to arrange to free the object thus allocated. */
128 free_current_contents (location)
134 /* Generally useful subroutines used throughout the program. */
136 /* Like malloc but get error if no storage available. */
142 register char *val = (char *) malloc (size);
144 fatal ("virtual memory exhausted.", 0);
148 /* Like realloc but get error if no storage available. */
155 register char *val = (char *) realloc (ptr, size);
157 fatal ("virtual memory exhausted.", 0);
161 /* Print the system error message for errno, and also mention STRING
162 as the file name for which the error was encountered.
163 Then return to command level. */
166 perror_with_name (string)
170 extern char *sys_errlist[];
175 if (errno < sys_nerr)
176 err = sys_errlist[errno];
178 err = "unknown error";
180 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
181 strcpy (combined, string);
182 strcat (combined, ": ");
183 strcat (combined, err);
185 error ("%s.", combined);
188 /* Print the system error message for ERRCODE, and also mention STRING
189 as the file name for which the error was encountered. */
192 print_sys_errmsg (string, errcode)
197 extern char *sys_errlist[];
201 if (errcode < sys_nerr)
202 err = sys_errlist[errcode];
204 err = "unknown error";
206 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
207 strcpy (combined, string);
208 strcat (combined, ": ");
209 strcat (combined, err);
211 printf ("%s.\n", combined);
219 ioctl (fileno (stdout), TCFLSH, 1);
220 #else /* not HAVE_TERMIO */
221 ioctl (fileno (stdout), TIOCFLUSH, 0);
222 #endif /* not HAVE_TERMIO */
227 error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
228 #endif /* TIOCGPGRP */
231 /* Control C comes here */
239 /* Restore the signal handler */
240 signal(SIGINT, request_quit);
247 /* Print an error message and return to command level.
248 STRING is the error message, used as a fprintf string,
249 and ARG is passed as an argument to it. */
252 error (string, arg1, arg2, arg3)
254 int arg1, arg2, arg3;
257 fprintf (stderr, string, arg1, arg2, arg3);
258 fprintf (stderr, "\n");
259 return_to_top_level ();
262 /* Print an error message and exit reporting failure.
263 This is for a error that we cannot continue from.
264 STRING and ARG are passed to fprintf. */
271 fprintf (stderr, "gdb: ");
272 fprintf (stderr, string, arg);
273 fprintf (stderr, "\n");
277 /* Make a copy of the string at PTR with SIZE characters
278 (and add a null character at the end in the copy).
279 Uses malloc to get the space. Returns the address of the copy. */
282 savestring (ptr, size)
286 register char *p = (char *) xmalloc (size + 1);
287 bcopy (ptr, p, size);
296 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
297 register char *val = (char *) xmalloc (len);
305 print_spaces (n, file)
313 /* Ask user a y-or-n question and return 1 iff answer is yes.
314 Takes three args which are given to printf to print the question.
315 The first, a control string, should end in "? ".
316 It should not say how to answer, because we do that. */
319 query (ctlstr, arg1, arg2)
324 /* Automatically answer "yes" if input is not from a terminal. */
325 if (!input_from_terminal_p ())
330 printf (ctlstr, arg1, arg2);
331 printf ("(y or n) ");
333 answer = fgetc (stdin);
334 clearerr (stdin); /* in case of C-d */
336 while (fgetc (stdin) != '\n') clearerr (stdin);
343 printf ("Please answer y or n.\n");
347 /* Parse a C escape sequence. STRING_PTR points to a variable
348 containing a pointer to the string to parse. That pointer
349 is updated past the characters we use. The value of the
350 escape sequence is returned.
352 A negative value means the sequence \ newline was seen,
353 which is supposed to be equivalent to nothing at all.
355 If \ is followed by a null character, we return a negative
356 value and leave the string pointer pointing at the null character.
358 If \ is followed by 000, we return 0 and leave the string pointer
359 after the zeros. A value of 0 does not mean end of string. */
362 parse_escape (string_ptr)
365 register int c = *(*string_ptr)++;
390 c = *(*string_ptr)++;
392 c = parse_escape (string_ptr);
395 return (c & 0200) | (c & 037);
406 register int i = c - '0';
407 register int count = 0;
410 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
428 /* Print the character CH on STREAM as part of the contents
429 of a literal string whose delimiter is QUOTER. */
432 printchar (ch, stream, quoter)
438 if (c < 040 || c >= 0177)
441 fprintf (stream, "\\n");
443 fprintf (stream, "\\b");
445 fprintf (stream, "\\t");
447 fprintf (stream, "\\f");
449 fprintf (stream, "\\r");
451 fprintf (stream, "\\e");
453 fprintf (stream, "\\a");
455 fprintf (stream, "\\%03o", c);
459 if (c == '\\' || c == quoter)
460 fputc ('\\', stream);
467 bcopy (from, to, count)
470 memcpy (to, from, count);
473 bcmp (from, to, count)
475 return (memcmp (to, from, count));
488 getcwd (buf, MAXPATHLEN);
496 return strchr (s, c);
504 return strrchr (s, c);
518 item->forw = after->forw;
519 after->forw->back = item;
528 item->forw->back = item->back;
529 item->back->forw = item->forw;
534 * There is too much variation in Sys V signal numbers and names, so
535 * we must initialize them at runtime. If C provided a way to initialize
536 * an array based on subscript and value, this would not be necessary.
538 static char undoc[] = "(undocumented)";
540 char *sys_siglist[NSIG];
546 for (i = 0; i < NSIG; i++)
547 sys_siglist[i] = undoc;
550 sys_siglist[SIGHUP ] = "SIGHUP";
553 sys_siglist[SIGINT ] = "SIGINT";
556 sys_siglist[SIGQUIT ] = "SIGQUIT";
559 sys_siglist[SIGILL ] = "SIGILL";
562 sys_siglist[SIGTRAP ] = "SIGTRAP";
565 sys_siglist[SIGIOT ] = "SIGIOT";
568 sys_siglist[SIGEMT ] = "SIGEMT";
571 sys_siglist[SIGFPE ] = "SIGFPE";
574 sys_siglist[SIGKILL ] = "SIGKILL";
577 sys_siglist[SIGBUS ] = "SIGBUS";
580 sys_siglist[SIGSEGV ] = "SIGSEGV";
583 sys_siglist[SIGSYS ] = "SIGSYS";
586 sys_siglist[SIGPIPE ] = "SIGPIPE";
589 sys_siglist[SIGALRM ] = "SIGALRM";
592 sys_siglist[SIGTERM ] = "SIGTERM";
595 sys_siglist[SIGUSR1 ] = "SIGUSR1";
598 sys_siglist[SIGUSR2 ] = "SIGUSR2";
601 sys_siglist[SIGCLD ] = "SIGCLD";
604 sys_siglist[SIGCHLD ] = "SIGCHLD";
607 sys_siglist[SIGPWR ] = "SIGPWR";
610 sys_siglist[SIGTSTP ] = "SIGTSTP";
613 sys_siglist[SIGTTIN ] = "SIGTTIN";
616 sys_siglist[SIGTTOU ] = "SIGTTOU";
619 sys_siglist[SIGSTOP ] = "SIGSTOP";
622 sys_siglist[SIGXCPU ] = "SIGXCPU";
625 sys_siglist[SIGXFSZ ] = "SIGXFSZ";
628 sys_siglist[SIGVTALRM ] = "SIGVTALRM";
631 sys_siglist[SIGPROF ] = "SIGPROF";
634 sys_siglist[SIGWINCH ] = "SIGWINCH";
637 sys_siglist[SIGCONT ] = "SIGCONT";
640 sys_siglist[SIGURG ] = "SIGURG";
643 sys_siglist[SIGIO ] = "SIGIO";
646 sys_siglist[SIGWIND ] = "SIGWIND";
649 sys_siglist[SIGPHONE ] = "SIGPHONE";
652 sys_siglist[SIGPOLL ] = "SIGPOLL";