* breakpoint.c, breakpoint.h (breakpoint_init_inferior): New function
[deliverable/binutils-gdb.git] / gdb / utils.c
CommitLineData
bd5635a1 1/* General utility routines for GDB, the GNU debugger.
7919c3ed 2 Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
351b221d 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
351b221d
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
351b221d 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
351b221d
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
d747e0af 20#include "defs.h"
51b57ded 21#if !defined(__GO32__)
bd5635a1
RP
22#include <sys/ioctl.h>
23#include <sys/param.h>
24#include <pwd.h>
51b57ded 25#endif
2bc2e684
FF
26#include <varargs.h>
27#include <ctype.h>
28#include <string.h>
29
bd5635a1
RP
30#include "signals.h"
31#include "gdbcmd.h"
159dd2aa
JK
32#include "serial.h"
33#if 0
34/* No longer needed, I suspect. */
bd5635a1 35#include "terminal.h"
159dd2aa 36#endif
bd5635a1
RP
37#include "bfd.h"
38#include "target.h"
bcf2e6ab 39#include "demangle.h"
bd5d07d9
FF
40#include "expression.h"
41#include "language.h"
bd5635a1 42
7919c3ed
JG
43/* Prototypes for local functions */
44
65ce5df4
JG
45#if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
46#else
3624c875 47
7919c3ed
JG
48static void
49malloc_botch PARAMS ((void));
3624c875 50
65ce5df4 51#endif /* NO_MMALLOC, etc */
7919c3ed
JG
52
53static void
54fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
55
56static void
57prompt_for_continue PARAMS ((void));
58
59static void
60set_width_command PARAMS ((char *, int, struct cmd_list_element *));
61
bd5635a1
RP
62/* If this definition isn't overridden by the header files, assume
63 that isatty and fileno exist on this system. */
64#ifndef ISATTY
65#define ISATTY(FP) (isatty (fileno (FP)))
66#endif
67
bd5635a1
RP
68/* Chain of cleanup actions established with make_cleanup,
69 to be executed if an error happens. */
70
71static struct cleanup *cleanup_chain;
72
73/* Nonzero means a quit has been requested. */
74
75int quit_flag;
76
159dd2aa
JK
77/* Nonzero means quit immediately if Control-C is typed now, rather
78 than waiting until QUIT is executed. Be careful in setting this;
79 code which executes with immediate_quit set has to be very careful
80 about being able to deal with being interrupted at any time. It is
81 almost always better to use QUIT; the only exception I can think of
82 is being able to quit out of a system call (using EINTR loses if
83 the SIGINT happens between the previous QUIT and the system call).
84 To immediately quit in the case in which a SIGINT happens between
85 the previous QUIT and setting immediate_quit (desirable anytime we
86 expect to block), call QUIT after setting immediate_quit. */
bd5635a1
RP
87
88int immediate_quit;
89
90/* Nonzero means that encoded C++ names should be printed out in their
91 C++ form rather than raw. */
92
93int demangle = 1;
94
95/* Nonzero means that encoded C++ names should be printed out in their
96 C++ form even in assembler language displays. If this is set, but
97 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
98
99int asm_demangle = 0;
100
101/* Nonzero means that strings with character values >0x7F should be printed
102 as octal escapes. Zero means just print the value (e.g. it's an
103 international character, and the terminal or window can cope.) */
104
105int sevenbit_strings = 0;
81066208
JG
106
107/* String to be printed before error messages, if any. */
108
109char *error_pre_print;
3624c875 110char *warning_pre_print = "\nwarning: ";
bd5635a1
RP
111\f
112/* Add a new cleanup to the cleanup_chain,
113 and return the previous chain pointer
114 to be passed later to do_cleanups or discard_cleanups.
115 Args are FUNCTION to clean up with, and ARG to pass to it. */
116
117struct cleanup *
118make_cleanup (function, arg)
7919c3ed
JG
119 void (*function) PARAMS ((PTR));
120 PTR arg;
bd5635a1
RP
121{
122 register struct cleanup *new
123 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
124 register struct cleanup *old_chain = cleanup_chain;
125
126 new->next = cleanup_chain;
127 new->function = function;
128 new->arg = arg;
129 cleanup_chain = new;
130
131 return old_chain;
132}
133
134/* Discard cleanups and do the actions they describe
135 until we get back to the point OLD_CHAIN in the cleanup_chain. */
136
137void
138do_cleanups (old_chain)
139 register struct cleanup *old_chain;
140{
141 register struct cleanup *ptr;
142 while ((ptr = cleanup_chain) != old_chain)
143 {
5e5215eb 144 cleanup_chain = ptr->next; /* Do this first incase recursion */
bd5635a1 145 (*ptr->function) (ptr->arg);
bd5635a1
RP
146 free (ptr);
147 }
148}
149
150/* Discard cleanups, not doing the actions they describe,
151 until we get back to the point OLD_CHAIN in the cleanup_chain. */
152
153void
154discard_cleanups (old_chain)
155 register struct cleanup *old_chain;
156{
157 register struct cleanup *ptr;
158 while ((ptr = cleanup_chain) != old_chain)
159 {
160 cleanup_chain = ptr->next;
be772100 161 free ((PTR)ptr);
bd5635a1
RP
162 }
163}
164
165/* Set the cleanup_chain to 0, and return the old cleanup chain. */
166struct cleanup *
167save_cleanups ()
168{
169 struct cleanup *old_chain = cleanup_chain;
170
171 cleanup_chain = 0;
172 return old_chain;
173}
174
175/* Restore the cleanup chain from a previously saved chain. */
176void
177restore_cleanups (chain)
178 struct cleanup *chain;
179{
180 cleanup_chain = chain;
181}
182
183/* This function is useful for cleanups.
184 Do
185
186 foo = xmalloc (...);
187 old_chain = make_cleanup (free_current_contents, &foo);
188
189 to arrange to free the object thus allocated. */
190
191void
192free_current_contents (location)
193 char **location;
194{
195 free (*location);
196}
088c3a0b
JG
197
198/* Provide a known function that does nothing, to use as a base for
199 for a possibly long chain of cleanups. This is useful where we
200 use the cleanup chain for handling normal cleanups as well as dealing
201 with cleanups that need to be done as a result of a call to error().
202 In such cases, we may not be certain where the first cleanup is, unless
203 we have a do-nothing one to always use as the base. */
204
205/* ARGSUSED */
206void
207null_cleanup (arg)
208 char **arg;
209{
210}
211
bd5635a1 212\f
2bc2e684
FF
213/* Provide a hook for modules wishing to print their own warning messages
214 to set up the terminal state in a compatible way, without them having
215 to import all the target_<...> macros. */
216
217void
218warning_setup ()
219{
220 target_terminal_ours ();
221 wrap_here(""); /* Force out any buffered output */
222 fflush (stdout);
223}
224
225/* Print a warning message.
226 The first argument STRING is the warning message, used as a fprintf string,
227 and the remaining args are passed as arguments to it.
228 The primary difference between warnings and errors is that a warning
229 does not force the return to command level. */
230
231/* VARARGS */
232void
233warning (va_alist)
234 va_dcl
235{
236 va_list args;
237 char *string;
238
239 va_start (args);
240 target_terminal_ours ();
241 wrap_here(""); /* Force out any buffered output */
242 fflush (stdout);
243 if (warning_pre_print)
244 fprintf (stderr, warning_pre_print);
245 string = va_arg (args, char *);
246 vfprintf (stderr, string, args);
247 fprintf (stderr, "\n");
248 va_end (args);
249}
250
bd5635a1
RP
251/* Print an error message and return to command level.
252 The first argument STRING is the error message, used as a fprintf string,
253 and the remaining args are passed as arguments to it. */
254
255/* VARARGS */
7919c3ed 256NORETURN void
bd5635a1
RP
257error (va_alist)
258 va_dcl
259{
260 va_list args;
261 char *string;
262
263 va_start (args);
264 target_terminal_ours ();
2bc2e684 265 wrap_here(""); /* Force out any buffered output */
bd5635a1 266 fflush (stdout);
81066208 267 if (error_pre_print)
bcf2e6ab 268 fprintf_filtered (stderr, error_pre_print);
bd5635a1 269 string = va_arg (args, char *);
bcf2e6ab
SG
270 vfprintf_filtered (stderr, string, args);
271 fprintf_filtered (stderr, "\n");
bd5635a1 272 va_end (args);
159dd2aa 273 return_to_top_level (RETURN_ERROR);
bd5635a1
RP
274}
275
276/* Print an error message and exit reporting failure.
277 This is for a error that we cannot continue from.
7919c3ed
JG
278 The arguments are printed a la printf.
279
280 This function cannot be declared volatile (NORETURN) in an
281 ANSI environment because exit() is not declared volatile. */
bd5635a1
RP
282
283/* VARARGS */
7919c3ed 284NORETURN void
bd5635a1
RP
285fatal (va_alist)
286 va_dcl
287{
288 va_list args;
289 char *string;
290
291 va_start (args);
292 string = va_arg (args, char *);
3624c875 293 fprintf (stderr, "\ngdb: ");
bd5635a1
RP
294 vfprintf (stderr, string, args);
295 fprintf (stderr, "\n");
296 va_end (args);
297 exit (1);
298}
299
300/* Print an error message and exit, dumping core.
301 The arguments are printed a la printf (). */
7919c3ed 302
bd5635a1 303/* VARARGS */
7919c3ed 304static void
bd5635a1
RP
305fatal_dump_core (va_alist)
306 va_dcl
307{
308 va_list args;
309 char *string;
310
311 va_start (args);
312 string = va_arg (args, char *);
313 /* "internal error" is always correct, since GDB should never dump
314 core, no matter what the input. */
3624c875 315 fprintf (stderr, "\ngdb internal error: ");
bd5635a1
RP
316 vfprintf (stderr, string, args);
317 fprintf (stderr, "\n");
318 va_end (args);
319
320 signal (SIGQUIT, SIG_DFL);
321 kill (getpid (), SIGQUIT);
322 /* We should never get here, but just in case... */
323 exit (1);
324}
7919c3ed 325
4ace50a5
FF
326/* The strerror() function can return NULL for errno values that are
327 out of range. Provide a "safe" version that always returns a
328 printable string. */
329
330char *
331safe_strerror (errnum)
332 int errnum;
333{
334 char *msg;
335 static char buf[32];
336
337 if ((msg = strerror (errnum)) == NULL)
338 {
339 sprintf (buf, "(undocumented errno %d)", errnum);
340 msg = buf;
341 }
342 return (msg);
343}
344
345/* The strsignal() function can return NULL for signal values that are
346 out of range. Provide a "safe" version that always returns a
347 printable string. */
348
349char *
350safe_strsignal (signo)
351 int signo;
352{
353 char *msg;
354 static char buf[32];
355
356 if ((msg = strsignal (signo)) == NULL)
357 {
358 sprintf (buf, "(undocumented signal %d)", signo);
359 msg = buf;
360 }
361 return (msg);
362}
363
364
bd5635a1
RP
365/* Print the system error message for errno, and also mention STRING
366 as the file name for which the error was encountered.
367 Then return to command level. */
368
369void
370perror_with_name (string)
371 char *string;
372{
bd5635a1
RP
373 char *err;
374 char *combined;
375
4ace50a5 376 err = safe_strerror (errno);
bd5635a1
RP
377 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
378 strcpy (combined, string);
379 strcat (combined, ": ");
380 strcat (combined, err);
381
382 /* I understand setting these is a matter of taste. Still, some people
383 may clear errno but not know about bfd_error. Doing this here is not
384 unreasonable. */
385 bfd_error = no_error;
386 errno = 0;
387
388 error ("%s.", combined);
389}
390
391/* Print the system error message for ERRCODE, and also mention STRING
392 as the file name for which the error was encountered. */
393
394void
395print_sys_errmsg (string, errcode)
396 char *string;
397 int errcode;
398{
bd5635a1
RP
399 char *err;
400 char *combined;
401
4ace50a5 402 err = safe_strerror (errcode);
bd5635a1
RP
403 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
404 strcpy (combined, string);
405 strcat (combined, ": ");
406 strcat (combined, err);
407
bcf2e6ab 408 fprintf (stderr, "%s.\n", combined);
bd5635a1
RP
409}
410
411/* Control C eventually causes this to be called, at a convenient time. */
412
413void
414quit ()
415{
159dd2aa
JK
416 serial_t stdout_serial = serial_fdopen (1);
417
bd5635a1 418 target_terminal_ours ();
d11c44f1 419 wrap_here ((char *)0); /* Force out any pending output */
159dd2aa
JK
420
421 SERIAL_FLUSH_OUTPUT (stdout_serial);
422
423 SERIAL_UN_FDOPEN (stdout_serial);
424
425 /* Don't use *_filtered; we don't want to prompt the user to continue. */
426 if (error_pre_print)
427 fprintf (stderr, error_pre_print);
428
429 if (job_control
430 /* If there is no terminal switching for this target, then we can't
431 possibly get screwed by the lack of job control. */
432 || current_target->to_terminal_ours == NULL)
433 fprintf (stderr, "Quit\n");
434 else
435 fprintf (stderr,
436 "Quit (expect signal SIGINT when the program is resumed)\n");
437 return_to_top_level (RETURN_QUIT);
bd5635a1
RP
438}
439
bd5d07d9
FF
440
441#ifdef __GO32__
442
443/* In the absence of signals, poll keyboard for a quit.
444 Called from #define QUIT pollquit() in xm-go32.h. */
445
446void
447pollquit()
448{
449 if (kbhit ())
450 {
451 int k = getkey ();
452 if (k == 1)
453 quit_flag = 1;
454 else if (k == 2)
455 immediate_quit = 1;
456 quit ();
457 }
458}
459
460#endif
461
bd5635a1
RP
462/* Control C comes here */
463
464void
088c3a0b
JG
465request_quit (signo)
466 int signo;
bd5635a1
RP
467{
468 quit_flag = 1;
469
470#ifdef USG
471 /* Restore the signal handler. */
088c3a0b 472 signal (signo, request_quit);
bd5635a1
RP
473#endif
474
475 if (immediate_quit)
476 quit ();
477}
3624c875
FF
478
479\f
480/* Memory management stuff (malloc friends). */
481
482#if defined (NO_MMALLOC)
483
484PTR
485mmalloc (md, size)
486 PTR md;
487 long size;
488{
489 return (malloc (size));
490}
491
492PTR
493mrealloc (md, ptr, size)
494 PTR md;
495 PTR ptr;
496 long size;
497{
4ace50a5
FF
498 if (ptr == 0) /* Guard against old realloc's */
499 return malloc (size);
500 else
501 return realloc (ptr, size);
3624c875
FF
502}
503
504void
505mfree (md, ptr)
506 PTR md;
507 PTR ptr;
508{
509 free (ptr);
510}
511
512#endif /* NO_MMALLOC */
513
514#if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
515
516void
517init_malloc (md)
518 PTR md;
519{
520}
521
522#else /* have mmalloc and want corruption checking */
523
524static void
525malloc_botch ()
526{
527 fatal_dump_core ("Memory corruption");
528}
529
530/* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
531 by MD, to detect memory corruption. Note that MD may be NULL to specify
532 the default heap that grows via sbrk.
533
534 Note that for freshly created regions, we must call mmcheck prior to any
535 mallocs in the region. Otherwise, any region which was allocated prior to
536 installing the checking hooks, which is later reallocated or freed, will
537 fail the checks! The mmcheck function only allows initial hooks to be
538 installed before the first mmalloc. However, anytime after we have called
539 mmcheck the first time to install the checking hooks, we can call it again
540 to update the function pointer to the memory corruption handler.
541
542 Returns zero on failure, non-zero on success. */
543
544void
545init_malloc (md)
546 PTR md;
547{
548 if (!mmcheck (md, malloc_botch))
549 {
550 warning ("internal error: failed to install memory consistency checks");
551 }
552
4ed3a9ea 553 mmtrace ();
3624c875
FF
554}
555
556#endif /* Have mmalloc and want corruption checking */
557
558/* Called when a memory allocation fails, with the number of bytes of
559 memory requested in SIZE. */
560
561NORETURN void
562nomem (size)
563 long size;
564{
565 if (size > 0)
566 {
567 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
568 }
569 else
570 {
571 fatal ("virtual memory exhausted.");
572 }
573}
574
575/* Like mmalloc but get error if no storage available, and protect against
576 the caller wanting to allocate zero bytes. Whether to return NULL for
577 a zero byte request, or translate the request into a request for one
578 byte of zero'd storage, is a religious issue. */
579
580PTR
581xmmalloc (md, size)
582 PTR md;
583 long size;
584{
585 register PTR val;
586
587 if (size == 0)
588 {
589 val = NULL;
590 }
591 else if ((val = mmalloc (md, size)) == NULL)
592 {
593 nomem (size);
594 }
595 return (val);
596}
597
598/* Like mrealloc but get error if no storage available. */
599
600PTR
601xmrealloc (md, ptr, size)
602 PTR md;
603 PTR ptr;
604 long size;
605{
606 register PTR val;
607
608 if (ptr != NULL)
609 {
610 val = mrealloc (md, ptr, size);
611 }
612 else
613 {
614 val = mmalloc (md, size);
615 }
616 if (val == NULL)
617 {
618 nomem (size);
619 }
620 return (val);
621}
622
623/* Like malloc but get error if no storage available, and protect against
624 the caller wanting to allocate zero bytes. */
625
626PTR
627xmalloc (size)
628 long size;
629{
630 return (xmmalloc ((void *) NULL, size));
631}
632
633/* Like mrealloc but get error if no storage available. */
634
635PTR
636xrealloc (ptr, size)
637 PTR ptr;
638 long size;
639{
640 return (xmrealloc ((void *) NULL, ptr, size));
641}
642
bd5635a1
RP
643\f
644/* My replacement for the read system call.
645 Used like `read' but keeps going if `read' returns too soon. */
646
647int
648myread (desc, addr, len)
649 int desc;
650 char *addr;
651 int len;
652{
653 register int val;
654 int orglen = len;
655
656 while (len > 0)
657 {
658 val = read (desc, addr, len);
659 if (val < 0)
660 return val;
661 if (val == 0)
662 return orglen - len;
663 len -= val;
664 addr += val;
665 }
666 return orglen;
667}
668\f
669/* Make a copy of the string at PTR with SIZE characters
670 (and add a null character at the end in the copy).
671 Uses malloc to get the space. Returns the address of the copy. */
672
673char *
674savestring (ptr, size)
088c3a0b 675 const char *ptr;
bd5635a1
RP
676 int size;
677{
678 register char *p = (char *) xmalloc (size + 1);
4ed3a9ea 679 memcpy (p, ptr, size);
bd5635a1
RP
680 p[size] = 0;
681 return p;
682}
683
3624c875
FF
684char *
685msavestring (md, ptr, size)
686 void *md;
687 const char *ptr;
688 int size;
689{
690 register char *p = (char *) xmmalloc (md, size + 1);
4ed3a9ea 691 memcpy (p, ptr, size);
3624c875
FF
692 p[size] = 0;
693 return p;
694}
695
8aa13b87
JK
696/* The "const" is so it compiles under DGUX (which prototypes strsave
697 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
698 Doesn't real strsave return NULL if out of memory? */
bd5635a1
RP
699char *
700strsave (ptr)
8aa13b87 701 const char *ptr;
bd5635a1
RP
702{
703 return savestring (ptr, strlen (ptr));
704}
705
3624c875
FF
706char *
707mstrsave (md, ptr)
708 void *md;
709 const char *ptr;
710{
711 return (msavestring (md, ptr, strlen (ptr)));
712}
713
bd5635a1
RP
714void
715print_spaces (n, file)
716 register int n;
717 register FILE *file;
718{
719 while (n-- > 0)
720 fputc (' ', file);
721}
722
723/* Ask user a y-or-n question and return 1 iff answer is yes.
724 Takes three args which are given to printf to print the question.
725 The first, a control string, should end in "? ".
726 It should not say how to answer, because we do that. */
727
728/* VARARGS */
729int
730query (va_alist)
731 va_dcl
732{
733 va_list args;
734 char *ctlstr;
735 register int answer;
736 register int ans2;
737
738 /* Automatically answer "yes" if input is not from a terminal. */
739 if (!input_from_terminal_p ())
740 return 1;
741
742 while (1)
743 {
546014f7
PB
744 wrap_here (""); /* Flush any buffered output */
745 fflush (stdout);
bd5635a1
RP
746 va_start (args);
747 ctlstr = va_arg (args, char *);
bcf2e6ab 748 vfprintf_filtered (stdout, ctlstr, args);
b36e3a9b 749 va_end (args);
bcf2e6ab 750 printf_filtered ("(y or n) ");
b36e3a9b
SG
751 fflush (stdout);
752 answer = fgetc (stdin);
753 clearerr (stdin); /* in case of C-d */
754 if (answer == EOF) /* C-d */
755 return 1;
756 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
757 do
758 {
759 ans2 = fgetc (stdin);
760 clearerr (stdin);
761 }
762 while (ans2 != EOF && ans2 != '\n');
bd5635a1
RP
763 if (answer >= 'a')
764 answer -= 040;
765 if (answer == 'Y')
766 return 1;
767 if (answer == 'N')
768 return 0;
bcf2e6ab 769 printf_filtered ("Please answer y or n.\n");
bd5635a1
RP
770 }
771}
7919c3ed 772
bd5635a1
RP
773\f
774/* Parse a C escape sequence. STRING_PTR points to a variable
775 containing a pointer to the string to parse. That pointer
776 should point to the character after the \. That pointer
777 is updated past the characters we use. The value of the
778 escape sequence is returned.
779
780 A negative value means the sequence \ newline was seen,
781 which is supposed to be equivalent to nothing at all.
782
783 If \ is followed by a null character, we return a negative
784 value and leave the string pointer pointing at the null character.
785
786 If \ is followed by 000, we return 0 and leave the string pointer
787 after the zeros. A value of 0 does not mean end of string. */
788
789int
790parse_escape (string_ptr)
791 char **string_ptr;
792{
793 register int c = *(*string_ptr)++;
794 switch (c)
795 {
796 case 'a':
2bc2e684 797 return 007; /* Bell (alert) char */
bd5635a1
RP
798 case 'b':
799 return '\b';
2bc2e684 800 case 'e': /* Escape character */
bd5635a1
RP
801 return 033;
802 case 'f':
803 return '\f';
804 case 'n':
805 return '\n';
806 case 'r':
807 return '\r';
808 case 't':
809 return '\t';
810 case 'v':
811 return '\v';
812 case '\n':
813 return -2;
814 case 0:
815 (*string_ptr)--;
816 return 0;
817 case '^':
818 c = *(*string_ptr)++;
819 if (c == '\\')
820 c = parse_escape (string_ptr);
821 if (c == '?')
822 return 0177;
823 return (c & 0200) | (c & 037);
824
825 case '0':
826 case '1':
827 case '2':
828 case '3':
829 case '4':
830 case '5':
831 case '6':
832 case '7':
833 {
834 register int i = c - '0';
835 register int count = 0;
836 while (++count < 3)
837 {
838 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
839 {
840 i *= 8;
841 i += c - '0';
842 }
843 else
844 {
845 (*string_ptr)--;
846 break;
847 }
848 }
849 return i;
850 }
851 default:
852 return c;
853 }
854}
855\f
51b80b00
FF
856/* Print the character C on STREAM as part of the contents of a literal
857 string whose delimiter is QUOTER. Note that this routine should only
858 be call for printing things which are independent of the language
859 of the program being debugged. */
bd5635a1
RP
860
861void
51b80b00 862gdb_printchar (c, stream, quoter)
088c3a0b 863 register int c;
bd5635a1
RP
864 FILE *stream;
865 int quoter;
866{
bd5635a1 867
7e7e2d40
JG
868 c &= 0xFF; /* Avoid sign bit follies */
869
fcdb113e
JG
870 if ( c < 0x20 || /* Low control chars */
871 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
872 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
bd5635a1
RP
873 switch (c)
874 {
875 case '\n':
876 fputs_filtered ("\\n", stream);
877 break;
878 case '\b':
879 fputs_filtered ("\\b", stream);
880 break;
881 case '\t':
882 fputs_filtered ("\\t", stream);
883 break;
884 case '\f':
885 fputs_filtered ("\\f", stream);
886 break;
887 case '\r':
888 fputs_filtered ("\\r", stream);
889 break;
890 case '\033':
891 fputs_filtered ("\\e", stream);
892 break;
893 case '\007':
894 fputs_filtered ("\\a", stream);
895 break;
896 default:
897 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
898 break;
899 }
2bc2e684
FF
900 } else {
901 if (c == '\\' || c == quoter)
902 fputs_filtered ("\\", stream);
903 fprintf_filtered (stream, "%c", c);
904 }
bd5635a1
RP
905}
906\f
907/* Number of lines per page or UINT_MAX if paging is disabled. */
908static unsigned int lines_per_page;
909/* Number of chars per line or UNIT_MAX is line folding is disabled. */
910static unsigned int chars_per_line;
911/* Current count of lines printed on this page, chars on this line. */
912static unsigned int lines_printed, chars_printed;
913
914/* Buffer and start column of buffered text, for doing smarter word-
915 wrapping. When someone calls wrap_here(), we start buffering output
916 that comes through fputs_filtered(). If we see a newline, we just
917 spit it out and forget about the wrap_here(). If we see another
918 wrap_here(), we spit it out and remember the newer one. If we see
919 the end of the line, we spit out a newline, the indent, and then
159dd2aa
JK
920 the buffered output. */
921
922/* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
923 are waiting to be output (they have already been counted in chars_printed).
924 When wrap_buffer[0] is null, the buffer is empty. */
925static char *wrap_buffer;
bd5635a1 926
159dd2aa
JK
927/* Pointer in wrap_buffer to the next character to fill. */
928static char *wrap_pointer;
bd5635a1 929
159dd2aa
JK
930/* String to indent by if the wrap occurs. Must not be NULL if wrap_column
931 is non-zero. */
932static char *wrap_indent;
933
934/* Column number on the screen where wrap_buffer begins, or 0 if wrapping
935 is not in effect. */
bd5635a1
RP
936static int wrap_column;
937
e1ce8aa5 938/* ARGSUSED */
bd5635a1
RP
939static void
940set_width_command (args, from_tty, c)
941 char *args;
942 int from_tty;
943 struct cmd_list_element *c;
944{
945 if (!wrap_buffer)
946 {
947 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
948 wrap_buffer[0] = '\0';
949 }
950 else
951 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
952 wrap_pointer = wrap_buffer; /* Start it at the beginning */
953}
954
d974236f
JG
955/* Wait, so the user can read what's on the screen. Prompt the user
956 to continue by pressing RETURN. */
957
bd5635a1
RP
958static void
959prompt_for_continue ()
960{
351b221d
JG
961 char *ignore;
962
d974236f
JG
963 /* We must do this *before* we call gdb_readline, else it will eventually
964 call us -- thinking that we're trying to print beyond the end of the
965 screen. */
966 reinitialize_more_filter ();
967
bd5635a1 968 immediate_quit++;
159dd2aa
JK
969 /* On a real operating system, the user can quit with SIGINT.
970 But not on GO32.
971
972 'q' is provided on all systems so users don't have to change habits
973 from system to system, and because telling them what to do in
974 the prompt is more user-friendly than expecting them to think of
975 SIGINT. */
976 ignore =
977 gdb_readline ("---Type <return> to continue, or q <return> to quit---");
351b221d 978 if (ignore)
159dd2aa
JK
979 {
980 char *p = ignore;
981 while (*p == ' ' || *p == '\t')
982 ++p;
983 if (p[0] == 'q')
984 request_quit (SIGINT);
985 free (ignore);
986 }
bd5635a1 987 immediate_quit--;
d974236f
JG
988
989 /* Now we have to do this again, so that GDB will know that it doesn't
990 need to save the ---Type <return>--- line at the top of the screen. */
991 reinitialize_more_filter ();
992
351b221d 993 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
bd5635a1
RP
994}
995
996/* Reinitialize filter; ie. tell it to reset to original values. */
997
998void
999reinitialize_more_filter ()
1000{
1001 lines_printed = 0;
1002 chars_printed = 0;
1003}
1004
1005/* Indicate that if the next sequence of characters overflows the line,
1006 a newline should be inserted here rather than when it hits the end.
159dd2aa 1007 If INDENT is non-null, it is a string to be printed to indent the
bd5635a1
RP
1008 wrapped part on the next line. INDENT must remain accessible until
1009 the next call to wrap_here() or until a newline is printed through
1010 fputs_filtered().
1011
1012 If the line is already overfull, we immediately print a newline and
1013 the indentation, and disable further wrapping.
1014
2bc2e684
FF
1015 If we don't know the width of lines, but we know the page height,
1016 we must not wrap words, but should still keep track of newlines
1017 that were explicitly printed.
1018
159dd2aa
JK
1019 INDENT should not contain tabs, as that will mess up the char count
1020 on the next line. FIXME.
1021
1022 This routine is guaranteed to force out any output which has been
1023 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1024 used to force out output from the wrap_buffer. */
bd5635a1
RP
1025
1026void
1027wrap_here(indent)
159dd2aa 1028 char *indent;
bd5635a1
RP
1029{
1030 if (wrap_buffer[0])
1031 {
1032 *wrap_pointer = '\0';
1033 fputs (wrap_buffer, stdout);
1034 }
1035 wrap_pointer = wrap_buffer;
1036 wrap_buffer[0] = '\0';
2bc2e684
FF
1037 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1038 {
1039 wrap_column = 0;
1040 }
1041 else if (chars_printed >= chars_per_line)
bd5635a1
RP
1042 {
1043 puts_filtered ("\n");
159dd2aa
JK
1044 if (indent != NULL)
1045 puts_filtered (indent);
bd5635a1
RP
1046 wrap_column = 0;
1047 }
1048 else
1049 {
1050 wrap_column = chars_printed;
159dd2aa
JK
1051 if (indent == NULL)
1052 wrap_indent = "";
1053 else
1054 wrap_indent = indent;
bd5635a1
RP
1055 }
1056}
1057
51b80b00
FF
1058/* Ensure that whatever gets printed next, using the filtered output
1059 commands, starts at the beginning of the line. I.E. if there is
1060 any pending output for the current line, flush it and start a new
1061 line. Otherwise do nothing. */
1062
1063void
1064begin_line ()
1065{
1066 if (chars_printed > 0)
1067 {
1068 puts_filtered ("\n");
1069 }
1070}
1071
bd5635a1
RP
1072/* Like fputs but pause after every screenful, and can wrap at points
1073 other than the final character of a line.
1074 Unlike fputs, fputs_filtered does not return a value.
1075 It is OK for LINEBUFFER to be NULL, in which case just don't print
1076 anything.
1077
1078 Note that a longjmp to top level may occur in this routine
1079 (since prompt_for_continue may do so) so this routine should not be
1080 called when cleanups are not in place. */
1081
1082void
1083fputs_filtered (linebuffer, stream)
088c3a0b 1084 const char *linebuffer;
bd5635a1
RP
1085 FILE *stream;
1086{
7919c3ed 1087 const char *lineptr;
bd5635a1
RP
1088
1089 if (linebuffer == 0)
1090 return;
1091
1092 /* Don't do any filtering if it is disabled. */
1093 if (stream != stdout
1094 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1095 {
1096 fputs (linebuffer, stream);
1097 return;
1098 }
1099
1100 /* Go through and output each character. Show line extension
1101 when this is necessary; prompt user for new page when this is
1102 necessary. */
1103
1104 lineptr = linebuffer;
1105 while (*lineptr)
1106 {
1107 /* Possible new page. */
1108 if (lines_printed >= lines_per_page - 1)
1109 prompt_for_continue ();
1110
1111 while (*lineptr && *lineptr != '\n')
1112 {
1113 /* Print a single line. */
1114 if (*lineptr == '\t')
1115 {
1116 if (wrap_column)
1117 *wrap_pointer++ = '\t';
1118 else
1119 putc ('\t', stream);
1120 /* Shifting right by 3 produces the number of tab stops
1121 we have already passed, and then adding one and
1122 shifting left 3 advances to the next tab stop. */
1123 chars_printed = ((chars_printed >> 3) + 1) << 3;
1124 lineptr++;
1125 }
1126 else
1127 {
1128 if (wrap_column)
1129 *wrap_pointer++ = *lineptr;
1130 else
1131 putc (*lineptr, stream);
1132 chars_printed++;
1133 lineptr++;
1134 }
1135
1136 if (chars_printed >= chars_per_line)
1137 {
1138 unsigned int save_chars = chars_printed;
1139
1140 chars_printed = 0;
1141 lines_printed++;
1142 /* If we aren't actually wrapping, don't output newline --
1143 if chars_per_line is right, we probably just overflowed
1144 anyway; if it's wrong, let us keep going. */
1145 if (wrap_column)
1146 putc ('\n', stream);
1147
1148 /* Possible new page. */
1149 if (lines_printed >= lines_per_page - 1)
1150 prompt_for_continue ();
1151
1152 /* Now output indentation and wrapped string */
1153 if (wrap_column)
1154 {
159dd2aa 1155 fputs (wrap_indent, stream);
bd5635a1
RP
1156 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1157 fputs (wrap_buffer, stream); /* and eject it */
1158 /* FIXME, this strlen is what prevents wrap_indent from
1159 containing tabs. However, if we recurse to print it
1160 and count its chars, we risk trouble if wrap_indent is
1161 longer than (the user settable) chars_per_line.
1162 Note also that this can set chars_printed > chars_per_line
1163 if we are printing a long string. */
1164 chars_printed = strlen (wrap_indent)
1165 + (save_chars - wrap_column);
1166 wrap_pointer = wrap_buffer; /* Reset buffer */
1167 wrap_buffer[0] = '\0';
1168 wrap_column = 0; /* And disable fancy wrap */
1169 }
1170 }
1171 }
1172
1173 if (*lineptr == '\n')
1174 {
1175 chars_printed = 0;
d11c44f1 1176 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
bd5635a1
RP
1177 lines_printed++;
1178 putc ('\n', stream);
1179 lineptr++;
1180 }
1181 }
1182}
1183
bd5635a1
RP
1184/* Print a variable number of ARGS using format FORMAT. If this
1185 information is going to put the amount written (since the last call
d974236f 1186 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
bd5635a1
RP
1187 print out a pause message and do a gdb_readline to get the users
1188 permision to continue.
1189
1190 Unlike fprintf, this function does not return a value.
1191
1192 We implement three variants, vfprintf (takes a vararg list and stream),
1193 fprintf (takes a stream to write on), and printf (the usual).
1194
1195 Note that this routine has a restriction that the length of the
1196 final output line must be less than 255 characters *or* it must be
1197 less than twice the size of the format string. This is a very
1198 arbitrary restriction, but it is an internal restriction, so I'll
1199 put it in. This means that the %s format specifier is almost
1200 useless; unless the caller can GUARANTEE that the string is short
1201 enough, fputs_filtered should be used instead.
1202
1203 Note also that a longjmp to top level may occur in this routine
1204 (since prompt_for_continue may do so) so this routine should not be
1205 called when cleanups are not in place. */
1206
d974236f
JG
1207#define MIN_LINEBUF 255
1208
a8e033f2 1209void
bd5635a1 1210vfprintf_filtered (stream, format, args)
bd5635a1
RP
1211 FILE *stream;
1212 char *format;
7919c3ed 1213 va_list args;
bd5635a1 1214{
d974236f
JG
1215 char line_buf[MIN_LINEBUF+10];
1216 char *linebuffer = line_buf;
bd5635a1
RP
1217 int format_length;
1218
1219 format_length = strlen (format);
1220
bd5635a1 1221 /* Reallocate buffer to a larger size if this is necessary. */
d974236f 1222 if (format_length * 2 > MIN_LINEBUF)
bd5635a1 1223 {
d974236f 1224 linebuffer = alloca (10 + format_length * 2);
bd5635a1
RP
1225 }
1226
bd5635a1
RP
1227 /* This won't blow up if the restrictions described above are
1228 followed. */
4ed3a9ea 1229 vsprintf (linebuffer, format, args);
bd5635a1
RP
1230
1231 fputs_filtered (linebuffer, stream);
1232}
1233
51b80b00
FF
1234void
1235vprintf_filtered (format, args)
1236 char *format;
1237 va_list args;
1238{
1239 vfprintf_filtered (stdout, format, args);
1240}
1241
bd5635a1
RP
1242/* VARARGS */
1243void
1244fprintf_filtered (va_alist)
1245 va_dcl
1246{
546014f7 1247 va_list args;
bd5635a1
RP
1248 FILE *stream;
1249 char *format;
546014f7
PB
1250
1251 va_start (args);
1252 stream = va_arg (args, FILE *);
1253 format = va_arg (args, char *);
1254
1255 /* This won't blow up if the restrictions described above are
1256 followed. */
1257 vfprintf_filtered (stream, format, args);
1258 va_end (args);
1259}
1260
1261/* Like fprintf_filtered, but prints it's result indent.
1262 Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */
1263
1264/* VARARGS */
1265void
1266fprintfi_filtered (va_alist)
1267 va_dcl
1268{
7919c3ed 1269 va_list args;
546014f7
PB
1270 int spaces;
1271 FILE *stream;
1272 char *format;
bd5635a1
RP
1273
1274 va_start (args);
546014f7 1275 spaces = va_arg (args, int);
bd5635a1
RP
1276 stream = va_arg (args, FILE *);
1277 format = va_arg (args, char *);
546014f7 1278 print_spaces_filtered (spaces, stream);
bd5635a1
RP
1279
1280 /* This won't blow up if the restrictions described above are
1281 followed. */
7919c3ed 1282 vfprintf_filtered (stream, format, args);
bd5635a1
RP
1283 va_end (args);
1284}
1285
1286/* VARARGS */
1287void
1288printf_filtered (va_alist)
1289 va_dcl
1290{
1291 va_list args;
1292 char *format;
1293
1294 va_start (args);
1295 format = va_arg (args, char *);
1296
7919c3ed 1297 vfprintf_filtered (stdout, format, args);
bd5635a1
RP
1298 va_end (args);
1299}
bd5635a1 1300
546014f7
PB
1301/* Like printf_filtered, but prints it's result indented.
1302 Called as printfi_filtered (spaces, format, arg1, arg2, ...); */
1303
1304/* VARARGS */
1305void
1306printfi_filtered (va_alist)
1307 va_dcl
1308{
1309 va_list args;
1310 int spaces;
1311 char *format;
1312
1313 va_start (args);
1314 spaces = va_arg (args, int);
1315 format = va_arg (args, char *);
1316 print_spaces_filtered (spaces, stdout);
1317 vfprintf_filtered (stdout, format, args);
1318 va_end (args);
1319}
1320
51b80b00
FF
1321/* Easy -- but watch out!
1322
1323 This routine is *not* a replacement for puts()! puts() appends a newline.
1324 This one doesn't, and had better not! */
bd5635a1
RP
1325
1326void
1327puts_filtered (string)
1328 char *string;
1329{
1330 fputs_filtered (string, stdout);
1331}
1332
1333/* Return a pointer to N spaces and a null. The pointer is good
1334 until the next call to here. */
1335char *
1336n_spaces (n)
1337 int n;
1338{
1339 register char *t;
1340 static char *spaces;
1341 static int max_spaces;
1342
1343 if (n > max_spaces)
1344 {
1345 if (spaces)
1346 free (spaces);
3624c875 1347 spaces = (char *) xmalloc (n+1);
bd5635a1
RP
1348 for (t = spaces+n; t != spaces;)
1349 *--t = ' ';
1350 spaces[n] = '\0';
1351 max_spaces = n;
1352 }
1353
1354 return spaces + max_spaces - n;
1355}
1356
1357/* Print N spaces. */
1358void
1359print_spaces_filtered (n, stream)
1360 int n;
1361 FILE *stream;
1362{
1363 fputs_filtered (n_spaces (n), stream);
1364}
1365\f
1366/* C++ demangler stuff. */
bd5635a1 1367
65ce5df4
JG
1368/* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1369 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1370 If the name is not mangled, or the language for the name is unknown, or
1371 demangling is off, the name is printed in its "raw" form. */
1372
bd5635a1 1373void
65ce5df4 1374fprintf_symbol_filtered (stream, name, lang, arg_mode)
bd5635a1
RP
1375 FILE *stream;
1376 char *name;
65ce5df4
JG
1377 enum language lang;
1378 int arg_mode;
bd5635a1 1379{
65ce5df4 1380 char *demangled;
bd5d07d9 1381
65ce5df4 1382 if (name != NULL)
bd5d07d9 1383 {
65ce5df4
JG
1384 /* If user wants to see raw output, no problem. */
1385 if (!demangle)
bd5d07d9 1386 {
65ce5df4
JG
1387 fputs_filtered (name, stream);
1388 }
1389 else
1390 {
1391 switch (lang)
1392 {
1393 case language_cplus:
1394 demangled = cplus_demangle (name, arg_mode);
1395 break;
65ce5df4
JG
1396 case language_chill:
1397 demangled = chill_demangle (name);
1398 break;
65ce5df4
JG
1399 default:
1400 demangled = NULL;
1401 break;
1402 }
1403 fputs_filtered (demangled ? demangled : name, stream);
1404 if (demangled != NULL)
1405 {
1406 free (demangled);
1407 }
bd5d07d9 1408 }
bd5635a1
RP
1409 }
1410}
51b57ded
FF
1411
1412/* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1413 differences in whitespace. Returns 0 if they match, non-zero if they
546014f7
PB
1414 don't (slightly different than strcmp()'s range of return values).
1415
1416 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2e4964ad
FF
1417 This "feature" is useful when searching for matching C++ function names
1418 (such as if the user types 'break FOO', where FOO is a mangled C++
1419 function). */
51b57ded 1420
51b80b00 1421int
51b57ded
FF
1422strcmp_iw (string1, string2)
1423 const char *string1;
1424 const char *string2;
1425{
1426 while ((*string1 != '\0') && (*string2 != '\0'))
1427 {
1428 while (isspace (*string1))
1429 {
1430 string1++;
1431 }
1432 while (isspace (*string2))
1433 {
1434 string2++;
1435 }
1436 if (*string1 != *string2)
1437 {
1438 break;
1439 }
1440 if (*string1 != '\0')
1441 {
1442 string1++;
1443 string2++;
1444 }
1445 }
546014f7 1446 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
51b57ded
FF
1447}
1448
bd5635a1 1449\f
bd5635a1
RP
1450void
1451_initialize_utils ()
1452{
1453 struct cmd_list_element *c;
1454
1455 c = add_set_cmd ("width", class_support, var_uinteger,
1456 (char *)&chars_per_line,
1457 "Set number of characters gdb thinks are in a line.",
1458 &setlist);
1459 add_show_from_set (c, &showlist);
d747e0af 1460 c->function.sfunc = set_width_command;
bd5635a1
RP
1461
1462 add_show_from_set
1463 (add_set_cmd ("height", class_support,
1464 var_uinteger, (char *)&lines_per_page,
1465 "Set number of lines gdb thinks are in a page.", &setlist),
1466 &showlist);
1467
1468 /* These defaults will be used if we are unable to get the correct
1469 values from termcap. */
51b57ded
FF
1470#if defined(__GO32__)
1471 lines_per_page = ScreenRows();
1472 chars_per_line = ScreenCols();
1473#else
bd5635a1
RP
1474 lines_per_page = 24;
1475 chars_per_line = 80;
1476 /* Initialize the screen height and width from termcap. */
1477 {
1478 char *termtype = getenv ("TERM");
1479
1480 /* Positive means success, nonpositive means failure. */
1481 int status;
1482
1483 /* 2048 is large enough for all known terminals, according to the
1484 GNU termcap manual. */
1485 char term_buffer[2048];
1486
1487 if (termtype)
1488 {
1489 status = tgetent (term_buffer, termtype);
1490 if (status > 0)
1491 {
1492 int val;
1493
1494 val = tgetnum ("li");
1495 if (val >= 0)
1496 lines_per_page = val;
1497 else
1498 /* The number of lines per page is not mentioned
1499 in the terminal description. This probably means
1500 that paging is not useful (e.g. emacs shell window),
1501 so disable paging. */
1502 lines_per_page = UINT_MAX;
1503
1504 val = tgetnum ("co");
1505 if (val >= 0)
1506 chars_per_line = val;
1507 }
1508 }
1509 }
1510
1eeba686
PB
1511#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1512
4ace50a5 1513 /* If there is a better way to determine the window size, use it. */
1eeba686
PB
1514 SIGWINCH_HANDLER ();
1515#endif
51b57ded 1516#endif
2bc2e684
FF
1517 /* If the output is not a terminal, don't paginate it. */
1518 if (!ISATTY (stdout))
1519 lines_per_page = UINT_MAX;
1520
bd5635a1
RP
1521 set_width_command ((char *)NULL, 0, c);
1522
1523 add_show_from_set
1524 (add_set_cmd ("demangle", class_support, var_boolean,
1525 (char *)&demangle,
1526 "Set demangling of encoded C++ names when displaying symbols.",
f266e564
JK
1527 &setprintlist),
1528 &showprintlist);
bd5635a1
RP
1529
1530 add_show_from_set
1531 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1532 (char *)&sevenbit_strings,
1533 "Set printing of 8-bit characters in strings as \\nnn.",
f266e564
JK
1534 &setprintlist),
1535 &showprintlist);
bd5635a1
RP
1536
1537 add_show_from_set
1538 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1539 (char *)&asm_demangle,
1540 "Set demangling of C++ names in disassembly listings.",
f266e564
JK
1541 &setprintlist),
1542 &showprintlist);
bd5635a1 1543}
1eeba686
PB
1544
1545/* Machine specific function to handle SIGWINCH signal. */
1546
1547#ifdef SIGWINCH_HANDLER_BODY
1548 SIGWINCH_HANDLER_BODY
1549#endif
bd5d07d9 1550
This page took 0.207092 seconds and 4 git commands to generate.