1 /* General utility routines for GDB, the GNU debugger.
2 Copyright 1986, 89, 90, 91, 92, 95, 1996 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
21 #ifdef ANSI_PROTOTYPES
27 #include "gdb_string.h"
38 #include "expression.h"
44 /* readline defines this. */
47 /* Prototypes for local functions */
49 static void vfprintf_maybe_filtered
PARAMS ((FILE *, const char *, va_list, int));
51 static void fputs_maybe_filtered
PARAMS ((const char *, FILE *, int));
53 #if !defined (NO_MMALLOC) && !defined (NO_MMCHECK)
54 static void malloc_botch
PARAMS ((void));
58 fatal_dump_core
PARAMS((char *, ...));
61 prompt_for_continue
PARAMS ((void));
64 set_width_command
PARAMS ((char *, int, struct cmd_list_element
*));
66 /* If this definition isn't overridden by the header files, assume
67 that isatty and fileno exist on this system. */
69 #define ISATTY(FP) (isatty (fileno (FP)))
72 /* Chain of cleanup actions established with make_cleanup,
73 to be executed if an error happens. */
75 static struct cleanup
*cleanup_chain
; /* cleaned up after a failed command */
76 static struct cleanup
*final_cleanup_chain
; /* cleaned up when gdb exits */
77 static struct cleanup
*run_cleanup_chain
; /* cleaned up on each 'run' */
79 /* Nonzero if we have job control. */
83 /* Nonzero means a quit has been requested. */
87 /* Nonzero means quit immediately if Control-C is typed now, rather
88 than waiting until QUIT is executed. Be careful in setting this;
89 code which executes with immediate_quit set has to be very careful
90 about being able to deal with being interrupted at any time. It is
91 almost always better to use QUIT; the only exception I can think of
92 is being able to quit out of a system call (using EINTR loses if
93 the SIGINT happens between the previous QUIT and the system call).
94 To immediately quit in the case in which a SIGINT happens between
95 the previous QUIT and setting immediate_quit (desirable anytime we
96 expect to block), call QUIT after setting immediate_quit. */
100 /* Nonzero means that encoded C++ names should be printed out in their
101 C++ form rather than raw. */
105 /* Nonzero means that encoded C++ names should be printed out in their
106 C++ form even in assembler language displays. If this is set, but
107 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
109 int asm_demangle
= 0;
111 /* Nonzero means that strings with character values >0x7F should be printed
112 as octal escapes. Zero means just print the value (e.g. it's an
113 international character, and the terminal or window can cope.) */
115 int sevenbit_strings
= 0;
117 /* String to be printed before error messages, if any. */
119 char *error_pre_print
;
121 /* String to be printed before quit messages, if any. */
123 char *quit_pre_print
;
125 /* String to be printed before warning messages, if any. */
127 char *warning_pre_print
= "\nwarning: ";
129 /* Add a new cleanup to the cleanup_chain,
130 and return the previous chain pointer
131 to be passed later to do_cleanups or discard_cleanups.
132 Args are FUNCTION to clean up with, and ARG to pass to it. */
135 make_cleanup (function
, arg
)
136 void (*function
) PARAMS ((PTR
));
139 return make_my_cleanup (&cleanup_chain
, function
, arg
);
143 make_final_cleanup (function
, arg
)
144 void (*function
) PARAMS ((PTR
));
147 return make_my_cleanup (&final_cleanup_chain
, function
, arg
);
150 make_run_cleanup (function
, arg
)
151 void (*function
) PARAMS ((PTR
));
154 return make_my_cleanup (&run_cleanup_chain
, function
, arg
);
157 make_my_cleanup (pmy_chain
, function
, arg
)
158 struct cleanup
**pmy_chain
;
159 void (*function
) PARAMS ((PTR
));
162 register struct cleanup
*new
163 = (struct cleanup
*) xmalloc (sizeof (struct cleanup
));
164 register struct cleanup
*old_chain
= *pmy_chain
;
166 new->next
= *pmy_chain
;
167 new->function
= function
;
174 /* Discard cleanups and do the actions they describe
175 until we get back to the point OLD_CHAIN in the cleanup_chain. */
178 do_cleanups (old_chain
)
179 register struct cleanup
*old_chain
;
181 do_my_cleanups (&cleanup_chain
, old_chain
);
185 do_final_cleanups (old_chain
)
186 register struct cleanup
*old_chain
;
188 do_my_cleanups (&final_cleanup_chain
, old_chain
);
192 do_run_cleanups (old_chain
)
193 register struct cleanup
*old_chain
;
195 do_my_cleanups (&run_cleanup_chain
, old_chain
);
199 do_my_cleanups (pmy_chain
, old_chain
)
200 register struct cleanup
**pmy_chain
;
201 register struct cleanup
*old_chain
;
203 register struct cleanup
*ptr
;
204 while ((ptr
= *pmy_chain
) != old_chain
)
206 *pmy_chain
= ptr
->next
; /* Do this first incase recursion */
207 (*ptr
->function
) (ptr
->arg
);
212 /* Discard cleanups, not doing the actions they describe,
213 until we get back to the point OLD_CHAIN in the cleanup_chain. */
216 discard_cleanups (old_chain
)
217 register struct cleanup
*old_chain
;
219 discard_my_cleanups (&cleanup_chain
, old_chain
);
223 discard_final_cleanups (old_chain
)
224 register struct cleanup
*old_chain
;
226 discard_my_cleanups (&final_cleanup_chain
, old_chain
);
230 discard_my_cleanups (pmy_chain
, old_chain
)
231 register struct cleanup
**pmy_chain
;
232 register struct cleanup
*old_chain
;
234 register struct cleanup
*ptr
;
235 while ((ptr
= *pmy_chain
) != old_chain
)
237 *pmy_chain
= ptr
->next
;
242 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
246 return save_my_cleanups (&cleanup_chain
);
250 save_final_cleanups ()
252 return save_my_cleanups (&final_cleanup_chain
);
256 save_my_cleanups (pmy_chain
)
257 struct cleanup
**pmy_chain
;
259 struct cleanup
*old_chain
= *pmy_chain
;
265 /* Restore the cleanup chain from a previously saved chain. */
267 restore_cleanups (chain
)
268 struct cleanup
*chain
;
270 restore_my_cleanups (&cleanup_chain
, chain
);
274 restore_final_cleanups (chain
)
275 struct cleanup
*chain
;
277 restore_my_cleanups (&final_cleanup_chain
, chain
);
281 restore_my_cleanups (pmy_chain
, chain
)
282 struct cleanup
**pmy_chain
;
283 struct cleanup
*chain
;
288 /* This function is useful for cleanups.
292 old_chain = make_cleanup (free_current_contents, &foo);
294 to arrange to free the object thus allocated. */
297 free_current_contents (location
)
303 /* Provide a known function that does nothing, to use as a base for
304 for a possibly long chain of cleanups. This is useful where we
305 use the cleanup chain for handling normal cleanups as well as dealing
306 with cleanups that need to be done as a result of a call to error().
307 In such cases, we may not be certain where the first cleanup is, unless
308 we have a do-nothing one to always use as the base. */
318 /* Print a warning message. Way to use this is to call warning_begin,
319 output the warning message (use unfiltered output to gdb_stderr),
320 ending in a newline. There is not currently a warning_end that you
321 call afterwards, but such a thing might be added if it is useful
322 for a GUI to separate warning messages from other output.
324 FIXME: Why do warnings use unfiltered output and errors filtered?
325 Is this anything other than a historical accident? */
330 target_terminal_ours ();
331 wrap_here(""); /* Force out any buffered output */
332 gdb_flush (gdb_stdout
);
333 if (warning_pre_print
)
334 fprintf_unfiltered (gdb_stderr
, warning_pre_print
);
337 /* Print a warning message.
338 The first argument STRING is the warning message, used as a fprintf string,
339 and the remaining args are passed as arguments to it.
340 The primary difference between warnings and errors is that a warning
341 does not force the return to command level. */
345 #ifdef ANSI_PROTOTYPES
346 warning (const char *string
, ...)
353 #ifdef ANSI_PROTOTYPES
354 va_start (args
, string
);
359 string
= va_arg (args
, char *);
362 vfprintf_unfiltered (gdb_stderr
, string
, args
);
363 fprintf_unfiltered (gdb_stderr
, "\n");
367 /* Start the printing of an error message. Way to use this is to call
368 this, output the error message (use filtered output to gdb_stderr
369 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
370 in a newline, and then call return_to_top_level (RETURN_ERROR).
371 error() provides a convenient way to do this for the special case
372 that the error message can be formatted with a single printf call,
373 but this is more general. */
377 target_terminal_ours ();
378 wrap_here (""); /* Force out any buffered output */
379 gdb_flush (gdb_stdout
);
381 annotate_error_begin ();
384 fprintf_filtered (gdb_stderr
, error_pre_print
);
387 /* Print an error message and return to command level.
388 The first argument STRING is the error message, used as a fprintf string,
389 and the remaining args are passed as arguments to it. */
393 #ifdef ANSI_PROTOTYPES
394 error (const char *string
, ...)
401 #ifdef ANSI_PROTOTYPES
402 va_start (args
, string
);
411 #ifdef ANSI_PROTOTYPES
412 vfprintf_filtered (gdb_stderr
, string
, args
);
417 string1
= va_arg (args
, char *);
418 vfprintf_filtered (gdb_stderr
, string1
, args
);
421 fprintf_filtered (gdb_stderr
, "\n");
423 return_to_top_level (RETURN_ERROR
);
428 /* Print an error message and exit reporting failure.
429 This is for a error that we cannot continue from.
430 The arguments are printed a la printf.
432 This function cannot be declared volatile (NORETURN) in an
433 ANSI environment because exit() is not declared volatile. */
437 #ifdef ANSI_PROTOTYPES
438 fatal (char *string
, ...)
445 #ifdef ANSI_PROTOTYPES
446 va_start (args
, string
);
450 string
= va_arg (args
, char *);
452 fprintf_unfiltered (gdb_stderr
, "\ngdb: ");
453 vfprintf_unfiltered (gdb_stderr
, string
, args
);
454 fprintf_unfiltered (gdb_stderr
, "\n");
459 /* Print an error message and exit, dumping core.
460 The arguments are printed a la printf (). */
464 #ifdef ANSI_PROTOTYPES
465 fatal_dump_core (char *string
, ...)
467 fatal_dump_core (va_alist
)
472 #ifdef ANSI_PROTOTYPES
473 va_start (args
, string
);
478 string
= va_arg (args
, char *);
480 /* "internal error" is always correct, since GDB should never dump
481 core, no matter what the input. */
482 fprintf_unfiltered (gdb_stderr
, "\ngdb internal error: ");
483 vfprintf_unfiltered (gdb_stderr
, string
, args
);
484 fprintf_unfiltered (gdb_stderr
, "\n");
487 signal (SIGQUIT
, SIG_DFL
);
488 kill (getpid (), SIGQUIT
);
489 /* We should never get here, but just in case... */
493 /* The strerror() function can return NULL for errno values that are
494 out of range. Provide a "safe" version that always returns a
498 safe_strerror (errnum
)
504 if ((msg
= strerror (errnum
)) == NULL
)
506 sprintf (buf
, "(undocumented errno %d)", errnum
);
512 /* The strsignal() function can return NULL for signal values that are
513 out of range. Provide a "safe" version that always returns a
517 safe_strsignal (signo
)
523 if ((msg
= strsignal (signo
)) == NULL
)
525 sprintf (buf
, "(undocumented signal %d)", signo
);
532 /* Print the system error message for errno, and also mention STRING
533 as the file name for which the error was encountered.
534 Then return to command level. */
537 perror_with_name (string
)
543 err
= safe_strerror (errno
);
544 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
545 strcpy (combined
, string
);
546 strcat (combined
, ": ");
547 strcat (combined
, err
);
549 /* I understand setting these is a matter of taste. Still, some people
550 may clear errno but not know about bfd_error. Doing this here is not
552 bfd_set_error (bfd_error_no_error
);
555 error ("%s.", combined
);
558 /* Print the system error message for ERRCODE, and also mention STRING
559 as the file name for which the error was encountered. */
562 print_sys_errmsg (string
, errcode
)
569 err
= safe_strerror (errcode
);
570 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
571 strcpy (combined
, string
);
572 strcat (combined
, ": ");
573 strcat (combined
, err
);
575 /* We want anything which was printed on stdout to come out first, before
577 gdb_flush (gdb_stdout
);
578 fprintf_unfiltered (gdb_stderr
, "%s.\n", combined
);
581 /* Control C eventually causes this to be called, at a convenient time. */
586 serial_t gdb_stdout_serial
= serial_fdopen (1);
588 target_terminal_ours ();
590 /* We want all output to appear now, before we print "Quit". We
591 have 3 levels of buffering we have to flush (it's possible that
592 some of these should be changed to flush the lower-level ones
595 /* 1. The _filtered buffer. */
596 wrap_here ((char *)0);
598 /* 2. The stdio buffer. */
599 gdb_flush (gdb_stdout
);
600 gdb_flush (gdb_stderr
);
602 /* 3. The system-level buffer. */
603 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial
);
604 SERIAL_UN_FDOPEN (gdb_stdout_serial
);
606 annotate_error_begin ();
608 /* Don't use *_filtered; we don't want to prompt the user to continue. */
610 fprintf_unfiltered (gdb_stderr
, quit_pre_print
);
613 /* If there is no terminal switching for this target, then we can't
614 possibly get screwed by the lack of job control. */
615 || current_target
.to_terminal_ours
== NULL
)
616 fprintf_unfiltered (gdb_stderr
, "Quit\n");
618 fprintf_unfiltered (gdb_stderr
,
619 "Quit (expect signal SIGINT when the program is resumed)\n");
620 return_to_top_level (RETURN_QUIT
);
624 #if defined(__GO32__)
626 /* In the absence of signals, poll keyboard for a quit.
627 Called from #define QUIT pollquit() in xm-go32.h. */
642 /* We just ignore it */
643 /* FIXME!! Don't think this actually works! */
644 fprintf_unfiltered (gdb_stderr
, "CTRL-A to quit, CTRL-B to quit harder\n");
649 #elif defined(_MSC_VER) /* should test for wingdb instead? */
652 * Windows translates all keyboard and mouse events
653 * into a message which is appended to the message
654 * queue for the process.
659 int k
= win32pollquit();
666 #else /* !defined(__GO32__) && !defined(_MSC_VER) */
670 /* Done by signals */
673 #endif /* !defined(__GO32__) && !defined(_MSC_VER) */
679 if (quit_flag
|| immediate_quit
)
683 /* Control C comes here */
690 /* Restore the signal handler. Harmless with BSD-style signals, needed
691 for System V-style signals. So just always do it, rather than worrying
692 about USG defines and stuff like that. */
693 signal (signo
, request_quit
);
704 /* Memory management stuff (malloc friends). */
706 /* Make a substitute size_t for non-ANSI compilers. */
708 #ifndef HAVE_STDDEF_H
710 #define size_t unsigned int
714 #if defined (NO_MMALLOC)
721 return malloc (size
);
725 mrealloc (md
, ptr
, size
)
730 if (ptr
== 0) /* Guard against old realloc's */
731 return malloc (size
);
733 return realloc (ptr
, size
);
744 #endif /* NO_MMALLOC */
746 #if defined (NO_MMALLOC) || defined (NO_MMCHECK)
754 #else /* Have mmalloc and want corruption checking */
759 fatal_dump_core ("Memory corruption");
762 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
763 by MD, to detect memory corruption. Note that MD may be NULL to specify
764 the default heap that grows via sbrk.
766 Note that for freshly created regions, we must call mmcheckf prior to any
767 mallocs in the region. Otherwise, any region which was allocated prior to
768 installing the checking hooks, which is later reallocated or freed, will
769 fail the checks! The mmcheck function only allows initial hooks to be
770 installed before the first mmalloc. However, anytime after we have called
771 mmcheck the first time to install the checking hooks, we can call it again
772 to update the function pointer to the memory corruption handler.
774 Returns zero on failure, non-zero on success. */
776 #ifndef MMCHECK_FORCE
777 #define MMCHECK_FORCE 0
784 if (!mmcheckf (md
, malloc_botch
, MMCHECK_FORCE
))
786 /* Don't use warning(), which relies on current_target being set
787 to something other than dummy_target, until after
788 initialize_all_files(). */
791 (gdb_stderr
, "warning: failed to install memory consistency checks; ");
793 (gdb_stderr
, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n");
799 #endif /* Have mmalloc and want corruption checking */
801 /* Called when a memory allocation fails, with the number of bytes of
802 memory requested in SIZE. */
810 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size
);
814 fatal ("virtual memory exhausted.");
818 /* Like mmalloc but get error if no storage available, and protect against
819 the caller wanting to allocate zero bytes. Whether to return NULL for
820 a zero byte request, or translate the request into a request for one
821 byte of zero'd storage, is a religious issue. */
834 else if ((val
= mmalloc (md
, size
)) == NULL
)
841 /* Like mrealloc but get error if no storage available. */
844 xmrealloc (md
, ptr
, size
)
853 val
= mrealloc (md
, ptr
, size
);
857 val
= mmalloc (md
, size
);
866 /* Like malloc but get error if no storage available, and protect against
867 the caller wanting to allocate zero bytes. */
873 return (xmmalloc ((PTR
) NULL
, size
));
876 /* Like mrealloc but get error if no storage available. */
883 return (xmrealloc ((PTR
) NULL
, ptr
, size
));
887 /* My replacement for the read system call.
888 Used like `read' but keeps going if `read' returns too soon. */
891 myread (desc
, addr
, len
)
901 val
= read (desc
, addr
, len
);
912 /* Make a copy of the string at PTR with SIZE characters
913 (and add a null character at the end in the copy).
914 Uses malloc to get the space. Returns the address of the copy. */
917 savestring (ptr
, size
)
921 register char *p
= (char *) xmalloc (size
+ 1);
922 memcpy (p
, ptr
, size
);
928 msavestring (md
, ptr
, size
)
933 register char *p
= (char *) xmmalloc (md
, size
+ 1);
934 memcpy (p
, ptr
, size
);
939 /* The "const" is so it compiles under DGUX (which prototypes strsave
940 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
941 Doesn't real strsave return NULL if out of memory? */
946 return savestring (ptr
, strlen (ptr
));
954 return (msavestring (md
, ptr
, strlen (ptr
)));
958 print_spaces (n
, file
)
966 /* Print a host address. */
969 gdb_print_address (addr
, stream
)
974 /* We could use the %p conversion specifier to fprintf if we had any
975 way of knowing whether this host supports it. But the following
976 should work on the Alpha and on 32 bit machines. */
978 fprintf_filtered (stream
, "0x%lx", (unsigned long)addr
);
981 /* Ask user a y-or-n question and return 1 iff answer is yes.
982 Takes three args which are given to printf to print the question.
983 The first, a control string, should end in "? ".
984 It should not say how to answer, because we do that. */
988 #ifdef ANSI_PROTOTYPES
989 query (char *ctlstr
, ...)
1000 #ifdef ANSI_PROTOTYPES
1001 va_start (args
, ctlstr
);
1005 ctlstr
= va_arg (args
, char *);
1010 return query_hook (ctlstr
, args
);
1013 /* Automatically answer "yes" if input is not from a terminal. */
1014 if (!input_from_terminal_p ())
1017 /* FIXME Automatically answer "yes" if called from MacGDB. */
1024 wrap_here (""); /* Flush any buffered output */
1025 gdb_flush (gdb_stdout
);
1027 if (annotation_level
> 1)
1028 printf_filtered ("\n\032\032pre-query\n");
1030 vfprintf_filtered (gdb_stdout
, ctlstr
, args
);
1031 printf_filtered ("(y or n) ");
1033 if (annotation_level
> 1)
1034 printf_filtered ("\n\032\032query\n");
1037 /* If not in MacGDB, move to a new line so the entered line doesn't
1038 have a prompt on the front of it. */
1040 fputs_unfiltered ("\n", gdb_stdout
);
1043 gdb_flush (gdb_stdout
);
1044 answer
= fgetc (stdin
);
1045 clearerr (stdin
); /* in case of C-d */
1046 if (answer
== EOF
) /* C-d */
1051 if (answer
!= '\n') /* Eat rest of input line, to EOF or newline */
1054 ans2
= fgetc (stdin
);
1057 while (ans2
!= EOF
&& ans2
!= '\n');
1070 printf_filtered ("Please answer y or n.\n");
1073 if (annotation_level
> 1)
1074 printf_filtered ("\n\032\032post-query\n");
1079 /* Parse a C escape sequence. STRING_PTR points to a variable
1080 containing a pointer to the string to parse. That pointer
1081 should point to the character after the \. That pointer
1082 is updated past the characters we use. The value of the
1083 escape sequence is returned.
1085 A negative value means the sequence \ newline was seen,
1086 which is supposed to be equivalent to nothing at all.
1088 If \ is followed by a null character, we return a negative
1089 value and leave the string pointer pointing at the null character.
1091 If \ is followed by 000, we return 0 and leave the string pointer
1092 after the zeros. A value of 0 does not mean end of string. */
1095 parse_escape (string_ptr
)
1098 register int c
= *(*string_ptr
)++;
1102 return 007; /* Bell (alert) char */
1105 case 'e': /* Escape character */
1123 c
= *(*string_ptr
)++;
1125 c
= parse_escape (string_ptr
);
1128 return (c
& 0200) | (c
& 037);
1139 register int i
= c
- '0';
1140 register int count
= 0;
1143 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
1161 /* Print the character C on STREAM as part of the contents of a literal
1162 string whose delimiter is QUOTER. Note that this routine should only
1163 be call for printing things which are independent of the language
1164 of the program being debugged. */
1167 gdb_printchar (c
, stream
, quoter
)
1173 c
&= 0xFF; /* Avoid sign bit follies */
1175 if ( c
< 0x20 || /* Low control chars */
1176 (c
>= 0x7F && c
< 0xA0) || /* DEL, High controls */
1177 (sevenbit_strings
&& c
>= 0x80)) { /* high order bit set */
1181 fputs_filtered ("\\n", stream
);
1184 fputs_filtered ("\\b", stream
);
1187 fputs_filtered ("\\t", stream
);
1190 fputs_filtered ("\\f", stream
);
1193 fputs_filtered ("\\r", stream
);
1196 fputs_filtered ("\\e", stream
);
1199 fputs_filtered ("\\a", stream
);
1202 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
1206 if (c
== '\\' || c
== quoter
)
1207 fputs_filtered ("\\", stream
);
1208 fprintf_filtered (stream
, "%c", c
);
1212 /* Number of lines per page or UINT_MAX if paging is disabled. */
1213 static unsigned int lines_per_page
;
1214 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1215 static unsigned int chars_per_line
;
1216 /* Current count of lines printed on this page, chars on this line. */
1217 static unsigned int lines_printed
, chars_printed
;
1219 /* Buffer and start column of buffered text, for doing smarter word-
1220 wrapping. When someone calls wrap_here(), we start buffering output
1221 that comes through fputs_filtered(). If we see a newline, we just
1222 spit it out and forget about the wrap_here(). If we see another
1223 wrap_here(), we spit it out and remember the newer one. If we see
1224 the end of the line, we spit out a newline, the indent, and then
1225 the buffered output. */
1227 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1228 are waiting to be output (they have already been counted in chars_printed).
1229 When wrap_buffer[0] is null, the buffer is empty. */
1230 static char *wrap_buffer
;
1232 /* Pointer in wrap_buffer to the next character to fill. */
1233 static char *wrap_pointer
;
1235 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1237 static char *wrap_indent
;
1239 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1240 is not in effect. */
1241 static int wrap_column
;
1245 set_width_command (args
, from_tty
, c
)
1248 struct cmd_list_element
*c
;
1252 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
1253 wrap_buffer
[0] = '\0';
1256 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
1257 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
1260 /* Wait, so the user can read what's on the screen. Prompt the user
1261 to continue by pressing RETURN. */
1264 prompt_for_continue ()
1267 char cont_prompt
[120];
1269 if (annotation_level
> 1)
1270 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1272 strcpy (cont_prompt
,
1273 "---Type <return> to continue, or q <return> to quit---");
1274 if (annotation_level
> 1)
1275 strcat (cont_prompt
, "\n\032\032prompt-for-continue\n");
1277 /* We must do this *before* we call gdb_readline, else it will eventually
1278 call us -- thinking that we're trying to print beyond the end of the
1280 reinitialize_more_filter ();
1283 /* On a real operating system, the user can quit with SIGINT.
1286 'q' is provided on all systems so users don't have to change habits
1287 from system to system, and because telling them what to do in
1288 the prompt is more user-friendly than expecting them to think of
1290 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1291 whereas control-C to gdb_readline will cause the user to get dumped
1293 ignore
= readline (cont_prompt
);
1295 if (annotation_level
> 1)
1296 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1301 while (*p
== ' ' || *p
== '\t')
1304 request_quit (SIGINT
);
1309 /* Now we have to do this again, so that GDB will know that it doesn't
1310 need to save the ---Type <return>--- line at the top of the screen. */
1311 reinitialize_more_filter ();
1313 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1316 /* Reinitialize filter; ie. tell it to reset to original values. */
1319 reinitialize_more_filter ()
1325 /* Indicate that if the next sequence of characters overflows the line,
1326 a newline should be inserted here rather than when it hits the end.
1327 If INDENT is non-null, it is a string to be printed to indent the
1328 wrapped part on the next line. INDENT must remain accessible until
1329 the next call to wrap_here() or until a newline is printed through
1332 If the line is already overfull, we immediately print a newline and
1333 the indentation, and disable further wrapping.
1335 If we don't know the width of lines, but we know the page height,
1336 we must not wrap words, but should still keep track of newlines
1337 that were explicitly printed.
1339 INDENT should not contain tabs, as that will mess up the char count
1340 on the next line. FIXME.
1342 This routine is guaranteed to force out any output which has been
1343 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1344 used to force out output from the wrap_buffer. */
1350 /* This should have been allocated, but be paranoid anyway. */
1356 *wrap_pointer
= '\0';
1357 fputs_unfiltered (wrap_buffer
, gdb_stdout
);
1359 wrap_pointer
= wrap_buffer
;
1360 wrap_buffer
[0] = '\0';
1361 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
1365 else if (chars_printed
>= chars_per_line
)
1367 puts_filtered ("\n");
1369 puts_filtered (indent
);
1374 wrap_column
= chars_printed
;
1378 wrap_indent
= indent
;
1382 /* Ensure that whatever gets printed next, using the filtered output
1383 commands, starts at the beginning of the line. I.E. if there is
1384 any pending output for the current line, flush it and start a new
1385 line. Otherwise do nothing. */
1390 if (chars_printed
> 0)
1392 puts_filtered ("\n");
1398 gdb_fopen (name
, mode
)
1402 return fopen (name
, mode
);
1410 && (stream
== gdb_stdout
1411 || stream
== gdb_stderr
))
1413 flush_hook (stream
);
1420 /* Like fputs but if FILTER is true, pause after every screenful.
1422 Regardless of FILTER can wrap at points other than the final
1423 character of a line.
1425 Unlike fputs, fputs_maybe_filtered does not return a value.
1426 It is OK for LINEBUFFER to be NULL, in which case just don't print
1429 Note that a longjmp to top level may occur in this routine (only if
1430 FILTER is true) (since prompt_for_continue may do so) so this
1431 routine should not be called when cleanups are not in place. */
1434 fputs_maybe_filtered (linebuffer
, stream
, filter
)
1435 const char *linebuffer
;
1439 const char *lineptr
;
1441 if (linebuffer
== 0)
1444 /* Don't do any filtering if it is disabled. */
1445 if (stream
!= gdb_stdout
1446 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
1448 fputs_unfiltered (linebuffer
, stream
);
1452 /* Go through and output each character. Show line extension
1453 when this is necessary; prompt user for new page when this is
1456 lineptr
= linebuffer
;
1459 /* Possible new page. */
1461 (lines_printed
>= lines_per_page
- 1))
1462 prompt_for_continue ();
1464 while (*lineptr
&& *lineptr
!= '\n')
1466 /* Print a single line. */
1467 if (*lineptr
== '\t')
1470 *wrap_pointer
++ = '\t';
1472 fputc_unfiltered ('\t', stream
);
1473 /* Shifting right by 3 produces the number of tab stops
1474 we have already passed, and then adding one and
1475 shifting left 3 advances to the next tab stop. */
1476 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
1482 *wrap_pointer
++ = *lineptr
;
1484 fputc_unfiltered (*lineptr
, stream
);
1489 if (chars_printed
>= chars_per_line
)
1491 unsigned int save_chars
= chars_printed
;
1495 /* If we aren't actually wrapping, don't output newline --
1496 if chars_per_line is right, we probably just overflowed
1497 anyway; if it's wrong, let us keep going. */
1499 fputc_unfiltered ('\n', stream
);
1501 /* Possible new page. */
1502 if (lines_printed
>= lines_per_page
- 1)
1503 prompt_for_continue ();
1505 /* Now output indentation and wrapped string */
1508 fputs_unfiltered (wrap_indent
, stream
);
1509 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
1510 fputs_unfiltered (wrap_buffer
, stream
); /* and eject it */
1511 /* FIXME, this strlen is what prevents wrap_indent from
1512 containing tabs. However, if we recurse to print it
1513 and count its chars, we risk trouble if wrap_indent is
1514 longer than (the user settable) chars_per_line.
1515 Note also that this can set chars_printed > chars_per_line
1516 if we are printing a long string. */
1517 chars_printed
= strlen (wrap_indent
)
1518 + (save_chars
- wrap_column
);
1519 wrap_pointer
= wrap_buffer
; /* Reset buffer */
1520 wrap_buffer
[0] = '\0';
1521 wrap_column
= 0; /* And disable fancy wrap */
1526 if (*lineptr
== '\n')
1529 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1531 fputc_unfiltered ('\n', stream
);
1538 fputs_filtered (linebuffer
, stream
)
1539 const char *linebuffer
;
1542 fputs_maybe_filtered (linebuffer
, stream
, 1);
1546 putchar_unfiltered (c
)
1553 fputs_unfiltered (buf
, gdb_stdout
);
1558 fputc_unfiltered (c
, stream
)
1566 fputs_unfiltered (buf
, stream
);
1571 /* puts_debug is like fputs_unfiltered, except it prints special
1572 characters in printable fashion. */
1575 puts_debug (prefix
, string
, suffix
)
1582 /* Print prefix and suffix after each line. */
1583 static int new_line
= 1;
1584 static int carriage_return
= 0;
1585 static char *prev_prefix
= "";
1586 static char *prev_suffix
= "";
1588 if (*string
== '\n')
1589 carriage_return
= 0;
1591 /* If the prefix is changing, print the previous suffix, a new line,
1592 and the new prefix. */
1593 if ((carriage_return
|| (strcmp(prev_prefix
, prefix
) != 0)) && !new_line
)
1595 fputs_unfiltered (prev_suffix
, gdb_stderr
);
1596 fputs_unfiltered ("\n", gdb_stderr
);
1597 fputs_unfiltered (prefix
, gdb_stderr
);
1600 /* Print prefix if we printed a newline during the previous call. */
1604 fputs_unfiltered (prefix
, gdb_stderr
);
1607 prev_prefix
= prefix
;
1608 prev_suffix
= suffix
;
1610 /* Output characters in a printable format. */
1611 while ((ch
= *string
++) != '\0')
1617 fputc_unfiltered (ch
, gdb_stderr
);
1620 fprintf_unfiltered (gdb_stderr
, "\\%03o", ch
);
1623 case '\\': fputs_unfiltered ("\\\\", gdb_stderr
); break;
1624 case '\b': fputs_unfiltered ("\\b", gdb_stderr
); break;
1625 case '\f': fputs_unfiltered ("\\f", gdb_stderr
); break;
1626 case '\n': new_line
= 1;
1627 fputs_unfiltered ("\\n", gdb_stderr
); break;
1628 case '\r': fputs_unfiltered ("\\r", gdb_stderr
); break;
1629 case '\t': fputs_unfiltered ("\\t", gdb_stderr
); break;
1630 case '\v': fputs_unfiltered ("\\v", gdb_stderr
); break;
1633 carriage_return
= ch
== '\r';
1636 /* Print suffix if we printed a newline. */
1639 fputs_unfiltered (suffix
, gdb_stderr
);
1640 fputs_unfiltered ("\n", gdb_stderr
);
1645 /* Print a variable number of ARGS using format FORMAT. If this
1646 information is going to put the amount written (since the last call
1647 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1648 call prompt_for_continue to get the users permision to continue.
1650 Unlike fprintf, this function does not return a value.
1652 We implement three variants, vfprintf (takes a vararg list and stream),
1653 fprintf (takes a stream to write on), and printf (the usual).
1655 Note also that a longjmp to top level may occur in this routine
1656 (since prompt_for_continue may do so) so this routine should not be
1657 called when cleanups are not in place. */
1660 vfprintf_maybe_filtered (stream
, format
, args
, filter
)
1667 struct cleanup
*old_cleanups
;
1669 vasprintf (&linebuffer
, format
, args
);
1670 if (linebuffer
== NULL
)
1672 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1675 old_cleanups
= make_cleanup (free
, linebuffer
);
1676 fputs_maybe_filtered (linebuffer
, stream
, filter
);
1677 do_cleanups (old_cleanups
);
1682 vfprintf_filtered (stream
, format
, args
)
1687 vfprintf_maybe_filtered (stream
, format
, args
, 1);
1691 vfprintf_unfiltered (stream
, format
, args
)
1697 struct cleanup
*old_cleanups
;
1699 vasprintf (&linebuffer
, format
, args
);
1700 if (linebuffer
== NULL
)
1702 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1705 old_cleanups
= make_cleanup (free
, linebuffer
);
1706 fputs_unfiltered (linebuffer
, stream
);
1707 do_cleanups (old_cleanups
);
1711 vprintf_filtered (format
, args
)
1715 vfprintf_maybe_filtered (gdb_stdout
, format
, args
, 1);
1719 vprintf_unfiltered (format
, args
)
1723 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1728 #ifdef ANSI_PROTOTYPES
1729 fprintf_filtered (FILE *stream
, const char *format
, ...)
1731 fprintf_filtered (va_alist
)
1736 #ifdef ANSI_PROTOTYPES
1737 va_start (args
, format
);
1743 stream
= va_arg (args
, FILE *);
1744 format
= va_arg (args
, char *);
1746 vfprintf_filtered (stream
, format
, args
);
1752 #ifdef ANSI_PROTOTYPES
1753 fprintf_unfiltered (FILE *stream
, const char *format
, ...)
1755 fprintf_unfiltered (va_alist
)
1760 #ifdef ANSI_PROTOTYPES
1761 va_start (args
, format
);
1767 stream
= va_arg (args
, FILE *);
1768 format
= va_arg (args
, char *);
1770 vfprintf_unfiltered (stream
, format
, args
);
1774 /* Like fprintf_filtered, but prints its result indented.
1775 Called as fprintfi_filtered (spaces, stream, format, ...); */
1779 #ifdef ANSI_PROTOTYPES
1780 fprintfi_filtered (int spaces
, FILE *stream
, const char *format
, ...)
1782 fprintfi_filtered (va_alist
)
1787 #ifdef ANSI_PROTOTYPES
1788 va_start (args
, format
);
1795 spaces
= va_arg (args
, int);
1796 stream
= va_arg (args
, FILE *);
1797 format
= va_arg (args
, char *);
1799 print_spaces_filtered (spaces
, stream
);
1801 vfprintf_filtered (stream
, format
, args
);
1808 #ifdef ANSI_PROTOTYPES
1809 printf_filtered (const char *format
, ...)
1811 printf_filtered (va_alist
)
1816 #ifdef ANSI_PROTOTYPES
1817 va_start (args
, format
);
1822 format
= va_arg (args
, char *);
1824 vfprintf_filtered (gdb_stdout
, format
, args
);
1831 #ifdef ANSI_PROTOTYPES
1832 printf_unfiltered (const char *format
, ...)
1834 printf_unfiltered (va_alist
)
1839 #ifdef ANSI_PROTOTYPES
1840 va_start (args
, format
);
1845 format
= va_arg (args
, char *);
1847 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1851 /* Like printf_filtered, but prints it's result indented.
1852 Called as printfi_filtered (spaces, format, ...); */
1856 #ifdef ANSI_PROTOTYPES
1857 printfi_filtered (int spaces
, const char *format
, ...)
1859 printfi_filtered (va_alist
)
1864 #ifdef ANSI_PROTOTYPES
1865 va_start (args
, format
);
1871 spaces
= va_arg (args
, int);
1872 format
= va_arg (args
, char *);
1874 print_spaces_filtered (spaces
, gdb_stdout
);
1875 vfprintf_filtered (gdb_stdout
, format
, args
);
1879 /* Easy -- but watch out!
1881 This routine is *not* a replacement for puts()! puts() appends a newline.
1882 This one doesn't, and had better not! */
1885 puts_filtered (string
)
1888 fputs_filtered (string
, gdb_stdout
);
1892 puts_unfiltered (string
)
1895 fputs_unfiltered (string
, gdb_stdout
);
1898 /* Return a pointer to N spaces and a null. The pointer is good
1899 until the next call to here. */
1905 static char *spaces
;
1906 static int max_spaces
;
1912 spaces
= (char *) xmalloc (n
+1);
1913 for (t
= spaces
+n
; t
!= spaces
;)
1919 return spaces
+ max_spaces
- n
;
1922 /* Print N spaces. */
1924 print_spaces_filtered (n
, stream
)
1928 fputs_filtered (n_spaces (n
), stream
);
1931 /* C++ demangler stuff. */
1933 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1934 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1935 If the name is not mangled, or the language for the name is unknown, or
1936 demangling is off, the name is printed in its "raw" form. */
1939 fprintf_symbol_filtered (stream
, name
, lang
, arg_mode
)
1949 /* If user wants to see raw output, no problem. */
1952 fputs_filtered (name
, stream
);
1958 case language_cplus
:
1959 demangled
= cplus_demangle (name
, arg_mode
);
1962 demangled
= cplus_demangle (name
, arg_mode
| DMGL_JAVA
);
1964 case language_chill
:
1965 demangled
= chill_demangle (name
);
1971 fputs_filtered (demangled
? demangled
: name
, stream
);
1972 if (demangled
!= NULL
)
1980 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1981 differences in whitespace. Returns 0 if they match, non-zero if they
1982 don't (slightly different than strcmp()'s range of return values).
1984 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1985 This "feature" is useful when searching for matching C++ function names
1986 (such as if the user types 'break FOO', where FOO is a mangled C++
1990 strcmp_iw (string1
, string2
)
1991 const char *string1
;
1992 const char *string2
;
1994 while ((*string1
!= '\0') && (*string2
!= '\0'))
1996 while (isspace (*string1
))
2000 while (isspace (*string2
))
2004 if (*string1
!= *string2
)
2008 if (*string1
!= '\0')
2014 return (*string1
!= '\0' && *string1
!= '(') || (*string2
!= '\0');
2021 struct cmd_list_element
*c
;
2023 c
= add_set_cmd ("width", class_support
, var_uinteger
,
2024 (char *)&chars_per_line
,
2025 "Set number of characters gdb thinks are in a line.",
2027 add_show_from_set (c
, &showlist
);
2028 c
->function
.sfunc
= set_width_command
;
2031 (add_set_cmd ("height", class_support
,
2032 var_uinteger
, (char *)&lines_per_page
,
2033 "Set number of lines gdb thinks are in a page.", &setlist
),
2036 /* These defaults will be used if we are unable to get the correct
2037 values from termcap. */
2038 #if defined(__GO32__)
2039 lines_per_page
= ScreenRows();
2040 chars_per_line
= ScreenCols();
2042 lines_per_page
= 24;
2043 chars_per_line
= 80;
2045 #if !defined (MPW) && !defined (_WIN32)
2046 /* No termcap under MPW, although might be cool to do something
2047 by looking at worksheet or console window sizes. */
2048 /* Initialize the screen height and width from termcap. */
2050 char *termtype
= getenv ("TERM");
2052 /* Positive means success, nonpositive means failure. */
2055 /* 2048 is large enough for all known terminals, according to the
2056 GNU termcap manual. */
2057 char term_buffer
[2048];
2061 status
= tgetent (term_buffer
, termtype
);
2066 val
= tgetnum ("li");
2068 lines_per_page
= val
;
2070 /* The number of lines per page is not mentioned
2071 in the terminal description. This probably means
2072 that paging is not useful (e.g. emacs shell window),
2073 so disable paging. */
2074 lines_per_page
= UINT_MAX
;
2076 val
= tgetnum ("co");
2078 chars_per_line
= val
;
2084 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2086 /* If there is a better way to determine the window size, use it. */
2087 SIGWINCH_HANDLER ();
2090 /* If the output is not a terminal, don't paginate it. */
2091 if (!ISATTY (gdb_stdout
))
2092 lines_per_page
= UINT_MAX
;
2094 set_width_command ((char *)NULL
, 0, c
);
2097 (add_set_cmd ("demangle", class_support
, var_boolean
,
2099 "Set demangling of encoded C++ names when displaying symbols.",
2104 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
2105 (char *)&sevenbit_strings
,
2106 "Set printing of 8-bit characters in strings as \\nnn.",
2111 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
2112 (char *)&asm_demangle
,
2113 "Set demangling of C++ names in disassembly listings.",
2118 /* Machine specific function to handle SIGWINCH signal. */
2120 #ifdef SIGWINCH_HANDLER_BODY
2121 SIGWINCH_HANDLER_BODY
2124 /* Support for converting target fp numbers into host DOUBLEST format. */
2126 /* XXX - This code should really be in libiberty/floatformat.c, however
2127 configuration issues with libiberty made this very difficult to do in the
2130 #include "floatformat.h"
2131 #include <math.h> /* ldexp */
2133 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
2134 going to bother with trying to muck around with whether it is defined in
2135 a system header, what we do if not, etc. */
2136 #define FLOATFORMAT_CHAR_BIT 8
2138 static unsigned long get_field
PARAMS ((unsigned char *,
2139 enum floatformat_byteorders
,
2144 /* Extract a field which starts at START and is LEN bytes long. DATA and
2145 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2146 static unsigned long
2147 get_field (data
, order
, total_len
, start
, len
)
2148 unsigned char *data
;
2149 enum floatformat_byteorders order
;
2150 unsigned int total_len
;
2154 unsigned long result
;
2155 unsigned int cur_byte
;
2158 /* Start at the least significant part of the field. */
2159 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
2160 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2161 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
2163 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
2164 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
2165 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
2166 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2171 /* Move towards the most significant part of the field. */
2172 while (cur_bitshift
< len
)
2174 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
2175 /* This is the last byte; zero out the bits which are not part of
2178 (*(data
+ cur_byte
) & ((1 << (len
- cur_bitshift
)) - 1))
2181 result
|= *(data
+ cur_byte
) << cur_bitshift
;
2182 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
2183 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2191 /* Convert from FMT to a DOUBLEST.
2192 FROM is the address of the extended float.
2193 Store the DOUBLEST in *TO. */
2196 floatformat_to_doublest (fmt
, from
, to
)
2197 const struct floatformat
*fmt
;
2201 unsigned char *ufrom
= (unsigned char *)from
;
2205 unsigned int mant_bits
, mant_off
;
2207 int special_exponent
; /* It's a NaN, denorm or zero */
2209 /* If the mantissa bits are not contiguous from one end of the
2210 mantissa to the other, we need to make a private copy of the
2211 source bytes that is in the right order since the unpacking
2212 algorithm assumes that the bits are contiguous.
2214 Swap the bytes individually rather than accessing them through
2215 "long *" since we have no guarantee that they start on a long
2216 alignment, and also sizeof(long) for the host could be different
2217 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
2218 for the target is 4. */
2220 if (fmt
-> byteorder
== floatformat_littlebyte_bigword
)
2222 static unsigned char *newfrom
;
2223 unsigned char *swapin
, *swapout
;
2226 longswaps
= fmt
-> totalsize
/ FLOATFORMAT_CHAR_BIT
;
2229 if (newfrom
== NULL
)
2231 newfrom
= xmalloc (fmt
-> totalsize
);
2236 while (longswaps
-- > 0)
2238 /* This is ugly, but efficient */
2239 *swapout
++ = swapin
[4];
2240 *swapout
++ = swapin
[5];
2241 *swapout
++ = swapin
[6];
2242 *swapout
++ = swapin
[7];
2243 *swapout
++ = swapin
[0];
2244 *swapout
++ = swapin
[1];
2245 *swapout
++ = swapin
[2];
2246 *swapout
++ = swapin
[3];
2251 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
2252 fmt
->exp_start
, fmt
->exp_len
);
2253 /* Note that if exponent indicates a NaN, we can't really do anything useful
2254 (not knowing if the host has NaN's, or how to build one). So it will
2255 end up as an infinity or something close; that is OK. */
2257 mant_bits_left
= fmt
->man_len
;
2258 mant_off
= fmt
->man_start
;
2261 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
2263 /* Don't bias zero's, denorms or NaNs. */
2264 if (!special_exponent
)
2265 exponent
-= fmt
->exp_bias
;
2267 /* Build the result algebraically. Might go infinite, underflow, etc;
2270 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
2271 increment the exponent by one to account for the integer bit. */
2273 if (!special_exponent
)
2274 if (fmt
->intbit
== floatformat_intbit_no
)
2275 dto
= ldexp (1.0, exponent
);
2279 while (mant_bits_left
> 0)
2281 mant_bits
= min (mant_bits_left
, 32);
2283 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
2284 mant_off
, mant_bits
);
2286 dto
+= ldexp ((double)mant
, exponent
- mant_bits
);
2287 exponent
-= mant_bits
;
2288 mant_off
+= mant_bits
;
2289 mant_bits_left
-= mant_bits
;
2292 /* Negate it if negative. */
2293 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
2298 static void put_field
PARAMS ((unsigned char *, enum floatformat_byteorders
,
2304 /* Set a field which starts at START and is LEN bytes long. DATA and
2305 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2307 put_field (data
, order
, total_len
, start
, len
, stuff_to_put
)
2308 unsigned char *data
;
2309 enum floatformat_byteorders order
;
2310 unsigned int total_len
;
2313 unsigned long stuff_to_put
;
2315 unsigned int cur_byte
;
2318 /* Start at the least significant part of the field. */
2319 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
2320 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2321 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
2323 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
2324 *(data
+ cur_byte
) &=
2325 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1) << (-cur_bitshift
));
2326 *(data
+ cur_byte
) |=
2327 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
2328 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
2329 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2334 /* Move towards the most significant part of the field. */
2335 while (cur_bitshift
< len
)
2337 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
2339 /* This is the last byte. */
2340 *(data
+ cur_byte
) &=
2341 ~((1 << (len
- cur_bitshift
)) - 1);
2342 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
2345 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
2346 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
2347 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
2348 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2355 #ifdef HAVE_LONG_DOUBLE
2356 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
2357 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
2358 frexp, but operates on the long double data type. */
2360 static long double ldfrexp
PARAMS ((long double value
, int *eptr
));
2363 ldfrexp (value
, eptr
)
2370 /* Unfortunately, there are no portable functions for extracting the exponent
2371 of a long double, so we have to do it iteratively by multiplying or dividing
2372 by two until the fraction is between 0.5 and 1.0. */
2380 if (value
>= tmp
) /* Value >= 1.0 */
2381 while (value
>= tmp
)
2386 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
2400 #endif /* HAVE_LONG_DOUBLE */
2403 /* The converse: convert the DOUBLEST *FROM to an extended float
2404 and store where TO points. Neither FROM nor TO have any alignment
2408 floatformat_from_doublest (fmt
, from
, to
)
2409 CONST
struct floatformat
*fmt
;
2416 unsigned int mant_bits
, mant_off
;
2418 unsigned char *uto
= (unsigned char *)to
;
2420 memcpy (&dfrom
, from
, sizeof (dfrom
));
2421 memset (uto
, 0, fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
);
2423 return; /* Result is zero */
2424 if (dfrom
!= dfrom
) /* Result is NaN */
2427 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
2428 fmt
->exp_len
, fmt
->exp_nan
);
2429 /* Be sure it's not infinity, but NaN value is irrel */
2430 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
2435 /* If negative, set the sign bit. */
2438 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
2442 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
2444 /* Infinity exponent is same as NaN's. */
2445 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
2446 fmt
->exp_len
, fmt
->exp_nan
);
2447 /* Infinity mantissa is all zeroes. */
2448 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
2453 #ifdef HAVE_LONG_DOUBLE
2454 mant
= ldfrexp (dfrom
, &exponent
);
2456 mant
= frexp (dfrom
, &exponent
);
2459 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
2460 exponent
+ fmt
->exp_bias
- 1);
2462 mant_bits_left
= fmt
->man_len
;
2463 mant_off
= fmt
->man_start
;
2464 while (mant_bits_left
> 0)
2466 unsigned long mant_long
;
2467 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
2469 mant
*= 4294967296.0;
2470 mant_long
= (unsigned long)mant
;
2473 /* If the integer bit is implicit, then we need to discard it.
2474 If we are discarding a zero, we should be (but are not) creating
2475 a denormalized number which means adjusting the exponent
2477 if (mant_bits_left
== fmt
->man_len
2478 && fmt
->intbit
== floatformat_intbit_no
)
2486 /* The bits we want are in the most significant MANT_BITS bits of
2487 mant_long. Move them to the least significant. */
2488 mant_long
>>= 32 - mant_bits
;
2491 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
2492 mant_off
, mant_bits
, mant_long
);
2493 mant_off
+= mant_bits
;
2494 mant_bits_left
-= mant_bits
;
2496 if (fmt
-> byteorder
== floatformat_littlebyte_bigword
)
2499 unsigned char *swaplow
= uto
;
2500 unsigned char *swaphigh
= uto
+ 4;
2503 for (count
= 0; count
< 4; count
++)
2506 *swaplow
++ = *swaphigh
;
2512 /* temporary storage using circular buffer */
2518 static char buf
[NUMCELLS
][CELLSIZE
];
2520 if (++cell
>=NUMCELLS
) cell
=0;
2524 /* print routines to handle variable size regs, etc.
2526 FIXME: Note that t_addr is a bfd_vma, which is currently either an
2527 unsigned long or unsigned long long, determined at configure time.
2528 If t_addr is an unsigned long long and sizeof (unsigned long long)
2529 is greater than sizeof (unsigned long), then I believe this code will
2530 probably lose, at least for little endian machines. I believe that
2531 it would also be better to eliminate the switch on the absolute size
2532 of t_addr and replace it with a sequence of if statements that compare
2533 sizeof t_addr with sizeof the various types and do the right thing,
2534 which includes knowing whether or not the host supports long long.
2539 static int thirty_two
= 32; /* eliminate warning from compiler on 32-bit systems */
2545 char *paddr_str
=get_cell();
2546 switch (sizeof(t_addr
))
2549 sprintf (paddr_str
, "%08lx%08lx",
2550 (unsigned long) (addr
>> thirty_two
), (unsigned long) (addr
& 0xffffffff));
2553 sprintf (paddr_str
, "%08lx", (unsigned long) addr
);
2556 sprintf (paddr_str
, "%04x", (unsigned short) (addr
& 0xffff));
2559 sprintf (paddr_str
, "%lx", (unsigned long) addr
);
2568 char *preg_str
=get_cell();
2569 switch (sizeof(t_reg
))
2572 sprintf (preg_str
, "%08lx%08lx",
2573 (unsigned long) (reg
>> thirty_two
), (unsigned long) (reg
& 0xffffffff));
2576 sprintf (preg_str
, "%08lx", (unsigned long) reg
);
2579 sprintf (preg_str
, "%04x", (unsigned short) (reg
& 0xffff));
2582 sprintf (preg_str
, "%lx", (unsigned long) reg
);
2591 char *paddr_str
=get_cell();
2592 switch (sizeof(t_addr
))
2596 unsigned long high
= (unsigned long) (addr
>> thirty_two
);
2598 sprintf (paddr_str
, "%lx", (unsigned long) (addr
& 0xffffffff));
2600 sprintf (paddr_str
, "%lx%08lx",
2601 high
, (unsigned long) (addr
& 0xffffffff));
2605 sprintf (paddr_str
, "%lx", (unsigned long) addr
);
2608 sprintf (paddr_str
, "%x", (unsigned short) (addr
& 0xffff));
2611 sprintf (paddr_str
,"%lx", (unsigned long) addr
);
2620 char *preg_str
=get_cell();
2621 switch (sizeof(t_reg
))
2625 unsigned long high
= (unsigned long) (reg
>> thirty_two
);
2627 sprintf (preg_str
, "%lx", (unsigned long) (reg
& 0xffffffff));
2629 sprintf (preg_str
, "%lx%08lx",
2630 high
, (unsigned long) (reg
& 0xffffffff));
2634 sprintf (preg_str
, "%lx", (unsigned long) reg
);
2637 sprintf (preg_str
, "%x", (unsigned short) (reg
& 0xffff));
2640 sprintf (preg_str
, "%lx", (unsigned long) reg
);