gdb/testsuite: Remove duplicates from gdb.base/stack-checking.exp
[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-2022 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 /* 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 ();
116 \f
117 /* Parsing utilities. */
118
119 extern int parse_pid_to_attach (const char *args);
120
121 extern int parse_escape (struct gdbarch *, const char **);
122
123 /* A wrapper for an array of char* that was allocated in the way that
124 'buildargv' does, and should be freed with 'freeargv'. */
125
126 class gdb_argv
127 {
128 public:
129
130 /* A constructor that initializes to NULL. */
131
132 gdb_argv ()
133 : m_argv (NULL)
134 {
135 }
136
137 /* A constructor that calls buildargv on STR. STR may be NULL, in
138 which case this object is initialized with a NULL array. */
139
140 explicit gdb_argv (const char *str)
141 : m_argv (NULL)
142 {
143 reset (str);
144 }
145
146 /* A constructor that takes ownership of an existing array. */
147
148 explicit gdb_argv (char **array)
149 : m_argv (array)
150 {
151 }
152
153 gdb_argv (const gdb_argv &) = delete;
154 gdb_argv &operator= (const gdb_argv &) = delete;
155
156 gdb_argv &operator= (gdb_argv &&other)
157 {
158 freeargv (m_argv);
159 m_argv = other.m_argv;
160 other.m_argv = nullptr;
161 return *this;
162 }
163
164 gdb_argv (gdb_argv &&other)
165 {
166 m_argv = other.m_argv;
167 other.m_argv = nullptr;
168 }
169
170 ~gdb_argv ()
171 {
172 freeargv (m_argv);
173 }
174
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
180 NULL. */
181
182 void reset (const char *str);
183
184 /* Return the underlying array. */
185
186 char **get ()
187 {
188 return m_argv;
189 }
190
191 const char * const * get () const
192 {
193 return m_argv;
194 }
195
196 /* Return the underlying array, transferring ownership to the
197 caller. */
198
199 ATTRIBUTE_UNUSED_RESULT char **release ()
200 {
201 char **result = m_argv;
202 m_argv = NULL;
203 return result;
204 }
205
206 /* Return the number of items in the array. */
207
208 int count () const
209 {
210 return countargv (m_argv);
211 }
212
213 /* Index into the array. */
214
215 char *operator[] (int arg)
216 {
217 gdb_assert (m_argv != NULL);
218 return m_argv[arg];
219 }
220
221 /* Return the arguments array as an array view. */
222
223 gdb::array_view<char *> as_array_view ()
224 {
225 return gdb::array_view<char *> (this->get (), this->count ());
226 }
227
228 gdb::array_view<const char * const> as_array_view () const
229 {
230 return gdb::array_view<const char * const> (this->get (), this->count ());
231 }
232
233 /* Append arguments to this array. */
234 void append (gdb_argv &&other)
235 {
236 int size = count ();
237 int argc = other.count ();
238 m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
239
240 for (int argi = 0; argi < argc; argi++)
241 {
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;
246 }
247 m_argv[size] = nullptr;
248 }
249
250 /* Append arguments to this array. */
251 void append (const gdb_argv &other)
252 {
253 int size = count ();
254 int argc = other.count ();
255 m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
256
257 for (int argi = 0; argi < argc; argi++)
258 m_argv[size++] = xstrdup (other.m_argv[argi]);
259 m_argv[size] = nullptr;
260 }
261
262 /* The iterator type. */
263
264 typedef char **iterator;
265
266 /* Return an iterator pointing to the start of the array. */
267
268 iterator begin ()
269 {
270 return m_argv;
271 }
272
273 /* Return an iterator pointing to the end of the array. */
274
275 iterator end ()
276 {
277 return m_argv + count ();
278 }
279
280 bool operator!= (std::nullptr_t)
281 {
282 return m_argv != NULL;
283 }
284
285 bool operator== (std::nullptr_t)
286 {
287 return m_argv == NULL;
288 }
289
290 private:
291
292 /* The wrapped array. */
293
294 char **m_argv;
295 };
296
297 \f
298 /* Cleanup utilities. */
299
300 /* A deleter for a hash table. */
301 struct htab_deleter
302 {
303 void operator() (htab *ptr) const
304 {
305 htab_delete (ptr);
306 }
307 };
308
309 /* A unique_ptr wrapper for htab_t. */
310 typedef std::unique_ptr<htab, htab_deleter> htab_up;
311
312 /* A wrapper for 'delete' that can used as a hash table entry deletion
313 function. */
314 template<typename T>
315 void
316 htab_delete_entry (void *ptr)
317 {
318 delete (T *) ptr;
319 }
320
321 extern void init_page_info (void);
322
323 /* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
324 Restore when destroyed. */
325
326 struct set_batch_flag_and_restore_page_info
327 {
328 public:
329
330 set_batch_flag_and_restore_page_info ();
331 ~set_batch_flag_and_restore_page_info ();
332
333 DISABLE_COPY_AND_ASSIGN (set_batch_flag_and_restore_page_info);
334
335 private:
336
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;
343 };
344
345 \f
346 /* Path utilities. */
347
348 extern int gdb_filename_fnmatch (const char *pattern, const char *string,
349 int flags);
350
351 extern void substitute_path_component (char **stringp, const char *from,
352 const char *to);
353
354 std::string ldirname (const char *filename);
355
356 extern int count_path_elements (const char *path);
357
358 extern const char *strip_leading_path_elements (const char *path, int n);
359 \f
360 /* GDB output, ui_file utilities. */
361
362 struct ui_file;
363
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);
367
368 extern void begin_line (void);
369
370 extern void wrap_here (const char *);
371
372 extern void reinitialize_more_filter (void);
373
374 /* Return the number of characters in a line. */
375
376 extern int get_chars_per_line ();
377
378 extern bool pagination_enabled;
379
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);
384
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 *);
390
391 /* The current top level's ui_file streams. */
392
393 /* Normal results */
394 #define gdb_stdout (*current_ui_gdb_stdout_ptr ())
395 /* Input stream */
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 ())
404
405 /* Truly global ui_file streams. These are all defined in main.c. */
406
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;
414
415 /* Set the screen dimensions to WIDTH and HEIGHT. */
416
417 extern void set_screen_width_and_height (int width, int height);
418
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. */
424
425 extern void fputs_filtered (const char *, struct ui_file *);
426
427 extern void fputs_unfiltered (const char *, struct ui_file *);
428
429 extern int fputc_filtered (int c, struct ui_file *);
430
431 extern int fputc_unfiltered (int c, struct ui_file *);
432
433 extern int putchar_filtered (int c);
434
435 extern int putchar_unfiltered (int c);
436
437 extern void puts_filtered (const char *);
438
439 extern void puts_unfiltered (const char *);
440
441 extern void puts_filtered_tabular (char *string, int width, int right);
442
443 extern void vprintf_filtered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
444
445 extern void vfprintf_filtered (struct ui_file *, const char *, va_list)
446 ATTRIBUTE_PRINTF (2, 0);
447
448 extern void fprintf_filtered (struct ui_file *, const char *, ...)
449 ATTRIBUTE_PRINTF (2, 3);
450
451 extern void printf_filtered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
452
453 extern void vprintf_unfiltered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
454
455 extern void vfprintf_unfiltered (struct ui_file *, const char *, va_list)
456 ATTRIBUTE_PRINTF (2, 0);
457
458 extern void fprintf_unfiltered (struct ui_file *, const char *, ...)
459 ATTRIBUTE_PRINTF (2, 3);
460
461 extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
462
463 extern void print_spaces_filtered (int, struct ui_file *);
464
465 extern const char *n_spaces (int);
466
467 /* Return nonzero if filtered printing is initialized. */
468 extern int filtered_printing_initialized (void);
469
470 /* Like fprintf_filtered, but styles the output according to STYLE,
471 when appropriate. */
472
473 extern void fprintf_styled (struct ui_file *stream,
474 const ui_file_style &style,
475 const char *fmt,
476 ...)
477 ATTRIBUTE_PRINTF (3, 4);
478
479 extern void vfprintf_styled (struct ui_file *stream,
480 const ui_file_style &style,
481 const char *fmt,
482 va_list args)
483 ATTRIBUTE_PRINTF (3, 0);
484
485 /* Like vfprintf_styled, but do not process gdb-specific format
486 specifiers. */
487 extern void vfprintf_styled_no_gdbfmt (struct ui_file *stream,
488 const ui_file_style &style,
489 bool filter,
490 const char *fmt, va_list args)
491 ATTRIBUTE_PRINTF (4, 0);
492
493 /* Like fputs_filtered, but styles the output according to STYLE, when
494 appropriate. */
495
496 extern void fputs_styled (const char *linebuffer,
497 const ui_file_style &style,
498 struct ui_file *stream);
499
500 /* Unfiltered variant of fputs_styled. */
501
502 extern void fputs_styled_unfiltered (const char *linebuffer,
503 const ui_file_style &style,
504 struct ui_file *stream);
505
506 /* Like fputs_styled, but uses highlight_style to highlight the
507 parts of STR that match HIGHLIGHT. */
508
509 extern void fputs_highlighted (const char *str, const compiled_regex &highlight,
510 struct ui_file *stream);
511
512 /* Reset the terminal style to the default, if needed. */
513
514 extern void reset_terminal_style (struct ui_file *stream);
515
516 /* Return the address only having significant bits. */
517 extern CORE_ADDR address_significant (gdbarch *gdbarch, CORE_ADDR addr);
518
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);
522
523 /* Return a string representation in hexadecimal notation of ADDRESS,
524 which is suitable for printing. */
525
526 extern const char *print_core_address (struct gdbarch *gdbarch,
527 CORE_ADDR address);
528
529 extern CORE_ADDR string_to_core_addr (const char *my_string);
530
531 extern void fprintf_symbol_filtered (struct ui_file *, const char *,
532 enum language, int);
533
534 extern void throw_perror_with_name (enum errors errcode, const char *string)
535 ATTRIBUTE_NORETURN;
536
537 extern void perror_warning_with_name (const char *string);
538
539 extern void print_sys_errmsg (const char *, int);
540 \f
541 /* Warnings and error messages. */
542
543 extern void (*deprecated_error_begin_hook) (void);
544
545 /* Message to be printed before the warning message, when a warning occurs. */
546
547 extern const char *warning_pre_print;
548
549 extern void error_stream (const string_file &) ATTRIBUTE_NORETURN;
550
551 extern void demangler_vwarning (const char *file, int line,
552 const char *, va_list ap)
553 ATTRIBUTE_PRINTF (3, 0);
554
555 extern void demangler_warning (const char *file, int line,
556 const char *, ...) ATTRIBUTE_PRINTF (3, 4);
557
558 \f
559 /* Misc. utilities. */
560
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);
565
566 #ifdef HAVE_WAITPID
567 extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout);
568 #endif
569
570 extern int myread (int, char *, int);
571
572 /* Integer exponentiation: Return V1**V2, where both arguments
573 are integers.
574
575 Requires V1 != 0 if V2 < 0.
576 Returns 1 for 0 ** 0. */
577 extern ULONGEST uinteger_pow (ULONGEST v1, LONGEST v2);
578
579 /* Resource limits used by getrlimit and setrlimit. */
580
581 enum resource_limit_kind
582 {
583 LIMIT_CUR,
584 LIMIT_MAX
585 };
586
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. */
591
592 extern int can_dump_core (enum resource_limit_kind limit_kind);
593
594 /* Print a warning that we cannot dump core. */
595
596 extern void warn_cant_dump_core (const char *reason);
597
598 /* Dump core trying to increase the core soft limit to hard limit
599 first. */
600
601 extern void dump_core (void);
602
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. */
606
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);
610
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. */
615
616 static inline unsigned int
617 fast_hash (const void *ptr, size_t len, unsigned int start_value = 0)
618 {
619 #ifdef HAVE_LIBXXHASH
620 return XXH64 (ptr, len, start_value);
621 #else
622 return iterative_hash (ptr, len, start_value);
623 #endif
624 }
625
626 #endif /* UTILS_H */