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