* ChangeLog, Makefile.in, configure, configure.in, v850_sim.h,
[deliverable/binutils-gdb.git] / gdb / utils.c
1 /* General utility routines for GDB, the GNU debugger.
2 Copyright 1986, 1989, 1990, 1991, 1992, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "defs.h"
21 #if !defined(__GO32__) && !defined(__WIN32__) && !defined(MPW)
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
24 #include <pwd.h>
25 #endif
26 #ifdef ANSI_PROTOTYPES
27 #include <stdarg.h>
28 #else
29 #include <varargs.h>
30 #endif
31 #include <ctype.h>
32 #include "gdb_string.h"
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include "signals.h"
38 #include "gdbcmd.h"
39 #include "serial.h"
40 #include "bfd.h"
41 #include "target.h"
42 #include "demangle.h"
43 #include "expression.h"
44 #include "language.h"
45 #include "annotate.h"
46
47 #include "readline.h"
48
49 /* readline defines this. */
50 #undef savestring
51
52 /* Prototypes for local functions */
53
54 static void vfprintf_maybe_filtered PARAMS ((FILE *, const char *, va_list, int));
55
56 static void fputs_maybe_filtered PARAMS ((const char *, FILE *, int));
57
58 #if !defined (NO_MMALLOC) && !defined (NO_MMCHECK)
59 static void malloc_botch PARAMS ((void));
60 #endif
61
62 static void
63 fatal_dump_core PARAMS((char *, ...));
64
65 static void
66 prompt_for_continue PARAMS ((void));
67
68 static void
69 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
70
71 /* If this definition isn't overridden by the header files, assume
72 that isatty and fileno exist on this system. */
73 #ifndef ISATTY
74 #define ISATTY(FP) (isatty (fileno (FP)))
75 #endif
76
77 /* Chain of cleanup actions established with make_cleanup,
78 to be executed if an error happens. */
79
80 static struct cleanup *cleanup_chain;
81
82 /* Nonzero if we have job control. */
83
84 int job_control;
85
86 /* Nonzero means a quit has been requested. */
87
88 int quit_flag;
89
90 /* Nonzero means quit immediately if Control-C is typed now, rather
91 than waiting until QUIT is executed. Be careful in setting this;
92 code which executes with immediate_quit set has to be very careful
93 about being able to deal with being interrupted at any time. It is
94 almost always better to use QUIT; the only exception I can think of
95 is being able to quit out of a system call (using EINTR loses if
96 the SIGINT happens between the previous QUIT and the system call).
97 To immediately quit in the case in which a SIGINT happens between
98 the previous QUIT and setting immediate_quit (desirable anytime we
99 expect to block), call QUIT after setting immediate_quit. */
100
101 int immediate_quit;
102
103 /* Nonzero means that encoded C++ names should be printed out in their
104 C++ form rather than raw. */
105
106 int demangle = 1;
107
108 /* Nonzero means that encoded C++ names should be printed out in their
109 C++ form even in assembler language displays. If this is set, but
110 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
111
112 int asm_demangle = 0;
113
114 /* Nonzero means that strings with character values >0x7F should be printed
115 as octal escapes. Zero means just print the value (e.g. it's an
116 international character, and the terminal or window can cope.) */
117
118 int sevenbit_strings = 0;
119
120 /* String to be printed before error messages, if any. */
121
122 char *error_pre_print;
123
124 /* String to be printed before quit messages, if any. */
125
126 char *quit_pre_print;
127
128 /* String to be printed before warning messages, if any. */
129
130 char *warning_pre_print = "\nwarning: ";
131 \f
132 /* Add a new cleanup to the cleanup_chain,
133 and return the previous chain pointer
134 to be passed later to do_cleanups or discard_cleanups.
135 Args are FUNCTION to clean up with, and ARG to pass to it. */
136
137 struct cleanup *
138 make_cleanup (function, arg)
139 void (*function) PARAMS ((PTR));
140 PTR arg;
141 {
142 register struct cleanup *new
143 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
144 register struct cleanup *old_chain = cleanup_chain;
145
146 new->next = cleanup_chain;
147 new->function = function;
148 new->arg = arg;
149 cleanup_chain = new;
150
151 return old_chain;
152 }
153
154 /* Discard cleanups and do the actions they describe
155 until we get back to the point OLD_CHAIN in the cleanup_chain. */
156
157 void
158 do_cleanups (old_chain)
159 register struct cleanup *old_chain;
160 {
161 register struct cleanup *ptr;
162 while ((ptr = cleanup_chain) != old_chain)
163 {
164 cleanup_chain = ptr->next; /* Do this first incase recursion */
165 (*ptr->function) (ptr->arg);
166 free (ptr);
167 }
168 }
169
170 /* Discard cleanups, not doing the actions they describe,
171 until we get back to the point OLD_CHAIN in the cleanup_chain. */
172
173 void
174 discard_cleanups (old_chain)
175 register struct cleanup *old_chain;
176 {
177 register struct cleanup *ptr;
178 while ((ptr = cleanup_chain) != old_chain)
179 {
180 cleanup_chain = ptr->next;
181 free ((PTR)ptr);
182 }
183 }
184
185 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
186 struct cleanup *
187 save_cleanups ()
188 {
189 struct cleanup *old_chain = cleanup_chain;
190
191 cleanup_chain = 0;
192 return old_chain;
193 }
194
195 /* Restore the cleanup chain from a previously saved chain. */
196 void
197 restore_cleanups (chain)
198 struct cleanup *chain;
199 {
200 cleanup_chain = chain;
201 }
202
203 /* This function is useful for cleanups.
204 Do
205
206 foo = xmalloc (...);
207 old_chain = make_cleanup (free_current_contents, &foo);
208
209 to arrange to free the object thus allocated. */
210
211 void
212 free_current_contents (location)
213 char **location;
214 {
215 free (*location);
216 }
217
218 /* Provide a known function that does nothing, to use as a base for
219 for a possibly long chain of cleanups. This is useful where we
220 use the cleanup chain for handling normal cleanups as well as dealing
221 with cleanups that need to be done as a result of a call to error().
222 In such cases, we may not be certain where the first cleanup is, unless
223 we have a do-nothing one to always use as the base. */
224
225 /* ARGSUSED */
226 void
227 null_cleanup (arg)
228 PTR arg;
229 {
230 }
231
232 \f
233 /* Print a warning message. Way to use this is to call warning_begin,
234 output the warning message (use unfiltered output to gdb_stderr),
235 ending in a newline. There is not currently a warning_end that you
236 call afterwards, but such a thing might be added if it is useful
237 for a GUI to separate warning messages from other output.
238
239 FIXME: Why do warnings use unfiltered output and errors filtered?
240 Is this anything other than a historical accident? */
241
242 void
243 warning_begin ()
244 {
245 target_terminal_ours ();
246 wrap_here(""); /* Force out any buffered output */
247 gdb_flush (gdb_stdout);
248 if (warning_pre_print)
249 fprintf_unfiltered (gdb_stderr, warning_pre_print);
250 }
251
252 /* Print a warning message.
253 The first argument STRING is the warning message, used as a fprintf string,
254 and the remaining args are passed as arguments to it.
255 The primary difference between warnings and errors is that a warning
256 does not force the return to command level. */
257
258 /* VARARGS */
259 void
260 #ifdef ANSI_PROTOTYPES
261 warning (char *string, ...)
262 #else
263 warning (va_alist)
264 va_dcl
265 #endif
266 {
267 va_list args;
268 #ifdef ANSI_PROTOTYPES
269 va_start (args, string);
270 #else
271 char *string;
272
273 va_start (args);
274 string = va_arg (args, char *);
275 #endif
276 warning_begin ();
277 vfprintf_unfiltered (gdb_stderr, string, args);
278 fprintf_unfiltered (gdb_stderr, "\n");
279 va_end (args);
280 }
281
282 /* Start the printing of an error message. Way to use this is to call
283 this, output the error message (use filtered output to gdb_stderr
284 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
285 in a newline, and then call return_to_top_level (RETURN_ERROR).
286 error() provides a convenient way to do this for the special case
287 that the error message can be formatted with a single printf call,
288 but this is more general. */
289 void
290 error_begin ()
291 {
292 target_terminal_ours ();
293 wrap_here (""); /* Force out any buffered output */
294 gdb_flush (gdb_stdout);
295
296 annotate_error_begin ();
297
298 if (error_pre_print)
299 fprintf_filtered (gdb_stderr, error_pre_print);
300 }
301
302 /* Print an error message and return to command level.
303 The first argument STRING is the error message, used as a fprintf string,
304 and the remaining args are passed as arguments to it. */
305
306 #ifdef ANSI_PROTOTYPES
307 NORETURN void
308 error (char *string, ...)
309 #else
310 void
311 error (va_alist)
312 va_dcl
313 #endif
314 {
315 va_list args;
316 #ifdef ANSI_PROTOTYPES
317 va_start (args, string);
318 #else
319 va_start (args);
320 #endif
321 if (error_hook)
322 (*error_hook) ();
323 else
324 {
325 error_begin ();
326 #ifdef ANSI_PROTOTYPES
327 vfprintf_filtered (gdb_stderr, string, args);
328 #else
329 {
330 char *string1;
331
332 string1 = va_arg (args, char *);
333 vfprintf_filtered (gdb_stderr, string1, args);
334 }
335 #endif
336 fprintf_filtered (gdb_stderr, "\n");
337 va_end (args);
338 return_to_top_level (RETURN_ERROR);
339 }
340 }
341
342
343 /* Print an error message and exit reporting failure.
344 This is for a error that we cannot continue from.
345 The arguments are printed a la printf.
346
347 This function cannot be declared volatile (NORETURN) in an
348 ANSI environment because exit() is not declared volatile. */
349
350 /* VARARGS */
351 NORETURN void
352 #ifdef ANSI_PROTOTYPES
353 fatal (char *string, ...)
354 #else
355 fatal (va_alist)
356 va_dcl
357 #endif
358 {
359 va_list args;
360 #ifdef ANSI_PROTOTYPES
361 va_start (args, string);
362 #else
363 char *string;
364 va_start (args);
365 string = va_arg (args, char *);
366 #endif
367 fprintf_unfiltered (gdb_stderr, "\ngdb: ");
368 vfprintf_unfiltered (gdb_stderr, string, args);
369 fprintf_unfiltered (gdb_stderr, "\n");
370 va_end (args);
371 exit (1);
372 }
373
374 /* Print an error message and exit, dumping core.
375 The arguments are printed a la printf (). */
376
377 /* VARARGS */
378 static void
379 #ifdef ANSI_PROTOTYPES
380 fatal_dump_core (char *string, ...)
381 #else
382 fatal_dump_core (va_alist)
383 va_dcl
384 #endif
385 {
386 va_list args;
387 #ifdef ANSI_PROTOTYPES
388 va_start (args, string);
389 #else
390 char *string;
391
392 va_start (args);
393 string = va_arg (args, char *);
394 #endif
395 /* "internal error" is always correct, since GDB should never dump
396 core, no matter what the input. */
397 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: ");
398 vfprintf_unfiltered (gdb_stderr, string, args);
399 fprintf_unfiltered (gdb_stderr, "\n");
400 va_end (args);
401
402 signal (SIGQUIT, SIG_DFL);
403 kill (getpid (), SIGQUIT);
404 /* We should never get here, but just in case... */
405 exit (1);
406 }
407
408 /* The strerror() function can return NULL for errno values that are
409 out of range. Provide a "safe" version that always returns a
410 printable string. */
411
412 char *
413 safe_strerror (errnum)
414 int errnum;
415 {
416 char *msg;
417 static char buf[32];
418
419 if ((msg = strerror (errnum)) == NULL)
420 {
421 sprintf (buf, "(undocumented errno %d)", errnum);
422 msg = buf;
423 }
424 return (msg);
425 }
426
427 /* The strsignal() function can return NULL for signal values that are
428 out of range. Provide a "safe" version that always returns a
429 printable string. */
430
431 char *
432 safe_strsignal (signo)
433 int signo;
434 {
435 char *msg;
436 static char buf[32];
437
438 if ((msg = strsignal (signo)) == NULL)
439 {
440 sprintf (buf, "(undocumented signal %d)", signo);
441 msg = buf;
442 }
443 return (msg);
444 }
445
446
447 /* Print the system error message for errno, and also mention STRING
448 as the file name for which the error was encountered.
449 Then return to command level. */
450
451 void
452 perror_with_name (string)
453 char *string;
454 {
455 char *err;
456 char *combined;
457
458 err = safe_strerror (errno);
459 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
460 strcpy (combined, string);
461 strcat (combined, ": ");
462 strcat (combined, err);
463
464 /* I understand setting these is a matter of taste. Still, some people
465 may clear errno but not know about bfd_error. Doing this here is not
466 unreasonable. */
467 bfd_set_error (bfd_error_no_error);
468 errno = 0;
469
470 error ("%s.", combined);
471 }
472
473 /* Print the system error message for ERRCODE, and also mention STRING
474 as the file name for which the error was encountered. */
475
476 void
477 print_sys_errmsg (string, errcode)
478 char *string;
479 int errcode;
480 {
481 char *err;
482 char *combined;
483
484 err = safe_strerror (errcode);
485 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
486 strcpy (combined, string);
487 strcat (combined, ": ");
488 strcat (combined, err);
489
490 /* We want anything which was printed on stdout to come out first, before
491 this message. */
492 gdb_flush (gdb_stdout);
493 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
494 }
495
496 /* Control C eventually causes this to be called, at a convenient time. */
497
498 void
499 quit ()
500 {
501 serial_t gdb_stdout_serial = serial_fdopen (1);
502
503 target_terminal_ours ();
504
505 /* We want all output to appear now, before we print "Quit". We
506 have 3 levels of buffering we have to flush (it's possible that
507 some of these should be changed to flush the lower-level ones
508 too): */
509
510 /* 1. The _filtered buffer. */
511 wrap_here ((char *)0);
512
513 /* 2. The stdio buffer. */
514 gdb_flush (gdb_stdout);
515 gdb_flush (gdb_stderr);
516
517 /* 3. The system-level buffer. */
518 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial);
519 SERIAL_UN_FDOPEN (gdb_stdout_serial);
520
521 annotate_error_begin ();
522
523 /* Don't use *_filtered; we don't want to prompt the user to continue. */
524 if (quit_pre_print)
525 fprintf_unfiltered (gdb_stderr, quit_pre_print);
526
527 if (job_control
528 /* If there is no terminal switching for this target, then we can't
529 possibly get screwed by the lack of job control. */
530 || current_target.to_terminal_ours == NULL)
531 fprintf_unfiltered (gdb_stderr, "Quit\n");
532 else
533 fprintf_unfiltered (gdb_stderr,
534 "Quit (expect signal SIGINT when the program is resumed)\n");
535 return_to_top_level (RETURN_QUIT);
536 }
537
538
539 #if defined(__GO32__)||defined(WINGDB)
540
541 /* In the absence of signals, poll keyboard for a quit.
542 Called from #define QUIT pollquit() in xm-go32.h. */
543
544 void
545 pollquit()
546 {
547 if (kbhit ())
548 {
549 int k = getkey ();
550 if (k == 1) {
551 quit_flag = 1;
552 quit();
553 }
554 else if (k == 2) {
555 immediate_quit = 1;
556 quit ();
557 }
558 else
559 {
560 /* We just ignore it */
561 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
562 }
563 }
564 }
565
566
567 #endif
568 #if defined(__GO32__)||defined(WINGDB)
569 void notice_quit()
570 {
571 if (kbhit ())
572 {
573 int k = getkey ();
574 if (k == 1) {
575 quit_flag = 1;
576 }
577 else if (k == 2)
578 {
579 immediate_quit = 1;
580 }
581 else
582 {
583 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
584 }
585 }
586 }
587 #else
588 void notice_quit()
589 {
590 /* Done by signals */
591 }
592 #endif
593 /* Control C comes here */
594
595 void
596 request_quit (signo)
597 int signo;
598 {
599 quit_flag = 1;
600 /* Restore the signal handler. Harmless with BSD-style signals, needed
601 for System V-style signals. So just always do it, rather than worrying
602 about USG defines and stuff like that. */
603 signal (signo, request_quit);
604
605 /* start-sanitize-gm */
606 #ifdef GENERAL_MAGIC
607 target_kill ();
608 #endif /* GENERAL_MAGIC */
609 /* end-sanitize-gm */
610
611 #ifdef REQUEST_QUIT
612 REQUEST_QUIT;
613 #else
614 if (immediate_quit)
615 quit ();
616 #endif
617 }
618
619 \f
620 /* Memory management stuff (malloc friends). */
621
622 #if defined (NO_MMALLOC)
623
624 /* Make a substitute size_t for non-ANSI compilers. */
625
626 #ifdef _AIX
627 #include <stddef.h>
628 #else /* Not AIX */
629 #ifndef __STDC__
630 #ifndef size_t
631 #define size_t unsigned int
632 #endif
633 #endif
634 #endif /* Not AIX */
635
636 PTR
637 mmalloc (md, size)
638 PTR md;
639 size_t size;
640 {
641 return malloc (size);
642 }
643
644 PTR
645 mrealloc (md, ptr, size)
646 PTR md;
647 PTR ptr;
648 size_t size;
649 {
650 if (ptr == 0) /* Guard against old realloc's */
651 return malloc (size);
652 else
653 return realloc (ptr, size);
654 }
655
656 void
657 mfree (md, ptr)
658 PTR md;
659 PTR ptr;
660 {
661 free (ptr);
662 }
663
664 #endif /* NO_MMALLOC */
665
666 #if defined (NO_MMALLOC) || defined (NO_MMCHECK)
667
668 void
669 init_malloc (md)
670 PTR md;
671 {
672 }
673
674 #else /* Have mmalloc and want corruption checking */
675
676 static void
677 malloc_botch ()
678 {
679 fatal_dump_core ("Memory corruption");
680 }
681
682 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
683 by MD, to detect memory corruption. Note that MD may be NULL to specify
684 the default heap that grows via sbrk.
685
686 Note that for freshly created regions, we must call mmcheckf prior to any
687 mallocs in the region. Otherwise, any region which was allocated prior to
688 installing the checking hooks, which is later reallocated or freed, will
689 fail the checks! The mmcheck function only allows initial hooks to be
690 installed before the first mmalloc. However, anytime after we have called
691 mmcheck the first time to install the checking hooks, we can call it again
692 to update the function pointer to the memory corruption handler.
693
694 Returns zero on failure, non-zero on success. */
695
696 #ifndef MMCHECK_FORCE
697 #define MMCHECK_FORCE 0
698 #endif
699
700 void
701 init_malloc (md)
702 PTR md;
703 {
704 if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE))
705 {
706 /* Don't use warning(), which relies on current_target being set
707 to something other than dummy_target, until after
708 initialize_all_files(). */
709
710 fprintf_unfiltered
711 (gdb_stderr, "warning: failed to install memory consistency checks; ");
712 fprintf_unfiltered
713 (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n");
714 }
715
716 mmtrace ();
717 }
718
719 #endif /* Have mmalloc and want corruption checking */
720
721 /* Called when a memory allocation fails, with the number of bytes of
722 memory requested in SIZE. */
723
724 NORETURN void
725 nomem (size)
726 long size;
727 {
728 if (size > 0)
729 {
730 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
731 }
732 else
733 {
734 fatal ("virtual memory exhausted.");
735 }
736 }
737
738 /* Like mmalloc but get error if no storage available, and protect against
739 the caller wanting to allocate zero bytes. Whether to return NULL for
740 a zero byte request, or translate the request into a request for one
741 byte of zero'd storage, is a religious issue. */
742
743 PTR
744 xmmalloc (md, size)
745 PTR md;
746 long size;
747 {
748 register PTR val;
749
750 if (size == 0)
751 {
752 val = NULL;
753 }
754 else if ((val = mmalloc (md, size)) == NULL)
755 {
756 nomem (size);
757 }
758 return (val);
759 }
760
761 /* Like mrealloc but get error if no storage available. */
762
763 PTR
764 xmrealloc (md, ptr, size)
765 PTR md;
766 PTR ptr;
767 long size;
768 {
769 register PTR val;
770
771 if (ptr != NULL)
772 {
773 val = mrealloc (md, ptr, size);
774 }
775 else
776 {
777 val = mmalloc (md, size);
778 }
779 if (val == NULL)
780 {
781 nomem (size);
782 }
783 return (val);
784 }
785
786 /* Like malloc but get error if no storage available, and protect against
787 the caller wanting to allocate zero bytes. */
788
789 PTR
790 xmalloc (size)
791 long size;
792 {
793 return (xmmalloc ((PTR) NULL, size));
794 }
795
796 /* Like mrealloc but get error if no storage available. */
797
798 PTR
799 xrealloc (ptr, size)
800 PTR ptr;
801 long size;
802 {
803 return (xmrealloc ((PTR) NULL, ptr, size));
804 }
805
806 \f
807 /* My replacement for the read system call.
808 Used like `read' but keeps going if `read' returns too soon. */
809
810 int
811 myread (desc, addr, len)
812 int desc;
813 char *addr;
814 int len;
815 {
816 register int val;
817 int orglen = len;
818
819 while (len > 0)
820 {
821 val = read (desc, addr, len);
822 if (val < 0)
823 return val;
824 if (val == 0)
825 return orglen - len;
826 len -= val;
827 addr += val;
828 }
829 return orglen;
830 }
831 \f
832 /* Make a copy of the string at PTR with SIZE characters
833 (and add a null character at the end in the copy).
834 Uses malloc to get the space. Returns the address of the copy. */
835
836 char *
837 savestring (ptr, size)
838 const char *ptr;
839 int size;
840 {
841 register char *p = (char *) xmalloc (size + 1);
842 memcpy (p, ptr, size);
843 p[size] = 0;
844 return p;
845 }
846
847 char *
848 msavestring (md, ptr, size)
849 PTR md;
850 const char *ptr;
851 int size;
852 {
853 register char *p = (char *) xmmalloc (md, size + 1);
854 memcpy (p, ptr, size);
855 p[size] = 0;
856 return p;
857 }
858
859 /* The "const" is so it compiles under DGUX (which prototypes strsave
860 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
861 Doesn't real strsave return NULL if out of memory? */
862 char *
863 strsave (ptr)
864 const char *ptr;
865 {
866 return savestring (ptr, strlen (ptr));
867 }
868
869 char *
870 mstrsave (md, ptr)
871 PTR md;
872 const char *ptr;
873 {
874 return (msavestring (md, ptr, strlen (ptr)));
875 }
876
877 void
878 print_spaces (n, file)
879 register int n;
880 register FILE *file;
881 {
882 while (n-- > 0)
883 fputc (' ', file);
884 }
885
886 /* Print a host address. */
887
888 void
889 gdb_print_address (addr, stream)
890 PTR addr;
891 GDB_FILE *stream;
892 {
893
894 /* We could use the %p conversion specifier to fprintf if we had any
895 way of knowing whether this host supports it. But the following
896 should work on the Alpha and on 32 bit machines. */
897
898 fprintf_filtered (stream, "0x%lx", (unsigned long)addr);
899 }
900
901 /* Ask user a y-or-n question and return 1 iff answer is yes.
902 Takes three args which are given to printf to print the question.
903 The first, a control string, should end in "? ".
904 It should not say how to answer, because we do that. */
905
906 /* VARARGS */
907 int
908 #ifdef ANSI_PROTOTYPES
909 query (char *ctlstr, ...)
910 #else
911 query (va_alist)
912 va_dcl
913 #endif
914 {
915 va_list args;
916 register int answer;
917 register int ans2;
918 int retval;
919
920 #ifdef ANSI_PROTOTYPES
921 va_start (args, ctlstr);
922 #else
923 char *ctlstr;
924 va_start (args);
925 ctlstr = va_arg (args, char *);
926 #endif
927
928 if (query_hook)
929 {
930 return query_hook (ctlstr, args);
931 }
932
933 /* Automatically answer "yes" if input is not from a terminal. */
934 if (!input_from_terminal_p ())
935 return 1;
936 #ifdef MPW
937 /* FIXME Automatically answer "yes" if called from MacGDB. */
938 if (mac_app)
939 return 1;
940 #endif /* MPW */
941
942 while (1)
943 {
944 wrap_here (""); /* Flush any buffered output */
945 gdb_flush (gdb_stdout);
946
947 if (annotation_level > 1)
948 printf_filtered ("\n\032\032pre-query\n");
949
950 vfprintf_filtered (gdb_stdout, ctlstr, args);
951 printf_filtered ("(y or n) ");
952
953 if (annotation_level > 1)
954 printf_filtered ("\n\032\032query\n");
955
956 #ifdef MPW
957 /* If not in MacGDB, move to a new line so the entered line doesn't
958 have a prompt on the front of it. */
959 if (!mac_app)
960 fputs_unfiltered ("\n", gdb_stdout);
961 #endif /* MPW */
962
963 gdb_flush (gdb_stdout);
964 answer = fgetc (stdin);
965 clearerr (stdin); /* in case of C-d */
966 if (answer == EOF) /* C-d */
967 {
968 retval = 1;
969 break;
970 }
971 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
972 do
973 {
974 ans2 = fgetc (stdin);
975 clearerr (stdin);
976 }
977 while (ans2 != EOF && ans2 != '\n');
978 if (answer >= 'a')
979 answer -= 040;
980 if (answer == 'Y')
981 {
982 retval = 1;
983 break;
984 }
985 if (answer == 'N')
986 {
987 retval = 0;
988 break;
989 }
990 printf_filtered ("Please answer y or n.\n");
991 }
992
993 if (annotation_level > 1)
994 printf_filtered ("\n\032\032post-query\n");
995 return retval;
996 }
997
998 \f
999 /* Parse a C escape sequence. STRING_PTR points to a variable
1000 containing a pointer to the string to parse. That pointer
1001 should point to the character after the \. That pointer
1002 is updated past the characters we use. The value of the
1003 escape sequence is returned.
1004
1005 A negative value means the sequence \ newline was seen,
1006 which is supposed to be equivalent to nothing at all.
1007
1008 If \ is followed by a null character, we return a negative
1009 value and leave the string pointer pointing at the null character.
1010
1011 If \ is followed by 000, we return 0 and leave the string pointer
1012 after the zeros. A value of 0 does not mean end of string. */
1013
1014 int
1015 parse_escape (string_ptr)
1016 char **string_ptr;
1017 {
1018 register int c = *(*string_ptr)++;
1019 switch (c)
1020 {
1021 case 'a':
1022 return 007; /* Bell (alert) char */
1023 case 'b':
1024 return '\b';
1025 case 'e': /* Escape character */
1026 return 033;
1027 case 'f':
1028 return '\f';
1029 case 'n':
1030 return '\n';
1031 case 'r':
1032 return '\r';
1033 case 't':
1034 return '\t';
1035 case 'v':
1036 return '\v';
1037 case '\n':
1038 return -2;
1039 case 0:
1040 (*string_ptr)--;
1041 return 0;
1042 case '^':
1043 c = *(*string_ptr)++;
1044 if (c == '\\')
1045 c = parse_escape (string_ptr);
1046 if (c == '?')
1047 return 0177;
1048 return (c & 0200) | (c & 037);
1049
1050 case '0':
1051 case '1':
1052 case '2':
1053 case '3':
1054 case '4':
1055 case '5':
1056 case '6':
1057 case '7':
1058 {
1059 register int i = c - '0';
1060 register int count = 0;
1061 while (++count < 3)
1062 {
1063 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
1064 {
1065 i *= 8;
1066 i += c - '0';
1067 }
1068 else
1069 {
1070 (*string_ptr)--;
1071 break;
1072 }
1073 }
1074 return i;
1075 }
1076 default:
1077 return c;
1078 }
1079 }
1080 \f
1081 /* Print the character C on STREAM as part of the contents of a literal
1082 string whose delimiter is QUOTER. Note that this routine should only
1083 be call for printing things which are independent of the language
1084 of the program being debugged. */
1085
1086 void
1087 gdb_printchar (c, stream, quoter)
1088 register int c;
1089 FILE *stream;
1090 int quoter;
1091 {
1092
1093 c &= 0xFF; /* Avoid sign bit follies */
1094
1095 if ( c < 0x20 || /* Low control chars */
1096 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1097 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
1098 switch (c)
1099 {
1100 case '\n':
1101 fputs_filtered ("\\n", stream);
1102 break;
1103 case '\b':
1104 fputs_filtered ("\\b", stream);
1105 break;
1106 case '\t':
1107 fputs_filtered ("\\t", stream);
1108 break;
1109 case '\f':
1110 fputs_filtered ("\\f", stream);
1111 break;
1112 case '\r':
1113 fputs_filtered ("\\r", stream);
1114 break;
1115 case '\033':
1116 fputs_filtered ("\\e", stream);
1117 break;
1118 case '\007':
1119 fputs_filtered ("\\a", stream);
1120 break;
1121 default:
1122 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
1123 break;
1124 }
1125 } else {
1126 if (c == '\\' || c == quoter)
1127 fputs_filtered ("\\", stream);
1128 fprintf_filtered (stream, "%c", c);
1129 }
1130 }
1131 \f
1132 /* Number of lines per page or UINT_MAX if paging is disabled. */
1133 static unsigned int lines_per_page;
1134 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1135 static unsigned int chars_per_line;
1136 /* Current count of lines printed on this page, chars on this line. */
1137 static unsigned int lines_printed, chars_printed;
1138
1139 /* Buffer and start column of buffered text, for doing smarter word-
1140 wrapping. When someone calls wrap_here(), we start buffering output
1141 that comes through fputs_filtered(). If we see a newline, we just
1142 spit it out and forget about the wrap_here(). If we see another
1143 wrap_here(), we spit it out and remember the newer one. If we see
1144 the end of the line, we spit out a newline, the indent, and then
1145 the buffered output. */
1146
1147 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1148 are waiting to be output (they have already been counted in chars_printed).
1149 When wrap_buffer[0] is null, the buffer is empty. */
1150 static char *wrap_buffer;
1151
1152 /* Pointer in wrap_buffer to the next character to fill. */
1153 static char *wrap_pointer;
1154
1155 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1156 is non-zero. */
1157 static char *wrap_indent;
1158
1159 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1160 is not in effect. */
1161 static int wrap_column;
1162
1163 /* ARGSUSED */
1164 static void
1165 set_width_command (args, from_tty, c)
1166 char *args;
1167 int from_tty;
1168 struct cmd_list_element *c;
1169 {
1170 if (!wrap_buffer)
1171 {
1172 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1173 wrap_buffer[0] = '\0';
1174 }
1175 else
1176 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1177 wrap_pointer = wrap_buffer; /* Start it at the beginning */
1178 }
1179
1180 /* Wait, so the user can read what's on the screen. Prompt the user
1181 to continue by pressing RETURN. */
1182
1183 static void
1184 prompt_for_continue ()
1185 {
1186 char *ignore;
1187 char cont_prompt[120];
1188
1189 if (annotation_level > 1)
1190 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1191
1192 strcpy (cont_prompt,
1193 "---Type <return> to continue, or q <return> to quit---");
1194 if (annotation_level > 1)
1195 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1196
1197 /* We must do this *before* we call gdb_readline, else it will eventually
1198 call us -- thinking that we're trying to print beyond the end of the
1199 screen. */
1200 reinitialize_more_filter ();
1201
1202 immediate_quit++;
1203 /* On a real operating system, the user can quit with SIGINT.
1204 But not on GO32.
1205
1206 'q' is provided on all systems so users don't have to change habits
1207 from system to system, and because telling them what to do in
1208 the prompt is more user-friendly than expecting them to think of
1209 SIGINT. */
1210 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1211 whereas control-C to gdb_readline will cause the user to get dumped
1212 out to DOS. */
1213 ignore = readline (cont_prompt);
1214
1215 if (annotation_level > 1)
1216 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1217
1218 if (ignore)
1219 {
1220 char *p = ignore;
1221 while (*p == ' ' || *p == '\t')
1222 ++p;
1223 if (p[0] == 'q')
1224 request_quit (SIGINT);
1225 free (ignore);
1226 }
1227 immediate_quit--;
1228
1229 /* Now we have to do this again, so that GDB will know that it doesn't
1230 need to save the ---Type <return>--- line at the top of the screen. */
1231 reinitialize_more_filter ();
1232
1233 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1234 }
1235
1236 /* Reinitialize filter; ie. tell it to reset to original values. */
1237
1238 void
1239 reinitialize_more_filter ()
1240 {
1241 lines_printed = 0;
1242 chars_printed = 0;
1243 }
1244
1245 /* Indicate that if the next sequence of characters overflows the line,
1246 a newline should be inserted here rather than when it hits the end.
1247 If INDENT is non-null, it is a string to be printed to indent the
1248 wrapped part on the next line. INDENT must remain accessible until
1249 the next call to wrap_here() or until a newline is printed through
1250 fputs_filtered().
1251
1252 If the line is already overfull, we immediately print a newline and
1253 the indentation, and disable further wrapping.
1254
1255 If we don't know the width of lines, but we know the page height,
1256 we must not wrap words, but should still keep track of newlines
1257 that were explicitly printed.
1258
1259 INDENT should not contain tabs, as that will mess up the char count
1260 on the next line. FIXME.
1261
1262 This routine is guaranteed to force out any output which has been
1263 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1264 used to force out output from the wrap_buffer. */
1265
1266 void
1267 wrap_here(indent)
1268 char *indent;
1269 {
1270 /* This should have been allocated, but be paranoid anyway. */
1271 if (!wrap_buffer)
1272 abort ();
1273
1274 if (wrap_buffer[0])
1275 {
1276 *wrap_pointer = '\0';
1277 fputs_unfiltered (wrap_buffer, gdb_stdout);
1278 }
1279 wrap_pointer = wrap_buffer;
1280 wrap_buffer[0] = '\0';
1281 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1282 {
1283 wrap_column = 0;
1284 }
1285 else if (chars_printed >= chars_per_line)
1286 {
1287 puts_filtered ("\n");
1288 if (indent != NULL)
1289 puts_filtered (indent);
1290 wrap_column = 0;
1291 }
1292 else
1293 {
1294 wrap_column = chars_printed;
1295 if (indent == NULL)
1296 wrap_indent = "";
1297 else
1298 wrap_indent = indent;
1299 }
1300 }
1301
1302 /* Ensure that whatever gets printed next, using the filtered output
1303 commands, starts at the beginning of the line. I.E. if there is
1304 any pending output for the current line, flush it and start a new
1305 line. Otherwise do nothing. */
1306
1307 void
1308 begin_line ()
1309 {
1310 if (chars_printed > 0)
1311 {
1312 puts_filtered ("\n");
1313 }
1314 }
1315
1316
1317 GDB_FILE *
1318 gdb_fopen (name, mode)
1319 char * name;
1320 char * mode;
1321 {
1322 return fopen (name, mode);
1323 }
1324
1325 void
1326 gdb_flush (stream)
1327 FILE *stream;
1328 {
1329 if (flush_hook)
1330 {
1331 flush_hook (stream);
1332 return;
1333 }
1334
1335 fflush (stream);
1336 }
1337
1338 /* Like fputs but if FILTER is true, pause after every screenful.
1339
1340 Regardless of FILTER can wrap at points other than the final
1341 character of a line.
1342
1343 Unlike fputs, fputs_maybe_filtered does not return a value.
1344 It is OK for LINEBUFFER to be NULL, in which case just don't print
1345 anything.
1346
1347 Note that a longjmp to top level may occur in this routine (only if
1348 FILTER is true) (since prompt_for_continue may do so) so this
1349 routine should not be called when cleanups are not in place. */
1350
1351 static void
1352 fputs_maybe_filtered (linebuffer, stream, filter)
1353 const char *linebuffer;
1354 FILE *stream;
1355 int filter;
1356 {
1357 const char *lineptr;
1358
1359 if (linebuffer == 0)
1360 return;
1361
1362 /* Don't do any filtering if it is disabled. */
1363 if (stream != gdb_stdout
1364 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1365 {
1366 fputs_unfiltered (linebuffer, stream);
1367 return;
1368 }
1369
1370 /* Go through and output each character. Show line extension
1371 when this is necessary; prompt user for new page when this is
1372 necessary. */
1373
1374 lineptr = linebuffer;
1375 while (*lineptr)
1376 {
1377 /* Possible new page. */
1378 if (filter &&
1379 (lines_printed >= lines_per_page - 1))
1380 prompt_for_continue ();
1381
1382 while (*lineptr && *lineptr != '\n')
1383 {
1384 /* Print a single line. */
1385 if (*lineptr == '\t')
1386 {
1387 if (wrap_column)
1388 *wrap_pointer++ = '\t';
1389 else
1390 fputc_unfiltered ('\t', stream);
1391 /* Shifting right by 3 produces the number of tab stops
1392 we have already passed, and then adding one and
1393 shifting left 3 advances to the next tab stop. */
1394 chars_printed = ((chars_printed >> 3) + 1) << 3;
1395 lineptr++;
1396 }
1397 else
1398 {
1399 if (wrap_column)
1400 *wrap_pointer++ = *lineptr;
1401 else
1402 fputc_unfiltered (*lineptr, stream);
1403 chars_printed++;
1404 lineptr++;
1405 }
1406
1407 if (chars_printed >= chars_per_line)
1408 {
1409 unsigned int save_chars = chars_printed;
1410
1411 chars_printed = 0;
1412 lines_printed++;
1413 /* If we aren't actually wrapping, don't output newline --
1414 if chars_per_line is right, we probably just overflowed
1415 anyway; if it's wrong, let us keep going. */
1416 if (wrap_column)
1417 fputc_unfiltered ('\n', stream);
1418
1419 /* Possible new page. */
1420 if (lines_printed >= lines_per_page - 1)
1421 prompt_for_continue ();
1422
1423 /* Now output indentation and wrapped string */
1424 if (wrap_column)
1425 {
1426 fputs_unfiltered (wrap_indent, stream);
1427 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1428 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
1429 /* FIXME, this strlen is what prevents wrap_indent from
1430 containing tabs. However, if we recurse to print it
1431 and count its chars, we risk trouble if wrap_indent is
1432 longer than (the user settable) chars_per_line.
1433 Note also that this can set chars_printed > chars_per_line
1434 if we are printing a long string. */
1435 chars_printed = strlen (wrap_indent)
1436 + (save_chars - wrap_column);
1437 wrap_pointer = wrap_buffer; /* Reset buffer */
1438 wrap_buffer[0] = '\0';
1439 wrap_column = 0; /* And disable fancy wrap */
1440 }
1441 }
1442 }
1443
1444 if (*lineptr == '\n')
1445 {
1446 chars_printed = 0;
1447 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1448 lines_printed++;
1449 fputc_unfiltered ('\n', stream);
1450 lineptr++;
1451 }
1452 }
1453 }
1454
1455 void
1456 fputs_filtered (linebuffer, stream)
1457 const char *linebuffer;
1458 FILE *stream;
1459 {
1460 fputs_maybe_filtered (linebuffer, stream, 1);
1461 }
1462
1463 int
1464 putchar_unfiltered (c)
1465 int c;
1466 {
1467 char buf[2];
1468
1469 buf[0] = c;
1470 buf[1] = 0;
1471 fputs_unfiltered (buf, gdb_stdout);
1472 return c;
1473 }
1474
1475 int
1476 fputc_unfiltered (c, stream)
1477 int c;
1478 FILE * stream;
1479 {
1480 char buf[2];
1481
1482 buf[0] = c;
1483 buf[1] = 0;
1484 fputs_unfiltered (buf, stream);
1485 return c;
1486 }
1487
1488
1489 /* Print a variable number of ARGS using format FORMAT. If this
1490 information is going to put the amount written (since the last call
1491 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1492 call prompt_for_continue to get the users permision to continue.
1493
1494 Unlike fprintf, this function does not return a value.
1495
1496 We implement three variants, vfprintf (takes a vararg list and stream),
1497 fprintf (takes a stream to write on), and printf (the usual).
1498
1499 Note also that a longjmp to top level may occur in this routine
1500 (since prompt_for_continue may do so) so this routine should not be
1501 called when cleanups are not in place. */
1502
1503 static void
1504 vfprintf_maybe_filtered (stream, format, args, filter)
1505 FILE *stream;
1506 const char *format;
1507 va_list args;
1508 int filter;
1509 {
1510 char *linebuffer;
1511 struct cleanup *old_cleanups;
1512
1513 vasprintf (&linebuffer, format, args);
1514 if (linebuffer == NULL)
1515 {
1516 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
1517 exit (1);
1518 }
1519 old_cleanups = make_cleanup (free, linebuffer);
1520 fputs_maybe_filtered (linebuffer, stream, filter);
1521 do_cleanups (old_cleanups);
1522 }
1523
1524
1525 void
1526 vfprintf_filtered (stream, format, args)
1527 FILE *stream;
1528 const char *format;
1529 va_list args;
1530 {
1531 vfprintf_maybe_filtered (stream, format, args, 1);
1532 }
1533
1534 void
1535 vfprintf_unfiltered (stream, format, args)
1536 FILE *stream;
1537 const char *format;
1538 va_list args;
1539 {
1540 char *linebuffer;
1541 struct cleanup *old_cleanups;
1542
1543 vasprintf (&linebuffer, format, args);
1544 if (linebuffer == NULL)
1545 {
1546 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
1547 exit (1);
1548 }
1549 old_cleanups = make_cleanup (free, linebuffer);
1550 fputs_unfiltered (linebuffer, stream);
1551 do_cleanups (old_cleanups);
1552 }
1553
1554 void
1555 vprintf_filtered (format, args)
1556 const char *format;
1557 va_list args;
1558 {
1559 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
1560 }
1561
1562 void
1563 vprintf_unfiltered (format, args)
1564 const char *format;
1565 va_list args;
1566 {
1567 vfprintf_unfiltered (gdb_stdout, format, args);
1568 }
1569
1570 /* VARARGS */
1571 void
1572 #ifdef ANSI_PROTOTYPES
1573 fprintf_filtered (FILE *stream, const char *format, ...)
1574 #else
1575 fprintf_filtered (va_alist)
1576 va_dcl
1577 #endif
1578 {
1579 va_list args;
1580 #ifdef ANSI_PROTOTYPES
1581 va_start (args, format);
1582 #else
1583 FILE *stream;
1584 char *format;
1585
1586 va_start (args);
1587 stream = va_arg (args, FILE *);
1588 format = va_arg (args, char *);
1589 #endif
1590 vfprintf_filtered (stream, format, args);
1591 va_end (args);
1592 }
1593
1594 /* VARARGS */
1595 void
1596 #ifdef ANSI_PROTOTYPES
1597 fprintf_unfiltered (FILE *stream, const char *format, ...)
1598 #else
1599 fprintf_unfiltered (va_alist)
1600 va_dcl
1601 #endif
1602 {
1603 va_list args;
1604 #ifdef ANSI_PROTOTYPES
1605 va_start (args, format);
1606 #else
1607 FILE *stream;
1608 char *format;
1609
1610 va_start (args);
1611 stream = va_arg (args, FILE *);
1612 format = va_arg (args, char *);
1613 #endif
1614 vfprintf_unfiltered (stream, format, args);
1615 va_end (args);
1616 }
1617
1618 /* Like fprintf_filtered, but prints its result indented.
1619 Called as fprintfi_filtered (spaces, stream, format, ...); */
1620
1621 /* VARARGS */
1622 void
1623 #ifdef ANSI_PROTOTYPES
1624 fprintfi_filtered (int spaces, FILE *stream, const char *format, ...)
1625 #else
1626 fprintfi_filtered (va_alist)
1627 va_dcl
1628 #endif
1629 {
1630 va_list args;
1631 #ifdef ANSI_PROTOTYPES
1632 va_start (args, format);
1633 #else
1634 int spaces;
1635 FILE *stream;
1636 char *format;
1637
1638 va_start (args);
1639 spaces = va_arg (args, int);
1640 stream = va_arg (args, FILE *);
1641 format = va_arg (args, char *);
1642 #endif
1643 print_spaces_filtered (spaces, stream);
1644
1645 vfprintf_filtered (stream, format, args);
1646 va_end (args);
1647 }
1648
1649
1650 /* VARARGS */
1651 void
1652 #ifdef ANSI_PROTOTYPES
1653 printf_filtered (const char *format, ...)
1654 #else
1655 printf_filtered (va_alist)
1656 va_dcl
1657 #endif
1658 {
1659 va_list args;
1660 #ifdef ANSI_PROTOTYPES
1661 va_start (args, format);
1662 #else
1663 char *format;
1664
1665 va_start (args);
1666 format = va_arg (args, char *);
1667 #endif
1668 vfprintf_filtered (gdb_stdout, format, args);
1669 va_end (args);
1670 }
1671
1672
1673 /* VARARGS */
1674 void
1675 #ifdef ANSI_PROTOTYPES
1676 printf_unfiltered (const char *format, ...)
1677 #else
1678 printf_unfiltered (va_alist)
1679 va_dcl
1680 #endif
1681 {
1682 va_list args;
1683 #ifdef ANSI_PROTOTYPES
1684 va_start (args, format);
1685 #else
1686 char *format;
1687
1688 va_start (args);
1689 format = va_arg (args, char *);
1690 #endif
1691 vfprintf_unfiltered (gdb_stdout, format, args);
1692 va_end (args);
1693 }
1694
1695 /* Like printf_filtered, but prints it's result indented.
1696 Called as printfi_filtered (spaces, format, ...); */
1697
1698 /* VARARGS */
1699 void
1700 #ifdef ANSI_PROTOTYPES
1701 printfi_filtered (int spaces, const char *format, ...)
1702 #else
1703 printfi_filtered (va_alist)
1704 va_dcl
1705 #endif
1706 {
1707 va_list args;
1708 #ifdef ANSI_PROTOTYPES
1709 va_start (args, format);
1710 #else
1711 int spaces;
1712 char *format;
1713
1714 va_start (args);
1715 spaces = va_arg (args, int);
1716 format = va_arg (args, char *);
1717 #endif
1718 print_spaces_filtered (spaces, gdb_stdout);
1719 vfprintf_filtered (gdb_stdout, format, args);
1720 va_end (args);
1721 }
1722
1723 /* Easy -- but watch out!
1724
1725 This routine is *not* a replacement for puts()! puts() appends a newline.
1726 This one doesn't, and had better not! */
1727
1728 void
1729 puts_filtered (string)
1730 const char *string;
1731 {
1732 fputs_filtered (string, gdb_stdout);
1733 }
1734
1735 void
1736 puts_unfiltered (string)
1737 const char *string;
1738 {
1739 fputs_unfiltered (string, gdb_stdout);
1740 }
1741
1742 /* Return a pointer to N spaces and a null. The pointer is good
1743 until the next call to here. */
1744 char *
1745 n_spaces (n)
1746 int n;
1747 {
1748 register char *t;
1749 static char *spaces;
1750 static int max_spaces;
1751
1752 if (n > max_spaces)
1753 {
1754 if (spaces)
1755 free (spaces);
1756 spaces = (char *) xmalloc (n+1);
1757 for (t = spaces+n; t != spaces;)
1758 *--t = ' ';
1759 spaces[n] = '\0';
1760 max_spaces = n;
1761 }
1762
1763 return spaces + max_spaces - n;
1764 }
1765
1766 /* Print N spaces. */
1767 void
1768 print_spaces_filtered (n, stream)
1769 int n;
1770 FILE *stream;
1771 {
1772 fputs_filtered (n_spaces (n), stream);
1773 }
1774 \f
1775 /* C++ demangler stuff. */
1776
1777 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1778 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1779 If the name is not mangled, or the language for the name is unknown, or
1780 demangling is off, the name is printed in its "raw" form. */
1781
1782 void
1783 fprintf_symbol_filtered (stream, name, lang, arg_mode)
1784 FILE *stream;
1785 char *name;
1786 enum language lang;
1787 int arg_mode;
1788 {
1789 char *demangled;
1790
1791 if (name != NULL)
1792 {
1793 /* If user wants to see raw output, no problem. */
1794 if (!demangle)
1795 {
1796 fputs_filtered (name, stream);
1797 }
1798 else
1799 {
1800 switch (lang)
1801 {
1802 case language_cplus:
1803 demangled = cplus_demangle (name, arg_mode);
1804 break;
1805 case language_chill:
1806 demangled = chill_demangle (name);
1807 break;
1808 default:
1809 demangled = NULL;
1810 break;
1811 }
1812 fputs_filtered (demangled ? demangled : name, stream);
1813 if (demangled != NULL)
1814 {
1815 free (demangled);
1816 }
1817 }
1818 }
1819 }
1820
1821 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1822 differences in whitespace. Returns 0 if they match, non-zero if they
1823 don't (slightly different than strcmp()'s range of return values).
1824
1825 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1826 This "feature" is useful when searching for matching C++ function names
1827 (such as if the user types 'break FOO', where FOO is a mangled C++
1828 function). */
1829
1830 int
1831 strcmp_iw (string1, string2)
1832 const char *string1;
1833 const char *string2;
1834 {
1835 while ((*string1 != '\0') && (*string2 != '\0'))
1836 {
1837 while (isspace (*string1))
1838 {
1839 string1++;
1840 }
1841 while (isspace (*string2))
1842 {
1843 string2++;
1844 }
1845 if (*string1 != *string2)
1846 {
1847 break;
1848 }
1849 if (*string1 != '\0')
1850 {
1851 string1++;
1852 string2++;
1853 }
1854 }
1855 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1856 }
1857
1858 \f
1859 void
1860 initialize_utils ()
1861 {
1862 struct cmd_list_element *c;
1863
1864 c = add_set_cmd ("width", class_support, var_uinteger,
1865 (char *)&chars_per_line,
1866 "Set number of characters gdb thinks are in a line.",
1867 &setlist);
1868 add_show_from_set (c, &showlist);
1869 c->function.sfunc = set_width_command;
1870
1871 add_show_from_set
1872 (add_set_cmd ("height", class_support,
1873 var_uinteger, (char *)&lines_per_page,
1874 "Set number of lines gdb thinks are in a page.", &setlist),
1875 &showlist);
1876
1877 /* These defaults will be used if we are unable to get the correct
1878 values from termcap. */
1879 #if defined(__GO32__) || defined(__WIN32__)
1880 lines_per_page = ScreenRows();
1881 chars_per_line = ScreenCols();
1882 #else
1883 lines_per_page = 24;
1884 chars_per_line = 80;
1885
1886 #ifndef MPW
1887 /* No termcap under MPW, although might be cool to do something
1888 by looking at worksheet or console window sizes. */
1889 /* Initialize the screen height and width from termcap. */
1890 {
1891 char *termtype = getenv ("TERM");
1892
1893 /* Positive means success, nonpositive means failure. */
1894 int status;
1895
1896 /* 2048 is large enough for all known terminals, according to the
1897 GNU termcap manual. */
1898 char term_buffer[2048];
1899
1900 if (termtype)
1901 {
1902 status = tgetent (term_buffer, termtype);
1903 if (status > 0)
1904 {
1905 int val;
1906
1907 val = tgetnum ("li");
1908 if (val >= 0)
1909 lines_per_page = val;
1910 else
1911 /* The number of lines per page is not mentioned
1912 in the terminal description. This probably means
1913 that paging is not useful (e.g. emacs shell window),
1914 so disable paging. */
1915 lines_per_page = UINT_MAX;
1916
1917 val = tgetnum ("co");
1918 if (val >= 0)
1919 chars_per_line = val;
1920 }
1921 }
1922 }
1923 #endif /* MPW */
1924
1925 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1926
1927 /* If there is a better way to determine the window size, use it. */
1928 SIGWINCH_HANDLER ();
1929 #endif
1930 #endif
1931 /* If the output is not a terminal, don't paginate it. */
1932 if (!ISATTY (gdb_stdout))
1933 lines_per_page = UINT_MAX;
1934
1935 set_width_command ((char *)NULL, 0, c);
1936
1937 add_show_from_set
1938 (add_set_cmd ("demangle", class_support, var_boolean,
1939 (char *)&demangle,
1940 "Set demangling of encoded C++ names when displaying symbols.",
1941 &setprintlist),
1942 &showprintlist);
1943
1944 add_show_from_set
1945 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1946 (char *)&sevenbit_strings,
1947 "Set printing of 8-bit characters in strings as \\nnn.",
1948 &setprintlist),
1949 &showprintlist);
1950
1951 add_show_from_set
1952 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1953 (char *)&asm_demangle,
1954 "Set demangling of C++ names in disassembly listings.",
1955 &setprintlist),
1956 &showprintlist);
1957 }
1958
1959 /* Machine specific function to handle SIGWINCH signal. */
1960
1961 #ifdef SIGWINCH_HANDLER_BODY
1962 SIGWINCH_HANDLER_BODY
1963 #endif
1964 \f
1965 /* Support for converting target fp numbers into host DOUBLEST format. */
1966
1967 /* XXX - This code should really be in libiberty/floatformat.c, however
1968 configuration issues with libiberty made this very difficult to do in the
1969 available time. */
1970
1971 #include "floatformat.h"
1972 #include <math.h> /* ldexp */
1973
1974 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
1975 going to bother with trying to muck around with whether it is defined in
1976 a system header, what we do if not, etc. */
1977 #define FLOATFORMAT_CHAR_BIT 8
1978
1979 static unsigned long get_field PARAMS ((unsigned char *,
1980 enum floatformat_byteorders,
1981 unsigned int,
1982 unsigned int,
1983 unsigned int));
1984
1985 /* Extract a field which starts at START and is LEN bytes long. DATA and
1986 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
1987 static unsigned long
1988 get_field (data, order, total_len, start, len)
1989 unsigned char *data;
1990 enum floatformat_byteorders order;
1991 unsigned int total_len;
1992 unsigned int start;
1993 unsigned int len;
1994 {
1995 unsigned long result;
1996 unsigned int cur_byte;
1997 int cur_bitshift;
1998
1999 /* Start at the least significant part of the field. */
2000 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
2001 if (order == floatformat_little)
2002 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
2003 cur_bitshift =
2004 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
2005 result = *(data + cur_byte) >> (-cur_bitshift);
2006 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2007 if (order == floatformat_little)
2008 ++cur_byte;
2009 else
2010 --cur_byte;
2011
2012 /* Move towards the most significant part of the field. */
2013 while (cur_bitshift < len)
2014 {
2015 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
2016 /* This is the last byte; zero out the bits which are not part of
2017 this field. */
2018 result |=
2019 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
2020 << cur_bitshift;
2021 else
2022 result |= *(data + cur_byte) << cur_bitshift;
2023 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2024 if (order == floatformat_little)
2025 ++cur_byte;
2026 else
2027 --cur_byte;
2028 }
2029 return result;
2030 }
2031
2032 /* Convert from FMT to a DOUBLEST.
2033 FROM is the address of the extended float.
2034 Store the DOUBLEST in *TO. */
2035
2036 void
2037 floatformat_to_doublest (fmt, from, to)
2038 const struct floatformat *fmt;
2039 char *from;
2040 DOUBLEST *to;
2041 {
2042 unsigned char *ufrom = (unsigned char *)from;
2043 DOUBLEST dto;
2044 long exponent;
2045 unsigned long mant;
2046 unsigned int mant_bits, mant_off;
2047 int mant_bits_left;
2048 int special_exponent; /* It's a NaN, denorm or zero */
2049
2050 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
2051 fmt->exp_start, fmt->exp_len);
2052 /* Note that if exponent indicates a NaN, we can't really do anything useful
2053 (not knowing if the host has NaN's, or how to build one). So it will
2054 end up as an infinity or something close; that is OK. */
2055
2056 mant_bits_left = fmt->man_len;
2057 mant_off = fmt->man_start;
2058 dto = 0.0;
2059
2060 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
2061
2062 /* Don't bias zero's, denorms or NaNs. */
2063 if (!special_exponent)
2064 exponent -= fmt->exp_bias;
2065
2066 /* Build the result algebraically. Might go infinite, underflow, etc;
2067 who cares. */
2068
2069 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
2070 increment the exponent by one to account for the integer bit. */
2071
2072 if (!special_exponent)
2073 if (fmt->intbit == floatformat_intbit_no)
2074 dto = ldexp (1.0, exponent);
2075 else
2076 exponent++;
2077
2078 while (mant_bits_left > 0)
2079 {
2080 mant_bits = min (mant_bits_left, 32);
2081
2082 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
2083 mant_off, mant_bits);
2084
2085 dto += ldexp ((double)mant, exponent - mant_bits);
2086 exponent -= mant_bits;
2087 mant_off += mant_bits;
2088 mant_bits_left -= mant_bits;
2089 }
2090
2091 /* Negate it if negative. */
2092 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
2093 dto = -dto;
2094 *to = dto;
2095 }
2096 \f
2097 static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
2098 unsigned int,
2099 unsigned int,
2100 unsigned int,
2101 unsigned long));
2102
2103 /* Set a field which starts at START and is LEN bytes long. DATA and
2104 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2105 static void
2106 put_field (data, order, total_len, start, len, stuff_to_put)
2107 unsigned char *data;
2108 enum floatformat_byteorders order;
2109 unsigned int total_len;
2110 unsigned int start;
2111 unsigned int len;
2112 unsigned long stuff_to_put;
2113 {
2114 unsigned int cur_byte;
2115 int cur_bitshift;
2116
2117 /* Start at the least significant part of the field. */
2118 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
2119 if (order == floatformat_little)
2120 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
2121 cur_bitshift =
2122 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
2123 *(data + cur_byte) &=
2124 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
2125 *(data + cur_byte) |=
2126 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
2127 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2128 if (order == floatformat_little)
2129 ++cur_byte;
2130 else
2131 --cur_byte;
2132
2133 /* Move towards the most significant part of the field. */
2134 while (cur_bitshift < len)
2135 {
2136 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
2137 {
2138 /* This is the last byte. */
2139 *(data + cur_byte) &=
2140 ~((1 << (len - cur_bitshift)) - 1);
2141 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
2142 }
2143 else
2144 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
2145 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
2146 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2147 if (order == floatformat_little)
2148 ++cur_byte;
2149 else
2150 --cur_byte;
2151 }
2152 }
2153
2154 #ifdef HAVE_LONG_DOUBLE
2155 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
2156 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
2157 frexp, but operates on the long double data type. */
2158
2159 static long double ldfrexp PARAMS ((long double value, int *eptr));
2160
2161 static long double
2162 ldfrexp (value, eptr)
2163 long double value;
2164 int *eptr;
2165 {
2166 long double tmp;
2167 int exp;
2168
2169 /* Unfortunately, there are no portable functions for extracting the exponent
2170 of a long double, so we have to do it iteratively by multiplying or dividing
2171 by two until the fraction is between 0.5 and 1.0. */
2172
2173 if (value < 0.0l)
2174 value = -value;
2175
2176 tmp = 1.0l;
2177 exp = 0;
2178
2179 if (value >= tmp) /* Value >= 1.0 */
2180 while (value >= tmp)
2181 {
2182 tmp *= 2.0l;
2183 exp++;
2184 }
2185 else if (value != 0.0l) /* Value < 1.0 and > 0.0 */
2186 {
2187 while (value < tmp)
2188 {
2189 tmp /= 2.0l;
2190 exp--;
2191 }
2192 tmp *= 2.0l;
2193 exp++;
2194 }
2195
2196 *eptr = exp;
2197 return value/tmp;
2198 }
2199 #endif /* HAVE_LONG_DOUBLE */
2200
2201
2202 /* The converse: convert the DOUBLEST *FROM to an extended float
2203 and store where TO points. Neither FROM nor TO have any alignment
2204 restrictions. */
2205
2206 void
2207 floatformat_from_doublest (fmt, from, to)
2208 CONST struct floatformat *fmt;
2209 DOUBLEST *from;
2210 char *to;
2211 {
2212 DOUBLEST dfrom;
2213 int exponent;
2214 DOUBLEST mant;
2215 unsigned int mant_bits, mant_off;
2216 int mant_bits_left;
2217 unsigned char *uto = (unsigned char *)to;
2218
2219 memcpy (&dfrom, from, sizeof (dfrom));
2220 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
2221 if (dfrom == 0)
2222 return; /* Result is zero */
2223 if (dfrom != dfrom)
2224 {
2225 /* From is NaN */
2226 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
2227 fmt->exp_len, fmt->exp_nan);
2228 /* Be sure it's not infinity, but NaN value is irrel */
2229 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
2230 32, 1);
2231 return;
2232 }
2233
2234 /* If negative, set the sign bit. */
2235 if (dfrom < 0)
2236 {
2237 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
2238 dfrom = -dfrom;
2239 }
2240
2241 /* How to tell an infinity from an ordinary number? FIXME-someday */
2242
2243 #ifdef HAVE_LONG_DOUBLE
2244 mant = ldfrexp (dfrom, &exponent);
2245 #else
2246 mant = frexp (dfrom, &exponent);
2247 #endif
2248
2249 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
2250 exponent + fmt->exp_bias - 1);
2251
2252 mant_bits_left = fmt->man_len;
2253 mant_off = fmt->man_start;
2254 while (mant_bits_left > 0)
2255 {
2256 unsigned long mant_long;
2257 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
2258
2259 mant *= 4294967296.0;
2260 mant_long = (unsigned long)mant;
2261 mant -= mant_long;
2262
2263 /* If the integer bit is implicit, then we need to discard it.
2264 If we are discarding a zero, we should be (but are not) creating
2265 a denormalized number which means adjusting the exponent
2266 (I think). */
2267 if (mant_bits_left == fmt->man_len
2268 && fmt->intbit == floatformat_intbit_no)
2269 {
2270 mant_long &= 0x7fffffff;
2271 mant_bits -= 1;
2272 }
2273 else if (mant_bits < 32)
2274 {
2275 /* The bits we want are in the most significant MANT_BITS bits of
2276 mant_long. Move them to the least significant. */
2277 mant_long >>= 32 - mant_bits;
2278 }
2279
2280 put_field (uto, fmt->byteorder, fmt->totalsize,
2281 mant_off, mant_bits, mant_long);
2282 mant_off += mant_bits;
2283 mant_bits_left -= mant_bits;
2284 }
2285 }
This page took 0.0752 seconds and 4 git commands to generate.