gas: Update commit 4780e5e4933
[binutils-gdb.git] / gdb / utils.h
1 /* *INDENT-OFF* */ /* ATTRIBUTE_PRINTF confuses indent, avoid running it
2 for now. */
3 /* I/O, string, cleanup, and other random utilities for GDB.
4 Copyright (C) 1986-2021 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 #ifndef UTILS_H
22 #define UTILS_H
23
24 #include "exceptions.h"
25 #include "gdbsupport/array-view.h"
26 #include "gdbsupport/scoped_restore.h"
27 #include <chrono>
28
29 #ifdef HAVE_LIBXXHASH
30 #include <xxhash.h>
31 #endif
32
33 struct completion_match_for_lcd;
34 class compiled_regex;
35
36 /* String utilities. */
37
38 extern bool sevenbit_strings;
39
40 /* Modes of operation for strncmp_iw_with_mode. */
41
42 enum class strncmp_iw_mode
43 {
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
47 values). */
48 NORMAL,
49
50 /* Like NORMAL, but also apply the strcmp_iw hack. I.e.,
51 string1=="FOO(PARAMS)" matches string2=="FOO". */
52 MATCH_PARAMS,
53 };
54
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.
58
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
62 MATCH_PARAMS mode.
63
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);
71
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,
80 size_t string2_len);
81
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
85 values).
86
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).
91
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);
95
96 extern int strcmp_iw_ordered (const char *, const char *);
97
98 /* Return true if the strings are equal. */
99
100 extern bool streq (const char *, const char *);
101
102 extern int subset_compare (const char *, const char *);
103
104 /* Compare C strings for std::sort. */
105
106 static inline bool
107 compare_cstrings (const char *str1, const char *str2)
108 {
109 return strcmp (str1, str2) < 0;
110 }
111
112 /* A wrapper for bfd_errmsg to produce a more helpful error message
113 in the case of bfd_error_file_ambiguously recognized.
114 MATCHING, if non-NULL, is the corresponding argument to
115 bfd_check_format_matches, and will be freed. */
116
117 extern std::string gdb_bfd_errmsg (bfd_error_type error_tag, char **matching);
118
119 /* Reset the prompt_for_continue clock. */
120 void reset_prompt_for_continue_wait_time (void);
121 /* Return the time spent in prompt_for_continue. */
122 std::chrono::steady_clock::duration get_prompt_for_continue_wait_time ();
123 \f
124 /* Parsing utilities. */
125
126 extern int parse_pid_to_attach (const char *args);
127
128 extern int parse_escape (struct gdbarch *, const char **);
129
130 /* A wrapper for an array of char* that was allocated in the way that
131 'buildargv' does, and should be freed with 'freeargv'. */
132
133 class gdb_argv
134 {
135 public:
136
137 /* A constructor that initializes to NULL. */
138
139 gdb_argv ()
140 : m_argv (NULL)
141 {
142 }
143
144 /* A constructor that calls buildargv on STR. STR may be NULL, in
145 which case this object is initialized with a NULL array. */
146
147 explicit gdb_argv (const char *str)
148 : m_argv (NULL)
149 {
150 reset (str);
151 }
152
153 /* A constructor that takes ownership of an existing array. */
154
155 explicit gdb_argv (char **array)
156 : m_argv (array)
157 {
158 }
159
160 gdb_argv (const gdb_argv &) = delete;
161 gdb_argv &operator= (const gdb_argv &) = delete;
162
163 gdb_argv &operator= (gdb_argv &&other)
164 {
165 freeargv (m_argv);
166 m_argv = other.m_argv;
167 other.m_argv = nullptr;
168 return *this;
169 }
170
171 gdb_argv (gdb_argv &&other)
172 {
173 m_argv = other.m_argv;
174 other.m_argv = nullptr;
175 }
176
177 ~gdb_argv ()
178 {
179 freeargv (m_argv);
180 }
181
182 /* Call buildargv on STR, storing the result in this object. Any
183 previous state is freed. STR may be NULL, in which case this
184 object is reset with a NULL array. If buildargv fails due to
185 out-of-memory, call malloc_failure. Therefore, the value is
186 guaranteed to be non-NULL, unless the parameter itself is
187 NULL. */
188
189 void reset (const char *str);
190
191 /* Return the underlying array. */
192
193 char **get ()
194 {
195 return m_argv;
196 }
197
198 const char * const * get () const
199 {
200 return m_argv;
201 }
202
203 /* Return the underlying array, transferring ownership to the
204 caller. */
205
206 ATTRIBUTE_UNUSED_RESULT char **release ()
207 {
208 char **result = m_argv;
209 m_argv = NULL;
210 return result;
211 }
212
213 /* Return the number of items in the array. */
214
215 int count () const
216 {
217 return countargv (m_argv);
218 }
219
220 /* Index into the array. */
221
222 char *operator[] (int arg)
223 {
224 gdb_assert (m_argv != NULL);
225 return m_argv[arg];
226 }
227
228 /* Return the arguments array as an array view. */
229
230 gdb::array_view<char *> as_array_view ()
231 {
232 return gdb::array_view<char *> (this->get (), this->count ());
233 }
234
235 gdb::array_view<const char * const> as_array_view () const
236 {
237 return gdb::array_view<const char * const> (this->get (), this->count ());
238 }
239
240 /* Append arguments to this array. */
241 void append (gdb_argv &&other)
242 {
243 int size = count ();
244 int argc = other.count ();
245 m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
246
247 for (int argi = 0; argi < argc; argi++)
248 {
249 /* Transfer ownership of the string. */
250 m_argv[size++] = other.m_argv[argi];
251 /* Ensure that destruction of OTHER works correctly. */
252 other.m_argv[argi] = nullptr;
253 }
254 m_argv[size] = nullptr;
255 }
256
257 /* Append arguments to this array. */
258 void append (const gdb_argv &other)
259 {
260 int size = count ();
261 int argc = other.count ();
262 m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
263
264 for (int argi = 0; argi < argc; argi++)
265 m_argv[size++] = xstrdup (other.m_argv[argi]);
266 m_argv[size] = nullptr;
267 }
268
269 /* The iterator type. */
270
271 typedef char **iterator;
272
273 /* Return an iterator pointing to the start of the array. */
274
275 iterator begin ()
276 {
277 return m_argv;
278 }
279
280 /* Return an iterator pointing to the end of the array. */
281
282 iterator end ()
283 {
284 return m_argv + count ();
285 }
286
287 bool operator!= (std::nullptr_t)
288 {
289 return m_argv != NULL;
290 }
291
292 bool operator== (std::nullptr_t)
293 {
294 return m_argv == NULL;
295 }
296
297 private:
298
299 /* The wrapped array. */
300
301 char **m_argv;
302 };
303
304 \f
305 /* Cleanup utilities. */
306
307 /* A deleter for a hash table. */
308 struct htab_deleter
309 {
310 void operator() (htab *ptr) const
311 {
312 htab_delete (ptr);
313 }
314 };
315
316 /* A unique_ptr wrapper for htab_t. */
317 typedef std::unique_ptr<htab, htab_deleter> htab_up;
318
319 /* A wrapper for 'delete' that can used as a hash table entry deletion
320 function. */
321 template<typename T>
322 void
323 htab_delete_entry (void *ptr)
324 {
325 delete (T *) ptr;
326 }
327
328 extern void init_page_info (void);
329
330 /* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
331 Restore when destroyed. */
332
333 struct set_batch_flag_and_restore_page_info
334 {
335 public:
336
337 set_batch_flag_and_restore_page_info ();
338 ~set_batch_flag_and_restore_page_info ();
339
340 DISABLE_COPY_AND_ASSIGN (set_batch_flag_and_restore_page_info);
341
342 private:
343
344 /* Note that this doesn't use scoped_restore, because it's important
345 to control the ordering of operations in the destruction, and it
346 was simpler to avoid introducing a new ad hoc class. */
347 unsigned m_save_lines_per_page;
348 unsigned m_save_chars_per_line;
349 int m_save_batch_flag;
350 };
351
352 \f
353 /* Path utilities. */
354
355 extern int gdb_filename_fnmatch (const char *pattern, const char *string,
356 int flags);
357
358 extern void substitute_path_component (char **stringp, const char *from,
359 const char *to);
360
361 std::string ldirname (const char *filename);
362
363 extern int count_path_elements (const char *path);
364
365 extern const char *strip_leading_path_elements (const char *path, int n);
366 \f
367 /* GDB output, ui_file utilities. */
368
369 struct ui_file;
370
371 extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
372 extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
373 extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
374
375 extern void begin_line (void);
376
377 extern void wrap_here (const char *);
378
379 extern void reinitialize_more_filter (void);
380
381 /* Return the number of characters in a line. */
382
383 extern int get_chars_per_line ();
384
385 extern bool pagination_enabled;
386
387 extern struct ui_file **current_ui_gdb_stdout_ptr (void);
388 extern struct ui_file **current_ui_gdb_stdin_ptr (void);
389 extern struct ui_file **current_ui_gdb_stderr_ptr (void);
390 extern struct ui_file **current_ui_gdb_stdlog_ptr (void);
391
392 /* Flush STREAM. This is a wrapper for ui_file_flush that also
393 flushes any output pending from uses of the *_filtered output
394 functions; that output is kept in a special buffer so that
395 pagination and styling are handled properly. */
396 extern void gdb_flush (struct ui_file *);
397
398 /* The current top level's ui_file streams. */
399
400 /* Normal results */
401 #define gdb_stdout (*current_ui_gdb_stdout_ptr ())
402 /* Input stream */
403 #define gdb_stdin (*current_ui_gdb_stdin_ptr ())
404 /* Serious error notifications */
405 #define gdb_stderr (*current_ui_gdb_stderr_ptr ())
406 /* Log/debug/trace messages that should bypass normal stdout/stderr
407 filtering. For moment, always call this stream using
408 *_unfiltered. In the very near future that restriction shall be
409 removed - either call shall be unfiltered. (cagney 1999-06-13). */
410 #define gdb_stdlog (*current_ui_gdb_stdlog_ptr ())
411
412 /* Truly global ui_file streams. These are all defined in main.c. */
413
414 /* Target output that should bypass normal stdout/stderr filtering.
415 For moment, always call this stream using *_unfiltered. In the
416 very near future that restriction shall be removed - either call
417 shall be unfiltered. (cagney 1999-07-02). */
418 extern struct ui_file *gdb_stdtarg;
419 extern struct ui_file *gdb_stdtargerr;
420 extern struct ui_file *gdb_stdtargin;
421
422 /* Set the screen dimensions to WIDTH and HEIGHT. */
423
424 extern void set_screen_width_and_height (int width, int height);
425
426 /* More generic printf like operations. Filtered versions may return
427 non-locally on error. As an extension over plain printf, these
428 support some GDB-specific format specifiers. Particularly useful
429 here are the styling formatters: '%p[', '%p]' and '%ps'. See
430 ui_out::message for details. */
431
432 extern void fputs_filtered (const char *, struct ui_file *);
433
434 extern void fputs_unfiltered (const char *, struct ui_file *);
435
436 extern int fputc_filtered (int c, struct ui_file *);
437
438 extern int fputc_unfiltered (int c, struct ui_file *);
439
440 extern int putchar_filtered (int c);
441
442 extern int putchar_unfiltered (int c);
443
444 extern void puts_filtered (const char *);
445
446 extern void puts_unfiltered (const char *);
447
448 extern void puts_filtered_tabular (char *string, int width, int right);
449
450 extern void puts_debug (char *prefix, char *string, char *suffix);
451
452 extern void vprintf_filtered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
453
454 extern void vfprintf_filtered (struct ui_file *, const char *, va_list)
455 ATTRIBUTE_PRINTF (2, 0);
456
457 extern void fprintf_filtered (struct ui_file *, const char *, ...)
458 ATTRIBUTE_PRINTF (2, 3);
459
460 extern void printf_filtered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
461
462 extern void vprintf_unfiltered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
463
464 extern void vfprintf_unfiltered (struct ui_file *, const char *, va_list)
465 ATTRIBUTE_PRINTF (2, 0);
466
467 extern void fprintf_unfiltered (struct ui_file *, const char *, ...)
468 ATTRIBUTE_PRINTF (2, 3);
469
470 extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
471
472 extern void print_spaces (int, struct ui_file *);
473
474 extern void print_spaces_filtered (int, struct ui_file *);
475
476 extern char *n_spaces (int);
477
478 extern void fputstr_filtered (const char *str, int quotr,
479 struct ui_file * stream);
480
481 extern void fputstr_unfiltered (const char *str, int quotr,
482 struct ui_file * stream);
483
484 extern void fputstrn_filtered (const char *str, int n, int quotr,
485 struct ui_file * stream);
486
487 typedef int (*do_fputc_ftype) (int c, ui_file *stream);
488
489 extern void fputstrn_unfiltered (const char *str, int n, int quotr,
490 do_fputc_ftype do_fputc,
491 struct ui_file * stream);
492
493 /* Return nonzero if filtered printing is initialized. */
494 extern int filtered_printing_initialized (void);
495
496 /* Like fprintf_filtered, but styles the output according to STYLE,
497 when appropriate. */
498
499 extern void fprintf_styled (struct ui_file *stream,
500 const ui_file_style &style,
501 const char *fmt,
502 ...)
503 ATTRIBUTE_PRINTF (3, 4);
504
505 extern void vfprintf_styled (struct ui_file *stream,
506 const ui_file_style &style,
507 const char *fmt,
508 va_list args)
509 ATTRIBUTE_PRINTF (3, 0);
510
511 /* Like vfprintf_styled, but do not process gdb-specific format
512 specifiers. */
513 extern void vfprintf_styled_no_gdbfmt (struct ui_file *stream,
514 const ui_file_style &style,
515 bool filter,
516 const char *fmt, va_list args)
517 ATTRIBUTE_PRINTF (4, 0);
518
519 /* Like fputs_filtered, but styles the output according to STYLE, when
520 appropriate. */
521
522 extern void fputs_styled (const char *linebuffer,
523 const ui_file_style &style,
524 struct ui_file *stream);
525
526 /* Unfiltered variant of fputs_styled. */
527
528 extern void fputs_styled_unfiltered (const char *linebuffer,
529 const ui_file_style &style,
530 struct ui_file *stream);
531
532 /* Like fputs_styled, but uses highlight_style to highlight the
533 parts of STR that match HIGHLIGHT. */
534
535 extern void fputs_highlighted (const char *str, const compiled_regex &highlight,
536 struct ui_file *stream);
537
538 /* Reset the terminal style to the default, if needed. */
539
540 extern void reset_terminal_style (struct ui_file *stream);
541
542 /* Display the host ADDR on STREAM formatted as ``0x%x''. */
543 extern void gdb_print_host_address_1 (const void *addr, struct ui_file *stream);
544
545 /* Wrapper that avoids adding a pointless cast to all callers. */
546 #define gdb_print_host_address(ADDR, STREAM) \
547 gdb_print_host_address_1 ((const void *) ADDR, STREAM)
548
549 /* Return the address only having significant bits. */
550 extern CORE_ADDR address_significant (gdbarch *gdbarch, CORE_ADDR addr);
551
552 /* Convert CORE_ADDR to string in platform-specific manner.
553 This is usually formatted similar to 0x%lx. */
554 extern const char *paddress (struct gdbarch *gdbarch, CORE_ADDR addr);
555
556 /* Return a string representation in hexadecimal notation of ADDRESS,
557 which is suitable for printing. */
558
559 extern const char *print_core_address (struct gdbarch *gdbarch,
560 CORE_ADDR address);
561
562 /* Callback hash_f and eq_f for htab_create_alloc or htab_create_alloc_ex. */
563 extern hashval_t core_addr_hash (const void *ap);
564 extern int core_addr_eq (const void *ap, const void *bp);
565
566 extern CORE_ADDR string_to_core_addr (const char *my_string);
567
568 extern void fprintf_symbol_filtered (struct ui_file *, const char *,
569 enum language, int);
570
571 extern void throw_perror_with_name (enum errors errcode, const char *string)
572 ATTRIBUTE_NORETURN;
573
574 extern void perror_warning_with_name (const char *string);
575
576 extern void print_sys_errmsg (const char *, int);
577 \f
578 /* Warnings and error messages. */
579
580 extern void (*deprecated_error_begin_hook) (void);
581
582 /* Message to be printed before the warning message, when a warning occurs. */
583
584 extern const char *warning_pre_print;
585
586 extern void error_stream (const string_file &) ATTRIBUTE_NORETURN;
587
588 extern void demangler_vwarning (const char *file, int line,
589 const char *, va_list ap)
590 ATTRIBUTE_PRINTF (3, 0);
591
592 extern void demangler_warning (const char *file, int line,
593 const char *, ...) ATTRIBUTE_PRINTF (3, 4);
594
595 \f
596 /* Misc. utilities. */
597
598 /* Allocation and deallocation functions for the libiberty hash table
599 which use obstacks. */
600 void *hashtab_obstack_allocate (void *data, size_t size, size_t count);
601 void dummy_obstack_deallocate (void *object, void *data);
602
603 #ifdef HAVE_WAITPID
604 extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout);
605 #endif
606
607 extern int myread (int, char *, int);
608
609 /* Integer exponentiation: Return V1**V2, where both arguments
610 are integers.
611
612 Requires V1 != 0 if V2 < 0.
613 Returns 1 for 0 ** 0. */
614 extern ULONGEST uinteger_pow (ULONGEST v1, LONGEST v2);
615
616 /* Resource limits used by getrlimit and setrlimit. */
617
618 enum resource_limit_kind
619 {
620 LIMIT_CUR,
621 LIMIT_MAX
622 };
623
624 /* Check whether GDB will be able to dump core using the dump_core
625 function. Returns zero if GDB cannot or should not dump core.
626 If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
627 If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
628
629 extern int can_dump_core (enum resource_limit_kind limit_kind);
630
631 /* Print a warning that we cannot dump core. */
632
633 extern void warn_cant_dump_core (const char *reason);
634
635 /* Dump core trying to increase the core soft limit to hard limit
636 first. */
637
638 extern void dump_core (void);
639
640 /* Copy NBITS bits from SOURCE to DEST starting at the given bit
641 offsets. Use the bit order as specified by BITS_BIG_ENDIAN.
642 Source and destination buffers must not overlap. */
643
644 extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
645 const gdb_byte *source, ULONGEST source_offset,
646 ULONGEST nbits, int bits_big_endian);
647
648 /* A fast hashing function. This can be used to hash data in a fast way
649 when the length is known. If no fast hashing library is available, falls
650 back to iterative_hash from libiberty. START_VALUE can be set to
651 continue hashing from a previous value. */
652
653 static inline unsigned int
654 fast_hash (const void *ptr, size_t len, unsigned int start_value = 0)
655 {
656 #ifdef HAVE_LIBXXHASH
657 return XXH64 (ptr, len, start_value);
658 #else
659 return iterative_hash (ptr, len, start_value);
660 #endif
661 }
662
663 #endif /* UTILS_H */