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 error (const char *string
,...)
529 va_start (args
, string
);
535 vfprintf_filtered (gdb_stderr
, string
, args
);
536 fprintf_filtered (gdb_stderr
, "\n");
537 /* Save it as the last error as well (no newline) */
538 gdb_file_rewind (gdb_lasterr
);
539 vfprintf_filtered (gdb_lasterr
, string
, args
);
541 return_to_top_level (RETURN_ERROR
);
545 /* Allows the error message to be passed on a stream buffer */
548 error_stream (GDB_FILE
*stream
)
550 error (tui_file_get_strbuf (stream
));
553 /* Get the last error message issued by gdb */
556 error_last_message (void)
558 return (tui_file_get_strbuf (gdb_lasterr
));
561 /* This is to be called by main() at the very beginning */
566 gdb_lasterr
= tui_sfileopen (132);
569 /* Print a message reporting an internal error. Ask the user if they
570 want to continue, dump core, or just exit. */
573 internal_error (char *string
, ...)
575 static char msg
[] = "Internal GDB error: recursive internal error.\n";
576 static int dejavu
= 0;
581 /* don't allow infinite error recursion. */
589 fputs_unfiltered (msg
, gdb_stderr
);
593 write (STDERR_FILENO
, msg
, sizeof (msg
));
597 /* Try to get the message out */
598 fputs_unfiltered ("gdb-internal-error: ", gdb_stderr
);
599 va_start (args
, string
);
600 vfprintf_unfiltered (gdb_stderr
, string
, args
);
602 fputs_unfiltered ("\n", gdb_stderr
);
604 /* Default (no case) is to quit GDB. When in batch mode this
605 lessens the likelhood of GDB going into an infinate loop. */
606 continue_p
= query ("\
607 An internal GDB error was detected. This may make make further\n\
608 debugging unreliable. Continue this debugging session? ");
610 /* Default (no case) is to not dump core. Lessen the chance of GDB
611 leaving random core files around. */
612 dump_core_p
= query ("\
613 Create a core file containing the current state of GDB? ");
632 return_to_top_level (RETURN_ERROR
);
635 /* The strerror() function can return NULL for errno values that are
636 out of range. Provide a "safe" version that always returns a
640 safe_strerror (errnum
)
646 if ((msg
= strerror (errnum
)) == NULL
)
648 sprintf (buf
, "(undocumented errno %d)", errnum
);
654 /* The strsignal() function can return NULL for signal values that are
655 out of range. Provide a "safe" version that always returns a
659 safe_strsignal (signo
)
665 if ((msg
= strsignal (signo
)) == NULL
)
667 sprintf (buf
, "(undocumented signal %d)", signo
);
674 /* Print the system error message for errno, and also mention STRING
675 as the file name for which the error was encountered.
676 Then return to command level. */
679 perror_with_name (string
)
685 err
= safe_strerror (errno
);
686 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
687 strcpy (combined
, string
);
688 strcat (combined
, ": ");
689 strcat (combined
, err
);
691 /* I understand setting these is a matter of taste. Still, some people
692 may clear errno but not know about bfd_error. Doing this here is not
694 bfd_set_error (bfd_error_no_error
);
697 error ("%s.", combined
);
700 /* Print the system error message for ERRCODE, and also mention STRING
701 as the file name for which the error was encountered. */
704 print_sys_errmsg (string
, errcode
)
711 err
= safe_strerror (errcode
);
712 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
713 strcpy (combined
, string
);
714 strcat (combined
, ": ");
715 strcat (combined
, err
);
717 /* We want anything which was printed on stdout to come out first, before
719 gdb_flush (gdb_stdout
);
720 fprintf_unfiltered (gdb_stderr
, "%s.\n", combined
);
723 /* Control C eventually causes this to be called, at a convenient time. */
728 serial_t gdb_stdout_serial
= serial_fdopen (1);
730 target_terminal_ours ();
732 /* We want all output to appear now, before we print "Quit". We
733 have 3 levels of buffering we have to flush (it's possible that
734 some of these should be changed to flush the lower-level ones
737 /* 1. The _filtered buffer. */
738 wrap_here ((char *) 0);
740 /* 2. The stdio buffer. */
741 gdb_flush (gdb_stdout
);
742 gdb_flush (gdb_stderr
);
744 /* 3. The system-level buffer. */
745 SERIAL_DRAIN_OUTPUT (gdb_stdout_serial
);
746 SERIAL_UN_FDOPEN (gdb_stdout_serial
);
748 annotate_error_begin ();
750 /* Don't use *_filtered; we don't want to prompt the user to continue. */
752 fprintf_unfiltered (gdb_stderr
, quit_pre_print
);
755 /* No steenking SIGINT will ever be coming our way when the
756 program is resumed. Don't lie. */
757 fprintf_unfiltered (gdb_stderr
, "Quit\n");
760 /* If there is no terminal switching for this target, then we can't
761 possibly get screwed by the lack of job control. */
762 || current_target
.to_terminal_ours
== NULL
)
763 fprintf_unfiltered (gdb_stderr
, "Quit\n");
765 fprintf_unfiltered (gdb_stderr
,
766 "Quit (expect signal SIGINT when the program is resumed)\n");
768 return_to_top_level (RETURN_QUIT
);
772 #if defined(_MSC_VER) /* should test for wingdb instead? */
775 * Windows translates all keyboard and mouse events
776 * into a message which is appended to the message
777 * queue for the process.
783 int k
= win32pollquit ();
790 #else /* !defined(__GO32__) && !defined(_MSC_VER) */
795 /* Done by signals */
798 #endif /* !defined(__GO32__) && !defined(_MSC_VER) */
800 /* Control C comes here */
806 /* Restore the signal handler. Harmless with BSD-style signals, needed
807 for System V-style signals. So just always do it, rather than worrying
808 about USG defines and stuff like that. */
809 signal (signo
, request_quit
);
819 /* Memory management stuff (malloc friends). */
821 /* Make a substitute size_t for non-ANSI compilers. */
823 #ifndef HAVE_STDDEF_H
825 #define size_t unsigned int
829 #if !defined (USE_MMALLOC)
836 return malloc (size
);
840 mrealloc (md
, ptr
, size
)
845 if (ptr
== 0) /* Guard against old realloc's */
846 return malloc (size
);
848 return realloc (ptr
, size
);
859 #endif /* USE_MMALLOC */
861 #if !defined (USE_MMALLOC) || defined (NO_MMCHECK)
869 #else /* Have mmalloc and want corruption checking */
874 fprintf_unfiltered (gdb_stderr
, "Memory corruption\n");
878 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
879 by MD, to detect memory corruption. Note that MD may be NULL to specify
880 the default heap that grows via sbrk.
882 Note that for freshly created regions, we must call mmcheckf prior to any
883 mallocs in the region. Otherwise, any region which was allocated prior to
884 installing the checking hooks, which is later reallocated or freed, will
885 fail the checks! The mmcheck function only allows initial hooks to be
886 installed before the first mmalloc. However, anytime after we have called
887 mmcheck the first time to install the checking hooks, we can call it again
888 to update the function pointer to the memory corruption handler.
890 Returns zero on failure, non-zero on success. */
892 #ifndef MMCHECK_FORCE
893 #define MMCHECK_FORCE 0
900 if (!mmcheckf (md
, malloc_botch
, MMCHECK_FORCE
))
902 /* Don't use warning(), which relies on current_target being set
903 to something other than dummy_target, until after
904 initialize_all_files(). */
907 (gdb_stderr
, "warning: failed to install memory consistency checks; ");
909 (gdb_stderr
, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n");
915 #endif /* Have mmalloc and want corruption checking */
917 /* Called when a memory allocation fails, with the number of bytes of
918 memory requested in SIZE. */
926 internal_error ("virtual memory exhausted: can't allocate %ld bytes.", size
);
930 internal_error ("virtual memory exhausted.");
934 /* Like mmalloc but get error if no storage available, and protect against
935 the caller wanting to allocate zero bytes. Whether to return NULL for
936 a zero byte request, or translate the request into a request for one
937 byte of zero'd storage, is a religious issue. */
950 else if ((val
= mmalloc (md
, size
)) == NULL
)
957 /* Like mrealloc but get error if no storage available. */
960 xmrealloc (md
, ptr
, size
)
969 val
= mrealloc (md
, ptr
, size
);
973 val
= mmalloc (md
, size
);
982 /* Like malloc but get error if no storage available, and protect against
983 the caller wanting to allocate zero bytes. */
989 return (xmmalloc ((PTR
) NULL
, size
));
992 /* Like mrealloc but get error if no storage available. */
999 return (xmrealloc ((PTR
) NULL
, ptr
, size
));
1003 /* My replacement for the read system call.
1004 Used like `read' but keeps going if `read' returns too soon. */
1007 myread (desc
, addr
, len
)
1017 val
= read (desc
, addr
, len
);
1021 return orglen
- len
;
1028 /* Make a copy of the string at PTR with SIZE characters
1029 (and add a null character at the end in the copy).
1030 Uses malloc to get the space. Returns the address of the copy. */
1033 savestring (ptr
, size
)
1037 register char *p
= (char *) xmalloc (size
+ 1);
1038 memcpy (p
, ptr
, size
);
1044 msavestring (md
, ptr
, size
)
1049 register char *p
= (char *) xmmalloc (md
, size
+ 1);
1050 memcpy (p
, ptr
, size
);
1055 /* The "const" is so it compiles under DGUX (which prototypes strsave
1056 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
1057 Doesn't real strsave return NULL if out of memory? */
1062 return savestring (ptr
, strlen (ptr
));
1070 return (msavestring (md
, ptr
, strlen (ptr
)));
1074 print_spaces (n
, file
)
1076 register GDB_FILE
*file
;
1078 fputs_unfiltered (n_spaces (n
), file
);
1081 /* Print a host address. */
1084 gdb_print_host_address (void *addr
, struct gdb_file
*stream
)
1087 /* We could use the %p conversion specifier to fprintf if we had any
1088 way of knowing whether this host supports it. But the following
1089 should work on the Alpha and on 32 bit machines. */
1091 fprintf_filtered (stream
, "0x%lx", (unsigned long) addr
);
1094 /* Ask user a y-or-n question and return 1 iff answer is yes.
1095 Takes three args which are given to printf to print the question.
1096 The first, a control string, should end in "? ".
1097 It should not say how to answer, because we do that. */
1101 query (char *ctlstr
,...)
1104 register int answer
;
1108 va_start (args
, ctlstr
);
1112 return query_hook (ctlstr
, args
);
1115 /* Automatically answer "yes" if input is not from a terminal. */
1116 if (!input_from_terminal_p ())
1119 /* FIXME Automatically answer "yes" if called from MacGDB. */
1126 wrap_here (""); /* Flush any buffered output */
1127 gdb_flush (gdb_stdout
);
1129 if (annotation_level
> 1)
1130 printf_filtered ("\n\032\032pre-query\n");
1132 vfprintf_filtered (gdb_stdout
, ctlstr
, args
);
1133 printf_filtered ("(y or n) ");
1135 if (annotation_level
> 1)
1136 printf_filtered ("\n\032\032query\n");
1139 /* If not in MacGDB, move to a new line so the entered line doesn't
1140 have a prompt on the front of it. */
1142 fputs_unfiltered ("\n", gdb_stdout
);
1146 gdb_flush (gdb_stdout
);
1149 if (!tui_version
|| cmdWin
== tuiWinWithFocus ())
1151 answer
= fgetc (stdin
);
1154 answer
= (unsigned char) tuiBufferGetc ();
1157 clearerr (stdin
); /* in case of C-d */
1158 if (answer
== EOF
) /* C-d */
1163 /* Eat rest of input line, to EOF or newline */
1164 if ((answer
!= '\n') || (tui_version
&& answer
!= '\r'))
1168 if (!tui_version
|| cmdWin
== tuiWinWithFocus ())
1170 ans2
= fgetc (stdin
);
1173 ans2
= (unsigned char) tuiBufferGetc ();
1177 while (ans2
!= EOF
&& ans2
!= '\n' && ans2
!= '\r');
1178 TUIDO (((TuiOpaqueFuncPtr
) tui_vStartNewLines
, 1));
1192 printf_filtered ("Please answer y or n.\n");
1195 if (annotation_level
> 1)
1196 printf_filtered ("\n\032\032post-query\n");
1201 /* Parse a C escape sequence. STRING_PTR points to a variable
1202 containing a pointer to the string to parse. That pointer
1203 should point to the character after the \. That pointer
1204 is updated past the characters we use. The value of the
1205 escape sequence is returned.
1207 A negative value means the sequence \ newline was seen,
1208 which is supposed to be equivalent to nothing at all.
1210 If \ is followed by a null character, we return a negative
1211 value and leave the string pointer pointing at the null character.
1213 If \ is followed by 000, we return 0 and leave the string pointer
1214 after the zeros. A value of 0 does not mean end of string. */
1217 parse_escape (string_ptr
)
1220 register int c
= *(*string_ptr
)++;
1224 return 007; /* Bell (alert) char */
1227 case 'e': /* Escape character */
1245 c
= *(*string_ptr
)++;
1247 c
= parse_escape (string_ptr
);
1250 return (c
& 0200) | (c
& 037);
1261 register int i
= c
- '0';
1262 register int count
= 0;
1265 if ((c
= *(*string_ptr
)++) >= '0' && c
<= '7')
1283 /* Print the character C on STREAM as part of the contents of a literal
1284 string whose delimiter is QUOTER. Note that this routine should only
1285 be call for printing things which are independent of the language
1286 of the program being debugged. */
1288 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
));
1291 printchar (c
, do_fputs
, do_fprintf
, stream
, quoter
)
1293 void (*do_fputs
) PARAMS ((const char *, GDB_FILE
*));
1294 void (*do_fprintf
) PARAMS ((GDB_FILE
*, const char *, ...));
1299 c
&= 0xFF; /* Avoid sign bit follies */
1301 if (c
< 0x20 || /* Low control chars */
1302 (c
>= 0x7F && c
< 0xA0) || /* DEL, High controls */
1303 (sevenbit_strings
&& c
>= 0x80))
1304 { /* high order bit set */
1308 do_fputs ("\\n", stream
);
1311 do_fputs ("\\b", stream
);
1314 do_fputs ("\\t", stream
);
1317 do_fputs ("\\f", stream
);
1320 do_fputs ("\\r", stream
);
1323 do_fputs ("\\e", stream
);
1326 do_fputs ("\\a", stream
);
1329 do_fprintf (stream
, "\\%.3o", (unsigned int) c
);
1335 if (c
== '\\' || c
== quoter
)
1336 do_fputs ("\\", stream
);
1337 do_fprintf (stream
, "%c", c
);
1341 /* Print the character C on STREAM as part of the contents of a
1342 literal string whose delimiter is QUOTER. Note that these routines
1343 should only be call for printing things which are independent of
1344 the language of the program being debugged. */
1347 fputstr_filtered (str
, quoter
, stream
)
1353 printchar (*str
++, fputs_filtered
, fprintf_filtered
, stream
, quoter
);
1357 fputstr_unfiltered (str
, quoter
, stream
)
1363 printchar (*str
++, fputs_unfiltered
, fprintf_unfiltered
, stream
, quoter
);
1367 fputstrn_unfiltered (str
, n
, quoter
, stream
)
1374 for (i
= 0; i
< n
; i
++)
1375 printchar (str
[i
], fputs_unfiltered
, fprintf_unfiltered
, stream
, quoter
);
1380 /* Number of lines per page or UINT_MAX if paging is disabled. */
1381 static unsigned int lines_per_page
;
1382 /* Number of chars per line or UNIT_MAX if line folding is disabled. */
1383 static unsigned int chars_per_line
;
1384 /* Current count of lines printed on this page, chars on this line. */
1385 static unsigned int lines_printed
, chars_printed
;
1387 /* Buffer and start column of buffered text, for doing smarter word-
1388 wrapping. When someone calls wrap_here(), we start buffering output
1389 that comes through fputs_filtered(). If we see a newline, we just
1390 spit it out and forget about the wrap_here(). If we see another
1391 wrap_here(), we spit it out and remember the newer one. If we see
1392 the end of the line, we spit out a newline, the indent, and then
1393 the buffered output. */
1395 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1396 are waiting to be output (they have already been counted in chars_printed).
1397 When wrap_buffer[0] is null, the buffer is empty. */
1398 static char *wrap_buffer
;
1400 /* Pointer in wrap_buffer to the next character to fill. */
1401 static char *wrap_pointer
;
1403 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1405 static char *wrap_indent
;
1407 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1408 is not in effect. */
1409 static int wrap_column
;
1412 /* Inialize the lines and chars per page */
1417 if (tui_version
&& m_winPtrNotNull (cmdWin
))
1419 lines_per_page
= cmdWin
->generic
.height
;
1420 chars_per_line
= cmdWin
->generic
.width
;
1425 /* These defaults will be used if we are unable to get the correct
1426 values from termcap. */
1427 #if defined(__GO32__)
1428 lines_per_page
= ScreenRows ();
1429 chars_per_line
= ScreenCols ();
1431 lines_per_page
= 24;
1432 chars_per_line
= 80;
1434 #if !defined (MPW) && !defined (_WIN32)
1435 /* No termcap under MPW, although might be cool to do something
1436 by looking at worksheet or console window sizes. */
1437 /* Initialize the screen height and width from termcap. */
1439 char *termtype
= getenv ("TERM");
1441 /* Positive means success, nonpositive means failure. */
1444 /* 2048 is large enough for all known terminals, according to the
1445 GNU termcap manual. */
1446 char term_buffer
[2048];
1450 status
= tgetent (term_buffer
, termtype
);
1454 int running_in_emacs
= getenv ("EMACS") != NULL
;
1456 val
= tgetnum ("li");
1457 if (val
>= 0 && !running_in_emacs
)
1458 lines_per_page
= val
;
1460 /* The number of lines per page is not mentioned
1461 in the terminal description. This probably means
1462 that paging is not useful (e.g. emacs shell window),
1463 so disable paging. */
1464 lines_per_page
= UINT_MAX
;
1466 val
= tgetnum ("co");
1468 chars_per_line
= val
;
1474 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1476 /* If there is a better way to determine the window size, use it. */
1477 SIGWINCH_HANDLER (SIGWINCH
);
1480 /* If the output is not a terminal, don't paginate it. */
1481 if (!GDB_FILE_ISATTY (gdb_stdout
))
1482 lines_per_page
= UINT_MAX
;
1483 } /* the command_line_version */
1490 if (chars_per_line
== 0)
1495 wrap_buffer
= (char *) xmalloc (chars_per_line
+ 2);
1496 wrap_buffer
[0] = '\0';
1499 wrap_buffer
= (char *) xrealloc (wrap_buffer
, chars_per_line
+ 2);
1500 wrap_pointer
= wrap_buffer
; /* Start it at the beginning */
1505 set_width_command (args
, from_tty
, c
)
1508 struct cmd_list_element
*c
;
1513 /* Wait, so the user can read what's on the screen. Prompt the user
1514 to continue by pressing RETURN. */
1517 prompt_for_continue ()
1520 char cont_prompt
[120];
1522 if (annotation_level
> 1)
1523 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1525 strcpy (cont_prompt
,
1526 "---Type <return> to continue, or q <return> to quit---");
1527 if (annotation_level
> 1)
1528 strcat (cont_prompt
, "\n\032\032prompt-for-continue\n");
1530 /* We must do this *before* we call gdb_readline, else it will eventually
1531 call us -- thinking that we're trying to print beyond the end of the
1533 reinitialize_more_filter ();
1536 /* On a real operating system, the user can quit with SIGINT.
1539 'q' is provided on all systems so users don't have to change habits
1540 from system to system, and because telling them what to do in
1541 the prompt is more user-friendly than expecting them to think of
1543 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1544 whereas control-C to gdb_readline will cause the user to get dumped
1546 ignore
= readline (cont_prompt
);
1548 if (annotation_level
> 1)
1549 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1554 while (*p
== ' ' || *p
== '\t')
1559 request_quit (SIGINT
);
1561 async_request_quit (0);
1567 /* Now we have to do this again, so that GDB will know that it doesn't
1568 need to save the ---Type <return>--- line at the top of the screen. */
1569 reinitialize_more_filter ();
1571 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1574 /* Reinitialize filter; ie. tell it to reset to original values. */
1577 reinitialize_more_filter ()
1583 /* Indicate that if the next sequence of characters overflows the line,
1584 a newline should be inserted here rather than when it hits the end.
1585 If INDENT is non-null, it is a string to be printed to indent the
1586 wrapped part on the next line. INDENT must remain accessible until
1587 the next call to wrap_here() or until a newline is printed through
1590 If the line is already overfull, we immediately print a newline and
1591 the indentation, and disable further wrapping.
1593 If we don't know the width of lines, but we know the page height,
1594 we must not wrap words, but should still keep track of newlines
1595 that were explicitly printed.
1597 INDENT should not contain tabs, as that will mess up the char count
1598 on the next line. FIXME.
1600 This routine is guaranteed to force out any output which has been
1601 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1602 used to force out output from the wrap_buffer. */
1608 /* This should have been allocated, but be paranoid anyway. */
1614 *wrap_pointer
= '\0';
1615 fputs_unfiltered (wrap_buffer
, gdb_stdout
);
1617 wrap_pointer
= wrap_buffer
;
1618 wrap_buffer
[0] = '\0';
1619 if (chars_per_line
== UINT_MAX
) /* No line overflow checking */
1623 else if (chars_printed
>= chars_per_line
)
1625 puts_filtered ("\n");
1627 puts_filtered (indent
);
1632 wrap_column
= chars_printed
;
1636 wrap_indent
= indent
;
1640 /* Ensure that whatever gets printed next, using the filtered output
1641 commands, starts at the beginning of the line. I.E. if there is
1642 any pending output for the current line, flush it and start a new
1643 line. Otherwise do nothing. */
1648 if (chars_printed
> 0)
1650 puts_filtered ("\n");
1655 /* ``struct gdb_file'' implementation that maps directly onto
1656 <stdio.h>'s FILE. */
1658 static gdb_file_write_ftype stdio_file_write
;
1659 static gdb_file_fputs_ftype stdio_file_fputs
;
1660 static gdb_file_isatty_ftype stdio_file_isatty
;
1661 static gdb_file_delete_ftype stdio_file_delete
;
1662 static struct gdb_file
*stdio_file_new
PARAMS ((FILE * file
, int close_p
));
1663 static gdb_file_flush_ftype stdio_file_flush
;
1665 static int stdio_file_magic
;
1674 static struct gdb_file
*
1675 stdio_file_new (file
, close_p
)
1679 struct gdb_file
*gdb_file
= gdb_file_new ();
1680 struct stdio_file
*stdio
= xmalloc (sizeof (struct stdio_file
));
1681 stdio
->magic
= &stdio_file_magic
;
1683 stdio
->close_p
= close_p
;
1684 set_gdb_file_data (gdb_file
, stdio
, stdio_file_delete
);
1685 set_gdb_file_flush (gdb_file
, stdio_file_flush
);
1686 set_gdb_file_write (gdb_file
, stdio_file_write
);
1687 set_gdb_file_fputs (gdb_file
, stdio_file_fputs
);
1688 set_gdb_file_isatty (gdb_file
, stdio_file_isatty
);
1693 stdio_file_delete (file
)
1694 struct gdb_file
*file
;
1696 struct stdio_file
*stdio
= gdb_file_data (file
);
1697 if (stdio
->magic
!= &stdio_file_magic
)
1698 internal_error ("stdio_file_delete: bad magic number");
1701 fclose (stdio
->file
);
1707 stdio_file_flush (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_flush: bad magic number");
1713 fflush (stdio
->file
);
1717 stdio_file_write (struct gdb_file
*file
, const char *buf
, long length_buf
)
1719 struct stdio_file
*stdio
= gdb_file_data (file
);
1720 if (stdio
->magic
!= &stdio_file_magic
)
1721 internal_error ("stdio_file_write: bad magic number");
1722 fwrite (buf
, length_buf
, 1, stdio
->file
);
1726 stdio_file_fputs (linebuffer
, file
)
1727 const char *linebuffer
;
1728 struct gdb_file
*file
;
1730 struct stdio_file
*stdio
= gdb_file_data (file
);
1731 if (stdio
->magic
!= &stdio_file_magic
)
1732 internal_error ("stdio_file_fputs: bad magic number");
1733 fputs (linebuffer
, stdio
->file
);
1737 stdio_file_isatty (file
)
1738 struct gdb_file
*file
;
1740 struct stdio_file
*stdio
= gdb_file_data (file
);
1741 if (stdio
->magic
!= &stdio_file_magic
)
1742 internal_error ("stdio_file_isatty: bad magic number");
1743 return (isatty (fileno (stdio
->file
)));
1746 /* Like fdopen(). Create a gdb_file from a previously opened FILE. */
1749 stdio_fileopen (file
)
1752 return stdio_file_new (file
, 0);
1756 /* A pure memory based ``struct gdb_file'' that can be used an output
1757 collector. It's input is available through gdb_file_put(). */
1767 extern gdb_file_fputs_ftype mem_file_fputs
;
1768 static gdb_file_rewind_ftype mem_file_rewind
;
1769 static gdb_file_put_ftype mem_file_put
;
1770 static gdb_file_delete_ftype mem_file_delete
;
1771 static struct gdb_file
*mem_file_new
PARAMS ((void));
1772 static int mem_file_magic
;
1774 static struct gdb_file
*
1777 struct mem_file
*stream
= XMALLOC (struct mem_file
);
1778 struct gdb_file
*file
= gdb_file_new ();
1779 set_gdb_file_data (file
, stream
, mem_file_delete
);
1780 set_gdb_file_fputs (file
, mem_file_fputs
);
1781 set_gdb_file_rewind (file
, mem_file_rewind
);
1782 set_gdb_file_put (file
, mem_file_put
);
1783 stream
->magic
= &mem_file_magic
;
1784 stream
->buffer
= NULL
;
1785 stream
->sizeof_buffer
= 0;
1790 mem_file_delete (struct gdb_file
*file
)
1792 struct mem_file
*stream
= gdb_file_data (file
);
1793 if (stream
->magic
!= &mem_file_magic
)
1794 internal_error ("mem_file_delete: bad magic number");
1795 if (stream
->buffer
!= NULL
)
1796 free (stream
->buffer
);
1803 return mem_file_new ();
1807 mem_file_rewind (struct gdb_file
*file
)
1809 struct mem_file
*stream
= gdb_file_data (file
);
1810 if (stream
->magic
!= &mem_file_magic
)
1811 internal_error ("mem_file_rewind: bad magic number");
1812 if (stream
->buffer
!= NULL
)
1814 stream
->buffer
[0] = '\0';
1815 stream
->strlen_buffer
= 0;
1820 mem_file_put (struct gdb_file
*file
, struct gdb_file
*dest
)
1822 struct mem_file
*stream
= gdb_file_data (file
);
1823 if (stream
->magic
!= &mem_file_magic
)
1824 internal_error ("mem_file_put: bad magic number");
1825 if (stream
->buffer
!= NULL
)
1826 fputs_unfiltered (stream
->buffer
, dest
);
1830 mem_file_fputs (const char *linebuffer
, struct gdb_file
*file
)
1832 struct mem_file
*stream
= gdb_file_data (file
);
1833 if (stream
->magic
!= &mem_file_magic
)
1834 internal_error ("mem_file_fputs: bad magic number");
1835 if (stream
->buffer
== NULL
)
1837 stream
->strlen_buffer
= strlen (linebuffer
);
1838 stream
->sizeof_buffer
= stream
->strlen_buffer
+ 1;
1839 stream
->buffer
= xmalloc (stream
->sizeof_buffer
);
1840 strcpy (stream
->buffer
, linebuffer
);
1844 int len
= strlen (linebuffer
);
1845 int new_strlen
= stream
->strlen_buffer
+ len
;
1846 int new_sizeof
= new_strlen
+ 1;
1847 if (new_sizeof
>= stream
->sizeof_buffer
)
1849 stream
->sizeof_buffer
= new_sizeof
;
1850 stream
->buffer
= xrealloc (stream
->buffer
, stream
->sizeof_buffer
);
1852 strcpy (stream
->buffer
+ stream
->strlen_buffer
, linebuffer
);
1853 stream
->strlen_buffer
= new_strlen
;
1858 /* A ``struct gdb_file'' that is compatible with all the legacy
1872 enum streamtype ts_streamtype
;
1873 FILE *ts_filestream
;
1878 static gdb_file_flush_ftype tui_file_flush
;
1879 extern gdb_file_fputs_ftype tui_file_fputs
;
1880 static gdb_file_isatty_ftype tui_file_isatty
;
1881 static gdb_file_rewind_ftype tui_file_rewind
;
1882 static gdb_file_put_ftype tui_file_put
;
1883 static gdb_file_delete_ftype tui_file_delete
;
1884 static struct gdb_file
*tui_file_new
PARAMS ((void));
1885 static int tui_file_magic
;
1887 static struct gdb_file
*
1890 struct tui_stream
*tui
= xmalloc (sizeof (struct tui_stream
));
1891 struct gdb_file
*file
= gdb_file_new ();
1892 set_gdb_file_data (file
, tui
, tui_file_delete
);
1893 set_gdb_file_flush (file
, tui_file_flush
);
1894 set_gdb_file_fputs (file
, tui_file_fputs
);
1895 set_gdb_file_isatty (file
, tui_file_isatty
);
1896 set_gdb_file_rewind (file
, tui_file_rewind
);
1897 set_gdb_file_put (file
, tui_file_put
);
1898 tui
->ts_magic
= &tui_file_magic
;
1903 tui_file_delete (file
)
1904 struct gdb_file
*file
;
1906 struct tui_stream
*tmpstream
= gdb_file_data (file
);
1907 if (tmpstream
->ts_magic
!= &tui_file_magic
)
1908 internal_error ("tui_file_delete: bad magic number");
1909 if ((tmpstream
->ts_streamtype
== astring
) &&
1910 (tmpstream
->ts_strbuf
!= NULL
))
1912 free (tmpstream
->ts_strbuf
);
1918 tui_fileopen (stream
)
1921 struct gdb_file
*file
= tui_file_new ();
1922 struct tui_stream
*tmpstream
= gdb_file_data (file
);
1923 tmpstream
->ts_streamtype
= afile
;
1924 tmpstream
->ts_filestream
= stream
;
1925 tmpstream
->ts_strbuf
= NULL
;
1926 tmpstream
->ts_buflen
= 0;
1934 struct gdb_file
*file
= tui_file_new ();
1935 struct tui_stream
*tmpstream
= gdb_file_data (file
);
1936 tmpstream
->ts_streamtype
= astring
;
1937 tmpstream
->ts_filestream
= NULL
;
1940 tmpstream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
1941 tmpstream
->ts_strbuf
[0] = '\0';
1944 /* Do not allocate the buffer now. The first time something is printed
1945 one will be allocated by tui_file_adjust_strbuf() */
1946 tmpstream
->ts_strbuf
= NULL
;
1947 tmpstream
->ts_buflen
= n
;
1952 tui_file_isatty (file
)
1953 struct gdb_file
*file
;
1955 struct tui_stream
*stream
= gdb_file_data (file
);
1956 if (stream
->ts_magic
!= &tui_file_magic
)
1957 internal_error ("tui_file_isatty: bad magic number");
1958 if (stream
->ts_streamtype
== afile
)
1959 return (isatty (fileno (stream
->ts_filestream
)));
1965 tui_file_rewind (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_rewind: bad magic number");
1971 stream
->ts_strbuf
[0] = '\0';
1975 tui_file_put (file
, dest
)
1976 struct gdb_file
*file
;
1977 struct gdb_file
*dest
;
1979 struct tui_stream
*stream
= gdb_file_data (file
);
1980 if (stream
->ts_magic
!= &tui_file_magic
)
1981 internal_error ("tui_file_put: bad magic number");
1982 if (stream
->ts_streamtype
== astring
)
1984 fputs_unfiltered (stream
->ts_strbuf
, dest
);
1988 /* All TUI I/O sent to the *_filtered and *_unfiltered functions
1989 eventually ends up here. The fputs_unfiltered_hook is primarily
1990 used by GUIs to collect all output and send it to the GUI, instead
1991 of the controlling terminal. Only output to gdb_stdout and
1992 gdb_stderr are sent to the hook. Everything else is sent on to
1993 fputs to allow file I/O to be handled appropriately. */
1995 /* FIXME: Should be broken up and moved to a TUI specific file. */
1998 tui_file_fputs (linebuffer
, file
)
1999 const char *linebuffer
;
2002 struct tui_stream
*stream
= gdb_file_data (file
);
2004 extern int tui_owns_terminal
;
2006 /* NOTE: cagney/1999-10-13: The use of fputs_unfiltered_hook is
2007 seriously discouraged. Those wanting to hook output should
2008 instead implement their own gdb_file object and install that. See
2009 also tui_file_flush(). */
2010 if (fputs_unfiltered_hook
2011 && (file
== gdb_stdout
2012 || file
== gdb_stderr
))
2013 fputs_unfiltered_hook (linebuffer
, file
);
2017 if (tui_version
&& tui_owns_terminal
)
2019 /* If we get here somehow while updating the TUI (from
2020 * within a tuiDo(), then we need to temporarily
2021 * set up the terminal for GDB output. This probably just
2022 * happens on error output.
2025 if (stream
->ts_streamtype
== astring
)
2027 tui_file_adjust_strbuf (strlen (linebuffer
), stream
);
2028 strcat (stream
->ts_strbuf
, linebuffer
);
2032 tuiTermUnsetup (0, (tui_version
) ? cmdWin
->detail
.commandInfo
.curch
: 0);
2033 fputs (linebuffer
, stream
->ts_filestream
);
2035 if (linebuffer
[strlen (linebuffer
) - 1] == '\n')
2036 tuiClearCommandCharCount ();
2038 tuiIncrCommandCharCountBy (strlen (linebuffer
));
2043 /* The normal case - just do a fputs() */
2044 if (stream
->ts_streamtype
== astring
)
2046 tui_file_adjust_strbuf (strlen (linebuffer
), stream
);
2047 strcat (stream
->ts_strbuf
, linebuffer
);
2050 fputs (linebuffer
, stream
->ts_filestream
);
2055 if (stream
->ts_streamtype
== astring
)
2057 tui_file_adjust_strbuf (strlen (linebuffer
), file
);
2058 strcat (stream
->ts_strbuf
, linebuffer
);
2061 fputs (linebuffer
, stream
->ts_filestream
);
2067 tui_file_get_strbuf (struct gdb_file
*file
)
2069 struct tui_stream
*stream
= gdb_file_data (file
);
2070 if (stream
->ts_magic
!= &tui_file_magic
)
2071 internal_error ("tui_file_get_strbuf: bad magic number");
2072 return (stream
->ts_strbuf
);
2075 /* adjust the length of the buffer by the amount necessary
2076 to accomodate appending a string of length N to the buffer contents */
2078 tui_file_adjust_strbuf (int n
, struct gdb_file
*file
)
2080 struct tui_stream
*stream
= gdb_file_data (file
);
2082 if (stream
->ts_magic
!= &tui_file_magic
)
2083 internal_error ("tui_file_adjust_strbuf: bad magic number");
2085 if (stream
->ts_streamtype
!= astring
)
2088 if (stream
->ts_strbuf
)
2090 /* There is already a buffer allocated */
2091 non_null_chars
= strlen (stream
->ts_strbuf
);
2093 if (n
> (stream
->ts_buflen
- non_null_chars
- 1))
2095 stream
->ts_buflen
= n
+ non_null_chars
+ 1;
2096 stream
->ts_strbuf
= xrealloc (stream
->ts_strbuf
, stream
->ts_buflen
);
2100 /* No buffer yet, so allocate one of the desired size */
2101 stream
->ts_strbuf
= xmalloc ((n
+ 1) * sizeof (char));
2105 gdb_fopen (name
, mode
)
2109 FILE *f
= fopen (name
, mode
);
2112 return stdio_file_new (f
, 1);
2116 tui_file_flush (file
)
2119 struct tui_stream
*stream
= gdb_file_data (file
);
2120 if (stream
->ts_magic
!= &tui_file_magic
)
2121 internal_error ("tui_file_flush: bad magic number");
2123 /* NOTE: cagney/1999-10-12: If we've been linked with code that uses
2124 fputs_unfiltered_hook then we assume that it doesn't need to know
2125 about flushes. Code that does need to know about flushes can
2126 implement a proper gdb_file object. */
2127 if (fputs_unfiltered_hook
)
2130 switch (stream
->ts_streamtype
)
2135 fflush (stream
->ts_filestream
);
2140 /* Implement the ``struct gdb_file'' object. */
2142 static gdb_file_isatty_ftype null_file_isatty
;
2143 static gdb_file_write_ftype null_file_write
;
2144 static gdb_file_fputs_ftype null_file_fputs
;
2145 static gdb_file_flush_ftype null_file_flush
;
2146 static gdb_file_delete_ftype null_file_delete
;
2147 static gdb_file_rewind_ftype null_file_rewind
;
2148 static gdb_file_put_ftype null_file_put
;
2152 gdb_file_flush_ftype
*to_flush
;
2153 gdb_file_write_ftype
*to_write
;
2154 gdb_file_fputs_ftype
*to_fputs
;
2155 gdb_file_delete_ftype
*to_delete
;
2156 gdb_file_isatty_ftype
*to_isatty
;
2157 gdb_file_rewind_ftype
*to_rewind
;
2158 gdb_file_put_ftype
*to_put
;
2165 struct gdb_file
*file
= xmalloc (sizeof (struct gdb_file
));
2166 set_gdb_file_data (file
, NULL
, null_file_delete
);
2167 set_gdb_file_flush (file
, null_file_flush
);
2168 set_gdb_file_write (file
, null_file_write
);
2169 set_gdb_file_fputs (file
, null_file_fputs
);
2170 set_gdb_file_isatty (file
, null_file_isatty
);
2171 set_gdb_file_rewind (file
, null_file_rewind
);
2172 set_gdb_file_put (file
, null_file_put
);
2177 gdb_file_delete (file
)
2178 struct gdb_file
*file
;
2180 file
->to_delete (file
);
2185 null_file_isatty (file
)
2186 struct gdb_file
*file
;
2192 null_file_rewind (file
)
2193 struct gdb_file
*file
;
2199 null_file_put (file
, src
)
2200 struct gdb_file
*file
;
2201 struct gdb_file
*src
;
2207 null_file_flush (file
)
2208 struct gdb_file
*file
;
2214 null_file_write (struct gdb_file
*file
,
2218 if (file
->to_fputs
== null_file_fputs
)
2219 /* Both the write and fputs methods are null. Discard the
2224 /* The fputs method isn't null, slowly pass the write request
2225 onto that. FYI, this isn't as bad as it may look - the
2226 current (as of 1999-11-07) printf_* function calls fputc and
2227 fputc does exactly the below. By having a write function it
2228 is possible to clean up that code. */
2232 for (i
= 0; i
< sizeof_buf
; i
++)
2235 file
->to_fputs (b
, file
);
2242 null_file_fputs (buf
, file
)
2244 struct gdb_file
*file
;
2246 if (file
->to_write
== null_file_write
)
2247 /* Both the write and fputs methods are null. Discard the
2252 /* The write method was implemented, use that. */
2253 file
->to_write (file
, buf
, strlen (buf
));
2258 null_file_delete (file
)
2259 struct gdb_file
*file
;
2265 gdb_file_data (file
)
2266 struct gdb_file
*file
;
2268 return file
->to_data
;
2273 struct gdb_file
*file
;
2275 file
->to_flush (file
);
2279 gdb_file_isatty (file
)
2280 struct gdb_file
*file
;
2282 return file
->to_isatty (file
);
2286 gdb_file_rewind (file
)
2287 struct gdb_file
*file
;
2289 file
->to_rewind (file
);
2293 gdb_file_put (file
, dest
)
2294 struct gdb_file
*file
;
2295 struct gdb_file
*dest
;
2297 file
->to_put (file
, dest
);
2301 gdb_file_write (struct gdb_file
*file
,
2305 file
->to_write (file
, buf
, length_buf
);
2309 fputs_unfiltered (buf
, file
)
2311 struct gdb_file
*file
;
2313 file
->to_fputs (buf
, file
);
2317 set_gdb_file_flush (file
, flush
)
2318 struct gdb_file
*file
;
2319 gdb_file_flush_ftype
*flush
;
2321 file
->to_flush
= flush
;
2325 set_gdb_file_isatty (file
, isatty
)
2326 struct gdb_file
*file
;
2327 gdb_file_isatty_ftype
*isatty
;
2329 file
->to_isatty
= isatty
;
2333 set_gdb_file_rewind (file
, rewind
)
2334 struct gdb_file
*file
;
2335 gdb_file_rewind_ftype
*rewind
;
2337 file
->to_rewind
= rewind
;
2341 set_gdb_file_put (file
, put
)
2342 struct gdb_file
*file
;
2343 gdb_file_put_ftype
*put
;
2349 set_gdb_file_write (struct gdb_file
*file
,
2350 gdb_file_write_ftype
*write
)
2352 file
->to_write
= write
;
2356 set_gdb_file_fputs (file
, fputs
)
2357 struct gdb_file
*file
;
2358 gdb_file_fputs_ftype
*fputs
;
2360 file
->to_fputs
= fputs
;
2364 set_gdb_file_data (file
, data
, delete)
2365 struct gdb_file
*file
;
2367 gdb_file_delete_ftype
*delete;
2369 file
->to_data
= data
;
2370 file
->to_delete
= delete;
2373 /* Like fputs but if FILTER is true, pause after every screenful.
2375 Regardless of FILTER can wrap at points other than the final
2376 character of a line.
2378 Unlike fputs, fputs_maybe_filtered does not return a value.
2379 It is OK for LINEBUFFER to be NULL, in which case just don't print
2382 Note that a longjmp to top level may occur in this routine (only if
2383 FILTER is true) (since prompt_for_continue may do so) so this
2384 routine should not be called when cleanups are not in place. */
2387 fputs_maybe_filtered (linebuffer
, stream
, filter
)
2388 const char *linebuffer
;
2392 const char *lineptr
;
2394 if (linebuffer
== 0)
2397 /* Don't do any filtering if it is disabled. */
2398 if ((stream
!= gdb_stdout
) || !pagination_enabled
2399 || (lines_per_page
== UINT_MAX
&& chars_per_line
== UINT_MAX
))
2401 fputs_unfiltered (linebuffer
, stream
);
2405 /* Go through and output each character. Show line extension
2406 when this is necessary; prompt user for new page when this is
2409 lineptr
= linebuffer
;
2412 /* Possible new page. */
2414 (lines_printed
>= lines_per_page
- 1))
2415 prompt_for_continue ();
2417 while (*lineptr
&& *lineptr
!= '\n')
2419 /* Print a single line. */
2420 if (*lineptr
== '\t')
2423 *wrap_pointer
++ = '\t';
2425 fputc_unfiltered ('\t', stream
);
2426 /* Shifting right by 3 produces the number of tab stops
2427 we have already passed, and then adding one and
2428 shifting left 3 advances to the next tab stop. */
2429 chars_printed
= ((chars_printed
>> 3) + 1) << 3;
2435 *wrap_pointer
++ = *lineptr
;
2437 fputc_unfiltered (*lineptr
, stream
);
2442 if (chars_printed
>= chars_per_line
)
2444 unsigned int save_chars
= chars_printed
;
2448 /* If we aren't actually wrapping, don't output newline --
2449 if chars_per_line is right, we probably just overflowed
2450 anyway; if it's wrong, let us keep going. */
2452 fputc_unfiltered ('\n', stream
);
2454 /* Possible new page. */
2455 if (lines_printed
>= lines_per_page
- 1)
2456 prompt_for_continue ();
2458 /* Now output indentation and wrapped string */
2461 fputs_unfiltered (wrap_indent
, stream
);
2462 *wrap_pointer
= '\0'; /* Null-terminate saved stuff */
2463 fputs_unfiltered (wrap_buffer
, stream
); /* and eject it */
2464 /* FIXME, this strlen is what prevents wrap_indent from
2465 containing tabs. However, if we recurse to print it
2466 and count its chars, we risk trouble if wrap_indent is
2467 longer than (the user settable) chars_per_line.
2468 Note also that this can set chars_printed > chars_per_line
2469 if we are printing a long string. */
2470 chars_printed
= strlen (wrap_indent
)
2471 + (save_chars
- wrap_column
);
2472 wrap_pointer
= wrap_buffer
; /* Reset buffer */
2473 wrap_buffer
[0] = '\0';
2474 wrap_column
= 0; /* And disable fancy wrap */
2479 if (*lineptr
== '\n')
2482 wrap_here ((char *) 0); /* Spit out chars, cancel further wraps */
2484 fputc_unfiltered ('\n', stream
);
2491 fputs_filtered (linebuffer
, stream
)
2492 const char *linebuffer
;
2495 fputs_maybe_filtered (linebuffer
, stream
, 1);
2499 putchar_unfiltered (c
)
2503 gdb_file_write (gdb_stdout
, &buf
, 1);
2508 fputc_unfiltered (c
, stream
)
2513 gdb_file_write (stream
, &buf
, 1);
2518 fputc_filtered (c
, stream
)
2526 fputs_filtered (buf
, stream
);
2530 /* puts_debug is like fputs_unfiltered, except it prints special
2531 characters in printable fashion. */
2534 puts_debug (prefix
, string
, suffix
)
2541 /* Print prefix and suffix after each line. */
2542 static int new_line
= 1;
2543 static int return_p
= 0;
2544 static char *prev_prefix
= "";
2545 static char *prev_suffix
= "";
2547 if (*string
== '\n')
2550 /* If the prefix is changing, print the previous suffix, a new line,
2551 and the new prefix. */
2552 if ((return_p
|| (strcmp (prev_prefix
, prefix
) != 0)) && !new_line
)
2554 fputs_unfiltered (prev_suffix
, gdb_stdlog
);
2555 fputs_unfiltered ("\n", gdb_stdlog
);
2556 fputs_unfiltered (prefix
, gdb_stdlog
);
2559 /* Print prefix if we printed a newline during the previous call. */
2563 fputs_unfiltered (prefix
, gdb_stdlog
);
2566 prev_prefix
= prefix
;
2567 prev_suffix
= suffix
;
2569 /* Output characters in a printable format. */
2570 while ((ch
= *string
++) != '\0')
2576 fputc_unfiltered (ch
, gdb_stdlog
);
2579 fprintf_unfiltered (gdb_stdlog
, "\\x%02x", ch
& 0xff);
2583 fputs_unfiltered ("\\\\", gdb_stdlog
);
2586 fputs_unfiltered ("\\b", gdb_stdlog
);
2589 fputs_unfiltered ("\\f", gdb_stdlog
);
2593 fputs_unfiltered ("\\n", gdb_stdlog
);
2596 fputs_unfiltered ("\\r", gdb_stdlog
);
2599 fputs_unfiltered ("\\t", gdb_stdlog
);
2602 fputs_unfiltered ("\\v", gdb_stdlog
);
2606 return_p
= ch
== '\r';
2609 /* Print suffix if we printed a newline. */
2612 fputs_unfiltered (suffix
, gdb_stdlog
);
2613 fputs_unfiltered ("\n", gdb_stdlog
);
2618 /* Print a variable number of ARGS using format FORMAT. If this
2619 information is going to put the amount written (since the last call
2620 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
2621 call prompt_for_continue to get the users permision to continue.
2623 Unlike fprintf, this function does not return a value.
2625 We implement three variants, vfprintf (takes a vararg list and stream),
2626 fprintf (takes a stream to write on), and printf (the usual).
2628 Note also that a longjmp to top level may occur in this routine
2629 (since prompt_for_continue may do so) so this routine should not be
2630 called when cleanups are not in place. */
2633 vfprintf_maybe_filtered (stream
, format
, args
, filter
)
2640 struct cleanup
*old_cleanups
;
2642 vasprintf (&linebuffer
, format
, args
);
2643 if (linebuffer
== NULL
)
2645 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
2648 old_cleanups
= make_cleanup (free
, linebuffer
);
2649 fputs_maybe_filtered (linebuffer
, stream
, filter
);
2650 do_cleanups (old_cleanups
);
2655 vfprintf_filtered (stream
, format
, args
)
2660 vfprintf_maybe_filtered (stream
, format
, args
, 1);
2664 vfprintf_unfiltered (stream
, format
, args
)
2670 struct cleanup
*old_cleanups
;
2672 vasprintf (&linebuffer
, format
, args
);
2673 if (linebuffer
== NULL
)
2675 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr
);
2678 old_cleanups
= make_cleanup (free
, linebuffer
);
2679 fputs_unfiltered (linebuffer
, stream
);
2680 do_cleanups (old_cleanups
);
2684 vprintf_filtered (format
, args
)
2688 vfprintf_maybe_filtered (gdb_stdout
, format
, args
, 1);
2692 vprintf_unfiltered (format
, args
)
2696 vfprintf_unfiltered (gdb_stdout
, format
, args
);
2700 fprintf_filtered (GDB_FILE
* stream
, const char *format
,...)
2703 va_start (args
, format
);
2704 vfprintf_filtered (stream
, format
, args
);
2709 fprintf_unfiltered (GDB_FILE
* stream
, const char *format
,...)
2712 va_start (args
, format
);
2713 vfprintf_unfiltered (stream
, format
, args
);
2717 /* Like fprintf_filtered, but prints its result indented.
2718 Called as fprintfi_filtered (spaces, stream, format, ...); */
2721 fprintfi_filtered (int spaces
, GDB_FILE
* stream
, const char *format
,...)
2724 va_start (args
, format
);
2725 print_spaces_filtered (spaces
, stream
);
2727 vfprintf_filtered (stream
, format
, args
);
2733 printf_filtered (const char *format
,...)
2736 va_start (args
, format
);
2737 vfprintf_filtered (gdb_stdout
, format
, args
);
2743 printf_unfiltered (const char *format
,...)
2746 va_start (args
, format
);
2747 vfprintf_unfiltered (gdb_stdout
, format
, args
);
2751 /* Like printf_filtered, but prints it's result indented.
2752 Called as printfi_filtered (spaces, format, ...); */
2755 printfi_filtered (int spaces
, const char *format
,...)
2758 va_start (args
, format
);
2759 print_spaces_filtered (spaces
, gdb_stdout
);
2760 vfprintf_filtered (gdb_stdout
, format
, args
);
2764 /* Easy -- but watch out!
2766 This routine is *not* a replacement for puts()! puts() appends a newline.
2767 This one doesn't, and had better not! */
2770 puts_filtered (string
)
2773 fputs_filtered (string
, gdb_stdout
);
2777 puts_unfiltered (string
)
2780 fputs_unfiltered (string
, gdb_stdout
);
2783 /* Return a pointer to N spaces and a null. The pointer is good
2784 until the next call to here. */
2790 static char *spaces
= 0;
2791 static int max_spaces
= -1;
2797 spaces
= (char *) xmalloc (n
+ 1);
2798 for (t
= spaces
+ n
; t
!= spaces
;)
2804 return spaces
+ max_spaces
- n
;
2807 /* Print N spaces. */
2809 print_spaces_filtered (n
, stream
)
2813 fputs_filtered (n_spaces (n
), stream
);
2816 /* C++ demangler stuff. */
2818 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
2819 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
2820 If the name is not mangled, or the language for the name is unknown, or
2821 demangling is off, the name is printed in its "raw" form. */
2824 fprintf_symbol_filtered (stream
, name
, lang
, arg_mode
)
2834 /* If user wants to see raw output, no problem. */
2837 fputs_filtered (name
, stream
);
2843 case language_cplus
:
2844 demangled
= cplus_demangle (name
, arg_mode
);
2847 demangled
= cplus_demangle (name
, arg_mode
| DMGL_JAVA
);
2849 case language_chill
:
2850 demangled
= chill_demangle (name
);
2856 fputs_filtered (demangled
? demangled
: name
, stream
);
2857 if (demangled
!= NULL
)
2865 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
2866 differences in whitespace. Returns 0 if they match, non-zero if they
2867 don't (slightly different than strcmp()'s range of return values).
2869 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2870 This "feature" is useful when searching for matching C++ function names
2871 (such as if the user types 'break FOO', where FOO is a mangled C++
2875 strcmp_iw (string1
, string2
)
2876 const char *string1
;
2877 const char *string2
;
2879 while ((*string1
!= '\0') && (*string2
!= '\0'))
2881 while (isspace (*string1
))
2885 while (isspace (*string2
))
2889 if (*string1
!= *string2
)
2893 if (*string1
!= '\0')
2899 return (*string1
!= '\0' && *string1
!= '(') || (*string2
!= '\0');
2905 ** Answer whether string_to_compare is a full or partial match to
2906 ** template_string. The partial match must be in sequence starting
2910 subset_compare (string_to_compare
, template_string
)
2911 char *string_to_compare
;
2912 char *template_string
;
2915 if (template_string
!= (char *) NULL
&& string_to_compare
!= (char *) NULL
&&
2916 strlen (string_to_compare
) <= strlen (template_string
))
2917 match
= (strncmp (template_string
,
2919 strlen (string_to_compare
)) == 0);
2926 static void pagination_on_command
PARAMS ((char *arg
, int from_tty
));
2928 pagination_on_command (arg
, from_tty
)
2932 pagination_enabled
= 1;
2935 static void pagination_on_command
PARAMS ((char *arg
, int from_tty
));
2937 pagination_off_command (arg
, from_tty
)
2941 pagination_enabled
= 0;
2948 struct cmd_list_element
*c
;
2950 c
= add_set_cmd ("width", class_support
, var_uinteger
,
2951 (char *) &chars_per_line
,
2952 "Set number of characters gdb thinks are in a line.",
2954 add_show_from_set (c
, &showlist
);
2955 c
->function
.sfunc
= set_width_command
;
2958 (add_set_cmd ("height", class_support
,
2959 var_uinteger
, (char *) &lines_per_page
,
2960 "Set number of lines gdb thinks are in a page.", &setlist
),
2965 /* If the output is not a terminal, don't paginate it. */
2966 if (!GDB_FILE_ISATTY (gdb_stdout
))
2967 lines_per_page
= UINT_MAX
;
2969 set_width_command ((char *) NULL
, 0, c
);
2972 (add_set_cmd ("demangle", class_support
, var_boolean
,
2974 "Set demangling of encoded C++ names when displaying symbols.",
2979 (add_set_cmd ("pagination", class_support
,
2980 var_boolean
, (char *) &pagination_enabled
,
2981 "Set state of pagination.", &setlist
),
2985 add_com ("am", class_support
, pagination_on_command
,
2986 "Enable pagination");
2987 add_com ("sm", class_support
, pagination_off_command
,
2988 "Disable pagination");
2992 (add_set_cmd ("sevenbit-strings", class_support
, var_boolean
,
2993 (char *) &sevenbit_strings
,
2994 "Set printing of 8-bit characters in strings as \\nnn.",
2999 (add_set_cmd ("asm-demangle", class_support
, var_boolean
,
3000 (char *) &asm_demangle
,
3001 "Set demangling of C++ names in disassembly listings.",
3006 /* Machine specific function to handle SIGWINCH signal. */
3008 #ifdef SIGWINCH_HANDLER_BODY
3009 SIGWINCH_HANDLER_BODY
3012 /* Support for converting target fp numbers into host DOUBLEST format. */
3014 /* XXX - This code should really be in libiberty/floatformat.c, however
3015 configuration issues with libiberty made this very difficult to do in the
3018 #include "floatformat.h"
3019 #include <math.h> /* ldexp */
3021 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
3022 going to bother with trying to muck around with whether it is defined in
3023 a system header, what we do if not, etc. */
3024 #define FLOATFORMAT_CHAR_BIT 8
3026 static unsigned long get_field
PARAMS ((unsigned char *,
3027 enum floatformat_byteorders
,
3032 /* Extract a field which starts at START and is LEN bytes long. DATA and
3033 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
3034 static unsigned long
3035 get_field (data
, order
, total_len
, start
, len
)
3036 unsigned char *data
;
3037 enum floatformat_byteorders order
;
3038 unsigned int total_len
;
3042 unsigned long result
;
3043 unsigned int cur_byte
;
3046 /* Start at the least significant part of the field. */
3047 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
3048 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
3049 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
3051 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
3052 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
3053 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
3054 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
3059 /* Move towards the most significant part of the field. */
3060 while (cur_bitshift
< len
)
3062 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
3063 /* This is the last byte; zero out the bits which are not part of
3066 (*(data
+ cur_byte
) & ((1 << (len
- cur_bitshift
)) - 1))
3069 result
|= *(data
+ cur_byte
) << cur_bitshift
;
3070 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
3071 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
3079 /* Convert from FMT to a DOUBLEST.
3080 FROM is the address of the extended float.
3081 Store the DOUBLEST in *TO. */
3084 floatformat_to_doublest (fmt
, from
, to
)
3085 const struct floatformat
*fmt
;
3089 unsigned char *ufrom
= (unsigned char *) from
;
3093 unsigned int mant_bits
, mant_off
;
3095 int special_exponent
; /* It's a NaN, denorm or zero */
3097 /* If the mantissa bits are not contiguous from one end of the
3098 mantissa to the other, we need to make a private copy of the
3099 source bytes that is in the right order since the unpacking
3100 algorithm assumes that the bits are contiguous.
3102 Swap the bytes individually rather than accessing them through
3103 "long *" since we have no guarantee that they start on a long
3104 alignment, and also sizeof(long) for the host could be different
3105 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
3106 for the target is 4. */
3108 if (fmt
->byteorder
== floatformat_littlebyte_bigword
)
3110 static unsigned char *newfrom
;
3111 unsigned char *swapin
, *swapout
;
3114 longswaps
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
3117 if (newfrom
== NULL
)
3119 newfrom
= (unsigned char *) xmalloc (fmt
->totalsize
);
3124 while (longswaps
-- > 0)
3126 /* This is ugly, but efficient */
3127 *swapout
++ = swapin
[4];
3128 *swapout
++ = swapin
[5];
3129 *swapout
++ = swapin
[6];
3130 *swapout
++ = swapin
[7];
3131 *swapout
++ = swapin
[0];
3132 *swapout
++ = swapin
[1];
3133 *swapout
++ = swapin
[2];
3134 *swapout
++ = swapin
[3];
3139 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
3140 fmt
->exp_start
, fmt
->exp_len
);
3141 /* Note that if exponent indicates a NaN, we can't really do anything useful
3142 (not knowing if the host has NaN's, or how to build one). So it will
3143 end up as an infinity or something close; that is OK. */
3145 mant_bits_left
= fmt
->man_len
;
3146 mant_off
= fmt
->man_start
;
3149 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
3151 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
3152 we don't check for zero as the exponent doesn't matter. */
3153 if (!special_exponent
)
3154 exponent
-= fmt
->exp_bias
;
3155 else if (exponent
== 0)
3156 exponent
= 1 - fmt
->exp_bias
;
3158 /* Build the result algebraically. Might go infinite, underflow, etc;
3161 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
3162 increment the exponent by one to account for the integer bit. */
3164 if (!special_exponent
)
3166 if (fmt
->intbit
== floatformat_intbit_no
)
3167 dto
= ldexp (1.0, exponent
);
3172 while (mant_bits_left
> 0)
3174 mant_bits
= min (mant_bits_left
, 32);
3176 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
3177 mant_off
, mant_bits
);
3179 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
3180 exponent
-= mant_bits
;
3181 mant_off
+= mant_bits
;
3182 mant_bits_left
-= mant_bits
;
3185 /* Negate it if negative. */
3186 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
3191 static void put_field
PARAMS ((unsigned char *, enum floatformat_byteorders
,
3197 /* Set a field which starts at START and is LEN bytes long. DATA and
3198 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
3200 put_field (data
, order
, total_len
, start
, len
, stuff_to_put
)
3201 unsigned char *data
;
3202 enum floatformat_byteorders order
;
3203 unsigned int total_len
;
3206 unsigned long stuff_to_put
;
3208 unsigned int cur_byte
;
3211 /* Start at the least significant part of the field. */
3212 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
3213 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
3214 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
3216 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
3217 *(data
+ cur_byte
) &=
3218 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1) << (-cur_bitshift
));
3219 *(data
+ cur_byte
) |=
3220 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
3221 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
3222 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
3227 /* Move towards the most significant part of the field. */
3228 while (cur_bitshift
< len
)
3230 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
3232 /* This is the last byte. */
3233 *(data
+ cur_byte
) &=
3234 ~((1 << (len
- cur_bitshift
)) - 1);
3235 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
3238 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
3239 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
3240 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
3241 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
3248 #ifdef HAVE_LONG_DOUBLE
3249 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
3250 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
3251 frexp, but operates on the long double data type. */
3253 static long double ldfrexp
PARAMS ((long double value
, int *eptr
));
3256 ldfrexp (value
, eptr
)
3263 /* Unfortunately, there are no portable functions for extracting the exponent
3264 of a long double, so we have to do it iteratively by multiplying or dividing
3265 by two until the fraction is between 0.5 and 1.0. */
3273 if (value
>= tmp
) /* Value >= 1.0 */
3274 while (value
>= tmp
)
3279 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
3293 #endif /* HAVE_LONG_DOUBLE */
3296 /* The converse: convert the DOUBLEST *FROM to an extended float
3297 and store where TO points. Neither FROM nor TO have any alignment
3301 floatformat_from_doublest (fmt
, from
, to
)
3302 CONST
struct floatformat
*fmt
;
3309 unsigned int mant_bits
, mant_off
;
3311 unsigned char *uto
= (unsigned char *) to
;
3313 memcpy (&dfrom
, from
, sizeof (dfrom
));
3314 memset (uto
, 0, fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
);
3316 return; /* Result is zero */
3317 if (dfrom
!= dfrom
) /* Result is NaN */
3320 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
3321 fmt
->exp_len
, fmt
->exp_nan
);
3322 /* Be sure it's not infinity, but NaN value is irrel */
3323 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
3328 /* If negative, set the sign bit. */
3331 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
3335 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
3337 /* Infinity exponent is same as NaN's. */
3338 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
3339 fmt
->exp_len
, fmt
->exp_nan
);
3340 /* Infinity mantissa is all zeroes. */
3341 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
3346 #ifdef HAVE_LONG_DOUBLE
3347 mant
= ldfrexp (dfrom
, &exponent
);
3349 mant
= frexp (dfrom
, &exponent
);
3352 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
3353 exponent
+ fmt
->exp_bias
- 1);
3355 mant_bits_left
= fmt
->man_len
;
3356 mant_off
= fmt
->man_start
;
3357 while (mant_bits_left
> 0)
3359 unsigned long mant_long
;
3360 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
3362 mant
*= 4294967296.0;
3363 mant_long
= (unsigned long) mant
;
3366 /* If the integer bit is implicit, then we need to discard it.
3367 If we are discarding a zero, we should be (but are not) creating
3368 a denormalized number which means adjusting the exponent
3370 if (mant_bits_left
== fmt
->man_len
3371 && fmt
->intbit
== floatformat_intbit_no
)
3379 /* The bits we want are in the most significant MANT_BITS bits of
3380 mant_long. Move them to the least significant. */
3381 mant_long
>>= 32 - mant_bits
;
3384 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
3385 mant_off
, mant_bits
, mant_long
);
3386 mant_off
+= mant_bits
;
3387 mant_bits_left
-= mant_bits
;
3389 if (fmt
->byteorder
== floatformat_littlebyte_bigword
)
3392 unsigned char *swaplow
= uto
;
3393 unsigned char *swaphigh
= uto
+ 4;
3396 for (count
= 0; count
< 4; count
++)
3399 *swaplow
++ = *swaphigh
;
3405 /* temporary storage using circular buffer */
3411 static char buf
[NUMCELLS
][CELLSIZE
];
3412 static int cell
= 0;
3413 if (++cell
>= NUMCELLS
)
3418 /* print routines to handle variable size regs, etc.
3420 FIXME: Note that t_addr is a bfd_vma, which is currently either an
3421 unsigned long or unsigned long long, determined at configure time.
3422 If t_addr is an unsigned long long and sizeof (unsigned long long)
3423 is greater than sizeof (unsigned long), then I believe this code will
3424 probably lose, at least for little endian machines. I believe that
3425 it would also be better to eliminate the switch on the absolute size
3426 of t_addr and replace it with a sequence of if statements that compare
3427 sizeof t_addr with sizeof the various types and do the right thing,
3428 which includes knowing whether or not the host supports long long.
3436 return (TARGET_PTR_BIT
/ 8 * 2);
3440 /* eliminate warning from compiler on 32-bit systems */
3441 static int thirty_two
= 32;
3444 paddr (CORE_ADDR addr
)
3446 char *paddr_str
= get_cell ();
3447 switch (TARGET_PTR_BIT
/ 8)
3450 sprintf (paddr_str
, "%08lx%08lx",
3451 (unsigned long) (addr
>> thirty_two
), (unsigned long) (addr
& 0xffffffff));
3454 sprintf (paddr_str
, "%08lx", (unsigned long) addr
);
3457 sprintf (paddr_str
, "%04x", (unsigned short) (addr
& 0xffff));
3460 sprintf (paddr_str
, "%lx", (unsigned long) addr
);
3466 paddr_nz (CORE_ADDR addr
)
3468 char *paddr_str
= get_cell ();
3469 switch (TARGET_PTR_BIT
/ 8)
3473 unsigned long high
= (unsigned long) (addr
>> thirty_two
);
3475 sprintf (paddr_str
, "%lx", (unsigned long) (addr
& 0xffffffff));
3477 sprintf (paddr_str
, "%lx%08lx",
3478 high
, (unsigned long) (addr
& 0xffffffff));
3482 sprintf (paddr_str
, "%lx", (unsigned long) addr
);
3485 sprintf (paddr_str
, "%x", (unsigned short) (addr
& 0xffff));
3488 sprintf (paddr_str
, "%lx", (unsigned long) addr
);
3494 decimal2str (char *paddr_str
, char *sign
, ULONGEST addr
)
3496 /* steal code from valprint.c:print_decimal(). Should this worry
3497 about the real size of addr as the above does? */
3498 unsigned long temp
[3];
3502 temp
[i
] = addr
% (1000 * 1000 * 1000);
3503 addr
/= (1000 * 1000 * 1000);
3506 while (addr
!= 0 && i
< (sizeof (temp
) / sizeof (temp
[0])));
3510 sprintf (paddr_str
, "%s%lu",
3514 sprintf (paddr_str
, "%s%lu%09lu",
3515 sign
, temp
[1], temp
[0]);
3518 sprintf (paddr_str
, "%s%lu%09lu%09lu",
3519 sign
, temp
[2], temp
[1], temp
[0]);
3527 paddr_u (CORE_ADDR addr
)
3529 char *paddr_str
= get_cell ();
3530 decimal2str (paddr_str
, "", addr
);
3535 paddr_d (LONGEST addr
)
3537 char *paddr_str
= get_cell ();
3539 decimal2str (paddr_str
, "-", -addr
);
3541 decimal2str (paddr_str
, "", addr
);
3549 char *preg_str
= get_cell ();
3550 switch (sizeof (t_reg
))
3553 sprintf (preg_str
, "%08lx%08lx",
3554 (unsigned long) (reg
>> thirty_two
), (unsigned long) (reg
& 0xffffffff));
3557 sprintf (preg_str
, "%08lx", (unsigned long) reg
);
3560 sprintf (preg_str
, "%04x", (unsigned short) (reg
& 0xffff));
3563 sprintf (preg_str
, "%lx", (unsigned long) reg
);
3572 char *preg_str
= get_cell ();
3573 switch (sizeof (t_reg
))
3577 unsigned long high
= (unsigned long) (reg
>> thirty_two
);
3579 sprintf (preg_str
, "%lx", (unsigned long) (reg
& 0xffffffff));
3581 sprintf (preg_str
, "%lx%08lx",
3582 high
, (unsigned long) (reg
& 0xffffffff));
3586 sprintf (preg_str
, "%lx", (unsigned long) reg
);
3589 sprintf (preg_str
, "%x", (unsigned short) (reg
& 0xffff));
3592 sprintf (preg_str
, "%lx", (unsigned long) reg
);
3597 /* Helper functions for INNER_THAN */
3599 core_addr_lessthan (lhs
, rhs
)
3607 core_addr_greaterthan (lhs
, rhs
)