]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/utils.c
* config/tc-i386.c (struct _i386_insn): Rename disp_reloc to reloc.
[thirdparty/binutils-gdb.git] / gdb / utils.c
CommitLineData
c906108c 1/* General utility routines for GDB, the GNU debugger.
b6ba6518
KB
2 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3 1997, 1998, 1999, 2000, 2001
d9fcf2fb 4 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include <ctype.h>
25#include "gdb_string.h"
c2c6d25f 26#include "event-top.h"
c906108c
SS
27
28#ifdef HAVE_CURSES_H
29#include <curses.h>
30#endif
31#ifdef HAVE_TERM_H
32#include <term.h>
33#endif
34
9d271fd8
AC
35#ifdef __GO32__
36#include <pc.h>
37#endif
38
c906108c
SS
39/* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */
40#ifdef reg
41#undef reg
42#endif
43
042be3a9 44#include <signal.h>
c906108c
SS
45#include "gdbcmd.h"
46#include "serial.h"
47#include "bfd.h"
48#include "target.h"
49#include "demangle.h"
50#include "expression.h"
51#include "language.h"
52#include "annotate.h"
53
ac2e2ef7
AC
54#include "inferior.h" /* for signed_pointer_to_address */
55
c906108c
SS
56#include <readline/readline.h>
57
917317f4
JM
58#undef XMALLOC
59#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
60
c906108c
SS
61/* readline defines this. */
62#undef savestring
63
507f3c78 64void (*error_begin_hook) (void);
c906108c 65
2acceee2
JM
66/* Holds the last error message issued by gdb */
67
d9fcf2fb 68static struct ui_file *gdb_lasterr;
2acceee2 69
c906108c
SS
70/* Prototypes for local functions */
71
d9fcf2fb
JM
72static void vfprintf_maybe_filtered (struct ui_file *, const char *,
73 va_list, int);
c906108c 74
d9fcf2fb 75static void fputs_maybe_filtered (const char *, struct ui_file *, int);
c906108c
SS
76
77#if defined (USE_MMALLOC) && !defined (NO_MMCHECK)
a14ed312 78static void malloc_botch (void);
c906108c
SS
79#endif
80
a14ed312 81static void prompt_for_continue (void);
c906108c 82
a14ed312 83static void set_width_command (char *, int, struct cmd_list_element *);
c906108c 84
a14ed312 85static void set_width (void);
c906108c 86
c906108c
SS
87/* Chain of cleanup actions established with make_cleanup,
88 to be executed if an error happens. */
89
c5aa993b
JM
90static struct cleanup *cleanup_chain; /* cleaned up after a failed command */
91static struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */
92static struct cleanup *run_cleanup_chain; /* cleaned up on each 'run' */
93static struct cleanup *exec_cleanup_chain; /* cleaned up on each execution command */
6426a772
JM
94/* cleaned up on each error from within an execution command */
95static struct cleanup *exec_error_cleanup_chain;
43ff13b4
JM
96
97/* Pointer to what is left to do for an execution command after the
98 target stops. Used only in asynchronous mode, by targets that
99 support async execution. The finish and until commands use it. So
100 does the target extended-remote command. */
101struct continuation *cmd_continuation;
c2d11a7d 102struct continuation *intermediate_continuation;
c906108c
SS
103
104/* Nonzero if we have job control. */
105
106int job_control;
107
108/* Nonzero means a quit has been requested. */
109
110int quit_flag;
111
112/* Nonzero means quit immediately if Control-C is typed now, rather
113 than waiting until QUIT is executed. Be careful in setting this;
114 code which executes with immediate_quit set has to be very careful
115 about being able to deal with being interrupted at any time. It is
116 almost always better to use QUIT; the only exception I can think of
117 is being able to quit out of a system call (using EINTR loses if
118 the SIGINT happens between the previous QUIT and the system call).
119 To immediately quit in the case in which a SIGINT happens between
120 the previous QUIT and setting immediate_quit (desirable anytime we
121 expect to block), call QUIT after setting immediate_quit. */
122
123int immediate_quit;
124
125/* Nonzero means that encoded C++ names should be printed out in their
126 C++ form rather than raw. */
127
128int demangle = 1;
129
130/* Nonzero means that encoded C++ names should be printed out in their
131 C++ form even in assembler language displays. If this is set, but
132 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
133
134int asm_demangle = 0;
135
136/* Nonzero means that strings with character values >0x7F should be printed
137 as octal escapes. Zero means just print the value (e.g. it's an
138 international character, and the terminal or window can cope.) */
139
140int sevenbit_strings = 0;
141
142/* String to be printed before error messages, if any. */
143
144char *error_pre_print;
145
146/* String to be printed before quit messages, if any. */
147
148char *quit_pre_print;
149
150/* String to be printed before warning messages, if any. */
151
152char *warning_pre_print = "\nwarning: ";
153
154int pagination_enabled = 1;
c906108c 155\f
c5aa993b 156
c906108c
SS
157/* Add a new cleanup to the cleanup_chain,
158 and return the previous chain pointer
159 to be passed later to do_cleanups or discard_cleanups.
160 Args are FUNCTION to clean up with, and ARG to pass to it. */
161
162struct cleanup *
e4005526 163make_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 164{
c5aa993b 165 return make_my_cleanup (&cleanup_chain, function, arg);
c906108c
SS
166}
167
168struct cleanup *
e4005526 169make_final_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 170{
c5aa993b 171 return make_my_cleanup (&final_cleanup_chain, function, arg);
c906108c 172}
7a292a7a 173
c906108c 174struct cleanup *
e4005526 175make_run_cleanup (make_cleanup_ftype *function, void *arg)
c906108c 176{
c5aa993b 177 return make_my_cleanup (&run_cleanup_chain, function, arg);
c906108c 178}
7a292a7a 179
43ff13b4 180struct cleanup *
e4005526 181make_exec_cleanup (make_cleanup_ftype *function, void *arg)
43ff13b4 182{
c5aa993b 183 return make_my_cleanup (&exec_cleanup_chain, function, arg);
43ff13b4
JM
184}
185
6426a772 186struct cleanup *
e4005526 187make_exec_error_cleanup (make_cleanup_ftype *function, void *arg)
6426a772
JM
188{
189 return make_my_cleanup (&exec_error_cleanup_chain, function, arg);
190}
191
7a292a7a 192static void
fba45db2 193do_freeargv (void *arg)
7a292a7a 194{
c5aa993b 195 freeargv ((char **) arg);
7a292a7a
SS
196}
197
198struct cleanup *
fba45db2 199make_cleanup_freeargv (char **arg)
7a292a7a
SS
200{
201 return make_my_cleanup (&cleanup_chain, do_freeargv, arg);
202}
203
5c65bbb6
AC
204static void
205do_bfd_close_cleanup (void *arg)
206{
207 bfd_close (arg);
208}
209
210struct cleanup *
211make_cleanup_bfd_close (bfd *abfd)
212{
213 return make_cleanup (do_bfd_close_cleanup, abfd);
214}
215
f5ff8c83
AC
216static void
217do_close_cleanup (void *arg)
218{
f042532c
AC
219 int *fd = arg;
220 close (*fd);
221 xfree (fd);
f5ff8c83
AC
222}
223
224struct cleanup *
225make_cleanup_close (int fd)
226{
f042532c
AC
227 int *saved_fd = xmalloc (sizeof (fd));
228 *saved_fd = fd;
229 return make_cleanup (do_close_cleanup, saved_fd);
f5ff8c83
AC
230}
231
11cf8741 232static void
d9fcf2fb 233do_ui_file_delete (void *arg)
11cf8741 234{
d9fcf2fb 235 ui_file_delete (arg);
11cf8741
JM
236}
237
238struct cleanup *
d9fcf2fb 239make_cleanup_ui_file_delete (struct ui_file *arg)
11cf8741 240{
d9fcf2fb 241 return make_my_cleanup (&cleanup_chain, do_ui_file_delete, arg);
11cf8741
JM
242}
243
c906108c 244struct cleanup *
e4005526
AC
245make_my_cleanup (struct cleanup **pmy_chain, make_cleanup_ftype *function,
246 void *arg)
c906108c
SS
247{
248 register struct cleanup *new
c5aa993b 249 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
c906108c
SS
250 register struct cleanup *old_chain = *pmy_chain;
251
252 new->next = *pmy_chain;
253 new->function = function;
254 new->arg = arg;
255 *pmy_chain = new;
256
257 return old_chain;
258}
259
260/* Discard cleanups and do the actions they describe
261 until we get back to the point OLD_CHAIN in the cleanup_chain. */
262
263void
fba45db2 264do_cleanups (register struct cleanup *old_chain)
c906108c 265{
c5aa993b 266 do_my_cleanups (&cleanup_chain, old_chain);
c906108c
SS
267}
268
269void
fba45db2 270do_final_cleanups (register struct cleanup *old_chain)
c906108c 271{
c5aa993b 272 do_my_cleanups (&final_cleanup_chain, old_chain);
c906108c
SS
273}
274
275void
fba45db2 276do_run_cleanups (register struct cleanup *old_chain)
c906108c 277{
c5aa993b 278 do_my_cleanups (&run_cleanup_chain, old_chain);
c906108c
SS
279}
280
43ff13b4 281void
fba45db2 282do_exec_cleanups (register struct cleanup *old_chain)
43ff13b4 283{
c5aa993b 284 do_my_cleanups (&exec_cleanup_chain, old_chain);
43ff13b4
JM
285}
286
6426a772 287void
fba45db2 288do_exec_error_cleanups (register struct cleanup *old_chain)
6426a772
JM
289{
290 do_my_cleanups (&exec_error_cleanup_chain, old_chain);
291}
292
c906108c 293void
fba45db2
KB
294do_my_cleanups (register struct cleanup **pmy_chain,
295 register struct cleanup *old_chain)
c906108c
SS
296{
297 register struct cleanup *ptr;
298 while ((ptr = *pmy_chain) != old_chain)
299 {
300 *pmy_chain = ptr->next; /* Do this first incase recursion */
301 (*ptr->function) (ptr->arg);
b8c9b27d 302 xfree (ptr);
c906108c
SS
303 }
304}
305
306/* Discard cleanups, not doing the actions they describe,
307 until we get back to the point OLD_CHAIN in the cleanup_chain. */
308
309void
fba45db2 310discard_cleanups (register struct cleanup *old_chain)
c906108c 311{
c5aa993b 312 discard_my_cleanups (&cleanup_chain, old_chain);
c906108c
SS
313}
314
315void
fba45db2 316discard_final_cleanups (register struct cleanup *old_chain)
c906108c 317{
c5aa993b 318 discard_my_cleanups (&final_cleanup_chain, old_chain);
c906108c
SS
319}
320
6426a772 321void
fba45db2 322discard_exec_error_cleanups (register struct cleanup *old_chain)
6426a772
JM
323{
324 discard_my_cleanups (&exec_error_cleanup_chain, old_chain);
325}
326
c906108c 327void
fba45db2
KB
328discard_my_cleanups (register struct cleanup **pmy_chain,
329 register struct cleanup *old_chain)
c906108c
SS
330{
331 register struct cleanup *ptr;
332 while ((ptr = *pmy_chain) != old_chain)
333 {
334 *pmy_chain = ptr->next;
b8c9b27d 335 xfree (ptr);
c906108c
SS
336 }
337}
338
339/* Set the cleanup_chain to 0, and return the old cleanup chain. */
340struct cleanup *
fba45db2 341save_cleanups (void)
c906108c 342{
c5aa993b 343 return save_my_cleanups (&cleanup_chain);
c906108c
SS
344}
345
346struct cleanup *
fba45db2 347save_final_cleanups (void)
c906108c 348{
c5aa993b 349 return save_my_cleanups (&final_cleanup_chain);
c906108c
SS
350}
351
352struct cleanup *
fba45db2 353save_my_cleanups (struct cleanup **pmy_chain)
c906108c
SS
354{
355 struct cleanup *old_chain = *pmy_chain;
356
357 *pmy_chain = 0;
358 return old_chain;
359}
360
361/* Restore the cleanup chain from a previously saved chain. */
362void
fba45db2 363restore_cleanups (struct cleanup *chain)
c906108c 364{
c5aa993b 365 restore_my_cleanups (&cleanup_chain, chain);
c906108c
SS
366}
367
368void
fba45db2 369restore_final_cleanups (struct cleanup *chain)
c906108c 370{
c5aa993b 371 restore_my_cleanups (&final_cleanup_chain, chain);
c906108c
SS
372}
373
374void
fba45db2 375restore_my_cleanups (struct cleanup **pmy_chain, struct cleanup *chain)
c906108c
SS
376{
377 *pmy_chain = chain;
378}
379
380/* This function is useful for cleanups.
381 Do
382
c5aa993b
JM
383 foo = xmalloc (...);
384 old_chain = make_cleanup (free_current_contents, &foo);
c906108c
SS
385
386 to arrange to free the object thus allocated. */
387
388void
2f9429ae 389free_current_contents (void *ptr)
c906108c 390{
2f9429ae 391 void **location = ptr;
e2f9c474 392 if (location == NULL)
8e65ff28
AC
393 internal_error (__FILE__, __LINE__,
394 "free_current_contents: NULL pointer");
2f9429ae 395 if (*location != NULL)
e2f9c474 396 {
b8c9b27d 397 xfree (*location);
e2f9c474
AC
398 *location = NULL;
399 }
c906108c
SS
400}
401
402/* Provide a known function that does nothing, to use as a base for
403 for a possibly long chain of cleanups. This is useful where we
404 use the cleanup chain for handling normal cleanups as well as dealing
405 with cleanups that need to be done as a result of a call to error().
406 In such cases, we may not be certain where the first cleanup is, unless
407 we have a do-nothing one to always use as the base. */
408
409/* ARGSUSED */
410void
e4005526 411null_cleanup (void *arg)
c906108c
SS
412{
413}
414
74f832da 415/* Add a continuation to the continuation list, the global list
c2d11a7d 416 cmd_continuation. The new continuation will be added at the front.*/
43ff13b4 417void
74f832da
KB
418add_continuation (void (*continuation_hook) (struct continuation_arg *),
419 struct continuation_arg *arg_list)
43ff13b4 420{
c5aa993b 421 struct continuation *continuation_ptr;
43ff13b4 422
c5aa993b
JM
423 continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation));
424 continuation_ptr->continuation_hook = continuation_hook;
425 continuation_ptr->arg_list = arg_list;
426 continuation_ptr->next = cmd_continuation;
427 cmd_continuation = continuation_ptr;
43ff13b4
JM
428}
429
430/* Walk down the cmd_continuation list, and execute all the
c2d11a7d
JM
431 continuations. There is a problem though. In some cases new
432 continuations may be added while we are in the middle of this
433 loop. If this happens they will be added in the front, and done
434 before we have a chance of exhausting those that were already
435 there. We need to then save the beginning of the list in a pointer
436 and do the continuations from there on, instead of using the
437 global beginning of list as our iteration pointer.*/
c5aa993b 438void
fba45db2 439do_all_continuations (void)
c2d11a7d
JM
440{
441 struct continuation *continuation_ptr;
442 struct continuation *saved_continuation;
443
444 /* Copy the list header into another pointer, and set the global
445 list header to null, so that the global list can change as a side
446 effect of invoking the continuations and the processing of
447 the preexisting continuations will not be affected. */
448 continuation_ptr = cmd_continuation;
449 cmd_continuation = NULL;
450
451 /* Work now on the list we have set aside. */
452 while (continuation_ptr)
453 {
454 (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
455 saved_continuation = continuation_ptr;
456 continuation_ptr = continuation_ptr->next;
b8c9b27d 457 xfree (saved_continuation);
c2d11a7d
JM
458 }
459}
460
461/* Walk down the cmd_continuation list, and get rid of all the
462 continuations. */
463void
fba45db2 464discard_all_continuations (void)
43ff13b4 465{
c5aa993b 466 struct continuation *continuation_ptr;
43ff13b4 467
c5aa993b
JM
468 while (cmd_continuation)
469 {
c5aa993b
JM
470 continuation_ptr = cmd_continuation;
471 cmd_continuation = continuation_ptr->next;
b8c9b27d 472 xfree (continuation_ptr);
c5aa993b 473 }
43ff13b4 474}
c2c6d25f 475
57e687d9 476/* Add a continuation to the continuation list, the global list
c2d11a7d
JM
477 intermediate_continuation. The new continuation will be added at the front.*/
478void
74f832da
KB
479add_intermediate_continuation (void (*continuation_hook)
480 (struct continuation_arg *),
481 struct continuation_arg *arg_list)
c2d11a7d
JM
482{
483 struct continuation *continuation_ptr;
484
485 continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation));
486 continuation_ptr->continuation_hook = continuation_hook;
487 continuation_ptr->arg_list = arg_list;
488 continuation_ptr->next = intermediate_continuation;
489 intermediate_continuation = continuation_ptr;
490}
491
492/* Walk down the cmd_continuation list, and execute all the
493 continuations. There is a problem though. In some cases new
494 continuations may be added while we are in the middle of this
495 loop. If this happens they will be added in the front, and done
496 before we have a chance of exhausting those that were already
497 there. We need to then save the beginning of the list in a pointer
498 and do the continuations from there on, instead of using the
499 global beginning of list as our iteration pointer.*/
500void
fba45db2 501do_all_intermediate_continuations (void)
c2d11a7d
JM
502{
503 struct continuation *continuation_ptr;
504 struct continuation *saved_continuation;
505
506 /* Copy the list header into another pointer, and set the global
507 list header to null, so that the global list can change as a side
508 effect of invoking the continuations and the processing of
509 the preexisting continuations will not be affected. */
510 continuation_ptr = intermediate_continuation;
511 intermediate_continuation = NULL;
512
513 /* Work now on the list we have set aside. */
514 while (continuation_ptr)
515 {
516 (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
517 saved_continuation = continuation_ptr;
518 continuation_ptr = continuation_ptr->next;
b8c9b27d 519 xfree (saved_continuation);
c2d11a7d
JM
520 }
521}
522
c2c6d25f
JM
523/* Walk down the cmd_continuation list, and get rid of all the
524 continuations. */
525void
fba45db2 526discard_all_intermediate_continuations (void)
c2c6d25f
JM
527{
528 struct continuation *continuation_ptr;
529
c2d11a7d 530 while (intermediate_continuation)
c2c6d25f 531 {
c2d11a7d
JM
532 continuation_ptr = intermediate_continuation;
533 intermediate_continuation = continuation_ptr->next;
b8c9b27d 534 xfree (continuation_ptr);
c2c6d25f
JM
535 }
536}
537
c906108c 538\f
c5aa993b 539
c906108c
SS
540/* Print a warning message. Way to use this is to call warning_begin,
541 output the warning message (use unfiltered output to gdb_stderr),
542 ending in a newline. There is not currently a warning_end that you
543 call afterwards, but such a thing might be added if it is useful
544 for a GUI to separate warning messages from other output.
545
546 FIXME: Why do warnings use unfiltered output and errors filtered?
547 Is this anything other than a historical accident? */
548
549void
fba45db2 550warning_begin (void)
c906108c
SS
551{
552 target_terminal_ours ();
c5aa993b 553 wrap_here (""); /* Force out any buffered output */
c906108c
SS
554 gdb_flush (gdb_stdout);
555 if (warning_pre_print)
556 fprintf_unfiltered (gdb_stderr, warning_pre_print);
557}
558
559/* Print a warning message.
560 The first argument STRING is the warning message, used as a fprintf string,
561 and the remaining args are passed as arguments to it.
562 The primary difference between warnings and errors is that a warning
563 does not force the return to command level. */
564
c906108c 565void
c5aa993b 566warning (const char *string,...)
c906108c
SS
567{
568 va_list args;
c906108c 569 va_start (args, string);
c906108c
SS
570 if (warning_hook)
571 (*warning_hook) (string, args);
572 else
c5aa993b
JM
573 {
574 warning_begin ();
575 vfprintf_unfiltered (gdb_stderr, string, args);
576 fprintf_unfiltered (gdb_stderr, "\n");
577 va_end (args);
578 }
c906108c
SS
579}
580
581/* Start the printing of an error message. Way to use this is to call
582 this, output the error message (use filtered output to gdb_stderr
583 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
584 in a newline, and then call return_to_top_level (RETURN_ERROR).
585 error() provides a convenient way to do this for the special case
586 that the error message can be formatted with a single printf call,
587 but this is more general. */
588void
fba45db2 589error_begin (void)
c906108c
SS
590{
591 if (error_begin_hook)
592 error_begin_hook ();
593
594 target_terminal_ours ();
c5aa993b 595 wrap_here (""); /* Force out any buffered output */
c906108c
SS
596 gdb_flush (gdb_stdout);
597
598 annotate_error_begin ();
599
600 if (error_pre_print)
601 fprintf_filtered (gdb_stderr, error_pre_print);
602}
603
604/* Print an error message and return to command level.
605 The first argument STRING is the error message, used as a fprintf string,
606 and the remaining args are passed as arguments to it. */
607
4ce44c66
JM
608NORETURN void
609verror (const char *string, va_list args)
610{
c2d11a7d
JM
611 char *err_string;
612 struct cleanup *err_string_cleanup;
4ce44c66 613 /* FIXME: cagney/1999-11-10: All error calls should come here.
e26cc349 614 Unfortunately some code uses the sequence: error_begin(); print
4ce44c66
JM
615 error message; return_to_top_level. That code should be
616 flushed. */
617 error_begin ();
c2d11a7d
JM
618 /* NOTE: It's tempting to just do the following...
619 vfprintf_filtered (gdb_stderr, string, args);
620 and then follow with a similar looking statement to cause the message
621 to also go to gdb_lasterr. But if we do this, we'll be traversing the
622 va_list twice which works on some platforms and fails miserably on
623 others. */
624 /* Save it as the last error */
d9fcf2fb 625 ui_file_rewind (gdb_lasterr);
4ce44c66 626 vfprintf_filtered (gdb_lasterr, string, args);
c2d11a7d
JM
627 /* Retrieve the last error and print it to gdb_stderr */
628 err_string = error_last_message ();
b8c9b27d 629 err_string_cleanup = make_cleanup (xfree, err_string);
c2d11a7d
JM
630 fputs_filtered (err_string, gdb_stderr);
631 fprintf_filtered (gdb_stderr, "\n");
632 do_cleanups (err_string_cleanup);
4ce44c66
JM
633 return_to_top_level (RETURN_ERROR);
634}
635
c906108c 636NORETURN void
c5aa993b 637error (const char *string,...)
c906108c
SS
638{
639 va_list args;
c906108c 640 va_start (args, string);
4ce44c66
JM
641 verror (string, args);
642 va_end (args);
c906108c
SS
643}
644
2acceee2 645NORETURN void
d9fcf2fb 646error_stream (struct ui_file *stream)
2acceee2 647{
4ce44c66 648 long size;
d9fcf2fb 649 char *msg = ui_file_xstrdup (stream, &size);
b8c9b27d 650 make_cleanup (xfree, msg);
4ce44c66 651 error ("%s", msg);
2acceee2
JM
652}
653
654/* Get the last error message issued by gdb */
655
656char *
657error_last_message (void)
658{
4ce44c66 659 long len;
d9fcf2fb 660 return ui_file_xstrdup (gdb_lasterr, &len);
2acceee2 661}
4ce44c66 662
2acceee2
JM
663/* This is to be called by main() at the very beginning */
664
665void
666error_init (void)
667{
4ce44c66 668 gdb_lasterr = mem_fileopen ();
2acceee2 669}
c906108c 670
96baa820
JM
671/* Print a message reporting an internal error. Ask the user if they
672 want to continue, dump core, or just exit. */
c906108c 673
c906108c 674NORETURN void
8e65ff28
AC
675internal_verror (const char *file, int line,
676 const char *fmt, va_list ap)
c906108c 677{
96baa820
JM
678 static char msg[] = "Internal GDB error: recursive internal error.\n";
679 static int dejavu = 0;
7be570e7
JM
680 int continue_p;
681 int dump_core_p;
c906108c 682
96baa820
JM
683 /* don't allow infinite error recursion. */
684 switch (dejavu)
685 {
686 case 0:
687 dejavu = 1;
688 break;
689 case 1:
690 dejavu = 2;
691 fputs_unfiltered (msg, gdb_stderr);
e1e9e218 692 internal_error (__FILE__, __LINE__, "failed internal consistency check");
96baa820
JM
693 default:
694 dejavu = 3;
695 write (STDERR_FILENO, msg, sizeof (msg));
696 exit (1);
697 }
c906108c 698
96baa820 699 /* Try to get the message out */
4261bedc 700 target_terminal_ours ();
8e65ff28 701 fprintf_unfiltered (gdb_stderr, "%s:%d: gdb-internal-error: ", file, line);
4ce44c66 702 vfprintf_unfiltered (gdb_stderr, fmt, ap);
96baa820 703 fputs_unfiltered ("\n", gdb_stderr);
c906108c 704
7be570e7
JM
705 /* Default (no case) is to quit GDB. When in batch mode this
706 lessens the likelhood of GDB going into an infinate loop. */
707 continue_p = query ("\
62fd9fad 708An internal GDB error was detected. This may make further\n\
7be570e7
JM
709debugging unreliable. Continue this debugging session? ");
710
711 /* Default (no case) is to not dump core. Lessen the chance of GDB
712 leaving random core files around. */
713 dump_core_p = query ("\
714Create a core file containing the current state of GDB? ");
715
716 if (continue_p)
717 {
718 if (dump_core_p)
719 {
720 if (fork () == 0)
e1e9e218 721 internal_error (__FILE__, __LINE__, "failed internal consistency check");
7be570e7
JM
722 }
723 }
724 else
725 {
726 if (dump_core_p)
e1e9e218 727 internal_error (__FILE__, __LINE__, "failed internal consistency check");
7be570e7
JM
728 else
729 exit (1);
730 }
96baa820
JM
731
732 dejavu = 0;
733 return_to_top_level (RETURN_ERROR);
c906108c
SS
734}
735
4ce44c66 736NORETURN void
8e65ff28 737internal_error (const char *file, int line, const char *string, ...)
4ce44c66
JM
738{
739 va_list ap;
740 va_start (ap, string);
4261bedc 741
8e65ff28 742 internal_verror (file, line, string, ap);
4ce44c66
JM
743 va_end (ap);
744}
745
c906108c
SS
746/* The strerror() function can return NULL for errno values that are
747 out of range. Provide a "safe" version that always returns a
748 printable string. */
749
750char *
fba45db2 751safe_strerror (int errnum)
c906108c
SS
752{
753 char *msg;
754 static char buf[32];
755
756 if ((msg = strerror (errnum)) == NULL)
757 {
758 sprintf (buf, "(undocumented errno %d)", errnum);
759 msg = buf;
760 }
761 return (msg);
762}
763
c906108c
SS
764/* Print the system error message for errno, and also mention STRING
765 as the file name for which the error was encountered.
766 Then return to command level. */
767
768NORETURN void
fba45db2 769perror_with_name (char *string)
c906108c
SS
770{
771 char *err;
772 char *combined;
773
774 err = safe_strerror (errno);
775 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
776 strcpy (combined, string);
777 strcat (combined, ": ");
778 strcat (combined, err);
779
780 /* I understand setting these is a matter of taste. Still, some people
781 may clear errno but not know about bfd_error. Doing this here is not
782 unreasonable. */
783 bfd_set_error (bfd_error_no_error);
784 errno = 0;
785
c5aa993b 786 error ("%s.", combined);
c906108c
SS
787}
788
789/* Print the system error message for ERRCODE, and also mention STRING
790 as the file name for which the error was encountered. */
791
792void
fba45db2 793print_sys_errmsg (char *string, int errcode)
c906108c
SS
794{
795 char *err;
796 char *combined;
797
798 err = safe_strerror (errcode);
799 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
800 strcpy (combined, string);
801 strcat (combined, ": ");
802 strcat (combined, err);
803
804 /* We want anything which was printed on stdout to come out first, before
805 this message. */
806 gdb_flush (gdb_stdout);
807 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
808}
809
810/* Control C eventually causes this to be called, at a convenient time. */
811
812void
fba45db2 813quit (void)
c906108c
SS
814{
815 serial_t gdb_stdout_serial = serial_fdopen (1);
816
817 target_terminal_ours ();
818
819 /* We want all output to appear now, before we print "Quit". We
820 have 3 levels of buffering we have to flush (it's possible that
821 some of these should be changed to flush the lower-level ones
822 too): */
823
824 /* 1. The _filtered buffer. */
c5aa993b 825 wrap_here ((char *) 0);
c906108c
SS
826
827 /* 2. The stdio buffer. */
828 gdb_flush (gdb_stdout);
829 gdb_flush (gdb_stderr);
830
831 /* 3. The system-level buffer. */
832 SERIAL_DRAIN_OUTPUT (gdb_stdout_serial);
833 SERIAL_UN_FDOPEN (gdb_stdout_serial);
834
835 annotate_error_begin ();
836
837 /* Don't use *_filtered; we don't want to prompt the user to continue. */
838 if (quit_pre_print)
839 fprintf_unfiltered (gdb_stderr, quit_pre_print);
840
7be570e7
JM
841#ifdef __MSDOS__
842 /* No steenking SIGINT will ever be coming our way when the
843 program is resumed. Don't lie. */
844 fprintf_unfiltered (gdb_stderr, "Quit\n");
845#else
c906108c 846 if (job_control
c5aa993b
JM
847 /* If there is no terminal switching for this target, then we can't
848 possibly get screwed by the lack of job control. */
c906108c
SS
849 || current_target.to_terminal_ours == NULL)
850 fprintf_unfiltered (gdb_stderr, "Quit\n");
851 else
852 fprintf_unfiltered (gdb_stderr,
c5aa993b 853 "Quit (expect signal SIGINT when the program is resumed)\n");
7be570e7 854#endif
c906108c
SS
855 return_to_top_level (RETURN_QUIT);
856}
857
858
7be570e7 859#if defined(_MSC_VER) /* should test for wingdb instead? */
c906108c
SS
860
861/*
862 * Windows translates all keyboard and mouse events
863 * into a message which is appended to the message
864 * queue for the process.
865 */
866
c5aa993b 867void
fba45db2 868notice_quit (void)
c906108c 869{
c5aa993b 870 int k = win32pollquit ();
c906108c
SS
871 if (k == 1)
872 quit_flag = 1;
873 else if (k == 2)
874 immediate_quit = 1;
875}
876
4ce44c66 877#else /* !defined(_MSC_VER) */
c906108c 878
c5aa993b 879void
fba45db2 880notice_quit (void)
c906108c
SS
881{
882 /* Done by signals */
883}
884
4ce44c66 885#endif /* !defined(_MSC_VER) */
c906108c 886
c906108c 887/* Control C comes here */
c906108c 888void
fba45db2 889request_quit (int signo)
c906108c
SS
890{
891 quit_flag = 1;
892 /* Restore the signal handler. Harmless with BSD-style signals, needed
893 for System V-style signals. So just always do it, rather than worrying
894 about USG defines and stuff like that. */
895 signal (signo, request_quit);
896
897#ifdef REQUEST_QUIT
898 REQUEST_QUIT;
899#else
c5aa993b 900 if (immediate_quit)
c906108c
SS
901 quit ();
902#endif
903}
c906108c
SS
904\f
905/* Memory management stuff (malloc friends). */
906
907/* Make a substitute size_t for non-ANSI compilers. */
908
909#ifndef HAVE_STDDEF_H
910#ifndef size_t
911#define size_t unsigned int
912#endif
913#endif
914
915#if !defined (USE_MMALLOC)
916
082faf24
AC
917PTR
918mcalloc (PTR md, size_t number, size_t size)
ed9a39eb
JM
919{
920 return calloc (number, size);
921}
922
c906108c 923PTR
fba45db2 924mmalloc (PTR md, size_t size)
c906108c
SS
925{
926 return malloc (size);
927}
928
929PTR
fba45db2 930mrealloc (PTR md, PTR ptr, size_t size)
c906108c 931{
c5aa993b 932 if (ptr == 0) /* Guard against old realloc's */
c906108c
SS
933 return malloc (size);
934 else
935 return realloc (ptr, size);
936}
937
938void
fba45db2 939mfree (PTR md, PTR ptr)
c906108c 940{
b8c9b27d 941 xfree (ptr);
c906108c
SS
942}
943
c5aa993b 944#endif /* USE_MMALLOC */
c906108c
SS
945
946#if !defined (USE_MMALLOC) || defined (NO_MMCHECK)
947
948void
082faf24 949init_malloc (void *md)
c906108c
SS
950{
951}
952
953#else /* Have mmalloc and want corruption checking */
954
955static void
fba45db2 956malloc_botch (void)
c906108c 957{
96baa820 958 fprintf_unfiltered (gdb_stderr, "Memory corruption\n");
e1e9e218 959 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
960}
961
962/* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
963 by MD, to detect memory corruption. Note that MD may be NULL to specify
964 the default heap that grows via sbrk.
965
966 Note that for freshly created regions, we must call mmcheckf prior to any
967 mallocs in the region. Otherwise, any region which was allocated prior to
968 installing the checking hooks, which is later reallocated or freed, will
969 fail the checks! The mmcheck function only allows initial hooks to be
970 installed before the first mmalloc. However, anytime after we have called
971 mmcheck the first time to install the checking hooks, we can call it again
972 to update the function pointer to the memory corruption handler.
973
974 Returns zero on failure, non-zero on success. */
975
976#ifndef MMCHECK_FORCE
977#define MMCHECK_FORCE 0
978#endif
979
980void
082faf24 981init_malloc (void *md)
c906108c
SS
982{
983 if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE))
984 {
985 /* Don't use warning(), which relies on current_target being set
c5aa993b
JM
986 to something other than dummy_target, until after
987 initialize_all_files(). */
c906108c
SS
988
989 fprintf_unfiltered
990 (gdb_stderr, "warning: failed to install memory consistency checks; ");
991 fprintf_unfiltered
992 (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n");
993 }
994
995 mmtrace ();
996}
997
998#endif /* Have mmalloc and want corruption checking */
999
1000/* Called when a memory allocation fails, with the number of bytes of
1001 memory requested in SIZE. */
1002
1003NORETURN void
fba45db2 1004nomem (long size)
c906108c
SS
1005{
1006 if (size > 0)
1007 {
8e65ff28
AC
1008 internal_error (__FILE__, __LINE__,
1009 "virtual memory exhausted: can't allocate %ld bytes.", size);
c906108c
SS
1010 }
1011 else
1012 {
8e65ff28
AC
1013 internal_error (__FILE__, __LINE__,
1014 "virtual memory exhausted.");
c906108c
SS
1015 }
1016}
1017
1018/* Like mmalloc but get error if no storage available, and protect against
1019 the caller wanting to allocate zero bytes. Whether to return NULL for
1020 a zero byte request, or translate the request into a request for one
1021 byte of zero'd storage, is a religious issue. */
1022
1023PTR
fba45db2 1024xmmalloc (PTR md, long size)
c906108c
SS
1025{
1026 register PTR val;
1027
1028 if (size == 0)
1029 {
1030 val = NULL;
1031 }
1032 else if ((val = mmalloc (md, size)) == NULL)
1033 {
1034 nomem (size);
1035 }
1036 return (val);
1037}
1038
1039/* Like mrealloc but get error if no storage available. */
1040
1041PTR
fba45db2 1042xmrealloc (PTR md, PTR ptr, long size)
c906108c
SS
1043{
1044 register PTR val;
1045
1046 if (ptr != NULL)
1047 {
1048 val = mrealloc (md, ptr, size);
1049 }
1050 else
1051 {
1052 val = mmalloc (md, size);
1053 }
1054 if (val == NULL)
1055 {
1056 nomem (size);
1057 }
1058 return (val);
1059}
1060
1061/* Like malloc but get error if no storage available, and protect against
1062 the caller wanting to allocate zero bytes. */
1063
1064PTR
fba45db2 1065xmalloc (size_t size)
c906108c
SS
1066{
1067 return (xmmalloc ((PTR) NULL, size));
1068}
1069
ed9a39eb
JM
1070/* Like calloc but get error if no storage available */
1071
1072PTR
1073xcalloc (size_t number, size_t size)
1074{
1075 void *mem = mcalloc (NULL, number, size);
1076 if (mem == NULL)
1077 nomem (number * size);
1078 return mem;
1079}
1080
c906108c
SS
1081/* Like mrealloc but get error if no storage available. */
1082
1083PTR
fba45db2 1084xrealloc (PTR ptr, size_t size)
c906108c
SS
1085{
1086 return (xmrealloc ((PTR) NULL, ptr, size));
1087}
b8c9b27d
KB
1088
1089/* Free up space allocated by one of xmalloc(), xcalloc(), or
1090 xrealloc(). */
1091
1092void
1093xfree (void *ptr)
1094{
1095 if (ptr != NULL)
1096 free (ptr);
1097}
c906108c 1098\f
c5aa993b 1099
76995688
AC
1100/* Like asprintf/vasprintf but get an internal_error if the call
1101 fails. */
1102
1103void
1104xasprintf (char **ret, const char *format, ...)
1105{
1106 va_list args;
1107 va_start (args, format);
1108 xvasprintf (ret, format, args);
1109 va_end (args);
1110}
1111
1112void
1113xvasprintf (char **ret, const char *format, va_list ap)
1114{
1115 int status = vasprintf (ret, format, ap);
1116 /* NULL could be returned due to a memory allocation problem; a
1117 badly format string; or something else. */
1118 if ((*ret) == NULL)
8e65ff28
AC
1119 internal_error (__FILE__, __LINE__,
1120 "vasprintf returned NULL buffer (errno %d)",
1121 errno);
76995688
AC
1122 /* A negative status with a non-NULL buffer shouldn't never
1123 happen. But to be sure. */
1124 if (status < 0)
8e65ff28
AC
1125 internal_error (__FILE__, __LINE__,
1126 "vasprintf call failed (errno %d)",
1127 errno);
76995688
AC
1128}
1129
1130
c906108c
SS
1131/* My replacement for the read system call.
1132 Used like `read' but keeps going if `read' returns too soon. */
1133
1134int
fba45db2 1135myread (int desc, char *addr, int len)
c906108c
SS
1136{
1137 register int val;
1138 int orglen = len;
1139
1140 while (len > 0)
1141 {
1142 val = read (desc, addr, len);
1143 if (val < 0)
1144 return val;
1145 if (val == 0)
1146 return orglen - len;
1147 len -= val;
1148 addr += val;
1149 }
1150 return orglen;
1151}
1152\f
1153/* Make a copy of the string at PTR with SIZE characters
1154 (and add a null character at the end in the copy).
1155 Uses malloc to get the space. Returns the address of the copy. */
1156
1157char *
fba45db2 1158savestring (const char *ptr, int size)
c906108c
SS
1159{
1160 register char *p = (char *) xmalloc (size + 1);
1161 memcpy (p, ptr, size);
1162 p[size] = 0;
1163 return p;
1164}
1165
1166char *
082faf24 1167msavestring (void *md, const char *ptr, int size)
c906108c
SS
1168{
1169 register char *p = (char *) xmmalloc (md, size + 1);
1170 memcpy (p, ptr, size);
1171 p[size] = 0;
1172 return p;
1173}
1174
c906108c 1175char *
082faf24 1176mstrsave (void *md, const char *ptr)
c906108c
SS
1177{
1178 return (msavestring (md, ptr, strlen (ptr)));
1179}
1180
1181void
fba45db2 1182print_spaces (register int n, register struct ui_file *file)
c906108c 1183{
392a587b 1184 fputs_unfiltered (n_spaces (n), file);
c906108c
SS
1185}
1186
1187/* Print a host address. */
1188
1189void
d9fcf2fb 1190gdb_print_host_address (void *addr, struct ui_file *stream)
c906108c
SS
1191{
1192
1193 /* We could use the %p conversion specifier to fprintf if we had any
1194 way of knowing whether this host supports it. But the following
1195 should work on the Alpha and on 32 bit machines. */
1196
c5aa993b 1197 fprintf_filtered (stream, "0x%lx", (unsigned long) addr);
c906108c
SS
1198}
1199
1200/* Ask user a y-or-n question and return 1 iff answer is yes.
1201 Takes three args which are given to printf to print the question.
1202 The first, a control string, should end in "? ".
1203 It should not say how to answer, because we do that. */
1204
1205/* VARARGS */
1206int
c5aa993b 1207query (char *ctlstr,...)
c906108c
SS
1208{
1209 va_list args;
1210 register int answer;
1211 register int ans2;
1212 int retval;
1213
c906108c 1214 va_start (args, ctlstr);
c906108c
SS
1215
1216 if (query_hook)
1217 {
1218 return query_hook (ctlstr, args);
1219 }
1220
1221 /* Automatically answer "yes" if input is not from a terminal. */
1222 if (!input_from_terminal_p ())
1223 return 1;
1224#ifdef MPW
1225 /* FIXME Automatically answer "yes" if called from MacGDB. */
1226 if (mac_app)
1227 return 1;
1228#endif /* MPW */
1229
1230 while (1)
1231 {
1232 wrap_here (""); /* Flush any buffered output */
1233 gdb_flush (gdb_stdout);
1234
1235 if (annotation_level > 1)
1236 printf_filtered ("\n\032\032pre-query\n");
1237
1238 vfprintf_filtered (gdb_stdout, ctlstr, args);
1239 printf_filtered ("(y or n) ");
1240
1241 if (annotation_level > 1)
1242 printf_filtered ("\n\032\032query\n");
1243
1244#ifdef MPW
1245 /* If not in MacGDB, move to a new line so the entered line doesn't
c5aa993b 1246 have a prompt on the front of it. */
c906108c
SS
1247 if (!mac_app)
1248 fputs_unfiltered ("\n", gdb_stdout);
1249#endif /* MPW */
1250
c5aa993b 1251 wrap_here ("");
c906108c
SS
1252 gdb_flush (gdb_stdout);
1253
1254#if defined(TUI)
c5aa993b 1255 if (!tui_version || cmdWin == tuiWinWithFocus ())
c906108c
SS
1256#endif
1257 answer = fgetc (stdin);
1258#if defined(TUI)
1259 else
c5aa993b 1260 answer = (unsigned char) tuiBufferGetc ();
c906108c
SS
1261
1262#endif
1263 clearerr (stdin); /* in case of C-d */
1264 if (answer == EOF) /* C-d */
c5aa993b 1265 {
c906108c
SS
1266 retval = 1;
1267 break;
1268 }
1269 /* Eat rest of input line, to EOF or newline */
1270 if ((answer != '\n') || (tui_version && answer != '\r'))
c5aa993b 1271 do
c906108c
SS
1272 {
1273#if defined(TUI)
c5aa993b 1274 if (!tui_version || cmdWin == tuiWinWithFocus ())
c906108c
SS
1275#endif
1276 ans2 = fgetc (stdin);
1277#if defined(TUI)
1278 else
c5aa993b 1279 ans2 = (unsigned char) tuiBufferGetc ();
c906108c
SS
1280#endif
1281 clearerr (stdin);
1282 }
c5aa993b
JM
1283 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
1284 TUIDO (((TuiOpaqueFuncPtr) tui_vStartNewLines, 1));
c906108c
SS
1285
1286 if (answer >= 'a')
1287 answer -= 040;
1288 if (answer == 'Y')
1289 {
1290 retval = 1;
1291 break;
1292 }
1293 if (answer == 'N')
1294 {
1295 retval = 0;
1296 break;
1297 }
1298 printf_filtered ("Please answer y or n.\n");
1299 }
1300
1301 if (annotation_level > 1)
1302 printf_filtered ("\n\032\032post-query\n");
1303 return retval;
1304}
c906108c 1305\f
c5aa993b 1306
c906108c
SS
1307/* Parse a C escape sequence. STRING_PTR points to a variable
1308 containing a pointer to the string to parse. That pointer
1309 should point to the character after the \. That pointer
1310 is updated past the characters we use. The value of the
1311 escape sequence is returned.
1312
1313 A negative value means the sequence \ newline was seen,
1314 which is supposed to be equivalent to nothing at all.
1315
1316 If \ is followed by a null character, we return a negative
1317 value and leave the string pointer pointing at the null character.
1318
1319 If \ is followed by 000, we return 0 and leave the string pointer
1320 after the zeros. A value of 0 does not mean end of string. */
1321
1322int
fba45db2 1323parse_escape (char **string_ptr)
c906108c
SS
1324{
1325 register int c = *(*string_ptr)++;
1326 switch (c)
1327 {
1328 case 'a':
1329 return 007; /* Bell (alert) char */
1330 case 'b':
1331 return '\b';
1332 case 'e': /* Escape character */
1333 return 033;
1334 case 'f':
1335 return '\f';
1336 case 'n':
1337 return '\n';
1338 case 'r':
1339 return '\r';
1340 case 't':
1341 return '\t';
1342 case 'v':
1343 return '\v';
1344 case '\n':
1345 return -2;
1346 case 0:
1347 (*string_ptr)--;
1348 return 0;
1349 case '^':
1350 c = *(*string_ptr)++;
1351 if (c == '\\')
1352 c = parse_escape (string_ptr);
1353 if (c == '?')
1354 return 0177;
1355 return (c & 0200) | (c & 037);
c5aa993b 1356
c906108c
SS
1357 case '0':
1358 case '1':
1359 case '2':
1360 case '3':
1361 case '4':
1362 case '5':
1363 case '6':
1364 case '7':
1365 {
1366 register int i = c - '0';
1367 register int count = 0;
1368 while (++count < 3)
1369 {
1370 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
1371 {
1372 i *= 8;
1373 i += c - '0';
1374 }
1375 else
1376 {
1377 (*string_ptr)--;
1378 break;
1379 }
1380 }
1381 return i;
1382 }
1383 default:
1384 return c;
1385 }
1386}
1387\f
1388/* Print the character C on STREAM as part of the contents of a literal
1389 string whose delimiter is QUOTER. Note that this routine should only
1390 be call for printing things which are independent of the language
1391 of the program being debugged. */
1392
43e526b9 1393static void
74f832da
KB
1394printchar (int c, void (*do_fputs) (const char *, struct ui_file *),
1395 void (*do_fprintf) (struct ui_file *, const char *, ...),
1396 struct ui_file *stream, int quoter)
c906108c
SS
1397{
1398
1399 c &= 0xFF; /* Avoid sign bit follies */
1400
c5aa993b
JM
1401 if (c < 0x20 || /* Low control chars */
1402 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1403 (sevenbit_strings && c >= 0x80))
1404 { /* high order bit set */
1405 switch (c)
1406 {
1407 case '\n':
43e526b9 1408 do_fputs ("\\n", stream);
c5aa993b
JM
1409 break;
1410 case '\b':
43e526b9 1411 do_fputs ("\\b", stream);
c5aa993b
JM
1412 break;
1413 case '\t':
43e526b9 1414 do_fputs ("\\t", stream);
c5aa993b
JM
1415 break;
1416 case '\f':
43e526b9 1417 do_fputs ("\\f", stream);
c5aa993b
JM
1418 break;
1419 case '\r':
43e526b9 1420 do_fputs ("\\r", stream);
c5aa993b
JM
1421 break;
1422 case '\033':
43e526b9 1423 do_fputs ("\\e", stream);
c5aa993b
JM
1424 break;
1425 case '\007':
43e526b9 1426 do_fputs ("\\a", stream);
c5aa993b
JM
1427 break;
1428 default:
43e526b9 1429 do_fprintf (stream, "\\%.3o", (unsigned int) c);
c5aa993b
JM
1430 break;
1431 }
1432 }
1433 else
1434 {
1435 if (c == '\\' || c == quoter)
43e526b9
JM
1436 do_fputs ("\\", stream);
1437 do_fprintf (stream, "%c", c);
c5aa993b 1438 }
c906108c 1439}
43e526b9
JM
1440
1441/* Print the character C on STREAM as part of the contents of a
1442 literal string whose delimiter is QUOTER. Note that these routines
1443 should only be call for printing things which are independent of
1444 the language of the program being debugged. */
1445
1446void
fba45db2 1447fputstr_filtered (const char *str, int quoter, struct ui_file *stream)
43e526b9
JM
1448{
1449 while (*str)
1450 printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter);
1451}
1452
1453void
fba45db2 1454fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream)
43e526b9
JM
1455{
1456 while (*str)
1457 printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1458}
1459
1460void
fba45db2 1461fputstrn_unfiltered (const char *str, int n, int quoter, struct ui_file *stream)
43e526b9
JM
1462{
1463 int i;
1464 for (i = 0; i < n; i++)
1465 printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter);
1466}
1467
c906108c 1468\f
c5aa993b 1469
c906108c
SS
1470/* Number of lines per page or UINT_MAX if paging is disabled. */
1471static unsigned int lines_per_page;
cbfbd72a 1472/* Number of chars per line or UINT_MAX if line folding is disabled. */
c906108c
SS
1473static unsigned int chars_per_line;
1474/* Current count of lines printed on this page, chars on this line. */
1475static unsigned int lines_printed, chars_printed;
1476
1477/* Buffer and start column of buffered text, for doing smarter word-
1478 wrapping. When someone calls wrap_here(), we start buffering output
1479 that comes through fputs_filtered(). If we see a newline, we just
1480 spit it out and forget about the wrap_here(). If we see another
1481 wrap_here(), we spit it out and remember the newer one. If we see
1482 the end of the line, we spit out a newline, the indent, and then
1483 the buffered output. */
1484
1485/* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1486 are waiting to be output (they have already been counted in chars_printed).
1487 When wrap_buffer[0] is null, the buffer is empty. */
1488static char *wrap_buffer;
1489
1490/* Pointer in wrap_buffer to the next character to fill. */
1491static char *wrap_pointer;
1492
1493/* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1494 is non-zero. */
1495static char *wrap_indent;
1496
1497/* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1498 is not in effect. */
1499static int wrap_column;
c906108c 1500\f
c5aa993b 1501
c906108c
SS
1502/* Inialize the lines and chars per page */
1503void
fba45db2 1504init_page_info (void)
c906108c
SS
1505{
1506#if defined(TUI)
c5aa993b 1507 if (tui_version && m_winPtrNotNull (cmdWin))
c906108c
SS
1508 {
1509 lines_per_page = cmdWin->generic.height;
1510 chars_per_line = cmdWin->generic.width;
1511 }
1512 else
1513#endif
1514 {
1515 /* These defaults will be used if we are unable to get the correct
1516 values from termcap. */
1517#if defined(__GO32__)
c5aa993b
JM
1518 lines_per_page = ScreenRows ();
1519 chars_per_line = ScreenCols ();
1520#else
c906108c
SS
1521 lines_per_page = 24;
1522 chars_per_line = 80;
1523
1524#if !defined (MPW) && !defined (_WIN32)
1525 /* No termcap under MPW, although might be cool to do something
1526 by looking at worksheet or console window sizes. */
1527 /* Initialize the screen height and width from termcap. */
1528 {
c5aa993b 1529 char *termtype = getenv ("TERM");
c906108c 1530
c5aa993b
JM
1531 /* Positive means success, nonpositive means failure. */
1532 int status;
c906108c 1533
c5aa993b
JM
1534 /* 2048 is large enough for all known terminals, according to the
1535 GNU termcap manual. */
1536 char term_buffer[2048];
c906108c 1537
c5aa993b
JM
1538 if (termtype)
1539 {
c906108c
SS
1540 status = tgetent (term_buffer, termtype);
1541 if (status > 0)
1542 {
c5aa993b 1543 int val;
c906108c 1544 int running_in_emacs = getenv ("EMACS") != NULL;
c5aa993b
JM
1545
1546 val = tgetnum ("li");
1547 if (val >= 0 && !running_in_emacs)
1548 lines_per_page = val;
1549 else
1550 /* The number of lines per page is not mentioned
c906108c
SS
1551 in the terminal description. This probably means
1552 that paging is not useful (e.g. emacs shell window),
1553 so disable paging. */
c5aa993b
JM
1554 lines_per_page = UINT_MAX;
1555
1556 val = tgetnum ("co");
1557 if (val >= 0)
1558 chars_per_line = val;
c906108c 1559 }
c5aa993b 1560 }
c906108c
SS
1561 }
1562#endif /* MPW */
1563
1564#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1565
1566 /* If there is a better way to determine the window size, use it. */
1567 SIGWINCH_HANDLER (SIGWINCH);
1568#endif
1569#endif
1570 /* If the output is not a terminal, don't paginate it. */
d9fcf2fb 1571 if (!ui_file_isatty (gdb_stdout))
c5aa993b
JM
1572 lines_per_page = UINT_MAX;
1573 } /* the command_line_version */
1574 set_width ();
c906108c
SS
1575}
1576
1577static void
fba45db2 1578set_width (void)
c906108c
SS
1579{
1580 if (chars_per_line == 0)
c5aa993b 1581 init_page_info ();
c906108c
SS
1582
1583 if (!wrap_buffer)
1584 {
1585 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1586 wrap_buffer[0] = '\0';
1587 }
1588 else
1589 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
c5aa993b 1590 wrap_pointer = wrap_buffer; /* Start it at the beginning */
c906108c
SS
1591}
1592
1593/* ARGSUSED */
c5aa993b 1594static void
fba45db2 1595set_width_command (char *args, int from_tty, struct cmd_list_element *c)
c906108c
SS
1596{
1597 set_width ();
1598}
1599
1600/* Wait, so the user can read what's on the screen. Prompt the user
1601 to continue by pressing RETURN. */
1602
1603static void
fba45db2 1604prompt_for_continue (void)
c906108c
SS
1605{
1606 char *ignore;
1607 char cont_prompt[120];
1608
1609 if (annotation_level > 1)
1610 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1611
1612 strcpy (cont_prompt,
1613 "---Type <return> to continue, or q <return> to quit---");
1614 if (annotation_level > 1)
1615 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1616
1617 /* We must do this *before* we call gdb_readline, else it will eventually
1618 call us -- thinking that we're trying to print beyond the end of the
1619 screen. */
1620 reinitialize_more_filter ();
1621
1622 immediate_quit++;
1623 /* On a real operating system, the user can quit with SIGINT.
1624 But not on GO32.
1625
1626 'q' is provided on all systems so users don't have to change habits
1627 from system to system, and because telling them what to do in
1628 the prompt is more user-friendly than expecting them to think of
1629 SIGINT. */
1630 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1631 whereas control-C to gdb_readline will cause the user to get dumped
1632 out to DOS. */
1633 ignore = readline (cont_prompt);
1634
1635 if (annotation_level > 1)
1636 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1637
1638 if (ignore)
1639 {
1640 char *p = ignore;
1641 while (*p == ' ' || *p == '\t')
1642 ++p;
1643 if (p[0] == 'q')
0f71a2f6 1644 {
6426a772 1645 if (!event_loop_p)
0f71a2f6
JM
1646 request_quit (SIGINT);
1647 else
c5aa993b 1648 async_request_quit (0);
0f71a2f6 1649 }
b8c9b27d 1650 xfree (ignore);
c906108c
SS
1651 }
1652 immediate_quit--;
1653
1654 /* Now we have to do this again, so that GDB will know that it doesn't
1655 need to save the ---Type <return>--- line at the top of the screen. */
1656 reinitialize_more_filter ();
1657
1658 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1659}
1660
1661/* Reinitialize filter; ie. tell it to reset to original values. */
1662
1663void
fba45db2 1664reinitialize_more_filter (void)
c906108c
SS
1665{
1666 lines_printed = 0;
1667 chars_printed = 0;
1668}
1669
1670/* Indicate that if the next sequence of characters overflows the line,
1671 a newline should be inserted here rather than when it hits the end.
1672 If INDENT is non-null, it is a string to be printed to indent the
1673 wrapped part on the next line. INDENT must remain accessible until
1674 the next call to wrap_here() or until a newline is printed through
1675 fputs_filtered().
1676
1677 If the line is already overfull, we immediately print a newline and
1678 the indentation, and disable further wrapping.
1679
1680 If we don't know the width of lines, but we know the page height,
1681 we must not wrap words, but should still keep track of newlines
1682 that were explicitly printed.
1683
1684 INDENT should not contain tabs, as that will mess up the char count
1685 on the next line. FIXME.
1686
1687 This routine is guaranteed to force out any output which has been
1688 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1689 used to force out output from the wrap_buffer. */
1690
1691void
fba45db2 1692wrap_here (char *indent)
c906108c
SS
1693{
1694 /* This should have been allocated, but be paranoid anyway. */
1695 if (!wrap_buffer)
e1e9e218 1696 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
1697
1698 if (wrap_buffer[0])
1699 {
1700 *wrap_pointer = '\0';
1701 fputs_unfiltered (wrap_buffer, gdb_stdout);
1702 }
1703 wrap_pointer = wrap_buffer;
1704 wrap_buffer[0] = '\0';
c5aa993b 1705 if (chars_per_line == UINT_MAX) /* No line overflow checking */
c906108c
SS
1706 {
1707 wrap_column = 0;
1708 }
1709 else if (chars_printed >= chars_per_line)
1710 {
1711 puts_filtered ("\n");
1712 if (indent != NULL)
1713 puts_filtered (indent);
1714 wrap_column = 0;
1715 }
1716 else
1717 {
1718 wrap_column = chars_printed;
1719 if (indent == NULL)
1720 wrap_indent = "";
1721 else
1722 wrap_indent = indent;
1723 }
1724}
1725
1726/* Ensure that whatever gets printed next, using the filtered output
1727 commands, starts at the beginning of the line. I.E. if there is
1728 any pending output for the current line, flush it and start a new
1729 line. Otherwise do nothing. */
1730
1731void
fba45db2 1732begin_line (void)
c906108c
SS
1733{
1734 if (chars_printed > 0)
1735 {
1736 puts_filtered ("\n");
1737 }
1738}
1739
ac9a91a7 1740
c906108c
SS
1741/* Like fputs but if FILTER is true, pause after every screenful.
1742
1743 Regardless of FILTER can wrap at points other than the final
1744 character of a line.
1745
1746 Unlike fputs, fputs_maybe_filtered does not return a value.
1747 It is OK for LINEBUFFER to be NULL, in which case just don't print
1748 anything.
1749
1750 Note that a longjmp to top level may occur in this routine (only if
1751 FILTER is true) (since prompt_for_continue may do so) so this
1752 routine should not be called when cleanups are not in place. */
1753
1754static void
fba45db2
KB
1755fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
1756 int filter)
c906108c
SS
1757{
1758 const char *lineptr;
1759
1760 if (linebuffer == 0)
1761 return;
1762
1763 /* Don't do any filtering if it is disabled. */
7a292a7a 1764 if ((stream != gdb_stdout) || !pagination_enabled
c5aa993b 1765 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
c906108c
SS
1766 {
1767 fputs_unfiltered (linebuffer, stream);
1768 return;
1769 }
1770
1771 /* Go through and output each character. Show line extension
1772 when this is necessary; prompt user for new page when this is
1773 necessary. */
c5aa993b 1774
c906108c
SS
1775 lineptr = linebuffer;
1776 while (*lineptr)
1777 {
1778 /* Possible new page. */
1779 if (filter &&
1780 (lines_printed >= lines_per_page - 1))
1781 prompt_for_continue ();
1782
1783 while (*lineptr && *lineptr != '\n')
1784 {
1785 /* Print a single line. */
1786 if (*lineptr == '\t')
1787 {
1788 if (wrap_column)
1789 *wrap_pointer++ = '\t';
1790 else
1791 fputc_unfiltered ('\t', stream);
1792 /* Shifting right by 3 produces the number of tab stops
1793 we have already passed, and then adding one and
c5aa993b 1794 shifting left 3 advances to the next tab stop. */
c906108c
SS
1795 chars_printed = ((chars_printed >> 3) + 1) << 3;
1796 lineptr++;
1797 }
1798 else
1799 {
1800 if (wrap_column)
1801 *wrap_pointer++ = *lineptr;
1802 else
c5aa993b 1803 fputc_unfiltered (*lineptr, stream);
c906108c
SS
1804 chars_printed++;
1805 lineptr++;
1806 }
c5aa993b 1807
c906108c
SS
1808 if (chars_printed >= chars_per_line)
1809 {
1810 unsigned int save_chars = chars_printed;
1811
1812 chars_printed = 0;
1813 lines_printed++;
1814 /* If we aren't actually wrapping, don't output newline --
c5aa993b
JM
1815 if chars_per_line is right, we probably just overflowed
1816 anyway; if it's wrong, let us keep going. */
c906108c
SS
1817 if (wrap_column)
1818 fputc_unfiltered ('\n', stream);
1819
1820 /* Possible new page. */
1821 if (lines_printed >= lines_per_page - 1)
1822 prompt_for_continue ();
1823
1824 /* Now output indentation and wrapped string */
1825 if (wrap_column)
1826 {
1827 fputs_unfiltered (wrap_indent, stream);
c5aa993b
JM
1828 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1829 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
c906108c
SS
1830 /* FIXME, this strlen is what prevents wrap_indent from
1831 containing tabs. However, if we recurse to print it
1832 and count its chars, we risk trouble if wrap_indent is
1833 longer than (the user settable) chars_per_line.
1834 Note also that this can set chars_printed > chars_per_line
1835 if we are printing a long string. */
1836 chars_printed = strlen (wrap_indent)
c5aa993b 1837 + (save_chars - wrap_column);
c906108c
SS
1838 wrap_pointer = wrap_buffer; /* Reset buffer */
1839 wrap_buffer[0] = '\0';
c5aa993b
JM
1840 wrap_column = 0; /* And disable fancy wrap */
1841 }
c906108c
SS
1842 }
1843 }
1844
1845 if (*lineptr == '\n')
1846 {
1847 chars_printed = 0;
c5aa993b 1848 wrap_here ((char *) 0); /* Spit out chars, cancel further wraps */
c906108c
SS
1849 lines_printed++;
1850 fputc_unfiltered ('\n', stream);
1851 lineptr++;
1852 }
1853 }
1854}
1855
1856void
fba45db2 1857fputs_filtered (const char *linebuffer, struct ui_file *stream)
c906108c
SS
1858{
1859 fputs_maybe_filtered (linebuffer, stream, 1);
1860}
1861
1862int
fba45db2 1863putchar_unfiltered (int c)
c906108c 1864{
11cf8741 1865 char buf = c;
d9fcf2fb 1866 ui_file_write (gdb_stdout, &buf, 1);
c906108c
SS
1867 return c;
1868}
1869
d1f4cff8
AC
1870/* Write character C to gdb_stdout using GDB's paging mechanism and return C.
1871 May return nonlocally. */
1872
1873int
1874putchar_filtered (int c)
1875{
1876 return fputc_filtered (c, gdb_stdout);
1877}
1878
c906108c 1879int
fba45db2 1880fputc_unfiltered (int c, struct ui_file *stream)
c906108c 1881{
11cf8741 1882 char buf = c;
d9fcf2fb 1883 ui_file_write (stream, &buf, 1);
c906108c
SS
1884 return c;
1885}
1886
1887int
fba45db2 1888fputc_filtered (int c, struct ui_file *stream)
c906108c
SS
1889{
1890 char buf[2];
1891
1892 buf[0] = c;
1893 buf[1] = 0;
1894 fputs_filtered (buf, stream);
1895 return c;
1896}
1897
1898/* puts_debug is like fputs_unfiltered, except it prints special
1899 characters in printable fashion. */
1900
1901void
fba45db2 1902puts_debug (char *prefix, char *string, char *suffix)
c906108c
SS
1903{
1904 int ch;
1905
1906 /* Print prefix and suffix after each line. */
1907 static int new_line = 1;
1908 static int return_p = 0;
1909 static char *prev_prefix = "";
1910 static char *prev_suffix = "";
1911
1912 if (*string == '\n')
1913 return_p = 0;
1914
1915 /* If the prefix is changing, print the previous suffix, a new line,
1916 and the new prefix. */
c5aa993b 1917 if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line)
c906108c 1918 {
9846de1b
JM
1919 fputs_unfiltered (prev_suffix, gdb_stdlog);
1920 fputs_unfiltered ("\n", gdb_stdlog);
1921 fputs_unfiltered (prefix, gdb_stdlog);
c906108c
SS
1922 }
1923
1924 /* Print prefix if we printed a newline during the previous call. */
1925 if (new_line)
1926 {
1927 new_line = 0;
9846de1b 1928 fputs_unfiltered (prefix, gdb_stdlog);
c906108c
SS
1929 }
1930
1931 prev_prefix = prefix;
1932 prev_suffix = suffix;
1933
1934 /* Output characters in a printable format. */
1935 while ((ch = *string++) != '\0')
1936 {
1937 switch (ch)
c5aa993b 1938 {
c906108c
SS
1939 default:
1940 if (isprint (ch))
9846de1b 1941 fputc_unfiltered (ch, gdb_stdlog);
c906108c
SS
1942
1943 else
9846de1b 1944 fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff);
c906108c
SS
1945 break;
1946
c5aa993b
JM
1947 case '\\':
1948 fputs_unfiltered ("\\\\", gdb_stdlog);
1949 break;
1950 case '\b':
1951 fputs_unfiltered ("\\b", gdb_stdlog);
1952 break;
1953 case '\f':
1954 fputs_unfiltered ("\\f", gdb_stdlog);
1955 break;
1956 case '\n':
1957 new_line = 1;
1958 fputs_unfiltered ("\\n", gdb_stdlog);
1959 break;
1960 case '\r':
1961 fputs_unfiltered ("\\r", gdb_stdlog);
1962 break;
1963 case '\t':
1964 fputs_unfiltered ("\\t", gdb_stdlog);
1965 break;
1966 case '\v':
1967 fputs_unfiltered ("\\v", gdb_stdlog);
1968 break;
1969 }
c906108c
SS
1970
1971 return_p = ch == '\r';
1972 }
1973
1974 /* Print suffix if we printed a newline. */
1975 if (new_line)
1976 {
9846de1b
JM
1977 fputs_unfiltered (suffix, gdb_stdlog);
1978 fputs_unfiltered ("\n", gdb_stdlog);
c906108c
SS
1979 }
1980}
1981
1982
1983/* Print a variable number of ARGS using format FORMAT. If this
1984 information is going to put the amount written (since the last call
1985 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1986 call prompt_for_continue to get the users permision to continue.
1987
1988 Unlike fprintf, this function does not return a value.
1989
1990 We implement three variants, vfprintf (takes a vararg list and stream),
1991 fprintf (takes a stream to write on), and printf (the usual).
1992
1993 Note also that a longjmp to top level may occur in this routine
1994 (since prompt_for_continue may do so) so this routine should not be
1995 called when cleanups are not in place. */
1996
1997static void
fba45db2
KB
1998vfprintf_maybe_filtered (struct ui_file *stream, const char *format,
1999 va_list args, int filter)
c906108c
SS
2000{
2001 char *linebuffer;
2002 struct cleanup *old_cleanups;
2003
76995688 2004 xvasprintf (&linebuffer, format, args);
b8c9b27d 2005 old_cleanups = make_cleanup (xfree, linebuffer);
c906108c
SS
2006 fputs_maybe_filtered (linebuffer, stream, filter);
2007 do_cleanups (old_cleanups);
2008}
2009
2010
2011void
fba45db2 2012vfprintf_filtered (struct ui_file *stream, const char *format, va_list args)
c906108c
SS
2013{
2014 vfprintf_maybe_filtered (stream, format, args, 1);
2015}
2016
2017void
fba45db2 2018vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
c906108c
SS
2019{
2020 char *linebuffer;
2021 struct cleanup *old_cleanups;
2022
76995688 2023 xvasprintf (&linebuffer, format, args);
b8c9b27d 2024 old_cleanups = make_cleanup (xfree, linebuffer);
c906108c
SS
2025 fputs_unfiltered (linebuffer, stream);
2026 do_cleanups (old_cleanups);
2027}
2028
2029void
fba45db2 2030vprintf_filtered (const char *format, va_list args)
c906108c
SS
2031{
2032 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
2033}
2034
2035void
fba45db2 2036vprintf_unfiltered (const char *format, va_list args)
c906108c
SS
2037{
2038 vfprintf_unfiltered (gdb_stdout, format, args);
2039}
2040
c906108c 2041void
d9fcf2fb 2042fprintf_filtered (struct ui_file * stream, const char *format,...)
c906108c
SS
2043{
2044 va_list args;
c906108c 2045 va_start (args, format);
c906108c
SS
2046 vfprintf_filtered (stream, format, args);
2047 va_end (args);
2048}
2049
c906108c 2050void
d9fcf2fb 2051fprintf_unfiltered (struct ui_file * stream, const char *format,...)
c906108c
SS
2052{
2053 va_list args;
c906108c 2054 va_start (args, format);
c906108c
SS
2055 vfprintf_unfiltered (stream, format, args);
2056 va_end (args);
2057}
2058
2059/* Like fprintf_filtered, but prints its result indented.
2060 Called as fprintfi_filtered (spaces, stream, format, ...); */
2061
c906108c 2062void
d9fcf2fb 2063fprintfi_filtered (int spaces, struct ui_file * stream, const char *format,...)
c906108c
SS
2064{
2065 va_list args;
c906108c 2066 va_start (args, format);
c906108c
SS
2067 print_spaces_filtered (spaces, stream);
2068
2069 vfprintf_filtered (stream, format, args);
2070 va_end (args);
2071}
2072
2073
c906108c 2074void
c5aa993b 2075printf_filtered (const char *format,...)
c906108c
SS
2076{
2077 va_list args;
c906108c 2078 va_start (args, format);
c906108c
SS
2079 vfprintf_filtered (gdb_stdout, format, args);
2080 va_end (args);
2081}
2082
2083
c906108c 2084void
c5aa993b 2085printf_unfiltered (const char *format,...)
c906108c
SS
2086{
2087 va_list args;
c906108c 2088 va_start (args, format);
c906108c
SS
2089 vfprintf_unfiltered (gdb_stdout, format, args);
2090 va_end (args);
2091}
2092
2093/* Like printf_filtered, but prints it's result indented.
2094 Called as printfi_filtered (spaces, format, ...); */
2095
c906108c 2096void
c5aa993b 2097printfi_filtered (int spaces, const char *format,...)
c906108c
SS
2098{
2099 va_list args;
c906108c 2100 va_start (args, format);
c906108c
SS
2101 print_spaces_filtered (spaces, gdb_stdout);
2102 vfprintf_filtered (gdb_stdout, format, args);
2103 va_end (args);
2104}
2105
2106/* Easy -- but watch out!
2107
2108 This routine is *not* a replacement for puts()! puts() appends a newline.
2109 This one doesn't, and had better not! */
2110
2111void
fba45db2 2112puts_filtered (const char *string)
c906108c
SS
2113{
2114 fputs_filtered (string, gdb_stdout);
2115}
2116
2117void
fba45db2 2118puts_unfiltered (const char *string)
c906108c
SS
2119{
2120 fputs_unfiltered (string, gdb_stdout);
2121}
2122
2123/* Return a pointer to N spaces and a null. The pointer is good
2124 until the next call to here. */
2125char *
fba45db2 2126n_spaces (int n)
c906108c 2127{
392a587b
JM
2128 char *t;
2129 static char *spaces = 0;
2130 static int max_spaces = -1;
c906108c
SS
2131
2132 if (n > max_spaces)
2133 {
2134 if (spaces)
b8c9b27d 2135 xfree (spaces);
c5aa993b
JM
2136 spaces = (char *) xmalloc (n + 1);
2137 for (t = spaces + n; t != spaces;)
c906108c
SS
2138 *--t = ' ';
2139 spaces[n] = '\0';
2140 max_spaces = n;
2141 }
2142
2143 return spaces + max_spaces - n;
2144}
2145
2146/* Print N spaces. */
2147void
fba45db2 2148print_spaces_filtered (int n, struct ui_file *stream)
c906108c
SS
2149{
2150 fputs_filtered (n_spaces (n), stream);
2151}
2152\f
2153/* C++ demangler stuff. */
2154
2155/* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
2156 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
2157 If the name is not mangled, or the language for the name is unknown, or
2158 demangling is off, the name is printed in its "raw" form. */
2159
2160void
fba45db2
KB
2161fprintf_symbol_filtered (struct ui_file *stream, char *name, enum language lang,
2162 int arg_mode)
c906108c
SS
2163{
2164 char *demangled;
2165
2166 if (name != NULL)
2167 {
2168 /* If user wants to see raw output, no problem. */
2169 if (!demangle)
2170 {
2171 fputs_filtered (name, stream);
2172 }
2173 else
2174 {
2175 switch (lang)
2176 {
2177 case language_cplus:
2178 demangled = cplus_demangle (name, arg_mode);
2179 break;
2180 case language_java:
2181 demangled = cplus_demangle (name, arg_mode | DMGL_JAVA);
2182 break;
2183 case language_chill:
2184 demangled = chill_demangle (name);
2185 break;
2186 default:
2187 demangled = NULL;
2188 break;
2189 }
2190 fputs_filtered (demangled ? demangled : name, stream);
2191 if (demangled != NULL)
2192 {
b8c9b27d 2193 xfree (demangled);
c906108c
SS
2194 }
2195 }
2196 }
2197}
2198
2199/* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
2200 differences in whitespace. Returns 0 if they match, non-zero if they
2201 don't (slightly different than strcmp()'s range of return values).
c5aa993b 2202
c906108c
SS
2203 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2204 This "feature" is useful when searching for matching C++ function names
2205 (such as if the user types 'break FOO', where FOO is a mangled C++
2206 function). */
2207
2208int
fba45db2 2209strcmp_iw (const char *string1, const char *string2)
c906108c
SS
2210{
2211 while ((*string1 != '\0') && (*string2 != '\0'))
2212 {
2213 while (isspace (*string1))
2214 {
2215 string1++;
2216 }
2217 while (isspace (*string2))
2218 {
2219 string2++;
2220 }
2221 if (*string1 != *string2)
2222 {
2223 break;
2224 }
2225 if (*string1 != '\0')
2226 {
2227 string1++;
2228 string2++;
2229 }
2230 }
2231 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
2232}
c906108c 2233\f
c5aa993b 2234
c906108c 2235/*
c5aa993b
JM
2236 ** subset_compare()
2237 ** Answer whether string_to_compare is a full or partial match to
2238 ** template_string. The partial match must be in sequence starting
2239 ** at index 0.
2240 */
c906108c 2241int
fba45db2 2242subset_compare (char *string_to_compare, char *template_string)
7a292a7a
SS
2243{
2244 int match;
c5aa993b
JM
2245 if (template_string != (char *) NULL && string_to_compare != (char *) NULL &&
2246 strlen (string_to_compare) <= strlen (template_string))
2247 match = (strncmp (template_string,
2248 string_to_compare,
2249 strlen (string_to_compare)) == 0);
7a292a7a
SS
2250 else
2251 match = 0;
2252 return match;
2253}
c906108c
SS
2254
2255
a14ed312 2256static void pagination_on_command (char *arg, int from_tty);
7a292a7a 2257static void
fba45db2 2258pagination_on_command (char *arg, int from_tty)
c906108c
SS
2259{
2260 pagination_enabled = 1;
2261}
2262
a14ed312 2263static void pagination_on_command (char *arg, int from_tty);
7a292a7a 2264static void
fba45db2 2265pagination_off_command (char *arg, int from_tty)
c906108c
SS
2266{
2267 pagination_enabled = 0;
2268}
c906108c 2269\f
c5aa993b 2270
c906108c 2271void
fba45db2 2272initialize_utils (void)
c906108c
SS
2273{
2274 struct cmd_list_element *c;
2275
c5aa993b
JM
2276 c = add_set_cmd ("width", class_support, var_uinteger,
2277 (char *) &chars_per_line,
2278 "Set number of characters gdb thinks are in a line.",
2279 &setlist);
c906108c
SS
2280 add_show_from_set (c, &showlist);
2281 c->function.sfunc = set_width_command;
2282
2283 add_show_from_set
2284 (add_set_cmd ("height", class_support,
c5aa993b 2285 var_uinteger, (char *) &lines_per_page,
c906108c
SS
2286 "Set number of lines gdb thinks are in a page.", &setlist),
2287 &showlist);
c5aa993b 2288
c906108c
SS
2289 init_page_info ();
2290
2291 /* If the output is not a terminal, don't paginate it. */
d9fcf2fb 2292 if (!ui_file_isatty (gdb_stdout))
c906108c
SS
2293 lines_per_page = UINT_MAX;
2294
c5aa993b 2295 set_width_command ((char *) NULL, 0, c);
c906108c
SS
2296
2297 add_show_from_set
c5aa993b
JM
2298 (add_set_cmd ("demangle", class_support, var_boolean,
2299 (char *) &demangle,
2300 "Set demangling of encoded C++ names when displaying symbols.",
c906108c
SS
2301 &setprintlist),
2302 &showprintlist);
2303
2304 add_show_from_set
2305 (add_set_cmd ("pagination", class_support,
c5aa993b 2306 var_boolean, (char *) &pagination_enabled,
c906108c
SS
2307 "Set state of pagination.", &setlist),
2308 &showlist);
4261bedc 2309
c906108c
SS
2310 if (xdb_commands)
2311 {
c5aa993b
JM
2312 add_com ("am", class_support, pagination_on_command,
2313 "Enable pagination");
2314 add_com ("sm", class_support, pagination_off_command,
2315 "Disable pagination");
c906108c
SS
2316 }
2317
2318 add_show_from_set
c5aa993b
JM
2319 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
2320 (char *) &sevenbit_strings,
2321 "Set printing of 8-bit characters in strings as \\nnn.",
c906108c
SS
2322 &setprintlist),
2323 &showprintlist);
2324
2325 add_show_from_set
c5aa993b
JM
2326 (add_set_cmd ("asm-demangle", class_support, var_boolean,
2327 (char *) &asm_demangle,
2328 "Set demangling of C++ names in disassembly listings.",
c906108c
SS
2329 &setprintlist),
2330 &showprintlist);
2331}
2332
2333/* Machine specific function to handle SIGWINCH signal. */
2334
2335#ifdef SIGWINCH_HANDLER_BODY
c5aa993b 2336SIGWINCH_HANDLER_BODY
c906108c
SS
2337#endif
2338\f
2339/* Support for converting target fp numbers into host DOUBLEST format. */
2340
2341/* XXX - This code should really be in libiberty/floatformat.c, however
2342 configuration issues with libiberty made this very difficult to do in the
2343 available time. */
2344
2345#include "floatformat.h"
2346#include <math.h> /* ldexp */
2347
2348/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
2349 going to bother with trying to muck around with whether it is defined in
2350 a system header, what we do if not, etc. */
2351#define FLOATFORMAT_CHAR_BIT 8
2352
a14ed312
KB
2353static unsigned long get_field (unsigned char *,
2354 enum floatformat_byteorders,
2355 unsigned int, unsigned int, unsigned int);
c906108c
SS
2356
2357/* Extract a field which starts at START and is LEN bytes long. DATA and
2358 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2359static unsigned long
fba45db2
KB
2360get_field (unsigned char *data, enum floatformat_byteorders order,
2361 unsigned int total_len, unsigned int start, unsigned int len)
c906108c
SS
2362{
2363 unsigned long result;
2364 unsigned int cur_byte;
2365 int cur_bitshift;
2366
2367 /* Start at the least significant part of the field. */
c906108c 2368 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
0fda6bd2
JM
2369 {
2370 /* We start counting from the other end (i.e, from the high bytes
2371 rather than the low bytes). As such, we need to be concerned
2372 with what happens if bit 0 doesn't start on a byte boundary.
2373 I.e, we need to properly handle the case where total_len is
2374 not evenly divisible by 8. So we compute ``excess'' which
2375 represents the number of bits from the end of our starting
2376 byte needed to get to bit 0. */
2377 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
2378 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
2379 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
2380 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
2381 - FLOATFORMAT_CHAR_BIT;
2382 }
2383 else
2384 {
2385 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
2386 cur_bitshift =
2387 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
2388 }
2389 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
2390 result = *(data + cur_byte) >> (-cur_bitshift);
2391 else
2392 result = 0;
c906108c
SS
2393 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2394 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2395 ++cur_byte;
2396 else
2397 --cur_byte;
2398
2399 /* Move towards the most significant part of the field. */
2400 while (cur_bitshift < len)
2401 {
0fda6bd2 2402 result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
c906108c
SS
2403 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2404 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2405 ++cur_byte;
2406 else
2407 --cur_byte;
2408 }
0fda6bd2
JM
2409 if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
2410 /* Mask out bits which are not part of the field */
2411 result &= ((1UL << len) - 1);
c906108c
SS
2412 return result;
2413}
c5aa993b 2414
c906108c
SS
2415/* Convert from FMT to a DOUBLEST.
2416 FROM is the address of the extended float.
2417 Store the DOUBLEST in *TO. */
2418
2419void
fba45db2
KB
2420floatformat_to_doublest (const struct floatformat *fmt, char *from,
2421 DOUBLEST *to)
c906108c 2422{
c5aa993b 2423 unsigned char *ufrom = (unsigned char *) from;
c906108c
SS
2424 DOUBLEST dto;
2425 long exponent;
2426 unsigned long mant;
2427 unsigned int mant_bits, mant_off;
2428 int mant_bits_left;
2429 int special_exponent; /* It's a NaN, denorm or zero */
2430
2431 /* If the mantissa bits are not contiguous from one end of the
2432 mantissa to the other, we need to make a private copy of the
2433 source bytes that is in the right order since the unpacking
2434 algorithm assumes that the bits are contiguous.
2435
2436 Swap the bytes individually rather than accessing them through
2437 "long *" since we have no guarantee that they start on a long
2438 alignment, and also sizeof(long) for the host could be different
2439 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
2440 for the target is 4. */
2441
c5aa993b 2442 if (fmt->byteorder == floatformat_littlebyte_bigword)
c906108c
SS
2443 {
2444 static unsigned char *newfrom;
2445 unsigned char *swapin, *swapout;
2446 int longswaps;
2447
c5aa993b 2448 longswaps = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
c906108c 2449 longswaps >>= 3;
c5aa993b 2450
c906108c
SS
2451 if (newfrom == NULL)
2452 {
c5aa993b 2453 newfrom = (unsigned char *) xmalloc (fmt->totalsize);
c906108c
SS
2454 }
2455 swapout = newfrom;
2456 swapin = ufrom;
2457 ufrom = newfrom;
2458 while (longswaps-- > 0)
2459 {
2460 /* This is ugly, but efficient */
2461 *swapout++ = swapin[4];
2462 *swapout++ = swapin[5];
2463 *swapout++ = swapin[6];
2464 *swapout++ = swapin[7];
2465 *swapout++ = swapin[0];
2466 *swapout++ = swapin[1];
2467 *swapout++ = swapin[2];
2468 *swapout++ = swapin[3];
2469 swapin += 8;
2470 }
2471 }
2472
2473 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
2474 fmt->exp_start, fmt->exp_len);
2475 /* Note that if exponent indicates a NaN, we can't really do anything useful
2476 (not knowing if the host has NaN's, or how to build one). So it will
2477 end up as an infinity or something close; that is OK. */
2478
2479 mant_bits_left = fmt->man_len;
2480 mant_off = fmt->man_start;
2481 dto = 0.0;
2482
2483 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
2484
11cf8741
JM
2485/* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
2486 we don't check for zero as the exponent doesn't matter. */
c906108c
SS
2487 if (!special_exponent)
2488 exponent -= fmt->exp_bias;
11cf8741
JM
2489 else if (exponent == 0)
2490 exponent = 1 - fmt->exp_bias;
c906108c
SS
2491
2492 /* Build the result algebraically. Might go infinite, underflow, etc;
2493 who cares. */
2494
2495/* If this format uses a hidden bit, explicitly add it in now. Otherwise,
2496 increment the exponent by one to account for the integer bit. */
2497
2498 if (!special_exponent)
7a292a7a
SS
2499 {
2500 if (fmt->intbit == floatformat_intbit_no)
2501 dto = ldexp (1.0, exponent);
2502 else
2503 exponent++;
2504 }
c906108c
SS
2505
2506 while (mant_bits_left > 0)
2507 {
2508 mant_bits = min (mant_bits_left, 32);
2509
2510 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
c5aa993b 2511 mant_off, mant_bits);
c906108c 2512
c5aa993b 2513 dto += ldexp ((double) mant, exponent - mant_bits);
c906108c
SS
2514 exponent -= mant_bits;
2515 mant_off += mant_bits;
2516 mant_bits_left -= mant_bits;
2517 }
2518
2519 /* Negate it if negative. */
2520 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
2521 dto = -dto;
2522 *to = dto;
2523}
2524\f
a14ed312
KB
2525static void put_field (unsigned char *, enum floatformat_byteorders,
2526 unsigned int,
2527 unsigned int, unsigned int, unsigned long);
c906108c
SS
2528
2529/* Set a field which starts at START and is LEN bytes long. DATA and
2530 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
2531static void
fba45db2
KB
2532put_field (unsigned char *data, enum floatformat_byteorders order,
2533 unsigned int total_len, unsigned int start, unsigned int len,
2534 unsigned long stuff_to_put)
c906108c
SS
2535{
2536 unsigned int cur_byte;
2537 int cur_bitshift;
2538
2539 /* Start at the least significant part of the field. */
c906108c 2540 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
0fda6bd2
JM
2541 {
2542 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
2543 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
2544 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
2545 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
2546 - FLOATFORMAT_CHAR_BIT;
2547 }
2548 else
2549 {
2550 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
2551 cur_bitshift =
2552 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
2553 }
2554 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
2555 {
2556 *(data + cur_byte) &=
2557 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
2558 << (-cur_bitshift));
2559 *(data + cur_byte) |=
2560 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
2561 }
c906108c
SS
2562 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2563 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2564 ++cur_byte;
2565 else
2566 --cur_byte;
2567
2568 /* Move towards the most significant part of the field. */
2569 while (cur_bitshift < len)
2570 {
2571 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
2572 {
2573 /* This is the last byte. */
2574 *(data + cur_byte) &=
2575 ~((1 << (len - cur_bitshift)) - 1);
2576 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
2577 }
2578 else
2579 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
2580 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
2581 cur_bitshift += FLOATFORMAT_CHAR_BIT;
2582 if (order == floatformat_little || order == floatformat_littlebyte_bigword)
2583 ++cur_byte;
2584 else
2585 --cur_byte;
2586 }
2587}
2588
2589#ifdef HAVE_LONG_DOUBLE
2590/* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
2591 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
2592 frexp, but operates on the long double data type. */
2593
a14ed312 2594static long double ldfrexp (long double value, int *eptr);
c906108c
SS
2595
2596static long double
fba45db2 2597ldfrexp (long double value, int *eptr)
c906108c
SS
2598{
2599 long double tmp;
2600 int exp;
2601
2602 /* Unfortunately, there are no portable functions for extracting the exponent
2603 of a long double, so we have to do it iteratively by multiplying or dividing
2604 by two until the fraction is between 0.5 and 1.0. */
2605
2606 if (value < 0.0l)
2607 value = -value;
2608
2609 tmp = 1.0l;
2610 exp = 0;
2611
2612 if (value >= tmp) /* Value >= 1.0 */
2613 while (value >= tmp)
2614 {
2615 tmp *= 2.0l;
2616 exp++;
2617 }
2618 else if (value != 0.0l) /* Value < 1.0 and > 0.0 */
2619 {
2620 while (value < tmp)
2621 {
2622 tmp /= 2.0l;
2623 exp--;
2624 }
2625 tmp *= 2.0l;
2626 exp++;
2627 }
2628
2629 *eptr = exp;
c5aa993b 2630 return value / tmp;
c906108c
SS
2631}
2632#endif /* HAVE_LONG_DOUBLE */
2633
2634
2635/* The converse: convert the DOUBLEST *FROM to an extended float
2636 and store where TO points. Neither FROM nor TO have any alignment
2637 restrictions. */
2638
2639void
fba45db2
KB
2640floatformat_from_doublest (CONST struct floatformat *fmt, DOUBLEST *from,
2641 char *to)
c906108c
SS
2642{
2643 DOUBLEST dfrom;
2644 int exponent;
2645 DOUBLEST mant;
2646 unsigned int mant_bits, mant_off;
2647 int mant_bits_left;
c5aa993b 2648 unsigned char *uto = (unsigned char *) to;
c906108c
SS
2649
2650 memcpy (&dfrom, from, sizeof (dfrom));
ba8966d6
KB
2651 memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
2652 / FLOATFORMAT_CHAR_BIT);
c906108c
SS
2653 if (dfrom == 0)
2654 return; /* Result is zero */
2655 if (dfrom != dfrom) /* Result is NaN */
2656 {
2657 /* From is NaN */
2658 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
2659 fmt->exp_len, fmt->exp_nan);
2660 /* Be sure it's not infinity, but NaN value is irrel */
2661 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
2662 32, 1);
2663 return;
2664 }
2665
2666 /* If negative, set the sign bit. */
2667 if (dfrom < 0)
2668 {
2669 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
2670 dfrom = -dfrom;
2671 }
2672
2673 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity */
2674 {
2675 /* Infinity exponent is same as NaN's. */
2676 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
2677 fmt->exp_len, fmt->exp_nan);
2678 /* Infinity mantissa is all zeroes. */
2679 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
2680 fmt->man_len, 0);
2681 return;
2682 }
2683
2684#ifdef HAVE_LONG_DOUBLE
2685 mant = ldfrexp (dfrom, &exponent);
2686#else
2687 mant = frexp (dfrom, &exponent);
2688#endif
2689
2690 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
2691 exponent + fmt->exp_bias - 1);
2692
2693 mant_bits_left = fmt->man_len;
2694 mant_off = fmt->man_start;
2695 while (mant_bits_left > 0)
2696 {
2697 unsigned long mant_long;
2698 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
2699
2700 mant *= 4294967296.0;
ba8966d6 2701 mant_long = ((unsigned long) mant) & 0xffffffffL;
c906108c
SS
2702 mant -= mant_long;
2703
2704 /* If the integer bit is implicit, then we need to discard it.
c5aa993b
JM
2705 If we are discarding a zero, we should be (but are not) creating
2706 a denormalized number which means adjusting the exponent
2707 (I think). */
c906108c
SS
2708 if (mant_bits_left == fmt->man_len
2709 && fmt->intbit == floatformat_intbit_no)
2710 {
2711 mant_long <<= 1;
ba8966d6 2712 mant_long &= 0xffffffffL;
c906108c
SS
2713 mant_bits -= 1;
2714 }
2715
2716 if (mant_bits < 32)
2717 {
2718 /* The bits we want are in the most significant MANT_BITS bits of
2719 mant_long. Move them to the least significant. */
2720 mant_long >>= 32 - mant_bits;
2721 }
2722
2723 put_field (uto, fmt->byteorder, fmt->totalsize,
2724 mant_off, mant_bits, mant_long);
2725 mant_off += mant_bits;
2726 mant_bits_left -= mant_bits;
2727 }
c5aa993b 2728 if (fmt->byteorder == floatformat_littlebyte_bigword)
c906108c
SS
2729 {
2730 int count;
2731 unsigned char *swaplow = uto;
2732 unsigned char *swaphigh = uto + 4;
2733 unsigned char tmp;
2734
2735 for (count = 0; count < 4; count++)
2736 {
2737 tmp = *swaplow;
2738 *swaplow++ = *swaphigh;
2739 *swaphigh++ = tmp;
2740 }
2741 }
2742}
2743
5683e87a
AC
2744/* print routines to handle variable size regs, etc. */
2745
c906108c
SS
2746/* temporary storage using circular buffer */
2747#define NUMCELLS 16
2748#define CELLSIZE 32
c5aa993b 2749static char *
fba45db2 2750get_cell (void)
c906108c
SS
2751{
2752 static char buf[NUMCELLS][CELLSIZE];
c5aa993b
JM
2753 static int cell = 0;
2754 if (++cell >= NUMCELLS)
2755 cell = 0;
c906108c
SS
2756 return buf[cell];
2757}
2758
d4f3574e
SS
2759int
2760strlen_paddr (void)
2761{
79496e2f 2762 return (TARGET_ADDR_BIT / 8 * 2);
d4f3574e
SS
2763}
2764
c5aa993b 2765char *
104c1213 2766paddr (CORE_ADDR addr)
c906108c 2767{
79496e2f 2768 return phex (addr, TARGET_ADDR_BIT / 8);
c906108c
SS
2769}
2770
c5aa993b 2771char *
104c1213 2772paddr_nz (CORE_ADDR addr)
c906108c 2773{
79496e2f 2774 return phex_nz (addr, TARGET_ADDR_BIT / 8);
c906108c
SS
2775}
2776
104c1213
JM
2777static void
2778decimal2str (char *paddr_str, char *sign, ULONGEST addr)
2779{
2780 /* steal code from valprint.c:print_decimal(). Should this worry
2781 about the real size of addr as the above does? */
2782 unsigned long temp[3];
2783 int i = 0;
2784 do
2785 {
2786 temp[i] = addr % (1000 * 1000 * 1000);
2787 addr /= (1000 * 1000 * 1000);
2788 i++;
2789 }
2790 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
2791 switch (i)
2792 {
2793 case 1:
2794 sprintf (paddr_str, "%s%lu",
2795 sign, temp[0]);
2796 break;
2797 case 2:
2798 sprintf (paddr_str, "%s%lu%09lu",
2799 sign, temp[1], temp[0]);
2800 break;
2801 case 3:
2802 sprintf (paddr_str, "%s%lu%09lu%09lu",
2803 sign, temp[2], temp[1], temp[0]);
2804 break;
2805 default:
e1e9e218 2806 internal_error (__FILE__, __LINE__, "failed internal consistency check");
104c1213
JM
2807 }
2808}
2809
2810char *
2811paddr_u (CORE_ADDR addr)
2812{
2813 char *paddr_str = get_cell ();
2814 decimal2str (paddr_str, "", addr);
2815 return paddr_str;
2816}
2817
2818char *
2819paddr_d (LONGEST addr)
2820{
2821 char *paddr_str = get_cell ();
2822 if (addr < 0)
2823 decimal2str (paddr_str, "-", -addr);
2824 else
2825 decimal2str (paddr_str, "", addr);
2826 return paddr_str;
2827}
2828
5683e87a
AC
2829/* eliminate warning from compiler on 32-bit systems */
2830static int thirty_two = 32;
2831
104c1213 2832char *
5683e87a 2833phex (ULONGEST l, int sizeof_l)
104c1213 2834{
5683e87a
AC
2835 char *str = get_cell ();
2836 switch (sizeof_l)
104c1213
JM
2837 {
2838 case 8:
5683e87a
AC
2839 sprintf (str, "%08lx%08lx",
2840 (unsigned long) (l >> thirty_two),
2841 (unsigned long) (l & 0xffffffff));
104c1213
JM
2842 break;
2843 case 4:
5683e87a 2844 sprintf (str, "%08lx", (unsigned long) l);
104c1213
JM
2845 break;
2846 case 2:
5683e87a 2847 sprintf (str, "%04x", (unsigned short) (l & 0xffff));
104c1213
JM
2848 break;
2849 default:
5683e87a
AC
2850 phex (l, sizeof (l));
2851 break;
104c1213 2852 }
5683e87a 2853 return str;
104c1213
JM
2854}
2855
c5aa993b 2856char *
5683e87a 2857phex_nz (ULONGEST l, int sizeof_l)
c906108c 2858{
5683e87a
AC
2859 char *str = get_cell ();
2860 switch (sizeof_l)
c906108c 2861 {
c5aa993b
JM
2862 case 8:
2863 {
5683e87a 2864 unsigned long high = (unsigned long) (l >> thirty_two);
c5aa993b 2865 if (high == 0)
5683e87a 2866 sprintf (str, "%lx", (unsigned long) (l & 0xffffffff));
c5aa993b 2867 else
5683e87a
AC
2868 sprintf (str, "%lx%08lx",
2869 high, (unsigned long) (l & 0xffffffff));
c906108c 2870 break;
c5aa993b
JM
2871 }
2872 case 4:
5683e87a 2873 sprintf (str, "%lx", (unsigned long) l);
c5aa993b
JM
2874 break;
2875 case 2:
5683e87a 2876 sprintf (str, "%x", (unsigned short) (l & 0xffff));
c5aa993b
JM
2877 break;
2878 default:
5683e87a
AC
2879 phex_nz (l, sizeof (l));
2880 break;
c906108c 2881 }
5683e87a 2882 return str;
c906108c 2883}
ac2e2ef7
AC
2884
2885
2886/* Convert to / from the hosts pointer to GDB's internal CORE_ADDR
2887 using the target's conversion routines. */
2888CORE_ADDR
2889host_pointer_to_address (void *ptr)
2890{
2891 if (sizeof (ptr) != TYPE_LENGTH (builtin_type_ptr))
8e65ff28
AC
2892 internal_error (__FILE__, __LINE__,
2893 "core_addr_to_void_ptr: bad cast");
ac2e2ef7
AC
2894 return POINTER_TO_ADDRESS (builtin_type_ptr, &ptr);
2895}
2896
2897void *
2898address_to_host_pointer (CORE_ADDR addr)
2899{
2900 void *ptr;
2901 if (sizeof (ptr) != TYPE_LENGTH (builtin_type_ptr))
8e65ff28
AC
2902 internal_error (__FILE__, __LINE__,
2903 "core_addr_to_void_ptr: bad cast");
ac2e2ef7
AC
2904 ADDRESS_TO_POINTER (builtin_type_ptr, &ptr, addr);
2905 return ptr;
2906}