]>
Commit | Line | Data |
---|---|---|
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 |
33 | struct completion_match_for_lcd; |
34 | class compiled_regex; | |
35 | ||
48faced0 DE |
36 | /* String utilities. */ |
37 | ||
491144b5 | 38 | extern bool sevenbit_strings; |
48faced0 | 39 | |
b5ec771e PA |
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 | |
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). */ | |
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); | |
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 |
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 | |
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 | 94 | extern int strcmp_iw (const char *string1, const char *string2); |
48faced0 DE |
95 | |
96 | extern int strcmp_iw_ordered (const char *, const char *); | |
97 | ||
459a2e4c TT |
98 | /* Return true if the strings are equal. */ |
99 | ||
100 | extern bool streq (const char *, const char *); | |
101 | ||
a121b7c1 | 102 | extern int subset_compare (const char *, const char *); |
48faced0 | 103 | |
47e77640 PA |
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 | ||
bd712aed DE |
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. */ | |
dcb07cfa | 115 | std::chrono::steady_clock::duration get_prompt_for_continue_wait_time (); |
48faced0 | 116 | \f |
30baf67b | 117 | /* Parsing utilities. */ |
48faced0 | 118 | |
c0939df1 | 119 | extern int parse_pid_to_attach (const char *args); |
48faced0 | 120 | |
d7561cbb | 121 | extern int parse_escape (struct gdbarch *, const char **); |
48faced0 | 122 | |
48faced0 DE |
123 | \f |
124 | /* Cleanup utilities. */ | |
125 | ||
48faced0 DE |
126 | extern void init_page_info (void); |
127 | ||
b95de2b7 TT |
128 | /* Temporarily set BATCH_FLAG and the associated unlimited terminal size. |
129 | Restore when destroyed. */ | |
130 | ||
131 | struct set_batch_flag_and_restore_page_info | |
132 | { | |
133 | public: | |
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 | ||
140 | private: | |
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 |
153 | extern int gdb_filename_fnmatch (const char *pattern, const char *string, |
154 | int flags); | |
155 | ||
156 | extern void substitute_path_component (char **stringp, const char *from, | |
157 | const char *to); | |
158 | ||
d721ba37 | 159 | std::string ldirname (const char *filename); |
cce0e923 DE |
160 | |
161 | extern int count_path_elements (const char *path); | |
162 | ||
163 | extern const char *strip_leading_path_elements (const char *path, int n); | |
48faced0 DE |
164 | \f |
165 | /* GDB output, ui_file utilities. */ | |
166 | ||
167 | struct ui_file; | |
168 | ||
48faced0 DE |
169 | extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2); |
170 | extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2); | |
171 | extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2); | |
172 | ||
173 | extern void begin_line (void); | |
174 | ||
d2c0eef4 | 175 | extern void wrap_here (const char *); |
48faced0 DE |
176 | |
177 | extern void reinitialize_more_filter (void); | |
178 | ||
2f228731 TT |
179 | /* Return the number of characters in a line. */ |
180 | ||
181 | extern int get_chars_per_line (); | |
182 | ||
491144b5 | 183 | extern bool pagination_enabled; |
74da6f00 | 184 | |
79aa2fe8 PA |
185 | extern struct ui_file **current_ui_gdb_stdout_ptr (void); |
186 | extern struct ui_file **current_ui_gdb_stdin_ptr (void); | |
187 | extern struct ui_file **current_ui_gdb_stderr_ptr (void); | |
188 | extern 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. */ | |
194 | extern 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). */ | |
216 | extern struct ui_file *gdb_stdtarg; | |
217 | extern struct ui_file *gdb_stdtargerr; | |
218 | extern struct ui_file *gdb_stdtargin; | |
219 | ||
d6e5e7f7 PP |
220 | /* Set the screen dimensions to WIDTH and HEIGHT. */ |
221 | ||
222 | extern 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 | |
230 | extern void fputs_filtered (const char *, struct ui_file *); | |
231 | ||
232 | extern void fputs_unfiltered (const char *, struct ui_file *); | |
233 | ||
234 | extern int fputc_filtered (int c, struct ui_file *); | |
235 | ||
236 | extern int fputc_unfiltered (int c, struct ui_file *); | |
237 | ||
238 | extern int putchar_filtered (int c); | |
239 | ||
240 | extern int putchar_unfiltered (int c); | |
241 | ||
242 | extern void puts_filtered (const char *); | |
243 | ||
244 | extern void puts_unfiltered (const char *); | |
245 | ||
246 | extern void puts_filtered_tabular (char *string, int width, int right); | |
247 | ||
48faced0 DE |
248 | extern void vprintf_filtered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0); |
249 | ||
250 | extern void vfprintf_filtered (struct ui_file *, const char *, va_list) | |
251 | ATTRIBUTE_PRINTF (2, 0); | |
252 | ||
253 | extern void fprintf_filtered (struct ui_file *, const char *, ...) | |
254 | ATTRIBUTE_PRINTF (2, 3); | |
255 | ||
48faced0 DE |
256 | extern void printf_filtered (const char *, ...) ATTRIBUTE_PRINTF (1, 2); |
257 | ||
48faced0 DE |
258 | extern void vprintf_unfiltered (const char *, va_list) ATTRIBUTE_PRINTF (1, 0); |
259 | ||
260 | extern void vfprintf_unfiltered (struct ui_file *, const char *, va_list) | |
261 | ATTRIBUTE_PRINTF (2, 0); | |
262 | ||
263 | extern void fprintf_unfiltered (struct ui_file *, const char *, ...) | |
264 | ATTRIBUTE_PRINTF (2, 3); | |
265 | ||
266 | extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2); | |
267 | ||
48faced0 DE |
268 | extern void print_spaces_filtered (int, struct ui_file *); |
269 | ||
dde238e0 | 270 | extern const char *n_spaces (int); |
48faced0 | 271 | |
2437fd32 GB |
272 | /* Return nonzero if filtered printing is initialized. */ |
273 | extern int filtered_printing_initialized (void); | |
274 | ||
cbe56571 TT |
275 | /* Like fprintf_filtered, but styles the output according to STYLE, |
276 | when appropriate. */ | |
277 | ||
278 | extern 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 |
284 | extern 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. */ | |
292 | extern 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 | ||
301 | extern 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 | ||
307 | extern 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 | ||
314 | extern 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 | ||
319 | extern void reset_terminal_style (struct ui_file *stream); | |
320 | ||
a738ea1d YQ |
321 | /* Return the address only having significant bits. */ |
322 | extern 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. */ | |
326 | extern 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 | ||
331 | extern const char *print_core_address (struct gdbarch *gdbarch, | |
332 | CORE_ADDR address); | |
333 | ||
48faced0 DE |
334 | extern CORE_ADDR string_to_core_addr (const char *my_string); |
335 | ||
48faced0 DE |
336 | extern void fprintf_symbol_filtered (struct ui_file *, const char *, |
337 | enum language, int); | |
338 | ||
598d3636 JK |
339 | extern void throw_perror_with_name (enum errors errcode, const char *string) |
340 | ATTRIBUTE_NORETURN; | |
48faced0 | 341 | |
7c647d61 JB |
342 | extern void perror_warning_with_name (const char *string); |
343 | ||
48faced0 DE |
344 | extern void print_sys_errmsg (const char *, int); |
345 | \f | |
346 | /* Warnings and error messages. */ | |
347 | ||
348 | extern void (*deprecated_error_begin_hook) (void); | |
349 | ||
48faced0 DE |
350 | /* Message to be printed before the warning message, when a warning occurs. */ |
351 | ||
69bbf465 | 352 | extern const char *warning_pre_print; |
48faced0 | 353 | |
d7e74731 | 354 | extern void error_stream (const string_file &) ATTRIBUTE_NORETURN; |
48faced0 | 355 | |
57fcfb1b GB |
356 | extern void demangler_vwarning (const char *file, int line, |
357 | const char *, va_list ap) | |
358 | ATTRIBUTE_PRINTF (3, 0); | |
359 | ||
360 | extern 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 |
367 | extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout); | |
368 | #endif | |
369 | ||
48faced0 DE |
370 | extern 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. */ | |
377 | extern ULONGEST uinteger_pow (ULONGEST v1, LONGEST v2); | |
378 | ||
eae7090b GB |
379 | /* Resource limits used by getrlimit and setrlimit. */ |
380 | ||
381 | enum 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 | ||
392 | extern int can_dump_core (enum resource_limit_kind limit_kind); | |
393 | ||
394 | /* Print a warning that we cannot dump core. */ | |
395 | ||
396 | extern 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 | ||
401 | extern 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 | ||
407 | extern 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 | |
416 | static inline unsigned int | |
4cbd39b2 | 417 | fast_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 */ |