Convert dwarf2_per_objfile::die_type_hash to new hash table
[binutils-gdb.git] / gdb / utils.h
blobd69c81ca834f32b7b44d2a2719bab5f801e76ac7
1 /* I/O, string, cleanup, and other random utilities for GDB.
2 Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #ifndef UTILS_H
20 #define UTILS_H
22 #include <chrono>
24 struct completion_match_for_lcd;
25 class compiled_regex;
27 /* String utilities. */
29 extern bool sevenbit_strings;
31 /* Modes of operation for strncmp_iw_with_mode. */
33 enum class strncmp_iw_mode
35 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
36 differences in whitespace. Returns 0 if they match, non-zero if
37 they don't (slightly different than strcmp()'s range of return
38 values). */
39 NORMAL,
41 /* Like NORMAL, but also apply the strcmp_iw hack. I.e.,
42 string1=="FOO(PARAMS)" matches string2=="FOO". */
43 MATCH_PARAMS,
46 /* Helper for strcmp_iw and strncmp_iw. Exported so that languages
47 can implement both NORMAL and MATCH_PARAMS variants in a single
48 function and defer part of the work to strncmp_iw_with_mode.
50 LANGUAGE is used to implement some context-sensitive
51 language-specific comparisons. For example, for C++,
52 "string1=operator()" should not match "string2=operator" even in
53 MATCH_PARAMS mode.
55 MATCH_FOR_LCD is passed down so that the function can mark parts of
56 the symbol name as ignored for completion matching purposes (e.g.,
57 to handle abi tags). If IGNORE_TEMPLATE_PARAMS is true, all template
58 parameter lists will be ignored when language is C++. */
60 extern int strncmp_iw_with_mode
61 (const char *string1, const char *string2, size_t string2_len,
62 strncmp_iw_mode mode, enum language language,
63 completion_match_for_lcd *match_for_lcd = NULL,
64 bool ignore_template_params = false);
66 /* Do a strncmp() type operation on STRING1 and STRING2, ignoring any
67 differences in whitespace. STRING2_LEN is STRING2's length.
68 Returns 0 if STRING1 matches STRING2_LEN characters of STRING2,
69 non-zero otherwise (slightly different than strncmp()'s range of
70 return values). Note: passes language_minimal to
71 strncmp_iw_with_mode, and should therefore be avoided if a more
72 suitable language is available. */
73 extern int strncmp_iw (const char *string1, const char *string2,
74 size_t string2_len);
76 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
77 differences in whitespace. Returns 0 if they match, non-zero if
78 they don't (slightly different than strcmp()'s range of return
79 values).
81 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
82 This "feature" is useful when searching for matching C++ function
83 names (such as if the user types 'break FOO', where FOO is a
84 mangled C++ function).
86 Note: passes language_minimal to strncmp_iw_with_mode, and should
87 therefore be avoided if a more suitable language is available. */
88 extern int strcmp_iw (const char *string1, const char *string2);
90 extern int strcmp_iw_ordered (const char *, const char *);
92 /* Reset the prompt_for_continue clock. */
93 void reset_prompt_for_continue_wait_time (void);
94 /* Return the time spent in prompt_for_continue. */
95 std::chrono::steady_clock::duration get_prompt_for_continue_wait_time ();
97 /* Parsing utilities. */
99 extern int parse_pid_to_attach (const char *args);
101 extern int parse_escape (struct gdbarch *, const char **);
104 /* Cleanup utilities. */
106 extern void init_page_info (void);
108 /* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
109 Restore when destroyed. */
111 struct set_batch_flag_and_restore_page_info
113 public:
115 set_batch_flag_and_restore_page_info ();
116 ~set_batch_flag_and_restore_page_info ();
118 DISABLE_COPY_AND_ASSIGN (set_batch_flag_and_restore_page_info);
120 private:
122 /* Note that this doesn't use scoped_restore, because it's important
123 to control the ordering of operations in the destruction, and it
124 was simpler to avoid introducing a new ad hoc class. */
125 unsigned m_save_lines_per_page;
126 unsigned m_save_chars_per_line;
127 int m_save_batch_flag;
131 /* Path utilities. */
133 extern int gdb_filename_fnmatch (const char *pattern, const char *string,
134 int flags);
136 extern void substitute_path_component (char **stringp, const char *from,
137 const char *to);
139 std::string ldirname (const char *filename);
141 extern int count_path_elements (const char *path);
143 extern const char *strip_leading_path_elements (const char *path, int n);
145 /* GDB output, ui_file utilities. */
147 struct ui_file;
149 extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
150 extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
151 extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
153 extern void begin_line (void);
155 extern void wrap_here (int);
157 extern void reinitialize_more_filter (void);
159 /* Return the number of characters in a line. */
161 extern int get_chars_per_line ();
163 extern bool pagination_enabled;
165 /* A flag indicating whether to timestamp debugging messages. */
166 extern bool debug_timestamp;
168 extern struct ui_file **current_ui_gdb_stdout_ptr (void);
169 extern struct ui_file **current_ui_gdb_stdin_ptr (void);
170 extern struct ui_file **current_ui_gdb_stderr_ptr (void);
171 extern struct ui_file **current_ui_gdb_stdlog_ptr (void);
173 /* Flush STREAM. */
174 extern void gdb_flush (struct ui_file *stream);
176 /* The current top level's ui_file streams. */
178 /* Normal results */
179 #define gdb_stdout (*current_ui_gdb_stdout_ptr ())
180 /* Input stream */
181 #define gdb_stdin (*current_ui_gdb_stdin_ptr ())
182 /* Serious error notifications. This bypasses the pager, if one is in
183 use. */
184 #define gdb_stderr (*current_ui_gdb_stderr_ptr ())
185 /* Log/debug/trace messages that bypasses the pager, if one is in
186 use. */
187 #define gdb_stdlog (*current_ui_gdb_stdlog_ptr ())
189 /* Truly global ui_file streams. These are all defined in main.c. */
191 /* Target output that should bypass the pager, if one is in use. */
192 extern struct ui_file *gdb_stdtarg;
193 extern struct ui_file *gdb_stdtargin;
195 /* Set the screen dimensions to WIDTH and HEIGHT. */
197 extern void set_screen_width_and_height (int width, int height);
199 /* Generic stdio-like operations. */
201 extern void gdb_puts (const char *, struct ui_file *);
203 extern void gdb_puts (const std::string &s, ui_file *stream);
205 extern void gdb_putc (int c, struct ui_file *);
207 extern void gdb_putc (int c);
209 extern void gdb_puts (const char *);
211 extern void puts_tabular (char *string, int width, int right);
213 /* Generic printf-like operations. As an extension over plain
214 printf, these support some GDB-specific format specifiers.
215 Particularly useful here are the styling formatters: '%p[', '%p]'
216 and '%ps'. See ui_out::message for details. */
218 extern void gdb_vprintf (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
220 extern void gdb_vprintf (struct ui_file *, const char *, va_list)
221 ATTRIBUTE_PRINTF (2, 0);
223 extern void gdb_printf (struct ui_file *, const char *, ...)
224 ATTRIBUTE_PRINTF (2, 3);
226 extern void gdb_printf (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
228 extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
230 extern void print_spaces (int, struct ui_file *);
232 extern const char *n_spaces (int);
234 /* Return nonzero if filtered printing is initialized. */
235 extern int filtered_printing_initialized (void);
237 /* Like gdb_printf, but styles the output according to STYLE,
238 when appropriate. */
240 extern void fprintf_styled (struct ui_file *stream,
241 const ui_file_style &style,
242 const char *fmt,
243 ...)
244 ATTRIBUTE_PRINTF (3, 4);
246 /* Like gdb_puts, but styles the output according to STYLE, when
247 appropriate. */
249 extern void fputs_styled (const char *linebuffer,
250 const ui_file_style &style,
251 struct ui_file *stream);
253 /* Like fputs_styled, but uses highlight_style to highlight the
254 parts of STR that match HIGHLIGHT. */
256 extern void fputs_highlighted (const char *str, const compiled_regex &highlight,
257 struct ui_file *stream);
259 /* Convert CORE_ADDR to string in platform-specific manner.
260 This is usually formatted similar to 0x%lx. */
261 extern const char *paddress (struct gdbarch *gdbarch, CORE_ADDR addr);
263 /* Return a string representation in hexadecimal notation of ADDRESS,
264 which is suitable for printing. */
266 extern const char *print_core_address (struct gdbarch *gdbarch,
267 CORE_ADDR address);
269 extern CORE_ADDR string_to_core_addr (const char *my_string);
271 extern void fprintf_symbol (struct ui_file *, const char *,
272 enum language, int);
274 extern void perror_warning_with_name (const char *string);
276 /* Issue a warning formatted as '<filename>: <explanation>', where
277 <filename> is FILENAME with filename styling applied. As such, don't
278 pass anything more than a filename in this string. The <explanation>
279 is a string returned from calling safe_strerror(SAVED_ERRNO). */
281 extern void warning_filename_and_errno (const char *filename,
282 int saved_errno);
284 /* Warnings and error messages. */
286 extern void (*deprecated_error_begin_hook) (void);
288 /* Message to be printed before the warning message, when a warning occurs. */
290 extern const char *warning_pre_print;
292 extern void demangler_vwarning (const char *file, int line,
293 const char *, va_list ap)
294 ATTRIBUTE_PRINTF (3, 0);
296 extern void demangler_warning (const char *file, int line,
297 const char *, ...) ATTRIBUTE_PRINTF (3, 4);
300 /* Misc. utilities. */
302 #ifdef HAVE_WAITPID
303 extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout);
304 #endif
306 extern int myread (int, char *, int);
308 /* Resource limits used by getrlimit and setrlimit. */
310 enum resource_limit_kind
312 LIMIT_CUR,
313 LIMIT_MAX
316 /* Check whether GDB will be able to dump core using the dump_core
317 function. Returns zero if GDB cannot or should not dump core.
318 If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
319 If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
321 extern int can_dump_core (enum resource_limit_kind limit_kind);
323 /* Print a warning that we cannot dump core. */
325 extern void warn_cant_dump_core (const char *reason);
327 /* Dump core trying to increase the core soft limit to hard limit
328 first. */
330 extern void dump_core (void);
332 /* Copy NBITS bits from SOURCE to DEST starting at the given bit
333 offsets. Use the bit order as specified by BITS_BIG_ENDIAN.
334 Source and destination buffers must not overlap. */
336 extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
337 const gdb_byte *source, ULONGEST source_offset,
338 ULONGEST nbits, int bits_big_endian);
340 /* When readline decides that the terminal cannot auto-wrap lines, it reduces
341 the width of the reported screen width by 1. This variable indicates
342 whether that's the case or not, allowing us to add it back where
343 necessary. See _rl_term_autowrap in readline/terminal.c. */
345 extern int readline_hidden_cols;
347 /* Assign VAL to LVAL, and set CHANGED to true if the assignment changed
348 LVAL. */
350 template<typename T>
351 void
352 assign_set_if_changed (T &lval, const T &val, bool &changed)
354 if (lval == val)
355 return;
357 lval = val;
358 changed = true;
361 /* Assign VAL to LVAL, and return true if the assignment changed LVAL. */
363 template<typename T>
364 bool
365 assign_return_if_changed (T &lval, const T &val)
367 if (lval == val)
368 return false;
370 lval = val;
371 return true;
374 /* ARG is an argument string as passed to a GDB command which is expected
375 to contain a single, possibly quoted, filename argument. Extract the
376 filename and return it as a string. If the filename is quoted then the
377 quotes will have been removed. If the filename is not quoted then any
378 escaping within the filename will have been removed.
380 If there is any content in ARG after the filename then an error will be
381 thrown complaining about the extra content.
383 If there is no filename in ARG, or if ARG is nullptr, then an empty
384 string will be returned. */
386 extern std::string extract_single_filename_arg (const char *arg);
388 /* A class that can be used to intercept warnings. A class is used
389 here, rather than a gdb::function_view because it proved difficult
390 to use a function view in conjunction with ATTRIBUTE_PRINTF in a
391 way that would satisfy all compilers on all systems. And, even
392 though gdb only ever uses deferred_warnings here, a virtual
393 function is used to help Insight. */
394 struct warning_hook_handler_type
396 virtual void warn (const char *format, va_list args) ATTRIBUTE_PRINTF (2, 0)
397 = 0;
400 typedef warning_hook_handler_type *warning_hook_handler;
402 /* Set the thread-local warning hook, and restore the old value when
403 finished. */
404 class scoped_restore_warning_hook
406 public:
407 explicit scoped_restore_warning_hook (warning_hook_handler new_handler);
409 ~scoped_restore_warning_hook ();
411 private:
412 scoped_restore_warning_hook (const scoped_restore_warning_hook &other)
413 = delete;
414 scoped_restore_warning_hook &operator= (const scoped_restore_warning_hook &)
415 = delete;
417 warning_hook_handler m_save;
420 /* Return the current warning handler. */
421 extern warning_hook_handler get_warning_hook_handler ();
423 /* In some cases GDB needs to try several different solutions to a problem,
424 if any of the solutions work then as far as the user is concerned the
425 problem is solved, and GDB should continue without warnings. However,
426 if none of the solutions work then GDB should emit any warnings that
427 occurred while trying each possible solution.
429 One example of this is locating separate debug info. There are several
430 different approaches for this; following the .gnu_debuglink, a build-id
431 based lookup, or using debuginfod. If any works, and debug info is
432 located, then the user doesn't want to see warnings from the earlier
433 approaches that were tried and failed.
435 However, GDB should emit all the warnings using separate calls to
436 warning -- this ensures that each warning is formatted on its own line,
437 and that any styling is emitted correctly.
439 This class helps with deferring warnings. Warnings can be added to an
440 instance of this class with the 'warn' function, and all warnings can be
441 emitted with a single call to 'emit'. */
443 struct deferred_warnings final : public warning_hook_handler_type
445 deferred_warnings ()
446 : m_can_style (gdb_stderr->can_emit_style_escape ())
450 /* Add a warning to the list of deferred warnings. */
451 void warn (const char *format, ...) ATTRIBUTE_PRINTF (2, 3)
453 va_list args;
454 va_start (args, format);
455 this->warn (format, args);
456 va_end (args);
459 /* A variant of 'warn' so that this object can be used as a warning
460 hook; see scoped_restore_warning_hook. Note that no locking is
461 done, so users have to be careful to only install this into a
462 single thread at a time. */
463 void warn (const char *format, va_list args) override
464 ATTRIBUTE_PRINTF (2, 0)
466 string_file msg (m_can_style);
467 msg.vprintf (format, args);
468 m_warnings.emplace_back (std::move (msg));
471 /* Emit all warnings. */
472 void emit () const
474 for (const auto &w : m_warnings)
475 warning ("%s", w.c_str ());
478 private:
480 /* True if gdb_stderr supports styling at the moment this object is
481 constructed. This is done just once so that objects of this type
482 can be used off the main thread. */
483 bool m_can_style;
485 /* The list of all deferred warnings. */
486 std::vector<string_file> m_warnings;
489 #endif /* UTILS_H */