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