]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/utils.c
arc: Put DBNZ instruction to a separate class
[thirdparty/binutils-gdb.git] / gdb / utils.c
CommitLineData
c906108c 1/* General utility routines for GDB, the GNU debugger.
1bac305b 2
1d506c26 3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
4e8f7a8b 20#include "defs.h"
4e8f7a8b 21#include <ctype.h>
268a13a5 22#include "gdbsupport/gdb_wait.h"
4e8f7a8b 23#include "event-top.h"
95e54da7 24#include "gdbthread.h"
202cbf1c 25#include "fnmatch.h"
cbb099e8 26#include "gdb_bfd.h"
7991dee7
JK
27#ifdef HAVE_SYS_RESOURCE_H
28#include <sys/resource.h>
29#endif /* HAVE_SYS_RESOURCE_H */
4e8f7a8b 30
6a83354a 31#ifdef TUI
a4f8f290
AB
32/* For tui_get_command_dimension and tui_disable. */
33#include "tui/tui.h"
6a83354a
AC
34#endif
35
9d271fd8
AC
36#ifdef __GO32__
37#include <pc.h>
38#endif
39
042be3a9 40#include <signal.h>
c906108c
SS
41#include "gdbcmd.h"
42#include "serial.h"
43#include "bfd.h"
44#include "target.h"
50f182aa 45#include "gdb-demangle.h"
c906108c
SS
46#include "expression.h"
47#include "language.h"
234b45d4 48#include "charset.h"
c906108c 49#include "annotate.h"
303c8ebd 50#include "filenames.h"
7b90c3f9 51#include "symfile.h"
bf31fd38 52#include "gdbsupport/gdb_obstack.h"
9544c605 53#include "gdbcore.h"
698ba934 54#include "top.h"
13d03262 55#include "ui.h"
7c953934 56#include "main.h"
cb08cc53 57#include "solist.h"
c906108c 58
ef0f16cc 59#include "inferior.h"
ac2e2ef7 60
3b78cdbb 61#include "gdb_curses.h"
020cc13c 62
dbda9972 63#include "readline/readline.h"
c906108c 64
dcb07cfa 65#include <chrono>
75feb17d 66
390a8aca 67#include "interps.h"
d322d6d6 68#include "gdbsupport/gdb_regex.h"
268a13a5
TT
69#include "gdbsupport/job-control.h"
70#include "gdbsupport/selftest.h"
6b09f134 71#include <optional>
0662b6a7
PA
72#include "cp-support.h"
73#include <algorithm>
268a13a5 74#include "gdbsupport/pathstuff.h"
cbe56571 75#include "cli/cli-style.h"
268a13a5 76#include "gdbsupport/scope-exit.h"
0d12e84c 77#include "gdbarch.h"
2a3c1174 78#include "cli-out.h"
51e2cfa2 79#include "gdbsupport/gdb-safe-ctype.h"
91f2597b 80#include "bt-utils.h"
7904e961 81#include "gdbsupport/buildargv.h"
3cd52293 82#include "pager.h"
47ccd6b8 83#include "run-on-main-thread.h"
8626589c 84
9a4105ab 85void (*deprecated_error_begin_hook) (void);
c906108c
SS
86
87/* Prototypes for local functions */
88
eb0d3137 89static void set_screen_size (void);
a14ed312 90static void set_width (void);
c906108c 91
260c0b2a
DE
92/* Time spent in prompt_for_continue in the currently executing command
93 waiting for user to respond.
94 Initialized in make_command_stats_cleanup.
95 Modified in prompt_for_continue and defaulted_query.
96 Used in report_command_stats. */
97
dcb07cfa 98static std::chrono::steady_clock::duration prompt_for_continue_wait_time;
260c0b2a 99
75feb17d
DJ
100/* A flag indicating whether to timestamp debugging messages. */
101
3c6c449e 102bool debug_timestamp = false;
75feb17d 103
491144b5
CB
104/* True means that strings with character values >0x7F should be printed
105 as octal escapes. False means just print the value (e.g. it's an
c906108c
SS
106 international character, and the terminal or window can cope.) */
107
491144b5 108bool sevenbit_strings = false;
920d2a44
AC
109static void
110show_sevenbit_strings (struct ui_file *file, int from_tty,
111 struct cmd_list_element *c, const char *value)
112{
6cb06a8c
TT
113 gdb_printf (file, _("Printing of 8-bit characters "
114 "in strings as \\nnn is %s.\n"),
115 value);
920d2a44 116}
c906108c 117
c906108c
SS
118/* String to be printed before warning messages, if any. */
119
69bbf465 120const char *warning_pre_print = "\nwarning: ";
c906108c 121
491144b5 122bool pagination_enabled = true;
920d2a44
AC
123static void
124show_pagination_enabled (struct ui_file *file, int from_tty,
125 struct cmd_list_element *c, const char *value)
126{
6cb06a8c 127 gdb_printf (file, _("State of pagination is %s.\n"), value);
920d2a44
AC
128}
129
c906108c 130\f
49346fa7
AVK
131/* Warning hook pointer. This has to be 'static' to avoid link
132 problems with thread-locals on AIX. */
c5aa993b 133
49346fa7
AVK
134static thread_local void (*warning_hook) (const char *, va_list);
135
136/* See utils.h. */
137
138warning_hook_handler
139get_warning_hook_handler ()
140{
141 return warning_hook;
142}
143
144/* See utils.h. */
145
146scoped_restore_warning_hook::scoped_restore_warning_hook
147 (warning_hook_handler new_handler)
6fb99666 148 : m_save (warning_hook)
49346fa7 149{
6fb99666
CW
150 warning_hook = new_handler;
151}
152
153scoped_restore_warning_hook::~scoped_restore_warning_hook ()
154{
155 warning_hook = m_save;
49346fa7 156}
8731e58e 157
f5a96129
AC
158/* Print a warning message. The first argument STRING is the warning
159 message, used as an fprintf format string, the second is the
160 va_list of arguments for that string. A warning is unfiltered (not
161 paginated) so that the user does not need to page through each
162 screen full of warnings when there are lots of them. */
c906108c
SS
163
164void
f5a96129 165vwarning (const char *string, va_list args)
c906108c 166{
49346fa7
AVK
167 if (warning_hook != nullptr)
168 warning_hook (string, args);
f5a96129
AC
169 else
170 {
6b09f134 171 std::optional<target_terminal::scoped_restore_terminal_state> term_state;
0d2f5c07 172 if (target_supports_terminal_ours ())
c5ac1540 173 {
223ffa71
TT
174 term_state.emplace ();
175 target_terminal::ours_for_output ();
c5ac1540 176 }
f5a96129 177 if (warning_pre_print)
0426ad51 178 gdb_puts (warning_pre_print, gdb_stderr);
19a7b8ab 179 gdb_vprintf (gdb_stderr, string, args);
6cb06a8c 180 gdb_printf (gdb_stderr, "\n");
f5a96129 181 }
c906108c
SS
182}
183
c906108c
SS
184/* Print an error message and return to command level.
185 The first argument STRING is the error message, used as a fprintf string,
186 and the remaining args are passed as arguments to it. */
187
c25c4a8b 188void
4ce44c66
JM
189verror (const char *string, va_list args)
190{
6b1b7650 191 throw_verror (GENERIC_ERROR, string, args);
4ce44c66
JM
192}
193
2437fd32
GB
194/* Emit a message and abort. */
195
196static void ATTRIBUTE_NORETURN
197abort_with_message (const char *msg)
198{
72542b8e 199 if (current_ui == NULL)
2437fd32
GB
200 fputs (msg, stderr);
201 else
0426ad51 202 gdb_puts (msg, gdb_stderr);
2437fd32 203
036003a6 204 abort (); /* ARI: abort */
2437fd32
GB
205}
206
7991dee7
JK
207/* Dump core trying to increase the core soft limit to hard limit first. */
208
eae7090b 209void
7991dee7
JK
210dump_core (void)
211{
212#ifdef HAVE_SETRLIMIT
206c1947 213 struct rlimit rlim = { (rlim_t) RLIM_INFINITY, (rlim_t) RLIM_INFINITY };
7991dee7
JK
214
215 setrlimit (RLIMIT_CORE, &rlim);
216#endif /* HAVE_SETRLIMIT */
217
0e6e4b59
AB
218 /* Ensure that the SIGABRT we're about to raise will immediately cause
219 GDB to exit and dump core, we don't want to trigger GDB's printing of
220 a backtrace to the console here. */
221 signal (SIGABRT, SIG_DFL);
222
036003a6 223 abort (); /* ARI: abort */
7991dee7
JK
224}
225
3e43a32a 226/* Check whether GDB will be able to dump core using the dump_core
eae7090b
GB
227 function. Returns zero if GDB cannot or should not dump core.
228 If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
229 If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
7991dee7 230
eae7090b
GB
231int
232can_dump_core (enum resource_limit_kind limit_kind)
7991dee7
JK
233{
234#ifdef HAVE_GETRLIMIT
235 struct rlimit rlim;
236
237 /* Be quiet and assume we can dump if an error is returned. */
238 if (getrlimit (RLIMIT_CORE, &rlim) != 0)
239 return 1;
240
eae7090b 241 switch (limit_kind)
7991dee7 242 {
eae7090b
GB
243 case LIMIT_CUR:
244 if (rlim.rlim_cur == 0)
245 return 0;
d182e398 246 [[fallthrough]];
eae7090b
GB
247
248 case LIMIT_MAX:
249 if (rlim.rlim_max == 0)
250 return 0;
7991dee7
JK
251 }
252#endif /* HAVE_GETRLIMIT */
253
254 return 1;
255}
256
eae7090b
GB
257/* Print a warning that we cannot dump core. */
258
259void
260warn_cant_dump_core (const char *reason)
261{
6cb06a8c
TT
262 gdb_printf (gdb_stderr,
263 _("%s\nUnable to dump core, use `ulimit -c"
264 " unlimited' before executing GDB next time.\n"),
265 reason);
eae7090b
GB
266}
267
268/* Check whether GDB will be able to dump core using the dump_core
269 function, and print a warning if we cannot. */
270
271static int
272can_dump_core_warn (enum resource_limit_kind limit_kind,
273 const char *reason)
274{
275 int core_dump_allowed = can_dump_core (limit_kind);
276
277 if (!core_dump_allowed)
278 warn_cant_dump_core (reason);
279
280 return core_dump_allowed;
281}
282
3c16cced
PA
283/* Allow the user to configure the debugger behavior with respect to
284 what to do when an internal problem is detected. */
285
286const char internal_problem_ask[] = "ask";
287const char internal_problem_yes[] = "yes";
288const char internal_problem_no[] = "no";
40478521 289static const char *const internal_problem_modes[] =
3c16cced
PA
290{
291 internal_problem_ask,
292 internal_problem_yes,
293 internal_problem_no,
294 NULL
295};
3c16cced 296
90f4cc60
AB
297/* Data structure used to control how the internal_vproblem function
298 should behave. An instance of this structure is created for each
299 problem type that GDB supports. */
c906108c 300
dec43320 301struct internal_problem
c906108c 302{
90f4cc60
AB
303 /* The name of this problem type. This must not contain white space as
304 this string is used to build command names. */
dec43320 305 const char *name;
90f4cc60
AB
306
307 /* When this is true then a user command is created (based on NAME) that
308 allows the SHOULD_QUIT field to be modified, otherwise, SHOULD_QUIT
309 can't be changed from its default value by the user. */
310 bool user_settable_should_quit;
311
312 /* Reference a value from internal_problem_modes to indicate if GDB
313 should quit when it hits a problem of this type. */
3c16cced 314 const char *should_quit;
90f4cc60
AB
315
316 /* Like USER_SETTABLE_SHOULD_QUIT but for SHOULD_DUMP_CORE. */
317 bool user_settable_should_dump_core;
318
319 /* Like SHOULD_QUIT, but whether GDB should dump core. */
3c16cced 320 const char *should_dump_core;
91f2597b
AB
321
322 /* Like USER_SETTABLE_SHOULD_QUIT but for SHOULD_PRINT_BACKTRACE. */
323 bool user_settable_should_print_backtrace;
324
325 /* When this is true GDB will print a backtrace when a problem of this
326 type is encountered. */
327 bool should_print_backtrace;
dec43320
AC
328};
329
86d77f6a
AB
330/* Return true if the readline callbacks have been initialized for UI.
331 This is always true once GDB is fully initialized, but during the early
332 startup phase this is initially false. */
333
334static bool
335readline_initialized (struct ui *ui)
336{
337 return ui->call_readline != nullptr;
338}
339
dec43320
AC
340/* Report a problem, internal to GDB, to the user. Once the problem
341 has been reported, and assuming GDB didn't quit, the caller can
342 either allow execution to resume or throw an error. */
343
a0b31db1 344static void ATTRIBUTE_PRINTF (4, 0)
dec43320 345internal_vproblem (struct internal_problem *problem,
8731e58e 346 const char *file, int line, const char *fmt, va_list ap)
dec43320 347{
dec43320 348 static int dejavu;
375fc983 349 int quit_p;
7be570e7 350 int dump_core_p;
e05550d7 351 std::string reason;
c906108c 352
dec43320 353 /* Don't allow infinite error/warning recursion. */
714b1282 354 {
02cf60c7 355 static const char msg[] = "Recursive internal problem.\n";
5d502164 356
714b1282
AC
357 switch (dejavu)
358 {
359 case 0:
360 dejavu = 1;
361 break;
362 case 1:
363 dejavu = 2;
2437fd32 364 abort_with_message (msg);
714b1282
AC
365 default:
366 dejavu = 3;
dda83cd7
SM
367 /* Newer GLIBC versions put the warn_unused_result attribute
368 on write, but this is one of those rare cases where
369 ignoring the return value is correct. Casting to (void)
370 does not fix this problem. This is the solution suggested
371 at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509. */
bf1d7d9c 372 if (write (STDERR_FILENO, msg, sizeof (msg)) != sizeof (msg))
dda83cd7 373 abort (); /* ARI: abort */
714b1282
AC
374 exit (1);
375 }
376 }
c906108c 377
a4f8f290
AB
378#ifdef TUI
379 tui_disable ();
380#endif
381
714b1282
AC
382 /* Create a string containing the full error/warning message. Need
383 to call query with this full string, as otherwize the reason
384 (error/warning) and question become separated. Format using a
385 style similar to a compiler error message. Include extra detail
386 so that the user knows that they are living on the edge. */
387 {
f8bfbf22 388 std::string msg = string_vprintf (fmt, ap);
e05550d7
TT
389 reason = string_printf ("%s:%d: %s: %s\n"
390 "A problem internal to GDB has been detected,\n"
391 "further debugging may prove unreliable.",
392 file, line, problem->name, msg.c_str ());
714b1282 393 }
7be570e7 394
2437fd32 395 /* Fall back to abort_with_message if gdb_stderr is not set up. */
72542b8e 396 if (current_ui == NULL)
2437fd32 397 {
e05550d7 398 fputs (reason.c_str (), stderr);
2437fd32
GB
399 abort_with_message ("\n");
400 }
401
402 /* Try to get the message out and at the start of a new line. */
6b09f134 403 std::optional<target_terminal::scoped_restore_terminal_state> term_state;
2437fd32 404 if (target_supports_terminal_ours ())
c5ac1540 405 {
223ffa71
TT
406 term_state.emplace ();
407 target_terminal::ours_for_output ();
c5ac1540 408 }
2437fd32
GB
409 if (filtered_printing_initialized ())
410 begin_line ();
411
196a707b 412 /* Emit the message unless query will emit it below. */
2437fd32
GB
413 if (problem->should_quit != internal_problem_ask
414 || !confirm
91f2597b 415 || !filtered_printing_initialized ()
86d77f6a 416 || !readline_initialized (current_ui)
91f2597b 417 || problem->should_print_backtrace)
6cb06a8c 418 gdb_printf (gdb_stderr, "%s\n", reason.c_str ());
196a707b 419
91f2597b
AB
420 if (problem->should_print_backtrace)
421 gdb_internal_backtrace ();
422
3c16cced 423 if (problem->should_quit == internal_problem_ask)
dec43320 424 {
dec43320 425 /* Default (yes/batch case) is to quit GDB. When in batch mode
3c16cced
PA
426 this lessens the likelihood of GDB going into an infinite
427 loop. */
86d77f6a
AB
428 if (!confirm || !filtered_printing_initialized ()
429 || !readline_initialized (current_ui))
196a707b 430 quit_p = 1;
26bb68be 431 else
dda83cd7 432 quit_p = query (_("%s\nQuit this debugging session? "),
e05550d7 433 reason.c_str ());
dec43320 434 }
3c16cced
PA
435 else if (problem->should_quit == internal_problem_yes)
436 quit_p = 1;
437 else if (problem->should_quit == internal_problem_no)
438 quit_p = 0;
439 else
f34652de 440 internal_error (_("bad switch"));
dec43320 441
0426ad51 442 gdb_puts (_("\nThis is a bug, please report it."), gdb_stderr);
add6c04d 443 if (REPORT_BUGS_TO[0])
8839e3f3
TT
444 gdb_printf (gdb_stderr, _(" For instructions, see:\n%ps."),
445 styled_string (file_name_style.style (),
446 REPORT_BUGS_TO));
0426ad51 447 gdb_puts ("\n\n", gdb_stderr);
add6c04d 448
3c16cced 449 if (problem->should_dump_core == internal_problem_ask)
dec43320 450 {
e05550d7 451 if (!can_dump_core_warn (LIMIT_MAX, reason.c_str ()))
7991dee7 452 dump_core_p = 0;
86d77f6a
AB
453 else if (!filtered_printing_initialized ()
454 || !readline_initialized (current_ui))
2437fd32 455 dump_core_p = 1;
7991dee7
JK
456 else
457 {
458 /* Default (yes/batch case) is to dump core. This leaves a GDB
459 `dropping' so that it is easier to see that something went
460 wrong in GDB. */
e05550d7
TT
461 dump_core_p = query (_("%s\nCreate a core file of GDB? "),
462 reason.c_str ());
7991dee7 463 }
dec43320 464 }
3c16cced 465 else if (problem->should_dump_core == internal_problem_yes)
e05550d7 466 dump_core_p = can_dump_core_warn (LIMIT_MAX, reason.c_str ());
3c16cced
PA
467 else if (problem->should_dump_core == internal_problem_no)
468 dump_core_p = 0;
469 else
f34652de 470 internal_error (_("bad switch"));
7be570e7 471
375fc983 472 if (quit_p)
7be570e7
JM
473 {
474 if (dump_core_p)
7991dee7 475 dump_core ();
375fc983
AC
476 else
477 exit (1);
7be570e7
JM
478 }
479 else
480 {
481 if (dump_core_p)
375fc983 482 {
9b265ec2 483#ifdef HAVE_WORKING_FORK
375fc983 484 if (fork () == 0)
7991dee7 485 dump_core ();
9b265ec2 486#endif
375fc983 487 }
7be570e7 488 }
96baa820
JM
489
490 dejavu = 0;
dec43320
AC
491}
492
493static struct internal_problem internal_error_problem = {
90f4cc60 494 "internal-error", true, internal_problem_ask, true, internal_problem_ask,
91f2597b 495 true, GDB_PRINT_INTERNAL_BACKTRACE_INIT_ON
dec43320
AC
496};
497
c25c4a8b 498void
8731e58e 499internal_verror (const char *file, int line, const char *fmt, va_list ap)
dec43320
AC
500{
501 internal_vproblem (&internal_error_problem, file, line, fmt, ap);
2c51604d 502 throw_quit (_("Command aborted."));
c906108c
SS
503}
504
dec43320 505static struct internal_problem internal_warning_problem = {
90f4cc60 506 "internal-warning", true, internal_problem_ask, true, internal_problem_ask,
91f2597b 507 true, false
dec43320
AC
508};
509
510void
8731e58e 511internal_vwarning (const char *file, int line, const char *fmt, va_list ap)
dec43320
AC
512{
513 internal_vproblem (&internal_warning_problem, file, line, fmt, ap);
514}
515
57fcfb1b 516static struct internal_problem demangler_warning_problem = {
90f4cc60 517 "demangler-warning", true, internal_problem_ask, false, internal_problem_no,
91f2597b 518 false, false
57fcfb1b
GB
519};
520
521void
522demangler_vwarning (const char *file, int line, const char *fmt, va_list ap)
523{
524 internal_vproblem (&demangler_warning_problem, file, line, fmt, ap);
525}
526
527void
528demangler_warning (const char *file, int line, const char *string, ...)
529{
530 va_list ap;
531
532 va_start (ap, string);
533 demangler_vwarning (file, line, string, ap);
534 va_end (ap);
535}
536
3c16cced
PA
537/* When GDB reports an internal problem (error or warning) it gives
538 the user the opportunity to quit GDB and/or create a core file of
539 the current debug session. This function registers a few commands
540 that make it possible to specify that GDB should always or never
541 quit or create a core file, without asking. The commands look
542 like:
543
544 maint set PROBLEM-NAME quit ask|yes|no
545 maint show PROBLEM-NAME quit
546 maint set PROBLEM-NAME corefile ask|yes|no
547 maint show PROBLEM-NAME corefile
548
549 Where PROBLEM-NAME is currently "internal-error" or
550 "internal-warning". */
551
552static void
553add_internal_problem_command (struct internal_problem *problem)
554{
555 struct cmd_list_element **set_cmd_list;
556 struct cmd_list_element **show_cmd_list;
3c16cced 557
8d749320
SM
558 set_cmd_list = XNEW (struct cmd_list_element *);
559 show_cmd_list = XNEW (struct cmd_list_element *);
3c16cced
PA
560 *set_cmd_list = NULL;
561 *show_cmd_list = NULL;
562
74765668
AB
563 /* The add_basic_prefix_cmd and add_show_prefix_cmd functions take
564 ownership of the string passed in, which is why we don't need to free
565 set_doc and show_doc in this function. */
566 const char *set_doc
567 = xstrprintf (_("Configure what GDB does when %s is detected."),
8579fd13 568 problem->name).release ();
74765668
AB
569 const char *show_doc
570 = xstrprintf (_("Show what GDB does when %s is detected."),
8579fd13 571 problem->name).release ();
3c16cced 572
f54bdb6d
SM
573 add_setshow_prefix_cmd (problem->name, class_maintenance,
574 set_doc, show_doc, set_cmd_list, show_cmd_list,
575 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
3c16cced 576
57fcfb1b
GB
577 if (problem->user_settable_should_quit)
578 {
74765668
AB
579 std::string set_quit_doc
580 = string_printf (_("Set whether GDB should quit when an %s is "
581 "detected."), problem->name);
582 std::string show_quit_doc
583 = string_printf (_("Show whether GDB will quit when an %s is "
584 "detected."), problem->name);
57fcfb1b
GB
585 add_setshow_enum_cmd ("quit", class_maintenance,
586 internal_problem_modes,
587 &problem->should_quit,
74765668
AB
588 set_quit_doc.c_str (),
589 show_quit_doc.c_str (),
57fcfb1b
GB
590 NULL, /* help_doc */
591 NULL, /* setfunc */
592 NULL, /* showfunc */
593 set_cmd_list,
594 show_cmd_list);
57fcfb1b 595 }
1eefb858 596
57fcfb1b
GB
597 if (problem->user_settable_should_dump_core)
598 {
74765668
AB
599 std::string set_core_doc
600 = string_printf (_("Set whether GDB should create a core file of "
601 "GDB when %s is detected."), problem->name);
602 std::string show_core_doc
603 = string_printf (_("Show whether GDB will create a core file of "
604 "GDB when %s is detected."), problem->name);
57fcfb1b
GB
605 add_setshow_enum_cmd ("corefile", class_maintenance,
606 internal_problem_modes,
607 &problem->should_dump_core,
74765668
AB
608 set_core_doc.c_str (),
609 show_core_doc.c_str (),
57fcfb1b
GB
610 NULL, /* help_doc */
611 NULL, /* setfunc */
612 NULL, /* showfunc */
613 set_cmd_list,
614 show_cmd_list);
57fcfb1b 615 }
91f2597b
AB
616
617 if (problem->user_settable_should_print_backtrace)
618 {
619 std::string set_bt_doc
620 = string_printf (_("Set whether GDB should print a backtrace of "
621 "GDB when %s is detected."), problem->name);
622 std::string show_bt_doc
623 = string_printf (_("Show whether GDB will print a backtrace of "
624 "GDB when %s is detected."), problem->name);
625 add_setshow_boolean_cmd ("backtrace", class_maintenance,
626 &problem->should_print_backtrace,
627 set_bt_doc.c_str (),
628 show_bt_doc.c_str (),
629 NULL, /* help_doc */
630 gdb_internal_backtrace_set_cmd,
631 NULL, /* showfunc */
632 set_cmd_list,
633 show_cmd_list);
634 }
3c16cced
PA
635}
636
7c647d61
JB
637/* Same as perror_with_name except that it prints a warning instead
638 of throwing an error. */
639
640void
641perror_warning_with_name (const char *string)
642{
18e9961f
TT
643 std::string combined = perror_string (string);
644 warning (_("%s"), combined.c_str ());
7c647d61
JB
645}
646
3d38b301 647/* See utils.h. */
c906108c
SS
648
649void
3d38b301 650warning_filename_and_errno (const char *filename, int saved_errno)
c906108c 651{
3d38b301
AB
652 warning (_("%ps: %s"), styled_string (file_name_style.style (), filename),
653 safe_strerror (saved_errno));
c906108c
SS
654}
655
656/* Control C eventually causes this to be called, at a convenient time. */
657
658void
fba45db2 659quit (void)
c906108c 660{
06c868a8
JK
661 if (sync_quit_force_run)
662 {
80d03917 663 sync_quit_force_run = false;
63509715 664 throw_forced_quit ("SIGTERM");
06c868a8
JK
665 }
666
7be570e7
JM
667#ifdef __MSDOS__
668 /* No steenking SIGINT will ever be coming our way when the
669 program is resumed. Don't lie. */
2c51604d 670 throw_quit ("Quit");
7be570e7 671#else
c906108c 672 if (job_control
8731e58e 673 /* If there is no terminal switching for this target, then we can't
dda83cd7 674 possibly get screwed by the lack of job control. */
b0ed115f 675 || !target_supports_terminal_ours ())
2c51604d 676 throw_quit ("Quit");
c906108c 677 else
2c51604d 678 throw_quit ("Quit (expect signal SIGINT when the program is resumed)");
7be570e7 679#endif
c906108c
SS
680}
681
abc56d60
PA
682/* See defs.h. */
683
684void
685maybe_quit (void)
686{
47ccd6b8
TT
687 if (!is_main_thread ())
688 return;
689
048094ac 690 if (sync_quit_force_run)
abc56d60 691 quit ();
048094ac
PA
692
693 quit_handler ();
abc56d60
PA
694}
695
c906108c 696\f
c906108c 697/* Called when a memory allocation fails, with the number of bytes of
581e13c1 698 memory requested in SIZE. */
c906108c 699
c25c4a8b 700void
d26e3629 701malloc_failure (long size)
c906108c
SS
702{
703 if (size > 0)
704 {
f34652de 705 internal_error (_("virtual memory exhausted: can't allocate %ld bytes."),
8731e58e 706 size);
c906108c
SS
707 }
708 else
709 {
f34652de 710 internal_error (_("virtual memory exhausted."));
c906108c
SS
711 }
712}
713
c1cd3163
TT
714/* See common/errors.h. */
715
716void
717flush_streams ()
718{
719 gdb_stdout->flush ();
720 gdb_stderr->flush ();
721}
722
c906108c
SS
723/* My replacement for the read system call.
724 Used like `read' but keeps going if `read' returns too soon. */
725
726int
fba45db2 727myread (int desc, char *addr, int len)
c906108c 728{
52f0bd74 729 int val;
c906108c
SS
730 int orglen = len;
731
732 while (len > 0)
733 {
734 val = read (desc, addr, len);
735 if (val < 0)
736 return val;
737 if (val == 0)
738 return orglen - len;
739 len -= val;
740 addr += val;
741 }
742 return orglen;
743}
d26e3629 744
c906108c 745\f
c5aa993b 746
223ffa71
TT
747/* An RAII class that sets up to handle input and then tears down
748 during destruction. */
3eb7562a 749
223ffa71 750class scoped_input_handler
3eb7562a 751{
223ffa71 752public:
3eb7562a 753
223ffa71 754 scoped_input_handler ()
c2f97536 755 : m_quit_handler (&quit_handler, default_quit_handler),
223ffa71
TT
756 m_ui (NULL)
757 {
758 target_terminal::ours ();
8f7f9b3a 759 current_ui->register_file_handler ();
223ffa71
TT
760 if (current_ui->prompt_state == PROMPT_BLOCKED)
761 m_ui = current_ui;
762 }
3eb7562a 763
223ffa71
TT
764 ~scoped_input_handler ()
765 {
766 if (m_ui != NULL)
8f7f9b3a 767 m_ui->unregister_file_handler ();
223ffa71 768 }
3eb7562a 769
223ffa71 770 DISABLE_COPY_AND_ASSIGN (scoped_input_handler);
3eb7562a 771
223ffa71 772private:
3eb7562a 773
223ffa71
TT
774 /* Save and restore the terminal state. */
775 target_terminal::scoped_restore_terminal_state m_term_state;
3eb7562a 776
223ffa71 777 /* Save and restore the quit handler. */
c2f97536 778 scoped_restore_tmpl<quit_handler_ftype *> m_quit_handler;
223ffa71
TT
779
780 /* The saved UI, if non-NULL. */
781 struct ui *m_ui;
782};
3eb7562a 783
db1ff28b
JK
784\f
785
981c7f5a 786/* This function supports the query, nquery, and yquery functions.
cbdeadca 787 Ask user a y-or-n question and return 0 if answer is no, 1 if
981c7f5a
DJ
788 answer is yes, or default the answer to the specified default
789 (for yquery or nquery). DEFCHAR may be 'y' or 'n' to provide a
790 default answer, or '\0' for no default.
cbdeadca
JJ
791 CTLSTR is the control string and should end in "? ". It should
792 not say how to answer, because we do that.
793 ARGS are the arguments passed along with the CTLSTR argument to
794 printf. */
795
a0b31db1 796static int ATTRIBUTE_PRINTF (1, 0)
cbdeadca
JJ
797defaulted_query (const char *ctlstr, const char defchar, va_list args)
798{
cbdeadca
JJ
799 int retval;
800 int def_value;
801 char def_answer, not_def_answer;
a121b7c1 802 const char *y_string, *n_string;
cbdeadca
JJ
803
804 /* Set up according to which answer is the default. */
981c7f5a
DJ
805 if (defchar == '\0')
806 {
807 def_value = 1;
808 def_answer = 'Y';
809 not_def_answer = 'N';
810 y_string = "y";
811 n_string = "n";
812 }
813 else if (defchar == 'y')
cbdeadca
JJ
814 {
815 def_value = 1;
816 def_answer = 'Y';
817 not_def_answer = 'N';
818 y_string = "[y]";
819 n_string = "n";
820 }
821 else
822 {
823 def_value = 0;
824 def_answer = 'N';
825 not_def_answer = 'Y';
826 y_string = "y";
827 n_string = "[n]";
828 }
829
981c7f5a 830 /* Automatically answer the default value if the user did not want
a502cf95 831 prompts or the command was issued with the server prefix. */
e360902b 832 if (!confirm || server_command)
981c7f5a
DJ
833 return def_value;
834
835 /* If input isn't coming from the user directly, just say what
7a01c6e0 836 question we're asking, and then answer the default automatically. This
981c7f5a
DJ
837 way, important error messages don't get lost when talking to GDB
838 over a pipe. */
268a799a 839 if (current_ui->instream != current_ui->stdin_stream
efd3baf0 840 || !current_ui->input_interactive_p ()
26a06916
SM
841 /* Restrict queries to the main UI. */
842 || current_ui != main_ui)
981c7f5a 843 {
223ffa71
TT
844 target_terminal::scoped_restore_terminal_state term_state;
845 target_terminal::ours_for_output ();
1285ce86 846 gdb_stdout->wrap_here (0);
19a7b8ab 847 gdb_vprintf (gdb_stdout, ctlstr, args);
981c7f5a 848
6cb06a8c
TT
849 gdb_printf (_("(%s or %s) [answered %c; "
850 "input not from terminal]\n"),
851 y_string, n_string, def_answer);
981c7f5a
DJ
852
853 return def_value;
854 }
855
9a4105ab 856 if (deprecated_query_hook)
cbdeadca 857 {
223ffa71
TT
858 target_terminal::scoped_restore_terminal_state term_state;
859 return deprecated_query_hook (ctlstr, args);
651ce16a 860 }
80dbc9fd 861
981c7f5a 862 /* Format the question outside of the loop, to avoid reusing args. */
e05550d7
TT
863 std::string question = string_vprintf (ctlstr, args);
864 std::string prompt
865 = string_printf (_("%s%s(%s or %s) %s"),
866 annotation_level > 1 ? "\n\032\032pre-query\n" : "",
867 question.c_str (), y_string, n_string,
868 annotation_level > 1 ? "\n\032\032query\n" : "");
981c7f5a 869
dcb07cfa
PA
870 /* Used to add duration we waited for user to respond to
871 prompt_for_continue_wait_time. */
872 using namespace std::chrono;
873 steady_clock::time_point prompt_started = steady_clock::now ();
260c0b2a 874
223ffa71 875 scoped_input_handler prepare_input;
651ce16a 876
cbdeadca
JJ
877 while (1)
878 {
588dcc3e 879 char *response, answer;
cbdeadca 880
cbdeadca 881 gdb_flush (gdb_stdout);
e05550d7 882 response = gdb_readline_wrapper (prompt.c_str ());
cbdeadca 883
588dcc3e 884 if (response == NULL) /* C-d */
cbdeadca 885 {
6cb06a8c 886 gdb_printf ("EOF [assumed %c]\n", def_answer);
cbdeadca
JJ
887 retval = def_value;
888 break;
889 }
588dcc3e
PP
890
891 answer = response[0];
892 xfree (response);
cbdeadca
JJ
893
894 if (answer >= 'a')
895 answer -= 040;
896 /* Check answer. For the non-default, the user must specify
dda83cd7 897 the non-default explicitly. */
cbdeadca
JJ
898 if (answer == not_def_answer)
899 {
900 retval = !def_value;
901 break;
902 }
981c7f5a 903 /* Otherwise, if a default was specified, the user may either
dda83cd7
SM
904 specify the required input or have it default by entering
905 nothing. */
981c7f5a 906 if (answer == def_answer
588dcc3e 907 || (defchar != '\0' && answer == '\0'))
cbdeadca
JJ
908 {
909 retval = def_value;
910 break;
911 }
912 /* Invalid entries are not defaulted and require another selection. */
6cb06a8c
TT
913 gdb_printf (_("Please answer %s or %s.\n"),
914 y_string, n_string);
cbdeadca
JJ
915 }
916
260c0b2a 917 /* Add time spend in this routine to prompt_for_continue_wait_time. */
dcb07cfa 918 prompt_for_continue_wait_time += steady_clock::now () - prompt_started;
260c0b2a 919
cbdeadca 920 if (annotation_level > 1)
6cb06a8c 921 gdb_printf (("\n\032\032post-query\n"));
cbdeadca
JJ
922 return retval;
923}
924\f
925
926/* Ask user a y-or-n question and return 0 if answer is no, 1 if
927 answer is yes, or 0 if answer is defaulted.
928 Takes three args which are given to printf to print the question.
929 The first, a control string, should end in "? ".
930 It should not say how to answer, because we do that. */
931
932int
933nquery (const char *ctlstr, ...)
934{
935 va_list args;
899500d6 936 int ret;
cbdeadca
JJ
937
938 va_start (args, ctlstr);
899500d6 939 ret = defaulted_query (ctlstr, 'n', args);
cbdeadca 940 va_end (args);
899500d6 941 return ret;
cbdeadca
JJ
942}
943
944/* Ask user a y-or-n question and return 0 if answer is no, 1 if
945 answer is yes, or 1 if answer is defaulted.
946 Takes three args which are given to printf to print the question.
947 The first, a control string, should end in "? ".
948 It should not say how to answer, because we do that. */
949
950int
951yquery (const char *ctlstr, ...)
952{
953 va_list args;
899500d6 954 int ret;
cbdeadca
JJ
955
956 va_start (args, ctlstr);
899500d6 957 ret = defaulted_query (ctlstr, 'y', args);
cbdeadca 958 va_end (args);
899500d6 959 return ret;
cbdeadca
JJ
960}
961
981c7f5a
DJ
962/* Ask user a y-or-n question and return 1 iff answer is yes.
963 Takes three args which are given to printf to print the question.
964 The first, a control string, should end in "? ".
965 It should not say how to answer, because we do that. */
966
967int
968query (const char *ctlstr, ...)
969{
970 va_list args;
899500d6 971 int ret;
981c7f5a
DJ
972
973 va_start (args, ctlstr);
899500d6 974 ret = defaulted_query (ctlstr, '\0', args);
981c7f5a 975 va_end (args);
899500d6 976 return ret;
981c7f5a
DJ
977}
978
6c7a06a3
TT
979/* A helper for parse_escape that converts a host character to a
980 target character. C is the host character. If conversion is
981 possible, then the target character is stored in *TARGET_C and the
982 function returns 1. Otherwise, the function returns 0. */
983
984static int
f870a310 985host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c)
234b45d4 986{
6c7a06a3 987 char the_char = c;
6c7a06a3 988 int result = 0;
234b45d4 989
8268c778 990 auto_obstack host_data;
234b45d4 991
f870a310 992 convert_between_encodings (target_charset (gdbarch), host_charset (),
ac91cd70
PA
993 (gdb_byte *) &the_char, 1, 1,
994 &host_data, translit_none);
6c7a06a3
TT
995
996 if (obstack_object_size (&host_data) == 1)
997 {
998 result = 1;
999 *target_c = *(char *) obstack_base (&host_data);
1000 }
1001
6c7a06a3 1002 return result;
234b45d4
KB
1003}
1004
c906108c
SS
1005/* Parse a C escape sequence. STRING_PTR points to a variable
1006 containing a pointer to the string to parse. That pointer
1007 should point to the character after the \. That pointer
1008 is updated past the characters we use. The value of the
1009 escape sequence is returned.
1010
1011 A negative value means the sequence \ newline was seen,
1012 which is supposed to be equivalent to nothing at all.
1013
1014 If \ is followed by a null character, we return a negative
1015 value and leave the string pointer pointing at the null character.
1016
1017 If \ is followed by 000, we return 0 and leave the string pointer
1018 after the zeros. A value of 0 does not mean end of string. */
1019
1020int
d7561cbb 1021parse_escape (struct gdbarch *gdbarch, const char **string_ptr)
c906108c 1022{
581e13c1 1023 int target_char = -2; /* Initialize to avoid GCC warnings. */
52f0bd74 1024 int c = *(*string_ptr)++;
e0627e85 1025
6c7a06a3
TT
1026 switch (c)
1027 {
8731e58e
AC
1028 case '\n':
1029 return -2;
1030 case 0:
1031 (*string_ptr)--;
1032 return 0;
8731e58e
AC
1033
1034 case '0':
1035 case '1':
1036 case '2':
1037 case '3':
1038 case '4':
1039 case '5':
1040 case '6':
1041 case '7':
1042 {
2b531492 1043 int i = fromhex (c);
aa1ee363 1044 int count = 0;
8731e58e
AC
1045 while (++count < 3)
1046 {
5cb316ef 1047 c = (**string_ptr);
51e2cfa2 1048 if (ISDIGIT (c) && c != '8' && c != '9')
8731e58e 1049 {
5cb316ef 1050 (*string_ptr)++;
8731e58e 1051 i *= 8;
2b531492 1052 i += fromhex (c);
8731e58e
AC
1053 }
1054 else
1055 {
8731e58e
AC
1056 break;
1057 }
1058 }
1059 return i;
1060 }
6c7a06a3
TT
1061
1062 case 'a':
1063 c = '\a';
1064 break;
1065 case 'b':
1066 c = '\b';
1067 break;
1068 case 'f':
1069 c = '\f';
1070 break;
1071 case 'n':
1072 c = '\n';
1073 break;
1074 case 'r':
1075 c = '\r';
1076 break;
1077 case 't':
1078 c = '\t';
1079 break;
1080 case 'v':
1081 c = '\v';
1082 break;
1083
1084 default:
1085 break;
1086 }
1087
f870a310 1088 if (!host_char_to_target (gdbarch, c, &target_char))
3351ea09
JB
1089 error (_("The escape sequence `\\%c' is equivalent to plain `%c',"
1090 " which has no equivalent\nin the `%s' character set."),
905b671b 1091 c, c, target_charset (gdbarch));
6c7a06a3 1092 return target_char;
c906108c
SS
1093}
1094\f
c5aa993b 1095
c906108c
SS
1096/* Number of lines per page or UINT_MAX if paging is disabled. */
1097static unsigned int lines_per_page;
920d2a44
AC
1098static void
1099show_lines_per_page (struct ui_file *file, int from_tty,
1100 struct cmd_list_element *c, const char *value)
1101{
6cb06a8c
TT
1102 gdb_printf (file,
1103 _("Number of lines gdb thinks are in a page is %s.\n"),
1104 value);
920d2a44 1105}
eb0d3137 1106
cbfbd72a 1107/* Number of chars per line or UINT_MAX if line folding is disabled. */
c906108c 1108static unsigned int chars_per_line;
920d2a44
AC
1109static void
1110show_chars_per_line (struct ui_file *file, int from_tty,
1111 struct cmd_list_element *c, const char *value)
1112{
6cb06a8c
TT
1113 gdb_printf (file,
1114 _("Number of characters gdb thinks "
1115 "are in a line is %s.\n"),
1116 value);
920d2a44 1117}
eb0d3137 1118
c906108c
SS
1119/* Current count of lines printed on this page, chars on this line. */
1120static unsigned int lines_printed, chars_printed;
1121
eb6af809
TT
1122/* True if pagination is disabled for just one command. */
1123
1124static bool pagination_disabled_for_command;
1125
c906108c
SS
1126/* Buffer and start column of buffered text, for doing smarter word-
1127 wrapping. When someone calls wrap_here(), we start buffering output
0426ad51 1128 that comes through gdb_puts(). If we see a newline, we just
c906108c
SS
1129 spit it out and forget about the wrap_here(). If we see another
1130 wrap_here(), we spit it out and remember the newer one. If we see
1131 the end of the line, we spit out a newline, the indent, and then
1132 the buffered output. */
1133
c5603d50 1134static bool filter_initialized = false;
c906108c 1135
c906108c 1136\f
c5aa993b 1137
deb1ba4e
TV
1138/* See utils.h. */
1139
1140int readline_hidden_cols = 0;
1141
26c4b26f 1142/* Initialize the number of lines per page and chars per line. */
eb0d3137 1143
c906108c 1144void
fba45db2 1145init_page_info (void)
c906108c 1146{
5da1313b
JK
1147 if (batch_flag)
1148 {
1149 lines_per_page = UINT_MAX;
1150 chars_per_line = UINT_MAX;
1151 }
1152 else
c906108c 1153#if defined(TUI)
5ecb1806 1154 if (!tui_get_command_dimension (&chars_per_line, &lines_per_page))
c906108c
SS
1155#endif
1156 {
eb0d3137 1157 int rows, cols;
c906108c 1158
ec145965
EZ
1159#if defined(__GO32__)
1160 rows = ScreenRows ();
1161 cols = ScreenCols ();
1162 lines_per_page = rows;
1163 chars_per_line = cols;
1164#else
eb0d3137
MK
1165 /* Make sure Readline has initialized its terminal settings. */
1166 rl_reset_terminal (NULL);
c906108c 1167
eb0d3137
MK
1168 /* Get the screen size from Readline. */
1169 rl_get_screen_size (&rows, &cols);
deb1ba4e
TV
1170
1171 /* Readline:
1172 - ignores the COLUMNS variable when detecting screen width
1173 (because rl_prefer_env_winsize defaults to 0)
1174 - puts the detected screen width in the COLUMNS variable
1175 (because rl_change_environment defaults to 1)
1176 - may report one less than the detected screen width in
1177 rl_get_screen_size (when _rl_term_autowrap == 0).
14e61dbb
TV
1178 We could use _rl_term_autowrap, but we want to avoid introducing
1179 another dependency on readline private variables, so set
1180 readline_hidden_cols by comparing COLUMNS to cols as returned by
1181 rl_get_screen_size. */
1182 const char *columns_env_str = getenv ("COLUMNS");
1183 gdb_assert (columns_env_str != nullptr);
1184 int columns_env_val = atoi (columns_env_str);
1185 gdb_assert (columns_env_val != 0);
1186 readline_hidden_cols = columns_env_val - cols;
1187 gdb_assert (readline_hidden_cols >= 0);
1188 gdb_assert (readline_hidden_cols <= 1);
deb1ba4e 1189
eb0d3137 1190 lines_per_page = rows;
f0f6df0a 1191 chars_per_line = cols + readline_hidden_cols;
c906108c 1192
1a66331e 1193 /* Readline should have fetched the termcap entry for us.
dda83cd7
SM
1194 Only try to use tgetnum function if rl_get_screen_size
1195 did not return a useful value. */
a121b7c1 1196 if (((rows <= 0) && (tgetnum ((char *) "li") < 0))
e681cf3f
EZ
1197 /* Also disable paging if inside Emacs. $EMACS was used
1198 before Emacs v25.1, $INSIDE_EMACS is used since then. */
1199 || getenv ("EMACS") || getenv ("INSIDE_EMACS"))
eb0d3137 1200 {
1a66331e 1201 /* The number of lines per page is not mentioned in the terminal
30baf67b 1202 description or EMACS environment variable is set. This probably
1a66331e 1203 means that paging is not useful, so disable paging. */
eb0d3137
MK
1204 lines_per_page = UINT_MAX;
1205 }
c906108c 1206
c906108c 1207 /* If the output is not a terminal, don't paginate it. */
da5bd37e 1208 if (!gdb_stdout->isatty ())
c5aa993b 1209 lines_per_page = UINT_MAX;
eb0d3137 1210#endif
ec145965 1211 }
eb0d3137 1212
24b73f8e
PP
1213 /* We handle SIGWINCH ourselves. */
1214 rl_catch_sigwinch = 0;
1215
eb0d3137 1216 set_screen_size ();
c5aa993b 1217 set_width ();
c906108c
SS
1218}
1219
2437fd32
GB
1220/* Return nonzero if filtered printing is initialized. */
1221int
1222filtered_printing_initialized (void)
1223{
c5603d50 1224 return filter_initialized;
2437fd32
GB
1225}
1226
b95de2b7
TT
1227set_batch_flag_and_restore_page_info::set_batch_flag_and_restore_page_info ()
1228 : m_save_lines_per_page (lines_per_page),
1229 m_save_chars_per_line (chars_per_line),
1230 m_save_batch_flag (batch_flag)
5da1313b 1231{
b95de2b7
TT
1232 batch_flag = 1;
1233 init_page_info ();
5da1313b
JK
1234}
1235
b95de2b7 1236set_batch_flag_and_restore_page_info::~set_batch_flag_and_restore_page_info ()
5da1313b 1237{
b95de2b7
TT
1238 batch_flag = m_save_batch_flag;
1239 chars_per_line = m_save_chars_per_line;
1240 lines_per_page = m_save_lines_per_page;
5da1313b 1241
b95de2b7
TT
1242 set_screen_size ();
1243 set_width ();
5da1313b
JK
1244}
1245
f1531d04
TV
1246/* An approximation of SQRT(INT_MAX) that is:
1247 - cheap to calculate,
1248 - guaranteed to be smaller than SQRT(INT_MAX), such that
1249 sqrt_int_max * sqrt_int_max doesn't overflow, and
1250 - "close enough" to SQRT(INT_MAX), for instance for INT_MAX == 2147483647,
1251 SQRT(INT_MAX) is ~46341 and sqrt_int_max == 32767. */
1252
1253static const int sqrt_int_max = INT_MAX >> (sizeof (int) * 8 / 2);
1254
eb0d3137
MK
1255/* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE. */
1256
1257static void
1258set_screen_size (void)
1259{
1260 int rows = lines_per_page;
1261 int cols = chars_per_line;
1262
23031e31
SJ
1263 /* If we get 0 or negative ROWS or COLS, treat as "infinite" size.
1264 A negative number can be seen here with the "set width/height"
1265 commands and either:
eb0d3137 1266
23031e31 1267 - the user specified "unlimited", which maps to UINT_MAX, or
85102364 1268 - the user specified some number between INT_MAX and UINT_MAX.
23031e31
SJ
1269
1270 Cap "infinity" to approximately sqrt(INT_MAX) so that we don't
1271 overflow in rl_set_screen_size, which multiplies rows and columns
1272 to compute the number of characters on the screen. */
1273
23031e31 1274 if (rows <= 0 || rows > sqrt_int_max)
8ed25214
PA
1275 {
1276 rows = sqrt_int_max;
1277 lines_per_page = UINT_MAX;
1278 }
23031e31
SJ
1279
1280 if (cols <= 0 || cols > sqrt_int_max)
8ed25214
PA
1281 {
1282 cols = sqrt_int_max;
1283 chars_per_line = UINT_MAX;
1284 }
eb0d3137
MK
1285
1286 /* Update Readline's idea of the terminal size. */
1287 rl_set_screen_size (rows, cols);
1288}
1289
c5603d50 1290/* Reinitialize WRAP_BUFFER. */
eb0d3137 1291
c906108c 1292static void
fba45db2 1293set_width (void)
c906108c
SS
1294{
1295 if (chars_per_line == 0)
c5aa993b 1296 init_page_info ();
c906108c 1297
c5603d50 1298 filter_initialized = true;
c906108c
SS
1299}
1300
c5aa993b 1301static void
eb4c3f4a 1302set_width_command (const char *args, int from_tty, struct cmd_list_element *c)
c906108c 1303{
eb0d3137 1304 set_screen_size ();
c906108c
SS
1305 set_width ();
1306}
1307
eb0d3137 1308static void
eb4c3f4a 1309set_height_command (const char *args, int from_tty, struct cmd_list_element *c)
eb0d3137
MK
1310{
1311 set_screen_size ();
1312}
1313
d6e5e7f7
PP
1314/* See utils.h. */
1315
1316void
1317set_screen_width_and_height (int width, int height)
1318{
1319 lines_per_page = height;
1320 chars_per_line = width;
1321
1322 set_screen_size ();
1323 set_width ();
1324}
1325
f1531d04
TV
1326/* Implement "maint info screen". */
1327
1328static void
1329maintenance_info_screen (const char *args, int from_tty)
1330{
1331 int rows, cols;
1332 rl_get_screen_size (&rows, &cols);
1333
1334 gdb_printf (gdb_stdout,
1335 _("Number of characters gdb thinks "
1336 "are in a line is %u%s.\n"),
1337 chars_per_line,
1338 chars_per_line == UINT_MAX ? " (unlimited)" : "");
1339
1340 gdb_printf (gdb_stdout,
1341 _("Number of characters readline reports "
1342 "are in a line is %d%s.\n"),
1343 cols,
1344 (cols == sqrt_int_max
1345 ? " (unlimited)"
1346 : (cols == sqrt_int_max - 1
1347 ? " (unlimited - 1)"
1348 : "")));
1349
e5cbbbf7 1350#ifdef HAVE_LIBCURSES
f1531d04
TV
1351 gdb_printf (gdb_stdout,
1352 _("Number of characters curses thinks "
1353 "are in a line is %d.\n"),
1354 COLS);
e5cbbbf7 1355#endif
f1531d04
TV
1356
1357 gdb_printf (gdb_stdout,
1358 _("Number of characters environment thinks "
1359 "are in a line is %s (COLUMNS).\n"),
1360 getenv ("COLUMNS"));
1361
1362 gdb_printf (gdb_stdout,
1363 _("Number of lines gdb thinks are in a page is %u%s.\n"),
1364 lines_per_page,
1365 lines_per_page == UINT_MAX ? " (unlimited)" : "");
1366
1367 gdb_printf (gdb_stdout,
1368 _("Number of lines readline reports "
1369 "are in a page is %d%s.\n"),
1370 rows,
1371 rows == sqrt_int_max ? " (unlimited)" : "");
1372
e5cbbbf7 1373#ifdef HAVE_LIBCURSES
f1531d04
TV
1374 gdb_printf (gdb_stdout,
1375 _("Number of lines curses thinks "
1376 "are in a page is %d.\n"),
1377 LINES);
e5cbbbf7 1378#endif
f1531d04
TV
1379
1380 gdb_printf (gdb_stdout,
1381 _("Number of lines environment thinks "
1382 "are in a page is %s (LINES).\n"),
1383 getenv ("LINES"));
1384}
1385
3cd52293
TT
1386void
1387pager_file::emit_style_escape (const ui_file_style &style)
cbe56571 1388{
3cd52293 1389 if (can_emit_style_escape () && style != m_applied_style)
e7b43072 1390 {
3cd52293
TT
1391 m_applied_style = style;
1392 if (m_paging)
1393 m_stream->emit_style_escape (style);
e7b43072 1394 else
3cd52293 1395 m_wrap_buffer.append (style.to_ansi ());
e7b43072 1396 }
cbe56571
TT
1397}
1398
3cd52293 1399/* See pager.h. */
ef1dfa36
TT
1400
1401void
3cd52293 1402pager_file::reset_style ()
ef1dfa36 1403{
3cd52293 1404 if (can_emit_style_escape ())
ef1dfa36 1405 {
3cd52293
TT
1406 m_applied_style = ui_file_style ();
1407 m_wrap_buffer.append (m_applied_style.to_ansi ());
ef1dfa36
TT
1408 }
1409}
1410
c906108c 1411/* Wait, so the user can read what's on the screen. Prompt the user
720d2e96
PA
1412 to continue by pressing RETURN. 'q' is also provided because
1413 telling users what to do in the prompt is more user-friendly than
1414 expecting them to think of Ctrl-C/SIGINT. */
c906108c 1415
3cd52293
TT
1416void
1417pager_file::prompt_for_continue ()
c906108c 1418{
c906108c 1419 char cont_prompt[120];
260c0b2a
DE
1420 /* Used to add duration we waited for user to respond to
1421 prompt_for_continue_wait_time. */
dcb07cfa
PA
1422 using namespace std::chrono;
1423 steady_clock::time_point prompt_started = steady_clock::now ();
eb6af809 1424 bool disable_pagination = pagination_disabled_for_command;
c906108c 1425
3cd52293
TT
1426 scoped_restore save_paging = make_scoped_restore (&m_paging, true);
1427
cbe56571 1428 /* Clear the current styling. */
3cd52293 1429 m_stream->emit_style_escape (ui_file_style ());
cbe56571 1430
c906108c 1431 if (annotation_level > 1)
3cd52293 1432 m_stream->puts (("\n\032\032pre-prompt-for-continue\n"));
c906108c
SS
1433
1434 strcpy (cont_prompt,
eb6af809
TT
1435 "--Type <RET> for more, q to quit, "
1436 "c to continue without paging--");
c906108c
SS
1437 if (annotation_level > 1)
1438 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1439
720d2e96
PA
1440 /* We must do this *before* we call gdb_readline_wrapper, else it
1441 will eventually call us -- thinking that we're trying to print
1442 beyond the end of the screen. */
c906108c
SS
1443 reinitialize_more_filter ();
1444
223ffa71 1445 scoped_input_handler prepare_input;
82584158 1446
720d2e96
PA
1447 /* Call gdb_readline_wrapper, not readline, in order to keep an
1448 event loop running. */
5aa89276 1449 gdb::unique_xmalloc_ptr<char> ignore (gdb_readline_wrapper (cont_prompt));
c906108c 1450
260c0b2a 1451 /* Add time spend in this routine to prompt_for_continue_wait_time. */
dcb07cfa 1452 prompt_for_continue_wait_time += steady_clock::now () - prompt_started;
260c0b2a 1453
c906108c 1454 if (annotation_level > 1)
3cd52293 1455 m_stream->puts (("\n\032\032post-prompt-for-continue\n"));
c906108c 1456
80dbc9fd 1457 if (ignore != NULL)
c906108c 1458 {
5aa89276 1459 char *p = ignore.get ();
5d502164 1460
c906108c
SS
1461 while (*p == ' ' || *p == '\t')
1462 ++p;
1463 if (p[0] == 'q')
1690b616
SL
1464 /* Do not call quit here; there is no possibility of SIGINT. */
1465 throw_quit ("Quit");
eb6af809
TT
1466 if (p[0] == 'c')
1467 disable_pagination = true;
c906108c 1468 }
c906108c
SS
1469
1470 /* Now we have to do this again, so that GDB will know that it doesn't
1471 need to save the ---Type <return>--- line at the top of the screen. */
1472 reinitialize_more_filter ();
eb6af809 1473 pagination_disabled_for_command = disable_pagination;
c906108c 1474
581e13c1 1475 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
c906108c
SS
1476}
1477
26c4b26f 1478/* Initialize timer to keep track of how long we waited for the user. */
bd712aed
DE
1479
1480void
1481reset_prompt_for_continue_wait_time (void)
1482{
dcb07cfa 1483 using namespace std::chrono;
bd712aed 1484
dcb07cfa 1485 prompt_for_continue_wait_time = steady_clock::duration::zero ();
bd712aed
DE
1486}
1487
1488/* Fetch the cumulative time spent in prompt_for_continue. */
1489
dcb07cfa
PA
1490std::chrono::steady_clock::duration
1491get_prompt_for_continue_wait_time ()
bd712aed
DE
1492{
1493 return prompt_for_continue_wait_time;
1494}
1495
c906108c
SS
1496/* Reinitialize filter; ie. tell it to reset to original values. */
1497
1498void
fba45db2 1499reinitialize_more_filter (void)
c906108c
SS
1500{
1501 lines_printed = 0;
1502 chars_printed = 0;
eb6af809 1503 pagination_disabled_for_command = false;
c906108c
SS
1504}
1505
3cd52293
TT
1506void
1507pager_file::flush_wrap_buffer ()
c5603d50 1508{
3cd52293 1509 if (!m_paging && !m_wrap_buffer.empty ())
c5603d50 1510 {
3cd52293
TT
1511 m_stream->puts (m_wrap_buffer.c_str ());
1512 m_wrap_buffer.clear ();
c5603d50
TT
1513 }
1514}
1515
3cd52293
TT
1516void
1517pager_file::flush ()
1518{
1519 flush_wrap_buffer ();
1520 m_stream->flush ();
1521}
1522
faa17681
IB
1523/* See utils.h. */
1524
1525void
1526gdb_flush (struct ui_file *stream)
1527{
da5bd37e 1528 stream->flush ();
faa17681
IB
1529}
1530
2f228731
TT
1531/* See utils.h. */
1532
1533int
1534get_chars_per_line ()
1535{
1536 return chars_per_line;
1537}
1538
27d326da 1539/* See ui-file.h. */
c906108c
SS
1540
1541void
3cd52293 1542pager_file::wrap_here (int indent)
c906108c 1543{
581e13c1 1544 /* This should have been allocated, but be paranoid anyway. */
e2ff18a0 1545 gdb_assert (filter_initialized);
c906108c 1546
3cd52293 1547 flush_wrap_buffer ();
3e43a32a 1548 if (chars_per_line == UINT_MAX) /* No line overflow checking. */
c906108c 1549 {
3cd52293 1550 m_wrap_column = 0;
c906108c
SS
1551 }
1552 else if (chars_printed >= chars_per_line)
1553 {
3cd52293 1554 this->puts ("\n");
6c92c339 1555 if (indent != 0)
3cd52293
TT
1556 this->puts (n_spaces (indent));
1557 m_wrap_column = 0;
c906108c
SS
1558 }
1559 else
1560 {
3cd52293
TT
1561 m_wrap_column = chars_printed;
1562 m_wrap_indent = indent;
1563 m_wrap_style = m_applied_style;
c906108c
SS
1564 }
1565}
1566
9fbf7f08
TT
1567/* Print input string to gdb_stdout arranging strings in columns of n
1568 chars. String can be right or left justified in the column. Never
1569 prints trailing spaces. String should never be longer than width.
1570 FIXME: this could be useful for the EXAMINE command, which
1571 currently doesn't tabulate very well. */
4a351cef
AF
1572
1573void
9fbf7f08 1574puts_tabular (char *string, int width, int right)
4a351cef
AF
1575{
1576 int spaces = 0;
1577 int stringlen;
1578 char *spacebuf;
1579
1580 gdb_assert (chars_per_line > 0);
1581 if (chars_per_line == UINT_MAX)
1582 {
0426ad51
TT
1583 gdb_puts (string);
1584 gdb_puts ("\n");
4a351cef
AF
1585 return;
1586 }
1587
1588 if (((chars_printed - 1) / width + 2) * width >= chars_per_line)
0426ad51 1589 gdb_puts ("\n");
4a351cef
AF
1590
1591 if (width >= chars_per_line)
1592 width = chars_per_line - 1;
1593
1594 stringlen = strlen (string);
1595
1596 if (chars_printed > 0)
1597 spaces = width - (chars_printed - 1) % width - 1;
1598 if (right)
1599 spaces += width - stringlen;
1600
224c3ddb 1601 spacebuf = (char *) alloca (spaces + 1);
4a351cef
AF
1602 spacebuf[spaces] = '\0';
1603 while (spaces--)
1604 spacebuf[spaces] = ' ';
1605
0426ad51
TT
1606 gdb_puts (spacebuf);
1607 gdb_puts (string);
4a351cef
AF
1608}
1609
1610
c906108c 1611/* Ensure that whatever gets printed next, using the filtered output
581e13c1 1612 commands, starts at the beginning of the line. I.e. if there is
c906108c 1613 any pending output for the current line, flush it and start a new
581e13c1 1614 line. Otherwise do nothing. */
c906108c
SS
1615
1616void
fba45db2 1617begin_line (void)
c906108c
SS
1618{
1619 if (chars_printed > 0)
1620 {
0426ad51 1621 gdb_puts ("\n");
c906108c
SS
1622 }
1623}
1624
3cd52293
TT
1625void
1626pager_file::puts (const char *linebuffer)
c906108c
SS
1627{
1628 const char *lineptr;
1629
1630 if (linebuffer == 0)
1631 return;
1632
1f0f8b5d
TT
1633 /* Don't do any filtering or wrapping if both are disabled. */
1634 if (batch_flag
390a8aca 1635 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)
58dadb1b 1636 || top_level_interpreter () == NULL
29f94340 1637 || top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
c906108c 1638 {
3cd52293
TT
1639 flush_wrap_buffer ();
1640 m_stream->puts (linebuffer);
c906108c
SS
1641 return;
1642 }
1643
a0087920
TT
1644 auto buffer_clearer
1645 = make_scope_exit ([&] ()
1646 {
3cd52293
TT
1647 m_wrap_buffer.clear ();
1648 m_wrap_column = 0;
1649 m_wrap_indent = 0;
a0087920
TT
1650 });
1651
111d1981
TT
1652 /* If the user does "set height 1" then the pager will exhibit weird
1653 behavior. This is pathological, though, so don't allow it. */
1654 const unsigned int lines_allowed = (lines_per_page > 1
1655 ? lines_per_page - 1
1656 : 1);
1657
c906108c
SS
1658 /* Go through and output each character. Show line extension
1659 when this is necessary; prompt user for new page when this is
1660 necessary. */
c5aa993b 1661
c906108c
SS
1662 lineptr = linebuffer;
1663 while (*lineptr)
1664 {
eb6af809
TT
1665 /* Possible new page. Note that PAGINATION_DISABLED_FOR_COMMAND
1666 might be set during this loop, so we must continue to check
1667 it here. */
1f0f8b5d
TT
1668 if (pagination_enabled
1669 && !pagination_disabled_for_command
111d1981 1670 && lines_printed >= lines_allowed)
c906108c
SS
1671 prompt_for_continue ();
1672
1673 while (*lineptr && *lineptr != '\n')
1674 {
a0087920
TT
1675 int skip_bytes;
1676
c906108c
SS
1677 /* Print a single line. */
1678 if (*lineptr == '\t')
1679 {
3cd52293 1680 m_wrap_buffer.push_back ('\t');
c906108c 1681 /* Shifting right by 3 produces the number of tab stops
dda83cd7
SM
1682 we have already passed, and then adding one and
1683 shifting left 3 advances to the next tab stop. */
c906108c
SS
1684 chars_printed = ((chars_printed >> 3) + 1) << 3;
1685 lineptr++;
1686 }
a0087920
TT
1687 else if (*lineptr == '\033'
1688 && skip_ansi_escape (lineptr, &skip_bytes))
1689 {
3cd52293 1690 m_wrap_buffer.append (lineptr, skip_bytes);
a0087920
TT
1691 /* Note that we don't consider this a character, so we
1692 don't increment chars_printed here. */
1693 lineptr += skip_bytes;
1694 }
2f228731
TT
1695 else if (*lineptr == '\r')
1696 {
3cd52293 1697 m_wrap_buffer.push_back (*lineptr);
2f228731
TT
1698 chars_printed = 0;
1699 lineptr++;
1700 }
c906108c
SS
1701 else
1702 {
3cd52293 1703 m_wrap_buffer.push_back (*lineptr);
c906108c
SS
1704 chars_printed++;
1705 lineptr++;
1706 }
c5aa993b 1707
c906108c
SS
1708 if (chars_printed >= chars_per_line)
1709 {
1710 unsigned int save_chars = chars_printed;
1711
99f20f08
TT
1712 /* If we change the style, below, we'll want to reset it
1713 before continuing to print. If there is no wrap
1714 column, then we'll only reset the style if the pager
1715 prompt is given; and to avoid emitting style
1716 sequences in the middle of a run of text, we track
1717 this as well. */
3cd52293 1718 ui_file_style save_style = m_applied_style;
99f20f08
TT
1719 bool did_paginate = false;
1720
c906108c
SS
1721 chars_printed = 0;
1722 lines_printed++;
3cd52293 1723 if (m_wrap_column)
cbe56571 1724 {
e7b43072
AB
1725 /* We are about to insert a newline at an historic
1726 location in the WRAP_BUFFER. Before we do we want to
1727 restore the default style. To know if we actually
1728 need to insert an escape sequence we must restore the
1729 current applied style to how it was at the WRAP_COLUMN
1730 location. */
3cd52293
TT
1731 m_applied_style = m_wrap_style;
1732 m_stream->emit_style_escape (ui_file_style ());
a0087920
TT
1733 /* If we aren't actually wrapping, don't output
1734 newline -- if chars_per_line is right, we
1735 probably just overflowed anyway; if it's wrong,
1736 let us keep going. */
3cd52293 1737 m_stream->puts ("\n");
cbe56571 1738 }
a0087920 1739 else
3cd52293 1740 this->flush_wrap_buffer ();
c906108c 1741
eb6af809
TT
1742 /* Possible new page. Note that
1743 PAGINATION_DISABLED_FOR_COMMAND might be set during
1744 this loop, so we must continue to check it here. */
1f0f8b5d
TT
1745 if (pagination_enabled
1746 && !pagination_disabled_for_command
111d1981 1747 && lines_printed >= lines_allowed)
99f20f08
TT
1748 {
1749 prompt_for_continue ();
1750 did_paginate = true;
1751 }
c906108c 1752
581e13c1 1753 /* Now output indentation and wrapped string. */
3cd52293 1754 if (m_wrap_column)
c906108c 1755 {
3cd52293 1756 m_stream->puts (n_spaces (m_wrap_indent));
e7b43072
AB
1757
1758 /* Having finished inserting the wrapping we should
1759 restore the style as it was at the WRAP_COLUMN. */
3cd52293 1760 m_stream->emit_style_escape (m_wrap_style);
e7b43072
AB
1761
1762 /* The WRAP_BUFFER will still contain content, and that
1763 content might set some alternative style. Restore
1764 APPLIED_STYLE as it was before we started wrapping,
1765 this reflects the current style for the last character
1766 in WRAP_BUFFER. */
3cd52293 1767 m_applied_style = save_style;
e7b43072 1768
6c92c339 1769 /* Note that this can set chars_printed > chars_per_line
c906108c 1770 if we are printing a long string. */
3cd52293
TT
1771 chars_printed = m_wrap_indent + (save_chars - m_wrap_column);
1772 m_wrap_column = 0; /* And disable fancy wrap */
c5aa993b 1773 }
3cd52293
TT
1774 else if (did_paginate)
1775 m_stream->emit_style_escape (save_style);
c906108c
SS
1776 }
1777 }
1778
1779 if (*lineptr == '\n')
1780 {
1781 chars_printed = 0;
3cd52293 1782 wrap_here (0); /* Spit out chars, cancel further wraps. */
c906108c 1783 lines_printed++;
3cd52293 1784 m_stream->puts ("\n");
c906108c
SS
1785 lineptr++;
1786 }
1787 }
a0087920
TT
1788
1789 buffer_clearer.release ();
c906108c
SS
1790}
1791
1792void
3cd52293 1793pager_file::write (const char *buf, long length_buf)
c906108c 1794{
3cd52293
TT
1795 /* We have to make a string here because the pager uses
1796 skip_ansi_escape, which requires NUL-termination. */
1797 std::string str (buf, length_buf);
1798 this->puts (str.c_str ());
c906108c
SS
1799}
1800
1f0f8b5d
TT
1801#if GDB_SELF_TEST
1802
1803/* Test that disabling the pager does not also disable word
1804 wrapping. */
1805
1806static void
1807test_pager ()
1808{
1809 string_file *strfile = new string_file ();
1810 pager_file pager (strfile);
1811
1812 /* Make sure the pager is disabled. */
1813 scoped_restore save_enabled
1814 = make_scoped_restore (&pagination_enabled, false);
1815 scoped_restore save_disabled
1816 = make_scoped_restore (&pagination_disabled_for_command, false);
1817 scoped_restore save_batch
1818 = make_scoped_restore (&batch_flag, false);
1819 scoped_restore save_lines
1820 = make_scoped_restore (&lines_per_page, 50);
1821 /* Make it easy to word wrap. */
1822 scoped_restore save_chars
1823 = make_scoped_restore (&chars_per_line, 15);
1824 scoped_restore save_printed
1825 = make_scoped_restore (&chars_printed, 0);
1826
1827 pager.puts ("aaaaaaaaaaaa");
1828 pager.wrap_here (2);
1829 pager.puts ("bbbbbbbbbbbb\n");
1830
1831 SELF_CHECK (strfile->string () == "aaaaaaaaaaaa\n bbbbbbbbbbbb\n");
1832}
1833
1834#endif /* GDB_SELF_TEST */
1835
dfcb27e4 1836void
0426ad51 1837gdb_puts (const char *linebuffer, struct ui_file *stream)
3cd52293
TT
1838{
1839 stream->puts (linebuffer);
dfcb27e4
IB
1840}
1841
cbe56571
TT
1842/* See utils.h. */
1843
1844void
1845fputs_styled (const char *linebuffer, const ui_file_style &style,
1846 struct ui_file *stream)
1847{
3cd52293 1848 stream->emit_style_escape (style);
0426ad51 1849 gdb_puts (linebuffer, stream);
3cd52293 1850 stream->emit_style_escape (ui_file_style ());
cbe56571
TT
1851}
1852
9303eb2f
PW
1853/* See utils.h. */
1854
1855void
1856fputs_highlighted (const char *str, const compiled_regex &highlight,
1857 struct ui_file *stream)
1858{
1859 regmatch_t pmatch;
1860
1861 while (*str && highlight.exec (str, 1, &pmatch, 0) == 0)
1862 {
1863 size_t n_highlight = pmatch.rm_eo - pmatch.rm_so;
1864
1865 /* Output the part before pmatch with current style. */
1866 while (pmatch.rm_so > 0)
1867 {
a11ac3b3 1868 gdb_putc (*str, stream);
9303eb2f
PW
1869 pmatch.rm_so--;
1870 str++;
1871 }
1872
1873 /* Output pmatch with the highlight style. */
3cd52293 1874 stream->emit_style_escape (highlight_style.style ());
9303eb2f
PW
1875 while (n_highlight > 0)
1876 {
a11ac3b3 1877 gdb_putc (*str, stream);
9303eb2f
PW
1878 n_highlight--;
1879 str++;
1880 }
3cd52293 1881 stream->emit_style_escape (ui_file_style ());
9303eb2f
PW
1882 }
1883
1884 /* Output the trailing part of STR not matching HIGHLIGHT. */
1885 if (*str)
0426ad51 1886 gdb_puts (str, stream);
9303eb2f
PW
1887}
1888
4311246b 1889void
a11ac3b3 1890gdb_putc (int c)
d1f4cff8 1891{
a11ac3b3 1892 return gdb_stdout->putc (c);
d1f4cff8
AC
1893}
1894
4311246b 1895void
a11ac3b3 1896gdb_putc (int c, struct ui_file *stream)
c906108c 1897{
a11ac3b3 1898 return stream->putc (c);
c906108c
SS
1899}
1900
c906108c 1901void
19a7b8ab 1902gdb_vprintf (struct ui_file *stream, const char *format, va_list args)
c906108c 1903{
19a7b8ab 1904 stream->vprintf (format, args);
c906108c
SS
1905}
1906
1907void
19a7b8ab 1908gdb_vprintf (const char *format, va_list args)
c906108c 1909{
19a7b8ab 1910 gdb_stdout->vprintf (format, args);
c906108c
SS
1911}
1912
c906108c 1913void
6cb06a8c 1914gdb_printf (struct ui_file *stream, const char *format, ...)
c906108c
SS
1915{
1916 va_list args;
e0627e85 1917
c906108c 1918 va_start (args, format);
19a7b8ab 1919 gdb_vprintf (stream, format, args);
c906108c
SS
1920 va_end (args);
1921}
1922
cbe56571
TT
1923/* See utils.h. */
1924
1925void
1926fprintf_styled (struct ui_file *stream, const ui_file_style &style,
1927 const char *format, ...)
1928{
1929 va_list args;
1930
3cd52293 1931 stream->emit_style_escape (style);
cbe56571 1932 va_start (args, format);
19a7b8ab 1933 gdb_vprintf (stream, format, args);
cbe56571 1934 va_end (args);
3cd52293 1935 stream->emit_style_escape (ui_file_style ());
cbe56571
TT
1936}
1937
c906108c 1938void
6cb06a8c 1939gdb_printf (const char *format, ...)
c906108c
SS
1940{
1941 va_list args;
e0627e85 1942
c906108c 1943 va_start (args, format);
19a7b8ab 1944 gdb_vprintf (gdb_stdout, format, args);
c906108c
SS
1945 va_end (args);
1946}
1947
1948
c906108c 1949void
8731e58e 1950printf_unfiltered (const char *format, ...)
c906108c
SS
1951{
1952 va_list args;
e0627e85 1953
c906108c 1954 va_start (args, format);
9b716718
TT
1955 string_file file (gdb_stdout->can_emit_style_escape ());
1956 file.vprintf (format, args);
1957 gdb_stdout->puts_unfiltered (file.string ().c_str ());
c906108c
SS
1958 va_end (args);
1959}
1960
c906108c
SS
1961/* Easy -- but watch out!
1962
1963 This routine is *not* a replacement for puts()! puts() appends a newline.
1964 This one doesn't, and had better not! */
1965
1966void
0426ad51 1967gdb_puts (const char *string)
c906108c 1968{
0426ad51 1969 gdb_stdout->puts (string);
c906108c
SS
1970}
1971
c906108c
SS
1972/* Return a pointer to N spaces and a null. The pointer is good
1973 until the next call to here. */
dde238e0 1974const char *
fba45db2 1975n_spaces (int n)
c906108c 1976{
392a587b
JM
1977 char *t;
1978 static char *spaces = 0;
1979 static int max_spaces = -1;
c906108c
SS
1980
1981 if (n > max_spaces)
1982 {
84d53fa9 1983 xfree (spaces);
c5aa993b
JM
1984 spaces = (char *) xmalloc (n + 1);
1985 for (t = spaces + n; t != spaces;)
c906108c
SS
1986 *--t = ' ';
1987 spaces[n] = '\0';
1988 max_spaces = n;
1989 }
1990
1991 return spaces + max_spaces - n;
1992}
1993
1994/* Print N spaces. */
1995void
d0b1020b 1996print_spaces (int n, struct ui_file *stream)
c906108c 1997{
0426ad51 1998 gdb_puts (n_spaces (n), stream);
c906108c
SS
1999}
2000\f
4a351cef 2001/* C++/ObjC demangler stuff. */
c906108c 2002
bed009b9 2003/* fprintf_symbol attempts to demangle NAME, a symbol in language
389e51db
AC
2004 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
2005 If the name is not mangled, or the language for the name is unknown, or
581e13c1 2006 demangling is off, the name is printed in its "raw" form. */
c906108c
SS
2007
2008void
bed009b9
TT
2009fprintf_symbol (struct ui_file *stream, const char *name,
2010 enum language lang, int arg_mode)
c906108c 2011{
c906108c
SS
2012 if (name != NULL)
2013 {
2014 /* If user wants to see raw output, no problem. */
2015 if (!demangle)
2016 {
0426ad51 2017 gdb_puts (name, stream);
c906108c
SS
2018 }
2019 else
2020 {
3456e70c 2021 gdb::unique_xmalloc_ptr<char> demangled
9497d69f 2022 = language_def (lang)->demangle_symbol (name, arg_mode);
0426ad51 2023 gdb_puts (demangled ? demangled.get () : name, stream);
c906108c
SS
2024 }
2025 }
2026}
2027
0662b6a7
PA
2028/* True if CH is a character that can be part of a symbol name. I.e.,
2029 either a number, a letter, or a '_'. */
2030
2031static bool
2032valid_identifier_name_char (int ch)
2033{
51e2cfa2 2034 return (ISALNUM (ch) || ch == '_');
0662b6a7
PA
2035}
2036
2037/* Skip to end of token, or to END, whatever comes first. Input is
2038 assumed to be a C++ operator name. */
2039
2040static const char *
2041cp_skip_operator_token (const char *token, const char *end)
2042{
2043 const char *p = token;
51e2cfa2 2044 while (p != end && !ISSPACE (*p) && *p != '(')
0662b6a7
PA
2045 {
2046 if (valid_identifier_name_char (*p))
2047 {
2048 while (p != end && valid_identifier_name_char (*p))
2049 p++;
2050 return p;
2051 }
2052 else
2053 {
2054 /* Note, ordered such that among ops that share a prefix,
2055 longer comes first. This is so that the loop below can
2056 bail on first match. */
2057 static const char *ops[] =
2058 {
2059 "[",
2060 "]",
2061 "~",
2062 ",",
2063 "-=", "--", "->", "-",
2064 "+=", "++", "+",
2065 "*=", "*",
2066 "/=", "/",
2067 "%=", "%",
2068 "|=", "||", "|",
2069 "&=", "&&", "&",
2070 "^=", "^",
2071 "!=", "!",
2072 "<<=", "<=", "<<", "<",
2073 ">>=", ">=", ">>", ">",
2074 "==", "=",
2075 };
2076
2077 for (const char *op : ops)
2078 {
2079 size_t oplen = strlen (op);
2080 size_t lencmp = std::min<size_t> (oplen, end - p);
2081
2082 if (strncmp (p, op, lencmp) == 0)
2083 return p + lencmp;
2084 }
2085 /* Some unidentified character. Return it. */
2086 return p + 1;
2087 }
2088 }
2089
2090 return p;
2091}
2092
2093/* Advance STRING1/STRING2 past whitespace. */
2094
2095static void
2096skip_ws (const char *&string1, const char *&string2, const char *end_str2)
2097{
51e2cfa2 2098 while (ISSPACE (*string1))
0662b6a7 2099 string1++;
51e2cfa2 2100 while (string2 < end_str2 && ISSPACE (*string2))
0662b6a7
PA
2101 string2++;
2102}
2103
2104/* True if STRING points at the start of a C++ operator name. START
2105 is the start of the string that STRING points to, hence when
2106 reading backwards, we must not read any character before START. */
2107
2108static bool
2109cp_is_operator (const char *string, const char *start)
2110{
2111 return ((string == start
2112 || !valid_identifier_name_char (string[-1]))
2113 && strncmp (string, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
2114 && !valid_identifier_name_char (string[CP_OPERATOR_LEN]));
2115}
2116
bd69330d
PA
2117/* If *NAME points at an ABI tag, skip it and return true. Otherwise
2118 leave *NAME unmodified and return false. (see GCC's abi_tag
2119 attribute), such names are demangled as e.g.,
2120 "function[abi:cxx11]()". */
2121
2122static bool
2123skip_abi_tag (const char **name)
2124{
2125 const char *p = *name;
2126
2127 if (startswith (p, "[abi:"))
2128 {
2129 p += 5;
2130
2131 while (valid_identifier_name_char (*p))
2132 p++;
2133
2134 if (*p == ']')
2135 {
2136 p++;
2137 *name = p;
2138 return true;
2139 }
2140 }
2141 return false;
2142}
2143
64a97606
KS
2144/* If *NAME points at a template parameter list, skip it and return true.
2145 Otherwise do nothing and return false. */
2146
2147static bool
2148skip_template_parameter_list (const char **name)
2149{
2150 const char *p = *name;
2151
2152 if (*p == '<')
2153 {
2154 const char *template_param_list_end = find_toplevel_char (p + 1, '>');
2155
2156 if (template_param_list_end == NULL)
2157 return false;
2158
2159 p = template_param_list_end + 1;
2160
2161 /* Skip any whitespace that might occur after the closing of the
2162 parameter list, but only if it is the end of parameter list. */
2163 const char *q = p;
2164 while (ISSPACE (*q))
2165 ++q;
2166 if (*q == '>')
2167 p = q;
2168 *name = p;
2169 return true;
2170 }
2171
2172 return false;
2173}
2174
b5ec771e 2175/* See utils.h. */
1d550c82 2176
b5ec771e 2177int
1d550c82 2178strncmp_iw_with_mode (const char *string1, const char *string2,
0662b6a7 2179 size_t string2_len, strncmp_iw_mode mode,
bd69330d 2180 enum language language,
64a97606
KS
2181 completion_match_for_lcd *match_for_lcd,
2182 bool ignore_template_params)
c906108c 2183{
0662b6a7 2184 const char *string1_start = string1;
1d550c82 2185 const char *end_str2 = string2 + string2_len;
0662b6a7
PA
2186 bool skip_spaces = true;
2187 bool have_colon_op = (language == language_cplus
2188 || language == language_rust
2189 || language == language_fortran);
1d550c82 2190
454f8b67
AB
2191 gdb_assert (match_for_lcd == nullptr || match_for_lcd->empty ());
2192
1d550c82 2193 while (1)
c906108c 2194 {
0662b6a7 2195 if (skip_spaces
51e2cfa2
PA
2196 || ((ISSPACE (*string1) && !valid_identifier_name_char (*string2))
2197 || (ISSPACE (*string2) && !valid_identifier_name_char (*string1))))
0662b6a7
PA
2198 {
2199 skip_ws (string1, string2, end_str2);
2200 skip_spaces = false;
2201 }
2202
bd69330d
PA
2203 /* Skip [abi:cxx11] tags in the symbol name if the lookup name
2204 doesn't include them. E.g.:
2205
2206 string1: function[abi:cxx1](int)
2207 string2: function
2208
2209 string1: function[abi:cxx1](int)
2210 string2: function(int)
2211
2212 string1: Struct[abi:cxx1]::function()
2213 string2: Struct::function()
2214
2215 string1: function(Struct[abi:cxx1], int)
2216 string2: function(Struct, int)
2217 */
2218 if (string2 == end_str2
2219 || (*string2 != '[' && !valid_identifier_name_char (*string2)))
2220 {
2221 const char *abi_start = string1;
2222
2223 /* There can be more than one tag. */
2224 while (*string1 == '[' && skip_abi_tag (&string1))
2225 ;
2226
2227 if (match_for_lcd != NULL && abi_start != string1)
2228 match_for_lcd->mark_ignored_range (abi_start, string1);
2229
51e2cfa2 2230 while (ISSPACE (*string1))
bd69330d
PA
2231 string1++;
2232 }
2233
64a97606
KS
2234 /* Skip template parameters in STRING1 if STRING2 does not contain
2235 any. E.g.:
2236
2237 Case 1: User is looking for all functions named "foo".
2238 string1: foo <...> (...)
2239 string2: foo
2240
2241 Case 2: User is looking for all methods named "foo" in all template
2242 class instantiations.
2243 string1: Foo<...>::foo <...> (...)
2244 string2: Foo::foo (...)
2245
2246 Case 3: User is looking for a specific overload of a template
2247 function or method.
2248 string1: foo<...>
2249 string2: foo(...)
2250
2251 Case 4: User is looking for a specific overload of a specific
2252 template instantiation.
2253 string1: foo<A> (...)
2254 string2: foo<B> (...)
2255
2256 Case 5: User is looking wild parameter match.
2257 string1: foo<A<a<b<...> > > > (...)
2258 string2: foo<A
2259 */
2260 if (language == language_cplus && ignore_template_params
2261 && *string1 == '<' && *string2 != '<')
2262 {
2263 /* Skip any parameter list in STRING1. */
2264 const char *template_start = string1;
2265
2266 if (skip_template_parameter_list (&string1))
2267 {
2268 /* Don't mark the parameter list ignored if the user didn't
2269 try to ignore it. [Case #5 above] */
2270 if (*string2 != '\0'
2271 && match_for_lcd != NULL && template_start != string1)
2272 match_for_lcd->mark_ignored_range (template_start, string1);
2273 }
2274 }
2275
1d550c82
PA
2276 if (*string1 == '\0' || string2 == end_str2)
2277 break;
0662b6a7
PA
2278
2279 /* Handle the :: operator. */
2280 if (have_colon_op && string1[0] == ':' && string1[1] == ':')
2281 {
2282 if (*string2 != ':')
2283 return 1;
2284
2285 string1++;
2286 string2++;
2287
2288 if (string2 == end_str2)
2289 break;
2290
2291 if (*string2 != ':')
2292 return 1;
2293
2294 string1++;
2295 string2++;
2296
51e2cfa2 2297 while (ISSPACE (*string1))
0662b6a7 2298 string1++;
51e2cfa2 2299 while (string2 < end_str2 && ISSPACE (*string2))
0662b6a7
PA
2300 string2++;
2301 continue;
2302 }
2303
2304 /* Handle C++ user-defined operators. */
2305 else if (language == language_cplus
2306 && *string1 == 'o')
2307 {
2308 if (cp_is_operator (string1, string1_start))
2309 {
2310 /* An operator name in STRING1. Check STRING2. */
2311 size_t cmplen
2312 = std::min<size_t> (CP_OPERATOR_LEN, end_str2 - string2);
2313 if (strncmp (string1, string2, cmplen) != 0)
2314 return 1;
2315
2316 string1 += cmplen;
2317 string2 += cmplen;
2318
2319 if (string2 != end_str2)
2320 {
2321 /* Check for "operatorX" in STRING2. */
2322 if (valid_identifier_name_char (*string2))
2323 return 1;
2324
2325 skip_ws (string1, string2, end_str2);
2326 }
2327
2328 /* Handle operator(). */
2329 if (*string1 == '(')
2330 {
2331 if (string2 == end_str2)
2332 {
2333 if (mode == strncmp_iw_mode::NORMAL)
2334 return 0;
2335 else
2336 {
2337 /* Don't break for the regular return at the
2338 bottom, because "operator" should not
2339 match "operator()", since this open
2340 parentheses is not the parameter list
2341 start. */
2342 return *string1 != '\0';
2343 }
2344 }
2345
2346 if (*string1 != *string2)
2347 return 1;
2348
2349 string1++;
2350 string2++;
2351 }
2352
2353 while (1)
2354 {
2355 skip_ws (string1, string2, end_str2);
2356
2357 /* Skip to end of token, or to END, whatever comes
2358 first. */
2359 const char *end_str1 = string1 + strlen (string1);
2360 const char *p1 = cp_skip_operator_token (string1, end_str1);
2361 const char *p2 = cp_skip_operator_token (string2, end_str2);
2362
2363 cmplen = std::min (p1 - string1, p2 - string2);
2364 if (p2 == end_str2)
2365 {
2366 if (strncmp (string1, string2, cmplen) != 0)
2367 return 1;
2368 }
2369 else
2370 {
2371 if (p1 - string1 != p2 - string2)
2372 return 1;
2373 if (strncmp (string1, string2, cmplen) != 0)
2374 return 1;
2375 }
2376
2377 string1 += cmplen;
2378 string2 += cmplen;
2379
2380 if (*string1 == '\0' || string2 == end_str2)
2381 break;
2382 if (*string1 == '(' || *string2 == '(')
2383 break;
64a97606
KS
2384
2385 /* If STRING1 or STRING2 starts with a template
2386 parameter list, break out of operator processing. */
2387 skip_ws (string1, string2, end_str2);
2388 if (*string1 == '<' || *string2 == '<')
2389 break;
0662b6a7
PA
2390 }
2391
2392 continue;
2393 }
2394 }
2395
559a7a62
JK
2396 if (case_sensitivity == case_sensitive_on && *string1 != *string2)
2397 break;
2398 if (case_sensitivity == case_sensitive_off
51e2cfa2
PA
2399 && (TOLOWER ((unsigned char) *string1)
2400 != TOLOWER ((unsigned char) *string2)))
559a7a62 2401 break;
1d550c82 2402
0662b6a7
PA
2403 /* If we see any non-whitespace, non-identifier-name character
2404 (any of "()<>*&" etc.), then skip spaces the next time
2405 around. */
51e2cfa2 2406 if (!ISSPACE (*string1) && !valid_identifier_name_char (*string1))
0662b6a7
PA
2407 skip_spaces = true;
2408
1d550c82
PA
2409 string1++;
2410 string2++;
c906108c 2411 }
1d550c82
PA
2412
2413 if (string2 == end_str2)
2414 {
2415 if (mode == strncmp_iw_mode::NORMAL)
bd69330d
PA
2416 {
2417 /* Strip abi tag markers from the matched symbol name.
2418 Usually the ABI marker will be found on function name
2419 (automatically added because the function returns an
2420 object marked with an ABI tag). However, it's also
2421 possible to see a marker in one of the function
2422 parameters, for example.
2423
2424 string2 (lookup name):
2425 func
2426 symbol name:
2427 function(some_struct[abi:cxx11], int)
2428
2429 and for completion LCD computation we want to say that
2430 the match was for:
2431 function(some_struct, int)
2432 */
2433 if (match_for_lcd != NULL)
2434 {
2435 while ((string1 = strstr (string1, "[abi:")) != NULL)
2436 {
2437 const char *abi_start = string1;
2438
2439 /* There can be more than one tag. */
2440 while (skip_abi_tag (&string1) && *string1 == '[')
2441 ;
2442
2443 if (abi_start != string1)
2444 match_for_lcd->mark_ignored_range (abi_start, string1);
2445 }
2446 }
2447
2448 return 0;
2449 }
1d550c82 2450 else
f0bdf68d
AB
2451 {
2452 if (*string1 == '(')
2453 {
2454 int p_count = 0;
2455
2456 do
2457 {
2458 if (*string1 == '(')
2459 ++p_count;
2460 else if (*string1 == ')')
2461 --p_count;
2462 ++string1;
2463 }
2464 while (*string1 != '\0' && p_count > 0);
2465
2466 /* There maybe things like 'const' after the parameters,
2467 which we do want to ignore. However, if there's an '@'
2468 then this likely indicates something like '@plt' which we
2469 should not ignore. */
2470 return *string1 == '@';
2471 }
2472
2473 return *string1 == '\0' ? 0 : 1;
2474 }
2475
1d550c82
PA
2476 }
2477 else
2478 return 1;
2479}
2480
b05752c2
KS
2481#if GDB_SELF_TEST
2482
2483/* Unit tests for strncmp_iw_with_mode. */
2484
2485#define CHECK_MATCH_LM(S1, S2, MODE, LANG, LCD) \
2486 SELF_CHECK (strncmp_iw_with_mode ((S1), (S2), strlen ((S2)), \
2487 strncmp_iw_mode::MODE, \
2488 (LANG), (LCD)) == 0)
2489
2490#define CHECK_MATCH_LANG(S1, S2, MODE, LANG) \
2491 CHECK_MATCH_LM ((S1), (S2), MODE, (LANG), nullptr)
2492
2493#define CHECK_MATCH(S1, S2, MODE) \
2494 CHECK_MATCH_LANG ((S1), (S2), MODE, language_minimal)
2495
2496#define CHECK_NO_MATCH_LM(S1, S2, MODE, LANG, LCD) \
2497 SELF_CHECK (strncmp_iw_with_mode ((S1), (S2), strlen ((S2)), \
2498 strncmp_iw_mode::MODE, \
2499 (LANG)) != 0)
2500
2501#define CHECK_NO_MATCH_LANG(S1, S2, MODE, LANG) \
2502 CHECK_NO_MATCH_LM ((S1), (S2), MODE, (LANG), nullptr)
2503
2504#define CHECK_NO_MATCH(S1, S2, MODE) \
2505 CHECK_NO_MATCH_LANG ((S1), (S2), MODE, language_minimal)
2506
2507static void
2508check_scope_operator (enum language lang)
2509{
2510 CHECK_MATCH_LANG ("::", "::", NORMAL, lang);
2511 CHECK_MATCH_LANG ("::foo", "::", NORMAL, lang);
2512 CHECK_MATCH_LANG ("::foo", "::foo", NORMAL, lang);
2513 CHECK_MATCH_LANG (" :: foo ", "::foo", NORMAL, lang);
2514 CHECK_MATCH_LANG ("a::b", "a ::b", NORMAL, lang);
2515 CHECK_MATCH_LANG ("a::b", "a\t::b", NORMAL, lang);
2516 CHECK_MATCH_LANG ("a::b", "a \t::b", NORMAL, lang);
2517 CHECK_MATCH_LANG ("a::b", "a\t ::b", NORMAL, lang);
2518 CHECK_MATCH_LANG ("a::b", "a:: b", NORMAL, lang);
2519 CHECK_MATCH_LANG ("a::b", "a::\tb", NORMAL, lang);
2520 CHECK_MATCH_LANG ("a::b", "a:: \tb", NORMAL, lang);
2521 CHECK_MATCH_LANG ("a::b", "a::\t b", NORMAL, lang);
2522 CHECK_MATCH_LANG ("a::b", "a :: b", NORMAL, lang);
2523 CHECK_MATCH_LANG ("a::b", "a ::\tb", NORMAL, lang);
2524 CHECK_MATCH_LANG ("a::b", "a\t:: b", NORMAL, lang);
2525 CHECK_MATCH_LANG ("a::b", "a \t::\t b", NORMAL, lang);
2526 CHECK_MATCH_LANG ("a ::b", "a::b", NORMAL, lang);
2527 CHECK_MATCH_LANG ("a\t::b", "a::b", NORMAL, lang);
2528 CHECK_MATCH_LANG ("a \t::b", "a::b", NORMAL, lang);
2529 CHECK_MATCH_LANG ("a\t ::b", "a::b", NORMAL, lang);
2530 CHECK_MATCH_LANG ("a:: b", "a::b", NORMAL, lang);
2531 CHECK_MATCH_LANG ("a::\tb", "a::b", NORMAL, lang);
2532 CHECK_MATCH_LANG ("a:: \tb", "a::b", NORMAL, lang);
2533 CHECK_MATCH_LANG ("a::\t b", "a::b", NORMAL, lang);
2534 CHECK_MATCH_LANG ("a :: b", "a::b", NORMAL, lang);
2535 CHECK_MATCH_LANG ("a ::\tb", "a::b", NORMAL, lang);
2536 CHECK_MATCH_LANG ("a\t:: b", "a::b", NORMAL, lang);
2537 CHECK_MATCH_LANG ("a \t::\t b", "a::b", NORMAL, lang);
2538 CHECK_MATCH_LANG ("a::b::c", "a::b::c", NORMAL, lang);
2539 CHECK_MATCH_LANG (" a:: b:: c", "a::b::c", NORMAL, lang);
2540 CHECK_MATCH_LANG ("a::b::c", " a:: b:: c", NORMAL, lang);
2541 CHECK_MATCH_LANG ("a ::b ::c", "a::b::c", NORMAL, lang);
2542 CHECK_MATCH_LANG ("a::b::c", "a :: b:: c", NORMAL, lang);
2543 CHECK_MATCH_LANG ("\ta::\tb::\tc", "\ta::\tb::\tc", NORMAL, lang);
2544 CHECK_MATCH_LANG ("a\t::b\t::c\t", "a\t::b\t::c\t", NORMAL, lang);
2545 CHECK_MATCH_LANG (" \ta:: \tb:: \tc", " \ta:: \tb:: \tc", NORMAL, lang);
2546 CHECK_MATCH_LANG ("\t a::\t b::\t c", "\t a::\t b::\t c", NORMAL, lang);
2547 CHECK_MATCH_LANG ("a::b::c", "\ta::\tb::\tc", NORMAL, lang);
2548 CHECK_MATCH_LANG ("a::b::c", "a\t::b\t::c\t", NORMAL, lang);
2549 CHECK_MATCH_LANG ("a::b::c", " \ta:: \tb:: \tc", NORMAL, lang);
2550 CHECK_MATCH_LANG ("a::b::c", "\t a::\t b::\t c", NORMAL, lang);
2551 CHECK_MATCH_LANG ("\ta::\tb::\tc", "a::b::c", NORMAL, lang);
2552 CHECK_MATCH_LANG ("a\t::b\t::c\t", "a::b::c", NORMAL, lang);
2553 CHECK_MATCH_LANG (" \ta:: \tb:: \tc", "a::b::c", NORMAL, lang);
2554 CHECK_MATCH_LANG ("\t a::\t b::\t c", "a::b::c", NORMAL, lang);
2555 CHECK_MATCH_LANG ("a :: b:: c\t", "\ta :: b\t:: c\t\t", NORMAL, lang);
2556 CHECK_MATCH_LANG (" a::\t \t b:: c\t", "\ta ::b:: c\t\t",
2557 NORMAL, lang);
2558 CHECK_MATCH_LANG ("a :: b :: \t\t\tc\t",
2559 "\t\t\t\ta :: \t\t\t b \t\t::c",
2560 NORMAL, lang);
2561 CHECK_MATCH_LANG ("a::b()", "a", NORMAL, lang);
2562 CHECK_MATCH_LANG ("a::b()", "a::", NORMAL, lang);
2563 CHECK_MATCH_LANG ("a::b()", "a::b", NORMAL, lang);
2564 CHECK_MATCH_LANG ("a::b(a)", "a", NORMAL, lang);
2565 CHECK_MATCH_LANG ("a::b(a)", "a::", NORMAL, lang);
2566 CHECK_MATCH_LANG ("a::b(a)", "a::b", NORMAL, lang);
2567 CHECK_MATCH_LANG ("a::b(a,b)", "a", NORMAL, lang);
2568 CHECK_MATCH_LANG ("a::b(a,b)", "a::", NORMAL, lang);
2569 CHECK_MATCH_LANG ("a::b(a,b)", "a::b", NORMAL, lang);
2570 CHECK_MATCH_LANG ("a::b(a,b,c)", "a", NORMAL, lang);
2571 CHECK_MATCH_LANG ("a::b(a,b,c)", "a::", NORMAL, lang);
2572 CHECK_MATCH_LANG ("a::b(a,b,c)", "a::b", NORMAL, lang);
2573
2574 CHECK_NO_MATCH_LANG ("a::", "::a", NORMAL, lang);
2575 CHECK_NO_MATCH_LANG ("::a", "::a()", NORMAL, lang);
2576 CHECK_NO_MATCH_LANG ("::", "::a", NORMAL, lang);
2577 CHECK_NO_MATCH_LANG ("a:::b", "a::b", NORMAL, lang);
2578 CHECK_NO_MATCH_LANG ("a::b()", "a::b(a)", NORMAL, lang);
2579 CHECK_NO_MATCH_LANG ("a::b(a)", "a::b()", NORMAL, lang);
2580 CHECK_NO_MATCH_LANG ("a::b(a,b)", "a::b(a,a)", NORMAL, lang);
2581 CHECK_NO_MATCH_LANG ("a::b", "a()", NORMAL, lang);
2582 CHECK_NO_MATCH_LANG ("a::b", "a::()", NORMAL, lang);
2583 CHECK_NO_MATCH_LANG ("a::b", "a::b()", NORMAL, lang);
2584 CHECK_NO_MATCH_LANG ("a::b", "a(a)", NORMAL, lang);
2585 CHECK_NO_MATCH_LANG ("a::b", "a::(a)", NORMAL, lang);
2586 CHECK_NO_MATCH_LANG ("a::b", "a::b()", NORMAL, lang);
2587 CHECK_NO_MATCH_LANG ("a::b", "a(a,b)", NORMAL, lang);
2588 CHECK_NO_MATCH_LANG ("a::b", "a::(a,b)", NORMAL, lang);
2589 CHECK_NO_MATCH_LANG ("a::b", "a::b(a,b)", NORMAL, lang);
2590 CHECK_NO_MATCH_LANG ("a::b", "a(a,b,c)", NORMAL, lang);
2591 CHECK_NO_MATCH_LANG ("a::b", "a::(a,b,c)", NORMAL, lang);
2592 CHECK_NO_MATCH_LANG ("a::b", "a::b(a,b,c)", NORMAL, lang);
2593}
2594
2595/* Callback for strncmp_iw_with_mode unit tests. */
2596
2597static void
2598strncmp_iw_with_mode_tests ()
2599{
2600 /* Some of the following tests are nonsensical, but could be input by a
2601 deranged script (or user). */
2602
2603 /* strncmp_iw_mode::NORMAL: strcmp()-like but ignore any whitespace... */
2604
2605 CHECK_MATCH ("", "", NORMAL);
2606 CHECK_MATCH ("foo", "foo", NORMAL);
2607 CHECK_MATCH (" foo", "foo", NORMAL);
2608 CHECK_MATCH ("foo ", "foo", NORMAL);
2609 CHECK_MATCH (" foo ", "foo", NORMAL);
2610 CHECK_MATCH (" foo", "foo", NORMAL);
2611 CHECK_MATCH ("foo ", "foo", NORMAL);
2612 CHECK_MATCH (" foo ", "foo", NORMAL);
2613 CHECK_MATCH ("\tfoo", "foo", NORMAL);
2614 CHECK_MATCH ("foo\t", "foo", NORMAL);
2615 CHECK_MATCH ("\tfoo\t", "foo", NORMAL);
2616 CHECK_MATCH (" \tfoo \t", "foo", NORMAL);
2617 CHECK_MATCH ("\t foo\t ", "foo", NORMAL);
2618 CHECK_MATCH ("\t \t \t\t\t\t foo\t\t\t \t\t \t \t \t \t ",
2619 "foo", NORMAL);
2620 CHECK_MATCH ("foo",
2621 "\t \t \t\t\t\t foo\t\t\t \t\t \t \t \t \t ",
2622 NORMAL);
2623 CHECK_MATCH ("foo bar", "foo", NORMAL);
2624 CHECK_NO_MATCH ("foo", "bar", NORMAL);
2625 CHECK_NO_MATCH ("foo bar", "foobar", NORMAL);
2626 CHECK_NO_MATCH (" foo ", "bar", NORMAL);
2627 CHECK_NO_MATCH ("foo", " bar ", NORMAL);
2628 CHECK_NO_MATCH (" \t\t foo\t\t ", "\t \t \tbar\t", NORMAL);
2629 CHECK_NO_MATCH ("@!%&", "@!%&foo", NORMAL);
2630
2631 /* ... and function parameters in STRING1. */
2632 CHECK_MATCH ("foo()", "foo()", NORMAL);
2633 CHECK_MATCH ("foo ()", "foo()", NORMAL);
2634 CHECK_MATCH ("foo ()", "foo()", NORMAL);
2635 CHECK_MATCH ("foo\t()", "foo()", NORMAL);
2636 CHECK_MATCH ("foo\t ()", "foo()", NORMAL);
2637 CHECK_MATCH ("foo \t()", "foo()", NORMAL);
2638 CHECK_MATCH ("foo()", "foo ()", NORMAL);
2639 CHECK_MATCH ("foo()", "foo ()", NORMAL);
2640 CHECK_MATCH ("foo()", "foo\t()", NORMAL);
2641 CHECK_MATCH ("foo()", "foo\t ()", NORMAL);
2642 CHECK_MATCH ("foo()", "foo \t()", NORMAL);
2643 CHECK_MATCH ("foo()", "foo()", NORMAL);
2644 CHECK_MATCH ("foo ()", "foo ()", NORMAL);
2645 CHECK_MATCH ("foo ()", "foo ()", NORMAL);
2646 CHECK_MATCH ("foo\t()", "foo\t()", NORMAL);
2647 CHECK_MATCH ("foo\t ()", "foo\t ()", NORMAL);
2648 CHECK_MATCH ("foo \t()", "foo \t()", NORMAL);
2649 CHECK_MATCH ("foo(a)", "foo(a)", NORMAL);
2650 CHECK_MATCH ("foo( a)", "foo(a)", NORMAL);
2651 CHECK_MATCH ("foo(a )", "foo(a)", NORMAL);
2652 CHECK_MATCH ("foo(\ta)", "foo(a)", NORMAL);
2653 CHECK_MATCH ("foo(a\t)", "foo(a)", NORMAL);
2654 CHECK_MATCH ("foo(\t a)", "foo(a)", NORMAL);
2655 CHECK_MATCH ("foo( \ta)", "foo(a)", NORMAL);
2656 CHECK_MATCH ("foo(a\t )", "foo(a)", NORMAL);
2657 CHECK_MATCH ("foo(a \t)", "foo(a)", NORMAL);
2658 CHECK_MATCH ("foo( a )", "foo(a)", NORMAL);
2659 CHECK_MATCH ("foo(\ta\t)", "foo(a)", NORMAL);
2660 CHECK_MATCH ("foo(\t a\t )", "foo(a)", NORMAL);
2661 CHECK_MATCH ("foo( \ta \t)", "foo(a)", NORMAL);
2662 CHECK_MATCH ("foo(a)", "foo( a)", NORMAL);
2663 CHECK_MATCH ("foo(a)", "foo(a )", NORMAL);
2664 CHECK_MATCH ("foo(a)", "foo(\ta)", NORMAL);
2665 CHECK_MATCH ("foo(a)", "foo(a\t)", NORMAL);
2666 CHECK_MATCH ("foo(a)", "foo(\t a)", NORMAL);
2667 CHECK_MATCH ("foo(a)", "foo( \ta)", NORMAL);
2668 CHECK_MATCH ("foo(a)", "foo(a\t )", NORMAL);
2669 CHECK_MATCH ("foo(a)", "foo(a \t)", NORMAL);
2670 CHECK_MATCH ("foo(a)", "foo( a )", NORMAL);
2671 CHECK_MATCH ("foo(a)", "foo(\ta\t)", NORMAL);
2672 CHECK_MATCH ("foo(a)", "foo(\t a\t )", NORMAL);
2673 CHECK_MATCH ("foo(a)", "foo( \ta \t)", NORMAL);
2674 CHECK_MATCH ("foo(a,b)", "foo(a,b)", NORMAL);
2675 CHECK_MATCH ("foo(a ,b)", "foo(a,b)", NORMAL);
2676 CHECK_MATCH ("foo(a\t,b)", "foo(a,b)", NORMAL);
2677 CHECK_MATCH ("foo(a,\tb)", "foo(a,b)", NORMAL);
2678 CHECK_MATCH ("foo(a\t,\tb)", "foo(a,b)", NORMAL);
2679 CHECK_MATCH ("foo(a \t,b)", "foo(a,b)", NORMAL);
2680 CHECK_MATCH ("foo(a\t ,b)", "foo(a,b)", NORMAL);
2681 CHECK_MATCH ("foo(a,\tb)", "foo(a,b)", NORMAL);
2682 CHECK_MATCH ("foo(a, \tb)", "foo(a,b)", NORMAL);
2683 CHECK_MATCH ("foo(a,\t b)", "foo(a,b)", NORMAL);
2684 CHECK_MATCH ("foo(a,b)", "foo(a ,b)", NORMAL);
2685 CHECK_MATCH ("foo(a,b)", "foo(a\t,b)", NORMAL);
2686 CHECK_MATCH ("foo(a,b)", "foo(a,\tb)", NORMAL);
2687 CHECK_MATCH ("foo(a,b)", "foo(a\t,\tb)", NORMAL);
2688 CHECK_MATCH ("foo(a,b)", "foo(a \t,b)", NORMAL);
2689 CHECK_MATCH ("foo(a,b)", "foo(a\t ,b)", NORMAL);
2690 CHECK_MATCH ("foo(a,b)", "foo(a,\tb)", NORMAL);
2691 CHECK_MATCH ("foo(a,b)", "foo(a, \tb)", NORMAL);
2692 CHECK_MATCH ("foo(a,b)", "foo(a,\t b)", NORMAL);
2693 CHECK_MATCH ("foo(a,b,c,d)", "foo(a,b,c,d)", NORMAL);
2694 CHECK_MATCH (" foo ( a , b , c , d ) ", "foo(a,b,c,d)", NORMAL);
2695 CHECK_MATCH (" foo ( a , b , c , d ) ", "foo( a , b , c , d )", NORMAL);
2696 CHECK_MATCH ("foo &\t*(\ta b *\t\t&)", "foo", NORMAL);
2697 CHECK_MATCH ("foo &\t*(\ta b *\t\t&)", "foo&*(a b * &)", NORMAL);
2698 CHECK_MATCH ("foo(a) b", "foo(a)", NORMAL);
2699 CHECK_MATCH ("*foo(*a&)", "*foo", NORMAL);
2700 CHECK_MATCH ("*foo(*a&)", "*foo(*a&)", NORMAL);
2701 CHECK_MATCH ("*a&b#c/^d$foo(*a&)", "*a&b#c/^d$foo", NORMAL);
2702 CHECK_MATCH ("* foo", "*foo", NORMAL);
2703 CHECK_MATCH ("foo&", "foo", NORMAL);
2704 CHECK_MATCH ("foo*", "foo", NORMAL);
2705 CHECK_MATCH ("foo.", "foo", NORMAL);
2706 CHECK_MATCH ("foo->", "foo", NORMAL);
2707
2708 CHECK_NO_MATCH ("foo", "foo(", NORMAL);
2709 CHECK_NO_MATCH ("foo", "foo()", NORMAL);
2710 CHECK_NO_MATCH ("foo", "foo(a)", NORMAL);
2711 CHECK_NO_MATCH ("foo", "foo(a)", NORMAL);
2712 CHECK_NO_MATCH ("foo", "foo*", NORMAL);
2713 CHECK_NO_MATCH ("foo", "foo (*", NORMAL);
2714 CHECK_NO_MATCH ("foo*", "foo (*", NORMAL);
2715 CHECK_NO_MATCH ("foo *", "foo (*", NORMAL);
2716 CHECK_NO_MATCH ("foo&", "foo (*", NORMAL);
2717 CHECK_NO_MATCH ("foo &", "foo (*", NORMAL);
2718 CHECK_NO_MATCH ("foo &*", "foo (&)", NORMAL);
2719 CHECK_NO_MATCH ("foo & \t *\t", "foo (*", NORMAL);
2720 CHECK_NO_MATCH ("foo & \t *\t", "foo (*", NORMAL);
2721 CHECK_NO_MATCH ("foo(a*) b", "foo(a) b", NORMAL);
2722 CHECK_NO_MATCH ("foo[aqi:A](a)", "foo(b)", NORMAL);
2723 CHECK_NO_MATCH ("*foo", "foo", NORMAL);
2724 CHECK_NO_MATCH ("*foo", "foo*", NORMAL);
2725 CHECK_NO_MATCH ("*foo*", "*foo&", NORMAL);
2726 CHECK_NO_MATCH ("*foo*", "foo *", NORMAL);
2727 CHECK_NO_MATCH ("&foo", "foo", NORMAL);
2728 CHECK_NO_MATCH ("&foo", "foo&", NORMAL);
2729 CHECK_NO_MATCH ("foo&", "&foo", NORMAL);
2730 CHECK_NO_MATCH ("foo", "foo&", NORMAL);
2731 CHECK_NO_MATCH ("foo", "foo*", NORMAL);
2732 CHECK_NO_MATCH ("foo", "foo.", NORMAL);
2733 CHECK_NO_MATCH ("foo", "foo->", NORMAL);
2734 CHECK_NO_MATCH ("foo bar", "foo()", NORMAL);
2735 CHECK_NO_MATCH ("foo bar", "foo bar()", NORMAL);
2736 CHECK_NO_MATCH ("foo()", "foo(a)", NORMAL);
2737 CHECK_NO_MATCH ("*(*)&", "*(*)*", NORMAL);
2738 CHECK_NO_MATCH ("foo(a)", "foo()", NORMAL);
2739 CHECK_NO_MATCH ("foo(a)", "foo(b)", NORMAL);
2740 CHECK_NO_MATCH ("foo(a,b)", "foo(a,b,c)", NORMAL);
2741 CHECK_NO_MATCH ("foo(a\\b)", "foo()", NORMAL);
2742 CHECK_NO_MATCH ("foo bar(a b c d)", "foobar", NORMAL);
2743 CHECK_NO_MATCH ("foo bar(a b c d)", "foobar ( a b c \td\t)\t", NORMAL);
2744
2745 /* Test scope operator. */
2746 check_scope_operator (language_minimal);
2747 check_scope_operator (language_cplus);
2748 check_scope_operator (language_fortran);
2749 check_scope_operator (language_rust);
2750
2751 /* Test C++ user-defined operators. */
2752 CHECK_MATCH_LANG ("operator foo(int&)", "operator foo(int &)", NORMAL,
2753 language_cplus);
2754 CHECK_MATCH_LANG ("operator foo(int &)", "operator foo(int &)", NORMAL,
2755 language_cplus);
2756 CHECK_MATCH_LANG ("operator foo(int\t&)", "operator foo(int\t&)", NORMAL,
2757 language_cplus);
2758 CHECK_MATCH_LANG ("operator foo (int)", "operator foo(int)", NORMAL,
2759 language_cplus);
2760 CHECK_MATCH_LANG ("operator foo\t(int)", "operator foo(int)", NORMAL,
2761 language_cplus);
2762 CHECK_MATCH_LANG ("operator foo \t(int)", "operator foo(int)", NORMAL,
2763 language_cplus);
2764 CHECK_MATCH_LANG ("operator foo (int)", "operator foo \t(int)", NORMAL,
2765 language_cplus);
2766 CHECK_MATCH_LANG ("operator foo\t(int)", "operator foo \t(int)", NORMAL,
2767 language_cplus);
2768 CHECK_MATCH_LANG ("operator foo \t(int)", "operator foo \t(int)", NORMAL,
2769 language_cplus);
2770
2771 CHECK_MATCH_LANG ("a::operator foo(int&)", "a::operator foo(int &)", NORMAL,
2772 language_cplus);
2773 CHECK_MATCH_LANG ("a :: operator foo(int &)", "a::operator foo(int &)", NORMAL,
2774 language_cplus);
2775 CHECK_MATCH_LANG ("a \t:: \toperator foo(int\t&)", "a::operator foo(int\t&)", NORMAL,
2776 language_cplus);
2777 CHECK_MATCH_LANG ("a::operator foo (int)", "a::operator foo(int)", NORMAL,
2778 language_cplus);
2779 CHECK_MATCH_LANG ("a::operator foo\t(int)", "a::operator foo(int)", NORMAL,
2780 language_cplus);
2781 CHECK_MATCH_LANG ("a::operator foo \t(int)", "a::operator foo(int)", NORMAL,
2782 language_cplus);
2783 CHECK_MATCH_LANG ("a::operator foo (int)", "a::operator foo \t(int)", NORMAL,
2784 language_cplus);
2785 CHECK_MATCH_LANG ("a::operator foo\t(int)", "a::operator foo \t(int)", NORMAL,
2786 language_cplus);
2787 CHECK_MATCH_LANG ("a::operator foo \t(int)", "a::operator foo \t(int)", NORMAL,
2788 language_cplus);
2789
2790 CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(char)", NORMAL,
2791 language_cplus);
2792 CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int *)", NORMAL,
2793 language_cplus);
2794 CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int &)", NORMAL,
2795 language_cplus);
2796 CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int, char *)", NORMAL,
2797 language_cplus);
2798 CHECK_NO_MATCH_LANG ("operator foo(int)", "operator bar(int)", NORMAL,
2799 language_cplus);
2800
2801 CHECK_NO_MATCH_LANG ("a::operator b::foo(int)", "a::operator a::foo(char)", NORMAL,
2802 language_cplus);
2803 CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int *)", NORMAL,
2804 language_cplus);
2805 CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int &)", NORMAL,
2806 language_cplus);
2807 CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int, char *)", NORMAL,
2808 language_cplus);
2809 CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator bar(int)", NORMAL,
2810 language_cplus);
2811
2812 /* Skip "[abi:cxx11]" tags in the symbol name if the lookup name
2813 doesn't include them. These are not language-specific in
2814 strncmp_iw_with_mode. */
2815
2816 CHECK_MATCH ("foo[abi:a]", "foo", NORMAL);
2817 CHECK_MATCH ("foo[abi:a]()", "foo", NORMAL);
2818 CHECK_MATCH ("foo[abi:a](a)", "foo", NORMAL);
2819 CHECK_MATCH ("foo[abi:a](a&,b*)", "foo", NORMAL);
2820 CHECK_MATCH ("foo[abi:a](a,b)", "foo(a,b)", NORMAL);
2821 CHECK_MATCH ("foo[abi:a](a,b) c", "foo(a,b) c", NORMAL);
2822 CHECK_MATCH ("foo[abi:a](a)", "foo(a)", NORMAL);
2823 CHECK_MATCH ("foo[abi:a](a,b)", "foo(a,b)", NORMAL);
2824 CHECK_MATCH ("foo[abi:a]", "foo[abi:a]", NORMAL);
2825 CHECK_MATCH ("foo[ abi:a]", "foo[abi:a]", NORMAL);
2826 CHECK_MATCH ("foo[\tabi:a]", "foo[abi:a]", NORMAL);
2827 CHECK_MATCH ("foo[ \tabi:a]", "foo[abi:a]", NORMAL);
2828 CHECK_MATCH ("foo[\t abi:a]", "foo[abi:a]", NORMAL);
2829 CHECK_MATCH ("foo[abi :a]", "foo[abi:a]", NORMAL);
2830 CHECK_MATCH ("foo[abi\t:a]", "foo[abi:a]", NORMAL);
2831 CHECK_MATCH ("foo[abi \t:a]", "foo[abi:a]", NORMAL);
2832 CHECK_MATCH ("foo[abi\t :a]", "foo[abi:a]", NORMAL);
2833 CHECK_MATCH ("foo[abi:a]", "foo[ abi:a]", NORMAL);
2834 CHECK_MATCH ("foo[abi:a]", "foo[\tabi:a]", NORMAL);
2835 CHECK_MATCH ("foo[abi:a]", "foo[ \tabi:a]", NORMAL);
2836 CHECK_MATCH ("foo[abi:a]", "foo[\t abi:a]", NORMAL);
2837 CHECK_MATCH ("foo[abi:a]", "foo[abi :a]", NORMAL);
2838 CHECK_MATCH ("foo[abi:a]", "foo[abi\t:a]", NORMAL);
2839 CHECK_MATCH ("foo[abi:a]", "foo[abi \t:a]", NORMAL);
2840 CHECK_MATCH ("foo[abi:a]", "foo[abi\t :a]", NORMAL);
2841 CHECK_MATCH ("foo[abi:a]", "foo[abi:a ]", NORMAL);
2842 CHECK_MATCH ("foo[abi:a]", "foo[abi:a\t]", NORMAL);
2843 CHECK_MATCH ("foo[abi:a]", "foo[abi:a \t]", NORMAL);
2844 CHECK_MATCH ("foo[abi:a]", "foo[abi:a\t ]", NORMAL);
2845 CHECK_MATCH ("foo[abi:a,b]", "foo[abi:a,b]", NORMAL);
2846 CHECK_MATCH ("foo[abi:::]", "foo[abi:::]", NORMAL);
2847 CHECK_MATCH ("foo[abi : : : ]", "foo[abi:::]", NORMAL);
2848 CHECK_MATCH ("foo[abi:::]", "foo[abi : : : ]", NORMAL);
2849 CHECK_MATCH ("foo[ \t abi \t:\t: : \t]",
2850 "foo[ abi : \t ::]",
2851 NORMAL);
2852 CHECK_MATCH ("foo< bar< baz< quxi > > >(int)", "foo<bar<baz<quxi>>>(int)",
2853 NORMAL);
2854 CHECK_MATCH ("\tfoo<\tbar<\tbaz\t<\tquxi\t>\t>\t>(int)",
2855 "foo<bar<baz<quxi>>>(int)", NORMAL);
2856 CHECK_MATCH (" \tfoo \t< \tbar \t< \tbaz \t< \tquxi \t> \t> \t> \t( \tint \t)",
2857 "foo<bar<baz<quxi>>>(int)", NORMAL);
2858 CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
2859 "foo < bar < baz < quxi > > > (int)", NORMAL);
2860 CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
2861 "\tfoo\t<\tbar\t<\tbaz\t<\tquxi\t>\t>\t>\t(int)", NORMAL);
2862 CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
2863 " \tfoo \t< \tbar \t< \tbaz \t< \tquxi \t> \t> \t> \t( \tint \t)", NORMAL);
2864 CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "fo", NORMAL);
2865 CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo", NORMAL);
2866 CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo<bar<baz>>::", NORMAL);
2867 CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo<bar<baz> >::foo", NORMAL);
2868 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c][abi:d])",
2869 NORMAL);
2870 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo", NORMAL);
2871 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo(bar)", NORMAL);
2872 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a](bar)", NORMAL);
2873 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo(bar[abi:c])", NORMAL);
2874 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a](bar[abi:c])", NORMAL);
2875 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar)", NORMAL);
2876 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c])",
2877 NORMAL);
2878 CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo", NORMAL);
2879 CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo()", NORMAL);
2880 CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>", NORMAL);
2881 CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz)", NORMAL);
2882 CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz[abi:b])",
2883 NORMAL);
2884 CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz[abi:A])",
2885 NORMAL);
2886 CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:a]>(char*, baz)",
2887 NORMAL);
2888 CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:A]>(char*, baz)",
2889 NORMAL);
2890 CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:a]>(char*, baz[abi:b])",
2891 NORMAL);
2892 CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])",
2893 "foo<bar[abi:a]>(char*, baz[abi:B])", NORMAL);
2894
2895 CHECK_NO_MATCH ("foo", "foo[", NORMAL);
2896 CHECK_NO_MATCH ("foo", "foo[]", NORMAL);
2897 CHECK_NO_MATCH ("foo", "foo[ a]", NORMAL);
2898 CHECK_NO_MATCH ("foo", "foo[a ]", NORMAL);
2899 CHECK_NO_MATCH ("foo", "foo[ a ]", NORMAL);
2900 CHECK_NO_MATCH ("foo", "foo[\ta]", NORMAL);
2901 CHECK_NO_MATCH ("foo", "foo[a \t]", NORMAL);
2902 CHECK_NO_MATCH ("foo", "foo[a\t ]", NORMAL);
2903 CHECK_NO_MATCH ("foo", "foo[ \ta]", NORMAL);
2904 CHECK_NO_MATCH ("foo", "foo[\t a]", NORMAL);
2905 CHECK_NO_MATCH ("foo", "foo[ \ta \t]", NORMAL);
2906 CHECK_NO_MATCH ("foo", "foo[\t a\t ]", NORMAL);
2907 CHECK_NO_MATCH ("foo", "foo[abi]", NORMAL);
2908 CHECK_NO_MATCH ("foo", "foo[ abi]", NORMAL);
2909 CHECK_NO_MATCH ("foo", "foo[abi ]", NORMAL);
2910 CHECK_NO_MATCH ("foo", "foo[\tabi]", NORMAL);
2911 CHECK_NO_MATCH ("foo", "foo[abi\t]", NORMAL);
2912 CHECK_NO_MATCH ("foo", "foo[ \tabi]", NORMAL);
2913 CHECK_NO_MATCH ("foo", "foo[\t abi]", NORMAL);
2914 CHECK_NO_MATCH ("foo", "foo[abi \t]", NORMAL);
2915 CHECK_NO_MATCH ("foo", "foo[abi\t ]", NORMAL);
2916 CHECK_NO_MATCH ("foo", "foo[abi :]", NORMAL);
2917 CHECK_NO_MATCH ("foo", "foo[abi\t:]", NORMAL);
2918 CHECK_NO_MATCH ("foo", "foo[abi \t:]", NORMAL);
2919 CHECK_NO_MATCH ("foo", "foo[abi\t :]", NORMAL);
2920 CHECK_NO_MATCH ("foo", "foo[abi: ]", NORMAL);
2921 CHECK_NO_MATCH ("foo", "foo[abi:\t]", NORMAL);
2922 CHECK_NO_MATCH ("foo", "foo[abi: \t]", NORMAL);
2923 CHECK_NO_MATCH ("foo", "foo[abi:\t ]", NORMAL);
2924 CHECK_NO_MATCH ("foo", "foo[abi: a]", NORMAL);
2925 CHECK_NO_MATCH ("foo", "foo[abi:\ta]", NORMAL);
2926 CHECK_NO_MATCH ("foo", "foo[abi: \ta]", NORMAL);
2927 CHECK_NO_MATCH ("foo", "foo[abi:\t a]", NORMAL);
2928 CHECK_NO_MATCH ("foo", "foo[abi:a ]", NORMAL);
2929 CHECK_NO_MATCH ("foo", "foo[abi:a\t]", NORMAL);
2930 CHECK_NO_MATCH ("foo", "foo[abi:a \t]", NORMAL);
2931 CHECK_NO_MATCH ("foo", "foo[abi:a\t ]", NORMAL);
2932 CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
2933 CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
2934 CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
2935 CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
2936 CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) c", NORMAL);
2937 CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) .", NORMAL);
2938 CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) *", NORMAL);
2939 CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) &", NORMAL);
2940 CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) c", NORMAL);
2941 CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) .", NORMAL);
2942 CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) *", NORMAL);
2943 CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) &", NORMAL);
2944 CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)c", NORMAL);
2945 CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b).", NORMAL);
2946 CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)*", NORMAL);
2947 CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)&", NORMAL);
2948 CHECK_NO_MATCH ("foo[abi:a](a,b) d", "foo(a,b) c", NORMAL);
2949 CHECK_NO_MATCH ("foo[abi:a](a)", "foo()", NORMAL);
2950 CHECK_NO_MATCH ("foo[abi:a](a)", "foo(b)", NORMAL);
2951 CHECK_NO_MATCH ("foo[abi:a](a)", "foo[abi:b](a)", NORMAL);
2952 CHECK_NO_MATCH ("foo[abi:a](a)", "foo[abi:a](b)", NORMAL);
2953 CHECK_NO_MATCH ("foo[abi:]", "foo[abi:a]", NORMAL);
2954 CHECK_NO_MATCH ("foo[abi:", "foo[abi:a]", NORMAL);
2955 CHECK_NO_MATCH ("foo[abi:]", "foo[abi:a", NORMAL);
2956 CHECK_NO_MATCH ("foo[abi:,]", "foo[abi:a]", NORMAL);
2957 CHECK_NO_MATCH ("foo[abi:a,b]", "foo[abi:a]", NORMAL);
2958 CHECK_NO_MATCH ("foo[abi::a]", "foo[abi:a]", NORMAL);
2959 CHECK_NO_MATCH ("foo[abi:,([a]", "foo[abi:a]", NORMAL);
2960
2961 CHECK_MATCH ("foo <a, b [, c (", "foo", NORMAL);
2962 CHECK_MATCH ("foo >a, b ], c )", "foo", NORMAL);
2963 CHECK_MATCH ("@!%&\\*", "@!%&\\*", NORMAL);
2964 CHECK_MATCH ("()", "()", NORMAL);
2965 CHECK_MATCH ("*(*)*", "*(*)*", NORMAL);
2966 CHECK_MATCH ("[]", "[]", NORMAL);
2967 CHECK_MATCH ("<>", "<>", NORMAL);
2968
2969 /* strncmp_iw_with_mode::MATCH_PARAMS: the "strcmp_iw hack." */
2970 CHECK_MATCH ("foo2", "foo", NORMAL);
2971 CHECK_NO_MATCH ("foo2", "foo", MATCH_PARAMS);
2972 CHECK_NO_MATCH ("foo2", "foo ", MATCH_PARAMS);
2973 CHECK_NO_MATCH ("foo2", "foo\t", MATCH_PARAMS);
2974 CHECK_NO_MATCH ("foo2", "foo \t", MATCH_PARAMS);
2975 CHECK_NO_MATCH ("foo2", "foo\t ", MATCH_PARAMS);
2976 CHECK_NO_MATCH ("foo2", "foo \t", MATCH_PARAMS);
2977 CHECK_NO_MATCH ("foo2", " foo", MATCH_PARAMS);
2978 CHECK_NO_MATCH ("foo2", "\tfoo", MATCH_PARAMS);
2979 CHECK_NO_MATCH ("foo2", " \tfoo", MATCH_PARAMS);
2980 CHECK_NO_MATCH ("foo2", "\t foo", MATCH_PARAMS);
2981 CHECK_NO_MATCH (" foo2", "foo", MATCH_PARAMS);
2982 CHECK_NO_MATCH ("\tfoo2", "foo", MATCH_PARAMS);
2983 CHECK_NO_MATCH (" \tfoo2", "foo", MATCH_PARAMS);
2984 CHECK_NO_MATCH ("\t foo2", "foo", MATCH_PARAMS);
2985 CHECK_NO_MATCH (" foo2 ", " foo ", MATCH_PARAMS);
2986 CHECK_NO_MATCH ("\tfoo2\t", "\tfoo\t", MATCH_PARAMS);
2987 CHECK_NO_MATCH (" \tfoo2 \t", " \tfoo \t", MATCH_PARAMS);
2988 CHECK_NO_MATCH ("\t foo2\t ", "\t foo\t ", MATCH_PARAMS);
2989 CHECK_NO_MATCH ("foo2 ", "foo", MATCH_PARAMS);
2990 CHECK_NO_MATCH ("foo2\t", "foo", MATCH_PARAMS);
2991 CHECK_NO_MATCH ("foo2 ", "foo", MATCH_PARAMS);
2992 CHECK_NO_MATCH ("foo2 \t", "foo", MATCH_PARAMS);
2993 CHECK_NO_MATCH ("foo2\t ", "foo", MATCH_PARAMS);
2994 CHECK_NO_MATCH ("foo2 (args)", "foo", MATCH_PARAMS);
2995 CHECK_NO_MATCH ("foo2 (args)", "foo", MATCH_PARAMS);
2996 CHECK_NO_MATCH ("foo2\t(args)", "foo", MATCH_PARAMS);
2997 CHECK_NO_MATCH ("foo2 \t(args)", "foo", MATCH_PARAMS);
2998 CHECK_NO_MATCH ("foo2\t (args)", "foo", MATCH_PARAMS);
2999 CHECK_NO_MATCH ("foo2 ( args)", "foo", MATCH_PARAMS);
3000 CHECK_NO_MATCH ("foo2(args )", "foo", MATCH_PARAMS);
3001 CHECK_NO_MATCH ("foo2(args\t)", "foo", MATCH_PARAMS);
3002 CHECK_NO_MATCH ("foo2 (args \t)", "foo", MATCH_PARAMS);
3003 CHECK_NO_MATCH ("foo2 (args\t )", "foo", MATCH_PARAMS);
3004 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c][abi:d])",
3005 MATCH_PARAMS);
3006 CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo", MATCH_PARAMS);
f0bdf68d
AB
3007 CHECK_NO_MATCH ("foo(args)@plt", "foo", MATCH_PARAMS);
3008 CHECK_NO_MATCH ("foo((())args(()))@plt", "foo", MATCH_PARAMS);
3009 CHECK_MATCH ("foo((())args(()))", "foo", MATCH_PARAMS);
3010 CHECK_MATCH ("foo(args) const", "foo", MATCH_PARAMS);
3011 CHECK_MATCH ("foo(args)const", "foo", MATCH_PARAMS);
b05752c2
KS
3012
3013 /* strncmp_iw_with_mode also supports case insensitivity. */
3014 {
3015 CHECK_NO_MATCH ("FoO", "foo", NORMAL);
3016 CHECK_NO_MATCH ("FoO", "foo", MATCH_PARAMS);
3017
3018 scoped_restore restore_case = make_scoped_restore (&case_sensitivity);
3019 case_sensitivity = case_sensitive_off;
3020
3021 CHECK_MATCH ("FoO", "foo", NORMAL);
3022 CHECK_MATCH ("FoO", "foo", MATCH_PARAMS);
3023 CHECK_MATCH ("foo", "FoO", NORMAL);
3024 CHECK_MATCH ("foo", "FoO", MATCH_PARAMS);
3025
3026 CHECK_MATCH ("FoO[AbI:abC]()", "foo", NORMAL);
3027 CHECK_NO_MATCH ("FoO[AbI:abC]()", "foo", MATCH_PARAMS);
3028 CHECK_MATCH ("FoO2[AbI:abC]()", "foo", NORMAL);
3029 CHECK_NO_MATCH ("FoO2[AbI:abC]()", "foo", MATCH_PARAMS);
3030
3031 CHECK_MATCH ("foo[abi:abc]()", "FoO[AbI:abC]()", NORMAL);
3032 CHECK_MATCH ("foo[abi:abc]()", "FoO[AbI:AbC]()", MATCH_PARAMS);
3033 CHECK_MATCH ("foo[abi:abc](xyz)", "FoO[AbI:abC](XyZ)", NORMAL);
3034 CHECK_MATCH ("foo[abi:abc](xyz)", "FoO[AbI:abC](XyZ)", MATCH_PARAMS);
3035 CHECK_MATCH ("foo[abi:abc][abi:def](xyz)", "FoO[AbI:abC](XyZ)", NORMAL);
3036 CHECK_MATCH ("foo[abi:abc][abi:def](xyz)", "FoO[AbI:abC](XyZ)",
3037 MATCH_PARAMS);
3038 CHECK_MATCH ("foo<bar<baz>>(bar<baz>)", "FoO<bAr<BaZ>>(bAr<BaZ>)",
3039 NORMAL);
3040 CHECK_MATCH ("foo<bar<baz>>(bar<baz>)", "FoO<bAr<BaZ>>(bAr<BaZ>)",
3041 MATCH_PARAMS);
3042 }
3043}
3044
3045#undef MATCH
3046#undef NO_MATCH
3047#endif
3048
1d550c82
PA
3049/* See utils.h. */
3050
3051int
3052strncmp_iw (const char *string1, const char *string2, size_t string2_len)
3053{
3054 return strncmp_iw_with_mode (string1, string2, string2_len,
0662b6a7 3055 strncmp_iw_mode::NORMAL, language_minimal);
1d550c82
PA
3056}
3057
3058/* See utils.h. */
3059
3060int
3061strcmp_iw (const char *string1, const char *string2)
3062{
3063 return strncmp_iw_with_mode (string1, string2, strlen (string2),
0662b6a7 3064 strncmp_iw_mode::MATCH_PARAMS, language_minimal);
c906108c 3065}
2de7ced7 3066
0fe19209
DC
3067/* This is like strcmp except that it ignores whitespace and treats
3068 '(' as the first non-NULL character in terms of ordering. Like
3069 strcmp (and unlike strcmp_iw), it returns negative if STRING1 <
3070 STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2
3071 according to that ordering.
3072
3073 If a list is sorted according to this function and if you want to
3074 find names in the list that match some fixed NAME according to
3075 strcmp_iw(LIST_ELT, NAME), then the place to start looking is right
3076 where this function would put NAME.
3077
559a7a62
JK
3078 This function must be neutral to the CASE_SENSITIVITY setting as the user
3079 may choose it during later lookup. Therefore this function always sorts
3080 primarily case-insensitively and secondarily case-sensitively.
3081
0fe19209
DC
3082 Here are some examples of why using strcmp to sort is a bad idea:
3083
3084 Whitespace example:
3085
3086 Say your partial symtab contains: "foo<char *>", "goo". Then, if
3087 we try to do a search for "foo<char*>", strcmp will locate this
3088 after "foo<char *>" and before "goo". Then lookup_partial_symbol
3089 will start looking at strings beginning with "goo", and will never
3090 see the correct match of "foo<char *>".
3091
3092 Parenthesis example:
3093
3094 In practice, this is less like to be an issue, but I'll give it a
3095 shot. Let's assume that '$' is a legitimate character to occur in
3096 symbols. (Which may well even be the case on some systems.) Then
3097 say that the partial symbol table contains "foo$" and "foo(int)".
3098 strcmp will put them in this order, since '$' < '('. Now, if the
3099 user searches for "foo", then strcmp will sort "foo" before "foo$".
3100 Then lookup_partial_symbol will notice that strcmp_iw("foo$",
3101 "foo") is false, so it won't proceed to the actual match of
3102 "foo(int)" with "foo". */
3103
3104int
3105strcmp_iw_ordered (const char *string1, const char *string2)
3106{
559a7a62
JK
3107 const char *saved_string1 = string1, *saved_string2 = string2;
3108 enum case_sensitivity case_pass = case_sensitive_off;
3109
3110 for (;;)
0fe19209 3111 {
b11b1f88
JK
3112 /* C1 and C2 are valid only if *string1 != '\0' && *string2 != '\0'.
3113 Provide stub characters if we are already at the end of one of the
3114 strings. */
3115 char c1 = 'X', c2 = 'X';
3116
3117 while (*string1 != '\0' && *string2 != '\0')
0fe19209 3118 {
51e2cfa2 3119 while (ISSPACE (*string1))
b11b1f88 3120 string1++;
51e2cfa2 3121 while (ISSPACE (*string2))
b11b1f88
JK
3122 string2++;
3123
559a7a62
JK
3124 switch (case_pass)
3125 {
3126 case case_sensitive_off:
51e2cfa2
PA
3127 c1 = TOLOWER ((unsigned char) *string1);
3128 c2 = TOLOWER ((unsigned char) *string2);
559a7a62
JK
3129 break;
3130 case case_sensitive_on:
b11b1f88
JK
3131 c1 = *string1;
3132 c2 = *string2;
559a7a62
JK
3133 break;
3134 }
b11b1f88
JK
3135 if (c1 != c2)
3136 break;
3137
3138 if (*string1 != '\0')
3139 {
3140 string1++;
3141 string2++;
3142 }
0fe19209 3143 }
b11b1f88
JK
3144
3145 switch (*string1)
0fe19209 3146 {
b11b1f88
JK
3147 /* Characters are non-equal unless they're both '\0'; we want to
3148 make sure we get the comparison right according to our
3149 comparison in the cases where one of them is '\0' or '('. */
3150 case '\0':
3151 if (*string2 == '\0')
559a7a62 3152 break;
b11b1f88
JK
3153 else
3154 return -1;
3155 case '(':
3156 if (*string2 == '\0')
3157 return 1;
3158 else
3159 return -1;
3160 default:
3161 if (*string2 == '\0' || *string2 == '(')
3162 return 1;
559a7a62
JK
3163 else if (c1 > c2)
3164 return 1;
3165 else if (c1 < c2)
3166 return -1;
3167 /* PASSTHRU */
0fe19209 3168 }
559a7a62
JK
3169
3170 if (case_pass == case_sensitive_on)
3171 return 0;
3172
3173 /* Otherwise the strings were equal in case insensitive way, make
3174 a more fine grained comparison in a case sensitive way. */
3175
3176 case_pass = case_sensitive_on;
3177 string1 = saved_string1;
3178 string2 = saved_string2;
0fe19209 3179 }
0fe19209
DC
3180}
3181
c906108c 3182\f
c5aa993b 3183
75feb17d
DJ
3184static void
3185show_debug_timestamp (struct ui_file *file, int from_tty,
3186 struct cmd_list_element *c, const char *value)
3187{
6cb06a8c
TT
3188 gdb_printf (file, _("Timestamping debugging messages is %s.\n"),
3189 value);
75feb17d 3190}
c906108c 3191\f
c5aa993b 3192
66bf4b3a 3193const char *
5af949e3 3194paddress (struct gdbarch *gdbarch, CORE_ADDR addr)
66bf4b3a
AC
3195{
3196 /* Truncate address to the size of a target address, avoiding shifts
3197 larger or equal than the width of a CORE_ADDR. The local
3198 variable ADDR_BIT stops the compiler reporting a shift overflow
581e13c1 3199 when it won't occur. */
66bf4b3a
AC
3200 /* NOTE: This assumes that the significant address information is
3201 kept in the least significant bits of ADDR - the upper bits were
76e71323 3202 either zero or sign extended. Should gdbarch_address_to_pointer or
66bf4b3a
AC
3203 some ADDRESS_TO_PRINTABLE() be used to do the conversion? */
3204
5af949e3 3205 int addr_bit = gdbarch_addr_bit (gdbarch);
66bf4b3a
AC
3206
3207 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
3208 addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
3209 return hex_string (addr);
3210}
3211
f1310107
TJB
3212/* This function is described in "defs.h". */
3213
3214const char *
3215print_core_address (struct gdbarch *gdbarch, CORE_ADDR address)
3216{
3217 int addr_bit = gdbarch_addr_bit (gdbarch);
3218
3219 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
3220 address &= ((CORE_ADDR) 1 << addr_bit) - 1;
3221
3222 /* FIXME: cagney/2002-05-03: Need local_address_string() function
3223 that returns the language localized string formatted to a width
3224 based on gdbarch_addr_bit. */
3225 if (addr_bit <= 32)
3226 return hex_string_custom (address, 8);
3227 else
3228 return hex_string_custom (address, 16);
3229}
3230
03dd37c3
AC
3231/* Convert a string back into a CORE_ADDR. */
3232CORE_ADDR
3233string_to_core_addr (const char *my_string)
3234{
3235 CORE_ADDR addr = 0;
9544c605 3236
51e2cfa2 3237 if (my_string[0] == '0' && TOLOWER (my_string[1]) == 'x')
03dd37c3 3238 {
ced572fe 3239 /* Assume that it is in hex. */
03dd37c3 3240 int i;
5d502164 3241
03dd37c3
AC
3242 for (i = 2; my_string[i] != '\0'; i++)
3243 {
51e2cfa2 3244 if (ISDIGIT (my_string[i]))
03dd37c3 3245 addr = (my_string[i] - '0') + (addr * 16);
51e2cfa2
PA
3246 else if (ISXDIGIT (my_string[i]))
3247 addr = (TOLOWER (my_string[i]) - 'a' + 0xa) + (addr * 16);
03dd37c3 3248 else
63f06803 3249 error (_("invalid hex \"%s\""), my_string);
03dd37c3
AC
3250 }
3251 }
3252 else
3253 {
3254 /* Assume that it is in decimal. */
3255 int i;
5d502164 3256
03dd37c3
AC
3257 for (i = 0; my_string[i] != '\0'; i++)
3258 {
51e2cfa2 3259 if (ISDIGIT (my_string[i]))
03dd37c3
AC
3260 addr = (my_string[i] - '0') + (addr * 10);
3261 else
63f06803 3262 error (_("invalid decimal \"%s\""), my_string);
03dd37c3
AC
3263 }
3264 }
9544c605 3265
03dd37c3
AC
3266 return addr;
3267}
58d370e0 3268
14278e1f
TT
3269#if GDB_SELF_TEST
3270
3271static void
3272gdb_realpath_check_trailer (const char *input, const char *trailer)
3273{
3274 gdb::unique_xmalloc_ptr<char> result = gdb_realpath (input);
3275
3276 size_t len = strlen (result.get ());
3277 size_t trail_len = strlen (trailer);
3278
3279 SELF_CHECK (len >= trail_len
3280 && strcmp (result.get () + len - trail_len, trailer) == 0);
3281}
3282
3283static void
3284gdb_realpath_tests ()
3285{
3286 /* A file which contains a directory prefix. */
3287 gdb_realpath_check_trailer ("./xfullpath.exp", "/xfullpath.exp");
3288 /* A file which contains a directory prefix. */
3289 gdb_realpath_check_trailer ("../../defs.h", "/defs.h");
3290 /* A one-character filename. */
3291 gdb_realpath_check_trailer ("./a", "/a");
3292 /* A file in the root directory. */
3293 gdb_realpath_check_trailer ("/root_file_which_should_exist",
3294 "/root_file_which_should_exist");
3295 /* A file which does not have a directory prefix. */
3296 gdb_realpath_check_trailer ("xfullpath.exp", "xfullpath.exp");
3297 /* A one-char filename without any directory prefix. */
3298 gdb_realpath_check_trailer ("a", "a");
3299 /* An empty filename. */
3300 gdb_realpath_check_trailer ("", "");
3301}
3302
d369b608
SM
3303/* Test the gdb_argv::as_array_view method. */
3304
3305static void
3306gdb_argv_as_array_view_test ()
3307{
3308 {
3309 gdb_argv argv;
3310
3311 gdb::array_view<char *> view = argv.as_array_view ();
3312
3313 SELF_CHECK (view.data () == nullptr);
3314 SELF_CHECK (view.size () == 0);
3315 }
3316 {
3317 gdb_argv argv ("une bonne 50");
3318
3319 gdb::array_view<char *> view = argv.as_array_view ();
3320
3321 SELF_CHECK (view.size () == 3);
3322 SELF_CHECK (strcmp (view[0], "une") == 0);
3323 SELF_CHECK (strcmp (view[1], "bonne") == 0);
3324 SELF_CHECK (strcmp (view[2], "50") == 0);
3325 }
3326}
3327
14278e1f
TT
3328#endif /* GDB_SELF_TEST */
3329
e1024ff1
DJ
3330/* Simple, portable version of dirname that does not modify its
3331 argument. */
3332
d721ba37 3333std::string
e1024ff1
DJ
3334ldirname (const char *filename)
3335{
d721ba37 3336 std::string dirname;
e1024ff1 3337 const char *base = lbasename (filename);
e1024ff1
DJ
3338
3339 while (base > filename && IS_DIR_SEPARATOR (base[-1]))
3340 --base;
3341
3342 if (base == filename)
d721ba37 3343 return dirname;
e1024ff1 3344
d721ba37 3345 dirname = std::string (filename, base - filename);
e1024ff1
DJ
3346
3347 /* On DOS based file systems, convert "d:foo" to "d:.", so that we
3348 create "d:./bar" later instead of the (different) "d:/bar". */
3349 if (base - filename == 2 && IS_ABSOLUTE_PATH (base)
3350 && !IS_DIR_SEPARATOR (filename[0]))
3351 dirname[base++ - filename] = '.';
3352
e1024ff1
DJ
3353 return dirname;
3354}
d1a41061 3355
74164c56
JK
3356/* Return ARGS parsed as a valid pid, or throw an error. */
3357
3358int
c0939df1 3359parse_pid_to_attach (const char *args)
74164c56
JK
3360{
3361 unsigned long pid;
3362 char *dummy;
3363
3364 if (!args)
3365 error_no_arg (_("process-id to attach"));
3366
c0939df1 3367 dummy = (char *) args;
74164c56
JK
3368 pid = strtoul (args, &dummy, 0);
3369 /* Some targets don't set errno on errors, grrr! */
3370 if ((pid == 0 && dummy == args) || dummy != &args[strlen (args)])
3371 error (_("Illegal process-id: %s."), args);
3372
3373 return pid;
3374}
3375
23e46b68
TT
3376/* Substitute all occurrences of string FROM by string TO in *STRINGP. *STRINGP
3377 must come from xrealloc-compatible allocator and it may be updated. FROM
3378 needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be
3379 located at the start or end of *STRINGP. */
3380
3381void
3382substitute_path_component (char **stringp, const char *from, const char *to)
3383{
3384 char *string = *stringp, *s;
3385 const size_t from_len = strlen (from);
3386 const size_t to_len = strlen (to);
3387
3388 for (s = string;;)
3389 {
3390 s = strstr (s, from);
3391 if (s == NULL)
3392 break;
3393
3394 if ((s == string || IS_DIR_SEPARATOR (s[-1])
3395 || s[-1] == DIRNAME_SEPARATOR)
3396 && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len])
3397 || s[from_len] == DIRNAME_SEPARATOR))
3398 {
3399 char *string_new;
3400
3401 string_new
3402 = (char *) xrealloc (string, (strlen (string) + to_len + 1));
3403
3404 /* Relocate the current S pointer. */
3405 s = s - string + string_new;
3406 string = string_new;
3407
3408 /* Replace from by to. */
3409 memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1);
3410 memcpy (s, to, to_len);
3411
3412 s += to_len;
3413 }
3414 else
3415 s++;
3416 }
3417
3418 *stringp = string;
3419}
3420
0b6cb71e
DE
3421#ifdef HAVE_WAITPID
3422
3423#ifdef SIGALRM
3424
3425/* SIGALRM handler for waitpid_with_timeout. */
3426
3427static void
3428sigalrm_handler (int signo)
3429{
3430 /* Nothing to do. */
3431}
3432
3433#endif
3434
3435/* Wrapper to wait for child PID to die with TIMEOUT.
3436 TIMEOUT is the time to stop waiting in seconds.
3437 If TIMEOUT is zero, pass WNOHANG to waitpid.
3438 Returns PID if it was successfully waited for, otherwise -1.
3439
3440 Timeouts are currently implemented with alarm and SIGALRM.
3441 If the host does not support them, this waits "forever".
3442 It would be odd though for a host to have waitpid and not SIGALRM. */
3443
3444pid_t
3445wait_to_die_with_timeout (pid_t pid, int *status, int timeout)
3446{
3447 pid_t waitpid_result;
3448
3449 gdb_assert (pid > 0);
3450 gdb_assert (timeout >= 0);
3451
3452 if (timeout > 0)
3453 {
3454#ifdef SIGALRM
3455#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
3456 struct sigaction sa, old_sa;
3457
3458 sa.sa_handler = sigalrm_handler;
3459 sigemptyset (&sa.sa_mask);
3460 sa.sa_flags = 0;
3461 sigaction (SIGALRM, &sa, &old_sa);
3462#else
a40805d4 3463 sighandler_t ofunc;
0b6cb71e 3464
a40805d4 3465 ofunc = signal (SIGALRM, sigalrm_handler);
0b6cb71e
DE
3466#endif
3467
3468 alarm (timeout);
3469#endif
3470
3471 waitpid_result = waitpid (pid, status, 0);
3472
3473#ifdef SIGALRM
3474 alarm (0);
3475#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
3476 sigaction (SIGALRM, &old_sa, NULL);
3477#else
3478 signal (SIGALRM, ofunc);
3479#endif
3480#endif
3481 }
3482 else
3483 waitpid_result = waitpid (pid, status, WNOHANG);
3484
3485 if (waitpid_result == pid)
3486 return pid;
3487 else
3488 return -1;
3489}
3490
3491#endif /* HAVE_WAITPID */
3492
202cbf1c
JK
3493/* Provide fnmatch compatible function for FNM_FILE_NAME matching of host files.
3494 Both FNM_FILE_NAME and FNM_NOESCAPE must be set in FLAGS.
3495
3496 It handles correctly HAVE_DOS_BASED_FILE_SYSTEM and
3497 HAVE_CASE_INSENSITIVE_FILE_SYSTEM. */
3498
3499int
3500gdb_filename_fnmatch (const char *pattern, const char *string, int flags)
3501{
3502 gdb_assert ((flags & FNM_FILE_NAME) != 0);
3503
3504 /* It is unclear how '\' escaping vs. directory separator should coexist. */
3505 gdb_assert ((flags & FNM_NOESCAPE) != 0);
3506
3507#ifdef HAVE_DOS_BASED_FILE_SYSTEM
3508 {
3509 char *pattern_slash, *string_slash;
3510
3511 /* Replace '\' by '/' in both strings. */
3512
0ae1c716 3513 pattern_slash = (char *) alloca (strlen (pattern) + 1);
202cbf1c
JK
3514 strcpy (pattern_slash, pattern);
3515 pattern = pattern_slash;
3516 for (; *pattern_slash != 0; pattern_slash++)
3517 if (IS_DIR_SEPARATOR (*pattern_slash))
3518 *pattern_slash = '/';
3519
0ae1c716 3520 string_slash = (char *) alloca (strlen (string) + 1);
202cbf1c
JK
3521 strcpy (string_slash, string);
3522 string = string_slash;
3523 for (; *string_slash != 0; string_slash++)
3524 if (IS_DIR_SEPARATOR (*string_slash))
3525 *string_slash = '/';
3526 }
3527#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
3528
3529#ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
3530 flags |= FNM_CASEFOLD;
3531#endif /* HAVE_CASE_INSENSITIVE_FILE_SYSTEM */
3532
3533 return fnmatch (pattern, string, flags);
3534}
3535
cce0e923
DE
3536/* Return the number of path elements in PATH.
3537 / = 1
3538 /foo = 2
3539 /foo/ = 2
3540 foo/bar = 2
3541 foo/ = 1 */
3542
3543int
3544count_path_elements (const char *path)
3545{
3546 int count = 0;
3547 const char *p = path;
3548
3549 if (HAS_DRIVE_SPEC (p))
3550 {
3551 p = STRIP_DRIVE_SPEC (p);
3552 ++count;
3553 }
3554
3555 while (*p != '\0')
3556 {
3557 if (IS_DIR_SEPARATOR (*p))
3558 ++count;
3559 ++p;
3560 }
3561
3562 /* Backup one if last character is /, unless it's the only one. */
3563 if (p > path + 1 && IS_DIR_SEPARATOR (p[-1]))
3564 --count;
3565
3566 /* Add one for the file name, if present. */
3567 if (p > path && !IS_DIR_SEPARATOR (p[-1]))
3568 ++count;
3569
3570 return count;
3571}
3572
3573/* Remove N leading path elements from PATH.
3574 N must be non-negative.
3575 If PATH has more than N path elements then return NULL.
3576 If PATH has exactly N path elements then return "".
3577 See count_path_elements for a description of how we do the counting. */
3578
3579const char *
3580strip_leading_path_elements (const char *path, int n)
3581{
3582 int i = 0;
3583 const char *p = path;
3584
3585 gdb_assert (n >= 0);
3586
3587 if (n == 0)
3588 return p;
3589
3590 if (HAS_DRIVE_SPEC (p))
3591 {
3592 p = STRIP_DRIVE_SPEC (p);
3593 ++i;
3594 }
3595
3596 while (i < n)
3597 {
3598 while (*p != '\0' && !IS_DIR_SEPARATOR (*p))
3599 ++p;
3600 if (*p == '\0')
3601 {
3602 if (i + 1 == n)
3603 return "";
3604 return NULL;
3605 }
3606 ++p;
3607 ++i;
3608 }
3609
3610 return p;
3611}
3612
a99bc3d2
JB
3613/* See utils.h. */
3614
3615void
3616copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
3617 const gdb_byte *source, ULONGEST source_offset,
3618 ULONGEST nbits, int bits_big_endian)
3619{
3620 unsigned int buf, avail;
3621
3622 if (nbits == 0)
3623 return;
3624
3625 if (bits_big_endian)
3626 {
3627 /* Start from the end, then work backwards. */
3628 dest_offset += nbits - 1;
3629 dest += dest_offset / 8;
3630 dest_offset = 7 - dest_offset % 8;
3631 source_offset += nbits - 1;
3632 source += source_offset / 8;
3633 source_offset = 7 - source_offset % 8;
3634 }
3635 else
3636 {
3637 dest += dest_offset / 8;
3638 dest_offset %= 8;
3639 source += source_offset / 8;
3640 source_offset %= 8;
3641 }
3642
3643 /* Fill BUF with DEST_OFFSET bits from the destination and 8 -
3644 SOURCE_OFFSET bits from the source. */
3645 buf = *(bits_big_endian ? source-- : source++) >> source_offset;
3646 buf <<= dest_offset;
3647 buf |= *dest & ((1 << dest_offset) - 1);
3648
3649 /* NBITS: bits yet to be written; AVAIL: BUF's fill level. */
3650 nbits += dest_offset;
3651 avail = dest_offset + 8 - source_offset;
3652
3653 /* Flush 8 bits from BUF, if appropriate. */
3654 if (nbits >= 8 && avail >= 8)
3655 {
3656 *(bits_big_endian ? dest-- : dest++) = buf;
3657 buf >>= 8;
3658 avail -= 8;
3659 nbits -= 8;
3660 }
3661
3662 /* Copy the middle part. */
3663 if (nbits >= 8)
3664 {
3665 size_t len = nbits / 8;
3666
3667 /* Use a faster method for byte-aligned copies. */
3668 if (avail == 0)
3669 {
3670 if (bits_big_endian)
3671 {
3672 dest -= len;
3673 source -= len;
3674 memcpy (dest + 1, source + 1, len);
3675 }
3676 else
3677 {
3678 memcpy (dest, source, len);
3679 dest += len;
3680 source += len;
3681 }
3682 }
3683 else
3684 {
3685 while (len--)
3686 {
3687 buf |= *(bits_big_endian ? source-- : source++) << avail;
3688 *(bits_big_endian ? dest-- : dest++) = buf;
3689 buf >>= 8;
3690 }
3691 }
3692 nbits %= 8;
3693 }
3694
3695 /* Write the last byte. */
3696 if (nbits)
3697 {
3698 if (avail < nbits)
3699 buf |= *source << avail;
3700
3701 buf &= (1 << nbits) - 1;
cf83625d 3702 *dest = (*dest & (~0U << nbits)) | buf;
a99bc3d2
JB
3703 }
3704}
3705
2e12e798
TV
3706#if GDB_SELF_TEST
3707static void
3708test_assign_set_return_if_changed ()
3709{
3710 bool changed;
3711 int a;
3712
3713 for (bool initial : { false, true })
3714 {
3715 changed = initial;
3716 a = 1;
3717 assign_set_if_changed (a, 1, changed);
3718 SELF_CHECK (a == 1);
3719 SELF_CHECK (changed == initial);
3720 }
3721
3722 for (bool initial : { false, true })
3723 {
3724 changed = initial;
3725 a = 1;
3726 assign_set_if_changed (a, 2, changed);
3727 SELF_CHECK (a == 2);
3728 SELF_CHECK (changed == true);
3729 }
3730
3731 a = 1;
3732 changed = assign_return_if_changed (a, 1);
3733 SELF_CHECK (a == 1);
3734 SELF_CHECK (changed == false);
3735
3736 a = 1;
3737 assign_set_if_changed (a, 2, changed);
3738 SELF_CHECK (a == 2);
3739 SELF_CHECK (changed == true);
3740}
3741#endif
3742
6c265988 3743void _initialize_utils ();
3c16cced 3744void
6c265988 3745_initialize_utils ()
3c16cced 3746{
12904d37
TT
3747 add_setshow_uinteger_cmd ("width", class_support, &chars_per_line, _("\
3748Set number of characters where GDB should wrap lines of its output."), _("\
3749Show number of characters where GDB should wrap lines of its output."), _("\
3750This affects where GDB wraps its output to fit the screen width.\n\
3751Setting this to \"unlimited\" or zero prevents GDB from wrapping its output."),
3752 set_width_command,
3753 show_chars_per_line,
3754 &setlist, &showlist);
3755
3756 add_setshow_uinteger_cmd ("height", class_support, &lines_per_page, _("\
3757Set number of lines in a page for GDB output pagination."), _("\
3758Show number of lines in a page for GDB output pagination."), _("\
3759This affects the number of lines after which GDB will pause\n\
3760its output and ask you whether to continue.\n\
3761Setting this to \"unlimited\" or zero causes GDB never pause during output."),
3762 set_height_command,
3763 show_lines_per_page,
3764 &setlist, &showlist);
3765
3766 add_setshow_boolean_cmd ("pagination", class_support,
3767 &pagination_enabled, _("\
3768Set state of GDB output pagination."), _("\
3769Show state of GDB output pagination."), _("\
3770When pagination is ON, GDB pauses at end of each screenful of\n\
3771its output and asks you whether to continue.\n\
3772Turning pagination off is an alternative to \"set height unlimited\"."),
3773 NULL,
3774 show_pagination_enabled,
3775 &setlist, &showlist);
3776
3777 add_setshow_boolean_cmd ("sevenbit-strings", class_support,
3778 &sevenbit_strings, _("\
3779Set printing of 8-bit characters in strings as \\nnn."), _("\
3780Show printing of 8-bit characters in strings as \\nnn."), NULL,
3781 NULL,
3782 show_sevenbit_strings,
3783 &setprintlist, &showprintlist);
3784
3785 add_setshow_boolean_cmd ("timestamp", class_maintenance,
3786 &debug_timestamp, _("\
3787Set timestamping of debugging messages."), _("\
3788Show timestamping of debugging messages."), _("\
3789When set, debugging messages will be marked with seconds and microseconds."),
3790 NULL,
3791 show_debug_timestamp,
3792 &setdebuglist, &showdebuglist);
3793
3c16cced
PA
3794 add_internal_problem_command (&internal_error_problem);
3795 add_internal_problem_command (&internal_warning_problem);
57fcfb1b 3796 add_internal_problem_command (&demangler_warning_problem);
14278e1f 3797
f1531d04
TV
3798 add_cmd ("screen", class_maintenance, &maintenance_info_screen,
3799 _("Show screen characteristics."), &maintenanceinfolist);
3800
14278e1f 3801#if GDB_SELF_TEST
1526853e 3802 selftests::register_test ("gdb_realpath", gdb_realpath_tests);
d369b608 3803 selftests::register_test ("gdb_argv_array_view", gdb_argv_as_array_view_test);
b05752c2
KS
3804 selftests::register_test ("strncmp_iw_with_mode",
3805 strncmp_iw_with_mode_tests);
1f0f8b5d 3806 selftests::register_test ("pager", test_pager);
2e12e798
TV
3807 selftests::register_test ("assign_set_return_if_changed",
3808 test_assign_set_return_if_changed);
14278e1f 3809#endif
3c16cced 3810}