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