* symtab.c (find_pc_symtab): some object file formats, notably mips,
[deliverable/binutils-gdb.git] / gdb / utils.c
1 /* General utility routines for GDB, the GNU debugger.
2 Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
20 #include "defs.h"
21 #if !defined(__GO32__)
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
24 #include <pwd.h>
25 #endif
26 #include <varargs.h>
27 #include <ctype.h>
28 #include <string.h>
29
30 #include "signals.h"
31 #include "gdbcmd.h"
32 #include "terminal.h"
33 #include "bfd.h"
34 #include "target.h"
35 #include "demangle.h"
36 #include "expression.h"
37 #include "language.h"
38
39 /* Prototypes for local functions */
40
41 #if !defined (NO_MALLOC_CHECK)
42
43 static void
44 malloc_botch PARAMS ((void));
45
46 #endif /* NO_MALLOC_CHECK */
47
48 static void
49 fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
50
51 static void
52 prompt_for_continue PARAMS ((void));
53
54 static void
55 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
56
57 /* If this definition isn't overridden by the header files, assume
58 that isatty and fileno exist on this system. */
59 #ifndef ISATTY
60 #define ISATTY(FP) (isatty (fileno (FP)))
61 #endif
62
63 /* Chain of cleanup actions established with make_cleanup,
64 to be executed if an error happens. */
65
66 static struct cleanup *cleanup_chain;
67
68 /* Nonzero means a quit has been requested. */
69
70 int quit_flag;
71
72 /* Nonzero means quit immediately if Control-C is typed now,
73 rather than waiting until QUIT is executed. */
74
75 int immediate_quit;
76
77 /* Nonzero means that encoded C++ names should be printed out in their
78 C++ form rather than raw. */
79
80 int demangle = 1;
81
82 /* Nonzero means that encoded C++ names should be printed out in their
83 C++ form even in assembler language displays. If this is set, but
84 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
85
86 int asm_demangle = 0;
87
88 /* Nonzero means that strings with character values >0x7F should be printed
89 as octal escapes. Zero means just print the value (e.g. it's an
90 international character, and the terminal or window can cope.) */
91
92 int sevenbit_strings = 0;
93
94 /* String to be printed before error messages, if any. */
95
96 char *error_pre_print;
97 char *warning_pre_print = "\nwarning: ";
98 \f
99 /* Add a new cleanup to the cleanup_chain,
100 and return the previous chain pointer
101 to be passed later to do_cleanups or discard_cleanups.
102 Args are FUNCTION to clean up with, and ARG to pass to it. */
103
104 struct cleanup *
105 make_cleanup (function, arg)
106 void (*function) PARAMS ((PTR));
107 PTR arg;
108 {
109 register struct cleanup *new
110 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
111 register struct cleanup *old_chain = cleanup_chain;
112
113 new->next = cleanup_chain;
114 new->function = function;
115 new->arg = arg;
116 cleanup_chain = new;
117
118 return old_chain;
119 }
120
121 /* Discard cleanups and do the actions they describe
122 until we get back to the point OLD_CHAIN in the cleanup_chain. */
123
124 void
125 do_cleanups (old_chain)
126 register struct cleanup *old_chain;
127 {
128 register struct cleanup *ptr;
129 while ((ptr = cleanup_chain) != old_chain)
130 {
131 cleanup_chain = ptr->next; /* Do this first incase recursion */
132 (*ptr->function) (ptr->arg);
133 free (ptr);
134 }
135 }
136
137 /* Discard cleanups, not doing the actions they describe,
138 until we get back to the point OLD_CHAIN in the cleanup_chain. */
139
140 void
141 discard_cleanups (old_chain)
142 register struct cleanup *old_chain;
143 {
144 register struct cleanup *ptr;
145 while ((ptr = cleanup_chain) != old_chain)
146 {
147 cleanup_chain = ptr->next;
148 free ((PTR)ptr);
149 }
150 }
151
152 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
153 struct cleanup *
154 save_cleanups ()
155 {
156 struct cleanup *old_chain = cleanup_chain;
157
158 cleanup_chain = 0;
159 return old_chain;
160 }
161
162 /* Restore the cleanup chain from a previously saved chain. */
163 void
164 restore_cleanups (chain)
165 struct cleanup *chain;
166 {
167 cleanup_chain = chain;
168 }
169
170 /* This function is useful for cleanups.
171 Do
172
173 foo = xmalloc (...);
174 old_chain = make_cleanup (free_current_contents, &foo);
175
176 to arrange to free the object thus allocated. */
177
178 void
179 free_current_contents (location)
180 char **location;
181 {
182 free (*location);
183 }
184
185 /* Provide a known function that does nothing, to use as a base for
186 for a possibly long chain of cleanups. This is useful where we
187 use the cleanup chain for handling normal cleanups as well as dealing
188 with cleanups that need to be done as a result of a call to error().
189 In such cases, we may not be certain where the first cleanup is, unless
190 we have a do-nothing one to always use as the base. */
191
192 /* ARGSUSED */
193 void
194 null_cleanup (arg)
195 char **arg;
196 {
197 }
198
199 \f
200 /* Provide a hook for modules wishing to print their own warning messages
201 to set up the terminal state in a compatible way, without them having
202 to import all the target_<...> macros. */
203
204 void
205 warning_setup ()
206 {
207 target_terminal_ours ();
208 wrap_here(""); /* Force out any buffered output */
209 fflush (stdout);
210 }
211
212 /* Print a warning message.
213 The first argument STRING is the warning message, used as a fprintf string,
214 and the remaining args are passed as arguments to it.
215 The primary difference between warnings and errors is that a warning
216 does not force the return to command level. */
217
218 /* VARARGS */
219 void
220 warning (va_alist)
221 va_dcl
222 {
223 va_list args;
224 char *string;
225
226 va_start (args);
227 target_terminal_ours ();
228 wrap_here(""); /* Force out any buffered output */
229 fflush (stdout);
230 if (warning_pre_print)
231 fprintf (stderr, warning_pre_print);
232 string = va_arg (args, char *);
233 vfprintf (stderr, string, args);
234 fprintf (stderr, "\n");
235 va_end (args);
236 }
237
238 /* Print an error message and return to command level.
239 The first argument STRING is the error message, used as a fprintf string,
240 and the remaining args are passed as arguments to it. */
241
242 /* VARARGS */
243 NORETURN void
244 error (va_alist)
245 va_dcl
246 {
247 va_list args;
248 char *string;
249
250 va_start (args);
251 target_terminal_ours ();
252 wrap_here(""); /* Force out any buffered output */
253 fflush (stdout);
254 if (error_pre_print)
255 fprintf_filtered (stderr, error_pre_print);
256 string = va_arg (args, char *);
257 vfprintf_filtered (stderr, string, args);
258 fprintf_filtered (stderr, "\n");
259 va_end (args);
260 return_to_top_level ();
261 }
262
263 /* Print an error message and exit reporting failure.
264 This is for a error that we cannot continue from.
265 The arguments are printed a la printf.
266
267 This function cannot be declared volatile (NORETURN) in an
268 ANSI environment because exit() is not declared volatile. */
269
270 /* VARARGS */
271 NORETURN void
272 fatal (va_alist)
273 va_dcl
274 {
275 va_list args;
276 char *string;
277
278 va_start (args);
279 string = va_arg (args, char *);
280 fprintf (stderr, "\ngdb: ");
281 vfprintf (stderr, string, args);
282 fprintf (stderr, "\n");
283 va_end (args);
284 exit (1);
285 }
286
287 /* Print an error message and exit, dumping core.
288 The arguments are printed a la printf (). */
289
290 /* VARARGS */
291 static void
292 fatal_dump_core (va_alist)
293 va_dcl
294 {
295 va_list args;
296 char *string;
297
298 va_start (args);
299 string = va_arg (args, char *);
300 /* "internal error" is always correct, since GDB should never dump
301 core, no matter what the input. */
302 fprintf (stderr, "\ngdb internal error: ");
303 vfprintf (stderr, string, args);
304 fprintf (stderr, "\n");
305 va_end (args);
306
307 signal (SIGQUIT, SIG_DFL);
308 kill (getpid (), SIGQUIT);
309 /* We should never get here, but just in case... */
310 exit (1);
311 }
312
313 /* The strerror() function can return NULL for errno values that are
314 out of range. Provide a "safe" version that always returns a
315 printable string. */
316
317 char *
318 safe_strerror (errnum)
319 int errnum;
320 {
321 char *msg;
322 static char buf[32];
323
324 if ((msg = strerror (errnum)) == NULL)
325 {
326 sprintf (buf, "(undocumented errno %d)", errnum);
327 msg = buf;
328 }
329 return (msg);
330 }
331
332 /* The strsignal() function can return NULL for signal values that are
333 out of range. Provide a "safe" version that always returns a
334 printable string. */
335
336 char *
337 safe_strsignal (signo)
338 int signo;
339 {
340 char *msg;
341 static char buf[32];
342
343 if ((msg = strsignal (signo)) == NULL)
344 {
345 sprintf (buf, "(undocumented signal %d)", signo);
346 msg = buf;
347 }
348 return (msg);
349 }
350
351
352 /* Print the system error message for errno, and also mention STRING
353 as the file name for which the error was encountered.
354 Then return to command level. */
355
356 void
357 perror_with_name (string)
358 char *string;
359 {
360 char *err;
361 char *combined;
362
363 err = safe_strerror (errno);
364 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
365 strcpy (combined, string);
366 strcat (combined, ": ");
367 strcat (combined, err);
368
369 /* I understand setting these is a matter of taste. Still, some people
370 may clear errno but not know about bfd_error. Doing this here is not
371 unreasonable. */
372 bfd_error = no_error;
373 errno = 0;
374
375 error ("%s.", combined);
376 }
377
378 /* Print the system error message for ERRCODE, and also mention STRING
379 as the file name for which the error was encountered. */
380
381 void
382 print_sys_errmsg (string, errcode)
383 char *string;
384 int errcode;
385 {
386 char *err;
387 char *combined;
388
389 err = safe_strerror (errcode);
390 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
391 strcpy (combined, string);
392 strcat (combined, ": ");
393 strcat (combined, err);
394
395 fprintf (stderr, "%s.\n", combined);
396 }
397
398 /* Control C eventually causes this to be called, at a convenient time. */
399
400 void
401 quit ()
402 {
403 target_terminal_ours ();
404 wrap_here ((char *)0); /* Force out any pending output */
405 #if !defined(__GO32__)
406 #ifdef HAVE_TERMIO
407 ioctl (fileno (stdout), TCFLSH, 1);
408 #else /* not HAVE_TERMIO */
409 ioctl (fileno (stdout), TIOCFLUSH, 0);
410 #endif /* not HAVE_TERMIO */
411 #ifdef TIOCGPGRP
412 error ("Quit");
413 #else
414 error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
415 #endif /* TIOCGPGRP */
416 #else
417 error ("Quit");
418 #endif
419 }
420
421
422 #ifdef __GO32__
423
424 /* In the absence of signals, poll keyboard for a quit.
425 Called from #define QUIT pollquit() in xm-go32.h. */
426
427 void
428 pollquit()
429 {
430 if (kbhit ())
431 {
432 int k = getkey ();
433 if (k == 1)
434 quit_flag = 1;
435 else if (k == 2)
436 immediate_quit = 1;
437 quit ();
438 }
439 }
440
441 #endif
442
443 /* Control C comes here */
444
445 void
446 request_quit (signo)
447 int signo;
448 {
449 quit_flag = 1;
450
451 #ifdef USG
452 /* Restore the signal handler. */
453 signal (signo, request_quit);
454 #endif
455
456 if (immediate_quit)
457 quit ();
458 }
459
460 \f
461 /* Memory management stuff (malloc friends). */
462
463 #if defined (NO_MMALLOC)
464
465 PTR
466 mmalloc (md, size)
467 PTR md;
468 long size;
469 {
470 return (malloc (size));
471 }
472
473 PTR
474 mrealloc (md, ptr, size)
475 PTR md;
476 PTR ptr;
477 long size;
478 {
479 if (ptr == 0) /* Guard against old realloc's */
480 return malloc (size);
481 else
482 return realloc (ptr, size);
483 }
484
485 void
486 mfree (md, ptr)
487 PTR md;
488 PTR ptr;
489 {
490 free (ptr);
491 }
492
493 #endif /* NO_MMALLOC */
494
495 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
496
497 void
498 init_malloc (md)
499 PTR md;
500 {
501 }
502
503 #else /* have mmalloc and want corruption checking */
504
505 static void
506 malloc_botch ()
507 {
508 fatal_dump_core ("Memory corruption");
509 }
510
511 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
512 by MD, to detect memory corruption. Note that MD may be NULL to specify
513 the default heap that grows via sbrk.
514
515 Note that for freshly created regions, we must call mmcheck prior to any
516 mallocs in the region. Otherwise, any region which was allocated prior to
517 installing the checking hooks, which is later reallocated or freed, will
518 fail the checks! The mmcheck function only allows initial hooks to be
519 installed before the first mmalloc. However, anytime after we have called
520 mmcheck the first time to install the checking hooks, we can call it again
521 to update the function pointer to the memory corruption handler.
522
523 Returns zero on failure, non-zero on success. */
524
525 void
526 init_malloc (md)
527 PTR md;
528 {
529 if (!mmcheck (md, malloc_botch))
530 {
531 warning ("internal error: failed to install memory consistency checks");
532 }
533
534 mmtrace ();
535 }
536
537 #endif /* Have mmalloc and want corruption checking */
538
539 /* Called when a memory allocation fails, with the number of bytes of
540 memory requested in SIZE. */
541
542 NORETURN void
543 nomem (size)
544 long size;
545 {
546 if (size > 0)
547 {
548 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
549 }
550 else
551 {
552 fatal ("virtual memory exhausted.");
553 }
554 }
555
556 /* Like mmalloc but get error if no storage available, and protect against
557 the caller wanting to allocate zero bytes. Whether to return NULL for
558 a zero byte request, or translate the request into a request for one
559 byte of zero'd storage, is a religious issue. */
560
561 PTR
562 xmmalloc (md, size)
563 PTR md;
564 long size;
565 {
566 register PTR val;
567
568 if (size == 0)
569 {
570 val = NULL;
571 }
572 else if ((val = mmalloc (md, size)) == NULL)
573 {
574 nomem (size);
575 }
576 return (val);
577 }
578
579 /* Like mrealloc but get error if no storage available. */
580
581 PTR
582 xmrealloc (md, ptr, size)
583 PTR md;
584 PTR ptr;
585 long size;
586 {
587 register PTR val;
588
589 if (ptr != NULL)
590 {
591 val = mrealloc (md, ptr, size);
592 }
593 else
594 {
595 val = mmalloc (md, size);
596 }
597 if (val == NULL)
598 {
599 nomem (size);
600 }
601 return (val);
602 }
603
604 /* Like malloc but get error if no storage available, and protect against
605 the caller wanting to allocate zero bytes. */
606
607 PTR
608 xmalloc (size)
609 long size;
610 {
611 return (xmmalloc ((void *) NULL, size));
612 }
613
614 /* Like mrealloc but get error if no storage available. */
615
616 PTR
617 xrealloc (ptr, size)
618 PTR ptr;
619 long size;
620 {
621 return (xmrealloc ((void *) NULL, ptr, size));
622 }
623
624 \f
625 /* My replacement for the read system call.
626 Used like `read' but keeps going if `read' returns too soon. */
627
628 int
629 myread (desc, addr, len)
630 int desc;
631 char *addr;
632 int len;
633 {
634 register int val;
635 int orglen = len;
636
637 while (len > 0)
638 {
639 val = read (desc, addr, len);
640 if (val < 0)
641 return val;
642 if (val == 0)
643 return orglen - len;
644 len -= val;
645 addr += val;
646 }
647 return orglen;
648 }
649 \f
650 /* Make a copy of the string at PTR with SIZE characters
651 (and add a null character at the end in the copy).
652 Uses malloc to get the space. Returns the address of the copy. */
653
654 char *
655 savestring (ptr, size)
656 const char *ptr;
657 int size;
658 {
659 register char *p = (char *) xmalloc (size + 1);
660 memcpy (p, ptr, size);
661 p[size] = 0;
662 return p;
663 }
664
665 char *
666 msavestring (md, ptr, size)
667 void *md;
668 const char *ptr;
669 int size;
670 {
671 register char *p = (char *) xmmalloc (md, size + 1);
672 memcpy (p, ptr, size);
673 p[size] = 0;
674 return p;
675 }
676
677 /* The "const" is so it compiles under DGUX (which prototypes strsave
678 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
679 Doesn't real strsave return NULL if out of memory? */
680 char *
681 strsave (ptr)
682 const char *ptr;
683 {
684 return savestring (ptr, strlen (ptr));
685 }
686
687 char *
688 mstrsave (md, ptr)
689 void *md;
690 const char *ptr;
691 {
692 return (msavestring (md, ptr, strlen (ptr)));
693 }
694
695 void
696 print_spaces (n, file)
697 register int n;
698 register FILE *file;
699 {
700 while (n-- > 0)
701 fputc (' ', file);
702 }
703
704 /* Ask user a y-or-n question and return 1 iff answer is yes.
705 Takes three args which are given to printf to print the question.
706 The first, a control string, should end in "? ".
707 It should not say how to answer, because we do that. */
708
709 /* VARARGS */
710 int
711 query (va_alist)
712 va_dcl
713 {
714 va_list args;
715 char *ctlstr;
716 register int answer;
717 register int ans2;
718
719 /* Automatically answer "yes" if input is not from a terminal. */
720 if (!input_from_terminal_p ())
721 return 1;
722
723 while (1)
724 {
725 wrap_here (""); /* Flush any buffered output */
726 fflush (stdout);
727 va_start (args);
728 ctlstr = va_arg (args, char *);
729 vfprintf_filtered (stdout, ctlstr, args);
730 va_end (args);
731 printf_filtered ("(y or n) ");
732 fflush (stdout);
733 answer = fgetc (stdin);
734 clearerr (stdin); /* in case of C-d */
735 if (answer == EOF) /* C-d */
736 return 1;
737 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
738 do
739 {
740 ans2 = fgetc (stdin);
741 clearerr (stdin);
742 }
743 while (ans2 != EOF && ans2 != '\n');
744 if (answer >= 'a')
745 answer -= 040;
746 if (answer == 'Y')
747 return 1;
748 if (answer == 'N')
749 return 0;
750 printf_filtered ("Please answer y or n.\n");
751 }
752 }
753
754 \f
755 /* Parse a C escape sequence. STRING_PTR points to a variable
756 containing a pointer to the string to parse. That pointer
757 should point to the character after the \. That pointer
758 is updated past the characters we use. The value of the
759 escape sequence is returned.
760
761 A negative value means the sequence \ newline was seen,
762 which is supposed to be equivalent to nothing at all.
763
764 If \ is followed by a null character, we return a negative
765 value and leave the string pointer pointing at the null character.
766
767 If \ is followed by 000, we return 0 and leave the string pointer
768 after the zeros. A value of 0 does not mean end of string. */
769
770 int
771 parse_escape (string_ptr)
772 char **string_ptr;
773 {
774 register int c = *(*string_ptr)++;
775 switch (c)
776 {
777 case 'a':
778 return 007; /* Bell (alert) char */
779 case 'b':
780 return '\b';
781 case 'e': /* Escape character */
782 return 033;
783 case 'f':
784 return '\f';
785 case 'n':
786 return '\n';
787 case 'r':
788 return '\r';
789 case 't':
790 return '\t';
791 case 'v':
792 return '\v';
793 case '\n':
794 return -2;
795 case 0:
796 (*string_ptr)--;
797 return 0;
798 case '^':
799 c = *(*string_ptr)++;
800 if (c == '\\')
801 c = parse_escape (string_ptr);
802 if (c == '?')
803 return 0177;
804 return (c & 0200) | (c & 037);
805
806 case '0':
807 case '1':
808 case '2':
809 case '3':
810 case '4':
811 case '5':
812 case '6':
813 case '7':
814 {
815 register int i = c - '0';
816 register int count = 0;
817 while (++count < 3)
818 {
819 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
820 {
821 i *= 8;
822 i += c - '0';
823 }
824 else
825 {
826 (*string_ptr)--;
827 break;
828 }
829 }
830 return i;
831 }
832 default:
833 return c;
834 }
835 }
836 \f
837 /* Print the character C on STREAM as part of the contents of a literal
838 string whose delimiter is QUOTER. Note that this routine should only
839 be call for printing things which are independent of the language
840 of the program being debugged. */
841
842 void
843 gdb_printchar (c, stream, quoter)
844 register int c;
845 FILE *stream;
846 int quoter;
847 {
848
849 c &= 0xFF; /* Avoid sign bit follies */
850
851 if ( c < 0x20 || /* Low control chars */
852 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
853 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
854 switch (c)
855 {
856 case '\n':
857 fputs_filtered ("\\n", stream);
858 break;
859 case '\b':
860 fputs_filtered ("\\b", stream);
861 break;
862 case '\t':
863 fputs_filtered ("\\t", stream);
864 break;
865 case '\f':
866 fputs_filtered ("\\f", stream);
867 break;
868 case '\r':
869 fputs_filtered ("\\r", stream);
870 break;
871 case '\033':
872 fputs_filtered ("\\e", stream);
873 break;
874 case '\007':
875 fputs_filtered ("\\a", stream);
876 break;
877 default:
878 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
879 break;
880 }
881 } else {
882 if (c == '\\' || c == quoter)
883 fputs_filtered ("\\", stream);
884 fprintf_filtered (stream, "%c", c);
885 }
886 }
887 \f
888 /* Number of lines per page or UINT_MAX if paging is disabled. */
889 static unsigned int lines_per_page;
890 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
891 static unsigned int chars_per_line;
892 /* Current count of lines printed on this page, chars on this line. */
893 static unsigned int lines_printed, chars_printed;
894
895 /* Buffer and start column of buffered text, for doing smarter word-
896 wrapping. When someone calls wrap_here(), we start buffering output
897 that comes through fputs_filtered(). If we see a newline, we just
898 spit it out and forget about the wrap_here(). If we see another
899 wrap_here(), we spit it out and remember the newer one. If we see
900 the end of the line, we spit out a newline, the indent, and then
901 the buffered output.
902
903 wrap_column is the column number on the screen where wrap_buffer begins.
904 When wrap_column is zero, wrapping is not in effect.
905 wrap_buffer is malloc'd with chars_per_line+2 bytes.
906 When wrap_buffer[0] is null, the buffer is empty.
907 wrap_pointer points into it at the next character to fill.
908 wrap_indent is the string that should be used as indentation if the
909 wrap occurs. */
910
911 static char *wrap_buffer, *wrap_pointer, *wrap_indent;
912 static int wrap_column;
913
914 /* ARGSUSED */
915 static void
916 set_width_command (args, from_tty, c)
917 char *args;
918 int from_tty;
919 struct cmd_list_element *c;
920 {
921 if (!wrap_buffer)
922 {
923 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
924 wrap_buffer[0] = '\0';
925 }
926 else
927 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
928 wrap_pointer = wrap_buffer; /* Start it at the beginning */
929 }
930
931 /* Wait, so the user can read what's on the screen. Prompt the user
932 to continue by pressing RETURN. */
933
934 static void
935 prompt_for_continue ()
936 {
937 char *ignore;
938
939 /* We must do this *before* we call gdb_readline, else it will eventually
940 call us -- thinking that we're trying to print beyond the end of the
941 screen. */
942 reinitialize_more_filter ();
943
944 immediate_quit++;
945 ignore = gdb_readline ("---Type <return> to continue---");
946 if (ignore)
947 free (ignore);
948 immediate_quit--;
949
950 /* Now we have to do this again, so that GDB will know that it doesn't
951 need to save the ---Type <return>--- line at the top of the screen. */
952 reinitialize_more_filter ();
953
954 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
955 }
956
957 /* Reinitialize filter; ie. tell it to reset to original values. */
958
959 void
960 reinitialize_more_filter ()
961 {
962 lines_printed = 0;
963 chars_printed = 0;
964 }
965
966 /* Indicate that if the next sequence of characters overflows the line,
967 a newline should be inserted here rather than when it hits the end.
968 If INDENT is nonzero, it is a string to be printed to indent the
969 wrapped part on the next line. INDENT must remain accessible until
970 the next call to wrap_here() or until a newline is printed through
971 fputs_filtered().
972
973 If the line is already overfull, we immediately print a newline and
974 the indentation, and disable further wrapping.
975
976 If we don't know the width of lines, but we know the page height,
977 we must not wrap words, but should still keep track of newlines
978 that were explicitly printed.
979
980 INDENT should not contain tabs, as that
981 will mess up the char count on the next line. FIXME. */
982
983 void
984 wrap_here(indent)
985 char *indent;
986 {
987 if (wrap_buffer[0])
988 {
989 *wrap_pointer = '\0';
990 fputs (wrap_buffer, stdout);
991 }
992 wrap_pointer = wrap_buffer;
993 wrap_buffer[0] = '\0';
994 if (chars_per_line == UINT_MAX) /* No line overflow checking */
995 {
996 wrap_column = 0;
997 }
998 else if (chars_printed >= chars_per_line)
999 {
1000 puts_filtered ("\n");
1001 puts_filtered (indent);
1002 wrap_column = 0;
1003 }
1004 else
1005 {
1006 wrap_column = chars_printed;
1007 wrap_indent = indent;
1008 }
1009 }
1010
1011 /* Ensure that whatever gets printed next, using the filtered output
1012 commands, starts at the beginning of the line. I.E. if there is
1013 any pending output for the current line, flush it and start a new
1014 line. Otherwise do nothing. */
1015
1016 void
1017 begin_line ()
1018 {
1019 if (chars_printed > 0)
1020 {
1021 puts_filtered ("\n");
1022 }
1023 }
1024
1025 /* Like fputs but pause after every screenful, and can wrap at points
1026 other than the final character of a line.
1027 Unlike fputs, fputs_filtered does not return a value.
1028 It is OK for LINEBUFFER to be NULL, in which case just don't print
1029 anything.
1030
1031 Note that a longjmp to top level may occur in this routine
1032 (since prompt_for_continue may do so) so this routine should not be
1033 called when cleanups are not in place. */
1034
1035 void
1036 fputs_filtered (linebuffer, stream)
1037 const char *linebuffer;
1038 FILE *stream;
1039 {
1040 const char *lineptr;
1041
1042 if (linebuffer == 0)
1043 return;
1044
1045 /* Don't do any filtering if it is disabled. */
1046 if (stream != stdout
1047 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1048 {
1049 fputs (linebuffer, stream);
1050 return;
1051 }
1052
1053 /* Go through and output each character. Show line extension
1054 when this is necessary; prompt user for new page when this is
1055 necessary. */
1056
1057 lineptr = linebuffer;
1058 while (*lineptr)
1059 {
1060 /* Possible new page. */
1061 if (lines_printed >= lines_per_page - 1)
1062 prompt_for_continue ();
1063
1064 while (*lineptr && *lineptr != '\n')
1065 {
1066 /* Print a single line. */
1067 if (*lineptr == '\t')
1068 {
1069 if (wrap_column)
1070 *wrap_pointer++ = '\t';
1071 else
1072 putc ('\t', stream);
1073 /* Shifting right by 3 produces the number of tab stops
1074 we have already passed, and then adding one and
1075 shifting left 3 advances to the next tab stop. */
1076 chars_printed = ((chars_printed >> 3) + 1) << 3;
1077 lineptr++;
1078 }
1079 else
1080 {
1081 if (wrap_column)
1082 *wrap_pointer++ = *lineptr;
1083 else
1084 putc (*lineptr, stream);
1085 chars_printed++;
1086 lineptr++;
1087 }
1088
1089 if (chars_printed >= chars_per_line)
1090 {
1091 unsigned int save_chars = chars_printed;
1092
1093 chars_printed = 0;
1094 lines_printed++;
1095 /* If we aren't actually wrapping, don't output newline --
1096 if chars_per_line is right, we probably just overflowed
1097 anyway; if it's wrong, let us keep going. */
1098 if (wrap_column)
1099 putc ('\n', stream);
1100
1101 /* Possible new page. */
1102 if (lines_printed >= lines_per_page - 1)
1103 prompt_for_continue ();
1104
1105 /* Now output indentation and wrapped string */
1106 if (wrap_column)
1107 {
1108 if (wrap_indent)
1109 fputs (wrap_indent, stream);
1110 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1111 fputs (wrap_buffer, stream); /* and eject it */
1112 /* FIXME, this strlen is what prevents wrap_indent from
1113 containing tabs. However, if we recurse to print it
1114 and count its chars, we risk trouble if wrap_indent is
1115 longer than (the user settable) chars_per_line.
1116 Note also that this can set chars_printed > chars_per_line
1117 if we are printing a long string. */
1118 chars_printed = strlen (wrap_indent)
1119 + (save_chars - wrap_column);
1120 wrap_pointer = wrap_buffer; /* Reset buffer */
1121 wrap_buffer[0] = '\0';
1122 wrap_column = 0; /* And disable fancy wrap */
1123 }
1124 }
1125 }
1126
1127 if (*lineptr == '\n')
1128 {
1129 chars_printed = 0;
1130 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1131 lines_printed++;
1132 putc ('\n', stream);
1133 lineptr++;
1134 }
1135 }
1136 }
1137
1138
1139 /* fputs_demangled attempts to demangle NAME, a symbol in language LANG, using
1140 demangling args ARG_MODE, and print it filtered to STREAM. If the name is
1141 not mangled, or the language for the name is unknown, or demangling is off,
1142 the name is printed in its "raw" form. */
1143
1144 void
1145 fputs_demangled (name, stream, arg_mode, lang)
1146 char *name;
1147 FILE *stream;
1148 int arg_mode;
1149 enum language lang;
1150 {
1151 char *demangled;
1152
1153 if (name != NULL)
1154 {
1155 /* If user wants to see raw output, no problem. */
1156 if (!demangle)
1157 {
1158 fputs_filtered (name, stream);
1159 }
1160 else
1161 {
1162 switch (lang)
1163 {
1164 case language_cplus:
1165 demangled = cplus_demangle (name, arg_mode);
1166 break;
1167 /* start-sanitize-chill */
1168 case language_chill:
1169 demangled = chill_demangle (name);
1170 break;
1171 /* end-sanitize-chill */
1172 default:
1173 demangled = NULL;
1174 break;
1175 }
1176 fputs_filtered (demangled ? demangled : name, stream);
1177 if (demangled != NULL)
1178 {
1179 free (demangled);
1180 }
1181 }
1182 }
1183 }
1184
1185 /* Print a variable number of ARGS using format FORMAT. If this
1186 information is going to put the amount written (since the last call
1187 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1188 print out a pause message and do a gdb_readline to get the users
1189 permision to continue.
1190
1191 Unlike fprintf, this function does not return a value.
1192
1193 We implement three variants, vfprintf (takes a vararg list and stream),
1194 fprintf (takes a stream to write on), and printf (the usual).
1195
1196 Note that this routine has a restriction that the length of the
1197 final output line must be less than 255 characters *or* it must be
1198 less than twice the size of the format string. This is a very
1199 arbitrary restriction, but it is an internal restriction, so I'll
1200 put it in. This means that the %s format specifier is almost
1201 useless; unless the caller can GUARANTEE that the string is short
1202 enough, fputs_filtered should be used instead.
1203
1204 Note also that a longjmp to top level may occur in this routine
1205 (since prompt_for_continue may do so) so this routine should not be
1206 called when cleanups are not in place. */
1207
1208 #define MIN_LINEBUF 255
1209
1210 void
1211 vfprintf_filtered (stream, format, args)
1212 FILE *stream;
1213 char *format;
1214 va_list args;
1215 {
1216 char line_buf[MIN_LINEBUF+10];
1217 char *linebuffer = line_buf;
1218 int format_length;
1219
1220 format_length = strlen (format);
1221
1222 /* Reallocate buffer to a larger size if this is necessary. */
1223 if (format_length * 2 > MIN_LINEBUF)
1224 {
1225 linebuffer = alloca (10 + format_length * 2);
1226 }
1227
1228 /* This won't blow up if the restrictions described above are
1229 followed. */
1230 vsprintf (linebuffer, format, args);
1231
1232 fputs_filtered (linebuffer, stream);
1233 }
1234
1235 void
1236 vprintf_filtered (format, args)
1237 char *format;
1238 va_list args;
1239 {
1240 vfprintf_filtered (stdout, format, args);
1241 }
1242
1243 /* VARARGS */
1244 void
1245 fprintf_filtered (va_alist)
1246 va_dcl
1247 {
1248 va_list args;
1249 FILE *stream;
1250 char *format;
1251
1252 va_start (args);
1253 stream = va_arg (args, FILE *);
1254 format = va_arg (args, char *);
1255
1256 /* This won't blow up if the restrictions described above are
1257 followed. */
1258 vfprintf_filtered (stream, format, args);
1259 va_end (args);
1260 }
1261
1262 /* Like fprintf_filtered, but prints it's result indent.
1263 Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */
1264
1265 /* VARARGS */
1266 void
1267 fprintfi_filtered (va_alist)
1268 va_dcl
1269 {
1270 va_list args;
1271 int spaces;
1272 FILE *stream;
1273 char *format;
1274
1275 va_start (args);
1276 spaces = va_arg (args, int);
1277 stream = va_arg (args, FILE *);
1278 format = va_arg (args, char *);
1279 print_spaces_filtered (spaces, stream);
1280
1281 /* This won't blow up if the restrictions described above are
1282 followed. */
1283 vfprintf_filtered (stream, format, args);
1284 va_end (args);
1285 }
1286
1287 /* VARARGS */
1288 void
1289 printf_filtered (va_alist)
1290 va_dcl
1291 {
1292 va_list args;
1293 char *format;
1294
1295 va_start (args);
1296 format = va_arg (args, char *);
1297
1298 vfprintf_filtered (stdout, format, args);
1299 va_end (args);
1300 }
1301
1302 /* Like printf_filtered, but prints it's result indented.
1303 Called as printfi_filtered (spaces, format, arg1, arg2, ...); */
1304
1305 /* VARARGS */
1306 void
1307 printfi_filtered (va_alist)
1308 va_dcl
1309 {
1310 va_list args;
1311 int spaces;
1312 char *format;
1313
1314 va_start (args);
1315 spaces = va_arg (args, int);
1316 format = va_arg (args, char *);
1317 print_spaces_filtered (spaces, stdout);
1318 vfprintf_filtered (stdout, format, args);
1319 va_end (args);
1320 }
1321
1322 /* Easy -- but watch out!
1323
1324 This routine is *not* a replacement for puts()! puts() appends a newline.
1325 This one doesn't, and had better not! */
1326
1327 void
1328 puts_filtered (string)
1329 char *string;
1330 {
1331 fputs_filtered (string, stdout);
1332 }
1333
1334 /* Return a pointer to N spaces and a null. The pointer is good
1335 until the next call to here. */
1336 char *
1337 n_spaces (n)
1338 int n;
1339 {
1340 register char *t;
1341 static char *spaces;
1342 static int max_spaces;
1343
1344 if (n > max_spaces)
1345 {
1346 if (spaces)
1347 free (spaces);
1348 spaces = (char *) xmalloc (n+1);
1349 for (t = spaces+n; t != spaces;)
1350 *--t = ' ';
1351 spaces[n] = '\0';
1352 max_spaces = n;
1353 }
1354
1355 return spaces + max_spaces - n;
1356 }
1357
1358 /* Print N spaces. */
1359 void
1360 print_spaces_filtered (n, stream)
1361 int n;
1362 FILE *stream;
1363 {
1364 fputs_filtered (n_spaces (n), stream);
1365 }
1366 \f
1367 /* C++ demangler stuff. */
1368
1369 /* Print NAME on STREAM, demangling if necessary. */
1370 void
1371 fprint_symbol (stream, name)
1372 FILE *stream;
1373 char *name;
1374 {
1375 char *demangled = NULL;
1376
1377 if (demangle)
1378 {
1379 /* Lacking a better method of knowing what demangler to use, pick
1380 one appropriate for whatever the current language is. (FIXME) */
1381 switch (current_language -> la_language)
1382 {
1383 case language_cplus:
1384 demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI);
1385 break;
1386 /* start-sanitize-chill */
1387 case language_chill:
1388 demangled = chill_demangle (name);
1389 break;
1390 /* end-sanitize-chill */
1391 }
1392 }
1393 if (demangled == NULL)
1394 {
1395 fputs_filtered (name, stream);
1396 }
1397 else
1398 {
1399 fputs_filtered (demangled, stream);
1400 free (demangled);
1401 }
1402 }
1403
1404 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1405 differences in whitespace. Returns 0 if they match, non-zero if they
1406 don't (slightly different than strcmp()'s range of return values).
1407
1408 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1409 This "feature" is useful when searching for matching C++ function names
1410 (such as if the user types 'break FOO', where FOO is a mangled C++
1411 function). */
1412
1413 int
1414 strcmp_iw (string1, string2)
1415 const char *string1;
1416 const char *string2;
1417 {
1418 while ((*string1 != '\0') && (*string2 != '\0'))
1419 {
1420 while (isspace (*string1))
1421 {
1422 string1++;
1423 }
1424 while (isspace (*string2))
1425 {
1426 string2++;
1427 }
1428 if (*string1 != *string2)
1429 {
1430 break;
1431 }
1432 if (*string1 != '\0')
1433 {
1434 string1++;
1435 string2++;
1436 }
1437 }
1438 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1439 }
1440
1441 \f
1442 void
1443 _initialize_utils ()
1444 {
1445 struct cmd_list_element *c;
1446
1447 c = add_set_cmd ("width", class_support, var_uinteger,
1448 (char *)&chars_per_line,
1449 "Set number of characters gdb thinks are in a line.",
1450 &setlist);
1451 add_show_from_set (c, &showlist);
1452 c->function.sfunc = set_width_command;
1453
1454 add_show_from_set
1455 (add_set_cmd ("height", class_support,
1456 var_uinteger, (char *)&lines_per_page,
1457 "Set number of lines gdb thinks are in a page.", &setlist),
1458 &showlist);
1459
1460 /* These defaults will be used if we are unable to get the correct
1461 values from termcap. */
1462 #if defined(__GO32__)
1463 lines_per_page = ScreenRows();
1464 chars_per_line = ScreenCols();
1465 #else
1466 lines_per_page = 24;
1467 chars_per_line = 80;
1468 /* Initialize the screen height and width from termcap. */
1469 {
1470 char *termtype = getenv ("TERM");
1471
1472 /* Positive means success, nonpositive means failure. */
1473 int status;
1474
1475 /* 2048 is large enough for all known terminals, according to the
1476 GNU termcap manual. */
1477 char term_buffer[2048];
1478
1479 if (termtype)
1480 {
1481 status = tgetent (term_buffer, termtype);
1482 if (status > 0)
1483 {
1484 int val;
1485
1486 val = tgetnum ("li");
1487 if (val >= 0)
1488 lines_per_page = val;
1489 else
1490 /* The number of lines per page is not mentioned
1491 in the terminal description. This probably means
1492 that paging is not useful (e.g. emacs shell window),
1493 so disable paging. */
1494 lines_per_page = UINT_MAX;
1495
1496 val = tgetnum ("co");
1497 if (val >= 0)
1498 chars_per_line = val;
1499 }
1500 }
1501 }
1502
1503 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1504
1505 /* If there is a better way to determine the window size, use it. */
1506 SIGWINCH_HANDLER ();
1507 #endif
1508 #endif
1509 /* If the output is not a terminal, don't paginate it. */
1510 if (!ISATTY (stdout))
1511 lines_per_page = UINT_MAX;
1512
1513 set_width_command ((char *)NULL, 0, c);
1514
1515 add_show_from_set
1516 (add_set_cmd ("demangle", class_support, var_boolean,
1517 (char *)&demangle,
1518 "Set demangling of encoded C++ names when displaying symbols.",
1519 &setprintlist),
1520 &showprintlist);
1521
1522 add_show_from_set
1523 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1524 (char *)&sevenbit_strings,
1525 "Set printing of 8-bit characters in strings as \\nnn.",
1526 &setprintlist),
1527 &showprintlist);
1528
1529 add_show_from_set
1530 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1531 (char *)&asm_demangle,
1532 "Set demangling of C++ names in disassembly listings.",
1533 &setprintlist),
1534 &showprintlist);
1535 }
1536
1537 /* Machine specific function to handle SIGWINCH signal. */
1538
1539 #ifdef SIGWINCH_HANDLER_BODY
1540 SIGWINCH_HANDLER_BODY
1541 #endif
1542
This page took 0.060881 seconds and 4 git commands to generate.