1 /* General utility routines for GDB, the GNU debugger.
2 Copyright 1986, 89, 90, 91, 92, 95, 96, 1998 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. */
22 #include "gdb_string.h"
34 /* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */
45 #include "expression.h"
49 #include "readline/readline.h"
51 /* readline defines this. */
54 /* Prototypes for local functions */
56 static void vfprintf_maybe_filtered PARAMS ((FILE *, const char *, va_list, int));
58 static void fputs_maybe_filtered PARAMS ((const char *, FILE *, int));
60 #if defined (USE_MMALLOC) && !defined (NO_MMCHECK)
61 static void malloc_botch PARAMS ((void));
65 fatal_dump_core PARAMS((char *, ...));
68 prompt_for_continue PARAMS ((void));
71 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
73 /* If this definition isn't overridden by the header files, assume
74 that isatty and fileno exist on this system. */
76 #define ISATTY(FP) (isatty (fileno (FP)))
79 /* Chain of cleanup actions established with make_cleanup,
80 to be executed if an error happens. */
82 static struct cleanup *cleanup_chain; /* cleaned up after a failed command */
83 static struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */
84 static struct cleanup *run_cleanup_chain; /* cleaned up on each 'run' */
86 /* Nonzero if we have job control. */
90 /* Nonzero means a quit has been requested. */
94 /* Nonzero means quit immediately if Control-C is typed now, rather
95 than waiting until QUIT is executed. Be careful in setting this;
96 code which executes with immediate_quit set has to be very careful
97 about being able to deal with being interrupted at any time. It is
98 almost always better to use QUIT; the only exception I can think of
99 is being able to quit out of a system call (using EINTR loses if
100 the SIGINT happens between the previous QUIT and the system call).
101 To immediately quit in the case in which a SIGINT happens between
102 the previous QUIT and setting immediate_quit (desirable anytime we
103 expect to block), call QUIT after setting immediate_quit. */
107 /* Nonzero means that encoded C++ names should be printed out in their
108 C++ form rather than raw. */
112 /* Nonzero means that encoded C++ names should be printed out in their
113 C++ form even in assembler language displays. If this is set, but
114 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
116 int asm_demangle = 0;
118 /* Nonzero means that strings with character values >0x7F should be printed
119 as octal escapes. Zero means just print the value (e.g. it's an
120 international character, and the terminal or window can cope.) */
122 int sevenbit_strings = 0;
124 /* String to be printed before error messages, if any. */
126 char *error_pre_print;
128 /* String to be printed before quit messages, if any. */
130 char *quit_pre_print;
132 /* String to be printed before warning messages, if any. */
134 char *warning_pre_print = "\nwarning: ";
136 /* Add a new cleanup to the cleanup_chain,
137 and return the previous chain pointer
138 to be passed later to do_cleanups or discard_cleanups.
139 Args are FUNCTION to clean up with, and ARG to pass to it. */
142 make_cleanup (function, arg)
143 void (*function) PARAMS ((PTR));
146 return make_my_cleanup (&cleanup_chain, function, arg);
150 make_final_cleanup (function, arg)
151 void (*function) PARAMS ((PTR));
154 return make_my_cleanup (&final_cleanup_chain, function, arg);
157 make_run_cleanup (function, arg)
158 void (*function) PARAMS ((PTR));
161 return make_my_cleanup (&run_cleanup_chain, function, arg);
164 make_my_cleanup (pmy_chain, function, arg)
165 struct cleanup **pmy_chain;
166 void (*function) PARAMS ((PTR));
169 register struct cleanup *new
170 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
171 register struct cleanup *old_chain = *pmy_chain;
173 new->next = *pmy_chain;
174 new->function = function;
181 /* Discard cleanups and do the actions they describe
182 until we get back to the point OLD_CHAIN in the cleanup_chain. */
185 do_cleanups (old_chain)
186 register struct cleanup *old_chain;
188 do_my_cleanups (&cleanup_chain, old_chain);
192 do_final_cleanups (old_chain)
193 register struct cleanup *old_chain;
195 do_my_cleanups (&final_cleanup_chain, old_chain);
199 do_run_cleanups (old_chain)
200 register struct cleanup *old_chain;
202 do_my_cleanups (&run_cleanup_chain, old_chain);
206 do_my_cleanups (pmy_chain, old_chain)
207 register struct cleanup **pmy_chain;
208 register struct cleanup *old_chain;
210 register struct cleanup *ptr;
211 while ((ptr = *pmy_chain) != old_chain)
213 *pmy_chain = ptr->next; /* Do this first incase recursion */
214 (*ptr->function) (ptr->arg);
219 /* Discard cleanups, not doing the actions they describe,
220 until we get back to the point OLD_CHAIN in the cleanup_chain. */
223 discard_cleanups (old_chain)
224 register struct cleanup *old_chain;
226 discard_my_cleanups (&cleanup_chain, old_chain);
230 discard_final_cleanups (old_chain)
231 register struct cleanup *old_chain;
233 discard_my_cleanups (&final_cleanup_chain, old_chain);
237 discard_my_cleanups (pmy_chain, old_chain)
238 register struct cleanup **pmy_chain;
239 register struct cleanup *old_chain;
241 register struct cleanup *ptr;
242 while ((ptr = *pmy_chain) != old_chain)
244 *pmy_chain = ptr->next;
249 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
253 return save_my_cleanups (&cleanup_chain);
257 save_final_cleanups ()
259 return save_my_cleanups (&final_cleanup_chain);
263 save_my_cleanups (pmy_chain)
264 struct cleanup **pmy_chain;
266 struct cleanup *old_chain = *pmy_chain;
272 /* Restore the cleanup chain from a previously saved chain. */
274 restore_cleanups (chain)
275 struct cleanup *chain;
277 restore_my_cleanups (&cleanup_chain, chain);
281 restore_final_cleanups (chain)
282 struct cleanup *chain;
284 restore_my_cleanups (&final_cleanup_chain, chain);
288 restore_my_cleanups (pmy_chain, chain)
289 struct cleanup **pmy_chain;
290 struct cleanup *chain;
295 /* This function is useful for cleanups.
299 old_chain = make_cleanup (free_current_contents, &foo);
301 to arrange to free the object thus allocated. */
304 free_current_contents (location)
310 /* Provide a known function that does nothing, to use as a base for
311 for a possibly long chain of cleanups. This is useful where we
312 use the cleanup chain for handling normal cleanups as well as dealing
313 with cleanups that need to be done as a result of a call to error().
314 In such cases, we may not be certain where the first cleanup is, unless
315 we have a do-nothing one to always use as the base. */
325 /* Print a warning message. Way to use this is to call warning_begin,
326 output the warning message (use unfiltered output to gdb_stderr),
327 ending in a newline. There is not currently a warning_end that you
328 call afterwards, but such a thing might be added if it is useful
329 for a GUI to separate warning messages from other output.
331 FIXME: Why do warnings use unfiltered output and errors filtered?
332 Is this anything other than a historical accident? */
337 target_terminal_ours ();
338 wrap_here(""); /* Force out any buffered output */
339 gdb_flush (gdb_stdout);
340 if (warning_pre_print)
341 fprintf_unfiltered (gdb_stderr, warning_pre_print);
344 /* Print a warning message.
345 The first argument STRING is the warning message, used as a fprintf string,
346 and the remaining args are passed as arguments to it.
347 The primary difference between warnings and errors is that a warning
348 does not force the return to command level. */
352 #ifdef ANSI_PROTOTYPES
353 warning (const char *string, ...)
360 #ifdef ANSI_PROTOTYPES
361 va_start (args, string);
366 string = va_arg (args, char *);
369 (*warning_hook) (string, args);
373 vfprintf_unfiltered (gdb_stderr, string, args);
374 fprintf_unfiltered (gdb_stderr, "\n");
379 /* Start the printing of an error message. Way to use this is to call
380 this, output the error message (use filtered output to gdb_stderr
381 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
382 in a newline, and then call return_to_top_level (RETURN_ERROR).
383 error() provides a convenient way to do this for the special case
384 that the error message can be formatted with a single printf call,
385 but this is more general. */
389 target_terminal_ours ();
390 wrap_here (""); /* Force out any buffered output */
391 gdb_flush (gdb_stdout);
393 annotate_error_begin ();
396 fprintf_filtered (gdb_stderr, error_pre_print);
399 /* Print an error message and return to command level.
400 The first argument STRING is the error message, used as a fprintf string,
401 and the remaining args are passed as arguments to it. */
405 #ifdef ANSI_PROTOTYPES
406 error (const char *string, ...)
413 #ifdef ANSI_PROTOTYPES
414 va_start (args, string);
423 #ifdef ANSI_PROTOTYPES
424 vfprintf_filtered (gdb_stderr, string, args);
429 string1 = va_arg (args, char *);
430 vfprintf_filtered (gdb_stderr, string1, args);
433 fprintf_filtered (gdb_stderr, "\n");
435 return_to_top_level (RETURN_ERROR);
440 /* Print an error message and exit reporting failure.
441 This is for a error that we cannot continue from.
442 The arguments are printed a la printf.
444 This function cannot be declared volatile (NORETURN) in an
445 ANSI environment because exit() is not declared volatile. */
449 #ifdef ANSI_PROTOTYPES
450 fatal (char *string, ...)
457 #ifdef ANSI_PROTOTYPES
458 va_start (args, string);
462 string = va_arg (args, char *);
464 fprintf_unfiltered (gdb_stderr, "\ngdb: ");
465 vfprintf_unfiltered (gdb_stderr, string, args);
466 fprintf_unfiltered (gdb_stderr, "\n");
471 /* Print an error message and exit, dumping core.
472 The arguments are printed a la printf (). */
476 #ifdef ANSI_PROTOTYPES
477 fatal_dump_core (char *string, ...)
479 fatal_dump_core (va_alist)
484 #ifdef ANSI_PROTOTYPES
485 va_start (args, string);
490 string = va_arg (args, char *);
492 /* "internal error" is always correct, since GDB should never dump
493 core, no matter what the input. */
494 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: ");
495 vfprintf_unfiltered (gdb_stderr, string, args);
496 fprintf_unfiltered (gdb_stderr, "\n");
499 signal (SIGQUIT, SIG_DFL);
500 kill (getpid (), SIGQUIT);
501 /* We should never get here, but just in case... */
505 /* The strerror() function can return NULL for errno values that are
506 out of range. Provide a "safe" version that always returns a
510 safe_strerror (errnum)
516 if ((msg = strerror (errnum)) == NULL)
518 sprintf (buf, "(undocumented errno %d)", errnum);
524 /* The strsignal() function can return NULL for signal values that are
525 out of range. Provide a "safe" version that always returns a
529 safe_strsignal (signo)
535 if ((msg = strsignal (signo)) == NULL)
537 sprintf (buf, "(undocumented signal %d)", signo);
544 /* Print the system error message for errno, and also mention STRING
545 as the file name for which the error was encountered.
546 Then return to command level. */
549 perror_with_name (string)
555 err = safe_strerror (errno);
556 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
557 strcpy (combined, string);
558 strcat (combined, ": ");
559 strcat (combined, err);
561 /* I understand setting these is a matter of taste. Still, some people
562 may clear errno but not know about bfd_error. Doing this here is not
564 bfd_set_error (bfd_error_no_error);
567 error ("%s.", combined);
570 /* Print the system error message for ERRCODE, and also mention STRING
571 as the file name for which the error was encountered. */
574 print_sys_errmsg (string, errcode)
581 err = safe_strerror (errcode);
582 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
583 strcpy (combined, string);
584 strcat (combined, ": ");
585 strcat (combined, err);
587 /* We want anything which was printed on stdout to come out first, before
589 gdb_flush (gdb_stdout);
590 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
593 /* Control C eventually causes this to be called, at a convenient time. */
598 serial_t gdb_stdout_serial = serial_fdopen (1);
600 target_terminal_ours ();
602 /* We want all output to appear now, before we print "Quit". We
603 have 3 levels of buffering we have to flush (it's possible that
604 some of these should be changed to flush the lower-level ones
607 /* 1. The _filtered buffer. */
608 wrap_here ((char *)0);
610 /* 2. The stdio buffer. */
611 gdb_flush (gdb_stdout);
612 gdb_flush (gdb_stderr);
614 /* 3. The system-level buffer. */
615 SERIAL_DRAIN_OUTPUT (gdb_stdout_serial);
616 SERIAL_UN_FDOPEN (gdb_stdout_serial);
618 annotate_error_begin ();
620 /* Don't use *_filtered; we don't want to prompt the user to continue. */
622 fprintf_unfiltered (gdb_stderr, quit_pre_print);
625 /* If there is no terminal switching for this target, then we can't
626 possibly get screwed by the lack of job control. */
627 || current_target.to_terminal_ours == NULL)
628 fprintf_unfiltered (gdb_stderr, "Quit\n");
630 fprintf_unfiltered (gdb_stderr,
631 "Quit (expect signal SIGINT when the program is resumed)\n");
632 return_to_top_level (RETURN_QUIT);
636 #if defined(__GO32__)
638 /* In the absence of signals, poll keyboard for a quit.
639 Called from #define QUIT pollquit() in xm-go32.h. */
654 /* We just ignore it */
655 /* FIXME!! Don't think this actually works! */
656 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
661 #elif defined(_MSC_VER) /* should test for wingdb instead? */
664 * Windows translates all keyboard and mouse events
665 * into a message which is appended to the message
666 * queue for the process.
671 int k = win32pollquit();
678 #else /* !defined(__GO32__) && !defined(_MSC_VER) */
682 /* Done by signals */
685 #endif /* !defined(__GO32__) && !defined(_MSC_VER) */
691 if (quit_flag || immediate_quit)
695 /* Control C comes here */
702 /* Restore the signal handler. Harmless with BSD-style signals, needed
703 for System V-style signals. So just always do it, rather than worrying
704 about USG defines and stuff like that. */
705 signal (signo, request_quit);
716 /* Memory management stuff (malloc friends). */
718 /* Make a substitute size_t for non-ANSI compilers. */
720 #ifndef HAVE_STDDEF_H
722 #define size_t unsigned int
726 #if !defined (USE_MMALLOC)
733 return malloc (size);
737 mrealloc (md, ptr, size)
742 if (ptr == 0) /* Guard against old realloc's */
743 return malloc (size);
745 return realloc (ptr, size);
756 #endif /* USE_MMALLOC */
758 #if !defined (USE_MMALLOC) || defined (NO_MMCHECK)
766 #else /* Have mmalloc and want corruption checking */
771 fatal_dump_core ("Memory corruption");
774 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
775 by MD, to detect memory corruption. Note that MD may be NULL to specify
776 the default heap that grows via sbrk.
778 Note that for freshly created regions, we must call mmcheckf prior to any
779 mallocs in the region. Otherwise, any region which was allocated prior to
780 installing the checking hooks, which is later reallocated or freed, will
781 fail the checks! The mmcheck function only allows initial hooks to be
782 installed before the first mmalloc. However, anytime after we have called
783 mmcheck the first time to install the checking hooks, we can call it again
784 to update the function pointer to the memory corruption handler.
786 Returns zero on failure, non-zero on success. */
788 #ifndef MMCHECK_FORCE
789 #define MMCHECK_FORCE 0
796 if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE))
798 /* Don't use warning(), which relies on current_target being set
799 to something other than dummy_target, until after
800 initialize_all_files(). */
803 (gdb_stderr, "warning: failed to install memory consistency checks; ");
805 (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n");
811 #endif /* Have mmalloc and want corruption checking */
813 /* Called when a memory allocation fails, with the number of bytes of
814 memory requested in SIZE. */
822 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
826 fatal ("virtual memory exhausted.");
830 /* Like mmalloc but get error if no storage available, and protect against
831 the caller wanting to allocate zero bytes. Whether to return NULL for
832 a zero byte request, or translate the request into a request for one
833 byte of zero'd storage, is a religious issue. */
846 else if ((val = mmalloc (md, size)) == NULL)
853 /* Like mrealloc but get error if no storage available. */
856 xmrealloc (md, ptr, size)
865 val = mrealloc (md, ptr, size);
869 val = mmalloc (md, size);
878 /* Like malloc but get error if no storage available, and protect against
879 the caller wanting to allocate zero bytes. */
885 return (xmmalloc ((PTR) NULL, size));
888 /* Like mrealloc but get error if no storage available. */
895 return (xmrealloc ((PTR) NULL, ptr, size));
899 /* My replacement for the read system call.
900 Used like `read' but keeps going if `read' returns too soon. */
903 myread (desc, addr, len)
913 val = read (desc, addr, len);
924 /* Make a copy of the string at PTR with SIZE characters
925 (and add a null character at the end in the copy).
926 Uses malloc to get the space. Returns the address of the copy. */
929 savestring (ptr, size)
933 register char *p = (char *) xmalloc (size + 1);
934 memcpy (p, ptr, size);
940 msavestring (md, ptr, size)
945 register char *p = (char *) xmmalloc (md, size + 1);
946 memcpy (p, ptr, size);
951 /* The "const" is so it compiles under DGUX (which prototypes strsave
952 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
953 Doesn't real strsave return NULL if out of memory? */
958 return savestring (ptr, strlen (ptr));
966 return (msavestring (md, ptr, strlen (ptr)));
970 print_spaces (n, file)
978 /* Print a host address. */
981 gdb_print_address (addr, stream)
986 /* We could use the %p conversion specifier to fprintf if we had any
987 way of knowing whether this host supports it. But the following
988 should work on the Alpha and on 32 bit machines. */
990 fprintf_filtered (stream, "0x%lx", (unsigned long)addr);
993 /* Ask user a y-or-n question and return 1 iff answer is yes.
994 Takes three args which are given to printf to print the question.
995 The first, a control string, should end in "? ".
996 It should not say how to answer, because we do that. */
1000 #ifdef ANSI_PROTOTYPES
1001 query (char *ctlstr, ...)
1008 register int answer;
1012 #ifdef ANSI_PROTOTYPES
1013 va_start (args, ctlstr);
1017 ctlstr = va_arg (args, char *);
1022 return query_hook (ctlstr, args);
1025 /* Automatically answer "yes" if input is not from a terminal. */
1026 if (!input_from_terminal_p ())
1029 /* FIXME Automatically answer "yes" if called from MacGDB. */
1036 wrap_here (""); /* Flush any buffered output */
1037 gdb_flush (gdb_stdout);
1039 if (annotation_level > 1)
1040 printf_filtered ("\n\032\032pre-query\n");
1042 vfprintf_filtered (gdb_stdout, ctlstr, args);
1043 printf_filtered ("(y or n) ");
1045 if (annotation_level > 1)
1046 printf_filtered ("\n\032\032query\n");
1049 /* If not in MacGDB, move to a new line so the entered line doesn't
1050 have a prompt on the front of it. */
1052 fputs_unfiltered ("\n", gdb_stdout);
1055 gdb_flush (gdb_stdout);
1056 answer = fgetc (stdin);
1057 clearerr (stdin); /* in case of C-d */
1058 if (answer == EOF) /* C-d */
1063 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
1066 ans2 = fgetc (stdin);
1069 while (ans2 != EOF && ans2 != '\n');
1082 printf_filtered ("Please answer y or n.\n");
1085 if (annotation_level > 1)
1086 printf_filtered ("\n\032\032post-query\n");
1091 /* Parse a C escape sequence. STRING_PTR points to a variable
1092 containing a pointer to the string to parse. That pointer
1093 should point to the character after the \. That pointer
1094 is updated past the characters we use. The value of the
1095 escape sequence is returned.
1097 A negative value means the sequence \ newline was seen,
1098 which is supposed to be equivalent to nothing at all.
1100 If \ is followed by a null character, we return a negative
1101 value and leave the string pointer pointing at the null character.
1103 If \ is followed by 000, we return 0 and leave the string pointer
1104 after the zeros. A value of 0 does not mean end of string. */
1107 parse_escape (string_ptr)
1110 register int c = *(*string_ptr)++;
1114 return 007; /* Bell (alert) char */
1117 case 'e': /* Escape character */
1135 c = *(*string_ptr)++;
1137 c = parse_escape (string_ptr);
1140 return (c & 0200) | (c & 037);
1151 register int i = c - '0';
1152 register int count = 0;
1155 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
1173 /* Print the character C on STREAM as part of the contents of a literal
1174 string whose delimiter is QUOTER. Note that this routine should only
1175 be call for printing things which are independent of the language
1176 of the program being debugged. */
1179 gdb_printchar (c, stream, quoter)
1185 c &= 0xFF; /* Avoid sign bit follies */
1187 if ( c < 0x20 || /* Low control chars */
1188 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1189 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
1193 fputs_filtered ("\\n", stream);
1196 fputs_filtered ("\\b", stream);
1199 fputs_filtered ("\\t", stream);
1202 fputs_filtered ("\\f", stream);
1205 fputs_filtered ("\\r", stream);
1208 fputs_filtered ("\\e", stream);
1211 fputs_filtered ("\\a", stream);
1214 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
1218 if (c == '\\' || c == quoter)
1219 fputs_filtered ("\\", stream);
1220 fprintf_filtered (stream, "%c", c);
1227 static char * hexlate = "0123456789abcdef" ;
1228 int fmthex(inbuf,outbuff,length,linelength)
1229 unsigned char * inbuf ;
1230 unsigned char * outbuff;
1234 unsigned char byte , nib ;
1239 if (outlength >= linelength) break ;
1243 *outbuff++ = hexlate[nib] ;
1245 *outbuff++ = hexlate[nib] ;
1250 *outbuff = '\0' ; /* null terminate our output line */
1255 /* Number of lines per page or UINT_MAX if paging is disabled. */
1256 static unsigned int lines_per_page;
1257 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1258 static unsigned int chars_per_line;
1259 /* Current count of lines printed on this page, chars on this line. */
1260 static unsigned int lines_printed, chars_printed;
1262 /* Buffer and start column of buffered text, for doing smarter word-
1263 wrapping. When someone calls wrap_here(), we start buffering output
1264 that comes through fputs_filtered(). If we see a newline, we just
1265 spit it out and forget about the wrap_here(). If we see another
1266 wrap_here(), we spit it out and remember the newer one. If we see
1267 the end of the line, we spit out a newline, the indent, and then
1268 the buffered output. */
1270 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1271 are waiting to be output (they have already been counted in chars_printed).
1272 When wrap_buffer[0] is null, the buffer is empty. */
1273 static char *wrap_buffer;
1275 /* Pointer in wrap_buffer to the next character to fill. */
1276 static char *wrap_pointer;
1278 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1280 static char *wrap_indent;
1282 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1283 is not in effect. */
1284 static int wrap_column;
1288 set_width_command (args, from_tty, c)
1291 struct cmd_list_element *c;
1295 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1296 wrap_buffer[0] = '\0';
1299 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1300 wrap_pointer = wrap_buffer; /* Start it at the beginning */
1303 /* Wait, so the user can read what's on the screen. Prompt the user
1304 to continue by pressing RETURN. */
1307 prompt_for_continue ()
1310 char cont_prompt[120];
1312 if (annotation_level > 1)
1313 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1315 strcpy (cont_prompt,
1316 "---Type <return> to continue, or q <return> to quit---");
1317 if (annotation_level > 1)
1318 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1320 /* We must do this *before* we call gdb_readline, else it will eventually
1321 call us -- thinking that we're trying to print beyond the end of the
1323 reinitialize_more_filter ();
1326 /* On a real operating system, the user can quit with SIGINT.
1329 'q' is provided on all systems so users don't have to change habits
1330 from system to system, and because telling them what to do in
1331 the prompt is more user-friendly than expecting them to think of
1333 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1334 whereas control-C to gdb_readline will cause the user to get dumped
1336 ignore = readline (cont_prompt);
1338 if (annotation_level > 1)
1339 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1344 while (*p == ' ' || *p == '\t')
1347 request_quit (SIGINT);
1352 /* Now we have to do this again, so that GDB will know that it doesn't
1353 need to save the ---Type <return>--- line at the top of the screen. */
1354 reinitialize_more_filter ();
1356 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1359 /* Reinitialize filter; ie. tell it to reset to original values. */
1362 reinitialize_more_filter ()
1368 /* Indicate that if the next sequence of characters overflows the line,
1369 a newline should be inserted here rather than when it hits the end.
1370 If INDENT is non-null, it is a string to be printed to indent the
1371 wrapped part on the next line. INDENT must remain accessible until
1372 the next call to wrap_here() or until a newline is printed through
1375 If the line is already overfull, we immediately print a newline and
1376 the indentation, and disable further wrapping.
1378 If we don't know the width of lines, but we know the page height,
1379 we must not wrap words, but should still keep track of newlines
1380 that were explicitly printed.
1382 INDENT should not contain tabs, as that will mess up the char count
1383 on the next line. FIXME.
1385 This routine is guaranteed to force out any output which has been
1386 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1387 used to force out output from the wrap_buffer. */
1393 /* This should have been allocated, but be paranoid anyway. */
1399 *wrap_pointer = '\0';
1400 fputs_unfiltered (wrap_buffer, gdb_stdout);
1402 wrap_pointer = wrap_buffer;
1403 wrap_buffer[0] = '\0';
1404 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1408 else if (chars_printed >= chars_per_line)
1410 puts_filtered ("\n");
1412 puts_filtered (indent);
1417 wrap_column = chars_printed;
1421 wrap_indent = indent;
1425 /* Ensure that whatever gets printed next, using the filtered output
1426 commands, starts at the beginning of the line. I.E. if there is
1427 any pending output for the current line, flush it and start a new
1428 line. Otherwise do nothing. */
1433 if (chars_printed > 0)
1435 puts_filtered ("\n");
1441 gdb_fopen (name, mode)
1445 return fopen (name, mode);
1453 && (stream == gdb_stdout
1454 || stream == gdb_stderr))
1456 flush_hook (stream);
1463 /* Like fputs but if FILTER is true, pause after every screenful.
1465 Regardless of FILTER can wrap at points other than the final
1466 character of a line.
1468 Unlike fputs, fputs_maybe_filtered does not return a value.
1469 It is OK for LINEBUFFER to be NULL, in which case just don't print
1472 Note that a longjmp to top level may occur in this routine (only if
1473 FILTER is true) (since prompt_for_continue may do so) so this
1474 routine should not be called when cleanups are not in place. */
1477 fputs_maybe_filtered (linebuffer, stream, filter)
1478 const char *linebuffer;
1482 const char *lineptr;
1484 if (linebuffer == 0)
1487 /* Don't do any filtering if it is disabled. */
1488 if (stream != gdb_stdout
1489 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1491 fputs_unfiltered (linebuffer, stream);
1495 /* Go through and output each character. Show line extension
1496 when this is necessary; prompt user for new page when this is
1499 lineptr = linebuffer;
1502 /* Possible new page. */
1504 (lines_printed >= lines_per_page - 1))
1505 prompt_for_continue ();
1507 while (*lineptr && *lineptr != '\n')
1509 /* Print a single line. */
1510 if (*lineptr == '\t')
1513 *wrap_pointer++ = '\t';
1515 fputc_unfiltered ('\t', stream);
1516 /* Shifting right by 3 produces the number of tab stops
1517 we have already passed, and then adding one and
1518 shifting left 3 advances to the next tab stop. */
1519 chars_printed = ((chars_printed >> 3) + 1) << 3;
1525 *wrap_pointer++ = *lineptr;
1527 fputc_unfiltered (*lineptr, stream);
1532 if (chars_printed >= chars_per_line)
1534 unsigned int save_chars = chars_printed;
1538 /* If we aren't actually wrapping, don't output newline --
1539 if chars_per_line is right, we probably just overflowed
1540 anyway; if it's wrong, let us keep going. */
1542 fputc_unfiltered ('\n', stream);
1544 /* Possible new page. */
1545 if (lines_printed >= lines_per_page - 1)
1546 prompt_for_continue ();
1548 /* Now output indentation and wrapped string */
1551 fputs_unfiltered (wrap_indent, stream);
1552 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1553 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
1554 /* FIXME, this strlen is what prevents wrap_indent from
1555 containing tabs. However, if we recurse to print it
1556 and count its chars, we risk trouble if wrap_indent is
1557 longer than (the user settable) chars_per_line.
1558 Note also that this can set chars_printed > chars_per_line
1559 if we are printing a long string. */
1560 chars_printed = strlen (wrap_indent)
1561 + (save_chars - wrap_column);
1562 wrap_pointer = wrap_buffer; /* Reset buffer */
1563 wrap_buffer[0] = '\0';
1564 wrap_column = 0; /* And disable fancy wrap */
1569 if (*lineptr == '\n')
1572 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1574 fputc_unfiltered ('\n', stream);
1581 fputs_filtered (linebuffer, stream)
1582 const char *linebuffer;
1585 fputs_maybe_filtered (linebuffer, stream, 1);
1589 putchar_unfiltered (c)
1596 fputs_unfiltered (buf, gdb_stdout);
1601 fputc_unfiltered (c, stream)
1609 fputs_unfiltered (buf, stream);
1614 fputc_filtered (c, stream)
1622 fputs_filtered (buf, stream);
1626 /* puts_debug is like fputs_unfiltered, except it prints special
1627 characters in printable fashion. */
1630 puts_debug (prefix, string, suffix)
1637 /* Print prefix and suffix after each line. */
1638 static int new_line = 1;
1639 static int return_p = 0;
1640 static char *prev_prefix = "";
1641 static char *prev_suffix = "";
1643 if (*string == '\n')
1646 /* If the prefix is changing, print the previous suffix, a new line,
1647 and the new prefix. */
1648 if ((return_p || (strcmp(prev_prefix, prefix) != 0)) && !new_line)
1650 fputs_unfiltered (prev_suffix, gdb_stderr);
1651 fputs_unfiltered ("\n", gdb_stderr);
1652 fputs_unfiltered (prefix, gdb_stderr);
1655 /* Print prefix if we printed a newline during the previous call. */
1659 fputs_unfiltered (prefix, gdb_stderr);
1662 prev_prefix = prefix;
1663 prev_suffix = suffix;
1665 /* Output characters in a printable format. */
1666 while ((ch = *string++) != '\0')
1672 fputc_unfiltered (ch, gdb_stderr);
1675 fprintf_unfiltered (gdb_stderr, "\\x%02x", ch & 0xff);
1678 case '\\': fputs_unfiltered ("\\\\", gdb_stderr); break;
1679 case '\b': fputs_unfiltered ("\\b", gdb_stderr); break;
1680 case '\f': fputs_unfiltered ("\\f", gdb_stderr); break;
1681 case '\n': new_line = 1;
1682 fputs_unfiltered ("\\n", gdb_stderr); break;
1683 case '\r': fputs_unfiltered ("\\r", gdb_stderr); break;
1684 case '\t': fputs_unfiltered ("\\t", gdb_stderr); break;
1685 case '\v': fputs_unfiltered ("\\v", gdb_stderr); break;
1688 return_p = ch == '\r';
1691 /* Print suffix if we printed a newline. */
1694 fputs_unfiltered (suffix, gdb_stderr);
1695 fputs_unfiltered ("\n", gdb_stderr);
1700 /* Print a variable number of ARGS using format FORMAT. If this
1701 information is going to put the amount written (since the last call
1702 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1703 call prompt_for_continue to get the users permision to continue.
1705 Unlike fprintf, this function does not return a value.
1707 We implement three variants, vfprintf (takes a vararg list and stream),
1708 fprintf (takes a stream to write on), and printf (the usual).
1710 Note also that a longjmp to top level may occur in this routine
1711 (since prompt_for_continue may do so) so this routine should not be
1712 called when cleanups are not in place. */
1715 vfprintf_maybe_filtered (stream, format, args, filter)
1722 struct cleanup *old_cleanups;
1724 vasprintf (&linebuffer, format, args);
1725 if (linebuffer == NULL)
1727 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
1730 old_cleanups = make_cleanup (free, linebuffer);
1731 fputs_maybe_filtered (linebuffer, stream, filter);
1732 do_cleanups (old_cleanups);
1737 vfprintf_filtered (stream, format, args)
1742 vfprintf_maybe_filtered (stream, format, args, 1);
1746 vfprintf_unfiltered (stream, format, args)
1752 struct cleanup *old_cleanups;
1754 vasprintf (&linebuffer, format, args);
1755 if (linebuffer == NULL)
1757 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
1760 old_cleanups = make_cleanup (free, linebuffer);
1761 fputs_unfiltered (linebuffer, stream);
1762 do_cleanups (old_cleanups);
1766 vprintf_filtered (format, args)
1770 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
1774 vprintf_unfiltered (format, args)
1778 vfprintf_unfiltered (gdb_stdout, format, args);
1783 #ifdef ANSI_PROTOTYPES
1784 fprintf_filtered (FILE *stream, const char *format, ...)
1786 fprintf_filtered (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_filtered (stream, format, args);
1807 #ifdef ANSI_PROTOTYPES
1808 fprintf_unfiltered (FILE *stream, const char *format, ...)
1810 fprintf_unfiltered (va_alist)
1815 #ifdef ANSI_PROTOTYPES
1816 va_start (args, format);
1822 stream = va_arg (args, FILE *);
1823 format = va_arg (args, char *);
1825 vfprintf_unfiltered (stream, format, args);
1829 /* Like fprintf_filtered, but prints its result indented.
1830 Called as fprintfi_filtered (spaces, stream, format, ...); */
1834 #ifdef ANSI_PROTOTYPES
1835 fprintfi_filtered (int spaces, FILE *stream, const char *format, ...)
1837 fprintfi_filtered (va_alist)
1842 #ifdef ANSI_PROTOTYPES
1843 va_start (args, format);
1850 spaces = va_arg (args, int);
1851 stream = va_arg (args, FILE *);
1852 format = va_arg (args, char *);
1854 print_spaces_filtered (spaces, stream);
1856 vfprintf_filtered (stream, format, args);
1863 #ifdef ANSI_PROTOTYPES
1864 printf_filtered (const char *format, ...)
1866 printf_filtered (va_alist)
1871 #ifdef ANSI_PROTOTYPES
1872 va_start (args, format);
1877 format = va_arg (args, char *);
1879 vfprintf_filtered (gdb_stdout, format, args);
1886 #ifdef ANSI_PROTOTYPES
1887 printf_unfiltered (const char *format, ...)
1889 printf_unfiltered (va_alist)
1894 #ifdef ANSI_PROTOTYPES
1895 va_start (args, format);
1900 format = va_arg (args, char *);
1902 vfprintf_unfiltered (gdb_stdout, format, args);
1906 /* Like printf_filtered, but prints it's result indented.
1907 Called as printfi_filtered (spaces, format, ...); */
1911 #ifdef ANSI_PROTOTYPES
1912 printfi_filtered (int spaces, const char *format, ...)
1914 printfi_filtered (va_alist)
1919 #ifdef ANSI_PROTOTYPES
1920 va_start (args, format);
1926 spaces = va_arg (args, int);
1927 format = va_arg (args, char *);
1929 print_spaces_filtered (spaces, gdb_stdout);
1930 vfprintf_filtered (gdb_stdout, format, args);
1934 /* Easy -- but watch out!
1936 This routine is *not* a replacement for puts()! puts() appends a newline.
1937 This one doesn't, and had better not! */
1940 puts_filtered (string)
1943 fputs_filtered (string, gdb_stdout);
1947 puts_unfiltered (string)
1950 fputs_unfiltered (string, gdb_stdout);
1953 /* Return a pointer to N spaces and a null. The pointer is good
1954 until the next call to here. */
1960 static char *spaces;
1961 static int max_spaces;
1967 spaces = (char *) xmalloc (n+1);
1968 for (t = spaces+n; t != spaces;)
1974 return spaces + max_spaces - n;
1977 /* Print N spaces. */
1979 print_spaces_filtered (n, stream)
1983 fputs_filtered (n_spaces (n), stream);
1986 /* C++ demangler stuff. */
1988 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1989 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1990 If the name is not mangled, or the language for the name is unknown, or
1991 demangling is off, the name is printed in its "raw" form. */
1994 fprintf_symbol_filtered (stream, name, lang, arg_mode)
2004 /* If user wants to see raw output, no problem. */
2007 fputs_filtered (name, stream);
2013 case language_cplus:
2014 demangled = cplus_demangle (name, arg_mode);
2016 /* start-sanitize-java */
2018 demangled = cplus_demangle (name, arg_mode | DMGL_JAVA);
2020 /* end-sanitize-java */
2021 case language_chill:
2022 demangled = chill_demangle (name);
2028 fputs_filtered (demangled ? demangled : name, stream);
2029 if (demangled != NULL)
2037 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
2038 differences in whitespace. Returns 0 if they match, non-zero if they
2039 don't (slightly different than strcmp()'s range of return values).
2041 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2042 This "feature" is useful when searching for matching C++ function names
2043 (such as if the user types 'break FOO', where FOO is a mangled C++
2047 strcmp_iw (string1, string2)
2048 const char *string1;
2049 const char *string2;
2051 while ((*string1 != '\0') && (*string2 != '\0'))
2053 while (isspace (*string1))
2057 while (isspace (*string2))
2061 if (*string1 != *string2)
2065 if (*string1 != '\0')
2071 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
2078 struct cmd_list_element *c;
2080 c = add_set_cmd ("width", class_support, var_uinteger,
2081 (char *)&chars_per_line,
2082 "Set number of characters gdb thinks are in a line.",
2084 add_show_from_set (c, &showlist);
2085 c->function.sfunc = set_width_command;
2088 (add_set_cmd ("height", class_support,
2089 var_uinteger, (char *)&lines_per_page,
2090 "Set number of lines gdb thinks are in a page.", &setlist),
2093 /* These defaults will be used if we are unable to get the correct
2094 values from termcap. */
2095 #if defined(__GO32__)
2096 lines_per_page = ScreenRows();
2097 chars_per_line = ScreenCols();
2099 lines_per_page = 24;
2100 chars_per_line = 80;
2102 #if !defined (MPW) && !defined (_WIN32)
2103 /* No termcap under MPW, although might be cool to do something
2104 by looking at worksheet or console window sizes. */
2105 /* Initialize the screen height and width from termcap. */
2107 char *termtype = getenv ("TERM");
2109 /* Positive means success, nonpositive means failure. */
2112 /* 2048 is large enough for all known terminals, according to the
2113 GNU termcap manual. */
2114 char term_buffer[2048];
2118 status = tgetent (term_buffer, termtype);
2123 val = tgetnum ("li");
2125 lines_per_page = val;
2127 /* The number of lines per page is not mentioned
2128 in the terminal description. This probably means
2129 that paging is not useful (e.g. emacs shell window),
2130 so disable paging. */
2131 lines_per_page = UINT_MAX;
2133 val = tgetnum ("co");
2135 chars_per_line = val;
2141 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2143 /* If there is a better way to determine the window size, use it. */
2144 SIGWINCH_HANDLER (SIGWINCH);
2147 /* If the output is not a terminal, don't paginate it. */
2148 if (!ISATTY (gdb_stdout))
2149 lines_per_page = UINT_MAX;
2151 set_width_command ((char *)NULL, 0, c);
2154 (add_set_cmd ("demangle", class_support, var_boolean,
2156 "Set demangling of encoded C++ names when displaying symbols.",
2161 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
2162 (char *)&sevenbit_strings,
2163 "Set printing of 8-bit characters in strings as \\nnn.",
2168 (add_set_cmd ("asm-demangle", class_support, var_boolean,
2169 (char *)&asm_demangle,
2170 "Set demangling of C++ names in disassembly listings.",
2175 /* Machine specific function to handle SIGWINCH signal. */
2177 #ifdef SIGWINCH_HANDLER_BODY
2178 SIGWINCH_HANDLER_BODY
2181 /* Support for converting target fp numbers into host DOUBLEST format. */
2183 /* XXX - This code should really be in libiberty/floatformat.c, however
2184 configuration issues with libiberty made this very difficult to do in the
2187 #include "floatformat.h"
2188 #include <math.h> /* ldexp */
2190 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
2191 going to bother with trying to muck around with whether it is defined in
2192 a system header, what we do if not, etc. */
2193 #define FLOATFORMAT_CHAR_BIT 8
2195 static unsigned long get_field PARAMS ((unsigned char *,
2196 enum floatformat_byteorders,
2201 /* Extract a field which starts at START and is LEN bytes long. DATA and
2202 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2203 static unsigned long
2204 get_field (data, order, total_len, start, len)
2205 unsigned char *data;
2206 enum floatformat_byteorders order;
2207 unsigned int total_len;
2211 unsigned long result;
2212 unsigned int cur_byte;
2215 /* Start at the least significant part of the field. */
2216 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
2217 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2218 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
2220 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
2221 result = *(data + cur_byte) >> (-cur_bitshift);
2222 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2223 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2228 /* Move towards the most significant part of the field. */
2229 while (cur_bitshift < len)
2231 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
2232 /* This is the last byte; zero out the bits which are not part of
2235 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
2238 result |= *(data + cur_byte) << cur_bitshift;
2239 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2240 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2248 /* Convert from FMT to a DOUBLEST.
2249 FROM is the address of the extended float.
2250 Store the DOUBLEST in *TO. */
2253 floatformat_to_doublest (fmt, from, to)
2254 const struct floatformat *fmt;
2258 unsigned char *ufrom = (unsigned char *)from;
2262 unsigned int mant_bits, mant_off;
2264 int special_exponent; /* It's a NaN, denorm or zero */
2266 /* If the mantissa bits are not contiguous from one end of the
2267 mantissa to the other, we need to make a private copy of the
2268 source bytes that is in the right order since the unpacking
2269 algorithm assumes that the bits are contiguous.
2271 Swap the bytes individually rather than accessing them through
2272 "long *" since we have no guarantee that they start on a long
2273 alignment, and also sizeof(long) for the host could be different
2274 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
2275 for the target is 4. */
2277 if (fmt -> byteorder == floatformat_littlebyte_bigword)
2279 static unsigned char *newfrom;
2280 unsigned char *swapin, *swapout;
2283 longswaps = fmt -> totalsize / FLOATFORMAT_CHAR_BIT;
2286 if (newfrom == NULL)
2288 newfrom = (unsigned char *) xmalloc (fmt -> totalsize);
2293 while (longswaps-- > 0)
2295 /* This is ugly, but efficient */
2296 *swapout++ = swapin[4];
2297 *swapout++ = swapin[5];
2298 *swapout++ = swapin[6];
2299 *swapout++ = swapin[7];
2300 *swapout++ = swapin[0];
2301 *swapout++ = swapin[1];
2302 *swapout++ = swapin[2];
2303 *swapout++ = swapin[3];
2308 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
2309 fmt->exp_start, fmt->exp_len);
2310 /* Note that if exponent indicates a NaN, we can't really do anything useful
2311 (not knowing if the host has NaN's, or how to build one). So it will
2312 end up as an infinity or something close; that is OK. */
2314 mant_bits_left = fmt->man_len;
2315 mant_off = fmt->man_start;
2318 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
2320 /* Don't bias zero's, denorms or NaNs. */
2321 if (!special_exponent)
2322 exponent -= fmt->exp_bias;
2324 /* Build the result algebraically. Might go infinite, underflow, etc;
2327 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
2328 increment the exponent by one to account for the integer bit. */
2330 if (!special_exponent)
2331 if (fmt->intbit == floatformat_intbit_no)
2332 dto = ldexp (1.0, exponent);
2336 while (mant_bits_left > 0)
2338 mant_bits = min (mant_bits_left, 32);
2340 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
2341 mant_off, mant_bits);
2343 dto += ldexp ((double)mant, exponent - mant_bits);
2344 exponent -= mant_bits;
2345 mant_off += mant_bits;
2346 mant_bits_left -= mant_bits;
2349 /* Negate it if negative. */
2350 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
2355 static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
2361 /* Set a field which starts at START and is LEN bytes long. DATA and
2362 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2364 put_field (data, order, total_len, start, len, stuff_to_put)
2365 unsigned char *data;
2366 enum floatformat_byteorders order;
2367 unsigned int total_len;
2370 unsigned long stuff_to_put;
2372 unsigned int cur_byte;
2375 /* Start at the least significant part of the field. */
2376 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
2377 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2378 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
2380 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
2381 *(data + cur_byte) &=
2382 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
2383 *(data + cur_byte) |=
2384 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
2385 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2386 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2391 /* Move towards the most significant part of the field. */
2392 while (cur_bitshift < len)
2394 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
2396 /* This is the last byte. */
2397 *(data + cur_byte) &=
2398 ~((1 << (len - cur_bitshift)) - 1);
2399 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
2402 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
2403 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
2404 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2405 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2412 #ifdef HAVE_LONG_DOUBLE
2413 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
2414 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
2415 frexp, but operates on the long double data type. */
2417 static long double ldfrexp PARAMS ((long double value, int *eptr));
2420 ldfrexp (value, eptr)
2427 /* Unfortunately, there are no portable functions for extracting the exponent
2428 of a long double, so we have to do it iteratively by multiplying or dividing
2429 by two until the fraction is between 0.5 and 1.0. */
2437 if (value >= tmp) /* Value >= 1.0 */
2438 while (value >= tmp)
2443 else if (value != 0.0l) /* Value < 1.0 and > 0.0 */
2457 #endif /* HAVE_LONG_DOUBLE */
2460 /* The converse: convert the DOUBLEST *FROM to an extended float
2461 and store where TO points. Neither FROM nor TO have any alignment
2465 floatformat_from_doublest (fmt, from, to)
2466 CONST struct floatformat *fmt;
2473 unsigned int mant_bits, mant_off;
2475 unsigned char *uto = (unsigned char *)to;
2477 memcpy (&dfrom, from, sizeof (dfrom));
2478 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
2480 return; /* Result is zero */
2481 if (dfrom != dfrom) /* Result is NaN */
2484 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
2485 fmt->exp_len, fmt->exp_nan);
2486 /* Be sure it's not infinity, but NaN value is irrel */
2487 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
2492 /* If negative, set the sign bit. */
2495 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
2499 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity */
2501 /* Infinity exponent is same as NaN's. */
2502 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
2503 fmt->exp_len, fmt->exp_nan);
2504 /* Infinity mantissa is all zeroes. */
2505 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
2510 #ifdef HAVE_LONG_DOUBLE
2511 mant = ldfrexp (dfrom, &exponent);
2513 mant = frexp (dfrom, &exponent);
2516 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
2517 exponent + fmt->exp_bias - 1);
2519 mant_bits_left = fmt->man_len;
2520 mant_off = fmt->man_start;
2521 while (mant_bits_left > 0)
2523 unsigned long mant_long;
2524 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
2526 mant *= 4294967296.0;
2527 mant_long = (unsigned long)mant;
2530 /* If the integer bit is implicit, then we need to discard it.
2531 If we are discarding a zero, we should be (but are not) creating
2532 a denormalized number which means adjusting the exponent
2534 if (mant_bits_left == fmt->man_len
2535 && fmt->intbit == floatformat_intbit_no)
2543 /* The bits we want are in the most significant MANT_BITS bits of
2544 mant_long. Move them to the least significant. */
2545 mant_long >>= 32 - mant_bits;
2548 put_field (uto, fmt->byteorder, fmt->totalsize,
2549 mant_off, mant_bits, mant_long);
2550 mant_off += mant_bits;
2551 mant_bits_left -= mant_bits;
2553 if (fmt -> byteorder == floatformat_littlebyte_bigword)
2556 unsigned char *swaplow = uto;
2557 unsigned char *swaphigh = uto + 4;
2560 for (count = 0; count < 4; count++)
2563 *swaplow++ = *swaphigh;
2569 /* temporary storage using circular buffer */
2575 static char buf[NUMCELLS][CELLSIZE];
2577 if (++cell>=NUMCELLS) cell=0;
2581 /* print routines to handle variable size regs, etc.
2583 FIXME: Note that t_addr is a bfd_vma, which is currently either an
2584 unsigned long or unsigned long long, determined at configure time.
2585 If t_addr is an unsigned long long and sizeof (unsigned long long)
2586 is greater than sizeof (unsigned long), then I believe this code will
2587 probably lose, at least for little endian machines. I believe that
2588 it would also be better to eliminate the switch on the absolute size
2589 of t_addr and replace it with a sequence of if statements that compare
2590 sizeof t_addr with sizeof the various types and do the right thing,
2591 which includes knowing whether or not the host supports long long.
2596 static int thirty_two = 32; /* eliminate warning from compiler on 32-bit systems */
2602 char *paddr_str=get_cell();
2603 switch (sizeof(t_addr))
2606 sprintf (paddr_str, "%08lx%08lx",
2607 (unsigned long) (addr >> thirty_two), (unsigned long) (addr & 0xffffffff));
2610 sprintf (paddr_str, "%08lx", (unsigned long) addr);
2613 sprintf (paddr_str, "%04x", (unsigned short) (addr & 0xffff));
2616 sprintf (paddr_str, "%lx", (unsigned long) addr);
2625 char *preg_str=get_cell();
2626 switch (sizeof(t_reg))
2629 sprintf (preg_str, "%08lx%08lx",
2630 (unsigned long) (reg >> thirty_two), (unsigned long) (reg & 0xffffffff));
2633 sprintf (preg_str, "%08lx", (unsigned long) reg);
2636 sprintf (preg_str, "%04x", (unsigned short) (reg & 0xffff));
2639 sprintf (preg_str, "%lx", (unsigned long) reg);
2648 char *paddr_str=get_cell();
2649 switch (sizeof(t_addr))
2653 unsigned long high = (unsigned long) (addr >> thirty_two);
2655 sprintf (paddr_str, "%lx", (unsigned long) (addr & 0xffffffff));
2657 sprintf (paddr_str, "%lx%08lx",
2658 high, (unsigned long) (addr & 0xffffffff));
2662 sprintf (paddr_str, "%lx", (unsigned long) addr);
2665 sprintf (paddr_str, "%x", (unsigned short) (addr & 0xffff));
2668 sprintf (paddr_str,"%lx", (unsigned long) addr);
2677 char *preg_str=get_cell();
2678 switch (sizeof(t_reg))
2682 unsigned long high = (unsigned long) (reg >> thirty_two);
2684 sprintf (preg_str, "%lx", (unsigned long) (reg & 0xffffffff));
2686 sprintf (preg_str, "%lx%08lx",
2687 high, (unsigned long) (reg & 0xffffffff));
2691 sprintf (preg_str, "%lx", (unsigned long) reg);
2694 sprintf (preg_str, "%x", (unsigned short) (reg & 0xffff));
2697 sprintf (preg_str, "%lx", (unsigned long) reg);