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