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