15796c8dcSSimon Schubert /* General utility routines for GDB, the GNU debugger.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
21a45ae5f8SJohn Marino #include "dyn-string.h"
225796c8dcSSimon Schubert #include "gdb_assert.h"
235796c8dcSSimon Schubert #include <ctype.h>
245796c8dcSSimon Schubert #include "gdb_string.h"
25*ef5ccd6cSJohn Marino #include "gdb_wait.h"
265796c8dcSSimon Schubert #include "event-top.h"
275796c8dcSSimon Schubert #include "exceptions.h"
285796c8dcSSimon Schubert #include "gdbthread.h"
29*ef5ccd6cSJohn Marino #include "fnmatch.h"
30*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
31cf7f2e2dSJohn Marino #ifdef HAVE_SYS_RESOURCE_H
32cf7f2e2dSJohn Marino #include <sys/resource.h>
33cf7f2e2dSJohn Marino #endif /* HAVE_SYS_RESOURCE_H */
345796c8dcSSimon Schubert
355796c8dcSSimon Schubert #ifdef TUI
365796c8dcSSimon Schubert #include "tui/tui.h" /* For tui_get_command_dimension. */
375796c8dcSSimon Schubert #endif
385796c8dcSSimon Schubert
395796c8dcSSimon Schubert #ifdef __GO32__
405796c8dcSSimon Schubert #include <pc.h>
415796c8dcSSimon Schubert #endif
425796c8dcSSimon Schubert
435796c8dcSSimon Schubert /* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */
445796c8dcSSimon Schubert #ifdef reg
455796c8dcSSimon Schubert #undef reg
465796c8dcSSimon Schubert #endif
475796c8dcSSimon Schubert
485796c8dcSSimon Schubert #include <signal.h>
49a45ae5f8SJohn Marino #include "timeval-utils.h"
505796c8dcSSimon Schubert #include "gdbcmd.h"
515796c8dcSSimon Schubert #include "serial.h"
525796c8dcSSimon Schubert #include "bfd.h"
535796c8dcSSimon Schubert #include "target.h"
54a45ae5f8SJohn Marino #include "gdb-demangle.h"
555796c8dcSSimon Schubert #include "expression.h"
565796c8dcSSimon Schubert #include "language.h"
575796c8dcSSimon Schubert #include "charset.h"
585796c8dcSSimon Schubert #include "annotate.h"
595796c8dcSSimon Schubert #include "filenames.h"
605796c8dcSSimon Schubert #include "symfile.h"
615796c8dcSSimon Schubert #include "gdb_obstack.h"
625796c8dcSSimon Schubert #include "gdbcore.h"
635796c8dcSSimon Schubert #include "top.h"
64cf7f2e2dSJohn Marino #include "main.h"
65a45ae5f8SJohn Marino #include "solist.h"
665796c8dcSSimon Schubert
675796c8dcSSimon Schubert #include "inferior.h" /* for signed_pointer_to_address */
685796c8dcSSimon Schubert
695796c8dcSSimon Schubert #include <sys/param.h> /* For MAXPATHLEN */
705796c8dcSSimon Schubert
715796c8dcSSimon Schubert #include "gdb_curses.h"
725796c8dcSSimon Schubert
735796c8dcSSimon Schubert #include "readline/readline.h"
745796c8dcSSimon Schubert
755796c8dcSSimon Schubert #include <sys/time.h>
765796c8dcSSimon Schubert #include <time.h>
775796c8dcSSimon Schubert
785796c8dcSSimon Schubert #include "gdb_usleep.h"
79cf7f2e2dSJohn Marino #include "interps.h"
80c50c785cSJohn Marino #include "gdb_regex.h"
815796c8dcSSimon Schubert
825796c8dcSSimon Schubert #if !HAVE_DECL_MALLOC
835796c8dcSSimon Schubert extern PTR malloc (); /* ARI: PTR */
845796c8dcSSimon Schubert #endif
855796c8dcSSimon Schubert #if !HAVE_DECL_REALLOC
865796c8dcSSimon Schubert extern PTR realloc (); /* ARI: PTR */
875796c8dcSSimon Schubert #endif
885796c8dcSSimon Schubert #if !HAVE_DECL_FREE
895796c8dcSSimon Schubert extern void free ();
905796c8dcSSimon Schubert #endif
915796c8dcSSimon Schubert
925796c8dcSSimon Schubert void (*deprecated_error_begin_hook) (void);
935796c8dcSSimon Schubert
945796c8dcSSimon Schubert /* Prototypes for local functions */
955796c8dcSSimon Schubert
965796c8dcSSimon Schubert static void vfprintf_maybe_filtered (struct ui_file *, const char *,
97cf7f2e2dSJohn Marino va_list, int) ATTRIBUTE_PRINTF (2, 0);
985796c8dcSSimon Schubert
995796c8dcSSimon Schubert static void fputs_maybe_filtered (const char *, struct ui_file *, int);
1005796c8dcSSimon Schubert
1015796c8dcSSimon Schubert static void prompt_for_continue (void);
1025796c8dcSSimon Schubert
1035796c8dcSSimon Schubert static void set_screen_size (void);
1045796c8dcSSimon Schubert static void set_width (void);
1055796c8dcSSimon Schubert
106*ef5ccd6cSJohn Marino /* Time spent in prompt_for_continue in the currently executing command
107*ef5ccd6cSJohn Marino waiting for user to respond.
108*ef5ccd6cSJohn Marino Initialized in make_command_stats_cleanup.
109*ef5ccd6cSJohn Marino Modified in prompt_for_continue and defaulted_query.
110*ef5ccd6cSJohn Marino Used in report_command_stats. */
111*ef5ccd6cSJohn Marino
112*ef5ccd6cSJohn Marino static struct timeval prompt_for_continue_wait_time;
113*ef5ccd6cSJohn Marino
1145796c8dcSSimon Schubert /* A flag indicating whether to timestamp debugging messages. */
1155796c8dcSSimon Schubert
1165796c8dcSSimon Schubert static int debug_timestamp = 0;
1175796c8dcSSimon Schubert
1185796c8dcSSimon Schubert /* Nonzero if we have job control. */
1195796c8dcSSimon Schubert
1205796c8dcSSimon Schubert int job_control;
1215796c8dcSSimon Schubert
122*ef5ccd6cSJohn Marino #ifndef HAVE_PYTHON
1235796c8dcSSimon Schubert /* Nonzero means a quit has been requested. */
1245796c8dcSSimon Schubert
1255796c8dcSSimon Schubert int quit_flag;
126*ef5ccd6cSJohn Marino #endif /* HAVE_PYTHON */
1275796c8dcSSimon Schubert
1285796c8dcSSimon Schubert /* Nonzero means quit immediately if Control-C is typed now, rather
1295796c8dcSSimon Schubert than waiting until QUIT is executed. Be careful in setting this;
1305796c8dcSSimon Schubert code which executes with immediate_quit set has to be very careful
1315796c8dcSSimon Schubert about being able to deal with being interrupted at any time. It is
1325796c8dcSSimon Schubert almost always better to use QUIT; the only exception I can think of
1335796c8dcSSimon Schubert is being able to quit out of a system call (using EINTR loses if
1345796c8dcSSimon Schubert the SIGINT happens between the previous QUIT and the system call).
1355796c8dcSSimon Schubert To immediately quit in the case in which a SIGINT happens between
1365796c8dcSSimon Schubert the previous QUIT and setting immediate_quit (desirable anytime we
1375796c8dcSSimon Schubert expect to block), call QUIT after setting immediate_quit. */
1385796c8dcSSimon Schubert
1395796c8dcSSimon Schubert int immediate_quit;
1405796c8dcSSimon Schubert
141*ef5ccd6cSJohn Marino #ifndef HAVE_PYTHON
142*ef5ccd6cSJohn Marino
143*ef5ccd6cSJohn Marino /* Clear the quit flag. */
144*ef5ccd6cSJohn Marino
145*ef5ccd6cSJohn Marino void
clear_quit_flag(void)146*ef5ccd6cSJohn Marino clear_quit_flag (void)
147*ef5ccd6cSJohn Marino {
148*ef5ccd6cSJohn Marino quit_flag = 0;
149*ef5ccd6cSJohn Marino }
150*ef5ccd6cSJohn Marino
151*ef5ccd6cSJohn Marino /* Set the quit flag. */
152*ef5ccd6cSJohn Marino
153*ef5ccd6cSJohn Marino void
set_quit_flag(void)154*ef5ccd6cSJohn Marino set_quit_flag (void)
155*ef5ccd6cSJohn Marino {
156*ef5ccd6cSJohn Marino quit_flag = 1;
157*ef5ccd6cSJohn Marino }
158*ef5ccd6cSJohn Marino
159*ef5ccd6cSJohn Marino /* Return true if the quit flag has been set, false otherwise. */
160*ef5ccd6cSJohn Marino
161*ef5ccd6cSJohn Marino int
check_quit_flag(void)162*ef5ccd6cSJohn Marino check_quit_flag (void)
163*ef5ccd6cSJohn Marino {
164*ef5ccd6cSJohn Marino /* This is written in a particular way to avoid races. */
165*ef5ccd6cSJohn Marino if (quit_flag)
166*ef5ccd6cSJohn Marino {
167*ef5ccd6cSJohn Marino quit_flag = 0;
168*ef5ccd6cSJohn Marino return 1;
169*ef5ccd6cSJohn Marino }
170*ef5ccd6cSJohn Marino
171*ef5ccd6cSJohn Marino return 0;
172*ef5ccd6cSJohn Marino }
173*ef5ccd6cSJohn Marino
174*ef5ccd6cSJohn Marino #endif /* HAVE_PYTHON */
175*ef5ccd6cSJohn Marino
1765796c8dcSSimon Schubert /* Nonzero means that strings with character values >0x7F should be printed
1775796c8dcSSimon Schubert as octal escapes. Zero means just print the value (e.g. it's an
1785796c8dcSSimon Schubert international character, and the terminal or window can cope.) */
1795796c8dcSSimon Schubert
1805796c8dcSSimon Schubert int sevenbit_strings = 0;
1815796c8dcSSimon Schubert static void
show_sevenbit_strings(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1825796c8dcSSimon Schubert show_sevenbit_strings (struct ui_file *file, int from_tty,
1835796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
1845796c8dcSSimon Schubert {
185c50c785cSJohn Marino fprintf_filtered (file, _("Printing of 8-bit characters "
186c50c785cSJohn Marino "in strings as \\nnn is %s.\n"),
1875796c8dcSSimon Schubert value);
1885796c8dcSSimon Schubert }
1895796c8dcSSimon Schubert
1905796c8dcSSimon Schubert /* String to be printed before error messages, if any. */
1915796c8dcSSimon Schubert
1925796c8dcSSimon Schubert char *error_pre_print;
1935796c8dcSSimon Schubert
1945796c8dcSSimon Schubert /* String to be printed before quit messages, if any. */
1955796c8dcSSimon Schubert
1965796c8dcSSimon Schubert char *quit_pre_print;
1975796c8dcSSimon Schubert
1985796c8dcSSimon Schubert /* String to be printed before warning messages, if any. */
1995796c8dcSSimon Schubert
2005796c8dcSSimon Schubert char *warning_pre_print = "\nwarning: ";
2015796c8dcSSimon Schubert
2025796c8dcSSimon Schubert int pagination_enabled = 1;
2035796c8dcSSimon Schubert static void
show_pagination_enabled(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2045796c8dcSSimon Schubert show_pagination_enabled (struct ui_file *file, int from_tty,
2055796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
2065796c8dcSSimon Schubert {
2075796c8dcSSimon Schubert fprintf_filtered (file, _("State of pagination is %s.\n"), value);
2085796c8dcSSimon Schubert }
2095796c8dcSSimon Schubert
2105796c8dcSSimon Schubert
211*ef5ccd6cSJohn Marino /* Cleanup utilities.
2125796c8dcSSimon Schubert
213*ef5ccd6cSJohn Marino These are not defined in cleanups.c (nor declared in cleanups.h)
214*ef5ccd6cSJohn Marino because while they use the "cleanup API" they are not part of the
215*ef5ccd6cSJohn Marino "cleanup API". */
2165796c8dcSSimon Schubert
2175796c8dcSSimon Schubert static void
do_freeargv(void * arg)2185796c8dcSSimon Schubert do_freeargv (void *arg)
2195796c8dcSSimon Schubert {
2205796c8dcSSimon Schubert freeargv ((char **) arg);
2215796c8dcSSimon Schubert }
2225796c8dcSSimon Schubert
2235796c8dcSSimon Schubert struct cleanup *
make_cleanup_freeargv(char ** arg)2245796c8dcSSimon Schubert make_cleanup_freeargv (char **arg)
2255796c8dcSSimon Schubert {
226*ef5ccd6cSJohn Marino return make_cleanup (do_freeargv, arg);
2275796c8dcSSimon Schubert }
2285796c8dcSSimon Schubert
2295796c8dcSSimon Schubert static void
do_dyn_string_delete(void * arg)230a45ae5f8SJohn Marino do_dyn_string_delete (void *arg)
231a45ae5f8SJohn Marino {
232a45ae5f8SJohn Marino dyn_string_delete ((dyn_string_t) arg);
233a45ae5f8SJohn Marino }
234a45ae5f8SJohn Marino
235a45ae5f8SJohn Marino struct cleanup *
make_cleanup_dyn_string_delete(dyn_string_t arg)236a45ae5f8SJohn Marino make_cleanup_dyn_string_delete (dyn_string_t arg)
237a45ae5f8SJohn Marino {
238*ef5ccd6cSJohn Marino return make_cleanup (do_dyn_string_delete, arg);
239a45ae5f8SJohn Marino }
240a45ae5f8SJohn Marino
241a45ae5f8SJohn Marino static void
do_bfd_close_cleanup(void * arg)2425796c8dcSSimon Schubert do_bfd_close_cleanup (void *arg)
2435796c8dcSSimon Schubert {
244*ef5ccd6cSJohn Marino gdb_bfd_unref (arg);
2455796c8dcSSimon Schubert }
2465796c8dcSSimon Schubert
2475796c8dcSSimon Schubert struct cleanup *
make_cleanup_bfd_unref(bfd * abfd)248*ef5ccd6cSJohn Marino make_cleanup_bfd_unref (bfd *abfd)
2495796c8dcSSimon Schubert {
2505796c8dcSSimon Schubert return make_cleanup (do_bfd_close_cleanup, abfd);
2515796c8dcSSimon Schubert }
2525796c8dcSSimon Schubert
2535796c8dcSSimon Schubert static void
do_close_cleanup(void * arg)2545796c8dcSSimon Schubert do_close_cleanup (void *arg)
2555796c8dcSSimon Schubert {
2565796c8dcSSimon Schubert int *fd = arg;
257cf7f2e2dSJohn Marino
2585796c8dcSSimon Schubert close (*fd);
2595796c8dcSSimon Schubert }
2605796c8dcSSimon Schubert
2615796c8dcSSimon Schubert struct cleanup *
make_cleanup_close(int fd)2625796c8dcSSimon Schubert make_cleanup_close (int fd)
2635796c8dcSSimon Schubert {
2645796c8dcSSimon Schubert int *saved_fd = xmalloc (sizeof (fd));
265cf7f2e2dSJohn Marino
2665796c8dcSSimon Schubert *saved_fd = fd;
2675796c8dcSSimon Schubert return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree);
2685796c8dcSSimon Schubert }
2695796c8dcSSimon Schubert
2705796c8dcSSimon Schubert /* Helper function which does the work for make_cleanup_fclose. */
2715796c8dcSSimon Schubert
2725796c8dcSSimon Schubert static void
do_fclose_cleanup(void * arg)2735796c8dcSSimon Schubert do_fclose_cleanup (void *arg)
2745796c8dcSSimon Schubert {
2755796c8dcSSimon Schubert FILE *file = arg;
276cf7f2e2dSJohn Marino
277cf7f2e2dSJohn Marino fclose (file);
2785796c8dcSSimon Schubert }
2795796c8dcSSimon Schubert
2805796c8dcSSimon Schubert /* Return a new cleanup that closes FILE. */
2815796c8dcSSimon Schubert
2825796c8dcSSimon Schubert struct cleanup *
make_cleanup_fclose(FILE * file)2835796c8dcSSimon Schubert make_cleanup_fclose (FILE *file)
2845796c8dcSSimon Schubert {
2855796c8dcSSimon Schubert return make_cleanup (do_fclose_cleanup, file);
2865796c8dcSSimon Schubert }
2875796c8dcSSimon Schubert
2885796c8dcSSimon Schubert /* Helper function which does the work for make_cleanup_obstack_free. */
2895796c8dcSSimon Schubert
2905796c8dcSSimon Schubert static void
do_obstack_free(void * arg)2915796c8dcSSimon Schubert do_obstack_free (void *arg)
2925796c8dcSSimon Schubert {
2935796c8dcSSimon Schubert struct obstack *ob = arg;
294cf7f2e2dSJohn Marino
2955796c8dcSSimon Schubert obstack_free (ob, NULL);
2965796c8dcSSimon Schubert }
2975796c8dcSSimon Schubert
2985796c8dcSSimon Schubert /* Return a new cleanup that frees OBSTACK. */
2995796c8dcSSimon Schubert
3005796c8dcSSimon Schubert struct cleanup *
make_cleanup_obstack_free(struct obstack * obstack)3015796c8dcSSimon Schubert make_cleanup_obstack_free (struct obstack *obstack)
3025796c8dcSSimon Schubert {
3035796c8dcSSimon Schubert return make_cleanup (do_obstack_free, obstack);
3045796c8dcSSimon Schubert }
3055796c8dcSSimon Schubert
3065796c8dcSSimon Schubert static void
do_ui_file_delete(void * arg)3075796c8dcSSimon Schubert do_ui_file_delete (void *arg)
3085796c8dcSSimon Schubert {
3095796c8dcSSimon Schubert ui_file_delete (arg);
3105796c8dcSSimon Schubert }
3115796c8dcSSimon Schubert
3125796c8dcSSimon Schubert struct cleanup *
make_cleanup_ui_file_delete(struct ui_file * arg)3135796c8dcSSimon Schubert make_cleanup_ui_file_delete (struct ui_file *arg)
3145796c8dcSSimon Schubert {
315*ef5ccd6cSJohn Marino return make_cleanup (do_ui_file_delete, arg);
3165796c8dcSSimon Schubert }
3175796c8dcSSimon Schubert
318c50c785cSJohn Marino /* Helper function for make_cleanup_ui_out_redirect_pop. */
319c50c785cSJohn Marino
320c50c785cSJohn Marino static void
do_ui_out_redirect_pop(void * arg)321c50c785cSJohn Marino do_ui_out_redirect_pop (void *arg)
322c50c785cSJohn Marino {
323c50c785cSJohn Marino struct ui_out *uiout = arg;
324c50c785cSJohn Marino
325c50c785cSJohn Marino if (ui_out_redirect (uiout, NULL) < 0)
326c50c785cSJohn Marino warning (_("Cannot restore redirection of the current output protocol"));
327c50c785cSJohn Marino }
328c50c785cSJohn Marino
329c50c785cSJohn Marino /* Return a new cleanup that pops the last redirection by ui_out_redirect
330c50c785cSJohn Marino with NULL parameter. */
331c50c785cSJohn Marino
332c50c785cSJohn Marino struct cleanup *
make_cleanup_ui_out_redirect_pop(struct ui_out * uiout)333c50c785cSJohn Marino make_cleanup_ui_out_redirect_pop (struct ui_out *uiout)
334c50c785cSJohn Marino {
335*ef5ccd6cSJohn Marino return make_cleanup (do_ui_out_redirect_pop, uiout);
336c50c785cSJohn Marino }
337c50c785cSJohn Marino
3385796c8dcSSimon Schubert static void
do_free_section_addr_info(void * arg)3395796c8dcSSimon Schubert do_free_section_addr_info (void *arg)
3405796c8dcSSimon Schubert {
3415796c8dcSSimon Schubert free_section_addr_info (arg);
3425796c8dcSSimon Schubert }
3435796c8dcSSimon Schubert
3445796c8dcSSimon Schubert struct cleanup *
make_cleanup_free_section_addr_info(struct section_addr_info * addrs)3455796c8dcSSimon Schubert make_cleanup_free_section_addr_info (struct section_addr_info *addrs)
3465796c8dcSSimon Schubert {
347*ef5ccd6cSJohn Marino return make_cleanup (do_free_section_addr_info, addrs);
3485796c8dcSSimon Schubert }
3495796c8dcSSimon Schubert
3505796c8dcSSimon Schubert struct restore_integer_closure
3515796c8dcSSimon Schubert {
3525796c8dcSSimon Schubert int *variable;
3535796c8dcSSimon Schubert int value;
3545796c8dcSSimon Schubert };
3555796c8dcSSimon Schubert
3565796c8dcSSimon Schubert static void
restore_integer(void * p)3575796c8dcSSimon Schubert restore_integer (void *p)
3585796c8dcSSimon Schubert {
3595796c8dcSSimon Schubert struct restore_integer_closure *closure = p;
360cf7f2e2dSJohn Marino
3615796c8dcSSimon Schubert *(closure->variable) = closure->value;
3625796c8dcSSimon Schubert }
3635796c8dcSSimon Schubert
364c50c785cSJohn Marino /* Remember the current value of *VARIABLE and make it restored when
365c50c785cSJohn Marino the cleanup is run. */
366cf7f2e2dSJohn Marino
3675796c8dcSSimon Schubert struct cleanup *
make_cleanup_restore_integer(int * variable)3685796c8dcSSimon Schubert make_cleanup_restore_integer (int *variable)
3695796c8dcSSimon Schubert {
3705796c8dcSSimon Schubert struct restore_integer_closure *c =
3715796c8dcSSimon Schubert xmalloc (sizeof (struct restore_integer_closure));
372cf7f2e2dSJohn Marino
3735796c8dcSSimon Schubert c->variable = variable;
3745796c8dcSSimon Schubert c->value = *variable;
3755796c8dcSSimon Schubert
376*ef5ccd6cSJohn Marino return make_cleanup_dtor (restore_integer, (void *) c, xfree);
3775796c8dcSSimon Schubert }
3785796c8dcSSimon Schubert
379c50c785cSJohn Marino /* Remember the current value of *VARIABLE and make it restored when
380c50c785cSJohn Marino the cleanup is run. */
381cf7f2e2dSJohn Marino
382cf7f2e2dSJohn Marino struct cleanup *
make_cleanup_restore_uinteger(unsigned int * variable)383cf7f2e2dSJohn Marino make_cleanup_restore_uinteger (unsigned int *variable)
384cf7f2e2dSJohn Marino {
385cf7f2e2dSJohn Marino return make_cleanup_restore_integer ((int *) variable);
386cf7f2e2dSJohn Marino }
387cf7f2e2dSJohn Marino
388c50c785cSJohn Marino /* Helper for make_cleanup_unpush_target. */
389c50c785cSJohn Marino
390c50c785cSJohn Marino static void
do_unpush_target(void * arg)391c50c785cSJohn Marino do_unpush_target (void *arg)
392c50c785cSJohn Marino {
393c50c785cSJohn Marino struct target_ops *ops = arg;
394c50c785cSJohn Marino
395c50c785cSJohn Marino unpush_target (ops);
396c50c785cSJohn Marino }
397c50c785cSJohn Marino
398c50c785cSJohn Marino /* Return a new cleanup that unpushes OPS. */
399c50c785cSJohn Marino
400c50c785cSJohn Marino struct cleanup *
make_cleanup_unpush_target(struct target_ops * ops)401c50c785cSJohn Marino make_cleanup_unpush_target (struct target_ops *ops)
402c50c785cSJohn Marino {
403*ef5ccd6cSJohn Marino return make_cleanup (do_unpush_target, ops);
404c50c785cSJohn Marino }
405c50c785cSJohn Marino
406a45ae5f8SJohn Marino /* Helper for make_cleanup_htab_delete compile time checking the types. */
407a45ae5f8SJohn Marino
408a45ae5f8SJohn Marino static void
do_htab_delete_cleanup(void * htab_voidp)409a45ae5f8SJohn Marino do_htab_delete_cleanup (void *htab_voidp)
410a45ae5f8SJohn Marino {
411a45ae5f8SJohn Marino htab_t htab = htab_voidp;
412a45ae5f8SJohn Marino
413a45ae5f8SJohn Marino htab_delete (htab);
414a45ae5f8SJohn Marino }
415a45ae5f8SJohn Marino
416a45ae5f8SJohn Marino /* Return a new cleanup that deletes HTAB. */
417a45ae5f8SJohn Marino
418a45ae5f8SJohn Marino struct cleanup *
make_cleanup_htab_delete(htab_t htab)419a45ae5f8SJohn Marino make_cleanup_htab_delete (htab_t htab)
420a45ae5f8SJohn Marino {
421a45ae5f8SJohn Marino return make_cleanup (do_htab_delete_cleanup, htab);
422a45ae5f8SJohn Marino }
423a45ae5f8SJohn Marino
424cf7f2e2dSJohn Marino struct restore_ui_file_closure
425cf7f2e2dSJohn Marino {
426cf7f2e2dSJohn Marino struct ui_file **variable;
427cf7f2e2dSJohn Marino struct ui_file *value;
428cf7f2e2dSJohn Marino };
429cf7f2e2dSJohn Marino
430cf7f2e2dSJohn Marino static void
do_restore_ui_file(void * p)431cf7f2e2dSJohn Marino do_restore_ui_file (void *p)
432cf7f2e2dSJohn Marino {
433cf7f2e2dSJohn Marino struct restore_ui_file_closure *closure = p;
434cf7f2e2dSJohn Marino
435cf7f2e2dSJohn Marino *(closure->variable) = closure->value;
436cf7f2e2dSJohn Marino }
437cf7f2e2dSJohn Marino
438cf7f2e2dSJohn Marino /* Remember the current value of *VARIABLE and make it restored when
439cf7f2e2dSJohn Marino the cleanup is run. */
440cf7f2e2dSJohn Marino
441cf7f2e2dSJohn Marino struct cleanup *
make_cleanup_restore_ui_file(struct ui_file ** variable)442cf7f2e2dSJohn Marino make_cleanup_restore_ui_file (struct ui_file **variable)
443cf7f2e2dSJohn Marino {
444cf7f2e2dSJohn Marino struct restore_ui_file_closure *c = XNEW (struct restore_ui_file_closure);
445cf7f2e2dSJohn Marino
446cf7f2e2dSJohn Marino c->variable = variable;
447cf7f2e2dSJohn Marino c->value = *variable;
448cf7f2e2dSJohn Marino
449cf7f2e2dSJohn Marino return make_cleanup_dtor (do_restore_ui_file, (void *) c, xfree);
450cf7f2e2dSJohn Marino }
451cf7f2e2dSJohn Marino
452a45ae5f8SJohn Marino /* Helper for make_cleanup_value_free_to_mark. */
453a45ae5f8SJohn Marino
454a45ae5f8SJohn Marino static void
do_value_free_to_mark(void * value)455a45ae5f8SJohn Marino do_value_free_to_mark (void *value)
456a45ae5f8SJohn Marino {
457a45ae5f8SJohn Marino value_free_to_mark ((struct value *) value);
458a45ae5f8SJohn Marino }
459a45ae5f8SJohn Marino
460a45ae5f8SJohn Marino /* Free all values allocated since MARK was obtained by value_mark
461a45ae5f8SJohn Marino (except for those released) when the cleanup is run. */
462a45ae5f8SJohn Marino
463a45ae5f8SJohn Marino struct cleanup *
make_cleanup_value_free_to_mark(struct value * mark)464a45ae5f8SJohn Marino make_cleanup_value_free_to_mark (struct value *mark)
465a45ae5f8SJohn Marino {
466*ef5ccd6cSJohn Marino return make_cleanup (do_value_free_to_mark, mark);
467a45ae5f8SJohn Marino }
468a45ae5f8SJohn Marino
469a45ae5f8SJohn Marino /* Helper for make_cleanup_value_free. */
470a45ae5f8SJohn Marino
471a45ae5f8SJohn Marino static void
do_value_free(void * value)472a45ae5f8SJohn Marino do_value_free (void *value)
473a45ae5f8SJohn Marino {
474a45ae5f8SJohn Marino value_free (value);
475a45ae5f8SJohn Marino }
476a45ae5f8SJohn Marino
477a45ae5f8SJohn Marino /* Free VALUE. */
478a45ae5f8SJohn Marino
479a45ae5f8SJohn Marino struct cleanup *
make_cleanup_value_free(struct value * value)480a45ae5f8SJohn Marino make_cleanup_value_free (struct value *value)
481a45ae5f8SJohn Marino {
482*ef5ccd6cSJohn Marino return make_cleanup (do_value_free, value);
483a45ae5f8SJohn Marino }
484a45ae5f8SJohn Marino
485a45ae5f8SJohn Marino /* Helper for make_cleanup_free_so. */
486a45ae5f8SJohn Marino
487a45ae5f8SJohn Marino static void
do_free_so(void * arg)488a45ae5f8SJohn Marino do_free_so (void *arg)
489a45ae5f8SJohn Marino {
490a45ae5f8SJohn Marino struct so_list *so = arg;
491a45ae5f8SJohn Marino
492a45ae5f8SJohn Marino free_so (so);
493a45ae5f8SJohn Marino }
494a45ae5f8SJohn Marino
495a45ae5f8SJohn Marino /* Make cleanup handler calling free_so for SO. */
496a45ae5f8SJohn Marino
497a45ae5f8SJohn Marino struct cleanup *
make_cleanup_free_so(struct so_list * so)498a45ae5f8SJohn Marino make_cleanup_free_so (struct so_list *so)
499a45ae5f8SJohn Marino {
500*ef5ccd6cSJohn Marino return make_cleanup (do_free_so, so);
501a45ae5f8SJohn Marino }
502a45ae5f8SJohn Marino
503*ef5ccd6cSJohn Marino /* Helper for make_cleanup_restore_current_language. */
5045796c8dcSSimon Schubert
5055796c8dcSSimon Schubert static void
do_restore_current_language(void * p)506*ef5ccd6cSJohn Marino do_restore_current_language (void *p)
5075796c8dcSSimon Schubert {
508*ef5ccd6cSJohn Marino enum language saved_lang = (uintptr_t) p;
509cf7f2e2dSJohn Marino
510*ef5ccd6cSJohn Marino set_language (saved_lang);
5115796c8dcSSimon Schubert }
5125796c8dcSSimon Schubert
513*ef5ccd6cSJohn Marino /* Remember the current value of CURRENT_LANGUAGE and make it restored when
514*ef5ccd6cSJohn Marino the cleanup is run. */
5155796c8dcSSimon Schubert
5165796c8dcSSimon Schubert struct cleanup *
make_cleanup_restore_current_language(void)517*ef5ccd6cSJohn Marino make_cleanup_restore_current_language (void)
5185796c8dcSSimon Schubert {
519*ef5ccd6cSJohn Marino enum language saved_lang = current_language->la_language;
5205796c8dcSSimon Schubert
521*ef5ccd6cSJohn Marino return make_cleanup (do_restore_current_language,
522*ef5ccd6cSJohn Marino (void *) (uintptr_t) saved_lang);
5235796c8dcSSimon Schubert }
5245796c8dcSSimon Schubert
5255796c8dcSSimon Schubert /* This function is useful for cleanups.
5265796c8dcSSimon Schubert Do
5275796c8dcSSimon Schubert
5285796c8dcSSimon Schubert foo = xmalloc (...);
5295796c8dcSSimon Schubert old_chain = make_cleanup (free_current_contents, &foo);
5305796c8dcSSimon Schubert
5315796c8dcSSimon Schubert to arrange to free the object thus allocated. */
5325796c8dcSSimon Schubert
5335796c8dcSSimon Schubert void
free_current_contents(void * ptr)5345796c8dcSSimon Schubert free_current_contents (void *ptr)
5355796c8dcSSimon Schubert {
5365796c8dcSSimon Schubert void **location = ptr;
537cf7f2e2dSJohn Marino
5385796c8dcSSimon Schubert if (location == NULL)
5395796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
5405796c8dcSSimon Schubert _("free_current_contents: NULL pointer"));
5415796c8dcSSimon Schubert if (*location != NULL)
5425796c8dcSSimon Schubert {
5435796c8dcSSimon Schubert xfree (*location);
5445796c8dcSSimon Schubert *location = NULL;
5455796c8dcSSimon Schubert }
5465796c8dcSSimon Schubert }
5475796c8dcSSimon Schubert
548cf7f2e2dSJohn Marino /* If nonzero, display time usage both at startup and for each command. */
549cf7f2e2dSJohn Marino
550cf7f2e2dSJohn Marino static int display_time;
551cf7f2e2dSJohn Marino
552cf7f2e2dSJohn Marino /* If nonzero, display space usage both at startup and for each command. */
553cf7f2e2dSJohn Marino
554cf7f2e2dSJohn Marino static int display_space;
555cf7f2e2dSJohn Marino
556cf7f2e2dSJohn Marino /* Records a run time and space usage to be used as a base for
557cf7f2e2dSJohn Marino reporting elapsed time or change in space. In addition,
558cf7f2e2dSJohn Marino the msg_type field indicates whether the saved time is from the
559cf7f2e2dSJohn Marino beginning of GDB execution (0) or the beginning of an individual
560cf7f2e2dSJohn Marino command execution (1). */
561cf7f2e2dSJohn Marino struct cmd_stats
562cf7f2e2dSJohn Marino {
563cf7f2e2dSJohn Marino int msg_type;
564a45ae5f8SJohn Marino long start_cpu_time;
565a45ae5f8SJohn Marino struct timeval start_wall_time;
566cf7f2e2dSJohn Marino long start_space;
567cf7f2e2dSJohn Marino };
568cf7f2e2dSJohn Marino
569cf7f2e2dSJohn Marino /* Set whether to display time statistics to NEW_VALUE (non-zero
570cf7f2e2dSJohn Marino means true). */
571cf7f2e2dSJohn Marino void
set_display_time(int new_value)572cf7f2e2dSJohn Marino set_display_time (int new_value)
573cf7f2e2dSJohn Marino {
574cf7f2e2dSJohn Marino display_time = new_value;
575cf7f2e2dSJohn Marino }
576cf7f2e2dSJohn Marino
577cf7f2e2dSJohn Marino /* Set whether to display space statistics to NEW_VALUE (non-zero
578cf7f2e2dSJohn Marino means true). */
579cf7f2e2dSJohn Marino void
set_display_space(int new_value)580cf7f2e2dSJohn Marino set_display_space (int new_value)
581cf7f2e2dSJohn Marino {
582cf7f2e2dSJohn Marino display_space = new_value;
583cf7f2e2dSJohn Marino }
584cf7f2e2dSJohn Marino
585cf7f2e2dSJohn Marino /* As indicated by display_time and display_space, report GDB's elapsed time
586cf7f2e2dSJohn Marino and space usage from the base time and space provided in ARG, which
587cf7f2e2dSJohn Marino must be a pointer to a struct cmd_stat. This function is intended
588cf7f2e2dSJohn Marino to be called as a cleanup. */
589cf7f2e2dSJohn Marino static void
report_command_stats(void * arg)590cf7f2e2dSJohn Marino report_command_stats (void *arg)
591cf7f2e2dSJohn Marino {
592cf7f2e2dSJohn Marino struct cmd_stats *start_stats = (struct cmd_stats *) arg;
593cf7f2e2dSJohn Marino int msg_type = start_stats->msg_type;
594cf7f2e2dSJohn Marino
595cf7f2e2dSJohn Marino if (display_time)
596cf7f2e2dSJohn Marino {
597a45ae5f8SJohn Marino long cmd_time = get_run_time () - start_stats->start_cpu_time;
598a45ae5f8SJohn Marino struct timeval now_wall_time, delta_wall_time;
599a45ae5f8SJohn Marino
600a45ae5f8SJohn Marino gettimeofday (&now_wall_time, NULL);
601a45ae5f8SJohn Marino timeval_sub (&delta_wall_time,
602a45ae5f8SJohn Marino &now_wall_time, &start_stats->start_wall_time);
603cf7f2e2dSJohn Marino
604*ef5ccd6cSJohn Marino /* Subtract time spend in prompt_for_continue from walltime. */
605*ef5ccd6cSJohn Marino timeval_sub (&delta_wall_time,
606*ef5ccd6cSJohn Marino &delta_wall_time, &prompt_for_continue_wait_time);
607*ef5ccd6cSJohn Marino
608cf7f2e2dSJohn Marino printf_unfiltered (msg_type == 0
609a45ae5f8SJohn Marino ? _("Startup time: %ld.%06ld (cpu), %ld.%06ld (wall)\n")
610a45ae5f8SJohn Marino : _("Command execution time: %ld.%06ld (cpu), %ld.%06ld (wall)\n"),
611a45ae5f8SJohn Marino cmd_time / 1000000, cmd_time % 1000000,
612a45ae5f8SJohn Marino (long) delta_wall_time.tv_sec,
613a45ae5f8SJohn Marino (long) delta_wall_time.tv_usec);
614cf7f2e2dSJohn Marino }
615cf7f2e2dSJohn Marino
616cf7f2e2dSJohn Marino if (display_space)
617cf7f2e2dSJohn Marino {
618cf7f2e2dSJohn Marino #ifdef HAVE_SBRK
619cf7f2e2dSJohn Marino char *lim = (char *) sbrk (0);
620cf7f2e2dSJohn Marino
621cf7f2e2dSJohn Marino long space_now = lim - lim_at_start;
622cf7f2e2dSJohn Marino long space_diff = space_now - start_stats->start_space;
623cf7f2e2dSJohn Marino
624cf7f2e2dSJohn Marino printf_unfiltered (msg_type == 0
625c50c785cSJohn Marino ? _("Space used: %ld (%s%ld during startup)\n")
626c50c785cSJohn Marino : _("Space used: %ld (%s%ld for this command)\n"),
627cf7f2e2dSJohn Marino space_now,
628c50c785cSJohn Marino (space_diff >= 0 ? "+" : ""),
629cf7f2e2dSJohn Marino space_diff);
630cf7f2e2dSJohn Marino #endif
631cf7f2e2dSJohn Marino }
632cf7f2e2dSJohn Marino }
633cf7f2e2dSJohn Marino
634cf7f2e2dSJohn Marino /* Create a cleanup that reports time and space used since its
635cf7f2e2dSJohn Marino creation. Precise messages depend on MSG_TYPE:
636cf7f2e2dSJohn Marino 0: Initial time/space
637cf7f2e2dSJohn Marino 1: Individual command time/space. */
638cf7f2e2dSJohn Marino struct cleanup *
make_command_stats_cleanup(int msg_type)639cf7f2e2dSJohn Marino make_command_stats_cleanup (int msg_type)
640cf7f2e2dSJohn Marino {
641*ef5ccd6cSJohn Marino static const struct timeval zero_timeval = { 0 };
642cf7f2e2dSJohn Marino struct cmd_stats *new_stat = XMALLOC (struct cmd_stats);
643cf7f2e2dSJohn Marino
644cf7f2e2dSJohn Marino #ifdef HAVE_SBRK
645cf7f2e2dSJohn Marino char *lim = (char *) sbrk (0);
646cf7f2e2dSJohn Marino new_stat->start_space = lim - lim_at_start;
647cf7f2e2dSJohn Marino #endif
648cf7f2e2dSJohn Marino
649cf7f2e2dSJohn Marino new_stat->msg_type = msg_type;
650a45ae5f8SJohn Marino new_stat->start_cpu_time = get_run_time ();
651a45ae5f8SJohn Marino gettimeofday (&new_stat->start_wall_time, NULL);
652cf7f2e2dSJohn Marino
653*ef5ccd6cSJohn Marino /* Initalize timer to keep track of how long we waited for the user. */
654*ef5ccd6cSJohn Marino prompt_for_continue_wait_time = zero_timeval;
655*ef5ccd6cSJohn Marino
656cf7f2e2dSJohn Marino return make_cleanup_dtor (report_command_stats, new_stat, xfree);
657cf7f2e2dSJohn Marino }
6585796c8dcSSimon Schubert
6595796c8dcSSimon Schubert
6605796c8dcSSimon Schubert
6615796c8dcSSimon Schubert /* Print a warning message. The first argument STRING is the warning
6625796c8dcSSimon Schubert message, used as an fprintf format string, the second is the
6635796c8dcSSimon Schubert va_list of arguments for that string. A warning is unfiltered (not
6645796c8dcSSimon Schubert paginated) so that the user does not need to page through each
6655796c8dcSSimon Schubert screen full of warnings when there are lots of them. */
6665796c8dcSSimon Schubert
6675796c8dcSSimon Schubert void
vwarning(const char * string,va_list args)6685796c8dcSSimon Schubert vwarning (const char *string, va_list args)
6695796c8dcSSimon Schubert {
6705796c8dcSSimon Schubert if (deprecated_warning_hook)
6715796c8dcSSimon Schubert (*deprecated_warning_hook) (string, args);
6725796c8dcSSimon Schubert else
6735796c8dcSSimon Schubert {
6745796c8dcSSimon Schubert target_terminal_ours ();
675c50c785cSJohn Marino wrap_here (""); /* Force out any buffered output. */
6765796c8dcSSimon Schubert gdb_flush (gdb_stdout);
6775796c8dcSSimon Schubert if (warning_pre_print)
6785796c8dcSSimon Schubert fputs_unfiltered (warning_pre_print, gdb_stderr);
6795796c8dcSSimon Schubert vfprintf_unfiltered (gdb_stderr, string, args);
6805796c8dcSSimon Schubert fprintf_unfiltered (gdb_stderr, "\n");
6815796c8dcSSimon Schubert va_end (args);
6825796c8dcSSimon Schubert }
6835796c8dcSSimon Schubert }
6845796c8dcSSimon Schubert
6855796c8dcSSimon Schubert /* Print a warning message.
6865796c8dcSSimon Schubert The first argument STRING is the warning message, used as a fprintf string,
6875796c8dcSSimon Schubert and the remaining args are passed as arguments to it.
6885796c8dcSSimon Schubert The primary difference between warnings and errors is that a warning
6895796c8dcSSimon Schubert does not force the return to command level. */
6905796c8dcSSimon Schubert
6915796c8dcSSimon Schubert void
warning(const char * string,...)6925796c8dcSSimon Schubert warning (const char *string, ...)
6935796c8dcSSimon Schubert {
6945796c8dcSSimon Schubert va_list args;
695cf7f2e2dSJohn Marino
6965796c8dcSSimon Schubert va_start (args, string);
6975796c8dcSSimon Schubert vwarning (string, args);
6985796c8dcSSimon Schubert va_end (args);
6995796c8dcSSimon Schubert }
7005796c8dcSSimon Schubert
7015796c8dcSSimon Schubert /* Print an error message and return to command level.
7025796c8dcSSimon Schubert The first argument STRING is the error message, used as a fprintf string,
7035796c8dcSSimon Schubert and the remaining args are passed as arguments to it. */
7045796c8dcSSimon Schubert
705cf7f2e2dSJohn Marino void
verror(const char * string,va_list args)7065796c8dcSSimon Schubert verror (const char *string, va_list args)
7075796c8dcSSimon Schubert {
7085796c8dcSSimon Schubert throw_verror (GENERIC_ERROR, string, args);
7095796c8dcSSimon Schubert }
7105796c8dcSSimon Schubert
711cf7f2e2dSJohn Marino void
error(const char * string,...)7125796c8dcSSimon Schubert error (const char *string, ...)
7135796c8dcSSimon Schubert {
7145796c8dcSSimon Schubert va_list args;
715cf7f2e2dSJohn Marino
7165796c8dcSSimon Schubert va_start (args, string);
7175796c8dcSSimon Schubert throw_verror (GENERIC_ERROR, string, args);
7185796c8dcSSimon Schubert va_end (args);
7195796c8dcSSimon Schubert }
7205796c8dcSSimon Schubert
7215796c8dcSSimon Schubert /* Print an error message and quit.
7225796c8dcSSimon Schubert The first argument STRING is the error message, used as a fprintf string,
7235796c8dcSSimon Schubert and the remaining args are passed as arguments to it. */
7245796c8dcSSimon Schubert
725cf7f2e2dSJohn Marino void
vfatal(const char * string,va_list args)7265796c8dcSSimon Schubert vfatal (const char *string, va_list args)
7275796c8dcSSimon Schubert {
7285796c8dcSSimon Schubert throw_vfatal (string, args);
7295796c8dcSSimon Schubert }
7305796c8dcSSimon Schubert
731cf7f2e2dSJohn Marino void
fatal(const char * string,...)7325796c8dcSSimon Schubert fatal (const char *string, ...)
7335796c8dcSSimon Schubert {
7345796c8dcSSimon Schubert va_list args;
735cf7f2e2dSJohn Marino
7365796c8dcSSimon Schubert va_start (args, string);
7375796c8dcSSimon Schubert throw_vfatal (string, args);
7385796c8dcSSimon Schubert va_end (args);
7395796c8dcSSimon Schubert }
7405796c8dcSSimon Schubert
741cf7f2e2dSJohn Marino void
error_stream(struct ui_file * stream)7425796c8dcSSimon Schubert error_stream (struct ui_file *stream)
7435796c8dcSSimon Schubert {
7445796c8dcSSimon Schubert char *message = ui_file_xstrdup (stream, NULL);
745cf7f2e2dSJohn Marino
7465796c8dcSSimon Schubert make_cleanup (xfree, message);
7475796c8dcSSimon Schubert error (("%s"), message);
7485796c8dcSSimon Schubert }
7495796c8dcSSimon Schubert
750cf7f2e2dSJohn Marino /* Dump core trying to increase the core soft limit to hard limit first. */
751cf7f2e2dSJohn Marino
752cf7f2e2dSJohn Marino static void
dump_core(void)753cf7f2e2dSJohn Marino dump_core (void)
754cf7f2e2dSJohn Marino {
755cf7f2e2dSJohn Marino #ifdef HAVE_SETRLIMIT
756cf7f2e2dSJohn Marino struct rlimit rlim = { RLIM_INFINITY, RLIM_INFINITY };
757cf7f2e2dSJohn Marino
758cf7f2e2dSJohn Marino setrlimit (RLIMIT_CORE, &rlim);
759cf7f2e2dSJohn Marino #endif /* HAVE_SETRLIMIT */
760cf7f2e2dSJohn Marino
761cf7f2e2dSJohn Marino abort (); /* NOTE: GDB has only three calls to abort(). */
762cf7f2e2dSJohn Marino }
763cf7f2e2dSJohn Marino
764c50c785cSJohn Marino /* Check whether GDB will be able to dump core using the dump_core
765c50c785cSJohn Marino function. */
766cf7f2e2dSJohn Marino
767cf7f2e2dSJohn Marino static int
can_dump_core(const char * reason)768cf7f2e2dSJohn Marino can_dump_core (const char *reason)
769cf7f2e2dSJohn Marino {
770cf7f2e2dSJohn Marino #ifdef HAVE_GETRLIMIT
771cf7f2e2dSJohn Marino struct rlimit rlim;
772cf7f2e2dSJohn Marino
773cf7f2e2dSJohn Marino /* Be quiet and assume we can dump if an error is returned. */
774cf7f2e2dSJohn Marino if (getrlimit (RLIMIT_CORE, &rlim) != 0)
775cf7f2e2dSJohn Marino return 1;
776cf7f2e2dSJohn Marino
777cf7f2e2dSJohn Marino if (rlim.rlim_max == 0)
778cf7f2e2dSJohn Marino {
779cf7f2e2dSJohn Marino fprintf_unfiltered (gdb_stderr,
780c50c785cSJohn Marino _("%s\nUnable to dump core, use `ulimit -c"
781c50c785cSJohn Marino " unlimited' before executing GDB next time.\n"),
782c50c785cSJohn Marino reason);
783cf7f2e2dSJohn Marino return 0;
784cf7f2e2dSJohn Marino }
785cf7f2e2dSJohn Marino #endif /* HAVE_GETRLIMIT */
786cf7f2e2dSJohn Marino
787cf7f2e2dSJohn Marino return 1;
788cf7f2e2dSJohn Marino }
789cf7f2e2dSJohn Marino
7905796c8dcSSimon Schubert /* Allow the user to configure the debugger behavior with respect to
7915796c8dcSSimon Schubert what to do when an internal problem is detected. */
7925796c8dcSSimon Schubert
7935796c8dcSSimon Schubert const char internal_problem_ask[] = "ask";
7945796c8dcSSimon Schubert const char internal_problem_yes[] = "yes";
7955796c8dcSSimon Schubert const char internal_problem_no[] = "no";
796*ef5ccd6cSJohn Marino static const char *const internal_problem_modes[] =
7975796c8dcSSimon Schubert {
7985796c8dcSSimon Schubert internal_problem_ask,
7995796c8dcSSimon Schubert internal_problem_yes,
8005796c8dcSSimon Schubert internal_problem_no,
8015796c8dcSSimon Schubert NULL
8025796c8dcSSimon Schubert };
8035796c8dcSSimon Schubert
8045796c8dcSSimon Schubert /* Print a message reporting an internal error/warning. Ask the user
8055796c8dcSSimon Schubert if they want to continue, dump core, or just exit. Return
8065796c8dcSSimon Schubert something to indicate a quit. */
8075796c8dcSSimon Schubert
8085796c8dcSSimon Schubert struct internal_problem
8095796c8dcSSimon Schubert {
8105796c8dcSSimon Schubert const char *name;
8115796c8dcSSimon Schubert const char *should_quit;
8125796c8dcSSimon Schubert const char *should_dump_core;
8135796c8dcSSimon Schubert };
8145796c8dcSSimon Schubert
8155796c8dcSSimon Schubert /* Report a problem, internal to GDB, to the user. Once the problem
8165796c8dcSSimon Schubert has been reported, and assuming GDB didn't quit, the caller can
8175796c8dcSSimon Schubert either allow execution to resume or throw an error. */
8185796c8dcSSimon Schubert
819cf7f2e2dSJohn Marino static void ATTRIBUTE_PRINTF (4, 0)
internal_vproblem(struct internal_problem * problem,const char * file,int line,const char * fmt,va_list ap)8205796c8dcSSimon Schubert internal_vproblem (struct internal_problem *problem,
8215796c8dcSSimon Schubert const char *file, int line, const char *fmt, va_list ap)
8225796c8dcSSimon Schubert {
8235796c8dcSSimon Schubert static int dejavu;
8245796c8dcSSimon Schubert int quit_p;
8255796c8dcSSimon Schubert int dump_core_p;
8265796c8dcSSimon Schubert char *reason;
8275796c8dcSSimon Schubert
8285796c8dcSSimon Schubert /* Don't allow infinite error/warning recursion. */
8295796c8dcSSimon Schubert {
8305796c8dcSSimon Schubert static char msg[] = "Recursive internal problem.\n";
831cf7f2e2dSJohn Marino
8325796c8dcSSimon Schubert switch (dejavu)
8335796c8dcSSimon Schubert {
8345796c8dcSSimon Schubert case 0:
8355796c8dcSSimon Schubert dejavu = 1;
8365796c8dcSSimon Schubert break;
8375796c8dcSSimon Schubert case 1:
8385796c8dcSSimon Schubert dejavu = 2;
8395796c8dcSSimon Schubert fputs_unfiltered (msg, gdb_stderr);
840cf7f2e2dSJohn Marino abort (); /* NOTE: GDB has only three calls to abort(). */
8415796c8dcSSimon Schubert default:
8425796c8dcSSimon Schubert dejavu = 3;
8435796c8dcSSimon Schubert /* Newer GLIBC versions put the warn_unused_result attribute
8445796c8dcSSimon Schubert on write, but this is one of those rare cases where
8455796c8dcSSimon Schubert ignoring the return value is correct. Casting to (void)
8465796c8dcSSimon Schubert does not fix this problem. This is the solution suggested
8475796c8dcSSimon Schubert at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509. */
8485796c8dcSSimon Schubert if (write (STDERR_FILENO, msg, sizeof (msg)) != sizeof (msg))
849cf7f2e2dSJohn Marino abort (); /* NOTE: GDB has only three calls to abort(). */
8505796c8dcSSimon Schubert exit (1);
8515796c8dcSSimon Schubert }
8525796c8dcSSimon Schubert }
8535796c8dcSSimon Schubert
8545796c8dcSSimon Schubert /* Try to get the message out and at the start of a new line. */
8555796c8dcSSimon Schubert target_terminal_ours ();
8565796c8dcSSimon Schubert begin_line ();
8575796c8dcSSimon Schubert
8585796c8dcSSimon Schubert /* Create a string containing the full error/warning message. Need
8595796c8dcSSimon Schubert to call query with this full string, as otherwize the reason
8605796c8dcSSimon Schubert (error/warning) and question become separated. Format using a
8615796c8dcSSimon Schubert style similar to a compiler error message. Include extra detail
8625796c8dcSSimon Schubert so that the user knows that they are living on the edge. */
8635796c8dcSSimon Schubert {
8645796c8dcSSimon Schubert char *msg;
865cf7f2e2dSJohn Marino
8665796c8dcSSimon Schubert msg = xstrvprintf (fmt, ap);
867c50c785cSJohn Marino reason = xstrprintf ("%s:%d: %s: %s\n"
868c50c785cSJohn Marino "A problem internal to GDB has been detected,\n"
869c50c785cSJohn Marino "further debugging may prove unreliable.",
870c50c785cSJohn Marino file, line, problem->name, msg);
8715796c8dcSSimon Schubert xfree (msg);
8725796c8dcSSimon Schubert make_cleanup (xfree, reason);
8735796c8dcSSimon Schubert }
8745796c8dcSSimon Schubert
8755796c8dcSSimon Schubert if (problem->should_quit == internal_problem_ask)
8765796c8dcSSimon Schubert {
8775796c8dcSSimon Schubert /* Default (yes/batch case) is to quit GDB. When in batch mode
8785796c8dcSSimon Schubert this lessens the likelihood of GDB going into an infinite
8795796c8dcSSimon Schubert loop. */
880*ef5ccd6cSJohn Marino if (!confirm)
8815796c8dcSSimon Schubert {
8825796c8dcSSimon Schubert /* Emit the message and quit. */
8835796c8dcSSimon Schubert fputs_unfiltered (reason, gdb_stderr);
8845796c8dcSSimon Schubert fputs_unfiltered ("\n", gdb_stderr);
8855796c8dcSSimon Schubert quit_p = 1;
8865796c8dcSSimon Schubert }
8875796c8dcSSimon Schubert else
8885796c8dcSSimon Schubert quit_p = query (_("%s\nQuit this debugging session? "), reason);
8895796c8dcSSimon Schubert }
8905796c8dcSSimon Schubert else if (problem->should_quit == internal_problem_yes)
8915796c8dcSSimon Schubert quit_p = 1;
8925796c8dcSSimon Schubert else if (problem->should_quit == internal_problem_no)
8935796c8dcSSimon Schubert quit_p = 0;
8945796c8dcSSimon Schubert else
8955796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("bad switch"));
8965796c8dcSSimon Schubert
8975796c8dcSSimon Schubert if (problem->should_dump_core == internal_problem_ask)
8985796c8dcSSimon Schubert {
899cf7f2e2dSJohn Marino if (!can_dump_core (reason))
900cf7f2e2dSJohn Marino dump_core_p = 0;
901cf7f2e2dSJohn Marino else
902cf7f2e2dSJohn Marino {
9035796c8dcSSimon Schubert /* Default (yes/batch case) is to dump core. This leaves a GDB
9045796c8dcSSimon Schubert `dropping' so that it is easier to see that something went
9055796c8dcSSimon Schubert wrong in GDB. */
9065796c8dcSSimon Schubert dump_core_p = query (_("%s\nCreate a core file of GDB? "), reason);
9075796c8dcSSimon Schubert }
908cf7f2e2dSJohn Marino }
9095796c8dcSSimon Schubert else if (problem->should_dump_core == internal_problem_yes)
910cf7f2e2dSJohn Marino dump_core_p = can_dump_core (reason);
9115796c8dcSSimon Schubert else if (problem->should_dump_core == internal_problem_no)
9125796c8dcSSimon Schubert dump_core_p = 0;
9135796c8dcSSimon Schubert else
9145796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("bad switch"));
9155796c8dcSSimon Schubert
9165796c8dcSSimon Schubert if (quit_p)
9175796c8dcSSimon Schubert {
9185796c8dcSSimon Schubert if (dump_core_p)
919cf7f2e2dSJohn Marino dump_core ();
9205796c8dcSSimon Schubert else
9215796c8dcSSimon Schubert exit (1);
9225796c8dcSSimon Schubert }
9235796c8dcSSimon Schubert else
9245796c8dcSSimon Schubert {
9255796c8dcSSimon Schubert if (dump_core_p)
9265796c8dcSSimon Schubert {
9275796c8dcSSimon Schubert #ifdef HAVE_WORKING_FORK
9285796c8dcSSimon Schubert if (fork () == 0)
929cf7f2e2dSJohn Marino dump_core ();
9305796c8dcSSimon Schubert #endif
9315796c8dcSSimon Schubert }
9325796c8dcSSimon Schubert }
9335796c8dcSSimon Schubert
9345796c8dcSSimon Schubert dejavu = 0;
9355796c8dcSSimon Schubert }
9365796c8dcSSimon Schubert
9375796c8dcSSimon Schubert static struct internal_problem internal_error_problem = {
9385796c8dcSSimon Schubert "internal-error", internal_problem_ask, internal_problem_ask
9395796c8dcSSimon Schubert };
9405796c8dcSSimon Schubert
941cf7f2e2dSJohn Marino void
internal_verror(const char * file,int line,const char * fmt,va_list ap)9425796c8dcSSimon Schubert internal_verror (const char *file, int line, const char *fmt, va_list ap)
9435796c8dcSSimon Schubert {
9445796c8dcSSimon Schubert internal_vproblem (&internal_error_problem, file, line, fmt, ap);
9455796c8dcSSimon Schubert deprecated_throw_reason (RETURN_ERROR);
9465796c8dcSSimon Schubert }
9475796c8dcSSimon Schubert
948cf7f2e2dSJohn Marino void
internal_error(const char * file,int line,const char * string,...)9495796c8dcSSimon Schubert internal_error (const char *file, int line, const char *string, ...)
9505796c8dcSSimon Schubert {
9515796c8dcSSimon Schubert va_list ap;
952cf7f2e2dSJohn Marino
9535796c8dcSSimon Schubert va_start (ap, string);
9545796c8dcSSimon Schubert internal_verror (file, line, string, ap);
9555796c8dcSSimon Schubert va_end (ap);
9565796c8dcSSimon Schubert }
9575796c8dcSSimon Schubert
9585796c8dcSSimon Schubert static struct internal_problem internal_warning_problem = {
9595796c8dcSSimon Schubert "internal-warning", internal_problem_ask, internal_problem_ask
9605796c8dcSSimon Schubert };
9615796c8dcSSimon Schubert
9625796c8dcSSimon Schubert void
internal_vwarning(const char * file,int line,const char * fmt,va_list ap)9635796c8dcSSimon Schubert internal_vwarning (const char *file, int line, const char *fmt, va_list ap)
9645796c8dcSSimon Schubert {
9655796c8dcSSimon Schubert internal_vproblem (&internal_warning_problem, file, line, fmt, ap);
9665796c8dcSSimon Schubert }
9675796c8dcSSimon Schubert
9685796c8dcSSimon Schubert void
internal_warning(const char * file,int line,const char * string,...)9695796c8dcSSimon Schubert internal_warning (const char *file, int line, const char *string, ...)
9705796c8dcSSimon Schubert {
9715796c8dcSSimon Schubert va_list ap;
972cf7f2e2dSJohn Marino
9735796c8dcSSimon Schubert va_start (ap, string);
9745796c8dcSSimon Schubert internal_vwarning (file, line, string, ap);
9755796c8dcSSimon Schubert va_end (ap);
9765796c8dcSSimon Schubert }
9775796c8dcSSimon Schubert
9785796c8dcSSimon Schubert /* Dummy functions to keep add_prefix_cmd happy. */
9795796c8dcSSimon Schubert
9805796c8dcSSimon Schubert static void
set_internal_problem_cmd(char * args,int from_tty)9815796c8dcSSimon Schubert set_internal_problem_cmd (char *args, int from_tty)
9825796c8dcSSimon Schubert {
9835796c8dcSSimon Schubert }
9845796c8dcSSimon Schubert
9855796c8dcSSimon Schubert static void
show_internal_problem_cmd(char * args,int from_tty)9865796c8dcSSimon Schubert show_internal_problem_cmd (char *args, int from_tty)
9875796c8dcSSimon Schubert {
9885796c8dcSSimon Schubert }
9895796c8dcSSimon Schubert
9905796c8dcSSimon Schubert /* When GDB reports an internal problem (error or warning) it gives
9915796c8dcSSimon Schubert the user the opportunity to quit GDB and/or create a core file of
9925796c8dcSSimon Schubert the current debug session. This function registers a few commands
9935796c8dcSSimon Schubert that make it possible to specify that GDB should always or never
9945796c8dcSSimon Schubert quit or create a core file, without asking. The commands look
9955796c8dcSSimon Schubert like:
9965796c8dcSSimon Schubert
9975796c8dcSSimon Schubert maint set PROBLEM-NAME quit ask|yes|no
9985796c8dcSSimon Schubert maint show PROBLEM-NAME quit
9995796c8dcSSimon Schubert maint set PROBLEM-NAME corefile ask|yes|no
10005796c8dcSSimon Schubert maint show PROBLEM-NAME corefile
10015796c8dcSSimon Schubert
10025796c8dcSSimon Schubert Where PROBLEM-NAME is currently "internal-error" or
10035796c8dcSSimon Schubert "internal-warning". */
10045796c8dcSSimon Schubert
10055796c8dcSSimon Schubert static void
add_internal_problem_command(struct internal_problem * problem)10065796c8dcSSimon Schubert add_internal_problem_command (struct internal_problem *problem)
10075796c8dcSSimon Schubert {
10085796c8dcSSimon Schubert struct cmd_list_element **set_cmd_list;
10095796c8dcSSimon Schubert struct cmd_list_element **show_cmd_list;
10105796c8dcSSimon Schubert char *set_doc;
10115796c8dcSSimon Schubert char *show_doc;
10125796c8dcSSimon Schubert
10135796c8dcSSimon Schubert set_cmd_list = xmalloc (sizeof (*set_cmd_list));
10145796c8dcSSimon Schubert show_cmd_list = xmalloc (sizeof (*set_cmd_list));
10155796c8dcSSimon Schubert *set_cmd_list = NULL;
10165796c8dcSSimon Schubert *show_cmd_list = NULL;
10175796c8dcSSimon Schubert
10185796c8dcSSimon Schubert set_doc = xstrprintf (_("Configure what GDB does when %s is detected."),
10195796c8dcSSimon Schubert problem->name);
10205796c8dcSSimon Schubert
10215796c8dcSSimon Schubert show_doc = xstrprintf (_("Show what GDB does when %s is detected."),
10225796c8dcSSimon Schubert problem->name);
10235796c8dcSSimon Schubert
10245796c8dcSSimon Schubert add_prefix_cmd ((char*) problem->name,
10255796c8dcSSimon Schubert class_maintenance, set_internal_problem_cmd, set_doc,
10265796c8dcSSimon Schubert set_cmd_list,
1027cf7f2e2dSJohn Marino concat ("maintenance set ", problem->name, " ",
1028cf7f2e2dSJohn Marino (char *) NULL),
10295796c8dcSSimon Schubert 0/*allow-unknown*/, &maintenance_set_cmdlist);
10305796c8dcSSimon Schubert
10315796c8dcSSimon Schubert add_prefix_cmd ((char*) problem->name,
10325796c8dcSSimon Schubert class_maintenance, show_internal_problem_cmd, show_doc,
10335796c8dcSSimon Schubert show_cmd_list,
1034cf7f2e2dSJohn Marino concat ("maintenance show ", problem->name, " ",
1035cf7f2e2dSJohn Marino (char *) NULL),
10365796c8dcSSimon Schubert 0/*allow-unknown*/, &maintenance_show_cmdlist);
10375796c8dcSSimon Schubert
1038c50c785cSJohn Marino set_doc = xstrprintf (_("Set whether GDB should quit "
1039c50c785cSJohn Marino "when an %s is detected"),
10405796c8dcSSimon Schubert problem->name);
1041c50c785cSJohn Marino show_doc = xstrprintf (_("Show whether GDB will quit "
1042c50c785cSJohn Marino "when an %s is detected"),
10435796c8dcSSimon Schubert problem->name);
10445796c8dcSSimon Schubert add_setshow_enum_cmd ("quit", class_maintenance,
10455796c8dcSSimon Schubert internal_problem_modes,
10465796c8dcSSimon Schubert &problem->should_quit,
10475796c8dcSSimon Schubert set_doc,
10485796c8dcSSimon Schubert show_doc,
10495796c8dcSSimon Schubert NULL, /* help_doc */
10505796c8dcSSimon Schubert NULL, /* setfunc */
10515796c8dcSSimon Schubert NULL, /* showfunc */
10525796c8dcSSimon Schubert set_cmd_list,
10535796c8dcSSimon Schubert show_cmd_list);
10545796c8dcSSimon Schubert
10555796c8dcSSimon Schubert xfree (set_doc);
10565796c8dcSSimon Schubert xfree (show_doc);
10575796c8dcSSimon Schubert
1058c50c785cSJohn Marino set_doc = xstrprintf (_("Set whether GDB should create a core "
1059c50c785cSJohn Marino "file of GDB when %s is detected"),
10605796c8dcSSimon Schubert problem->name);
1061c50c785cSJohn Marino show_doc = xstrprintf (_("Show whether GDB will create a core "
1062c50c785cSJohn Marino "file of GDB when %s is detected"),
10635796c8dcSSimon Schubert problem->name);
10645796c8dcSSimon Schubert add_setshow_enum_cmd ("corefile", class_maintenance,
10655796c8dcSSimon Schubert internal_problem_modes,
10665796c8dcSSimon Schubert &problem->should_dump_core,
10675796c8dcSSimon Schubert set_doc,
10685796c8dcSSimon Schubert show_doc,
10695796c8dcSSimon Schubert NULL, /* help_doc */
10705796c8dcSSimon Schubert NULL, /* setfunc */
10715796c8dcSSimon Schubert NULL, /* showfunc */
10725796c8dcSSimon Schubert set_cmd_list,
10735796c8dcSSimon Schubert show_cmd_list);
10745796c8dcSSimon Schubert
10755796c8dcSSimon Schubert xfree (set_doc);
10765796c8dcSSimon Schubert xfree (show_doc);
10775796c8dcSSimon Schubert }
10785796c8dcSSimon Schubert
10795796c8dcSSimon Schubert /* Print the system error message for errno, and also mention STRING
1080*ef5ccd6cSJohn Marino as the file name for which the error was encountered. Use ERRCODE
1081*ef5ccd6cSJohn Marino for the thrown exception. Then return to command level. */
10825796c8dcSSimon Schubert
1083cf7f2e2dSJohn Marino void
throw_perror_with_name(enum errors errcode,const char * string)1084*ef5ccd6cSJohn Marino throw_perror_with_name (enum errors errcode, const char *string)
10855796c8dcSSimon Schubert {
10865796c8dcSSimon Schubert char *err;
10875796c8dcSSimon Schubert char *combined;
10885796c8dcSSimon Schubert
10895796c8dcSSimon Schubert err = safe_strerror (errno);
10905796c8dcSSimon Schubert combined = (char *) alloca (strlen (err) + strlen (string) + 3);
10915796c8dcSSimon Schubert strcpy (combined, string);
10925796c8dcSSimon Schubert strcat (combined, ": ");
10935796c8dcSSimon Schubert strcat (combined, err);
10945796c8dcSSimon Schubert
10955796c8dcSSimon Schubert /* I understand setting these is a matter of taste. Still, some people
10965796c8dcSSimon Schubert may clear errno but not know about bfd_error. Doing this here is not
10975796c8dcSSimon Schubert unreasonable. */
10985796c8dcSSimon Schubert bfd_set_error (bfd_error_no_error);
10995796c8dcSSimon Schubert errno = 0;
11005796c8dcSSimon Schubert
1101*ef5ccd6cSJohn Marino throw_error (errcode, _("%s."), combined);
1102*ef5ccd6cSJohn Marino }
1103*ef5ccd6cSJohn Marino
1104*ef5ccd6cSJohn Marino /* See throw_perror_with_name, ERRCODE defaults here to GENERIC_ERROR. */
1105*ef5ccd6cSJohn Marino
1106*ef5ccd6cSJohn Marino void
perror_with_name(const char * string)1107*ef5ccd6cSJohn Marino perror_with_name (const char *string)
1108*ef5ccd6cSJohn Marino {
1109*ef5ccd6cSJohn Marino throw_perror_with_name (GENERIC_ERROR, string);
11105796c8dcSSimon Schubert }
11115796c8dcSSimon Schubert
11125796c8dcSSimon Schubert /* Print the system error message for ERRCODE, and also mention STRING
11135796c8dcSSimon Schubert as the file name for which the error was encountered. */
11145796c8dcSSimon Schubert
11155796c8dcSSimon Schubert void
print_sys_errmsg(const char * string,int errcode)11165796c8dcSSimon Schubert print_sys_errmsg (const char *string, int errcode)
11175796c8dcSSimon Schubert {
11185796c8dcSSimon Schubert char *err;
11195796c8dcSSimon Schubert char *combined;
11205796c8dcSSimon Schubert
11215796c8dcSSimon Schubert err = safe_strerror (errcode);
11225796c8dcSSimon Schubert combined = (char *) alloca (strlen (err) + strlen (string) + 3);
11235796c8dcSSimon Schubert strcpy (combined, string);
11245796c8dcSSimon Schubert strcat (combined, ": ");
11255796c8dcSSimon Schubert strcat (combined, err);
11265796c8dcSSimon Schubert
11275796c8dcSSimon Schubert /* We want anything which was printed on stdout to come out first, before
11285796c8dcSSimon Schubert this message. */
11295796c8dcSSimon Schubert gdb_flush (gdb_stdout);
11305796c8dcSSimon Schubert fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
11315796c8dcSSimon Schubert }
11325796c8dcSSimon Schubert
11335796c8dcSSimon Schubert /* Control C eventually causes this to be called, at a convenient time. */
11345796c8dcSSimon Schubert
11355796c8dcSSimon Schubert void
quit(void)11365796c8dcSSimon Schubert quit (void)
11375796c8dcSSimon Schubert {
11385796c8dcSSimon Schubert #ifdef __MSDOS__
11395796c8dcSSimon Schubert /* No steenking SIGINT will ever be coming our way when the
11405796c8dcSSimon Schubert program is resumed. Don't lie. */
11415796c8dcSSimon Schubert fatal ("Quit");
11425796c8dcSSimon Schubert #else
11435796c8dcSSimon Schubert if (job_control
11445796c8dcSSimon Schubert /* If there is no terminal switching for this target, then we can't
11455796c8dcSSimon Schubert possibly get screwed by the lack of job control. */
11465796c8dcSSimon Schubert || current_target.to_terminal_ours == NULL)
11475796c8dcSSimon Schubert fatal ("Quit");
11485796c8dcSSimon Schubert else
11495796c8dcSSimon Schubert fatal ("Quit (expect signal SIGINT when the program is resumed)");
11505796c8dcSSimon Schubert #endif
11515796c8dcSSimon Schubert }
11525796c8dcSSimon Schubert
11535796c8dcSSimon Schubert
11545796c8dcSSimon Schubert /* Called when a memory allocation fails, with the number of bytes of
11555796c8dcSSimon Schubert memory requested in SIZE. */
11565796c8dcSSimon Schubert
1157cf7f2e2dSJohn Marino void
malloc_failure(long size)1158a45ae5f8SJohn Marino malloc_failure (long size)
11595796c8dcSSimon Schubert {
11605796c8dcSSimon Schubert if (size > 0)
11615796c8dcSSimon Schubert {
11625796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
11635796c8dcSSimon Schubert _("virtual memory exhausted: can't allocate %ld bytes."),
11645796c8dcSSimon Schubert size);
11655796c8dcSSimon Schubert }
11665796c8dcSSimon Schubert else
11675796c8dcSSimon Schubert {
11685796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("virtual memory exhausted."));
11695796c8dcSSimon Schubert }
11705796c8dcSSimon Schubert }
11715796c8dcSSimon Schubert
11725796c8dcSSimon Schubert /* My replacement for the read system call.
11735796c8dcSSimon Schubert Used like `read' but keeps going if `read' returns too soon. */
11745796c8dcSSimon Schubert
11755796c8dcSSimon Schubert int
myread(int desc,char * addr,int len)11765796c8dcSSimon Schubert myread (int desc, char *addr, int len)
11775796c8dcSSimon Schubert {
11785796c8dcSSimon Schubert int val;
11795796c8dcSSimon Schubert int orglen = len;
11805796c8dcSSimon Schubert
11815796c8dcSSimon Schubert while (len > 0)
11825796c8dcSSimon Schubert {
11835796c8dcSSimon Schubert val = read (desc, addr, len);
11845796c8dcSSimon Schubert if (val < 0)
11855796c8dcSSimon Schubert return val;
11865796c8dcSSimon Schubert if (val == 0)
11875796c8dcSSimon Schubert return orglen - len;
11885796c8dcSSimon Schubert len -= val;
11895796c8dcSSimon Schubert addr += val;
11905796c8dcSSimon Schubert }
11915796c8dcSSimon Schubert return orglen;
11925796c8dcSSimon Schubert }
1193a45ae5f8SJohn Marino
11945796c8dcSSimon Schubert void
print_spaces(int n,struct ui_file * file)11955796c8dcSSimon Schubert print_spaces (int n, struct ui_file *file)
11965796c8dcSSimon Schubert {
11975796c8dcSSimon Schubert fputs_unfiltered (n_spaces (n), file);
11985796c8dcSSimon Schubert }
11995796c8dcSSimon Schubert
12005796c8dcSSimon Schubert /* Print a host address. */
12015796c8dcSSimon Schubert
12025796c8dcSSimon Schubert void
gdb_print_host_address(const void * addr,struct ui_file * stream)12035796c8dcSSimon Schubert gdb_print_host_address (const void *addr, struct ui_file *stream)
12045796c8dcSSimon Schubert {
12055796c8dcSSimon Schubert fprintf_filtered (stream, "%s", host_address_to_string (addr));
12065796c8dcSSimon Schubert }
12075796c8dcSSimon Schubert
12085796c8dcSSimon Schubert
1209c50c785cSJohn Marino /* A cleanup function that calls regfree. */
1210c50c785cSJohn Marino
1211c50c785cSJohn Marino static void
do_regfree_cleanup(void * r)1212c50c785cSJohn Marino do_regfree_cleanup (void *r)
1213c50c785cSJohn Marino {
1214c50c785cSJohn Marino regfree (r);
1215c50c785cSJohn Marino }
1216c50c785cSJohn Marino
1217c50c785cSJohn Marino /* Create a new cleanup that frees the compiled regular expression R. */
1218c50c785cSJohn Marino
1219c50c785cSJohn Marino struct cleanup *
make_regfree_cleanup(regex_t * r)1220c50c785cSJohn Marino make_regfree_cleanup (regex_t *r)
1221c50c785cSJohn Marino {
1222c50c785cSJohn Marino return make_cleanup (do_regfree_cleanup, r);
1223c50c785cSJohn Marino }
1224c50c785cSJohn Marino
1225c50c785cSJohn Marino /* Return an xmalloc'd error message resulting from a regular
1226c50c785cSJohn Marino expression compilation failure. */
1227c50c785cSJohn Marino
1228c50c785cSJohn Marino char *
get_regcomp_error(int code,regex_t * rx)1229c50c785cSJohn Marino get_regcomp_error (int code, regex_t *rx)
1230c50c785cSJohn Marino {
1231c50c785cSJohn Marino size_t length = regerror (code, rx, NULL, 0);
1232c50c785cSJohn Marino char *result = xmalloc (length);
1233c50c785cSJohn Marino
1234c50c785cSJohn Marino regerror (code, rx, result, length);
1235c50c785cSJohn Marino return result;
1236c50c785cSJohn Marino }
1237c50c785cSJohn Marino
1238c50c785cSJohn Marino
1239c50c785cSJohn Marino
12405796c8dcSSimon Schubert /* This function supports the query, nquery, and yquery functions.
12415796c8dcSSimon Schubert Ask user a y-or-n question and return 0 if answer is no, 1 if
12425796c8dcSSimon Schubert answer is yes, or default the answer to the specified default
12435796c8dcSSimon Schubert (for yquery or nquery). DEFCHAR may be 'y' or 'n' to provide a
12445796c8dcSSimon Schubert default answer, or '\0' for no default.
12455796c8dcSSimon Schubert CTLSTR is the control string and should end in "? ". It should
12465796c8dcSSimon Schubert not say how to answer, because we do that.
12475796c8dcSSimon Schubert ARGS are the arguments passed along with the CTLSTR argument to
12485796c8dcSSimon Schubert printf. */
12495796c8dcSSimon Schubert
1250cf7f2e2dSJohn Marino static int ATTRIBUTE_PRINTF (1, 0)
defaulted_query(const char * ctlstr,const char defchar,va_list args)12515796c8dcSSimon Schubert defaulted_query (const char *ctlstr, const char defchar, va_list args)
12525796c8dcSSimon Schubert {
12535796c8dcSSimon Schubert int answer;
12545796c8dcSSimon Schubert int ans2;
12555796c8dcSSimon Schubert int retval;
12565796c8dcSSimon Schubert int def_value;
12575796c8dcSSimon Schubert char def_answer, not_def_answer;
12585796c8dcSSimon Schubert char *y_string, *n_string, *question;
1259*ef5ccd6cSJohn Marino /* Used to add duration we waited for user to respond to
1260*ef5ccd6cSJohn Marino prompt_for_continue_wait_time. */
1261*ef5ccd6cSJohn Marino struct timeval prompt_started, prompt_ended, prompt_delta;
12625796c8dcSSimon Schubert
12635796c8dcSSimon Schubert /* Set up according to which answer is the default. */
12645796c8dcSSimon Schubert if (defchar == '\0')
12655796c8dcSSimon Schubert {
12665796c8dcSSimon Schubert def_value = 1;
12675796c8dcSSimon Schubert def_answer = 'Y';
12685796c8dcSSimon Schubert not_def_answer = 'N';
12695796c8dcSSimon Schubert y_string = "y";
12705796c8dcSSimon Schubert n_string = "n";
12715796c8dcSSimon Schubert }
12725796c8dcSSimon Schubert else if (defchar == 'y')
12735796c8dcSSimon Schubert {
12745796c8dcSSimon Schubert def_value = 1;
12755796c8dcSSimon Schubert def_answer = 'Y';
12765796c8dcSSimon Schubert not_def_answer = 'N';
12775796c8dcSSimon Schubert y_string = "[y]";
12785796c8dcSSimon Schubert n_string = "n";
12795796c8dcSSimon Schubert }
12805796c8dcSSimon Schubert else
12815796c8dcSSimon Schubert {
12825796c8dcSSimon Schubert def_value = 0;
12835796c8dcSSimon Schubert def_answer = 'N';
12845796c8dcSSimon Schubert not_def_answer = 'Y';
12855796c8dcSSimon Schubert y_string = "y";
12865796c8dcSSimon Schubert n_string = "[n]";
12875796c8dcSSimon Schubert }
12885796c8dcSSimon Schubert
12895796c8dcSSimon Schubert /* Automatically answer the default value if the user did not want
12905796c8dcSSimon Schubert prompts or the command was issued with the server prefix. */
1291*ef5ccd6cSJohn Marino if (!confirm || server_command)
12925796c8dcSSimon Schubert return def_value;
12935796c8dcSSimon Schubert
12945796c8dcSSimon Schubert /* If input isn't coming from the user directly, just say what
12955796c8dcSSimon Schubert question we're asking, and then answer the default automatically. This
12965796c8dcSSimon Schubert way, important error messages don't get lost when talking to GDB
12975796c8dcSSimon Schubert over a pipe. */
12985796c8dcSSimon Schubert if (! input_from_terminal_p ())
12995796c8dcSSimon Schubert {
13005796c8dcSSimon Schubert wrap_here ("");
13015796c8dcSSimon Schubert vfprintf_filtered (gdb_stdout, ctlstr, args);
13025796c8dcSSimon Schubert
1303c50c785cSJohn Marino printf_filtered (_("(%s or %s) [answered %c; "
1304c50c785cSJohn Marino "input not from terminal]\n"),
13055796c8dcSSimon Schubert y_string, n_string, def_answer);
13065796c8dcSSimon Schubert gdb_flush (gdb_stdout);
13075796c8dcSSimon Schubert
13085796c8dcSSimon Schubert return def_value;
13095796c8dcSSimon Schubert }
13105796c8dcSSimon Schubert
13115796c8dcSSimon Schubert if (deprecated_query_hook)
13125796c8dcSSimon Schubert {
13135796c8dcSSimon Schubert return deprecated_query_hook (ctlstr, args);
13145796c8dcSSimon Schubert }
13155796c8dcSSimon Schubert
13165796c8dcSSimon Schubert /* Format the question outside of the loop, to avoid reusing args. */
13175796c8dcSSimon Schubert question = xstrvprintf (ctlstr, args);
13185796c8dcSSimon Schubert
1319*ef5ccd6cSJohn Marino /* Used for calculating time spend waiting for user. */
1320*ef5ccd6cSJohn Marino gettimeofday (&prompt_started, NULL);
1321*ef5ccd6cSJohn Marino
13225796c8dcSSimon Schubert while (1)
13235796c8dcSSimon Schubert {
1324c50c785cSJohn Marino wrap_here (""); /* Flush any buffered output. */
13255796c8dcSSimon Schubert gdb_flush (gdb_stdout);
13265796c8dcSSimon Schubert
13275796c8dcSSimon Schubert if (annotation_level > 1)
13285796c8dcSSimon Schubert printf_filtered (("\n\032\032pre-query\n"));
13295796c8dcSSimon Schubert
13305796c8dcSSimon Schubert fputs_filtered (question, gdb_stdout);
13315796c8dcSSimon Schubert printf_filtered (_("(%s or %s) "), y_string, n_string);
13325796c8dcSSimon Schubert
13335796c8dcSSimon Schubert if (annotation_level > 1)
13345796c8dcSSimon Schubert printf_filtered (("\n\032\032query\n"));
13355796c8dcSSimon Schubert
13365796c8dcSSimon Schubert wrap_here ("");
13375796c8dcSSimon Schubert gdb_flush (gdb_stdout);
13385796c8dcSSimon Schubert
13395796c8dcSSimon Schubert answer = fgetc (stdin);
13405796c8dcSSimon Schubert
13415796c8dcSSimon Schubert /* We expect fgetc to block until a character is read. But
13425796c8dcSSimon Schubert this may not be the case if the terminal was opened with
13435796c8dcSSimon Schubert the NONBLOCK flag. In that case, if there is nothing to
13445796c8dcSSimon Schubert read on stdin, fgetc returns EOF, but also sets the error
13455796c8dcSSimon Schubert condition flag on stdin and errno to EAGAIN. With a true
13465796c8dcSSimon Schubert EOF, stdin's error condition flag is not set.
13475796c8dcSSimon Schubert
13485796c8dcSSimon Schubert A situation where this behavior was observed is a pseudo
13495796c8dcSSimon Schubert terminal on AIX. */
13505796c8dcSSimon Schubert while (answer == EOF && ferror (stdin) && errno == EAGAIN)
13515796c8dcSSimon Schubert {
13525796c8dcSSimon Schubert /* Not a real EOF. Wait a little while and try again until
13535796c8dcSSimon Schubert we read something. */
13545796c8dcSSimon Schubert clearerr (stdin);
13555796c8dcSSimon Schubert gdb_usleep (10000);
13565796c8dcSSimon Schubert answer = fgetc (stdin);
13575796c8dcSSimon Schubert }
13585796c8dcSSimon Schubert
13595796c8dcSSimon Schubert clearerr (stdin); /* in case of C-d */
13605796c8dcSSimon Schubert if (answer == EOF) /* C-d */
13615796c8dcSSimon Schubert {
13625796c8dcSSimon Schubert printf_filtered ("EOF [assumed %c]\n", def_answer);
13635796c8dcSSimon Schubert retval = def_value;
13645796c8dcSSimon Schubert break;
13655796c8dcSSimon Schubert }
1366c50c785cSJohn Marino /* Eat rest of input line, to EOF or newline. */
13675796c8dcSSimon Schubert if (answer != '\n')
13685796c8dcSSimon Schubert do
13695796c8dcSSimon Schubert {
13705796c8dcSSimon Schubert ans2 = fgetc (stdin);
13715796c8dcSSimon Schubert clearerr (stdin);
13725796c8dcSSimon Schubert }
13735796c8dcSSimon Schubert while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
13745796c8dcSSimon Schubert
13755796c8dcSSimon Schubert if (answer >= 'a')
13765796c8dcSSimon Schubert answer -= 040;
13775796c8dcSSimon Schubert /* Check answer. For the non-default, the user must specify
13785796c8dcSSimon Schubert the non-default explicitly. */
13795796c8dcSSimon Schubert if (answer == not_def_answer)
13805796c8dcSSimon Schubert {
13815796c8dcSSimon Schubert retval = !def_value;
13825796c8dcSSimon Schubert break;
13835796c8dcSSimon Schubert }
13845796c8dcSSimon Schubert /* Otherwise, if a default was specified, the user may either
13855796c8dcSSimon Schubert specify the required input or have it default by entering
13865796c8dcSSimon Schubert nothing. */
13875796c8dcSSimon Schubert if (answer == def_answer
13885796c8dcSSimon Schubert || (defchar != '\0' &&
13895796c8dcSSimon Schubert (answer == '\n' || answer == '\r' || answer == EOF)))
13905796c8dcSSimon Schubert {
13915796c8dcSSimon Schubert retval = def_value;
13925796c8dcSSimon Schubert break;
13935796c8dcSSimon Schubert }
13945796c8dcSSimon Schubert /* Invalid entries are not defaulted and require another selection. */
13955796c8dcSSimon Schubert printf_filtered (_("Please answer %s or %s.\n"),
13965796c8dcSSimon Schubert y_string, n_string);
13975796c8dcSSimon Schubert }
13985796c8dcSSimon Schubert
1399*ef5ccd6cSJohn Marino /* Add time spend in this routine to prompt_for_continue_wait_time. */
1400*ef5ccd6cSJohn Marino gettimeofday (&prompt_ended, NULL);
1401*ef5ccd6cSJohn Marino timeval_sub (&prompt_delta, &prompt_ended, &prompt_started);
1402*ef5ccd6cSJohn Marino timeval_add (&prompt_for_continue_wait_time,
1403*ef5ccd6cSJohn Marino &prompt_for_continue_wait_time, &prompt_delta);
1404*ef5ccd6cSJohn Marino
14055796c8dcSSimon Schubert xfree (question);
14065796c8dcSSimon Schubert if (annotation_level > 1)
14075796c8dcSSimon Schubert printf_filtered (("\n\032\032post-query\n"));
14085796c8dcSSimon Schubert return retval;
14095796c8dcSSimon Schubert }
14105796c8dcSSimon Schubert
14115796c8dcSSimon Schubert
14125796c8dcSSimon Schubert /* Ask user a y-or-n question and return 0 if answer is no, 1 if
14135796c8dcSSimon Schubert answer is yes, or 0 if answer is defaulted.
14145796c8dcSSimon Schubert Takes three args which are given to printf to print the question.
14155796c8dcSSimon Schubert The first, a control string, should end in "? ".
14165796c8dcSSimon Schubert It should not say how to answer, because we do that. */
14175796c8dcSSimon Schubert
14185796c8dcSSimon Schubert int
nquery(const char * ctlstr,...)14195796c8dcSSimon Schubert nquery (const char *ctlstr, ...)
14205796c8dcSSimon Schubert {
14215796c8dcSSimon Schubert va_list args;
1422c50c785cSJohn Marino int ret;
14235796c8dcSSimon Schubert
14245796c8dcSSimon Schubert va_start (args, ctlstr);
1425c50c785cSJohn Marino ret = defaulted_query (ctlstr, 'n', args);
14265796c8dcSSimon Schubert va_end (args);
1427c50c785cSJohn Marino return ret;
14285796c8dcSSimon Schubert }
14295796c8dcSSimon Schubert
14305796c8dcSSimon Schubert /* Ask user a y-or-n question and return 0 if answer is no, 1 if
14315796c8dcSSimon Schubert answer is yes, or 1 if answer is defaulted.
14325796c8dcSSimon Schubert Takes three args which are given to printf to print the question.
14335796c8dcSSimon Schubert The first, a control string, should end in "? ".
14345796c8dcSSimon Schubert It should not say how to answer, because we do that. */
14355796c8dcSSimon Schubert
14365796c8dcSSimon Schubert int
yquery(const char * ctlstr,...)14375796c8dcSSimon Schubert yquery (const char *ctlstr, ...)
14385796c8dcSSimon Schubert {
14395796c8dcSSimon Schubert va_list args;
1440c50c785cSJohn Marino int ret;
14415796c8dcSSimon Schubert
14425796c8dcSSimon Schubert va_start (args, ctlstr);
1443c50c785cSJohn Marino ret = defaulted_query (ctlstr, 'y', args);
14445796c8dcSSimon Schubert va_end (args);
1445c50c785cSJohn Marino return ret;
14465796c8dcSSimon Schubert }
14475796c8dcSSimon Schubert
14485796c8dcSSimon Schubert /* Ask user a y-or-n question and return 1 iff answer is yes.
14495796c8dcSSimon Schubert Takes three args which are given to printf to print the question.
14505796c8dcSSimon Schubert The first, a control string, should end in "? ".
14515796c8dcSSimon Schubert It should not say how to answer, because we do that. */
14525796c8dcSSimon Schubert
14535796c8dcSSimon Schubert int
query(const char * ctlstr,...)14545796c8dcSSimon Schubert query (const char *ctlstr, ...)
14555796c8dcSSimon Schubert {
14565796c8dcSSimon Schubert va_list args;
1457c50c785cSJohn Marino int ret;
14585796c8dcSSimon Schubert
14595796c8dcSSimon Schubert va_start (args, ctlstr);
1460c50c785cSJohn Marino ret = defaulted_query (ctlstr, '\0', args);
14615796c8dcSSimon Schubert va_end (args);
1462c50c785cSJohn Marino return ret;
14635796c8dcSSimon Schubert }
14645796c8dcSSimon Schubert
14655796c8dcSSimon Schubert /* A helper for parse_escape that converts a host character to a
14665796c8dcSSimon Schubert target character. C is the host character. If conversion is
14675796c8dcSSimon Schubert possible, then the target character is stored in *TARGET_C and the
14685796c8dcSSimon Schubert function returns 1. Otherwise, the function returns 0. */
14695796c8dcSSimon Schubert
14705796c8dcSSimon Schubert static int
host_char_to_target(struct gdbarch * gdbarch,int c,int * target_c)1471cf7f2e2dSJohn Marino host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c)
14725796c8dcSSimon Schubert {
14735796c8dcSSimon Schubert struct obstack host_data;
14745796c8dcSSimon Schubert char the_char = c;
14755796c8dcSSimon Schubert struct cleanup *cleanups;
14765796c8dcSSimon Schubert int result = 0;
14775796c8dcSSimon Schubert
14785796c8dcSSimon Schubert obstack_init (&host_data);
14795796c8dcSSimon Schubert cleanups = make_cleanup_obstack_free (&host_data);
14805796c8dcSSimon Schubert
1481cf7f2e2dSJohn Marino convert_between_encodings (target_charset (gdbarch), host_charset (),
1482*ef5ccd6cSJohn Marino (gdb_byte *) &the_char, 1, 1,
1483*ef5ccd6cSJohn Marino &host_data, translit_none);
14845796c8dcSSimon Schubert
14855796c8dcSSimon Schubert if (obstack_object_size (&host_data) == 1)
14865796c8dcSSimon Schubert {
14875796c8dcSSimon Schubert result = 1;
14885796c8dcSSimon Schubert *target_c = *(char *) obstack_base (&host_data);
14895796c8dcSSimon Schubert }
14905796c8dcSSimon Schubert
14915796c8dcSSimon Schubert do_cleanups (cleanups);
14925796c8dcSSimon Schubert return result;
14935796c8dcSSimon Schubert }
14945796c8dcSSimon Schubert
14955796c8dcSSimon Schubert /* Parse a C escape sequence. STRING_PTR points to a variable
14965796c8dcSSimon Schubert containing a pointer to the string to parse. That pointer
14975796c8dcSSimon Schubert should point to the character after the \. That pointer
14985796c8dcSSimon Schubert is updated past the characters we use. The value of the
14995796c8dcSSimon Schubert escape sequence is returned.
15005796c8dcSSimon Schubert
15015796c8dcSSimon Schubert A negative value means the sequence \ newline was seen,
15025796c8dcSSimon Schubert which is supposed to be equivalent to nothing at all.
15035796c8dcSSimon Schubert
15045796c8dcSSimon Schubert If \ is followed by a null character, we return a negative
15055796c8dcSSimon Schubert value and leave the string pointer pointing at the null character.
15065796c8dcSSimon Schubert
15075796c8dcSSimon Schubert If \ is followed by 000, we return 0 and leave the string pointer
15085796c8dcSSimon Schubert after the zeros. A value of 0 does not mean end of string. */
15095796c8dcSSimon Schubert
15105796c8dcSSimon Schubert int
parse_escape(struct gdbarch * gdbarch,char ** string_ptr)1511cf7f2e2dSJohn Marino parse_escape (struct gdbarch *gdbarch, char **string_ptr)
15125796c8dcSSimon Schubert {
1513c50c785cSJohn Marino int target_char = -2; /* Initialize to avoid GCC warnings. */
15145796c8dcSSimon Schubert int c = *(*string_ptr)++;
1515cf7f2e2dSJohn Marino
15165796c8dcSSimon Schubert switch (c)
15175796c8dcSSimon Schubert {
15185796c8dcSSimon Schubert case '\n':
15195796c8dcSSimon Schubert return -2;
15205796c8dcSSimon Schubert case 0:
15215796c8dcSSimon Schubert (*string_ptr)--;
15225796c8dcSSimon Schubert return 0;
15235796c8dcSSimon Schubert
15245796c8dcSSimon Schubert case '0':
15255796c8dcSSimon Schubert case '1':
15265796c8dcSSimon Schubert case '2':
15275796c8dcSSimon Schubert case '3':
15285796c8dcSSimon Schubert case '4':
15295796c8dcSSimon Schubert case '5':
15305796c8dcSSimon Schubert case '6':
15315796c8dcSSimon Schubert case '7':
15325796c8dcSSimon Schubert {
15335796c8dcSSimon Schubert int i = host_hex_value (c);
15345796c8dcSSimon Schubert int count = 0;
15355796c8dcSSimon Schubert while (++count < 3)
15365796c8dcSSimon Schubert {
15375796c8dcSSimon Schubert c = (**string_ptr);
15385796c8dcSSimon Schubert if (isdigit (c) && c != '8' && c != '9')
15395796c8dcSSimon Schubert {
15405796c8dcSSimon Schubert (*string_ptr)++;
15415796c8dcSSimon Schubert i *= 8;
15425796c8dcSSimon Schubert i += host_hex_value (c);
15435796c8dcSSimon Schubert }
15445796c8dcSSimon Schubert else
15455796c8dcSSimon Schubert {
15465796c8dcSSimon Schubert break;
15475796c8dcSSimon Schubert }
15485796c8dcSSimon Schubert }
15495796c8dcSSimon Schubert return i;
15505796c8dcSSimon Schubert }
15515796c8dcSSimon Schubert
15525796c8dcSSimon Schubert case 'a':
15535796c8dcSSimon Schubert c = '\a';
15545796c8dcSSimon Schubert break;
15555796c8dcSSimon Schubert case 'b':
15565796c8dcSSimon Schubert c = '\b';
15575796c8dcSSimon Schubert break;
15585796c8dcSSimon Schubert case 'f':
15595796c8dcSSimon Schubert c = '\f';
15605796c8dcSSimon Schubert break;
15615796c8dcSSimon Schubert case 'n':
15625796c8dcSSimon Schubert c = '\n';
15635796c8dcSSimon Schubert break;
15645796c8dcSSimon Schubert case 'r':
15655796c8dcSSimon Schubert c = '\r';
15665796c8dcSSimon Schubert break;
15675796c8dcSSimon Schubert case 't':
15685796c8dcSSimon Schubert c = '\t';
15695796c8dcSSimon Schubert break;
15705796c8dcSSimon Schubert case 'v':
15715796c8dcSSimon Schubert c = '\v';
15725796c8dcSSimon Schubert break;
15735796c8dcSSimon Schubert
15745796c8dcSSimon Schubert default:
15755796c8dcSSimon Schubert break;
15765796c8dcSSimon Schubert }
15775796c8dcSSimon Schubert
1578cf7f2e2dSJohn Marino if (!host_char_to_target (gdbarch, c, &target_char))
1579c50c785cSJohn Marino error (_("The escape sequence `\\%c' is equivalent to plain `%c',"
1580c50c785cSJohn Marino " which has no equivalent\nin the `%s' character set."),
1581c50c785cSJohn Marino c, c, target_charset (gdbarch));
15825796c8dcSSimon Schubert return target_char;
15835796c8dcSSimon Schubert }
15845796c8dcSSimon Schubert
15855796c8dcSSimon Schubert /* Print the character C on STREAM as part of the contents of a literal
15865796c8dcSSimon Schubert string whose delimiter is QUOTER. Note that this routine should only
15875796c8dcSSimon Schubert be call for printing things which are independent of the language
15885796c8dcSSimon Schubert of the program being debugged. */
15895796c8dcSSimon Schubert
15905796c8dcSSimon Schubert static void
printchar(int c,void (* do_fputs)(const char *,struct ui_file *),void (* do_fprintf)(struct ui_file *,const char *,...)ATTRIBUTE_FPTR_PRINTF_2,struct ui_file * stream,int quoter)15915796c8dcSSimon Schubert printchar (int c, void (*do_fputs) (const char *, struct ui_file *),
15925796c8dcSSimon Schubert void (*do_fprintf) (struct ui_file *, const char *, ...)
15935796c8dcSSimon Schubert ATTRIBUTE_FPTR_PRINTF_2, struct ui_file *stream, int quoter)
15945796c8dcSSimon Schubert {
15955796c8dcSSimon Schubert c &= 0xFF; /* Avoid sign bit follies */
15965796c8dcSSimon Schubert
15975796c8dcSSimon Schubert if (c < 0x20 || /* Low control chars */
15985796c8dcSSimon Schubert (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
15995796c8dcSSimon Schubert (sevenbit_strings && c >= 0x80))
16005796c8dcSSimon Schubert { /* high order bit set */
16015796c8dcSSimon Schubert switch (c)
16025796c8dcSSimon Schubert {
16035796c8dcSSimon Schubert case '\n':
16045796c8dcSSimon Schubert do_fputs ("\\n", stream);
16055796c8dcSSimon Schubert break;
16065796c8dcSSimon Schubert case '\b':
16075796c8dcSSimon Schubert do_fputs ("\\b", stream);
16085796c8dcSSimon Schubert break;
16095796c8dcSSimon Schubert case '\t':
16105796c8dcSSimon Schubert do_fputs ("\\t", stream);
16115796c8dcSSimon Schubert break;
16125796c8dcSSimon Schubert case '\f':
16135796c8dcSSimon Schubert do_fputs ("\\f", stream);
16145796c8dcSSimon Schubert break;
16155796c8dcSSimon Schubert case '\r':
16165796c8dcSSimon Schubert do_fputs ("\\r", stream);
16175796c8dcSSimon Schubert break;
16185796c8dcSSimon Schubert case '\033':
16195796c8dcSSimon Schubert do_fputs ("\\e", stream);
16205796c8dcSSimon Schubert break;
16215796c8dcSSimon Schubert case '\007':
16225796c8dcSSimon Schubert do_fputs ("\\a", stream);
16235796c8dcSSimon Schubert break;
16245796c8dcSSimon Schubert default:
16255796c8dcSSimon Schubert do_fprintf (stream, "\\%.3o", (unsigned int) c);
16265796c8dcSSimon Schubert break;
16275796c8dcSSimon Schubert }
16285796c8dcSSimon Schubert }
16295796c8dcSSimon Schubert else
16305796c8dcSSimon Schubert {
16315796c8dcSSimon Schubert if (c == '\\' || c == quoter)
16325796c8dcSSimon Schubert do_fputs ("\\", stream);
16335796c8dcSSimon Schubert do_fprintf (stream, "%c", c);
16345796c8dcSSimon Schubert }
16355796c8dcSSimon Schubert }
16365796c8dcSSimon Schubert
16375796c8dcSSimon Schubert /* Print the character C on STREAM as part of the contents of a
16385796c8dcSSimon Schubert literal string whose delimiter is QUOTER. Note that these routines
16395796c8dcSSimon Schubert should only be call for printing things which are independent of
16405796c8dcSSimon Schubert the language of the program being debugged. */
16415796c8dcSSimon Schubert
16425796c8dcSSimon Schubert void
fputstr_filtered(const char * str,int quoter,struct ui_file * stream)16435796c8dcSSimon Schubert fputstr_filtered (const char *str, int quoter, struct ui_file *stream)
16445796c8dcSSimon Schubert {
16455796c8dcSSimon Schubert while (*str)
16465796c8dcSSimon Schubert printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter);
16475796c8dcSSimon Schubert }
16485796c8dcSSimon Schubert
16495796c8dcSSimon Schubert void
fputstr_unfiltered(const char * str,int quoter,struct ui_file * stream)16505796c8dcSSimon Schubert fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream)
16515796c8dcSSimon Schubert {
16525796c8dcSSimon Schubert while (*str)
16535796c8dcSSimon Schubert printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter);
16545796c8dcSSimon Schubert }
16555796c8dcSSimon Schubert
16565796c8dcSSimon Schubert void
fputstrn_filtered(const char * str,int n,int quoter,struct ui_file * stream)16575796c8dcSSimon Schubert fputstrn_filtered (const char *str, int n, int quoter,
16585796c8dcSSimon Schubert struct ui_file *stream)
16595796c8dcSSimon Schubert {
16605796c8dcSSimon Schubert int i;
1661cf7f2e2dSJohn Marino
16625796c8dcSSimon Schubert for (i = 0; i < n; i++)
16635796c8dcSSimon Schubert printchar (str[i], fputs_filtered, fprintf_filtered, stream, quoter);
16645796c8dcSSimon Schubert }
16655796c8dcSSimon Schubert
16665796c8dcSSimon Schubert void
fputstrn_unfiltered(const char * str,int n,int quoter,struct ui_file * stream)16675796c8dcSSimon Schubert fputstrn_unfiltered (const char *str, int n, int quoter,
16685796c8dcSSimon Schubert struct ui_file *stream)
16695796c8dcSSimon Schubert {
16705796c8dcSSimon Schubert int i;
1671cf7f2e2dSJohn Marino
16725796c8dcSSimon Schubert for (i = 0; i < n; i++)
16735796c8dcSSimon Schubert printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter);
16745796c8dcSSimon Schubert }
16755796c8dcSSimon Schubert
16765796c8dcSSimon Schubert
16775796c8dcSSimon Schubert /* Number of lines per page or UINT_MAX if paging is disabled. */
16785796c8dcSSimon Schubert static unsigned int lines_per_page;
16795796c8dcSSimon Schubert static void
show_lines_per_page(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)16805796c8dcSSimon Schubert show_lines_per_page (struct ui_file *file, int from_tty,
16815796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
16825796c8dcSSimon Schubert {
1683c50c785cSJohn Marino fprintf_filtered (file,
1684c50c785cSJohn Marino _("Number of lines gdb thinks are in a page is %s.\n"),
16855796c8dcSSimon Schubert value);
16865796c8dcSSimon Schubert }
16875796c8dcSSimon Schubert
16885796c8dcSSimon Schubert /* Number of chars per line or UINT_MAX if line folding is disabled. */
16895796c8dcSSimon Schubert static unsigned int chars_per_line;
16905796c8dcSSimon Schubert static void
show_chars_per_line(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)16915796c8dcSSimon Schubert show_chars_per_line (struct ui_file *file, int from_tty,
16925796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
16935796c8dcSSimon Schubert {
1694c50c785cSJohn Marino fprintf_filtered (file,
1695c50c785cSJohn Marino _("Number of characters gdb thinks "
1696c50c785cSJohn Marino "are in a line is %s.\n"),
16975796c8dcSSimon Schubert value);
16985796c8dcSSimon Schubert }
16995796c8dcSSimon Schubert
17005796c8dcSSimon Schubert /* Current count of lines printed on this page, chars on this line. */
17015796c8dcSSimon Schubert static unsigned int lines_printed, chars_printed;
17025796c8dcSSimon Schubert
17035796c8dcSSimon Schubert /* Buffer and start column of buffered text, for doing smarter word-
17045796c8dcSSimon Schubert wrapping. When someone calls wrap_here(), we start buffering output
17055796c8dcSSimon Schubert that comes through fputs_filtered(). If we see a newline, we just
17065796c8dcSSimon Schubert spit it out and forget about the wrap_here(). If we see another
17075796c8dcSSimon Schubert wrap_here(), we spit it out and remember the newer one. If we see
17085796c8dcSSimon Schubert the end of the line, we spit out a newline, the indent, and then
17095796c8dcSSimon Schubert the buffered output. */
17105796c8dcSSimon Schubert
17115796c8dcSSimon Schubert /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
17125796c8dcSSimon Schubert are waiting to be output (they have already been counted in chars_printed).
17135796c8dcSSimon Schubert When wrap_buffer[0] is null, the buffer is empty. */
17145796c8dcSSimon Schubert static char *wrap_buffer;
17155796c8dcSSimon Schubert
17165796c8dcSSimon Schubert /* Pointer in wrap_buffer to the next character to fill. */
17175796c8dcSSimon Schubert static char *wrap_pointer;
17185796c8dcSSimon Schubert
17195796c8dcSSimon Schubert /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
17205796c8dcSSimon Schubert is non-zero. */
17215796c8dcSSimon Schubert static char *wrap_indent;
17225796c8dcSSimon Schubert
17235796c8dcSSimon Schubert /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
17245796c8dcSSimon Schubert is not in effect. */
17255796c8dcSSimon Schubert static int wrap_column;
17265796c8dcSSimon Schubert
17275796c8dcSSimon Schubert
17285796c8dcSSimon Schubert /* Inialize the number of lines per page and chars per line. */
17295796c8dcSSimon Schubert
17305796c8dcSSimon Schubert void
init_page_info(void)17315796c8dcSSimon Schubert init_page_info (void)
17325796c8dcSSimon Schubert {
1733cf7f2e2dSJohn Marino if (batch_flag)
1734cf7f2e2dSJohn Marino {
1735cf7f2e2dSJohn Marino lines_per_page = UINT_MAX;
1736cf7f2e2dSJohn Marino chars_per_line = UINT_MAX;
1737cf7f2e2dSJohn Marino }
1738cf7f2e2dSJohn Marino else
17395796c8dcSSimon Schubert #if defined(TUI)
17405796c8dcSSimon Schubert if (!tui_get_command_dimension (&chars_per_line, &lines_per_page))
17415796c8dcSSimon Schubert #endif
17425796c8dcSSimon Schubert {
17435796c8dcSSimon Schubert int rows, cols;
17445796c8dcSSimon Schubert
17455796c8dcSSimon Schubert #if defined(__GO32__)
17465796c8dcSSimon Schubert rows = ScreenRows ();
17475796c8dcSSimon Schubert cols = ScreenCols ();
17485796c8dcSSimon Schubert lines_per_page = rows;
17495796c8dcSSimon Schubert chars_per_line = cols;
17505796c8dcSSimon Schubert #else
17515796c8dcSSimon Schubert /* Make sure Readline has initialized its terminal settings. */
17525796c8dcSSimon Schubert rl_reset_terminal (NULL);
17535796c8dcSSimon Schubert
17545796c8dcSSimon Schubert /* Get the screen size from Readline. */
17555796c8dcSSimon Schubert rl_get_screen_size (&rows, &cols);
17565796c8dcSSimon Schubert lines_per_page = rows;
17575796c8dcSSimon Schubert chars_per_line = cols;
17585796c8dcSSimon Schubert
17595796c8dcSSimon Schubert /* Readline should have fetched the termcap entry for us. */
17605796c8dcSSimon Schubert if (tgetnum ("li") < 0 || getenv ("EMACS"))
17615796c8dcSSimon Schubert {
17625796c8dcSSimon Schubert /* The number of lines per page is not mentioned in the
17635796c8dcSSimon Schubert terminal description. This probably means that paging is
17645796c8dcSSimon Schubert not useful (e.g. emacs shell window), so disable paging. */
17655796c8dcSSimon Schubert lines_per_page = UINT_MAX;
17665796c8dcSSimon Schubert }
17675796c8dcSSimon Schubert
17685796c8dcSSimon Schubert /* If the output is not a terminal, don't paginate it. */
17695796c8dcSSimon Schubert if (!ui_file_isatty (gdb_stdout))
17705796c8dcSSimon Schubert lines_per_page = UINT_MAX;
17715796c8dcSSimon Schubert #endif
17725796c8dcSSimon Schubert }
17735796c8dcSSimon Schubert
17745796c8dcSSimon Schubert set_screen_size ();
17755796c8dcSSimon Schubert set_width ();
17765796c8dcSSimon Schubert }
17775796c8dcSSimon Schubert
1778cf7f2e2dSJohn Marino /* Helper for make_cleanup_restore_page_info. */
1779cf7f2e2dSJohn Marino
1780cf7f2e2dSJohn Marino static void
do_restore_page_info_cleanup(void * arg)1781cf7f2e2dSJohn Marino do_restore_page_info_cleanup (void *arg)
1782cf7f2e2dSJohn Marino {
1783cf7f2e2dSJohn Marino set_screen_size ();
1784cf7f2e2dSJohn Marino set_width ();
1785cf7f2e2dSJohn Marino }
1786cf7f2e2dSJohn Marino
1787cf7f2e2dSJohn Marino /* Provide cleanup for restoring the terminal size. */
1788cf7f2e2dSJohn Marino
1789cf7f2e2dSJohn Marino struct cleanup *
make_cleanup_restore_page_info(void)1790cf7f2e2dSJohn Marino make_cleanup_restore_page_info (void)
1791cf7f2e2dSJohn Marino {
1792cf7f2e2dSJohn Marino struct cleanup *back_to;
1793cf7f2e2dSJohn Marino
1794cf7f2e2dSJohn Marino back_to = make_cleanup (do_restore_page_info_cleanup, NULL);
1795cf7f2e2dSJohn Marino make_cleanup_restore_uinteger (&lines_per_page);
1796cf7f2e2dSJohn Marino make_cleanup_restore_uinteger (&chars_per_line);
1797cf7f2e2dSJohn Marino
1798cf7f2e2dSJohn Marino return back_to;
1799cf7f2e2dSJohn Marino }
1800cf7f2e2dSJohn Marino
1801cf7f2e2dSJohn Marino /* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
1802cf7f2e2dSJohn Marino Provide cleanup for restoring the original state. */
1803cf7f2e2dSJohn Marino
1804cf7f2e2dSJohn Marino struct cleanup *
set_batch_flag_and_make_cleanup_restore_page_info(void)1805cf7f2e2dSJohn Marino set_batch_flag_and_make_cleanup_restore_page_info (void)
1806cf7f2e2dSJohn Marino {
1807cf7f2e2dSJohn Marino struct cleanup *back_to = make_cleanup_restore_page_info ();
1808cf7f2e2dSJohn Marino
1809cf7f2e2dSJohn Marino make_cleanup_restore_integer (&batch_flag);
1810cf7f2e2dSJohn Marino batch_flag = 1;
1811cf7f2e2dSJohn Marino init_page_info ();
1812cf7f2e2dSJohn Marino
1813cf7f2e2dSJohn Marino return back_to;
1814cf7f2e2dSJohn Marino }
1815cf7f2e2dSJohn Marino
18165796c8dcSSimon Schubert /* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE. */
18175796c8dcSSimon Schubert
18185796c8dcSSimon Schubert static void
set_screen_size(void)18195796c8dcSSimon Schubert set_screen_size (void)
18205796c8dcSSimon Schubert {
18215796c8dcSSimon Schubert int rows = lines_per_page;
18225796c8dcSSimon Schubert int cols = chars_per_line;
18235796c8dcSSimon Schubert
18245796c8dcSSimon Schubert if (rows <= 0)
18255796c8dcSSimon Schubert rows = INT_MAX;
18265796c8dcSSimon Schubert
18275796c8dcSSimon Schubert if (cols <= 0)
18285796c8dcSSimon Schubert cols = INT_MAX;
18295796c8dcSSimon Schubert
18305796c8dcSSimon Schubert /* Update Readline's idea of the terminal size. */
18315796c8dcSSimon Schubert rl_set_screen_size (rows, cols);
18325796c8dcSSimon Schubert }
18335796c8dcSSimon Schubert
18345796c8dcSSimon Schubert /* Reinitialize WRAP_BUFFER according to the current value of
18355796c8dcSSimon Schubert CHARS_PER_LINE. */
18365796c8dcSSimon Schubert
18375796c8dcSSimon Schubert static void
set_width(void)18385796c8dcSSimon Schubert set_width (void)
18395796c8dcSSimon Schubert {
18405796c8dcSSimon Schubert if (chars_per_line == 0)
18415796c8dcSSimon Schubert init_page_info ();
18425796c8dcSSimon Schubert
18435796c8dcSSimon Schubert if (!wrap_buffer)
18445796c8dcSSimon Schubert {
18455796c8dcSSimon Schubert wrap_buffer = (char *) xmalloc (chars_per_line + 2);
18465796c8dcSSimon Schubert wrap_buffer[0] = '\0';
18475796c8dcSSimon Schubert }
18485796c8dcSSimon Schubert else
18495796c8dcSSimon Schubert wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
18505796c8dcSSimon Schubert wrap_pointer = wrap_buffer; /* Start it at the beginning. */
18515796c8dcSSimon Schubert }
18525796c8dcSSimon Schubert
18535796c8dcSSimon Schubert static void
set_width_command(char * args,int from_tty,struct cmd_list_element * c)18545796c8dcSSimon Schubert set_width_command (char *args, int from_tty, struct cmd_list_element *c)
18555796c8dcSSimon Schubert {
18565796c8dcSSimon Schubert set_screen_size ();
18575796c8dcSSimon Schubert set_width ();
18585796c8dcSSimon Schubert }
18595796c8dcSSimon Schubert
18605796c8dcSSimon Schubert static void
set_height_command(char * args,int from_tty,struct cmd_list_element * c)18615796c8dcSSimon Schubert set_height_command (char *args, int from_tty, struct cmd_list_element *c)
18625796c8dcSSimon Schubert {
18635796c8dcSSimon Schubert set_screen_size ();
18645796c8dcSSimon Schubert }
18655796c8dcSSimon Schubert
18665796c8dcSSimon Schubert /* Wait, so the user can read what's on the screen. Prompt the user
18675796c8dcSSimon Schubert to continue by pressing RETURN. */
18685796c8dcSSimon Schubert
18695796c8dcSSimon Schubert static void
prompt_for_continue(void)18705796c8dcSSimon Schubert prompt_for_continue (void)
18715796c8dcSSimon Schubert {
18725796c8dcSSimon Schubert char *ignore;
18735796c8dcSSimon Schubert char cont_prompt[120];
1874*ef5ccd6cSJohn Marino /* Used to add duration we waited for user to respond to
1875*ef5ccd6cSJohn Marino prompt_for_continue_wait_time. */
1876*ef5ccd6cSJohn Marino struct timeval prompt_started, prompt_ended, prompt_delta;
1877*ef5ccd6cSJohn Marino
1878*ef5ccd6cSJohn Marino gettimeofday (&prompt_started, NULL);
18795796c8dcSSimon Schubert
18805796c8dcSSimon Schubert if (annotation_level > 1)
18815796c8dcSSimon Schubert printf_unfiltered (("\n\032\032pre-prompt-for-continue\n"));
18825796c8dcSSimon Schubert
18835796c8dcSSimon Schubert strcpy (cont_prompt,
18845796c8dcSSimon Schubert "---Type <return> to continue, or q <return> to quit---");
18855796c8dcSSimon Schubert if (annotation_level > 1)
18865796c8dcSSimon Schubert strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
18875796c8dcSSimon Schubert
18885796c8dcSSimon Schubert /* We must do this *before* we call gdb_readline, else it will eventually
18895796c8dcSSimon Schubert call us -- thinking that we're trying to print beyond the end of the
18905796c8dcSSimon Schubert screen. */
18915796c8dcSSimon Schubert reinitialize_more_filter ();
18925796c8dcSSimon Schubert
18935796c8dcSSimon Schubert immediate_quit++;
1894*ef5ccd6cSJohn Marino QUIT;
18955796c8dcSSimon Schubert /* On a real operating system, the user can quit with SIGINT.
18965796c8dcSSimon Schubert But not on GO32.
18975796c8dcSSimon Schubert
18985796c8dcSSimon Schubert 'q' is provided on all systems so users don't have to change habits
18995796c8dcSSimon Schubert from system to system, and because telling them what to do in
19005796c8dcSSimon Schubert the prompt is more user-friendly than expecting them to think of
19015796c8dcSSimon Schubert SIGINT. */
19025796c8dcSSimon Schubert /* Call readline, not gdb_readline, because GO32 readline handles control-C
19035796c8dcSSimon Schubert whereas control-C to gdb_readline will cause the user to get dumped
19045796c8dcSSimon Schubert out to DOS. */
19055796c8dcSSimon Schubert ignore = gdb_readline_wrapper (cont_prompt);
19065796c8dcSSimon Schubert
1907*ef5ccd6cSJohn Marino /* Add time spend in this routine to prompt_for_continue_wait_time. */
1908*ef5ccd6cSJohn Marino gettimeofday (&prompt_ended, NULL);
1909*ef5ccd6cSJohn Marino timeval_sub (&prompt_delta, &prompt_ended, &prompt_started);
1910*ef5ccd6cSJohn Marino timeval_add (&prompt_for_continue_wait_time,
1911*ef5ccd6cSJohn Marino &prompt_for_continue_wait_time, &prompt_delta);
1912*ef5ccd6cSJohn Marino
19135796c8dcSSimon Schubert if (annotation_level > 1)
19145796c8dcSSimon Schubert printf_unfiltered (("\n\032\032post-prompt-for-continue\n"));
19155796c8dcSSimon Schubert
19165796c8dcSSimon Schubert if (ignore)
19175796c8dcSSimon Schubert {
19185796c8dcSSimon Schubert char *p = ignore;
1919cf7f2e2dSJohn Marino
19205796c8dcSSimon Schubert while (*p == ' ' || *p == '\t')
19215796c8dcSSimon Schubert ++p;
19225796c8dcSSimon Schubert if (p[0] == 'q')
1923*ef5ccd6cSJohn Marino quit ();
19245796c8dcSSimon Schubert xfree (ignore);
19255796c8dcSSimon Schubert }
19265796c8dcSSimon Schubert immediate_quit--;
19275796c8dcSSimon Schubert
19285796c8dcSSimon Schubert /* Now we have to do this again, so that GDB will know that it doesn't
19295796c8dcSSimon Schubert need to save the ---Type <return>--- line at the top of the screen. */
19305796c8dcSSimon Schubert reinitialize_more_filter ();
19315796c8dcSSimon Schubert
19325796c8dcSSimon Schubert dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
19335796c8dcSSimon Schubert }
19345796c8dcSSimon Schubert
19355796c8dcSSimon Schubert /* Reinitialize filter; ie. tell it to reset to original values. */
19365796c8dcSSimon Schubert
19375796c8dcSSimon Schubert void
reinitialize_more_filter(void)19385796c8dcSSimon Schubert reinitialize_more_filter (void)
19395796c8dcSSimon Schubert {
19405796c8dcSSimon Schubert lines_printed = 0;
19415796c8dcSSimon Schubert chars_printed = 0;
19425796c8dcSSimon Schubert }
19435796c8dcSSimon Schubert
19445796c8dcSSimon Schubert /* Indicate that if the next sequence of characters overflows the line,
19455796c8dcSSimon Schubert a newline should be inserted here rather than when it hits the end.
19465796c8dcSSimon Schubert If INDENT is non-null, it is a string to be printed to indent the
19475796c8dcSSimon Schubert wrapped part on the next line. INDENT must remain accessible until
19485796c8dcSSimon Schubert the next call to wrap_here() or until a newline is printed through
19495796c8dcSSimon Schubert fputs_filtered().
19505796c8dcSSimon Schubert
19515796c8dcSSimon Schubert If the line is already overfull, we immediately print a newline and
19525796c8dcSSimon Schubert the indentation, and disable further wrapping.
19535796c8dcSSimon Schubert
19545796c8dcSSimon Schubert If we don't know the width of lines, but we know the page height,
19555796c8dcSSimon Schubert we must not wrap words, but should still keep track of newlines
19565796c8dcSSimon Schubert that were explicitly printed.
19575796c8dcSSimon Schubert
19585796c8dcSSimon Schubert INDENT should not contain tabs, as that will mess up the char count
19595796c8dcSSimon Schubert on the next line. FIXME.
19605796c8dcSSimon Schubert
19615796c8dcSSimon Schubert This routine is guaranteed to force out any output which has been
19625796c8dcSSimon Schubert squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
19635796c8dcSSimon Schubert used to force out output from the wrap_buffer. */
19645796c8dcSSimon Schubert
19655796c8dcSSimon Schubert void
wrap_here(char * indent)19665796c8dcSSimon Schubert wrap_here (char *indent)
19675796c8dcSSimon Schubert {
19685796c8dcSSimon Schubert /* This should have been allocated, but be paranoid anyway. */
19695796c8dcSSimon Schubert if (!wrap_buffer)
1970c50c785cSJohn Marino internal_error (__FILE__, __LINE__,
1971c50c785cSJohn Marino _("failed internal consistency check"));
19725796c8dcSSimon Schubert
19735796c8dcSSimon Schubert if (wrap_buffer[0])
19745796c8dcSSimon Schubert {
19755796c8dcSSimon Schubert *wrap_pointer = '\0';
19765796c8dcSSimon Schubert fputs_unfiltered (wrap_buffer, gdb_stdout);
19775796c8dcSSimon Schubert }
19785796c8dcSSimon Schubert wrap_pointer = wrap_buffer;
19795796c8dcSSimon Schubert wrap_buffer[0] = '\0';
1980c50c785cSJohn Marino if (chars_per_line == UINT_MAX) /* No line overflow checking. */
19815796c8dcSSimon Schubert {
19825796c8dcSSimon Schubert wrap_column = 0;
19835796c8dcSSimon Schubert }
19845796c8dcSSimon Schubert else if (chars_printed >= chars_per_line)
19855796c8dcSSimon Schubert {
19865796c8dcSSimon Schubert puts_filtered ("\n");
19875796c8dcSSimon Schubert if (indent != NULL)
19885796c8dcSSimon Schubert puts_filtered (indent);
19895796c8dcSSimon Schubert wrap_column = 0;
19905796c8dcSSimon Schubert }
19915796c8dcSSimon Schubert else
19925796c8dcSSimon Schubert {
19935796c8dcSSimon Schubert wrap_column = chars_printed;
19945796c8dcSSimon Schubert if (indent == NULL)
19955796c8dcSSimon Schubert wrap_indent = "";
19965796c8dcSSimon Schubert else
19975796c8dcSSimon Schubert wrap_indent = indent;
19985796c8dcSSimon Schubert }
19995796c8dcSSimon Schubert }
20005796c8dcSSimon Schubert
20015796c8dcSSimon Schubert /* Print input string to gdb_stdout, filtered, with wrap,
20025796c8dcSSimon Schubert arranging strings in columns of n chars. String can be
20035796c8dcSSimon Schubert right or left justified in the column. Never prints
20045796c8dcSSimon Schubert trailing spaces. String should never be longer than
20055796c8dcSSimon Schubert width. FIXME: this could be useful for the EXAMINE
2006c50c785cSJohn Marino command, which currently doesn't tabulate very well. */
20075796c8dcSSimon Schubert
20085796c8dcSSimon Schubert void
puts_filtered_tabular(char * string,int width,int right)20095796c8dcSSimon Schubert puts_filtered_tabular (char *string, int width, int right)
20105796c8dcSSimon Schubert {
20115796c8dcSSimon Schubert int spaces = 0;
20125796c8dcSSimon Schubert int stringlen;
20135796c8dcSSimon Schubert char *spacebuf;
20145796c8dcSSimon Schubert
20155796c8dcSSimon Schubert gdb_assert (chars_per_line > 0);
20165796c8dcSSimon Schubert if (chars_per_line == UINT_MAX)
20175796c8dcSSimon Schubert {
20185796c8dcSSimon Schubert fputs_filtered (string, gdb_stdout);
20195796c8dcSSimon Schubert fputs_filtered ("\n", gdb_stdout);
20205796c8dcSSimon Schubert return;
20215796c8dcSSimon Schubert }
20225796c8dcSSimon Schubert
20235796c8dcSSimon Schubert if (((chars_printed - 1) / width + 2) * width >= chars_per_line)
20245796c8dcSSimon Schubert fputs_filtered ("\n", gdb_stdout);
20255796c8dcSSimon Schubert
20265796c8dcSSimon Schubert if (width >= chars_per_line)
20275796c8dcSSimon Schubert width = chars_per_line - 1;
20285796c8dcSSimon Schubert
20295796c8dcSSimon Schubert stringlen = strlen (string);
20305796c8dcSSimon Schubert
20315796c8dcSSimon Schubert if (chars_printed > 0)
20325796c8dcSSimon Schubert spaces = width - (chars_printed - 1) % width - 1;
20335796c8dcSSimon Schubert if (right)
20345796c8dcSSimon Schubert spaces += width - stringlen;
20355796c8dcSSimon Schubert
20365796c8dcSSimon Schubert spacebuf = alloca (spaces + 1);
20375796c8dcSSimon Schubert spacebuf[spaces] = '\0';
20385796c8dcSSimon Schubert while (spaces--)
20395796c8dcSSimon Schubert spacebuf[spaces] = ' ';
20405796c8dcSSimon Schubert
20415796c8dcSSimon Schubert fputs_filtered (spacebuf, gdb_stdout);
20425796c8dcSSimon Schubert fputs_filtered (string, gdb_stdout);
20435796c8dcSSimon Schubert }
20445796c8dcSSimon Schubert
20455796c8dcSSimon Schubert
20465796c8dcSSimon Schubert /* Ensure that whatever gets printed next, using the filtered output
2047c50c785cSJohn Marino commands, starts at the beginning of the line. I.e. if there is
20485796c8dcSSimon Schubert any pending output for the current line, flush it and start a new
20495796c8dcSSimon Schubert line. Otherwise do nothing. */
20505796c8dcSSimon Schubert
20515796c8dcSSimon Schubert void
begin_line(void)20525796c8dcSSimon Schubert begin_line (void)
20535796c8dcSSimon Schubert {
20545796c8dcSSimon Schubert if (chars_printed > 0)
20555796c8dcSSimon Schubert {
20565796c8dcSSimon Schubert puts_filtered ("\n");
20575796c8dcSSimon Schubert }
20585796c8dcSSimon Schubert }
20595796c8dcSSimon Schubert
20605796c8dcSSimon Schubert
20615796c8dcSSimon Schubert /* Like fputs but if FILTER is true, pause after every screenful.
20625796c8dcSSimon Schubert
20635796c8dcSSimon Schubert Regardless of FILTER can wrap at points other than the final
20645796c8dcSSimon Schubert character of a line.
20655796c8dcSSimon Schubert
20665796c8dcSSimon Schubert Unlike fputs, fputs_maybe_filtered does not return a value.
20675796c8dcSSimon Schubert It is OK for LINEBUFFER to be NULL, in which case just don't print
20685796c8dcSSimon Schubert anything.
20695796c8dcSSimon Schubert
20705796c8dcSSimon Schubert Note that a longjmp to top level may occur in this routine (only if
20715796c8dcSSimon Schubert FILTER is true) (since prompt_for_continue may do so) so this
20725796c8dcSSimon Schubert routine should not be called when cleanups are not in place. */
20735796c8dcSSimon Schubert
20745796c8dcSSimon Schubert static void
fputs_maybe_filtered(const char * linebuffer,struct ui_file * stream,int filter)20755796c8dcSSimon Schubert fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
20765796c8dcSSimon Schubert int filter)
20775796c8dcSSimon Schubert {
20785796c8dcSSimon Schubert const char *lineptr;
20795796c8dcSSimon Schubert
20805796c8dcSSimon Schubert if (linebuffer == 0)
20815796c8dcSSimon Schubert return;
20825796c8dcSSimon Schubert
20835796c8dcSSimon Schubert /* Don't do any filtering if it is disabled. */
2084cf7f2e2dSJohn Marino if (stream != gdb_stdout
2085cf7f2e2dSJohn Marino || !pagination_enabled
2086a45ae5f8SJohn Marino || batch_flag
2087cf7f2e2dSJohn Marino || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)
2088cf7f2e2dSJohn Marino || top_level_interpreter () == NULL
2089cf7f2e2dSJohn Marino || ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())))
20905796c8dcSSimon Schubert {
20915796c8dcSSimon Schubert fputs_unfiltered (linebuffer, stream);
20925796c8dcSSimon Schubert return;
20935796c8dcSSimon Schubert }
20945796c8dcSSimon Schubert
20955796c8dcSSimon Schubert /* Go through and output each character. Show line extension
20965796c8dcSSimon Schubert when this is necessary; prompt user for new page when this is
20975796c8dcSSimon Schubert necessary. */
20985796c8dcSSimon Schubert
20995796c8dcSSimon Schubert lineptr = linebuffer;
21005796c8dcSSimon Schubert while (*lineptr)
21015796c8dcSSimon Schubert {
21025796c8dcSSimon Schubert /* Possible new page. */
21035796c8dcSSimon Schubert if (filter && (lines_printed >= lines_per_page - 1))
21045796c8dcSSimon Schubert prompt_for_continue ();
21055796c8dcSSimon Schubert
21065796c8dcSSimon Schubert while (*lineptr && *lineptr != '\n')
21075796c8dcSSimon Schubert {
21085796c8dcSSimon Schubert /* Print a single line. */
21095796c8dcSSimon Schubert if (*lineptr == '\t')
21105796c8dcSSimon Schubert {
21115796c8dcSSimon Schubert if (wrap_column)
21125796c8dcSSimon Schubert *wrap_pointer++ = '\t';
21135796c8dcSSimon Schubert else
21145796c8dcSSimon Schubert fputc_unfiltered ('\t', stream);
21155796c8dcSSimon Schubert /* Shifting right by 3 produces the number of tab stops
21165796c8dcSSimon Schubert we have already passed, and then adding one and
21175796c8dcSSimon Schubert shifting left 3 advances to the next tab stop. */
21185796c8dcSSimon Schubert chars_printed = ((chars_printed >> 3) + 1) << 3;
21195796c8dcSSimon Schubert lineptr++;
21205796c8dcSSimon Schubert }
21215796c8dcSSimon Schubert else
21225796c8dcSSimon Schubert {
21235796c8dcSSimon Schubert if (wrap_column)
21245796c8dcSSimon Schubert *wrap_pointer++ = *lineptr;
21255796c8dcSSimon Schubert else
21265796c8dcSSimon Schubert fputc_unfiltered (*lineptr, stream);
21275796c8dcSSimon Schubert chars_printed++;
21285796c8dcSSimon Schubert lineptr++;
21295796c8dcSSimon Schubert }
21305796c8dcSSimon Schubert
21315796c8dcSSimon Schubert if (chars_printed >= chars_per_line)
21325796c8dcSSimon Schubert {
21335796c8dcSSimon Schubert unsigned int save_chars = chars_printed;
21345796c8dcSSimon Schubert
21355796c8dcSSimon Schubert chars_printed = 0;
21365796c8dcSSimon Schubert lines_printed++;
21375796c8dcSSimon Schubert /* If we aren't actually wrapping, don't output newline --
21385796c8dcSSimon Schubert if chars_per_line is right, we probably just overflowed
21395796c8dcSSimon Schubert anyway; if it's wrong, let us keep going. */
21405796c8dcSSimon Schubert if (wrap_column)
21415796c8dcSSimon Schubert fputc_unfiltered ('\n', stream);
21425796c8dcSSimon Schubert
21435796c8dcSSimon Schubert /* Possible new page. */
21445796c8dcSSimon Schubert if (lines_printed >= lines_per_page - 1)
21455796c8dcSSimon Schubert prompt_for_continue ();
21465796c8dcSSimon Schubert
2147c50c785cSJohn Marino /* Now output indentation and wrapped string. */
21485796c8dcSSimon Schubert if (wrap_column)
21495796c8dcSSimon Schubert {
21505796c8dcSSimon Schubert fputs_unfiltered (wrap_indent, stream);
2151c50c785cSJohn Marino *wrap_pointer = '\0'; /* Null-terminate saved stuff, */
2152c50c785cSJohn Marino fputs_unfiltered (wrap_buffer, stream); /* and eject it. */
21535796c8dcSSimon Schubert /* FIXME, this strlen is what prevents wrap_indent from
21545796c8dcSSimon Schubert containing tabs. However, if we recurse to print it
21555796c8dcSSimon Schubert and count its chars, we risk trouble if wrap_indent is
21565796c8dcSSimon Schubert longer than (the user settable) chars_per_line.
21575796c8dcSSimon Schubert Note also that this can set chars_printed > chars_per_line
21585796c8dcSSimon Schubert if we are printing a long string. */
21595796c8dcSSimon Schubert chars_printed = strlen (wrap_indent)
21605796c8dcSSimon Schubert + (save_chars - wrap_column);
21615796c8dcSSimon Schubert wrap_pointer = wrap_buffer; /* Reset buffer */
21625796c8dcSSimon Schubert wrap_buffer[0] = '\0';
21635796c8dcSSimon Schubert wrap_column = 0; /* And disable fancy wrap */
21645796c8dcSSimon Schubert }
21655796c8dcSSimon Schubert }
21665796c8dcSSimon Schubert }
21675796c8dcSSimon Schubert
21685796c8dcSSimon Schubert if (*lineptr == '\n')
21695796c8dcSSimon Schubert {
21705796c8dcSSimon Schubert chars_printed = 0;
2171c50c785cSJohn Marino wrap_here ((char *) 0); /* Spit out chars, cancel
2172c50c785cSJohn Marino further wraps. */
21735796c8dcSSimon Schubert lines_printed++;
21745796c8dcSSimon Schubert fputc_unfiltered ('\n', stream);
21755796c8dcSSimon Schubert lineptr++;
21765796c8dcSSimon Schubert }
21775796c8dcSSimon Schubert }
21785796c8dcSSimon Schubert }
21795796c8dcSSimon Schubert
21805796c8dcSSimon Schubert void
fputs_filtered(const char * linebuffer,struct ui_file * stream)21815796c8dcSSimon Schubert fputs_filtered (const char *linebuffer, struct ui_file *stream)
21825796c8dcSSimon Schubert {
21835796c8dcSSimon Schubert fputs_maybe_filtered (linebuffer, stream, 1);
21845796c8dcSSimon Schubert }
21855796c8dcSSimon Schubert
21865796c8dcSSimon Schubert int
putchar_unfiltered(int c)21875796c8dcSSimon Schubert putchar_unfiltered (int c)
21885796c8dcSSimon Schubert {
21895796c8dcSSimon Schubert char buf = c;
2190cf7f2e2dSJohn Marino
21915796c8dcSSimon Schubert ui_file_write (gdb_stdout, &buf, 1);
21925796c8dcSSimon Schubert return c;
21935796c8dcSSimon Schubert }
21945796c8dcSSimon Schubert
21955796c8dcSSimon Schubert /* Write character C to gdb_stdout using GDB's paging mechanism and return C.
21965796c8dcSSimon Schubert May return nonlocally. */
21975796c8dcSSimon Schubert
21985796c8dcSSimon Schubert int
putchar_filtered(int c)21995796c8dcSSimon Schubert putchar_filtered (int c)
22005796c8dcSSimon Schubert {
22015796c8dcSSimon Schubert return fputc_filtered (c, gdb_stdout);
22025796c8dcSSimon Schubert }
22035796c8dcSSimon Schubert
22045796c8dcSSimon Schubert int
fputc_unfiltered(int c,struct ui_file * stream)22055796c8dcSSimon Schubert fputc_unfiltered (int c, struct ui_file *stream)
22065796c8dcSSimon Schubert {
22075796c8dcSSimon Schubert char buf = c;
2208cf7f2e2dSJohn Marino
22095796c8dcSSimon Schubert ui_file_write (stream, &buf, 1);
22105796c8dcSSimon Schubert return c;
22115796c8dcSSimon Schubert }
22125796c8dcSSimon Schubert
22135796c8dcSSimon Schubert int
fputc_filtered(int c,struct ui_file * stream)22145796c8dcSSimon Schubert fputc_filtered (int c, struct ui_file *stream)
22155796c8dcSSimon Schubert {
22165796c8dcSSimon Schubert char buf[2];
22175796c8dcSSimon Schubert
22185796c8dcSSimon Schubert buf[0] = c;
22195796c8dcSSimon Schubert buf[1] = 0;
22205796c8dcSSimon Schubert fputs_filtered (buf, stream);
22215796c8dcSSimon Schubert return c;
22225796c8dcSSimon Schubert }
22235796c8dcSSimon Schubert
22245796c8dcSSimon Schubert /* puts_debug is like fputs_unfiltered, except it prints special
22255796c8dcSSimon Schubert characters in printable fashion. */
22265796c8dcSSimon Schubert
22275796c8dcSSimon Schubert void
puts_debug(char * prefix,char * string,char * suffix)22285796c8dcSSimon Schubert puts_debug (char *prefix, char *string, char *suffix)
22295796c8dcSSimon Schubert {
22305796c8dcSSimon Schubert int ch;
22315796c8dcSSimon Schubert
22325796c8dcSSimon Schubert /* Print prefix and suffix after each line. */
22335796c8dcSSimon Schubert static int new_line = 1;
22345796c8dcSSimon Schubert static int return_p = 0;
22355796c8dcSSimon Schubert static char *prev_prefix = "";
22365796c8dcSSimon Schubert static char *prev_suffix = "";
22375796c8dcSSimon Schubert
22385796c8dcSSimon Schubert if (*string == '\n')
22395796c8dcSSimon Schubert return_p = 0;
22405796c8dcSSimon Schubert
22415796c8dcSSimon Schubert /* If the prefix is changing, print the previous suffix, a new line,
22425796c8dcSSimon Schubert and the new prefix. */
22435796c8dcSSimon Schubert if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line)
22445796c8dcSSimon Schubert {
22455796c8dcSSimon Schubert fputs_unfiltered (prev_suffix, gdb_stdlog);
22465796c8dcSSimon Schubert fputs_unfiltered ("\n", gdb_stdlog);
22475796c8dcSSimon Schubert fputs_unfiltered (prefix, gdb_stdlog);
22485796c8dcSSimon Schubert }
22495796c8dcSSimon Schubert
22505796c8dcSSimon Schubert /* Print prefix if we printed a newline during the previous call. */
22515796c8dcSSimon Schubert if (new_line)
22525796c8dcSSimon Schubert {
22535796c8dcSSimon Schubert new_line = 0;
22545796c8dcSSimon Schubert fputs_unfiltered (prefix, gdb_stdlog);
22555796c8dcSSimon Schubert }
22565796c8dcSSimon Schubert
22575796c8dcSSimon Schubert prev_prefix = prefix;
22585796c8dcSSimon Schubert prev_suffix = suffix;
22595796c8dcSSimon Schubert
22605796c8dcSSimon Schubert /* Output characters in a printable format. */
22615796c8dcSSimon Schubert while ((ch = *string++) != '\0')
22625796c8dcSSimon Schubert {
22635796c8dcSSimon Schubert switch (ch)
22645796c8dcSSimon Schubert {
22655796c8dcSSimon Schubert default:
22665796c8dcSSimon Schubert if (isprint (ch))
22675796c8dcSSimon Schubert fputc_unfiltered (ch, gdb_stdlog);
22685796c8dcSSimon Schubert
22695796c8dcSSimon Schubert else
22705796c8dcSSimon Schubert fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff);
22715796c8dcSSimon Schubert break;
22725796c8dcSSimon Schubert
22735796c8dcSSimon Schubert case '\\':
22745796c8dcSSimon Schubert fputs_unfiltered ("\\\\", gdb_stdlog);
22755796c8dcSSimon Schubert break;
22765796c8dcSSimon Schubert case '\b':
22775796c8dcSSimon Schubert fputs_unfiltered ("\\b", gdb_stdlog);
22785796c8dcSSimon Schubert break;
22795796c8dcSSimon Schubert case '\f':
22805796c8dcSSimon Schubert fputs_unfiltered ("\\f", gdb_stdlog);
22815796c8dcSSimon Schubert break;
22825796c8dcSSimon Schubert case '\n':
22835796c8dcSSimon Schubert new_line = 1;
22845796c8dcSSimon Schubert fputs_unfiltered ("\\n", gdb_stdlog);
22855796c8dcSSimon Schubert break;
22865796c8dcSSimon Schubert case '\r':
22875796c8dcSSimon Schubert fputs_unfiltered ("\\r", gdb_stdlog);
22885796c8dcSSimon Schubert break;
22895796c8dcSSimon Schubert case '\t':
22905796c8dcSSimon Schubert fputs_unfiltered ("\\t", gdb_stdlog);
22915796c8dcSSimon Schubert break;
22925796c8dcSSimon Schubert case '\v':
22935796c8dcSSimon Schubert fputs_unfiltered ("\\v", gdb_stdlog);
22945796c8dcSSimon Schubert break;
22955796c8dcSSimon Schubert }
22965796c8dcSSimon Schubert
22975796c8dcSSimon Schubert return_p = ch == '\r';
22985796c8dcSSimon Schubert }
22995796c8dcSSimon Schubert
23005796c8dcSSimon Schubert /* Print suffix if we printed a newline. */
23015796c8dcSSimon Schubert if (new_line)
23025796c8dcSSimon Schubert {
23035796c8dcSSimon Schubert fputs_unfiltered (suffix, gdb_stdlog);
23045796c8dcSSimon Schubert fputs_unfiltered ("\n", gdb_stdlog);
23055796c8dcSSimon Schubert }
23065796c8dcSSimon Schubert }
23075796c8dcSSimon Schubert
23085796c8dcSSimon Schubert
23095796c8dcSSimon Schubert /* Print a variable number of ARGS using format FORMAT. If this
23105796c8dcSSimon Schubert information is going to put the amount written (since the last call
23115796c8dcSSimon Schubert to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
23125796c8dcSSimon Schubert call prompt_for_continue to get the users permision to continue.
23135796c8dcSSimon Schubert
23145796c8dcSSimon Schubert Unlike fprintf, this function does not return a value.
23155796c8dcSSimon Schubert
23165796c8dcSSimon Schubert We implement three variants, vfprintf (takes a vararg list and stream),
23175796c8dcSSimon Schubert fprintf (takes a stream to write on), and printf (the usual).
23185796c8dcSSimon Schubert
23195796c8dcSSimon Schubert Note also that a longjmp to top level may occur in this routine
23205796c8dcSSimon Schubert (since prompt_for_continue may do so) so this routine should not be
23215796c8dcSSimon Schubert called when cleanups are not in place. */
23225796c8dcSSimon Schubert
23235796c8dcSSimon Schubert static void
vfprintf_maybe_filtered(struct ui_file * stream,const char * format,va_list args,int filter)23245796c8dcSSimon Schubert vfprintf_maybe_filtered (struct ui_file *stream, const char *format,
23255796c8dcSSimon Schubert va_list args, int filter)
23265796c8dcSSimon Schubert {
23275796c8dcSSimon Schubert char *linebuffer;
23285796c8dcSSimon Schubert struct cleanup *old_cleanups;
23295796c8dcSSimon Schubert
23305796c8dcSSimon Schubert linebuffer = xstrvprintf (format, args);
23315796c8dcSSimon Schubert old_cleanups = make_cleanup (xfree, linebuffer);
23325796c8dcSSimon Schubert fputs_maybe_filtered (linebuffer, stream, filter);
23335796c8dcSSimon Schubert do_cleanups (old_cleanups);
23345796c8dcSSimon Schubert }
23355796c8dcSSimon Schubert
23365796c8dcSSimon Schubert
23375796c8dcSSimon Schubert void
vfprintf_filtered(struct ui_file * stream,const char * format,va_list args)23385796c8dcSSimon Schubert vfprintf_filtered (struct ui_file *stream, const char *format, va_list args)
23395796c8dcSSimon Schubert {
23405796c8dcSSimon Schubert vfprintf_maybe_filtered (stream, format, args, 1);
23415796c8dcSSimon Schubert }
23425796c8dcSSimon Schubert
23435796c8dcSSimon Schubert void
vfprintf_unfiltered(struct ui_file * stream,const char * format,va_list args)23445796c8dcSSimon Schubert vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
23455796c8dcSSimon Schubert {
23465796c8dcSSimon Schubert char *linebuffer;
23475796c8dcSSimon Schubert struct cleanup *old_cleanups;
23485796c8dcSSimon Schubert
23495796c8dcSSimon Schubert linebuffer = xstrvprintf (format, args);
23505796c8dcSSimon Schubert old_cleanups = make_cleanup (xfree, linebuffer);
23515796c8dcSSimon Schubert if (debug_timestamp && stream == gdb_stdlog)
23525796c8dcSSimon Schubert {
23535796c8dcSSimon Schubert struct timeval tm;
23545796c8dcSSimon Schubert char *timestamp;
23555796c8dcSSimon Schubert int len, need_nl;
23565796c8dcSSimon Schubert
23575796c8dcSSimon Schubert gettimeofday (&tm, NULL);
23585796c8dcSSimon Schubert
23595796c8dcSSimon Schubert len = strlen (linebuffer);
23605796c8dcSSimon Schubert need_nl = (len > 0 && linebuffer[len - 1] != '\n');
23615796c8dcSSimon Schubert
23625796c8dcSSimon Schubert timestamp = xstrprintf ("%ld:%ld %s%s",
23635796c8dcSSimon Schubert (long) tm.tv_sec, (long) tm.tv_usec,
23645796c8dcSSimon Schubert linebuffer,
23655796c8dcSSimon Schubert need_nl ? "\n": "");
23665796c8dcSSimon Schubert make_cleanup (xfree, timestamp);
23675796c8dcSSimon Schubert fputs_unfiltered (timestamp, stream);
23685796c8dcSSimon Schubert }
23695796c8dcSSimon Schubert else
23705796c8dcSSimon Schubert fputs_unfiltered (linebuffer, stream);
23715796c8dcSSimon Schubert do_cleanups (old_cleanups);
23725796c8dcSSimon Schubert }
23735796c8dcSSimon Schubert
23745796c8dcSSimon Schubert void
vprintf_filtered(const char * format,va_list args)23755796c8dcSSimon Schubert vprintf_filtered (const char *format, va_list args)
23765796c8dcSSimon Schubert {
23775796c8dcSSimon Schubert vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
23785796c8dcSSimon Schubert }
23795796c8dcSSimon Schubert
23805796c8dcSSimon Schubert void
vprintf_unfiltered(const char * format,va_list args)23815796c8dcSSimon Schubert vprintf_unfiltered (const char *format, va_list args)
23825796c8dcSSimon Schubert {
23835796c8dcSSimon Schubert vfprintf_unfiltered (gdb_stdout, format, args);
23845796c8dcSSimon Schubert }
23855796c8dcSSimon Schubert
23865796c8dcSSimon Schubert void
fprintf_filtered(struct ui_file * stream,const char * format,...)23875796c8dcSSimon Schubert fprintf_filtered (struct ui_file *stream, const char *format, ...)
23885796c8dcSSimon Schubert {
23895796c8dcSSimon Schubert va_list args;
2390cf7f2e2dSJohn Marino
23915796c8dcSSimon Schubert va_start (args, format);
23925796c8dcSSimon Schubert vfprintf_filtered (stream, format, args);
23935796c8dcSSimon Schubert va_end (args);
23945796c8dcSSimon Schubert }
23955796c8dcSSimon Schubert
23965796c8dcSSimon Schubert void
fprintf_unfiltered(struct ui_file * stream,const char * format,...)23975796c8dcSSimon Schubert fprintf_unfiltered (struct ui_file *stream, const char *format, ...)
23985796c8dcSSimon Schubert {
23995796c8dcSSimon Schubert va_list args;
2400cf7f2e2dSJohn Marino
24015796c8dcSSimon Schubert va_start (args, format);
24025796c8dcSSimon Schubert vfprintf_unfiltered (stream, format, args);
24035796c8dcSSimon Schubert va_end (args);
24045796c8dcSSimon Schubert }
24055796c8dcSSimon Schubert
24065796c8dcSSimon Schubert /* Like fprintf_filtered, but prints its result indented.
24075796c8dcSSimon Schubert Called as fprintfi_filtered (spaces, stream, format, ...); */
24085796c8dcSSimon Schubert
24095796c8dcSSimon Schubert void
fprintfi_filtered(int spaces,struct ui_file * stream,const char * format,...)24105796c8dcSSimon Schubert fprintfi_filtered (int spaces, struct ui_file *stream, const char *format,
24115796c8dcSSimon Schubert ...)
24125796c8dcSSimon Schubert {
24135796c8dcSSimon Schubert va_list args;
2414cf7f2e2dSJohn Marino
24155796c8dcSSimon Schubert va_start (args, format);
24165796c8dcSSimon Schubert print_spaces_filtered (spaces, stream);
24175796c8dcSSimon Schubert
24185796c8dcSSimon Schubert vfprintf_filtered (stream, format, args);
24195796c8dcSSimon Schubert va_end (args);
24205796c8dcSSimon Schubert }
24215796c8dcSSimon Schubert
24225796c8dcSSimon Schubert
24235796c8dcSSimon Schubert void
printf_filtered(const char * format,...)24245796c8dcSSimon Schubert printf_filtered (const char *format, ...)
24255796c8dcSSimon Schubert {
24265796c8dcSSimon Schubert va_list args;
2427cf7f2e2dSJohn Marino
24285796c8dcSSimon Schubert va_start (args, format);
24295796c8dcSSimon Schubert vfprintf_filtered (gdb_stdout, format, args);
24305796c8dcSSimon Schubert va_end (args);
24315796c8dcSSimon Schubert }
24325796c8dcSSimon Schubert
24335796c8dcSSimon Schubert
24345796c8dcSSimon Schubert void
printf_unfiltered(const char * format,...)24355796c8dcSSimon Schubert printf_unfiltered (const char *format, ...)
24365796c8dcSSimon Schubert {
24375796c8dcSSimon Schubert va_list args;
2438cf7f2e2dSJohn Marino
24395796c8dcSSimon Schubert va_start (args, format);
24405796c8dcSSimon Schubert vfprintf_unfiltered (gdb_stdout, format, args);
24415796c8dcSSimon Schubert va_end (args);
24425796c8dcSSimon Schubert }
24435796c8dcSSimon Schubert
24445796c8dcSSimon Schubert /* Like printf_filtered, but prints it's result indented.
24455796c8dcSSimon Schubert Called as printfi_filtered (spaces, format, ...); */
24465796c8dcSSimon Schubert
24475796c8dcSSimon Schubert void
printfi_filtered(int spaces,const char * format,...)24485796c8dcSSimon Schubert printfi_filtered (int spaces, const char *format, ...)
24495796c8dcSSimon Schubert {
24505796c8dcSSimon Schubert va_list args;
2451cf7f2e2dSJohn Marino
24525796c8dcSSimon Schubert va_start (args, format);
24535796c8dcSSimon Schubert print_spaces_filtered (spaces, gdb_stdout);
24545796c8dcSSimon Schubert vfprintf_filtered (gdb_stdout, format, args);
24555796c8dcSSimon Schubert va_end (args);
24565796c8dcSSimon Schubert }
24575796c8dcSSimon Schubert
24585796c8dcSSimon Schubert /* Easy -- but watch out!
24595796c8dcSSimon Schubert
24605796c8dcSSimon Schubert This routine is *not* a replacement for puts()! puts() appends a newline.
24615796c8dcSSimon Schubert This one doesn't, and had better not! */
24625796c8dcSSimon Schubert
24635796c8dcSSimon Schubert void
puts_filtered(const char * string)24645796c8dcSSimon Schubert puts_filtered (const char *string)
24655796c8dcSSimon Schubert {
24665796c8dcSSimon Schubert fputs_filtered (string, gdb_stdout);
24675796c8dcSSimon Schubert }
24685796c8dcSSimon Schubert
24695796c8dcSSimon Schubert void
puts_unfiltered(const char * string)24705796c8dcSSimon Schubert puts_unfiltered (const char *string)
24715796c8dcSSimon Schubert {
24725796c8dcSSimon Schubert fputs_unfiltered (string, gdb_stdout);
24735796c8dcSSimon Schubert }
24745796c8dcSSimon Schubert
24755796c8dcSSimon Schubert /* Return a pointer to N spaces and a null. The pointer is good
24765796c8dcSSimon Schubert until the next call to here. */
24775796c8dcSSimon Schubert char *
n_spaces(int n)24785796c8dcSSimon Schubert n_spaces (int n)
24795796c8dcSSimon Schubert {
24805796c8dcSSimon Schubert char *t;
24815796c8dcSSimon Schubert static char *spaces = 0;
24825796c8dcSSimon Schubert static int max_spaces = -1;
24835796c8dcSSimon Schubert
24845796c8dcSSimon Schubert if (n > max_spaces)
24855796c8dcSSimon Schubert {
24865796c8dcSSimon Schubert if (spaces)
24875796c8dcSSimon Schubert xfree (spaces);
24885796c8dcSSimon Schubert spaces = (char *) xmalloc (n + 1);
24895796c8dcSSimon Schubert for (t = spaces + n; t != spaces;)
24905796c8dcSSimon Schubert *--t = ' ';
24915796c8dcSSimon Schubert spaces[n] = '\0';
24925796c8dcSSimon Schubert max_spaces = n;
24935796c8dcSSimon Schubert }
24945796c8dcSSimon Schubert
24955796c8dcSSimon Schubert return spaces + max_spaces - n;
24965796c8dcSSimon Schubert }
24975796c8dcSSimon Schubert
24985796c8dcSSimon Schubert /* Print N spaces. */
24995796c8dcSSimon Schubert void
print_spaces_filtered(int n,struct ui_file * stream)25005796c8dcSSimon Schubert print_spaces_filtered (int n, struct ui_file *stream)
25015796c8dcSSimon Schubert {
25025796c8dcSSimon Schubert fputs_filtered (n_spaces (n), stream);
25035796c8dcSSimon Schubert }
25045796c8dcSSimon Schubert
25055796c8dcSSimon Schubert /* C++/ObjC demangler stuff. */
25065796c8dcSSimon Schubert
25075796c8dcSSimon Schubert /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
25085796c8dcSSimon Schubert LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
25095796c8dcSSimon Schubert If the name is not mangled, or the language for the name is unknown, or
25105796c8dcSSimon Schubert demangling is off, the name is printed in its "raw" form. */
25115796c8dcSSimon Schubert
25125796c8dcSSimon Schubert void
fprintf_symbol_filtered(struct ui_file * stream,const char * name,enum language lang,int arg_mode)2513*ef5ccd6cSJohn Marino fprintf_symbol_filtered (struct ui_file *stream, const char *name,
25145796c8dcSSimon Schubert enum language lang, int arg_mode)
25155796c8dcSSimon Schubert {
25165796c8dcSSimon Schubert char *demangled;
25175796c8dcSSimon Schubert
25185796c8dcSSimon Schubert if (name != NULL)
25195796c8dcSSimon Schubert {
25205796c8dcSSimon Schubert /* If user wants to see raw output, no problem. */
25215796c8dcSSimon Schubert if (!demangle)
25225796c8dcSSimon Schubert {
25235796c8dcSSimon Schubert fputs_filtered (name, stream);
25245796c8dcSSimon Schubert }
25255796c8dcSSimon Schubert else
25265796c8dcSSimon Schubert {
25275796c8dcSSimon Schubert demangled = language_demangle (language_def (lang), name, arg_mode);
25285796c8dcSSimon Schubert fputs_filtered (demangled ? demangled : name, stream);
25295796c8dcSSimon Schubert if (demangled != NULL)
25305796c8dcSSimon Schubert {
25315796c8dcSSimon Schubert xfree (demangled);
25325796c8dcSSimon Schubert }
25335796c8dcSSimon Schubert }
25345796c8dcSSimon Schubert }
25355796c8dcSSimon Schubert }
25365796c8dcSSimon Schubert
25375796c8dcSSimon Schubert /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
25385796c8dcSSimon Schubert differences in whitespace. Returns 0 if they match, non-zero if they
25395796c8dcSSimon Schubert don't (slightly different than strcmp()'s range of return values).
25405796c8dcSSimon Schubert
25415796c8dcSSimon Schubert As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
25425796c8dcSSimon Schubert This "feature" is useful when searching for matching C++ function names
25435796c8dcSSimon Schubert (such as if the user types 'break FOO', where FOO is a mangled C++
25445796c8dcSSimon Schubert function). */
25455796c8dcSSimon Schubert
25465796c8dcSSimon Schubert int
strcmp_iw(const char * string1,const char * string2)25475796c8dcSSimon Schubert strcmp_iw (const char *string1, const char *string2)
25485796c8dcSSimon Schubert {
25495796c8dcSSimon Schubert while ((*string1 != '\0') && (*string2 != '\0'))
25505796c8dcSSimon Schubert {
25515796c8dcSSimon Schubert while (isspace (*string1))
25525796c8dcSSimon Schubert {
25535796c8dcSSimon Schubert string1++;
25545796c8dcSSimon Schubert }
25555796c8dcSSimon Schubert while (isspace (*string2))
25565796c8dcSSimon Schubert {
25575796c8dcSSimon Schubert string2++;
25585796c8dcSSimon Schubert }
2559a45ae5f8SJohn Marino if (case_sensitivity == case_sensitive_on && *string1 != *string2)
25605796c8dcSSimon Schubert break;
2561a45ae5f8SJohn Marino if (case_sensitivity == case_sensitive_off
2562a45ae5f8SJohn Marino && (tolower ((unsigned char) *string1)
2563a45ae5f8SJohn Marino != tolower ((unsigned char) *string2)))
2564a45ae5f8SJohn Marino break;
25655796c8dcSSimon Schubert if (*string1 != '\0')
25665796c8dcSSimon Schubert {
25675796c8dcSSimon Schubert string1++;
25685796c8dcSSimon Schubert string2++;
25695796c8dcSSimon Schubert }
25705796c8dcSSimon Schubert }
25715796c8dcSSimon Schubert return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
25725796c8dcSSimon Schubert }
25735796c8dcSSimon Schubert
25745796c8dcSSimon Schubert /* This is like strcmp except that it ignores whitespace and treats
25755796c8dcSSimon Schubert '(' as the first non-NULL character in terms of ordering. Like
25765796c8dcSSimon Schubert strcmp (and unlike strcmp_iw), it returns negative if STRING1 <
25775796c8dcSSimon Schubert STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2
25785796c8dcSSimon Schubert according to that ordering.
25795796c8dcSSimon Schubert
25805796c8dcSSimon Schubert If a list is sorted according to this function and if you want to
25815796c8dcSSimon Schubert find names in the list that match some fixed NAME according to
25825796c8dcSSimon Schubert strcmp_iw(LIST_ELT, NAME), then the place to start looking is right
25835796c8dcSSimon Schubert where this function would put NAME.
25845796c8dcSSimon Schubert
2585a45ae5f8SJohn Marino This function must be neutral to the CASE_SENSITIVITY setting as the user
2586a45ae5f8SJohn Marino may choose it during later lookup. Therefore this function always sorts
2587a45ae5f8SJohn Marino primarily case-insensitively and secondarily case-sensitively.
2588a45ae5f8SJohn Marino
25895796c8dcSSimon Schubert Here are some examples of why using strcmp to sort is a bad idea:
25905796c8dcSSimon Schubert
25915796c8dcSSimon Schubert Whitespace example:
25925796c8dcSSimon Schubert
25935796c8dcSSimon Schubert Say your partial symtab contains: "foo<char *>", "goo". Then, if
25945796c8dcSSimon Schubert we try to do a search for "foo<char*>", strcmp will locate this
25955796c8dcSSimon Schubert after "foo<char *>" and before "goo". Then lookup_partial_symbol
25965796c8dcSSimon Schubert will start looking at strings beginning with "goo", and will never
25975796c8dcSSimon Schubert see the correct match of "foo<char *>".
25985796c8dcSSimon Schubert
25995796c8dcSSimon Schubert Parenthesis example:
26005796c8dcSSimon Schubert
26015796c8dcSSimon Schubert In practice, this is less like to be an issue, but I'll give it a
26025796c8dcSSimon Schubert shot. Let's assume that '$' is a legitimate character to occur in
26035796c8dcSSimon Schubert symbols. (Which may well even be the case on some systems.) Then
26045796c8dcSSimon Schubert say that the partial symbol table contains "foo$" and "foo(int)".
26055796c8dcSSimon Schubert strcmp will put them in this order, since '$' < '('. Now, if the
26065796c8dcSSimon Schubert user searches for "foo", then strcmp will sort "foo" before "foo$".
26075796c8dcSSimon Schubert Then lookup_partial_symbol will notice that strcmp_iw("foo$",
26085796c8dcSSimon Schubert "foo") is false, so it won't proceed to the actual match of
26095796c8dcSSimon Schubert "foo(int)" with "foo". */
26105796c8dcSSimon Schubert
26115796c8dcSSimon Schubert int
strcmp_iw_ordered(const char * string1,const char * string2)26125796c8dcSSimon Schubert strcmp_iw_ordered (const char *string1, const char *string2)
26135796c8dcSSimon Schubert {
2614a45ae5f8SJohn Marino const char *saved_string1 = string1, *saved_string2 = string2;
2615a45ae5f8SJohn Marino enum case_sensitivity case_pass = case_sensitive_off;
2616a45ae5f8SJohn Marino
2617a45ae5f8SJohn Marino for (;;)
2618a45ae5f8SJohn Marino {
2619a45ae5f8SJohn Marino /* C1 and C2 are valid only if *string1 != '\0' && *string2 != '\0'.
2620a45ae5f8SJohn Marino Provide stub characters if we are already at the end of one of the
2621a45ae5f8SJohn Marino strings. */
2622a45ae5f8SJohn Marino char c1 = 'X', c2 = 'X';
2623a45ae5f8SJohn Marino
2624a45ae5f8SJohn Marino while (*string1 != '\0' && *string2 != '\0')
26255796c8dcSSimon Schubert {
26265796c8dcSSimon Schubert while (isspace (*string1))
26275796c8dcSSimon Schubert string1++;
26285796c8dcSSimon Schubert while (isspace (*string2))
26295796c8dcSSimon Schubert string2++;
2630a45ae5f8SJohn Marino
2631a45ae5f8SJohn Marino switch (case_pass)
26325796c8dcSSimon Schubert {
2633a45ae5f8SJohn Marino case case_sensitive_off:
2634a45ae5f8SJohn Marino c1 = tolower ((unsigned char) *string1);
2635a45ae5f8SJohn Marino c2 = tolower ((unsigned char) *string2);
2636a45ae5f8SJohn Marino break;
2637a45ae5f8SJohn Marino case case_sensitive_on:
2638a45ae5f8SJohn Marino c1 = *string1;
2639a45ae5f8SJohn Marino c2 = *string2;
26405796c8dcSSimon Schubert break;
26415796c8dcSSimon Schubert }
2642a45ae5f8SJohn Marino if (c1 != c2)
2643a45ae5f8SJohn Marino break;
2644a45ae5f8SJohn Marino
26455796c8dcSSimon Schubert if (*string1 != '\0')
26465796c8dcSSimon Schubert {
26475796c8dcSSimon Schubert string1++;
26485796c8dcSSimon Schubert string2++;
26495796c8dcSSimon Schubert }
26505796c8dcSSimon Schubert }
26515796c8dcSSimon Schubert
26525796c8dcSSimon Schubert switch (*string1)
26535796c8dcSSimon Schubert {
26545796c8dcSSimon Schubert /* Characters are non-equal unless they're both '\0'; we want to
26555796c8dcSSimon Schubert make sure we get the comparison right according to our
26565796c8dcSSimon Schubert comparison in the cases where one of them is '\0' or '('. */
26575796c8dcSSimon Schubert case '\0':
26585796c8dcSSimon Schubert if (*string2 == '\0')
2659a45ae5f8SJohn Marino break;
26605796c8dcSSimon Schubert else
26615796c8dcSSimon Schubert return -1;
26625796c8dcSSimon Schubert case '(':
26635796c8dcSSimon Schubert if (*string2 == '\0')
26645796c8dcSSimon Schubert return 1;
26655796c8dcSSimon Schubert else
26665796c8dcSSimon Schubert return -1;
26675796c8dcSSimon Schubert default:
2668a45ae5f8SJohn Marino if (*string2 == '\0' || *string2 == '(')
26695796c8dcSSimon Schubert return 1;
2670a45ae5f8SJohn Marino else if (c1 > c2)
2671a45ae5f8SJohn Marino return 1;
2672a45ae5f8SJohn Marino else if (c1 < c2)
2673a45ae5f8SJohn Marino return -1;
2674a45ae5f8SJohn Marino /* PASSTHRU */
2675a45ae5f8SJohn Marino }
2676a45ae5f8SJohn Marino
2677a45ae5f8SJohn Marino if (case_pass == case_sensitive_on)
2678a45ae5f8SJohn Marino return 0;
2679a45ae5f8SJohn Marino
2680a45ae5f8SJohn Marino /* Otherwise the strings were equal in case insensitive way, make
2681a45ae5f8SJohn Marino a more fine grained comparison in a case sensitive way. */
2682a45ae5f8SJohn Marino
2683a45ae5f8SJohn Marino case_pass = case_sensitive_on;
2684a45ae5f8SJohn Marino string1 = saved_string1;
2685a45ae5f8SJohn Marino string2 = saved_string2;
26865796c8dcSSimon Schubert }
26875796c8dcSSimon Schubert }
26885796c8dcSSimon Schubert
26895796c8dcSSimon Schubert /* A simple comparison function with opposite semantics to strcmp. */
26905796c8dcSSimon Schubert
26915796c8dcSSimon Schubert int
streq(const char * lhs,const char * rhs)26925796c8dcSSimon Schubert streq (const char *lhs, const char *rhs)
26935796c8dcSSimon Schubert {
26945796c8dcSSimon Schubert return !strcmp (lhs, rhs);
26955796c8dcSSimon Schubert }
26965796c8dcSSimon Schubert
26975796c8dcSSimon Schubert
26985796c8dcSSimon Schubert /*
26995796c8dcSSimon Schubert ** subset_compare()
27005796c8dcSSimon Schubert ** Answer whether string_to_compare is a full or partial match to
27015796c8dcSSimon Schubert ** template_string. The partial match must be in sequence starting
27025796c8dcSSimon Schubert ** at index 0.
27035796c8dcSSimon Schubert */
27045796c8dcSSimon Schubert int
subset_compare(char * string_to_compare,char * template_string)27055796c8dcSSimon Schubert subset_compare (char *string_to_compare, char *template_string)
27065796c8dcSSimon Schubert {
27075796c8dcSSimon Schubert int match;
2708cf7f2e2dSJohn Marino
27095796c8dcSSimon Schubert if (template_string != (char *) NULL && string_to_compare != (char *) NULL
27105796c8dcSSimon Schubert && strlen (string_to_compare) <= strlen (template_string))
27115796c8dcSSimon Schubert match =
27125796c8dcSSimon Schubert (strncmp
27135796c8dcSSimon Schubert (template_string, string_to_compare, strlen (string_to_compare)) == 0);
27145796c8dcSSimon Schubert else
27155796c8dcSSimon Schubert match = 0;
27165796c8dcSSimon Schubert return match;
27175796c8dcSSimon Schubert }
27185796c8dcSSimon Schubert
27195796c8dcSSimon Schubert static void
pagination_on_command(char * arg,int from_tty)27205796c8dcSSimon Schubert pagination_on_command (char *arg, int from_tty)
27215796c8dcSSimon Schubert {
27225796c8dcSSimon Schubert pagination_enabled = 1;
27235796c8dcSSimon Schubert }
27245796c8dcSSimon Schubert
27255796c8dcSSimon Schubert static void
pagination_off_command(char * arg,int from_tty)27265796c8dcSSimon Schubert pagination_off_command (char *arg, int from_tty)
27275796c8dcSSimon Schubert {
27285796c8dcSSimon Schubert pagination_enabled = 0;
27295796c8dcSSimon Schubert }
27305796c8dcSSimon Schubert
27315796c8dcSSimon Schubert static void
show_debug_timestamp(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)27325796c8dcSSimon Schubert show_debug_timestamp (struct ui_file *file, int from_tty,
27335796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
27345796c8dcSSimon Schubert {
2735c50c785cSJohn Marino fprintf_filtered (file, _("Timestamping debugging messages is %s.\n"),
2736c50c785cSJohn Marino value);
27375796c8dcSSimon Schubert }
27385796c8dcSSimon Schubert
27395796c8dcSSimon Schubert
27405796c8dcSSimon Schubert void
initialize_utils(void)27415796c8dcSSimon Schubert initialize_utils (void)
27425796c8dcSSimon Schubert {
27435796c8dcSSimon Schubert add_setshow_uinteger_cmd ("width", class_support, &chars_per_line, _("\
2744*ef5ccd6cSJohn Marino Set number of characters where GDB should wrap lines of its output."), _("\
2745*ef5ccd6cSJohn Marino Show number of characters where GDB should wrap lines of its output."), _("\
2746*ef5ccd6cSJohn Marino This affects where GDB wraps its output to fit the screen width.\n\
2747*ef5ccd6cSJohn Marino Setting this to zero prevents GDB from wrapping its output."),
27485796c8dcSSimon Schubert set_width_command,
27495796c8dcSSimon Schubert show_chars_per_line,
27505796c8dcSSimon Schubert &setlist, &showlist);
27515796c8dcSSimon Schubert
27525796c8dcSSimon Schubert add_setshow_uinteger_cmd ("height", class_support, &lines_per_page, _("\
2753*ef5ccd6cSJohn Marino Set number of lines in a page for GDB output pagination."), _("\
2754*ef5ccd6cSJohn Marino Show number of lines in a page for GDB output pagination."), _("\
2755*ef5ccd6cSJohn Marino This affects the number of lines after which GDB will pause\n\
2756*ef5ccd6cSJohn Marino its output and ask you whether to continue.\n\
2757*ef5ccd6cSJohn Marino Setting this to zero causes GDB never pause during output."),
27585796c8dcSSimon Schubert set_height_command,
27595796c8dcSSimon Schubert show_lines_per_page,
27605796c8dcSSimon Schubert &setlist, &showlist);
27615796c8dcSSimon Schubert
27625796c8dcSSimon Schubert init_page_info ();
27635796c8dcSSimon Schubert
27645796c8dcSSimon Schubert add_setshow_boolean_cmd ("pagination", class_support,
27655796c8dcSSimon Schubert &pagination_enabled, _("\
2766*ef5ccd6cSJohn Marino Set state of GDB output pagination."), _("\
2767*ef5ccd6cSJohn Marino Show state of GDB output pagination."), _("\
2768*ef5ccd6cSJohn Marino When pagination is ON, GDB pauses at end of each screenful of\n\
2769*ef5ccd6cSJohn Marino its output and asks you whether to continue.\n\
2770*ef5ccd6cSJohn Marino Turning pagination off is an alternative to \"set height 0\"."),
27715796c8dcSSimon Schubert NULL,
27725796c8dcSSimon Schubert show_pagination_enabled,
27735796c8dcSSimon Schubert &setlist, &showlist);
27745796c8dcSSimon Schubert
27755796c8dcSSimon Schubert if (xdb_commands)
27765796c8dcSSimon Schubert {
27775796c8dcSSimon Schubert add_com ("am", class_support, pagination_on_command,
27785796c8dcSSimon Schubert _("Enable pagination"));
27795796c8dcSSimon Schubert add_com ("sm", class_support, pagination_off_command,
27805796c8dcSSimon Schubert _("Disable pagination"));
27815796c8dcSSimon Schubert }
27825796c8dcSSimon Schubert
27835796c8dcSSimon Schubert add_setshow_boolean_cmd ("sevenbit-strings", class_support,
27845796c8dcSSimon Schubert &sevenbit_strings, _("\
27855796c8dcSSimon Schubert Set printing of 8-bit characters in strings as \\nnn."), _("\
27865796c8dcSSimon Schubert Show printing of 8-bit characters in strings as \\nnn."), NULL,
27875796c8dcSSimon Schubert NULL,
27885796c8dcSSimon Schubert show_sevenbit_strings,
27895796c8dcSSimon Schubert &setprintlist, &showprintlist);
27905796c8dcSSimon Schubert
27915796c8dcSSimon Schubert add_setshow_boolean_cmd ("timestamp", class_maintenance,
27925796c8dcSSimon Schubert &debug_timestamp, _("\
27935796c8dcSSimon Schubert Set timestamping of debugging messages."), _("\
27945796c8dcSSimon Schubert Show timestamping of debugging messages."), _("\
27955796c8dcSSimon Schubert When set, debugging messages will be marked with seconds and microseconds."),
27965796c8dcSSimon Schubert NULL,
27975796c8dcSSimon Schubert show_debug_timestamp,
27985796c8dcSSimon Schubert &setdebuglist, &showdebuglist);
27995796c8dcSSimon Schubert }
28005796c8dcSSimon Schubert
2801c50c785cSJohn Marino /* Print routines to handle variable size regs, etc. */
2802c50c785cSJohn Marino /* Temporary storage using circular buffer. */
28035796c8dcSSimon Schubert #define NUMCELLS 16
28045796c8dcSSimon Schubert #define CELLSIZE 50
28055796c8dcSSimon Schubert static char *
get_cell(void)28065796c8dcSSimon Schubert get_cell (void)
28075796c8dcSSimon Schubert {
28085796c8dcSSimon Schubert static char buf[NUMCELLS][CELLSIZE];
28095796c8dcSSimon Schubert static int cell = 0;
2810cf7f2e2dSJohn Marino
28115796c8dcSSimon Schubert if (++cell >= NUMCELLS)
28125796c8dcSSimon Schubert cell = 0;
28135796c8dcSSimon Schubert return buf[cell];
28145796c8dcSSimon Schubert }
28155796c8dcSSimon Schubert
28165796c8dcSSimon Schubert const char *
paddress(struct gdbarch * gdbarch,CORE_ADDR addr)28175796c8dcSSimon Schubert paddress (struct gdbarch *gdbarch, CORE_ADDR addr)
28185796c8dcSSimon Schubert {
28195796c8dcSSimon Schubert /* Truncate address to the size of a target address, avoiding shifts
28205796c8dcSSimon Schubert larger or equal than the width of a CORE_ADDR. The local
28215796c8dcSSimon Schubert variable ADDR_BIT stops the compiler reporting a shift overflow
28225796c8dcSSimon Schubert when it won't occur. */
28235796c8dcSSimon Schubert /* NOTE: This assumes that the significant address information is
28245796c8dcSSimon Schubert kept in the least significant bits of ADDR - the upper bits were
28255796c8dcSSimon Schubert either zero or sign extended. Should gdbarch_address_to_pointer or
28265796c8dcSSimon Schubert some ADDRESS_TO_PRINTABLE() be used to do the conversion? */
28275796c8dcSSimon Schubert
28285796c8dcSSimon Schubert int addr_bit = gdbarch_addr_bit (gdbarch);
28295796c8dcSSimon Schubert
28305796c8dcSSimon Schubert if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
28315796c8dcSSimon Schubert addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
28325796c8dcSSimon Schubert return hex_string (addr);
28335796c8dcSSimon Schubert }
28345796c8dcSSimon Schubert
2835c50c785cSJohn Marino /* This function is described in "defs.h". */
2836c50c785cSJohn Marino
2837c50c785cSJohn Marino const char *
print_core_address(struct gdbarch * gdbarch,CORE_ADDR address)2838c50c785cSJohn Marino print_core_address (struct gdbarch *gdbarch, CORE_ADDR address)
2839c50c785cSJohn Marino {
2840c50c785cSJohn Marino int addr_bit = gdbarch_addr_bit (gdbarch);
2841c50c785cSJohn Marino
2842c50c785cSJohn Marino if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
2843c50c785cSJohn Marino address &= ((CORE_ADDR) 1 << addr_bit) - 1;
2844c50c785cSJohn Marino
2845c50c785cSJohn Marino /* FIXME: cagney/2002-05-03: Need local_address_string() function
2846c50c785cSJohn Marino that returns the language localized string formatted to a width
2847c50c785cSJohn Marino based on gdbarch_addr_bit. */
2848c50c785cSJohn Marino if (addr_bit <= 32)
2849c50c785cSJohn Marino return hex_string_custom (address, 8);
2850c50c785cSJohn Marino else
2851c50c785cSJohn Marino return hex_string_custom (address, 16);
2852c50c785cSJohn Marino }
2853c50c785cSJohn Marino
2854a45ae5f8SJohn Marino /* Callback hash_f for htab_create_alloc or htab_create_alloc_ex. */
2855a45ae5f8SJohn Marino
2856a45ae5f8SJohn Marino hashval_t
core_addr_hash(const void * ap)2857a45ae5f8SJohn Marino core_addr_hash (const void *ap)
2858a45ae5f8SJohn Marino {
2859a45ae5f8SJohn Marino const CORE_ADDR *addrp = ap;
2860a45ae5f8SJohn Marino
2861a45ae5f8SJohn Marino return *addrp;
2862a45ae5f8SJohn Marino }
2863a45ae5f8SJohn Marino
2864a45ae5f8SJohn Marino /* Callback eq_f for htab_create_alloc or htab_create_alloc_ex. */
2865a45ae5f8SJohn Marino
2866a45ae5f8SJohn Marino int
core_addr_eq(const void * ap,const void * bp)2867a45ae5f8SJohn Marino core_addr_eq (const void *ap, const void *bp)
2868a45ae5f8SJohn Marino {
2869a45ae5f8SJohn Marino const CORE_ADDR *addr_ap = ap;
2870a45ae5f8SJohn Marino const CORE_ADDR *addr_bp = bp;
2871a45ae5f8SJohn Marino
2872a45ae5f8SJohn Marino return *addr_ap == *addr_bp;
2873a45ae5f8SJohn Marino }
2874a45ae5f8SJohn Marino
28755796c8dcSSimon Schubert static char *
decimal2str(char * sign,ULONGEST addr,int width)28765796c8dcSSimon Schubert decimal2str (char *sign, ULONGEST addr, int width)
28775796c8dcSSimon Schubert {
28785796c8dcSSimon Schubert /* Steal code from valprint.c:print_decimal(). Should this worry
28795796c8dcSSimon Schubert about the real size of addr as the above does? */
28805796c8dcSSimon Schubert unsigned long temp[3];
28815796c8dcSSimon Schubert char *str = get_cell ();
28825796c8dcSSimon Schubert int i = 0;
2883cf7f2e2dSJohn Marino
28845796c8dcSSimon Schubert do
28855796c8dcSSimon Schubert {
28865796c8dcSSimon Schubert temp[i] = addr % (1000 * 1000 * 1000);
28875796c8dcSSimon Schubert addr /= (1000 * 1000 * 1000);
28885796c8dcSSimon Schubert i++;
28895796c8dcSSimon Schubert width -= 9;
28905796c8dcSSimon Schubert }
28915796c8dcSSimon Schubert while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
28925796c8dcSSimon Schubert
28935796c8dcSSimon Schubert width += 9;
28945796c8dcSSimon Schubert if (width < 0)
28955796c8dcSSimon Schubert width = 0;
28965796c8dcSSimon Schubert
28975796c8dcSSimon Schubert switch (i)
28985796c8dcSSimon Schubert {
28995796c8dcSSimon Schubert case 1:
29005796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
29015796c8dcSSimon Schubert break;
29025796c8dcSSimon Schubert case 2:
29035796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
29045796c8dcSSimon Schubert temp[1], temp[0]);
29055796c8dcSSimon Schubert break;
29065796c8dcSSimon Schubert case 3:
29075796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
29085796c8dcSSimon Schubert temp[2], temp[1], temp[0]);
29095796c8dcSSimon Schubert break;
29105796c8dcSSimon Schubert default:
29115796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
29125796c8dcSSimon Schubert _("failed internal consistency check"));
29135796c8dcSSimon Schubert }
29145796c8dcSSimon Schubert
29155796c8dcSSimon Schubert return str;
29165796c8dcSSimon Schubert }
29175796c8dcSSimon Schubert
29185796c8dcSSimon Schubert static char *
octal2str(ULONGEST addr,int width)29195796c8dcSSimon Schubert octal2str (ULONGEST addr, int width)
29205796c8dcSSimon Schubert {
29215796c8dcSSimon Schubert unsigned long temp[3];
29225796c8dcSSimon Schubert char *str = get_cell ();
29235796c8dcSSimon Schubert int i = 0;
2924cf7f2e2dSJohn Marino
29255796c8dcSSimon Schubert do
29265796c8dcSSimon Schubert {
29275796c8dcSSimon Schubert temp[i] = addr % (0100000 * 0100000);
29285796c8dcSSimon Schubert addr /= (0100000 * 0100000);
29295796c8dcSSimon Schubert i++;
29305796c8dcSSimon Schubert width -= 10;
29315796c8dcSSimon Schubert }
29325796c8dcSSimon Schubert while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
29335796c8dcSSimon Schubert
29345796c8dcSSimon Schubert width += 10;
29355796c8dcSSimon Schubert if (width < 0)
29365796c8dcSSimon Schubert width = 0;
29375796c8dcSSimon Schubert
29385796c8dcSSimon Schubert switch (i)
29395796c8dcSSimon Schubert {
29405796c8dcSSimon Schubert case 1:
29415796c8dcSSimon Schubert if (temp[0] == 0)
29425796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%*o", width, 0);
29435796c8dcSSimon Schubert else
29445796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "0%0*lo", width, temp[0]);
29455796c8dcSSimon Schubert break;
29465796c8dcSSimon Schubert case 2:
29475796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "0%0*lo%010lo", width, temp[1], temp[0]);
29485796c8dcSSimon Schubert break;
29495796c8dcSSimon Schubert case 3:
29505796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "0%0*lo%010lo%010lo", width,
29515796c8dcSSimon Schubert temp[2], temp[1], temp[0]);
29525796c8dcSSimon Schubert break;
29535796c8dcSSimon Schubert default:
29545796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
29555796c8dcSSimon Schubert _("failed internal consistency check"));
29565796c8dcSSimon Schubert }
29575796c8dcSSimon Schubert
29585796c8dcSSimon Schubert return str;
29595796c8dcSSimon Schubert }
29605796c8dcSSimon Schubert
29615796c8dcSSimon Schubert char *
pulongest(ULONGEST u)29625796c8dcSSimon Schubert pulongest (ULONGEST u)
29635796c8dcSSimon Schubert {
29645796c8dcSSimon Schubert return decimal2str ("", u, 0);
29655796c8dcSSimon Schubert }
29665796c8dcSSimon Schubert
29675796c8dcSSimon Schubert char *
plongest(LONGEST l)29685796c8dcSSimon Schubert plongest (LONGEST l)
29695796c8dcSSimon Schubert {
29705796c8dcSSimon Schubert if (l < 0)
29715796c8dcSSimon Schubert return decimal2str ("-", -l, 0);
29725796c8dcSSimon Schubert else
29735796c8dcSSimon Schubert return decimal2str ("", l, 0);
29745796c8dcSSimon Schubert }
29755796c8dcSSimon Schubert
29765796c8dcSSimon Schubert /* Eliminate warning from compiler on 32-bit systems. */
29775796c8dcSSimon Schubert static int thirty_two = 32;
29785796c8dcSSimon Schubert
29795796c8dcSSimon Schubert char *
phex(ULONGEST l,int sizeof_l)29805796c8dcSSimon Schubert phex (ULONGEST l, int sizeof_l)
29815796c8dcSSimon Schubert {
29825796c8dcSSimon Schubert char *str;
29835796c8dcSSimon Schubert
29845796c8dcSSimon Schubert switch (sizeof_l)
29855796c8dcSSimon Schubert {
29865796c8dcSSimon Schubert case 8:
29875796c8dcSSimon Schubert str = get_cell ();
29885796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%08lx%08lx",
29895796c8dcSSimon Schubert (unsigned long) (l >> thirty_two),
29905796c8dcSSimon Schubert (unsigned long) (l & 0xffffffff));
29915796c8dcSSimon Schubert break;
29925796c8dcSSimon Schubert case 4:
29935796c8dcSSimon Schubert str = get_cell ();
29945796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%08lx", (unsigned long) l);
29955796c8dcSSimon Schubert break;
29965796c8dcSSimon Schubert case 2:
29975796c8dcSSimon Schubert str = get_cell ();
29985796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%04x", (unsigned short) (l & 0xffff));
29995796c8dcSSimon Schubert break;
30005796c8dcSSimon Schubert default:
30015796c8dcSSimon Schubert str = phex (l, sizeof (l));
30025796c8dcSSimon Schubert break;
30035796c8dcSSimon Schubert }
30045796c8dcSSimon Schubert
30055796c8dcSSimon Schubert return str;
30065796c8dcSSimon Schubert }
30075796c8dcSSimon Schubert
30085796c8dcSSimon Schubert char *
phex_nz(ULONGEST l,int sizeof_l)30095796c8dcSSimon Schubert phex_nz (ULONGEST l, int sizeof_l)
30105796c8dcSSimon Schubert {
30115796c8dcSSimon Schubert char *str;
30125796c8dcSSimon Schubert
30135796c8dcSSimon Schubert switch (sizeof_l)
30145796c8dcSSimon Schubert {
30155796c8dcSSimon Schubert case 8:
30165796c8dcSSimon Schubert {
30175796c8dcSSimon Schubert unsigned long high = (unsigned long) (l >> thirty_two);
3018cf7f2e2dSJohn Marino
30195796c8dcSSimon Schubert str = get_cell ();
30205796c8dcSSimon Schubert if (high == 0)
30215796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%lx",
30225796c8dcSSimon Schubert (unsigned long) (l & 0xffffffff));
30235796c8dcSSimon Schubert else
30245796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%lx%08lx", high,
30255796c8dcSSimon Schubert (unsigned long) (l & 0xffffffff));
30265796c8dcSSimon Schubert break;
30275796c8dcSSimon Schubert }
30285796c8dcSSimon Schubert case 4:
30295796c8dcSSimon Schubert str = get_cell ();
30305796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
30315796c8dcSSimon Schubert break;
30325796c8dcSSimon Schubert case 2:
30335796c8dcSSimon Schubert str = get_cell ();
30345796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
30355796c8dcSSimon Schubert break;
30365796c8dcSSimon Schubert default:
30375796c8dcSSimon Schubert str = phex_nz (l, sizeof (l));
30385796c8dcSSimon Schubert break;
30395796c8dcSSimon Schubert }
30405796c8dcSSimon Schubert
30415796c8dcSSimon Schubert return str;
30425796c8dcSSimon Schubert }
30435796c8dcSSimon Schubert
30445796c8dcSSimon Schubert /* Converts a LONGEST to a C-format hexadecimal literal and stores it
30455796c8dcSSimon Schubert in a static string. Returns a pointer to this string. */
30465796c8dcSSimon Schubert char *
hex_string(LONGEST num)30475796c8dcSSimon Schubert hex_string (LONGEST num)
30485796c8dcSSimon Schubert {
30495796c8dcSSimon Schubert char *result = get_cell ();
3050cf7f2e2dSJohn Marino
30515796c8dcSSimon Schubert xsnprintf (result, CELLSIZE, "0x%s", phex_nz (num, sizeof (num)));
30525796c8dcSSimon Schubert return result;
30535796c8dcSSimon Schubert }
30545796c8dcSSimon Schubert
30555796c8dcSSimon Schubert /* Converts a LONGEST number to a C-format hexadecimal literal and
30565796c8dcSSimon Schubert stores it in a static string. Returns a pointer to this string
30575796c8dcSSimon Schubert that is valid until the next call. The number is padded on the
30585796c8dcSSimon Schubert left with 0s to at least WIDTH characters. */
30595796c8dcSSimon Schubert char *
hex_string_custom(LONGEST num,int width)30605796c8dcSSimon Schubert hex_string_custom (LONGEST num, int width)
30615796c8dcSSimon Schubert {
30625796c8dcSSimon Schubert char *result = get_cell ();
30635796c8dcSSimon Schubert char *result_end = result + CELLSIZE - 1;
30645796c8dcSSimon Schubert const char *hex = phex_nz (num, sizeof (num));
30655796c8dcSSimon Schubert int hex_len = strlen (hex);
30665796c8dcSSimon Schubert
30675796c8dcSSimon Schubert if (hex_len > width)
30685796c8dcSSimon Schubert width = hex_len;
30695796c8dcSSimon Schubert if (width + 2 >= CELLSIZE)
3070c50c785cSJohn Marino internal_error (__FILE__, __LINE__, _("\
3071c50c785cSJohn Marino hex_string_custom: insufficient space to store result"));
30725796c8dcSSimon Schubert
30735796c8dcSSimon Schubert strcpy (result_end - width - 2, "0x");
30745796c8dcSSimon Schubert memset (result_end - width, '0', width);
30755796c8dcSSimon Schubert strcpy (result_end - hex_len, hex);
30765796c8dcSSimon Schubert return result_end - width - 2;
30775796c8dcSSimon Schubert }
30785796c8dcSSimon Schubert
30795796c8dcSSimon Schubert /* Convert VAL to a numeral in the given radix. For
30805796c8dcSSimon Schubert * radix 10, IS_SIGNED may be true, indicating a signed quantity;
30815796c8dcSSimon Schubert * otherwise VAL is interpreted as unsigned. If WIDTH is supplied,
30825796c8dcSSimon Schubert * it is the minimum width (0-padded if needed). USE_C_FORMAT means
30835796c8dcSSimon Schubert * to use C format in all cases. If it is false, then 'x'
30845796c8dcSSimon Schubert * and 'o' formats do not include a prefix (0x or leading 0). */
30855796c8dcSSimon Schubert
30865796c8dcSSimon Schubert char *
int_string(LONGEST val,int radix,int is_signed,int width,int use_c_format)30875796c8dcSSimon Schubert int_string (LONGEST val, int radix, int is_signed, int width,
30885796c8dcSSimon Schubert int use_c_format)
30895796c8dcSSimon Schubert {
30905796c8dcSSimon Schubert switch (radix)
30915796c8dcSSimon Schubert {
30925796c8dcSSimon Schubert case 16:
30935796c8dcSSimon Schubert {
30945796c8dcSSimon Schubert char *result;
3095cf7f2e2dSJohn Marino
30965796c8dcSSimon Schubert if (width == 0)
30975796c8dcSSimon Schubert result = hex_string (val);
30985796c8dcSSimon Schubert else
30995796c8dcSSimon Schubert result = hex_string_custom (val, width);
31005796c8dcSSimon Schubert if (! use_c_format)
31015796c8dcSSimon Schubert result += 2;
31025796c8dcSSimon Schubert return result;
31035796c8dcSSimon Schubert }
31045796c8dcSSimon Schubert case 10:
31055796c8dcSSimon Schubert {
31065796c8dcSSimon Schubert if (is_signed && val < 0)
31075796c8dcSSimon Schubert return decimal2str ("-", -val, width);
31085796c8dcSSimon Schubert else
31095796c8dcSSimon Schubert return decimal2str ("", val, width);
31105796c8dcSSimon Schubert }
31115796c8dcSSimon Schubert case 8:
31125796c8dcSSimon Schubert {
31135796c8dcSSimon Schubert char *result = octal2str (val, width);
3114cf7f2e2dSJohn Marino
31155796c8dcSSimon Schubert if (use_c_format || val == 0)
31165796c8dcSSimon Schubert return result;
31175796c8dcSSimon Schubert else
31185796c8dcSSimon Schubert return result + 1;
31195796c8dcSSimon Schubert }
31205796c8dcSSimon Schubert default:
31215796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
31225796c8dcSSimon Schubert _("failed internal consistency check"));
31235796c8dcSSimon Schubert }
31245796c8dcSSimon Schubert }
31255796c8dcSSimon Schubert
31265796c8dcSSimon Schubert /* Convert a CORE_ADDR into a string. */
31275796c8dcSSimon Schubert const char *
core_addr_to_string(const CORE_ADDR addr)31285796c8dcSSimon Schubert core_addr_to_string (const CORE_ADDR addr)
31295796c8dcSSimon Schubert {
31305796c8dcSSimon Schubert char *str = get_cell ();
3131cf7f2e2dSJohn Marino
31325796c8dcSSimon Schubert strcpy (str, "0x");
31335796c8dcSSimon Schubert strcat (str, phex (addr, sizeof (addr)));
31345796c8dcSSimon Schubert return str;
31355796c8dcSSimon Schubert }
31365796c8dcSSimon Schubert
31375796c8dcSSimon Schubert const char *
core_addr_to_string_nz(const CORE_ADDR addr)31385796c8dcSSimon Schubert core_addr_to_string_nz (const CORE_ADDR addr)
31395796c8dcSSimon Schubert {
31405796c8dcSSimon Schubert char *str = get_cell ();
3141cf7f2e2dSJohn Marino
31425796c8dcSSimon Schubert strcpy (str, "0x");
31435796c8dcSSimon Schubert strcat (str, phex_nz (addr, sizeof (addr)));
31445796c8dcSSimon Schubert return str;
31455796c8dcSSimon Schubert }
31465796c8dcSSimon Schubert
31475796c8dcSSimon Schubert /* Convert a string back into a CORE_ADDR. */
31485796c8dcSSimon Schubert CORE_ADDR
string_to_core_addr(const char * my_string)31495796c8dcSSimon Schubert string_to_core_addr (const char *my_string)
31505796c8dcSSimon Schubert {
31515796c8dcSSimon Schubert CORE_ADDR addr = 0;
31525796c8dcSSimon Schubert
31535796c8dcSSimon Schubert if (my_string[0] == '0' && tolower (my_string[1]) == 'x')
31545796c8dcSSimon Schubert {
31555796c8dcSSimon Schubert /* Assume that it is in hex. */
31565796c8dcSSimon Schubert int i;
3157cf7f2e2dSJohn Marino
31585796c8dcSSimon Schubert for (i = 2; my_string[i] != '\0'; i++)
31595796c8dcSSimon Schubert {
31605796c8dcSSimon Schubert if (isdigit (my_string[i]))
31615796c8dcSSimon Schubert addr = (my_string[i] - '0') + (addr * 16);
31625796c8dcSSimon Schubert else if (isxdigit (my_string[i]))
31635796c8dcSSimon Schubert addr = (tolower (my_string[i]) - 'a' + 0xa) + (addr * 16);
31645796c8dcSSimon Schubert else
31655796c8dcSSimon Schubert error (_("invalid hex \"%s\""), my_string);
31665796c8dcSSimon Schubert }
31675796c8dcSSimon Schubert }
31685796c8dcSSimon Schubert else
31695796c8dcSSimon Schubert {
31705796c8dcSSimon Schubert /* Assume that it is in decimal. */
31715796c8dcSSimon Schubert int i;
3172cf7f2e2dSJohn Marino
31735796c8dcSSimon Schubert for (i = 0; my_string[i] != '\0'; i++)
31745796c8dcSSimon Schubert {
31755796c8dcSSimon Schubert if (isdigit (my_string[i]))
31765796c8dcSSimon Schubert addr = (my_string[i] - '0') + (addr * 10);
31775796c8dcSSimon Schubert else
31785796c8dcSSimon Schubert error (_("invalid decimal \"%s\""), my_string);
31795796c8dcSSimon Schubert }
31805796c8dcSSimon Schubert }
31815796c8dcSSimon Schubert
31825796c8dcSSimon Schubert return addr;
31835796c8dcSSimon Schubert }
31845796c8dcSSimon Schubert
31855796c8dcSSimon Schubert const char *
host_address_to_string(const void * addr)31865796c8dcSSimon Schubert host_address_to_string (const void *addr)
31875796c8dcSSimon Schubert {
31885796c8dcSSimon Schubert char *str = get_cell ();
31895796c8dcSSimon Schubert
31905796c8dcSSimon Schubert xsnprintf (str, CELLSIZE, "0x%s", phex_nz ((uintptr_t) addr, sizeof (addr)));
31915796c8dcSSimon Schubert return str;
31925796c8dcSSimon Schubert }
31935796c8dcSSimon Schubert
31945796c8dcSSimon Schubert char *
gdb_realpath(const char * filename)31955796c8dcSSimon Schubert gdb_realpath (const char *filename)
31965796c8dcSSimon Schubert {
31975796c8dcSSimon Schubert /* Method 1: The system has a compile time upper bound on a filename
31985796c8dcSSimon Schubert path. Use that and realpath() to canonicalize the name. This is
31995796c8dcSSimon Schubert the most common case. Note that, if there isn't a compile time
32005796c8dcSSimon Schubert upper bound, you want to avoid realpath() at all costs. */
32015796c8dcSSimon Schubert #if defined(HAVE_REALPATH)
32025796c8dcSSimon Schubert {
32035796c8dcSSimon Schubert # if defined (PATH_MAX)
32045796c8dcSSimon Schubert char buf[PATH_MAX];
32055796c8dcSSimon Schubert # define USE_REALPATH
32065796c8dcSSimon Schubert # elif defined (MAXPATHLEN)
32075796c8dcSSimon Schubert char buf[MAXPATHLEN];
32085796c8dcSSimon Schubert # define USE_REALPATH
32095796c8dcSSimon Schubert # endif
32105796c8dcSSimon Schubert # if defined (USE_REALPATH)
32115796c8dcSSimon Schubert const char *rp = realpath (filename, buf);
3212cf7f2e2dSJohn Marino
32135796c8dcSSimon Schubert if (rp == NULL)
32145796c8dcSSimon Schubert rp = filename;
32155796c8dcSSimon Schubert return xstrdup (rp);
32165796c8dcSSimon Schubert # endif
32175796c8dcSSimon Schubert }
32185796c8dcSSimon Schubert #endif /* HAVE_REALPATH */
32195796c8dcSSimon Schubert
32205796c8dcSSimon Schubert /* Method 2: The host system (i.e., GNU) has the function
32215796c8dcSSimon Schubert canonicalize_file_name() which malloc's a chunk of memory and
32225796c8dcSSimon Schubert returns that, use that. */
32235796c8dcSSimon Schubert #if defined(HAVE_CANONICALIZE_FILE_NAME)
32245796c8dcSSimon Schubert {
32255796c8dcSSimon Schubert char *rp = canonicalize_file_name (filename);
3226cf7f2e2dSJohn Marino
32275796c8dcSSimon Schubert if (rp == NULL)
32285796c8dcSSimon Schubert return xstrdup (filename);
32295796c8dcSSimon Schubert else
32305796c8dcSSimon Schubert return rp;
32315796c8dcSSimon Schubert }
32325796c8dcSSimon Schubert #endif
32335796c8dcSSimon Schubert
32345796c8dcSSimon Schubert /* FIXME: cagney/2002-11-13:
32355796c8dcSSimon Schubert
32365796c8dcSSimon Schubert Method 2a: Use realpath() with a NULL buffer. Some systems, due
3237a45ae5f8SJohn Marino to the problems described in method 3, have modified their
32385796c8dcSSimon Schubert realpath() implementation so that it will allocate a buffer when
32395796c8dcSSimon Schubert NULL is passed in. Before this can be used, though, some sort of
32405796c8dcSSimon Schubert configure time test would need to be added. Otherwize the code
32415796c8dcSSimon Schubert will likely core dump. */
32425796c8dcSSimon Schubert
32435796c8dcSSimon Schubert /* Method 3: Now we're getting desperate! The system doesn't have a
32445796c8dcSSimon Schubert compile time buffer size and no alternative function. Query the
32455796c8dcSSimon Schubert OS, using pathconf(), for the buffer limit. Care is needed
32465796c8dcSSimon Schubert though, some systems do not limit PATH_MAX (return -1 for
32475796c8dcSSimon Schubert pathconf()) making it impossible to pass a correctly sized buffer
32485796c8dcSSimon Schubert to realpath() (it could always overflow). On those systems, we
32495796c8dcSSimon Schubert skip this. */
32505796c8dcSSimon Schubert #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H) && defined(HAVE_ALLOCA)
32515796c8dcSSimon Schubert {
32525796c8dcSSimon Schubert /* Find out the max path size. */
32535796c8dcSSimon Schubert long path_max = pathconf ("/", _PC_PATH_MAX);
3254cf7f2e2dSJohn Marino
32555796c8dcSSimon Schubert if (path_max > 0)
32565796c8dcSSimon Schubert {
32575796c8dcSSimon Schubert /* PATH_MAX is bounded. */
32585796c8dcSSimon Schubert char *buf = alloca (path_max);
32595796c8dcSSimon Schubert char *rp = realpath (filename, buf);
3260cf7f2e2dSJohn Marino
32615796c8dcSSimon Schubert return xstrdup (rp ? rp : filename);
32625796c8dcSSimon Schubert }
32635796c8dcSSimon Schubert }
32645796c8dcSSimon Schubert #endif
32655796c8dcSSimon Schubert
3266*ef5ccd6cSJohn Marino /* The MS Windows method. If we don't have realpath, we assume we
3267*ef5ccd6cSJohn Marino don't have symlinks and just canonicalize to a Windows absolute
3268*ef5ccd6cSJohn Marino path. GetFullPath converts ../ and ./ in relative paths to
3269*ef5ccd6cSJohn Marino absolute paths, filling in current drive if one is not given
3270*ef5ccd6cSJohn Marino or using the current directory of a specified drive (eg, "E:foo").
3271*ef5ccd6cSJohn Marino It also converts all forward slashes to back slashes. */
3272*ef5ccd6cSJohn Marino /* The file system is case-insensitive but case-preserving.
3273*ef5ccd6cSJohn Marino So we do not lowercase the path. Otherwise, we might not
3274*ef5ccd6cSJohn Marino be able to display the original casing in a given path. */
3275*ef5ccd6cSJohn Marino #if defined (_WIN32)
32765796c8dcSSimon Schubert {
3277*ef5ccd6cSJohn Marino char buf[MAX_PATH];
3278*ef5ccd6cSJohn Marino DWORD len = GetFullPathName (filename, MAX_PATH, buf, NULL);
32795796c8dcSSimon Schubert
3280*ef5ccd6cSJohn Marino if (len > 0 && len < MAX_PATH)
3281*ef5ccd6cSJohn Marino return xstrdup (buf);
32825796c8dcSSimon Schubert }
32835796c8dcSSimon Schubert #endif
32845796c8dcSSimon Schubert
3285*ef5ccd6cSJohn Marino /* This system is a lost cause, just dup the buffer. */
3286*ef5ccd6cSJohn Marino return xstrdup (filename);
32875796c8dcSSimon Schubert }
32885796c8dcSSimon Schubert
32895796c8dcSSimon Schubert ULONGEST
align_up(ULONGEST v,int n)32905796c8dcSSimon Schubert align_up (ULONGEST v, int n)
32915796c8dcSSimon Schubert {
32925796c8dcSSimon Schubert /* Check that N is really a power of two. */
32935796c8dcSSimon Schubert gdb_assert (n && (n & (n-1)) == 0);
32945796c8dcSSimon Schubert return (v + n - 1) & -n;
32955796c8dcSSimon Schubert }
32965796c8dcSSimon Schubert
32975796c8dcSSimon Schubert ULONGEST
align_down(ULONGEST v,int n)32985796c8dcSSimon Schubert align_down (ULONGEST v, int n)
32995796c8dcSSimon Schubert {
33005796c8dcSSimon Schubert /* Check that N is really a power of two. */
33015796c8dcSSimon Schubert gdb_assert (n && (n & (n-1)) == 0);
33025796c8dcSSimon Schubert return (v & -n);
33035796c8dcSSimon Schubert }
33045796c8dcSSimon Schubert
33055796c8dcSSimon Schubert /* Allocation function for the libiberty hash table which uses an
33065796c8dcSSimon Schubert obstack. The obstack is passed as DATA. */
33075796c8dcSSimon Schubert
33085796c8dcSSimon Schubert void *
hashtab_obstack_allocate(void * data,size_t size,size_t count)33095796c8dcSSimon Schubert hashtab_obstack_allocate (void *data, size_t size, size_t count)
33105796c8dcSSimon Schubert {
33115796c8dcSSimon Schubert unsigned int total = size * count;
33125796c8dcSSimon Schubert void *ptr = obstack_alloc ((struct obstack *) data, total);
3313cf7f2e2dSJohn Marino
33145796c8dcSSimon Schubert memset (ptr, 0, total);
33155796c8dcSSimon Schubert return ptr;
33165796c8dcSSimon Schubert }
33175796c8dcSSimon Schubert
33185796c8dcSSimon Schubert /* Trivial deallocation function for the libiberty splay tree and hash
33195796c8dcSSimon Schubert table - don't deallocate anything. Rely on later deletion of the
33205796c8dcSSimon Schubert obstack. DATA will be the obstack, although it is not needed
33215796c8dcSSimon Schubert here. */
33225796c8dcSSimon Schubert
33235796c8dcSSimon Schubert void
dummy_obstack_deallocate(void * object,void * data)33245796c8dcSSimon Schubert dummy_obstack_deallocate (void *object, void *data)
33255796c8dcSSimon Schubert {
33265796c8dcSSimon Schubert return;
33275796c8dcSSimon Schubert }
33285796c8dcSSimon Schubert
33295796c8dcSSimon Schubert /* The bit offset of the highest byte in a ULONGEST, for overflow
33305796c8dcSSimon Schubert checking. */
33315796c8dcSSimon Schubert
33325796c8dcSSimon Schubert #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
33335796c8dcSSimon Schubert
33345796c8dcSSimon Schubert /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
33355796c8dcSSimon Schubert where 2 <= BASE <= 36. */
33365796c8dcSSimon Schubert
33375796c8dcSSimon Schubert static int
is_digit_in_base(unsigned char digit,int base)33385796c8dcSSimon Schubert is_digit_in_base (unsigned char digit, int base)
33395796c8dcSSimon Schubert {
33405796c8dcSSimon Schubert if (!isalnum (digit))
33415796c8dcSSimon Schubert return 0;
33425796c8dcSSimon Schubert if (base <= 10)
33435796c8dcSSimon Schubert return (isdigit (digit) && digit < base + '0');
33445796c8dcSSimon Schubert else
33455796c8dcSSimon Schubert return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
33465796c8dcSSimon Schubert }
33475796c8dcSSimon Schubert
33485796c8dcSSimon Schubert static int
digit_to_int(unsigned char c)33495796c8dcSSimon Schubert digit_to_int (unsigned char c)
33505796c8dcSSimon Schubert {
33515796c8dcSSimon Schubert if (isdigit (c))
33525796c8dcSSimon Schubert return c - '0';
33535796c8dcSSimon Schubert else
33545796c8dcSSimon Schubert return tolower (c) - 'a' + 10;
33555796c8dcSSimon Schubert }
33565796c8dcSSimon Schubert
33575796c8dcSSimon Schubert /* As for strtoul, but for ULONGEST results. */
33585796c8dcSSimon Schubert
33595796c8dcSSimon Schubert ULONGEST
strtoulst(const char * num,const char ** trailer,int base)33605796c8dcSSimon Schubert strtoulst (const char *num, const char **trailer, int base)
33615796c8dcSSimon Schubert {
33625796c8dcSSimon Schubert unsigned int high_part;
33635796c8dcSSimon Schubert ULONGEST result;
33645796c8dcSSimon Schubert int minus = 0;
33655796c8dcSSimon Schubert int i = 0;
33665796c8dcSSimon Schubert
33675796c8dcSSimon Schubert /* Skip leading whitespace. */
33685796c8dcSSimon Schubert while (isspace (num[i]))
33695796c8dcSSimon Schubert i++;
33705796c8dcSSimon Schubert
33715796c8dcSSimon Schubert /* Handle prefixes. */
33725796c8dcSSimon Schubert if (num[i] == '+')
33735796c8dcSSimon Schubert i++;
33745796c8dcSSimon Schubert else if (num[i] == '-')
33755796c8dcSSimon Schubert {
33765796c8dcSSimon Schubert minus = 1;
33775796c8dcSSimon Schubert i++;
33785796c8dcSSimon Schubert }
33795796c8dcSSimon Schubert
33805796c8dcSSimon Schubert if (base == 0 || base == 16)
33815796c8dcSSimon Schubert {
33825796c8dcSSimon Schubert if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
33835796c8dcSSimon Schubert {
33845796c8dcSSimon Schubert i += 2;
33855796c8dcSSimon Schubert if (base == 0)
33865796c8dcSSimon Schubert base = 16;
33875796c8dcSSimon Schubert }
33885796c8dcSSimon Schubert }
33895796c8dcSSimon Schubert
33905796c8dcSSimon Schubert if (base == 0 && num[i] == '0')
33915796c8dcSSimon Schubert base = 8;
33925796c8dcSSimon Schubert
33935796c8dcSSimon Schubert if (base == 0)
33945796c8dcSSimon Schubert base = 10;
33955796c8dcSSimon Schubert
33965796c8dcSSimon Schubert if (base < 2 || base > 36)
33975796c8dcSSimon Schubert {
33985796c8dcSSimon Schubert errno = EINVAL;
33995796c8dcSSimon Schubert return 0;
34005796c8dcSSimon Schubert }
34015796c8dcSSimon Schubert
34025796c8dcSSimon Schubert result = high_part = 0;
34035796c8dcSSimon Schubert for (; is_digit_in_base (num[i], base); i += 1)
34045796c8dcSSimon Schubert {
34055796c8dcSSimon Schubert result = result * base + digit_to_int (num[i]);
34065796c8dcSSimon Schubert high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
34075796c8dcSSimon Schubert result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
34085796c8dcSSimon Schubert if (high_part > 0xff)
34095796c8dcSSimon Schubert {
34105796c8dcSSimon Schubert errno = ERANGE;
34115796c8dcSSimon Schubert result = ~ (ULONGEST) 0;
34125796c8dcSSimon Schubert high_part = 0;
34135796c8dcSSimon Schubert minus = 0;
34145796c8dcSSimon Schubert break;
34155796c8dcSSimon Schubert }
34165796c8dcSSimon Schubert }
34175796c8dcSSimon Schubert
34185796c8dcSSimon Schubert if (trailer != NULL)
34195796c8dcSSimon Schubert *trailer = &num[i];
34205796c8dcSSimon Schubert
34215796c8dcSSimon Schubert result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
34225796c8dcSSimon Schubert if (minus)
34235796c8dcSSimon Schubert return -result;
34245796c8dcSSimon Schubert else
34255796c8dcSSimon Schubert return result;
34265796c8dcSSimon Schubert }
34275796c8dcSSimon Schubert
34285796c8dcSSimon Schubert /* Simple, portable version of dirname that does not modify its
34295796c8dcSSimon Schubert argument. */
34305796c8dcSSimon Schubert
34315796c8dcSSimon Schubert char *
ldirname(const char * filename)34325796c8dcSSimon Schubert ldirname (const char *filename)
34335796c8dcSSimon Schubert {
34345796c8dcSSimon Schubert const char *base = lbasename (filename);
34355796c8dcSSimon Schubert char *dirname;
34365796c8dcSSimon Schubert
34375796c8dcSSimon Schubert while (base > filename && IS_DIR_SEPARATOR (base[-1]))
34385796c8dcSSimon Schubert --base;
34395796c8dcSSimon Schubert
34405796c8dcSSimon Schubert if (base == filename)
34415796c8dcSSimon Schubert return NULL;
34425796c8dcSSimon Schubert
34435796c8dcSSimon Schubert dirname = xmalloc (base - filename + 2);
34445796c8dcSSimon Schubert memcpy (dirname, filename, base - filename);
34455796c8dcSSimon Schubert
34465796c8dcSSimon Schubert /* On DOS based file systems, convert "d:foo" to "d:.", so that we
34475796c8dcSSimon Schubert create "d:./bar" later instead of the (different) "d:/bar". */
34485796c8dcSSimon Schubert if (base - filename == 2 && IS_ABSOLUTE_PATH (base)
34495796c8dcSSimon Schubert && !IS_DIR_SEPARATOR (filename[0]))
34505796c8dcSSimon Schubert dirname[base++ - filename] = '.';
34515796c8dcSSimon Schubert
34525796c8dcSSimon Schubert dirname[base - filename] = '\0';
34535796c8dcSSimon Schubert return dirname;
34545796c8dcSSimon Schubert }
34555796c8dcSSimon Schubert
34565796c8dcSSimon Schubert /* Call libiberty's buildargv, and return the result.
34575796c8dcSSimon Schubert If buildargv fails due to out-of-memory, call nomem.
34585796c8dcSSimon Schubert Therefore, the returned value is guaranteed to be non-NULL,
34595796c8dcSSimon Schubert unless the parameter itself is NULL. */
34605796c8dcSSimon Schubert
34615796c8dcSSimon Schubert char **
gdb_buildargv(const char * s)34625796c8dcSSimon Schubert gdb_buildargv (const char *s)
34635796c8dcSSimon Schubert {
34645796c8dcSSimon Schubert char **argv = buildargv (s);
3465cf7f2e2dSJohn Marino
34665796c8dcSSimon Schubert if (s != NULL && argv == NULL)
3467a45ae5f8SJohn Marino malloc_failure (0);
34685796c8dcSSimon Schubert return argv;
34695796c8dcSSimon Schubert }
34705796c8dcSSimon Schubert
3471cf7f2e2dSJohn Marino int
compare_positive_ints(const void * ap,const void * bp)3472cf7f2e2dSJohn Marino compare_positive_ints (const void *ap, const void *bp)
3473cf7f2e2dSJohn Marino {
3474cf7f2e2dSJohn Marino /* Because we know we're comparing two ints which are positive,
3475cf7f2e2dSJohn Marino there's no danger of overflow here. */
3476cf7f2e2dSJohn Marino return * (int *) ap - * (int *) bp;
3477cf7f2e2dSJohn Marino }
3478cf7f2e2dSJohn Marino
3479a45ae5f8SJohn Marino /* String compare function for qsort. */
3480a45ae5f8SJohn Marino
3481a45ae5f8SJohn Marino int
compare_strings(const void * arg1,const void * arg2)3482a45ae5f8SJohn Marino compare_strings (const void *arg1, const void *arg2)
3483a45ae5f8SJohn Marino {
3484a45ae5f8SJohn Marino const char **s1 = (const char **) arg1;
3485a45ae5f8SJohn Marino const char **s2 = (const char **) arg2;
3486a45ae5f8SJohn Marino
3487a45ae5f8SJohn Marino return strcmp (*s1, *s2);
3488a45ae5f8SJohn Marino }
3489a45ae5f8SJohn Marino
3490cf7f2e2dSJohn Marino #define AMBIGUOUS_MESS1 ".\nMatching formats:"
3491c50c785cSJohn Marino #define AMBIGUOUS_MESS2 \
3492c50c785cSJohn Marino ".\nUse \"set gnutarget format-name\" to specify the format."
3493cf7f2e2dSJohn Marino
3494cf7f2e2dSJohn Marino const char *
gdb_bfd_errmsg(bfd_error_type error_tag,char ** matching)3495cf7f2e2dSJohn Marino gdb_bfd_errmsg (bfd_error_type error_tag, char **matching)
3496cf7f2e2dSJohn Marino {
3497cf7f2e2dSJohn Marino char *ret, *retp;
3498cf7f2e2dSJohn Marino int ret_len;
3499cf7f2e2dSJohn Marino char **p;
3500cf7f2e2dSJohn Marino
3501cf7f2e2dSJohn Marino /* Check if errmsg just need simple return. */
3502cf7f2e2dSJohn Marino if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL)
3503cf7f2e2dSJohn Marino return bfd_errmsg (error_tag);
3504cf7f2e2dSJohn Marino
3505cf7f2e2dSJohn Marino ret_len = strlen (bfd_errmsg (error_tag)) + strlen (AMBIGUOUS_MESS1)
3506cf7f2e2dSJohn Marino + strlen (AMBIGUOUS_MESS2);
3507cf7f2e2dSJohn Marino for (p = matching; *p; p++)
3508cf7f2e2dSJohn Marino ret_len += strlen (*p) + 1;
3509cf7f2e2dSJohn Marino ret = xmalloc (ret_len + 1);
3510cf7f2e2dSJohn Marino retp = ret;
3511cf7f2e2dSJohn Marino make_cleanup (xfree, ret);
3512cf7f2e2dSJohn Marino
3513cf7f2e2dSJohn Marino strcpy (retp, bfd_errmsg (error_tag));
3514cf7f2e2dSJohn Marino retp += strlen (retp);
3515cf7f2e2dSJohn Marino
3516cf7f2e2dSJohn Marino strcpy (retp, AMBIGUOUS_MESS1);
3517cf7f2e2dSJohn Marino retp += strlen (retp);
3518cf7f2e2dSJohn Marino
3519cf7f2e2dSJohn Marino for (p = matching; *p; p++)
3520cf7f2e2dSJohn Marino {
3521cf7f2e2dSJohn Marino sprintf (retp, " %s", *p);
3522cf7f2e2dSJohn Marino retp += strlen (retp);
3523cf7f2e2dSJohn Marino }
3524cf7f2e2dSJohn Marino xfree (matching);
3525cf7f2e2dSJohn Marino
3526cf7f2e2dSJohn Marino strcpy (retp, AMBIGUOUS_MESS2);
3527cf7f2e2dSJohn Marino
3528cf7f2e2dSJohn Marino return ret;
3529cf7f2e2dSJohn Marino }
3530cf7f2e2dSJohn Marino
3531cf7f2e2dSJohn Marino /* Return ARGS parsed as a valid pid, or throw an error. */
3532cf7f2e2dSJohn Marino
3533cf7f2e2dSJohn Marino int
parse_pid_to_attach(char * args)3534cf7f2e2dSJohn Marino parse_pid_to_attach (char *args)
3535cf7f2e2dSJohn Marino {
3536cf7f2e2dSJohn Marino unsigned long pid;
3537cf7f2e2dSJohn Marino char *dummy;
3538cf7f2e2dSJohn Marino
3539cf7f2e2dSJohn Marino if (!args)
3540cf7f2e2dSJohn Marino error_no_arg (_("process-id to attach"));
3541cf7f2e2dSJohn Marino
3542cf7f2e2dSJohn Marino dummy = args;
3543cf7f2e2dSJohn Marino pid = strtoul (args, &dummy, 0);
3544cf7f2e2dSJohn Marino /* Some targets don't set errno on errors, grrr! */
3545cf7f2e2dSJohn Marino if ((pid == 0 && dummy == args) || dummy != &args[strlen (args)])
3546cf7f2e2dSJohn Marino error (_("Illegal process-id: %s."), args);
3547cf7f2e2dSJohn Marino
3548cf7f2e2dSJohn Marino return pid;
3549cf7f2e2dSJohn Marino }
3550cf7f2e2dSJohn Marino
3551a45ae5f8SJohn Marino /* Helper for make_bpstat_clear_actions_cleanup. */
3552a45ae5f8SJohn Marino
3553a45ae5f8SJohn Marino static void
do_bpstat_clear_actions_cleanup(void * unused)3554a45ae5f8SJohn Marino do_bpstat_clear_actions_cleanup (void *unused)
3555a45ae5f8SJohn Marino {
3556a45ae5f8SJohn Marino bpstat_clear_actions ();
3557a45ae5f8SJohn Marino }
3558a45ae5f8SJohn Marino
3559a45ae5f8SJohn Marino /* Call bpstat_clear_actions for the case an exception is throw. You should
3560a45ae5f8SJohn Marino discard_cleanups if no exception is caught. */
3561a45ae5f8SJohn Marino
3562a45ae5f8SJohn Marino struct cleanup *
make_bpstat_clear_actions_cleanup(void)3563a45ae5f8SJohn Marino make_bpstat_clear_actions_cleanup (void)
3564a45ae5f8SJohn Marino {
3565a45ae5f8SJohn Marino return make_cleanup (do_bpstat_clear_actions_cleanup, NULL);
3566a45ae5f8SJohn Marino }
3567a45ae5f8SJohn Marino
3568a45ae5f8SJohn Marino /* Check for GCC >= 4.x according to the symtab->producer string. Return minor
3569a45ae5f8SJohn Marino version (x) of 4.x in such case. If it is not GCC or it is GCC older than
3570a45ae5f8SJohn Marino 4.x return -1. If it is GCC 5.x or higher return INT_MAX. */
3571a45ae5f8SJohn Marino
3572a45ae5f8SJohn Marino int
producer_is_gcc_ge_4(const char * producer)3573a45ae5f8SJohn Marino producer_is_gcc_ge_4 (const char *producer)
3574a45ae5f8SJohn Marino {
3575a45ae5f8SJohn Marino const char *cs;
3576a45ae5f8SJohn Marino int major, minor;
3577a45ae5f8SJohn Marino
3578a45ae5f8SJohn Marino if (producer == NULL)
3579a45ae5f8SJohn Marino {
3580a45ae5f8SJohn Marino /* For unknown compilers expect their behavior is not compliant. For GCC
3581a45ae5f8SJohn Marino this case can also happen for -gdwarf-4 type units supported since
3582a45ae5f8SJohn Marino gcc-4.5. */
3583a45ae5f8SJohn Marino
3584a45ae5f8SJohn Marino return -1;
3585a45ae5f8SJohn Marino }
3586a45ae5f8SJohn Marino
3587a45ae5f8SJohn Marino /* Skip any identifier after "GNU " - such as "C++" or "Java". */
3588a45ae5f8SJohn Marino
3589a45ae5f8SJohn Marino if (strncmp (producer, "GNU ", strlen ("GNU ")) != 0)
3590a45ae5f8SJohn Marino {
3591a45ae5f8SJohn Marino /* For non-GCC compilers expect their behavior is not compliant. */
3592a45ae5f8SJohn Marino
3593a45ae5f8SJohn Marino return -1;
3594a45ae5f8SJohn Marino }
3595a45ae5f8SJohn Marino cs = &producer[strlen ("GNU ")];
3596a45ae5f8SJohn Marino while (*cs && !isdigit (*cs))
3597a45ae5f8SJohn Marino cs++;
3598a45ae5f8SJohn Marino if (sscanf (cs, "%d.%d", &major, &minor) != 2)
3599a45ae5f8SJohn Marino {
3600a45ae5f8SJohn Marino /* Not recognized as GCC. */
3601a45ae5f8SJohn Marino
3602a45ae5f8SJohn Marino return -1;
3603a45ae5f8SJohn Marino }
3604a45ae5f8SJohn Marino
3605a45ae5f8SJohn Marino if (major < 4)
3606a45ae5f8SJohn Marino return -1;
3607a45ae5f8SJohn Marino if (major > 4)
3608a45ae5f8SJohn Marino return INT_MAX;
3609a45ae5f8SJohn Marino return minor;
3610a45ae5f8SJohn Marino }
3611a45ae5f8SJohn Marino
3612*ef5ccd6cSJohn Marino /* Helper for make_cleanup_free_char_ptr_vec. */
3613*ef5ccd6cSJohn Marino
3614*ef5ccd6cSJohn Marino static void
do_free_char_ptr_vec(void * arg)3615*ef5ccd6cSJohn Marino do_free_char_ptr_vec (void *arg)
3616*ef5ccd6cSJohn Marino {
3617*ef5ccd6cSJohn Marino VEC (char_ptr) *char_ptr_vec = arg;
3618*ef5ccd6cSJohn Marino
3619*ef5ccd6cSJohn Marino free_char_ptr_vec (char_ptr_vec);
3620*ef5ccd6cSJohn Marino }
3621*ef5ccd6cSJohn Marino
3622*ef5ccd6cSJohn Marino /* Make cleanup handler calling xfree for each element of CHAR_PTR_VEC and
3623*ef5ccd6cSJohn Marino final VEC_free for CHAR_PTR_VEC itself.
3624*ef5ccd6cSJohn Marino
3625*ef5ccd6cSJohn Marino You must not modify CHAR_PTR_VEC after this cleanup registration as the
3626*ef5ccd6cSJohn Marino CHAR_PTR_VEC base address may change on its updates. Contrary to VEC_free
3627*ef5ccd6cSJohn Marino this function does not (cannot) clear the pointer. */
3628*ef5ccd6cSJohn Marino
3629*ef5ccd6cSJohn Marino struct cleanup *
make_cleanup_free_char_ptr_vec(VEC (char_ptr)* char_ptr_vec)3630*ef5ccd6cSJohn Marino make_cleanup_free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec)
3631*ef5ccd6cSJohn Marino {
3632*ef5ccd6cSJohn Marino return make_cleanup (do_free_char_ptr_vec, char_ptr_vec);
3633*ef5ccd6cSJohn Marino }
3634*ef5ccd6cSJohn Marino
3635*ef5ccd6cSJohn Marino /* Substitute all occurences of string FROM by string TO in *STRINGP. *STRINGP
3636*ef5ccd6cSJohn Marino must come from xrealloc-compatible allocator and it may be updated. FROM
3637*ef5ccd6cSJohn Marino needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be
3638*ef5ccd6cSJohn Marino located at the start or end of *STRINGP. */
3639*ef5ccd6cSJohn Marino
3640*ef5ccd6cSJohn Marino void
substitute_path_component(char ** stringp,const char * from,const char * to)3641*ef5ccd6cSJohn Marino substitute_path_component (char **stringp, const char *from, const char *to)
3642*ef5ccd6cSJohn Marino {
3643*ef5ccd6cSJohn Marino char *string = *stringp, *s;
3644*ef5ccd6cSJohn Marino const size_t from_len = strlen (from);
3645*ef5ccd6cSJohn Marino const size_t to_len = strlen (to);
3646*ef5ccd6cSJohn Marino
3647*ef5ccd6cSJohn Marino for (s = string;;)
3648*ef5ccd6cSJohn Marino {
3649*ef5ccd6cSJohn Marino s = strstr (s, from);
3650*ef5ccd6cSJohn Marino if (s == NULL)
3651*ef5ccd6cSJohn Marino break;
3652*ef5ccd6cSJohn Marino
3653*ef5ccd6cSJohn Marino if ((s == string || IS_DIR_SEPARATOR (s[-1])
3654*ef5ccd6cSJohn Marino || s[-1] == DIRNAME_SEPARATOR)
3655*ef5ccd6cSJohn Marino && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len])
3656*ef5ccd6cSJohn Marino || s[from_len] == DIRNAME_SEPARATOR))
3657*ef5ccd6cSJohn Marino {
3658*ef5ccd6cSJohn Marino char *string_new;
3659*ef5ccd6cSJohn Marino
3660*ef5ccd6cSJohn Marino string_new = xrealloc (string, (strlen (string) + to_len + 1));
3661*ef5ccd6cSJohn Marino
3662*ef5ccd6cSJohn Marino /* Relocate the current S pointer. */
3663*ef5ccd6cSJohn Marino s = s - string + string_new;
3664*ef5ccd6cSJohn Marino string = string_new;
3665*ef5ccd6cSJohn Marino
3666*ef5ccd6cSJohn Marino /* Replace from by to. */
3667*ef5ccd6cSJohn Marino memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1);
3668*ef5ccd6cSJohn Marino memcpy (s, to, to_len);
3669*ef5ccd6cSJohn Marino
3670*ef5ccd6cSJohn Marino s += to_len;
3671*ef5ccd6cSJohn Marino }
3672*ef5ccd6cSJohn Marino else
3673*ef5ccd6cSJohn Marino s++;
3674*ef5ccd6cSJohn Marino }
3675*ef5ccd6cSJohn Marino
3676*ef5ccd6cSJohn Marino *stringp = string;
3677*ef5ccd6cSJohn Marino }
3678*ef5ccd6cSJohn Marino
3679*ef5ccd6cSJohn Marino #ifdef HAVE_WAITPID
3680*ef5ccd6cSJohn Marino
3681*ef5ccd6cSJohn Marino #ifdef SIGALRM
3682*ef5ccd6cSJohn Marino
3683*ef5ccd6cSJohn Marino /* SIGALRM handler for waitpid_with_timeout. */
3684*ef5ccd6cSJohn Marino
3685*ef5ccd6cSJohn Marino static void
sigalrm_handler(int signo)3686*ef5ccd6cSJohn Marino sigalrm_handler (int signo)
3687*ef5ccd6cSJohn Marino {
3688*ef5ccd6cSJohn Marino /* Nothing to do. */
3689*ef5ccd6cSJohn Marino }
3690*ef5ccd6cSJohn Marino
3691*ef5ccd6cSJohn Marino #endif
3692*ef5ccd6cSJohn Marino
3693*ef5ccd6cSJohn Marino /* Wrapper to wait for child PID to die with TIMEOUT.
3694*ef5ccd6cSJohn Marino TIMEOUT is the time to stop waiting in seconds.
3695*ef5ccd6cSJohn Marino If TIMEOUT is zero, pass WNOHANG to waitpid.
3696*ef5ccd6cSJohn Marino Returns PID if it was successfully waited for, otherwise -1.
3697*ef5ccd6cSJohn Marino
3698*ef5ccd6cSJohn Marino Timeouts are currently implemented with alarm and SIGALRM.
3699*ef5ccd6cSJohn Marino If the host does not support them, this waits "forever".
3700*ef5ccd6cSJohn Marino It would be odd though for a host to have waitpid and not SIGALRM. */
3701*ef5ccd6cSJohn Marino
3702*ef5ccd6cSJohn Marino pid_t
wait_to_die_with_timeout(pid_t pid,int * status,int timeout)3703*ef5ccd6cSJohn Marino wait_to_die_with_timeout (pid_t pid, int *status, int timeout)
3704*ef5ccd6cSJohn Marino {
3705*ef5ccd6cSJohn Marino pid_t waitpid_result;
3706*ef5ccd6cSJohn Marino
3707*ef5ccd6cSJohn Marino gdb_assert (pid > 0);
3708*ef5ccd6cSJohn Marino gdb_assert (timeout >= 0);
3709*ef5ccd6cSJohn Marino
3710*ef5ccd6cSJohn Marino if (timeout > 0)
3711*ef5ccd6cSJohn Marino {
3712*ef5ccd6cSJohn Marino #ifdef SIGALRM
3713*ef5ccd6cSJohn Marino #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
3714*ef5ccd6cSJohn Marino struct sigaction sa, old_sa;
3715*ef5ccd6cSJohn Marino
3716*ef5ccd6cSJohn Marino sa.sa_handler = sigalrm_handler;
3717*ef5ccd6cSJohn Marino sigemptyset (&sa.sa_mask);
3718*ef5ccd6cSJohn Marino sa.sa_flags = 0;
3719*ef5ccd6cSJohn Marino sigaction (SIGALRM, &sa, &old_sa);
3720*ef5ccd6cSJohn Marino #else
3721*ef5ccd6cSJohn Marino void (*ofunc) ();
3722*ef5ccd6cSJohn Marino
3723*ef5ccd6cSJohn Marino ofunc = (void (*)()) signal (SIGALRM, sigalrm_handler);
3724*ef5ccd6cSJohn Marino #endif
3725*ef5ccd6cSJohn Marino
3726*ef5ccd6cSJohn Marino alarm (timeout);
3727*ef5ccd6cSJohn Marino #endif
3728*ef5ccd6cSJohn Marino
3729*ef5ccd6cSJohn Marino waitpid_result = waitpid (pid, status, 0);
3730*ef5ccd6cSJohn Marino
3731*ef5ccd6cSJohn Marino #ifdef SIGALRM
3732*ef5ccd6cSJohn Marino alarm (0);
3733*ef5ccd6cSJohn Marino #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
3734*ef5ccd6cSJohn Marino sigaction (SIGALRM, &old_sa, NULL);
3735*ef5ccd6cSJohn Marino #else
3736*ef5ccd6cSJohn Marino signal (SIGALRM, ofunc);
3737*ef5ccd6cSJohn Marino #endif
3738*ef5ccd6cSJohn Marino #endif
3739*ef5ccd6cSJohn Marino }
3740*ef5ccd6cSJohn Marino else
3741*ef5ccd6cSJohn Marino waitpid_result = waitpid (pid, status, WNOHANG);
3742*ef5ccd6cSJohn Marino
3743*ef5ccd6cSJohn Marino if (waitpid_result == pid)
3744*ef5ccd6cSJohn Marino return pid;
3745*ef5ccd6cSJohn Marino else
3746*ef5ccd6cSJohn Marino return -1;
3747*ef5ccd6cSJohn Marino }
3748*ef5ccd6cSJohn Marino
3749*ef5ccd6cSJohn Marino #endif /* HAVE_WAITPID */
3750*ef5ccd6cSJohn Marino
3751*ef5ccd6cSJohn Marino /* Provide fnmatch compatible function for FNM_FILE_NAME matching of host files.
3752*ef5ccd6cSJohn Marino Both FNM_FILE_NAME and FNM_NOESCAPE must be set in FLAGS.
3753*ef5ccd6cSJohn Marino
3754*ef5ccd6cSJohn Marino It handles correctly HAVE_DOS_BASED_FILE_SYSTEM and
3755*ef5ccd6cSJohn Marino HAVE_CASE_INSENSITIVE_FILE_SYSTEM. */
3756*ef5ccd6cSJohn Marino
3757*ef5ccd6cSJohn Marino int
gdb_filename_fnmatch(const char * pattern,const char * string,int flags)3758*ef5ccd6cSJohn Marino gdb_filename_fnmatch (const char *pattern, const char *string, int flags)
3759*ef5ccd6cSJohn Marino {
3760*ef5ccd6cSJohn Marino gdb_assert ((flags & FNM_FILE_NAME) != 0);
3761*ef5ccd6cSJohn Marino
3762*ef5ccd6cSJohn Marino /* It is unclear how '\' escaping vs. directory separator should coexist. */
3763*ef5ccd6cSJohn Marino gdb_assert ((flags & FNM_NOESCAPE) != 0);
3764*ef5ccd6cSJohn Marino
3765*ef5ccd6cSJohn Marino #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3766*ef5ccd6cSJohn Marino {
3767*ef5ccd6cSJohn Marino char *pattern_slash, *string_slash;
3768*ef5ccd6cSJohn Marino
3769*ef5ccd6cSJohn Marino /* Replace '\' by '/' in both strings. */
3770*ef5ccd6cSJohn Marino
3771*ef5ccd6cSJohn Marino pattern_slash = alloca (strlen (pattern) + 1);
3772*ef5ccd6cSJohn Marino strcpy (pattern_slash, pattern);
3773*ef5ccd6cSJohn Marino pattern = pattern_slash;
3774*ef5ccd6cSJohn Marino for (; *pattern_slash != 0; pattern_slash++)
3775*ef5ccd6cSJohn Marino if (IS_DIR_SEPARATOR (*pattern_slash))
3776*ef5ccd6cSJohn Marino *pattern_slash = '/';
3777*ef5ccd6cSJohn Marino
3778*ef5ccd6cSJohn Marino string_slash = alloca (strlen (string) + 1);
3779*ef5ccd6cSJohn Marino strcpy (string_slash, string);
3780*ef5ccd6cSJohn Marino string = string_slash;
3781*ef5ccd6cSJohn Marino for (; *string_slash != 0; string_slash++)
3782*ef5ccd6cSJohn Marino if (IS_DIR_SEPARATOR (*string_slash))
3783*ef5ccd6cSJohn Marino *string_slash = '/';
3784*ef5ccd6cSJohn Marino }
3785*ef5ccd6cSJohn Marino #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
3786*ef5ccd6cSJohn Marino
3787*ef5ccd6cSJohn Marino #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
3788*ef5ccd6cSJohn Marino flags |= FNM_CASEFOLD;
3789*ef5ccd6cSJohn Marino #endif /* HAVE_CASE_INSENSITIVE_FILE_SYSTEM */
3790*ef5ccd6cSJohn Marino
3791*ef5ccd6cSJohn Marino return fnmatch (pattern, string, flags);
3792*ef5ccd6cSJohn Marino }
3793*ef5ccd6cSJohn Marino
37945796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes. */
37955796c8dcSSimon Schubert extern initialize_file_ftype _initialize_utils;
37965796c8dcSSimon Schubert
37975796c8dcSSimon Schubert void
_initialize_utils(void)37985796c8dcSSimon Schubert _initialize_utils (void)
37995796c8dcSSimon Schubert {
38005796c8dcSSimon Schubert add_internal_problem_command (&internal_error_problem);
38015796c8dcSSimon Schubert add_internal_problem_command (&internal_warning_problem);
38025796c8dcSSimon Schubert }
3803