]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/utils.h
gdb/python: improve the auto help text for gdb.Parameter
[thirdparty/binutils-gdb.git] / gdb / utils.h
CommitLineData
48faced0
DE
1/* *INDENT-OFF* */ /* ATTRIBUTE_PRINTF confuses indent, avoid running it
2 for now. */
3/* I/O, string, cleanup, and other random utilities for GDB.
4a94e368 4 Copyright (C) 1986-2022 Free Software Foundation, Inc.
48faced0
DE
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
af880d85 24#include "exceptions.h"
d369b608 25#include "gdbsupport/array-view.h"
268a13a5 26#include "gdbsupport/scoped_restore.h"
dcb07cfa 27#include <chrono>
48faced0 28
3a494279
CB
29#ifdef HAVE_LIBXXHASH
30#include <xxhash.h>
31#endif
32
0d12e84c
TT
33struct completion_match_for_lcd;
34class compiled_regex;
35
48faced0
DE
36/* String utilities. */
37
491144b5 38extern bool sevenbit_strings;
48faced0 39
b5ec771e
PA
40/* Modes of operation for strncmp_iw_with_mode. */
41
42enum 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
0662b6a7 57 function and defer part of the work to strncmp_iw_with_mode.
bd69330d 58
0662b6a7
PA
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
bd69330d
PA
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). */
67extern 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);
b5ec771e 71
1d550c82
PA
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
0662b6a7
PA
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. */
1d550c82
PA
79extern 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
0662b6a7
PA
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. */
1d550c82 94extern int strcmp_iw (const char *string1, const char *string2);
48faced0
DE
95
96extern int strcmp_iw_ordered (const char *, const char *);
97
459a2e4c
TT
98/* Return true if the strings are equal. */
99
100extern bool streq (const char *, const char *);
101
a121b7c1 102extern int subset_compare (const char *, const char *);
48faced0 103
47e77640
PA
104/* Compare C strings for std::sort. */
105
106static inline bool
107compare_cstrings (const char *str1, const char *str2)
108{
109 return strcmp (str1, str2) < 0;
110}
111
bd712aed
DE
112/* Reset the prompt_for_continue clock. */
113void reset_prompt_for_continue_wait_time (void);
114/* Return the time spent in prompt_for_continue. */
dcb07cfa 115std::chrono::steady_clock::duration get_prompt_for_continue_wait_time ();
48faced0 116\f
30baf67b 117/* Parsing utilities. */
48faced0 118
c0939df1 119extern int parse_pid_to_attach (const char *args);
48faced0 120
d7561cbb 121extern int parse_escape (struct gdbarch *, const char **);
48faced0 122
48faced0
DE
123\f
124/* Cleanup utilities. */
125
48faced0
DE
126extern void init_page_info (void);
127
b95de2b7
TT
128/* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
129 Restore when destroyed. */
130
131struct set_batch_flag_and_restore_page_info
132{
133public:
134
135 set_batch_flag_and_restore_page_info ();
136 ~set_batch_flag_and_restore_page_info ();
137
138 DISABLE_COPY_AND_ASSIGN (set_batch_flag_and_restore_page_info);
139
140private:
141
142 /* Note that this doesn't use scoped_restore, because it's important
143 to control the ordering of operations in the destruction, and it
144 was simpler to avoid introducing a new ad hoc class. */
145 unsigned m_save_lines_per_page;
146 unsigned m_save_chars_per_line;
147 int m_save_batch_flag;
148};
48faced0 149
48faced0
DE
150\f
151/* Path utilities. */
152
48faced0
DE
153extern int gdb_filename_fnmatch (const char *pattern, const char *string,
154 int flags);
155
156extern void substitute_path_component (char **stringp, const char *from,
157 const char *to);
158
d721ba37 159std::string ldirname (const char *filename);
cce0e923
DE
160
161extern int count_path_elements (const char *path);
162
163extern const char *strip_leading_path_elements (const char *path, int n);
48faced0
DE
164\f
165/* GDB output, ui_file utilities. */
166
167struct ui_file;
168
48faced0
DE
169extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
170extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
171extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
172
173extern void begin_line (void);
174
d2c0eef4 175extern void wrap_here (const char *);
48faced0
DE
176
177extern void reinitialize_more_filter (void);
178
2f228731
TT
179/* Return the number of characters in a line. */
180
181extern int get_chars_per_line ();
182
491144b5 183extern bool pagination_enabled;
74da6f00 184
79aa2fe8
PA
185extern struct ui_file **current_ui_gdb_stdout_ptr (void);
186extern struct ui_file **current_ui_gdb_stdin_ptr (void);
187extern struct ui_file **current_ui_gdb_stderr_ptr (void);
188extern struct ui_file **current_ui_gdb_stdlog_ptr (void);
189
faa17681
IB
190/* Flush STREAM. This is a wrapper for ui_file_flush that also
191 flushes any output pending from uses of the *_filtered output
192 functions; that output is kept in a special buffer so that
193 pagination and styling are handled properly. */
194extern void gdb_flush (struct ui_file *);
195
79aa2fe8
PA
196/* The current top level's ui_file streams. */
197
48faced0 198/* Normal results */
79aa2fe8 199#define gdb_stdout (*current_ui_gdb_stdout_ptr ())
48faced0 200/* Input stream */
79aa2fe8 201#define gdb_stdin (*current_ui_gdb_stdin_ptr ())
48faced0 202/* Serious error notifications */
79aa2fe8 203#define gdb_stderr (*current_ui_gdb_stderr_ptr ())
48faced0
DE
204/* Log/debug/trace messages that should bypass normal stdout/stderr
205 filtering. For moment, always call this stream using
206 *_unfiltered. In the very near future that restriction shall be
207 removed - either call shall be unfiltered. (cagney 1999-06-13). */
79aa2fe8
PA
208#define gdb_stdlog (*current_ui_gdb_stdlog_ptr ())
209
210/* Truly global ui_file streams. These are all defined in main.c. */
211
48faced0
DE
212/* Target output that should bypass normal stdout/stderr filtering.
213 For moment, always call this stream using *_unfiltered. In the
214 very near future that restriction shall be removed - either call
215 shall be unfiltered. (cagney 1999-07-02). */
216extern struct ui_file *gdb_stdtarg;
217extern struct ui_file *gdb_stdtargerr;
218extern struct ui_file *gdb_stdtargin;
219
d6e5e7f7
PP
220/* Set the screen dimensions to WIDTH and HEIGHT. */
221
222extern void set_screen_width_and_height (int width, int height);
223
48faced0 224/* More generic printf like operations. Filtered versions may return
2a3c1174
PA
225 non-locally on error. As an extension over plain printf, these
226 support some GDB-specific format specifiers. Particularly useful
227 here are the styling formatters: '%p[', '%p]' and '%ps'. See
228 ui_out::message for details. */
48faced0
DE
229
230extern void fputs_filtered (const char *, struct ui_file *);
231
232extern void fputs_unfiltered (const char *, struct ui_file *);
233
234extern int fputc_filtered (int c, struct ui_file *);
235
236extern int fputc_unfiltered (int c, struct ui_file *);
237
238extern int putchar_filtered (int c);
239
240extern int putchar_unfiltered (int c);
241
242extern void puts_filtered (const char *);
243
244extern void puts_unfiltered (const char *);
245
246extern void puts_filtered_tabular (char *string, int width, int right);
247
48faced0
DE
248extern void vprintf_filtered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
249
250extern void vfprintf_filtered (struct ui_file *, const char *, va_list)
251 ATTRIBUTE_PRINTF (2, 0);
252
253extern void fprintf_filtered (struct ui_file *, const char *, ...)
254 ATTRIBUTE_PRINTF (2, 3);
255
48faced0
DE
256extern void printf_filtered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
257
48faced0
DE
258extern void vprintf_unfiltered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
259
260extern void vfprintf_unfiltered (struct ui_file *, const char *, va_list)
261 ATTRIBUTE_PRINTF (2, 0);
262
263extern void fprintf_unfiltered (struct ui_file *, const char *, ...)
264 ATTRIBUTE_PRINTF (2, 3);
265
266extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
267
48faced0
DE
268extern void print_spaces_filtered (int, struct ui_file *);
269
dde238e0 270extern const char *n_spaces (int);
48faced0 271
2437fd32
GB
272/* Return nonzero if filtered printing is initialized. */
273extern int filtered_printing_initialized (void);
274
cbe56571
TT
275/* Like fprintf_filtered, but styles the output according to STYLE,
276 when appropriate. */
277
278extern void fprintf_styled (struct ui_file *stream,
279 const ui_file_style &style,
280 const char *fmt,
281 ...)
282 ATTRIBUTE_PRINTF (3, 4);
283
2a3c1174
PA
284extern void vfprintf_styled (struct ui_file *stream,
285 const ui_file_style &style,
286 const char *fmt,
287 va_list args)
288 ATTRIBUTE_PRINTF (3, 0);
289
290/* Like vfprintf_styled, but do not process gdb-specific format
291 specifiers. */
292extern void vfprintf_styled_no_gdbfmt (struct ui_file *stream,
293 const ui_file_style &style,
294 bool filter,
295 const char *fmt, va_list args)
296 ATTRIBUTE_PRINTF (4, 0);
297
cbe56571
TT
298/* Like fputs_filtered, but styles the output according to STYLE, when
299 appropriate. */
300
301extern void fputs_styled (const char *linebuffer,
302 const ui_file_style &style,
303 struct ui_file *stream);
304
2a3c1174
PA
305/* Unfiltered variant of fputs_styled. */
306
307extern void fputs_styled_unfiltered (const char *linebuffer,
308 const ui_file_style &style,
309 struct ui_file *stream);
310
9303eb2f
PW
311/* Like fputs_styled, but uses highlight_style to highlight the
312 parts of STR that match HIGHLIGHT. */
313
314extern void fputs_highlighted (const char *str, const compiled_regex &highlight,
315 struct ui_file *stream);
316
ef1dfa36
TT
317/* Reset the terminal style to the default, if needed. */
318
319extern void reset_terminal_style (struct ui_file *stream);
320
a738ea1d
YQ
321/* Return the address only having significant bits. */
322extern CORE_ADDR address_significant (gdbarch *gdbarch, CORE_ADDR addr);
323
48faced0
DE
324/* Convert CORE_ADDR to string in platform-specific manner.
325 This is usually formatted similar to 0x%lx. */
326extern const char *paddress (struct gdbarch *gdbarch, CORE_ADDR addr);
327
328/* Return a string representation in hexadecimal notation of ADDRESS,
329 which is suitable for printing. */
330
331extern const char *print_core_address (struct gdbarch *gdbarch,
332 CORE_ADDR address);
333
48faced0
DE
334extern CORE_ADDR string_to_core_addr (const char *my_string);
335
48faced0
DE
336extern void fprintf_symbol_filtered (struct ui_file *, const char *,
337 enum language, int);
338
598d3636
JK
339extern void throw_perror_with_name (enum errors errcode, const char *string)
340 ATTRIBUTE_NORETURN;
48faced0 341
7c647d61
JB
342extern void perror_warning_with_name (const char *string);
343
48faced0
DE
344extern void print_sys_errmsg (const char *, int);
345\f
346/* Warnings and error messages. */
347
348extern void (*deprecated_error_begin_hook) (void);
349
48faced0
DE
350/* Message to be printed before the warning message, when a warning occurs. */
351
69bbf465 352extern const char *warning_pre_print;
48faced0 353
d7e74731 354extern void error_stream (const string_file &) ATTRIBUTE_NORETURN;
48faced0 355
57fcfb1b
GB
356extern void demangler_vwarning (const char *file, int line,
357 const char *, va_list ap)
358 ATTRIBUTE_PRINTF (3, 0);
359
360extern void demangler_warning (const char *file, int line,
361 const char *, ...) ATTRIBUTE_PRINTF (3, 4);
362
48faced0
DE
363\f
364/* Misc. utilities. */
365
48faced0
DE
366#ifdef HAVE_WAITPID
367extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout);
368#endif
369
48faced0
DE
370extern int myread (int, char *, int);
371
e55c6530
JB
372/* Integer exponentiation: Return V1**V2, where both arguments
373 are integers.
374
375 Requires V1 != 0 if V2 < 0.
376 Returns 1 for 0 ** 0. */
377extern ULONGEST uinteger_pow (ULONGEST v1, LONGEST v2);
378
eae7090b
GB
379/* Resource limits used by getrlimit and setrlimit. */
380
381enum resource_limit_kind
382 {
383 LIMIT_CUR,
384 LIMIT_MAX
385 };
386
387/* Check whether GDB will be able to dump core using the dump_core
388 function. Returns zero if GDB cannot or should not dump core.
389 If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
390 If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
391
392extern int can_dump_core (enum resource_limit_kind limit_kind);
393
394/* Print a warning that we cannot dump core. */
395
396extern void warn_cant_dump_core (const char *reason);
397
398/* Dump core trying to increase the core soft limit to hard limit
399 first. */
400
401extern void dump_core (void);
402
a99bc3d2
JB
403/* Copy NBITS bits from SOURCE to DEST starting at the given bit
404 offsets. Use the bit order as specified by BITS_BIG_ENDIAN.
405 Source and destination buffers must not overlap. */
406
407extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
408 const gdb_byte *source, ULONGEST source_offset,
409 ULONGEST nbits, int bits_big_endian);
410
4cbd39b2 411/* A fast hashing function. This can be used to hash data in a fast way
1a6ff1a9 412 when the length is known. If no fast hashing library is available, falls
4cbd39b2
CB
413 back to iterative_hash from libiberty. START_VALUE can be set to
414 continue hashing from a previous value. */
1a6ff1a9
CB
415
416static inline unsigned int
4cbd39b2 417fast_hash (const void *ptr, size_t len, unsigned int start_value = 0)
1a6ff1a9 418{
ccb1ba62 419#ifdef HAVE_LIBXXHASH
4cbd39b2 420 return XXH64 (ptr, len, start_value);
ccb1ba62 421#else
4cbd39b2 422 return iterative_hash (ptr, len, start_value);
ccb1ba62 423#endif
1a6ff1a9
CB
424}
425
48faced0 426#endif /* UTILS_H */