1 /* *INDENT-OFF* */ /* ATTRIBUTE_PRINTF confuses indent, avoid running it
3 /* I/O, string, cleanup, and other random utilities for GDB.
4 Copyright (C) 1986-2022 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "exceptions.h"
25 #include "gdbsupport/array-view.h"
26 #include "gdbsupport/scoped_restore.h"
33 struct completion_match_for_lcd
;
36 /* String utilities. */
38 extern bool sevenbit_strings
;
40 /* Modes of operation for strncmp_iw_with_mode. */
42 enum class strncmp_iw_mode
44 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
45 differences in whitespace. Returns 0 if they match, non-zero if
46 they don't (slightly different than strcmp()'s range of return
50 /* Like NORMAL, but also apply the strcmp_iw hack. I.e.,
51 string1=="FOO(PARAMS)" matches string2=="FOO". */
55 /* Helper for strcmp_iw and strncmp_iw. Exported so that languages
56 can implement both NORMAL and MATCH_PARAMS variants in a single
57 function and defer part of the work to strncmp_iw_with_mode.
59 LANGUAGE is used to implement some context-sensitive
60 language-specific comparisons. For example, for C++,
61 "string1=operator()" should not match "string2=operator" even in
64 MATCH_FOR_LCD is passed down so that the function can mark parts of
65 the symbol name as ignored for completion matching purposes (e.g.,
66 to handle abi tags). */
67 extern int strncmp_iw_with_mode
68 (const char *string1
, const char *string2
, size_t string2_len
,
69 strncmp_iw_mode mode
, enum language language
,
70 completion_match_for_lcd
*match_for_lcd
= NULL
);
72 /* Do a strncmp() type operation on STRING1 and STRING2, ignoring any
73 differences in whitespace. STRING2_LEN is STRING2's length.
74 Returns 0 if STRING1 matches STRING2_LEN characters of STRING2,
75 non-zero otherwise (slightly different than strncmp()'s range of
76 return values). Note: passes language_minimal to
77 strncmp_iw_with_mode, and should therefore be avoided if a more
78 suitable language is available. */
79 extern int strncmp_iw (const char *string1
, const char *string2
,
82 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
83 differences in whitespace. Returns 0 if they match, non-zero if
84 they don't (slightly different than strcmp()'s range of return
87 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
88 This "feature" is useful when searching for matching C++ function
89 names (such as if the user types 'break FOO', where FOO is a
90 mangled C++ function).
92 Note: passes language_minimal to strncmp_iw_with_mode, and should
93 therefore be avoided if a more suitable language is available. */
94 extern int strcmp_iw (const char *string1
, const char *string2
);
96 extern int strcmp_iw_ordered (const char *, const char *);
98 /* Return true if the strings are equal. */
100 extern bool streq (const char *, const char *);
102 extern int subset_compare (const char *, const char *);
104 /* Compare C strings for std::sort. */
107 compare_cstrings (const char *str1
, const char *str2
)
109 return strcmp (str1
, str2
) < 0;
112 /* Reset the prompt_for_continue clock. */
113 void reset_prompt_for_continue_wait_time (void);
114 /* Return the time spent in prompt_for_continue. */
115 std::chrono::steady_clock::duration
get_prompt_for_continue_wait_time ();
117 /* Parsing utilities. */
119 extern int parse_pid_to_attach (const char *args
);
121 extern int parse_escape (struct gdbarch
*, const char **);
123 /* A wrapper for an array of char* that was allocated in the way that
124 'buildargv' does, and should be freed with 'freeargv'. */
130 /* A constructor that initializes to NULL. */
137 /* A constructor that calls buildargv on STR. STR may be NULL, in
138 which case this object is initialized with a NULL array. */
140 explicit gdb_argv (const char *str
)
146 /* A constructor that takes ownership of an existing array. */
148 explicit gdb_argv (char **array
)
153 gdb_argv (const gdb_argv
&) = delete;
154 gdb_argv
&operator= (const gdb_argv
&) = delete;
156 gdb_argv
&operator= (gdb_argv
&&other
)
159 m_argv
= other
.m_argv
;
160 other
.m_argv
= nullptr;
164 gdb_argv (gdb_argv
&&other
)
166 m_argv
= other
.m_argv
;
167 other
.m_argv
= nullptr;
175 /* Call buildargv on STR, storing the result in this object. Any
176 previous state is freed. STR may be NULL, in which case this
177 object is reset with a NULL array. If buildargv fails due to
178 out-of-memory, call malloc_failure. Therefore, the value is
179 guaranteed to be non-NULL, unless the parameter itself is
182 void reset (const char *str
);
184 /* Return the underlying array. */
191 const char * const * get () const
196 /* Return the underlying array, transferring ownership to the
199 ATTRIBUTE_UNUSED_RESULT
char **release ()
201 char **result
= m_argv
;
206 /* Return the number of items in the array. */
210 return countargv (m_argv
);
213 /* Index into the array. */
215 char *operator[] (int arg
)
217 gdb_assert (m_argv
!= NULL
);
221 /* Return the arguments array as an array view. */
223 gdb::array_view
<char *> as_array_view ()
225 return gdb::array_view
<char *> (this->get (), this->count ());
228 gdb::array_view
<const char * const> as_array_view () const
230 return gdb::array_view
<const char * const> (this->get (), this->count ());
233 /* Append arguments to this array. */
234 void append (gdb_argv
&&other
)
237 int argc
= other
.count ();
238 m_argv
= XRESIZEVEC (char *, m_argv
, (size
+ argc
+ 1));
240 for (int argi
= 0; argi
< argc
; argi
++)
242 /* Transfer ownership of the string. */
243 m_argv
[size
++] = other
.m_argv
[argi
];
244 /* Ensure that destruction of OTHER works correctly. */
245 other
.m_argv
[argi
] = nullptr;
247 m_argv
[size
] = nullptr;
250 /* Append arguments to this array. */
251 void append (const gdb_argv
&other
)
254 int argc
= other
.count ();
255 m_argv
= XRESIZEVEC (char *, m_argv
, (size
+ argc
+ 1));
257 for (int argi
= 0; argi
< argc
; argi
++)
258 m_argv
[size
++] = xstrdup (other
.m_argv
[argi
]);
259 m_argv
[size
] = nullptr;
262 /* The iterator type. */
264 typedef char **iterator
;
266 /* Return an iterator pointing to the start of the array. */
273 /* Return an iterator pointing to the end of the array. */
277 return m_argv
+ count ();
280 bool operator!= (std::nullptr_t
)
282 return m_argv
!= NULL
;
285 bool operator== (std::nullptr_t
)
287 return m_argv
== NULL
;
292 /* The wrapped array. */
298 /* Cleanup utilities. */
300 /* A deleter for a hash table. */
303 void operator() (htab
*ptr
) const
309 /* A unique_ptr wrapper for htab_t. */
310 typedef std::unique_ptr
<htab
, htab_deleter
> htab_up
;
312 /* A wrapper for 'delete' that can used as a hash table entry deletion
316 htab_delete_entry (void *ptr
)
321 extern void init_page_info (void);
323 /* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
324 Restore when destroyed. */
326 struct set_batch_flag_and_restore_page_info
330 set_batch_flag_and_restore_page_info ();
331 ~set_batch_flag_and_restore_page_info ();
333 DISABLE_COPY_AND_ASSIGN (set_batch_flag_and_restore_page_info
);
337 /* Note that this doesn't use scoped_restore, because it's important
338 to control the ordering of operations in the destruction, and it
339 was simpler to avoid introducing a new ad hoc class. */
340 unsigned m_save_lines_per_page
;
341 unsigned m_save_chars_per_line
;
342 int m_save_batch_flag
;
346 /* Path utilities. */
348 extern int gdb_filename_fnmatch (const char *pattern
, const char *string
,
351 extern void substitute_path_component (char **stringp
, const char *from
,
354 std::string
ldirname (const char *filename
);
356 extern int count_path_elements (const char *path
);
358 extern const char *strip_leading_path_elements (const char *path
, int n
);
360 /* GDB output, ui_file utilities. */
364 extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
365 extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
366 extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
368 extern void begin_line (void);
370 extern void wrap_here (const char *);
372 extern void reinitialize_more_filter (void);
374 /* Return the number of characters in a line. */
376 extern int get_chars_per_line ();
378 extern bool pagination_enabled
;
380 extern struct ui_file
**current_ui_gdb_stdout_ptr (void);
381 extern struct ui_file
**current_ui_gdb_stdin_ptr (void);
382 extern struct ui_file
**current_ui_gdb_stderr_ptr (void);
383 extern struct ui_file
**current_ui_gdb_stdlog_ptr (void);
385 /* Flush STREAM. This is a wrapper for ui_file_flush that also
386 flushes any output pending from uses of the *_filtered output
387 functions; that output is kept in a special buffer so that
388 pagination and styling are handled properly. */
389 extern void gdb_flush (struct ui_file
*);
391 /* The current top level's ui_file streams. */
394 #define gdb_stdout (*current_ui_gdb_stdout_ptr ())
396 #define gdb_stdin (*current_ui_gdb_stdin_ptr ())
397 /* Serious error notifications */
398 #define gdb_stderr (*current_ui_gdb_stderr_ptr ())
399 /* Log/debug/trace messages that should bypass normal stdout/stderr
400 filtering. For moment, always call this stream using
401 *_unfiltered. In the very near future that restriction shall be
402 removed - either call shall be unfiltered. (cagney 1999-06-13). */
403 #define gdb_stdlog (*current_ui_gdb_stdlog_ptr ())
405 /* Truly global ui_file streams. These are all defined in main.c. */
407 /* Target output that should bypass normal stdout/stderr filtering.
408 For moment, always call this stream using *_unfiltered. In the
409 very near future that restriction shall be removed - either call
410 shall be unfiltered. (cagney 1999-07-02). */
411 extern struct ui_file
*gdb_stdtarg
;
412 extern struct ui_file
*gdb_stdtargerr
;
413 extern struct ui_file
*gdb_stdtargin
;
415 /* Set the screen dimensions to WIDTH and HEIGHT. */
417 extern void set_screen_width_and_height (int width
, int height
);
419 /* More generic printf like operations. Filtered versions may return
420 non-locally on error. As an extension over plain printf, these
421 support some GDB-specific format specifiers. Particularly useful
422 here are the styling formatters: '%p[', '%p]' and '%ps'. See
423 ui_out::message for details. */
425 extern void fputs_filtered (const char *, struct ui_file
*);
427 extern void fputs_unfiltered (const char *, struct ui_file
*);
429 extern int fputc_filtered (int c
, struct ui_file
*);
431 extern int fputc_unfiltered (int c
, struct ui_file
*);
433 extern int putchar_filtered (int c
);
435 extern int putchar_unfiltered (int c
);
437 extern void puts_filtered (const char *);
439 extern void puts_unfiltered (const char *);
441 extern void puts_filtered_tabular (char *string
, int width
, int right
);
443 extern void vprintf_filtered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
445 extern void vfprintf_filtered (struct ui_file
*, const char *, va_list)
446 ATTRIBUTE_PRINTF (2, 0);
448 extern void fprintf_filtered (struct ui_file
*, const char *, ...)
449 ATTRIBUTE_PRINTF (2, 3);
451 extern void printf_filtered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
453 extern void vprintf_unfiltered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
455 extern void vfprintf_unfiltered (struct ui_file
*, const char *, va_list)
456 ATTRIBUTE_PRINTF (2, 0);
458 extern void fprintf_unfiltered (struct ui_file
*, const char *, ...)
459 ATTRIBUTE_PRINTF (2, 3);
461 extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
463 extern void print_spaces_filtered (int, struct ui_file
*);
465 extern const char *n_spaces (int);
467 /* Return nonzero if filtered printing is initialized. */
468 extern int filtered_printing_initialized (void);
470 /* Like fprintf_filtered, but styles the output according to STYLE,
473 extern void fprintf_styled (struct ui_file
*stream
,
474 const ui_file_style
&style
,
477 ATTRIBUTE_PRINTF (3, 4);
479 extern void vfprintf_styled (struct ui_file
*stream
,
480 const ui_file_style
&style
,
483 ATTRIBUTE_PRINTF (3, 0);
485 /* Like vfprintf_styled, but do not process gdb-specific format
487 extern void vfprintf_styled_no_gdbfmt (struct ui_file
*stream
,
488 const ui_file_style
&style
,
490 const char *fmt
, va_list args
)
491 ATTRIBUTE_PRINTF (4, 0);
493 /* Like fputs_filtered, but styles the output according to STYLE, when
496 extern void fputs_styled (const char *linebuffer
,
497 const ui_file_style
&style
,
498 struct ui_file
*stream
);
500 /* Unfiltered variant of fputs_styled. */
502 extern void fputs_styled_unfiltered (const char *linebuffer
,
503 const ui_file_style
&style
,
504 struct ui_file
*stream
);
506 /* Like fputs_styled, but uses highlight_style to highlight the
507 parts of STR that match HIGHLIGHT. */
509 extern void fputs_highlighted (const char *str
, const compiled_regex
&highlight
,
510 struct ui_file
*stream
);
512 /* Reset the terminal style to the default, if needed. */
514 extern void reset_terminal_style (struct ui_file
*stream
);
516 /* Return the address only having significant bits. */
517 extern CORE_ADDR
address_significant (gdbarch
*gdbarch
, CORE_ADDR addr
);
519 /* Convert CORE_ADDR to string in platform-specific manner.
520 This is usually formatted similar to 0x%lx. */
521 extern const char *paddress (struct gdbarch
*gdbarch
, CORE_ADDR addr
);
523 /* Return a string representation in hexadecimal notation of ADDRESS,
524 which is suitable for printing. */
526 extern const char *print_core_address (struct gdbarch
*gdbarch
,
529 extern CORE_ADDR
string_to_core_addr (const char *my_string
);
531 extern void fprintf_symbol_filtered (struct ui_file
*, const char *,
534 extern void throw_perror_with_name (enum errors errcode
, const char *string
)
537 extern void perror_warning_with_name (const char *string
);
539 extern void print_sys_errmsg (const char *, int);
541 /* Warnings and error messages. */
543 extern void (*deprecated_error_begin_hook
) (void);
545 /* Message to be printed before the warning message, when a warning occurs. */
547 extern const char *warning_pre_print
;
549 extern void error_stream (const string_file
&) ATTRIBUTE_NORETURN
;
551 extern void demangler_vwarning (const char *file
, int line
,
552 const char *, va_list ap
)
553 ATTRIBUTE_PRINTF (3, 0);
555 extern void demangler_warning (const char *file
, int line
,
556 const char *, ...) ATTRIBUTE_PRINTF (3, 4);
559 /* Misc. utilities. */
561 /* Allocation and deallocation functions for the libiberty hash table
562 which use obstacks. */
563 void *hashtab_obstack_allocate (void *data
, size_t size
, size_t count
);
564 void dummy_obstack_deallocate (void *object
, void *data
);
567 extern pid_t
wait_to_die_with_timeout (pid_t pid
, int *status
, int timeout
);
570 extern int myread (int, char *, int);
572 /* Integer exponentiation: Return V1**V2, where both arguments
575 Requires V1 != 0 if V2 < 0.
576 Returns 1 for 0 ** 0. */
577 extern ULONGEST
uinteger_pow (ULONGEST v1
, LONGEST v2
);
579 /* Resource limits used by getrlimit and setrlimit. */
581 enum resource_limit_kind
587 /* Check whether GDB will be able to dump core using the dump_core
588 function. Returns zero if GDB cannot or should not dump core.
589 If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
590 If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
592 extern int can_dump_core (enum resource_limit_kind limit_kind
);
594 /* Print a warning that we cannot dump core. */
596 extern void warn_cant_dump_core (const char *reason
);
598 /* Dump core trying to increase the core soft limit to hard limit
601 extern void dump_core (void);
603 /* Copy NBITS bits from SOURCE to DEST starting at the given bit
604 offsets. Use the bit order as specified by BITS_BIG_ENDIAN.
605 Source and destination buffers must not overlap. */
607 extern void copy_bitwise (gdb_byte
*dest
, ULONGEST dest_offset
,
608 const gdb_byte
*source
, ULONGEST source_offset
,
609 ULONGEST nbits
, int bits_big_endian
);
611 /* A fast hashing function. This can be used to hash data in a fast way
612 when the length is known. If no fast hashing library is available, falls
613 back to iterative_hash from libiberty. START_VALUE can be set to
614 continue hashing from a previous value. */
616 static inline unsigned int
617 fast_hash (const void *ptr
, size_t len
, unsigned int start_value
= 0)
619 #ifdef HAVE_LIBXXHASH
620 return XXH64 (ptr
, len
, start_value
);
622 return iterative_hash (ptr
, len
, start_value
);