]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/utils.h
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / utils.h
CommitLineData
48faced0 1/* I/O, string, cleanup, and other random utilities for GDB.
d01e8234 2 Copyright (C) 1986-2025 Free Software Foundation, Inc.
48faced0
DE
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
cc709640
TT
19#ifndef GDB_UTILS_H
20#define GDB_UTILS_H
48faced0 21
dcb07cfa 22#include <chrono>
48faced0 23
0d12e84c
TT
24struct completion_match_for_lcd;
25class compiled_regex;
26
48faced0
DE
27/* String utilities. */
28
491144b5 29extern bool sevenbit_strings;
48faced0 30
b5ec771e
PA
31/* Modes of operation for strncmp_iw_with_mode. */
32
33enum class strncmp_iw_mode
34{
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,
40
41 /* Like NORMAL, but also apply the strcmp_iw hack. I.e.,
42 string1=="FOO(PARAMS)" matches string2=="FOO". */
43 MATCH_PARAMS,
44};
45
46/* Helper for strcmp_iw and strncmp_iw. Exported so that languages
47 can implement both NORMAL and MATCH_PARAMS variants in a single
0662b6a7 48 function and defer part of the work to strncmp_iw_with_mode.
bd69330d 49
0662b6a7
PA
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
bd69330d
PA
53 MATCH_PARAMS mode.
54
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.,
64a97606
KS
57 to handle abi tags). If IGNORE_TEMPLATE_PARAMS is true, all template
58 parameter lists will be ignored when language is C++. */
59
bd69330d
PA
60extern int strncmp_iw_with_mode
61 (const char *string1, const char *string2, size_t string2_len,
62 strncmp_iw_mode mode, enum language language,
64a97606
KS
63 completion_match_for_lcd *match_for_lcd = NULL,
64 bool ignore_template_params = false);
b5ec771e 65
1d550c82
PA
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
0662b6a7
PA
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. */
1d550c82
PA
73extern int strncmp_iw (const char *string1, const char *string2,
74 size_t string2_len);
75
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).
80
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
0662b6a7
PA
84 mangled C++ function).
85
86 Note: passes language_minimal to strncmp_iw_with_mode, and should
87 therefore be avoided if a more suitable language is available. */
1d550c82 88extern int strcmp_iw (const char *string1, const char *string2);
48faced0
DE
89
90extern int strcmp_iw_ordered (const char *, const char *);
91
bd712aed
DE
92/* Reset the prompt_for_continue clock. */
93void reset_prompt_for_continue_wait_time (void);
94/* Return the time spent in prompt_for_continue. */
dcb07cfa 95std::chrono::steady_clock::duration get_prompt_for_continue_wait_time ();
48faced0 96\f
30baf67b 97/* Parsing utilities. */
48faced0 98
c0939df1 99extern int parse_pid_to_attach (const char *args);
48faced0 100
d7561cbb 101extern int parse_escape (struct gdbarch *, const char **);
48faced0 102
48faced0
DE
103\f
104/* Cleanup utilities. */
105
48faced0
DE
106extern void init_page_info (void);
107
b95de2b7
TT
108/* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
109 Restore when destroyed. */
110
111struct set_batch_flag_and_restore_page_info
112{
113public:
114
115 set_batch_flag_and_restore_page_info ();
116 ~set_batch_flag_and_restore_page_info ();
117
118 DISABLE_COPY_AND_ASSIGN (set_batch_flag_and_restore_page_info);
119
120private:
121
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;
128};
48faced0 129
48faced0
DE
130\f
131/* Path utilities. */
132
48faced0
DE
133extern int gdb_filename_fnmatch (const char *pattern, const char *string,
134 int flags);
135
a22a215f 136std::string gdb_ldirname (const char *filename);
cce0e923
DE
137
138extern int count_path_elements (const char *path);
139
140extern const char *strip_leading_path_elements (const char *path, int n);
48faced0
DE
141\f
142/* GDB output, ui_file utilities. */
143
144struct ui_file;
145
48faced0
DE
146extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
147extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
148extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
149
150extern void begin_line (void);
151
6c92c339 152extern void wrap_here (int);
48faced0
DE
153
154extern void reinitialize_more_filter (void);
155
2f228731
TT
156/* Return the number of characters in a line. */
157
158extern int get_chars_per_line ();
159
491144b5 160extern bool pagination_enabled;
74da6f00 161
3c6c449e
TT
162/* A flag indicating whether to timestamp debugging messages. */
163extern bool debug_timestamp;
164
79aa2fe8
PA
165extern struct ui_file **current_ui_gdb_stdout_ptr (void);
166extern struct ui_file **current_ui_gdb_stdin_ptr (void);
167extern struct ui_file **current_ui_gdb_stderr_ptr (void);
168extern struct ui_file **current_ui_gdb_stdlog_ptr (void);
169
6b0c1154
TT
170/* Flush STREAM. */
171extern void gdb_flush (struct ui_file *stream);
faa17681 172
79aa2fe8
PA
173/* The current top level's ui_file streams. */
174
48faced0 175/* Normal results */
79aa2fe8 176#define gdb_stdout (*current_ui_gdb_stdout_ptr ())
48faced0 177/* Input stream */
79aa2fe8 178#define gdb_stdin (*current_ui_gdb_stdin_ptr ())
6b0c1154
TT
179/* Serious error notifications. This bypasses the pager, if one is in
180 use. */
79aa2fe8 181#define gdb_stderr (*current_ui_gdb_stderr_ptr ())
6b0c1154
TT
182/* Log/debug/trace messages that bypasses the pager, if one is in
183 use. */
79aa2fe8
PA
184#define gdb_stdlog (*current_ui_gdb_stdlog_ptr ())
185
186/* Truly global ui_file streams. These are all defined in main.c. */
187
6b0c1154 188/* Target output that should bypass the pager, if one is in use. */
48faced0 189extern struct ui_file *gdb_stdtarg;
48faced0
DE
190extern struct ui_file *gdb_stdtargin;
191
d6e5e7f7
PP
192/* Set the screen dimensions to WIDTH and HEIGHT. */
193
194extern void set_screen_width_and_height (int width, int height);
195
6b0c1154 196/* Generic stdio-like operations. */
48faced0 197
0426ad51 198extern void gdb_puts (const char *, struct ui_file *);
48faced0 199
a887499c
SM
200extern void gdb_puts (const std::string &s, ui_file *stream);
201
4311246b 202extern void gdb_putc (int c, struct ui_file *);
48faced0 203
4311246b 204extern void gdb_putc (int c);
48faced0 205
0426ad51 206extern void gdb_puts (const char *);
48faced0 207
9fbf7f08 208extern void puts_tabular (char *string, int width, int right);
48faced0 209
6b0c1154
TT
210/* Generic printf-like operations. As an extension over plain
211 printf, these support some GDB-specific format specifiers.
212 Particularly useful here are the styling formatters: '%p[', '%p]'
213 and '%ps'. See ui_out::message for details. */
214
19a7b8ab 215extern void gdb_vprintf (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
48faced0 216
19a7b8ab 217extern void gdb_vprintf (struct ui_file *, const char *, va_list)
48faced0
DE
218 ATTRIBUTE_PRINTF (2, 0);
219
6cb06a8c 220extern void gdb_printf (struct ui_file *, const char *, ...)
48faced0
DE
221 ATTRIBUTE_PRINTF (2, 3);
222
6cb06a8c 223extern void gdb_printf (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
48faced0
DE
224
225extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
226
d0b1020b 227extern void print_spaces (int, struct ui_file *);
48faced0 228
dde238e0 229extern const char *n_spaces (int);
48faced0 230
2437fd32
GB
231/* Return nonzero if filtered printing is initialized. */
232extern int filtered_printing_initialized (void);
233
6cb06a8c 234/* Like gdb_printf, but styles the output according to STYLE,
cbe56571
TT
235 when appropriate. */
236
237extern void fprintf_styled (struct ui_file *stream,
238 const ui_file_style &style,
239 const char *fmt,
240 ...)
241 ATTRIBUTE_PRINTF (3, 4);
242
0426ad51 243/* Like gdb_puts, but styles the output according to STYLE, when
cbe56571
TT
244 appropriate. */
245
246extern void fputs_styled (const char *linebuffer,
247 const ui_file_style &style,
248 struct ui_file *stream);
249
9303eb2f
PW
250/* Like fputs_styled, but uses highlight_style to highlight the
251 parts of STR that match HIGHLIGHT. */
252
253extern void fputs_highlighted (const char *str, const compiled_regex &highlight,
254 struct ui_file *stream);
255
48faced0
DE
256/* Convert CORE_ADDR to string in platform-specific manner.
257 This is usually formatted similar to 0x%lx. */
258extern const char *paddress (struct gdbarch *gdbarch, CORE_ADDR addr);
259
260/* Return a string representation in hexadecimal notation of ADDRESS,
261 which is suitable for printing. */
262
263extern const char *print_core_address (struct gdbarch *gdbarch,
264 CORE_ADDR address);
265
48faced0
DE
266extern CORE_ADDR string_to_core_addr (const char *my_string);
267
bed009b9
TT
268extern void fprintf_symbol (struct ui_file *, const char *,
269 enum language, int);
48faced0 270
7c647d61
JB
271extern void perror_warning_with_name (const char *string);
272
3d38b301
AB
273/* Issue a warning formatted as '<filename>: <explanation>', where
274 <filename> is FILENAME with filename styling applied. As such, don't
275 pass anything more than a filename in this string. The <explanation>
276 is a string returned from calling safe_strerror(SAVED_ERRNO). */
277
278extern void warning_filename_and_errno (const char *filename,
279 int saved_errno);
48faced0
DE
280\f
281/* Warnings and error messages. */
282
283extern void (*deprecated_error_begin_hook) (void);
284
48faced0
DE
285/* Message to be printed before the warning message, when a warning occurs. */
286
69bbf465 287extern const char *warning_pre_print;
48faced0 288
57fcfb1b
GB
289extern void demangler_vwarning (const char *file, int line,
290 const char *, va_list ap)
291 ATTRIBUTE_PRINTF (3, 0);
292
293extern void demangler_warning (const char *file, int line,
294 const char *, ...) ATTRIBUTE_PRINTF (3, 4);
295
48faced0
DE
296\f
297/* Misc. utilities. */
298
48faced0
DE
299#ifdef HAVE_WAITPID
300extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout);
301#endif
302
48faced0
DE
303extern int myread (int, char *, int);
304
eae7090b
GB
305/* Resource limits used by getrlimit and setrlimit. */
306
307enum resource_limit_kind
308 {
309 LIMIT_CUR,
310 LIMIT_MAX
311 };
312
313/* Check whether GDB will be able to dump core using the dump_core
314 function. Returns zero if GDB cannot or should not dump core.
315 If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
316 If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
317
318extern int can_dump_core (enum resource_limit_kind limit_kind);
319
320/* Print a warning that we cannot dump core. */
321
322extern void warn_cant_dump_core (const char *reason);
323
324/* Dump core trying to increase the core soft limit to hard limit
325 first. */
326
327extern void dump_core (void);
328
a99bc3d2
JB
329/* Copy NBITS bits from SOURCE to DEST starting at the given bit
330 offsets. Use the bit order as specified by BITS_BIG_ENDIAN.
331 Source and destination buffers must not overlap. */
332
333extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
334 const gdb_byte *source, ULONGEST source_offset,
335 ULONGEST nbits, int bits_big_endian);
336
deb1ba4e
TV
337/* When readline decides that the terminal cannot auto-wrap lines, it reduces
338 the width of the reported screen width by 1. This variable indicates
339 whether that's the case or not, allowing us to add it back where
340 necessary. See _rl_term_autowrap in readline/terminal.c. */
341
342extern int readline_hidden_cols;
343
2e12e798
TV
344/* Assign VAL to LVAL, and set CHANGED to true if the assignment changed
345 LVAL. */
346
347template<typename T>
348void
349assign_set_if_changed (T &lval, const T &val, bool &changed)
350{
351 if (lval == val)
352 return;
353
354 lval = val;
355 changed = true;
356}
357
358/* Assign VAL to LVAL, and return true if the assignment changed LVAL. */
359
360template<typename T>
361bool
362assign_return_if_changed (T &lval, const T &val)
363{
364 if (lval == val)
365 return false;
366
367 lval = val;
368 return true;
369}
370
1217ae67
AB
371/* ARG is an argument string as passed to a GDB command which is expected
372 to contain a single, possibly quoted, filename argument. Extract the
373 filename and return it as a string. If the filename is quoted then the
374 quotes will have been removed. If the filename is not quoted then any
375 escaping within the filename will have been removed.
376
377 If there is any content in ARG after the filename then an error will be
378 thrown complaining about the extra content.
379
380 If there is no filename in ARG, or if ARG is nullptr, then an empty
381 string will be returned. */
382
383extern std::string extract_single_filename_arg (const char *arg);
384
75670e00
TT
385/* A class that can be used to intercept warnings. A class is used
386 here, rather than a gdb::function_view because it proved difficult
387 to use a function view in conjunction with ATTRIBUTE_PRINTF in a
388 way that would satisfy all compilers on all systems. And, even
389 though gdb only ever uses deferred_warnings here, a virtual
390 function is used to help Insight. */
391struct warning_hook_handler_type
392{
393 virtual void warn (const char *format, va_list args) ATTRIBUTE_PRINTF (2, 0)
394 = 0;
395};
396
397typedef warning_hook_handler_type *warning_hook_handler;
49346fa7
AVK
398
399/* Set the thread-local warning hook, and restore the old value when
400 finished. */
401class scoped_restore_warning_hook
402{
403public:
404 explicit scoped_restore_warning_hook (warning_hook_handler new_handler);
405
6fb99666
CW
406 ~scoped_restore_warning_hook ();
407
49346fa7 408private:
6fb99666
CW
409 scoped_restore_warning_hook (const scoped_restore_warning_hook &other)
410 = delete;
411 scoped_restore_warning_hook &operator= (const scoped_restore_warning_hook &)
412 = delete;
413
414 warning_hook_handler m_save;
49346fa7
AVK
415};
416
417/* Return the current warning handler. */
418extern warning_hook_handler get_warning_hook_handler ();
419
34f997c8
AB
420/* In some cases GDB needs to try several different solutions to a problem,
421 if any of the solutions work then as far as the user is concerned the
422 problem is solved, and GDB should continue without warnings. However,
423 if none of the solutions work then GDB should emit any warnings that
424 occurred while trying each possible solution.
425
426 One example of this is locating separate debug info. There are several
427 different approaches for this; following the .gnu_debuglink, a build-id
428 based lookup, or using debuginfod. If any works, and debug info is
429 located, then the user doesn't want to see warnings from the earlier
430 approaches that were tried and failed.
431
432 However, GDB should emit all the warnings using separate calls to
433 warning -- this ensures that each warning is formatted on its own line,
434 and that any styling is emitted correctly.
435
436 This class helps with deferring warnings. Warnings can be added to an
437 instance of this class with the 'warn' function, and all warnings can be
438 emitted with a single call to 'emit'. */
439
75670e00 440struct deferred_warnings final : public warning_hook_handler_type
34f997c8 441{
8e279fda
TT
442 deferred_warnings ()
443 : m_can_style (gdb_stderr->can_emit_style_escape ())
444 {
445 }
446
34f997c8 447 /* Add a warning to the list of deferred warnings. */
75670e00 448 void warn (const char *format, ...) ATTRIBUTE_PRINTF (2, 3)
34f997c8 449 {
34f997c8
AB
450 va_list args;
451 va_start (args, format);
75670e00 452 this->warn (format, args);
34f997c8 453 va_end (args);
34f997c8
AB
454 }
455
818ef5f4
TT
456 /* A variant of 'warn' so that this object can be used as a warning
457 hook; see scoped_restore_warning_hook. Note that no locking is
458 done, so users have to be careful to only install this into a
459 single thread at a time. */
75670e00
TT
460 void warn (const char *format, va_list args) override
461 ATTRIBUTE_PRINTF (2, 0)
818ef5f4
TT
462 {
463 string_file msg (m_can_style);
464 msg.vprintf (format, args);
465 m_warnings.emplace_back (std::move (msg));
466 }
467
34f997c8
AB
468 /* Emit all warnings. */
469 void emit () const
470 {
471 for (const auto &w : m_warnings)
472 warning ("%s", w.c_str ());
473 }
474
475private:
476
8e279fda
TT
477 /* True if gdb_stderr supports styling at the moment this object is
478 constructed. This is done just once so that objects of this type
479 can be used off the main thread. */
480 bool m_can_style;
481
34f997c8
AB
482 /* The list of all deferred warnings. */
483 std::vector<string_file> m_warnings;
484};
485
cc709640 486#endif /* GDB_UTILS_H */