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 (USE_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_DRAIN_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 (USE_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 /* USE_MMALLOC */
746 #if !defined (USE_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
);
1215 static char * hexlate
= "0123456789abcdef" ;
1216 int fmthex(inbuf
,outbuff
,length
,linelength
)
1217 unsigned char * inbuf
;
1218 unsigned char * outbuff
;
1222 unsigned char byte
, nib
;
1227 if (outlength
>= linelength
) break ;
1231 *outbuff
++ = hexlate
[nib
] ;
1233 *outbuff
++ = hexlate
[nib
] ;
1238 *outbuff
= '\0' ; /* null terminate our output line */
1243 /* Number of lines per page or UINT_MAX if paging is disabled. */
1244 static unsigned int lines_per_page
;
1245 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1246 static unsigned int chars_per_line
;
1247 /* Current count of lines printed on this page, chars on this line. */
1248 static unsigned int lines_printed
, chars_printed
;
1250 /* Buffer and start column of buffered text, for doing smarter word-
1251 wrapping. When someone calls wrap_here(), we start buffering output
1252 that comes through fputs_filtered(). If we see a newline, we just
1253 spit it out and forget about the wrap_here(). If we see another
1254 wrap_here(), we spit it out and remember the newer one. If we see
1255 the end of the line, we spit out a newline, the indent, and then
1256 the buffered output. */
1258 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1259 are waiting to be output (they have already been counted in chars_printed).
1260 When wrap_buffer[0] is null, the buffer is empty. */
1261 static char *wrap_buffer
;
1263 /* Pointer in wrap_buffer to the next character to fill. */
1264 static char *wrap_pointer
;
1266 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1268 static char *wrap_indent
;
1270 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1271 is not in effect. */
1272 static int wrap_column
;
1276 set_width_command (args
, from_tty
, c
)
1279 struct cmd_list_element
*c
;
1283 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
1284 wrap_buffer
[0] = '\0';
1287 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
1288 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
1291 /* Wait, so the user can read what's on the screen. Prompt the user
1292 to continue by pressing RETURN. */
1295 prompt_for_continue ()
1298 char cont_prompt
[120];
1300 if (annotation_level
> 1)
1301 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1303 strcpy (cont_prompt
,
1304 "---Type <return> to continue, or q <return> to quit---");
1305 if (annotation_level
> 1)
1306 strcat (cont_prompt
, "\n\032\032prompt-for-continue\n");
1308 /* We must do this *before* we call gdb_readline, else it will eventually
1309 call us -- thinking that we're trying to print beyond the end of the
1311 reinitialize_more_filter ();
1314 /* On a real operating system, the user can quit with SIGINT.
1317 'q' is provided on all systems so users don't have to change habits
1318 from system to system, and because telling them what to do in
1319 the prompt is more user-friendly than expecting them to think of
1321 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1322 whereas control-C to gdb_readline will cause the user to get dumped
1324 ignore
= readline (cont_prompt
);
1326 if (annotation_level
> 1)
1327 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1332 while (*p
== ' ' || *p
== '\t')
1335 request_quit (SIGINT
);
1340 /* Now we have to do this again, so that GDB will know that it doesn't
1341 need to save the ---Type <return>--- line at the top of the screen. */
1342 reinitialize_more_filter ();
1344 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1347 /* Reinitialize filter; ie. tell it to reset to original values. */
1350 reinitialize_more_filter ()
1356 /* Indicate that if the next sequence of characters overflows the line,
1357 a newline should be inserted here rather than when it hits the end.
1358 If INDENT is non-null, it is a string to be printed to indent the
1359 wrapped part on the next line. INDENT must remain accessible until
1360 the next call to wrap_here() or until a newline is printed through
1363 If the line is already overfull, we immediately print a newline and
1364 the indentation, and disable further wrapping.
1366 If we don't know the width of lines, but we know the page height,
1367 we must not wrap words, but should still keep track of newlines
1368 that were explicitly printed.
1370 INDENT should not contain tabs, as that will mess up the char count
1371 on the next line. FIXME.
1373 This routine is guaranteed to force out any output which has been
1374 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1375 used to force out output from the wrap_buffer. */
1381 /* This should have been allocated, but be paranoid anyway. */
1387 *wrap_pointer
= '\0';
1388 fputs_unfiltered (wrap_buffer
, gdb_stdout
);
1390 wrap_pointer
= wrap_buffer
;
1391 wrap_buffer
[0] = '\0';
1392 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
1396 else if (chars_printed
>= chars_per_line
)
1398 puts_filtered ("\n");
1400 puts_filtered (indent
);
1405 wrap_column
= chars_printed
;
1409 wrap_indent
= indent
;
1413 /* Ensure that whatever gets printed next, using the filtered output
1414 commands, starts at the beginning of the line. I.E. if there is
1415 any pending output for the current line, flush it and start a new
1416 line. Otherwise do nothing. */
1421 if (chars_printed
> 0)
1423 puts_filtered ("\n");
1429 gdb_fopen (name
, mode
)
1433 return fopen (name
, mode
);
1441 && (stream
== gdb_stdout
1442 || stream
== gdb_stderr
))
1444 flush_hook (stream
);
1451 /* Like fputs but if FILTER is true, pause after every screenful.
1453 Regardless of FILTER can wrap at points other than the final
1454 character of a line.
1456 Unlike fputs, fputs_maybe_filtered does not return a value.
1457 It is OK for LINEBUFFER to be NULL, in which case just don't print
1460 Note that a longjmp to top level may occur in this routine (only if
1461 FILTER is true) (since prompt_for_continue may do so) so this
1462 routine should not be called when cleanups are not in place. */
1465 fputs_maybe_filtered (linebuffer
, stream
, filter
)
1466 const char *linebuffer
;
1470 const char *lineptr
;
1472 if (linebuffer
== 0)
1475 /* Don't do any filtering if it is disabled. */
1476 if (stream
!= gdb_stdout
1477 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
1479 fputs_unfiltered (linebuffer
, stream
);
1483 /* Go through and output each character. Show line extension
1484 when this is necessary; prompt user for new page when this is
1487 lineptr
= linebuffer
;
1490 /* Possible new page. */
1492 (lines_printed
>= lines_per_page
- 1))
1493 prompt_for_continue ();
1495 while (*lineptr
&& *lineptr
!= '\n')
1497 /* Print a single line. */
1498 if (*lineptr
== '\t')
1501 *wrap_pointer
++ = '\t';
1503 fputc_unfiltered ('\t', stream
);
1504 /* Shifting right by 3 produces the number of tab stops
1505 we have already passed, and then adding one and
1506 shifting left 3 advances to the next tab stop. */
1507 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
1513 *wrap_pointer
++ = *lineptr
;
1515 fputc_unfiltered (*lineptr
, stream
);
1520 if (chars_printed
>= chars_per_line
)
1522 unsigned int save_chars
= chars_printed
;
1526 /* If we aren't actually wrapping, don't output newline --
1527 if chars_per_line is right, we probably just overflowed
1528 anyway; if it's wrong, let us keep going. */
1530 fputc_unfiltered ('\n', stream
);
1532 /* Possible new page. */
1533 if (lines_printed
>= lines_per_page
- 1)
1534 prompt_for_continue ();
1536 /* Now output indentation and wrapped string */
1539 fputs_unfiltered (wrap_indent
, stream
);
1540 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
1541 fputs_unfiltered (wrap_buffer
, stream
); /* and eject it */
1542 /* FIXME, this strlen is what prevents wrap_indent from
1543 containing tabs. However, if we recurse to print it
1544 and count its chars, we risk trouble if wrap_indent is
1545 longer than (the user settable) chars_per_line.
1546 Note also that this can set chars_printed > chars_per_line
1547 if we are printing a long string. */
1548 chars_printed
= strlen (wrap_indent
)
1549 + (save_chars
- wrap_column
);
1550 wrap_pointer
= wrap_buffer
; /* Reset buffer */
1551 wrap_buffer
[0] = '\0';
1552 wrap_column
= 0; /* And disable fancy wrap */
1557 if (*lineptr
== '\n')
1560 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1562 fputc_unfiltered ('\n', stream
);
1569 fputs_filtered (linebuffer
, stream
)
1570 const char *linebuffer
;
1573 fputs_maybe_filtered (linebuffer
, stream
, 1);
1577 putchar_unfiltered (c
)
1584 fputs_unfiltered (buf
, gdb_stdout
);
1589 fputc_unfiltered (c
, stream
)
1597 fputs_unfiltered (buf
, stream
);
1602 /* puts_debug is like fputs_unfiltered, except it prints special
1603 characters in printable fashion. */
1606 puts_debug (prefix
, string
, suffix
)
1613 /* Print prefix and suffix after each line. */
1614 static int new_line
= 1;
1615 static int carriage_return
= 0;
1616 static char *prev_prefix
= "";
1617 static char *prev_suffix
= "";
1619 if (*string
== '\n')
1620 carriage_return
= 0;
1622 /* If the prefix is changing, print the previous suffix, a new line,
1623 and the new prefix. */
1624 if ((carriage_return
|| (strcmp(prev_prefix
, prefix
) != 0)) && !new_line
)
1626 fputs_unfiltered (prev_suffix
, gdb_stderr
);
1627 fputs_unfiltered ("\n", gdb_stderr
);
1628 fputs_unfiltered (prefix
, gdb_stderr
);
1631 /* Print prefix if we printed a newline during the previous call. */
1635 fputs_unfiltered (prefix
, gdb_stderr
);
1638 prev_prefix
= prefix
;
1639 prev_suffix
= suffix
;
1641 /* Output characters in a printable format. */
1642 while ((ch
= *string
++) != '\0')
1648 fputc_unfiltered (ch
, gdb_stderr
);
1651 fprintf_unfiltered (gdb_stderr
, "\\%03o", ch
);
1654 case '\\': fputs_unfiltered ("\\\\", gdb_stderr
); break;
1655 case '\b': fputs_unfiltered ("\\b", gdb_stderr
); break;
1656 case '\f': fputs_unfiltered ("\\f", gdb_stderr
); break;
1657 case '\n': new_line
= 1;
1658 fputs_unfiltered ("\\n", gdb_stderr
); break;
1659 case '\r': fputs_unfiltered ("\\r", gdb_stderr
); break;
1660 case '\t': fputs_unfiltered ("\\t", gdb_stderr
); break;
1661 case '\v': fputs_unfiltered ("\\v", gdb_stderr
); break;
1664 carriage_return
= ch
== '\r';
1667 /* Print suffix if we printed a newline. */
1670 fputs_unfiltered (suffix
, gdb_stderr
);
1671 fputs_unfiltered ("\n", gdb_stderr
);
1676 /* Print a variable number of ARGS using format FORMAT. If this
1677 information is going to put the amount written (since the last call
1678 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1679 call prompt_for_continue to get the users permision to continue.
1681 Unlike fprintf, this function does not return a value.
1683 We implement three variants, vfprintf (takes a vararg list and stream),
1684 fprintf (takes a stream to write on), and printf (the usual).
1686 Note also that a longjmp to top level may occur in this routine
1687 (since prompt_for_continue may do so) so this routine should not be
1688 called when cleanups are not in place. */
1691 vfprintf_maybe_filtered (stream
, format
, args
, filter
)
1698 struct cleanup
*old_cleanups
;
1700 vasprintf (&linebuffer
, format
, args
);
1701 if (linebuffer
== NULL
)
1703 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1706 old_cleanups
= make_cleanup (free
, linebuffer
);
1707 fputs_maybe_filtered (linebuffer
, stream
, filter
);
1708 do_cleanups (old_cleanups
);
1713 vfprintf_filtered (stream
, format
, args
)
1718 vfprintf_maybe_filtered (stream
, format
, args
, 1);
1722 vfprintf_unfiltered (stream
, format
, args
)
1728 struct cleanup
*old_cleanups
;
1730 vasprintf (&linebuffer
, format
, args
);
1731 if (linebuffer
== NULL
)
1733 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
1736 old_cleanups
= make_cleanup (free
, linebuffer
);
1737 fputs_unfiltered (linebuffer
, stream
);
1738 do_cleanups (old_cleanups
);
1742 vprintf_filtered (format
, args
)
1746 vfprintf_maybe_filtered (gdb_stdout
, format
, args
, 1);
1750 vprintf_unfiltered (format
, args
)
1754 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1759 #ifdef ANSI_PROTOTYPES
1760 fprintf_filtered (FILE *stream
, const char *format
, ...)
1762 fprintf_filtered (va_alist
)
1767 #ifdef ANSI_PROTOTYPES
1768 va_start (args
, format
);
1774 stream
= va_arg (args
, FILE *);
1775 format
= va_arg (args
, char *);
1777 vfprintf_filtered (stream
, format
, args
);
1783 #ifdef ANSI_PROTOTYPES
1784 fprintf_unfiltered (FILE *stream
, const char *format
, ...)
1786 fprintf_unfiltered (va_alist
)
1791 #ifdef ANSI_PROTOTYPES
1792 va_start (args
, format
);
1798 stream
= va_arg (args
, FILE *);
1799 format
= va_arg (args
, char *);
1801 vfprintf_unfiltered (stream
, format
, args
);
1805 /* Like fprintf_filtered, but prints its result indented.
1806 Called as fprintfi_filtered (spaces, stream, format, ...); */
1810 #ifdef ANSI_PROTOTYPES
1811 fprintfi_filtered (int spaces
, FILE *stream
, const char *format
, ...)
1813 fprintfi_filtered (va_alist
)
1818 #ifdef ANSI_PROTOTYPES
1819 va_start (args
, format
);
1826 spaces
= va_arg (args
, int);
1827 stream
= va_arg (args
, FILE *);
1828 format
= va_arg (args
, char *);
1830 print_spaces_filtered (spaces
, stream
);
1832 vfprintf_filtered (stream
, format
, args
);
1839 #ifdef ANSI_PROTOTYPES
1840 printf_filtered (const char *format
, ...)
1842 printf_filtered (va_alist
)
1847 #ifdef ANSI_PROTOTYPES
1848 va_start (args
, format
);
1853 format
= va_arg (args
, char *);
1855 vfprintf_filtered (gdb_stdout
, format
, args
);
1862 #ifdef ANSI_PROTOTYPES
1863 printf_unfiltered (const char *format
, ...)
1865 printf_unfiltered (va_alist
)
1870 #ifdef ANSI_PROTOTYPES
1871 va_start (args
, format
);
1876 format
= va_arg (args
, char *);
1878 vfprintf_unfiltered (gdb_stdout
, format
, args
);
1882 /* Like printf_filtered, but prints it's result indented.
1883 Called as printfi_filtered (spaces, format, ...); */
1887 #ifdef ANSI_PROTOTYPES
1888 printfi_filtered (int spaces
, const char *format
, ...)
1890 printfi_filtered (va_alist
)
1895 #ifdef ANSI_PROTOTYPES
1896 va_start (args
, format
);
1902 spaces
= va_arg (args
, int);
1903 format
= va_arg (args
, char *);
1905 print_spaces_filtered (spaces
, gdb_stdout
);
1906 vfprintf_filtered (gdb_stdout
, format
, args
);
1910 /* Easy -- but watch out!
1912 This routine is *not* a replacement for puts()! puts() appends a newline.
1913 This one doesn't, and had better not! */
1916 puts_filtered (string
)
1919 fputs_filtered (string
, gdb_stdout
);
1923 puts_unfiltered (string
)
1926 fputs_unfiltered (string
, gdb_stdout
);
1929 /* Return a pointer to N spaces and a null. The pointer is good
1930 until the next call to here. */
1936 static char *spaces
;
1937 static int max_spaces
;
1943 spaces
= (char *) xmalloc (n
+1);
1944 for (t
= spaces
+n
; t
!= spaces
;)
1950 return spaces
+ max_spaces
- n
;
1953 /* Print N spaces. */
1955 print_spaces_filtered (n
, stream
)
1959 fputs_filtered (n_spaces (n
), stream
);
1962 /* C++ demangler stuff. */
1964 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1965 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1966 If the name is not mangled, or the language for the name is unknown, or
1967 demangling is off, the name is printed in its "raw" form. */
1970 fprintf_symbol_filtered (stream
, name
, lang
, arg_mode
)
1980 /* If user wants to see raw output, no problem. */
1983 fputs_filtered (name
, stream
);
1989 case language_cplus
:
1990 demangled
= cplus_demangle (name
, arg_mode
);
1993 demangled
= cplus_demangle (name
, arg_mode
| DMGL_JAVA
);
1995 case language_chill
:
1996 demangled
= chill_demangle (name
);
2002 fputs_filtered (demangled
? demangled
: name
, stream
);
2003 if (demangled
!= NULL
)
2011 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
2012 differences in whitespace. Returns 0 if they match, non-zero if they
2013 don't (slightly different than strcmp()'s range of return values).
2015 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2016 This "feature" is useful when searching for matching C++ function names
2017 (such as if the user types 'break FOO', where FOO is a mangled C++
2021 strcmp_iw (string1
, string2
)
2022 const char *string1
;
2023 const char *string2
;
2025 while ((*string1
!= '\0') && (*string2
!= '\0'))
2027 while (isspace (*string1
))
2031 while (isspace (*string2
))
2035 if (*string1
!= *string2
)
2039 if (*string1
!= '\0')
2045 return (*string1
!= '\0' && *string1
!= '(') || (*string2
!= '\0');
2052 struct cmd_list_element
*c
;
2054 c
= add_set_cmd ("width", class_support
, var_uinteger
,
2055 (char *)&chars_per_line
,
2056 "Set number of characters gdb thinks are in a line.",
2058 add_show_from_set (c
, &showlist
);
2059 c
->function
.sfunc
= set_width_command
;
2062 (add_set_cmd ("height", class_support
,
2063 var_uinteger
, (char *)&lines_per_page
,
2064 "Set number of lines gdb thinks are in a page.", &setlist
),
2067 /* These defaults will be used if we are unable to get the correct
2068 values from termcap. */
2069 #if defined(__GO32__)
2070 lines_per_page
= ScreenRows();
2071 chars_per_line
= ScreenCols();
2073 lines_per_page
= 24;
2074 chars_per_line
= 80;
2076 #if !defined (MPW) && !defined (_WIN32)
2077 /* No termcap under MPW, although might be cool to do something
2078 by looking at worksheet or console window sizes. */
2079 /* Initialize the screen height and width from termcap. */
2081 char *termtype
= getenv ("TERM");
2083 /* Positive means success, nonpositive means failure. */
2086 /* 2048 is large enough for all known terminals, according to the
2087 GNU termcap manual. */
2088 char term_buffer
[2048];
2092 status
= tgetent (term_buffer
, termtype
);
2097 val
= tgetnum ("li");
2099 lines_per_page
= val
;
2101 /* The number of lines per page is not mentioned
2102 in the terminal description. This probably means
2103 that paging is not useful (e.g. emacs shell window),
2104 so disable paging. */
2105 lines_per_page
= UINT_MAX
;
2107 val
= tgetnum ("co");
2109 chars_per_line
= val
;
2115 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2117 /* If there is a better way to determine the window size, use it. */
2118 SIGWINCH_HANDLER ();
2121 /* If the output is not a terminal, don't paginate it. */
2122 if (!ISATTY (gdb_stdout
))
2123 lines_per_page
= UINT_MAX
;
2125 set_width_command ((char *)NULL
, 0, c
);
2128 (add_set_cmd ("demangle", class_support
, var_boolean
,
2130 "Set demangling of encoded C++ names when displaying symbols.",
2135 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
2136 (char *)&sevenbit_strings
,
2137 "Set printing of 8-bit characters in strings as \\nnn.",
2142 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
2143 (char *)&asm_demangle
,
2144 "Set demangling of C++ names in disassembly listings.",
2149 /* Machine specific function to handle SIGWINCH signal. */
2151 #ifdef SIGWINCH_HANDLER_BODY
2152 SIGWINCH_HANDLER_BODY
2155 /* Support for converting target fp numbers into host DOUBLEST format. */
2157 /* XXX - This code should really be in libiberty/floatformat.c, however
2158 configuration issues with libiberty made this very difficult to do in the
2161 #include "floatformat.h"
2162 #include <math.h> /* ldexp */
2164 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
2165 going to bother with trying to muck around with whether it is defined in
2166 a system header, what we do if not, etc. */
2167 #define FLOATFORMAT_CHAR_BIT 8
2169 static unsigned long get_field
PARAMS ((unsigned char *,
2170 enum floatformat_byteorders
,
2175 /* Extract a field which starts at START and is LEN bytes long. DATA and
2176 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2177 static unsigned long
2178 get_field (data
, order
, total_len
, start
, len
)
2179 unsigned char *data
;
2180 enum floatformat_byteorders order
;
2181 unsigned int total_len
;
2185 unsigned long result
;
2186 unsigned int cur_byte
;
2189 /* Start at the least significant part of the field. */
2190 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
2191 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2192 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
2194 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
2195 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
2196 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
2197 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2202 /* Move towards the most significant part of the field. */
2203 while (cur_bitshift
< len
)
2205 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
2206 /* This is the last byte; zero out the bits which are not part of
2209 (*(data
+ cur_byte
) & ((1 << (len
- cur_bitshift
)) - 1))
2212 result
|= *(data
+ cur_byte
) << cur_bitshift
;
2213 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
2214 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2222 /* Convert from FMT to a DOUBLEST.
2223 FROM is the address of the extended float.
2224 Store the DOUBLEST in *TO. */
2227 floatformat_to_doublest (fmt
, from
, to
)
2228 const struct floatformat
*fmt
;
2232 unsigned char *ufrom
= (unsigned char *)from
;
2236 unsigned int mant_bits
, mant_off
;
2238 int special_exponent
; /* It's a NaN, denorm or zero */
2240 /* If the mantissa bits are not contiguous from one end of the
2241 mantissa to the other, we need to make a private copy of the
2242 source bytes that is in the right order since the unpacking
2243 algorithm assumes that the bits are contiguous.
2245 Swap the bytes individually rather than accessing them through
2246 "long *" since we have no guarantee that they start on a long
2247 alignment, and also sizeof(long) for the host could be different
2248 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
2249 for the target is 4. */
2251 if (fmt
-> byteorder
== floatformat_littlebyte_bigword
)
2253 static unsigned char *newfrom
;
2254 unsigned char *swapin
, *swapout
;
2257 longswaps
= fmt
-> totalsize
/ FLOATFORMAT_CHAR_BIT
;
2260 if (newfrom
== NULL
)
2262 newfrom
= xmalloc (fmt
-> totalsize
);
2267 while (longswaps
-- > 0)
2269 /* This is ugly, but efficient */
2270 *swapout
++ = swapin
[4];
2271 *swapout
++ = swapin
[5];
2272 *swapout
++ = swapin
[6];
2273 *swapout
++ = swapin
[7];
2274 *swapout
++ = swapin
[0];
2275 *swapout
++ = swapin
[1];
2276 *swapout
++ = swapin
[2];
2277 *swapout
++ = swapin
[3];
2282 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
2283 fmt
->exp_start
, fmt
->exp_len
);
2284 /* Note that if exponent indicates a NaN, we can't really do anything useful
2285 (not knowing if the host has NaN's, or how to build one). So it will
2286 end up as an infinity or something close; that is OK. */
2288 mant_bits_left
= fmt
->man_len
;
2289 mant_off
= fmt
->man_start
;
2292 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
2294 /* Don't bias zero's, denorms or NaNs. */
2295 if (!special_exponent
)
2296 exponent
-= fmt
->exp_bias
;
2298 /* Build the result algebraically. Might go infinite, underflow, etc;
2301 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
2302 increment the exponent by one to account for the integer bit. */
2304 if (!special_exponent
)
2305 if (fmt
->intbit
== floatformat_intbit_no
)
2306 dto
= ldexp (1.0, exponent
);
2310 while (mant_bits_left
> 0)
2312 mant_bits
= min (mant_bits_left
, 32);
2314 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
2315 mant_off
, mant_bits
);
2317 dto
+= ldexp ((double)mant
, exponent
- mant_bits
);
2318 exponent
-= mant_bits
;
2319 mant_off
+= mant_bits
;
2320 mant_bits_left
-= mant_bits
;
2323 /* Negate it if negative. */
2324 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
2329 static void put_field
PARAMS ((unsigned char *, enum floatformat_byteorders
,
2335 /* Set a field which starts at START and is LEN bytes long. DATA and
2336 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2338 put_field (data
, order
, total_len
, start
, len
, stuff_to_put
)
2339 unsigned char *data
;
2340 enum floatformat_byteorders order
;
2341 unsigned int total_len
;
2344 unsigned long stuff_to_put
;
2346 unsigned int cur_byte
;
2349 /* Start at the least significant part of the field. */
2350 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
2351 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2352 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
2354 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
2355 *(data
+ cur_byte
) &=
2356 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1) << (-cur_bitshift
));
2357 *(data
+ cur_byte
) |=
2358 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
2359 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
2360 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2365 /* Move towards the most significant part of the field. */
2366 while (cur_bitshift
< len
)
2368 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
2370 /* This is the last byte. */
2371 *(data
+ cur_byte
) &=
2372 ~((1 << (len
- cur_bitshift
)) - 1);
2373 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
2376 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
2377 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
2378 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
2379 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
2386 #ifdef HAVE_LONG_DOUBLE
2387 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
2388 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
2389 frexp, but operates on the long double data type. */
2391 static long double ldfrexp
PARAMS ((long double value
, int *eptr
));
2394 ldfrexp (value
, eptr
)
2401 /* Unfortunately, there are no portable functions for extracting the exponent
2402 of a long double, so we have to do it iteratively by multiplying or dividing
2403 by two until the fraction is between 0.5 and 1.0. */
2411 if (value
>= tmp
) /* Value >= 1.0 */
2412 while (value
>= tmp
)
2417 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
2431 #endif /* HAVE_LONG_DOUBLE */
2434 /* The converse: convert the DOUBLEST *FROM to an extended float
2435 and store where TO points. Neither FROM nor TO have any alignment
2439 floatformat_from_doublest (fmt
, from
, to
)
2440 CONST
struct floatformat
*fmt
;
2447 unsigned int mant_bits
, mant_off
;
2449 unsigned char *uto
= (unsigned char *)to
;
2451 memcpy (&dfrom
, from
, sizeof (dfrom
));
2452 memset (uto
, 0, fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
);
2454 return; /* Result is zero */
2455 if (dfrom
!= dfrom
) /* Result is NaN */
2458 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
2459 fmt
->exp_len
, fmt
->exp_nan
);
2460 /* Be sure it's not infinity, but NaN value is irrel */
2461 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
2466 /* If negative, set the sign bit. */
2469 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
2473 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
2475 /* Infinity exponent is same as NaN's. */
2476 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
2477 fmt
->exp_len
, fmt
->exp_nan
);
2478 /* Infinity mantissa is all zeroes. */
2479 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
2484 #ifdef HAVE_LONG_DOUBLE
2485 mant
= ldfrexp (dfrom
, &exponent
);
2487 mant
= frexp (dfrom
, &exponent
);
2490 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
2491 exponent
+ fmt
->exp_bias
- 1);
2493 mant_bits_left
= fmt
->man_len
;
2494 mant_off
= fmt
->man_start
;
2495 while (mant_bits_left
> 0)
2497 unsigned long mant_long
;
2498 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
2500 mant
*= 4294967296.0;
2501 mant_long
= (unsigned long)mant
;
2504 /* If the integer bit is implicit, then we need to discard it.
2505 If we are discarding a zero, we should be (but are not) creating
2506 a denormalized number which means adjusting the exponent
2508 if (mant_bits_left
== fmt
->man_len
2509 && fmt
->intbit
== floatformat_intbit_no
)
2517 /* The bits we want are in the most significant MANT_BITS bits of
2518 mant_long. Move them to the least significant. */
2519 mant_long
>>= 32 - mant_bits
;
2522 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
2523 mant_off
, mant_bits
, mant_long
);
2524 mant_off
+= mant_bits
;
2525 mant_bits_left
-= mant_bits
;
2527 if (fmt
-> byteorder
== floatformat_littlebyte_bigword
)
2530 unsigned char *swaplow
= uto
;
2531 unsigned char *swaphigh
= uto
+ 4;
2534 for (count
= 0; count
< 4; count
++)
2537 *swaplow
++ = *swaphigh
;
2543 /* temporary storage using circular buffer */
2549 static char buf
[NUMCELLS
][CELLSIZE
];
2551 if (++cell
>=NUMCELLS
) cell
=0;
2555 /* print routines to handle variable size regs, etc.
2557 FIXME: Note that t_addr is a bfd_vma, which is currently either an
2558 unsigned long or unsigned long long, determined at configure time.
2559 If t_addr is an unsigned long long and sizeof (unsigned long long)
2560 is greater than sizeof (unsigned long), then I believe this code will
2561 probably lose, at least for little endian machines. I believe that
2562 it would also be better to eliminate the switch on the absolute size
2563 of t_addr and replace it with a sequence of if statements that compare
2564 sizeof t_addr with sizeof the various types and do the right thing,
2565 which includes knowing whether or not the host supports long long.
2570 static int thirty_two
= 32; /* eliminate warning from compiler on 32-bit systems */
2576 char *paddr_str
=get_cell();
2577 switch (sizeof(t_addr
))
2580 sprintf (paddr_str
, "%08lx%08lx",
2581 (unsigned long) (addr
>> thirty_two
), (unsigned long) (addr
& 0xffffffff));
2584 sprintf (paddr_str
, "%08lx", (unsigned long) addr
);
2587 sprintf (paddr_str
, "%04x", (unsigned short) (addr
& 0xffff));
2590 sprintf (paddr_str
, "%lx", (unsigned long) addr
);
2599 char *preg_str
=get_cell();
2600 switch (sizeof(t_reg
))
2603 sprintf (preg_str
, "%08lx%08lx",
2604 (unsigned long) (reg
>> thirty_two
), (unsigned long) (reg
& 0xffffffff));
2607 sprintf (preg_str
, "%08lx", (unsigned long) reg
);
2610 sprintf (preg_str
, "%04x", (unsigned short) (reg
& 0xffff));
2613 sprintf (preg_str
, "%lx", (unsigned long) reg
);
2622 char *paddr_str
=get_cell();
2623 switch (sizeof(t_addr
))
2627 unsigned long high
= (unsigned long) (addr
>> thirty_two
);
2629 sprintf (paddr_str
, "%lx", (unsigned long) (addr
& 0xffffffff));
2631 sprintf (paddr_str
, "%lx%08lx",
2632 high
, (unsigned long) (addr
& 0xffffffff));
2636 sprintf (paddr_str
, "%lx", (unsigned long) addr
);
2639 sprintf (paddr_str
, "%x", (unsigned short) (addr
& 0xffff));
2642 sprintf (paddr_str
,"%lx", (unsigned long) addr
);
2651 char *preg_str
=get_cell();
2652 switch (sizeof(t_reg
))
2656 unsigned long high
= (unsigned long) (reg
>> thirty_two
);
2658 sprintf (preg_str
, "%lx", (unsigned long) (reg
& 0xffffffff));
2660 sprintf (preg_str
, "%lx%08lx",
2661 high
, (unsigned long) (reg
& 0xffffffff));
2665 sprintf (preg_str
, "%lx", (unsigned long) reg
);
2668 sprintf (preg_str
, "%x", (unsigned short) (reg
& 0xffff));
2671 sprintf (preg_str
, "%lx", (unsigned long) reg
);