Fix: strip --strip-debug breaks relocations
[binutils-gdb.git] / gdb / utils.h
blobf646b300530aed52b7a50479a75c6925bf77ad6b
1 /* I/O, string, cleanup, and other random utilities for GDB.
2 Copyright (C) 1986-2023 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 "exceptions.h"
23 #include "gdbsupport/array-view.h"
24 #include "gdbsupport/scoped_restore.h"
25 #include <chrono>
27 struct completion_match_for_lcd;
28 class compiled_regex;
30 /* String utilities. */
32 extern bool sevenbit_strings;
34 /* Modes of operation for strncmp_iw_with_mode. */
36 enum class strncmp_iw_mode
38 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
39 differences in whitespace. Returns 0 if they match, non-zero if
40 they don't (slightly different than strcmp()'s range of return
41 values). */
42 NORMAL,
44 /* Like NORMAL, but also apply the strcmp_iw hack. I.e.,
45 string1=="FOO(PARAMS)" matches string2=="FOO". */
46 MATCH_PARAMS,
49 /* Helper for strcmp_iw and strncmp_iw. Exported so that languages
50 can implement both NORMAL and MATCH_PARAMS variants in a single
51 function and defer part of the work to strncmp_iw_with_mode.
53 LANGUAGE is used to implement some context-sensitive
54 language-specific comparisons. For example, for C++,
55 "string1=operator()" should not match "string2=operator" even in
56 MATCH_PARAMS mode.
58 MATCH_FOR_LCD is passed down so that the function can mark parts of
59 the symbol name as ignored for completion matching purposes (e.g.,
60 to handle abi tags). If IGNORE_TEMPLATE_PARAMS is true, all template
61 parameter lists will be ignored when language is C++. */
63 extern int strncmp_iw_with_mode
64 (const char *string1, const char *string2, size_t string2_len,
65 strncmp_iw_mode mode, enum language language,
66 completion_match_for_lcd *match_for_lcd = NULL,
67 bool ignore_template_params = false);
69 /* Do a strncmp() type operation on STRING1 and STRING2, ignoring any
70 differences in whitespace. STRING2_LEN is STRING2's length.
71 Returns 0 if STRING1 matches STRING2_LEN characters of STRING2,
72 non-zero otherwise (slightly different than strncmp()'s range of
73 return values). Note: passes language_minimal to
74 strncmp_iw_with_mode, and should therefore be avoided if a more
75 suitable language is available. */
76 extern int strncmp_iw (const char *string1, const char *string2,
77 size_t string2_len);
79 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
80 differences in whitespace. Returns 0 if they match, non-zero if
81 they don't (slightly different than strcmp()'s range of return
82 values).
84 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
85 This "feature" is useful when searching for matching C++ function
86 names (such as if the user types 'break FOO', where FOO is a
87 mangled C++ function).
89 Note: passes language_minimal to strncmp_iw_with_mode, and should
90 therefore be avoided if a more suitable language is available. */
91 extern int strcmp_iw (const char *string1, const char *string2);
93 extern int strcmp_iw_ordered (const char *, const char *);
95 /* Reset the prompt_for_continue clock. */
96 void reset_prompt_for_continue_wait_time (void);
97 /* Return the time spent in prompt_for_continue. */
98 std::chrono::steady_clock::duration get_prompt_for_continue_wait_time ();
100 /* Parsing utilities. */
102 extern int parse_pid_to_attach (const char *args);
104 extern int parse_escape (struct gdbarch *, const char **);
107 /* Cleanup utilities. */
109 extern void init_page_info (void);
111 /* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
112 Restore when destroyed. */
114 struct set_batch_flag_and_restore_page_info
116 public:
118 set_batch_flag_and_restore_page_info ();
119 ~set_batch_flag_and_restore_page_info ();
121 DISABLE_COPY_AND_ASSIGN (set_batch_flag_and_restore_page_info);
123 private:
125 /* Note that this doesn't use scoped_restore, because it's important
126 to control the ordering of operations in the destruction, and it
127 was simpler to avoid introducing a new ad hoc class. */
128 unsigned m_save_lines_per_page;
129 unsigned m_save_chars_per_line;
130 int m_save_batch_flag;
134 /* Path utilities. */
136 extern int gdb_filename_fnmatch (const char *pattern, const char *string,
137 int flags);
139 extern void substitute_path_component (char **stringp, const char *from,
140 const char *to);
142 std::string ldirname (const char *filename);
144 extern int count_path_elements (const char *path);
146 extern const char *strip_leading_path_elements (const char *path, int n);
148 /* GDB output, ui_file utilities. */
150 struct ui_file;
152 extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
153 extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
154 extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
156 extern void begin_line (void);
158 extern void wrap_here (int);
160 extern void reinitialize_more_filter (void);
162 /* Return the number of characters in a line. */
164 extern int get_chars_per_line ();
166 extern bool pagination_enabled;
168 /* A flag indicating whether to timestamp debugging messages. */
169 extern bool debug_timestamp;
171 extern struct ui_file **current_ui_gdb_stdout_ptr (void);
172 extern struct ui_file **current_ui_gdb_stdin_ptr (void);
173 extern struct ui_file **current_ui_gdb_stderr_ptr (void);
174 extern struct ui_file **current_ui_gdb_stdlog_ptr (void);
176 /* Flush STREAM. */
177 extern void gdb_flush (struct ui_file *stream);
179 /* The current top level's ui_file streams. */
181 /* Normal results */
182 #define gdb_stdout (*current_ui_gdb_stdout_ptr ())
183 /* Input stream */
184 #define gdb_stdin (*current_ui_gdb_stdin_ptr ())
185 /* Serious error notifications. This bypasses the pager, if one is in
186 use. */
187 #define gdb_stderr (*current_ui_gdb_stderr_ptr ())
188 /* Log/debug/trace messages that bypasses the pager, if one is in
189 use. */
190 #define gdb_stdlog (*current_ui_gdb_stdlog_ptr ())
192 /* Truly global ui_file streams. These are all defined in main.c. */
194 /* Target output that should bypass the pager, if one is in use. */
195 extern struct ui_file *gdb_stdtarg;
196 extern struct ui_file *gdb_stdtargerr;
197 extern struct ui_file *gdb_stdtargin;
199 /* Set the screen dimensions to WIDTH and HEIGHT. */
201 extern void set_screen_width_and_height (int width, int height);
203 /* Generic stdio-like operations. */
205 extern void gdb_puts (const char *, struct ui_file *);
207 extern void gdb_putc (int c, struct ui_file *);
209 extern void gdb_putc (int c);
211 extern void gdb_puts (const char *);
213 extern void puts_tabular (char *string, int width, int right);
215 /* Generic printf-like operations. As an extension over plain
216 printf, these support some GDB-specific format specifiers.
217 Particularly useful here are the styling formatters: '%p[', '%p]'
218 and '%ps'. See ui_out::message for details. */
220 extern void gdb_vprintf (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
222 extern void gdb_vprintf (struct ui_file *, const char *, va_list)
223 ATTRIBUTE_PRINTF (2, 0);
225 extern void gdb_printf (struct ui_file *, const char *, ...)
226 ATTRIBUTE_PRINTF (2, 3);
228 extern void gdb_printf (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
230 extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
232 extern void print_spaces (int, struct ui_file *);
234 extern const char *n_spaces (int);
236 /* Return nonzero if filtered printing is initialized. */
237 extern int filtered_printing_initialized (void);
239 /* Like gdb_printf, but styles the output according to STYLE,
240 when appropriate. */
242 extern void fprintf_styled (struct ui_file *stream,
243 const ui_file_style &style,
244 const char *fmt,
245 ...)
246 ATTRIBUTE_PRINTF (3, 4);
248 /* Like gdb_puts, but styles the output according to STYLE, when
249 appropriate. */
251 extern void fputs_styled (const char *linebuffer,
252 const ui_file_style &style,
253 struct ui_file *stream);
255 /* Like fputs_styled, but uses highlight_style to highlight the
256 parts of STR that match HIGHLIGHT. */
258 extern void fputs_highlighted (const char *str, const compiled_regex &highlight,
259 struct ui_file *stream);
261 /* Convert CORE_ADDR to string in platform-specific manner.
262 This is usually formatted similar to 0x%lx. */
263 extern const char *paddress (struct gdbarch *gdbarch, CORE_ADDR addr);
265 /* Return a string representation in hexadecimal notation of ADDRESS,
266 which is suitable for printing. */
268 extern const char *print_core_address (struct gdbarch *gdbarch,
269 CORE_ADDR address);
271 extern CORE_ADDR string_to_core_addr (const char *my_string);
273 extern void fprintf_symbol (struct ui_file *, const char *,
274 enum language, int);
276 extern void perror_warning_with_name (const char *string);
278 /* Issue a warning formatted as '<filename>: <explanation>', where
279 <filename> is FILENAME with filename styling applied. As such, don't
280 pass anything more than a filename in this string. The <explanation>
281 is a string returned from calling safe_strerror(SAVED_ERRNO). */
283 extern void warning_filename_and_errno (const char *filename,
284 int saved_errno);
286 /* Warnings and error messages. */
288 extern void (*deprecated_error_begin_hook) (void);
290 /* Message to be printed before the warning message, when a warning occurs. */
292 extern const char *warning_pre_print;
294 extern void demangler_vwarning (const char *file, int line,
295 const char *, va_list ap)
296 ATTRIBUTE_PRINTF (3, 0);
298 extern void demangler_warning (const char *file, int line,
299 const char *, ...) ATTRIBUTE_PRINTF (3, 4);
302 /* Misc. utilities. */
304 #ifdef HAVE_WAITPID
305 extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout);
306 #endif
308 extern int myread (int, char *, int);
310 /* Resource limits used by getrlimit and setrlimit. */
312 enum resource_limit_kind
314 LIMIT_CUR,
315 LIMIT_MAX
318 /* Check whether GDB will be able to dump core using the dump_core
319 function. Returns zero if GDB cannot or should not dump core.
320 If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
321 If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
323 extern int can_dump_core (enum resource_limit_kind limit_kind);
325 /* Print a warning that we cannot dump core. */
327 extern void warn_cant_dump_core (const char *reason);
329 /* Dump core trying to increase the core soft limit to hard limit
330 first. */
332 extern void dump_core (void);
334 /* Copy NBITS bits from SOURCE to DEST starting at the given bit
335 offsets. Use the bit order as specified by BITS_BIG_ENDIAN.
336 Source and destination buffers must not overlap. */
338 extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
339 const gdb_byte *source, ULONGEST source_offset,
340 ULONGEST nbits, int bits_big_endian);
342 /* When readline decides that the terminal cannot auto-wrap lines, it reduces
343 the width of the reported screen width by 1. This variable indicates
344 whether that's the case or not, allowing us to add it back where
345 necessary. See _rl_term_autowrap in readline/terminal.c. */
347 extern int readline_hidden_cols;
349 /* Assign VAL to LVAL, and set CHANGED to true if the assignment changed
350 LVAL. */
352 template<typename T>
353 void
354 assign_set_if_changed (T &lval, const T &val, bool &changed)
356 if (lval == val)
357 return;
359 lval = val;
360 changed = true;
363 /* Assign VAL to LVAL, and return true if the assignment changed LVAL. */
365 template<typename T>
366 bool
367 assign_return_if_changed (T &lval, const T &val)
369 if (lval == val)
370 return false;
372 lval = val;
373 return true;
376 /* In some cases GDB needs to try several different solutions to a problem,
377 if any of the solutions work then as far as the user is concerned the
378 problem is solved, and GDB should continue without warnings. However,
379 if none of the solutions work then GDB should emit any warnings that
380 occurred while trying each possible solution.
382 One example of this is locating separate debug info. There are several
383 different approaches for this; following the .gnu_debuglink, a build-id
384 based lookup, or using debuginfod. If any works, and debug info is
385 located, then the user doesn't want to see warnings from the earlier
386 approaches that were tried and failed.
388 However, GDB should emit all the warnings using separate calls to
389 warning -- this ensures that each warning is formatted on its own line,
390 and that any styling is emitted correctly.
392 This class helps with deferring warnings. Warnings can be added to an
393 instance of this class with the 'warn' function, and all warnings can be
394 emitted with a single call to 'emit'. */
396 struct deferred_warnings
398 /* Add a warning to the list of deferred warnings. */
399 void warn (const char *format, ...) ATTRIBUTE_PRINTF(2,3)
401 /* Generate the warning text into a string_file. We allow the text to
402 be styled only if gdb_stderr allows styling -- warnings are sent to
403 gdb_stderr. */
404 string_file msg (gdb_stderr->can_emit_style_escape ());
406 va_list args;
407 va_start (args, format);
408 msg.vprintf (format, args);
409 va_end (args);
411 /* Move the text into the list of deferred warnings. */
412 m_warnings.emplace_back (std::move (msg));
415 /* Emit all warnings. */
416 void emit () const
418 for (const auto &w : m_warnings)
419 warning ("%s", w.c_str ());
422 private:
424 /* The list of all deferred warnings. */
425 std::vector<string_file> m_warnings;
428 #endif /* UTILS_H */