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,
19 Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
24 #include "event-top.h"
33 /* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */
44 #include "expression.h"
48 #include <readline/readline.h>
51 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
53 /* readline defines this. */
56 void (*error_begin_hook) PARAMS ((void));
58 /* Holds the last error message issued by gdb */
60 static GDB_FILE *gdb_lasterr;
62 /* Prototypes for local functions */
64 static void vfprintf_maybe_filtered PARAMS ((GDB_FILE *, const char *,
67 static void fputs_maybe_filtered PARAMS ((const char *, GDB_FILE *, int));
69 #if defined (USE_MMALLOC) && !defined (NO_MMCHECK)
70 static void malloc_botch PARAMS ((void));
74 prompt_for_continue PARAMS ((void));
77 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
80 set_width PARAMS ((void));
82 #ifndef GDB_FILE_ISATTY
83 #define GDB_FILE_ISATTY(GDB_FILE_PTR) (gdb_file_isatty(GDB_FILE_PTR))
86 /* Chain of cleanup actions established with make_cleanup,
87 to be executed if an error happens. */
89 static struct cleanup *cleanup_chain; /* cleaned up after a failed command */
90 static struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */
91 static struct cleanup *run_cleanup_chain; /* cleaned up on each 'run' */
92 static struct cleanup *exec_cleanup_chain; /* cleaned up on each execution command */
93 /* cleaned up on each error from within an execution command */
94 static struct cleanup *exec_error_cleanup_chain;
96 /* Pointer to what is left to do for an execution command after the
97 target stops. Used only in asynchronous mode, by targets that
98 support async execution. The finish and until commands use it. So
99 does the target extended-remote command. */
100 struct continuation *cmd_continuation;
102 /* Nonzero if we have job control. */
106 /* Nonzero means a quit has been requested. */
110 /* Nonzero means quit immediately if Control-C is typed now, rather
111 than waiting until QUIT is executed. Be careful in setting this;
112 code which executes with immediate_quit set has to be very careful
113 about being able to deal with being interrupted at any time. It is
114 almost always better to use QUIT; the only exception I can think of
115 is being able to quit out of a system call (using EINTR loses if
116 the SIGINT happens between the previous QUIT and the system call).
117 To immediately quit in the case in which a SIGINT happens between
118 the previous QUIT and setting immediate_quit (desirable anytime we
119 expect to block), call QUIT after setting immediate_quit. */
123 /* Nonzero means that encoded C++ names should be printed out in their
124 C++ form rather than raw. */
128 /* Nonzero means that encoded C++ names should be printed out in their
129 C++ form even in assembler language displays. If this is set, but
130 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
132 int asm_demangle = 0;
134 /* Nonzero means that strings with character values >0x7F should be printed
135 as octal escapes. Zero means just print the value (e.g. it's an
136 international character, and the terminal or window can cope.) */
138 int sevenbit_strings = 0;
140 /* String to be printed before error messages, if any. */
142 char *error_pre_print;
144 /* String to be printed before quit messages, if any. */
146 char *quit_pre_print;
148 /* String to be printed before warning messages, if any. */
150 char *warning_pre_print = "\nwarning: ";
152 int pagination_enabled = 1;
155 /* Add a new cleanup to the cleanup_chain,
156 and return the previous chain pointer
157 to be passed later to do_cleanups or discard_cleanups.
158 Args are FUNCTION to clean up with, and ARG to pass to it. */
161 make_cleanup (function, arg)
162 void (*function) PARAMS ((PTR));
165 return make_my_cleanup (&cleanup_chain, function, arg);
169 make_final_cleanup (function, arg)
170 void (*function) PARAMS ((PTR));
173 return make_my_cleanup (&final_cleanup_chain, function, arg);
177 make_run_cleanup (function, arg)
178 void (*function) PARAMS ((PTR));
181 return make_my_cleanup (&run_cleanup_chain, function, arg);
185 make_exec_cleanup (function, arg)
186 void (*function) PARAMS ((PTR));
189 return make_my_cleanup (&exec_cleanup_chain, function, arg);
193 make_exec_error_cleanup (function, arg)
194 void (*function) PARAMS ((PTR));
197 return make_my_cleanup (&exec_error_cleanup_chain, function, arg);
204 freeargv ((char **) arg);
208 make_cleanup_freeargv (arg)
211 return make_my_cleanup (&cleanup_chain, do_freeargv, arg);
215 do_gdb_file_delete (void *arg)
217 gdb_file_delete (arg);
221 make_cleanup_gdb_file_delete (struct gdb_file *arg)
223 return make_my_cleanup (&cleanup_chain, do_gdb_file_delete, arg);
227 make_my_cleanup (pmy_chain, function, arg)
228 struct cleanup **pmy_chain;
229 void (*function) PARAMS ((PTR));
232 register struct cleanup *new
233 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
234 register struct cleanup *old_chain = *pmy_chain;
236 new->next = *pmy_chain;
237 new->function = function;
244 /* Discard cleanups and do the actions they describe
245 until we get back to the point OLD_CHAIN in the cleanup_chain. */
248 do_cleanups (old_chain)
249 register struct cleanup *old_chain;
251 do_my_cleanups (&cleanup_chain, old_chain);
255 do_final_cleanups (old_chain)
256 register struct cleanup *old_chain;
258 do_my_cleanups (&final_cleanup_chain, old_chain);
262 do_run_cleanups (old_chain)
263 register struct cleanup *old_chain;
265 do_my_cleanups (&run_cleanup_chain, old_chain);
269 do_exec_cleanups (old_chain)
270 register struct cleanup *old_chain;
272 do_my_cleanups (&exec_cleanup_chain, old_chain);
276 do_exec_error_cleanups (old_chain)
277 register struct cleanup *old_chain;
279 do_my_cleanups (&exec_error_cleanup_chain, old_chain);
283 do_my_cleanups (pmy_chain, old_chain)
284 register struct cleanup **pmy_chain;
285 register struct cleanup *old_chain;
287 register struct cleanup *ptr;
288 while ((ptr = *pmy_chain) != old_chain)
290 *pmy_chain = ptr->next; /* Do this first incase recursion */
291 (*ptr->function) (ptr->arg);
296 /* Discard cleanups, not doing the actions they describe,
297 until we get back to the point OLD_CHAIN in the cleanup_chain. */
300 discard_cleanups (old_chain)
301 register struct cleanup *old_chain;
303 discard_my_cleanups (&cleanup_chain, old_chain);
307 discard_final_cleanups (old_chain)
308 register struct cleanup *old_chain;
310 discard_my_cleanups (&final_cleanup_chain, old_chain);
314 discard_exec_error_cleanups (old_chain)
315 register struct cleanup *old_chain;
317 discard_my_cleanups (&exec_error_cleanup_chain, old_chain);
321 discard_my_cleanups (pmy_chain, old_chain)
322 register struct cleanup **pmy_chain;
323 register struct cleanup *old_chain;
325 register struct cleanup *ptr;
326 while ((ptr = *pmy_chain) != old_chain)
328 *pmy_chain = ptr->next;
333 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
337 return save_my_cleanups (&cleanup_chain);
341 save_final_cleanups ()
343 return save_my_cleanups (&final_cleanup_chain);
347 save_my_cleanups (pmy_chain)
348 struct cleanup **pmy_chain;
350 struct cleanup *old_chain = *pmy_chain;
356 /* Restore the cleanup chain from a previously saved chain. */
358 restore_cleanups (chain)
359 struct cleanup *chain;
361 restore_my_cleanups (&cleanup_chain, chain);
365 restore_final_cleanups (chain)
366 struct cleanup *chain;
368 restore_my_cleanups (&final_cleanup_chain, chain);
372 restore_my_cleanups (pmy_chain, chain)
373 struct cleanup **pmy_chain;
374 struct cleanup *chain;
379 /* This function is useful for cleanups.
383 old_chain = make_cleanup (free_current_contents, &foo);
385 to arrange to free the object thus allocated. */
388 free_current_contents (location)
394 /* Provide a known function that does nothing, to use as a base for
395 for a possibly long chain of cleanups. This is useful where we
396 use the cleanup chain for handling normal cleanups as well as dealing
397 with cleanups that need to be done as a result of a call to error().
398 In such cases, we may not be certain where the first cleanup is, unless
399 we have a do-nothing one to always use as the base. */
408 /* Add a continuation to the continuation list, the gloabl list
411 add_continuation (continuation_hook, arg_list)
412 void (*continuation_hook) PARAMS ((struct continuation_arg *));
413 struct continuation_arg *arg_list;
415 struct continuation *continuation_ptr;
417 continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation));
418 continuation_ptr->continuation_hook = continuation_hook;
419 continuation_ptr->arg_list = arg_list;
420 continuation_ptr->next = cmd_continuation;
421 cmd_continuation = continuation_ptr;
424 /* Walk down the cmd_continuation list, and execute all the
427 do_all_continuations ()
429 struct continuation *continuation_ptr;
431 while (cmd_continuation)
433 (cmd_continuation->continuation_hook) (cmd_continuation->arg_list);
434 continuation_ptr = cmd_continuation;
435 cmd_continuation = continuation_ptr->next;
436 free (continuation_ptr);
440 /* Walk down the cmd_continuation list, and get rid of all the
443 discard_all_continuations ()
445 struct continuation *continuation_ptr;
447 while (cmd_continuation)
449 continuation_ptr = cmd_continuation;
450 cmd_continuation = continuation_ptr->next;
451 free (continuation_ptr);
457 /* Print a warning message. Way to use this is to call warning_begin,
458 output the warning message (use unfiltered output to gdb_stderr),
459 ending in a newline. There is not currently a warning_end that you
460 call afterwards, but such a thing might be added if it is useful
461 for a GUI to separate warning messages from other output.
463 FIXME: Why do warnings use unfiltered output and errors filtered?
464 Is this anything other than a historical accident? */
469 target_terminal_ours ();
470 wrap_here (""); /* Force out any buffered output */
471 gdb_flush (gdb_stdout);
472 if (warning_pre_print)
473 fprintf_unfiltered (gdb_stderr, warning_pre_print);
476 /* Print a warning message.
477 The first argument STRING is the warning message, used as a fprintf string,
478 and the remaining args are passed as arguments to it.
479 The primary difference between warnings and errors is that a warning
480 does not force the return to command level. */
483 warning (const char *string,...)
486 va_start (args, string);
488 (*warning_hook) (string, args);
492 vfprintf_unfiltered (gdb_stderr, string, args);
493 fprintf_unfiltered (gdb_stderr, "\n");
498 /* Start the printing of an error message. Way to use this is to call
499 this, output the error message (use filtered output to gdb_stderr
500 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
501 in a newline, and then call return_to_top_level (RETURN_ERROR).
502 error() provides a convenient way to do this for the special case
503 that the error message can be formatted with a single printf call,
504 but this is more general. */
508 if (error_begin_hook)
511 target_terminal_ours ();
512 wrap_here (""); /* Force out any buffered output */
513 gdb_flush (gdb_stdout);
515 annotate_error_begin ();
518 fprintf_filtered (gdb_stderr, error_pre_print);
521 /* Print an error message and return to command level.
522 The first argument STRING is the error message, used as a fprintf string,
523 and the remaining args are passed as arguments to it. */
526 verror (const char *string, va_list args)
528 /* FIXME: cagney/1999-11-10: All error calls should come here.
529 Unfortunatly some code uses the sequence: error_begin(); print
530 error message; return_to_top_level. That code should be
533 vfprintf_filtered (gdb_stderr, string, args);
534 fprintf_filtered (gdb_stderr, "\n");
535 /* Save it as the last error as well (no newline) */
536 gdb_file_rewind (gdb_lasterr);
537 vfprintf_filtered (gdb_lasterr, string, args);
539 return_to_top_level (RETURN_ERROR);
543 error (const char *string,...)
546 va_start (args, string);
547 verror (string, args);
552 error_stream (GDB_FILE *stream)
555 char *msg = gdb_file_xstrdup (stream, &size);
556 make_cleanup (free, msg);
560 /* Get the last error message issued by gdb */
563 error_last_message (void)
566 return gdb_file_xstrdup (gdb_lasterr, &len);
569 /* This is to be called by main() at the very beginning */
574 gdb_lasterr = mem_fileopen ();
577 /* Print a message reporting an internal error. Ask the user if they
578 want to continue, dump core, or just exit. */
581 internal_verror (const char *fmt, va_list ap)
583 static char msg[] = "Internal GDB error: recursive internal error.\n";
584 static int dejavu = 0;
588 /* don't allow infinite error recursion. */
596 fputs_unfiltered (msg, gdb_stderr);
600 write (STDERR_FILENO, msg, sizeof (msg));
604 /* Try to get the message out */
605 fputs_unfiltered ("gdb-internal-error: ", gdb_stderr);
606 vfprintf_unfiltered (gdb_stderr, fmt, ap);
607 fputs_unfiltered ("\n", gdb_stderr);
609 /* Default (no case) is to quit GDB. When in batch mode this
610 lessens the likelhood of GDB going into an infinate loop. */
611 continue_p = query ("\
612 An internal GDB error was detected. This may make make further\n\
613 debugging unreliable. Continue this debugging session? ");
615 /* Default (no case) is to not dump core. Lessen the chance of GDB
616 leaving random core files around. */
617 dump_core_p = query ("\
618 Create a core file containing the current state of GDB? ");
637 return_to_top_level (RETURN_ERROR);
641 internal_error (char *string, ...)
644 va_start (ap, string);
645 internal_verror (string, ap);
649 /* The strerror() function can return NULL for errno values that are
650 out of range. Provide a "safe" version that always returns a
654 safe_strerror (errnum)
660 if ((msg = strerror (errnum)) == NULL)
662 sprintf (buf, "(undocumented errno %d)", errnum);
668 /* The strsignal() function can return NULL for signal values that are
669 out of range. Provide a "safe" version that always returns a
673 safe_strsignal (signo)
679 if ((msg = strsignal (signo)) == NULL)
681 sprintf (buf, "(undocumented signal %d)", signo);
688 /* Print the system error message for errno, and also mention STRING
689 as the file name for which the error was encountered.
690 Then return to command level. */
693 perror_with_name (string)
699 err = safe_strerror (errno);
700 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
701 strcpy (combined, string);
702 strcat (combined, ": ");
703 strcat (combined, err);
705 /* I understand setting these is a matter of taste. Still, some people
706 may clear errno but not know about bfd_error. Doing this here is not
708 bfd_set_error (bfd_error_no_error);
711 error ("%s.", combined);
714 /* Print the system error message for ERRCODE, and also mention STRING
715 as the file name for which the error was encountered. */
718 print_sys_errmsg (string, errcode)
725 err = safe_strerror (errcode);
726 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
727 strcpy (combined, string);
728 strcat (combined, ": ");
729 strcat (combined, err);
731 /* We want anything which was printed on stdout to come out first, before
733 gdb_flush (gdb_stdout);
734 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
737 /* Control C eventually causes this to be called, at a convenient time. */
742 serial_t gdb_stdout_serial = serial_fdopen (1);
744 target_terminal_ours ();
746 /* We want all output to appear now, before we print "Quit". We
747 have 3 levels of buffering we have to flush (it's possible that
748 some of these should be changed to flush the lower-level ones
751 /* 1. The _filtered buffer. */
752 wrap_here ((char *) 0);
754 /* 2. The stdio buffer. */
755 gdb_flush (gdb_stdout);
756 gdb_flush (gdb_stderr);
758 /* 3. The system-level buffer. */
759 SERIAL_DRAIN_OUTPUT (gdb_stdout_serial);
760 SERIAL_UN_FDOPEN (gdb_stdout_serial);
762 annotate_error_begin ();
764 /* Don't use *_filtered; we don't want to prompt the user to continue. */
766 fprintf_unfiltered (gdb_stderr, quit_pre_print);
769 /* No steenking SIGINT will ever be coming our way when the
770 program is resumed. Don't lie. */
771 fprintf_unfiltered (gdb_stderr, "Quit\n");
774 /* If there is no terminal switching for this target, then we can't
775 possibly get screwed by the lack of job control. */
776 || current_target.to_terminal_ours == NULL)
777 fprintf_unfiltered (gdb_stderr, "Quit\n");
779 fprintf_unfiltered (gdb_stderr,
780 "Quit (expect signal SIGINT when the program is resumed)\n");
782 return_to_top_level (RETURN_QUIT);
786 #if defined(_MSC_VER) /* should test for wingdb instead? */
789 * Windows translates all keyboard and mouse events
790 * into a message which is appended to the message
791 * queue for the process.
797 int k = win32pollquit ();
804 #else /* !defined(_MSC_VER) */
809 /* Done by signals */
812 #endif /* !defined(_MSC_VER) */
814 /* Control C comes here */
820 /* Restore the signal handler. Harmless with BSD-style signals, needed
821 for System V-style signals. So just always do it, rather than worrying
822 about USG defines and stuff like that. */
823 signal (signo, request_quit);
833 /* Memory management stuff (malloc friends). */
835 /* Make a substitute size_t for non-ANSI compilers. */
837 #ifndef HAVE_STDDEF_H
839 #define size_t unsigned int
843 #if !defined (USE_MMALLOC)
850 return malloc (size);
854 mrealloc (md, ptr, size)
859 if (ptr == 0) /* Guard against old realloc's */
860 return malloc (size);
862 return realloc (ptr, size);
873 #endif /* USE_MMALLOC */
875 #if !defined (USE_MMALLOC) || defined (NO_MMCHECK)
883 #else /* Have mmalloc and want corruption checking */
888 fprintf_unfiltered (gdb_stderr, "Memory corruption\n");
892 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
893 by MD, to detect memory corruption. Note that MD may be NULL to specify
894 the default heap that grows via sbrk.
896 Note that for freshly created regions, we must call mmcheckf prior to any
897 mallocs in the region. Otherwise, any region which was allocated prior to
898 installing the checking hooks, which is later reallocated or freed, will
899 fail the checks! The mmcheck function only allows initial hooks to be
900 installed before the first mmalloc. However, anytime after we have called
901 mmcheck the first time to install the checking hooks, we can call it again
902 to update the function pointer to the memory corruption handler.
904 Returns zero on failure, non-zero on success. */
906 #ifndef MMCHECK_FORCE
907 #define MMCHECK_FORCE 0
914 if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE))
916 /* Don't use warning(), which relies on current_target being set
917 to something other than dummy_target, until after
918 initialize_all_files(). */
921 (gdb_stderr, "warning: failed to install memory consistency checks; ");
923 (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n");
929 #endif /* Have mmalloc and want corruption checking */
931 /* Called when a memory allocation fails, with the number of bytes of
932 memory requested in SIZE. */
940 internal_error ("virtual memory exhausted: can't allocate %ld bytes.", size);
944 internal_error ("virtual memory exhausted.");
948 /* Like mmalloc but get error if no storage available, and protect against
949 the caller wanting to allocate zero bytes. Whether to return NULL for
950 a zero byte request, or translate the request into a request for one
951 byte of zero'd storage, is a religious issue. */
964 else if ((val = mmalloc (md, size)) == NULL)
971 /* Like mrealloc but get error if no storage available. */
974 xmrealloc (md, ptr, size)
983 val = mrealloc (md, ptr, size);
987 val = mmalloc (md, size);
996 /* Like malloc but get error if no storage available, and protect against
997 the caller wanting to allocate zero bytes. */
1003 return (xmmalloc ((PTR) NULL, size));
1006 /* Like mrealloc but get error if no storage available. */
1009 xrealloc (ptr, size)
1013 return (xmrealloc ((PTR) NULL, ptr, size));
1017 /* My replacement for the read system call.
1018 Used like `read' but keeps going if `read' returns too soon. */
1021 myread (desc, addr, len)
1031 val = read (desc, addr, len);
1035 return orglen - len;
1042 /* Make a copy of the string at PTR with SIZE characters
1043 (and add a null character at the end in the copy).
1044 Uses malloc to get the space. Returns the address of the copy. */
1047 savestring (ptr, size)
1051 register char *p = (char *) xmalloc (size + 1);
1052 memcpy (p, ptr, size);
1058 msavestring (md, ptr, size)
1063 register char *p = (char *) xmmalloc (md, size + 1);
1064 memcpy (p, ptr, size);
1069 /* The "const" is so it compiles under DGUX (which prototypes strsave
1070 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
1071 Doesn't real strsave return NULL if out of memory? */
1076 return savestring (ptr, strlen (ptr));
1084 return (msavestring (md, ptr, strlen (ptr)));
1088 print_spaces (n, file)
1090 register GDB_FILE *file;
1092 fputs_unfiltered (n_spaces (n), file);
1095 /* Print a host address. */
1098 gdb_print_host_address (void *addr, struct gdb_file *stream)
1101 /* We could use the %p conversion specifier to fprintf if we had any
1102 way of knowing whether this host supports it. But the following
1103 should work on the Alpha and on 32 bit machines. */
1105 fprintf_filtered (stream, "0x%lx", (unsigned long) addr);
1108 /* Ask user a y-or-n question and return 1 iff answer is yes.
1109 Takes three args which are given to printf to print the question.
1110 The first, a control string, should end in "? ".
1111 It should not say how to answer, because we do that. */
1115 query (char *ctlstr,...)
1118 register int answer;
1122 va_start (args, ctlstr);
1126 return query_hook (ctlstr, args);
1129 /* Automatically answer "yes" if input is not from a terminal. */
1130 if (!input_from_terminal_p ())
1133 /* FIXME Automatically answer "yes" if called from MacGDB. */
1140 wrap_here (""); /* Flush any buffered output */
1141 gdb_flush (gdb_stdout);
1143 if (annotation_level > 1)
1144 printf_filtered ("\n\032\032pre-query\n");
1146 vfprintf_filtered (gdb_stdout, ctlstr, args);
1147 printf_filtered ("(y or n) ");
1149 if (annotation_level > 1)
1150 printf_filtered ("\n\032\032query\n");
1153 /* If not in MacGDB, move to a new line so the entered line doesn't
1154 have a prompt on the front of it. */
1156 fputs_unfiltered ("\n", gdb_stdout);
1160 gdb_flush (gdb_stdout);
1163 if (!tui_version || cmdWin == tuiWinWithFocus ())
1165 answer = fgetc (stdin);
1168 answer = (unsigned char) tuiBufferGetc ();
1171 clearerr (stdin); /* in case of C-d */
1172 if (answer == EOF) /* C-d */
1177 /* Eat rest of input line, to EOF or newline */
1178 if ((answer != '\n') || (tui_version && answer != '\r'))
1182 if (!tui_version || cmdWin == tuiWinWithFocus ())
1184 ans2 = fgetc (stdin);
1187 ans2 = (unsigned char) tuiBufferGetc ();
1191 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
1192 TUIDO (((TuiOpaqueFuncPtr) tui_vStartNewLines, 1));
1206 printf_filtered ("Please answer y or n.\n");
1209 if (annotation_level > 1)
1210 printf_filtered ("\n\032\032post-query\n");
1215 /* Parse a C escape sequence. STRING_PTR points to a variable
1216 containing a pointer to the string to parse. That pointer
1217 should point to the character after the \. That pointer
1218 is updated past the characters we use. The value of the
1219 escape sequence is returned.
1221 A negative value means the sequence \ newline was seen,
1222 which is supposed to be equivalent to nothing at all.
1224 If \ is followed by a null character, we return a negative
1225 value and leave the string pointer pointing at the null character.
1227 If \ is followed by 000, we return 0 and leave the string pointer
1228 after the zeros. A value of 0 does not mean end of string. */
1231 parse_escape (string_ptr)
1234 register int c = *(*string_ptr)++;
1238 return 007; /* Bell (alert) char */
1241 case 'e': /* Escape character */
1259 c = *(*string_ptr)++;
1261 c = parse_escape (string_ptr);
1264 return (c & 0200) | (c & 037);
1275 register int i = c - '0';
1276 register int count = 0;
1279 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
1297 /* Print the character C on STREAM as part of the contents of a literal
1298 string whose delimiter is QUOTER. Note that this routine should only
1299 be call for printing things which are independent of the language
1300 of the program being debugged. */
1302 static void printchar PARAMS ((int c, void (*do_fputs) (const char *, GDB_FILE*), void (*do_fprintf) (GDB_FILE*, const char *, ...), GDB_FILE *stream, int quoter));
1305 printchar (c, do_fputs, do_fprintf, stream, quoter)
1307 void (*do_fputs) PARAMS ((const char *, GDB_FILE*));
1308 void (*do_fprintf) PARAMS ((GDB_FILE*, const char *, ...));
1313 c &= 0xFF; /* Avoid sign bit follies */
1315 if (c < 0x20 || /* Low control chars */
1316 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1317 (sevenbit_strings && c >= 0x80))
1318 { /* high order bit set */
1322 do_fputs ("\\n", stream);
1325 do_fputs ("\\b", stream);
1328 do_fputs ("\\t", stream);
1331 do_fputs ("\\f", stream);
1334 do_fputs ("\\r", stream);
1337 do_fputs ("\\e", stream);
1340 do_fputs ("\\a", stream);
1343 do_fprintf (stream, "\\%.3o", (unsigned int) c);
1349 if (c == '\\' || c == quoter)
1350 do_fputs ("\\", stream);
1351 do_fprintf (stream, "%c", c);
1355 /* Print the character C on STREAM as part of the contents of a
1356 literal string whose delimiter is QUOTER. Note that these routines
1357 should only be call for printing things which are independent of
1358 the language of the program being debugged. */
1361 fputstr_filtered (str, quoter, stream)
1367 printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter);
1371 fputstr_unfiltered (str, quoter, stream)
1377 printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1381 fputstrn_unfiltered (str, n, quoter, stream)
1388 for (i = 0; i < n; i++)
1389 printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1394 /* Number of lines per page or UINT_MAX if paging is disabled. */
1395 static unsigned int lines_per_page;
1396 /* Number of chars per line or UNIT_MAX if line folding is disabled. */
1397 static unsigned int chars_per_line;
1398 /* Current count of lines printed on this page, chars on this line. */
1399 static unsigned int lines_printed, chars_printed;
1401 /* Buffer and start column of buffered text, for doing smarter word-
1402 wrapping. When someone calls wrap_here(), we start buffering output
1403 that comes through fputs_filtered(). If we see a newline, we just
1404 spit it out and forget about the wrap_here(). If we see another
1405 wrap_here(), we spit it out and remember the newer one. If we see
1406 the end of the line, we spit out a newline, the indent, and then
1407 the buffered output. */
1409 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1410 are waiting to be output (they have already been counted in chars_printed).
1411 When wrap_buffer[0] is null, the buffer is empty. */
1412 static char *wrap_buffer;
1414 /* Pointer in wrap_buffer to the next character to fill. */
1415 static char *wrap_pointer;
1417 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1419 static char *wrap_indent;
1421 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1422 is not in effect. */
1423 static int wrap_column;
1426 /* Inialize the lines and chars per page */
1431 if (tui_version && m_winPtrNotNull (cmdWin))
1433 lines_per_page = cmdWin->generic.height;
1434 chars_per_line = cmdWin->generic.width;
1439 /* These defaults will be used if we are unable to get the correct
1440 values from termcap. */
1441 #if defined(__GO32__)
1442 lines_per_page = ScreenRows ();
1443 chars_per_line = ScreenCols ();
1445 lines_per_page = 24;
1446 chars_per_line = 80;
1448 #if !defined (MPW) && !defined (_WIN32)
1449 /* No termcap under MPW, although might be cool to do something
1450 by looking at worksheet or console window sizes. */
1451 /* Initialize the screen height and width from termcap. */
1453 char *termtype = getenv ("TERM");
1455 /* Positive means success, nonpositive means failure. */
1458 /* 2048 is large enough for all known terminals, according to the
1459 GNU termcap manual. */
1460 char term_buffer[2048];
1464 status = tgetent (term_buffer, termtype);
1468 int running_in_emacs = getenv ("EMACS") != NULL;
1470 val = tgetnum ("li");
1471 if (val >= 0 && !running_in_emacs)
1472 lines_per_page = val;
1474 /* The number of lines per page is not mentioned
1475 in the terminal description. This probably means
1476 that paging is not useful (e.g. emacs shell window),
1477 so disable paging. */
1478 lines_per_page = UINT_MAX;
1480 val = tgetnum ("co");
1482 chars_per_line = val;
1488 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1490 /* If there is a better way to determine the window size, use it. */
1491 SIGWINCH_HANDLER (SIGWINCH);
1494 /* If the output is not a terminal, don't paginate it. */
1495 if (!GDB_FILE_ISATTY (gdb_stdout))
1496 lines_per_page = UINT_MAX;
1497 } /* the command_line_version */
1504 if (chars_per_line == 0)
1509 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1510 wrap_buffer[0] = '\0';
1513 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1514 wrap_pointer = wrap_buffer; /* Start it at the beginning */
1519 set_width_command (args, from_tty, c)
1522 struct cmd_list_element *c;
1527 /* Wait, so the user can read what's on the screen. Prompt the user
1528 to continue by pressing RETURN. */
1531 prompt_for_continue ()
1534 char cont_prompt[120];
1536 if (annotation_level > 1)
1537 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1539 strcpy (cont_prompt,
1540 "---Type <return> to continue, or q <return> to quit---");
1541 if (annotation_level > 1)
1542 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1544 /* We must do this *before* we call gdb_readline, else it will eventually
1545 call us -- thinking that we're trying to print beyond the end of the
1547 reinitialize_more_filter ();
1550 /* On a real operating system, the user can quit with SIGINT.
1553 'q' is provided on all systems so users don't have to change habits
1554 from system to system, and because telling them what to do in
1555 the prompt is more user-friendly than expecting them to think of
1557 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1558 whereas control-C to gdb_readline will cause the user to get dumped
1560 ignore = readline (cont_prompt);
1562 if (annotation_level > 1)
1563 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1568 while (*p == ' ' || *p == '\t')
1573 request_quit (SIGINT);
1575 async_request_quit (0);
1581 /* Now we have to do this again, so that GDB will know that it doesn't
1582 need to save the ---Type <return>--- line at the top of the screen. */
1583 reinitialize_more_filter ();
1585 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1588 /* Reinitialize filter; ie. tell it to reset to original values. */
1591 reinitialize_more_filter ()
1597 /* Indicate that if the next sequence of characters overflows the line,
1598 a newline should be inserted here rather than when it hits the end.
1599 If INDENT is non-null, it is a string to be printed to indent the
1600 wrapped part on the next line. INDENT must remain accessible until
1601 the next call to wrap_here() or until a newline is printed through
1604 If the line is already overfull, we immediately print a newline and
1605 the indentation, and disable further wrapping.
1607 If we don't know the width of lines, but we know the page height,
1608 we must not wrap words, but should still keep track of newlines
1609 that were explicitly printed.
1611 INDENT should not contain tabs, as that will mess up the char count
1612 on the next line. FIXME.
1614 This routine is guaranteed to force out any output which has been
1615 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1616 used to force out output from the wrap_buffer. */
1622 /* This should have been allocated, but be paranoid anyway. */
1628 *wrap_pointer = '\0';
1629 fputs_unfiltered (wrap_buffer, gdb_stdout);
1631 wrap_pointer = wrap_buffer;
1632 wrap_buffer[0] = '\0';
1633 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1637 else if (chars_printed >= chars_per_line)
1639 puts_filtered ("\n");
1641 puts_filtered (indent);
1646 wrap_column = chars_printed;
1650 wrap_indent = indent;
1654 /* Ensure that whatever gets printed next, using the filtered output
1655 commands, starts at the beginning of the line. I.E. if there is
1656 any pending output for the current line, flush it and start a new
1657 line. Otherwise do nothing. */
1662 if (chars_printed > 0)
1664 puts_filtered ("\n");
1669 /* ``struct gdb_file'' implementation that maps directly onto
1670 <stdio.h>'s FILE. */
1672 static gdb_file_write_ftype stdio_file_write;
1673 static gdb_file_fputs_ftype stdio_file_fputs;
1674 static gdb_file_isatty_ftype stdio_file_isatty;
1675 static gdb_file_delete_ftype stdio_file_delete;
1676 static struct gdb_file *stdio_file_new PARAMS ((FILE * file, int close_p));
1677 static gdb_file_flush_ftype stdio_file_flush;
1679 static int stdio_file_magic;
1688 static struct gdb_file *
1689 stdio_file_new (file, close_p)
1693 struct gdb_file *gdb_file = gdb_file_new ();
1694 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
1695 stdio->magic = &stdio_file_magic;
1697 stdio->close_p = close_p;
1698 set_gdb_file_data (gdb_file, stdio, stdio_file_delete);
1699 set_gdb_file_flush (gdb_file, stdio_file_flush);
1700 set_gdb_file_write (gdb_file, stdio_file_write);
1701 set_gdb_file_fputs (gdb_file, stdio_file_fputs);
1702 set_gdb_file_isatty (gdb_file, stdio_file_isatty);
1707 stdio_file_delete (file)
1708 struct gdb_file *file;
1710 struct stdio_file *stdio = gdb_file_data (file);
1711 if (stdio->magic != &stdio_file_magic)
1712 internal_error ("stdio_file_delete: bad magic number");
1715 fclose (stdio->file);
1721 stdio_file_flush (file)
1722 struct gdb_file *file;
1724 struct stdio_file *stdio = gdb_file_data (file);
1725 if (stdio->magic != &stdio_file_magic)
1726 internal_error ("stdio_file_flush: bad magic number");
1727 fflush (stdio->file);
1731 stdio_file_write (struct gdb_file *file, const char *buf, long length_buf)
1733 struct stdio_file *stdio = gdb_file_data (file);
1734 if (stdio->magic != &stdio_file_magic)
1735 internal_error ("stdio_file_write: bad magic number");
1736 fwrite (buf, length_buf, 1, stdio->file);
1740 stdio_file_fputs (linebuffer, file)
1741 const char *linebuffer;
1742 struct gdb_file *file;
1744 struct stdio_file *stdio = gdb_file_data (file);
1745 if (stdio->magic != &stdio_file_magic)
1746 internal_error ("stdio_file_fputs: bad magic number");
1747 fputs (linebuffer, stdio->file);
1751 stdio_file_isatty (file)
1752 struct gdb_file *file;
1754 struct stdio_file *stdio = gdb_file_data (file);
1755 if (stdio->magic != &stdio_file_magic)
1756 internal_error ("stdio_file_isatty: bad magic number");
1757 return (isatty (fileno (stdio->file)));
1760 /* Like fdopen(). Create a gdb_file from a previously opened FILE. */
1763 stdio_fileopen (file)
1766 return stdio_file_new (file, 0);
1770 /* A pure memory based ``struct gdb_file'' that can be used an output
1771 buffer. The buffers accumulated contents are available via
1782 static gdb_file_rewind_ftype mem_file_rewind;
1783 static gdb_file_put_ftype mem_file_put;
1784 static gdb_file_write_ftype mem_file_write;
1785 static gdb_file_delete_ftype mem_file_delete;
1786 static struct gdb_file *mem_file_new PARAMS ((void));
1787 static int mem_file_magic;
1789 static struct gdb_file *
1792 struct mem_file *stream = XMALLOC (struct mem_file);
1793 struct gdb_file *file = gdb_file_new ();
1794 set_gdb_file_data (file, stream, mem_file_delete);
1795 set_gdb_file_rewind (file, mem_file_rewind);
1796 set_gdb_file_put (file, mem_file_put);
1797 set_gdb_file_write (file, mem_file_write);
1798 stream->magic = &mem_file_magic;
1799 stream->buffer = NULL;
1800 stream->sizeof_buffer = 0;
1801 stream->length_buffer = 0;
1806 mem_file_delete (struct gdb_file *file)
1808 struct mem_file *stream = gdb_file_data (file);
1809 if (stream->magic != &mem_file_magic)
1810 internal_error ("mem_file_delete: bad magic number");
1811 if (stream->buffer != NULL)
1812 free (stream->buffer);
1819 return mem_file_new ();
1823 mem_file_rewind (struct gdb_file *file)
1825 struct mem_file *stream = gdb_file_data (file);
1826 if (stream->magic != &mem_file_magic)
1827 internal_error ("mem_file_rewind: bad magic number");
1828 stream->length_buffer = 0;
1832 mem_file_put (struct gdb_file *file,
1833 gdb_file_put_method_ftype *write,
1836 struct mem_file *stream = gdb_file_data (file);
1837 if (stream->magic != &mem_file_magic)
1838 internal_error ("mem_file_put: bad magic number");
1839 if (stream->length_buffer > 0)
1840 write (dest, stream->buffer, stream->length_buffer);
1844 mem_file_write (struct gdb_file *file,
1848 struct mem_file *stream = gdb_file_data (file);
1849 if (stream->magic != &mem_file_magic)
1850 internal_error ("mem_file_write: bad magic number");
1851 if (stream->buffer == NULL)
1853 stream->length_buffer = length_buffer;
1854 stream->sizeof_buffer = length_buffer;
1855 stream->buffer = xmalloc (stream->sizeof_buffer);
1856 memcpy (stream->buffer, buffer, length_buffer);
1860 int new_length = stream->length_buffer + length_buffer;
1861 if (new_length >= stream->sizeof_buffer)
1863 stream->sizeof_buffer = new_length;
1864 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
1866 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
1867 stream->length_buffer = new_length;
1871 /* A ``struct gdb_file'' that is compatible with all the legacy
1885 enum streamtype ts_streamtype;
1886 FILE *ts_filestream;
1891 static gdb_file_flush_ftype tui_file_flush;
1892 extern gdb_file_fputs_ftype tui_file_fputs;
1893 static gdb_file_isatty_ftype tui_file_isatty;
1894 static gdb_file_rewind_ftype tui_file_rewind;
1895 static gdb_file_put_ftype tui_file_put;
1896 static gdb_file_delete_ftype tui_file_delete;
1897 static struct gdb_file *tui_file_new PARAMS ((void));
1898 static int tui_file_magic;
1900 static struct gdb_file *
1903 struct tui_stream *tui = xmalloc (sizeof (struct tui_stream));
1904 struct gdb_file *file = gdb_file_new ();
1905 set_gdb_file_data (file, tui, tui_file_delete);
1906 set_gdb_file_flush (file, tui_file_flush);
1907 set_gdb_file_fputs (file, tui_file_fputs);
1908 set_gdb_file_isatty (file, tui_file_isatty);
1909 set_gdb_file_rewind (file, tui_file_rewind);
1910 set_gdb_file_put (file, tui_file_put);
1911 tui->ts_magic = &tui_file_magic;
1916 tui_file_delete (file)
1917 struct gdb_file *file;
1919 struct tui_stream *tmpstream = gdb_file_data (file);
1920 if (tmpstream->ts_magic != &tui_file_magic)
1921 internal_error ("tui_file_delete: bad magic number");
1922 if ((tmpstream->ts_streamtype == astring) &&
1923 (tmpstream->ts_strbuf != NULL))
1925 free (tmpstream->ts_strbuf);
1931 tui_fileopen (stream)
1934 struct gdb_file *file = tui_file_new ();
1935 struct tui_stream *tmpstream = gdb_file_data (file);
1936 tmpstream->ts_streamtype = afile;
1937 tmpstream->ts_filestream = stream;
1938 tmpstream->ts_strbuf = NULL;
1939 tmpstream->ts_buflen = 0;
1947 struct gdb_file *file = tui_file_new ();
1948 struct tui_stream *tmpstream = gdb_file_data (file);
1949 tmpstream->ts_streamtype = astring;
1950 tmpstream->ts_filestream = NULL;
1953 tmpstream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
1954 tmpstream->ts_strbuf[0] = '\0';
1957 /* Do not allocate the buffer now. The first time something is printed
1958 one will be allocated by tui_file_adjust_strbuf() */
1959 tmpstream->ts_strbuf = NULL;
1960 tmpstream->ts_buflen = n;
1965 tui_file_isatty (file)
1966 struct gdb_file *file;
1968 struct tui_stream *stream = gdb_file_data (file);
1969 if (stream->ts_magic != &tui_file_magic)
1970 internal_error ("tui_file_isatty: bad magic number");
1971 if (stream->ts_streamtype == afile)
1972 return (isatty (fileno (stream->ts_filestream)));
1978 tui_file_rewind (file)
1979 struct gdb_file *file;
1981 struct tui_stream *stream = gdb_file_data (file);
1982 if (stream->ts_magic != &tui_file_magic)
1983 internal_error ("tui_file_rewind: bad magic number");
1984 stream->ts_strbuf[0] = '\0';
1988 tui_file_put (struct gdb_file *file,
1989 gdb_file_put_method_ftype *write,
1992 struct tui_stream *stream = gdb_file_data (file);
1993 if (stream->ts_magic != &tui_file_magic)
1994 internal_error ("tui_file_put: bad magic number");
1995 if (stream->ts_streamtype == astring)
1996 write (dest, stream->ts_strbuf, strlen (stream->ts_strbuf));
1999 /* All TUI I/O sent to the *_filtered and *_unfiltered functions
2000 eventually ends up here. The fputs_unfiltered_hook is primarily
2001 used by GUIs to collect all output and send it to the GUI, instead
2002 of the controlling terminal. Only output to gdb_stdout and
2003 gdb_stderr are sent to the hook. Everything else is sent on to
2004 fputs to allow file I/O to be handled appropriately. */
2006 /* FIXME: Should be broken up and moved to a TUI specific file. */
2009 tui_file_fputs (linebuffer, file)
2010 const char *linebuffer;
2013 struct tui_stream *stream = gdb_file_data (file);
2015 extern int tui_owns_terminal;
2017 /* NOTE: cagney/1999-10-13: The use of fputs_unfiltered_hook is
2018 seriously discouraged. Those wanting to hook output should
2019 instead implement their own gdb_file object and install that. See
2020 also tui_file_flush(). */
2021 if (fputs_unfiltered_hook
2022 && (file == gdb_stdout
2023 || file == gdb_stderr))
2024 fputs_unfiltered_hook (linebuffer, file);
2028 if (tui_version && tui_owns_terminal)
2030 /* If we get here somehow while updating the TUI (from
2031 * within a tuiDo(), then we need to temporarily
2032 * set up the terminal for GDB output. This probably just
2033 * happens on error output.
2036 if (stream->ts_streamtype == astring)
2038 tui_file_adjust_strbuf (strlen (linebuffer), stream);
2039 strcat (stream->ts_strbuf, linebuffer);
2043 tuiTermUnsetup (0, (tui_version) ? cmdWin->detail.commandInfo.curch : 0);
2044 fputs (linebuffer, stream->ts_filestream);
2046 if (linebuffer[strlen (linebuffer) - 1] == '\n')
2047 tuiClearCommandCharCount ();
2049 tuiIncrCommandCharCountBy (strlen (linebuffer));
2054 /* The normal case - just do a fputs() */
2055 if (stream->ts_streamtype == astring)
2057 tui_file_adjust_strbuf (strlen (linebuffer), stream);
2058 strcat (stream->ts_strbuf, linebuffer);
2061 fputs (linebuffer, stream->ts_filestream);
2066 if (stream->ts_streamtype == astring)
2068 tui_file_adjust_strbuf (strlen (linebuffer), file);
2069 strcat (stream->ts_strbuf, linebuffer);
2072 fputs (linebuffer, stream->ts_filestream);
2078 tui_file_get_strbuf (struct gdb_file *file)
2080 struct tui_stream *stream = gdb_file_data (file);
2081 if (stream->ts_magic != &tui_file_magic)
2082 internal_error ("tui_file_get_strbuf: bad magic number");
2083 return (stream->ts_strbuf);
2086 /* adjust the length of the buffer by the amount necessary
2087 to accomodate appending a string of length N to the buffer contents */
2089 tui_file_adjust_strbuf (int n, struct gdb_file *file)
2091 struct tui_stream *stream = gdb_file_data (file);
2093 if (stream->ts_magic != &tui_file_magic)
2094 internal_error ("tui_file_adjust_strbuf: bad magic number");
2096 if (stream->ts_streamtype != astring)
2099 if (stream->ts_strbuf)
2101 /* There is already a buffer allocated */
2102 non_null_chars = strlen (stream->ts_strbuf);
2104 if (n > (stream->ts_buflen - non_null_chars - 1))
2106 stream->ts_buflen = n + non_null_chars + 1;
2107 stream->ts_strbuf = xrealloc (stream->ts_strbuf, stream->ts_buflen);
2111 /* No buffer yet, so allocate one of the desired size */
2112 stream->ts_strbuf = xmalloc ((n + 1) * sizeof (char));
2116 gdb_fopen (name, mode)
2120 FILE *f = fopen (name, mode);
2123 return stdio_file_new (f, 1);
2127 tui_file_flush (file)
2130 struct tui_stream *stream = gdb_file_data (file);
2131 if (stream->ts_magic != &tui_file_magic)
2132 internal_error ("tui_file_flush: bad magic number");
2134 /* NOTE: cagney/1999-10-12: If we've been linked with code that uses
2135 fputs_unfiltered_hook then we assume that it doesn't need to know
2136 about flushes. Code that does need to know about flushes can
2137 implement a proper gdb_file object. */
2138 if (fputs_unfiltered_hook)
2141 switch (stream->ts_streamtype)
2146 fflush (stream->ts_filestream);
2151 /* Implement the ``struct gdb_file'' object. */
2153 static gdb_file_isatty_ftype null_file_isatty;
2154 static gdb_file_write_ftype null_file_write;
2155 static gdb_file_fputs_ftype null_file_fputs;
2156 static gdb_file_flush_ftype null_file_flush;
2157 static gdb_file_delete_ftype null_file_delete;
2158 static gdb_file_rewind_ftype null_file_rewind;
2159 static gdb_file_put_ftype null_file_put;
2164 gdb_file_flush_ftype *to_flush;
2165 gdb_file_write_ftype *to_write;
2166 gdb_file_fputs_ftype *to_fputs;
2167 gdb_file_delete_ftype *to_delete;
2168 gdb_file_isatty_ftype *to_isatty;
2169 gdb_file_rewind_ftype *to_rewind;
2170 gdb_file_put_ftype *to_put;
2178 struct gdb_file *file = xmalloc (sizeof (struct gdb_file));
2179 file->magic = &gdb_file_magic;
2180 set_gdb_file_data (file, NULL, null_file_delete);
2181 set_gdb_file_flush (file, null_file_flush);
2182 set_gdb_file_write (file, null_file_write);
2183 set_gdb_file_fputs (file, null_file_fputs);
2184 set_gdb_file_isatty (file, null_file_isatty);
2185 set_gdb_file_rewind (file, null_file_rewind);
2186 set_gdb_file_put (file, null_file_put);
2191 gdb_file_delete (file)
2192 struct gdb_file *file;
2194 file->to_delete (file);
2199 null_file_isatty (file)
2200 struct gdb_file *file;
2206 null_file_rewind (file)
2207 struct gdb_file *file;
2213 null_file_put (struct gdb_file *file,
2214 gdb_file_put_method_ftype *write,
2221 null_file_flush (file)
2222 struct gdb_file *file;
2228 null_file_write (struct gdb_file *file,
2232 if (file->to_fputs == null_file_fputs)
2233 /* Both the write and fputs methods are null. Discard the
2238 /* The fputs method isn't null, slowly pass the write request
2239 onto that. FYI, this isn't as bad as it may look - the
2240 current (as of 1999-11-07) printf_* function calls fputc and
2241 fputc does exactly the below. By having a write function it
2242 is possible to clean up that code. */
2246 for (i = 0; i < sizeof_buf; i++)
2249 file->to_fputs (b, file);
2256 null_file_fputs (buf, file)
2258 struct gdb_file *file;
2260 if (file->to_write == null_file_write)
2261 /* Both the write and fputs methods are null. Discard the
2266 /* The write method was implemented, use that. */
2267 file->to_write (file, buf, strlen (buf));
2272 null_file_delete (file)
2273 struct gdb_file *file;
2279 gdb_file_data (file)
2280 struct gdb_file *file;
2282 if (file->magic != &gdb_file_magic)
2283 internal_error ("gdb_file_data: bad magic number");
2284 return file->to_data;
2289 struct gdb_file *file;
2291 file->to_flush (file);
2295 gdb_file_isatty (file)
2296 struct gdb_file *file;
2298 return file->to_isatty (file);
2302 gdb_file_rewind (file)
2303 struct gdb_file *file;
2305 file->to_rewind (file);
2309 gdb_file_put (struct gdb_file *file,
2310 gdb_file_put_method_ftype *write,
2313 file->to_put (file, write, dest);
2317 gdb_file_write (struct gdb_file *file,
2321 file->to_write (file, buf, length_buf);
2325 fputs_unfiltered (buf, file)
2327 struct gdb_file *file;
2329 file->to_fputs (buf, file);
2333 set_gdb_file_flush (file, flush)
2334 struct gdb_file *file;
2335 gdb_file_flush_ftype *flush;
2337 file->to_flush = flush;
2341 set_gdb_file_isatty (file, isatty)
2342 struct gdb_file *file;
2343 gdb_file_isatty_ftype *isatty;
2345 file->to_isatty = isatty;
2349 set_gdb_file_rewind (file, rewind)
2350 struct gdb_file *file;
2351 gdb_file_rewind_ftype *rewind;
2353 file->to_rewind = rewind;
2357 set_gdb_file_put (file, put)
2358 struct gdb_file *file;
2359 gdb_file_put_ftype *put;
2365 set_gdb_file_write (struct gdb_file *file,
2366 gdb_file_write_ftype *write)
2368 file->to_write = write;
2372 set_gdb_file_fputs (file, fputs)
2373 struct gdb_file *file;
2374 gdb_file_fputs_ftype *fputs;
2376 file->to_fputs = fputs;
2380 set_gdb_file_data (file, data, delete)
2381 struct gdb_file *file;
2383 gdb_file_delete_ftype *delete;
2385 file->to_data = data;
2386 file->to_delete = delete;
2389 /* gdb_file utility function for converting a ``struct gdb_file'' into
2390 a memory buffer''. */
2392 struct accumulated_gdb_file
2399 do_gdb_file_xstrdup (void *context, const char *buffer, long length)
2401 struct accumulated_gdb_file *acc = context;
2402 if (acc->buffer == NULL)
2403 acc->buffer = xmalloc (length + 1);
2405 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
2406 memcpy (acc->buffer + acc->length, buffer, length);
2407 acc->length += length;
2408 acc->buffer[acc->length] = '\0';
2412 gdb_file_xstrdup (struct gdb_file *file,
2415 struct accumulated_gdb_file acc;
2418 gdb_file_put (file, do_gdb_file_xstrdup, &acc);
2419 if (acc.buffer == NULL)
2420 acc.buffer = xstrdup ("");
2421 *length = acc.length;
2426 /* Like fputs but if FILTER is true, pause after every screenful.
2428 Regardless of FILTER can wrap at points other than the final
2429 character of a line.
2431 Unlike fputs, fputs_maybe_filtered does not return a value.
2432 It is OK for LINEBUFFER to be NULL, in which case just don't print
2435 Note that a longjmp to top level may occur in this routine (only if
2436 FILTER is true) (since prompt_for_continue may do so) so this
2437 routine should not be called when cleanups are not in place. */
2440 fputs_maybe_filtered (linebuffer, stream, filter)
2441 const char *linebuffer;
2445 const char *lineptr;
2447 if (linebuffer == 0)
2450 /* Don't do any filtering if it is disabled. */
2451 if ((stream != gdb_stdout) || !pagination_enabled
2452 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
2454 fputs_unfiltered (linebuffer, stream);
2458 /* Go through and output each character. Show line extension
2459 when this is necessary; prompt user for new page when this is
2462 lineptr = linebuffer;
2465 /* Possible new page. */
2467 (lines_printed >= lines_per_page - 1))
2468 prompt_for_continue ();
2470 while (*lineptr && *lineptr != '\n')
2472 /* Print a single line. */
2473 if (*lineptr == '\t')
2476 *wrap_pointer++ = '\t';
2478 fputc_unfiltered ('\t', stream);
2479 /* Shifting right by 3 produces the number of tab stops
2480 we have already passed, and then adding one and
2481 shifting left 3 advances to the next tab stop. */
2482 chars_printed = ((chars_printed >> 3) + 1) << 3;
2488 *wrap_pointer++ = *lineptr;
2490 fputc_unfiltered (*lineptr, stream);
2495 if (chars_printed >= chars_per_line)
2497 unsigned int save_chars = chars_printed;
2501 /* If we aren't actually wrapping, don't output newline --
2502 if chars_per_line is right, we probably just overflowed
2503 anyway; if it's wrong, let us keep going. */
2505 fputc_unfiltered ('\n', stream);
2507 /* Possible new page. */
2508 if (lines_printed >= lines_per_page - 1)
2509 prompt_for_continue ();
2511 /* Now output indentation and wrapped string */
2514 fputs_unfiltered (wrap_indent, stream);
2515 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
2516 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
2517 /* FIXME, this strlen is what prevents wrap_indent from
2518 containing tabs. However, if we recurse to print it
2519 and count its chars, we risk trouble if wrap_indent is
2520 longer than (the user settable) chars_per_line.
2521 Note also that this can set chars_printed > chars_per_line
2522 if we are printing a long string. */
2523 chars_printed = strlen (wrap_indent)
2524 + (save_chars - wrap_column);
2525 wrap_pointer = wrap_buffer; /* Reset buffer */
2526 wrap_buffer[0] = '\0';
2527 wrap_column = 0; /* And disable fancy wrap */
2532 if (*lineptr == '\n')
2535 wrap_here ((char *) 0); /* Spit out chars, cancel further wraps */
2537 fputc_unfiltered ('\n', stream);
2544 fputs_filtered (linebuffer, stream)
2545 const char *linebuffer;
2548 fputs_maybe_filtered (linebuffer, stream, 1);
2552 putchar_unfiltered (c)
2556 gdb_file_write (gdb_stdout, &buf, 1);
2561 fputc_unfiltered (c, stream)
2566 gdb_file_write (stream, &buf, 1);
2571 fputc_filtered (c, stream)
2579 fputs_filtered (buf, stream);
2583 /* puts_debug is like fputs_unfiltered, except it prints special
2584 characters in printable fashion. */
2587 puts_debug (prefix, string, suffix)
2594 /* Print prefix and suffix after each line. */
2595 static int new_line = 1;
2596 static int return_p = 0;
2597 static char *prev_prefix = "";
2598 static char *prev_suffix = "";
2600 if (*string == '\n')
2603 /* If the prefix is changing, print the previous suffix, a new line,
2604 and the new prefix. */
2605 if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line)
2607 fputs_unfiltered (prev_suffix, gdb_stdlog);
2608 fputs_unfiltered ("\n", gdb_stdlog);
2609 fputs_unfiltered (prefix, gdb_stdlog);
2612 /* Print prefix if we printed a newline during the previous call. */
2616 fputs_unfiltered (prefix, gdb_stdlog);
2619 prev_prefix = prefix;
2620 prev_suffix = suffix;
2622 /* Output characters in a printable format. */
2623 while ((ch = *string++) != '\0')
2629 fputc_unfiltered (ch, gdb_stdlog);
2632 fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff);
2636 fputs_unfiltered ("\\\\", gdb_stdlog);
2639 fputs_unfiltered ("\\b", gdb_stdlog);
2642 fputs_unfiltered ("\\f", gdb_stdlog);
2646 fputs_unfiltered ("\\n", gdb_stdlog);
2649 fputs_unfiltered ("\\r", gdb_stdlog);
2652 fputs_unfiltered ("\\t", gdb_stdlog);
2655 fputs_unfiltered ("\\v", gdb_stdlog);
2659 return_p = ch == '\r';
2662 /* Print suffix if we printed a newline. */
2665 fputs_unfiltered (suffix, gdb_stdlog);
2666 fputs_unfiltered ("\n", gdb_stdlog);
2671 /* Print a variable number of ARGS using format FORMAT. If this
2672 information is going to put the amount written (since the last call
2673 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
2674 call prompt_for_continue to get the users permision to continue.
2676 Unlike fprintf, this function does not return a value.
2678 We implement three variants, vfprintf (takes a vararg list and stream),
2679 fprintf (takes a stream to write on), and printf (the usual).
2681 Note also that a longjmp to top level may occur in this routine
2682 (since prompt_for_continue may do so) so this routine should not be
2683 called when cleanups are not in place. */
2686 vfprintf_maybe_filtered (stream, format, args, filter)
2693 struct cleanup *old_cleanups;
2695 vasprintf (&linebuffer, format, args);
2696 if (linebuffer == NULL)
2698 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
2701 old_cleanups = make_cleanup (free, linebuffer);
2702 fputs_maybe_filtered (linebuffer, stream, filter);
2703 do_cleanups (old_cleanups);
2708 vfprintf_filtered (stream, format, args)
2713 vfprintf_maybe_filtered (stream, format, args, 1);
2717 vfprintf_unfiltered (stream, format, args)
2723 struct cleanup *old_cleanups;
2725 vasprintf (&linebuffer, format, args);
2726 if (linebuffer == NULL)
2728 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
2731 old_cleanups = make_cleanup (free, linebuffer);
2732 fputs_unfiltered (linebuffer, stream);
2733 do_cleanups (old_cleanups);
2737 vprintf_filtered (format, args)
2741 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
2745 vprintf_unfiltered (format, args)
2749 vfprintf_unfiltered (gdb_stdout, format, args);
2753 fprintf_filtered (GDB_FILE * stream, const char *format,...)
2756 va_start (args, format);
2757 vfprintf_filtered (stream, format, args);
2762 fprintf_unfiltered (GDB_FILE * stream, const char *format,...)
2765 va_start (args, format);
2766 vfprintf_unfiltered (stream, format, args);
2770 /* Like fprintf_filtered, but prints its result indented.
2771 Called as fprintfi_filtered (spaces, stream, format, ...); */
2774 fprintfi_filtered (int spaces, GDB_FILE * stream, const char *format,...)
2777 va_start (args, format);
2778 print_spaces_filtered (spaces, stream);
2780 vfprintf_filtered (stream, format, args);
2786 printf_filtered (const char *format,...)
2789 va_start (args, format);
2790 vfprintf_filtered (gdb_stdout, format, args);
2796 printf_unfiltered (const char *format,...)
2799 va_start (args, format);
2800 vfprintf_unfiltered (gdb_stdout, format, args);
2804 /* Like printf_filtered, but prints it's result indented.
2805 Called as printfi_filtered (spaces, format, ...); */
2808 printfi_filtered (int spaces, const char *format,...)
2811 va_start (args, format);
2812 print_spaces_filtered (spaces, gdb_stdout);
2813 vfprintf_filtered (gdb_stdout, format, args);
2817 /* Easy -- but watch out!
2819 This routine is *not* a replacement for puts()! puts() appends a newline.
2820 This one doesn't, and had better not! */
2823 puts_filtered (string)
2826 fputs_filtered (string, gdb_stdout);
2830 puts_unfiltered (string)
2833 fputs_unfiltered (string, gdb_stdout);
2836 /* Return a pointer to N spaces and a null. The pointer is good
2837 until the next call to here. */
2843 static char *spaces = 0;
2844 static int max_spaces = -1;
2850 spaces = (char *) xmalloc (n + 1);
2851 for (t = spaces + n; t != spaces;)
2857 return spaces + max_spaces - n;
2860 /* Print N spaces. */
2862 print_spaces_filtered (n, stream)
2866 fputs_filtered (n_spaces (n), stream);
2869 /* C++ demangler stuff. */
2871 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
2872 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
2873 If the name is not mangled, or the language for the name is unknown, or
2874 demangling is off, the name is printed in its "raw" form. */
2877 fprintf_symbol_filtered (stream, name, lang, arg_mode)
2887 /* If user wants to see raw output, no problem. */
2890 fputs_filtered (name, stream);
2896 case language_cplus:
2897 demangled = cplus_demangle (name, arg_mode);
2900 demangled = cplus_demangle (name, arg_mode | DMGL_JAVA);
2902 case language_chill:
2903 demangled = chill_demangle (name);
2909 fputs_filtered (demangled ? demangled : name, stream);
2910 if (demangled != NULL)
2918 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
2919 differences in whitespace. Returns 0 if they match, non-zero if they
2920 don't (slightly different than strcmp()'s range of return values).
2922 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2923 This "feature" is useful when searching for matching C++ function names
2924 (such as if the user types 'break FOO', where FOO is a mangled C++
2928 strcmp_iw (string1, string2)
2929 const char *string1;
2930 const char *string2;
2932 while ((*string1 != '\0') && (*string2 != '\0'))
2934 while (isspace (*string1))
2938 while (isspace (*string2))
2942 if (*string1 != *string2)
2946 if (*string1 != '\0')
2952 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
2958 ** Answer whether string_to_compare is a full or partial match to
2959 ** template_string. The partial match must be in sequence starting
2963 subset_compare (string_to_compare, template_string)
2964 char *string_to_compare;
2965 char *template_string;
2968 if (template_string != (char *) NULL && string_to_compare != (char *) NULL &&
2969 strlen (string_to_compare) <= strlen (template_string))
2970 match = (strncmp (template_string,
2972 strlen (string_to_compare)) == 0);
2979 static void pagination_on_command PARAMS ((char *arg, int from_tty));
2981 pagination_on_command (arg, from_tty)
2985 pagination_enabled = 1;
2988 static void pagination_on_command PARAMS ((char *arg, int from_tty));
2990 pagination_off_command (arg, from_tty)
2994 pagination_enabled = 0;
3001 struct cmd_list_element *c;
3003 c = add_set_cmd ("width", class_support, var_uinteger,
3004 (char *) &chars_per_line,
3005 "Set number of characters gdb thinks are in a line.",
3007 add_show_from_set (c, &showlist);
3008 c->function.sfunc = set_width_command;
3011 (add_set_cmd ("height", class_support,
3012 var_uinteger, (char *) &lines_per_page,
3013 "Set number of lines gdb thinks are in a page.", &setlist),
3018 /* If the output is not a terminal, don't paginate it. */
3019 if (!GDB_FILE_ISATTY (gdb_stdout))
3020 lines_per_page = UINT_MAX;
3022 set_width_command ((char *) NULL, 0, c);
3025 (add_set_cmd ("demangle", class_support, var_boolean,
3027 "Set demangling of encoded C++ names when displaying symbols.",
3032 (add_set_cmd ("pagination", class_support,
3033 var_boolean, (char *) &pagination_enabled,
3034 "Set state of pagination.", &setlist),
3038 add_com ("am", class_support, pagination_on_command,
3039 "Enable pagination");
3040 add_com ("sm", class_support, pagination_off_command,
3041 "Disable pagination");
3045 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
3046 (char *) &sevenbit_strings,
3047 "Set printing of 8-bit characters in strings as \\nnn.",
3052 (add_set_cmd ("asm-demangle", class_support, var_boolean,
3053 (char *) &asm_demangle,
3054 "Set demangling of C++ names in disassembly listings.",
3059 /* Machine specific function to handle SIGWINCH signal. */
3061 #ifdef SIGWINCH_HANDLER_BODY
3062 SIGWINCH_HANDLER_BODY
3065 /* Support for converting target fp numbers into host DOUBLEST format. */
3067 /* XXX - This code should really be in libiberty/floatformat.c, however
3068 configuration issues with libiberty made this very difficult to do in the
3071 #include "floatformat.h"
3072 #include <math.h> /* ldexp */
3074 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
3075 going to bother with trying to muck around with whether it is defined in
3076 a system header, what we do if not, etc. */
3077 #define FLOATFORMAT_CHAR_BIT 8
3079 static unsigned long get_field PARAMS ((unsigned char *,
3080 enum floatformat_byteorders,
3085 /* Extract a field which starts at START and is LEN bytes long. DATA and
3086 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
3087 static unsigned long
3088 get_field (data, order, total_len, start, len)
3089 unsigned char *data;
3090 enum floatformat_byteorders order;
3091 unsigned int total_len;
3095 unsigned long result;
3096 unsigned int cur_byte;
3099 /* Start at the least significant part of the field. */
3100 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
3101 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
3102 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
3104 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
3105 result = *(data + cur_byte) >> (-cur_bitshift);
3106 cur_bitshift += FLOATFORMAT_CHAR_BIT;
3107 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
3112 /* Move towards the most significant part of the field. */
3113 while (cur_bitshift < len)
3115 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
3116 /* This is the last byte; zero out the bits which are not part of
3119 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
3122 result |= *(data + cur_byte) << cur_bitshift;
3123 cur_bitshift += FLOATFORMAT_CHAR_BIT;
3124 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
3132 /* Convert from FMT to a DOUBLEST.
3133 FROM is the address of the extended float.
3134 Store the DOUBLEST in *TO. */
3137 floatformat_to_doublest (fmt, from, to)
3138 const struct floatformat *fmt;
3142 unsigned char *ufrom = (unsigned char *) from;
3146 unsigned int mant_bits, mant_off;
3148 int special_exponent; /* It's a NaN, denorm or zero */
3150 /* If the mantissa bits are not contiguous from one end of the
3151 mantissa to the other, we need to make a private copy of the
3152 source bytes that is in the right order since the unpacking
3153 algorithm assumes that the bits are contiguous.
3155 Swap the bytes individually rather than accessing them through
3156 "long *" since we have no guarantee that they start on a long
3157 alignment, and also sizeof(long) for the host could be different
3158 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
3159 for the target is 4. */
3161 if (fmt->byteorder == floatformat_littlebyte_bigword)
3163 static unsigned char *newfrom;
3164 unsigned char *swapin, *swapout;
3167 longswaps = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
3170 if (newfrom == NULL)
3172 newfrom = (unsigned char *) xmalloc (fmt->totalsize);
3177 while (longswaps-- > 0)
3179 /* This is ugly, but efficient */
3180 *swapout++ = swapin[4];
3181 *swapout++ = swapin[5];
3182 *swapout++ = swapin[6];
3183 *swapout++ = swapin[7];
3184 *swapout++ = swapin[0];
3185 *swapout++ = swapin[1];
3186 *swapout++ = swapin[2];
3187 *swapout++ = swapin[3];
3192 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
3193 fmt->exp_start, fmt->exp_len);
3194 /* Note that if exponent indicates a NaN, we can't really do anything useful
3195 (not knowing if the host has NaN's, or how to build one). So it will
3196 end up as an infinity or something close; that is OK. */
3198 mant_bits_left = fmt->man_len;
3199 mant_off = fmt->man_start;
3202 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
3204 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
3205 we don't check for zero as the exponent doesn't matter. */
3206 if (!special_exponent)
3207 exponent -= fmt->exp_bias;
3208 else if (exponent == 0)
3209 exponent = 1 - fmt->exp_bias;
3211 /* Build the result algebraically. Might go infinite, underflow, etc;
3214 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
3215 increment the exponent by one to account for the integer bit. */
3217 if (!special_exponent)
3219 if (fmt->intbit == floatformat_intbit_no)
3220 dto = ldexp (1.0, exponent);
3225 while (mant_bits_left > 0)
3227 mant_bits = min (mant_bits_left, 32);
3229 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
3230 mant_off, mant_bits);
3232 dto += ldexp ((double) mant, exponent - mant_bits);
3233 exponent -= mant_bits;
3234 mant_off += mant_bits;
3235 mant_bits_left -= mant_bits;
3238 /* Negate it if negative. */
3239 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
3244 static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
3250 /* Set a field which starts at START and is LEN bytes long. DATA and
3251 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
3253 put_field (data, order, total_len, start, len, stuff_to_put)
3254 unsigned char *data;
3255 enum floatformat_byteorders order;
3256 unsigned int total_len;
3259 unsigned long stuff_to_put;
3261 unsigned int cur_byte;
3264 /* Start at the least significant part of the field. */
3265 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
3266 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
3267 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
3269 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
3270 *(data + cur_byte) &=
3271 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
3272 *(data + cur_byte) |=
3273 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
3274 cur_bitshift += FLOATFORMAT_CHAR_BIT;
3275 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
3280 /* Move towards the most significant part of the field. */
3281 while (cur_bitshift < len)
3283 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
3285 /* This is the last byte. */
3286 *(data + cur_byte) &=
3287 ~((1 << (len - cur_bitshift)) - 1);
3288 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
3291 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
3292 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
3293 cur_bitshift += FLOATFORMAT_CHAR_BIT;
3294 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
3301 #ifdef HAVE_LONG_DOUBLE
3302 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
3303 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
3304 frexp, but operates on the long double data type. */
3306 static long double ldfrexp PARAMS ((long double value, int *eptr));
3309 ldfrexp (value, eptr)
3316 /* Unfortunately, there are no portable functions for extracting the exponent
3317 of a long double, so we have to do it iteratively by multiplying or dividing
3318 by two until the fraction is between 0.5 and 1.0. */
3326 if (value >= tmp) /* Value >= 1.0 */
3327 while (value >= tmp)
3332 else if (value != 0.0l) /* Value < 1.0 and > 0.0 */
3346 #endif /* HAVE_LONG_DOUBLE */
3349 /* The converse: convert the DOUBLEST *FROM to an extended float
3350 and store where TO points. Neither FROM nor TO have any alignment
3354 floatformat_from_doublest (fmt, from, to)
3355 CONST struct floatformat *fmt;
3362 unsigned int mant_bits, mant_off;
3364 unsigned char *uto = (unsigned char *) to;
3366 memcpy (&dfrom, from, sizeof (dfrom));
3367 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
3369 return; /* Result is zero */
3370 if (dfrom != dfrom) /* Result is NaN */
3373 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
3374 fmt->exp_len, fmt->exp_nan);
3375 /* Be sure it's not infinity, but NaN value is irrel */
3376 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
3381 /* If negative, set the sign bit. */
3384 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
3388 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity */
3390 /* Infinity exponent is same as NaN's. */
3391 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
3392 fmt->exp_len, fmt->exp_nan);
3393 /* Infinity mantissa is all zeroes. */
3394 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
3399 #ifdef HAVE_LONG_DOUBLE
3400 mant = ldfrexp (dfrom, &exponent);
3402 mant = frexp (dfrom, &exponent);
3405 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
3406 exponent + fmt->exp_bias - 1);
3408 mant_bits_left = fmt->man_len;
3409 mant_off = fmt->man_start;
3410 while (mant_bits_left > 0)
3412 unsigned long mant_long;
3413 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
3415 mant *= 4294967296.0;
3416 mant_long = (unsigned long) mant;
3419 /* If the integer bit is implicit, then we need to discard it.
3420 If we are discarding a zero, we should be (but are not) creating
3421 a denormalized number which means adjusting the exponent
3423 if (mant_bits_left == fmt->man_len
3424 && fmt->intbit == floatformat_intbit_no)
3432 /* The bits we want are in the most significant MANT_BITS bits of
3433 mant_long. Move them to the least significant. */
3434 mant_long >>= 32 - mant_bits;
3437 put_field (uto, fmt->byteorder, fmt->totalsize,
3438 mant_off, mant_bits, mant_long);
3439 mant_off += mant_bits;
3440 mant_bits_left -= mant_bits;
3442 if (fmt->byteorder == floatformat_littlebyte_bigword)
3445 unsigned char *swaplow = uto;
3446 unsigned char *swaphigh = uto + 4;
3449 for (count = 0; count < 4; count++)
3452 *swaplow++ = *swaphigh;
3458 /* temporary storage using circular buffer */
3464 static char buf[NUMCELLS][CELLSIZE];
3465 static int cell = 0;
3466 if (++cell >= NUMCELLS)
3471 /* print routines to handle variable size regs, etc.
3473 FIXME: Note that t_addr is a bfd_vma, which is currently either an
3474 unsigned long or unsigned long long, determined at configure time.
3475 If t_addr is an unsigned long long and sizeof (unsigned long long)
3476 is greater than sizeof (unsigned long), then I believe this code will
3477 probably lose, at least for little endian machines. I believe that
3478 it would also be better to eliminate the switch on the absolute size
3479 of t_addr and replace it with a sequence of if statements that compare
3480 sizeof t_addr with sizeof the various types and do the right thing,
3481 which includes knowing whether or not the host supports long long.
3489 return (TARGET_PTR_BIT / 8 * 2);
3493 /* eliminate warning from compiler on 32-bit systems */
3494 static int thirty_two = 32;
3497 paddr (CORE_ADDR addr)
3499 char *paddr_str = get_cell ();
3500 switch (TARGET_PTR_BIT / 8)
3503 sprintf (paddr_str, "%08lx%08lx",
3504 (unsigned long) (addr >> thirty_two), (unsigned long) (addr & 0xffffffff));
3507 sprintf (paddr_str, "%08lx", (unsigned long) addr);
3510 sprintf (paddr_str, "%04x", (unsigned short) (addr & 0xffff));
3513 sprintf (paddr_str, "%lx", (unsigned long) addr);
3519 paddr_nz (CORE_ADDR addr)
3521 char *paddr_str = get_cell ();
3522 switch (TARGET_PTR_BIT / 8)
3526 unsigned long high = (unsigned long) (addr >> thirty_two);
3528 sprintf (paddr_str, "%lx", (unsigned long) (addr & 0xffffffff));
3530 sprintf (paddr_str, "%lx%08lx",
3531 high, (unsigned long) (addr & 0xffffffff));
3535 sprintf (paddr_str, "%lx", (unsigned long) addr);
3538 sprintf (paddr_str, "%x", (unsigned short) (addr & 0xffff));
3541 sprintf (paddr_str, "%lx", (unsigned long) addr);
3547 decimal2str (char *paddr_str, char *sign, ULONGEST addr)
3549 /* steal code from valprint.c:print_decimal(). Should this worry
3550 about the real size of addr as the above does? */
3551 unsigned long temp[3];
3555 temp[i] = addr % (1000 * 1000 * 1000);
3556 addr /= (1000 * 1000 * 1000);
3559 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
3563 sprintf (paddr_str, "%s%lu",
3567 sprintf (paddr_str, "%s%lu%09lu",
3568 sign, temp[1], temp[0]);
3571 sprintf (paddr_str, "%s%lu%09lu%09lu",
3572 sign, temp[2], temp[1], temp[0]);
3580 paddr_u (CORE_ADDR addr)
3582 char *paddr_str = get_cell ();
3583 decimal2str (paddr_str, "", addr);
3588 paddr_d (LONGEST addr)
3590 char *paddr_str = get_cell ();
3592 decimal2str (paddr_str, "-", -addr);
3594 decimal2str (paddr_str, "", addr);
3602 char *preg_str = get_cell ();
3603 switch (sizeof (t_reg))
3606 sprintf (preg_str, "%08lx%08lx",
3607 (unsigned long) (reg >> thirty_two), (unsigned long) (reg & 0xffffffff));
3610 sprintf (preg_str, "%08lx", (unsigned long) reg);
3613 sprintf (preg_str, "%04x", (unsigned short) (reg & 0xffff));
3616 sprintf (preg_str, "%lx", (unsigned long) reg);
3625 char *preg_str = get_cell ();
3626 switch (sizeof (t_reg))
3630 unsigned long high = (unsigned long) (reg >> thirty_two);
3632 sprintf (preg_str, "%lx", (unsigned long) (reg & 0xffffffff));
3634 sprintf (preg_str, "%lx%08lx",
3635 high, (unsigned long) (reg & 0xffffffff));
3639 sprintf (preg_str, "%lx", (unsigned long) reg);
3642 sprintf (preg_str, "%x", (unsigned short) (reg & 0xffff));
3645 sprintf (preg_str, "%lx", (unsigned long) reg);
3650 /* Helper functions for INNER_THAN */
3652 core_addr_lessthan (lhs, rhs)
3660 core_addr_greaterthan (lhs, rhs)